From patchwork Mon Sep 7 10:16:24 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Armbruster X-Patchwork-Id: 515057 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 9A56D140321 for ; Mon, 7 Sep 2015 20:20:26 +1000 (AEST) Received: from localhost ([::1]:55470 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZYtXE-0003r6-MC for incoming@patchwork.ozlabs.org; Mon, 07 Sep 2015 06:20:24 -0400 Received: from eggs.gnu.org ([208.118.235.92]:34218) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZYtTp-0001VK-U7 for qemu-devel@nongnu.org; Mon, 07 Sep 2015 06:16:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZYtTo-0000Pt-9Y for qemu-devel@nongnu.org; Mon, 07 Sep 2015 06:16:53 -0400 Received: from mx1.redhat.com ([209.132.183.28]:42748) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZYtTo-0000PP-02 for qemu-devel@nongnu.org; Mon, 07 Sep 2015 06:16:52 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id 8CA3F2DD2E1; Mon, 7 Sep 2015 10:16:51 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-17.ams2.redhat.com [10.36.116.17]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t87AGmsU024459 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Mon, 7 Sep 2015 06:16:50 -0400 Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id AAB203043914; Mon, 7 Sep 2015 12:16:44 +0200 (CEST) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Mon, 7 Sep 2015 12:16:24 +0200 Message-Id: <1441621003-2434-14-git-send-email-armbru@redhat.com> In-Reply-To: <1441621003-2434-1-git-send-email-armbru@redhat.com> References: <1441621003-2434-1-git-send-email-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: mdroth@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH RFC v5 13/32] qapi: De-duplicate enum code generation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Duplicated in commit 21cd70d. Yes, we can't import qapi-types, but that's no excuse. Move the helpers from qapi-types.py to qapi.py, and replace the duplicates in qapi-event.py. The generated event enumeration type's lookup table becomes const-correct (see commit 2e4450f), and uses explicit indexes instead of relying on order (see commit 912ae9c). Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake --- docs/qapi-code-gen.txt | 9 ++++--- scripts/qapi-event.py | 67 +++----------------------------------------------- scripts/qapi-types.py | 55 ----------------------------------------- scripts/qapi.py | 55 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 122 deletions(-) diff --git a/docs/qapi-code-gen.txt b/docs/qapi-code-gen.txt index 710defc..7c500b6 100644 --- a/docs/qapi-code-gen.txt +++ b/docs/qapi-code-gen.txt @@ -821,9 +821,9 @@ Example: QDECREF(qmp); } - const char *example_QAPIEvent_lookup[] = { - "MY_EVENT", - NULL, + const char *const example_QAPIEvent_lookup[] = { + [EXAMPLE_QAPI_EVENT_MY_EVENT] = "MY_EVENT", + [EXAMPLE_QAPI_EVENT_MAX] = NULL, }; $ cat qapi-generated/example-qapi-event.h [Uninteresting stuff omitted...] @@ -838,10 +838,11 @@ Example: void qapi_event_send_my_event(Error **errp); - extern const char *example_QAPIEvent_lookup[]; typedef enum example_QAPIEvent { EXAMPLE_QAPI_EVENT_MY_EVENT = 0, EXAMPLE_QAPI_EVENT_MAX = 1, } example_QAPIEvent; + extern const char *const example_QAPIEvent_lookup[]; + #endif diff --git a/scripts/qapi-event.py b/scripts/qapi-event.py index aec2d32..aed45d6 100644 --- a/scripts/qapi-event.py +++ b/scripts/qapi-event.py @@ -153,63 +153,6 @@ def generate_event_implement(api_name, event_name, params): return ret - -# Following are the functions that generate an enum type for all defined -# events, similar to qapi-types.py. Here we already have enum name and -# values which were generated before and recorded in event_enum_*. It also -# works around the issue that "import qapi-types" can't work. - -def generate_event_enum_decl(event_enum_name, event_enum_values): - lookup_decl = mcgen(''' - -extern const char *%(event_enum_name)s_lookup[]; -''', - event_enum_name = event_enum_name) - - enum_decl = mcgen(''' -typedef enum %(event_enum_name)s { -''', - event_enum_name = event_enum_name) - - # append automatically generated _MAX value - enum_max_value = c_enum_const(event_enum_name, "MAX") - enum_values = event_enum_values + [ enum_max_value ] - - i = 0 - for value in enum_values: - enum_decl += mcgen(''' - %(value)s = %(i)d, -''', - value = value, - i = i) - i += 1 - - enum_decl += mcgen(''' -} %(event_enum_name)s; -''', - event_enum_name = event_enum_name) - - return lookup_decl + enum_decl - -def generate_event_enum_lookup(event_enum_name, event_enum_strings): - ret = mcgen(''' - -const char *%(event_enum_name)s_lookup[] = { -''', - event_enum_name = event_enum_name) - - for string in event_enum_strings: - ret += mcgen(''' - "%(string)s", -''', - string = string) - - ret += mcgen(''' - NULL, -}; -''') - return ret - (input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line() c_comment = ''' @@ -266,8 +209,7 @@ fdecl.write(mcgen(''' exprs = QAPISchema(input_file).get_exprs() event_enum_name = c_name(prefix + "QAPIEvent", protect=False) -event_enum_values = [] -event_enum_strings = [] +event_names = [] for expr in exprs: if expr.has_key('event'): @@ -286,12 +228,11 @@ for expr in exprs: fdef.write(ret) # Record it, and generate enum later - event_enum_values.append(event_enum_value) - event_enum_strings.append(event_name) + event_names.append(event_name) -ret = generate_event_enum_decl(event_enum_name, event_enum_values) +ret = generate_enum(event_enum_name, event_names) fdecl.write(ret) -ret = generate_event_enum_lookup(event_enum_name, event_enum_strings) +ret = generate_enum_lookup(event_enum_name, event_names) fdef.write(ret) close_output(fdef, fdecl) diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py index 9c422d1..bc20136 100644 --- a/scripts/qapi-types.py +++ b/scripts/qapi-types.py @@ -80,61 +80,6 @@ struct %(name)s { return ret -def generate_enum_lookup(name, values): - ret = mcgen(''' - -const char *const %(name)s_lookup[] = { -''', - name=c_name(name)) - for value in values: - index = c_enum_const(name, value) - ret += mcgen(''' - [%(index)s] = "%(value)s", -''', - index = index, value = value) - - max_index = c_enum_const(name, 'MAX') - ret += mcgen(''' - [%(max_index)s] = NULL, -}; -''', - max_index=max_index) - return ret - -def generate_enum(name, values): - name = c_name(name) - lookup_decl = mcgen(''' - -extern const char *const %(name)s_lookup[]; -''', - name=name) - - enum_decl = mcgen(''' - -typedef enum %(name)s { -''', - name=name) - - # append automatically generated _MAX value - enum_values = values + [ 'MAX' ] - - i = 0 - for value in enum_values: - enum_full_value = c_enum_const(name, value) - enum_decl += mcgen(''' - %(enum_full_value)s = %(i)d, -''', - enum_full_value = enum_full_value, - i=i) - i += 1 - - enum_decl += mcgen(''' -} %(name)s; -''', - name=name) - - return enum_decl + lookup_decl - def gen_alternate_qtypes_decl(name): return mcgen(''' diff --git a/scripts/qapi.py b/scripts/qapi.py index e695a26..e1268f4 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -1485,6 +1485,61 @@ def guardend(name): ''', name=guardname(name)) +def generate_enum_lookup(name, values): + ret = mcgen(''' + +const char *const %(name)s_lookup[] = { +''', + name=c_name(name)) + for value in values: + index = c_enum_const(name, value) + ret += mcgen(''' + [%(index)s] = "%(value)s", +''', + index = index, value = value) + + max_index = c_enum_const(name, 'MAX') + ret += mcgen(''' + [%(max_index)s] = NULL, +}; +''', + max_index=max_index) + return ret + +def generate_enum(name, values): + name = c_name(name) + lookup_decl = mcgen(''' + +extern const char *const %(name)s_lookup[]; +''', + name=name) + + enum_decl = mcgen(''' + +typedef enum %(name)s { +''', + name=name) + + # append automatically generated _MAX value + enum_values = values + [ 'MAX' ] + + i = 0 + for value in enum_values: + enum_full_value = c_enum_const(name, value) + enum_decl += mcgen(''' + %(enum_full_value)s = %(i)d, +''', + enum_full_value = enum_full_value, + i=i) + i += 1 + + enum_decl += mcgen(''' +} %(name)s; +''', + name=name) + + return enum_decl + lookup_decl + # # Common command line parsing #