From patchwork Mon Sep 21 21:57:29 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 520622 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 492C714012C for ; Tue, 22 Sep 2015 08:12:13 +1000 (AEST) Received: from localhost ([::1]:34295 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ze9Jj-00051O-F0 for incoming@patchwork.ozlabs.org; Mon, 21 Sep 2015 18:12:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60427) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ze96I-0007e4-JS for qemu-devel@nongnu.org; Mon, 21 Sep 2015 17:58:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Ze96G-0000qP-0l for qemu-devel@nongnu.org; Mon, 21 Sep 2015 17:58:18 -0400 Received: from mx1.redhat.com ([209.132.183.28]:56528) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ze96F-0000qC-OC for qemu-devel@nongnu.org; Mon, 21 Sep 2015 17:58:15 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id 604F88E697; Mon, 21 Sep 2015 21:58:15 +0000 (UTC) Received: from red.redhat.com (ovpn-113-166.phx2.redhat.com [10.3.113.166]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t8LLw4bZ027229; Mon, 21 Sep 2015 17:58:14 -0400 From: Eric Blake To: qemu-devel@nongnu.org Date: Mon, 21 Sep 2015 15:57:29 -0600 Message-Id: <1442872682-6523-14-git-send-email-eblake@redhat.com> In-Reply-To: <1442872682-6523-1-git-send-email-eblake@redhat.com> References: <1442872682-6523-1-git-send-email-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Michael Roth , marcandre.lureau@redhat.com, DirtY.iCE.hu@gmail.com, armbru@redhat.com, ehabkost@redhat.com Subject: [Qemu-devel] [PATCH v5 13/46] qapi: Track owner of each object member X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 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-bounces+incoming=patchwork.ozlabs.org@nongnu.org Future commits will migrate semantic checking away from parsing and over to the various QAPISchema*.check() methods. But to report an error message about an incorrect semantic use of a member of an object type, we need to know which type, command, or event owns the member. Rather than making all the check() methods have to pass around additional information, it is easier to have each member track who owns it in the first place. The source information is intended for human consumption in error messages, and a new describe() method is added to access the resulting information. For example, given the qapi: { 'command': 'foo', 'data': { 'string': 'str' } } an implementation of visit_command() that calls arg_type.members[0].describe() will see "'string' (member of foo arguments)". I intentionally chose the terminology "member" for struct fields, and "branch" for variants; this is because we have two different types of collisions that future patches will detect: duplicated keys in the QMP wire format (members common to a base type and its descendent), and collisions in the generated C struct (a union branch colliding with a common member, even though the QMP wire code never passes the branch name as a key). Where implicit types are involved, the code intentionally tries to pick the name of the owner of that implicit type, rather than the type name itself (a user reading the error message should be able to grep for the problem in their original file, but will not be able to locate a generated implicit name). For simple unions, I chose not to pass the union name to the variants constructor; but this should be okay because in practice nothing should ever conflict with the implicit 'type' of a simple union. Signed-off-by: Eric Blake --- scripts/qapi.py | 54 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/scripts/qapi.py b/scripts/qapi.py index e982970..6bc13f1 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -986,14 +986,16 @@ class QAPISchemaObjectType(QAPISchemaType): class QAPISchemaObjectTypeMember(object): - def __init__(self, name, typ, optional): + def __init__(self, name, typ, optional, owner): assert isinstance(name, str) assert isinstance(typ, str) assert isinstance(optional, bool) + assert isinstance(owner, str) and owner[0] != ':' self.name = name self._type_name = typ self.type = None self.optional = optional + self._owner = owner def check(self, schema, all_members, seen): assert self.name not in seen @@ -1002,6 +1004,9 @@ class QAPISchemaObjectTypeMember(object): all_members.append(self) seen[self.name] = self + def describe(self): + return "'%s' (member of %s)" % (self.name, self._owner) + class QAPISchemaObjectTypeVariants(object): def __init__(self, tag_name, tag_enum, variants): @@ -1015,7 +1020,8 @@ class QAPISchemaObjectTypeVariants(object): self.tag_member = None else: self.tag_member = QAPISchemaObjectTypeMember('type', tag_enum, - False) + False, + '(implicit)') self.variants = variants def check(self, schema, members, seen): @@ -1030,13 +1036,16 @@ class QAPISchemaObjectTypeVariants(object): class QAPISchemaObjectTypeVariant(QAPISchemaObjectTypeMember): - def __init__(self, name, typ): - QAPISchemaObjectTypeMember.__init__(self, name, typ, False) + def __init__(self, name, typ, owner): + QAPISchemaObjectTypeMember.__init__(self, name, typ, False, owner) def check(self, schema, tag_type, seen): QAPISchemaObjectTypeMember.check(self, schema, [], seen) assert self.name in tag_type.values + def describe(self): + return "'%s' (branch of %s)'" % (self.name, self._owner) + # This function exists to support ugly simple union special cases # TODO get rid of them, and drop the function def simple_union_type(self): @@ -1186,7 +1195,7 @@ class QAPISchema(object): self._def_entity(QAPISchemaEnumType(name, info, data, prefix)) self._make_array_type(name) # TODO really needed? - def _make_member(self, name, typ): + def _make_member(self, name, typ, owner): optional = False if name.startswith('*'): name = name[1:] @@ -1194,10 +1203,10 @@ class QAPISchema(object): if isinstance(typ, list): assert len(typ) == 1 typ = self._make_array_type(typ[0]) - return QAPISchemaObjectTypeMember(name, typ, optional) + return QAPISchemaObjectTypeMember(name, typ, optional, owner) - def _make_members(self, data): - return [self._make_member(key, value) + def _make_members(self, data, owner): + return [self._make_member(key, value, owner) for (key, value) in data.iteritems()] def _def_struct_type(self, expr, info): @@ -1205,20 +1214,21 @@ class QAPISchema(object): base = expr.get('base') data = expr['data'] self._def_entity(QAPISchemaObjectType(name, info, base, - self._make_members(data), + self._make_members(data, name), None)) self._make_array_type(name) # TODO really needed? - def _make_variant(self, case, typ): - return QAPISchemaObjectTypeVariant(case, typ) + def _make_variant(self, case, typ, owner): + return QAPISchemaObjectTypeVariant(case, typ, owner) - def _make_simple_variant(self, info, case, typ): + def _make_simple_variant(self, info, case, typ, owner): if isinstance(typ, list): assert len(typ) == 1 typ = self._make_array_type(typ[0]) typ = self._make_implicit_object_type(typ, info, 'wrapper', - [self._make_member('data', typ)]) - return QAPISchemaObjectTypeVariant(case, typ) + [self._make_member('data', typ, + owner)]) + return QAPISchemaObjectTypeVariant(case, typ, owner) def _make_tag_enum(self, type_name, variants): return self._make_implicit_enum_type(type_name, @@ -1231,15 +1241,15 @@ class QAPISchema(object): tag_name = expr.get('discriminator') tag_enum = None if tag_name: - variants = [self._make_variant(key, value) + variants = [self._make_variant(key, value, name) for (key, value) in data.iteritems()] else: - variants = [self._make_simple_variant(info, key, value) + variants = [self._make_simple_variant(info, key, value, name) for (key, value) in data.iteritems()] tag_enum = self._make_tag_enum(name, variants) self._def_entity( QAPISchemaObjectType(name, info, base, - self._make_members(OrderedDict()), + self._make_members(OrderedDict(), name), QAPISchemaObjectTypeVariants(tag_name, tag_enum, variants))) @@ -1248,7 +1258,7 @@ class QAPISchema(object): def _def_alternate_type(self, expr, info): name = expr['alternate'] data = expr['data'] - variants = [self._make_variant(key, value) + variants = [self._make_variant(key, value, name) for (key, value) in data.iteritems()] tag_enum = self._make_tag_enum(name, variants) self._def_entity( @@ -1265,8 +1275,10 @@ class QAPISchema(object): gen = expr.get('gen', True) success_response = expr.get('success-response', True) if isinstance(data, OrderedDict): + owner = name + ' arguments' data = self._make_implicit_object_type(name, info, 'arg', - self._make_members(data)) + self._make_members(data, + owner)) if isinstance(rets, list): assert len(rets) == 1 rets = self._make_array_type(rets[0]) @@ -1277,8 +1289,10 @@ class QAPISchema(object): name = expr['event'] data = expr.get('data') if isinstance(data, OrderedDict): + owner = name + ' data' data = self._make_implicit_object_type(name, info, 'arg', - self._make_members(data)) + self._make_members(data, + owner)) self._def_entity(QAPISchemaEvent(name, info, data)) def _def_exprs(self):