qapi-event: Convert to QAPISchemaVisitor, fixing data with base

Fixes events whose data is struct with base to include the struct's
base members.  Test case is qapi-schema-test.json's event
__org.qemu_x-command:

    { 'event': '__ORG.QEMU_X-EVENT', 'data': '__org.qemu_x-Struct' }

    { 'struct': '__org.qemu_x-Struct', 'base': '__org.qemu_x-Base',
      'data': { '__org.qemu_x-member2': 'str' } }

    { 'struct': '__org.qemu_x-Base',
      'data': { '__org.qemu_x-member1': '__org.qemu_x-Enum' } }

Patch's effect on generated qapi_event_send___org_qemu_x_event():

    -void qapi_event_send___org_qemu_x_event(const char *__org_qemu_x_member2,
    +void qapi_event_send___org_qemu_x_event(__org_qemu_x_Enum __org_qemu_x_member1,
    +                                        const char *__org_qemu_x_member2,
                                             Error **errp)
     {
         QDict *qmp;
    @@ -224,6 +225,10 @@ void qapi_event_send___org_qemu_x_event(
             goto clean;
         }

    +    visit_type___org_qemu_x_Enum(v, &__org_qemu_x_member1, "__org.qemu_x-member1", &local_err);
    +    if (local_err) {
    +        goto clean;
    +    }
         visit_type_str(v, (char **)&__org_qemu_x_member2, "__org.qemu_x-member2", &local_err);
         if (local_err) {
             goto clean;

Code is generated in a different order now, but that doesn't matter.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Markus Armbruster 2015-09-16 13:06:14 +02:00
parent 7b24626cd0
commit 05f43a9608
2 changed files with 47 additions and 45 deletions

View File

@ -2,14 +2,15 @@
# QAPI event generator # QAPI event generator
# #
# Copyright (c) 2014 Wenchao Xia # Copyright (c) 2014 Wenchao Xia
# Copyright (c) 2015 Red Hat Inc.
# #
# Authors: # Authors:
# Wenchao Xia <wenchaoqemu@gmail.com> # Wenchao Xia <wenchaoqemu@gmail.com>
# Markus Armbruster <armbru@redhat.com>
# #
# This work is licensed under the terms of the GNU GPL, version 2. # This work is licensed under the terms of the GNU GPL, version 2.
# See the COPYING file in the top-level directory. # See the COPYING file in the top-level directory.
from ordereddict import OrderedDict
from qapi import * from qapi import *
def _generate_event_api_name(event_name, params): def _generate_event_api_name(event_name, params):
@ -17,13 +18,13 @@ def _generate_event_api_name(event_name, params):
l = len(api_name) l = len(api_name)
if params: if params:
for argname, argentry, optional in parse_args(params): for m in params.members:
if optional: if m.optional:
api_name += "bool has_%s,\n" % c_name(argname) api_name += "bool has_%s,\n" % c_name(m.name)
api_name += "".ljust(l) api_name += "".ljust(l)
api_name += "%s %s,\n" % (c_type(argentry, is_param=True), api_name += "%s %s,\n" % (m.type.c_type(is_param=True),
c_name(argname)) c_name(m.name))
api_name += "".ljust(l) api_name += "".ljust(l)
api_name += "Error **errp)" api_name += "Error **errp)"
@ -51,7 +52,7 @@ def generate_event_implement(api_name, event_name, params):
""", """,
api_name = api_name) api_name = api_name)
if params: if params and params.members:
ret += mcgen(""" ret += mcgen("""
QmpOutputVisitor *qov; QmpOutputVisitor *qov;
Visitor *v; Visitor *v;
@ -72,7 +73,7 @@ def generate_event_implement(api_name, event_name, params):
event_name = event_name) event_name = event_name)
# step 3: visit the params if params != None # step 3: visit the params if params != None
if params: if params and params.members:
ret += mcgen(""" ret += mcgen("""
qov = qmp_output_visitor_new(); qov = qmp_output_visitor_new();
g_assert(qov); g_assert(qov);
@ -89,15 +90,15 @@ def generate_event_implement(api_name, event_name, params):
""", """,
event_name = event_name) event_name = event_name)
for argname, argentry, optional in parse_args(params): for memb in params.members:
if optional: if memb.optional:
ret += mcgen(""" ret += mcgen("""
if (has_%(var)s) { if (has_%(var)s) {
""", """,
var = c_name(argname)) var=c_name(memb.name))
push_indent() push_indent()
if argentry == "str": if memb.type.name == "str":
var_type = "(char **)" var_type = "(char **)"
else: else:
var_type = "" var_type = ""
@ -109,11 +110,11 @@ def generate_event_implement(api_name, event_name, params):
} }
""", """,
var_type = var_type, var_type = var_type,
var = c_name(argname), var=c_name(memb.name),
type = type_name(argentry), type=memb.type.c_name(),
name = argname) name=memb.name)
if optional: if memb.optional:
pop_indent() pop_indent()
ret += mcgen(""" ret += mcgen("""
} }
@ -140,7 +141,7 @@ def generate_event_implement(api_name, event_name, params):
event_enum_value = c_enum_const(event_enum_name, event_name)) event_enum_value = c_enum_const(event_enum_name, event_name))
# step 5: clean up # step 5: clean up
if params: if params and params.members:
ret += mcgen(""" ret += mcgen("""
clean: clean:
qmp_output_visitor_cleanup(qov); qmp_output_visitor_cleanup(qov);
@ -153,6 +154,30 @@ def generate_event_implement(api_name, event_name, params):
return ret return ret
class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
def __init__(self):
self.decl = None
self.defn = None
self._event_names = None
def visit_begin(self, schema):
self.decl = ''
self.defn = ''
self._event_names = []
def visit_end(self):
self.decl += generate_enum(event_enum_name, self._event_names)
self.defn += generate_enum_lookup(event_enum_name, self._event_names)
self._event_names = None
def visit_event(self, name, info, arg_type):
api_name = _generate_event_api_name(name, arg_type)
self.decl += generate_event_declaration(api_name)
self.defn += generate_event_implement(api_name, name, arg_type)
self._event_names.append(name)
(input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line() (input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line()
c_comment = ''' c_comment = '''
@ -206,32 +231,12 @@ fdecl.write(mcgen('''
''', ''',
prefix=prefix)) prefix=prefix))
exprs = QAPISchema(input_file).get_exprs()
event_enum_name = c_name(prefix + "QAPIEvent", protect=False) event_enum_name = c_name(prefix + "QAPIEvent", protect=False)
event_names = []
for expr in exprs: schema = QAPISchema(input_file)
if expr.has_key('event'): gen = QAPISchemaGenEventVisitor()
event_name = expr['event'] schema.visit(gen)
params = expr.get('data') fdef.write(gen.defn)
if params and len(params) == 0: fdecl.write(gen.decl)
params = None
api_name = _generate_event_api_name(event_name, params)
ret = generate_event_declaration(api_name)
fdecl.write(ret)
# We need an enum value per event
ret = generate_event_implement(api_name, event_name, params)
fdef.write(ret)
# Record it, and generate enum later
event_names.append(event_name)
ret = generate_enum(event_enum_name, event_names)
fdecl.write(ret)
ret = generate_enum_lookup(event_enum_name, event_names)
fdef.write(ret)
close_output(fdef, fdecl) close_output(fdef, fdecl)

View File

@ -131,9 +131,6 @@
{ 'alternate': '__org.qemu_x-Alt', { 'alternate': '__org.qemu_x-Alt',
'data': { '__org.qemu_x-branch': 'str', 'b': '__org.qemu_x-Base' } } 'data': { '__org.qemu_x-branch': 'str', 'b': '__org.qemu_x-Base' } }
{ 'event': '__ORG.QEMU_X-EVENT', 'data': '__org.qemu_x-Struct' } { 'event': '__ORG.QEMU_X-EVENT', 'data': '__org.qemu_x-Struct' }
# FIXME generated qapi_event_send___org_qemu_x_event() has only a
# parameter for data's member __org_qemu_x_member2, none for its base
# __org.qemu_x-Base's member __org_qemu_x_member1
{ 'command': '__org.qemu_x-command', { 'command': '__org.qemu_x-command',
'data': { 'a': ['__org.qemu_x-Enum'], 'b': ['__org.qemu_x-Struct'], 'data': { 'a': ['__org.qemu_x-Enum'], 'b': ['__org.qemu_x-Struct'],
'c': '__org.qemu_x-Union2', 'd': '__org.qemu_x-Alt' }, 'c': '__org.qemu_x-Union2', 'd': '__org.qemu_x-Alt' },