qapi: Generated code cleanup

Clean up white-space, brace placement, and superfluous #ifdef
QAPI_TYPES_BUILTIN_CLEANUP_DEF.

Backports commit 3a864e7c52af15017d5082a9ee39a7919f46d2b5 from qemu
This commit is contained in:
Markus Armbruster 2018-02-19 16:29:11 -05:00 committed by Lioncash
parent c5e8b70b5a
commit 46cf5ed6ae
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7
3 changed files with 27 additions and 36 deletions

View file

@ -167,8 +167,7 @@ extern const char *%(event_enum_name)s_lookup[];
event_enum_name = event_enum_name)
enum_decl = mcgen('''
typedef enum %(event_enum_name)s
{
typedef enum %(event_enum_name)s {
''',
event_enum_name = event_enum_name)

View file

@ -15,8 +15,7 @@ from qapi import *
def generate_fwd_builtin(name):
return mcgen('''
typedef struct %(name)sList
{
typedef struct %(name)sList {
union {
%(type)s value;
uint64_t padding;
@ -32,8 +31,7 @@ def generate_fwd_struct(name):
typedef struct %(name)s %(name)s;
typedef struct %(name)sList
{
typedef struct %(name)sList {
union {
%(name)s *value;
uint64_t padding;
@ -45,8 +43,7 @@ typedef struct %(name)sList
def generate_fwd_enum_struct(name):
return mcgen('''
typedef struct %(name)sList
{
typedef struct %(name)sList {
union {
%(name)s value;
uint64_t padding;
@ -79,8 +76,7 @@ def generate_struct(expr):
base = expr.get('base')
ret = mcgen('''
struct %(name)s
{
struct %(name)s {
''',
name=c_name(structname))
@ -105,7 +101,7 @@ struct %(name)s
def generate_enum_lookup(name, values):
ret = mcgen('''
const char * const %(name)s_lookup[] = {
const char *const %(name)s_lookup[] = {
''',
name=c_name(name))
for value in values:
@ -117,20 +113,18 @@ const char * const %(name)s_lookup[] = {
ret += mcgen('''
NULL,
};
''')
return ret
def generate_enum(name, values):
name = c_name(name)
lookup_decl = mcgen('''
extern const char * const %(name)s_lookup[];
extern const char *const %(name)s_lookup[];
''',
name=name)
enum_decl = mcgen('''
typedef enum %(name)s
{
typedef enum %(name)s {
''',
name=name)
@ -152,7 +146,7 @@ typedef enum %(name)s
''',
name=name)
return lookup_decl + enum_decl
return enum_decl + lookup_decl
def generate_alternate_qtypes(expr):
@ -160,6 +154,7 @@ def generate_alternate_qtypes(expr):
members = expr['data']
ret = mcgen('''
const int %(name)s_qtypes[QTYPE_MAX] = {
''',
name=c_name(name))
@ -196,8 +191,7 @@ def generate_union(expr, meta):
discriminator_type_name = '%sKind' % (name)
ret = mcgen('''
struct %(name)s
{
struct %(name)s {
''',
name=name)
if base:
@ -325,7 +319,6 @@ fdef.write(mcgen('''
#include "qapi/dealloc-visitor.h"
#include "%(prefix)sqapi-types.h"
#include "%(prefix)sqapi-visit.h"
''',
prefix=prefix))
@ -341,22 +334,22 @@ for typename in builtin_types.keys():
fdecl.write(guardend("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
for expr in exprs:
ret = "\n"
ret = ""
if expr.has_key('struct'):
ret += generate_fwd_struct(expr['struct'])
elif expr.has_key('enum'):
ret += generate_enum(expr['enum'], expr['data']) + "\n"
ret += generate_enum(expr['enum'], expr['data'])
ret += generate_fwd_enum_struct(expr['enum'])
fdef.write(generate_enum_lookup(expr['enum'], expr['data']))
elif expr.has_key('union'):
ret += generate_fwd_struct(expr['union']) + "\n"
ret += generate_fwd_struct(expr['union'])
enum_define = discriminator_find_enum_define(expr)
if not enum_define:
ret += generate_enum('%sKind' % expr['union'], expr['data'].keys())
fdef.write(generate_enum_lookup('%sKind' % expr['union'],
expr['data'].keys()))
elif expr.has_key('alternate'):
ret += generate_fwd_struct(expr['alternate']) + "\n"
ret += generate_fwd_struct(expr['alternate'])
ret += generate_enum('%sKind' % expr['alternate'], expr['data'].keys())
fdef.write(generate_enum_lookup('%sKind' % expr['alternate'],
expr['data'].keys()))
@ -376,34 +369,32 @@ fdecl.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
# have the functions defined, so we use -b option to provide control
# over these cases
if do_builtins:
fdef.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
for typename in builtin_types.keys():
fdef.write(generate_type_cleanup(typename + "List"))
fdef.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
for expr in exprs:
ret = "\n"
ret = ""
if expr.has_key('struct'):
ret += generate_struct(expr) + "\n"
ret += generate_type_cleanup_decl(expr['struct'] + "List")
fdef.write(generate_type_cleanup(expr['struct'] + "List") + "\n")
fdef.write(generate_type_cleanup(expr['struct'] + "List"))
ret += generate_type_cleanup_decl(expr['struct'])
fdef.write(generate_type_cleanup(expr['struct']) + "\n")
fdef.write(generate_type_cleanup(expr['struct']))
elif expr.has_key('union'):
ret += generate_union(expr, 'union')
ret += generate_union(expr, 'union') + "\n"
ret += generate_type_cleanup_decl(expr['union'] + "List")
fdef.write(generate_type_cleanup(expr['union'] + "List") + "\n")
fdef.write(generate_type_cleanup(expr['union'] + "List"))
ret += generate_type_cleanup_decl(expr['union'])
fdef.write(generate_type_cleanup(expr['union']) + "\n")
fdef.write(generate_type_cleanup(expr['union']))
elif expr.has_key('alternate'):
ret += generate_union(expr, 'alternate')
ret += generate_union(expr, 'alternate') + "\n"
ret += generate_type_cleanup_decl(expr['alternate'] + "List")
fdef.write(generate_type_cleanup(expr['alternate'] + "List") + "\n")
fdef.write(generate_type_cleanup(expr['alternate'] + "List"))
ret += generate_type_cleanup_decl(expr['alternate'])
fdef.write(generate_type_cleanup(expr['alternate']) + "\n")
fdef.write(generate_type_cleanup(expr['alternate']))
elif expr.has_key('enum'):
ret += generate_type_cleanup_decl(expr['enum'] + "List")
fdef.write(generate_type_cleanup(expr['enum'] + "List") + "\n")
ret += "\n" + generate_type_cleanup_decl(expr['enum'] + "List")
fdef.write(generate_type_cleanup(expr['enum'] + "List"))
else:
continue
fdecl.write(ret)

View file

@ -62,6 +62,7 @@ def generate_visit_struct_fields(name, members, base = None):
static void visit_type_%(name)s_fields(Visitor *m, %(name)s **obj, Error **errp)
{
Error *err = NULL;
''',
name=c_name(name))
push_indent()