The attached patch fixes bug 79548 - [5/6/7 Regression] missing -Wunused-variable on a typedef'd variable in a function template, most likely broken by the introduction of -Wunused-local-typedefs.
While testing the patch I came across a couple of other bugs: 79585 - spurious -Wunused-variable on a pointer with attribute unused in function template and 79586 - missing -Wdeprecated depending on position of attribute The test I added for 79548 fails two assertions due to the first of these two so I xfailed them. The second doesn't have an impact on it. Neither of these is a regression so I didn't try to fix them. Martin
PR c++/79548 - [5/6/7 Regression] missing -Wunused-variable on a typedef'd variable in a function template gcc/cp/ChangeLog: PR c++/79548 * decl.c (poplevel): Avoid diagnosing entities declared with attribute unused. (initialize_local_var): Do not consider the type of a variable when determining whether or not it's used. gcc/testsuite/ChangeLog: PR c++/79548 * g++.dg/warn/Wunused-var-26.C: New test. diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 70c44fb..e315ad0 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -664,7 +664,8 @@ poplevel (int keep, int reverse, int functionbody) && (!CLASS_TYPE_P (type) || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) || lookup_attribute ("warn_unused", - TYPE_ATTRIBUTES (TREE_TYPE (decl))))) + TYPE_ATTRIBUTES (TREE_TYPE (decl)))) + && !lookup_attribute ("unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))) { if (! TREE_USED (decl)) warning_at (DECL_SOURCE_LOCATION (decl), @@ -6546,7 +6547,6 @@ initialize_local_var (tree decl, tree init) { tree type = TREE_TYPE (decl); tree cleanup; - int already_used; gcc_assert (VAR_P (decl) || TREE_CODE (decl) == RESULT_DECL); @@ -6564,7 +6564,7 @@ initialize_local_var (tree decl, tree init) return; /* Compute and store the initial value. */ - already_used = TREE_USED (decl) || TREE_USED (type); + bool already_used = TREE_USED (decl); if (TREE_USED (type)) DECL_READ_P (decl) = 1; diff --git a/gcc/testsuite/g++.dg/warn/Wunused-var-26.C b/gcc/testsuite/g++.dg/warn/Wunused-var-26.C new file mode 100644 index 0000000..562f25b --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wunused-var-26.C @@ -0,0 +1,127 @@ +// PR c++/79548 - missing -Wunused-variable on a typedef'd variable +// in a function template +// { dg-do compile } +// { dg-options "-Wunused" } + + +#define UNUSED __attribute__ ((unused)) + +template <class T> +void f_int () +{ + T t; // { dg-warning "unused variable" } + + typedef T U; + U u; // { dg-warning "unused variable" } +} + +template void f_int<int>(); + + +template <class T> +void f_intptr () +{ + T *t = 0; // { dg-warning "unused variable" } + + typedef T U; + U *u = 0; // { dg-warning "unused variable" } +} + +template void f_intptr<int>(); + + +template <class T> +void f_var_unused () +{ + // The variable is marked unused. + T t UNUSED; + + typedef T U; + U u UNUSED; +} + +template void f_var_unused<int>(); + + +template <class T> +void f_var_type_unused () +{ + // The variable's type is marked unused. + T* UNUSED t = new T; // { dg-bogus "unused variable" "bug 79585" { xfail *-*-* } } + + typedef T U; + U* UNUSED u = new U; // { dg-bogus "unused variable" "bug 79585" { xfail *-*-* } } + + typedef T UNUSED U; + U v = U (); // { dg-bogus "unused variable" "bug 79585" { xfail *-*-* } } +} + +template void f_var_type_unused<int>(); + + +struct A { int i; }; + +template <class T> +void f_A () +{ + T t; // { dg-warning "unused variable" } + + typedef T U; + U u; // { dg-warning "unused variable" } +} + +template void f_A<A>(); + + +template <class T> +void f_A_unused () +{ + T t UNUSED; + + typedef T U; + U u UNUSED; +} + +template void f_A_unused<A>(); + + +struct B { B (); }; + +template <class T> +void f_B () +{ + T t; + + typedef T U; + U u; +} + +template void f_B<B>(); + + +struct C { ~C (); }; + +template <class T> +void f_C () +{ + T t; + + typedef T U; + U u; +} + +template void f_C<C>(); + + +struct D { B b; }; + +template <class T> +void f_D () +{ + T t; + + typedef T U; + U u; +} + +template void f_D<D>();