qapi: Enforce union and alternate branch naming rules
authorMarkus Armbruster <armbru@redhat.com>
Tue, 23 Mar 2021 09:40:25 +0000 (10:40 +0100)
committerMarkus Armbruster <armbru@redhat.com>
Tue, 23 Mar 2021 21:31:53 +0000 (22:31 +0100)
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]

scripts/qapi/expr.py
tests/qapi-schema/alternate-clash.err
tests/qapi-schema/alternate-clash.json
tests/qapi-schema/qapi-schema-test.json
tests/qapi-schema/qapi-schema-test.out
tests/qapi-schema/union-clash-branches.err
tests/qapi-schema/union-clash-branches.json

index d968609c48d4d23a6db3a536555665b2405838cb..540b3982b10470e0a7dc15f230c483fb160b3749 100644 (file)
@@ -274,7 +274,7 @@ def check_union(expr, info):
     for (key, value) in members.items():
         source = "'data' member '%s'" % key
         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
         check_keys(value, info, source, ['type'], ['if'])
         check_if(value, info, source)
@@ -288,7 +288,7 @@ def check_alternate(expr, info):
         raise QAPISemError(info, "'data' must not be empty")
     for (key, value) in members.items():
         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_if(value, info, source)
         check_type(value['type'], info, source)
index 0fe02f2c996dcf22e23ea695413be024f87526c4..caa2d42e3f70f8b9b4f88f4ac508dd66fbdb95fd 100644 (file)
@@ -1,2 +1,2 @@
 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 '_'
index 039c4be65811e55587092e2f23fd5a835575cde0..87f061a74afe93ad1ffd5c90b61b2c22ee5985ca 100644 (file)
@@ -1,5 +1,7 @@
 # Alternate branch name collision
-# Reject an alternate that would result in a collision in generated C
-# names (this would try to generate two union members named 'a_b').
+# Naming rules make collision impossible (even with the pragma).  If
+# 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',
   'data': { 'a-b': 'bool', 'a_b': 'int' } }
index 387678acbb6bd07a00fec3711e5c976a7ecd20cf..84b9d41f158b815157c105b152afca8169621008 100644 (file)
 
 { 'union': 'TestIfUnion', 'data':
   { '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)' }
 
 { 'command': 'test-if-union-cmd',
index 51efe5d7cdd7a317740295a19d170b02a9671d42..e0b8a5f0b69ab03ce7295e900ab65cc197a115da 100644 (file)
@@ -309,14 +309,14 @@ object q_obj_TestStruct-wrapper
     member data: TestStruct optional=False
 enum TestIfUnionKind
     member foo
-    member union_bar
+    member bar
         if ['defined(TEST_IF_UNION_BAR)']
     if ['defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)']
 object TestIfUnion
     member type: TestIfUnionKind optional=False
     tag type
     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) && defined(TEST_IF_STRUCT)']
 object q_obj_test-if-union-cmd-arg
index 73bbc2cabd09815ea0cff5f1f48b6663a46bbc67..ef5364572819785961341e685627e3dde75f8097 100644 (file)
@@ -1,2 +1,2 @@
 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 '_'
index 3bece8c948b90ed57655d1dc37d941def6a5ebeb..7bdda0b0da8009bf124d1f8eefcacd9406f3c0ae 100644 (file)
@@ -1,5 +1,7 @@
 # Union branch name collision
-# Reject a union that would result in a collision in generated C names (this
-# would try to generate two members 'a_b').
+# Naming rules make collision impossible (even with the pragma).  If
+# 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',
   'data': { 'a-b': 'int', 'a_b': 'str' } }