From nobody Tue Feb 10 10:54:49 2026 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.zohomail.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 1503409812152845.6601858607539; Tue, 22 Aug 2017 06:50:12 -0700 (PDT) Received: from localhost ([::1]:47132 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dk9ZK-00058T-SH for importer@patchew.org; Tue, 22 Aug 2017 09:50:10 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54463) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dk9H3-0005xz-74 for qemu-devel@nongnu.org; Tue, 22 Aug 2017 09:31:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dk9Gz-0005KR-4O for qemu-devel@nongnu.org; Tue, 22 Aug 2017 09:31:17 -0400 Received: from mx1.redhat.com ([209.132.183.28]:63581) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dk9Gy-0005K2-Ru for qemu-devel@nongnu.org; Tue, 22 Aug 2017 09:31:13 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 12C2F769E1; Tue, 22 Aug 2017 13:24:28 +0000 (UTC) Received: from localhost (ovpn-112-43.ams2.redhat.com [10.36.112.43]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9802460606; Tue, 22 Aug 2017 13:24:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 12C2F769E1 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=marcandre.lureau@redhat.com From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Tue, 22 Aug 2017 15:22:16 +0200 Message-Id: <20170822132255.23945-16-marcandre.lureau@redhat.com> In-Reply-To: <20170822132255.23945-1-marcandre.lureau@redhat.com> References: <20170822132255.23945-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Tue, 22 Aug 2017 13:24:28 +0000 (UTC) Content-Transfer-Encoding: quoted-printable 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 15/54] qapi: add 'if' to top-level expressions 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: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , 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-Type: text/plain; charset="utf-8" Accept 'if' key in top-level elements, accepted as string or list of string type. The following patches will modify the test visitor to check the value is correctly saved, and generate #if/#endif code (as a single #if/endif line or a series for a list). Example of 'if' key: { 'struct': 'TestIfStruct', 'data': { 'foo': 'int' }, = = 'if': 'define= d(TEST_IF_STRUCT)' } A following patch for qapi-code-gen.txt will provide more complete documentation for 'if' usage. Signed-off-by: Marc-Andr=C3=A9 Lureau --- scripts/qapi.py | 13 +++++++++++++ tests/test-qmp-commands.c | 6 ++++++ tests/qapi-schema/qapi-schema-test.json | 20 ++++++++++++++++++++ tests/qapi-schema/qapi-schema-test.out | 22 ++++++++++++++++++++++ 4 files changed, 61 insertions(+) diff --git a/scripts/qapi.py b/scripts/qapi.py index 73adb90379..a94d93ada4 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -639,6 +639,16 @@ def add_name(name, info, meta, implicit=3DFalse): all_names[name] =3D meta =20 =20 +def check_if(expr, info): + ifcond =3D expr.get('if') + if not ifcond or isinstance(ifcond, str): + return + if (not isinstance(ifcond, list) or + any([not isinstance(elt, str) for elt in ifcond])): + raise QAPISemError(info, "'if' condition requires a string or " + "a list of string") + + def check_type(info, source, value, allow_array=3DFalse, allow_dict=3DFalse, allow_optional=3DFalse, allow_metas=3D[]): @@ -865,6 +875,7 @@ def check_keys(expr_elem, meta, required, optional=3D[]= ): expr =3D expr_elem['expr'] info =3D expr_elem['info'] name =3D expr[meta] + optional =3D optional + ['if'] # all top-level elem accept if if not isinstance(name, str): raise QAPISemError(info, "'%s' key must have a string value" % met= a) required =3D required + [meta] @@ -880,6 +891,8 @@ def check_keys(expr_elem, meta, required, optional=3D[]= ): raise QAPISemError(info, "'%s' of %s '%s' should only use true value" % (key, meta, name)) + if key =3D=3D 'if': + check_if(expr, info) for key in required: if key not in expr: raise QAPISemError(info, "Key '%s' is missing from %s '%s'" diff --git a/tests/test-qmp-commands.c b/tests/test-qmp-commands.c index 904c89d4d4..9b9a7ffee7 100644 --- a/tests/test-qmp-commands.c +++ b/tests/test-qmp-commands.c @@ -10,6 +10,12 @@ =20 static QmpCommandList qmp_commands; =20 +/* #if defined(TEST_IF_CMD) */ +void qmp_TestIfCmd(TestIfStruct *foo, Error **errp) +{ +} +/* #endif */ + void qmp_user_def_cmd(Error **errp) { } diff --git a/tests/qapi-schema/qapi-schema-test.json b/tests/qapi-schema/qa= pi-schema-test.json index c72dbd8050..dc2c444fc1 100644 --- a/tests/qapi-schema/qapi-schema-test.json +++ b/tests/qapi-schema/qapi-schema-test.json @@ -188,3 +188,23 @@ 'data': { 'a': ['__org.qemu_x-Enum'], 'b': ['__org.qemu_x-Struct'], 'c': '__org.qemu_x-Union2', 'd': '__org.qemu_x-Alt' }, 'returns': '__org.qemu_x-Union1' } + +# test 'if' condition handling + +{ 'struct': 'TestIfStruct', 'data': { 'foo': 'int' }, + 'if': 'defined(TEST_IF_STRUCT)' } + +{ 'enum': 'TestIfEnum', 'data': [ 'foo', 'bar' ], + 'if': 'defined(TEST_IF_ENUM)' } + +{ 'union': 'TestIfUnion', 'data': { 'foo': 'TestStruct' }, + 'if': 'defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)' } + +{ 'alternate': 'TestIfAlternate', 'data': { 'foo': 'int', 'bar': 'TestStru= ct' }, + 'if': 'defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)' } + +{ 'command': 'TestIfCmd', 'data': { 'foo': 'TestIfStruct' }, + 'if': 'defined(TEST_IF_CMD) && defined(TEST_IF_STRUCT)' } + +{ 'event': 'TestIfEvent', 'data': { 'foo': 'TestIfStruct' }, + 'if': 'defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)' } diff --git a/tests/qapi-schema/qapi-schema-test.out b/tests/qapi-schema/qap= i-schema-test.out index 3b1e9082d3..7fbaea19bc 100644 --- a/tests/qapi-schema/qapi-schema-test.out +++ b/tests/qapi-schema/qapi-schema-test.out @@ -52,6 +52,22 @@ enum QEnumTwo ['value1', 'value2'] prefix QENUM_TWO enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool'] prefix QTYPE +alternate TestIfAlternate + tag type + case foo: int + case bar: TestStruct +command TestIfCmd q_obj_TestIfCmd-arg -> None + gen=3DTrue success_response=3DTrue boxed=3DFalse +enum TestIfEnum ['foo', 'bar'] +event TestIfEvent q_obj_TestIfEvent-arg + boxed=3DFalse +object TestIfStruct + member foo: int optional=3DFalse +object TestIfUnion + member type: TestIfUnionKind optional=3DFalse + tag type + case foo: q_obj_TestStruct-wrapper +enum TestIfUnionKind ['foo'] object TestStruct member integer: int optional=3DFalse member boolean: bool optional=3DFalse @@ -172,6 +188,12 @@ object q_obj_EVENT_D-arg member b: str optional=3DFalse member c: str optional=3DTrue member enum3: EnumOne optional=3DTrue +object q_obj_TestIfCmd-arg + member foo: TestIfStruct optional=3DFalse +object q_obj_TestIfEvent-arg + member foo: TestIfStruct optional=3DFalse +object q_obj_TestStruct-wrapper + member data: TestStruct optional=3DFalse object q_obj_UserDefFlatUnion2-base member integer: int optional=3DTrue member string: str optional=3DFalse --=20 2.14.1.146.gd35faa819