From: Eric Blake <eblake@redhat.com>
To: qemu-devel@nongnu.org
Cc: armbru@redhat.com, Michael Roth <mdroth@linux.vnet.ibm.com>
Subject: [Qemu-devel] [PATCH v8 02/16] qapi: Require all branches of flat union enum to be covered
Date: Sat, 2 Jul 2016 20:58:35 -0600 [thread overview]
Message-ID: <1467514729-29366-3-git-send-email-eblake@redhat.com> (raw)
In-Reply-To: <1467514729-29366-1-git-send-email-eblake@redhat.com>
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 <eblake@redhat.com>
---
v8: enhance commit message
v7: new patch
---
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 1163807..df232f6 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
next prev parent reply other threads:[~2016-07-03 2:59 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-07-03 2:58 [Qemu-devel] [PATCH for-2.7 v8 00/16] qapi netdev_add introspection (post-introspection cleanups subset F) Eric Blake
2016-07-03 2:58 ` [Qemu-devel] [PATCH v8 01/16] net: use Netdev instead of NetClientOptions in client init Eric Blake
2016-07-03 2:58 ` Eric Blake [this message]
2016-07-03 2:58 ` [Qemu-devel] [PATCH v8 03/16] qapi: Hide tag_name data member of variants Eric Blake
2016-07-03 2:58 ` [Qemu-devel] [PATCH v8 04/16] qapi: Add type.is_empty() helper Eric Blake
2016-07-03 2:58 ` [Qemu-devel] [PATCH v8 05/16] qapi: Drop useless gen_err_check() Eric Blake
2016-07-03 2:58 ` [Qemu-devel] [PATCH v8 06/16] qapi-event: Simplify visit of non-implicit data Eric Blake
2016-07-03 2:58 ` [Qemu-devel] [PATCH v8 07/16] qapi: Plumb in 'box' to qapi generator lower levels Eric Blake
2016-07-03 2:58 ` [Qemu-devel] [PATCH v8 08/16] qapi: Implement boxed types for commands/events Eric Blake
2016-07-07 10:52 ` Markus Armbruster
2016-07-07 16:02 ` Eric Blake
2016-07-08 7:06 ` Markus Armbruster
2016-07-03 2:58 ` [Qemu-devel] [PATCH v8 09/16] block: Simplify block_set_io_throttle Eric Blake
2016-07-03 2:58 ` [Qemu-devel] [PATCH v8 10/16] block: Simplify drive-mirror Eric Blake
2016-07-05 20:27 ` [Qemu-devel] [Qemu-block] " John Snow
2016-07-05 22:16 ` Eric Blake
2016-07-05 22:17 ` John Snow
2016-07-03 2:58 ` [Qemu-devel] [PATCH v8 11/16] qapi-event: Reduce chance of collision with event data Eric Blake
2016-07-07 11:37 ` Markus Armbruster
2016-07-13 21:05 ` Eric Blake
2016-07-03 2:58 ` [Qemu-arm] [PATCH v8 12/16] qapi: Change Netdev into a flat union Eric Blake
2016-07-03 2:58 ` Eric Blake
2016-07-03 2:58 ` [Qemu-devel] " Eric Blake
2016-07-03 2:58 ` [Qemu-devel] [PATCH v8 13/16] net: Use correct type for bool flag Eric Blake
2016-07-03 2:58 ` [Qemu-devel] [PATCH v8 14/16] net: Complete qapi-fication of netdev_add Eric Blake
2016-07-07 12:57 ` Markus Armbruster
2016-07-03 2:58 ` [Qemu-devel] [PATCH v8 15/16] qapi: Allow anonymous branch types in flat union Eric Blake
2016-07-03 2:58 ` [Qemu-devel] [PATCH v8 16/16] schema: Drop pointless empty type CpuInfoOther Eric Blake
2016-07-07 13:40 ` [Qemu-devel] [PATCH for-2.7 v8 00/16] qapi netdev_add introspection (post-introspection cleanups subset F) Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1467514729-29366-3-git-send-email-eblake@redhat.com \
--to=eblake@redhat.com \
--cc=armbru@redhat.com \
--cc=mdroth@linux.vnet.ibm.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.