From nobody Wed May 1 10:12:02 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1494015278919939.0129488390542; Fri, 5 May 2017 13:14:38 -0700 (PDT) Received: from localhost ([::1]:48838 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6jcb-0005sP-Dy for importer@patchew.org; Fri, 05 May 2017 16:14:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50417) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6jZh-0003jx-JR for qemu-devel@nongnu.org; Fri, 05 May 2017 16:11:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d6jZe-0001hI-14 for qemu-devel@nongnu.org; Fri, 05 May 2017 16:11:37 -0400 Received: from mx1.redhat.com ([209.132.183.28]:56410) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d6jZd-0001gI-Nt for qemu-devel@nongnu.org; Fri, 05 May 2017 16:11:33 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C2FE180C2C; Fri, 5 May 2017 20:11:32 +0000 (UTC) Received: from localhost (ovpn-116-28.gru2.redhat.com [10.97.116.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id 477DF1820C; Fri, 5 May 2017 20:11:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C2FE180C2C Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=ehabkost@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com C2FE180C2C From: Eduardo Habkost To: qemu-devel@nongnu.org Date: Fri, 5 May 2017 17:11:26 -0300 Message-Id: <20170505201128.12099-2-ehabkost@redhat.com> In-Reply-To: <20170505201128.12099-1-ehabkost@redhat.com> References: <20170505201128.12099-1-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Fri, 05 May 2017 20:11:32 +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] [PATCH v2 1/3] visitor: Add 'supported_qtypes' parameter to visit_start_alternate() 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: Markus Armbruster , Michael Roth Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This will allow visitors to make decisions based on the supported qtypes of a given alternate type. The new parameter can replace the old 'promote_int' argument, as qobject-input-visitor can simply check if QTYPE_QINT is set in supported_qtypes. Signed-off-by: Eduardo Habkost Reviewed-by: Eric Blake --- Changes v1 -> v2: * Change supported_qtypes to uint32_t (Eric Blake) * Replace assert() on all generated visitor functions with a single QEMU_BUILD_BUG_ON() on visit_start_alternate() * Extra spaces around "|" on generated visitor code (Eric Blake) * Don't use bitops.h and just use (1U << QTYPE_FOO) (Markus Armbruster) --- include/qapi/visitor.h | 5 +++-- include/qapi/visitor-impl.h | 2 +- scripts/qapi-visit.py | 12 ++++++------ qapi/qapi-visit-core.c | 9 ++++++--- qapi/qapi-clone-visitor.c | 3 ++- qapi/qapi-dealloc-visitor.c | 3 ++- qapi/qobject-input-visitor.c | 6 ++++-- qapi/trace-events | 2 +- 8 files changed, 25 insertions(+), 17 deletions(-) diff --git a/include/qapi/visitor.h b/include/qapi/visitor.h index 1a1b62012b..8f5a223714 100644 --- a/include/qapi/visitor.h +++ b/include/qapi/visitor.h @@ -408,7 +408,8 @@ void visit_end_list(Visitor *v, void **list); * the qtype of the next thing to be visited, stored in (*@obj)->type. * Other visitors will leave @obj unchanged. * - * If @promote_int, treat integers as QTYPE_FLOAT. + * @supported_qtypes is a bit mask indicating which QTypes are supported + * by the alternate. * * If successful, this must be paired with visit_end_alternate() with * the same @obj to clean up, even if visiting the contents of the @@ -416,7 +417,7 @@ void visit_end_list(Visitor *v, void **list); */ void visit_start_alternate(Visitor *v, const char *name, GenericAlternate **obj, size_t size, - bool promote_int, Error **errp); + uint32_t supported_qtypes, Error **errp); =20 /* * Finish visiting an alternate type. diff --git a/include/qapi/visitor-impl.h b/include/qapi/visitor-impl.h index e87709db5c..8afcde0f5d 100644 --- a/include/qapi/visitor-impl.h +++ b/include/qapi/visitor-impl.h @@ -71,7 +71,7 @@ struct Visitor * optional for output visitors. */ void (*start_alternate)(Visitor *v, const char *name, GenericAlternate **obj, size_t size, - bool promote_int, Error **errp); + uint32_t supported_qtypes, Error **errp); =20 /* Optional, needed for dealloc visitor */ void (*end_alternate)(Visitor *v, void **obj); diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py index 5737aefa05..41c54982e2 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -161,20 +161,20 @@ void visit_type_%(c_name)s(Visitor *v, const char *na= me, %(c_name)s *obj, Error =20 =20 def gen_visit_alternate(name, variants): - promote_int =3D 'true' + qtypes =3D ['(1U << %s)' % (var.type.alternate_qtype()) + for var in variants.variants] + supported_qtypes =3D ' | '.join(qtypes) ret =3D '' - for var in variants.variants: - if var.type.alternate_qtype() =3D=3D 'QTYPE_QINT': - promote_int =3D 'false' =20 ret +=3D mcgen(''' =20 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj,= Error **errp) { Error *err =3D NULL; + uint32_t supported_qtypes =3D %(supported_qtypes)s; =20 visit_start_alternate(v, name, (GenericAlternate **)obj, sizeof(**obj), - %(promote_int)s, &err); + supported_qtypes, &err); if (err) { goto out; } @@ -183,7 +183,7 @@ void visit_type_%(c_name)s(Visitor *v, const char *name= , %(c_name)s **obj, Error } switch ((*obj)->type) { ''', - c_name=3Dc_name(name), promote_int=3Dpromote_int) + c_name=3Dc_name(name), supported_qtypes=3Dsupported_qtype= s) =20 for var in variants.variants: ret +=3D mcgen(''' diff --git a/qapi/qapi-visit-core.c b/qapi/qapi-visit-core.c index 43a09d147d..8d62914393 100644 --- a/qapi/qapi-visit-core.c +++ b/qapi/qapi-visit-core.c @@ -106,15 +106,18 @@ void visit_end_list(Visitor *v, void **obj) =20 void visit_start_alternate(Visitor *v, const char *name, GenericAlternate **obj, size_t size, - bool promote_int, Error **errp) + uint32_t supported_qtypes, + Error **errp) { Error *err =3D NULL; =20 + QEMU_BUILD_BUG_ON(QTYPE__MAX > 32); + assert(obj && size >=3D sizeof(GenericAlternate)); assert(!(v->type & VISITOR_OUTPUT) || *obj); - trace_visit_start_alternate(v, name, obj, size, promote_int); + trace_visit_start_alternate(v, name, obj, size, supported_qtypes); if (v->start_alternate) { - v->start_alternate(v, name, obj, size, promote_int, &err); + v->start_alternate(v, name, obj, size, supported_qtypes, &err); } if (v->type & VISITOR_INPUT) { assert(v->start_alternate && !err !=3D !*obj); diff --git a/qapi/qapi-clone-visitor.c b/qapi/qapi-clone-visitor.c index 34086cbfc0..2e40da5981 100644 --- a/qapi/qapi-clone-visitor.c +++ b/qapi/qapi-clone-visitor.c @@ -70,7 +70,8 @@ static GenericList *qapi_clone_next_list(Visitor *v, Gene= ricList *tail, =20 static void qapi_clone_start_alternate(Visitor *v, const char *name, GenericAlternate **obj, size_t size, - bool promote_int, Error **errp) + uint32_t supported_qtypes, + Error **errp) { qapi_clone_start_struct(v, name, (void **)obj, size, errp); } diff --git a/qapi/qapi-dealloc-visitor.c b/qapi/qapi-dealloc-visitor.c index e39457bc79..23b64c21a4 100644 --- a/qapi/qapi-dealloc-visitor.c +++ b/qapi/qapi-dealloc-visitor.c @@ -38,7 +38,8 @@ static void qapi_dealloc_end_struct(Visitor *v, void **ob= j) =20 static void qapi_dealloc_start_alternate(Visitor *v, const char *name, GenericAlternate **obj, size_t si= ze, - bool promote_int, Error **errp) + uint32_t supported_qtypes, + Error **errp) { } =20 diff --git a/qapi/qobject-input-visitor.c b/qapi/qobject-input-visitor.c index 865e948ac0..25997ee816 100644 --- a/qapi/qobject-input-visitor.c +++ b/qapi/qobject-input-visitor.c @@ -338,7 +338,8 @@ static void qobject_input_check_list(Visitor *v, Error = **errp) =20 static void qobject_input_start_alternate(Visitor *v, const char *name, GenericAlternate **obj, size_t s= ize, - bool promote_int, Error **errp) + uint32_t supported_qtypes, + Error **errp) { QObjectInputVisitor *qiv =3D to_qiv(v); QObject *qobj =3D qobject_input_get_object(qiv, name, false, errp); @@ -349,7 +350,8 @@ static void qobject_input_start_alternate(Visitor *v, c= onst char *name, } *obj =3D g_malloc0(size); (*obj)->type =3D qobject_type(qobj); - if (promote_int && (*obj)->type =3D=3D QTYPE_QINT) { + if (!(supported_qtypes & (1U << QTYPE_QINT)) && + (*obj)->type =3D=3D QTYPE_QINT) { (*obj)->type =3D QTYPE_QFLOAT; } } diff --git a/qapi/trace-events b/qapi/trace-events index 339cacf0ad..b15a55b797 100644 --- a/qapi/trace-events +++ b/qapi/trace-events @@ -11,7 +11,7 @@ visit_next_list(void *v, void *tail, size_t size) "v=3D%p= tail=3D%p size=3D%zu" visit_check_list(void *v) "v=3D%p" visit_end_list(void *v, void *obj) "v=3D%p obj=3D%p" =20 -visit_start_alternate(void *v, const char *name, void *obj, size_t size, b= ool promote_int) "v=3D%p name=3D%s obj=3D%p size=3D%zu promote_int=3D%d" +visit_start_alternate(void *v, const char *name, void *obj, size_t size, u= int32_t supported_qtypes) "v=3D%p name=3D%s obj=3D%p size=3D%zu supported_q= types=3D0x%x" visit_end_alternate(void *v, void *obj) "v=3D%p obj=3D%p" =20 visit_optional(void *v, const char *name, bool *present) "v=3D%p name=3D%s= present=3D%p" --=20 2.11.0.259.g40922b1 From nobody Wed May 1 10:12:02 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1494015243845841.4371296004138; Fri, 5 May 2017 13:14:03 -0700 (PDT) Received: from localhost ([::1]:48831 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6jc2-0005Sx-Bk for importer@patchew.org; Fri, 05 May 2017 16:14:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50441) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6jZi-0003kf-RS for qemu-devel@nongnu.org; Fri, 05 May 2017 16:11:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d6jZh-0001kz-Jl for qemu-devel@nongnu.org; Fri, 05 May 2017 16:11:38 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44996) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d6jZh-0001jx-AD for qemu-devel@nongnu.org; Fri, 05 May 2017 16:11:37 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 514D0C05490E; Fri, 5 May 2017 20:11:36 +0000 (UTC) Received: from localhost (ovpn-116-28.gru2.redhat.com [10.97.116.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id 304F178A2E; Fri, 5 May 2017 20:11:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 514D0C05490E Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=ehabkost@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 514D0C05490E From: Eduardo Habkost To: qemu-devel@nongnu.org Date: Fri, 5 May 2017 17:11:27 -0300 Message-Id: <20170505201128.12099-3-ehabkost@redhat.com> In-Reply-To: <20170505201128.12099-1-ehabkost@redhat.com> References: <20170505201128.12099-1-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Fri, 05 May 2017 20:11:36 +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] [PATCH v2 2/3] qapi: Add enum_table[] parameter to start_alternate 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: Markus Armbruster , Michael Roth Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The new parameter will be used by the string input visitor to detect alternate types that can't be parsed unambiguously. Signed-off-by: Eduardo Habkost --- Changes v1 -> v2: * (new patch added to series) --- include/qapi/visitor.h | 10 +++++++++- include/qapi/visitor-impl.h | 4 +++- scripts/qapi-visit.py | 11 +++++++++-- qapi/qapi-visit-core.c | 7 +++++-- qapi/qapi-clone-visitor.c | 1 + qapi/qapi-dealloc-visitor.c | 1 + qapi/qobject-input-visitor.c | 1 + qapi/trace-events | 2 +- 8 files changed, 30 insertions(+), 7 deletions(-) diff --git a/include/qapi/visitor.h b/include/qapi/visitor.h index 8f5a223714..05e5ca0eb2 100644 --- a/include/qapi/visitor.h +++ b/include/qapi/visitor.h @@ -411,13 +411,21 @@ void visit_end_list(Visitor *v, void **list); * @supported_qtypes is a bit mask indicating which QTypes are supported * by the alternate. * + * @enum_table contains the enum value lookup table, in case + * strings in the input are going to be parsed as enums. Visitors + * aren't required to validate string input according to + * enum_table, as visit_type_enum() will be called automatically + * if (*obj)->type is QTYPE_QSTRING. + * * If successful, this must be paired with visit_end_alternate() with * the same @obj to clean up, even if visiting the contents of the * alternate fails. */ void visit_start_alternate(Visitor *v, const char *name, GenericAlternate **obj, size_t size, - uint32_t supported_qtypes, Error **errp); + uint32_t supported_qtypes, + const char *const enum_table[], + Error **errp); =20 /* * Finish visiting an alternate type. diff --git a/include/qapi/visitor-impl.h b/include/qapi/visitor-impl.h index 8afcde0f5d..b98370fabb 100644 --- a/include/qapi/visitor-impl.h +++ b/include/qapi/visitor-impl.h @@ -71,7 +71,9 @@ struct Visitor * optional for output visitors. */ void (*start_alternate)(Visitor *v, const char *name, GenericAlternate **obj, size_t size, - uint32_t supported_qtypes, Error **errp); + uint32_t supported_qtypes, + const char *const enum_table[], + Error **errp); =20 /* Optional, needed for dealloc visitor */ void (*end_alternate)(Visitor *v, void **obj); diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py index 41c54982e2..2f4dc56918 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -166,6 +166,12 @@ def gen_visit_alternate(name, variants): supported_qtypes =3D ' | '.join(qtypes) ret =3D '' =20 + enum_table =3D 'NULL' + for var in variants.variants: + if isinstance(var.type, QAPISchemaEnumType): + enum_table =3D '%s_lookup' % (var.type.c_name()) + break + ret +=3D mcgen(''' =20 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj,= Error **errp) @@ -174,7 +180,7 @@ void visit_type_%(c_name)s(Visitor *v, const char *name= , %(c_name)s **obj, Error uint32_t supported_qtypes =3D %(supported_qtypes)s; =20 visit_start_alternate(v, name, (GenericAlternate **)obj, sizeof(**obj), - supported_qtypes, &err); + supported_qtypes, %(enum_table)s, &err); if (err) { goto out; } @@ -183,7 +189,8 @@ void visit_type_%(c_name)s(Visitor *v, const char *name= , %(c_name)s **obj, Error } switch ((*obj)->type) { ''', - c_name=3Dc_name(name), supported_qtypes=3Dsupported_qtype= s) + c_name=3Dc_name(name), supported_qtypes=3Dsupported_qtype= s, + enum_table=3Denum_table) =20 for var in variants.variants: ret +=3D mcgen(''' diff --git a/qapi/qapi-visit-core.c b/qapi/qapi-visit-core.c index 8d62914393..479aa763c8 100644 --- a/qapi/qapi-visit-core.c +++ b/qapi/qapi-visit-core.c @@ -107,6 +107,7 @@ void visit_end_list(Visitor *v, void **obj) void visit_start_alternate(Visitor *v, const char *name, GenericAlternate **obj, size_t size, uint32_t supported_qtypes, + const char *const enum_table[], Error **errp) { Error *err =3D NULL; @@ -115,9 +116,11 @@ void visit_start_alternate(Visitor *v, const char *nam= e, =20 assert(obj && size >=3D sizeof(GenericAlternate)); assert(!(v->type & VISITOR_OUTPUT) || *obj); - trace_visit_start_alternate(v, name, obj, size, supported_qtypes); + trace_visit_start_alternate(v, name, obj, size, supported_qtypes, + (void *)enum_table); if (v->start_alternate) { - v->start_alternate(v, name, obj, size, supported_qtypes, &err); + v->start_alternate(v, name, obj, size, supported_qtypes, + enum_table, &err); } if (v->type & VISITOR_INPUT) { assert(v->start_alternate && !err !=3D !*obj); diff --git a/qapi/qapi-clone-visitor.c b/qapi/qapi-clone-visitor.c index 2e40da5981..9340441ee6 100644 --- a/qapi/qapi-clone-visitor.c +++ b/qapi/qapi-clone-visitor.c @@ -71,6 +71,7 @@ static GenericList *qapi_clone_next_list(Visitor *v, Gene= ricList *tail, static void qapi_clone_start_alternate(Visitor *v, const char *name, GenericAlternate **obj, size_t size, uint32_t supported_qtypes, + const char *const enum_table[], Error **errp) { qapi_clone_start_struct(v, name, (void **)obj, size, errp); diff --git a/qapi/qapi-dealloc-visitor.c b/qapi/qapi-dealloc-visitor.c index 23b64c21a4..fed366c660 100644 --- a/qapi/qapi-dealloc-visitor.c +++ b/qapi/qapi-dealloc-visitor.c @@ -39,6 +39,7 @@ static void qapi_dealloc_end_struct(Visitor *v, void **ob= j) static void qapi_dealloc_start_alternate(Visitor *v, const char *name, GenericAlternate **obj, size_t si= ze, uint32_t supported_qtypes, + const char *const enum_table[], Error **errp) { } diff --git a/qapi/qobject-input-visitor.c b/qapi/qobject-input-visitor.c index 25997ee816..ae4a6a04e1 100644 --- a/qapi/qobject-input-visitor.c +++ b/qapi/qobject-input-visitor.c @@ -339,6 +339,7 @@ static void qobject_input_check_list(Visitor *v, Error = **errp) static void qobject_input_start_alternate(Visitor *v, const char *name, GenericAlternate **obj, size_t s= ize, uint32_t supported_qtypes, + const char *const enum_table[], Error **errp) { QObjectInputVisitor *qiv =3D to_qiv(v); diff --git a/qapi/trace-events b/qapi/trace-events index b15a55b797..384c251814 100644 --- a/qapi/trace-events +++ b/qapi/trace-events @@ -11,7 +11,7 @@ visit_next_list(void *v, void *tail, size_t size) "v=3D%p= tail=3D%p size=3D%zu" visit_check_list(void *v) "v=3D%p" visit_end_list(void *v, void *obj) "v=3D%p obj=3D%p" =20 -visit_start_alternate(void *v, const char *name, void *obj, size_t size, u= int32_t supported_qtypes) "v=3D%p name=3D%s obj=3D%p size=3D%zu supported_q= types=3D0x%x" +visit_start_alternate(void *v, const char *name, void *obj, size_t size, u= int32_t supported_qtypes, void *enum_table) "v=3D%p name=3D%s obj=3D%p size= =3D%zu supported_qtypes=3D0x%x enum_table=3D%p" visit_end_alternate(void *v, void *obj) "v=3D%p obj=3D%p" =20 visit_optional(void *v, const char *name, bool *present) "v=3D%p name=3D%s= present=3D%p" --=20 2.11.0.259.g40922b1 From nobody Wed May 1 10:12:02 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 14940151852941023.901513484115; Fri, 5 May 2017 13:13:05 -0700 (PDT) Received: from localhost ([::1]:48827 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6jb4-0004eI-Nm for importer@patchew.org; Fri, 05 May 2017 16:13:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50472) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6jZl-0003mm-9d for qemu-devel@nongnu.org; Fri, 05 May 2017 16:11:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d6jZj-0001m9-I7 for qemu-devel@nongnu.org; Fri, 05 May 2017 16:11:41 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51114) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d6jZj-0001lY-8N for qemu-devel@nongnu.org; Fri, 05 May 2017 16:11:39 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 493163D97B; Fri, 5 May 2017 20:11:38 +0000 (UTC) Received: from localhost (ovpn-116-28.gru2.redhat.com [10.97.116.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id A019E79821; Fri, 5 May 2017 20:11:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 493163D97B Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=ehabkost@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 493163D97B From: Eduardo Habkost To: qemu-devel@nongnu.org Date: Fri, 5 May 2017 17:11:28 -0300 Message-Id: <20170505201128.12099-4-ehabkost@redhat.com> In-Reply-To: <20170505201128.12099-1-ehabkost@redhat.com> References: <20170505201128.12099-1-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Fri, 05 May 2017 20:11:38 +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] [PATCH v2 3/3] string-input-visitor: Support alternate types 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: Markus Armbruster , Michael Roth Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" When parsing alternates from a string, there are some limitations in what we can do, but it is a valid use case in some situations. We can support booleans, integer types, and enums. This will be used to support 'feature=3Dforce' in -cpu options, while keeping 'feature=3Don|off|true|false' represented as boolean values. Signed-off-by: Eduardo Habkost --- Changes v1 -> v2: * Updated string_input_visitor_new() documentation to mention alternate support (Markus Armbruster) * Detect ambiguous alternates at runtime. Test case included. * Removed support for integers. We don't need it yet, and it would require sorting out the parse_str() mess. * Change supported_qtypes to uint32_t (Eric Blake) * Update tests/qapi-schema/qapi-schema-test.out to match qapi-schema-test.json updates (Eric Blake) * Code indentation fix (Markus Armbruster) * Use &error_abort on test cases instead of g_assert(!err) (Markus Armbruster) --- include/qapi/string-input-visitor.h | 6 +- qapi/string-input-visitor.c | 99 +++++++++++++++++++++++++++++= ---- tests/test-string-input-visitor.c | 76 +++++++++++++++++++++++++ tests/qapi-schema/qapi-schema-test.json | 8 +++ tests/qapi-schema/qapi-schema-test.out | 9 +++ 5 files changed, 187 insertions(+), 11 deletions(-) diff --git a/include/qapi/string-input-visitor.h b/include/qapi/string-inpu= t-visitor.h index 33551340e3..e7f359f225 100644 --- a/include/qapi/string-input-visitor.h +++ b/include/qapi/string-input-visitor.h @@ -19,8 +19,12 @@ typedef struct StringInputVisitor StringInputVisitor; =20 /* * The string input visitor does not implement support for visiting - * QAPI structs, alternates, null, or arbitrary QTypes. It also + * QAPI structs, null, or arbitrary QTypes. It also * requires a non-null list argument to visit_start_list(). + * + * Support for alternates is very limited: only bool and enum + * members are supported, and only when the enum members' + * representations can't be confused with a bool value. */ Visitor *string_input_visitor_new(const char *str); =20 diff --git a/qapi/string-input-visitor.c b/qapi/string-input-visitor.c index c089491c24..e339b88192 100644 --- a/qapi/string-input-visitor.c +++ b/qapi/string-input-visitor.c @@ -19,6 +19,7 @@ #include "qemu/option.h" #include "qemu/queue.h" #include "qemu/range.h" +#include "qemu/host-utils.h" =20 =20 struct StringInputVisitor @@ -278,21 +279,34 @@ static void parse_type_size(Visitor *v, const char *n= ame, uint64_t *obj, *obj =3D val; } =20 +static int try_parse_bool(const char *s, bool *result) +{ + if (!strcasecmp(s, "on") || + !strcasecmp(s, "yes") || + !strcasecmp(s, "true")) { + if (result) { + *result =3D true; + } + return 0; + } + if (!strcasecmp(s, "off") || + !strcasecmp(s, "no") || + !strcasecmp(s, "false")) { + if (result) { + *result =3D false; + } + return 0; + } + + return -1; +} + static void parse_type_bool(Visitor *v, const char *name, bool *obj, Error **errp) { StringInputVisitor *siv =3D to_siv(v); =20 - if (!strcasecmp(siv->string, "on") || - !strcasecmp(siv->string, "yes") || - !strcasecmp(siv->string, "true")) { - *obj =3D true; - return; - } - if (!strcasecmp(siv->string, "off") || - !strcasecmp(siv->string, "no") || - !strcasecmp(siv->string, "false")) { - *obj =3D false; + if (try_parse_bool(siv->string, obj) =3D=3D 0) { return; } =20 @@ -326,6 +340,70 @@ static void parse_type_number(Visitor *v, const char *= name, double *obj, *obj =3D val; } =20 +/* + * Check if alternate type string representation is ambiguous and + * can't be parsed by StringInputVisitor + */ +static bool ambiguous_alternate(uint32_t qtypes, const char *const enum_ta= ble[]) +{ + uint32_t non_str_qtypes =3D qtypes & ~(1U << QTYPE_QSTRING); + + if ((qtypes & (1U << QTYPE_QSTRING)) && !enum_table && non_str_qtypes)= { + return true; + } + + if (qtypes & (1U << QTYPE_QBOOL)) { + const char *const *e; + /* + * If the string representation of enum members can be parsed as + * booleans, the alternate string representation is ambiguous. + */ + for (e =3D enum_table; e && *e; e++) { + if (try_parse_bool(*e, NULL) =3D=3D 0) { + return true; + } + } + } + + return false; +} + +static void start_alternate(Visitor *v, const char *name, + GenericAlternate **obj, size_t size, + uint32_t qtypes, const char *const enum_table[= ], + Error **errp) +{ + /* + * Enum types are represented as QTYPE_QSTRING, so this is + * the default. Actual parsing of the string as an enum is + * done by visit_type_(), which is called just + * after visit_start_alternate(). + */ + QType qtype =3D QTYPE_QSTRING; + uint32_t unsupported_qtypes =3D qtypes & ~((1U << QTYPE_QSTRING) | + (1U << QTYPE_QBOOL)); + StringInputVisitor *siv =3D to_siv(v); + + if (ambiguous_alternate(qtypes, enum_table)) { + error_setg(errp, "Can't parse ambiguous alternate type"); + return; + } + + if (unsupported_qtypes) { + error_setg(errp, "Can't parse %s' alternate member", + QType_lookup[ctz32(unsupported_qtypes)]); + return; + } + + if ((qtypes & (1U << QTYPE_QBOOL)) && + try_parse_bool(siv->string, NULL) =3D=3D 0) { + qtype =3D QTYPE_QBOOL; + } + + *obj =3D g_malloc0(size); + (*obj)->type =3D qtype; +} + static void string_input_free(Visitor *v) { StringInputVisitor *siv =3D to_siv(v); @@ -353,6 +431,7 @@ Visitor *string_input_visitor_new(const char *str) v->visitor.next_list =3D next_list; v->visitor.check_list =3D check_list; v->visitor.end_list =3D end_list; + v->visitor.start_alternate =3D start_alternate; v->visitor.free =3D string_input_free; =20 v->string =3D str; diff --git a/tests/test-string-input-visitor.c b/tests/test-string-input-vi= sitor.c index 79313a7f7a..263fcc2b8c 100644 --- a/tests/test-string-input-visitor.c +++ b/tests/test-string-input-visitor.c @@ -290,6 +290,76 @@ static void test_visitor_in_enum(TestInputVisitorData = *data, } } =20 +static void test_visitor_in_alt_bool_enum(TestInputVisitorData *data, + const void *unused) +{ + Error *err =3D NULL; + Visitor *v; + AltBoolEnum *be =3D NULL; + + v =3D visitor_input_test_init(data, "true"); + visit_type_AltBoolEnum(v, NULL, &be, &error_abort); + g_assert_cmpint(be->type, =3D=3D, QTYPE_QBOOL); + g_assert(be->u.b); + qapi_free_AltBoolEnum(be); + + v =3D visitor_input_test_init(data, "off"); + visit_type_AltBoolEnum(v, NULL, &be, &error_abort); + g_assert_cmpint(be->type, =3D=3D, QTYPE_QBOOL); + g_assert(!be->u.b); + qapi_free_AltBoolEnum(be); + + v =3D visitor_input_test_init(data, "value2"); + visit_type_AltBoolEnum(v, NULL, &be, &error_abort); + g_assert_cmpint(be->type, =3D=3D, QTYPE_QSTRING); + g_assert_cmpint(be->u.e, =3D=3D, ENUM_ONE_VALUE2); + qapi_free_AltBoolEnum(be); + + v =3D visitor_input_test_init(data, "value100"); + visit_type_AltBoolEnum(v, NULL, &be, &err); + error_free_or_abort(&err); + qapi_free_AltBoolEnum(be); + + v =3D visitor_input_test_init(data, "10"); + visit_type_AltBoolEnum(v, NULL, &be, &err); + error_free_or_abort(&err); + qapi_free_AltBoolEnum(be); +} + +static void test_visitor_in_alt_ambig_str(TestInputVisitorData *data, + const void *unused) +{ + Error *err =3D NULL; + Visitor *v; + AltStrBool *sb =3D NULL; + + /* + * AltStrBool has an ambiguous string representation, and + * can't be handled by string-input-visitor: + */ + v =3D visitor_input_test_init(data, "s"); + visit_type_AltStrBool(v, NULL, &sb, &err); + error_free_or_abort(&err); + qapi_free_AltStrBool(sb); +} + +static void test_visitor_in_alt_ambig_enum(TestInputVisitorData *data, + const void *unused) +{ + Error *err =3D NULL; + Visitor *v; + AltOnOffBool *ob =3D NULL; + + /* + * AltOnOffBool has an ambiguous string representation, and + * can't be handled by string-input-visitor: + */ + v =3D visitor_input_test_init(data, "on"); + visit_type_AltOnOffBool(v, NULL, &ob, &err); + error_free_or_abort(&err); + qapi_free_AltOnOffBool(ob); +} + /* Try to crash the visitors */ static void test_visitor_in_fuzz(TestInputVisitorData *data, const void *unused) @@ -366,6 +436,12 @@ int main(int argc, char **argv) &in_visitor_data, test_visitor_in_string); input_visitor_test_add("/string-visitor/input/enum", &in_visitor_data, test_visitor_in_enum); + input_visitor_test_add("/string-visitor/input/alternate/bool_enum", + &in_visitor_data, test_visitor_in_alt_bool_enu= m); + input_visitor_test_add("/string-visitor/input/alternate/ambig_enum", + &in_visitor_data, test_visitor_in_alt_ambig_en= um); + input_visitor_test_add("/string-visitor/input/alternate/ambig_str", + &in_visitor_data, test_visitor_in_alt_ambig_st= r); input_visitor_test_add("/string-visitor/input/fuzz", &in_visitor_data, test_visitor_in_fuzz); =20 diff --git a/tests/qapi-schema/qapi-schema-test.json b/tests/qapi-schema/qa= pi-schema-test.json index 842ea3c5e3..d602f3d40b 100644 --- a/tests/qapi-schema/qapi-schema-test.json +++ b/tests/qapi-schema/qapi-schema-test.json @@ -106,6 +106,14 @@ { 'alternate': 'AltIntNum', 'data': { 'i': 'int', 'n': 'number' } } { 'alternate': 'AltNumInt', 'data': { 'n': 'number', 'i': 'int' } } =20 +# for testing string-input-visitor handling of alternates: +{ 'alternate': 'AltBoolEnum', 'data': { 'b': 'bool', 'e': 'EnumOne' } } + +# this alternate type will be detected as ambiguous by string-input-visito= r: +{ 'enum': 'TestOnOffAuto', + 'data': [ 'on', 'off', 'auto' ] } +{ 'alternate': 'AltOnOffBool', 'data': { 'o': 'TestOnOffAuto', 'b': 'bool'= } } + # for testing native lists { 'union': 'UserDefNativeListUnion', 'data': { 'integer': ['int'], diff --git a/tests/qapi-schema/qapi-schema-test.out b/tests/qapi-schema/qap= i-schema-test.out index 9d99c4eebb..17cd276295 100644 --- a/tests/qapi-schema/qapi-schema-test.out +++ b/tests/qapi-schema/qapi-schema-test.out @@ -1,3 +1,7 @@ +alternate AltBoolEnum + tag type + case b: bool + case e: EnumOne alternate AltIntNum tag type case i: int @@ -10,6 +14,10 @@ alternate AltNumStr tag type case n: number case s: str +alternate AltOnOffBool + tag type + case o: TestOnOffAuto + case b: bool alternate AltStrBool tag type case s: str @@ -56,6 +64,7 @@ enum QEnumTwo ['value1', 'value2'] prefix QENUM_TWO enum QType ['none', 'qnull', 'qint', 'qstring', 'qdict', 'qlist', 'qfloat'= , 'qbool'] prefix QTYPE +enum TestOnOffAuto ['on', 'off', 'auto'] object TestStruct member integer: int optional=3DFalse member boolean: bool optional=3DFalse --=20 2.11.0.259.g40922b1