qapi: Enforce union and alternate branch naming rules

Union branch names should use '-', not '_'.  Enforce this.  The only
offenders are in tests/.  Fix them.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20210323094025.3569441-29-armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
[Commit message typo fixed]
This commit is contained in:
Markus Armbruster 2021-03-23 10:40:25 +01:00
parent 407efbf9e7
commit d83b47646e
7 changed files with 15 additions and 11 deletions

View File

@ -274,7 +274,7 @@ def check_union(expr, info):
for (key, value) in members.items(): for (key, value) in members.items():
source = "'data' member '%s'" % key source = "'data' member '%s'" % key
if discriminator is None: if discriminator is None:
check_name_lower(key, info, source, permit_underscore=True) check_name_lower(key, info, source)
# else: name is in discriminator enum, which gets checked # else: name is in discriminator enum, which gets checked
check_keys(value, info, source, ['type'], ['if']) check_keys(value, info, source, ['type'], ['if'])
check_if(value, info, source) check_if(value, info, source)
@ -288,7 +288,7 @@ def check_alternate(expr, info):
raise QAPISemError(info, "'data' must not be empty") raise QAPISemError(info, "'data' must not be empty")
for (key, value) in members.items(): for (key, value) in members.items():
source = "'data' member '%s'" % key source = "'data' member '%s'" % key
check_name_lower(key, info, source, permit_underscore=True) check_name_lower(key, info, source)
check_keys(value, info, source, ['type'], ['if']) check_keys(value, info, source, ['type'], ['if'])
check_if(value, info, source) check_if(value, info, source)
check_type(value['type'], info, source) check_type(value['type'], info, source)

View File

@ -1,2 +1,2 @@
alternate-clash.json: In alternate 'Alt1': alternate-clash.json: In alternate 'Alt1':
alternate-clash.json:4: branch 'a_b' collides with branch 'a-b' alternate-clash.json:6: name of 'data' member 'a_b' must not use uppercase or '_'

View File

@ -1,5 +1,7 @@
# Alternate branch name collision # Alternate branch name collision
# Reject an alternate that would result in a collision in generated C # Naming rules make collision impossible (even with the pragma). If
# names (this would try to generate two union members named 'a_b'). # that wasn't the case, then we'd get a collision in generated C: two
# union members a_b.
{ 'pragma': { 'member-name-exceptions': [ 'Alt1' ] } }
{ 'alternate': 'Alt1', { 'alternate': 'Alt1',
'data': { 'a-b': 'bool', 'a_b': 'int' } } 'data': { 'a-b': 'bool', 'a_b': 'int' } }

View File

@ -231,7 +231,7 @@
{ 'union': 'TestIfUnion', 'data': { 'union': 'TestIfUnion', 'data':
{ 'foo': 'TestStruct', { 'foo': 'TestStruct',
'union_bar': { 'type': 'str', 'if': 'defined(TEST_IF_UNION_BAR)'} }, 'bar': { 'type': 'str', 'if': 'defined(TEST_IF_UNION_BAR)'} },
'if': 'defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)' } 'if': 'defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)' }
{ 'command': 'test-if-union-cmd', { 'command': 'test-if-union-cmd',

View File

@ -309,14 +309,14 @@ object q_obj_TestStruct-wrapper
member data: TestStruct optional=False member data: TestStruct optional=False
enum TestIfUnionKind enum TestIfUnionKind
member foo member foo
member union_bar member bar
if ['defined(TEST_IF_UNION_BAR)'] if ['defined(TEST_IF_UNION_BAR)']
if ['defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)'] if ['defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)']
object TestIfUnion object TestIfUnion
member type: TestIfUnionKind optional=False member type: TestIfUnionKind optional=False
tag type tag type
case foo: q_obj_TestStruct-wrapper case foo: q_obj_TestStruct-wrapper
case union_bar: q_obj_str-wrapper case bar: q_obj_str-wrapper
if ['defined(TEST_IF_UNION_BAR)'] if ['defined(TEST_IF_UNION_BAR)']
if ['defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)'] if ['defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)']
object q_obj_test-if-union-cmd-arg object q_obj_test-if-union-cmd-arg

View File

@ -1,2 +1,2 @@
union-clash-branches.json: In union 'TestUnion': union-clash-branches.json: In union 'TestUnion':
union-clash-branches.json:4: branch 'a_b' collides with branch 'a-b' union-clash-branches.json:6: name of 'data' member 'a_b' must not use uppercase or '_'

View File

@ -1,5 +1,7 @@
# Union branch name collision # Union branch name collision
# Reject a union that would result in a collision in generated C names (this # Naming rules make collision impossible (even with the pragma). If
# would try to generate two members 'a_b'). # that wasn't the case, then we'd get collisions in generated C: two
# union members a_b, and two enum members TEST_UNION_A_B.
{ 'pragma': { 'member-name-exceptions': [ 'TestUnion' ] } }
{ 'union': 'TestUnion', { 'union': 'TestUnion',
'data': { 'a-b': 'int', 'a_b': 'str' } } 'data': { 'a-b': 'int', 'a_b': 'str' } }