On Thu, May 07, 2020 at 06:09:30PM -0600, Martin Sebor wrote:
> On 5/7/20 5:03 PM, Marek Polacek via Gcc-patches wrote:
> > On Wed, Jan 29, 2020 at 10:06:51PM +0100, Paolo Carlini wrote:
> > > Hi,
> > > 
> > > On 29/01/20 19:00, Jason Merrill wrote:
> > > > On 1/29/20 4:31 AM, Paolo Carlini wrote:
> > > > > Hi,
> > > > > 
> > > > > in this regression we issue a diagnostic about an incomplete type
> > > > > (only a warning by default) and then we crash when we try to query
> > > > > has_attribute on a member of the type because in such cases the
> > > > > member remains an IDENTIFIER_NODE which of course doesn't have a
> > > > > TREE_TYPE neither a DECL_ATTRIBUTES... Simply recognizing
> > > > > IDENTIFIER_NODEs and returning false works fine, not sure if we want
> > > > > to do something more sophisticated. Tested x86_64-linux.
> > > > 
> > > > Why are we getting to has_attribute at all for a type-dependent 
> > > > argument?
> > > 
> > > Because the implementation of __builtin_has_attribute, largely shared with
> > > the C front-end, doesn't know about templates at all? :-/
> > > 
> > > Not sure it's the best time to complete it, but shouldn't be too 
> > > difficult.
> > 
> > This ICEs even with a more reasonable test like
> > 
> > template<typename T>
> > void foo ()
> > {
> >    static_assert(!__builtin_has_attribute(T::a, aligned));
> > }
> > 
> > The problem here is that __builtin_has_attribute doesn't handle 
> > type-dependent
> > arguments at all.  To handle type-dependent arguments we'd have to introduce
> > a new template code, like STATIC_ASSERT or ADDRESSOF_EXPR (or a new generic
> > template code for built-ins?), but that's always a pain.
> > 
> > Or, meanwhile, we could just sorry.  Martin, what do you think?
> 
> I never did implement the template handling and I didn't think to put
> in a stopgap like the one below.  It makes sense until I get around to
> implementing it, hopefully for GCC 11.

Ah, and we have PR92104 to track that.   Here's a complete patch then:

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

>From 7ed334b7998314bab12fe4741bc311a47457ea3a Mon Sep 17 00:00:00 2001
From: Marek Polacek <pola...@redhat.com>
Date: Thu, 7 May 2020 21:10:42 -0400
Subject: [PATCH] c++: Sorry about type-dependent arg for
 __builtin_has_attribute [PR90915]

Until 92104 is fixed, let's sorry rather than crash.

        PR c++/90915
        * parser.c (cp_parser_has_attribute_expression): Sorry on a
        type-dependent argument.

        * g++.dg/ext/builtin-has-attribute.C: New test.
---
 gcc/cp/parser.c                                  | 7 ++++++-
 gcc/testsuite/g++.dg/ext/builtin-has-attribute.C | 8 ++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/ext/builtin-has-attribute.C

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index d67fa3b13d1..f586c89b109 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -8682,7 +8682,12 @@ cp_parser_has_attribute_expression (cp_parser *parser)
   location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
   if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
     {
-      if (oper != error_mark_node)
+      if (oper == error_mark_node)
+       /* Nothing.  */;
+      else if (type_dependent_expression_p (oper))
+       sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
+                 "not supported yet");
+      else
        {
          /* Fold constant expressions used in attributes first.  */
          cp_check_const_attributes (attr);
diff --git a/gcc/testsuite/g++.dg/ext/builtin-has-attribute.C 
b/gcc/testsuite/g++.dg/ext/builtin-has-attribute.C
new file mode 100644
index 00000000000..3438dd59ba3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/builtin-has-attribute.C
@@ -0,0 +1,8 @@
+// PR c++/90915
+// { dg-do compile { target c++11 } }
+
+template<typename T>
+void foo ()
+{
+  static_assert(!__builtin_has_attribute(T::a, aligned), ""); // { dg-message 
"sorry, unimplemented: .__builtin_has_attribute. with dependent argument not 
supported yet" }
+}

base-commit: 74d58ad2c208c9c445bb3e8288db08e092a66316
-- 
Marek Polacek • Red Hat, Inc. • 300 A St, Boston, MA

Reply via email to