[patch] PR c/19333

Steven Bosscher stevenb@suse.de
Mon Jan 31 07:15:00 GMT 2005


Hi,

The attached patch is my shot at PR19333.  It is an ice-on-invalid
code, where we would build arrays of elements with an incomplete type.
The best discussion about the bug is in the thread with this message:
http://gcc.gnu.org/ml/gcc-patches/2005-01/msg00948.html.

Bootstrapped and tested on {i686,x86_64,ia64,ppc,ppc64}-suse-linux-gnu.
OK?

Gr.
Steven

	PR c/19333
	* c-decl.c (start_decl): Do not warn about arrays of elements with
	an incomplete type here.
	(grokdeclarator): Do it here by making a pedwarn an error.
	* c-typeck.c (push_init_level): If there were previous errors with
	the constructor type, do not warn about braces for initializers.
	(process_init_element): Likewise for excess initializer elements.

testsuite/
	PR
	* testsuite/gcc.c-torture/compile/20011130-1.c: Reorder to make
	the test case valid.
	* testsuite/gcc.dg/20030815-1.c: Remove invalid tests.
	* testsuite/gcc.dg/array-7.c: Adjust expected result.
	* testsuite/gcc.dg/pr18596-3.c: Likewise.
	* testsuite/gcc.dg/noncompile/20000901-1.c: Likewise.
	* testsuite/gcc.dg/noncompile/init-2.c: Likewise.
	* testsuite/gcc.dg/noncompile/init-4.c: Likewise.

Index: c-decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-decl.c,v
retrieving revision 1.628
diff -u -3 -p -r1.628 c-decl.c
--- c-decl.c	29 Jan 2005 16:12:37 -0000	1.628
+++ c-decl.c	30 Jan 2005 15:58:30 -0000
@@ -3039,11 +3039,6 @@ start_decl (struct c_declarator *declara
 	    error ("variable %qD has initializer but incomplete type", decl);
 	    initialized = 0;
 	  }
-	else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
-	  {
-	    error ("elements of array %qD have incomplete type", decl);
-	    initialized = 0;
-	  }
 	else if (C_DECL_VARIABLE_SIZE (decl))
 	  {
 	    /* Although C99 is unclear about whether incomplete arrays
@@ -4148,11 +4143,14 @@ grokdeclarator (const struct c_declarato
 		itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
 	      }
 
-	    /* If pedantic, complain about arrays of incomplete types.  */
-	    if (pedantic && !COMPLETE_TYPE_P (type))
-	      pedwarn ("array type has incomplete element type");
-
-	    type = build_array_type (type, itype);
+	     /* Complain about arrays of incomplete types.  */
+	    if (!COMPLETE_TYPE_P (type))
+	      {
+		error ("array type has incomplete element type");
+	        type = error_mark_node;
+	      }
+	    else
+	      type = build_array_type (type, itype);
 
 	    if (size_varies)
 	      C_TYPE_VARIABLE_SIZE (type) = 1;
Index: c-typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.415
diff -u -3 -p -r1.415 c-typeck.c
--- c-typeck.c	29 Jan 2005 16:12:38 -0000	1.415
+++ c-typeck.c	30 Jan 2005 15:58:30 -0000
@@ -4878,7 +4878,8 @@ push_init_level (int implicit)
     }
   else
     {
-      warning_init ("braces around scalar initializer");
+      if (constructor_type != error_mark_node)
+	warning_init ("braces around scalar initializer");
       constructor_fields = constructor_type;
       constructor_unfilled_fields = constructor_type;
     }
@@ -6240,7 +6241,8 @@ process_init_element (struct c_expr valu
 
       /* Handle the sole element allowed in a braced initializer
 	 for a scalar variable.  */
-      else if (constructor_fields == 0)
+      else if (constructor_type != error_mark_node
+	       && constructor_fields == 0)
 	{
 	  pedwarn_init ("excess elements in scalar initializer");
 	  break;
Index: testsuite/gcc.c-torture/compile/20011130-1.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/gcc.c-torture/compile/20011130-1.c,v
retrieving revision 1.1
diff -u -3 -p -r1.1 20011130-1.c
--- testsuite/gcc.c-torture/compile/20011130-1.c	5 Dec 2001 14:11:14 -0000	1.1
+++ testsuite/gcc.c-torture/compile/20011130-1.c	30 Jan 2005 15:58:32 -0000
@@ -1,5 +1,5 @@
-extern struct S x[];
 struct S { int i; };
+extern struct S x[];
 char *bar (const struct S *);
 void foo (void)
 {
Index: testsuite/gcc.dg/20030815-1.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/gcc.dg/20030815-1.c,v
retrieving revision 1.1
diff -u -3 -p -r1.1 20030815-1.c
--- testsuite/gcc.dg/20030815-1.c	23 Aug 2003 22:18:54 -0000	1.1
+++ testsuite/gcc.dg/20030815-1.c	30 Jan 2005 15:58:32 -0000
@@ -4,10 +4,6 @@
 /* { dg-do compile } */
 /* { dg-options "" } */
 
-typedef struct a A[1];
-typedef struct b B[1];
-typedef struct c C[1];
-typedef struct d D[1];
 typedef struct a E;
 typedef struct b F;
 typedef struct c G;
@@ -16,10 +12,6 @@ struct a { int a; };
 struct c { int c; };
 struct d { int d; };
 struct b { int b; };
-int sa = sizeof (A);
-int sb = sizeof (B);
-int sc = sizeof (C);
-int sd = sizeof (D);
 int se = sizeof (E);
 int sf = sizeof (F);
 int sg = sizeof (G);
Index: testsuite/gcc.dg/array-7.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/gcc.dg/array-7.c,v
retrieving revision 1.1
diff -u -3 -p -r1.1 array-7.c
--- testsuite/gcc.dg/array-7.c	3 Aug 2004 14:19:39 -0000	1.1
+++ testsuite/gcc.dg/array-7.c	30 Jan 2005 15:58:32 -0000
@@ -11,4 +11,4 @@ f (void)
   struct foo { int a; int b; };
 }
 
-struct foo array[5]; /* { dg-error "storage size" } */
+struct foo array[5]; /* { dg-error "array type has incomplete element type" } */
Index: testsuite/gcc.dg/pr18596-3.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/gcc.dg/pr18596-3.c,v
retrieving revision 1.1
diff -u -3 -p -r1.1 pr18596-3.c
--- testsuite/gcc.dg/pr18596-3.c	21 Dec 2004 17:01:08 -0000	1.1
+++ testsuite/gcc.dg/pr18596-3.c	30 Jan 2005 15:58:32 -0000
@@ -10,5 +10,3 @@ int foo ()
   static int j () = /* { dg-error "invalid storage class" } */
 	{ 0, 0.0 };
 }
-/* { dg-warning "excess elements" "" { target *-*-* } 11 } */
-/* { dg-warning "near initialization" "" { target *-*-* } 11 } */
Index: testsuite/gcc.dg/noncompile/20000901-1.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/gcc.dg/noncompile/20000901-1.c,v
retrieving revision 1.1
diff -u -3 -p -r1.1 20000901-1.c
--- testsuite/gcc.dg/noncompile/20000901-1.c	9 Jan 2002 21:34:31 -0000	1.1
+++ testsuite/gcc.dg/noncompile/20000901-1.c	30 Jan 2005 15:58:32 -0000
@@ -1 +1 @@
-struct foo bar[] = { {"baz"} }; /* { dg-error "have incomplete type|excess elements|near|assumed|storage size" } */
+struct foo bar[] = { {"baz"} }; /* { dg-error "array type has incomplete element type" } */
Index: testsuite/gcc.dg/noncompile/init-2.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/gcc.dg/noncompile/init-2.c,v
retrieving revision 1.2
diff -u -3 -p -r1.2 init-2.c
--- testsuite/gcc.dg/noncompile/init-2.c	28 Oct 2000 00:18:39 -0000	1.2
+++ testsuite/gcc.dg/noncompile/init-2.c	30 Jan 2005 15:58:32 -0000
@@ -1 +1 @@
-int d[][] = { {1}, {2}, {3} };	/* { dg-error "incomplete type|storage size|one element" } */
+int d[][] = { {1}, {2}, {3} };	/* { dg-error "incomplete element type" } */
Index: testsuite/gcc.dg/noncompile/init-4.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/gcc.dg/noncompile/init-4.c,v
retrieving revision 1.2
diff -u -3 -p -r1.2 init-4.c
--- testsuite/gcc.dg/noncompile/init-4.c	9 Jul 2004 21:30:35 -0000	1.2
+++ testsuite/gcc.dg/noncompile/init-4.c	30 Jan 2005 15:58:32 -0000
@@ -1,2 +1 @@
-struct a { char *b; } c[D] /* { dg-error "undeclared" } */
-  = { { "" } } ;  /* { dg-warning "braces around scalar initializer|near" } */
+struct a { char *b; } c[D]; /* { dg-error "undeclared" } */



More information about the Gcc-patches mailing list