From patchwork Wed Jun 13 15:53:47 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Nefedov X-Patchwork-Id: 10462605 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7D914603B4 for ; Wed, 13 Jun 2018 15:57:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5EDB728D24 for ; Wed, 13 Jun 2018 15:57:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 513C928D2A; Wed, 13 Jun 2018 15:57:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAD_ENC_HEADER,BAYES_00, DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7C42C28D24 for ; Wed, 13 Jun 2018 15:57:29 +0000 (UTC) Received: from localhost ([::1]:35309 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fT89I-00018L-SH for patchwork-qemu-devel@patchwork.kernel.org; Wed, 13 Jun 2018 11:57:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43754) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fT864-0007VC-5h for qemu-devel@nongnu.org; Wed, 13 Jun 2018 11:54:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fT860-0000lJ-I4 for qemu-devel@nongnu.org; Wed, 13 Jun 2018 11:54:08 -0400 Received: from mail-he1eur04on0719.outbound.protection.outlook.com ([2a01:111:f400:fe0d::719]:12324 helo=EUR04-HE1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fT860-0000d6-40 for qemu-devel@nongnu.org; Wed, 13 Jun 2018 11:54:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=hZ1APnLVnwSxfRyGNh8a1RgYyA4XRfATTgaNP6I5rrQ=; b=ZIMketcDpG/VSKtBSyV5Quu7LtMD/ceVWZgvSDTb2YXdMEBCTaS5ug0Oyo46qkAB2PZYQG7mXiUpvKA9BKWg5G/ZRHsNtbxTOcNRXrPEUPVWfJaLzf+JaSPZUbpP4Bdip1+lmPJO/98HLi1Bg+Q8AQEuoWDFjKEm1OUmeulugvw= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=anton.nefedov@virtuozzo.com; Received: from xantnef-ws.sw.ru (195.214.232.6) by VI1PR08MB3360.eurprd08.prod.outlook.com (2603:10a6:803:3f::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.841.16; Wed, 13 Jun 2018 15:54:01 +0000 From: Anton Nefedov To: qemu-devel@nongnu.org Date: Wed, 13 Jun 2018 18:53:47 +0300 Message-Id: <1528905228-25694-2-git-send-email-anton.nefedov@virtuozzo.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1528905228-25694-1-git-send-email-anton.nefedov@virtuozzo.com> References: <1528905228-25694-1-git-send-email-anton.nefedov@virtuozzo.com> MIME-Version: 1.0 X-Originating-IP: [195.214.232.6] X-ClientProxiedBy: HE1P190CA0003.EURP190.PROD.OUTLOOK.COM (2603:10a6:3:bc::13) To VI1PR08MB3360.eurprd08.prod.outlook.com (2603:10a6:803:3f::19) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 122f4a47-7d04-4d6c-6baf-08d5d145e1f2 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(5600026)(711020)(4534165)(7168020)(4627221)(201703031133081)(201702281549075)(2017052603328)(7153060)(7193020); SRVR:VI1PR08MB3360; X-Microsoft-Exchange-Diagnostics: 1; VI1PR08MB3360; 3:PSDVBS8L5l2da6exaYGcCw0BEF8g2YcusDkRcRaZjqIlrOGNVvH/GDFgmTFsJV9x/77LdfrlWG1lIpjKlc6BZxwRgrSTaDIxMWd6e+rw/R7M60FjTQBHxsMlsKQDrmSnPa5V1A8wU5I1MJIwfXzkxeEv1PkZrRMoNhTEp0bywjof4ZL+SymX2jrxSACt1R5R2WnUWcE+ZE8IAX+Nxq9t6L4B9wnI7LaZpx6tTStOA6w/qx3El2j25gQ4B99I07Yt; 25:k4lkqaxBDzqiXrhd54nH3Qa4ElkYdNJcFQT/3O4J0dK0t+M3RoTmo71ahr741VvDt+77kZ2Jo+5f6OvVTW/u5aNcgSpihUNYYhv7Gz5BdSfPOVOM/U/OoEboFJ5D3mu1Q46T57G1IyuhYJAXzj2QrelUfKEXITWCtTyViyAg/x/DbivhpexRXhf/g3p6J4NolSJS//rWqiyRrWqb3tNZ7lz1pKV9Ktk7AQ9+npyLBKmiHytyHeIaWOMqLwLNBuaUgF+M7GqBkutbPkSp5sGkRC6qnZdnJ+6BiA6+iWS8tu/6ZXxGLBnoNl9fyhO2F5AhWVp7xdInRKY4BBpBZnoXMA==; 31:ZVYZSx6A+BMiH9Rjs1YclRByOrGyliB4bxEo5srJvWvdLBT3oKRxF9LVh+MHVmA7jkkpghLD9mjDAcuxwLMtWn4dbRDvm65N56N1lhyfQUY+Ges5KoBKq1trBMjNZYMQ+u3eUqpshp0juea+5nWyTBCCuxKW/02CHyWZlnA6YfIVT9L2iCgsDq/ZgMG2BKGXC0z8+C7cVvtCj3bgXdLCuqRt3UHfy/2Fi33u4IVkOGQ= X-MS-TrafficTypeDiagnostic: VI1PR08MB3360: X-Microsoft-Exchange-Diagnostics: 1; VI1PR08MB3360; 20:4MHDc6coE3MlJaXwBVKNQ6e1u4sF52KsTm4ynORyzOQV6ASGMC91hB9tP5UMXJxq6zDjXWIITD9jamK+uKF/5gU8O1QQZIQaUX1gku57N12wG+oC2NCT7qlmJs0wMCmU7iwza+LD9U8+UYKGQjcAW1rO7zDn7LaZcvb6zyiFspgfKcW+WOd+Frxb5X1GU1N0pMl16czjOkVNZFLnEtC/GXNER/P60Y2z/dPIDIK7K3Fp9prqcXt2xeO1e+2smm8K9OilV7vduLUxdFFlW0REQO3bFp3zTRvNTxDU4Dbc3YX9JDJR9LwrPs/whx7VCLZbfhodYTRZyc+YudxmPFZ/ssGDwrlPE8NvtUDOrfKI5plr75yINU9V4AYP92Richon1VnK131XnkWGApcPxiYz246J3V8OmUF0SKUp1VBsoHMv18/ZwZA8Pk63/D3hQyvkex5ocGU9/dM0zRYt7ceVUbT4+f4TMHWZXbzBCRWXvnD3fCqmEXZP01lWJxTAFxXq; 4:3ZKFU84qkyEacXXM6xgxXYgD/2HKhHh+WUXjwcLDL5u90Bl7ZWQ3BybmuVRjsHKmaJAleHA6fS3O458mCZE5mj+ckg3tGLpvZMLPLzsV8fiL3i4ico1J37t1rhF9K0OqGk/oI/7pfQtEI51Rc12xNIoCEa20TWTwZx65X2AJ5KuXlyVKlIvKnGAPlZDeXf5UuAM9oCh3n+iG0YOEpbtjMAw3FcN8gRrn3WKwLcge1UdIp2qe6nn3LSjfVG7O+XJbLMg8aszf2DJhwNyATxnHlA== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-MS-Exchange-SenderADCheck: 1 X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040522)(2401047)(5005006)(8121501046)(3231254)(944501410)(52105095)(10201501046)(93006095)(93001095)(3002001)(149027)(150027)(6041310)(20161123562045)(20161123564045)(20161123560045)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011)(7699016); SRVR:VI1PR08MB3360; BCL:0; PCL:0; RULEID:; SRVR:VI1PR08MB3360; X-Forefront-PRVS: 07025866F6 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(39380400002)(396003)(39850400004)(376002)(366004)(346002)(199004)(189003)(5660300001)(446003)(97736004)(956004)(11346002)(81156014)(575784001)(2351001)(7736002)(44832011)(26005)(2616005)(69596002)(2906002)(66066001)(476003)(50226002)(6116002)(8936002)(86362001)(36756003)(3846002)(478600001)(81166006)(6666003)(486006)(6916009)(47776003)(8676002)(316002)(16586007)(55236004)(305945005)(6512007)(68736007)(76176011)(48376002)(6506007)(16526019)(107886003)(53936002)(4326008)(51416003)(25786009)(186003)(6486002)(50466002)(53416004)(52116002)(2361001)(59450400001)(106356001)(105586002)(386003); DIR:OUT; SFP:1102; SCL:1; SRVR:VI1PR08MB3360; H:xantnef-ws.sw.ru; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; Received-SPF: None (protection.outlook.com: virtuozzo.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; VI1PR08MB3360; 23:HKhY0Ht3A3ZSZ78HX0BRThNpdOPfnCnXsib7kR2+e?= =?us-ascii?Q?Uhqx4yMCgEljEsO5jjzCKCT0OL3+Set7Lg8y+mVDBYeDQaC6OitzAV/4A/iy?= =?us-ascii?Q?il+KwEIXfX3D/1HfSMgxB6oyc8Ok8iCaAa+NngnTf6zpH/wJ3zpD1JhLhalQ?= =?us-ascii?Q?beUrSkGXi5S9LwwRR+XkVQko7RFWZPVdEuKL0yArtxY9saX8D1qF0ngfbihR?= =?us-ascii?Q?648PFM3eKWS6ubv4EXwfxYdV9u/GZi1SqNjyRDXhQMdH/X3x24Fo3NKTMwla?= =?us-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3j9eDBpRoYIAHgNciveKOlT21n8WRbhl8WJc8ggNFflwI+Bx7/TnsIqpdOjRfAUlfbeYNcC/dfzQMQgcqnWzFprVZ/pb7q90jSb0I+yf2IwmI=; 24:J4iUeVVOaMkUKnX0n0dznz0NPK6piH8ATE7IrwQxtd/oYSMfScfiPOAZBiA67q72icEW23+lpxuL5XwGwLJsOIYzNFNlEwOTivEF9AGI414= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; VI1PR08MB3360; 7:cPIDOpNaKUDIrdqUt1ltks1FKuj56/G9WPVN6Xh2fU/A1rMGpiAaEN+bv5whAdRWPrrU8E0G1264k69x+yr3BFCPqMqgNXYoI99YiOMW76uaEd3leHmdBY1yTFx0EWF09afTOulk/6Ej0AiKQBwc+Oleetx6f4PCqCYTP6j9lbHtY/qUu/gZ6EcqzQjtWTpiYvU5V7ko/4RVPxLMshmt/L8wFVb2VaVLT5lqHdyB6G4pzPdV+dPOxkX9BbDa0cMp; 20:YVKRAbYG5K2b1Hbtu6bkjh96/ZbIfsWP63GaFXDadPUWDcSXxWMrYuEESRE7sQfMiggz4w1QXkBxVgW7W1rMPgrEZb5UYuR10U31LuPeDjO6gY+dO7gsFjUyPIIRYUTEYy5u8NXQTC+/Y6w9pvdvfvexg6D5I07Qzflgn1B1a/k= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Jun 2018 15:54:01.1749 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 122f4a47-7d04-4d6c-6baf-08d5d145e1f2 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR08MB3360 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 2a01:111:f400:fe0d::719 Subject: [Qemu-devel] [PATCH v4 1/2] qapi: allow empty branches in flat unions 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: Anton Nefedov , armbru@redhat.com, mdroth@linux.vnet.ibm.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP It often happens that just a few discriminator values imply extra data in a flat union. Existing checks did not make possible to leave other values uncovered. Such cases had to be worked around by either stating a dummy (empty) type or introducing another (subset) discriminator enumeration. Both options create redundant entities in qapi files for little profit. With this patch it is not necessary anymore to add designated union fields for every possible value of a discriminator enumeration. Signed-off-by: Anton Nefedov Reviewed-by: Eric Blake --- docs/devel/qapi-code-gen.txt | 8 +++++--- tests/qapi-schema/flat-union-incomplete-branch.json | 9 --------- tests/qapi-schema/qapi-schema-test.json | 4 ++-- scripts/qapi/common.py | 11 ++++------- scripts/qapi/types.py | 4 +++- scripts/qapi/visit.py | 17 +++++++++++++---- tests/Makefile.include | 1 - tests/qapi-schema/flat-union-incomplete-branch.err | 1 - tests/qapi-schema/flat-union-incomplete-branch.exit | 1 - tests/qapi-schema/flat-union-incomplete-branch.out | 0 tests/qapi-schema/qapi-schema-test.out | 3 ++- 11 files changed, 29 insertions(+), 30 deletions(-) delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.json delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.err delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.exit delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.out diff --git a/docs/devel/qapi-code-gen.txt b/docs/devel/qapi-code-gen.txt index 1366228..88a70e4 100644 --- a/docs/devel/qapi-code-gen.txt +++ b/docs/devel/qapi-code-gen.txt @@ -496,9 +496,11 @@ Resulting in these JSON objects: Notice that in a flat union, the discriminator name is controlled by the user, but because it must map to a base member with enum type, the -code generator can ensure that branches exist for all values of the -enum (although the order of the keys need not match the declaration of -the enum). In the resulting generated C data types, a flat union is +code generator ensures that branches match the existing values of the +enum. The order of the keys need not match the declaration of the enum. +The keys need not cover all possible enum values. Omitted enum values +are still valid branches that add no additional members to the data type. +In the resulting generated C data types, a flat union is represented as a struct with the base members included directly, and then a union of structures for each branch of the struct. diff --git a/tests/qapi-schema/flat-union-incomplete-branch.json b/tests/qapi-schema/flat-union-incomplete-branch.json deleted file mode 100644 index 25a411b..0000000 --- a/tests/qapi-schema/flat-union-incomplete-branch.json +++ /dev/null @@ -1,9 +0,0 @@ -# we require all branches of the union to be covered -{ 'enum': 'TestEnum', - 'data': [ 'value1', 'value2' ] } -{ 'struct': 'TestTypeA', - 'data': { 'string': 'str' } } -{ 'union': 'TestUnion', - 'base': { 'type': 'TestEnum' }, - 'discriminator': 'type', - 'data': { 'value1': 'TestTypeA' } } diff --git a/tests/qapi-schema/qapi-schema-test.json b/tests/qapi-schema/qapi-schema-test.json index 46c7282..881b53b 100644 --- a/tests/qapi-schema/qapi-schema-test.json +++ b/tests/qapi-schema/qapi-schema-test.json @@ -39,7 +39,7 @@ '*enum1': 'EnumOne' } } # intentional forward reference { 'enum': 'EnumOne', - 'data': [ 'value1', 'value2', 'value3' ] } + 'data': [ 'value1', 'value2', 'value3', 'value4' ] } { 'struct': 'UserDefZero', 'data': { 'integer': 'int' } } @@ -76,7 +76,7 @@ 'discriminator': 'enum1', 'data': { 'value1' : 'UserDefA', 'value2' : 'UserDefB', - 'value3' : 'UserDefB' } } + 'value3' : 'UserDefB' } } # skip 'value4' { 'struct': 'UserDefUnionBase', 'base': 'UserDefZero', diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py index 2462fc0..0f3d89b 100644 --- a/scripts/qapi/common.py +++ b/scripts/qapi/common.py @@ -779,13 +779,6 @@ def check_union(expr, info): "enum '%s'" % (key, enum_define['enum'])) - # If discriminator is user-defined, ensure all values are covered - if enum_define: - for value in enum_define['data']: - if value not in members.keys(): - raise QAPISemError(info, "Union '%s' data missing '%s' branch" - % (name, value)) - def check_alternate(expr, info): name = expr['alternate'] @@ -1644,6 +1637,10 @@ class QAPISchema(object): if tag_name: variants = [self._make_variant(key, value) for (key, value) in data.items()] + # branches that are not explicitly covered get an empty type + variants += [self._make_variant(key, 'q_empty') + for key in discriminator_find_enum_define(expr)['data'] + if key not in data.keys()] members = [] else: variants = [self._make_simple_variant(key, value, info) diff --git a/scripts/qapi/types.py b/scripts/qapi/types.py index 64d9c0f..1fb2b6d 100644 --- a/scripts/qapi/types.py +++ b/scripts/qapi/types.py @@ -124,7 +124,9 @@ def gen_variants(variants): ''', c_name=c_name(variants.tag_member.name)) - for var in variants.variants: + # filter out the empty types + for var in filter(lambda var: var.type.name != 'q_empty', + variants.variants): ret += mcgen(''' %(c_type)s %(c_name)s; ''', diff --git a/scripts/qapi/visit.py b/scripts/qapi/visit.py index 5d72d89..96bd32c 100644 --- a/scripts/qapi/visit.py +++ b/scripts/qapi/visit.py @@ -81,14 +81,23 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) c_name=c_name(variants.tag_member.name)) for var in variants.variants: - ret += mcgen(''' + case_str = c_enum_const(variants.tag_member.type.name, + var.name, + variants.tag_member.type.prefix) + if var.type.name == 'q_empty': + # valid variant and nothing to do + ret += mcgen(''' + case %(case)s: + break; +''', + case=case_str) + else: + ret += mcgen(''' case %(case)s: visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, &err); break; ''', - case=c_enum_const(variants.tag_member.type.name, - var.name, - variants.tag_member.type.prefix), + case=case_str, c_type=var.type.c_name(), c_name=c_name(var.name)) ret += mcgen(''' diff --git a/tests/Makefile.include b/tests/Makefile.include index bb08e37..afdd0db 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -496,7 +496,6 @@ qapi-schema += flat-union-base-any.json qapi-schema += flat-union-base-union.json qapi-schema += flat-union-clash-member.json qapi-schema += flat-union-empty.json -qapi-schema += flat-union-incomplete-branch.json qapi-schema += flat-union-inline.json qapi-schema += flat-union-int-branch.json qapi-schema += flat-union-invalid-branch-key.json diff --git a/tests/qapi-schema/flat-union-incomplete-branch.err b/tests/qapi-schema/flat-union-incomplete-branch.err deleted file mode 100644 index e826bf0..0000000 --- a/tests/qapi-schema/flat-union-incomplete-branch.err +++ /dev/null @@ -1 +0,0 @@ -tests/qapi-schema/flat-union-incomplete-branch.json:6: Union 'TestUnion' data missing 'value2' branch diff --git a/tests/qapi-schema/flat-union-incomplete-branch.exit b/tests/qapi-schema/flat-union-incomplete-branch.exit deleted file mode 100644 index d00491f..0000000 --- a/tests/qapi-schema/flat-union-incomplete-branch.exit +++ /dev/null @@ -1 +0,0 @@ -1 diff --git a/tests/qapi-schema/flat-union-incomplete-branch.out b/tests/qapi-schema/flat-union-incomplete-branch.out deleted file mode 100644 index e69de29..0000000 diff --git a/tests/qapi-schema/qapi-schema-test.out b/tests/qapi-schema/qapi-schema-test.out index 542a19c..0dbcdaf 100644 --- a/tests/qapi-schema/qapi-schema-test.out +++ b/tests/qapi-schema/qapi-schema-test.out @@ -23,7 +23,7 @@ object UserDefOne base UserDefZero member string: str optional=False member enum1: EnumOne optional=True -enum EnumOne ['value1', 'value2', 'value3'] +enum EnumOne ['value1', 'value2', 'value3', 'value4'] object UserDefZero member integer: int optional=False object UserDefTwoDictDict @@ -52,6 +52,7 @@ object UserDefFlatUnion case value1: UserDefA case value2: UserDefB case value3: UserDefB + case value4: q_empty object UserDefUnionBase base UserDefZero member string: str optional=False