* C++ PATCH for c++/20416 (lifetime of local static ref temp)
@ 2007-10-04 17:57 Jason Merrill
0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2007-10-04 17:57 UTC (permalink / raw)
To: gcc-patches List
[-- Attachment #1: Type: text/plain, Size: 113 bytes --]
initialize_reference just didn't consider the local static case.
Tested x86_64-pc-linux-gnu, applied to trunk.
[-- Attachment #2: static-reftemp.patch --]
[-- Type: text/x-patch, Size: 1777 bytes --]
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;
+ }
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2007-10-04 17:57 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-04 17:57 C++ PATCH for c++/20416 (lifetime of local static ref temp) Jason Merrill
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).