qapi: Enforce command naming rules

Command names should be lower-case.  Enforce this.  Fix the fixable
offenders (all in tests/), and add the remainder to pragma
command-name-exceptions.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20210323094025.3569441-25-armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Markus Armbruster 2021-03-23 10:40:21 +01:00
parent e744708a77
commit 05ebf841ef
8 changed files with 62 additions and 31 deletions

View File

@ -149,6 +149,7 @@ prevent incomplete include files.
Syntax: Syntax:
PRAGMA = { 'pragma': { PRAGMA = { 'pragma': {
'*doc-required': BOOL, '*doc-required': BOOL,
'*command-name-exceptions': [ STRING, ... ],
'*command-returns-exceptions': [ STRING, ... ], '*command-returns-exceptions': [ STRING, ... ],
'*member-name-exceptions': [ STRING, ... ] } } '*member-name-exceptions': [ STRING, ... ] } }
@ -160,6 +161,9 @@ pragma to different values in parts of the schema doesn't work.
Pragma 'doc-required' takes a boolean value. If true, documentation Pragma 'doc-required' takes a boolean value. If true, documentation
is required. Default is false. is required. Default is false.
Pragma 'command-name-exceptions' takes a list of commands whose names
may contain '_' instead of '-'. Default is none.
Pragma 'command-returns-exceptions' takes a list of commands that may Pragma 'command-returns-exceptions' takes a list of commands that may
violate the rules on permitted return types. Default is none. violate the rules on permitted return types. Default is none.
@ -756,8 +760,8 @@ Any name (command, event, type, member, or enum value) beginning with
"x-" is marked experimental, and may be withdrawn or changed "x-" is marked experimental, and may be withdrawn or changed
incompatibly in a future release. incompatibly in a future release.
Pragma 'member-name-exceptions' lets you violate the rules on use of Pragmas 'command-name-exceptions' and 'member-name-exceptions' let you
upper and lower case. Use for new code is strongly discouraged. violate naming rules. Use for new code is strongly discouraged.
=== Downstream extensions === === Downstream extensions ===

View File

@ -4,6 +4,24 @@
# add to them! # add to them!
{ 'pragma': { { 'pragma': {
# Commands allowed to return a non-dictionary: # Commands allowed to return a non-dictionary:
'command-name-exceptions': [
'add_client',
'block_passwd',
'block_resize',
'block_set_io_throttle',
'client_migrate_info',
'device_add',
'device_del',
'expire_password',
'migrate_cancel',
'netdev_add',
'netdev_del',
'qmp_capabilities',
'set_link',
'set_password',
'system_powerdown',
'system_reset',
'system_wakeup' ],
'command-returns-exceptions': [ 'command-returns-exceptions': [
'human-monitor-command', 'human-monitor-command',
'qom-get', 'qom-get',

View File

@ -70,8 +70,9 @@ def check_defn_name_str(name, info, meta):
if meta == 'event': if meta == 'event':
check_name_upper(name, info, meta) check_name_upper(name, info, meta)
elif meta == 'command': elif meta == 'command':
check_name_lower(name, info, meta, check_name_lower(
permit_upper=True, permit_underscore=True) name, info, meta,
permit_underscore=name in info.pragma.command_name_exceptions)
else: else:
check_name_camel(name, info, meta) check_name_camel(name, info, meta)
if name.endswith('Kind') or name.endswith('List'): if name.endswith('Kind') or name.endswith('List'):

View File

@ -132,6 +132,9 @@ class QAPISchemaParser:
raise QAPISemError(info, raise QAPISemError(info,
"pragma 'doc-required' must be boolean") "pragma 'doc-required' must be boolean")
info.pragma.doc_required = value info.pragma.doc_required = value
elif name == 'command-name-exceptions':
self._check_pragma_list_of_str(name, value, info)
info.pragma.command_name_exceptions = value
elif name == 'command-returns-exceptions': elif name == 'command-returns-exceptions':
self._check_pragma_list_of_str(name, value, info) self._check_pragma_list_of_str(name, value, info)
info.pragma.command_returns_exceptions = value info.pragma.command_returns_exceptions = value

View File

@ -21,6 +21,8 @@ class QAPISchemaPragma:
def __init__(self) -> None: def __init__(self) -> None:
# Are documentation comments required? # Are documentation comments required?
self.doc_required = False self.doc_required = False
# Commands whose names may use '_'
self.command_name_exceptions: List[str] = []
# Commands allowed to return a non-dictionary # Commands allowed to return a non-dictionary
self.command_returns_exceptions: List[str] = [] self.command_returns_exceptions: List[str] = []
# Types whose member names may violate case conventions # Types whose member names may violate case conventions

View File

@ -31,7 +31,7 @@
'base': { 'type': 'EnumOne' }, 'discriminator': 'type', 'base': { 'type': 'EnumOne' }, 'discriminator': 'type',
'data': { } } 'data': { } }
{ 'command': 'user_def_cmd0', 'data': 'Empty2', 'returns': 'Empty2' } { 'command': 'user-def-cmd0', 'data': 'Empty2', 'returns': 'Empty2' }
# for testing override of default naming heuristic # for testing override of default naming heuristic
{ 'enum': 'QEnumTwo', { 'enum': 'QEnumTwo',
@ -141,9 +141,9 @@
{ 'include': 'include/sub-module.json' } { 'include': 'include/sub-module.json' }
# testing commands # testing commands
{ 'command': 'user_def_cmd', 'data': {} } { 'command': 'user-def-cmd', 'data': {} }
{ 'command': 'user_def_cmd1', 'data': {'ud1a': 'UserDefOne'} } { 'command': 'user-def-cmd1', 'data': {'ud1a': 'UserDefOne'} }
{ 'command': 'user_def_cmd2', { 'command': 'user-def-cmd2',
'data': {'ud1a': {'type': 'UserDefOne'}, '*ud1b': 'UserDefOne'}, 'data': {'ud1a': {'type': 'UserDefOne'}, '*ud1b': 'UserDefOne'},
'returns': 'UserDefTwo' } 'returns': 'UserDefTwo' }
@ -230,7 +230,8 @@
'union_bar': { 'type': 'str', 'if': 'defined(TEST_IF_UNION_BAR)'} }, 'union_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': 'TestIfUnionCmd', 'data': { 'union_cmd_arg': 'TestIfUnion' }, { 'command': 'test-if-union-cmd',
'data': { 'union_cmd_arg': 'TestIfUnion' },
'if': 'defined(TEST_IF_UNION)' } 'if': 'defined(TEST_IF_UNION)' }
{ 'alternate': 'TestIfAlternate', 'data': { 'alternate': 'TestIfAlternate', 'data':
@ -238,16 +239,18 @@
'bar': { 'type': 'TestStruct', 'if': 'defined(TEST_IF_ALT_BAR)'} }, 'bar': { 'type': 'TestStruct', 'if': 'defined(TEST_IF_ALT_BAR)'} },
'if': 'defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)' } 'if': 'defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)' }
{ 'command': 'TestIfAlternateCmd', 'data': { 'alt_cmd_arg': 'TestIfAlternate' }, { 'command': 'test-if-alternate-cmd',
'data': { 'alt_cmd_arg': 'TestIfAlternate' },
'if': 'defined(TEST_IF_ALT)' } 'if': 'defined(TEST_IF_ALT)' }
{ 'command': 'TestIfCmd', 'data': { 'command': 'test-if-cmd',
{ 'foo': 'TestIfStruct', 'data': {
'foo': 'TestIfStruct',
'bar': { 'type': 'TestIfEnum', 'if': 'defined(TEST_IF_CMD_BAR)' } }, 'bar': { 'type': 'TestIfEnum', 'if': 'defined(TEST_IF_CMD_BAR)' } },
'returns': 'UserDefThree', 'returns': 'UserDefThree',
'if': ['defined(TEST_IF_CMD)', 'defined(TEST_IF_STRUCT)'] } 'if': ['defined(TEST_IF_CMD)', 'defined(TEST_IF_STRUCT)'] }
{ 'command': 'TestCmdReturnDefThree', 'returns': 'UserDefThree' } { 'command': 'test-cmd-return-def-three', 'returns': 'UserDefThree' }
{ 'event': 'TEST_IF_EVENT', 'data': { 'event': 'TEST_IF_EVENT', 'data':
{ 'foo': 'TestIfStruct', { 'foo': 'TestIfStruct',

View File

@ -32,7 +32,7 @@ object Union
case value2: q_empty case value2: q_empty
case value3: q_empty case value3: q_empty
case value4: q_empty case value4: q_empty
command user_def_cmd0 Empty2 -> Empty2 command user-def-cmd0 Empty2 -> Empty2
gen=True success_response=True boxed=False oob=False preconfig=False gen=True success_response=True boxed=False oob=False preconfig=False
enum QEnumTwo enum QEnumTwo
prefix QENUM_TWO prefix QENUM_TWO
@ -190,16 +190,16 @@ object UserDefListUnion
case any: q_obj_anyList-wrapper case any: q_obj_anyList-wrapper
case user: q_obj_StatusList-wrapper case user: q_obj_StatusList-wrapper
include include/sub-module.json include include/sub-module.json
command user_def_cmd None -> None command user-def-cmd None -> None
gen=True success_response=True boxed=False oob=False preconfig=False gen=True success_response=True boxed=False oob=False preconfig=False
object q_obj_user_def_cmd1-arg object q_obj_user-def-cmd1-arg
member ud1a: UserDefOne optional=False member ud1a: UserDefOne optional=False
command user_def_cmd1 q_obj_user_def_cmd1-arg -> None command user-def-cmd1 q_obj_user-def-cmd1-arg -> None
gen=True success_response=True boxed=False oob=False preconfig=False gen=True success_response=True boxed=False oob=False preconfig=False
object q_obj_user_def_cmd2-arg object q_obj_user-def-cmd2-arg
member ud1a: UserDefOne optional=False member ud1a: UserDefOne optional=False
member ud1b: UserDefOne optional=True member ud1b: UserDefOne optional=True
command user_def_cmd2 q_obj_user_def_cmd2-arg -> UserDefTwo command user-def-cmd2 q_obj_user-def-cmd2-arg -> UserDefTwo
gen=True success_response=True boxed=False oob=False preconfig=False gen=True success_response=True boxed=False oob=False preconfig=False
command cmd-success-response None -> None command cmd-success-response None -> None
gen=True success_response=False boxed=False oob=False preconfig=False gen=True success_response=False boxed=False oob=False preconfig=False
@ -319,10 +319,10 @@ object TestIfUnion
case union_bar: q_obj_str-wrapper case union_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_TestIfUnionCmd-arg object q_obj_test-if-union-cmd-arg
member union_cmd_arg: TestIfUnion optional=False member union_cmd_arg: TestIfUnion optional=False
if ['defined(TEST_IF_UNION)'] if ['defined(TEST_IF_UNION)']
command TestIfUnionCmd q_obj_TestIfUnionCmd-arg -> None command test-if-union-cmd q_obj_test-if-union-cmd-arg -> None
gen=True success_response=True boxed=False oob=False preconfig=False gen=True success_response=True boxed=False oob=False preconfig=False
if ['defined(TEST_IF_UNION)'] if ['defined(TEST_IF_UNION)']
alternate TestIfAlternate alternate TestIfAlternate
@ -331,21 +331,21 @@ alternate TestIfAlternate
case bar: TestStruct case bar: TestStruct
if ['defined(TEST_IF_ALT_BAR)'] if ['defined(TEST_IF_ALT_BAR)']
if ['defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)'] if ['defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)']
object q_obj_TestIfAlternateCmd-arg object q_obj_test-if-alternate-cmd-arg
member alt_cmd_arg: TestIfAlternate optional=False member alt_cmd_arg: TestIfAlternate optional=False
if ['defined(TEST_IF_ALT)'] if ['defined(TEST_IF_ALT)']
command TestIfAlternateCmd q_obj_TestIfAlternateCmd-arg -> None command test-if-alternate-cmd q_obj_test-if-alternate-cmd-arg -> None
gen=True success_response=True boxed=False oob=False preconfig=False gen=True success_response=True boxed=False oob=False preconfig=False
if ['defined(TEST_IF_ALT)'] if ['defined(TEST_IF_ALT)']
object q_obj_TestIfCmd-arg object q_obj_test-if-cmd-arg
member foo: TestIfStruct optional=False member foo: TestIfStruct optional=False
member bar: TestIfEnum optional=False member bar: TestIfEnum optional=False
if ['defined(TEST_IF_CMD_BAR)'] if ['defined(TEST_IF_CMD_BAR)']
if ['defined(TEST_IF_CMD)', 'defined(TEST_IF_STRUCT)'] if ['defined(TEST_IF_CMD)', 'defined(TEST_IF_STRUCT)']
command TestIfCmd q_obj_TestIfCmd-arg -> UserDefThree command test-if-cmd q_obj_test-if-cmd-arg -> UserDefThree
gen=True success_response=True boxed=False oob=False preconfig=False gen=True success_response=True boxed=False oob=False preconfig=False
if ['defined(TEST_IF_CMD)', 'defined(TEST_IF_STRUCT)'] if ['defined(TEST_IF_CMD)', 'defined(TEST_IF_STRUCT)']
command TestCmdReturnDefThree None -> UserDefThree command test-cmd-return-def-three None -> UserDefThree
gen=True success_response=True boxed=False oob=False preconfig=False gen=True success_response=True boxed=False oob=False preconfig=False
array TestIfEnumList TestIfEnum array TestIfEnumList TestIfEnum
if ['defined(TEST_IF_ENUM)'] if ['defined(TEST_IF_ENUM)']

View File

@ -13,7 +13,7 @@
static QmpCommandList qmp_commands; static QmpCommandList qmp_commands;
UserDefThree *qmp_TestCmdReturnDefThree(Error **errp) UserDefThree *qmp_test_cmd_return_def_three(Error **errp)
{ {
return NULL; return NULL;
} }
@ -199,7 +199,7 @@ static void test_dispatch_cmd(void)
ret = qobject_to(QDict, ret = qobject_to(QDict,
do_qmp_dispatch(false, do_qmp_dispatch(false,
"{ 'execute': 'user_def_cmd' }")); "{ 'execute': 'user-def-cmd' }"));
assert(ret && qdict_size(ret) == 0); assert(ret && qdict_size(ret) == 0);
qobject_unref(ret); qobject_unref(ret);
} }
@ -220,11 +220,11 @@ static void test_dispatch_cmd_failure(void)
{ {
/* missing arguments */ /* missing arguments */
do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR, do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR,
"{ 'execute': 'user_def_cmd2' }"); "{ 'execute': 'user-def-cmd2' }");
/* extra arguments */ /* extra arguments */
do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR, do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR,
"{ 'execute': 'user_def_cmd'," "{ 'execute': 'user-def-cmd',"
" 'arguments': { 'a': 66 } }"); " 'arguments': { 'a': 66 } }");
} }
@ -248,7 +248,7 @@ static void test_dispatch_cmd_io(void)
int64_t val; int64_t val;
ret = qobject_to(QDict, do_qmp_dispatch(false, ret = qobject_to(QDict, do_qmp_dispatch(false,
"{ 'execute': 'user_def_cmd2', 'arguments': {" "{ 'execute': 'user-def-cmd2', 'arguments': {"
" 'ud1a': { 'integer': 42, 'string': 'hello' }," " 'ud1a': { 'integer': 42, 'string': 'hello' },"
" 'ud1b': { 'integer': 422, 'string': 'hello2' } } }")); " 'ud1b': { 'integer': 422, 'string': 'hello2' } } }"));