From f3d2380f6d01106612cc8ae8063da0abf30f395b Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Mon, 19 Feb 2018 22:00:34 -0500 Subject: [PATCH] qapi: Simplify visits of optional fields None of the visitor callbacks would set an error when testing if an optional field was present; make this part of the interface contract by eliminating the errp argument. The resulting generated code has a nice diff: |- visit_optional(v, &has_fdset_id, "fdset-id", &err); |- if (err) { |- goto out; |- } |+ visit_optional(v, &has_fdset_id, "fdset-id"); | if (has_fdset_id) { | visit_type_int(v, &fdset_id, "fdset-id", &err); | if (err) { | goto out; | } | } Backports commit 5cdc8831a795fb8452d7e34f644202fd724e122a from qemu --- msvc/unicorn/qapi-visit.c | 5 +---- qemu/include/qapi/visitor-impl.h | 5 ++--- qemu/include/qapi/visitor.h | 11 +++++++++-- qemu/qapi/qapi-visit-core.c | 5 ++--- qemu/qapi/qmp-input-visitor.c | 3 +-- qemu/qapi/string-input-visitor.c | 3 +-- qemu/scripts/qapi.py | 8 ++------ 7 files changed, 18 insertions(+), 22 deletions(-) diff --git a/msvc/unicorn/qapi-visit.c b/msvc/unicorn/qapi-visit.c index 3d15115c..a9737c60 100644 --- a/msvc/unicorn/qapi-visit.c +++ b/msvc/unicorn/qapi-visit.c @@ -62,10 +62,7 @@ static void visit_type_X86CPUFeatureWordInfo_fields(Visitor *v, X86CPUFeatureWor if (err) { goto out; } - visit_optional(v, &(*obj)->has_cpuid_input_ecx, "cpuid-input-ecx", &err); - if (err) { - goto out; - } + visit_optional(v, &(*obj)->has_cpuid_input_ecx, "cpuid-input-ecx"); if ((*obj)->has_cpuid_input_ecx) { visit_type_int(v, &(*obj)->cpuid_input_ecx, "cpuid-input-ecx", &err); if (err) { diff --git a/qemu/include/qapi/visitor-impl.h b/qemu/include/qapi/visitor-impl.h index beb9a8e3..c9604611 100644 --- a/qemu/include/qapi/visitor-impl.h +++ b/qemu/include/qapi/visitor-impl.h @@ -52,9 +52,8 @@ struct Visitor void (*type_any)(Visitor *v, QObject **obj, const char *name, Error **errp); - /* May be NULL */ - void (*optional)(Visitor *v, bool *present, const char *name, - Error **errp); + /* May be NULL; most useful for input visitors. */ + void (*optional)(Visitor *v, bool *present, const char *name); void (*type_uint8)(Visitor *v, uint8_t *obj, const char *name, Error **errp); void (*type_uint16)(Visitor *v, uint16_t *obj, const char *name, Error **errp); diff --git a/qemu/include/qapi/visitor.h b/qemu/include/qapi/visitor.h index 7e340a11..51ade9a2 100644 --- a/qemu/include/qapi/visitor.h +++ b/qemu/include/qapi/visitor.h @@ -36,8 +36,15 @@ void visit_end_implicit_struct(Visitor *v); void visit_start_list(Visitor *v, const char *name, Error **errp); GenericList *visit_next_list(Visitor *v, GenericList **list); void visit_end_list(Visitor *v); -void visit_optional(Visitor *v, bool *present, const char *name, - Error **errp); + +/** + * Check if an optional member @name of an object needs visiting. + * For input visitors, set *@present according to whether the + * corresponding visit_type_*() needs calling; for other visitors, + * leave *@present unchanged. + */ +void visit_optional(Visitor *v, bool *present, const char *name); + /** * Determine the qtype of the item @name in the current object visit. * For input visitors, set *@type to the correct qtype of a qapi diff --git a/qemu/qapi/qapi-visit-core.c b/qemu/qapi/qapi-visit-core.c index 5711107c..064f5c3e 100644 --- a/qemu/qapi/qapi-visit-core.c +++ b/qemu/qapi/qapi-visit-core.c @@ -74,11 +74,10 @@ void visit_end_union(Visitor *v, bool data_present, Error **errp) } } -void visit_optional(Visitor *v, bool *present, const char *name, - Error **errp) +void visit_optional(Visitor *v, bool *present, const char *name) { if (v->optional) { - v->optional(v, present, name, errp); + v->optional(v, present, name); } } diff --git a/qemu/qapi/qmp-input-visitor.c b/qemu/qapi/qmp-input-visitor.c index 89d96f54..3979053f 100644 --- a/qemu/qapi/qmp-input-visitor.c +++ b/qemu/qapi/qmp-input-visitor.c @@ -319,8 +319,7 @@ static void qmp_input_type_any(Visitor *v, QObject **obj, const char *name, *obj = qobj; } -static void qmp_input_optional(Visitor *v, bool *present, const char *name, - Error **errp) +static void qmp_input_optional(Visitor *v, bool *present, const char *name) { QmpInputVisitor *qiv = to_qiv(v); QObject *qobj = qmp_input_get_object(qiv, name, true); diff --git a/qemu/qapi/string-input-visitor.c b/qemu/qapi/string-input-visitor.c index 9cc4937a..38a6d973 100644 --- a/qemu/qapi/string-input-visitor.c +++ b/qemu/qapi/string-input-visitor.c @@ -297,8 +297,7 @@ static void parse_type_number(Visitor *v, double *obj, const char *name, *obj = val; } -static void parse_optional(Visitor *v, bool *present, const char *name, - Error **errp) +static void parse_optional(Visitor *v, bool *present, const char *name) { StringInputVisitor *siv = to_siv(v); diff --git a/qemu/scripts/qapi.py b/qemu/scripts/qapi.py index 520387aa..7d1e974d 100644 --- a/qemu/scripts/qapi.py +++ b/qemu/scripts/qapi.py @@ -1656,15 +1656,11 @@ def gen_visit_fields(members, prefix='', need_cast=False, skiperr=False): for memb in members: if memb.optional: ret += mcgen(''' - visit_optional(v, &%(prefix)shas_%(c_name)s, "%(name)s", %(errp)s); + visit_optional(v, &%(prefix)shas_%(c_name)s, "%(name)s"); + if (%(prefix)shas_%(c_name)s) { ''', prefix=prefix, c_name=c_name(memb.name), name=memb.name, errp=errparg) - ret += gen_err_check(skiperr=skiperr) - ret += mcgen(''' - if (%(prefix)shas_%(c_name)s) { -''', - prefix=prefix, c_name=c_name(memb.name)) push_indent() # Ugly: sometimes we need to cast away const