From patchwork Wed May 30 16:37:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Nefedov X-Patchwork-Id: 10439383 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 F03C160327 for ; Wed, 30 May 2018 16:39:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E033F291DA for ; Wed, 30 May 2018 16:39:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D3F4A291E7; Wed, 30 May 2018 16:39:24 +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 59268291DA for ; Wed, 30 May 2018 16:39:13 +0000 (UTC) Received: from localhost ([::1]:39692 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fO480-0003Up-Fm for patchwork-qemu-devel@patchwork.kernel.org; Wed, 30 May 2018 12:39:12 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40124) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fO46q-0002YW-Ph for qemu-devel@nongnu.org; Wed, 30 May 2018 12:38:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fO46m-0002ca-GJ for qemu-devel@nongnu.org; Wed, 30 May 2018 12:38:00 -0400 Received: from mail-ve1eur02on0717.outbound.protection.outlook.com ([2a01:111:f400:fe06::717]:49226 helo=EUR02-VE1-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 1fO46m-0002cC-1v for qemu-devel@nongnu.org; Wed, 30 May 2018 12:37:56 -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=odh4oNGFiawY8AX8XRRtw/isxSaGcMXGSbgcZTrN0cI=; b=GJi7hvWL0VCY/6pqAT1s3Jiy478+l/ZpGjBXorSEC9wQOf/i7kLxTklUI7HWJ1fNXFjBIb5yQwLnKOHK3DnFYTtrkL02I/PORmykAregxdKTs0jWCCDrF0hMrcZk4IF8VgW2RzoRhqYRN17eFskLNUHzYYI8S8jbynlmhTrMFSU= Received: from xantnef-ws.sw.ru (195.214.232.6) by AM0PR08MB3346.eurprd08.prod.outlook.com (2603:10a6:208:5f::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.820.11; Wed, 30 May 2018 16:37:53 +0000 From: Anton Nefedov To: qemu-devel@nongnu.org Date: Wed, 30 May 2018 19:37:31 +0300 Message-Id: <1527698252-86258-2-git-send-email-anton.nefedov@virtuozzo.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1527698252-86258-1-git-send-email-anton.nefedov@virtuozzo.com> References: <1527698252-86258-1-git-send-email-anton.nefedov@virtuozzo.com> MIME-Version: 1.0 X-Originating-IP: [195.214.232.6] X-ClientProxiedBy: HE1PR0402CA0045.eurprd04.prod.outlook.com (2603:10a6:7:7c::34) To AM0PR08MB3346.eurprd08.prod.outlook.com (2603:10a6:208:5f::19) X-MS-PublicTrafficType: Email X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(5600026)(4534165)(7168020)(4627221)(201703031133081)(201702281549075)(2017052603328)(7153060)(7193020); SRVR:AM0PR08MB3346; X-Microsoft-Exchange-Diagnostics: 1; AM0PR08MB3346; 3:uUGJI2JJzQfLvyf3HaRBOziQ8MH0oetkKlOLPXiMwvdx0Dob+5lVeHBpJP6W5fA7z36Ri6/sCwDwOaf3xthkAZXctN1Qhpwa3JtvPf2H0ZulMkWoCeoD+onWXHJ5wB/Tkg9KAMdesThPyh10shP8xoEHXrMkaxTwaw0W87/rBT4YUocO+fsTVCODqQpc2RYCfYPoCDAKn3RlZZzdN6w8KDg3bhv/oO5gJiuX+Wkm97hijAW8FXgIr2eVMIbar0/3; 25:YDyWIWwc5nqdVcrJwZufMgBY8Z03QXhcLUkvclhv9YyZTlGeh9IdEnPx4xooTLy5b3FF3TRitqiy/JifOQKRoIImhLlxuEWgnVY2HKLxqiZ2UFH4xj6ixxS1c2K5x9Vku/4RCqUZJ/uNUjHLUS7rwXs0LbXo3zyAZZnIx0QB4/PxaRPU//cYIgyFpVGSNkxbuhViCwWP50FPDurfdMqQP+sAPK4UICgMrBjUymdD61EeevVgMQ91qF8gJyCdEPXWQDT+Rk3hkj0QN4lxnHbWn34/cL4ffeS4CwU6iUQNmQZMdUlrMUGlGswYbx4VPxCwEecXIqFNeB1xrEqkk/jGXw==; 31:NYoc3pLmxwRV6j51RJGAA9qPAgJWWqV4fE11qjWAr3rZGjzP7CjXHLKObz24HPpKejUtFhsvNz+3iKOzb3GaKsvhHVHs98+lilwDctqorMv3wNzNcluEqVozcre1uVo3h7W4LadtRiYjEh6MY7RojBP1YYYBQov/xmiY/IrZzMBzQvC2DLG0F6ghhdLkFFwvwuTj0lehYtGZ2z5fTIVa7Ygv9A/9Psf6tekfY0FFKLs= X-MS-TrafficTypeDiagnostic: AM0PR08MB3346: Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=anton.nefedov@virtuozzo.com; X-Microsoft-Exchange-Diagnostics: 1; AM0PR08MB3346; 20:NeHIdSPxAVwJBJgY/SLRodhj7+YQ7KBt+RSvXFh47telui6Nj3LWFa5I02e+dK90E/+CRntwtCNS5AjUC8e0rTMMArkYdPG9rGUxgpH1LZDfh4LD30sFCEMhYCLfp1bIKG2lxLx6O+RPYwNAk0o6nHgBMh+1PqTI2c40AmXfNMzh0UXqsiE3tcT1jk5ip+Baq1FbsC6Q2VQtsI+LZ3JCYMyc/1c5YH0tDXs5JowOIBEKf2nJf2gEt9CF/UtvnAQnl/4ZGEWESf+k36+jwL4Ct+IN7V1/BpUfPEiHvWGB0GUNvXZsOpP0y4Qdc6mIxdVGjOx65T3FJeG4As17zttAZbqRPFi8iw6NmbXzLzkLf9QnBWnQrlLNQJhwg9CK0Xspal429r/3p8TnW9mD0QbpGT8mBMVLbi+8wMTzMYMFDynKBdz5ayLrGuCvOZ2Bq6IpuHrnFwhCrMTr8X4GLjvrVWAeCgt51ulqnGHAuel8DqXzblxZvEG7Lj/hSa+5CNq/; 4:k32GnhtslO4WC66Pywz9so6geGihRyNNLUIVsYh5EBOg1b4qYJVHVprFc8sF5SgAwQMGzXQeSRig3FO+Rf3XS//SodTIyhqAK4AWmBGJWU4xsNR+tUb76Qp46ajf3JKBQhFs9KdStTbGvcNNc7Vgt+fVy9gEvMCHRTBu9fiHMQdATXAHttqpIsGMXX2nVRerdi/JQ6No+POm7n5VHD6oBcpE1mnLICyePQ8uVTwFywTSxp8UtwijufL84MhKzvv/rOqzXLQwcAELLK9myAkswA== 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)(8121501046)(5005006)(93006095)(93001095)(3002001)(10201501046)(3231254)(944501410)(52105095)(149027)(150027)(6041310)(20161123564045)(20161123562045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123560045)(20161123558120)(6072148)(201708071742011)(7699016); SRVR:AM0PR08MB3346; BCL:0; PCL:0; RULEID:; SRVR:AM0PR08MB3346; X-Forefront-PRVS: 0688BF9B46 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(979002)(366004)(396003)(346002)(39850400004)(376002)(39380400002)(199004)(189003)(68736007)(478600001)(53416004)(3846002)(106356001)(316002)(105586002)(6116002)(305945005)(6666003)(186003)(6486002)(16526019)(7736002)(6916009)(107886003)(53936002)(97736004)(5660300001)(8676002)(26005)(81156014)(8936002)(81166006)(86362001)(4326008)(50226002)(66066001)(575784001)(36756003)(44832011)(52116002)(6512007)(48376002)(6506007)(386003)(59450400001)(486006)(2906002)(69596002)(956004)(11346002)(2361001)(2351001)(47776003)(51416003)(476003)(25786009)(55236004)(50466002)(16586007)(2616005)(446003)(76176011)(969003)(989001)(999001)(1009001)(1019001); DIR:OUT; SFP:1102; SCL:1; SRVR:AM0PR08MB3346; 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; AM0PR08MB3346; 23:yHqYXVS+w0ZFOnIACarvWLfPgc402d7vfOItSmbMa?= =?us-ascii?Q?NKsP7gjv3b/52OO4l+xlELmfBNnobQutLioMWKBnEx3gtzj0eoCjEPK51U3T?= =?us-ascii?Q?vm8UxLiDOKUAxukD3OtYymM7epHXijHEdAZ5UMQuZlCGogzgXMBeXinIxu5l?= =?us-ascii?Q?xKXoD3LzlxLzsjVMwrgAtFHghQrZX7+C0mwdY0JE29DhSFroKVgbNl7QpUDq?= =?us-ascii?Q?up5/c1LyQGPYbpJsOlEiHnXNNdYRsnyBbkjNAy7dHezxHHFMNC1mP1fWLl/3?= =?us-ascii?Q?LlOEMO3/RGMjSleD/W4h6Ky4XB9VaUKauG0AaXWUxTgUEo/kNSuwUyMVMJuy?= =?us-ascii?Q?rw+OzuwoLfQUyQ8j5ydVXgzNKmcD34qQbVdoxaj8Tr8bBIpKMFtUCb3gVYUS?= =?us-ascii?Q?RxrrSSQPdUFu9yhXkpWfqQIdY0S7enpUvYpvSL7uPA0ByZvmTaT23nrJHdxv?= =?us-ascii?Q?33wX0nMfmX21DpukVJgiajOvM3/8FtB8wHrc6pPEzbVoa0wiBdm5W4X/RjB8?= =?us-ascii?Q?6CvKPcZ3sjYi1k7LeTIv3Id0WEo8HVpxs2Uej5PSLbR+iOEz1GDDRt05/X+F?= =?us-ascii?Q?+6DTiZGxU0xlb/EFhTsqF4e8cFki6mR/BjHqZPoOAjdPVYBXCWf6yGMpAHOk?= =?us-ascii?Q?Kq2mWMoK8svFTATD82Sj2cVGyQHbB9qAY++Q8Igzi+BbH5ahAVGzgUI7XrLD?= =?us-ascii?Q?XXulR+E4lf87LBti3972gKxrUOCxbVAEozbnVN3dEd52zla68FGsI4hIl8bh?= =?us-ascii?Q?zvOe6PxeeDpNq8qO/S18pYg/iRwmn84eO16JOUvkT99b+0GX6VnYT3USC1t3?= =?us-ascii?Q?iJCMI+MY8RBIwCCV8BRpgLzUwsHoA/IziaBfqD3Ceo85EyxZOtVL1oslWL3d?= =?us-ascii?Q?NtfHi9GrO8YD/oc/Hmqj96PbZaufhobdaMC0QQ1xYlUycRbXzZ02E3SLvFqm?= =?us-ascii?Q?qjxviAwpZ4z3MYCNhfAkXwV6LchJbHWUlPqjiDPr29g/MyRjJl0hGXOXbdL+?= =?us-ascii?Q?Hxi427qUVIymUqULHjostnQpCETvLJP4yl89YfkJbdyBmaWL1CACYptcktor?= =?us-ascii?Q?N7WqDgLatspHJ4COUBsXZnc1RBtH7AxnJCtpJP0D0SwSdzs4RqTljqsS8D2z?= =?us-ascii?Q?O0qqjcts64yHzNM6VMKLqIkHC/+lvJpaZPcOoqDjQpFgylkO6FatjFZI1n+W?= =?us-ascii?Q?ZfjpDRlo3uHyl/pehde2RpZYUJJhTJqHPukVyYrIdIsUar8eTDO0KdjAOspt?= =?us-ascii?Q?lZ3XRh/OPvFX9VnnfJ2MUF8/aNi8m8/Z7bGNjSqJzly+R46zT5+MAvE9ATDX?= =?us-ascii?Q?MXzQumnO3eRQHg/LYY++TraUJKawFiznzi3FMOVYIHA/JyxmkrwXwMlQXnof?= =?us-ascii?Q?xgjQHJtBLEJC2K0sV9ZFzlNTS2feijoUPlWE85ymZwioRFepw5d8alPseHoO?= =?us-ascii?Q?Fz8zC7e36/tk5oHqarOqenrgMQqRJD4V+uwE7+NRIKrr0mBcurb?= X-Microsoft-Antispam-Message-Info: KBuHWGGWHfPJAqn8ujiitmBA8eDJCZkn6/COG96Aof0PTCqHrUxQAngakapVM309UdRn5DzIF1USua0RHf+MdjMhIu09RNoUK53RcRZgn9Gm+TyJ5o0pl/kW8JEcKR328Z5tvSJJTpTt6tp2EYHKUC3WeVIzUW0tSf7+dYwZhojwaNSiP0ZUmvZANW4rdzVn X-Microsoft-Exchange-Diagnostics: 1; AM0PR08MB3346; 6:cxB88S+xzSis9yBp1HYgAd9u1nIMjQd4cYYlrJA9GRONlUE+JlWAMqZ+FMV66lubYlofONDObxgtS5h+4CphBPX0vSx/5s61Ks9/AIXrwW89msv4IPDE0HrT2oSd2z3nFL0ysvEzIlYhPf7AYlvmPYxhzVXm80iAZuijQnVn07X5u/2XhL7Pz3qFqbxW0egby+XsLJ7UwRaIrYfLna+hFnDbKDHK4BqY3WN8OFZmk3uxjS2l78rfQ3WyoNIvfIys7InZG6GeDZIedcLzxAzQHzSURar1DnG+sS9C2zVx6ZuRJgT4ACzbvoKFACOmqsPWScS6UDjvS+uJSmtaFMUWO27+boi0v5Z2adiOAE4bpgv9ChExJo+xu6ka6e7enjikHO27c6MZTKZ6audYT9GmILuN9ZrQGJ7M3hI570M78ze6K7Umb726qZcOMo45G/xLHyD2nBYWaUcHMexgulOANw==; 5:DlIDs1qyvsqUbjH2EPxy+iVxnc9dX6gVRqfZpeztQNeQYdfA3dDmAke96tuJIO/uUyKkTvhDikdNG0YOt7Oa5nJo5hEg+wAXfa264maQYlfeAMHs89k3uItppE4r3UtieJ5ECiZyXbPpkNMkuh8yiPry6KhvEIVnAwkiQiamzdk=; 24:ew6XiGVSNDmyW5aMMfct773sMjJ/1mkma9wj40AufRhFyHYQSvOeXkguYFK66YRi7VEBgod38rnUr26e3oAvyfPRMC6Tp1g44wvo3Tmqo8E= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; AM0PR08MB3346; 7:SrjWBw0gLTVfJE8FXO2hwUGdQwu8xARRUTMrpZVK+RpFjuQfFmCvb1wjyewfzHm+jiV+iZsL0I1y5WlQfO5+yGneHpKYV51sl3wKq4r0foyYTAHGfQBX+UC2EicFLWobe7x2byALfKZTgSGQJBO1AcUnv28fme+TZYrE5a6flvjbHq12UXI+7Yxab0pdGjOHfIFMnhzMCwwchEpIYEM5YW/cGJiYeh1mqw46ImgGQ9K3Mb0yqrGotr48Mu9hoMJz; 20:ot2HEH/ve9UWh+Dozb/oU89p1gWGRF3LtRH/+lKwP7Sp8LztXJgKmw6/cJmvUoupeOGQhzPjYYN+anIpHlctmD3Ov8wrE/HJc0mElBHjQAPyDw0m3pLG1EI0zgQPMgs+O8/gQKtcpZRwXca12ZCOHEFW2S7GKujlMwmUFYhCu/E= X-MS-Office365-Filtering-Correlation-Id: fa3f09df-ad54-47ab-db85-08d5c64bb145 X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 May 2018 16:37:53.7566 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fa3f09df-ad54-47ab-db85-08d5c64bb145 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR08MB3346 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 2a01:111:f400:fe06::717 Subject: [Qemu-devel] [PATCH v3 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 b9b6eab..3dfedd5 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 06e30f4..7ceb409 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 a032cec..df6190a 100644 --- a/scripts/qapi/common.py +++ b/scripts/qapi/common.py @@ -782,13 +782,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 b499ba1..1f1e0ea 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -494,7 +494,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 467577d..9dc9532 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