This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

C++ PATCH to maybe_warn_about_returning_address_of_local for c++/26671


In 26671, we return a reference to a COMPONENT_REF of a local variable. We already have a function for this sort of thing, just needed to make it look through COMPONENT_REF.

Tested x86_64-pc-linux-gnu, applied to trunk.
2006-09-05  Jason Merrill  <jason@redhat.com>

	PR c++/26671
	* typeck.c (maybe_warn_about_returning_address_of_local): Look 
	through COMPONENT_REF and ARRAY_REF.	

Index: cp/typeck.c
===================================================================
*** cp/typeck.c	(revision 116703)
--- cp/typeck.c	(working copy)
*************** maybe_warn_about_returning_address_of_lo
*** 6365,6370 ****
--- 6365,6374 ----
  	}
      }
  
+   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)
Index: testsuite/g++.dg/warn/return-reference2.C
===================================================================
*** testsuite/g++.dg/warn/return-reference2.C	(revision 0)
--- testsuite/g++.dg/warn/return-reference2.C	(revision 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];
+ }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]