From 18df515ebbefa9f13474b128b8050d5fa346ea1e Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Thu, 14 May 2015 06:50:48 -0600 Subject: qapi: Rename identical c_fun()/c_var() into c_name() Now that the two functions are identical, we only need one of them, and we might as well give it a more descriptive name. Basically, the function serves as the translation from a QAPI name into a (portion of a) C identifier, without regards to whether it is a variable or function name. Signed-off-by: Eric Blake Signed-off-by: Markus Armbruster --- scripts/qapi-commands.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'scripts/qapi-commands.py') diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py index 93e43f0e48..8c125cac1f 100644 --- a/scripts/qapi-commands.py +++ b/scripts/qapi-commands.py @@ -31,12 +31,13 @@ def generate_command_decl(name, args, ret_type): for argname, argtype, optional in parse_args(args): argtype = c_type(argtype, is_param=True) if optional: - arglist += "bool has_%s, " % c_var(argname) - arglist += "%s %s, " % (argtype, c_var(argname)) + arglist += "bool has_%s, " % c_name(argname) + arglist += "%s %s, " % (argtype, c_name(argname)) return mcgen(''' %(ret_type)s qmp_%(name)s(%(args)sError **errp); ''', - ret_type=c_type(ret_type), name=c_fun(name), args=arglist).strip() + ret_type=c_type(ret_type), name=c_name(name), + args=arglist).strip() def gen_err_check(errvar): if errvar: @@ -55,14 +56,14 @@ def gen_sync_call(name, args, ret_type, indent=0): retval = "retval = " for argname, argtype, optional in parse_args(args): if optional: - arglist += "has_%s, " % c_var(argname) - arglist += "%s, " % (c_var(argname)) + arglist += "has_%s, " % c_name(argname) + arglist += "%s, " % (c_name(argname)) push_indent(indent) ret = mcgen(''' %(retval)sqmp_%(name)s(%(args)s&local_err); ''', - name=c_fun(name), args=arglist, retval=retval).rstrip() + name=c_name(name), args=arglist, retval=retval).rstrip() if ret_type: ret += "\n" + gen_err_check('local_err') ret += "\n" + mcgen('''' @@ -76,7 +77,7 @@ def gen_sync_call(name, args, ret_type, indent=0): def gen_marshal_output_call(name, ret_type): if not ret_type: return "" - return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_fun(name) + return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_name(name) def gen_visitor_input_containers_decl(args, obj): ret = "" @@ -101,17 +102,17 @@ def gen_visitor_input_vars_decl(args): ret += mcgen(''' bool has_%(argname)s = false; ''', - argname=c_var(argname)) + argname=c_name(argname)) if is_c_ptr(argtype): ret += mcgen(''' %(argtype)s %(argname)s = NULL; ''', - argname=c_var(argname), argtype=c_type(argtype)) + argname=c_name(argname), argtype=c_type(argtype)) else: ret += mcgen(''' %(argtype)s %(argname)s = {0}; ''', - argname=c_var(argname), argtype=c_type(argtype)) + argname=c_name(argname), argtype=c_type(argtype)) pop_indent() return ret.rstrip() @@ -144,17 +145,17 @@ v = qmp_input_get_visitor(mi); ret += mcgen(''' visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s); ''', - c_name=c_var(argname), name=argname, errp=errparg) + c_name=c_name(argname), name=argname, errp=errparg) ret += gen_err_check(errarg) ret += mcgen(''' if (has_%(c_name)s) { ''', - c_name=c_var(argname)) + c_name=c_name(argname)) push_indent() ret += mcgen(''' %(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s); ''', - c_name=c_var(argname), name=argname, argtype=argtype, + c_name=c_name(argname), name=argname, argtype=argtype, visitor=type_visitor(argtype), errp=errparg) ret += gen_err_check(errarg) if optional: @@ -198,16 +199,16 @@ out: qapi_dealloc_visitor_cleanup(md); } ''', - c_ret_type=c_type(ret_type), c_name=c_fun(name), + c_ret_type=c_type(ret_type), c_name=c_name(name), visitor=type_visitor(ret_type)) return ret def gen_marshal_input_decl(name, args, ret_type, middle_mode): if middle_mode: - return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_fun(name) + return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_name(name) else: - return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_fun(name) + return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name) @@ -304,7 +305,7 @@ def gen_registry(commands): registry += mcgen(''' qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s); ''', - name=cmd['command'], c_name=c_fun(cmd['command']), + name=cmd['command'], c_name=c_name(cmd['command']), opts=options) pop_indent() ret = mcgen(''' -- cgit v1.2.3 From e3c4c3d796c1147d32f66fa1413d5d7c49d5aa37 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Thu, 14 May 2015 06:51:01 -0600 Subject: qapi: Support downstream events and commands Enhance the testsuite to cover downstream events and commands. Events worked without more tweaks, but commands needed a few final updates in the generator to mangle names in the appropriate places. In making those tweaks, it was easier to drop type_visitor() and inline its actions instead. Signed-off-by: Eric Blake Signed-off-by: Markus Armbruster --- scripts/qapi-commands.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'scripts/qapi-commands.py') diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py index 8c125cac1f..0a1d636b18 100644 --- a/scripts/qapi-commands.py +++ b/scripts/qapi-commands.py @@ -20,12 +20,6 @@ import os import getopt import errno -def type_visitor(name): - if type(name) == list: - return 'visit_type_%sList' % name[0] - else: - return 'visit_type_%s' % name - def generate_command_decl(name, args, ret_type): arglist="" for argname, argtype, optional in parse_args(args): @@ -153,10 +147,10 @@ if (has_%(c_name)s) { c_name=c_name(argname)) push_indent() ret += mcgen(''' -%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s); +visit_type_%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s); ''', c_name=c_name(argname), name=argname, argtype=argtype, - visitor=type_visitor(argtype), errp=errparg) + visitor=type_name(argtype), errp=errparg) ret += gen_err_check(errarg) if optional: pop_indent() @@ -184,7 +178,7 @@ static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_o Visitor *v; v = qmp_output_get_visitor(mo); - %(visitor)s(v, &ret_in, "unused", &local_err); + visit_type_%(visitor)s(v, &ret_in, "unused", &local_err); if (local_err) { goto out; } @@ -195,12 +189,12 @@ out: qmp_output_visitor_cleanup(mo); md = qapi_dealloc_visitor_new(); v = qapi_dealloc_get_visitor(md); - %(visitor)s(v, &ret_in, "unused", NULL); + visit_type_%(visitor)s(v, &ret_in, "unused", NULL); qapi_dealloc_visitor_cleanup(md); } ''', c_ret_type=c_type(ret_type), c_name=c_name(name), - visitor=type_visitor(ret_type)) + visitor=type_name(ret_type)) return ret -- cgit v1.2.3 From 72aaa73a4acef06bfaed750064c40a597f0cf745 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 2 Apr 2015 11:41:22 +0200 Subject: qapi: qapi-commands.py option --type is unused, drop it Anything but --type sync (which is the default) suppresses output entirely, which makes no sense. Dates back to the initial commit c17d990. Commit message says "Currently only generators for synchronous qapi/qmp functions are supported", so maybe output other than "synchronous qapi/qmp" was planned at the time, to be selected with --type. Should other kinds of output ever materialize, we can put the option back. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake --- scripts/qapi-commands.py | 68 +++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 36 deletions(-) (limited to 'scripts/qapi-commands.py') diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py index 0a1d636b18..c94a19b91d 100644 --- a/scripts/qapi-commands.py +++ b/scripts/qapi-commands.py @@ -381,14 +381,13 @@ try: opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:i:o:m", ["source", "header", "prefix=", "input-file=", "output-dir=", - "type=", "middle"]) + "middle"]) except getopt.GetoptError, err: print str(err) sys.exit(1) output_dir = "" prefix = "" -dispatch_type = "sync" c_file = 'qmp-marshal.c' h_file = 'qmp-commands.h' middle_mode = False @@ -403,8 +402,6 @@ for o, a in opts: input_file = a elif o in ("-o", "--output-dir"): output_dir = a + "/" - elif o in ("-t", "--type"): - dispatch_type = a elif o in ("-m", "--middle"): middle_mode = True elif o in ("-c", "--source"): @@ -436,40 +433,39 @@ exprs = parse_schema(input_file) commands = filter(lambda expr: expr.has_key('command'), exprs) commands = filter(lambda expr: not expr.has_key('gen'), commands) -if dispatch_type == "sync": - fdecl = maybe_open(do_h, h_file, 'w') - fdef = maybe_open(do_c, c_file, 'w') - ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix) +fdecl = maybe_open(do_h, h_file, 'w') +fdef = maybe_open(do_c, c_file, 'w') +ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix) +fdecl.write(ret) +ret = gen_command_def_prologue(prefix=prefix) +fdef.write(ret) + +for cmd in commands: + arglist = [] + ret_type = None + if cmd.has_key('data'): + arglist = cmd['data'] + if cmd.has_key('returns'): + ret_type = cmd['returns'] + ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n" fdecl.write(ret) - ret = gen_command_def_prologue(prefix=prefix) - fdef.write(ret) - - for cmd in commands: - arglist = [] - ret_type = None - if cmd.has_key('data'): - arglist = cmd['data'] - if cmd.has_key('returns'): - ret_type = cmd['returns'] - ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n" - fdecl.write(ret) - if ret_type: - ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n" - fdef.write(ret) - - if middle_mode: - fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode)) - - ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n" + if ret_type: + ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n" fdef.write(ret) - fdecl.write("\n#endif\n"); + if middle_mode: + fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode)) - if not middle_mode: - ret = gen_registry(commands) - fdef.write(ret) + ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n" + fdef.write(ret) + +fdecl.write("\n#endif\n"); + +if not middle_mode: + ret = gen_registry(commands) + fdef.write(ret) - fdef.flush() - fdef.close() - fdecl.flush() - fdecl.close() +fdef.flush() +fdef.close() +fdecl.flush() +fdecl.close() -- cgit v1.2.3 From 2114f5a98d0d80774306279e1694de074ca86aa0 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 2 Apr 2015 13:12:21 +0200 Subject: qapi: Factor parse_command_line() out of the generators Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake --- scripts/qapi-commands.py | 34 +++------------------------------- 1 file changed, 3 insertions(+), 31 deletions(-) (limited to 'scripts/qapi-commands.py') diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py index c94a19b91d..2889877680 100644 --- a/scripts/qapi-commands.py +++ b/scripts/qapi-commands.py @@ -15,9 +15,7 @@ from ordereddict import OrderedDict from qapi import * import re -import sys import os -import getopt import errno def generate_command_decl(name, args, ret_type): @@ -376,42 +374,16 @@ def gen_command_def_prologue(prefix="", proxy=False): ret += '#include "%sqmp-commands.h"' % prefix return ret + "\n\n" - -try: - opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:i:o:m", - ["source", "header", "prefix=", - "input-file=", "output-dir=", - "middle"]) -except getopt.GetoptError, err: - print str(err) - sys.exit(1) - -output_dir = "" -prefix = "" c_file = 'qmp-marshal.c' h_file = 'qmp-commands.h' middle_mode = False -do_c = False -do_h = False +(input_file, output_dir, do_c, do_h, prefix, opts) = \ + parse_command_line("m", ["middle"]) for o, a in opts: - if o in ("-p", "--prefix"): - prefix = a - elif o in ("-i", "--input-file"): - input_file = a - elif o in ("-o", "--output-dir"): - output_dir = a + "/" - elif o in ("-m", "--middle"): + if o in ("-m", "--middle"): middle_mode = True - elif o in ("-c", "--source"): - do_c = True - elif o in ("-h", "--header"): - do_h = True - -if not do_c and not do_h: - do_c = True - do_h = True c_file = output_dir + prefix + c_file h_file = output_dir + prefix + h_file -- cgit v1.2.3 From 12f8e1b9ff57e99dafbb13f89cd5a99ad5c28527 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 2 Apr 2015 14:46:39 +0200 Subject: qapi: Factor open_output(), close_output() out of generators Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake --- scripts/qapi-commands.py | 101 +++++++++++++++++------------------------------ 1 file changed, 37 insertions(+), 64 deletions(-) (limited to 'scripts/qapi-commands.py') diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py index 2889877680..c3e420e6c9 100644 --- a/scripts/qapi-commands.py +++ b/scripts/qapi-commands.py @@ -15,8 +15,6 @@ from ordereddict import OrderedDict from qapi import * import re -import os -import errno def generate_command_decl(name, args, ret_type): arglist="" @@ -311,51 +309,18 @@ qapi_init(qmp_init_marshal); registry=registry.rstrip()) return ret -def gen_command_decl_prologue(header, guard, prefix=""): +def gen_command_decl_prologue(prefix=""): ret = mcgen(''' -/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */ - -/* - * schema-defined QAPI function prototypes - * - * Copyright IBM, Corp. 2011 - * - * Authors: - * Anthony Liguori - * - * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. - * See the COPYING.LIB file in the top-level directory. - * - */ - -#ifndef %(guard)s -#define %(guard)s - #include "%(prefix)sqapi-types.h" #include "qapi/qmp/qdict.h" #include "qapi/error.h" ''', - header=basename(header), guard=guardname(header), prefix=prefix) + prefix=prefix) return ret def gen_command_def_prologue(prefix="", proxy=False): ret = mcgen(''' -/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */ - -/* - * schema-defined QMP->QAPI command dispatch - * - * Copyright IBM, Corp. 2011 - * - * Authors: - * Anthony Liguori - * - * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. - * See the COPYING.LIB file in the top-level directory. - * - */ - #include "qemu-common.h" #include "qemu/module.h" #include "qapi/qmp/qerror.h" @@ -374,8 +339,6 @@ def gen_command_def_prologue(prefix="", proxy=False): ret += '#include "%sqmp-commands.h"' % prefix return ret + "\n\n" -c_file = 'qmp-marshal.c' -h_file = 'qmp-commands.h' middle_mode = False (input_file, output_dir, do_c, do_h, prefix, opts) = \ @@ -385,29 +348,44 @@ for o, a in opts: if o in ("-m", "--middle"): middle_mode = True -c_file = output_dir + prefix + c_file -h_file = output_dir + prefix + h_file - -def maybe_open(really, name, opt): - if really: - return open(name, opt) - else: - import StringIO - return StringIO.StringIO() - -try: - os.makedirs(output_dir) -except os.error, e: - if e.errno != errno.EEXIST: - raise - exprs = parse_schema(input_file) commands = filter(lambda expr: expr.has_key('command'), exprs) commands = filter(lambda expr: not expr.has_key('gen'), commands) -fdecl = maybe_open(do_h, h_file, 'w') -fdef = maybe_open(do_c, c_file, 'w') -ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix) +c_comment = ''' +/* + * schema-defined QMP->QAPI command dispatch + * + * Copyright IBM, Corp. 2011 + * + * Authors: + * Anthony Liguori + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. + * See the COPYING.LIB file in the top-level directory. + * + */ +''' +h_comment = ''' +/* + * schema-defined QAPI function prototypes + * + * Copyright IBM, Corp. 2011 + * + * Authors: + * Anthony Liguori + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. + * See the COPYING.LIB file in the top-level directory. + * + */ +''' + +(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix, + 'qmp-marshal.c', 'qmp-commands.h', + c_comment, h_comment) + +ret = gen_command_decl_prologue(prefix=prefix) fdecl.write(ret) ret = gen_command_def_prologue(prefix=prefix) fdef.write(ret) @@ -431,13 +409,8 @@ for cmd in commands: ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n" fdef.write(ret) -fdecl.write("\n#endif\n"); - if not middle_mode: ret = gen_registry(commands) fdef.write(ret) -fdef.flush() -fdef.close() -fdecl.flush() -fdecl.close() +close_output(fdef, fdecl) -- cgit v1.2.3 From 4180978c9205c50acd2d6c385def9b3e81911696 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 2 Apr 2015 14:52:55 +0200 Subject: qapi: Inline gen_command_decl_prologue(), gen_command_def_prologue() Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake --- scripts/qapi-commands.py | 58 ++++++++++++++++++++---------------------------- 1 file changed, 24 insertions(+), 34 deletions(-) (limited to 'scripts/qapi-commands.py') diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py index c3e420e6c9..1c1d3aa029 100644 --- a/scripts/qapi-commands.py +++ b/scripts/qapi-commands.py @@ -309,36 +309,6 @@ qapi_init(qmp_init_marshal); registry=registry.rstrip()) return ret -def gen_command_decl_prologue(prefix=""): - ret = mcgen(''' -#include "%(prefix)sqapi-types.h" -#include "qapi/qmp/qdict.h" -#include "qapi/error.h" - -''', - prefix=prefix) - return ret - -def gen_command_def_prologue(prefix="", proxy=False): - ret = mcgen(''' -#include "qemu-common.h" -#include "qemu/module.h" -#include "qapi/qmp/qerror.h" -#include "qapi/qmp/types.h" -#include "qapi/qmp/dispatch.h" -#include "qapi/visitor.h" -#include "qapi/qmp-output-visitor.h" -#include "qapi/qmp-input-visitor.h" -#include "qapi/dealloc-visitor.h" -#include "%(prefix)sqapi-types.h" -#include "%(prefix)sqapi-visit.h" - -''', - prefix=prefix) - if not proxy: - ret += '#include "%sqmp-commands.h"' % prefix - return ret + "\n\n" - middle_mode = False (input_file, output_dir, do_c, do_h, prefix, opts) = \ @@ -385,10 +355,30 @@ h_comment = ''' 'qmp-marshal.c', 'qmp-commands.h', c_comment, h_comment) -ret = gen_command_decl_prologue(prefix=prefix) -fdecl.write(ret) -ret = gen_command_def_prologue(prefix=prefix) -fdef.write(ret) +fdef.write(mcgen(''' +#include "qemu-common.h" +#include "qemu/module.h" +#include "qapi/qmp/qerror.h" +#include "qapi/qmp/types.h" +#include "qapi/qmp/dispatch.h" +#include "qapi/visitor.h" +#include "qapi/qmp-output-visitor.h" +#include "qapi/qmp-input-visitor.h" +#include "qapi/dealloc-visitor.h" +#include "%(prefix)sqapi-types.h" +#include "%(prefix)sqapi-visit.h" +#include "%(prefix)sqmp-commands.h" + +''', + prefix=prefix)) + +fdecl.write(mcgen(''' +#include "%(prefix)sqapi-types.h" +#include "qapi/qmp/qdict.h" +#include "qapi/error.h" + +''', + prefix=prefix)) for cmd in commands: arglist = [] -- cgit v1.2.3