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]

Re: [PATCH] Do not discard the constructors of empty structs [PR c++/64527]


On Thu, 16 Apr 2015, Jason Merrill wrote:

On 04/15/2015 09:00 PM, Patrick Palka wrote:
-	if (!cleared || num_nonzero_elements > 0)

How about adding || TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p), 1) to this test rather than removing it entirely?

That works too.  Does the following patch look OK if testing succeeds?

gcc/
	PR c++/64527
	* gcc/gimplify.c (gimplify_init_constructor): Always emit a
	side-effecting constructor.

gcc/testsuite/
	* g++.dg/init/pr64527.C: New test.
---
 gcc/gimplify.c                      | 11 ++++++++---
 gcc/testsuite/g++.dg/init/pr64527.C | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/init/pr64527.C

diff --git a/gcc/gimplify.c b/gcc/gimplify.c
index ff0a225..29d3f4d 100644
--- a/gcc/gimplify.c
+++ b/gcc/gimplify.c
@@ -3994,6 +3994,9 @@ gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
 					pre_p, post_p, &preeval_data);
 	  }

+	bool ctor_has_side_effects_p
+	  = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
+
 	if (cleared)
 	  {
 	    /* Zap the CONSTRUCTOR element list, which simplifies this case.
@@ -4006,9 +4009,11 @@ gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
 	  }

 	/* If we have not block cleared the object, or if there are nonzero
-	   elements in the constructor, add assignments to the individual
-	   scalar fields of the object.  */
-	if (!cleared || num_nonzero_elements > 0)
+	   elements in the constructor, or if the constructor has side effects,
+	   add assignments to the individual scalar fields of the object.  */
+	if (!cleared
+	    || num_nonzero_elements > 0
+	    || ctor_has_side_effects_p)
 	  gimplify_init_ctor_eval (object, elts, pre_p, cleared);

 	*expr_p = NULL_TREE;
diff --git a/gcc/testsuite/g++.dg/init/pr64527.C b/gcc/testsuite/g++.dg/init/pr64527.C
new file mode 100644
index 0000000..36b8214
--- /dev/null
+++ b/gcc/testsuite/g++.dg/init/pr64527.C
@@ -0,0 +1,21 @@
+// { dg-do run { target c++11 } }
+
+static int g;
+
+struct A {
+  A() { g = 1; }
+};
+
+struct accessor {
+  A a;
+  int x;
+};
+
+int
+main (void)
+{
+  (void) accessor{};
+
+  if (g != 1)
+    __builtin_abort ();
+}
--
2.4.0.rc2


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