public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Martin Sebor <msebor@gmail.com>
To: Jakub Jelinek <jakub@redhat.com>
Cc: Jeff Law <law@redhat.com>, Gcc Patch List <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] handle expressions in __builtin_has_attribute (PR 88383)
Date: Fri, 14 Dec 2018 04:04:00 -0000	[thread overview]
Message-ID: <5c5214f3-3165-707b-4e43-bfac000851a6@gmail.com> (raw)
In-Reply-To: <20181213233954.GH12380@tucnak>

On 12/13/18 4:39 PM, Jakub Jelinek wrote:
> On Thu, Dec 13, 2018 at 02:05:29PM -0700, Martin Sebor wrote:
>>> So how is the builtin defined then?  Is the argument always an expression
>>> and you only return whether its type has the attribute, or do something
>>> different if the expression is of certain kind?
>>
>> For a DECL the built-in looks at both the DECL_ATTRIBUTES and
>> the DECL's type's TYPE_ATTRIBUTES.  For expressions, it just
>> looks at TYPE_ATTRIBUTES of the expression's type.  I'm not
>> sure that this is necessarily the cleanest design but it's
>> meant to follow how attributes are applied to DECLs.  Sometimes
>> they get applied to the DECL itself and other times to its type.
>> This has been causing confusion to both users and GCC devs such
>> as myself so at some point I'd like to make this clearer somehow.
>> Not sure exactly how yet.
> 
> But some users of the attribute look for the attribute on TYPE_ATTRIBUTES,
> other on DECL_ATTRIBUTES, and the attributes have bools which say to what
> they apply.  So, I think the API need to be very clear whether the attribute
> is on a decl or on a type.
> 
> If it is similar to say sizeof/__alignof__ etc., where it accepts either
> a type (for which one can use __typeof/decltype etc.), in which case it
> would check the type attributes, or an expression and in that case require
> it to be a decl and use decl attributes, one API is fine, but supporting
> arbitrary expressions and sometimes looking at type, other times at decl
> or both is not a good design.

It's as good as the design of GCC attributes allows.  Based on
the manual a function declaration like

   __attribute__ ((alloc_size (1), malloc))
   void* allocate (unsigned);

should have those two attributes applied to it.  Yet, alloc_size
is actually applied to its type (but not to its decl) while malloc
to the function's decl but not its type (bug 88397).

I'm pretty sure most users still expect the following to pass:

   _Static_assert (__builtin_has_attribute (allocate, alloc_size));
   _Static_assert (__builtin_has_attribute (allocate, malloc));

It wouldn't if the built-in differentiated between decls and types.

Even if/when we make these function attributes (and others like it)
apply to types as I think we should so it's possible to apply them
to function pointers as users expect, there still will be others
that will apply only to the function decls.  The distinction
between when GCC chooses to apply an attribute to a function decl
vs to its type is not intuitive and cannot very well be, and neither
would be an API that queried just a decl attribute or just a type
attribute.  The most common use case is to query whether a function
has been declared with an attribute -- it doesn't matter whether
it's on its unique type or on its decl.  (The others might perhaps
be useful too, but a lot less often.)

Martin

  reply	other threads:[~2018-12-14  4:04 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-06  3:56 Martin Sebor
2018-12-13 18:59 ` Jeff Law
2018-12-13 19:21   ` Martin Sebor
2018-12-13 19:48     ` Martin Sebor
2018-12-13 19:56       ` Jakub Jelinek
2018-12-13 22:22         ` Martin Sebor
2018-12-13 23:40           ` Jakub Jelinek
2018-12-14  4:04             ` Martin Sebor [this message]
2018-12-14  7:41               ` Jakub Jelinek
2018-12-15  0:18                 ` Martin Sebor
2018-12-21  2:56       ` Martin Sebor
2019-01-03 22:22         ` PING " Martin Sebor
2019-01-08  0:32           ` PING #2 " Martin Sebor
2019-01-15 16:31             ` Martin Sebor
2019-01-17  1:16               ` Jeff Law

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=5c5214f3-3165-707b-4e43-bfac000851a6@gmail.com \
    --to=msebor@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=law@redhat.com \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).