qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: marcandre.lureau@redhat.com, qemu-devel@nongnu.org,
	ehabkost@redhat.com, Michael Roth <mdroth@linux.vnet.ibm.com>
Subject: Re: [Qemu-devel] [PATCH v6 07/12] qapi: Detect collisions in C member names
Date: Fri, 2 Oct 2015 09:12:14 -0600	[thread overview]
Message-ID: <560E9ECE.9050904@redhat.com> (raw)
In-Reply-To: <87bnch76hs.fsf@blackfin.pond.sub.org>

[-- Attachment #1: Type: text/plain, Size: 4429 bytes --]

On 10/02/2015 07:19 AM, Markus Armbruster wrote:
> Eric Blake <eblake@redhat.com> writes:
> 
>> Detect attempts to declare two object members that would result
>> in the same C member name, by keying the 'seen' dictionary off
>> of the C name rather than the qapi name.  Doing this was made
>> easier by adding a member.c_name() helper function.
> 
> This gains protection against colliding C names.  It keeps protection
> against colliding QMP names as long as QMP name collision implies C name
> collision.  I think that's the case, but it's definitely worth spelling
> out in the code, and possibly in the commit message.
> 
>> As this is the first error raised within the QAPISchema*.check()
>> methods, we also have to pass 'info' through the call stack, and
>> fix the overall 'try' to display errors detected during the
>> check() phase.
> 
> Could also be a separate patch, if the parts are easier to review.

I'm still debating about that.


>> +    def check(self, schema, info, all_members, seen):
>> +        if self.c_name() in seen:
>> +            raise QAPIExprError(info,
>> +                                "%s collides with %s"
>> +                                % (self.describe(),
>> +                                   seen[self.c_name()].describe()))
>>          self.type = schema.lookup_type(self._type_name)
>>          assert self.type
>>          all_members.append(self)
>> -        seen[self.name] = self
>> +        seen[self.c_name()] = self
>> +
>> +    def c_name(self):
>> +        return c_name(self.name)
> 
> Why wrap function c_name() in a method?  Why not simply call the
> function?

'self.c_name()' is shorter than 'c_name(self.name)'.  And I already had
long lines with that seen[self.c_name()].describe() pattern.

> 
> It's method in QAPISchemaEntity only because this lets us add special
> cases in a neat way.

True, but I _did_ mention in the commit message that I did it for less
typing.

But as to special cases, yes, I have one in mind (although I have not
played with it yet).  In v5 19/46 Simplify visiting of alternate types,
I want to change alternates to have variants.tag_member == None, and the
generated C code to use 'qtype_code type;' as the tag variable.  In the
v5 representation, it led to a lot of special-casing (many uses of
QAPISchemaObjectTypeVariants became more complex because tag_member was
not always defined).  But now that I've been working on these front-end
patches, my idea was to do something like:

class QAPISchemaVariantTag(QAPISchemaObjectTypeMember):
    def __init__(self, info):
        QAPISchemaObjectTypeMember(self, 'type', None, False)

    def c_type(self):
        return 'qtype_code'

and then, we WOULD need member.c_type() rather than member.type.c_type()
(at which point having member.c_name() to match member.c_type() makes
more sense).

>> @@ -1028,23 +1037,24 @@ class QAPISchemaObjectTypeVariants(object):
>>          self.tag_member = tag_member
>>          self.variants = variants
>>
>> -    def check(self, schema, members, seen):
>> +    def check(self, schema, info, members, seen):
>>          if self.tag_name:
>> -            self.tag_member = seen[self.tag_name]
>> +            self.tag_member = seen[c_name(self.tag_name)]
>> +            assert self.tag_name == self.tag_member.name
> 
> Assertion should hold before the patch, but it's kind of trivial then.

My worry here was that if I have:

{ 'enum': 'Enum', 'data': ['a'] }
{ 'base': 'Base', 'data': { 'b-c': 'Enum' } }
{ 'union': 'Flat', 'base': 'Base', 'discriminator': 'b_c',
  'data': { 'a': 'Struct...' } }

the old ad hoc parser rejects 'b_c' as not being a member of Enum (bad
discriminator).  But once we convert from the old parser to the check()
method (basically, anywhere the check() methods now have an assert will
become if statements that raise an exception), we need to make sure that
we don't get confused by the fact that seen[c_name('b_c')] maps to the
same value as seen[c_name('b-c')], so that we are still flagging the
flat union as invalid.

> 
> I'm fine with not splitting this patch.  I'd be also fine with splitting
> it up.  Your choice.

I'm still thinking about it; may depend on how much other refactoring I do.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

  reply	other threads:[~2015-10-02 15:12 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-02  4:31 [Qemu-devel] [PATCH v6 00/12] post-introspection cleanups, subset B Eric Blake
2015-10-02  4:31 ` [Qemu-devel] [PATCH v6 01/12] qapi: Use predicate callback to determine visit filtering Eric Blake
2015-10-02  6:47   ` Markus Armbruster
2015-10-02 12:16     ` Eric Blake
2015-10-02  4:31 ` [Qemu-devel] [PATCH v6 02/12] qapi: Don't use info as witness of implicit object type Eric Blake
2015-10-02  7:02   ` Markus Armbruster
2015-10-02 12:54     ` Eric Blake
2015-10-02 14:12       ` Markus Armbruster
2015-10-02 14:33         ` Eric Blake
2015-10-02 16:48           ` Markus Armbruster
2015-10-02 16:57             ` Eric Blake
2015-10-02 22:40               ` Eric Blake
2015-10-06  8:32               ` Markus Armbruster
2015-10-06 11:56                 ` Eric Blake
2015-10-06 13:31                   ` Markus Armbruster
2015-10-02  4:31 ` [Qemu-devel] [PATCH v6 03/12] qapi: Lazy creation of array types Eric Blake
2015-10-02  8:06   ` Markus Armbruster
2015-10-02 13:05     ` Eric Blake
2015-10-06  8:35       ` Markus Armbruster
2015-10-02  4:31 ` [Qemu-devel] [PATCH v6 04/12] qapi: Create simple union type member earlier Eric Blake
2015-10-02  8:34   ` Markus Armbruster
2015-10-02 13:08     ` Eric Blake
2015-10-02  4:31 ` [Qemu-devel] [PATCH v6 05/12] qapi: Track location that created an implicit type Eric Blake
2015-10-02  8:54   ` Markus Armbruster
2015-10-02 14:07     ` Eric Blake
2015-10-02 16:07       ` Markus Armbruster
2015-10-02 16:13         ` Eric Blake
2015-10-02  4:31 ` [Qemu-devel] [PATCH v6 06/12] qapi: Track owner of each object member Eric Blake
2015-10-02  9:50   ` Markus Armbruster
2015-10-02 14:48     ` Eric Blake
2015-10-02 17:05       ` Markus Armbruster
2015-10-02 22:35         ` Eric Blake
2015-10-02  4:31 ` [Qemu-devel] [PATCH v6 07/12] qapi: Detect collisions in C member names Eric Blake
2015-10-02 13:19   ` Markus Armbruster
2015-10-02 15:12     ` Eric Blake [this message]
2015-10-02 17:11       ` Markus Armbruster
2015-10-03  1:01         ` Eric Blake
2015-10-06  8:41           ` Markus Armbruster
2015-10-02  4:31 ` [Qemu-devel] [PATCH v6 08/12] qapi: Defer duplicate member checks to schema check() Eric Blake
2015-10-02 14:00   ` Markus Armbruster
2015-10-02 15:52     ` Eric Blake
2015-10-02  4:31 ` [Qemu-devel] [PATCH v6 09/12] qapi: Defer duplicate enum value " Eric Blake
2015-10-02  4:31 ` [Qemu-devel] [PATCH v6 10/12] qapi: Correct error for union branch 'kind' clash Eric Blake
2015-10-03 17:56   ` Eric Blake
2015-10-02  4:31 ` [Qemu-devel] [PATCH v6 11/12] qapi: Detect base class loops Eric Blake
2015-10-02  4:31 ` [Qemu-devel] [PATCH v6 12/12] RFC: qapi: Hide _info member Eric Blake

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=560E9ECE.9050904@redhat.com \
    --to=eblake@redhat.com \
    --cc=armbru@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).