diff mbox series

[22/28] qapi: Prepare for rejecting underscore in command and member names

Message ID 20210323094025.3569441-23-armbru@redhat.com
State New
Headers show
Series qapi: Enforce naming rules | expand

Commit Message

Markus Armbruster March 23, 2021, 9:40 a.m. UTC
Command names and member names within a type should be all lower case
with words separated by a hyphen.  We also accept underscore.  Rework
check_name_lower() to optionally reject underscores, but don't use
that option, yet.

Update expected test output for the changed error message.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 scripts/qapi/expr.py                    | 24 ++++++++++++++----------
 tests/qapi-schema/args-member-case.err  |  2 +-
 tests/qapi-schema/enum-member-case.err  |  2 +-
 tests/qapi-schema/union-branch-case.err |  2 +-
 4 files changed, 17 insertions(+), 13 deletions(-)

Comments

Eric Blake March 23, 2021, 3:15 p.m. UTC | #1
On 3/23/21 4:40 AM, Markus Armbruster wrote:
> Command names and member names within a type should be all lower case
> with words separated by a hyphen.  We also accept underscore.  Rework
> check_name_lower() to optionally reject underscores, but don't use
> that option, yet.
> 
> Update expected test output for the changed error message.
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>  scripts/qapi/expr.py                    | 24 ++++++++++++++----------
>  tests/qapi-schema/args-member-case.err  |  2 +-
>  tests/qapi-schema/enum-member-case.err  |  2 +-
>  tests/qapi-schema/union-branch-case.err |  2 +-
>  4 files changed, 17 insertions(+), 13 deletions(-)
> 

Reviewed-by: Eric Blake <eblake@redhat.com>
diff mbox series

Patch

diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py
index 30fd37aa24..ce37b426c9 100644
--- a/scripts/qapi/expr.py
+++ b/scripts/qapi/expr.py
@@ -51,12 +51,13 @@  def check_name_upper(name, info, source):
 
 
 def check_name_lower(name, info, source,
-                     permit_upper=False):
+                     permit_upper=False,
+                     permit_underscore=False):
     stem = check_name_str(name, info, source)
-    if not permit_upper and re.search(r'[A-Z]', stem):
+    if ((not permit_upper and re.search(r'[A-Z]', stem))
+            or (not permit_underscore and '_' in stem)):
         raise QAPISemError(
-            info, "%s uses uppercase in name" % source)
-    # TODO reject '_' in stem
+            info, "name of %s must not use uppercase or '_'" % source)
 
 
 def check_name_camel(name, info, source):
@@ -69,7 +70,8 @@  def check_defn_name_str(name, info, meta):
     if meta == 'event':
         check_name_upper(name, info, meta)
     elif meta == 'command':
-        check_name_lower(name, info, meta, permit_upper=True)
+        check_name_lower(name, info, meta,
+                         permit_upper=True, permit_underscore=True)
     else:
         check_name_camel(name, info, meta)
     if name.endswith('Kind') or name.endswith('List'):
@@ -188,7 +190,8 @@  def check_type(value, info, source,
         key_source = "%s member '%s'" % (source, key)
         if key.startswith('*'):
             key = key[1:]
-        check_name_lower(key, info, key_source, permit_upper)
+        check_name_lower(key, info, key_source,
+                         permit_upper, permit_underscore=True)
         if c_name(key, False) == 'u' or c_name(key, False).startswith('has_'):
             raise QAPISemError(info, "%s uses reserved name" % key_source)
         check_keys(arg, info, key_source, ['type'], ['if', 'features'])
@@ -210,7 +213,7 @@  def check_features(features, info):
         check_keys(f, info, source, ['name'], ['if'])
         check_name_is_str(f['name'], info, source)
         source = "%s '%s'" % (source, f['name'])
-        check_name_lower(f['name'], info, source)
+        check_name_lower(f['name'], info, source, permit_underscore=True)
         check_if(f, info, source)
 
 
@@ -237,7 +240,8 @@  def check_enum(expr, info):
         # Enum members may start with a digit
         if member_name[0].isdigit():
             member_name = 'd' + member_name # Hack: hide the digit
-        check_name_lower(member_name, info, source, permit_upper)
+        check_name_lower(member_name, info, source,
+                         permit_upper, permit_underscore=True)
         check_if(member, info, source)
 
 
@@ -267,7 +271,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)
+            check_name_lower(key, info, source, permit_underscore=True)
         # else: name is in discriminator enum, which gets checked
         check_keys(value, info, source, ['type'], ['if'])
         check_if(value, info, source)
@@ -281,7 +285,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)
+        check_name_lower(key, info, source, permit_underscore=True)
         check_keys(value, info, source, ['type'], ['if'])
         check_if(value, info, source)
         check_type(value['type'], info, source)
diff --git a/tests/qapi-schema/args-member-case.err b/tests/qapi-schema/args-member-case.err
index 4f33dbbc38..25c3910a17 100644
--- a/tests/qapi-schema/args-member-case.err
+++ b/tests/qapi-schema/args-member-case.err
@@ -1,2 +1,2 @@ 
 args-member-case.json: In command 'no-way-this-will-get-whitelisted':
-args-member-case.json:2: 'data' member 'Arg' uses uppercase in name
+args-member-case.json:2: name of 'data' member 'Arg' must not use uppercase or '_'
diff --git a/tests/qapi-schema/enum-member-case.err b/tests/qapi-schema/enum-member-case.err
index 8b3aefe37a..c813522c50 100644
--- a/tests/qapi-schema/enum-member-case.err
+++ b/tests/qapi-schema/enum-member-case.err
@@ -1,2 +1,2 @@ 
 enum-member-case.json: In enum 'NoWayThisWillGetWhitelisted':
-enum-member-case.json:4: 'data' member 'Value' uses uppercase in name
+enum-member-case.json:4: name of 'data' member 'Value' must not use uppercase or '_'
diff --git a/tests/qapi-schema/union-branch-case.err b/tests/qapi-schema/union-branch-case.err
index b1e9417303..d2d5cb8993 100644
--- a/tests/qapi-schema/union-branch-case.err
+++ b/tests/qapi-schema/union-branch-case.err
@@ -1,2 +1,2 @@ 
 union-branch-case.json: In union 'Uni':
-union-branch-case.json:2: 'data' member 'Branch' uses uppercase in name
+union-branch-case.json:2: name of 'data' member 'Branch' must not use uppercase or '_'