https://gcc.gnu.org/g:6dc335c506aca39af537180b2a0b23e640e5346e

commit r16-6462-g6dc335c506aca39af537180b2a0b23e640e5346e
Author: Jakub Jelinek <[email protected]>
Date:   Fri Jan 2 09:18:02 2026 +0100

    c++: Fix up is_late_template_attribute for [[maybe_unused]] [PR123277]
    
    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  <[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.

Diff:
---
 gcc/cp/decl2.cc                                      |  9 ++++++---
 gcc/testsuite/g++.dg/warn/Wunused-local-typedefs-5.C | 15 +++++++++++++++
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index cb022d0b671c..7bd6cbe56801 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -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
index 000000000000..a041690300e3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wunused-local-typedefs-5.C
@@ -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;
+}

Reply via email to