From 994490d19762535e029784d0bc8bbcf86b280e27 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Mon, 19 Feb 2018 22:02:43 -0500 Subject: [PATCH] qapi: Shorter visits of optional fields For less code, reflect the determined boolean value of an optional visit back to the caller instead of making the caller read the boolean after the fact. The resulting generated code has the following diff: |- visit_optional(v, &has_fdset_id, "fdset-id"); |- if (has_fdset_id) { |+ if (visit_optional(v, &has_fdset_id, "fdset-id")) { | visit_type_int(v, &fdset_id, "fdset-id", &err); | if (err) { | goto out; | } | } Backports commit 29637a6ee913df8fcdf371426ee48956b945b618 from qemu --- msvc/unicorn/qapi-visit.c | 3 +-- qemu/include/qapi/visitor.h | 4 ++-- qemu/qapi/qapi-visit-core.c | 3 ++- qemu/scripts/qapi.py | 3 +-- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/msvc/unicorn/qapi-visit.c b/msvc/unicorn/qapi-visit.c index a9737c60..bcab3b39 100644 --- a/msvc/unicorn/qapi-visit.c +++ b/msvc/unicorn/qapi-visit.c @@ -62,8 +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"); - if ((*obj)->has_cpuid_input_ecx) { + if (visit_optional(v, &(*obj)->has_cpuid_input_ecx, "cpuid-input-ecx")) { visit_type_int(v, &(*obj)->cpuid_input_ecx, "cpuid-input-ecx", &err); if (err) { goto out; diff --git a/qemu/include/qapi/visitor.h b/qemu/include/qapi/visitor.h index 51ade9a2..6b08bbee 100644 --- a/qemu/include/qapi/visitor.h +++ b/qemu/include/qapi/visitor.h @@ -41,9 +41,9 @@ void visit_end_list(Visitor *v); * 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. + * leave *@present unchanged. Return *@present for convenience. */ -void visit_optional(Visitor *v, bool *present, const char *name); +bool visit_optional(Visitor *v, bool *present, const char *name); /** * Determine the qtype of the item @name in the current object visit. diff --git a/qemu/qapi/qapi-visit-core.c b/qemu/qapi/qapi-visit-core.c index 064f5c3e..a2bd3a34 100644 --- a/qemu/qapi/qapi-visit-core.c +++ b/qemu/qapi/qapi-visit-core.c @@ -74,11 +74,12 @@ void visit_end_union(Visitor *v, bool data_present, Error **errp) } } -void visit_optional(Visitor *v, bool *present, const char *name) +bool visit_optional(Visitor *v, bool *present, const char *name) { if (v->optional) { v->optional(v, present, name); } + return *present; } void visit_get_next_type(Visitor *v, QType *type, bool promote_int, diff --git a/qemu/scripts/qapi.py b/qemu/scripts/qapi.py index 7d1e974d..72660597 100644 --- a/qemu/scripts/qapi.py +++ b/qemu/scripts/qapi.py @@ -1656,8 +1656,7 @@ 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"); - if (%(prefix)shas_%(c_name)s) { + if (visit_optional(v, &%(prefix)shas_%(c_name)s, "%(name)s")) { ''', prefix=prefix, c_name=c_name(memb.name), name=memb.name, errp=errparg)