[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL v2 for-2.7 04/15] qapi: Require all branches of flat
From: |
Markus Armbruster |
Subject: |
[Qemu-devel] [PULL v2 for-2.7 04/15] qapi: Require all branches of flat union enum to be covered |
Date: |
Tue, 19 Jul 2016 20:41:47 +0200 |
From: Eric Blake <address@hidden>
We were previously enforcing that all flat union branches were
found in the corresponding enum, but not that all enum values
were covered by branches. The resulting generated code would
abort() if the user passes the uncovered enum value.
We don't automatically treat non-present branches in a flat
union as empty types, for symmetry with simple unions (there,
the enum type is generated from the list of all branches, so
there is no way to omit a branch but still have it be part of
the union).
A later patch will add shorthand so that branches that are empty
in flat unions can be declared as 'branch':{} instead of
'branch':'Empty', to avoid the need for an otherwise useless
explicit empty type. [Such shorthand for simple unions is a bit
harder to justify, since we would still have to generate a
wrapper type that parses 'data':{}, rather than truly being an
empty branch with no additional siblings to the 'type' member.]
Signed-off-by: Eric Blake <address@hidden>
Message-Id: <address@hidden>
Reviewed-by: Markus Armbruster <address@hidden>
Signed-off-by: Markus Armbruster <address@hidden>
---
scripts/qapi.py | 8 ++++++++
tests/Makefile.include | 1 +
tests/qapi-schema/flat-union-incomplete-branch.err | 1 +
tests/qapi-schema/flat-union-incomplete-branch.exit | 1 +
tests/qapi-schema/flat-union-incomplete-branch.json | 9 +++++++++
tests/qapi-schema/flat-union-incomplete-branch.out | 0
6 files changed, 20 insertions(+)
create mode 100644 tests/qapi-schema/flat-union-incomplete-branch.err
create mode 100644 tests/qapi-schema/flat-union-incomplete-branch.exit
create mode 100644 tests/qapi-schema/flat-union-incomplete-branch.json
create mode 100644 tests/qapi-schema/flat-union-incomplete-branch.out
diff --git a/scripts/qapi.py b/scripts/qapi.py
index b13ae47..0635bbb 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -612,6 +612,14 @@ def check_union(expr, expr_info):
"enum '%s'" %
(key, enum_define["enum_name"]))
+ # If discriminator is user-defined, ensure all values are covered
+ if enum_define:
+ for value in enum_define['enum_values']:
+ if value not in members.keys():
+ raise QAPIExprError(expr_info,
+ "Union '%s' data missing '%s' branch"
+ % (name, value))
+
def check_alternate(expr, expr_info):
name = expr['alternate']
diff --git a/tests/Makefile.include b/tests/Makefile.include
index 2010b11..a04c199 100644
--- a/tests/Makefile.include
+++ b/tests/Makefile.include
@@ -326,6 +326,7 @@ qapi-schema += flat-union-base-any.json
qapi-schema += flat-union-base-union.json
qapi-schema += flat-union-clash-member.json
qapi-schema += flat-union-empty.json
+qapi-schema += flat-union-incomplete-branch.json
qapi-schema += flat-union-inline.json
qapi-schema += flat-union-int-branch.json
qapi-schema += flat-union-invalid-branch-key.json
diff --git a/tests/qapi-schema/flat-union-incomplete-branch.err
b/tests/qapi-schema/flat-union-incomplete-branch.err
new file mode 100644
index 0000000..e826bf0
--- /dev/null
+++ b/tests/qapi-schema/flat-union-incomplete-branch.err
@@ -0,0 +1 @@
+tests/qapi-schema/flat-union-incomplete-branch.json:6: Union 'TestUnion' data
missing 'value2' branch
diff --git a/tests/qapi-schema/flat-union-incomplete-branch.exit
b/tests/qapi-schema/flat-union-incomplete-branch.exit
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/tests/qapi-schema/flat-union-incomplete-branch.exit
@@ -0,0 +1 @@
+1
diff --git a/tests/qapi-schema/flat-union-incomplete-branch.json
b/tests/qapi-schema/flat-union-incomplete-branch.json
new file mode 100644
index 0000000..25a411b
--- /dev/null
+++ b/tests/qapi-schema/flat-union-incomplete-branch.json
@@ -0,0 +1,9 @@
+# we require all branches of the union to be covered
+{ 'enum': 'TestEnum',
+ 'data': [ 'value1', 'value2' ] }
+{ 'struct': 'TestTypeA',
+ 'data': { 'string': 'str' } }
+{ 'union': 'TestUnion',
+ 'base': { 'type': 'TestEnum' },
+ 'discriminator': 'type',
+ 'data': { 'value1': 'TestTypeA' } }
diff --git a/tests/qapi-schema/flat-union-incomplete-branch.out
b/tests/qapi-schema/flat-union-incomplete-branch.out
new file mode 100644
index 0000000..e69de29
--
2.5.5
- [Qemu-devel] [PULL v2 for-2.7 00/15] QAPI patches for 2016-07-19, Markus Armbruster, 2016/07/19
- [Qemu-devel] [PULL v2 for-2.7 05/15] qapi: Special case c_name() for empty type, Markus Armbruster, 2016/07/19
- [Qemu-devel] [PULL v2 for-2.7 08/15] qapi: Drop useless gen_err_check(), Markus Armbruster, 2016/07/19
- [Qemu-devel] [PULL v2 for-2.7 02/15] qapi: change QmpInputVisitor to QSLIST, Markus Armbruster, 2016/07/19
- [Qemu-devel] [PULL v2 for-2.7 01/15] qapi: change QmpOutputVisitor to QSLIST, Markus Armbruster, 2016/07/19
- [Qemu-devel] [PULL v2 for-2.7 06/15] qapi: Hide tag_name data member of variants, Markus Armbruster, 2016/07/19
- [Qemu-devel] [PULL v2 for-2.7 07/15] qapi: Add type.is_empty() helper, Markus Armbruster, 2016/07/19
- [Qemu-devel] [PULL v2 for-2.7 03/15] net: use Netdev instead of NetClientOptions in client init, Markus Armbruster, 2016/07/19
- [Qemu-devel] [PULL v2 for-2.7 13/15] block: Simplify drive-mirror, Markus Armbruster, 2016/07/19
- [Qemu-devel] [PULL v2 for-2.7 04/15] qapi: Require all branches of flat union enum to be covered,
Markus Armbruster <=
- [Qemu-devel] [PULL v2 for-2.7 09/15] qapi-event: Simplify visit of non-implicit data, Markus Armbruster, 2016/07/19
- [Qemu-devel] [PULL v2 for-2.7 12/15] block: Simplify block_set_io_throttle, Markus Armbruster, 2016/07/19
- [Qemu-devel] [PULL v2 for-2.7 10/15] qapi: Plumb in 'boxed' to qapi generator lower levels, Markus Armbruster, 2016/07/19
- [Qemu-devel] [PULL v2 for-2.7 11/15] qapi: Implement boxed types for commands/events, Markus Armbruster, 2016/07/19
- [Qemu-devel] [PULL v2 for-2.7 14/15] qapi: Change Netdev into a flat union, Markus Armbruster, 2016/07/19
- [Qemu-devel] [PULL v2 for-2.7 15/15] net: Use correct type for bool flag, Markus Armbruster, 2016/07/19
- Re: [Qemu-devel] [PULL v2 for-2.7 00/15] QAPI patches for 2016-07-19, Peter Maydell, 2016/07/20