mirror of
https://github.com/yuzu-emu/unicorn.git
synced 2025-01-22 10:41:08 +00:00
qapi: Share gen_visit_fields()
Consolidate the code between visit, command marshalling, and event generation that iterates over the members of a struct. It reduces code duplication in the generator, so that a future patch can reduce the size of generated code while touching only one instead of three locations. There are no changes to the generated marshal code. The visitor code becomes slightly more verbose, but remains semantically equivalent, and is actually easier to read as it follows a more common idiom: | visit_optional(v, &(*obj)->has_device, "device", &err); |- if (!err && (*obj)->has_device) { |- visit_type_str(v, &(*obj)->device, "device", &err); |- } | if (err) { | goto out; | } |+ if ((*obj)->has_device) { |+ visit_type_str(v, &(*obj)->device, "device", &err); |+ if (err) { |+ goto out; |+ } |+ } The event code becomes slightly more verbose, but this is arguably a bug fix: although the visitors are not well documented, use of an optional member should not be attempted unless guarded by a prior call to visit_optional(). Works only because the output qmp visitor has a no-op visit_optional(): |+ visit_optional(v, &has_offset, "offset", &err); |+ if (err) { |+ goto out; |+ } | if (has_offset) { | visit_type_int(v, &offset, "offset", &err); Backports commit 82ca8e469666b169ccf818a0e36136aee97d7db0 from qemu
This commit is contained in:
parent
13b3a6c92f
commit
4ca10929a7
|
@ -70,36 +70,7 @@ def gen_event_send(name, arg_type):
|
|||
''',
|
||||
name=name)
|
||||
ret += gen_err_check()
|
||||
|
||||
for memb in arg_type.members:
|
||||
if memb.optional:
|
||||
ret += mcgen('''
|
||||
if (has_%(c_name)s) {
|
||||
''',
|
||||
c_name=c_name(memb.name))
|
||||
push_indent()
|
||||
|
||||
# Ugly: need to cast away the const
|
||||
if memb.type.name == "str":
|
||||
cast = '(char **)'
|
||||
else:
|
||||
cast = ''
|
||||
|
||||
ret += mcgen('''
|
||||
visit_type_%(c_type)s(v, %(cast)s&%(c_name)s, "%(name)s", &err);
|
||||
''',
|
||||
cast=cast,
|
||||
c_name=c_name(memb.name),
|
||||
c_type=memb.type.c_name(),
|
||||
name=memb.name)
|
||||
ret += gen_err_check()
|
||||
|
||||
if memb.optional:
|
||||
pop_indent()
|
||||
ret += mcgen('''
|
||||
}
|
||||
''')
|
||||
|
||||
ret += gen_visit_fields(arg_type.members, need_cast=True)
|
||||
ret += mcgen('''
|
||||
visit_end_struct(v, &err);
|
||||
if (err) {
|
||||
|
|
|
@ -83,27 +83,7 @@ static void visit_type_%(c_name)s_fields(Visitor *v, %(c_name)s **obj, Error **e
|
|||
c_type=base.c_name(), c_name=c_name('base'))
|
||||
ret += gen_err_check()
|
||||
|
||||
for memb in members:
|
||||
if memb.optional:
|
||||
ret += mcgen('''
|
||||
visit_optional(v, &(*obj)->has_%(c_name)s, "%(name)s", &err);
|
||||
if (!err && (*obj)->has_%(c_name)s) {
|
||||
''',
|
||||
c_name=c_name(memb.name), name=memb.name)
|
||||
push_indent()
|
||||
|
||||
ret += mcgen('''
|
||||
visit_type_%(c_type)s(v, &(*obj)->%(c_name)s, "%(name)s", &err);
|
||||
''',
|
||||
c_type=memb.type.c_name(), c_name=c_name(memb.name),
|
||||
name=memb.name)
|
||||
|
||||
if memb.optional:
|
||||
pop_indent()
|
||||
ret += mcgen('''
|
||||
}
|
||||
''')
|
||||
ret += gen_err_check()
|
||||
ret += gen_visit_fields(members, prefix='(*obj)->')
|
||||
|
||||
if re.search('^ *goto out;', ret, re.MULTILINE):
|
||||
ret += mcgen('''
|
||||
|
|
|
@ -1543,6 +1543,48 @@ def gen_err_check(err='err', label='out'):
|
|||
err=err, label=label)
|
||||
|
||||
|
||||
def gen_visit_fields(members, prefix='', need_cast=False, errarg='err'):
|
||||
ret = ''
|
||||
if errarg:
|
||||
errparg = '&' + errarg
|
||||
else:
|
||||
errparg = 'NULL'
|
||||
|
||||
for memb in members:
|
||||
if memb.optional:
|
||||
ret += mcgen('''
|
||||
visit_optional(v, &%(prefix)shas_%(c_name)s, "%(name)s", %(errp)s);
|
||||
''',
|
||||
prefix=prefix, c_name=c_name(memb.name),
|
||||
name=memb.name, errp=errparg)
|
||||
ret += gen_err_check(err=errarg)
|
||||
ret += mcgen('''
|
||||
if (%(prefix)shas_%(c_name)s) {
|
||||
''',
|
||||
prefix=prefix, c_name=c_name(memb.name))
|
||||
push_indent()
|
||||
|
||||
# Ugly: sometimes we need to cast away const
|
||||
if need_cast and memb.type.name == 'str':
|
||||
cast = '(char **)'
|
||||
else:
|
||||
cast = ''
|
||||
|
||||
ret += mcgen('''
|
||||
visit_type_%(c_type)s(v, %(cast)s&%(prefix)s%(c_name)s, "%(name)s", %(errp)s);
|
||||
''',
|
||||
c_type=memb.type.c_name(), prefix=prefix, cast=cast,
|
||||
c_name=c_name(memb.name), name=memb.name,
|
||||
errp=errparg)
|
||||
ret += gen_err_check(err=errarg)
|
||||
|
||||
if memb.optional:
|
||||
pop_indent()
|
||||
ret += mcgen('''
|
||||
}
|
||||
''')
|
||||
return ret
|
||||
|
||||
#
|
||||
# Common command line parsing
|
||||
#
|
||||
|
|
Loading…
Reference in a new issue