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++/51458 (accepts-invalid designated initializers)


We were ignoring designators in cases where they aren't meaningful, but we ought to reject them instead.

Tested x86_64-pc-linux-gnu, applying to trunk.
commit b719497ead3a8be29d1842fa8715409c82fd1bd3
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Dec 14 19:02:00 2011 -0500

    	PR c++/51458
    	* decl.c (has_designator_problem): New.
    	(reshape_init_r): Check for improper use of
    	designated initializers.

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 480d211..b800664 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -5122,6 +5122,24 @@ reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
   return new_init;
 }
 
+/* Subroutine of reshape_init_r.  We're in a context where C99 initializer
+   designators are not valid; either complain or return true to indicate
+   that reshape_init_r should return error_mark_node.  */
+
+static bool
+has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
+{
+  if (d->cur->index)
+    {
+      if (complain & tf_error)
+	error ("C99 designator %qE outside aggregate initializer",
+	       d->cur->index);
+      else
+	return true;
+    }
+  return false;
+}
+
 /* Subroutine of reshape_init, which processes a single initializer (part of
    a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
    iterator within the CONSTRUCTOR which points to the initializer to process.
@@ -5137,6 +5155,10 @@ reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
   if (error_operand_p (init))
     return error_mark_node;
 
+  if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
+      && has_designator_problem (d, complain))
+    return error_mark_node;
+
   if (TREE_CODE (type) == COMPLEX_TYPE)
     {
       /* A complex type can be initialized from one or two initializers,
@@ -5157,6 +5179,8 @@ reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
 	  VEC(constructor_elt, gc) *v = 0;
 	  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
 	  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
+	  if (has_designator_problem (d, complain))
+	    return error_mark_node;
 	  d->cur++;
 	  init = build_constructor (init_list_type_node, v);
 	}
@@ -5236,6 +5260,8 @@ reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
 	 array types (one value per array element).  */
       if (TREE_CODE (str_init) == STRING_CST)
 	{
+	  if (has_designator_problem (d, complain))
+	    return error_mark_node;
 	  d->cur++;
 	  return str_init;
 	}
diff --git a/gcc/testsuite/g++.dg/ext/desig4.C b/gcc/testsuite/g++.dg/ext/desig4.C
new file mode 100644
index 0000000..48d629a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/desig4.C
@@ -0,0 +1,10 @@
+// PR c++/51458
+// { dg-options "" }
+
+char g[] = { [7] = "abcd" };	     // { dg-error "designator" }
+int a = { .foo = 6 };		     // { dg-error "designator" }
+int b = { [0] = 1 };		     // { dg-error "designator" }
+_Complex float c = { .foo = 0,  1 }; // { dg-error "designator" }
+_Complex float d = { [0] = 0,  1 };  // { dg-error "designator" }
+_Complex float e = { 0, .foo = 1 };  // { dg-error "designator" }
+_Complex float f = { 0, [0] = 1 };   // { dg-error "designator" }

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