diff mbox

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

Message ID 1429146015-25249-1-git-send-email-patrick@parcs.ath.cx
State New
Headers show

Commit Message

Patrick Palka April 16, 2015, 1 a.m. UTC
gimplify_init_constructor() only attempts to gimplify a CONSTRUCTOR's
elts under two conditions: if the object has not been zero-initialized,
or if categorize_ctor_elements() detected more than one "nonzero" elt in
the CONSTRUCTOR.  With the PR's testcase, both of these conditions are
false: the gimplifier decides to zero-initialize the object (apparently
because one of the object's field was not initialized), and
categorize_ctor_elements() rightly does not count an empty struct field
as a non-zero component of the CONSTRUCTOR (so num_nonzero_elements
remains 0).  So we end up not emitting the call to field "A a"'s
constructor.

In order to fix this issue, I think we should unconditionally call
gimplify_init_ctor_eval even if we have cleared the object and even if
the object has no nonzero elements.  Because even so, one of the
constructor's elts may be side-effecting and in such a case we would
still want to emit it.  The function already has logic to skip over
redundant zero-initializations so we should be safe in that regard.

Bootstrap and regtesting (including fortran + ada) on x86_64-unknown-linux-gnu
in progress.  OK for trunk if it succeeds?

gcc/
	PR c++/64527
	* gcc/gimplify.c (gimplify_init_constructor): Unconditionally
	call gimplify_init_ctor_eval.

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

Comments

Jason Merrill April 16, 2015, 2:32 p.m. UTC | #1
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?

Jason
diff mbox

Patch

diff --git a/gcc/gimplify.c b/gcc/gimplify.c
index ff0a225..5158243 100644
--- a/gcc/gimplify.c
+++ b/gcc/gimplify.c
@@ -4005,11 +4005,7 @@  gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
 	    gimplify_stmt (expr_p, pre_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)
-	  gimplify_init_ctor_eval (object, elts, pre_p, cleared);
+	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 ();
+}