[C PATCH] Fix ICE-on-invalid with enum forward declaration (PR c/70851)

Marek Polacek polacek@redhat.com
Mon May 2 10:27:00 GMT 2016


Here, the problem was that we weren't diagnosing invalid code when an array
dimension was of incomplete enum type.  That led to an ICE in gimplifier.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2016-05-02  Marek Polacek  <polacek@redhat.com>

	PR c/70851
	* c-decl.c (grokdeclarator): Diagnose when array's size has an
	incomplete type.

	* gcc.dg/enum-incomplete-3.c: New test.

diff --git gcc/c/c-decl.c gcc/c/c-decl.c
index 16e4250..7094efc 100644
--- gcc/c/c-decl.c
+++ gcc/c/c-decl.c
@@ -5799,10 +5799,21 @@ grokdeclarator (const struct c_declarator *declarator,
 		  {
 		    if (name)
 		      error_at (loc, "size of array %qE has non-integer type",
-			  	name);
+				name);
 		    else
 		      error_at (loc,
-			  	"size of unnamed array has non-integer type");
+				"size of unnamed array has non-integer type");
+		    size = integer_one_node;
+		  }
+		/* This can happen with enum forward declaration.  */
+		else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
+		  {
+		    if (name)
+		      error_at (loc, "size of array %qE has incomplete type",
+				name);
+		    else
+		      error_at (loc, "size of unnamed array has incomplete "
+				"type");
 		    size = integer_one_node;
 		  }
 
diff --git gcc/testsuite/gcc.dg/enum-incomplete-3.c gcc/testsuite/gcc.dg/enum-incomplete-3.c
index e69de29..db1138b 100644
--- gcc/testsuite/gcc.dg/enum-incomplete-3.c
+++ gcc/testsuite/gcc.dg/enum-incomplete-3.c
@@ -0,0 +1,20 @@
+/* PR c/70851 */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+enum E e; /* { dg-error "storage size" } */
+
+void bar (int [e]); /* { dg-error "size of unnamed array has incomplete type" } */
+void bar2 (int [][e]); /* { dg-error "size of unnamed array has incomplete type" } */
+
+void
+foo (void)
+{
+  int a1[e]; /* { dg-error "size of array .a1. has incomplete type" } */
+  int a2[e][3]; /* { dg-error "size of array .a2. has incomplete type" } */
+
+  struct S
+  {
+    int a3[e]; /* { dg-error "size of array .a3. has incomplete type" } */
+  };
+}

	Marek



More information about the Gcc-patches mailing list