target: Simplify type checks for CpuModelInfo member @props

CpuModelInfo member @props is semantically a mapping from name to
value, and syntactically a JSON object on the wire.  This translates
to QDict in C.  Since the QAPI schema language lacks the means to
express 'object', we use 'any' instead.  This is QObject in C.
Commands taking a CpuModelInfo argument need to check the QObject is a
QDict.

For arm, riscv, and s390x, the code checks right before passing the
QObject to visit_start_struct().  visit_start_struct() then checks
again.

Delete the first check.

The error message for @props that are not an object changes slightly
to the the message we get for this kind of type error in other
contexts.  Minor improvement.

Additionally, error messages about members of @props now refer to
'props.prop-name' instead of just 'prop-name'.  Another minor
improvement.

Both changes are visible in tests/qtest/arm-cpu-features.c.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-ID: <20240305145919.2186971-2-armbru@redhat.com>
Acked-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com>
[Drop #include now superfluous]
This commit is contained in:
Markus Armbruster 2024-03-05 15:59:15 +01:00
parent 05ec974671
commit ef6783d3f7
4 changed files with 19 additions and 45 deletions

View File

@ -28,7 +28,6 @@
#include "qapi/qobject-input-visitor.h"
#include "qapi/qapi-commands-machine-target.h"
#include "qapi/qapi-commands-misc-target.h"
#include "qapi/qmp/qerror.h"
#include "qapi/qmp/qdict.h"
#include "qom/qom-qobject.h"
@ -104,7 +103,7 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
Error **errp)
{
CpuModelExpansionInfo *expansion_info;
const QDict *qdict_in = NULL;
const QDict *qdict_in;
QDict *qdict_out;
ObjectClass *oc;
Object *obj;
@ -151,27 +150,20 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
}
}
if (model->props) {
qdict_in = qobject_to(QDict, model->props);
if (!qdict_in) {
error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
return NULL;
}
}
obj = object_new(object_class_get_name(oc));
if (qdict_in) {
if (model->props) {
Visitor *visitor;
Error *err = NULL;
visitor = qobject_input_visitor_new(model->props);
if (!visit_start_struct(visitor, NULL, NULL, 0, errp)) {
if (!visit_start_struct(visitor, "props", NULL, 0, errp)) {
visit_free(visitor);
object_unref(obj);
return NULL;
}
qdict_in = qobject_to(QDict, model->props);
i = 0;
while ((name = cpu_model_advertised_features[i++]) != NULL) {
if (qdict_get(qdict_in, name)) {

View File

@ -28,7 +28,6 @@
#include "qapi/qapi-commands-machine-target.h"
#include "qapi/qmp/qbool.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qerror.h"
#include "qapi/qobject-input-visitor.h"
#include "qapi/visitor.h"
#include "qom/qom-qobject.h"
@ -129,18 +128,19 @@ static void riscv_obj_add_profiles_qdict(Object *obj, QDict *qdict_out)
}
static void riscv_cpuobj_validate_qdict_in(Object *obj, QObject *props,
const QDict *qdict_in,
Error **errp)
{
const QDict *qdict_in;
const QDictEntry *qe;
Visitor *visitor;
Error *local_err = NULL;
visitor = qobject_input_visitor_new(props);
if (!visit_start_struct(visitor, NULL, NULL, 0, &local_err)) {
if (!visit_start_struct(visitor, "props", NULL, 0, &local_err)) {
goto err;
}
qdict_in = qobject_to(QDict, props);
for (qe = qdict_first(qdict_in); qe; qe = qdict_next(qdict_in, qe)) {
object_property_find_err(obj, qe->key, &local_err);
if (local_err) {
@ -170,7 +170,6 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
Error **errp)
{
CpuModelExpansionInfo *expansion_info;
const QDict *qdict_in = NULL;
QDict *qdict_out;
ObjectClass *oc;
Object *obj;
@ -188,14 +187,6 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
return NULL;
}
if (model->props) {
qdict_in = qobject_to(QDict, model->props);
if (!qdict_in) {
error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
return NULL;
}
}
obj = object_new(object_class_get_name(oc));
riscv_check_if_cpu_available(RISCV_CPU(obj), &local_err);
@ -205,9 +196,8 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
return NULL;
}
if (qdict_in) {
riscv_cpuobj_validate_qdict_in(obj, model->props, qdict_in,
&local_err);
if (model->props) {
riscv_cpuobj_validate_qdict_in(obj, model->props, &local_err);
if (local_err) {
error_propagate(errp, local_err);
object_unref(obj);

View File

@ -17,7 +17,6 @@
#include "sysemu/kvm.h"
#include "qapi/error.h"
#include "qapi/visitor.h"
#include "qapi/qmp/qerror.h"
#include "qapi/qobject-input-visitor.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qapi-commands-machine-target.h"
@ -101,21 +100,13 @@ static void cpu_model_from_info(S390CPUModel *model, const CpuModelInfo *info,
Error **errp)
{
Error *err = NULL;
const QDict *qdict = NULL;
const QDict *qdict;
const QDictEntry *e;
Visitor *visitor;
ObjectClass *oc;
S390CPU *cpu;
Object *obj;
if (info->props) {
qdict = qobject_to(QDict, info->props);
if (!qdict) {
error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
return;
}
}
oc = cpu_class_by_name(TYPE_S390_CPU, info->name);
if (!oc) {
error_setg(errp, "The CPU definition \'%s\' is unknown.", info->name);
@ -135,13 +126,14 @@ static void cpu_model_from_info(S390CPUModel *model, const CpuModelInfo *info,
return;
}
if (qdict) {
if (info->props) {
visitor = qobject_input_visitor_new(info->props);
if (!visit_start_struct(visitor, NULL, NULL, 0, errp)) {
if (!visit_start_struct(visitor, "props", NULL, 0, errp)) {
visit_free(visitor);
object_unref(obj);
return;
}
qdict = qobject_to(QDict, info->props);
for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
if (!object_property_set(obj, e->key, visitor, &err)) {
break;

View File

@ -79,7 +79,7 @@ static const char *resp_get_error(QDict *resp)
g_assert(_resp); \
_error = resp_get_error(_resp); \
g_assert(_error); \
g_assert(g_str_equal(_error, expected_error)); \
g_assert_cmpstr(_error, ==, expected_error); \
qobject_unref(_resp); \
})
@ -194,8 +194,8 @@ static void assert_type_full(QTestState *qts)
g_assert(resp);
error = resp_get_error(resp);
g_assert(error);
g_assert(g_str_equal(error,
"The requested expansion type is not supported"));
g_assert_cmpstr(error, ==,
"The requested expansion type is not supported");
qobject_unref(resp);
}
@ -212,8 +212,8 @@ static void assert_bad_props(QTestState *qts, const char *cpu_type)
g_assert(resp);
error = resp_get_error(resp);
g_assert(error);
g_assert(g_str_equal(error,
"Invalid parameter type for 'props', expected: dict"));
g_assert_cmpstr(error, ==,
"Invalid parameter type for 'props', expected: object");
qobject_unref(resp);
}
@ -446,7 +446,7 @@ static void test_query_cpu_model_expansion(const void *data)
assert_bad_props(qts, "max");
assert_error(qts, "foo", "The CPU type 'foo' is not a recognized "
"ARM CPU type", NULL);
assert_error(qts, "max", "Parameter 'not-a-prop' is unexpected",
assert_error(qts, "max", "Parameter 'props.not-a-prop' is unexpected",
"{ 'not-a-prop': false }");
assert_error(qts, "host", "The CPU type 'host' requires KVM", NULL);