* [Qemu-devel] [PATCH 1/9] qapi: Fix type generator for structured type members
2011-04-29 15:21 [Qemu-devel] [PULL] QAPI code generator fix-ups Michael Roth
@ 2011-04-29 15:21 ` Michael Roth
2011-04-29 15:21 ` [Qemu-devel] [PATCH 2/9] qapi: input visiter, don't always allocate memory for structs Michael Roth
` (7 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Michael Roth @ 2011-04-29 15:21 UTC (permalink / raw)
To: qemu-devel; +Cc: aliguori, agl, mdroth
This 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.
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
scripts/qapi-types.py | 39 +++++++++++++++++++++++----------------
scripts/qapi.py | 7 +++++--
2 files changed, 28 insertions(+), 18 deletions(-)
diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py
index 42ad6a5..d645bad 100644
--- a/scripts/qapi-types.py
+++ b/scripts/qapi-types.py
@@ -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('''
diff --git a/scripts/qapi.py b/scripts/qapi.py
index 74533d0..1bc4604 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -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 = ''
--
1.7.0.4
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [Qemu-devel] [PATCH 2/9] qapi: input visiter, don't always allocate memory for structs
2011-04-29 15:21 [Qemu-devel] [PULL] QAPI code generator fix-ups Michael Roth
2011-04-29 15:21 ` [Qemu-devel] [PATCH 1/9] qapi: Fix type generator for structured type members Michael Roth
@ 2011-04-29 15:21 ` Michael Roth
2011-04-29 15:21 ` [Qemu-devel] [PATCH 3/9] qapi: Fix visiter generator for nested structs/qobjects Michael Roth
` (6 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Michael Roth @ 2011-04-29 15:21 UTC (permalink / raw)
To: qemu-devel; +Cc: aliguori, agl, mdroth
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.
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
qapi/qmp-input-visiter.c | 4 +++-
1 files changed, 3 insertions(+), 1 deletions(-)
diff --git a/qapi/qmp-input-visiter.c b/qapi/qmp-input-visiter.c
index 53379fd..4bae85e 100644
--- a/qapi/qmp-input-visiter.c
+++ b/qapi/qmp-input-visiter.c
@@ -75,7 +75,9 @@ static void qmp_input_start_struct(Visiter *v, void **obj, const char *kind, con
qmp_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)
--
1.7.0.4
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [Qemu-devel] [PATCH 3/9] qapi: Fix visiter generator for nested structs/qobjects
2011-04-29 15:21 [Qemu-devel] [PULL] QAPI code generator fix-ups Michael Roth
2011-04-29 15:21 ` [Qemu-devel] [PATCH 1/9] qapi: Fix type generator for structured type members Michael Roth
2011-04-29 15:21 ` [Qemu-devel] [PATCH 2/9] qapi: input visiter, don't always allocate memory for structs Michael Roth
@ 2011-04-29 15:21 ` Michael Roth
2011-04-29 15:21 ` [Qemu-devel] [PATCH 4/9] qapi: some basename/guardname py utility functions Michael Roth
` (5 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Michael Roth @ 2011-04-29 15:21 UTC (permalink / raw)
To: qemu-devel; +Cc: aliguori, agl, mdroth
Recursively handle structured types as identified by the schema parser.
Generated code Uses visiter stack's push/pop logic to traverse the
structure's tree.
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
scripts/qapi-visit.py | 55 +++++++++++++++++++++++++++++++++---------------
1 files changed, 38 insertions(+), 17 deletions(-)
diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py
index 6bcd84d..bf005c6 100644
--- a/scripts/qapi-visit.py
+++ b/scripts/qapi-visit.py
@@ -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);
--
1.7.0.4
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [Qemu-devel] [PATCH 4/9] qapi: some basename/guardname py utility functions
2011-04-29 15:21 [Qemu-devel] [PULL] QAPI code generator fix-ups Michael Roth
` (2 preceding siblings ...)
2011-04-29 15:21 ` [Qemu-devel] [PATCH 3/9] qapi: Fix visiter generator for nested structs/qobjects Michael Roth
@ 2011-04-29 15:21 ` Michael Roth
2011-04-29 15:21 ` [Qemu-devel] [PATCH 5/9] qapi: add --prefix option to type generator Michael Roth
` (4 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Michael Roth @ 2011-04-29 15:21 UTC (permalink / raw)
To: qemu-devel; +Cc: aliguori, agl, mdroth
Some 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.
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
scripts/qapi.py | 5 +++++
1 files changed, 5 insertions(+), 0 deletions(-)
diff --git a/scripts/qapi.py b/scripts/qapi.py
index 1bc4604..2786430 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -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()
--
1.7.0.4
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [Qemu-devel] [PATCH 5/9] qapi: add --prefix option to type generator
2011-04-29 15:21 [Qemu-devel] [PULL] QAPI code generator fix-ups Michael Roth
` (3 preceding siblings ...)
2011-04-29 15:21 ` [Qemu-devel] [PATCH 4/9] qapi: some basename/guardname py utility functions Michael Roth
@ 2011-04-29 15:21 ` Michael Roth
2011-04-29 15:21 ` [Qemu-devel] [PATCH 6/9] qapi: add --prefix option for visiter generator Michael Roth
` (3 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Michael Roth @ 2011-04-29 15:21 UTC (permalink / raw)
To: qemu-devel; +Cc: aliguori, agl, mdroth
This 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.
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
scripts/qapi-types.py | 30 ++++++++++++++++++++++++------
1 files changed, 24 insertions(+), 6 deletions(-)
diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py
index d645bad..e38a651 100644
--- a/scripts/qapi-types.py
+++ b/scripts/qapi-types.py
@@ -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"
--
1.7.0.4
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [Qemu-devel] [PATCH 6/9] qapi: add --prefix option for visiter generator
2011-04-29 15:21 [Qemu-devel] [PULL] QAPI code generator fix-ups Michael Roth
` (4 preceding siblings ...)
2011-04-29 15:21 ` [Qemu-devel] [PATCH 5/9] qapi: add --prefix option to type generator Michael Roth
@ 2011-04-29 15:21 ` Michael Roth
2011-04-29 15:22 ` [Qemu-devel] [PATCH 7/9] qapi: test schema for test-visiter unit tests Michael Roth
` (2 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Michael Roth @ 2011-04-29 15:21 UTC (permalink / raw)
To: qemu-devel; +Cc: aliguori, agl, mdroth
Generated code assumes the required types header was generated using the
same prefix.
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
scripts/qapi-visit.py | 41 ++++++++++++++++++++++++++++++++---------
1 files changed, 32 insertions(+), 9 deletions(-)
diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py
index bf005c6..ee6b031 100644
--- a/scripts/qapi-visit.py
+++ b/scripts/qapi-visit.py
@@ -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)
--
1.7.0.4
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [Qemu-devel] [PATCH 7/9] qapi: test schema for test-visiter unit tests
2011-04-29 15:21 [Qemu-devel] [PULL] QAPI code generator fix-ups Michael Roth
` (5 preceding siblings ...)
2011-04-29 15:21 ` [Qemu-devel] [PATCH 6/9] qapi: add --prefix option for visiter generator Michael Roth
@ 2011-04-29 15:22 ` Michael Roth
2011-04-29 15:22 ` [Qemu-devel] [PATCH 8/9] qapi: Makefile, build test-visiter with generated test code Michael Roth
2011-04-29 15:22 ` [Qemu-devel] [PATCH 9/9] qapi: test-visiter, pull in gen code, tests for nested structures Michael Roth
8 siblings, 0 replies; 10+ messages in thread
From: Michael Roth @ 2011-04-29 15:22 UTC (permalink / raw)
To: qemu-devel; +Cc: aliguori, agl, mdroth
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
qapi-schema-test.json | 11 +++++++++++
1 files changed, 11 insertions(+), 0 deletions(-)
create mode 100644 qapi-schema-test.json
diff --git a/qapi-schema-test.json b/qapi-schema-test.json
new file mode 100644
index 0000000..4717dee
--- /dev/null
+++ b/qapi-schema-test.json
@@ -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' } } } }
--
1.7.0.4
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [Qemu-devel] [PATCH 8/9] qapi: Makefile, build test-visiter with generated test code
2011-04-29 15:21 [Qemu-devel] [PULL] QAPI code generator fix-ups Michael Roth
` (6 preceding siblings ...)
2011-04-29 15:22 ` [Qemu-devel] [PATCH 7/9] qapi: test schema for test-visiter unit tests Michael Roth
@ 2011-04-29 15:22 ` Michael Roth
2011-04-29 15:22 ` [Qemu-devel] [PATCH 9/9] qapi: test-visiter, pull in gen code, tests for nested structures Michael Roth
8 siblings, 0 replies; 10+ messages in thread
From: Michael Roth @ 2011-04-29 15:22 UTC (permalink / raw)
To: qemu-devel; +Cc: aliguori, agl, mdroth
This 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.
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
Makefile | 15 ++++++++++++---
1 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/Makefile b/Makefile
index d510779..d05cf74 100644
--- a/Makefile
+++ b/Makefile
@@ -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 $@")
--
1.7.0.4
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [Qemu-devel] [PATCH 9/9] qapi: test-visiter, pull in gen code, tests for nested structures
2011-04-29 15:21 [Qemu-devel] [PULL] QAPI code generator fix-ups Michael Roth
` (7 preceding siblings ...)
2011-04-29 15:22 ` [Qemu-devel] [PATCH 8/9] qapi: Makefile, build test-visiter with generated test code Michael Roth
@ 2011-04-29 15:22 ` Michael Roth
8 siblings, 0 replies; 10+ messages in thread
From: Michael Roth @ 2011-04-29 15:22 UTC (permalink / raw)
To: qemu-devel; +Cc: aliguori, agl, mdroth
Pull 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.
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
test-visiter.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 98 insertions(+), 0 deletions(-)
diff --git a/test-visiter.c b/test-visiter.c
index 4aec8ab..28810d2 100644
--- a/test-visiter.c
+++ b/test-visiter.c
@@ -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 c
visit_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;
--
1.7.0.4
^ permalink raw reply related [flat|nested] 10+ messages in thread