From dcac64711ea906e844ae60a5927e5580f7252c1e Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 14 Feb 2019 16:22:36 +0100 Subject: qapi: Clean up modular built-in code generation a bit We neglect to call .visit_module() for the special module we use for built-ins. Harmless, but clean it up anyway. The tests/qapi-schema/*.out now show the built-in module as 'module None'. Subclasses of QAPISchemaModularCVisitor need to ._add_module() this special module to enable code generation for built-ins. When this hasn't been done, QAPISchemaModularCVisitor.visit_module() does nothing for the special module. That looks like built-ins could accidentally be generated into the wrong module when a subclass neglects to call ._add_module(). Can't happen, because built-ins are all visited before any other module. But that's non-obvious. Switch off code generation explicitly. Rename QAPISchemaModularCVisitor._begin_module() to ._begin_user_module(). New QAPISchemaModularCVisitor._is_builtin_module(), for clarity. Signed-off-by: Markus Armbruster Reviewed-by: Marc-André Lureau Message-Id: <20190214152251.2073-4-armbru@redhat.com> --- scripts/qapi/commands.py | 2 +- scripts/qapi/common.py | 24 ++++++++++++++++++------ scripts/qapi/events.py | 2 +- scripts/qapi/types.py | 2 +- scripts/qapi/visit.py | 2 +- 5 files changed, 22 insertions(+), 10 deletions(-) (limited to 'scripts/qapi') diff --git a/scripts/qapi/commands.py b/scripts/qapi/commands.py index 0f3c991918..ebf488953d 100644 --- a/scripts/qapi/commands.py +++ b/scripts/qapi/commands.py @@ -242,7 +242,7 @@ class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor): self._regy = QAPIGenCCode() self._visited_ret_types = {} - def _begin_module(self, name): + def _begin_user_module(self, name): self._visited_ret_types[self._genc] = set() commands = self._module_basename('qapi-commands', name) types = self._module_basename('qapi-types', name) diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py index c89edc0cb0..0e3ec598a4 100644 --- a/scripts/qapi/common.py +++ b/scripts/qapi/common.py @@ -1868,6 +1868,7 @@ class QAPISchema(object): def visit(self, visitor): visitor.visit_begin(self) module = None + visitor.visit_module(module) for entity in self._entity_list: if visitor.visit_needed(entity): if entity.module != module: @@ -2321,9 +2322,15 @@ class QAPISchemaModularCVisitor(QAPISchemaVisitor): self._what = what self._blurb = blurb self._pydoc = pydoc + self._genc = None + self._genh = None self._module = {} self._main_module = None + @staticmethod + def _is_builtin_module(name): + return not name + def _module_basename(self, what, name): if name is None: return re.sub(r'-', '-builtin-', what) @@ -2334,7 +2341,7 @@ class QAPISchemaModularCVisitor(QAPISchemaVisitor): return basename + '-' + os.path.splitext(os.path.basename(name))[0] def _add_module(self, name, blurb): - if self._main_module is None and name is not None: + if self._main_module is None and not self._is_builtin_module(name): self._main_module = name genc = QAPIGenC(blurb, self._pydoc) genh = QAPIGenH(blurb, self._pydoc) @@ -2346,22 +2353,27 @@ class QAPISchemaModularCVisitor(QAPISchemaVisitor): def write(self, output_dir, opt_builtins=False): for name in self._module: - if name is None and not opt_builtins: + if self._is_builtin_module(name) and not opt_builtins: continue basename = self._module_basename(self._what, name) (genc, genh) = self._module[name] genc.write(output_dir, basename + '.c') genh.write(output_dir, basename + '.h') - def _begin_module(self, name): + def _begin_user_module(self, name): pass def visit_module(self, name): if name in self._module: self._set_module(name) - return - self._add_module(name, self._blurb) - self._begin_module(name) + elif self._is_builtin_module(name): + # The built-in module has not been created. No code may + # be generated. + self._genc = None + self._genh = None + else: + self._add_module(name, self._blurb) + self._begin_user_module(name) def visit_include(self, name, info): basename = self._module_basename(self._what, name) diff --git a/scripts/qapi/events.py b/scripts/qapi/events.py index d86a2d2b3e..6f39cf8196 100644 --- a/scripts/qapi/events.py +++ b/scripts/qapi/events.py @@ -142,7 +142,7 @@ class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor): self._event_enum_members = [] self._event_emit_name = c_name(prefix + 'qapi_event_emit') - def _begin_module(self, name): + def _begin_user_module(self, name): types = self._module_basename('qapi-types', name) visit = self._module_basename('qapi-visit', name) self._genc.add(mcgen(''' diff --git a/scripts/qapi/types.py b/scripts/qapi/types.py index 62d4cf9f95..9fa510f7df 100644 --- a/scripts/qapi/types.py +++ b/scripts/qapi/types.py @@ -194,7 +194,7 @@ class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): #include "qapi/util.h" ''')) - def _begin_module(self, name): + def _begin_user_module(self, name): types = self._module_basename('qapi-types', name) visit = self._module_basename('qapi-visit', name) self._genc.preamble_add(mcgen(''' diff --git a/scripts/qapi/visit.py b/scripts/qapi/visit.py index 82eab72b21..ca86009398 100644 --- a/scripts/qapi/visit.py +++ b/scripts/qapi/visit.py @@ -298,7 +298,7 @@ class QAPISchemaGenVisitVisitor(QAPISchemaModularCVisitor): ''', prefix=prefix)) - def _begin_module(self, name): + def _begin_user_module(self, name): types = self._module_basename('qapi-types', name) visit = self._module_basename('qapi-visit', name) self._genc.preamble_add(mcgen(''' -- cgit v1.2.3-55-g7522 From c2e196a9b41235a308fb6d1c516aa91ba0a807c8 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 14 Feb 2019 16:22:37 +0100 Subject: qapi: Prepare for system modules other than 'builtin' The next commit wants to generate qapi-emit-events.{c.h}. To enable that, extend QAPISchemaModularCVisitor to support additional "system modules", i.e. modules that don't correspond to a (user-defined) QAPI schema module. Signed-off-by: Markus Armbruster Reviewed-by: Marc-André Lureau Message-Id: <20190214152251.2073-5-armbru@redhat.com> --- scripts/qapi/common.py | 35 +++++++++++++++++++++++++---------- scripts/qapi/types.py | 2 +- scripts/qapi/visit.py | 2 +- 3 files changed, 27 insertions(+), 12 deletions(-) (limited to 'scripts/qapi') diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py index 0e3ec598a4..c327ae5036 100644 --- a/scripts/qapi/common.py +++ b/scripts/qapi/common.py @@ -2327,27 +2327,42 @@ class QAPISchemaModularCVisitor(QAPISchemaVisitor): self._module = {} self._main_module = None + @staticmethod + def _is_user_module(name): + return name and not name.startswith('./') + @staticmethod def _is_builtin_module(name): return not name def _module_basename(self, what, name): - if name is None: - return re.sub(r'-', '-builtin-', what) - basename = os.path.join(os.path.dirname(name), - self._prefix + what) - if name == self._main_module: - return basename - return basename + '-' + os.path.splitext(os.path.basename(name))[0] + ret = '' if self._is_builtin_module(name) else self._prefix + if self._is_user_module(name): + dirname, basename = os.path.split(name) + ret += what + if name != self._main_module: + ret += '-' + os.path.splitext(basename)[0] + ret = os.path.join(dirname, ret) + else: + name = name[2:] if name else 'builtin' + ret += re.sub(r'-', '-' + name + '-', what) + return ret def _add_module(self, name, blurb): - if self._main_module is None and not self._is_builtin_module(name): - self._main_module = name genc = QAPIGenC(blurb, self._pydoc) genh = QAPIGenH(blurb, self._pydoc) self._module[name] = (genc, genh) self._set_module(name) + def _add_user_module(self, name, blurb): + assert self._is_user_module(name) + if self._main_module is None: + self._main_module = name + self._add_module(name, blurb) + + def _add_system_module(self, name, blurb): + self._add_module(name and './' + name, blurb) + def _set_module(self, name): self._genc, self._genh = self._module[name] @@ -2372,7 +2387,7 @@ class QAPISchemaModularCVisitor(QAPISchemaVisitor): self._genc = None self._genh = None else: - self._add_module(name, self._blurb) + self._add_user_module(name, self._blurb) self._begin_user_module(name) def visit_include(self, name, info): diff --git a/scripts/qapi/types.py b/scripts/qapi/types.py index 9fa510f7df..2bd6fcd44f 100644 --- a/scripts/qapi/types.py +++ b/scripts/qapi/types.py @@ -183,7 +183,7 @@ class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): QAPISchemaModularCVisitor.__init__( self, prefix, 'qapi-types', ' * Schema-defined QAPI types', __doc__) - self._add_module(None, ' * Built-in QAPI types') + self._add_system_module(None, ' * Built-in QAPI types') self._genc.preamble_add(mcgen(''' #include "qemu/osdep.h" #include "qapi/dealloc-visitor.h" diff --git a/scripts/qapi/visit.py b/scripts/qapi/visit.py index ca86009398..826b8066e1 100644 --- a/scripts/qapi/visit.py +++ b/scripts/qapi/visit.py @@ -284,7 +284,7 @@ class QAPISchemaGenVisitVisitor(QAPISchemaModularCVisitor): QAPISchemaModularCVisitor.__init__( self, prefix, 'qapi-visit', ' * Schema-defined QAPI visitors', __doc__) - self._add_module(None, ' * Built-in QAPI visitors') + self._add_system_module(None, ' * Built-in QAPI visitors') self._genc.preamble_add(mcgen(''' #include "qemu/osdep.h" #include "qemu-common.h" -- cgit v1.2.3-55-g7522 From 5d75648b56e6e400eafaa1c2c8616ffc5ae6148e Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 14 Feb 2019 16:22:38 +0100 Subject: qapi: Generate QAPIEvent stuff into separate files Having to include qapi-events.h just for QAPIEvent is suboptimal, but quite tolerable now. It'll become problematic when we have events conditional on the target, because then qapi-events.h won't be usable from target-independent code anymore. Avoid that by generating it into separate files. Signed-off-by: Markus Armbruster Reviewed-by: Marc-André Lureau Message-Id: <20190214152251.2073-6-armbru@redhat.com> --- .gitignore | 1 + Makefile | 1 + Makefile.objs | 1 + docs/devel/qapi-code-gen.txt | 46 +++++++++++++++++++++++++++++--------------- monitor.c | 2 +- scripts/qapi/events.py | 32 +++++++++++++++++++----------- stubs/monitor.c | 2 +- tests/test-qmp-event.c | 1 + ui/vnc.c | 3 ++- 9 files changed, 60 insertions(+), 29 deletions(-) (limited to 'scripts/qapi') diff --git a/.gitignore b/.gitignore index 321095bf1a..b66b772551 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,7 @@ /qapi/qapi-builtin-visit.[ch] /qapi/qapi-commands-*.[ch] /qapi/qapi-commands.[ch] +/qapi/qapi-emit-events.[ch] /qapi/qapi-events-*.[ch] /qapi/qapi-events.[ch] /qapi/qapi-introspect.[ch] diff --git a/Makefile b/Makefile index 3658310b95..53d161b65f 100644 --- a/Makefile +++ b/Makefile @@ -101,6 +101,7 @@ GENERATED_QAPI_FILES += $(QAPI_MODULES:%=qapi/qapi-visit-%.c) GENERATED_QAPI_FILES += qapi/qapi-commands.h qapi/qapi-commands.c GENERATED_QAPI_FILES += $(QAPI_MODULES:%=qapi/qapi-commands-%.h) GENERATED_QAPI_FILES += $(QAPI_MODULES:%=qapi/qapi-commands-%.c) +GENERATED_QAPI_FILES += qapi/qapi-emit-events.h qapi/qapi-emit-events.c GENERATED_QAPI_FILES += qapi/qapi-events.h qapi/qapi-events.c GENERATED_QAPI_FILES += $(QAPI_MODULES:%=qapi/qapi-events-%.h) GENERATED_QAPI_FILES += $(QAPI_MODULES:%=qapi/qapi-events-%.c) diff --git a/Makefile.objs b/Makefile.objs index b7aae33367..ec11a0f55b 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -12,6 +12,7 @@ util-obj-y += $(QAPI_MODULES:%=qapi/qapi-types-%.o) util-obj-y += qapi/qapi-builtin-visit.o util-obj-y += qapi/qapi-visit.o util-obj-y += $(QAPI_MODULES:%=qapi/qapi-visit-%.o) +util-obj-y += qapi/qapi-emit-events.o util-obj-y += qapi/qapi-events.o util-obj-y += $(QAPI_MODULES:%=qapi/qapi-events-%.o) util-obj-y += qapi/qapi-introspect.o diff --git a/docs/devel/qapi-code-gen.txt b/docs/devel/qapi-code-gen.txt index c9ba8ddb2e..b517b0cfbf 100644 --- a/docs/devel/qapi-code-gen.txt +++ b/docs/devel/qapi-code-gen.txt @@ -1381,11 +1381,15 @@ qapi_event_send_EVENT(). The following files are created: -$(prefix)qapi-events.h - Function prototypes for each event type, plus an - enumeration of all event names +$(prefix)qapi-events.h - Function prototypes for each event type $(prefix)qapi-events.c - Implementation of functions to send an event +$(prefix)qapi-emit-events.h - Enumeration of all event names, and + common event code declarations + +$(prefix)qapi-emit-events.c - Common event code definitions + Example: $ cat qapi-generated/example-qapi-events.h @@ -1397,21 +1401,8 @@ Example: #include "qapi/util.h" #include "example-qapi-types.h" - void qapi_event_send_my_event(void); - typedef enum example_QAPIEvent { - EXAMPLE_QAPI_EVENT_MY_EVENT, - EXAMPLE_QAPI_EVENT__MAX, - } example_QAPIEvent; - - #define example_QAPIEvent_str(val) \ - qapi_enum_lookup(&example_QAPIEvent_lookup, (val)) - - extern const QEnumLookup example_QAPIEvent_lookup; - - void example_qapi_event_emit(example_QAPIEvent event, QDict *qdict); - #endif /* EXAMPLE_QAPI_EVENTS_H */ $ cat qapi-generated/example-qapi-events.c [Uninteresting stuff omitted...] @@ -1427,6 +1418,31 @@ Example: qobject_unref(qmp); } +[Uninteresting stuff omitted...] + $ cat qapi-generated/example-qapi-emit-events.h +[Uninteresting stuff omitted...] + + #ifndef EXAMPLE_QAPI_EMIT_EVENTS_H + #define EXAMPLE_QAPI_EMIT_EVENTS_H + + #include "qapi/util.h" + + typedef enum example_QAPIEvent { + EXAMPLE_QAPI_EVENT_MY_EVENT, + EXAMPLE_QAPI_EVENT__MAX, + } example_QAPIEvent; + + #define example_QAPIEvent_str(val) \ + qapi_enum_lookup(&example_QAPIEvent_lookup, (val)) + + extern const QEnumLookup example_QAPIEvent_lookup; + + void example_qapi_event_emit(example_QAPIEvent event, QDict *qdict); + + #endif /* EXAMPLE_QAPI_EMIT_EVENTS_H */ + $ cat qapi-generated/example-qapi-emit-events.c +[Uninteresting stuff omitted...] + const QEnumLookup example_QAPIEvent_lookup = { .array = (const char *const[]) { [EXAMPLE_QAPI_EVENT_MY_EVENT] = "MY_EVENT", diff --git a/monitor.c b/monitor.c index e5de5765b8..8e02a001a3 100644 --- a/monitor.c +++ b/monitor.c @@ -75,7 +75,7 @@ #include "qemu/thread.h" #include "block/qapi.h" #include "qapi/qapi-commands.h" -#include "qapi/qapi-events.h" +#include "qapi/qapi-emit-events.h" #include "qapi/error.h" #include "qapi/qmp-event.h" #include "qapi/qapi-introspect.h" diff --git a/scripts/qapi/events.py b/scripts/qapi/events.py index 6f39cf8196..28bbc3745d 100644 --- a/scripts/qapi/events.py +++ b/scripts/qapi/events.py @@ -143,12 +143,14 @@ class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor): self._event_emit_name = c_name(prefix + 'qapi_event_emit') def _begin_user_module(self, name): + events = self._module_basename('qapi-events', name) types = self._module_basename('qapi-types', name) visit = self._module_basename('qapi-visit', name) self._genc.add(mcgen(''' #include "qemu/osdep.h" #include "qemu-common.h" -#include "%(prefix)sqapi-events.h" +#include "%(prefix)sqapi-emit-events.h" +#include "%(events)s.h" #include "%(visit)s.h" #include "qapi/error.h" #include "qapi/qmp/qdict.h" @@ -156,26 +158,34 @@ class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor): #include "qapi/qmp-event.h" ''', - visit=visit, prefix=self._prefix)) + events=events, visit=visit, + prefix=self._prefix)) self._genh.add(mcgen(''' #include "qapi/util.h" #include "%(types)s.h" - ''', types=types)) def visit_end(self): - (genc, genh) = self._module[self._main_module] - genh.add(gen_enum(self._event_enum_name, - self._event_enum_members)) - genc.add(gen_enum_lookup(self._event_enum_name, - self._event_enum_members)) - genh.add(mcgen(''' + self._add_system_module('emit', ' * QAPI Events emission') + self._genc.preamble_add(mcgen(''' +#include "qemu/osdep.h" +#include "%(prefix)sqapi-emit-events.h" +''', + prefix=self._prefix)) + self._genh.preamble_add(mcgen(''' +#include "qapi/util.h" +''')) + self._genh.add(gen_enum(self._event_enum_name, + self._event_enum_members)) + self._genc.add(gen_enum_lookup(self._event_enum_name, + self._event_enum_members)) + self._genh.add(mcgen(''' void %(event_emit)s(%(event_enum)s event, QDict *qdict); ''', - event_emit=self._event_emit_name, - event_enum=self._event_enum_name)) + event_emit=self._event_emit_name, + event_enum=self._event_enum_name)) def visit_event(self, name, info, ifcond, arg_type, boxed): with ifcontext(ifcond, self._genh, self._genc): diff --git a/stubs/monitor.c b/stubs/monitor.c index 32bd7012c3..b57fe6c32f 100644 --- a/stubs/monitor.c +++ b/stubs/monitor.c @@ -1,6 +1,6 @@ #include "qemu/osdep.h" #include "qapi/error.h" -#include "qapi/qapi-events.h" +#include "qapi/qapi-emit-events.h" #include "qemu-common.h" #include "monitor/monitor.h" diff --git a/tests/test-qmp-event.c b/tests/test-qmp-event.c index bf900f14f4..eee7e08ab6 100644 --- a/tests/test-qmp-event.c +++ b/tests/test-qmp-event.c @@ -21,6 +21,7 @@ #include "qapi/qmp/qstring.h" #include "qapi/qmp-event.h" #include "test-qapi-events.h" +#include "test-qapi-emit-events.h" typedef struct TestEventData { QDict *expect; diff --git a/ui/vnc.c b/ui/vnc.c index 0fef646fc4..7e0710ed8f 100644 --- a/ui/vnc.c +++ b/ui/vnc.c @@ -35,7 +35,8 @@ #include "qemu/timer.h" #include "qemu/acl.h" #include "qemu/config-file.h" -#include "qapi/qapi-events.h" +#include "qapi/qapi-emit-events.h" +#include "qapi/qapi-events-ui.h" #include "qapi/error.h" #include "qapi/qapi-commands-ui.h" #include "ui/input.h" -- cgit v1.2.3-55-g7522 From 093e3679517f910cc99f35b2259469472c6df9b8 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 14 Feb 2019 16:22:49 +0100 Subject: Revert "qapi-events: add 'if' condition to implicit event enum" This reverts commit 7bd263490590ee6fcf34ecb6203437e22f6e5a9c. The commit applied the events' conditions to the members of enum QAPIEvent. Awkward, because it renders QAPIEvent unusable in target-independent code as soon as we make an event target-dependent. Reverting this has the following effects: * ui/vnc.c can remain target independent. * monitor_qapi_event_conf[] doesn't have to muck around with #ifdef. * query-events again doesn't reflect conditionals. I'm going to deprecate it in favor of query-qmp-schema. Another option would be to split target-dependent parts off enum QAPIEvent into a target-dependent enum. Doesn't seem worthwhile right now. Signed-off-by: Markus Armbruster Reviewed-by: Marc-André Lureau Message-Id: <20190214152251.2073-17-armbru@redhat.com> --- scripts/qapi/events.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'scripts/qapi') diff --git a/scripts/qapi/events.py b/scripts/qapi/events.py index 28bbc3745d..2067660be4 100644 --- a/scripts/qapi/events.py +++ b/scripts/qapi/events.py @@ -193,7 +193,9 @@ void %(event_emit)s(%(event_enum)s event, QDict *qdict); self._genc.add(gen_event_send(name, arg_type, boxed, self._event_enum_name, self._event_emit_name)) - self._event_enum_members.append(QAPISchemaMember(name, ifcond)) + # Note: we generate the enum member regardless of @ifcond, to + # keep the enumeration usable in target-independent code. + self._event_enum_members.append(QAPISchemaMember(name)) def gen_events(schema, output_dir, prefix): -- cgit v1.2.3-55-g7522