qapi: Enforce struct member naming rules

Struct members, including command arguments, event data, and union
inline base members, should use '-', not '_'.  Enforce this.  Fix the
fixable offenders (all in tests/), and add the remainder to pragma
member-name-exceptions.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20210323094025.3569441-27-armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Markus Armbruster 2021-03-23 10:40:23 +01:00
parent e75d4225b7
commit 5aceeac04d
8 changed files with 36 additions and 8 deletions

View File

@ -168,7 +168,8 @@ Pragma 'command-returns-exceptions' takes a list of commands that may
violate the rules on permitted return types. Default is none.
Pragma 'member-name-exceptions' takes a list of types whose member
names may contain uppercase letters. Default is none.
names may contain uppercase letters, and '_' instead of '-'. Default
is none.
=== Enumeration types ===

View File

@ -31,10 +31,27 @@
# Externally visible types whose member names may use uppercase
'member-name-exceptions': [ # visible in:
'ACPISlotType', # query-acpi-ospm-status
'AcpiTableOptions', # -acpitable
'BlkdebugSetStateOptions', # blockdev-add, -blockdev
'BlockDeviceInfo', # query-block
'BlockDeviceStats', # query-blockstats
'BlockDeviceTimedStats', # query-blockstats
'BlockIOThrottle', # block_set_io_throttle
'BlockInfo', # query-block
'BlockdevVmdkAdapterType', # blockdev-create (to match VMDK spec)
'BlockdevVmdkSubformat', # blockdev-create (to match VMDK spec)
'ColoCompareProperties', # object_add, -object
'FilterMirrorProperties', # object_add, -object
'FilterRedirectorProperties', # object_add, -object
'FilterRewriterProperties', # object_add, -object
'InputLinuxProperties', # object_add, -object
'NetdevTapOptions', # netdev_add, query-netdev, -netdev
'PciBusInfo', # query-pci
'PciDeviceInfo', # query-pci
'PciMemoryRegion', # query-pci
'QapiErrorClass', # QMP error replies
'UuidInfo', # query-uuid
'VncClientInfo', # query-vnc, query-vnc-servers, ...
'X86CPURegister32' # qom-get of x86 CPU properties
# feature-words, filtered-features
] } }

View File

@ -19,6 +19,10 @@
# Whitelists to permit QAPI rule violations; think twice before you
# add to them!
{ 'pragma': {
# Types whose member names may use '_'
'member-name-exceptions': [
'GuestAgentInfo'
],
# Commands allowed to return a non-dictionary:
'command-returns-exceptions': [
'guest-file-open',

View File

@ -184,7 +184,7 @@ def check_type(value, info, source,
raise QAPISemError(info,
"%s should be an object or type name" % source)
permit_upper = allow_dict in info.pragma.member_name_exceptions
permissive = allow_dict in info.pragma.member_name_exceptions
# value is a dictionary, check that each member is okay
for (key, arg) in value.items():
@ -192,7 +192,8 @@ def check_type(value, info, source,
if key.startswith('*'):
key = key[1:]
check_name_lower(key, info, key_source,
permit_upper, permit_underscore=True)
permit_upper=permissive,
permit_underscore=permissive)
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'])

View File

@ -6,6 +6,10 @@
# Whitelists to permit QAPI rule violations
{ 'pragma': {
# Types whose member names may use '_'
'member-name-exceptions': [
'UserDefA'
],
# Commands allowed to return a non-dictionary:
'command-returns-exceptions': [
'guest-get-time',
@ -231,7 +235,7 @@
'if': 'defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)' }
{ 'command': 'test-if-union-cmd',
'data': { 'union_cmd_arg': 'TestIfUnion' },
'data': { 'union-cmd-arg': 'TestIfUnion' },
'if': 'defined(TEST_IF_UNION)' }
{ 'alternate': 'TestIfAlternate', 'data':
@ -240,7 +244,7 @@
'if': 'defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)' }
{ 'command': 'test-if-alternate-cmd',
'data': { 'alt_cmd_arg': 'TestIfAlternate' },
'data': { 'alt-cmd-arg': 'TestIfAlternate' },
'if': 'defined(TEST_IF_ALT)' }
{ 'command': 'test-if-cmd',

View File

@ -320,7 +320,7 @@ object TestIfUnion
if ['defined(TEST_IF_UNION_BAR)']
if ['defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)']
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)']
command test-if-union-cmd q_obj_test-if-union-cmd-arg -> None
gen=True success_response=True boxed=False oob=False preconfig=False
@ -332,7 +332,7 @@ alternate TestIfAlternate
if ['defined(TEST_IF_ALT_BAR)']
if ['defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)']
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)']
command test-if-alternate-cmd q_obj_test-if-alternate-cmd-arg -> None
gen=True success_response=True boxed=False oob=False preconfig=False

View File

@ -1,2 +1,2 @@
struct-member-name-clash.json: In struct 'Oops':
struct-member-name-clash.json:4: member 'a_b' collides with member 'a-b'
struct-member-name-clash.json:5: member 'a_b' collides with member 'a-b'

View File

@ -1,4 +1,5 @@
# C member name collision
# Reject members that clash when mapped to C names (we would have two 'a_b'
# members).
{ 'pragma': { 'member-name-exceptions': [ 'Oops' ] } }
{ 'struct': 'Oops', 'data': { 'a-b': 'str', 'a_b': 'str' } }