From patchwork Thu Aug 20 00:04:44 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 509198 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 4BF8A1402AD for ; Fri, 21 Aug 2015 09:13:09 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=comcast.net header.i=@comcast.net header.b=fUD+GG1d; dkim-atps=neutral Received: from localhost ([::1]:36432 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZSDjd-0005qg-QW for incoming@patchwork.ozlabs.org; Wed, 19 Aug 2015 20:29:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55155) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZSDOi-0003ee-5u for qemu-devel@nongnu.org; Wed, 19 Aug 2015 20:08:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZSDOd-0001Vk-Ce for qemu-devel@nongnu.org; Wed, 19 Aug 2015 20:08:00 -0400 Received: from resqmta-po-11v.sys.comcast.net ([96.114.154.170]:35608) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZSDOd-00015m-7E for qemu-devel@nongnu.org; Wed, 19 Aug 2015 20:07:55 -0400 Received: from resomta-po-12v.sys.comcast.net ([96.114.154.236]) by resqmta-po-11v.sys.comcast.net with comcast id 6o6K1r00556HXL001o7v7h; Thu, 20 Aug 2015 00:07:55 +0000 Received: from red.redhat.com ([69.84.245.29]) by resomta-po-12v.sys.comcast.net with comcast id 6o4l1r00H0emvsd01o5lfq; Thu, 20 Aug 2015 00:05:53 +0000 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 19 Aug 2015 17:04:44 -0700 Message-Id: <1440029085-14539-5-git-send-email-eblake@redhat.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1440029085-14539-1-git-send-email-eblake@redhat.com> References: <1440029085-14539-1-git-send-email-eblake@redhat.com> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=comcast.net; s=q20140121; t=1440029275; bh=2ZEKgcZ1ZoOzASWKpRH/QOfYvLOaC62CjOCJ5hwzvuY=; h=Received:Received:From:To:Subject:Date:Message-Id; b=fUD+GG1dzNarVawoQomky2yfRoMG9E0xS6a2ds3H3x7H/Pdv5xAQNEwX/cjm2Bdfw jvRB7IZuljRu2NZg7kerSCGN3RZM4PnJTs+dKIoptVn0R9I505wtQU1EA06TOAv+hW CuG0qpbMZGwRN0XOi+Ps1t7O9qXCWOnsH1ZWQIGKhxwd6+nSjvtQdbmumkHnfU5Qz5 4hgia9xtU7cdSf7DV1pxOv918soZTETfP9y6p/YjlggdPJtpYrFrdzhRYnNU6iAjuz 856ABaM8WnWEl7gpVA7eyB4gT/LccVl3+bNnLrRX9Wjy/MwYJcgjt9rF/r+0cgbhPG teLq+sYE8QGXg== X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 96.114.154.170 Cc: armbru@redhat.com, Michael Roth Subject: [Qemu-devel] [PATCH RFC 4/5] qapi: Simplify visits of optional fields 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 None of the visitor callbacks would set an error when testing if an optional field was present; make this part of the interface contract by eliminating the errp argument. Then, for less code, reflect the determined boolean value back to the caller instead of making the caller read the boolean after the fact. The resulting generated code has a nice diff: |- visit_optional(v, &has_fdset_id, "fdset-id", &err); |- if (err) { |- goto out; |- } |- if (has_fdset_id) { |+ if (visit_optional(v, &has_fdset_id, "fdset-id")) { | visit_type_int(v, &fdset_id, "fdset-id", &err); | if (err) { | goto out; | } | } Signed-off-by: Eric Blake --- include/qapi/visitor-impl.h | 3 +-- include/qapi/visitor.h | 5 ++--- qapi/opts-visitor.c | 2 +- qapi/qapi-visit-core.c | 6 +++--- qapi/qmp-input-visitor.c | 3 +-- qapi/string-input-visitor.c | 3 +-- scripts/qapi.py | 9 ++------- 7 files changed, 11 insertions(+), 20 deletions(-) diff --git a/include/qapi/visitor-impl.h b/include/qapi/visitor-impl.h index 7e8f728..6feb654 100644 --- a/include/qapi/visitor-impl.h +++ b/include/qapi/visitor-impl.h @@ -69,8 +69,7 @@ struct Visitor Error **errp); /* May be NULL; most useful for input visitors. */ - void (*optional)(Visitor *v, bool *present, const char *name, - Error **errp); + void (*optional)(Visitor *v, bool *present, const char *name); /* Only required to visit uint64 differently than (*type_int)(). */ void (*type_uint64)(Visitor *v, uint64_t *obj, const char *name, diff --git a/include/qapi/visitor.h b/include/qapi/visitor.h index cd1431a..ef1105b 100644 --- a/include/qapi/visitor.h +++ b/include/qapi/visitor.h @@ -118,10 +118,9 @@ void visit_end_list(Visitor *v, Error **errp); * Check if an optional member @name of a QDict needs visiting. * For input visitors, set *@present according to whether the * corresponding visit_type_*() needs calling; for other visitors, - * leave *@present unchanged. + * leave *@present unchanged. Return *@present for convenience. */ -void visit_optional(Visitor *v, bool *present, const char *name, - Error **errp); +bool visit_optional(Visitor *v, bool *present, const char *name); /** * Determine the qtype of the item @name in the current QDict visit. diff --git a/qapi/opts-visitor.c b/qapi/opts-visitor.c index 7ae33b3..b2cd5b3 100644 --- a/qapi/opts-visitor.c +++ b/qapi/opts-visitor.c @@ -488,7 +488,7 @@ opts_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp) static void -opts_optional(Visitor *v, bool *present, const char *name, Error **errp) +opts_optional(Visitor *v, bool *present, const char *name) { OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v); diff --git a/qapi/qapi-visit-core.c b/qapi/qapi-visit-core.c index 9cd17f8..d885347 100644 --- a/qapi/qapi-visit-core.c +++ b/qapi/qapi-visit-core.c @@ -62,12 +62,12 @@ void visit_end_list(Visitor *v, Error **errp) v->end_list(v, errp); } -void visit_optional(Visitor *v, bool *present, const char *name, - Error **errp) +bool visit_optional(Visitor *v, bool *present, const char *name) { if (v->optional) { - v->optional(v, present, name, errp); + v->optional(v, present, name); } + return *present; } void visit_get_next_type(Visitor *v, qtype_code *type, bool promote_int, diff --git a/qapi/qmp-input-visitor.c b/qapi/qmp-input-visitor.c index 5310db5..f714dfc 100644 --- a/qapi/qmp-input-visitor.c +++ b/qapi/qmp-input-visitor.c @@ -300,8 +300,7 @@ static void qmp_input_type_any(Visitor *v, QObject **obj, const char *name, *obj = qobj; } -static void qmp_input_optional(Visitor *v, bool *present, const char *name, - Error **errp) +static void qmp_input_optional(Visitor *v, bool *present, const char *name) { QmpInputVisitor *qiv = to_qiv(v); QObject *qobj = qmp_input_get_object(qiv, name, true); diff --git a/qapi/string-input-visitor.c b/qapi/string-input-visitor.c index bbd6a54..dee780a 100644 --- a/qapi/string-input-visitor.c +++ b/qapi/string-input-visitor.c @@ -299,8 +299,7 @@ static void parse_type_number(Visitor *v, double *obj, const char *name, *obj = val; } -static void parse_optional(Visitor *v, bool *present, const char *name, - Error **errp) +static void parse_optional(Visitor *v, bool *present, const char *name) { StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v); diff --git a/scripts/qapi.py b/scripts/qapi.py index 063d299..9af310f 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -1475,15 +1475,10 @@ def gen_visit_fields(members, prefix, errarg): for memb in members: if memb.optional: ret += mcgen(''' -visit_optional(v, &%(prefix)shas_%(c_name)s, "%(name)s", %(errp)s); +if (visit_optional(v, &%(prefix)shas_%(c_name)s, "%(name)s")) { ''', prefix=prefix, c_name=c_name(memb.name), - name=memb.name, errp=errparg) - ret += gen_err_check(errarg) - ret += mcgen(''' -if (%(prefix)shas_%(c_name)s) { -''', - prefix=prefix, c_name=c_name(memb.name)) + name=memb.name) push_indent() ret += mcgen('''