Skip to contentSkip to conversation

drop qapi nested structs

PATCH Superseded Eric Blake 3 participants 29 messages Aug 6, 2014 – Aug 15, 2014

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

v3 cover drop qapi nested structs

According to this email:
https://lists.gnu.org/archive/html/qemu-devel/2014-05/msg00708.html
we want to repurpose 'data': { 'name': {dict...} } in qapi files for future use of designating default values of optional parameters. But to do that, we must first nuke existing use of that syntax for declaring nested structs. Enhancing the testsuite while at it never hurts.

v3:
No code changes, but fix mis-send of 11-14 [Peter]

v2:
New patches: 1-2, 5-9
consistent TAB usage in Makefile [Fam]
catch more bad coding constructs, and test them
avoid code duplication in type validity checks (patch 14 [former 7] is simpler because of patch 9)

Eric Blake (14):
qapi: consistent whitespace in tests/Makefile
qapi: ignore files created during make check
qapi: add some enum tests
qapi: better error message for bad enum
qapi: add some expr tests
qapi: require valid expressions
qapi: add some type check tests
qapi: add expr_name() helper
qapi: add check_type helper function
qapi: merge UserDefTwo and UserDefNested in tests
qapi: drop tests for inline subtypes
qapi: drop inline subtype in query-version
qapi: drop inline subtype in query-pci
qapi: drop support for inline subtypes

 hmp.c                                        | 28 ++++----
 hw/pci/pci.c                                 | 42 ++++++------
 qapi-schema.json                             | 90 ++++++++++++++++++--------
 qapi/common.json                             | 26 ++++++--
 qmp.c                                        |  9 +--
 scripts/qapi-commands.py                     |  8 +--
 scripts/qapi-event.py                        |  4 +-
 scripts/qapi-types.py                        |  9 +--
 scripts/qapi-visit.py                        | 37 ++---------
 scripts/qapi.py                              | 96 ++++++++++++++++++++++------
 tests/.gitignore                             |  3 +
 tests/Makefile                               | 37 ++++++-----
 tests/qapi-schema/data-array-empty.err       |  0
 tests/qapi-schema/data-array-empty.exit      |  1 +
 tests/qapi-schema/data-array-empty.json      |  1 +
 tests/qapi-schema/data-array-empty.out       |  3 +
 tests/qapi-schema/data-array-unknown.err     |  1 +
 tests/qapi-schema/data-array-unknown.exit    |  1 +
 tests/qapi-schema/data-array-unknown.json    |  1 +
 tests/qapi-schema/data-array-unknown.out     |  0
 tests/qapi-schema/data-int.err               |  1 +
 tests/qapi-schema/data-int.exit              |  1 +
 tests/qapi-schema/data-int.json              |  1 +
 tests/qapi-schema/data-int.out               |  0
 tests/qapi-schema/data-unknown.err           |  1 +
 tests/qapi-schema/data-unknown.exit          |  1 +
 tests/qapi-schema/data-unknown.json          |  1 +
 tests/qapi-schema/data-unknown.out           |  0
 tests/qapi-schema/double-type.err            |  1 +
 tests/qapi-schema/double-type.exit           |  1 +
 tests/qapi-schema/double-type.json           |  1 +
 tests/qapi-schema/double-type.out            |  0
 tests/qapi-schema/enum-empty.err             |  0
 tests/qapi-schema/enum-empty.exit            |  1 +
 tests/qapi-schema/enum-empty.json            |  1 +
 tests/qapi-schema/enum-empty.out             |  3 +
 tests/qapi-schema/enum-missing-data.err      |  1 +
 tests/qapi-schema/enum-missing-data.exit     |  1 +
 tests/qapi-schema/enum-missing-data.json     |  1 +
 tests/qapi-schema/enum-missing-data.out      |  0
 tests/qapi-schema/enum-wrong-data.err        |  1 +
 tests/qapi-schema/enum-wrong-data.exit       |  1 +
 tests/qapi-schema/enum-wrong-data.json       |  1 +
 tests/qapi-schema/enum-wrong-data.out        |  0
 tests/qapi-schema/event-nest-struct.err      |  2 +-
 tests/qapi-schema/indented-expr.json         |  4 +-
 tests/qapi-schema/indented-expr.out          |  2 +-
 tests/qapi-schema/missing-type.err           |  1 +
 tests/qapi-schema/missing-type.exit          |  1 +
 tests/qapi-schema/missing-type.json          |  1 +
 tests/qapi-schema/missing-type.out           |  0
 tests/qapi-schema/nested-struct-data.err     |  1 +
 tests/qapi-schema/nested-struct-data.exit    |  1 +
 tests/qapi-schema/nested-struct-data.json    |  3 +
 tests/qapi-schema/nested-struct-data.out     |  0
 tests/qapi-schema/nested-struct-returns.err  |  1 +
 tests/qapi-schema/nested-struct-returns.exit |  1 +
 tests/qapi-schema/nested-struct-returns.json |  2 +
 tests/qapi-schema/nested-struct-returns.out  |  0
 tests/qapi-schema/qapi-schema-test.json      | 18 +++---
 tests/qapi-schema/qapi-schema-test.out       | 10 +--
 tests/qapi-schema/returns-array-bad.err      |  1 +
 tests/qapi-schema/returns-array-bad.exit     |  1 +
 tests/qapi-schema/returns-array-bad.json     |  1 +
 tests/qapi-schema/returns-array-bad.out      |  0
 tests/qapi-schema/returns-int.err            |  0
 tests/qapi-schema/returns-int.exit           |  1 +
 tests/qapi-schema/returns-int.json           |  1 +
 tests/qapi-schema/returns-int.out            |  3 +
 tests/qapi-schema/returns-unknown.err        |  1 +
 tests/qapi-schema/returns-unknown.exit       |  1 +
 tests/qapi-schema/returns-unknown.json       |  1 +
 tests/qapi-schema/returns-unknown.out        |  0
 tests/test-qmp-commands.c                    | 35 +++++-----
 tests/test-qmp-input-strict.c                | 19 +++---
 tests/test-qmp-input-visitor.c               | 25 +++++---
 tests/test-qmp-output-visitor.c              | 64 ++++++++++---------
 tests/test-visitor-serialization.c           | 84 +++++++++++++-----------
 78 files changed, 433 insertions(+), 270 deletions(-)
 create mode 100644 tests/qapi-schema/data-array-empty.err
 create mode 100644 tests/qapi-schema/data-array-empty.exit
 create mode 100644 tests/qapi-schema/data-array-empty.json
 create mode 100644 tests/qapi-schema/data-array-empty.out
 create mode 100644 tests/qapi-schema/data-array-unknown.err
 create mode 100644 tests/qapi-schema/data-array-unknown.exit
 create mode 100644 tests/qapi-schema/data-array-unknown.json
 create mode 100644 tests/qapi-schema/data-array-unknown.out
 create mode 100644 tests/qapi-schema/data-int.err
 create mode 100644 tests/qapi-schema/data-int.exit
 create mode 100644 tests/qapi-schema/data-int.json
 create mode 100644 tests/qapi-schema/data-int.out
 create mode 100644 tests/qapi-schema/data-unknown.err
 create mode 100644 tests/qapi-schema/data-unknown.exit
 create mode 100644 tests/qapi-schema/data-unknown.json
 create mode 100644 tests/qapi-schema/data-unknown.out
 create mode 100644 tests/qapi-schema/double-type.err
 create mode 100644 tests/qapi-schema/double-type.exit
 create mode 100644 tests/qapi-schema/double-type.json
 create mode 100644 tests/qapi-schema/double-type.out
 create mode 100644 tests/qapi-schema/enum-empty.err
 create mode 100644 tests/qapi-schema/enum-empty.exit
 create mode 100644 tests/qapi-schema/enum-empty.json
 create mode 100644 tests/qapi-schema/enum-empty.out
 create mode 100644 tests/qapi-schema/enum-missing-data.err
 create mode 100644 tests/qapi-schema/enum-missing-data.exit
 create mode 100644 tests/qapi-schema/enum-missing-data.json
 create mode 100644 tests/qapi-schema/enum-missing-data.out
 create mode 100644 tests/qapi-schema/enum-wrong-data.err
 create mode 100644 tests/qapi-schema/enum-wrong-data.exit
 create mode 100644 tests/qapi-schema/enum-wrong-data.json
 create mode 100644 tests/qapi-schema/enum-wrong-data.out
 create mode 100644 tests/qapi-schema/missing-type.err
 create mode 100644 tests/qapi-schema/missing-type.exit
 create mode 100644 tests/qapi-schema/missing-type.json
 create mode 100644 tests/qapi-schema/missing-type.out
 create mode 100644 tests/qapi-schema/nested-struct-data.err
 create mode 100644 tests/qapi-schema/nested-struct-data.exit
 create mode 100644 tests/qapi-schema/nested-struct-data.json
 create mode 100644 tests/qapi-schema/nested-struct-data.out
 create mode 100644 tests/qapi-schema/nested-struct-returns.err
 create mode 100644 tests/qapi-schema/nested-struct-returns.exit
 create mode 100644 tests/qapi-schema/nested-struct-returns.json
 create mode 100644 tests/qapi-schema/nested-struct-returns.out
 create mode 100644 tests/qapi-schema/returns-array-bad.err
 create mode 100644 tests/qapi-schema/returns-array-bad.exit
 create mode 100644 tests/qapi-schema/returns-array-bad.json
 create mode 100644 tests/qapi-schema/returns-array-bad.out
 create mode 100644 tests/qapi-schema/returns-int.err
 create mode 100644 tests/qapi-schema/returns-int.exit
 create mode 100644 tests/qapi-schema/returns-int.json
 create mode 100644 tests/qapi-schema/returns-int.out
 create mode 100644 tests/qapi-schema/returns-unknown.err
 create mode 100644 tests/qapi-schema/returns-unknown.exit
 create mode 100644 tests/qapi-schema/returns-unknown.json
 create mode 100644 tests/qapi-schema/returns-unknown.out
Signature · 1 line
1.9.3
author
#1 lore raw

PATCH v3 1/14 qapi: consistent whitespace in tests/Makefile

This file had a mix of TAB vs. 8-space indentation; given that it is a Makefile, TAB is more idiomatic even though in these particular cases the choice of whitespace didn't matter.

* tests/Makefile (check-qapi-schema-y, GENERATED_HEADERS) (test-qapi-obj-y): Consistent tab indentation.

---
 tests/Makefile | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)
tests/Makefile +16 -16
index 4b2e1bb..aa17e44 100644
@@ -190,23 +190,23 @@ $(foreach target,$(SYSEMU_TARGET_LIST), \
     $(eval check-qtest-$(target)-y += tests/qom-test$(EXESUF)))
 
 check-qapi-schema-y := $(addprefix tests/qapi-schema/, \
-        comments.json empty.json funny-char.json indented-expr.json \
-        missing-colon.json missing-comma-list.json \
-        missing-comma-object.json non-objects.json \
-        qapi-schema-test.json quoted-structural-chars.json \
-        trailing-comma-list.json trailing-comma-object.json \
-        unclosed-list.json unclosed-object.json unclosed-string.json \
-        duplicate-key.json union-invalid-base.json flat-union-no-base.json \
-        flat-union-invalid-discriminator.json \
-        flat-union-invalid-branch-key.json flat-union-reverse-define.json \
-        flat-union-string-discriminator.json \
-        include-simple.json include-relpath.json include-format-err.json \
-        include-non-file.json include-no-file.json include-before-err.json \
-        include-nested-err.json include-self-cycle.json include-cycle.json \
-        include-repetition.json event-nest-struct.json)
+	comments.json empty.json funny-char.json indented-expr.json \
+	missing-colon.json missing-comma-list.json \
+	missing-comma-object.json non-objects.json \
+	qapi-schema-test.json quoted-structural-chars.json \
+	trailing-comma-list.json trailing-comma-object.json \
+	unclosed-list.json unclosed-object.json unclosed-string.json \
+	duplicate-key.json union-invalid-base.json flat-union-no-base.json \
+	flat-union-invalid-discriminator.json \
+	flat-union-invalid-branch-key.json flat-union-reverse-define.json \
+	flat-union-string-discriminator.json \
+	include-simple.json include-relpath.json include-format-err.json \
+	include-non-file.json include-no-file.json include-before-err.json \
+	include-nested-err.json include-self-cycle.json include-cycle.json \
+	include-repetition.json event-nest-struct.json)
 
 GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h \
-                     tests/test-qmp-commands.h tests/test-qapi-event.h
+		     tests/test-qmp-commands.h tests/test-qapi-event.h
 
 test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \
 	tests/check-qlist.o tests/check-qfloat.o tests/check-qjson.o \
@@ -218,7 +218,7 @@ test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \
 	tests/test-opts-visitor.o tests/test-qmp-event.o
 
 test-qapi-obj-y = tests/test-qapi-visit.o tests/test-qapi-types.o \
-                  tests/test-qapi-event.o
+		  tests/test-qapi-event.o
 
 $(test-obj-y): QEMU_INCLUDES += -Itests
 QEMU_CFLAGS += -I$(SRC_PATH)/tests
Signature · 1 line
1.9.3
author
#2 lore raw

PATCH v3 2/14 qapi: ignore files created during make check

In reply to Eric Blake

After an in-tree build and run of 'make check-{qapi-schema,unit}', I noticed some leftover files.

* tests/.gitignore: Ignore more testsuite droppings.

---
 tests/.gitignore | 3 +++
 1 file changed, 3 insertions(+)
tests/.gitignore +3 -0
index c71c110..e2e4957 100644
@@ -14,11 +14,14 @@ test-int128
 test-iov
 test-mul64
 test-opts-visitor
+test-qapi-event.[ch]
 test-qapi-types.[ch]
 test-qapi-visit.[ch]
 test-qdev-global-props
+test-qemu-opts
 test-qmp-commands
 test-qmp-commands.h
+test-qmp-event
 test-qmp-input-strict
 test-qmp-input-visitor
 test-qmp-marshal.c
Signature · 1 line
1.9.3
author
#3 lore raw

PATCH v3 3/14 qapi: add some enum tests

In reply to Eric Blake

Demonstrate that the qapi generator doesn't deal well with enums that aren't up to par. Later patches will update the expected results as the generator is made stricter.

* tests/qapi-schema/enum-empty.*: New files.
* tests/qapi-schema/enum-missing-data.*: Likewise.
* tests/qapi-schema/enum-wrong-data.*: Likewise.
* tests/Makefile (check-qapi-schema-y): Run them.

---
 tests/Makefile                           | 3 ++-
 tests/qapi-schema/enum-empty.err         | 0
 tests/qapi-schema/enum-empty.exit        | 1 +
 tests/qapi-schema/enum-empty.json        | 1 +
 tests/qapi-schema/enum-empty.out         | 3 +++
 tests/qapi-schema/enum-missing-data.err  | 6 ++++++
 tests/qapi-schema/enum-missing-data.exit | 1 +
 tests/qapi-schema/enum-missing-data.json | 1 +
 tests/qapi-schema/enum-missing-data.out  | 0
 tests/qapi-schema/enum-wrong-data.err    | 0
 tests/qapi-schema/enum-wrong-data.exit   | 1 +
 tests/qapi-schema/enum-wrong-data.json   | 1 +
 tests/qapi-schema/enum-wrong-data.out    | 3 +++
 13 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 tests/qapi-schema/enum-empty.err
 create mode 100644 tests/qapi-schema/enum-empty.exit
 create mode 100644 tests/qapi-schema/enum-empty.json
 create mode 100644 tests/qapi-schema/enum-empty.out
 create mode 100644 tests/qapi-schema/enum-missing-data.err
 create mode 100644 tests/qapi-schema/enum-missing-data.exit
 create mode 100644 tests/qapi-schema/enum-missing-data.json
 create mode 100644 tests/qapi-schema/enum-missing-data.out
 create mode 100644 tests/qapi-schema/enum-wrong-data.err
 create mode 100644 tests/qapi-schema/enum-wrong-data.exit
 create mode 100644 tests/qapi-schema/enum-wrong-data.json
 create mode 100644 tests/qapi-schema/enum-wrong-data.out
tests/Makefile +2 -1
index aa17e44..fbf6909 100644
@@ -190,7 +190,8 @@ $(foreach target,$(SYSEMU_TARGET_LIST), \
     $(eval check-qtest-$(target)-y += tests/qom-test$(EXESUF)))
 
 check-qapi-schema-y := $(addprefix tests/qapi-schema/, \
-	comments.json empty.json funny-char.json indented-expr.json \
+	comments.json empty.json enum-empty.json enum-missing-data.json \
+	enum-wrong-data.json funny-char.json indented-expr.json \
 	missing-colon.json missing-comma-list.json \
 	missing-comma-object.json non-objects.json \
 	qapi-schema-test.json quoted-structural-chars.json \
tests/qapi-schema/enum-empty.err new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/enum-empty.exit new file +1 -0
new file mode 100644
index 0000000..573541a
@@ -0,0 +1 @@
+0
tests/qapi-schema/enum-empty.json new file +1 -0
new file mode 100644
index 0000000..f550aa0
@@ -0,0 +1 @@
+{ 'enum': 'MyEnum', 'data': [ ] }
tests/qapi-schema/enum-empty.out new file +3 -0
new file mode 100644
index 0000000..3b75c16
@@ -0,0 +1,3 @@
+[OrderedDict([('enum', 'MyEnum'), ('data', [])])]
+[{'enum_name': 'MyEnum', 'enum_values': []}]
+[]
tests/qapi-schema/enum-missing-data.err new file +6 -0
new file mode 100644
index 0000000..1fec213
@@ -0,0 +1,6 @@
+Traceback (most recent call last):
+  File "tests/qapi-schema/test-qapi.py", line 19, in <module>
+    exprs = parse_schema(sys.argv[1])
+  File "scripts/qapi.py", line 339, in parse_schema
+    add_enum(expr['enum'], expr['data'])
+KeyError: 'data'
tests/qapi-schema/enum-missing-data.exit new file +1 -0
new file mode 100644
index 0000000..d00491f
@@ -0,0 +1 @@
+1
tests/qapi-schema/enum-missing-data.json new file +1 -0
new file mode 100644
index 0000000..6e2acd8
@@ -0,0 +1 @@
+{ 'enum': 'MyEnum' }
tests/qapi-schema/enum-missing-data.out new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/enum-wrong-data.err new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/enum-wrong-data.exit new file +1 -0
new file mode 100644
index 0000000..573541a
@@ -0,0 +1 @@
+0
tests/qapi-schema/enum-wrong-data.json new file +1 -0
new file mode 100644
index 0000000..4b7e90c
@@ -0,0 +1 @@
+{ 'enum': 'MyEnum', 'data': { 'value': 'str' } }
tests/qapi-schema/enum-wrong-data.out new file +3 -0
new file mode 100644
index 0000000..28d2211
@@ -0,0 +1,3 @@
+[OrderedDict([('enum', 'MyEnum'), ('data', OrderedDict([('value', 'str')]))])]
+[{'enum_name': 'MyEnum', 'enum_values': OrderedDict([('value', 'str')])}]
+[]
Signature · 1 line
1.9.3
#4 lore raw

Re: [Qemu-devel] [PATCH v3 03/14] qapi: add some enum tests

Quoted 10 lines · Eric Blake

Demonstrate that the qapi generator doesn't deal well with enums that aren't up to par. Later patches will update the expected results as the generator is made stricter.

* tests/qapi-schema/enum-empty.*: New files.
* tests/qapi-schema/enum-missing-data.*: Likewise.
* tests/qapi-schema/enum-wrong-data.*: Likewise.
* tests/Makefile (check-qapi-schema-y): Run them.

[...]

Quoted 52 lines
tests/qapi-schema/enum-missing-data.err new file +6 -0
new file mode 100644
index 0000000..1fec213
@@ -0,0 +1,6 @@
+Traceback (most recent call last):
+  File "tests/qapi-schema/test-qapi.py", line 19, in <module>
+    exprs = parse_schema(sys.argv[1])
+  File "scripts/qapi.py", line 339, in parse_schema
+    add_enum(expr['enum'], expr['data'])
+KeyError: 'data'
tests/qapi-schema/enum-missing-data.exit new file +1 -0
new file mode 100644
index 0000000..d00491f
@@ -0,0 +1 @@
+1
tests/qapi-schema/enum-missing-data.json new file +1 -0
new file mode 100644
index 0000000..6e2acd8
@@ -0,0 +1 @@
+{ 'enum': 'MyEnum' }
tests/qapi-schema/enum-missing-data.out new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/enum-wrong-data.err new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/enum-wrong-data.exit new file +1 -0
new file mode 100644
index 0000000..573541a
@@ -0,0 +1 @@
+0
tests/qapi-schema/enum-wrong-data.json new file +1 -0
new file mode 100644
index 0000000..4b7e90c
@@ -0,0 +1 @@
+{ 'enum': 'MyEnum', 'data': { 'value': 'str' } }
tests/qapi-schema/enum-wrong-data.out new file +0 -0
new file mode 100644
index 0000000..28d2211

tests/qapi-schema/enum-wrong-data.out line 3

Quoted 3 lines
tests/qapi-schema/enum-wrong-data.out new file +3 -0
@@ -0,0 +1,3 @@
+[OrderedDict([('enum', 'MyEnum'), ('data', OrderedDict([('value', 'str')]))])]
+[{'enum_name': 'MyEnum', 'enum_values': OrderedDict([('value', 'str')])}]
+[]

For tests demonstrating incorrect behavior, a comment describing the expected behavior is always welcome.

author
#5 lore raw

Re: [Qemu-devel] [PATCH v3 03/14] qapi: add some enum tests

Quoted 12 lines · Markus Armbruster
Quoted 9 lines · Eric Blake

Demonstrate that the qapi generator doesn't deal well with enums that aren't up to par. Later patches will update the expected results as the generator is made stricter.

* tests/qapi-schema/enum-empty.*: New files.
* tests/qapi-schema/enum-missing-data.*: Likewise.
* tests/qapi-schema/enum-wrong-data.*: Likewise.
* tests/Makefile (check-qapi-schema-y): Run them.

Quoted 8 lines

tests/qapi-schema/enum-wrong-data.out line 3

Quoted 5 lines

+++ b/tests/qapi-schema/enum-wrong-data.out

tests/qapi-schema/enum-wrong-data.out new file +3 -0
@@ -0,0 +1,3 @@
+[OrderedDict([('enum', 'MyEnum'), ('data', OrderedDict([('value', 'str')]))])]
+[{'enum_name': 'MyEnum', 'enum_values': OrderedDict([('value', 'str')])}]
+[]

For tests demonstrating incorrect behavior, a comment describing the expected behavior is always welcome.

Okay, I'll add comments to the .json files that expose weaknesses, when submitting v4

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#6 lore raw

PATCH v3 4/14 qapi: better error message for bad enum

In reply to Eric Blake

The previous commit demonstrated that the generator choked if an enum forgot 'data', and silently ignored an enum where 'data' was the wrong type. Fix both cases to give a sane error message.

* scripts/qapi.py (parse_schema): Avoid bad deref. (check_exprs): Check for array on enums.
* tests/qapi-schema/enum-missing-data.err: Update expected results.
* tests/qapi-schema/enum-wrong-data.*: Likewise.

---
 scripts/qapi.py                         | 16 ++++++++++++----
 tests/qapi-schema/enum-missing-data.err |  7 +------
 tests/qapi-schema/enum-wrong-data.err   |  1 +
 tests/qapi-schema/enum-wrong-data.exit  |  2 +-
 tests/qapi-schema/enum-wrong-data.out   |  3 ---
 5 files changed, 15 insertions(+), 14 deletions(-)
scripts/qapi.py +12 -4
index f2c6d1f..1082416 100644
@@ -2,7 +2,7 @@
 # QAPI helper library
 #
 # Copyright IBM, Corp. 2011
-# Copyright (c) 2013 Red Hat Inc.
+# Copyright (c) 2013-2014 Red Hat Inc.
 #
 # Authors:
 #  Anthony Liguori <[email protected]>
@@ -319,10 +319,18 @@ def check_union(expr, expr_info):
 def check_exprs(schema):
     for expr_elem in schema.exprs:
         expr = expr_elem['expr']
+        info = expr_elem['info']
+        members = expr.get('data')
+
         if expr.has_key('union'):
-            check_union(expr, expr_elem['info'])
+            check_union(expr, info)
         if expr.has_key('event'):
-            check_event(expr, expr_elem['info'])
+            check_event(expr, info)
+        if expr.has_key('enum'):
+            if not isinstance(members, list):
+                raise QAPIExprError(info,
+                                    "enum '%s' requires an array for 'data'"
+                                    % expr.get('enum'))
 
 def parse_schema(input_file):
     try:
@@ -336,7 +344,7 @@ def parse_schema(input_file):
     for expr_elem in schema.exprs:
         expr = expr_elem['expr']
         if expr.has_key('enum'):
-            add_enum(expr['enum'], expr['data'])
+            add_enum(expr['enum'], expr.get('data'))
         elif expr.has_key('union'):
             add_union(expr)
         elif expr.has_key('type'):
tests/qapi-schema/enum-missing-data.err +1 -6
index 1fec213..4912aab 100644
@@ -1,6 +1 @@
-Traceback (most recent call last):
-  File "tests/qapi-schema/test-qapi.py", line 19, in <module>
-    exprs = parse_schema(sys.argv[1])
-  File "scripts/qapi.py", line 339, in parse_schema
-    add_enum(expr['enum'], expr['data'])
-KeyError: 'data'
+tests/qapi-schema/enum-missing-data.json:1: enum 'MyEnum' requires an array for 'data'
tests/qapi-schema/enum-wrong-data.err +1 -0
index e69de29..857f0bd 100644
@@ -0,0 +1 @@
+tests/qapi-schema/enum-wrong-data.json:1: enum 'MyEnum' requires an array for 'data'
tests/qapi-schema/enum-wrong-data.exit +1 -1
index 573541a..d00491f 100644
@@ -1 +1 @@
-0
+1
tests/qapi-schema/enum-wrong-data.out +0 -3
index 28d2211..e69de29 100644
@@ -1,3 +0,0 @@
-[OrderedDict([('enum', 'MyEnum'), ('data', OrderedDict([('value', 'str')]))])]
-[{'enum_name': 'MyEnum', 'enum_values': OrderedDict([('value', 'str')])}]
-[]
Signature · 1 line
1.9.3
author
#7 lore raw

PATCH v3 5/14 qapi: add some expr tests

In reply to Eric Blake

Demonstrate that the qapi generator doesn't deal well with expressions that aren't up to par. Later patches will improve the expected results as the generator is made stricter.

* tests/qapi-schema/missing-type.*: New files.
* tests/qapi-schema/double-type.*: Likewise.
* tests/Makefile (check-qapi-schema-y): Run them.

---
 tests/Makefile                      | 1 +
 tests/qapi-schema/double-type.err   | 0
 tests/qapi-schema/double-type.exit  | 1 +
 tests/qapi-schema/double-type.json  | 1 +
 tests/qapi-schema/double-type.out   | 3 +++
 tests/qapi-schema/missing-type.err  | 0
 tests/qapi-schema/missing-type.exit | 1 +
 tests/qapi-schema/missing-type.json | 1 +
 tests/qapi-schema/missing-type.out  | 3 +++
 9 files changed, 11 insertions(+)
 create mode 100644 tests/qapi-schema/double-type.err
 create mode 100644 tests/qapi-schema/double-type.exit
 create mode 100644 tests/qapi-schema/double-type.json
 create mode 100644 tests/qapi-schema/double-type.out
 create mode 100644 tests/qapi-schema/missing-type.err
 create mode 100644 tests/qapi-schema/missing-type.exit
 create mode 100644 tests/qapi-schema/missing-type.json
 create mode 100644 tests/qapi-schema/missing-type.out
tests/Makefile +1 -0
index fbf6909..1b0c96f 100644
@@ -192,6 +192,7 @@ $(foreach target,$(SYSEMU_TARGET_LIST), \
 check-qapi-schema-y := $(addprefix tests/qapi-schema/, \
 	comments.json empty.json enum-empty.json enum-missing-data.json \
 	enum-wrong-data.json funny-char.json indented-expr.json \
+	missing-type.json double-type.json \
 	missing-colon.json missing-comma-list.json \
 	missing-comma-object.json non-objects.json \
 	qapi-schema-test.json quoted-structural-chars.json \
tests/qapi-schema/double-type.err new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/double-type.exit new file +1 -0
new file mode 100644
index 0000000..573541a
@@ -0,0 +1 @@
+0
tests/qapi-schema/double-type.json new file +1 -0
new file mode 100644
index 0000000..2d4d97d
@@ -0,0 +1 @@
+{ 'command': 'foo', 'type': 'bar', 'data': { } }
tests/qapi-schema/double-type.out new file +3 -0
new file mode 100644
index 0000000..3e244f5
@@ -0,0 +1,3 @@
+[OrderedDict([('command', 'foo'), ('type', 'bar'), ('data', OrderedDict())])]
+[]
+[OrderedDict([('command', 'foo'), ('type', 'bar'), ('data', OrderedDict())])]
tests/qapi-schema/missing-type.err new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/missing-type.exit new file +1 -0
new file mode 100644
index 0000000..573541a
@@ -0,0 +1 @@
+0
tests/qapi-schema/missing-type.json new file +1 -0
new file mode 100644
index 0000000..9043aa4
@@ -0,0 +1 @@
+{ 'data': { } }
tests/qapi-schema/missing-type.out new file +3 -0
new file mode 100644
index 0000000..67fd4fa
@@ -0,0 +1,3 @@
+[OrderedDict([('data', OrderedDict())])]
+[]
+[]
Signature · 1 line
1.9.3
author
#8 lore raw

PATCH v3 6/14 qapi: require valid expressions

In reply to Eric Blake

The previous patch demonstrated that the generator could get confused if an expression had conflicting meta-types, and silently ignored expressions that lacked a known meta-type. Fix both cases to give a sane error message.

* scripts/qapi.py (check_exprs): Require a valid meta-type for every expression.
* tests/qapi-schema/indented-expr.*: Use valid types.
* tests/qapi-schema/missing-type.*: Update expected output.
* tests/qapi-schema/double-type.*: Likewise.

---
 scripts/qapi.py                      | 12 ++++++++++++
 tests/qapi-schema/double-type.err    |  1 +
 tests/qapi-schema/double-type.exit   |  2 +-
 tests/qapi-schema/double-type.out    |  3 ---
 tests/qapi-schema/indented-expr.json |  4 ++--
 tests/qapi-schema/indented-expr.out  |  2 +-
 tests/qapi-schema/missing-type.err   |  1 +
 tests/qapi-schema/missing-type.exit  |  2 +-
 tests/qapi-schema/missing-type.out   |  3 ---
 9 files changed, 19 insertions(+), 11 deletions(-)
scripts/qapi.py +12 -0
index 1082416..910e422 100644
@@ -322,6 +322,18 @@ def check_exprs(schema):
         info = expr_elem['info']
         members = expr.get('data')
 
+        # 'include' has already been flattened; at this point, all exprs
+        # should have one of the remaining keys
+        keys = (expr.has_key('enum') + expr.has_key('union') +
+                expr.has_key('type') + expr.has_key('command') +
+                expr.has_key('event'))
+        if keys < 1:
+            raise QAPIExprError(info,
+                                "Missing expression meta-type")
+        if keys > 1:
+            raise QAPIExprError(info,
+                                "Conflicting expression meta-types")
+
         if expr.has_key('union'):
             check_union(expr, info)
         if expr.has_key('event'):
tests/qapi-schema/double-type.err +1 -0
index e69de29..2df4a12 100644
@@ -0,0 +1 @@
+tests/qapi-schema/double-type.json:1: Conflicting expression meta-types
tests/qapi-schema/double-type.exit +1 -1
index 573541a..d00491f 100644
@@ -1 +1 @@
-0
+1
tests/qapi-schema/double-type.out +0 -3
index 3e244f5..e69de29 100644
@@ -1,3 +0,0 @@
-[OrderedDict([('command', 'foo'), ('type', 'bar'), ('data', OrderedDict())])]
-[]
-[OrderedDict([('command', 'foo'), ('type', 'bar'), ('data', OrderedDict())])]
tests/qapi-schema/indented-expr.json +2 -2
index d80af60..7115d31 100644
@@ -1,2 +1,2 @@
-{ 'id' : 'eins' }
- { 'id' : 'zwei' }
+{ 'command' : 'eins' }
+ { 'command' : 'zwei' }
tests/qapi-schema/indented-expr.out +1 -1
index 98af89a..b5ce915 100644
@@ -1,3 +1,3 @@
-[OrderedDict([('id', 'eins')]), OrderedDict([('id', 'zwei')])]
+[OrderedDict([('command', 'eins')]), OrderedDict([('command', 'zwei')])]
 []
 []
tests/qapi-schema/missing-type.err +1 -0
index e69de29..af650e0 100644
@@ -0,0 +1 @@
+tests/qapi-schema/missing-type.json:1: Missing expression meta-type
tests/qapi-schema/missing-type.exit +1 -1
index 573541a..d00491f 100644
@@ -1 +1 @@
-0
+1
tests/qapi-schema/missing-type.out +0 -3
index 67fd4fa..e69de29 100644
@@ -1,3 +0,0 @@
-[OrderedDict([('data', OrderedDict())])]
-[]
-[]
Signature · 1 line
1.9.3
#9 lore raw

Re: [Qemu-devel] [PATCH v3 06/14] qapi: require valid expressions

Quoted 10 lines · Eric Blake

The previous patch demonstrated that the generator could get confused if an expression had conflicting meta-types, and silently ignored expressions that lacked a known meta-type. Fix both cases to give a sane error message.

* scripts/qapi.py (check_exprs): Require a valid meta-type for every expression.
* tests/qapi-schema/indented-expr.*: Use valid types.
* tests/qapi-schema/missing-type.*: Update expected output.
* tests/qapi-schema/double-type.*: Likewise.

Conventional git commit message followed by GNU change log. Unusual in QEMU. Same elsewhere.

Explanation of the change to indented-expr.json hides in the change log part, where I promptly missed it :)

Quoted 26 lines
---
 scripts/qapi.py                      | 12 ++++++++++++
 tests/qapi-schema/double-type.err    |  1 +
 tests/qapi-schema/double-type.exit   |  2 +-
 tests/qapi-schema/double-type.out    |  3 ---
 tests/qapi-schema/indented-expr.json |  4 ++--
 tests/qapi-schema/indented-expr.out  |  2 +-
 tests/qapi-schema/missing-type.err   |  1 +
 tests/qapi-schema/missing-type.exit  |  2 +-
 tests/qapi-schema/missing-type.out   |  3 ---
 9 files changed, 19 insertions(+), 11 deletions(-)
scripts/qapi.py +5 -0
index 1082416..910e422 100644
@@ -322,6 +322,18 @@ def check_exprs(schema):
         info = expr_elem['info']
         members = expr.get('data')
 
+        # 'include' has already been flattened; at this point, all exprs
+        # should have one of the remaining keys
+        keys = (expr.has_key('enum') + expr.has_key('union') +
+                expr.has_key('type') + expr.has_key('command') +
+                expr.has_key('event'))

scripts/qapi.py def check_exprs(schema): · line 339

Quoted 10 lines
scripts/qapi.py +7 -0
+        if keys < 1:
+            raise QAPIExprError(info,
+                                "Missing expression meta-type")
+        if keys > 1:
+            raise QAPIExprError(info,
+                                "Conflicting expression meta-types")
+
         if expr.has_key('union'):
             check_union(expr, info)
         if expr.has_key('event'):

Not the friendliest error messages, but they'll do.

author
#10 lore raw

Re: [Qemu-devel] [PATCH v3 06/14] qapi: require valid expressions

Quoted 7 lines · Markus Armbruster
Quoted 5 lines · Eric Blake

The previous patch demonstrated that the generator could get confused if an expression had conflicting meta-types, and silently ignored expressions that lacked a known meta-type. Fix both cases to give a sane error message.

Quoted 8 lines
Quoted 5 lines

* scripts/qapi.py (check_exprs): Require a valid meta-type for every expression.
* tests/qapi-schema/indented-expr.*: Use valid types.
* tests/qapi-schema/missing-type.*: Update expected output.
* tests/qapi-schema/double-type.*: Likewise.

Conventional git commit message followed by GNU change log. Unusual in QEMU. Same elsewhere.

Habits die hard :)

Quoted 3 lines

Explanation of the change to indented-expr.json hides in the change log part, where I promptly missed it :)

Okay, I'll make the subject line capitalized (which is more popular, even if not universal) and ditch the GNU style suffix (inlining any meat into the paragraph form) on my respin.

Quoted 16 lines
Quoted 5 lines
scripts/qapi.py +5 -0
+        # 'include' has already been flattened; at this point, all exprs
+        # should have one of the remaining keys
+        keys = (expr.has_key('enum') + expr.has_key('union') +
+                expr.has_key('type') + expr.has_key('command') +
+                expr.has_key('event'))

scripts/qapi.py def check_exprs(schema): · line 339

Quoted 10 lines
scripts/qapi.py +7 -0
+        if keys < 1:
+            raise QAPIExprError(info,
+                                "Missing expression meta-type")
+        if keys > 1:
+            raise QAPIExprError(info,
+                                "Conflicting expression meta-types")
+
         if expr.has_key('union'):
             check_union(expr, info)
         if expr.has_key('event'):
Quoted 1 line

Not the friendliest error messages, but they'll do.

I'm open to bike-shedding suggestions :) But in the absence of a suggestion, I'll keep this part unchanged in v4.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#11 lore raw

PATCH v3 7/14 qapi: add some type check tests

In reply to Eric Blake

Demonstrate that the qapi generator silently parses confusing types, which may cause other errors later on. Later patches will update the expected results as the generator is made stricter.

* tests/qapi-schema/data-array-empty.*: New files.
* tests/qapi-schema/data-array-unknown.*: Likewise.
* tests/qapi-schema/data-unknown.*: Likewise.
* tests/qapi-schema/data-int.*: Likewise.
* tests/qapi-schema/returns-unknown.*: Likewise.
* tests/qapi-schema/returns-int.*: Likewise.
* tests/qapi-schema/returns-array-bad.*: Likewise.
* tests/Makefile (check-qapi-schema-y): Run them.

---
 tests/Makefile                            | 4 +++-
 tests/qapi-schema/data-array-empty.err    | 0
 tests/qapi-schema/data-array-empty.exit   | 1 +
 tests/qapi-schema/data-array-empty.json   | 1 +
 tests/qapi-schema/data-array-empty.out    | 3 +++
 tests/qapi-schema/data-array-unknown.err  | 0
 tests/qapi-schema/data-array-unknown.exit | 1 +
 tests/qapi-schema/data-array-unknown.json | 1 +
 tests/qapi-schema/data-array-unknown.out  | 3 +++
 tests/qapi-schema/data-int.err            | 0
 tests/qapi-schema/data-int.exit           | 1 +
 tests/qapi-schema/data-int.json           | 1 +
 tests/qapi-schema/data-int.out            | 3 +++
 tests/qapi-schema/data-unknown.err        | 0
 tests/qapi-schema/data-unknown.exit       | 1 +
 tests/qapi-schema/data-unknown.json       | 1 +
 tests/qapi-schema/data-unknown.out        | 3 +++
 tests/qapi-schema/returns-array-bad.err   | 0
 tests/qapi-schema/returns-array-bad.exit  | 1 +
 tests/qapi-schema/returns-array-bad.json  | 1 +
 tests/qapi-schema/returns-array-bad.out   | 3 +++
 tests/qapi-schema/returns-int.err         | 0
 tests/qapi-schema/returns-int.exit        | 1 +
 tests/qapi-schema/returns-int.json        | 1 +
 tests/qapi-schema/returns-int.out         | 3 +++
 tests/qapi-schema/returns-unknown.err     | 0
 tests/qapi-schema/returns-unknown.exit    | 1 +
 tests/qapi-schema/returns-unknown.json    | 1 +
 tests/qapi-schema/returns-unknown.out     | 3 +++
 29 files changed, 38 insertions(+), 1 deletion(-)
 create mode 100644 tests/qapi-schema/data-array-empty.err
 create mode 100644 tests/qapi-schema/data-array-empty.exit
 create mode 100644 tests/qapi-schema/data-array-empty.json
 create mode 100644 tests/qapi-schema/data-array-empty.out
 create mode 100644 tests/qapi-schema/data-array-unknown.err
 create mode 100644 tests/qapi-schema/data-array-unknown.exit
 create mode 100644 tests/qapi-schema/data-array-unknown.json
 create mode 100644 tests/qapi-schema/data-array-unknown.out
 create mode 100644 tests/qapi-schema/data-int.err
 create mode 100644 tests/qapi-schema/data-int.exit
 create mode 100644 tests/qapi-schema/data-int.json
 create mode 100644 tests/qapi-schema/data-int.out
 create mode 100644 tests/qapi-schema/data-unknown.err
 create mode 100644 tests/qapi-schema/data-unknown.exit
 create mode 100644 tests/qapi-schema/data-unknown.json
 create mode 100644 tests/qapi-schema/data-unknown.out
 create mode 100644 tests/qapi-schema/returns-array-bad.err
 create mode 100644 tests/qapi-schema/returns-array-bad.exit
 create mode 100644 tests/qapi-schema/returns-array-bad.json
 create mode 100644 tests/qapi-schema/returns-array-bad.out
 create mode 100644 tests/qapi-schema/returns-int.err
 create mode 100644 tests/qapi-schema/returns-int.exit
 create mode 100644 tests/qapi-schema/returns-int.json
 create mode 100644 tests/qapi-schema/returns-int.out
 create mode 100644 tests/qapi-schema/returns-unknown.err
 create mode 100644 tests/qapi-schema/returns-unknown.exit
 create mode 100644 tests/qapi-schema/returns-unknown.json
 create mode 100644 tests/qapi-schema/returns-unknown.out
tests/Makefile +3 -1
index 1b0c96f..c36faca 100644
@@ -192,7 +192,9 @@ $(foreach target,$(SYSEMU_TARGET_LIST), \
 check-qapi-schema-y := $(addprefix tests/qapi-schema/, \
 	comments.json empty.json enum-empty.json enum-missing-data.json \
 	enum-wrong-data.json funny-char.json indented-expr.json \
-	missing-type.json double-type.json \
+	missing-type.json double-type.json data-array-empty.json \
+	data-array-unknown.json data-unknown.json data-int.json \
+	returns-unknown.json returns-int.json returns-array-bad.json \
 	missing-colon.json missing-comma-list.json \
 	missing-comma-object.json non-objects.json \
 	qapi-schema-test.json quoted-structural-chars.json \
tests/qapi-schema/data-array-empty.err new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/data-array-empty.exit new file +1 -0
new file mode 100644
index 0000000..573541a
@@ -0,0 +1 @@
+0
tests/qapi-schema/data-array-empty.json new file +1 -0
new file mode 100644
index 0000000..00352ac
@@ -0,0 +1 @@
+{ 'command': 'oops', 'data': [ ] }
tests/qapi-schema/data-array-empty.out new file +3 -0
new file mode 100644
index 0000000..67802be
@@ -0,0 +1,3 @@
+[OrderedDict([('command', 'oops'), ('data', [])])]
+[]
+[]
tests/qapi-schema/data-array-unknown.err new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/data-array-unknown.exit new file +1 -0
new file mode 100644
index 0000000..573541a
@@ -0,0 +1 @@
+0
tests/qapi-schema/data-array-unknown.json new file +1 -0
new file mode 100644
index 0000000..03ef321
@@ -0,0 +1 @@
+{ 'command': 'oops', 'data': [ 'NoSuchType' ] }
tests/qapi-schema/data-array-unknown.out new file +3 -0
new file mode 100644
index 0000000..c3bc05e
@@ -0,0 +1,3 @@
+[OrderedDict([('command', 'oops'), ('data', ['NoSuchType'])])]
+[]
+[]
tests/qapi-schema/data-int.err new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/data-int.exit new file +1 -0
new file mode 100644
index 0000000..573541a
@@ -0,0 +1 @@
+0
tests/qapi-schema/data-int.json new file +1 -0
new file mode 100644
index 0000000..926c75e
@@ -0,0 +1 @@
+{ 'command': 'oops', 'data': 'int' }
tests/qapi-schema/data-int.out new file +3 -0
new file mode 100644
index 0000000..e589a4f
@@ -0,0 +1,3 @@
+[OrderedDict([('command', 'oops'), ('data', 'int')])]
+[]
+[]
tests/qapi-schema/data-unknown.err new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/data-unknown.exit new file +1 -0
new file mode 100644
index 0000000..573541a
@@ -0,0 +1 @@
+0
tests/qapi-schema/data-unknown.json new file +1 -0
new file mode 100644
index 0000000..4415584
@@ -0,0 +1 @@
+{ 'command': 'oops', 'data': 'NoSuchType' }
tests/qapi-schema/data-unknown.out new file +3 -0
new file mode 100644
index 0000000..2c60726
@@ -0,0 +1,3 @@
+[OrderedDict([('command', 'oops'), ('data', 'NoSuchType')])]
+[]
+[]
tests/qapi-schema/returns-array-bad.err new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/returns-array-bad.exit new file +1 -0
new file mode 100644
index 0000000..573541a
@@ -0,0 +1 @@
+0
tests/qapi-schema/returns-array-bad.json new file +1 -0
new file mode 100644
index 0000000..76bf6df
@@ -0,0 +1 @@
+{ 'command': 'oops', 'data': [ 'str', 'str' ] }
tests/qapi-schema/returns-array-bad.out new file +3 -0
new file mode 100644
index 0000000..cfc474e
@@ -0,0 +1,3 @@
+[OrderedDict([('command', 'oops'), ('data', ['str', 'str'])])]
+[]
+[]
tests/qapi-schema/returns-int.err new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/returns-int.exit new file +1 -0
new file mode 100644
index 0000000..573541a
@@ -0,0 +1 @@
+0
tests/qapi-schema/returns-int.json new file +1 -0
new file mode 100644
index 0000000..0884968
@@ -0,0 +1 @@
+{ 'command': 'okay', 'returns': 'int' }
tests/qapi-schema/returns-int.out new file +3 -0
new file mode 100644
index 0000000..36b00a9
@@ -0,0 +1,3 @@
+[OrderedDict([('command', 'okay'), ('returns', 'int')])]
+[]
+[]
tests/qapi-schema/returns-unknown.err new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/returns-unknown.exit new file +1 -0
new file mode 100644
index 0000000..573541a
@@ -0,0 +1 @@
+0
tests/qapi-schema/returns-unknown.json new file +1 -0
new file mode 100644
index 0000000..508df3e
@@ -0,0 +1 @@
+{ 'command': 'oops', 'returns': 'NoSuchType' }
tests/qapi-schema/returns-unknown.out new file +3 -0
new file mode 100644
index 0000000..a482c83
@@ -0,0 +1,3 @@
+[OrderedDict([('command', 'oops'), ('returns', 'NoSuchType')])]
+[]
+[]
Signature · 1 line
1.9.3
#12 lore raw

Re: [Qemu-devel] [PATCH v3 07/14] qapi: add some type check tests

Quoted 14 lines · Eric Blake

Demonstrate that the qapi generator silently parses confusing types, which may cause other errors later on. Later patches will update the expected results as the generator is made stricter.

* tests/qapi-schema/data-array-empty.*: New files.
* tests/qapi-schema/data-array-unknown.*: Likewise.
* tests/qapi-schema/data-unknown.*: Likewise.
* tests/qapi-schema/data-int.*: Likewise.
* tests/qapi-schema/returns-unknown.*: Likewise.
* tests/qapi-schema/returns-int.*: Likewise.
* tests/qapi-schema/returns-array-bad.*: Likewise.
* tests/Makefile (check-qapi-schema-y): Run them.

[...]

Quoted 16 lines
tests/qapi-schema/returns-array-bad.err new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/returns-array-bad.exit new file +1 -0
new file mode 100644
index 0000000..573541a
@@ -0,0 +1 @@
+0
tests/qapi-schema/returns-array-bad.json new file +0 -0
new file mode 100644
index 0000000..76bf6df

tests/qapi-schema/returns-array-bad.json line 1

Quoted 1 line
tests/qapi-schema/returns-array-bad.json new file +1 -0
@@ -0,0 +1 @@
+{ 'command': 'oops', 'data': [ 'str', 'str' ] }

The file name suggests you want to test 'returns' rather than 'data' here.

[...]

author
#13 lore raw

Re: [Qemu-devel] [PATCH v3 07/14] qapi: add some type check tests

Quoted 16 lines · Markus Armbruster
Quoted 13 lines · Eric Blake

Demonstrate that the qapi generator silently parses confusing types, which may cause other errors later on. Later patches will update the expected results as the generator is made stricter.

* tests/qapi-schema/data-array-empty.*: New files.
* tests/qapi-schema/data-array-unknown.*: Likewise.
* tests/qapi-schema/data-unknown.*: Likewise.
* tests/qapi-schema/data-int.*: Likewise.
* tests/qapi-schema/returns-unknown.*: Likewise.
* tests/qapi-schema/returns-int.*: Likewise.
* tests/qapi-schema/returns-array-bad.*: Likewise.
* tests/Makefile (check-qapi-schema-y): Run them.

Quoted 5 lines

tests/qapi-schema/returns-array-bad.json line 1

Quoted 3 lines

+++ b/tests/qapi-schema/returns-array-bad.json

tests/qapi-schema/returns-array-bad.json new file +1 -0
@@ -0,0 +1 @@
+{ 'command': 'oops', 'data': [ 'str', 'str' ] }

The file name suggests you want to test 'returns' rather than 'data' here.

D'oh. Definite reason to respin the series.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#14 lore raw

PATCH v3 8/14 qapi: add expr_name() helper

In reply to Eric Blake

Now that we know every expression has a known meta-type, we can add a helper function that retrieves the name of an arbitrary expression, for use in future error messages.

* scripts/qapi.py (expr_name): New function.

---
 scripts/qapi.py | 13 +++++++++++++
 1 file changed, 13 insertions(+)
scripts/qapi.py +13 -0
index 910e422..e02fa0b 100644
@@ -248,6 +248,19 @@ def discriminator_find_enum_define(expr):
 
     return find_enum(discriminator_type)
 
+def expr_name(expr):
+    if expr.has_key('union'):
+        return expr['union']
+    if expr.has_key('type'):
+        return expr['type']
+    if expr.has_key('enum'):
+        return expr['enum']
+    if expr.has_key('command'):
+        return expr['command']
+    if expr.has_key('event'):
+        return expr['event']
+    return None
+
 def check_event(expr, expr_info):
     params = expr.get('data')
     if params:
Signature · 1 line
1.9.3
#15 lore raw

Re: [Qemu-devel] [PATCH v3 08/14] qapi: add expr_name() helper

Quoted 25 lines · Eric Blake

Now that we know every expression has a known meta-type, we can add a helper function that retrieves the name of an arbitrary expression, for use in future error messages.

* scripts/qapi.py (expr_name): New function.

---
 scripts/qapi.py | 13 +++++++++++++
 1 file changed, 13 insertions(+)
scripts/qapi.py +6 -0
index 910e422..e02fa0b 100644
@@ -248,6 +248,19 @@ def discriminator_find_enum_define(expr):
 
     return find_enum(discriminator_type)
 
+def expr_name(expr):
+    if expr.has_key('union'):
+        return expr['union']
+    if expr.has_key('type'):
+        return expr['type']
+    if expr.has_key('enum'):

scripts/qapi.py def discriminator_find_enum_define(expr): · line 266

Quoted 10 lines
scripts/qapi.py +7 -0
+        return expr['enum']
+    if expr.has_key('command'):
+        return expr['command']
+    if expr.has_key('event'):
+        return expr['event']
+    return None
+
 def check_event(expr, expr_info):
     params = expr.get('data')
     if params:

Squash into the next commit?

author
#16 lore raw

PATCH v3 9/14 qapi: add check_type helper function

In reply to Eric Blake

Add a helper function for use in a later patch that will validate that a 'data':... or 'returns':... member of an expression evaluates to a valid type.

* scripts/qapi.py (check_type): New function.
(check_exprs): Use it.

---
 scripts/qapi.py                           | 30 ++++++++++++++++++++++++++++++
 tests/qapi-schema/data-array-unknown.err  |  1 +
 tests/qapi-schema/data-array-unknown.exit |  2 +-
 tests/qapi-schema/data-array-unknown.out  |  3 ---
 tests/qapi-schema/data-int.err            |  1 +
 tests/qapi-schema/data-int.exit           |  2 +-
 tests/qapi-schema/data-int.out            |  3 ---
 tests/qapi-schema/data-unknown.err        |  1 +
 tests/qapi-schema/data-unknown.exit       |  2 +-
 tests/qapi-schema/data-unknown.out        |  3 ---
 tests/qapi-schema/returns-array-bad.err   |  1 +
 tests/qapi-schema/returns-array-bad.exit  |  2 +-
 tests/qapi-schema/returns-array-bad.out   |  3 ---
 tests/qapi-schema/returns-unknown.err     |  1 +
 tests/qapi-schema/returns-unknown.exit    |  2 +-
 tests/qapi-schema/returns-unknown.out     |  3 ---
 16 files changed, 40 insertions(+), 20 deletions(-)
scripts/qapi.py +30 -0
index e02fa0b..e4d27d6 100644
@@ -329,6 +329,32 @@ def check_union(expr, expr_info):
         # Todo: add checking for values. Key is checked as above, value can be
         # also checked here, but we need more functions to handle array case.
 
+def check_type(expr_info, name, data, allow_native = False):
+    if not data:
+        return
+    if isinstance(data, list):
+        if len(data) != 1 or not isinstance(data[0], basestring):
+            raise QAPIExprError(expr_info,
+                                "Use of array type in '%s' must contain "
+                                "single element type name" % name)
+        data = data[0]
+
+    if isinstance(data, basestring):
+        if find_struct(data) or find_enum(data) or find_union(data):
+            return
+        if data == 'str' or data == 'int' or data == 'visitor':
+            if allow_native:
+                return
+            raise QAPIExprError(expr_info,
+                                "Primitive type '%s' not allowed as data "
+                                "of '%s'" % (data, name))
+        raise QAPIExprError(expr_info,
+                            "Unknown type '%s' as data of '%s'"
+                            % (data, name))
+    elif not isinstance(data, OrderedDict):
+        raise QAPIExprError(expr_info,
+                            "Expecting dictionary for data of '%s'" % name)
+
 def check_exprs(schema):
     for expr_elem in schema.exprs:
         expr = expr_elem['expr']
@@ -356,6 +382,10 @@ def check_exprs(schema):
                 raise QAPIExprError(info,
                                     "enum '%s' requires an array for 'data'"
                                     % expr.get('enum'))
+        else:
+            check_type(info, expr_name(expr), members)
+            if expr.has_key('command') and expr.has_key('returns'):
+                check_type(info, expr['command'], expr['returns'], True)
 
 def parse_schema(input_file):
     try:
tests/qapi-schema/data-array-unknown.err +1 -0
index e69de29..e0433b8 100644
@@ -0,0 +1 @@
+tests/qapi-schema/data-array-unknown.json:1: Unknown type 'NoSuchType' as data of 'oops'
tests/qapi-schema/data-array-unknown.exit +1 -1
index 573541a..d00491f 100644
@@ -1 +1 @@
-0
+1
tests/qapi-schema/data-array-unknown.out +0 -3
index c3bc05e..e69de29 100644
@@ -1,3 +0,0 @@
-[OrderedDict([('command', 'oops'), ('data', ['NoSuchType'])])]
-[]
-[]
tests/qapi-schema/data-int.err +1 -0
index e69de29..30bbffc 100644
@@ -0,0 +1 @@
+tests/qapi-schema/data-int.json:1: Primitive type 'int' not allowed as data of 'oops'
tests/qapi-schema/data-int.exit +1 -1
index 573541a..d00491f 100644
@@ -1 +1 @@
-0
+1
tests/qapi-schema/data-int.out +0 -3
index e589a4f..e69de29 100644
@@ -1,3 +0,0 @@
-[OrderedDict([('command', 'oops'), ('data', 'int')])]
-[]
-[]
tests/qapi-schema/data-unknown.err +1 -0
index e69de29..f7fd635 100644
@@ -0,0 +1 @@
+tests/qapi-schema/data-unknown.json:1: Unknown type 'NoSuchType' as data of 'oops'
tests/qapi-schema/data-unknown.exit +1 -1
index 573541a..d00491f 100644
@@ -1 +1 @@
-0
+1
tests/qapi-schema/data-unknown.out +0 -3
index 2c60726..e69de29 100644
@@ -1,3 +0,0 @@
-[OrderedDict([('command', 'oops'), ('data', 'NoSuchType')])]
-[]
-[]
tests/qapi-schema/returns-array-bad.err +1 -0
index e69de29..d2a216f 100644
@@ -0,0 +1 @@
+tests/qapi-schema/returns-array-bad.json:1: Use of array type in 'oops' must contain single element type name
tests/qapi-schema/returns-array-bad.exit +1 -1
index 573541a..d00491f 100644
@@ -1 +1 @@
-0
+1
tests/qapi-schema/returns-array-bad.out +0 -3
index cfc474e..e69de29 100644
@@ -1,3 +0,0 @@
-[OrderedDict([('command', 'oops'), ('data', ['str', 'str'])])]
-[]
-[]
tests/qapi-schema/returns-unknown.err +1 -0
index e69de29..d09fb92 100644
@@ -0,0 +1 @@
+tests/qapi-schema/returns-unknown.json:1: Unknown type 'NoSuchType' as data of 'oops'
tests/qapi-schema/returns-unknown.exit +1 -1
index 573541a..d00491f 100644
@@ -1 +1 @@
-0
+1
tests/qapi-schema/returns-unknown.out +0 -3
index a482c83..e69de29 100644
@@ -1,3 +0,0 @@
-[OrderedDict([('command', 'oops'), ('returns', 'NoSuchType')])]
-[]
-[]
Signature · 1 line
1.9.3
#17 lore raw

Re: [Qemu-devel] [PATCH v3 09/14] qapi: add check_type helper function

Subject suggests you're merely adding a helper function. You're actually fixing bugs. Something like

qapi: More rigorous checking of type expressions

would be clearer.

While I'm nit-picking your subjects: start with a capital letter after the subsystem: prefix.

Quoted 34 lines · Eric Blake

Add a helper function for use in a later patch that will validate that a 'data':... or 'returns':... member of an expression evaluates to a valid type.

* scripts/qapi.py (check_type): New function.
(check_exprs): Use it.

---
 scripts/qapi.py                           | 30 ++++++++++++++++++++++++++++++
 tests/qapi-schema/data-array-unknown.err  |  1 +
 tests/qapi-schema/data-array-unknown.exit |  2 +-
 tests/qapi-schema/data-array-unknown.out  |  3 ---
 tests/qapi-schema/data-int.err            |  1 +
 tests/qapi-schema/data-int.exit           |  2 +-
 tests/qapi-schema/data-int.out            |  3 ---
 tests/qapi-schema/data-unknown.err        |  1 +
 tests/qapi-schema/data-unknown.exit       |  2 +-
 tests/qapi-schema/data-unknown.out        |  3 ---
 tests/qapi-schema/returns-array-bad.err   |  1 +
 tests/qapi-schema/returns-array-bad.exit  |  2 +-
 tests/qapi-schema/returns-array-bad.out   |  3 ---
 tests/qapi-schema/returns-unknown.err     |  1 +
 tests/qapi-schema/returns-unknown.exit    |  2 +-
 tests/qapi-schema/returns-unknown.out     |  3 ---
 16 files changed, 40 insertions(+), 20 deletions(-)
scripts/qapi.py +0 -0
index e02fa0b..e4d27d6 100644
@@ -329,6 +329,32 @@ def check_union(expr, expr_info):
         # Todo: add checking for values. Key is checked as above, value can be
         # also checked here, but we need more functions to handle array case.

scripts/qapi.py def check_union(expr, expr_info): · line 340

Quoted 10 lines
scripts/qapi.py +9 -0
 
+def check_type(expr_info, name, data, allow_native = False):
+    if not data:
+        return
+    if isinstance(data, list):
+        if len(data) != 1 or not isinstance(data[0], basestring):
+            raise QAPIExprError(expr_info,
+                                "Use of array type in '%s' must contain "
+                                "single element type name" % name)
+        data = data[0]

Peeling off the array here means error messages below won't mention it. Visible in data-array-unknown.err below. But good enough is good enough.

scripts/qapi.py def check_union(expr, expr_info): · line 345

Quoted 5 lines
scripts/qapi.py +5 -0
+
+    if isinstance(data, basestring):
+        if find_struct(data) or find_enum(data) or find_union(data):
+            return
+        if data == 'str' or data == 'int' or data == 'visitor':

Is this complete? Should we be checking against builtin_types[]?

Pardon my ignorance: where does 'visitor' come from?

scripts/qapi.py def check_union(expr, expr_info): · line 353

Quoted 8 lines
scripts/qapi.py +8 -0
+            if allow_native:
+                return
+            raise QAPIExprError(expr_info,
+                                "Primitive type '%s' not allowed as data "
+                                "of '%s'" % (data, name))
+        raise QAPIExprError(expr_info,
+                            "Unknown type '%s' as data of '%s'"
+                            % (data, name))

"as data of" suggests the problem is in member 'data', even when it's actually in member 'returns'. Visible in returns-unknown.err below.

scripts/qapi.py def check_exprs(schema): · line 385

Quoted 12 lines
scripts/qapi.py +5 -0
+    elif not isinstance(data, OrderedDict):
+        raise QAPIExprError(expr_info,
+                            "Expecting dictionary for data of '%s'" % name)
+
 def check_exprs(schema):
     for expr_elem in schema.exprs:
         expr = expr_elem['expr']
@@ -356,6 +382,10 @@ def check_exprs(schema):
                 raise QAPIExprError(info,
                                     "enum '%s' requires an array for 'data'"
                                     % expr.get('enum'))
+        else:

This is for 'type' and 'command', right?

scripts/qapi.py def check_exprs(schema): · line 391

Quoted 6 lines
scripts/qapi.py +3 -0
+            check_type(info, expr_name(expr), members)
+            if expr.has_key('command') and expr.has_key('returns'):
+                check_type(info, expr['command'], expr['returns'], True)
 
 def parse_schema(input_file):
     try:

Looks pretty good, but I didn't check systematically against qapi-code-gen.txt for correctness and completeness. We can always improve on top.

Quoted 105 lines
tests/qapi-schema/data-array-unknown.err +1 -0
index e69de29..e0433b8 100644
@@ -0,0 +1 @@
+tests/qapi-schema/data-array-unknown.json:1: Unknown type 'NoSuchType' as data of 'oops'
tests/qapi-schema/data-array-unknown.exit +1 -1
index 573541a..d00491f 100644
@@ -1 +1 @@
-0
+1
tests/qapi-schema/data-array-unknown.out +0 -3
index c3bc05e..e69de29 100644
@@ -1,3 +0,0 @@
-[OrderedDict([('command', 'oops'), ('data', ['NoSuchType'])])]
-[]
-[]
tests/qapi-schema/data-int.err +1 -0
index e69de29..30bbffc 100644
@@ -0,0 +1 @@
+tests/qapi-schema/data-int.json:1: Primitive type 'int' not allowed as data of 'oops'
tests/qapi-schema/data-int.exit +1 -1
index 573541a..d00491f 100644
@@ -1 +1 @@
-0
+1
tests/qapi-schema/data-int.out +0 -3
index e589a4f..e69de29 100644
@@ -1,3 +0,0 @@
-[OrderedDict([('command', 'oops'), ('data', 'int')])]
-[]
-[]
tests/qapi-schema/data-unknown.err +1 -0
index e69de29..f7fd635 100644
@@ -0,0 +1 @@
+tests/qapi-schema/data-unknown.json:1: Unknown type 'NoSuchType' as data of 'oops'
tests/qapi-schema/data-unknown.exit +1 -1
index 573541a..d00491f 100644
@@ -1 +1 @@
-0
+1
tests/qapi-schema/data-unknown.out +0 -3
index 2c60726..e69de29 100644
@@ -1,3 +0,0 @@
-[OrderedDict([('command', 'oops'), ('data', 'NoSuchType')])]
-[]
-[]
tests/qapi-schema/returns-array-bad.err +1 -0
index e69de29..d2a216f 100644
@@ -0,0 +1 @@
+tests/qapi-schema/returns-array-bad.json:1: Use of array type in 'oops' must contain single element type name
tests/qapi-schema/returns-array-bad.exit +1 -1
index 573541a..d00491f 100644
@@ -1 +1 @@
-0
+1
tests/qapi-schema/returns-array-bad.out +0 -3
index cfc474e..e69de29 100644
@@ -1,3 +0,0 @@
-[OrderedDict([('command', 'oops'), ('data', ['str', 'str'])])]
-[]
-[]
tests/qapi-schema/returns-unknown.err +1 -0
index e69de29..d09fb92 100644
@@ -0,0 +1 @@
+tests/qapi-schema/returns-unknown.json:1: Unknown type 'NoSuchType' as data of 'oops'
tests/qapi-schema/returns-unknown.exit +1 -1
index 573541a..d00491f 100644
@@ -1 +1 @@
-0
+1
tests/qapi-schema/returns-unknown.out +0 -3
index a482c83..e69de29 100644
@@ -1,3 +0,0 @@
-[OrderedDict([('command', 'oops'), ('returns', 'NoSuchType')])]
-[]
-[]
author
#18 lore raw

Re: [Qemu-devel] [PATCH v3 09/14] qapi: add check_type helper function

Quoted 6 lines · Markus Armbruster

Subject suggests you're merely adding a helper function. You're actually fixing bugs. Something like

qapi: More rigorous checking of type expressions

would be clearer.

Along with squashing in 8/14, you are correct about this fixing bugs (serves me right for refactoring, then realizing that we needed more tests, then fixing the tests without checking that the subject line stayed appropriate).

Quoted 19 lines
Quoted 4 lines

+++ b/scripts/qapi.py

scripts/qapi.py +0 -0
@@ -329,6 +329,32 @@ def check_union(expr, expr_info):
         # Todo: add checking for values. Key is checked as above, value can be
         # also checked here, but we need more functions to handle array case.

scripts/qapi.py def check_union(expr, expr_info): · line 340

Quoted 10 lines
scripts/qapi.py +9 -0
 
+def check_type(expr_info, name, data, allow_native = False):
+    if not data:
+        return
+    if isinstance(data, list):
+        if len(data) != 1 or not isinstance(data[0], basestring):
+            raise QAPIExprError(expr_info,
+                                "Use of array type in '%s' must contain "
+                                "single element type name" % name)
+        data = data[0]

Peeling off the array here means error messages below won't mention it. Visible in data-array-unknown.err below. But good enough is good enough.

Quoted 7 lines

scripts/qapi.py def check_union(expr, expr_info): · line 345

Quoted 5 lines
scripts/qapi.py +5 -0
+
+    if isinstance(data, basestring):
+        if find_struct(data) or find_enum(data) or find_union(data):
+            return
+        if data == 'str' or data == 'int' or data == 'visitor':

Is this complete? Should we be checking against builtin_types[]?

It was complete insofar that it passes with the current qapi-schema.json. Checking builtin_types[] would indeed be nicer (I didn't know that existed).

Quoted 2 lines

Pardon my ignorance: where does 'visitor' come from?

qom-set, qom-get. Nasty commands that aren't typesafe, and which explicitly use 'gen':'no' to abuse the qapi system by bypassing the code generator while still exposing a backdoor command that can break the rules.

netdev_add ('*props':'**') and object-add ('*props':'dict') are the only other 'gen':'no' clients; I'm not sure why they didn't cause the parser to barf the way 'visitor' did. Maybe my approach should instead be to unify all four 'gen':'no' expressions to use the same syntax of "**" to represent the fact that the QMP code is not type-safe, as well as actually documenting this (ab)use of ** (the fact that none of the documentation mentions 'gen' is telling). Looks like I've just added a pre-req patch into my v4 :)

Quoted 10 lines

scripts/qapi.py def check_union(expr, expr_info): · line 353

Quoted 9 lines
scripts/qapi.py +8 -0
+            if allow_native:
+                return
+            raise QAPIExprError(expr_info,
+                                "Primitive type '%s' not allowed as data "
+                                "of '%s'" % (data, name))
+        raise QAPIExprError(expr_info,
+                            "Unknown type '%s' as data of '%s'"
+                            % (data, name))
Quoted 2 lines

"as data of" suggests the problem is in member 'data', even when it's actually in member 'returns'. Visible in returns-unknown.err below.

The function is declared as:

Quoted 1 line

scripts/qapi.py def check_union(expr, expr_info): · line 332

Quoted 1 line
scripts/qapi.py +1 -0
+def check_type(expr_info, name, data, allow_native = False):

but allow_native is True only for the 'returns' case. Maybe I should just repurpose that parameter for what it really is - 'data' vs. 'returns', and use it for improving the error message in addition to deciding whether native types are allowed.

Quoted 15 lines

scripts/qapi.py def check_exprs(schema): · line 385

Quoted 13 lines
scripts/qapi.py +5 -0
+    elif not isinstance(data, OrderedDict):
+        raise QAPIExprError(expr_info,
+                            "Expecting dictionary for data of '%s'" % name)
+
 def check_exprs(schema):
     for expr_elem in schema.exprs:
         expr = expr_elem['expr']
@@ -356,6 +382,10 @@ def check_exprs(schema):
                 raise QAPIExprError(info,
                                     "enum '%s' requires an array for 'data'"
                                     % expr.get('enum'))
+        else:

This is for 'type' and 'command', right?

'type', 'union', 'event', and 'command' ('data' is a dict for those four, and an array for the fifth meta-type of 'enum'). I'll add a comment.

Quoted 11 lines

scripts/qapi.py def check_exprs(schema): · line 391

Quoted 7 lines
scripts/qapi.py +3 -0
+            check_type(info, expr_name(expr), members)
+            if expr.has_key('command') and expr.has_key('returns'):
+                check_type(info, expr['command'], expr['returns'], True)
 
 def parse_schema(input_file):
     try:

Looks pretty good, but I didn't check systematically against qapi-code-gen.txt for correctness and completeness. We can always improve on top.

I'll double-check things as well, when posting v4.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#19 lore raw

Re: [Qemu-devel] [PATCH v3 09/14] qapi: add check_type helper function

Quoted 53 lines · Eric Blake
Quoted 6 lines · Markus Armbruster

Subject suggests you're merely adding a helper function. You're actually fixing bugs. Something like

qapi: More rigorous checking of type expressions

would be clearer.

Along with squashing in 8/14, you are correct about this fixing bugs (serves me right for refactoring, then realizing that we needed more tests, then fixing the tests without checking that the subject line stayed appropriate).

Quoted 21 lines
Quoted 7 lines

+++ b/scripts/qapi.py

(quoted diff) +0 -0
@@ -329,6 +329,32 @@ def check_union(expr, expr_info):
         # Todo: add checking for values. Key is checked as above,

value can be

         # also checked here, but we need more functions to handle
array case.

scripts/qapi.py def check_union(expr, expr_info): · line 340

Quoted 9 lines
scripts/qapi.py +9 -0
+def check_type(expr_info, name, data, allow_native = False):
+    if not data:
+        return
+    if isinstance(data, list):
+        if len(data) != 1 or not isinstance(data[0], basestring):
+            raise QAPIExprError(expr_info,
+                                "Use of array type in '%s' must contain "
+                                "single element type name" % name)
+        data = data[0]

Peeling off the array here means error messages below won't mention it. Visible in data-array-unknown.err below. But good enough is good enough.

Quoted 7 lines

scripts/qapi.py def check_union(expr, expr_info): · line 345

Quoted 5 lines
scripts/qapi.py +5 -0
+
+    if isinstance(data, basestring):
+        if find_struct(data) or find_enum(data) or find_union(data):
+            return
+        if data == 'str' or data == 'int' or data == 'visitor':

Is this complete? Should we be checking against builtin_types[]?

It was complete insofar that it passes with the current qapi-schema.json. Checking builtin_types[] would indeed be nicer (I didn't know that existed).

Quoted 2 lines

Pardon my ignorance: where does 'visitor' come from?

qom-set, qom-get. Nasty commands that aren't typesafe, and which explicitly use 'gen':'no' to abuse the qapi system by bypassing the code generator while still exposing a backdoor command that can break the rules.

Quoted 8 lines

netdev_add ('*props':'**') and object-add ('*props':'dict') are the only other 'gen':'no' clients; I'm not sure why they didn't cause the parser to barf the way 'visitor' did. Maybe my approach should instead be to unify all four 'gen':'no' expressions to use the same syntax of "**" to represent the fact that the QMP code is not type-safe, as well as actually documenting this (ab)use of ** (the fact that none of the documentation mentions 'gen' is telling). Looks like I've just added a pre-req patch into my v4 :)

Documentation for 'gen': 'no', '**', and 'visitor' is most welcome.

I wonder why 'no', and not simply false. Hmm, looks like the code generator ignores the value. 'gen': 'sure, why not, if you feel like it'.

Should 'visitor' be in builtin_types[]?

Quoted 12 lines
Quoted 11 lines

scripts/qapi.py def check_union(expr, expr_info): · line 353

Quoted 8 lines
scripts/qapi.py +8 -0
+            if allow_native:
+                return
+            raise QAPIExprError(expr_info,
+                                "Primitive type '%s' not allowed as data "
+                                "of '%s'" % (data, name))
+        raise QAPIExprError(expr_info,
+                            "Unknown type '%s' as data of '%s'"
+                            % (data, name))

"as data of" suggests the problem is in member 'data', even when it's actually in member 'returns'. Visible in returns-unknown.err below.

Quoted 8 lines

The function is declared as:

Quoted 1 line
Quoted 1 line

+def check_type(expr_info, name, data, allow_native = False):

but allow_native is True only for the 'returns' case. Maybe I should just repurpose that parameter for what it really is - 'data' vs. 'returns', and use it for improving the error message in addition to deciding whether native types are allowed.

Yeah.

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
scripts/qapi.py +4 -0
+    elif not isinstance(data, OrderedDict):
+        raise QAPIExprError(expr_info,
+                            "Expecting dictionary for data of '%s'" % name)
+
 def check_exprs(schema):
     for expr_elem in schema.exprs:
Quoted 8 lines

scripts/qapi.py def check_exprs(schema): · line 385

Quoted 6 lines

expr = expr_elem['expr']

scripts/qapi.py +1 -0
@@ -356,6 +382,10 @@ def check_exprs(schema):
                 raise QAPIExprError(info,
                                     "enum '%s' requires an array for 'data'"
                                     % expr.get('enum'))
+        else:

This is for 'type' and 'command', right?

Quoted 2 lines

'type', 'union', 'event', and 'command' ('data' is a dict for those four, and an array for the fifth meta-type of 'enum'). I'll add a comment.

You're right. I misread the sequence of if statements as one big if... elif... elif... else statement.

A blank line before the if... else would probably do.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
scripts/qapi.py +3 -0
+            check_type(info, expr_name(expr), members)
+            if expr.has_key('command') and expr.has_key('returns'):
+                check_type(info, expr['command'], expr['returns'], True)
 
Quoted 8 lines
Quoted 6 lines

scripts/qapi.py def check_exprs(schema): · line 391

Quoted 2 lines
scripts/qapi.py +0 -0
 def parse_schema(input_file):
     try:

Looks pretty good, but I didn't check systematically against qapi-code-gen.txt for correctness and completeness. We can always improve on top.

I'll double-check things as well, when posting v4.

Thanks!

author
#20 lore raw

PATCH v3 10/14 qapi: merge UserDefTwo and UserDefNested in tests

In reply to Eric Blake

In the testsuite, UserDefTwo and UserDefNested were identical types other than the member names. Reduce code duplication by having just one type, and choose names that also favor reuse. This will also make it easier for a later patch to get rid of nested inline subtypes in QAPI.

Ensure that 'make check-qapi-schema check-unit' still passes.

* tests/qapi-schema/qapi-schema-test.json (UserDefNested): Drop. (UserDefTwo): Rename element members for clarity.
* tests/qapi-schema/qapi-schema-test.out: Update expected output.
* tests/test-qmp-output-visitor.c (test_visitor_out_struct_nested, test_visitor_out_list_qapi_free):
Update clients.
* tests/test-qmp-input-visitor.c (test_visitor_in_struct_nested):
Likewise.
* tests/test-qmp-input-strict.c (test_validate_struct_nested) (test_validate_fail_struct_nested): Likewise.
* tests/test-qmp-commands.c (qmp_user_def_cmd2) (test_dealloc_partial, test_dispatch_cmd_io): Likewise.
* tests/test-visitor-serialization.c (nested_struct_create) (nested_struct_compare, nested_struct_cleanup) (visit_nested_struct, visit_nested_struct_list) (test_nested_struct, test_nested_struct_list): Likewise.

---
 tests/qapi-schema/qapi-schema-test.json | 10 +----
 tests/qapi-schema/qapi-schema-test.out  |  6 +--
 tests/test-qmp-commands.c               | 32 +++++++--------
 tests/test-qmp-input-strict.c           | 19 +++++----
 tests/test-qmp-input-visitor.c          | 19 +++++----
 tests/test-qmp-output-visitor.c         | 50 +++++++++++------------
 tests/test-visitor-serialization.c      | 71 +++++++++++++++++----------------
 7 files changed, 103 insertions(+), 104 deletions(-)
tests/qapi-schema/qapi-schema-test.json +2 -8
index ab4d3d9..2e13f09 100644
@@ -15,16 +15,10 @@
   'data': { 'string': 'str', '*enum1': 'EnumOne' } }
 
 { 'type': 'UserDefTwo',
-  'data': { 'string': 'str',
-            'dict': { 'string': 'str',
-                      'dict': { 'userdef': 'UserDefOne', 'string': 'str' },
-                      '*dict2': { 'userdef': 'UserDefOne', 'string': 'str' } } } }
-
-{ 'type': 'UserDefNested',
   'data': { 'string0': 'str',
             'dict1': { 'string1': 'str',
-                       'dict2': { 'userdef1': 'UserDefOne', 'string2': 'str' },
-                       '*dict3': { 'userdef2': 'UserDefOne', 'string3': 'str' } } } }
+                       'dict2': { 'userdef': 'UserDefOne', 'string': 'str' },
+                       '*dict3': { 'userdef': 'UserDefOne', 'string': 'str' } } } }
 
 # for testing unions
 { 'type': 'UserDefA',
tests/qapi-schema/qapi-schema-test.out +2 -4
index 95e9899..f94bbcc 100644
@@ -2,8 +2,7 @@
  OrderedDict([('type', 'NestedEnumsOne'), ('data', OrderedDict([('enum1', 'EnumOne'), ('*enum2', 'EnumOne'), ('enum3', 'EnumOne'), ('*enum4', 'EnumOne')]))]),
  OrderedDict([('type', 'UserDefZero'), ('data', OrderedDict([('integer', 'int')]))]),
  OrderedDict([('type', 'UserDefOne'), ('base', 'UserDefZero'), ('data', OrderedDict([('string', 'str'), ('*enum1', 'EnumOne')]))]),
- OrderedDict([('type', 'UserDefTwo'), ('data', OrderedDict([('string', 'str'), ('dict', OrderedDict([('string', 'str'), ('dict', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')])), ('*dict2', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')]))]))]))]),
- OrderedDict([('type', 'UserDefNested'), ('data', OrderedDict([('string0', 'str'), ('dict1', OrderedDict([('string1', 'str'), ('dict2', OrderedDict([('userdef1', 'UserDefOne'), ('string2', 'str')])), ('*dict3', OrderedDict([('userdef2', 'UserDefOne'), ('string3', 'str')]))]))]))]),
+ OrderedDict([('type', 'UserDefTwo'), ('data', OrderedDict([('string0', 'str'), ('dict1', OrderedDict([('string1', 'str'), ('dict2', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')])), ('*dict3', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')]))]))]))]),
  OrderedDict([('type', 'UserDefA'), ('data', OrderedDict([('boolean', 'bool')]))]),
  OrderedDict([('type', 'UserDefB'), ('data', OrderedDict([('integer', 'int')]))]),
  OrderedDict([('union', 'UserDefUnion'), ('base', 'UserDefZero'), ('data', OrderedDict([('a', 'UserDefA'), ('b', 'UserDefB')]))]),
@@ -28,8 +27,7 @@
 [OrderedDict([('type', 'NestedEnumsOne'), ('data', OrderedDict([('enum1', 'EnumOne'), ('*enum2', 'EnumOne'), ('enum3', 'EnumOne'), ('*enum4', 'EnumOne')]))]),
  OrderedDict([('type', 'UserDefZero'), ('data', OrderedDict([('integer', 'int')]))]),
  OrderedDict([('type', 'UserDefOne'), ('base', 'UserDefZero'), ('data', OrderedDict([('string', 'str'), ('*enum1', 'EnumOne')]))]),
- OrderedDict([('type', 'UserDefTwo'), ('data', OrderedDict([('string', 'str'), ('dict', OrderedDict([('string', 'str'), ('dict', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')])), ('*dict2', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')]))]))]))]),
- OrderedDict([('type', 'UserDefNested'), ('data', OrderedDict([('string0', 'str'), ('dict1', OrderedDict([('string1', 'str'), ('dict2', OrderedDict([('userdef1', 'UserDefOne'), ('string2', 'str')])), ('*dict3', OrderedDict([('userdef2', 'UserDefOne'), ('string3', 'str')]))]))]))]),
+ OrderedDict([('type', 'UserDefTwo'), ('data', OrderedDict([('string0', 'str'), ('dict1', OrderedDict([('string1', 'str'), ('dict2', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')])), ('*dict3', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')]))]))]))]),
  OrderedDict([('type', 'UserDefA'), ('data', OrderedDict([('boolean', 'bool')]))]),
  OrderedDict([('type', 'UserDefB'), ('data', OrderedDict([('integer', 'int')]))]),
  OrderedDict([('type', 'UserDefUnionBase'), ('data', OrderedDict([('string', 'str'), ('enum1', 'EnumOne')]))]),
tests/test-qmp-commands.c +16 -16
index 554e222..9189cd2 100644
@@ -32,13 +32,13 @@ UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a,
     ud1d->base->integer = has_udb1 ? ud1b->base->integer : 0;
 
     ret = g_malloc0(sizeof(UserDefTwo));
-    ret->string = strdup("blah1");
-    ret->dict.string = strdup("blah2");
-    ret->dict.dict.userdef = ud1c;
-    ret->dict.dict.string = strdup("blah3");
-    ret->dict.has_dict2 = true;
-    ret->dict.dict2.userdef = ud1d;
-    ret->dict.dict2.string = strdup("blah4");
+    ret->string0 = strdup("blah1");
+    ret->dict1.string1 = strdup("blah2");
+    ret->dict1.dict2.userdef = ud1c;
+    ret->dict1.dict2.string = strdup("blah3");
+    ret->dict1.has_dict3 = true;
+    ret->dict1.dict3.userdef = ud1d;
+    ret->dict1.dict3.string = strdup("blah4");
 
     return ret;
 }
@@ -120,15 +120,15 @@ static void test_dispatch_cmd_io(void)
 
     ret = qobject_to_qdict(test_qmp_dispatch(req));
 
-    assert(!strcmp(qdict_get_str(ret, "string"), "blah1"));
-    ret_dict = qdict_get_qdict(ret, "dict");
-    assert(!strcmp(qdict_get_str(ret_dict, "string"), "blah2"));
-    ret_dict_dict = qdict_get_qdict(ret_dict, "dict");
+    assert(!strcmp(qdict_get_str(ret, "string0"), "blah1"));
+    ret_dict = qdict_get_qdict(ret, "dict1");
+    assert(!strcmp(qdict_get_str(ret_dict, "string1"), "blah2"));
+    ret_dict_dict = qdict_get_qdict(ret_dict, "dict2");
     ret_dict_dict_userdef = qdict_get_qdict(ret_dict_dict, "userdef");
     assert(qdict_get_int(ret_dict_dict_userdef, "integer") == 42);
     assert(!strcmp(qdict_get_str(ret_dict_dict_userdef, "string"), "hello"));
     assert(!strcmp(qdict_get_str(ret_dict_dict, "string"), "blah3"));
-    ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict2");
+    ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict3");
     ret_dict_dict2_userdef = qdict_get_qdict(ret_dict_dict2, "userdef");
     assert(qdict_get_int(ret_dict_dict2_userdef, "integer") == 422);
     assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef, "string"), "hello2"));
@@ -192,7 +192,7 @@ static void test_dealloc_partial(void)
         QmpInputVisitor *qiv;
 
         ud2_dict = qdict_new();
-        qdict_put_obj(ud2_dict, "string", QOBJECT(qstring_from_str(text)));
+        qdict_put_obj(ud2_dict, "string0", QOBJECT(qstring_from_str(text)));
 
         qiv = qmp_input_visitor_new(QOBJECT(ud2_dict));
         visit_type_UserDefTwo(qmp_input_get_visitor(qiv), &ud2, NULL, &err);
@@ -202,9 +202,9 @@ static void test_dealloc_partial(void)
 
     /* verify partial success */
     assert(ud2 != NULL);
-    assert(ud2->string != NULL);
-    assert(strcmp(ud2->string, text) == 0);
-    assert(ud2->dict.dict.userdef == NULL);
+    assert(ud2->string0 != NULL);
+    assert(strcmp(ud2->string0, text) == 0);
+    assert(ud2->dict1.dict2.userdef == NULL);
 
     /* confirm & release construction error */
     assert(err != NULL);
tests/test-qmp-input-strict.c +11 -8
index 0f77003..29d4f82 100644
@@ -1,7 +1,7 @@
 /*
  * QMP Input Visitor unit-tests (strict mode).
  *
- * Copyright (C) 2011-2012 Red Hat Inc.
+ * Copyright (C) 2011-2012, 2014 Red Hat Inc.
  *
  * Authors:
  *  Luiz Capitulino <[email protected]>
@@ -116,15 +116,18 @@ static void test_validate_struct(TestInputVisitorData *data,
 static void test_validate_struct_nested(TestInputVisitorData *data,
                                          const void *unused)
 {
-    UserDefNested *udp = NULL;
+    UserDefTwo *udp = NULL;
     Error *err = NULL;
     Visitor *v;
 
-    v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
+    v = validate_test_init(data, "{ 'string0': 'string0', "
+                           "'dict1': { 'string1': 'string1', "
+                           "'dict2': { 'userdef': { 'integer': 42, "
+                           "'string': 'string' }, 'string': 'string2'}}}");
 
-    visit_type_UserDefNested(v, &udp, NULL, &err);
+    visit_type_UserDefTwo(v, &udp, NULL, &err);
     g_assert(!err);
-    qapi_free_UserDefNested(udp);
+    qapi_free_UserDefTwo(udp);
 }
 
 static void test_validate_list(TestInputVisitorData *data,
@@ -207,15 +210,15 @@ static void test_validate_fail_struct(TestInputVisitorData *data,
 static void test_validate_fail_struct_nested(TestInputVisitorData *data,
                                               const void *unused)
 {
-    UserDefNested *udp = NULL;
+    UserDefTwo *udp = NULL;
     Error *err = NULL;
     Visitor *v;
 
     v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
 
-    visit_type_UserDefNested(v, &udp, NULL, &err);
+    visit_type_UserDefTwo(v, &udp, NULL, &err);
     g_assert(err);
-    qapi_free_UserDefNested(udp);
+    qapi_free_UserDefTwo(udp);
 }
 
 static void test_validate_fail_list(TestInputVisitorData *data,
tests/test-qmp-input-visitor.c +11 -8
index 1c8e872..3e1da9d 100644
@@ -1,7 +1,7 @@
 /*
  * QMP Input Visitor unit-tests.
  *
- * Copyright (C) 2011 Red Hat Inc.
+ * Copyright (C) 2011, 2014 Red Hat Inc.
  *
  * Authors:
  *  Luiz Capitulino <[email protected]>
@@ -248,23 +248,26 @@ static void check_and_free_str(char *str, const char *cmp)
 static void test_visitor_in_struct_nested(TestInputVisitorData *data,
                                           const void *unused)
 {
-    UserDefNested *udp = NULL;
+    UserDefTwo *udp = NULL;
     Error *err = NULL;
     Visitor *v;
 
-    v = visitor_input_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
+    v = visitor_input_test_init(data, "{ 'string0': 'string0', "
+                                "'dict1': { 'string1': 'string1', "
+                                "'dict2': { 'userdef': { 'integer': 42, "
+                                "'string': 'string' }, 'string': 'string2'}}}");
 
-    visit_type_UserDefNested(v, &udp, NULL, &err);
+    visit_type_UserDefTwo(v, &udp, NULL, &err);
     g_assert(!err);
 
     check_and_free_str(udp->string0, "string0");
     check_and_free_str(udp->dict1.string1, "string1");
-    g_assert_cmpint(udp->dict1.dict2.userdef1->base->integer, ==, 42);
-    check_and_free_str(udp->dict1.dict2.userdef1->string, "string");
-    check_and_free_str(udp->dict1.dict2.string2, "string2");
+    g_assert_cmpint(udp->dict1.dict2.userdef->base->integer, ==, 42);
+    check_and_free_str(udp->dict1.dict2.userdef->string, "string");
+    check_and_free_str(udp->dict1.dict2.string, "string2");
     g_assert(udp->dict1.has_dict3 == false);
 
-    g_free(udp->dict1.dict2.userdef1);
+    g_free(udp->dict1.dict2.userdef);
     g_free(udp);
 }
 
tests/test-qmp-output-visitor.c +25 -25

106 lines not on this page · raw message

tests/test-visitor-serialization.c +36 -35

127 lines not on this page · raw message

Signature · 1 line
1.9.3
author
#21 lore raw

PATCH v3 11/14 qapi: drop tests for inline subtypes

In reply to Eric Blake

A future patch will be using a 'name':{dictionary} entry in the QAPI schema to specify a default value for an optional argument; but existing use of inline substructs conflicts with that goal. This patch fixes the testsuite to avoid nested subtypes, by breaking the nesting into explicit types.

* tests/qapi-schema/qapi-schema-test.json: Flatten UserDefTwo.
* tests/qapi-schema/qapi-schema-test.out: Update expected output.
* tests/test-qmp-output-visitor.c (test_visitor_out_struct_nested) (test_visitor_out_list_qapi_free): Update clients.
* tests/test-qmp-input-visitor.c (test_visitor_in_struct_nested):
Likewise.
* tests/test-qmp-commands.c (qmp_user_def_cmd2) (test_dealloc_partial): Likewise.
* tests/test-visitor-serialization.c (nested_struct_create) (nested_struct_compare): Likewise.

---
 tests/qapi-schema/qapi-schema-test.json | 12 ++++++--
 tests/qapi-schema/qapi-schema-test.out  |  8 +++--
 tests/test-qmp-commands.c               | 17 ++++++-----
 tests/test-qmp-input-visitor.c          | 14 +++++----
 tests/test-qmp-output-visitor.c         | 44 +++++++++++++++------------
 tests/test-visitor-serialization.c      | 53 ++++++++++++++++++---------------
 6 files changed, 87 insertions(+), 61 deletions(-)
tests/qapi-schema/qapi-schema-test.json +9 -3
index 2e13f09..1f922c1 100644
@@ -14,11 +14,17 @@
   'base': 'UserDefZero',
   'data': { 'string': 'str', '*enum1': 'EnumOne' } }
 
+{ 'type': 'UserDefTwoDictDict',
+  'data': { 'userdef': 'UserDefOne', 'string': 'str' } }
+
+{ 'type': 'UserDefTwoDict',
+  'data': { 'string1': 'str',
+            'dict2': 'UserDefTwoDictDict',
+            '*dict3': 'UserDefTwoDictDict' } }
+
 { 'type': 'UserDefTwo',
   'data': { 'string0': 'str',
-            'dict1': { 'string1': 'str',
-                       'dict2': { 'userdef': 'UserDefOne', 'string': 'str' },
-                       '*dict3': { 'userdef': 'UserDefOne', 'string': 'str' } } } }
+            'dict1': 'UserDefTwoDict' } }
 
 # for testing unions
 { 'type': 'UserDefA',
tests/qapi-schema/qapi-schema-test.out +6 -2
index f94bbcc..1d7b53c 100644
@@ -2,7 +2,9 @@
  OrderedDict([('type', 'NestedEnumsOne'), ('data', OrderedDict([('enum1', 'EnumOne'), ('*enum2', 'EnumOne'), ('enum3', 'EnumOne'), ('*enum4', 'EnumOne')]))]),
  OrderedDict([('type', 'UserDefZero'), ('data', OrderedDict([('integer', 'int')]))]),
  OrderedDict([('type', 'UserDefOne'), ('base', 'UserDefZero'), ('data', OrderedDict([('string', 'str'), ('*enum1', 'EnumOne')]))]),
- OrderedDict([('type', 'UserDefTwo'), ('data', OrderedDict([('string0', 'str'), ('dict1', OrderedDict([('string1', 'str'), ('dict2', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')])), ('*dict3', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')]))]))]))]),
+ OrderedDict([('type', 'UserDefTwoDictDict'), ('data', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')]))]),
+ OrderedDict([('type', 'UserDefTwoDict'), ('data', OrderedDict([('string1', 'str'), ('dict2', 'UserDefTwoDictDict'), ('*dict3', 'UserDefTwoDictDict')]))]),
+ OrderedDict([('type', 'UserDefTwo'), ('data', OrderedDict([('string0', 'str'), ('dict1', 'UserDefTwoDict')]))]),
  OrderedDict([('type', 'UserDefA'), ('data', OrderedDict([('boolean', 'bool')]))]),
  OrderedDict([('type', 'UserDefB'), ('data', OrderedDict([('integer', 'int')]))]),
  OrderedDict([('union', 'UserDefUnion'), ('base', 'UserDefZero'), ('data', OrderedDict([('a', 'UserDefA'), ('b', 'UserDefB')]))]),
@@ -27,7 +29,9 @@
 [OrderedDict([('type', 'NestedEnumsOne'), ('data', OrderedDict([('enum1', 'EnumOne'), ('*enum2', 'EnumOne'), ('enum3', 'EnumOne'), ('*enum4', 'EnumOne')]))]),
  OrderedDict([('type', 'UserDefZero'), ('data', OrderedDict([('integer', 'int')]))]),
  OrderedDict([('type', 'UserDefOne'), ('base', 'UserDefZero'), ('data', OrderedDict([('string', 'str'), ('*enum1', 'EnumOne')]))]),
- OrderedDict([('type', 'UserDefTwo'), ('data', OrderedDict([('string0', 'str'), ('dict1', OrderedDict([('string1', 'str'), ('dict2', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')])), ('*dict3', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')]))]))]))]),
+ OrderedDict([('type', 'UserDefTwoDictDict'), ('data', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')]))]),
+ OrderedDict([('type', 'UserDefTwoDict'), ('data', OrderedDict([('string1', 'str'), ('dict2', 'UserDefTwoDictDict'), ('*dict3', 'UserDefTwoDictDict')]))]),
+ OrderedDict([('type', 'UserDefTwo'), ('data', OrderedDict([('string0', 'str'), ('dict1', 'UserDefTwoDict')]))]),
  OrderedDict([('type', 'UserDefA'), ('data', OrderedDict([('boolean', 'bool')]))]),
  OrderedDict([('type', 'UserDefB'), ('data', OrderedDict([('integer', 'int')]))]),
  OrderedDict([('type', 'UserDefUnionBase'), ('data', OrderedDict([('string', 'str'), ('enum1', 'EnumOne')]))]),
tests/test-qmp-commands.c +10 -7
index 9189cd2..dc199d3 100644
@@ -33,12 +33,15 @@ UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a,
 
     ret = g_malloc0(sizeof(UserDefTwo));
     ret->string0 = strdup("blah1");
-    ret->dict1.string1 = strdup("blah2");
-    ret->dict1.dict2.userdef = ud1c;
-    ret->dict1.dict2.string = strdup("blah3");
-    ret->dict1.has_dict3 = true;
-    ret->dict1.dict3.userdef = ud1d;
-    ret->dict1.dict3.string = strdup("blah4");
+    ret->dict1 = g_malloc0(sizeof(UserDefTwoDict));
+    ret->dict1->string1 = strdup("blah2");
+    ret->dict1->dict2 = g_malloc0(sizeof(UserDefTwoDictDict));
+    ret->dict1->dict2->userdef = ud1c;
+    ret->dict1->dict2->string = strdup("blah3");
+    ret->dict1->dict3 = g_malloc0(sizeof(UserDefTwoDictDict));
+    ret->dict1->has_dict3 = true;
+    ret->dict1->dict3->userdef = ud1d;
+    ret->dict1->dict3->string = strdup("blah4");
 
     return ret;
 }
@@ -204,7 +207,7 @@ static void test_dealloc_partial(void)
     assert(ud2 != NULL);
     assert(ud2->string0 != NULL);
     assert(strcmp(ud2->string0, text) == 0);
-    assert(ud2->dict1.dict2.userdef == NULL);
+    assert(ud2->dict1 == NULL);
 
     /* confirm & release construction error */
     assert(err != NULL);
tests/test-qmp-input-visitor.c +8 -6
index 3e1da9d..30a5441 100644
@@ -261,13 +261,15 @@ static void test_visitor_in_struct_nested(TestInputVisitorData *data,
     g_assert(!err);
 
     check_and_free_str(udp->string0, "string0");
-    check_and_free_str(udp->dict1.string1, "string1");
-    g_assert_cmpint(udp->dict1.dict2.userdef->base->integer, ==, 42);
-    check_and_free_str(udp->dict1.dict2.userdef->string, "string");
-    check_and_free_str(udp->dict1.dict2.string, "string2");
-    g_assert(udp->dict1.has_dict3 == false);
+    check_and_free_str(udp->dict1->string1, "string1");
+    g_assert_cmpint(udp->dict1->dict2->userdef->base->integer, ==, 42);
+    check_and_free_str(udp->dict1->dict2->userdef->string, "string");
+    check_and_free_str(udp->dict1->dict2->string, "string2");
+    g_assert(udp->dict1->has_dict3 == false);
 
-    g_free(udp->dict1.dict2.userdef);
+    g_free(udp->dict1->dict2->userdef);
+    g_free(udp->dict1->dict2);
+    g_free(udp->dict1);
     g_free(udp);
 }
 
tests/test-qmp-output-visitor.c +25 -19

57 lines not on this page · raw message

tests/test-visitor-serialization.c +29 -24

65 lines not on this page · raw message

Signature · 1 line
1.9.3
author
#22 lore raw

PATCH v3 12/14 qapi: drop inline subtype in query-version

In reply to Eric Blake

A future patch will be using a 'name':{dictionary} entry in the QAPI schema to specify a default value for an optional argument; but existing use of inline substructs conflicts with that goal. This patch fixes one of only two commands relying on nested subtypes, by breaking the nesting into an explicit type.

Prefer the safer g_new0() while making the conversion.

* qapi/common.json (VersionInfo): Split inline data... (VersionTriple): ...into new type.
* qmp.c (qmp_query_version): Update caller.
* hmp.c (hmp_info_version): Likewise.

---
 hmp.c            |  2 +-
 qapi/common.json | 26 +++++++++++++++++++-------
 qmp.c            |  9 +++++----
 3 files changed, 25 insertions(+), 12 deletions(-)
hmp.c +1 -1
index 4d1838e..e637fbc 100644
@@ -55,7 +55,7 @@ void hmp_info_version(Monitor *mon, const QDict *qdict)
     info = qmp_query_version(NULL);
 
     monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
-                   info->qemu.major, info->qemu.minor, info->qemu.micro,
+                   info->qemu->major, info->qemu->minor, info->qemu->micro,
                    info->package);
 
     qapi_free_VersionInfo(info);
qapi/common.json +19 -7
index 4e9a21f..d007095 100644
@@ -29,15 +29,28 @@
             'DeviceNotActive', 'DeviceNotFound', 'KVMMissingCap' ] }
 
 ##
+# @VersionTriple
+#
+# A three-part version number.
+#
+# @qemu.major:  The major version number.
+#
+# @qemu.minor:  The minor version number.
+#
+# @qemu.micro:  The micro version number.
+#
+# Since: 2.2
+##
+{ 'type': 'VersionTriple',
+  'data': {'major': 'int', 'minor': 'int', 'micro': 'int'} }
+
+
+##
 # @VersionInfo:
 #
 # A description of QEMU's version.
 #
-# @qemu.major:  The major version of QEMU
-#
-# @qemu.minor:  The minor version of QEMU
-#
-# @qemu.micro:  The micro version of QEMU.  By current convention, a micro
+# @qemu:        The version of QEMU.  By current convention, a micro
 #               version of 50 signifies a development branch.  A micro version
 #               greater than or equal to 90 signifies a release candidate for
 #               the next minor version.  A micro version of less than 50
@@ -51,8 +64,7 @@
 # Since: 0.14.0
 ##
 { 'type': 'VersionInfo',
-  'data': {'qemu': {'major': 'int', 'minor': 'int', 'micro': 'int'},
-           'package': 'str'} }
+  'data': {'qemu': 'VersionTriple', 'package': 'str'} }
 
 ##
 # @query-version:
qmp.c +5 -4
index 0d2553a..35e6f7a 100644
@@ -45,15 +45,16 @@ NameInfo *qmp_query_name(Error **errp)
 
 VersionInfo *qmp_query_version(Error **errp)
 {
-    VersionInfo *info = g_malloc0(sizeof(*info));
+    VersionInfo *info = g_new0(VersionInfo, 1);
     const char *version = QEMU_VERSION;
     char *tmp;
 
-    info->qemu.major = strtol(version, &tmp, 10);
+    info->qemu = g_new0(VersionTriple, 1);
+    info->qemu->major = strtol(version, &tmp, 10);
     tmp++;
-    info->qemu.minor = strtol(tmp, &tmp, 10);
+    info->qemu->minor = strtol(tmp, &tmp, 10);
     tmp++;
-    info->qemu.micro = strtol(tmp, &tmp, 10);
+    info->qemu->micro = strtol(tmp, &tmp, 10);
     info->package = g_strdup(QEMU_PKGVERSION);
 
     return info;
Signature · 1 line
1.9.3
#23 lore raw

Re: [Qemu-devel] [PATCH v3 12/14] qapi: drop inline subtype in query-version

Quoted 25 lines · Eric Blake

A future patch will be using a 'name':{dictionary} entry in the QAPI schema to specify a default value for an optional argument; but existing use of inline substructs conflicts with that goal. This patch fixes one of only two commands relying on nested subtypes, by breaking the nesting into an explicit type.

Prefer the safer g_new0() while making the conversion.

* qapi/common.json (VersionInfo): Split inline data... (VersionTriple): ...into new type.
* qmp.c (qmp_query_version): Update caller.
* hmp.c (hmp_info_version): Likewise.

---
 hmp.c            |  2 +-
 qapi/common.json | 26 +++++++++++++++++++-------
 qmp.c            |  9 +++++----
 3 files changed, 25 insertions(+), 12 deletions(-)
hmp.c +0 -0
index 4d1838e..e637fbc 100644

hmp.c void hmp_info_version(Monitor *mon, const QDict *qdict) · line 61

Quoted 8 lines
hmp.c +1 -1
@@ -55,7 +55,7 @@ void hmp_info_version(Monitor *mon, const QDict *qdict)
     info = qmp_query_version(NULL);
 
     monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
-                   info->qemu.major, info->qemu.minor, info->qemu.micro,
+                   info->qemu->major, info->qemu->minor, info->qemu->micro,
                    info->package);
 
     qapi_free_VersionInfo(info);

This demonstrates the difference between anonymous and named complex member types: anonymous means unboxed, named means boxed. Already visible in the previous patch, but it's easier to see here (less noise).

I'm no friend of excessive boxing in C, and QAPI code generator is too boxing-happy for my taste already. However, I like tying boxed vs. unboxed to named vs. anonymous even less.

Quoted 53 lines
qapi/common.json +19 -7
index 4e9a21f..d007095 100644
@@ -29,15 +29,28 @@
             'DeviceNotActive', 'DeviceNotFound', 'KVMMissingCap' ] }
 
 ##
+# @VersionTriple
+#
+# A three-part version number.
+#
+# @qemu.major:  The major version number.
+#
+# @qemu.minor:  The minor version number.
+#
+# @qemu.micro:  The micro version number.
+#
+# Since: 2.2
+##
+{ 'type': 'VersionTriple',
+  'data': {'major': 'int', 'minor': 'int', 'micro': 'int'} }
+
+
+##
 # @VersionInfo:
 #
 # A description of QEMU's version.
 #
-# @qemu.major:  The major version of QEMU
-#
-# @qemu.minor:  The minor version of QEMU
-#
-# @qemu.micro:  The micro version of QEMU.  By current convention, a micro
+# @qemu:        The version of QEMU.  By current convention, a micro
 #               version of 50 signifies a development branch.  A micro version
 #               greater than or equal to 90 signifies a release candidate for
 #               the next minor version.  A micro version of less than 50
@@ -51,8 +64,7 @@
 # Since: 0.14.0
 ##
 { 'type': 'VersionInfo',
-  'data': {'qemu': {'major': 'int', 'minor': 'int', 'micro': 'int'},
-           'package': 'str'} }
+  'data': {'qemu': 'VersionTriple', 'package': 'str'} }
 
 ##
 # @query-version:
qmp.c +0 -0
index 0d2553a..35e6f7a 100644

qmp.c NameInfo *qmp_query_name(Error **errp) · line 48

Quoted 5 lines
qmp.c +1 -1
@@ -45,15 +45,16 @@ NameInfo *qmp_query_name(Error **errp)
 
 VersionInfo *qmp_query_version(Error **errp)
 {
-    VersionInfo *info = g_malloc0(sizeof(*info));
+    VersionInfo *info = g_new0(VersionInfo, 1);

Unrelated improvement. I figure you want it for consistency with the g_new0() you add. Okay.

Quoted 15 lines
qmp.c +4 -3
     const char *version = QEMU_VERSION;
     char *tmp;
 
-    info->qemu.major = strtol(version, &tmp, 10);
+    info->qemu = g_new0(VersionTriple, 1);
+    info->qemu->major = strtol(version, &tmp, 10);
     tmp++;
-    info->qemu.minor = strtol(tmp, &tmp, 10);
+    info->qemu->minor = strtol(tmp, &tmp, 10);
     tmp++;
-    info->qemu.micro = strtol(tmp, &tmp, 10);
+    info->qemu->micro = strtol(tmp, &tmp, 10);
     info->package = g_strdup(QEMU_PKGVERSION);
 
     return info;
author
#24 lore raw

PATCH v3 13/14 qapi: drop inline subtype in query-pci

In reply to Eric Blake

A future patch will be using a 'name':{dictionary} entry in the QAPI schema to specify a default value for an optional argument; but existing use of inline substructs conflicts with that goal. This patch fixes one of only two commands relying on nested subtypes, by breaking the nesting into an explicit type.

Prefer the safer g_new0() while making the conversion, and reduce some long lines.

* qapi-schema.json (PciBridgeInfo, PciDeviceInfo): Split inline data...
(PciBusInfo, PciDeviceClass, PciDeviceId): ...into new types.
* hmp.c (hmp_info_pci_device): Update caller.
* hw/pci/pci.c (qmp_query_pci_bridge, qmp_query_pci_device):
Likewise.

---
 hmp.c            | 26 ++++++++--------
 hw/pci/pci.c     | 42 ++++++++++++++------------
 qapi-schema.json | 90 ++++++++++++++++++++++++++++++++++++++------------------
 3 files changed, 98 insertions(+), 60 deletions(-)
hmp.c +13 -13
index e637fbc..00085b2 100644
@@ -553,14 +553,14 @@ static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
                    dev->slot, dev->function);
     monitor_printf(mon, "    ");
 
-    if (dev->class_info.has_desc) {
-        monitor_printf(mon, "%s", dev->class_info.desc);
+    if (dev->class_info->has_desc) {
+        monitor_printf(mon, "%s", dev->class_info->desc);
     } else {
-        monitor_printf(mon, "Class %04" PRId64, dev->class_info.q_class);
+        monitor_printf(mon, "Class %04" PRId64, dev->class_info->q_class);
     }
 
     monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
-                   dev->id.vendor, dev->id.device);
+                   dev->id->vendor, dev->id->device);
 
     if (dev->has_irq) {
         monitor_printf(mon, "      IRQ %" PRId64 ".\n", dev->irq);
@@ -568,25 +568,25 @@ static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
 
     if (dev->has_pci_bridge) {
         monitor_printf(mon, "      BUS %" PRId64 ".\n",
-                       dev->pci_bridge->bus.number);
+                       dev->pci_bridge->bus->number);
         monitor_printf(mon, "      secondary bus %" PRId64 ".\n",
-                       dev->pci_bridge->bus.secondary);
+                       dev->pci_bridge->bus->secondary);
         monitor_printf(mon, "      subordinate bus %" PRId64 ".\n",
-                       dev->pci_bridge->bus.subordinate);
+                       dev->pci_bridge->bus->subordinate);
 
         monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
-                       dev->pci_bridge->bus.io_range->base,
-                       dev->pci_bridge->bus.io_range->limit);
+                       dev->pci_bridge->bus->io_range->base,
+                       dev->pci_bridge->bus->io_range->limit);
 
         monitor_printf(mon,
                        "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
-                       dev->pci_bridge->bus.memory_range->base,
-                       dev->pci_bridge->bus.memory_range->limit);
+                       dev->pci_bridge->bus->memory_range->base,
+                       dev->pci_bridge->bus->memory_range->limit);
 
         monitor_printf(mon, "      prefetchable memory range "
                        "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
-                       dev->pci_bridge->bus.prefetchable_range->base,
-                       dev->pci_bridge->bus.prefetchable_range->limit);
+                       dev->pci_bridge->bus->prefetchable_range->base,
+                       dev->pci_bridge->bus->prefetchable_range->limit);
     }
 
     for (region = dev->regions; region; region = region->next) {
hw/pci/pci.c +23 -19

68 lines not on this page · raw message

qapi-schema.json +62 -28

119 lines not on this page · raw message

Signature · 1 line
1.9.3
author
#25 lore raw

PATCH v3 14/14 qapi: drop support for inline subtypes

In reply to Eric Blake

A future patch will be using a 'name':{dictionary} entry in the QAPI schema to specify a default value for an optional argument; but existing use of inline substructs conflicts with that goal. Now that all commands have been fixed to avoid inline substructs, nuke support for them, and turn it into a hard error.

* scripts/qapi.py (check_type): Enforce no nested dicts up front. (parse_args): Drop structured support.
(check_event): Drop; no longer needed.
* scripts/qapi-types.py (generate_struct_fields): Adjust caller.
* scripts/qapi-visit.py (generate_visit_struct_fields): Likewise.
* scripts/qapi-event.py (_generate_event_api_name) (generate_event_implement): Likewise.
* scripts/qapi-commands.py (generate_command_decl) (gen_sync_call, gen_visitor_input_vars_decl) (gen_visitor_input_block): Likewise.
* tests/qapi-schema/event-nest-struct.err: Update expected result.
* tests/qapi-schema/nested-struct.*: New files.
* tests/qapi-schema/nested-struct-returns.*: Likewise.
* tests/Makefile (check-qapi-schema-y): Run them.

---
 scripts/qapi-commands.py                     |  8 +++---
 scripts/qapi-event.py                        |  4 +--
 scripts/qapi-types.py                        |  9 ++-----
 scripts/qapi-visit.py                        | 37 ++++------------------------
 scripts/qapi.py                              | 29 ++++++++++------------
 tests/Makefile                               |  3 ++-
 tests/qapi-schema/event-nest-struct.err      |  2 +-
 tests/qapi-schema/nested-struct-data.err     |  1 +
 tests/qapi-schema/nested-struct-data.exit    |  1 +
 tests/qapi-schema/nested-struct-data.json    |  3 +++
 tests/qapi-schema/nested-struct-data.out     |  0
 tests/qapi-schema/nested-struct-returns.err  |  1 +
 tests/qapi-schema/nested-struct-returns.exit |  1 +
 tests/qapi-schema/nested-struct-returns.json |  2 ++
 tests/qapi-schema/nested-struct-returns.out  |  0
 15 files changed, 38 insertions(+), 63 deletions(-)
 create mode 100644 tests/qapi-schema/nested-struct-data.err
 create mode 100644 tests/qapi-schema/nested-struct-data.exit
 create mode 100644 tests/qapi-schema/nested-struct-data.json
 create mode 100644 tests/qapi-schema/nested-struct-data.out
 create mode 100644 tests/qapi-schema/nested-struct-returns.err
 create mode 100644 tests/qapi-schema/nested-struct-returns.exit
 create mode 100644 tests/qapi-schema/nested-struct-returns.json
 create mode 100644 tests/qapi-schema/nested-struct-returns.out
scripts/qapi-commands.py +4 -4
index 053ba85..db81044 100644
@@ -28,7 +28,7 @@ def type_visitor(name):
 
 def generate_command_decl(name, args, ret_type):
     arglist=""
-    for argname, argtype, optional, structured in parse_args(args):
+    for argname, argtype, optional in parse_args(args):
         argtype = c_type(argtype, is_param=True)
         if optional:
             arglist += "bool has_%s, " % c_var(argname)
@@ -53,7 +53,7 @@ def gen_sync_call(name, args, ret_type, indent=0):
     retval=""
     if ret_type:
         retval = "retval = "
-    for argname, argtype, optional, structured in parse_args(args):
+    for argname, argtype, optional in parse_args(args):
         if optional:
             arglist += "has_%s, " % c_var(argname)
         arglist += "%s, " % (c_var(argname))
@@ -96,7 +96,7 @@ Visitor *v;
 def gen_visitor_input_vars_decl(args):
     ret = ""
     push_indent()
-    for argname, argtype, optional, structured in parse_args(args):
+    for argname, argtype, optional in parse_args(args):
         if optional:
             ret += mcgen('''
 bool has_%(argname)s = false;
@@ -139,7 +139,7 @@ v = qapi_dealloc_get_visitor(md);
 v = qmp_input_get_visitor(mi);
 ''')
 
-    for argname, argtype, optional, structured in parse_args(args):
+    for argname, argtype, optional in parse_args(args):
         if optional:
             ret += mcgen('''
 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
scripts/qapi-event.py +2 -2
index 601e307..47dc041 100644
@@ -21,7 +21,7 @@ def _generate_event_api_name(event_name, params):
     l = len(api_name)
 
     if params:
-        for argname, argentry, optional, structured in parse_args(params):
+        for argname, argentry, optional in parse_args(params):
             if optional:
                 api_name += "bool has_%s,\n" % c_var(argname)
                 api_name += "".ljust(l)
@@ -93,7 +93,7 @@ def generate_event_implement(api_name, event_name, params):
 """,
                 event_name = event_name)
 
-        for argname, argentry, optional, structured in parse_args(params):
+        for argname, argentry, optional in parse_args(params):
             if optional:
                 ret += mcgen("""
     if (has_%(var)s) {
scripts/qapi-types.py +2 -7
index b463232..5d0e6e7 100644
@@ -63,18 +63,13 @@ typedef struct %(name)sList
 def generate_struct_fields(members):
     ret = ''
 
-    for argname, argentry, optional, structured in parse_args(members):
+    for argname, argentry, optional in parse_args(members):
         if optional:
             ret += mcgen('''
     bool has_%(c_name)s;
 ''',
                          c_name=c_var(argname))
-        if structured:
-            push_indent()
-            ret += generate_struct({ "field": argname, "data": argentry})
-            pop_indent()
-        else:
-            ret += mcgen('''
+        ret += mcgen('''
     %(c_type)s %(c_name)s;
 ''',
                      c_type=c_type(argentry), c_name=c_var(argname))
scripts/qapi-visit.py +5 -32
index c129697..7674dd5 100644
@@ -51,27 +51,6 @@ def generate_visit_struct_fields(name, field_prefix, fn_prefix, members, base =
     else:
         full_name = "%s_%s" % (name, fn_prefix)
 
-    for argname, argentry, optional, structured in parse_args(members):
-        if structured:
-            if not fn_prefix:
-                nested_fn_prefix = argname
-            else:
-                nested_fn_prefix = "%s_%s" % (fn_prefix, argname)
-
-            nested_field_prefix = "%s%s." % (field_prefix, argname)
-            ret += generate_visit_struct_fields(name, nested_field_prefix,
-                                                nested_fn_prefix, argentry)
-            ret += mcgen('''
-
-static void visit_type_%(full_name)s_field_%(c_name)s(Visitor *m, %(name)s **obj, Error **errp)
-{
-''',
-                         name=name, full_name=full_name, c_name=c_var(argname))
-            ret += generate_visit_struct_body(full_name, argname, argentry)
-            ret += mcgen('''
-}
-''')
-
     if base:
         ret += generate_visit_implicit_struct(base)
 
@@ -94,7 +73,7 @@ if (err) {
                      c_prefix=c_var(field_prefix),
                      type=type_name(base), c_name=c_var('base'))
 
-    for argname, argentry, optional, structured in parse_args(members):
+    for argname, argentry, optional in parse_args(members):
         if optional:
             ret += mcgen('''
 visit_optional(m, &(*obj)->%(c_prefix)shas_%(c_name)s, "%(name)s", &err);
@@ -104,18 +83,12 @@ if (!err && (*obj)->%(prefix)shas_%(c_name)s) {
                          c_name=c_var(argname), name=argname)
             push_indent()
 
-        if structured:
-            ret += mcgen('''
-visit_type_%(full_name)s_field_%(c_name)s(m, obj, &err);
-''',
-                         full_name=full_name, c_name=c_var(argname))
-        else:
-            ret += mcgen('''
+        ret += mcgen('''
 visit_type_%(type)s(m, &(*obj)->%(c_prefix)s%(c_name)s, "%(name)s", &err);
 ''',
-                         c_prefix=c_var(field_prefix), prefix=field_prefix,
-                         type=type_name(argentry), c_name=c_var(argname),
-                         name=argname)
+                     c_prefix=c_var(field_prefix), prefix=field_prefix,
+                     type=type_name(argentry), c_name=c_var(argname),
+                     name=argname)
 
         if optional:
             pop_indent()
scripts/qapi.py +13 -16
index e4d27d6..7fec348 100644
@@ -261,16 +261,6 @@ def expr_name(expr):
         return expr['event']
     return None
 
-def check_event(expr, expr_info):
-    params = expr.get('data')
-    if params:
-        for argname, argentry, optional, structured in parse_args(params):
-            if structured:
-                raise QAPIExprError(expr_info,
-                                    "Nested structure define in event is not "
-                                    "supported, event '%s', argname '%s'"
-                                    % (expr['event'], argname))
-
 def check_union(expr, expr_info):
     name = expr['union']
     base = expr.get('base')
@@ -354,6 +344,14 @@ def check_type(expr_info, name, data, allow_native = False):
     elif not isinstance(data, OrderedDict):
         raise QAPIExprError(expr_info,
                             "Expecting dictionary for data of '%s'" % name)
+    else:
+        for (key, value) in data.items():
+            # Todo: allow dictionaries to represent default values of
+            # an optional argument.
+            if isinstance(value, OrderedDict):
+                raise QAPIExprError(expr_info,
+                                    "Nested structure not supported for field "
+                                    "'%s' of '%s'" % (key, name))
 
 def check_exprs(schema):
     for expr_elem in schema.exprs:
@@ -375,8 +373,8 @@ def check_exprs(schema):
 
         if expr.has_key('union'):
             check_union(expr, info)
-        if expr.has_key('event'):
-            check_event(expr, info)
+        # 'enum' must have 'data':[]; all other expressions have optional
+        # 'data'; if present it is either a typename or a dict
         if expr.has_key('enum'):
             if not isinstance(members, list):
                 raise QAPIExprError(info,
@@ -431,13 +429,12 @@ def parse_args(typeinfo):
         argname = member
         argentry = typeinfo[member]
         optional = False
-        structured = False
         if member.startswith('*'):
             argname = member[1:]
             optional = True
-        if isinstance(argentry, OrderedDict):
-            structured = True
-        yield (argname, argentry, optional, structured)
+        # Todo: allow argentry to be OrderedDict, for providing the
+        # value of an optional argument.
+        yield (argname, argentry, optional)
 
 def de_camel_case(name):
     new_name = ''
tests/Makefile +2 -1
index c36faca..cf60e23 100644
@@ -196,7 +196,8 @@ check-qapi-schema-y := $(addprefix tests/qapi-schema/, \
 	data-array-unknown.json data-unknown.json data-int.json \
 	returns-unknown.json returns-int.json returns-array-bad.json \
 	missing-colon.json missing-comma-list.json \
-	missing-comma-object.json non-objects.json \
+	missing-comma-object.json nested-struct-data.json \
+	nested-struct-returns.json non-objects.json \
 	qapi-schema-test.json quoted-structural-chars.json \
 	trailing-comma-list.json trailing-comma-object.json \
 	unclosed-list.json unclosed-object.json unclosed-string.json \
tests/qapi-schema/event-nest-struct.err +1 -1
index 91bde1c..3c0fc2c 100644
@@ -1 +1 @@
-tests/qapi-schema/event-nest-struct.json:1: Nested structure define in event is not supported, event 'EVENT_A', argname 'a'
+tests/qapi-schema/event-nest-struct.json:1: Nested structure not supported for field 'a' of 'EVENT_A'
tests/qapi-schema/nested-struct-data.err new file +1 -0
new file mode 100644
index 0000000..5d384ad
@@ -0,0 +1 @@
+tests/qapi-schema/nested-struct-data.json:1: Nested structure not supported for field 'a' of 'foo'
tests/qapi-schema/nested-struct-data.exit new file +1 -0
new file mode 100644
index 0000000..d00491f
@@ -0,0 +1 @@
+1
tests/qapi-schema/nested-struct-data.json new file +3 -0
new file mode 100644
index 0000000..a18fc17
@@ -0,0 +1,3 @@
+{ 'command': 'foo',
+  'data': { 'a' : { 'string' : 'str', 'integer': 'int' }, 'b' : 'str' },
+  'returns': {} }
tests/qapi-schema/nested-struct-data.out new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/nested-struct-returns.err new file +1 -0
new file mode 100644
index 0000000..ce5a675
@@ -0,0 +1 @@
+tests/qapi-schema/nested-struct-returns.json:1: Nested structure not supported for field 'a' of 'foo'
tests/qapi-schema/nested-struct-returns.exit new file +1 -0
new file mode 100644
index 0000000..d00491f
@@ -0,0 +1 @@
+1
tests/qapi-schema/nested-struct-returns.json new file +2 -0
new file mode 100644
index 0000000..f7ddf80
@@ -0,0 +1,2 @@
+{ 'command': 'foo',
+  'returns': { 'a' : { 'string' : 'str', 'integer': 'int' }, 'b' : 'str' } }
tests/qapi-schema/nested-struct-returns.out new file +0 -0
new file mode 100644
index 0000000..e69de29
Signature · 1 line
1.9.3
author
#26 lore raw
In reply to Eric Blake

ping

Quoted 169 lines · Eric Blake

According to this email:
https://lists.gnu.org/archive/html/qemu-devel/2014-05/msg00708.html
we want to repurpose 'data': { 'name': {dict...} } in qapi files for future use of designating default values of optional parameters. But to do that, we must first nuke existing use of that syntax for declaring nested structs. Enhancing the testsuite while at it never hurts.

v3:
No code changes, but fix mis-send of 11-14 [Peter]

v2:
New patches: 1-2, 5-9
consistent TAB usage in Makefile [Fam]
catch more bad coding constructs, and test them
avoid code duplication in type validity checks (patch 14 [former 7] is simpler because of patch 9)

Eric Blake (14):
qapi: consistent whitespace in tests/Makefile
qapi: ignore files created during make check
qapi: add some enum tests
qapi: better error message for bad enum
qapi: add some expr tests
qapi: require valid expressions
qapi: add some type check tests
qapi: add expr_name() helper
qapi: add check_type helper function
qapi: merge UserDefTwo and UserDefNested in tests
qapi: drop tests for inline subtypes
qapi: drop inline subtype in query-version
qapi: drop inline subtype in query-pci
qapi: drop support for inline subtypes

 hmp.c                                        | 28 ++++----
 hw/pci/pci.c                                 | 42 ++++++------
 qapi-schema.json                             | 90 ++++++++++++++++++--------
 qapi/common.json                             | 26 ++++++--
 qmp.c                                        |  9 +--
 scripts/qapi-commands.py                     |  8 +--
 scripts/qapi-event.py                        |  4 +-
 scripts/qapi-types.py                        |  9 +--
 scripts/qapi-visit.py                        | 37 ++---------
 scripts/qapi.py                              | 96 ++++++++++++++++++++++------
 tests/.gitignore                             |  3 +
 tests/Makefile                               | 37 ++++++-----
 tests/qapi-schema/data-array-empty.err       |  0
 tests/qapi-schema/data-array-empty.exit      |  1 +
 tests/qapi-schema/data-array-empty.json      |  1 +
 tests/qapi-schema/data-array-empty.out       |  3 +
 tests/qapi-schema/data-array-unknown.err     |  1 +
 tests/qapi-schema/data-array-unknown.exit    |  1 +
 tests/qapi-schema/data-array-unknown.json    |  1 +
 tests/qapi-schema/data-array-unknown.out     |  0
 tests/qapi-schema/data-int.err               |  1 +
 tests/qapi-schema/data-int.exit              |  1 +
 tests/qapi-schema/data-int.json              |  1 +
 tests/qapi-schema/data-int.out               |  0
 tests/qapi-schema/data-unknown.err           |  1 +
 tests/qapi-schema/data-unknown.exit          |  1 +
 tests/qapi-schema/data-unknown.json          |  1 +
 tests/qapi-schema/data-unknown.out           |  0
 tests/qapi-schema/double-type.err            |  1 +
 tests/qapi-schema/double-type.exit           |  1 +
 tests/qapi-schema/double-type.json           |  1 +
 tests/qapi-schema/double-type.out            |  0
 tests/qapi-schema/enum-empty.err             |  0
 tests/qapi-schema/enum-empty.exit            |  1 +
 tests/qapi-schema/enum-empty.json            |  1 +
 tests/qapi-schema/enum-empty.out             |  3 +
 tests/qapi-schema/enum-missing-data.err      |  1 +
 tests/qapi-schema/enum-missing-data.exit     |  1 +
 tests/qapi-schema/enum-missing-data.json     |  1 +
 tests/qapi-schema/enum-missing-data.out      |  0
 tests/qapi-schema/enum-wrong-data.err        |  1 +
 tests/qapi-schema/enum-wrong-data.exit       |  1 +
 tests/qapi-schema/enum-wrong-data.json       |  1 +
 tests/qapi-schema/enum-wrong-data.out        |  0
 tests/qapi-schema/event-nest-struct.err      |  2 +-
 tests/qapi-schema/indented-expr.json         |  4 +-
 tests/qapi-schema/indented-expr.out          |  2 +-
 tests/qapi-schema/missing-type.err           |  1 +
 tests/qapi-schema/missing-type.exit          |  1 +
 tests/qapi-schema/missing-type.json          |  1 +
 tests/qapi-schema/missing-type.out           |  0
 tests/qapi-schema/nested-struct-data.err     |  1 +
 tests/qapi-schema/nested-struct-data.exit    |  1 +
 tests/qapi-schema/nested-struct-data.json    |  3 +
 tests/qapi-schema/nested-struct-data.out     |  0
 tests/qapi-schema/nested-struct-returns.err  |  1 +
 tests/qapi-schema/nested-struct-returns.exit |  1 +
 tests/qapi-schema/nested-struct-returns.json |  2 +
 tests/qapi-schema/nested-struct-returns.out  |  0
 tests/qapi-schema/qapi-schema-test.json      | 18 +++---
 tests/qapi-schema/qapi-schema-test.out       | 10 +--
 tests/qapi-schema/returns-array-bad.err      |  1 +
 tests/qapi-schema/returns-array-bad.exit     |  1 +
 tests/qapi-schema/returns-array-bad.json     |  1 +
 tests/qapi-schema/returns-array-bad.out      |  0
 tests/qapi-schema/returns-int.err            |  0
 tests/qapi-schema/returns-int.exit           |  1 +
 tests/qapi-schema/returns-int.json           |  1 +
 tests/qapi-schema/returns-int.out            |  3 +
 tests/qapi-schema/returns-unknown.err        |  1 +
 tests/qapi-schema/returns-unknown.exit       |  1 +
 tests/qapi-schema/returns-unknown.json       |  1 +
 tests/qapi-schema/returns-unknown.out        |  0
 tests/test-qmp-commands.c                    | 35 +++++-----
 tests/test-qmp-input-strict.c                | 19 +++---
 tests/test-qmp-input-visitor.c               | 25 +++++---
 tests/test-qmp-output-visitor.c              | 64 ++++++++++---------
 tests/test-visitor-serialization.c           | 84 +++++++++++++-----------
 78 files changed, 433 insertions(+), 270 deletions(-)
 create mode 100644 tests/qapi-schema/data-array-empty.err
 create mode 100644 tests/qapi-schema/data-array-empty.exit
 create mode 100644 tests/qapi-schema/data-array-empty.json
 create mode 100644 tests/qapi-schema/data-array-empty.out
 create mode 100644 tests/qapi-schema/data-array-unknown.err
 create mode 100644 tests/qapi-schema/data-array-unknown.exit
 create mode 100644 tests/qapi-schema/data-array-unknown.json
 create mode 100644 tests/qapi-schema/data-array-unknown.out
 create mode 100644 tests/qapi-schema/data-int.err
 create mode 100644 tests/qapi-schema/data-int.exit
 create mode 100644 tests/qapi-schema/data-int.json
 create mode 100644 tests/qapi-schema/data-int.out
 create mode 100644 tests/qapi-schema/data-unknown.err
 create mode 100644 tests/qapi-schema/data-unknown.exit
 create mode 100644 tests/qapi-schema/data-unknown.json
 create mode 100644 tests/qapi-schema/data-unknown.out
 create mode 100644 tests/qapi-schema/double-type.err
 create mode 100644 tests/qapi-schema/double-type.exit
 create mode 100644 tests/qapi-schema/double-type.json
 create mode 100644 tests/qapi-schema/double-type.out
 create mode 100644 tests/qapi-schema/enum-empty.err
 create mode 100644 tests/qapi-schema/enum-empty.exit
 create mode 100644 tests/qapi-schema/enum-empty.json
 create mode 100644 tests/qapi-schema/enum-empty.out
 create mode 100644 tests/qapi-schema/enum-missing-data.err
 create mode 100644 tests/qapi-schema/enum-missing-data.exit
 create mode 100644 tests/qapi-schema/enum-missing-data.json
 create mode 100644 tests/qapi-schema/enum-missing-data.out
 create mode 100644 tests/qapi-schema/enum-wrong-data.err
 create mode 100644 tests/qapi-schema/enum-wrong-data.exit
 create mode 100644 tests/qapi-schema/enum-wrong-data.json
 create mode 100644 tests/qapi-schema/enum-wrong-data.out
 create mode 100644 tests/qapi-schema/missing-type.err
 create mode 100644 tests/qapi-schema/missing-type.exit
 create mode 100644 tests/qapi-schema/missing-type.json
 create mode 100644 tests/qapi-schema/missing-type.out
 create mode 100644 tests/qapi-schema/nested-struct-data.err
 create mode 100644 tests/qapi-schema/nested-struct-data.exit
 create mode 100644 tests/qapi-schema/nested-struct-data.json
 create mode 100644 tests/qapi-schema/nested-struct-data.out
 create mode 100644 tests/qapi-schema/nested-struct-returns.err
 create mode 100644 tests/qapi-schema/nested-struct-returns.exit
 create mode 100644 tests/qapi-schema/nested-struct-returns.json
 create mode 100644 tests/qapi-schema/nested-struct-returns.out
 create mode 100644 tests/qapi-schema/returns-array-bad.err
 create mode 100644 tests/qapi-schema/returns-array-bad.exit
 create mode 100644 tests/qapi-schema/returns-array-bad.json
 create mode 100644 tests/qapi-schema/returns-array-bad.out
 create mode 100644 tests/qapi-schema/returns-int.err
 create mode 100644 tests/qapi-schema/returns-int.exit
 create mode 100644 tests/qapi-schema/returns-int.json
 create mode 100644 tests/qapi-schema/returns-int.out
 create mode 100644 tests/qapi-schema/returns-unknown.err
 create mode 100644 tests/qapi-schema/returns-unknown.exit
 create mode 100644 tests/qapi-schema/returns-unknown.json
 create mode 100644 tests/qapi-schema/returns-unknown.out
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#27 lore raw
In reply to Eric Blake
Quoted 7 lines · Eric Blake

According to this email:
https://lists.gnu.org/archive/html/qemu-devel/2014-05/msg00708.html
we want to repurpose 'data': { 'name': {dict...} } in qapi files for future use of designating default values of optional parameters. But to do that, we must first nuke existing use of that syntax for declaring nested structs. Enhancing the testsuite while at it never hurts.

I had a few remarks here and there, but overall, this is solid work. Thanks for tackling this!

#28 lore raw
In reply to Eric Blake
Quoted 7 lines · On Tue, 5 Aug 2014 19:14:19 -0600 Eric Blake <[email protected]> wrote:

According to this email:
https://lists.gnu.org/archive/html/qemu-devel/2014-05/msg00708.html
we want to repurpose 'data': { 'name': {dict...} } in qapi files for future use of designating default values of optional parameters. But to do that, we must first nuke existing use of that syntax for declaring nested structs. Enhancing the testsuite while at it never hurts.

I've skimmed over the series (as Markus is on top of it anyways) and it looks great to me. Waiting for v4 :)