qapi script: support enum as discriminator and better enum name
v2 cover qapi script: support enum as discriminator and better enum name
This series is respined from RFC series at:
http://lists.nongnu.org/archive/html/qemu-devel/2013-11/msg00363.html
Patch 1-6 add support for enum as discriminator.
Patch 7 improve enum name generation, now AIOContext->AIO_CONTEXT, X86CPU-> X86_CPU.
Patch 8 are the test cases.
Changes from RFC:
Mainly address Eric's comments: fix typo, add patch 2 to allow partly mapping
enum value in union, add related test case, remove direct inherit support "_base" and related test case.
v2:
General:
3: use Raise exception instead of sys.error.write in qapi.py. Address Eric's comments:
2,3: more check for enum value at compile time.
8: correspond test case change.
Wenchao Xia (8):
1 qapi script: remember enum values
2 qapi script: add check for duplicated key
3 qapi script: check correctness of discriminator values in union 4 qapi script: code move for generate_enum_name()
5 qapi script: use same function to generate enum string
6 qapi script: not generate hidden enum type for pre-defined enum discriminator 7 qapi script: do not add "_" for every capitalized char in enum
8 tests: add cases for inherited struct and union with discriminator
include/qapi/qmp/qerror.h | 2 +- scripts/qapi-types.py | 34 ++++---- scripts/qapi-visit.py | 55 +++++++++-- scripts/qapi.py | 84 ++++++++++++++++- target-i386/cpu.c | 2 +- tests/qapi-schema/comments.out | 2 +- tests/qapi-schema/qapi-schema-test.json | 27 ++++++ tests/qapi-schema/qapi-schema-test.out | 15 +++- tests/test-qmp-input-visitor.c | 120 +++++++++++++++++++++++++ tests/test-qmp-output-visitor.c | 149 +++++++++++++++++++++++++++++++ 10 files changed, 454 insertions(+), 36 deletions(-)
PATCH v2 2/8 qapi script: add check for duplicated key
It is bad that same key was specified twice, especially when a union have two branches with same condition. This patch can prevent it.
--- scripts/qapi.py | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
scripts/qapi.py +2 -0
@@ -116,6 +116,8 @@ class QAPISchema: if self.tok != ':': raise QAPISchemaError(self, 'Expected ":"') self.accept() + if key in expr: + raise QAPISchemaError(self, 'Duplicated key "%s"' % key) expr[key] = self.get_expr(True) if self.tok == '}': self.accept()
Signature · 1 line
1.7.1
PATCH v2 5/8 qapi script: use same function to generate enum string
In reply to Wenchao XiaOne function one rule, so the enum string generating have same behavior for different caller. If multiple caller exist for one enum define in schema, it is for sure the generated string is identical.
Note before the patch qapi-visit.py used custom function to generate the string in union visit, although the patch changes it, the final string generated is not changed. The custom function used before will met problem when capitalized discriminator value is introduced.
--- scripts/qapi-types.py | 6 +++--- scripts/qapi-visit.py | 21 +++++++++++++++------ scripts/qapi.py | 15 +++++++++++---- 3 files changed, 29 insertions(+), 13 deletions(-)
scripts/qapi-types.py +3 -3
@@ -144,11 +144,11 @@ typedef enum %(name)s i = 0 for value in enum_values: + enum_full_value_string = generate_enum_full_value_string(name, value) enum_decl += mcgen(''' - %(abbrev)s_%(value)s = %(i)d, + %(enum_full_value_string)s = %(i)d, ''', - abbrev=de_camel_case(name).upper(), - value=generate_enum_name(value), + enum_full_value_string=enum_full_value_string, i=i) i += 1
scripts/qapi-visit.py +15 -6
@@ -208,18 +208,23 @@ void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error ** ''', name=name) + # For anon union, always use the default enum type automatically generated + # as "'%sKind' % (name)" + discriminator_type_name = '%sKind' % (name) + for key in members: assert (members[key] in builtin_types or find_struct(members[key]) or find_union(members[key])), "Invalid anonymous union member" + enum_full_value_string = \ + generate_enum_full_value_string(discriminator_type_name, key) ret += mcgen(''' - case %(abbrev)s_KIND_%(enum)s: + case %(enum_full_value_string)s: visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, name, &err); break; ''', - abbrev = de_camel_case(name).upper(), - enum = c_fun(de_camel_case(key),False).upper(), + enum_full_value_string = enum_full_value_string, c_type = type_name(members[key]), c_name = c_fun(key))
@@ -266,7 +271,10 @@ def generate_visit_union(expr): (key, name)) sys.exit(1) + # There will always be a discriminator in the C switch code, by default it + # is an enum type generated silently as "'%sKind' % (name)" ret = generate_visit_enum('%sKind' % name, members.keys()) + discriminator_type_name = '%sKind' % (name) if base: base_fields = find_struct(base)['data']
@@ -319,13 +327,14 @@ void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error ** visit_end_implicit_struct(m, &err); }''' + enum_full_value_string = \ + generate_enum_full_value_string(discriminator_type_name, key) ret += mcgen(''' - case %(abbrev)s_KIND_%(enum)s: + case %(enum_full_value_string)s: ''' + fmt + ''' break; ''', - abbrev = de_camel_case(name).upper(), - enum = c_fun(de_camel_case(key),False).upper(), + enum_full_value_string = enum_full_value_string, c_type=type_name(members[key]), c_name=c_fun(key))
scripts/qapi.py +11 -4
@@ -417,12 +417,19 @@ def discriminator_find_enum_define(expr): return find_enum(discriminator_type) -def generate_enum_name(name): - if name.isupper(): - return c_fun(name, False) +def _generate_enum_value_string(value): + if value.isupper(): + return c_fun(value, False) new_name = '' - for c in c_fun(name, False): + for c in c_fun(value, False): if c.isupper(): new_name += '_' new_name += c return new_name.lstrip('_').upper() + +def generate_enum_full_value_string(enum_name, enum_value): + # generate abbrev string + abbrev_string = de_camel_case(enum_name).upper() + # generate value string + value_string = _generate_enum_value_string(enum_value) + return "%s_%s" % (abbrev_string, value_string)
Signature · 1 line
1.7.1
PATCH v2 1/8 qapi script: remember enum values
In reply to Wenchao XiaLater other scripts will need to check the enum values.
--- scripts/qapi.py | 18 ++++++++++++++---- tests/qapi-schema/comments.out | 2 +- tests/qapi-schema/qapi-schema-test.out | 4 +++- 3 files changed, 18 insertions(+), 6 deletions(-)
scripts/qapi.py +14 -4
@@ -169,7 +169,7 @@ def parse_schema(fp): for expr in schema.exprs: if expr.has_key('enum'): - add_enum(expr['enum']) + add_enum(expr['enum'], expr['data']) elif expr.has_key('union'): add_union(expr) add_enum('%sKind' % expr['union'])
@@ -289,13 +289,23 @@ def find_union(name): return union return None -def add_enum(name): +def add_enum(name, enum_values = None): global enum_types - enum_types.append(name) + enum_types.append({"enum_name": name, "enum_values": enum_values}) + +def find_enum(name): + global enum_types + for enum in enum_types: + if enum['enum_name'] == name: + return enum + return None def is_enum(name): global enum_types - return (name in enum_types) + for enum in enum_types: + if enum['enum_name'] == name: + return True + return False def c_type(name): if name == 'str':
tests/qapi-schema/comments.out +1 -1
@@ -1,3 +1,3 @@ [OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])] -['Status'] +[{'enum_name': 'Status', 'enum_values': ['good', 'bad', 'ugly']}] []
tests/qapi-schema/qapi-schema-test.out +3 -1
@@ -11,7 +11,9 @@ OrderedDict([('command', 'user_def_cmd1'), ('data', OrderedDict([('ud1a', 'UserDefOne')]))]), OrderedDict([('command', 'user_def_cmd2'), ('data', OrderedDict([('ud1a', 'UserDefOne'), ('ud1b', 'UserDefOne')])), ('returns', 'UserDefTwo')]), OrderedDict([('type', 'UserDefOptions'), ('data', OrderedDict([('*i64', ['int']), ('*u64', ['uint64']), ('*u16', ['uint16']), ('*i64x', 'int'), ('*u64x', 'uint64')]))])] -['EnumOne', 'UserDefUnionKind', 'UserDefNativeListUnionKind'] +[{'enum_name': 'EnumOne', 'enum_values': ['value1', 'value2', 'value3']}, + {'enum_name': 'UserDefUnionKind', 'enum_values': None}, + {'enum_name': 'UserDefNativeListUnionKind', 'enum_values': None}] [OrderedDict([('type', 'NestedEnumsOne'), ('data', OrderedDict([('enum1', 'EnumOne'), ('*enum2', 'EnumOne'), ('enum3', 'EnumOne'), ('*enum4', 'EnumOne')]))]), OrderedDict([('type', 'UserDefOne'), ('data', OrderedDict([('integer', 'int'), ('string', 'str'), ('*enum1', 'EnumOne')]))]), OrderedDict([('type', 'UserDefTwo'), ('data', OrderedDict([('string', 'str'), ('dict', OrderedDict([('string', 'str'), ('dict', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')])), ('*dict2', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')]))]))]))]),
Signature · 1 line
1.7.1
PATCH v2 3/8 qapi script: check correctness of discriminator values in union
In reply to Wenchao XiaIt will check whether the values specified are written correctly, and whether all enum values are covered, when discriminator is a pre-defined enum type
--- scripts/qapi-visit.py | 17 +++++++++++++++++ scripts/qapi.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 0 deletions(-)
scripts/qapi-visit.py +17 -0
@@ -249,6 +249,23 @@ def generate_visit_union(expr): assert not base return generate_visit_anon_union(name, members) + # If discriminator is specified and it is a pre-defined enum in schema, + # check its correctness + enum_define = discriminator_find_enum_define(expr) + if enum_define: + for key in members: + if not key in enum_define["enum_values"]: + sys.stderr.write("Discriminator value '%s' is not found in " + "enum '%s'\n" % + (key, enum_define["enum_name"])) + sys.exit(1) + for key in enum_define["enum_values"]: + if not key in members: + sys.stderr.write("Enum value '%s' is not covered by a branch " + "of union '%s'\n" % + (key, name)) + sys.exit(1) + ret = generate_visit_enum('%sKind' % name, members.keys()) if base:
scripts/qapi.py +31 -0
@@ -385,3 +385,34 @@ def guardend(name): ''', name=guardname(name)) + +# This function can be used to check whether "base" is valid +def find_base_fields(base): + base_struct_define = find_struct(base) + if not base_struct_define: + return None + return base_struct_define.get('data') + +# Return the discriminator enum define, if discriminator is specified in +# @expr and it is a pre-defined enum type +def discriminator_find_enum_define(expr): + discriminator = expr.get('discriminator') + base = expr.get('base') + + # Only support discriminator when base present + if not (discriminator and base): + return None + + base_fields = find_base_fields(base) + + if not base_fields: + raise StandardError("Base '%s' is not a valid type\n" + % base) + + discriminator_type = base_fields.get(discriminator) + + if not discriminator_type: + raise StandardError("Discriminator '%s' not found in schema\n" + % discriminator) + + return find_enum(discriminator_type)
Signature · 1 line
1.7.1
PATCH v2 4/8 qapi script: code move for generate_enum_name()
In reply to Wenchao XiaLater both qapi-types.py and qapi-visit.py need a common function for enum name generation.
--- scripts/qapi-types.py | 10 ---------- scripts/qapi.py | 10 ++++++++++ 2 files changed, 10 insertions(+), 10 deletions(-)
scripts/qapi-types.py +0 -10
@@ -127,16 +127,6 @@ const char *%(name)s_lookup[] = { ''') return ret -def generate_enum_name(name): - if name.isupper(): - return c_fun(name, False) - new_name = '' - for c in c_fun(name, False): - if c.isupper(): - new_name += '_' - new_name += c - return new_name.lstrip('_').upper() - def generate_enum(name, values): lookup_decl = mcgen(''' extern const char *%(name)s_lookup[];
scripts/qapi.py +10 -0
@@ -416,3 +416,13 @@ def discriminator_find_enum_define(expr): % discriminator) return find_enum(discriminator_type) + +def generate_enum_name(name): + if name.isupper(): + return c_fun(name, False) + new_name = '' + for c in c_fun(name, False): + if c.isupper(): + new_name += '_' + new_name += c + return new_name.lstrip('_').upper()
Signature · 1 line
1.7.1
PATCH v2 7/8 qapi script: do not add "_" for every capitalized char in enum
In reply to Wenchao XiaNow "enum AIOContext" will generate AIO_CONTEXT instead of A_I_O_CONTEXT, "X86CPU" will generate X86_CPU instead of X86_C_P_U.
--- include/qapi/qmp/qerror.h | 2 +- scripts/qapi.py | 26 +++++++++++++++++++------- target-i386/cpu.c | 2 +- 3 files changed, 21 insertions(+), 9 deletions(-)
include/qapi/qmp/qerror.h +1 -1
@@ -160,7 +160,7 @@ void assert_no_error(Error *err); ERROR_CLASS_GENERIC_ERROR, "Invalid JSON syntax"#define QERR_KVM_MISSING_CAP \ - ERROR_CLASS_K_V_M_MISSING_CAP, "Using KVM without %s, %s unavailable" + ERROR_CLASS_KVM_MISSING_CAP, "Using KVM without %s, %s unavailable"#define QERR_MIGRATION_ACTIVE \ ERROR_CLASS_GENERIC_ERROR, "There's a migration process in progress"
scripts/qapi.py +19 -7
@@ -419,19 +419,31 @@ def discriminator_find_enum_define(expr): return find_enum(discriminator_type) -def _generate_enum_value_string(value): +# ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1 +# ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2 +# ENUM24_Name -> ENUM24_NAME +def _generate_enum_string(value): + c_fun_str = c_fun(value, False) if value.isupper(): - return c_fun(value, False) + return c_fun_str + new_name = '' - for c in c_fun(value, False): - if c.isupper(): - new_name += '_' + l = len(c_fun_str) + for i in range(l): + c = c_fun_str[i] + # When c is upper and no "_" appear before, do more check + if c.isupper() and (i > 0) and c_fun_str[i - 1] != "_": + # Case 1: Next string is lower + # Case 2: previous string is digit + if (i < (l - 1) and c_fun_str[i + 1].islower()) or \ + c_fun_str[i - 1].isdigit(): + new_name += '_' new_name += c return new_name.lstrip('_').upper() def generate_enum_full_value_string(enum_name, enum_value): # generate abbrev string - abbrev_string = de_camel_case(enum_name).upper() + abbrev_string = _generate_enum_string(enum_name) # generate value string - value_string = _generate_enum_value_string(enum_value) + value_string = _generate_enum_string(enum_value) return "%s_%s" % (abbrev_string, value_string)
target-i386/cpu.c +1 -1
@@ -315,7 +315,7 @@ typedef struct X86RegisterInfo32 { } X86RegisterInfo32;#define REGISTER (reg) \ - [R_##reg] = { .name = #reg, .qapi_enum = X86_C_P_U_REGISTER32_##reg } + [R_##reg] = { .name = #reg, .qapi_enum = X86_CPU_REGISTER32_##reg }X86RegisterInfo32 x86_reg_info_32[CPU_NB_REGS32 ] = {REGISTER (EAX ),REGISTER (ECX ),
Signature · 1 line
1.7.1
PATCH v2 6/8 qapi script: not generate hidden enum type for pre-defined enum discriminator
In reply to Wenchao XiaBy default, any union will automatically generate a enum type as "[UnionName]Kind" in C code, and it is duplicated when the discriminator is specified as a pre-defined enum type in schema. After this patch, the pre-defined enum type will be really used as the switch case condition in generated C code.
In short, enum type as discriminator is fully supported now.
--- scripts/qapi-types.py | 18 ++++++++++++++---- scripts/qapi-visit.py | 23 ++++++++++++++++------- scripts/qapi.py | 4 +++- 3 files changed, 33 insertions(+), 12 deletions(-)
scripts/qapi-types.py +14 -4
@@ -201,14 +201,21 @@ def generate_union(expr): base = expr.get('base') discriminator = expr.get('discriminator') + enum_define = discriminator_find_enum_define(expr) + if enum_define: + discriminator_type_name = enum_define['enum_name'] + else: + discriminator_type_name = '%sKind' % (name) + ret = mcgen(''' struct %(name)s { - %(name)sKind kind; + %(discriminator_type_name)s kind; union { void *data; ''', - name=name) + name=name, + discriminator_type_name=discriminator_type_name) for key in typeinfo: ret += mcgen('''
@@ -389,8 +396,11 @@ for expr in exprs: fdef.write(generate_enum_lookup(expr['enum'], expr['data'])) elif expr.has_key('union'): ret += generate_fwd_struct(expr['union'], expr['data']) + "\n" - ret += generate_enum('%sKind' % expr['union'], expr['data'].keys()) - fdef.write(generate_enum_lookup('%sKind' % expr['union'], expr['data'].keys())) + enum_define = discriminator_find_enum_define(expr) + if not enum_define: + ret += generate_enum('%sKind' % expr['union'], expr['data'].keys()) + fdef.write(generate_enum_lookup('%sKind' % expr['union'], + expr['data'].keys())) if expr.get('discriminator') == {}: fdef.write(generate_anon_union_qtypes(expr)) else:
scripts/qapi-visit.py +16 -7
@@ -271,10 +271,14 @@ def generate_visit_union(expr): (key, name)) sys.exit(1) - # There will always be a discriminator in the C switch code, by default it - # is an enum type generated silently as "'%sKind' % (name)" - ret = generate_visit_enum('%sKind' % name, members.keys()) - discriminator_type_name = '%sKind' % (name) + # Use the predefined enum type as discriminator + ret = "" + discriminator_type_name = enum_define['enum_name'] + else: + # There will always be a discriminator in the C switch code, by default it + # is an enum type generated silently as "'%sKind' % (name)" + ret = generate_visit_enum('%sKind' % name, members.keys()) + discriminator_type_name = '%sKind' % (name) if base: base_fields = find_struct(base)['data']
@@ -309,11 +313,12 @@ void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error ** pop_indent() ret += mcgen(''' - visit_type_%(name)sKind(m, &(*obj)->kind, "%(type)s", &err); + visit_type_%(discriminator_type_name)s(m, &(*obj)->kind, "%(type)s", &err); if (!err) { switch ((*obj)->kind) { ''', - name=name, type="type" if not discriminator else discriminator) + discriminator_type_name=discriminator_type_name, + type="type" if not discriminator else discriminator) for key in members: if not discriminator:
@@ -527,7 +532,11 @@ for expr in exprs: ret += generate_visit_list(expr['union'], expr['data']) fdef.write(ret) - ret = generate_decl_enum('%sKind' % expr['union'], expr['data'].keys()) + enum_define = discriminator_find_enum_define(expr) + ret = "" + if not enum_define: + ret = generate_decl_enum('%sKind' % expr['union'], + expr['data'].keys()) ret += generate_declaration(expr['union'], expr['data']) fdecl.write(ret) elif expr.has_key('enum'):
scripts/qapi.py +3 -1
@@ -174,7 +174,9 @@ def parse_schema(fp): add_enum(expr['enum'], expr['data']) elif expr.has_key('union'): add_union(expr) - add_enum('%sKind' % expr['union']) + enum_define = discriminator_find_enum_define(expr) + if not enum_define: + add_enum('%sKind' % expr['union']) elif expr.has_key('type'): add_struct(expr) exprs.append(expr)
Signature · 1 line
1.7.1
PATCH v2 8/8 tests: add cases for inherited struct and union with discriminator
In reply to Wenchao XiaTest for inherit and complex union.
--- tests/qapi-schema/qapi-schema-test.json | 27 ++++++ tests/qapi-schema/qapi-schema-test.out | 11 +++ tests/test-qmp-input-visitor.c | 120 +++++++++++++++++++++++++ tests/test-qmp-output-visitor.c | 149 +++++++++++++++++++++++++++++++ 4 files changed, 307 insertions(+), 0 deletions(-)
tests/qapi-schema/qapi-schema-test.json +27 -0
@@ -21,8 +21,18 @@ 'dict1': { 'string1': 'str', 'dict2': { 'userdef1': 'UserDefOne', 'string2': 'str' }, '*dict3': { 'userdef2': 'UserDefOne', 'string3': 'str' } } } } +# for testing base +{ 'type': 'UserDefBase', + 'data': { 'string': 'str', 'enum1': 'EnumOne' } } + +{ 'type': 'UserDefInherit', + 'base': 'UserDefBase', + 'data': { 'boolean': 'bool', 'integer': 'int' } } # for testing unions +{ 'type': 'UserDefBase0', + 'data': { 'base-string0': 'str', 'base-enum0': 'EnumOne' } } + { 'type': 'UserDefA', 'data': { 'boolean': 'bool' } }
@@ -32,6 +42,23 @@ { 'union': 'UserDefUnion', 'data': { 'a' : 'UserDefA', 'b' : 'UserDefB' } } +{ 'union': 'UserDefBaseUnion', + 'base': 'UserDefBase0', + 'data': { 'a' : 'UserDefA', 'b' : 'UserDefB' } } + +{ 'union': 'UserDefDiscriminatorUnion', + 'base': 'UserDefBase0', + 'discriminator' : 'base-string0', + 'data': { 'a' : 'UserDefA', 'b' : 'UserDefB' } } + +# A complex type +{ 'union': 'UserDefEnumDiscriminatorUnion', + 'base': 'UserDefBase0', + 'discriminator' : 'base-enum0', + 'data': { 'value1' : 'UserDefA', + 'value2' : 'UserDefInherit', + 'value3' : 'UserDefB' } } + # for testing native lists { 'union': 'UserDefNativeListUnion', 'data': { 'integer': ['int'],
tests/qapi-schema/qapi-schema-test.out +11 -0
@@ -3,9 +3,15 @@ OrderedDict([('type', 'UserDefOne'), ('data', OrderedDict([('integer', 'int'), ('string', 'str'), ('*enum1', 'EnumOne')]))]), OrderedDict([('type', 'UserDefTwo'), ('data', OrderedDict([('string', 'str'), ('dict', OrderedDict([('string', 'str'), ('dict', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')])), ('*dict2', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')]))]))]))]), OrderedDict([('type', 'UserDefNested'), ('data', OrderedDict([('string0', 'str'), ('dict1', OrderedDict([('string1', 'str'), ('dict2', OrderedDict([('userdef1', 'UserDefOne'), ('string2', 'str')])), ('*dict3', OrderedDict([('userdef2', 'UserDefOne'), ('string3', 'str')]))]))]))]), + OrderedDict([('type', 'UserDefBase'), ('data', OrderedDict([('string', 'str'), ('enum1', 'EnumOne')]))]), + OrderedDict([('type', 'UserDefInherit'), ('base', 'UserDefBase'), ('data', OrderedDict([('boolean', 'bool'), ('integer', 'int')]))]), + OrderedDict([('type', 'UserDefBase0'), ('data', OrderedDict([('base-string0', 'str'), ('base-enum0', 'EnumOne')]))]), OrderedDict([('type', 'UserDefA'), ('data', OrderedDict([('boolean', 'bool')]))]), OrderedDict([('type', 'UserDefB'), ('data', OrderedDict([('integer', 'int')]))]), OrderedDict([('union', 'UserDefUnion'), ('data', OrderedDict([('a', 'UserDefA'), ('b', 'UserDefB')]))]), + OrderedDict([('union', 'UserDefBaseUnion'), ('base', 'UserDefBase0'), ('data', OrderedDict([('a', 'UserDefA'), ('b', 'UserDefB')]))]), + OrderedDict([('union', 'UserDefDiscriminatorUnion'), ('base', 'UserDefBase0'), ('discriminator', 'base-string0'), ('data', OrderedDict([('a', 'UserDefA'), ('b', 'UserDefB')]))]), + OrderedDict([('union', 'UserDefEnumDiscriminatorUnion'), ('base', 'UserDefBase0'), ('discriminator', 'base-enum0'), ('data', OrderedDict([('value1', 'UserDefA'), ('value2', 'UserDefInherit'), ('value3', 'UserDefB')]))]), OrderedDict([('union', 'UserDefNativeListUnion'), ('data', OrderedDict([('integer', ['int']), ('s8', ['int8']), ('s16', ['int16']), ('s32', ['int32']), ('s64', ['int64']), ('u8', ['uint8']), ('u16', ['uint16']), ('u32', ['uint32']), ('u64', ['uint64']), ('number', ['number']), ('boolean', ['bool']), ('string', ['str'])]))]), OrderedDict([('command', 'user_def_cmd'), ('data', OrderedDict())]), OrderedDict([('command', 'user_def_cmd1'), ('data', OrderedDict([('ud1a', 'UserDefOne')]))]),
@@ -13,11 +19,16 @@ OrderedDict([('type', 'UserDefOptions'), ('data', OrderedDict([('*i64', ['int']), ('*u64', ['uint64']), ('*u16', ['uint16']), ('*i64x', 'int'), ('*u64x', 'uint64')]))])] [{'enum_name': 'EnumOne', 'enum_values': ['value1', 'value2', 'value3']}, {'enum_name': 'UserDefUnionKind', 'enum_values': None}, + {'enum_name': 'UserDefBaseUnionKind', 'enum_values': None}, + {'enum_name': 'UserDefDiscriminatorUnionKind', 'enum_values': None}, {'enum_name': 'UserDefNativeListUnionKind', 'enum_values': None}] [OrderedDict([('type', 'NestedEnumsOne'), ('data', OrderedDict([('enum1', 'EnumOne'), ('*enum2', 'EnumOne'), ('enum3', 'EnumOne'), ('*enum4', 'EnumOne')]))]), OrderedDict([('type', 'UserDefOne'), ('data', OrderedDict([('integer', 'int'), ('string', 'str'), ('*enum1', 'EnumOne')]))]), OrderedDict([('type', 'UserDefTwo'), ('data', OrderedDict([('string', 'str'), ('dict', OrderedDict([('string', 'str'), ('dict', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')])), ('*dict2', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')]))]))]))]), OrderedDict([('type', 'UserDefNested'), ('data', OrderedDict([('string0', 'str'), ('dict1', OrderedDict([('string1', 'str'), ('dict2', OrderedDict([('userdef1', 'UserDefOne'), ('string2', 'str')])), ('*dict3', OrderedDict([('userdef2', 'UserDefOne'), ('string3', 'str')]))]))]))]), + OrderedDict([('type', 'UserDefBase'), ('data', OrderedDict([('string', 'str'), ('enum1', 'EnumOne')]))]), + OrderedDict([('type', 'UserDefInherit'), ('base', 'UserDefBase'), ('data', OrderedDict([('boolean', 'bool'), ('integer', 'int')]))]), + OrderedDict([('type', 'UserDefBase0'), ('data', OrderedDict([('base-string0', 'str'), ('base-enum0', 'EnumOne')]))]), OrderedDict([('type', 'UserDefA'), ('data', OrderedDict([('boolean', 'bool')]))]), OrderedDict([('type', 'UserDefB'), ('data', OrderedDict([('integer', 'int')]))]), OrderedDict([('type', 'UserDefOptions'), ('data', OrderedDict([('*i64', ['int']), ('*u64', ['uint64']), ('*u16', ['uint16']), ('*i64x', 'int'), ('*u64x', 'uint64')]))])]
tests/test-qmp-input-visitor.c +120 -0
140 lines not on this page · raw message
tests/test-qmp-output-visitor.c +149 -0
169 lines not on this page · raw message
Signature · 1 line
1.7.1
Just found timestamp of this series seems older than v1, so ping to
correct it.
Quoted 7 lines · On Wed, 13 Nov 2013 06:25:00 +0800 Wenchao Xia <[email protected]>…
This series is respined from RFC series at:
http://lists.nongnu.org/archive/html/qemu-devel/2013-11/msg00363.htmlPatch 1-6 add support for enum as discriminator.
Patch 7 improve enum name generation, nowAIOContext->AIO_CONTEXT, X86CPU->X86_CPU.
Patch 8 are the test cases.
Can you please clarify what is the problem this series is trying to solve, how it does it and provide before/after type of examples?
That's what I'd expect from an intro email, but this one has only a reference to an RFC series that has no better info, and some crypt changelog with magic numbers :(
Besides, this doesn't apply anymore...
Quoted 35 lines
Changes from RFC:
Mainly address Eric's comments: fix typo, add patch 2 to allow partly mapping
enum value in union, add related test case, remove direct inherit support "_base" and related test case.v2:
General:
3: use Raise exception instead of sys.error.write inqapi.py. Address Eric's comments:
2,3: more check for enum value at compile time.
8: correspond test case change.Wenchao Xia (8):
1 qapi script: remember enum values
2 qapi script: add check for duplicated key
3 qapi script: check correctness of discriminator values in union 4 qapi script: code move forgenerate_enum_name()
5 qapi script: use same function to generate enum string
6 qapi script: not generate hidden enum type for pre-defined enum discriminator 7 qapi script: do not add "_" for every capitalized char in enum
8 tests: add cases for inherited struct and union with discriminatorinclude/qapi/qmp/qerror.h | 2 +- scripts/qapi-types.py | 34 ++++---- scripts/qapi-visit.py | 55 +++++++++-- scripts/qapi.py | 84 ++++++++++++++++- target-i386/cpu.c | 2 +- tests/qapi-schema/comments.out | 2 +- tests/qapi-schema/qapi-schema-test.json | 27 ++++++ tests/qapi-schema/qapi-schema-test.out | 15 +++- tests/test-qmp-input-visitor.c | 120 +++++++++++++++++++++++++ tests/test-qmp-output-visitor.c | 149 +++++++++++++++++++++++++++++++ 10 files changed, 454 insertions(+), 36 deletions(-)
Quoted 11 lines · Luiz Capitulino
Quoted 7 lines · On Wed, 13 Nov 2013 06:25:00 +0800 Wenchao Xia <[email protected]>…
This series is respined from RFC series at:
http://lists.nongnu.org/archive/html/qemu-devel/2013-11/msg00363.htmlPatch 1-6 add support for enum as discriminator.
Patch 7 improve enum name generation, nowAIOContext->AIO_CONTEXT, X86CPU->X86_CPU.
Patch 8 are the test cases.
Quoted 9 lines
Can you please clarify what is the problem this series is trying to solve, how it does it and provide before/after type of examples?
That's what I'd expect from an intro email, but this one has only a reference to an RFC series that has no better info, and some crypt changelog with magic numbers :(
Besides, this doesn't apply anymore...
Let me have an introduction:
1. support using enum as discriminator in union. For example, if we have following define in qapi schema:
{ 'enum': 'EnumOne',
'data': [ 'value1', 'value2', 'value3' ] }
{ 'type': 'UserDefBase0',
'data': { 'base-string0': 'str', 'base-enum0': 'EnumOne' } }
Before this series, discriminator in union must be a string, and a hidden enum type as discriminator is generated. After this series, qapi schema can directly use predefined enum type:
{ 'union': 'UserDefEnumDiscriminatorUnion',
'base': 'UserDefBase0',
'discriminator' : 'base-enum0',
'data': { 'value1' : 'UserDefA',
'value2' : 'UserDefInherit',
'value3' : 'UserDefB' } }
The implement is done by:
1.1 remember the enum defines by qapi scripts.(patch 1)
1.2 use the remembered enum define to check correctness at compile time.(patch 3)
1.3 use the same enum name generation rule to avoid C code mismatch, esp for "case [ENUM_VALUE]" in qapi-visit.c.(patch 4,5)
1.4 when pre-defined enum type is used as discriminator, don't generate a hidden enum type, switch the code path to support enum as discriminator.
1.5 test case shows how it looks like.(Patch 8)
2. Better enum name generation
Before this patch, AIOContext->A_I_O_Context, after this patch, AIOContet->AIO_Context. Since previous patch has foldered enum name generation codes into one function, it is done easily by modifying it.(Patch 7)
Quoted 36 lines
Quoted 35 lines
Changes from RFC:
Mainly address Eric's comments: fix typo, add patch 2 to allow partly mapping
enum value in union, add related test case, remove direct inherit support "_base" and related test case.v2:
General:
3: use Raise exception instead of sys.error.write inqapi.py. Address Eric's comments:
2,3: more check for enum value at compile time.
8: correspond test case change.Wenchao Xia (8):
1 qapi script: remember enum values
2 qapi script: add check for duplicated key
3 qapi script: check correctness of discriminator values in union 4 qapi script: code move forgenerate_enum_name()
5 qapi script: use same function to generate enum string
6 qapi script: not generate hidden enum type for pre-defined enum discriminator 7 qapi script: do not add "_" for every capitalized char in enum
8 tests: add cases for inherited struct and union with discriminatorinclude/qapi/qmp/qerror.h | 2 +- scripts/qapi-types.py | 34 ++++---- scripts/qapi-visit.py | 55 +++++++++-- scripts/qapi.py | 84 ++++++++++++++++- target-i386/cpu.c | 2 +- tests/qapi-schema/comments.out | 2 +- tests/qapi-schema/qapi-schema-test.json | 27 ++++++ tests/qapi-schema/qapi-schema-test.out | 15 +++- tests/test-qmp-input-visitor.c | 120 +++++++++++++++++++++++++ tests/test-qmp-output-visitor.c | 149 +++++++++++++++++++++++++++++++ 10 files changed, 454 insertions(+), 36 deletions(-)
Quoted 31 lines · On Thu, 28 Nov 2013 14:19:48 +0800 Wenchao Xia <[email protected]>…
Quoted 11 lines · Luiz Capitulino
Quoted 7 lines · On Wed, 13 Nov 2013 06:25:00 +0800 Wenchao Xia <[email protected]>…
This series is respined from RFC series at:
http://lists.nongnu.org/archive/html/qemu-devel/2013-11/msg00363.htmlPatch 1-6 add support for enum as discriminator.
Patch 7 improve enum name generation, nowAIOContext->AIO_CONTEXT, X86CPU->X86_CPU.
Patch 8 are the test cases.Quoted 9 lines
Can you please clarify what is the problem this series is trying to solve, how it does it and provide before/after type of examples?
That's what I'd expect from an intro email, but this one has only a reference to an RFC series that has no better info, and some crypt changelog with magic numbers :(
Besides, this doesn't apply anymore...
Let me have an introduction:
1. support using enum as discriminator in union. For example, if we have following define in qapi schema:
{ 'enum': 'EnumOne',
'data': [ 'value1', 'value2', 'value3' ] }{ 'type': 'UserDefBase0',
'data': { 'base-string0': 'str', 'base-enum0': 'EnumOne' } }
Quoted 9 lines
Before this series, discriminator in union must be a string, and a hidden enum type as discriminator is generated. After this series, qapi schema can directly use predefined enum type:
{ 'union': 'UserDefEnumDiscriminatorUnion',
'base': 'UserDefBase0',
'discriminator' : 'base-enum0',
'data': { 'value1' : 'UserDefA',
'value2' : 'UserDefInherit',
'value3' : 'UserDefB' } }
OK, now tell me who will benefit from this. You don't have to answer right now, you can resend your series with this information. Note that I'm not asking to be convinced, I'm asking to have enough info so that I can understand what this is about when I get to review this (and this is just what an intro email is about).
Quoted 55 lines
The implement is done by:
1.1 remember the enum defines by qapi scripts.(patch 1)
1.2 use the remembered enum define to check correctness at compile time.(patch 3)
1.3 use the same enum name generation rule to avoid C code mismatch, esp for "case [ENUM_VALUE]" in qapi-visit.c.(patch 4,5)
1.4 when pre-defined enum type is used as discriminator, don't generate a hidden enum type, switch the code path to support enum as discriminator.
1.5 test case shows how it looks like.(Patch 8)2. Better enum name generation
Before this patch,AIOContext->A_I_O_Context, after this patch,AIOContet->AIO_Context. Since previous patch has foldered enum name generation codes into one function, it is done easily by modifying it.(Patch 7)Quoted 36 lines
Quoted 35 lines
Changes from RFC:
Mainly address Eric's comments: fix typo, add patch 2 to allow partly mapping
enum value in union, add related test case, remove direct inherit support "_base" and related test case.v2:
General:
3: use Raise exception instead of sys.error.write inqapi.py. Address Eric's comments:
2,3: more check for enum value at compile time.
8: correspond test case change.Wenchao Xia (8):
1 qapi script: remember enum values
2 qapi script: add check for duplicated key
3 qapi script: check correctness of discriminator values in union 4 qapi script: code move forgenerate_enum_name()
5 qapi script: use same function to generate enum string
6 qapi script: not generate hidden enum type for pre-defined enum discriminator 7 qapi script: do not add "_" for every capitalized char in enum
8 tests: add cases for inherited struct and union with discriminatorinclude/qapi/qmp/qerror.h | 2 +- scripts/qapi-types.py | 34 ++++---- scripts/qapi-visit.py | 55 +++++++++-- scripts/qapi.py | 84 ++++++++++++++++- target-i386/cpu.c | 2 +- tests/qapi-schema/comments.out | 2 +- tests/qapi-schema/qapi-schema-test.json | 27 ++++++ tests/qapi-schema/qapi-schema-test.out | 15 +++- tests/test-qmp-input-visitor.c | 120 +++++++++++++++++++++++++ tests/test-qmp-output-visitor.c | 149 +++++++++++++++++++++++++++++++ 10 files changed, 454 insertions(+), 36 deletions(-)
Am 12.11.2013 um 23:25 hat Wenchao Xia geschrieben:
Quoted 30 lines
This series is respined from RFC series at:
http://lists.nongnu.org/archive/html/qemu-devel/2013-11/msg00363.htmlPatch 1-6 add support for enum as discriminator.
Patch 7 improve enum name generation, nowAIOContext->AIO_CONTEXT, X86CPU->X86_CPU.
Patch 8 are the test cases.Changes from RFC:
Mainly address Eric's comments: fix typo, add patch 2 to allow partly mapping
enum value in union, add related test case, remove direct inherit support "_base" and related test case.v2:
General:
3: use Raise exception instead of sys.error.write inqapi.py. Address Eric's comments:
2,3: more check for enum value at compile time.
8: correspond test case change.Wenchao Xia (8):
1 qapi script: remember enum values
2 qapi script: add check for duplicated key
3 qapi script: check correctness of discriminator values in union 4 qapi script: code move forgenerate_enum_name()
5 qapi script: use same function to generate enum string
6 qapi script: not generate hidden enum type for pre-defined enum discriminator 7 qapi script: do not add "_" for every capitalized char in enum
8 tests: add cases for inherited struct and union with discriminator
Quoted 11 lines
include/qapi/qmp/qerror.h | 2 +- scripts/qapi-types.py | 34 ++++---- scripts/qapi-visit.py | 55 +++++++++-- scripts/qapi.py | 84 ++++++++++++++++- target-i386/cpu.c | 2 +- tests/qapi-schema/comments.out | 2 +- tests/qapi-schema/qapi-schema-test.json | 27 ++++++ tests/qapi-schema/qapi-schema-test.out | 15 +++- tests/test-qmp-input-visitor.c | 120 +++++++++++++++++++++++++ tests/test-qmp-output-visitor.c | 149 +++++++++++++++++++++++++++++++ 10 files changed, 454 insertions(+), 36 deletions(-)
I think there should be an update for docs/qapi-code-gen.txt somewhere in the series. If you put it in the same patch as the actual generator change, you can simplify the commit message by pointing to the hunk modifying the documentation.
Kevin
Quoted 44 lines · Luiz Capitulino
Quoted 35 lines · On Thu, 28 Nov 2013 14:19:48 +0800 Wenchao Xia <[email protected]>…
Quoted 11 lines · Luiz Capitulino
Quoted 7 lines · On Wed, 13 Nov 2013 06:25:00 +0800 Wenchao Xia <[email protected]>…
This series is respined from RFC series at:
http://lists.nongnu.org/archive/html/qemu-devel/2013-11/msg00363.htmlPatch 1-6 add support for enum as discriminator.
Patch 7 improve enum name generation, nowAIOContext->AIO_CONTEXT, X86CPU->X86_CPU.
Patch 8 are the test cases.Quoted 9 lines
Can you please clarify what is the problem this series is trying to solve, how it does it and provide before/after type of examples?
That's what I'd expect from an intro email, but this one has only a reference to an RFC series that has no better info, and some crypt changelog with magic numbers :(
Besides, this doesn't apply anymore...
Let me have an introduction:
1. support using enum as discriminator in union. For example, if we have following define in qapi schema:
{ 'enum': 'EnumOne','data': [ 'value1', 'value2', 'value3' ] }{ 'type': 'UserDefBase0',
'data': { 'base-string0': 'str', 'base-enum0': 'EnumOne' } }Before this series, discriminator in union must be a string, and a hidden enum type as discriminator is generated. After this series, qapi schema can directly use predefined enum type:
{ 'union': 'UserDefEnumDiscriminatorUnion',Quoted 5 lines
'base': 'UserDefBase0', 'discriminator' : 'base-enum0', 'data': { 'value1' : 'UserDefA', 'value2' : 'UserDefInherit', 'value3' : 'UserDefB' } }
Quoted 6 lines
OK, now tell me who will benefit from this. You don't have to answer right now, you can resend your series with this information. Note that I'm not asking to be convinced, I'm asking to have enough info so that I can understand what this is about when I get to review this (and this is just what an intro email is about).
OK, I will send v3 with better doc.
Quoted 57 lines
Quoted 55 lines
The implement is done by:
1.1 remember the enum defines by qapi scripts.(patch 1)
1.2 use the remembered enum define to check correctness at compile time.(patch 3)
1.3 use the same enum name generation rule to avoid C code mismatch, esp for "case [ENUM_VALUE]" in qapi-visit.c.(patch 4,5)
1.4 when pre-defined enum type is used as discriminator, don't generate a hidden enum type, switch the code path to support enum as discriminator.
1.5 test case shows how it looks like.(Patch 8)2. Better enum name generation
Before this patch,AIOContext->A_I_O_Context, after this patch,AIOContet->AIO_Context. Since previous patch has foldered enum name generation codes into one function, it is done easily by modifying it.(Patch 7)Quoted 36 lines
Quoted 35 lines
Changes from RFC:
Mainly address Eric's comments: fix typo, add patch 2 to allow partly mapping
enum value in union, add related test case, remove direct inherit support "_base" and related test case.v2:
General:
3: use Raise exception instead of sys.error.write inqapi.py. Address Eric's comments:
2,3: more check for enum value at compile time.
8: correspond test case change.Wenchao Xia (8):
1 qapi script: remember enum values
2 qapi script: add check for duplicated key
3 qapi script: check correctness of discriminator values in union 4 qapi script: code move forgenerate_enum_name()
5 qapi script: use same function to generate enum string
6 qapi script: not generate hidden enum type for pre-defined enum discriminator 7 qapi script: do not add "_" for every capitalized char in enum
8 tests: add cases for inherited struct and union with discriminatorinclude/qapi/qmp/qerror.h | 2 +- scripts/qapi-types.py | 34 ++++---- scripts/qapi-visit.py | 55 +++++++++-- scripts/qapi.py | 84 ++++++++++++++++- target-i386/cpu.c | 2 +- tests/qapi-schema/comments.out | 2 +- tests/qapi-schema/qapi-schema-test.json | 27 ++++++ tests/qapi-schema/qapi-schema-test.out | 15 +++- tests/test-qmp-input-visitor.c | 120 +++++++++++++++++++++++++ tests/test-qmp-output-visitor.c | 149 +++++++++++++++++++++++++++++++ 10 files changed, 454 insertions(+), 36 deletions(-)
Re: [Qemu-devel] [PATCH V2 2/8] qapi script: add check for duplicated key
In reply to Wenchao XiaQuoted 7 lines · Wenchao Xia
It is bad that same key was specified twice, especially when a union have two branches with same condition. This patch can prevent it.
Signed-off-by Wenchao Xia <[email protected]>--- scripts/qapi.py | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
Quoted 15 lines
scripts/qapi.py +2
-0@@ -116,6 +116,8 @@ class QAPISchema: if self.tok != ':': raise QAPISchemaError(self, 'Expected ":"') self.accept() + if key in expr: + raise QAPISchemaError(self, 'Duplicated key "%s"' % key) expr[key] = self.get_expr(True) if self.tok == '}': self.accept()
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH V2 3/8] qapi script: check correctness of discriminator values in union
In reply to Wenchao XiaQuoted 9 lines · Wenchao Xia
It will check whether the values specified are written correctly, and whether all enum values are covered, when discriminator is a pre-defined enum type
Signed-off-by Wenchao Xia <[email protected]>--- scripts/qapi-visit.py | 17 +++++++++++++++++ scripts/qapi.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 0 deletions(-)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH V2 5/8] qapi script: use same function to generate enum string
In reply to Wenchao XiaQuoted 10 lines · Wenchao Xia
One function one rule, so the enum string generating have same behavior for different caller. If multiple caller exist for one enum define in schema, it is for sure the generated string is identical.
Note before the patch
qapi-visit.pyused custom function to generate the string in union visit, although the patch changes it, the final string generated is not changed. The custom function used before will met problem when capitalized discriminator value is introduced.
I'm finding this commit message awkward to read and rather wordy. May I suggest the shorter:
Prior to this patch, qapi-visit.py used custom code to generate enum names used for handling a qapi union. Fix it to instead reuse common code, with identical generated results, and allowing future updates to generation to only need to touch one place.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH V2 7/8] qapi script: do not add "_" for every capitalized char in enum
In reply to Wenchao XiaQuoted 10 lines · Wenchao Xia
Now "enum AIOContext" will generate
AIO_CONTEXTinstead ofA_I_O_CONTEXT, "X86CPU" will generateX86_CPUinstead ofX86_C_P_U.Signed-off-by Wenchao Xia <[email protected]>--- include/qapi/qmp/qerror.h | 2 +- scripts/qapi.py | 26 +++++++++++++++++++------- target-i386/cpu.c | 2 +- 3 files changed, 21 insertions(+), 9 deletions(-)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH V2 5/8] qapi script: use same function to generate enum string
In reply to Eric BlakeQuoted 12 lines · Eric Blake
Quoted 10 lines · Wenchao Xia
One function one rule, so the enum string generating have same behavior for different caller. If multiple caller exist for one enum define in schema, it is for sure the generated string is identical.
Note before the patch
qapi-visit.pyused custom function to generate the string in union visit, although the patch changes it, the final string generated is not changed. The custom function used before will met problem when capitalized discriminator value is introduced.
Quoted 8 lines
I'm finding this commit message awkward to read and rather wordy. May I suggest the shorter:
Prior to this patch,
qapi-visit.pyused custom code to generate enum names used for handling a qapi union. Fix it to instead reuse common code, with identical generated results, and allowing future updates to generation to only need to touch one place.
That sounds better, thanks for reviewing. I have sent v3 before at:
http://lists.nongnu.org/archive/html/qemu-devel/2013-11/msg03980.html
There is no change except better intro/doc, will use the new commit message in V4.