QAPI code generator fix-ups
The following changes since commit 4e277a7c641698faf8f01599043b622f80237b42:
Anthony Liguori (1):
Add input marshaller
are available in the git repository at:
git://repo.or.cz/qemu/mdroth.git qapi-for-anthony
Michael Roth (9):
qapi: Fix type generator for structured type members
qapi: input visiter, don't always allocate memory for structs
qapi: Fix visiter generator for nested structs/qobjects
qapi: some basename/guardname py utility functions
qapi: add --prefix option to type generator
qapi: add --prefix option for visiter generator
qapi: test schema for test-visiter unit tests
qapi: Makefile, build test-visiter with generated test code
qapi: test-visiter, pull in gen code, tests for nested structures
Makefile | 15 ++++++- qapi-schema-test.json | 11 +++++ qapi/qmp-input-visiter.c | 4 +- scripts/qapi-types.py | 69 ++++++++++++++++++++++---------- scripts/qapi-visit.py | 96 +++++++++++++++++++++++++++++++++------------ scripts/qapi.py | 12 +++++- test-visiter.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 251 insertions(+), 54 deletions(-) create mode 100644 qapi-schema-test.json
PATCH 2/9 qapi: input visiter, don't always allocate memory for structs
Sometimes we have anonymous nested structs rather than references. In these cases we call visit_start_struct() primarilly to push the nested qobject onto the stack, and specify a NULL obj value to avoid doing any memory allocation. So add a simple check for this NULL value.
--- qapi/qmp-input-visiter.c | 4 +++- 1 files changed, 3 insertions(+), 1 deletions(-)
qapi/qmp-input-visiter.c +3 -1
@@ -75,7 +75,9 @@ static void qmp_input_start_struct(Visiter *v, void **obj, const char *kind, conqmp_input_push (qiv, qobj); - *obj =qemu_mallocz (QAPI_OBJECT_SIZE ); +if (obj) { + *obj =qemu_mallocz (QAPI_OBJECT_SIZE ); + } }static void qmp_input_end_struct (Visiter *v,Error **errp)
Signature · 1 line
1.7.0.4
PATCH 3/9 qapi: Fix visiter generator for nested structs/qobjects
In reply to Michael RothRecursively handle structured types as identified by the schema parser. Generated code Uses visiter stack's push/pop logic to traverse the structure's tree.
--- scripts/qapi-visit.py | 55 +++++++++++++++++++++++++++++++++--------------- 1 files changed, 38 insertions(+), 17 deletions(-)
scripts/qapi-visit.py +38 -17
@@ -2,35 +2,56 @@ from ordereddict import OrderedDict from qapi import * import sys -def generate_visit_struct(name, members): - ret = mcgen(''' - -void visit_type_%(name)s(Visiter *m, %(name)s ** obj, const char *name, Error **errp) -{ - visit_start_struct(m, (void **)obj, "%(name)s", name, errp); -''', - name=name) - - for argname, argtype, optional in parse_args(members): +def generate_visit_struct_body(field_prefix, members): + ret = "" + if len(field_prefix): + field_prefix = field_prefix + "." + for argname, argentry, optional, structured in parse_args(members): if optional: ret += mcgen(''' - visit_start_optional(m, &(*obj)->has_%(c_name)s, "%(name)s", errp); - if ((*obj)->has_%(c_name)s) { +visit_start_optional(m, &(*obj)->%(c_prefix)shas_%(c_name)s, "%(name)s", errp); +if ((*obj)->%(prefix)shas_%(c_name)s) { ''', + c_prefix=c_var(field_prefix), prefix=field_prefix, c_name=c_var(argname), name=argname) push_indent() - ret += mcgen(''' - visit_type_%(type)s(m, &(*obj)->%(c_name)s, "%(name)s", errp); + if structured: + ret += mcgen(''' +visit_start_struct(m, NULL, "", "%(name)s", errp); ''', - type=type_name(argtype), c_name=c_var(argname), name=argname) + name=argname) + ret += generate_visit_struct_body(field_prefix + argname, argentry) + ret += mcgen(''' +visit_end_struct(m, errp); +''') + else: + ret += mcgen(''' +visit_type_%(type)s(m, &(*obj)->%(c_prefix)s%(c_name)s, "%(name)s", errp); +''', + c_prefix=c_var(field_prefix), prefix=field_prefix, + type=type_name(argentry), c_name=c_var(argname), + name=argname) if optional: pop_indent() ret += mcgen(''' - } - visit_end_optional(m, errp); +} +visit_end_optional(m, errp); ''') + return ret + +def generate_visit_struct(name, members): + ret = mcgen(''' + +void visit_type_%(name)s(Visiter *m, %(name)s ** obj, const char *name, Error **errp) +{ + visit_start_struct(m, (void **)obj, "%(name)s", name, errp); +''', + name=name) + push_indent() + ret += generate_visit_struct_body("", members) + pop_indent() ret += mcgen(''' visit_end_struct(m, errp);
Signature · 1 line
1.7.0.4
PATCH 1/9 qapi: Fix type generator for structured type members
In reply to Michael RothThis generalizes the generate_struct() function to support generating structs for top-level named structs as well anonymous nested structs. generate_struct() now calls itself recursively to support arbitrary nesting by checking if a fields "type" is actually an OrderedDict.
--- scripts/qapi-types.py | 39 +++++++++++++++++++++++---------------- scripts/qapi.py | 7 +++++-- 2 files changed, 28 insertions(+), 18 deletions(-)
scripts/qapi-types.py +23 -16
@@ -4,7 +4,6 @@ import sys def generate_fwd_struct(name, members): return mcgen(''' - typedef struct %(name)s %(name)s; typedef struct %(name)sList
@@ -15,34 +14,40 @@ typedef struct %(name)sList ''', name=name) -def generate_struct(name, members): +def generate_struct(structname, fieldname, members): ret = mcgen(''' - struct %(name)s { ''', - name=name) + name=structname) - for argname, argtype, optional in parse_args(members): + for argname, argentry, optional, structured in parse_args(members): if optional: ret += mcgen(''' bool has_%(c_name)s; ''', c_name=c_var(argname)) - ret += mcgen(''' + if structured: + push_indent() + ret += generate_struct("", argname, argentry) + pop_indent() + else: + ret += mcgen(''' %(c_type)s %(c_name)s; ''', - c_type=c_type(argtype), c_name=c_var(argname)) + c_type=c_type(argentry), c_name=c_var(argname)) + if len(fieldname): + fieldname = " " + fieldname ret += mcgen(''' -}; -''') +}%(field)s; +''', + field=fieldname) return ret def generate_handle(name, typeinfo): return mcgen(''' - typedef struct %(name)s { %(c_type)s handle;
@@ -58,7 +63,6 @@ typedef struct %(name)sList def generate_enum(name, values): ret = mcgen(''' - typedef enum %(name)s { ''',
@@ -83,7 +87,6 @@ typedef enum %(name)s def generate_union(name, typeinfo): ret = mcgen(''' - struct %(name)s { %(name)sKind kind;
@@ -117,7 +120,7 @@ fdecl.write('''/* AUTOMATICALLY GENERATED, DO NOT MODIFY */ ''') for expr in exprs: - ret = '' + ret = "\n" if expr.has_key('type'): ret += generate_fwd_struct(expr['type'], expr['data']) elif expr.has_key('enum'):
@@ -125,18 +128,22 @@ for expr in exprs: ret += generate_enum(expr['enum'], expr['data']) elif expr.has_key('union'): add_enum('%sKind' % expr['union']) - ret += generate_fwd_struct(expr['union'], expr['data']) + ret += generate_fwd_struct(expr['union'], expr['data']) + "\n" ret += generate_enum('%sKind' % expr['union'], expr['data'].keys()) + else: + continue fdecl.write(ret) for expr in exprs: - ret = '' + ret = "\n" if expr.has_key('type'): - ret += generate_struct(expr['type'], expr['data']) + ret += generate_struct(expr['type'], "", expr['data']) elif expr.has_key('handle'): ret += generate_handle(expr['handle'], expr['data']) elif expr.has_key('union'): ret += generate_union(expr['union'], expr['data']) + else: + continue fdecl.write(ret) fdecl.write('''
scripts/qapi.py +5 -2
@@ -74,12 +74,15 @@ def parse_schema(fp): def parse_args(typeinfo): for member in typeinfo: argname = member - argtype = typeinfo[member] + argentry = typeinfo[member] optional = False + structured = False if member.startswith('*'): argname = member[1:] optional = True - yield (argname, argtype, optional) + if isinstance(argentry, OrderedDict): + structured = True + yield (argname, argentry, optional, structured) def de_camel_case(name): new_name = ''
Signature · 1 line
1.7.0.4
PATCH 4/9 qapi: some basename/guardname py utility functions
In reply to Michael RothSome utility functions to get basename/guardname from a filename/filepath. These will be used to allow arbitrarilly named test files to be generated by code generators for use in unit tests.
--- scripts/qapi.py | 5 +++++ 1 files changed, 5 insertions(+), 0 deletions(-)
scripts/qapi.py +5 -0
@@ -157,3 +157,8 @@ def cgen(code, **kwds): def mcgen(code, **kwds): return cgen('\n'.join(code.split('\n')[1:-1]), **kwds) +def basename(filename): + return filename.split("/")[-1] + +def guardname(filename): + return filename.replace("/", "_").replace("-", "_").split(".")[0].upper()
Signature · 1 line
1.7.0.4
PATCH 5/9 qapi: add --prefix option to type generator
In reply to Michael RothThis is mainly so we can generate a header file with the filename {prefix}qapi-types.h by passing in a test schema for use with unit tests.
--- scripts/qapi-types.py | 30 ++++++++++++++++++++++++------ 1 files changed, 24 insertions(+), 6 deletions(-)
scripts/qapi-types.py +24 -6
@@ -1,6 +1,7 @@ from ordereddict import OrderedDict from qapi import * import sys +import getopt def generate_fwd_struct(name, members): return mcgen('''
@@ -108,16 +109,33 @@ struct %(name)s return ret -fdecl = open('qapi-types.h', 'w') +try: + opts, args = getopt.gnu_getopt(sys.argv[1:], "p:", ["prefix="]) +except getopt.GetoptError, err: + print str(err) + sys.exit(1) -exprs = parse_schema(sys.stdin) +prefix = "" +h_file = 'qapi-types.h' + +for o, a in opts: + if o in ("-p", "--prefix"): + prefix = a + +h_file = prefix + h_file + +fdecl = open(h_file, 'w') -fdecl.write('''/* AUTOMATICALLY GENERATED, DO NOT MODIFY */ -#ifndef QAPI_TYPES_H -#define QAPI_TYPES_H +fdecl.write(mcgen(''' +/* AUTOMATICALLY GENERATED, DO NOT MODIFY */ +#ifndef %(guard)s +#define %(guard)s #include "qapi-types-core.h" -''') +''', + guard=guardname(h_file))) + +exprs = parse_schema(sys.stdin) for expr in exprs: ret = "\n"
Signature · 1 line
1.7.0.4
PATCH 6/9 qapi: add --prefix option for visiter generator
In reply to Michael RothGenerated code assumes the required types header was generated using the same prefix.
--- scripts/qapi-visit.py | 41 ++++++++++++++++++++++++++++++++--------- 1 files changed, 32 insertions(+), 9 deletions(-)
scripts/qapi-visit.py +32 -9
@@ -1,6 +1,7 @@ from ordereddict import OrderedDict from qapi import * import sys +import getopt def generate_visit_struct_body(field_prefix, members): ret = ""
@@ -135,21 +136,43 @@ void visit_type_%(name)s(Visiter *m, %(name)s * obj, const char *name, Error **e ''', name=name) -fdef = open('qapi-visit.c', 'w') -fdecl = open('qapi-visit.h', 'w') +try: + opts, args = getopt.gnu_getopt(sys.argv[1:], "p:", ["prefix="]) +except getopt.GetoptError, err: + print str(err) + sys.exit(1) -fdef.write('''/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */ +prefix = "" +c_file = 'qapi-visit.c' +h_file = 'qapi-visit.h' -#include "qapi-visit.h" -''') +for o, a in opts: + if o in ("-p", "--prefix"): + prefix = a + +c_file = prefix + c_file +h_file = prefix + h_file + +fdef = open(c_file, 'w') +fdecl = open(h_file, 'w') + +fdef.write(mcgen(''' +/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */ -fdecl.write('''/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */ +#include "%(header)s" +''', + header=basename(h_file))) + +fdecl.write(mcgen(''' +/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */ -#ifndef QAPI_VISIT_H -#define QAPI_VISIT_H +#ifndef %(guard)s +#define %(guard)s #include "qapi-visit-core.h" -''') +#include "%(prefix)sqapi-types.h" +''', + prefix=prefix, guard=guardname(h_file))) exprs = parse_schema(sys.stdin)
Signature · 1 line
1.7.0.4
PATCH 7/9 qapi: test schema for test-visiter unit tests
In reply to Michael Roth--- qapi-schema-test.json | 11 +++++++++++ 1 files changed, 11 insertions(+), 0 deletions(-) create mode 100644 qapi-schema-test.json
qapi-schema-test.json new file +11 -0
@@ -0,0 +1,11 @@ +# *-*- Mode: Python -*-* + +# for testing nested structs +{ 'type': 'UserDefOne', + 'data': { 'integer': 'int', 'string': 'str' } } + +{ 'type': 'UserDefTwo', + 'data': { 'string': 'str', + 'dict': { 'string': 'str', + 'dict': { 'userdef': 'UserDefOne', 'string': 'str' }, + '*dict2': { 'userdef': 'UserDefOne', 'string': 'str' } } } }
Signature · 1 line
1.7.0.4
PATCH 8/9 qapi: Makefile, build test-visiter with generated test code
In reply to Michael RothThis pulls in test-qapi-visit.c/.h and test-qapi-types.h, which are generated from qapi-schema-test.json using the --prefix arguments for the various code generators. Useful for targetted testing of the schema parser/code generators.
--- Makefile | 15 ++++++++++++--- 1 files changed, 12 insertions(+), 3 deletions(-)
Makefile +12 -3
@@ -1,7 +1,7 @@ # Makefile for QEMU. GENERATED_HEADERS = config-host.h trace.h qemu-options.def qmp.h libqmp.h qdev-marshal.h -GENERATED_HEADERS += qapi-types.h qmp-marshal-types.h qcfg-marshal.h +GENERATED_HEADERS += qapi-types.h qmp-marshal-types.h qcfg-marshal.h test-qapi-types.h ifeq ($(TRACE_BACKEND),dtrace) GENERATED_HEADERS += trace-dtrace.h endif
@@ -72,7 +72,7 @@ defconfig: -include config-all-devices.mak -TOOLS += test-libqmp test-qcfg qsh +TOOLS += test-libqmp test-qcfg qsh test-visiter build-all: $(DOCS) $(TOOLS) recurse-all
@@ -109,6 +109,9 @@ QEMU_CFLAGS+=$(CURL_CFLAGS) QEMU_CFLAGS+=$(GLIB_CFLAGS) +QEMU_CFLAGS+="-I." +QEMU_CFLAGS+="-Iqapi" + ui/cocoa.o: ui/cocoa.m ui/sdl.o audio/sdlaudio.o ui/sdl_zoom.o baum.o: QEMU_CFLAGS += $(SDL_CFLAGS)
@@ -252,7 +255,13 @@ test-qcfg: test-qcfg.o $(QCFG_OBJS) qemu-timer-common.o qapi-obj-y := qapi/qmp-output-visiter.o qapi/qmp-input-visiter.o -test-visiter: test-visiter.o qfloat.o qint.o qdict.o qstring.o qlist.o qbool.o $(qapi-obj-y) error.o osdep.o qemu-malloc.o $(oslib-obj-y) qjson.o json-streamer.o json-lexer.o json-parser.o qerror.o +test-qapi-types.h: $(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-types.py + $(call quiet-command,python $(SRC_PATH)/scripts/qapi-types.py --prefix="test-" < $<, " GEN $@") +test-qapi-visit.c: test-qapi-visit.h +test-qapi-visit.h: $(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-visit.py + $(call quiet-command,python $(SRC_PATH)/scripts/qapi-visit.py --prefix="test-" < $<, " GEN $@") +test-visiter.o: test-qapi-types.h test-qapi-visit.c +test-visiter: test-visiter.o qfloat.o qint.o qdict.o qstring.o qlist.o qbool.o $(qapi-obj-y) error.o osdep.o qemu-malloc.o $(oslib-obj-y) qjson.o json-streamer.o json-lexer.o json-parser.o qerror.o test-qapi-visit.o qmp-check: build-all $(call quiet-command, ./test-libqmp, " CHECK $@")
Signature · 1 line
1.7.0.4
PATCH 9/9 qapi: test-visiter, pull in gen code, tests for nested structures
In reply to Michael RothPull into test files generated from qapi-schema-test.json by visiter/types generators so we can do unit tests against the generated code.
Also, add test cases for handling of nested structs/qobjects. Note:
optional members are implemented yet so test case is slightly gimped.
--- test-visiter.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 98 insertions(+), 0 deletions(-)
test-visiter.c +98 -0
@@ -1,6 +1,8 @@#include <glib.h> #include "qapi/qmp-output-visiter.h" #include "qapi/qmp-input-visiter.h" +#include "test-qapi-types.h" +#include "test-qapi-visit.h" #include "qemu-objects.h" typedef struct TestStruct
@@ -37,6 +39,100 @@ static void visit_type_TestStructList(Visiter *m, TestStructList ** obj, const cvisit_end_list (m, errp); } +/* test deep nesting with refs to other user-defined types */ +static void test_nested_structs (void ) +{ +QmpOutputVisiter *mo; +QmpInputVisiter *mi; +Visiter *v; +UserDefOne ud1; +UserDefOne *ud1_p = &ud1, *ud1c_p =NULL ; +UserDefTwo ud2; +UserDefTwo *ud2_p = &ud2, *ud2c_p =NULL ; +Error *err =NULL ; +QObject *obj; +QString *str; + + ud1.integer =42 ; + ud1.string =strdup ("fourty two" ); + +/* sanity check */ + mo =qmp_output_visiter_new (); + v =qmp_output_get_visiter (mo); +visit_type_UserDefOne (v, &ud1_p,"o_O" , &err); +if (err) { +g_error ("%s" ,error_get_pretty (err)); + } + obj =qmp_output_get_qobject (mo); +g_assert (obj); +qobject_decref (obj); + + ud2.string =strdup ("fourty three" ); + ud2.dict.string =strdup ("fourty four" ); + ud2.dict.dict.userdef = ud1_p; + ud2.dict.dict.string =strdup ("fourty five" ); + ud2.dict.has_dict2 = true; + ud2.dict.dict2.userdef = ud1_p; + ud2.dict.dict2.string =strdup ("fourty six" ); + +/* c type -> qobject */ + mo =qmp_output_visiter_new (); + v =qmp_output_get_visiter (mo); +visit_type_UserDefTwo (v, &ud2_p,"unused" , &err); +if (err) { +g_error ("%s" ,error_get_pretty (err)); + } + obj =qmp_output_get_qobject (mo); +g_assert (obj); + str =qobject_to_json_pretty (obj); +g_print ("%s\n" ,qstring_get_str (str)); +QDECREF (str); + +/* qobject -> c type, should match original struct */ + mi =qmp_input_visiter_new (obj); + v =qmp_input_get_visiter (mi); +visit_type_UserDefTwo (v, &ud2c_p,"unused" , &err); +if (err) { +g_error ("%s" ,error_get_pretty (err)); + } + +g_assert (!g_strcmp0 (ud2c_p->string, ud2.string)); +g_assert (!g_strcmp0 (ud2c_p->dict.string, ud2.dict.string)); + + ud1c_p = ud2c_p->dict.dict.userdef; +g_assert (ud1c_p->integer == ud1_p->integer); +g_assert (!g_strcmp0 (ud1c_p->string, ud1_p->string)); + +g_assert (!g_strcmp0 (ud2c_p->dict.dict.string, ud2.dict.dict.string)); + +/* TODO: ud2.dict.dict2 is optional dict, so these require +* visit_start_optional() to be implemented for input visiter +*/ +/* +ud1c_p = ud2c_p->dict.dict2.userdef; +g_assert(ud1c_p->integer == ud1_p->integer); +g_assert(!g_strcmp0(ud1c_p->string, ud1_p->string)); +*/ + +g_assert (g_strcmp0 (ud2c_p->dict.dict2.string, ud2.dict.dict2.string)); + +qemu_free (ud1.string); +qemu_free (ud2.string); +qemu_free (ud2.dict.string); +qemu_free (ud2.dict.dict.string); +qemu_free (ud2.dict.dict2.string); + +qemu_free (ud1c_p->string); +qemu_free (ud1c_p); +qemu_free (ud2c_p->string); +qemu_free (ud2c_p->dict.string); +qemu_free (ud2c_p->dict.dict.string); +qemu_free (ud2c_p->dict.dict2.string); +qemu_free (ud2c_p); + +qobject_decref (obj); +} +int main (int argc,char **argv) {QmpOutputVisiter *mo;
@@ -122,6 +218,8 @@ int main(int argc, char **argv)qobject_decref (obj); +g_test_add_func ("/0.15/nested_structs" , test_nested_structs); +g_test_run ();return 0 ;
Signature · 1 line
1.7.0.4