From patchwork Tue Jul 19 18:41:47 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Armbruster X-Patchwork-Id: 650434 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rvB2x3bqFz9sR8 for ; Wed, 20 Jul 2016 06:09:29 +1000 (AEST) Received: from localhost ([::1]:58444 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bPbKZ-0000wk-7p for incoming@patchwork.ozlabs.org; Tue, 19 Jul 2016 16:09:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44084) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bPZy2-0002VS-3V for qemu-devel@nongnu.org; Tue, 19 Jul 2016 14:42:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bPZxz-0004EU-0i for qemu-devel@nongnu.org; Tue, 19 Jul 2016 14:42:05 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57827) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bPZxy-0004EQ-P5 for qemu-devel@nongnu.org; Tue, 19 Jul 2016 14:42:02 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5B92F63E24 for ; Tue, 19 Jul 2016 18:42:02 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-35.ams2.redhat.com [10.36.116.35]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u6JIfxIE022560 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Tue, 19 Jul 2016 14:42:00 -0400 Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id D119611AD294; Tue, 19 Jul 2016 20:41:58 +0200 (CEST) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Tue, 19 Jul 2016 20:41:47 +0200 Message-Id: <1468953718-27661-5-git-send-email-armbru@redhat.com> In-Reply-To: <1468953718-27661-1-git-send-email-armbru@redhat.com> References: <1468953718-27661-1-git-send-email-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 19 Jul 2016 18:42:02 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL v2 for-2.7 04/15] qapi: Require all branches of flat union enum to be covered 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: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Eric Blake We were previously enforcing that all flat union branches were found in the corresponding enum, but not that all enum values were covered by branches. The resulting generated code would abort() if the user passes the uncovered enum value. We don't automatically treat non-present branches in a flat union as empty types, for symmetry with simple unions (there, the enum type is generated from the list of all branches, so there is no way to omit a branch but still have it be part of the union). A later patch will add shorthand so that branches that are empty in flat unions can be declared as 'branch':{} instead of 'branch':'Empty', to avoid the need for an otherwise useless explicit empty type. [Such shorthand for simple unions is a bit harder to justify, since we would still have to generate a wrapper type that parses 'data':{}, rather than truly being an empty branch with no additional siblings to the 'type' member.] Signed-off-by: Eric Blake Message-Id: <1468468228-27827-3-git-send-email-eblake@redhat.com> Reviewed-by: Markus Armbruster Signed-off-by: Markus Armbruster --- scripts/qapi.py | 8 ++++++++ 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.json | 9 +++++++++ tests/qapi-schema/flat-union-incomplete-branch.out | 0 6 files changed, 20 insertions(+) create mode 100644 tests/qapi-schema/flat-union-incomplete-branch.err create mode 100644 tests/qapi-schema/flat-union-incomplete-branch.exit create mode 100644 tests/qapi-schema/flat-union-incomplete-branch.json create mode 100644 tests/qapi-schema/flat-union-incomplete-branch.out diff --git a/tests/qapi-schema/flat-union-incomplete-branch.out b/tests/qapi-schema/flat-union-incomplete-branch.out new file mode 100644 index 0000000..e69de29 diff --git a/scripts/qapi.py b/scripts/qapi.py index b13ae47..0635bbb 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -612,6 +612,14 @@ def check_union(expr, expr_info): "enum '%s'" % (key, enum_define["enum_name"])) + # If discriminator is user-defined, ensure all values are covered + if enum_define: + for value in enum_define['enum_values']: + if value not in members.keys(): + raise QAPIExprError(expr_info, + "Union '%s' data missing '%s' branch" + % (name, value)) + def check_alternate(expr, expr_info): name = expr['alternate'] diff --git a/tests/Makefile.include b/tests/Makefile.include index 2010b11..a04c199 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -326,6 +326,7 @@ 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 new file mode 100644 index 0000000..e826bf0 --- /dev/null +++ b/tests/qapi-schema/flat-union-incomplete-branch.err @@ -0,0 +1 @@ +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 new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/tests/qapi-schema/flat-union-incomplete-branch.exit @@ -0,0 +1 @@ +1 diff --git a/tests/qapi-schema/flat-union-incomplete-branch.json b/tests/qapi-schema/flat-union-incomplete-branch.json new file mode 100644 index 0000000..25a411b --- /dev/null +++ b/tests/qapi-schema/flat-union-incomplete-branch.json @@ -0,0 +1,9 @@ +# 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' } }