]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Fix up is_late_template_attribute for [[maybe_unused]] [PR123277]
authorJakub Jelinek <jakub@redhat.com>
Fri, 2 Jan 2026 08:18:02 +0000 (09:18 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Fri, 2 Jan 2026 08:18:02 +0000 (09:18 +0100)
is_late_template_attribute wants to return false for gnu::unused, gnu::used
and maybe_unused attributes, so that -Wunused-local-typedefs sees those
attributes applied even to typedefs to dependent types.
Before my recent r16-5937 change, for maybe_unused this happened to work
through the lookup_attribute_spec returning NULL in that case because
there is no gnu::maybe_unused attribute, but when that has been fixed,
maybe_unused needs to be listed in the exceptions next to unused and used
attributes.

2026-01-02  Jakub Jelinek  <jakub@redhat.com>

PR c++/123277
* decl2.cc (is_late_template_attribute): Return false also for
[[maybe_unused]] attribute on TYPE_DECLs to dependent types.

* g++.dg/warn/Wunused-local-typedefs-5.C: New test.

gcc/cp/decl2.cc
gcc/testsuite/g++.dg/warn/Wunused-local-typedefs-5.C [new file with mode: 0644]

index cb022d0b671cd6062a3fe17e595e0cf435fbc407..7bd6cbe568016f785c20e0abfc41e72e9739f15f 100644 (file)
@@ -1480,11 +1480,14 @@ is_late_template_attribute (tree attr, tree decl)
   if (is_attribute_p ("weak", name))
     return true;
 
-  /* Attributes used and unused are applied directly to typedefs for the
-     benefit of maybe_warn_unused_local_typedefs.  */
+  /* Attributes used and unused or std attribute maybe_unused are applied
+     directly to typedefs for the benefit of
+     maybe_warn_unused_local_typedefs.  */
   if (TREE_CODE (decl) == TYPE_DECL
       && (is_attribute_p ("unused", name)
-         || is_attribute_p ("used", name)))
+         || is_attribute_p ("used", name)
+         || (is_attribute_p ("maybe_unused", name)
+             && get_attribute_namespace (attr) == NULL_TREE)))
     return false;
 
   /* Attribute tls_model wants to modify the symtab.  */
diff --git a/gcc/testsuite/g++.dg/warn/Wunused-local-typedefs-5.C b/gcc/testsuite/g++.dg/warn/Wunused-local-typedefs-5.C
new file mode 100644 (file)
index 0000000..a041690
--- /dev/null
@@ -0,0 +1,15 @@
+// PR c++/123277
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wunused-local-typedefs" }
+
+template <typename T>
+void
+foo (T B)
+{
+  typedef T C [[maybe_unused]];
+  typedef T D [[gnu::unused]];
+  typedef T E [[gnu::used]];
+  typedef T F;
+  typedef T G;                 // { dg-warning "typedef 'G' locally defined but not used" }
+  F f;
+}