[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH RFC v4 16/29] qapi: Forbid empty unions and useless
From: |
Eric Blake |
Subject: |
[Qemu-devel] [PATCH RFC v4 16/29] qapi: Forbid empty unions and useless alternates |
Date: |
Wed, 9 Sep 2015 22:06:18 -0600 |
Empty unions serve no purpose, and while we compile with gcc
which permits them, strict C99 forbids them. We could inject
a dummy member (and in fact, we do for empty structs), but while
empty structs make sense in qapi, empty unions don't add any
expressiveness to the QMP language. So prohibit them at parse
time. Update the documentation and testsuite to match.
Signed-off-by: Eric Blake <address@hidden>
---
docs/qapi-code-gen.txt | 13 +++++++------
scripts/qapi.py | 12 ++++++++++--
tests/qapi-schema/alternate-empty.err | 1 +
tests/qapi-schema/alternate-empty.exit | 2 +-
tests/qapi-schema/alternate-empty.json | 2 +-
tests/qapi-schema/alternate-empty.out | 3 ---
tests/qapi-schema/flat-union-empty.err | 1 +
tests/qapi-schema/flat-union-empty.exit | 2 +-
tests/qapi-schema/flat-union-empty.json | 2 +-
tests/qapi-schema/flat-union-empty.out | 7 -------
tests/qapi-schema/union-empty.err | 1 +
tests/qapi-schema/union-empty.exit | 2 +-
tests/qapi-schema/union-empty.json | 2 +-
tests/qapi-schema/union-empty.out | 4 ----
14 files changed, 26 insertions(+), 28 deletions(-)
diff --git a/docs/qapi-code-gen.txt b/docs/qapi-code-gen.txt
index 3272b24..ff57010 100644
--- a/docs/qapi-code-gen.txt
+++ b/docs/qapi-code-gen.txt
@@ -182,11 +182,11 @@ prevent incomplete include files.
Usage: { 'struct': STRING, 'data': DICT, '*base': STRUCT-NAME }
-A struct is a dictionary containing a single 'data' key whose
-value is a dictionary. This corresponds to a struct in C or an Object
-in JSON. Each value of the 'data' dictionary must be the name of a
-type, or a one-element array containing a type name. An example of a
-struct is:
+A struct is a dictionary containing a single 'data' key whose value is
+a dictionary; the dictionary may be empty. This corresponds to a
+struct in C or an Object in JSON. Each value of the 'data' dictionary
+must be the name of a type, or a one-element array containing a type
+name. An example of a struct is:
{ 'struct': 'MyType',
'data': { 'member1': 'str', 'member2': 'int', '*member3': 'str' } }
@@ -277,7 +277,8 @@ Union types are used to let the user choose between several
different
variants for an object. There are two flavors: simple (no
discriminator or base), flat (both discriminator and base). A union
type is defined using a data dictionary as explained in the following
-paragraphs.
+paragraphs. The data dictionary for either type of union must not
+be empty.
A simple union type defines a mapping from automatic discriminator
values to data types like in this example:
diff --git a/scripts/qapi.py b/scripts/qapi.py
index a0eca74..475d2c2 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -563,7 +563,10 @@ def check_union(expr, expr_info):
"Discriminator '%s' must be of enumeration "
"type" % discriminator)
- # Check every branch
+ # Check every branch; don't allow an empty union
+ if len(members) == 0:
+ raise QAPIExprError(expr_info,
+ "Union '%s' cannot have empty 'data'" % name)
for (key, value) in members.items():
check_name(expr_info, "Member of union '%s'" % name, key)
@@ -601,7 +604,11 @@ def check_alternate(expr, expr_info):
values = { 'MAX': '(automatic)' }
types_seen = {}
- # Check every branch
+ # Check every branch; require at least two branches
+ if len(members) < 2:
+ raise QAPIExprError(expr_info,
+ "Alternate '%s' should have at least two branches "
+ "in 'data'" % name)
for (key, value) in members.items():
check_name(expr_info, "Member of alternate '%s'" % name, key)
@@ -964,6 +971,7 @@ class QAPISchemaObjectTypeVariants(object):
def __init__(self, tag_name, tag_enum, variants):
assert tag_name is None or isinstance(tag_name, str)
assert tag_enum is None or isinstance(tag_enum, str)
+ assert len(variants) > 0
for v in variants:
assert isinstance(v, QAPISchemaObjectTypeVariant)
self._tag_name = tag_name
diff --git a/tests/qapi-schema/alternate-empty.err
b/tests/qapi-schema/alternate-empty.err
index e69de29..bb06c5b 100644
--- a/tests/qapi-schema/alternate-empty.err
+++ b/tests/qapi-schema/alternate-empty.err
@@ -0,0 +1 @@
+tests/qapi-schema/alternate-empty.json:2: Alternate 'Alt' should have at least
two branches in 'data'
diff --git a/tests/qapi-schema/alternate-empty.exit
b/tests/qapi-schema/alternate-empty.exit
index 573541a..d00491f 100644
--- a/tests/qapi-schema/alternate-empty.exit
+++ b/tests/qapi-schema/alternate-empty.exit
@@ -1 +1 @@
-0
+1
diff --git a/tests/qapi-schema/alternate-empty.json
b/tests/qapi-schema/alternate-empty.json
index db3820f..fff15ba 100644
--- a/tests/qapi-schema/alternate-empty.json
+++ b/tests/qapi-schema/alternate-empty.json
@@ -1,2 +1,2 @@
-# FIXME - alternates should list at least two types to be useful
+# alternates must list at least two types to be useful
{ 'alternate': 'Alt', 'data': { 'i': 'int' } }
diff --git a/tests/qapi-schema/alternate-empty.out
b/tests/qapi-schema/alternate-empty.out
index 9b010d8..e69de29 100644
--- a/tests/qapi-schema/alternate-empty.out
+++ b/tests/qapi-schema/alternate-empty.out
@@ -1,3 +0,0 @@
-object :empty
-alternate Alt
- case i: int
diff --git a/tests/qapi-schema/flat-union-empty.err
b/tests/qapi-schema/flat-union-empty.err
index e69de29..15754f5 100644
--- a/tests/qapi-schema/flat-union-empty.err
+++ b/tests/qapi-schema/flat-union-empty.err
@@ -0,0 +1 @@
+tests/qapi-schema/flat-union-empty.json:4: Union 'Union' cannot have empty
'data'
diff --git a/tests/qapi-schema/flat-union-empty.exit
b/tests/qapi-schema/flat-union-empty.exit
index 573541a..d00491f 100644
--- a/tests/qapi-schema/flat-union-empty.exit
+++ b/tests/qapi-schema/flat-union-empty.exit
@@ -1 +1 @@
-0
+1
diff --git a/tests/qapi-schema/flat-union-empty.json
b/tests/qapi-schema/flat-union-empty.json
index 67dd297..77f1d9a 100644
--- a/tests/qapi-schema/flat-union-empty.json
+++ b/tests/qapi-schema/flat-union-empty.json
@@ -1,4 +1,4 @@
-# FIXME - flat unions should not be empty
+# flat unions cannot be empty
{ 'enum': 'Empty', 'data': [ ] }
{ 'struct': 'Base', 'data': { 'type': 'Empty' } }
{ 'union': 'Union', 'base': 'Base', 'discriminator': 'type', 'data': { } }
diff --git a/tests/qapi-schema/flat-union-empty.out
b/tests/qapi-schema/flat-union-empty.out
index 0e0665a..e69de29 100644
--- a/tests/qapi-schema/flat-union-empty.out
+++ b/tests/qapi-schema/flat-union-empty.out
@@ -1,7 +0,0 @@
-object :empty
-object Base
- member type: Empty optional=False
-enum Empty []
-object Union
- base Base
- tag type
diff --git a/tests/qapi-schema/union-empty.err
b/tests/qapi-schema/union-empty.err
index e69de29..12c2022 100644
--- a/tests/qapi-schema/union-empty.err
+++ b/tests/qapi-schema/union-empty.err
@@ -0,0 +1 @@
+tests/qapi-schema/union-empty.json:2: Union 'Union' cannot have empty 'data'
diff --git a/tests/qapi-schema/union-empty.exit
b/tests/qapi-schema/union-empty.exit
index 573541a..d00491f 100644
--- a/tests/qapi-schema/union-empty.exit
+++ b/tests/qapi-schema/union-empty.exit
@@ -1 +1 @@
-0
+1
diff --git a/tests/qapi-schema/union-empty.json
b/tests/qapi-schema/union-empty.json
index 1785007..1f0b13c 100644
--- a/tests/qapi-schema/union-empty.json
+++ b/tests/qapi-schema/union-empty.json
@@ -1,2 +1,2 @@
-# FIXME - unions should not be empty
+# unions cannot be empty
{ 'union': 'Union', 'data': { } }
diff --git a/tests/qapi-schema/union-empty.out
b/tests/qapi-schema/union-empty.out
index c5500da..e69de29 100644
--- a/tests/qapi-schema/union-empty.out
+++ b/tests/qapi-schema/union-empty.out
@@ -1,4 +0,0 @@
-object :empty
-object Union
- tag type
-enum UnionKind []
--
2.4.3
- [Qemu-devel] [PATCH RFC v4 05/29] qapi: Unbox base members, (continued)
- [Qemu-devel] [PATCH RFC v4 05/29] qapi: Unbox base members, Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 07/29] qapi: Test use of 'number' within alternates, Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 09/29] qapi: Hide tag_name data member of variants, Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 08/29] qapi: Simplify visiting of alternate types, Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 12/29] qapi: Use consistent generated code patterns, Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 17/29] qapi: Drop useless 'data' member of unions, Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 11/29] qapi: Don't pass pre-existing error to later call, Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 14/29] qapi: Rework deallocation of partial struct, Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 10/29] qapi: Fix alternates that accept 'number' but not 'int', Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 13/29] qapi: Add tests for empty unions, Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 16/29] qapi: Forbid empty unions and useless alternates,
Eric Blake <=
- [Qemu-devel] [PATCH RFC v4 15/29] qapi: Avoid use of 'data' member of qapi unions, Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 18/29] qapi: Remove dead visitor code, Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 21/29] qapi: Test failure in middle of array parse, Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 19/29] qapi: Document visitor interfaces, Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 20/29] qapi: Plug leaks in test-qmp-input-visitor, Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 22/29] qapi: Change visit_type_FOO() to no longer return partial objects, Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 03/29] qapi: use 'type' in generated C code to match QMP union wire form, Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 24/29] qapi: Implement boxed structs for commands/events, Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 23/29] qapi: Plumb in 'box' to qapi generator lower levels, Eric Blake, 2015/09/10
- [Qemu-devel] [PATCH RFC v4 25/29] qapi: Support boxed unions, Eric Blake, 2015/09/10