This is the mail archive of the 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]

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

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

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?

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

	* 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

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;
+main (void)
+  (void) accessor{};
+  if (g != 1)
+    __builtin_abort ();

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