From d7014c66df206a7e4ba3d1b237de433d592f99bf Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Sun, 25 Feb 2018 20:07:41 -0500 Subject: [PATCH] qapi: Add type.is_empty() helper In the near future, we want to lift our artificial restriction of no variants at the top level of an event, at which point the currently open-coded check for empty members will become insufficient. Factor it out into a new helper method is_empty() now, and future-proof it by checking variants, too, along with an assert that it is not used prior to the completion of .check(). Update places that were checking for (non-)empty .members to use the new helper. All of the current callers assert that there are no variants (either directly, or by qapi.py asserting that base types have no variants), so this is not a semantic change. No change to generated code. Backports commit b6167706829c6e0d3572daa2b6769594ced276f7 from qemu --- qemu/scripts/qapi-event.py | 6 +++--- qemu/scripts/qapi-types.py | 2 +- qemu/scripts/qapi.py | 4 ++++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/qemu/scripts/qapi-event.py b/qemu/scripts/qapi-event.py index afdf9015..10ad7bb1 100644 --- a/qemu/scripts/qapi-event.py +++ b/qemu/scripts/qapi-event.py @@ -68,7 +68,7 @@ def gen_event_send(name, arg_type): ''', proto=gen_event_send_proto(name, arg_type)) - if arg_type and arg_type.members: + if arg_type and not arg_type.is_empty(): assert not arg_type.variants ret += mcgen(''' QObject *obj; @@ -88,7 +88,7 @@ def gen_event_send(name, arg_type): ''', name=name) - if arg_type and arg_type.members: + if arg_type and not arg_type.is_empty(): ret += mcgen(''' v = qmp_output_visitor_new(&obj); @@ -116,7 +116,7 @@ def gen_event_send(name, arg_type): ''', c_enum=c_enum_const(event_enum_name, name)) - if arg_type and arg_type.members: + if arg_type and not arg_type.is_empty(): ret += mcgen(''' out: visit_free(v); diff --git a/qemu/scripts/qapi-types.py b/qemu/scripts/qapi-types.py index 7fba35db..4aca34e1 100644 --- a/qemu/scripts/qapi-types.py +++ b/qemu/scripts/qapi-types.py @@ -89,7 +89,7 @@ struct %(c_name)s { # potential issues with attempting to malloc space for zero-length # structs in C, and also incompatibility with C++ (where an empty # struct is size 1). - if not (base and base.members) and not members and not variants: + if (not base or base.is_empty()) and not members and not variants: ret += mcgen(''' char qapi_dummy_for_empty_struct; ''') diff --git a/qemu/scripts/qapi.py b/qemu/scripts/qapi.py index b897b560..9e700afa 100644 --- a/qemu/scripts/qapi.py +++ b/qemu/scripts/qapi.py @@ -1005,6 +1005,10 @@ class QAPISchemaObjectType(QAPISchemaType): # _def_predefineds() return self.name.startswith('q_') + def is_empty(self): + assert self.members is not None + return not self.members and not self.variants + def c_name(self): assert self.name != 'q_empty' return QAPISchemaType.c_name(self)