From 9127c38e118f34e261face580e21de592d46c7f3 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Fri, 21 Nov 2014 21:21:35 -0500 Subject: [PATCH] re PR c++/63657 (-Wunused-variable: warning supressed by virtual dtor) PR c++/63657 PR c++/38958 * call.c (set_up_extended_ref_temp): Set TREE_USED on the reference if the temporary has a non-trivial destructor. * decl.c (poplevel): Don't look through references. From-SVN: r217957 --- gcc/cp/ChangeLog | 6 ++++++ gcc/cp/call.c | 4 ++++ gcc/cp/decl.c | 3 +-- gcc/testsuite/g++.dg/warn/Wunused-var-22.C | 12 ++++++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wunused-var-22.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index b1d9cea6ed7..6cc7e620970 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,11 @@ 2014-11-21 Jason Merrill + PR c++/63657 + PR c++/38958 + * call.c (set_up_extended_ref_temp): Set TREE_USED on the reference + if the temporary has a non-trivial destructor. + * decl.c (poplevel): Don't look through references. + PR c++/63942 * name-lookup.c (supplement_binding_1): Override a mangling alias. * mangle.c (maybe_remove_implicit_alias): New. diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 5cda1b1ee2b..a7a8667b5f8 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -9622,6 +9622,10 @@ set_up_extended_ref_temp (tree decl, tree expr, vec **cleanups, /* Check whether the dtor is callable. */ cxx_maybe_build_cleanup (var, tf_warning_or_error); } + /* Avoid -Wunused-variable warning (c++/38958). */ + if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) + && TREE_CODE (decl) == VAR_DECL) + TREE_USED (decl) = DECL_READ_P (decl) = true; *initp = init; return var; diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 899637f68f4..225d4089882 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -638,8 +638,7 @@ poplevel (int keep, int reverse, int functionbody) push_local_binding where the list of decls returned by getdecls is built. */ decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d; - // See through references for improved -Wunused-variable (PR 38958). - tree type = non_reference (TREE_TYPE (decl)); + tree type = TREE_TYPE (decl); if (VAR_P (decl) && (! TREE_USED (decl) || !DECL_READ_P (decl)) && ! DECL_IN_SYSTEM_HEADER (decl) diff --git a/gcc/testsuite/g++.dg/warn/Wunused-var-22.C b/gcc/testsuite/g++.dg/warn/Wunused-var-22.C new file mode 100644 index 00000000000..8ae46c17f82 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wunused-var-22.C @@ -0,0 +1,12 @@ +// PR c++/63657 +// { dg-options "-Wunused-variable" } + +class Bar +{ + virtual ~Bar() {} +}; +Bar& getbar(); +void bar() +{ + Bar& b = getbar(); // { dg-warning "unused" } +} -- 2.30.2