aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorJason Merrill <jason@redhat.com>2007-10-04 13:58:07 -0400
committerJason Merrill <jason@gcc.gnu.org>2007-10-04 13:58:07 -0400
commit42f02385bdfb21849e984584082047881f53d8c9 (patch)
tree4842f3ff4a89a087a33325db52de73941ed4aadd /gcc
parent37b22c82838f375dc1302a1ee1220fd5a81a3a1a (diff)
downloadgcc-42f02385bdfb21849e984584082047881f53d8c9.zip
gcc-42f02385bdfb21849e984584082047881f53d8c9.tar.gz
gcc-42f02385bdfb21849e984584082047881f53d8c9.tar.bz2
re PR c++/20416 (Incorrect lifetime for temporary with static const reference)
PR c++/20416 * call.c (initialize_reference): Handle local static reference temps properly. From-SVN: r129020
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/ChangeLog6
-rw-r--r--gcc/cp/call.c6
-rw-r--r--gcc/testsuite/g++.dg/init/ref15.C32
3 files changed, 43 insertions, 1 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 0cec087..240990e 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,9 @@
+2007-10-04 Jason Merrill <jason@redhat.com>
+
+ PR c++/20416
+ * call.c (initialize_reference): Handle local static reference
+ temps properly.
+
2007-10-03 Jason Merrill <jason@redhat.com>
PR c++/32470
diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index 645eeb2..cf3aea7 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -6824,7 +6824,11 @@ initialize_reference (tree type, tree expr, tree decl, tree *cleanup)
if (at_function_scope_p ())
{
add_decl_expr (var);
- *cleanup = cxx_maybe_build_cleanup (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
diff --git a/gcc/testsuite/g++.dg/init/ref15.C b/gcc/testsuite/g++.dg/init/ref15.C
new file mode 100644
index 0000000..d3a9422
--- /dev/null
+++ b/gcc/testsuite/g++.dg/init/ref15.C
@@ -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;
+}