From patchwork Thu Mar 1 19:42:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 10252425 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 746D36037D for ; Thu, 1 Mar 2018 19:57:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5EF5328535 for ; Thu, 1 Mar 2018 19:57:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5332C2857F; Thu, 1 Mar 2018 19:57:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 8C7C928535 for ; Thu, 1 Mar 2018 19:57:12 +0000 (UTC) Received: from localhost ([::1]:59010 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1erUKF-0003a0-NT for patchwork-qemu-devel@patchwork.kernel.org; Thu, 01 Mar 2018 14:57:11 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40432) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1erU6i-0000kf-Lv for qemu-devel@nongnu.org; Thu, 01 Mar 2018 14:43:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1erU6g-0007NX-Vi for qemu-devel@nongnu.org; Thu, 01 Mar 2018 14:43:12 -0500 Received: from mx1.redhat.com ([209.132.183.28]:58466) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1erU6g-0007Lx-MO for qemu-devel@nongnu.org; Thu, 01 Mar 2018 14:43:10 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CEDAAC057F87; Thu, 1 Mar 2018 19:43:09 +0000 (UTC) Received: from red.redhat.com (ovpn-122-122.rdu2.redhat.com [10.10.122.122]) by smtp.corp.redhat.com (Postfix) with ESMTP id 342D1620A8; Thu, 1 Mar 2018 19:43:07 +0000 (UTC) From: Eric Blake To: qemu-devel@nongnu.org Date: Thu, 1 Mar 2018 13:42:21 -0600 Message-Id: <20180301194245.29854-7-eblake@redhat.com> In-Reply-To: <20180301194245.29854-1-eblake@redhat.com> References: <20180301194245.29854-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Thu, 01 Mar 2018 19:43:09 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 06/30] qapi: Reduce use of global variables in generators some X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Cleber Rosa , Markus Armbruster , Eduardo Habkost , Michael Roth Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Markus Armbruster In preparation of the next commit, which will turn the generators into modules. These global variables will become local to main() then. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Reviewed-by: Marc-André Lureau Message-Id: <20180211093607.27351-7-armbru@redhat.com> Reviewed-by: Michael Roth Signed-off-by: Eric Blake --- scripts/qapi-commands.py | 9 +++++---- scripts/qapi-event.py | 15 +++++++-------- scripts/qapi-introspect.py | 7 ++++--- scripts/qapi-types.py | 17 +++++++++-------- scripts/qapi-visit.py | 17 +++++++++-------- 5 files changed, 34 insertions(+), 31 deletions(-) diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py index 8435a890baf..e75e32e4898 100644 --- a/scripts/qapi-commands.py +++ b/scripts/qapi-commands.py @@ -207,7 +207,7 @@ def gen_register_command(name, success_response): return ret -def gen_registry(registry): +def gen_registry(registry, prefix): ret = mcgen(''' void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds) @@ -224,7 +224,8 @@ void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds) class QAPISchemaGenCommandVisitor(QAPISchemaVisitor): - def __init__(self): + def __init__(self, prefix): + self._prefix = prefix self.decl = None self.defn = None self._regy = None @@ -237,7 +238,7 @@ class QAPISchemaGenCommandVisitor(QAPISchemaVisitor): self._visited_ret_types = set() def visit_end(self): - self.defn += gen_registry(self._regy) + self.defn += gen_registry(self._regy, self._prefix) self._regy = None self._visited_ret_types = None @@ -287,7 +288,7 @@ void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds); prefix=prefix, c_prefix=c_name(prefix, protect=False))) schema = QAPISchema(input_file) -vis = QAPISchemaGenCommandVisitor() +vis = QAPISchemaGenCommandVisitor(prefix) schema.visit(vis) genc.add(vis.defn) genh.add(vis.decl) diff --git a/scripts/qapi-event.py b/scripts/qapi-event.py index e063f6b8d13..f65ccdc8eaa 100644 --- a/scripts/qapi-event.py +++ b/scripts/qapi-event.py @@ -58,7 +58,7 @@ def gen_param_var(typ): return ret -def gen_event_send(name, arg_type, boxed): +def gen_event_send(name, arg_type, boxed, event_enum_name): # FIXME: Our declaration of local variables (and of 'errp' in the # parameter list) can collide with exploded members of the event's # data type passed in as parameters. If this collision ever hits in @@ -149,7 +149,8 @@ out: class QAPISchemaGenEventVisitor(QAPISchemaVisitor): - def __init__(self): + def __init__(self, prefix): + self._enum_name = c_name(prefix + 'QAPIEvent', protect=False) self.decl = None self.defn = None self._event_names = None @@ -160,13 +161,13 @@ class QAPISchemaGenEventVisitor(QAPISchemaVisitor): self._event_names = [] def visit_end(self): - self.decl += gen_enum(event_enum_name, self._event_names) - self.defn += gen_enum_lookup(event_enum_name, self._event_names) + self.decl += gen_enum(self._enum_name, self._event_names) + self.defn += gen_enum_lookup(self._enum_name, self._event_names) self._event_names = None def visit_event(self, name, info, arg_type, boxed): self.decl += gen_event_send_decl(name, arg_type, boxed) - self.defn += gen_event_send(name, arg_type, boxed) + self.defn += gen_event_send(name, arg_type, boxed, self._enum_name) self._event_names.append(name) @@ -197,10 +198,8 @@ genh.add(mcgen(''' ''', prefix=prefix)) -event_enum_name = c_name(prefix + 'QAPIEvent', protect=False) - schema = QAPISchema(input_file) -vis = QAPISchemaGenEventVisitor() +vis = QAPISchemaGenEventVisitor(prefix) schema.visit(vis) genc.add(vis.defn) genh.add(vis.decl) diff --git a/scripts/qapi-introspect.py b/scripts/qapi-introspect.py index 0638acda9ca..5d9a7abeb8b 100644 --- a/scripts/qapi-introspect.py +++ b/scripts/qapi-introspect.py @@ -41,7 +41,8 @@ def to_c_string(string): class QAPISchemaGenIntrospectVisitor(QAPISchemaVisitor): - def __init__(self, unmask): + def __init__(self, prefix, unmask): + self._prefix = prefix self._unmask = unmask self.defn = None self.decl = None @@ -65,7 +66,7 @@ class QAPISchemaGenIntrospectVisitor(QAPISchemaVisitor): # generate C # TODO can generate awfully long lines jsons.extend(self._jsons) - name = c_name(prefix, protect=False) + 'qmp_schema_json' + name = c_name(self._prefix, protect=False) + 'qmp_schema_json' self.decl = mcgen(''' extern const char %(c_name)s[]; ''', @@ -190,7 +191,7 @@ genc.add(mcgen(''' prefix=prefix)) schema = QAPISchema(input_file) -vis = QAPISchemaGenIntrospectVisitor(opt_unmask) +vis = QAPISchemaGenIntrospectVisitor(prefix, opt_unmask) schema.visit(vis) genc.add(vis.defn) genh.add(vis.decl) diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py index 1eb59b1dbb9..6ef8c40d5b9 100644 --- a/scripts/qapi-types.py +++ b/scripts/qapi-types.py @@ -168,7 +168,8 @@ void qapi_free_%(c_name)s(%(c_name)s *obj) class QAPISchemaGenTypeVisitor(QAPISchemaVisitor): - def __init__(self): + def __init__(self, opt_builtins): + self._opt_builtins = opt_builtins self.decl = None self.defn = None self._fwdecl = None @@ -187,7 +188,7 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor): self._fwdecl = None # To avoid header dependency hell, we always generate # declarations for built-in types in our header files and - # simply guard them. See also do_builtins (command line + # simply guard them. See also opt_builtins (command line # option -b). self._btin += guardend('QAPI_TYPES_BUILTIN') self.decl = self._btin + self.decl @@ -202,7 +203,7 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor): # TODO use something cleaner than existence of info if not info: self._btin += gen_enum(name, values, prefix) - if do_builtins: + if self._opt_builtins: self.defn += gen_enum_lookup(name, values, prefix) else: self._fwdecl += gen_enum(name, values, prefix) @@ -213,7 +214,7 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor): self._btin += gen_fwd_object_or_array(name) self._btin += gen_array(name, element_type) self._btin += gen_type_cleanup_decl(name) - if do_builtins: + if self._opt_builtins: self.defn += gen_type_cleanup(name) else: self._fwdecl += gen_fwd_object_or_array(name) @@ -241,16 +242,16 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor): # If you link code generated from multiple schemata, you want only one # instance of the code for built-in types. Generate it only when -# do_builtins, enabled by command line option -b. See also +# opt_builtins, enabled by command line option -b. See also # QAPISchemaGenTypeVisitor.visit_end(). -do_builtins = False +opt_builtins = False (input_file, output_dir, do_c, do_h, prefix, opts) = \ parse_command_line('b', ['builtins']) for o, a in opts: if o in ('-b', '--builtins'): - do_builtins = True + opt_builtins = True blurb = ' * Schema-defined QAPI types' @@ -270,7 +271,7 @@ genh.add(mcgen(''' ''')) schema = QAPISchema(input_file) -vis = QAPISchemaGenTypeVisitor() +vis = QAPISchemaGenTypeVisitor(opt_builtins) schema.visit(vis) genc.add(vis.defn) genh.add(vis.decl) diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py index 51eeaa1fc22..d5ca4804215 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -264,7 +264,8 @@ out: class QAPISchemaGenVisitVisitor(QAPISchemaVisitor): - def __init__(self): + def __init__(self, opt_builtins): + self._opt_builtins = opt_builtins self.decl = None self.defn = None self._btin = None @@ -277,7 +278,7 @@ class QAPISchemaGenVisitVisitor(QAPISchemaVisitor): def visit_end(self): # To avoid header dependency hell, we always generate # declarations for built-in types in our header files and - # simply guard them. See also do_builtins (command line + # simply guard them. See also opt_builtins (command line # option -b). self._btin += guardend('QAPI_VISIT_BUILTIN') self.decl = self._btin + self.decl @@ -288,7 +289,7 @@ class QAPISchemaGenVisitVisitor(QAPISchemaVisitor): # TODO use something cleaner than existence of info if not info: self._btin += gen_visit_decl(name, scalar=True) - if do_builtins: + if self._opt_builtins: self.defn += gen_visit_enum(name) else: self.decl += gen_visit_decl(name, scalar=True) @@ -299,7 +300,7 @@ class QAPISchemaGenVisitVisitor(QAPISchemaVisitor): defn = gen_visit_list(name, element_type) if isinstance(element_type, QAPISchemaBuiltinType): self._btin += decl - if do_builtins: + if self._opt_builtins: self.defn += defn else: self.decl += decl @@ -324,16 +325,16 @@ class QAPISchemaGenVisitVisitor(QAPISchemaVisitor): # If you link code generated from multiple schemata, you want only one # instance of the code for built-in types. Generate it only when -# do_builtins, enabled by command line option -b. See also +# opt_builtins, enabled by command line option -b. See also # QAPISchemaGenVisitVisitor.visit_end(). -do_builtins = False +opt_builtins = False (input_file, output_dir, do_c, do_h, prefix, opts) = \ parse_command_line('b', ['builtins']) for o, a in opts: if o in ('-b', '--builtins'): - do_builtins = True + opt_builtins = True blurb = ' * Schema-defined QAPI visitors' @@ -357,7 +358,7 @@ genh.add(mcgen(''' prefix=prefix)) schema = QAPISchema(input_file) -vis = QAPISchemaGenVisitVisitor() +vis = QAPISchemaGenVisitVisitor(opt_builtins) schema.visit(vis) genc.add(vis.defn) genh.add(vis.decl)