From patchwork Mon Jun 18 08:40:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Nefedov X-Patchwork-Id: 10470221 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 A738B600CC for ; Mon, 18 Jun 2018 08:44:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 95AA6287E7 for ; Mon, 18 Jun 2018 08:44:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8A27828863; Mon, 18 Jun 2018 08:44:16 +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 BAFA8287E7 for ; Mon, 18 Jun 2018 08:44:15 +0000 (UTC) Received: from localhost ([::1]:33477 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fUplm-0007OB-V0 for patchwork-qemu-devel@patchwork.kernel.org; Mon, 18 Jun 2018 04:44:14 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59759) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fUpiA-0005ZX-Q6 for qemu-devel@nongnu.org; Mon, 18 Jun 2018 04:40:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fUpi8-0000JZ-9d for qemu-devel@nongnu.org; Mon, 18 Jun 2018 04:40:30 -0400 Received: from mail-db5eur01on0097.outbound.protection.outlook.com ([104.47.2.97]:43712 helo=EUR01-DB5-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 1fUpi7-0000FC-Qq for qemu-devel@nongnu.org; Mon, 18 Jun 2018 04:40:28 -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=ZZ4cSfo97dZGW+0wYaoKI/OXzsUUIDVavN3tLKUtVFE=; b=iB0FINgWZCGXzNizjBqTe7nMungZQVwc4M1nTuMAp7phoAg497oP7tBQ22kg7UlkuRLMJFjY6/m9LQ7tsZLeoNfW3gMfkXjfgAHIYRUlh0tTXB/Sx1rq6ROmxq2vxWaQ8MyqwGad+QSiieIuU7CfAYXqlkIInMfsxRJxkZ0GujI= Received: from xantnef-ws.sw.ru (195.214.232.6) by AM6PR08MB3351.eurprd08.prod.outlook.com (2603:10a6:209:48::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.863.19; Mon, 18 Jun 2018 08:40:22 +0000 From: Anton Nefedov To: qemu-devel@nongnu.org Date: Mon, 18 Jun 2018 11:40:05 +0300 Message-Id: <1529311206-76847-2-git-send-email-anton.nefedov@virtuozzo.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1529311206-76847-1-git-send-email-anton.nefedov@virtuozzo.com> References: <1529311206-76847-1-git-send-email-anton.nefedov@virtuozzo.com> MIME-Version: 1.0 X-Originating-IP: [195.214.232.6] X-ClientProxiedBy: HE1PR0802CA0006.eurprd08.prod.outlook.com (2603:10a6:3:bd::16) To AM6PR08MB3351.eurprd08.prod.outlook.com (2603:10a6:209:48::20) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7fce3e7c-c1fa-4494-7ceb-08d5d4f721ea X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(4534165)(7168020)(4627221)(201703031133081)(201702281549075)(5600026)(711020)(2017052603328)(7153060)(7193020); SRVR:AM6PR08MB3351; X-Microsoft-Exchange-Diagnostics: 1; AM6PR08MB3351; 3:v+DtDIQMFysAWk2utUPf6CMa8OPQ0RO20P6IEWaKANRarwjDRuI41AZ7kIILgxnqL3FVwF31kXN2H78rT70B8q1mF44IqzsVpq75OQeawV+sWcaEwXP2f0c2f0vWDO5/99HZfys6ZCZ6BAaYwJjvACSYstvIUOCZk0gxPIXyC6xH3JBn1a9xAp9UZSL2FODIbcVU/11P9sAMZcE5pmCk9zNkfmv74MHiyrRfZHfwDiG2hRHY5Gep83a5sZOA/iEN; 25:vqjbGyX07flK0v2AwJH3JM9ZhHvHiFWbicZ4Rnsu3k95bQxbQtXG7moEBJZ/q7I50CIHi2I5ohiwwJvudgo9Y0NFXEUAsdqT3nkPljuFlaLrMXet/QQtD7/kxgVdkS6VkbGGvwfOmtvVqnNsQuWxh3vdUL3Fnt9MeY9NRP17JH5KdNIgaPwlAeKLETxrQyIGupbr1QA3C82c9E6HYTHUB0gLMr3oIbqh5Pf8ZsLujAuZPzr9bB/QTwkUFK96nwkWj7FLjAHBRxzsieqyJip8eu+jQncpYpjgWs+h10oqIYmGi4MtMip4kBvTqe4TSzQmD/sSZiR3qbtsul500k1Evw==; 31:9nWINJX0oeTpkXh/fZ8rFyZ4vw5dJjbomVguiZTRJXTQ5Rds72OmAi7nCDT8H9UDtItOYd1J43ulX/XbU53L83wtwVICAcV2pi67t0sbtPwB3pt9KKIxXlCLul/GxE8q0kiP/asBneoougDLs0jb+iTuzUDbHakOsr7kdT3+9R/NIzTsHe0srb9MW7Hm6Fp0mN+idUvSSyTYbOtaBKzFqLWOyf+7CLAUhajjQ1WvJMI= X-MS-TrafficTypeDiagnostic: AM6PR08MB3351: Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=anton.nefedov@virtuozzo.com; X-Microsoft-Exchange-Diagnostics: 1; AM6PR08MB3351; 20:XuGuilu7YVOhGfc9vyeeUNiivypZ5kMfDpfMkukK/j6GibYhcVVxJMJd8wwtO6ttVP5mswRwtGBX6Tb0TLM660iI3m1XjQ+qYPjSMmxmBF5txpncq5iZbUL2U5Sjzl04M6dOHMgaW62Bjnx/O77yAGZjrjrwYfX9F33XnYihaqaGhST7Ry1FqeaK1b4eFZcqP7cA6bgUcFRMSbPpe8G/Ucl2eev5aApX6hvZxn/fiwIARajOQNLFhrp0LAA4dnEKR5kg67Ypu5VqtUs7rmosi9ouGJpMkiSf8O0aqhDYd0v6tLTvS18cfKMEsImm+cR0AzsbQYX1XO/12QYWdHNLkcl5hdZGhcbWaS9fMf9XsjuugBp8rBAYQ10YaolfF1syBPovCVDjTdkiscPfRpyV8aCSdQCKKbf3LW0X7DhNVWGHsovrrpfFTpg/PqD0vtfW5Rkkem+LIVOfMi56tBCiIfyhqlo3lACxxlYE9QBha2Jrbg47vFgteXyPVxaVG9TU; 4:4XT4h2+9lnHEYQLOEN352quDu2Ur/9mg03MsJoE6gy/0351q7pCoKC0hlPhPODEY9ziKo+UXEH01tl1mwSSBlvzIknvyJGWT+cJM8CiklGjtLE5bMFm/+aQ+aWrbb5/gMJkL/xvczsvm+RHECO9qvEQk/ib5/c+FlnhXwDrtJIAHx1olx0tPSTLPBDhdFfPA+m66X04c9siKsjDNCGUc0u04iIOXl6g8o6hRS2IB3vIfC/RA7znWkSNUjlMlYubHqud+wv9zxZhb7Zf1uNtNLA== 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)(10201501046)(3002001)(93006095)(93001095)(3231254)(944501410)(52105095)(149027)(150027)(6041310)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123560045)(20161123562045)(20161123558120)(20161123564045)(6072148)(201708071742011)(7699016); SRVR:AM6PR08MB3351; BCL:0; PCL:0; RULEID:; SRVR:AM6PR08MB3351; X-Forefront-PRVS: 0707248B64 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(366004)(39840400004)(376002)(346002)(396003)(39380400002)(189003)(199004)(16526019)(316002)(106356001)(11346002)(48376002)(956004)(2616005)(6512007)(2351001)(476003)(53416004)(8936002)(6486002)(4326008)(305945005)(97736004)(186003)(86362001)(26005)(25786009)(50226002)(575784001)(16586007)(69596002)(6116002)(3846002)(2906002)(55236004)(6666003)(47776003)(6506007)(105586002)(386003)(81166006)(66066001)(478600001)(107886003)(81156014)(44832011)(51416003)(50466002)(53936002)(6916009)(486006)(59450400001)(68736007)(8676002)(5660300001)(446003)(2361001)(36756003)(76176011)(52116002)(7736002); DIR:OUT; SFP:1102; SCL:1; SRVR:AM6PR08MB3351; H:xantnef-ws.sw.ru; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; Received-SPF: None (protection.outlook.com: virtuozzo.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; AM6PR08MB3351; 23:idGSZSLfOWl3UAQOV4vfIK84jBm6TXI5NWnNeyUWI?= =?us-ascii?Q?80Ych6cjnubzNi/wVPBEpns6jUCwi9fpFK0Q/AnY8ZSTteqMuNUV+nTPKHkr?= =?us-ascii?Q?j0MYCDkXIBKV7jhh/wuAC65lDJGiyulOvP2Fm+Jx/2vZzoX1AmCUEkegnJ1z?= =?us-ascii?Q?oRpwrgsJLf+I4mKFxqciqvWQf7/qpS12UtUDHvNMXdpUkhe7EoHVW2qLYHdj?= =?us-ascii?Q?t9ga5NsgVLTKk/15OLk1cZSsiQu5EmkwUffFtVgxeGWbTXJDL1D+U9ni02u6?= =?us-ascii?Q?Sj0ZhhfVHtehw6VIWJHU5DnvH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ts9lxe5a471D8rxKoNwf7aBBRRxmpJoI2WIErscK+RECJ78GIr6E6d5PdDv7Q==; 5:0TogJ68+gonkT+o7SeAb04l+Z5rJzIsmbFsU2D1QvmVAAntrM0/CbCLNcznYzuoHeS5vynk6ukT6y60z558SM1o4BCqRQ3QkdrtI22aUlLmpCrOpwUfSAIusIg1J293NOXOtwJrnhjwKW7bauR3y4Jgx0d2zN0zGhWNZ9mNPtR8=; 24:kRs6rbaVxaWC3qUDJtTOEJww0OLv31nYcy7sT5Gt5I0Ydc3epcFHjWRZ7rcUy0C0qGLSUvOcvBOdkWqleRDdQbcrGT9NjY2zDwjK5yFvUK8= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; AM6PR08MB3351; 7:c1GUEWSpUC82O2fIMQdC/rcqnqEMPUuRd1PGKMwIZes0PzJai/a5wu7oTmx4M9HrAS91baHOBuOXFKWN50HQ/briv09oWgL1RZp7SJxvpV17AmvWKbdFfvq1+q3GW00gxJI8DQTbhP+h4toHK36lCEURU5k4gNQ79dsSmMq4lvgcX6+RJDXslOvUKCQKnFMudUi5GJbgAoXQvyJWOtmcU+jDQzHnh0oGnFS5igLUbFrefnMin8AoHrn6EzOiAdZY; 20:SBwUcMBn3/hZPU3JaAxzVKmHFugIjTuknF8HoclbY5+qZdD6TDvo45/WBKMEl5Cdjo5cGjaoljBClk9EsSFJ9FuQEFmgUkqMVW7f8oXpESNkKAvW6McnE4mmOixBxC8SmK4Gq4j5jzaMYQJApnehtThX0KEG+OTSzixL2uRpqOQ= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Jun 2018 08:40:22.9896 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7fce3e7c-c1fa-4494-7ceb-08d5d4f721ea X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR08MB3351 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.2.97 Subject: [Qemu-devel] [PATCH v5 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: Markus Armbruster --- docs/devel/qapi-code-gen.txt | 8 +++++--- tests/qapi-schema/flat-union-incomplete-branch.json | 9 --------- tests/qapi-schema/qapi-schema-test.json | 6 ++++-- scripts/qapi/common.py | 15 ++++++++------- scripts/qapi/types.py | 2 ++ scripts/qapi/visit.py | 19 ++++++++++++++----- 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, 35 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..7b59817 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,9 @@ 'discriminator': 'enum1', 'data': { 'value1' : 'UserDefA', 'value2' : 'UserDefB', - 'value3' : 'UserDefB' } } + 'value3' : 'UserDefB' + # 'value4' defaults to empty + } } { 'struct': 'UserDefUnionBase', 'base': 'UserDefZero', diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py index 2462fc0..4b53f08 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'] @@ -1357,6 +1350,14 @@ class QAPISchemaObjectTypeVariants(object): self.tag_member = seen[c_name(self._tag_name)] assert self._tag_name == self.tag_member.name assert isinstance(self.tag_member.type, QAPISchemaEnumType) + if self._tag_name: # flat union + # branches that are not explicitly covered get an empty type + cases = set([v.name for v in self.variants]) + for val in self.tag_member.type.values: + if val.name not in cases: + v = QAPISchemaObjectTypeVariant(val.name, 'q_empty') + v.set_owner(self.tag_member.owner) + self.variants.append(v) for v in self.variants: v.check(schema) # Union names must match enum values; alternate names are diff --git a/scripts/qapi/types.py b/scripts/qapi/types.py index 64d9c0f..a599352 100644 --- a/scripts/qapi/types.py +++ b/scripts/qapi/types.py @@ -125,6 +125,8 @@ def gen_variants(variants): c_name=c_name(variants.tag_member.name)) for var in variants.variants: + if var.type.name == 'q_empty': + continue ret += mcgen(''' %(c_type)s %(c_name)s; ''', diff --git a/scripts/qapi/visit.py b/scripts/qapi/visit.py index 5d72d89..60ae8cf 100644 --- a/scripts/qapi/visit.py +++ b/scripts/qapi/visit.py @@ -81,15 +81,24 @@ 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), - c_type=var.type.c_name(), c_name=c_name(var.name)) + case=case_str, + c_type=var.type.c_name(), c_name=c_name(var.name)) ret += mcgen(''' default: diff --git a/tests/Makefile.include b/tests/Makefile.include index ca91da2..114aab7 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -498,7 +498,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