Hi!

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.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2025-12-31  Jakub Jelinek  <[email protected]>

        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.jj  2025-12-27 11:44:31.534958161 +0100
+++ gcc/cp/decl2.cc     2025-12-31 12:32:56.653139050 +0100
@@ -1480,11 +1480,14 @@ is_late_template_attribute (tree attr, t
   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.  */
--- gcc/testsuite/g++.dg/warn/Wunused-local-typedefs-5.C.jj     2025-12-31 
12:26:55.138450671 +0100
+++ gcc/testsuite/g++.dg/warn/Wunused-local-typedefs-5.C        2025-12-31 
12:26:02.483371704 +0100
@@ -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;
+}

        Jakub

Reply via email to