From a1a9524971df4d4991760ccb1189049f067c7e80 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Wed, 6 Sep 2006 01:25:29 -0400 Subject: [PATCH] re PR c++/26671 (Missing "warning: reference to local variable returned") PR c++/26671 * typeck.c (maybe_warn_about_returning_address_of_local): Look through COMPONENT_REF and ARRAY_REF. From-SVN: r116714 --- gcc/cp/ChangeLog | 4 ++++ gcc/cp/typeck.c | 4 ++++ gcc/testsuite/g++.dg/warn/return-reference2.C | 21 +++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 gcc/testsuite/g++.dg/warn/return-reference2.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 3b534128ef7..014c76bbb0a 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,9 @@ 2006-09-05 Jason Merrill + PR c++/26671 + * typeck.c (maybe_warn_about_returning_address_of_local): Look + through COMPONENT_REF and ARRAY_REF. + PR c++/26102 * name-lookup.c (do_class_using_decl): Try to find the base even if bases_dependent_p. diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index c07a78d1ef4..220be7eda86 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -6365,6 +6365,10 @@ maybe_warn_about_returning_address_of_local (tree retval) } } + while (TREE_CODE (whats_returned) == COMPONENT_REF + || TREE_CODE (whats_returned) == ARRAY_REF) + whats_returned = TREE_OPERAND (whats_returned, 0); + if (DECL_P (whats_returned) && DECL_NAME (whats_returned) && DECL_FUNCTION_SCOPE_P (whats_returned) diff --git a/gcc/testsuite/g++.dg/warn/return-reference2.C b/gcc/testsuite/g++.dg/warn/return-reference2.C new file mode 100644 index 00000000000..96e44a1ae5f --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/return-reference2.C @@ -0,0 +1,21 @@ +// PR c++/26671 + +class A +{ +public: + int first; + int second; + +}; + +int &f() +{ + A a; // { dg-error "local" } + return a.second; +} + +int &g() +{ + int ar[42]; // { dg-error "local" } + return ar[20]; +}