]> gcc.gnu.org Git - gcc.git/commitdiff
re PR c++/51458 (Accepts invalid designated initializers)
authorJason Merrill <jason@redhat.com>
Thu, 15 Dec 2011 21:55:31 +0000 (16:55 -0500)
committerJason Merrill <jason@gcc.gnu.org>
Thu, 15 Dec 2011 21:55:31 +0000 (16:55 -0500)
PR c++/51458
* decl.c (has_designator_problem): New.
(reshape_init_r): Check for improper use of
designated initializers.

From-SVN: r182391

gcc/cp/ChangeLog
gcc/cp/decl.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/ext/desig4.C [new file with mode: 0644]

index c9ada9a60a45abacdc9d10e2a132978891e0424f..59538669fdeff2a741e7f6f3a923951d96baca2a 100644 (file)
@@ -1,3 +1,10 @@
+2011-12-15  Jason Merrill  <jason@redhat.com>
+
+       PR c++/51458
+       * decl.c (has_designator_problem): New.
+       (reshape_init_r): Check for improper use of
+       designated initializers.
+
 2011-12-15  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/51463
index 123953571700e0b3130952cd963dd282ba14e426..919e2355d88d6b5fd9b2ecd207994e43d77ab819 100644 (file)
@@ -5128,6 +5128,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.
@@ -5143,6 +5161,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,
@@ -5163,6 +5185,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);
        }
@@ -5242,6 +5266,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;
        }
index 8786f421b4f432ded43bf570e5becb5230f79d18..c38491bb737bb7a8fd4251e8a398a2df4c9810c5 100644 (file)
@@ -1,3 +1,8 @@
+2011-12-15  Jason Merrill  <jason@redhat.com>
+
+       PR c++/51458
+       * g++.dg/ext/desig4.C: New.
+
 2011-12-15  Paul Thomas  <pault@gcc.gnu.org>
 
        * gfortran.dg/class_array_3.f03: Remove explicit indexing of
diff --git a/gcc/testsuite/g++.dg/ext/desig4.C b/gcc/testsuite/g++.dg/ext/desig4.C
new file mode 100644 (file)
index 0000000..48d629a
--- /dev/null
@@ -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" }
This page took 0.090736 seconds and 5 git commands to generate.