]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
attribs: Don't canonicalize lookup_scoped_attribute_spec argument [PR113674]
authorJakub Jelinek <jakub@redhat.com>
Mon, 12 Feb 2024 19:45:01 +0000 (20:45 +0100)
committerJakub Jelinek <jakub@redhat.com>
Thu, 20 Jun 2024 13:07:23 +0000 (15:07 +0200)
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)

gcc/attribs.c
gcc/c-family/c-lex.c
gcc/testsuite/c-c++-common/Wattributes-3.c [new file with mode: 0644]

index 47969bd7742680fb6993827fe5cab7db9b403507..1dd0212448e40db7f1ae8c20184fb70960808c77 100644 (file)
@@ -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));
index 6374b72ed2de7195fd6bd908abb9fa8f32656ebe..88ec7a48fcf1989ad091b2a2f24c86c3ed6871fd 100644 (file)
@@ -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 (file)
index 0000000..a1a6d9a
--- /dev/null
@@ -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;
+}