For bugs 54372 and 60063, we changed attributes used and unused to be applied immediately in a template even if what they apply to is dependent, to avoid bogus warnings from maybe_warn_unused_local_typedefs. But that's only an issue for TYPE_DECL, so we can use the normal logic for other things.
Tested x86_64-pc-linux-gnu, applying to trunk.
commit dac88137f97d9737fa2e0c5b27ec6431b2d29c7c Author: Jason Merrill <ja...@redhat.com> Date: Wed Apr 4 13:43:26 2018 -0400 PR c++/84221 - bogus -Wunused with attribute and template. * decl2.c (is_late_template_attribute): Handle unused and used normally on non-TYPE_DECL. diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c index 6ae6cef78dd..6078fb668a7 100644 --- a/gcc/cp/decl2.c +++ b/gcc/cp/decl2.c @@ -1145,10 +1145,11 @@ is_late_template_attribute (tree attr, tree decl) if (is_attribute_p ("weak", name)) return true; - /* Attributes used and unused are applied directly, as they appertain to - decls. */ - if (is_attribute_p ("unused", name) - || is_attribute_p ("used", name)) + /* Attributes used and 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))) return false; /* Attribute tls_model wants to modify the symtab. */ diff --git a/gcc/testsuite/g++.dg/warn/Wunused-var-32.C b/gcc/testsuite/g++.dg/warn/Wunused-var-32.C new file mode 100644 index 00000000000..5558f9361fc --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wunused-var-32.C @@ -0,0 +1,9 @@ +// PR c++/84221 +// { dg-additional-options -Wunused } + +template <class T> struct __attribute((unused)) A { }; + +void f (void) +{ + A<int> a; // shouldn't warn +}