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]

C++ PATCH for c++/57402 (ICE with VLA initialization)


build_vec_init wasn't handling VLAs very well.

Tested x86_64-pc-linux-gnu, applying to trunk.
commit 8c0d85d60fa6e49ce4c8ac0eae88b77b4edd98fa
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Jul 9 18:38:13 2013 -0400

    	PR c++/57402
    	* init.c (build_vec_init): Don't take shortcuts when initializing
    	a VLA.

diff --git a/gcc/cp/init.c b/gcc/cp/init.c
index 58cec9e..2096230 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -3333,6 +3333,7 @@ build_vec_init (tree base, tree maxindex, tree init,
 
   if (init
       && TREE_CODE (atype) == ARRAY_TYPE
+      && TREE_CONSTANT (maxindex)
       && (from_array == 2
 	  ? (!CLASS_TYPE_P (inner_elt_type)
 	     || !TYPE_HAS_COMPLEX_COPY_ASSIGN (inner_elt_type))
@@ -3453,6 +3454,7 @@ build_vec_init (tree base, tree maxindex, tree init,
       tree field, elt;
       /* Should we try to create a constant initializer?  */
       bool try_const = (TREE_CODE (atype) == ARRAY_TYPE
+			&& TREE_CONSTANT (maxindex)
 			&& (literal_type_p (inner_elt_type)
 			    || TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type)));
       /* If the constructor already has the array type, it's been through
@@ -3562,6 +3564,8 @@ build_vec_init (tree base, tree maxindex, tree init,
 
       /* Clear out INIT so that we don't get confused below.  */
       init = NULL_TREE;
+      /* Any elements without explicit initializers get {}.  */
+      explicit_value_init_p = true;
     }
   else if (from_array)
     {
diff --git a/gcc/testsuite/g++.dg/cpp1y/vla10.C b/gcc/testsuite/g++.dg/cpp1y/vla10.C
new file mode 100644
index 0000000..1c67290
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/vla10.C
@@ -0,0 +1,24 @@
+// PR c++/57402
+// { dg-options "-std=c++1y -pedantic-errors" }
+
+int i = 2;
+
+int main()
+{
+  {
+    int a[i];
+    a[1] = 0xbeef;
+  }
+  {
+    int a[i] = { 1 };
+    if (a[1] != 0)
+      __builtin_abort ();
+    a[1] = 0xbeef;
+  }
+  {
+    int a[i] = { };
+    if (a[1] != 0)
+      __builtin_abort ();
+    a[1] = 0xbeef;
+  }
+}

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