From patchwork Thu Sep 10 04:06:08 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 516092 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 2886F140157 for ; Thu, 10 Sep 2015 14:09:27 +1000 (AEST) Received: from localhost ([::1]:46754 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZZtAr-0007zq-2l for incoming@patchwork.ozlabs.org; Thu, 10 Sep 2015 00:09:25 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33917) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZZt8I-0002r8-Hb for qemu-devel@nongnu.org; Thu, 10 Sep 2015 00:06:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZZt8G-0004z4-Ne for qemu-devel@nongnu.org; Thu, 10 Sep 2015 00:06:46 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43498) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZZt8G-0004yk-Db for qemu-devel@nongnu.org; Thu, 10 Sep 2015 00:06:44 -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 02F608EA3E; Thu, 10 Sep 2015 04:06:44 +0000 (UTC) Received: from red.redhat.com ([10.3.113.15]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t8A46ZhT031165; Thu, 10 Sep 2015 00:06:43 -0400 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 9 Sep 2015 22:06:08 -0600 Message-Id: <1441857991-7309-7-git-send-email-eblake@redhat.com> In-Reply-To: <1441857991-7309-1-git-send-email-eblake@redhat.com> References: <1441857991-7309-1-git-send-email-eblake@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: Michael Roth , marcandre.lureau@redhat.com, armbru@redhat.com, DirtY.iCE.hu@gmail.com Subject: [Qemu-devel] [PATCH RFC v4 06/29] qapi-visit: Remove redundant functions for flat union base 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 The code for visiting the base class of a child struct created visit_type_Base_fields(); the code for visiting the base class of a flat union created visit_type_Union_fields(). If the same type is shared between a struct and a union, the two functions differed only by whether they visited the discriminator used by the union. But if the base class always visits all its fields, then we don't need to revisit the discriminator specially for a union. By consistently visiting the base class fields under the name of the base class, we can eliminate some redundant code. Now that gen_visit_struct_fields() can potentially collect more than one function into 'ret', a regular expression searching for whether a label was used may hit a false positive within the body of the first function. But using a regex was overkill, since we can easily determine when we jumped to a label. Signed-off-by: Eric Blake --- scripts/qapi-visit.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py index f5dc257..c3e4c49 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -118,7 +118,7 @@ if (err) { ''') pop_indent() - if re.search('^ *goto out;', ret, re.MULTILINE): + if base or members: ret += mcgen(''' out: @@ -249,8 +249,8 @@ def gen_visit_union(name, base, variants): ret = '' if base: - members = [m for m in base.members if m != variants.tag_member] - ret += gen_visit_struct_fields(name, None, members) + ret += gen_visit_struct_fields(base.name, base.base, + base.local_members) for var in variants.variants: # Ugly special case for simple union TODO get rid of it @@ -271,18 +271,19 @@ void visit_type_%(c_name)s(Visitor *m, %(c_name)s **obj, const char *name, Error ''', c_name=c_name(name), name=name) - if base: - ret += mcgen(''' - visit_type_%(c_name)s_fields(m, obj, &err); - if (err) { - goto out_obj; - } -''', - c_name=c_name(name)) - tag_key = variants.tag_member.name - ret += mcgen(''' + if base: + ret += mcgen(''' + visit_type_%(c_name)s_fields(m, (%(c_name)s **)obj, &err); +''', + c_name=c_name(base.name)) + else: + ret += mcgen(''' visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "%(name)s", &err); +''', + c_type=variants.tag_member.type.c_name(), + c_name=c_name(tag_key), name=tag_key) + ret += mcgen(''' if (err) { goto out_obj; } @@ -291,8 +292,7 @@ void visit_type_%(c_name)s(Visitor *m, %(c_name)s **obj, const char *name, Error } switch ((*obj)->%(c_name)s) { ''', - c_type=variants.tag_member.type.c_name(), - c_name=c_name(tag_key), name=tag_key) + c_name=c_name(tag_key)) for var in variants.variants: # TODO ugly special case for simple union