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 for c++/20416 (lifetime of local static ref temp)


initialize_reference just didn't consider the local static case.

Tested x86_64-pc-linux-gnu, applied to trunk.

2007-10-04  Jason Merrill  <jason@redhat.com>

	PR c++/20416
	* call.c (initialize_reference): Handle local static reference
	temps properly.

Index: cp/call.c
===================================================================
*** cp/call.c	(revision 128889)
--- cp/call.c	(working copy)
*************** initialize_reference (tree type, tree ex
*** 6824,6830 ****
  	      if (at_function_scope_p ())
  		{
  		  add_decl_expr (var);
! 		  *cleanup = cxx_maybe_build_cleanup (var);
  
  		  /* We must be careful to destroy the temporary only
  		     after its initialization has taken place.  If the
--- 6824,6834 ----
  	      if (at_function_scope_p ())
  		{
  		  add_decl_expr (var);
! 
! 		  if (TREE_STATIC (var))
! 		    init = add_stmt_to_compound (init, register_dtor_fn (var));
! 		  else
! 		    *cleanup = cxx_maybe_build_cleanup (var);
  
  		  /* We must be careful to destroy the temporary only
  		     after its initialization has taken place.  If the
Index: testsuite/g++.dg/init/ref15.C
===================================================================
*** testsuite/g++.dg/init/ref15.C	(revision 0)
--- testsuite/g++.dg/init/ref15.C	(revision 0)
***************
*** 0 ****
--- 1,32 ----
+ // PR c++/20416.  We correctly constructed the temporary S in foo(),
+ // but incorrectly destroyed it every time foo() was called.
+ // { dg-do run }
+ extern "C" void abort (void);
+ extern "C" void _exit (int);
+ 
+ int c, exiting;
+ struct S {
+   S() { ++c; }
+   S(const S &) { ++c; }
+   ~S()
+   {
+     if (!exiting) abort ();
+     _exit (0);
+   }
+ };
+ void
+ foo (void)
+ {
+   static const S &s = S();
+ }
+ int main ()
+ {
+   if (c != 0)
+     abort ();
+   foo ();
+   foo ();
+   if (c != 1)
+     abort ();
+   exiting = 1;
+   return 1;
+ }

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