On 6/14/20 11:05 AM, Asher Gordon wrote:
Hello,

Asher Gordon <asd...@posteo.net> writes:

I also added a note after the warning showing where the field was
defined in the structure, like this:

     inform (DECL_SOURCE_LOCATION (info.field),
            "in definition of %qT", constructor_type);

However, I think it would be preferable to point to the definition of
the structure itself, since that is where the attribute is. Something
like this:

     inform (DECL_SOURCE_LOCATION (constructor_type),
            "%qT defined here", constructor_type);

Unfortunately, the above does not work since 'constructor_type' is a
type rather than a variable, and thus DECL_SOURCE_LOCATION does not work
on it. Do you know if there is something similar to DECL_SOURCE_LOCATION
that can be used on types like 'constructor_type'?

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.


Actually, even that would not be perfect, because the structure may be
defined somewhere and the attribute applied to it elsewhere, even in a
different file. For example:

     struct S {
       int foo, bar;
     };
     struct __attribute__((designated_init)) S;

So it would be better to point to the second declaration of S, because
that is where the attribute is applied. Actually, it would be ideal to
point to the attribute itself, so a note something like the following
could be produced:

     test.c:4:22: note: ‘designated_init’ attribute applied here
         4 | struct __attribute__((designated_init)) S;
           |                       ^~~~~~~~~~~~~~~

Do you know if that's possible?

Unfortunately, attributes don't have locations (yet).  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.

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).

Martin

Reply via email to