[gcc r11-11494] attribs: Don't canonicalize lookup_scoped_attribute_spec argument [PR113674]

Jakub Jelinek jakub@gcc.gnu.org
Thu Jun 20 13:22:13 GMT 2024


https://gcc.gnu.org/g:f520f125920460d9360e2a5619b22c7ac81d35ba

commit r11-11494-gf520f125920460d9360e2a5619b22c7ac81d35ba
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Feb 12 20:45:01 2024 +0100

    attribs: Don't canonicalize lookup_scoped_attribute_spec argument [PR113674]
    
    The C and C++ FEs when parsing attributes already canonicalize them
    (i.e. if they start with __ and end with __ substrings, we remove those).
    lookup_attribute already verifies in gcc_assert that the first character
    of name is not an underscore, and even lookup_scoped_attribute_spec doesn't
    attempt to canonicalize the namespace it is passed.  But for some historic
    reason it was canonicalizing the name argument, which misbehaves when
    an attribute starts with ____ and ends with ____.
    I believe it is just wrong to try to canonicalize
    lookup_scope_attribute_spec name attribute, it should have been
    canonicalized already, in other spots where it is called it is already
    canonicalized before.
    
    2024-02-12  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/113674
            * attribs.c (extract_attribute_substring): Remove.
            (lookup_scoped_attribute_spec): Don't call it.
    
            * c-lex.c (c_common_has_attribute): Call canonicalize_attr_name.
    
            * c-c++-common/Wattributes-3.c: New test.
    
    (cherry picked from commit b42e978f29b33071addff6d7bb8bcdb11d176606)

Diff:
---
 gcc/attribs.c                              | 15 ---------------
 gcc/c-family/c-lex.c                       |  1 +
 gcc/testsuite/c-c++-common/Wattributes-3.c | 13 +++++++++++++
 3 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/gcc/attribs.c b/gcc/attribs.c
index 47969bd77426..1dd0212448e4 100644
--- a/gcc/attribs.c
+++ b/gcc/attribs.c
@@ -105,20 +105,6 @@ static const struct attribute_spec empty_attribute_table[] =
   { NULL, 0, 0, false, false, false, false, NULL, NULL }
 };
 
-/* Return base name of the attribute.  Ie '__attr__' is turned into 'attr'.
-   To avoid need for copying, we simply return length of the string.  */
-
-static void
-extract_attribute_substring (struct substring *str)
-{
-  if (str->length > 4 && str->str[0] == '_' && str->str[1] == '_'
-      && str->str[str->length - 1] == '_' && str->str[str->length - 2] == '_')
-    {
-      str->length -= 4;
-      str->str += 2;
-    }
-}
-
 /* Insert an array of attributes ATTRIBUTES into a namespace.  This
    array must be NULL terminated.  NS is the name of attribute
    namespace.  The function returns the namespace into which the
@@ -312,7 +298,6 @@ lookup_scoped_attribute_spec (const_tree ns, const_tree name)
 
   attr.str = IDENTIFIER_POINTER (name);
   attr.length = IDENTIFIER_LENGTH (name);
-  extract_attribute_substring (&attr);
   return attrs->attribute_hash->find_with_hash (&attr,
 						substring_hash (attr.str,
 							       	attr.length));
diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c
index 6374b72ed2de..88ec7a48fcf1 100644
--- a/gcc/c-family/c-lex.c
+++ b/gcc/c-family/c-lex.c
@@ -338,6 +338,7 @@ c_common_has_attribute (cpp_reader *pfile, bool std_syntax)
 	      tree attr_id
 		= get_identifier ((const char *)
 				  cpp_token_as_text (pfile, nxt_token));
+	      attr_id = canonicalize_attr_name (attr_id);
 	      attr_name = build_tree_list (attr_ns, attr_id);
 	    }
 	  else
diff --git a/gcc/testsuite/c-c++-common/Wattributes-3.c b/gcc/testsuite/c-c++-common/Wattributes-3.c
new file mode 100644
index 000000000000..a1a6d9a58955
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Wattributes-3.c
@@ -0,0 +1,13 @@
+/* PR c++/113674 */
+/* { dg-do compile { target { c || c++11 } } } */
+/* { dg-options "" } */
+
+[[____noreturn____]] int foo (int i)		/* { dg-warning "'__noreturn__' attribute (directive )?ignored" } */
+{
+  return i;
+}
+
+[[____maybe_unused____]] int bar (int i)	/* { dg-warning "'__maybe_unused__' attribute (directive )?ignored" } */
+{
+  return i;
+}


More information about the Gcc-cvs mailing list