qapi: Better error messages for bad expressions

The previous commit demonstrated that the generator overlooked some
fairly basic broken expressions:
- missing metataype
- metatype key has a non-string value
- unknown key in relation to the metatype
- conflicting metatype (this patch treats the second metatype as an
unknown key of the first key visited, which is not necessarily the
first key the user typed)

Add check_keys to cover these situations, and update testcases to
match. A couple other tests (enum-missing-data, indented-expr) had
to change since the validation added here occurs so early.
Conversely, changes to ident-with-escape results show that we still
have problems where our handling of escape sequences differs from
true JSON, which will matter down the road if we allow arbitrary
default string values for optional parameters (but for now is not
too bad, as we currently can avoid unicode escaping as we don't
need to represent anything beyond C identifier material).

While valid .json files won't trigger any of these cases, we might
as well be nicer to developers that make a typo while trying to add
new QAPI code.

Backports commit 0545f6b8874c28d97369f2c83e5077e0461d4f12 from qemu
This commit is contained in:
Eric Blake 2018-02-19 13:52:10 -05:00 committed by Lioncash
parent 8744d16fbe
commit 75ba2155af
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7

View file

@ -347,11 +347,6 @@ def check_alternate(expr, expr_info):
values = { 'MAX': '(automatic)' }
types_seen = {}
if expr.get('base') is not None:
raise QAPIExprError(expr_info,
"Alternate '%s' must not have a base"
% name)
# Check every branch
for (key, value) in members.items():
# Check for conflicts in the generated enum
@ -413,6 +408,26 @@ def check_exprs(schema):
elif expr.has_key('event'):
check_event(expr, info)
def check_keys(expr_elem, meta, required, optional=[]):
expr = expr_elem['expr']
info = expr_elem['info']
name = expr[meta]
if not isinstance(name, str):
raise QAPIExprError(info,
"'%s' key must have a string value" % meta)
required = required + [ meta ]
for (key, value) in expr.items():
if not key in required and not key in optional:
raise QAPIExprError(info,
"Unknown key '%s' in %s '%s'"
% (key, meta, name))
for key in required:
if not expr.has_key(key):
raise QAPIExprError(info,
"Key '%s' is missing from %s '%s'"
% (key, meta, name))
def parse_schema(input_file):
# First pass: read entire file into memory
try:
@ -424,15 +439,30 @@ def parse_schema(input_file):
exprs = []
try:
# Next pass: learn the types.
# Next pass: learn the types and check for valid expression keys. At
# this point, top-level 'include' has already been flattened.
for expr_elem in schema.exprs:
expr = expr_elem['expr']
if expr.has_key('enum'):
add_enum(expr['enum'], expr.get('data'))
check_keys(expr_elem, 'enum', ['data'])
add_enum(expr['enum'], expr['data'])
elif expr.has_key('union'):
check_keys(expr_elem, 'union', ['data'],
['base', 'discriminator'])
add_union(expr)
elif expr.has_key('alternate'):
check_keys(expr_elem, 'alternate', ['data'])
elif expr.has_key('type'):
check_keys(expr_elem, 'type', ['data'], ['base'])
add_struct(expr)
elif expr.has_key('command'):
check_keys(expr_elem, 'command', [],
['data', 'returns', 'gen', 'success-response'])
elif expr.has_key('event'):
check_keys(expr_elem, 'event', [], ['data'])
else:
raise QAPIExprError(expr_elem['info'],
"Expression is missing metatype")
exprs.append(expr)
# Try again for hidden UnionKind enum