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 +void f_int () +{ + T t; // { dg-warning "unused variable" } + + typedef T U; + U u; // { dg-warning "unused variable" } +} + +template void f_int(); + + +template +void f_intptr () +{ + T *t = 0; // { dg-warning "unused variable" } + + typedef T U; + U *u = 0; // { dg-warning "unused variable" } +} + +template void f_intptr(); + + +template +void f_var_unused () +{ + // The variable is marked unused. + T t UNUSED; + + typedef T U; + U u UNUSED; +} + +template void f_var_unused(); + + +template +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(); + + +struct A { int i; }; + +template +void f_A () +{ + T t; // { dg-warning "unused variable" } + + typedef T U; + U u; // { dg-warning "unused variable" } +} + +template void f_A(); + + +template +void f_A_unused () +{ + T t UNUSED; + + typedef T U; + U u UNUSED; +} + +template void f_A_unused(); + + +struct B { B (); }; + +template +void f_B () +{ + T t; + + typedef T U; + U u; +} + +template void f_B(); + + +struct C { ~C (); }; + +template +void f_C () +{ + T t; + + typedef T U; + U u; +} + +template void f_C(); + + +struct D { B b; }; + +template +void f_D () +{ + T t; + + typedef T U; + U u; +} + +template void f_D();