qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

qapi: Detect collisions in C member names

Detect attempts to declare two object members that would result
in the same C member name, by keying the 'seen' dictionary off
of the C name rather than the qapi name. It also requires passing
info through the check_clash() methods.

This addresses a TODO and fixes the previously-broken
args-name-clash test. The resulting error message demonstrates
the utility of the .describe() method added previously. No change
to generated code.

Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1447836791-369-17-git-send-email-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>

authored by

Eric Blake and committed by
Markus Armbruster
27b60ab9 88d4ef8b

+23 -22
+19 -12
scripts/qapi.py
··· 977 977 self.base = schema.lookup_type(self._base_name) 978 978 assert isinstance(self.base, QAPISchemaObjectType) 979 979 self.base.check(schema) 980 - self.base.check_clash(schema, seen) 980 + self.base.check_clash(schema, self.info, seen) 981 981 for m in self.local_members: 982 982 m.check(schema) 983 - m.check_clash(seen) 983 + m.check_clash(self.info, seen) 984 984 self.members = seen.values() 985 985 if self.variants: 986 986 self.variants.check(schema, seen) 987 987 assert self.variants.tag_member in self.members 988 - self.variants.check_clash(schema, seen) 988 + self.variants.check_clash(schema, self.info, seen) 989 989 990 - def check_clash(self, schema, seen): 990 + # Check that the members of this type do not cause duplicate JSON fields, 991 + # and update seen to track the members seen so far. Report any errors 992 + # on behalf of info, which is not necessarily self.info 993 + def check_clash(self, schema, info, seen): 991 994 assert not self.variants # not implemented 992 995 for m in self.members: 993 - m.check_clash(seen) 996 + m.check_clash(info, seen) 994 997 995 998 def is_implicit(self): 996 999 # See QAPISchema._make_implicit_object_type() ··· 1036 1039 self.type = schema.lookup_type(self._type_name) 1037 1040 assert self.type 1038 1041 1039 - def check_clash(self, seen): 1040 - # TODO change key of seen from QAPI name to C name 1041 - assert self.name not in seen 1042 - seen[self.name] = self 1042 + def check_clash(self, info, seen): 1043 + cname = c_name(self.name) 1044 + if cname in seen: 1045 + raise QAPIExprError(info, 1046 + "%s collides with %s" 1047 + % (self.describe(), seen[cname].describe())) 1048 + seen[cname] = self 1043 1049 1044 1050 def _pretty_owner(self): 1045 1051 owner = self.owner ··· 1080 1086 1081 1087 def check(self, schema, seen): 1082 1088 if not self.tag_member: # flat union 1083 - self.tag_member = seen[self.tag_name] 1089 + self.tag_member = seen[c_name(self.tag_name)] 1090 + assert self.tag_name == self.tag_member.name 1084 1091 assert isinstance(self.tag_member.type, QAPISchemaEnumType) 1085 1092 for v in self.variants: 1086 1093 v.check(schema) ··· 1088 1095 if isinstance(v.type, QAPISchemaObjectType): 1089 1096 v.type.check(schema) 1090 1097 1091 - def check_clash(self, schema, seen): 1098 + def check_clash(self, schema, info, seen): 1092 1099 for v in self.variants: 1093 1100 # Reset seen map for each variant, since qapi names from one 1094 1101 # branch do not affect another branch 1095 1102 assert isinstance(v.type, QAPISchemaObjectType) 1096 - v.type.check_clash(schema, dict(seen)) 1103 + v.type.check_clash(schema, info, dict(seen)) 1097 1104 1098 1105 1099 1106 class QAPISchemaObjectTypeVariant(QAPISchemaObjectTypeMember):
+1
tests/qapi-schema/args-name-clash.err
··· 1 + tests/qapi-schema/args-name-clash.json:4: 'a_b' (parameter of oops) collides with 'a-b' (parameter of oops)
+1 -1
tests/qapi-schema/args-name-clash.exit
··· 1 - 0 1 + 1
+2 -3
tests/qapi-schema/args-name-clash.json
··· 1 1 # C member name collision 2 - # FIXME - This parses, but fails to compile, because the C struct is given 3 - # two 'a_b' members. Either reject this at parse time, or munge the C names 4 - # to avoid the collision. 2 + # Reject members that clash when mapped to C names (we would have two 'a_b' 3 + # members). 5 4 { 'command': 'oops', 'data': { 'a-b': 'str', 'a_b': 'str' } }
-6
tests/qapi-schema/args-name-clash.out
··· 1 - object :empty 2 - object :obj-oops-arg 3 - member a-b: str optional=False 4 - member a_b: str optional=False 5 - command oops :obj-oops-arg -> None 6 - gen=True success_response=True