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] Error out on [N] = style designated initializers for classes (PR c++/51229)


Hi!

We already error out on .foo = style designators for arrays, but for
[N] = style designators for classes we'd just ICE.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

We still have a bunch of accepts-invalid, no idea how to solve them,
because reshape_init_r is called often several times with the same d->cur.
IMHO we should error on all of:
struct A { int i; };
int a = { .foo = 6 };
int b = { [0] = 1 };
_Complex float c = { .foo = 0,  1 };
_Complex float d = { [0] = 0,  1 };
_Complex float e = { 0, .foo = 1 };
_Complex float f = { 0, [0] = 1 };
char g[] = { [7] = "abcd" };
The last one is a accepts-invalid even for C.  Jason, do you think you could
look at that on the C++ side, I'll look at the C one?

2011-12-07  Jakub Jelinek  <jakub@redhat.com>

	PR c++/51229
	* decl.c (reshape_init_class): Complain if d->cur->index is
	INTEGER_CST.
	* parser.c (cp_parser_initializer_list): If cp_parser_parse_definitely
	fails, clear designator.

	* g++.dg/ext/desig3.C: New test.

--- gcc/cp/decl.c.jj	2011-12-01 11:45:04.000000000 +0100
+++ gcc/cp/decl.c	2011-12-07 12:51:17.455762864 +0100
@@ -5078,6 +5078,14 @@ reshape_init_class (tree type, reshape_i
       /* Handle designated initializers, as an extension.  */
       if (d->cur->index)
 	{
+	  if (TREE_CODE (d->cur->index) == INTEGER_CST)
+	    {
+	      if (complain & tf_error)
+		error ("%<[%E] =%> used in a GNU-style designated initializer"
+		       " for class %qT", d->cur->index, type);
+	      return error_mark_node;
+	    }
+
 	  field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
 
 	  if (!field || TREE_CODE (field) != FIELD_DECL)
--- gcc/cp/parser.c.jj	2011-11-28 17:58:00.000000000 +0100
+++ gcc/cp/parser.c	2011-12-07 11:31:33.525651711 +0100
@@ -17713,7 +17713,8 @@ cp_parser_initializer_list (cp_parser* p
 	  designator = cp_parser_constant_expression (parser, false, NULL);
 	  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
 	  cp_parser_require (parser, CPP_EQ, RT_EQ);
-	  cp_parser_parse_definitely (parser);
+	  if (!cp_parser_parse_definitely (parser))
+	    designator = NULL_TREE;
 	}
       else
 	designator = NULL_TREE;
--- gcc/testsuite/g++.dg/ext/desig3.C.jj	2011-12-07 12:55:34.894323455 +0100
+++ gcc/testsuite/g++.dg/ext/desig3.C	2011-12-07 12:56:32.172001945 +0100
@@ -0,0 +1,9 @@
+// PR c++/51229
+// { dg-do compile }
+// { dg-options "" }
+
+struct A { int i; };
+
+int a[5] = { .foo = 7 };// { dg-error "used in a GNU-style designated initializer for an array" }
+int b[] = { .foo = 8 };	// { dg-error "used in a GNU-style designated initializer for an array" }
+A c = { [0] = {} };	// { dg-error "used in a GNU-style designated initializer for class" }

	Jakub


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