This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [C++ PATCH] Fix up array initializations from compound literal (PR c++/40948)
On 08/03/2009 06:35 PM, Jakub Jelinek wrote:
I think it is destroyed after it is copied.
True, but that's still too soon; it shouldn't be destroyed until end of
full-expression. Better, I think, to just pull the CONSTRUCTOR out of
the TARGET_EXPR, since we now know what we're initializing; that lets us
construct the objects being initialized directly, rather than build a
temporary, copy it and then destroy it.
I'm checking this patch in:
2009-08-03 Jason Merrill <jason@redhat.com>
Jakub Jelinek <jakub@redhat.com>
PR c++/40948
* init.c (build_vec_init): Look through a TARGET_EXPR around a
CONSTRUCTOR.
diff --git a/gcc/cp/init.c b/gcc/cp/init.c
index 3da8ab8..4462428 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -2695,6 +2695,12 @@ build_vec_init (tree base, tree maxindex, tree init,
gcc_assert (!init);
inner_elt_type = strip_array_types (type);
+
+ /* Look through the TARGET_EXPR around a compound literal. */
+ if (init && TREE_CODE (init) == TARGET_EXPR
+ && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR)
+ init = TARGET_EXPR_INITIAL (init);
+
if (init
&& TREE_CODE (atype) == ARRAY_TYPE
&& (from_array == 2
diff --git a/gcc/testsuite/g++.dg/ext/complit12.C b/gcc/testsuite/g++.dg/ext/complit12.C
new file mode 100644
index 0000000..8105621
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/complit12.C
@@ -0,0 +1,54 @@
+// PR c++/40948
+// { dg-do run }
+// { dg-options "" }
+
+int c;
+struct M
+{
+ M () { ++c; }
+ M (const M&) { ++c; }
+ ~M () { --c; }
+};
+
+struct S
+{
+ S ();
+ M m[1];
+};
+
+S::S () : m ((M[1]) { M () })
+{
+}
+
+struct T
+{
+ T ();
+ M m[4];
+};
+
+T::T () : m ((M[4]) { M (), M (), M (), M () })
+{
+}
+
+int main ()
+{
+ {
+ M m[1] = (M[1]) { M () };
+ if (c != 1)
+ return 1;
+ M n = (M) { M () };
+ if (c != 2)
+ return 2;
+ M o[4] = (M[4]) { M (), M (), M (), M () };
+ if (c != 6)
+ return 3;
+ S s;
+ if (c != 7)
+ return 4;
+ T t;
+ if (c != 11)
+ return 5;
+ }
+ if (c != 0)
+ return 6;
+}