public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r9-9455] c++: cxx_eval_vec_init after zero-initialization [PR96282]
@ 2021-04-21 21:09 Patrick Palka
  0 siblings, 0 replies; only message in thread
From: Patrick Palka @ 2021-04-21 21:09 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:fef6ee0790de58f16128a0de87571ba7e04b8320

commit r9-9455-gfef6ee0790de58f16128a0de87571ba7e04b8320
Author: Patrick Palka <ppalka@redhat.com>
Date:   Wed Aug 5 15:05:30 2020 -0400

    c++: cxx_eval_vec_init after zero-initialization [PR96282]
    
    In the first testcase below, expand_aggr_init_1 sets up t's default
    constructor such that the ctor first zero-initializes the entire base b,
    followed by calling b's default constructor, the latter of which just
    default-initializes the array member b::m via a VEC_INIT_EXPR.
    
    So upon constexpr evaluation of this latter VEC_INIT_EXPR, ctx->ctor is
    nonempty due to the prior zero-initialization, and we proceed in
    cxx_eval_vec_init to append new constructor_elts to the end of ctx->ctor
    without first checking if a matching constructor_elt already exists.
    This leads to ctx->ctor having two matching constructor_elts for each
    index.
    
    This patch fixes this issue by truncating a zero-initialized array
    CONSTRUCTOR in cxx_eval_vec_init_1 before we begin appending array
    elements to it.  We propagate its zeroed out state during evaluation by
    clearing CONSTRUCTOR_NO_CLEARING on each new appended aggregate element.
    
    gcc/cp/ChangeLog:
    
            PR c++/96282
            * constexpr.c (cxx_eval_vec_init_1): Truncate ctx->ctor and
            then clear CONSTRUCTOR_NO_CLEARING on each appended element
            initializer if we're initializing a previously zero-initialized
            array object.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/96282
            * g++.dg/cpp0x/constexpr-array26.C: New test.
            * g++.dg/cpp0x/constexpr-array27.C: New test.
    
    Co-authored-by: Jason Merrill <jason@redhat.com>
    (cherry picked from commit d21252de6c81ed236d8981d47b9a57dc4f1c6d57)

Diff:
---
 gcc/cp/constexpr.c                             | 18 +++++++++++++++++-
 gcc/testsuite/g++.dg/cpp0x/constexpr-array26.C | 13 +++++++++++++
 gcc/testsuite/g++.dg/cpp0x/constexpr-array27.C | 13 +++++++++++++
 3 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index afbc92147b2..43929e5c40b 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -3166,6 +3166,18 @@ cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
       pre_init = true;
     }
 
+  bool zeroed_out = false;
+  if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
+    {
+      /* We're initializing an array object that had been zero-initialized
+	 earlier.  Truncate ctx->ctor, and propagate its zeroed state by
+	 clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
+	 initializers we append to it.  */
+      gcc_checking_assert (initializer_zerop (ctx->ctor));
+      zeroed_out = true;
+      vec_safe_truncate (*p, 0);
+    }
+
   tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
 					  overflow_p);
   unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
@@ -3177,7 +3189,11 @@ cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
       constexpr_ctx new_ctx;
       init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
       if (new_ctx.ctor != ctx->ctor)
-	CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
+	{
+	  if (zeroed_out)
+	    CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
+	  CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
+	}
       if (TREE_CODE (elttype) == ARRAY_TYPE)
 	{
 	  /* A multidimensional array; recurse.  */
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-array26.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-array26.C
new file mode 100644
index 00000000000..274f55a88bf
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-array26.C
@@ -0,0 +1,13 @@
+// PR c++/96282
+// { dg-do compile { target c++11 } }
+
+struct e { bool v = true; };
+
+template<int N>
+struct b { e m[N]; };
+
+template<int N>
+struct t : b<N> { constexpr t() : b<N>() {} };
+
+constexpr t<1> h1;
+constexpr t<42> h2;
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-array27.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-array27.C
new file mode 100644
index 00000000000..1234caef31d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-array27.C
@@ -0,0 +1,13 @@
+// PR c++/96282
+// { dg-do compile { target c++11 } }
+
+struct e { bool v = true; e *p = this; };
+
+template<int N>
+struct b { e m[N][N]; };
+
+template<int N>
+struct t : b<N> { constexpr t() : b<N>() {} };
+
+constexpr t<1> h1;
+constexpr t<42> h2;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-04-21 21:09 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-21 21:09 [gcc r9-9455] c++: cxx_eval_vec_init after zero-initialization [PR96282] Patrick Palka

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).