qapi: Require all branches of flat union enum to be covered

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.]

Backports commit d0b182392d0281ef780e3effcb82677a004f1f97 from qemu
This commit is contained in:
Eric Blake 2018-02-25 20:04:11 -05:00 committed by Lioncash
parent 674805745b
commit 8ccfff95fe
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7

View file

@ -617,6 +617,14 @@ def check_union(expr, expr_info):
"enum '%s'" % "enum '%s'" %
(key, enum_define["enum_name"])) (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): def check_alternate(expr, expr_info):
name = expr['alternate'] name = expr['alternate']