Hi Martin,

Thanks for your help.

Martin Sebor <mse...@gmail.com> writes:

> This can be a little confusing and I don't work with the front end
> stuff often enough to remember it so I always have to look it up.
> There's a TYPE_DECL that corresponds to the definition of the type
> that for user-defined types has a source location, otherwise, if T
> it's a TYPE_P(T), it doesn't.
>
> I think the TYPE_NAME(T) macro gets the TYPE_DECL for a type T.
> I find the best way to figure this out is to think of something
> similar to what you're trying to do (like an error or warning in
> this case) and look at/step through the code that implements it.

If I insert an assertion in the code, I find that 'constructor_type' is
indeed a TYPE_P, and TYPE_NAME doesn't work on it. If I try to use
TYPE_NAME, this happens:

    $ install/bin/gcc -c -o test.o test.c
    test.c:14:17: warning: positional initialization of field ‘foo’ in ‘struct 
S’ declared with ‘designated_init’ attribute [-Wdesignated-init]
       14 | struct S s2 = { 0, 0 };  /* should warn */
          |                 ^
    test.c:14:17: note: (near initialization for ‘s2’)
    test.c:14:8: internal compiler error: tree check: expected tree that 
contains ‘decl minimal’ structure, have ‘identifier_node’ in pop_init_level, at 
c/c-typeck.c:8790
       14 | struct S s2 = { 0, 0 };  /* should warn */
          |        ^
    [backtrace]
    $ cat -n gcc/c/c-typeck.c | grep -E -A 3 '^\s*8790\s'
      8790                inform (DECL_SOURCE_LOCATION
      8791                        (TYPE_NAME
      8792                         (constructor_type)),
      8793                        "in definition of %qT", constructor_type);

So it appears that TYPE_NAME is returning an IDENTIFIER_NODE. Strangely,
the gccint documentation says, referring to 'TYPE_NAME':

    (Note this macro does _not_ return an 'IDENTIFIER_NODE', as you
    might expect, given its name!)

> Unfortunately, attributes don't have locations (yet).

Hmm, well maybe I could implement that. I'm not very familiar with the
GCC source (this is my first patch), but I'll see if I can figure it
out. It would probably be useful for other warnings/errors too.

> The warning could have two notes, one pointing to the member and
> another pointing to its type.  It's a judgment call what's more
> important in each case.  It's usually straightforward to find the
> enclosing type given a note pointing to a member.

If it's not possible to point to the attribute itself, then this is
probably the next best thing. Still, it would be nice to be able to
point directly to the attribute...

> I was under the impression that GCC silently ignored attributes on
> type declarations and only respected them on definitions.  But it's
> also possible it's inconsistent (attribute handling is still a bit
> of a mess).

Yes, actually you're right. I tried defining a struct and then applying
an attribute to a later declaration, and it had no effect. Incidentally,
this seems to me to be confusing behavior, and should probably produce a
warning (or even an error), but that's another issue.

Thanks,
Asher

-- 
Violence is the last refuge of the incompetent.
                -- Salvor Hardin
                               --------
I prefer to send and receive mail encrypted. Please send me your
public key, and if you do not have my public key, please let me
know. Thanks.

GPG fingerprint: 38F3 975C D173 4037 B397  8095 D4C9 C4FC 5460 8E68

Attachment: signature.asc
Description: PGP signature

Reply via email to