GCC 4.5: "nonconstant array index in initializer" error

Joseph S. Myers joseph@codesourcery.com
Fri Apr 24 17:37:00 GMT 2009


On Thu, 23 Apr 2009, Denis Onischenko wrote:

> The minimal code example is following:
> 
> 
> extern unsigned int __invalid_size_argument;
> #define TYPECHECK(t)    ( sizeof(t) == sizeof(t[1]) ?  sizeof(t) :
> __invalid_size_argument )
> 
> static int arr[] = {
> 	[TYPECHECK(int)] = 0,
> };
> 
> int main()
> {
>   return 0;
> }

Given this usage in the Linux kernel I've applied this patch to make
this case into a pedwarn-if-pedantic, as previously done for case
labels.  Bootstrapped with no regressions on i686-pc-linux-gnu.

2009-04-24  Joseph Myers  <joseph@codesourcery.com>

	* c-typeck.c (set_init_index): Allow array designators that are
	not integer constant expressions with a pedwarn if pedantic.

testsuite:
2009-04-24  Joseph Myers  <joseph@codesourcery.com>

	* gcc.dg/array-const-1.c, gcc.dg/array-const-2.c,
	gcc.dg/array-const-3.c: New tests.

Index: gcc/testsuite/gcc.dg/array-const-2.c
===================================================================
--- gcc/testsuite/gcc.dg/array-const-2.c	(revision 0)
+++ gcc/testsuite/gcc.dg/array-const-2.c	(revision 0)
@@ -0,0 +1,9 @@
+/* Test for array designators not integer constant expressions but
+   folding to integer constants (used in Linux kernel,
+   <http://gcc.gnu.org/ml/gcc/2009-04/msg00611.html>).  */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu99 -pedantic" } */
+
+extern int i;
+int a[] = { [1 ? 1 : i] = 0 }; /* { dg-warning "array index in initializer is not an integer constant expression" } */
+/* { dg-warning "near initialization" "near init" { target *-*-* } 8 } */
Index: gcc/testsuite/gcc.dg/array-const-1.c
===================================================================
--- gcc/testsuite/gcc.dg/array-const-1.c	(revision 0)
+++ gcc/testsuite/gcc.dg/array-const-1.c	(revision 0)
@@ -0,0 +1,8 @@
+/* Test for array designators not integer constant expressions but
+   folding to integer constants (used in Linux kernel,
+   <http://gcc.gnu.org/ml/gcc/2009-04/msg00611.html>).  */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu99" } */
+
+extern int i;
+int a[] = { [1 ? 1 : i] = 0 };
Index: gcc/testsuite/gcc.dg/array-const-3.c
===================================================================
--- gcc/testsuite/gcc.dg/array-const-3.c	(revision 0)
+++ gcc/testsuite/gcc.dg/array-const-3.c	(revision 0)
@@ -0,0 +1,9 @@
+/* Test for array designators not integer constant expressions but
+   folding to integer constants (used in Linux kernel,
+   <http://gcc.gnu.org/ml/gcc/2009-04/msg00611.html>).  */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu99 -pedantic-errors" } */
+
+extern int i;
+int a[] = { [1 ? 1 : i] = 0 }; /* { dg-error "array index in initializer is not an integer constant expression" } */
+/* { dg-error "near initialization" "near init" { target *-*-* } 8 } */
Index: gcc/c-typeck.c
===================================================================
--- gcc/c-typeck.c	(revision 146679)
+++ gcc/c-typeck.c	(working copy)
@@ -6403,6 +6403,24 @@ set_init_index (tree first, tree last)
     }
 
   if (TREE_CODE (first) != INTEGER_CST)
+    {
+      first = c_fully_fold (first, false, NULL);
+      if (TREE_CODE (first) == INTEGER_CST)
+	pedwarn_init (input_location, OPT_pedantic,
+		      "array index in initializer is not "
+		      "an integer constant expression");
+    }
+
+  if (last && TREE_CODE (last) != INTEGER_CST)
+    {
+      last = c_fully_fold (last, false, NULL);
+      if (TREE_CODE (last) == INTEGER_CST)
+	pedwarn_init (input_location, OPT_pedantic,
+		      "array index in initializer is not "
+		      "an integer constant expression");
+    }
+
+  if (TREE_CODE (first) != INTEGER_CST)
     error_init ("nonconstant array index in initializer");
   else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
     error_init ("nonconstant array index in initializer");

-- 
Joseph S. Myers
joseph@codesourcery.com



More information about the Gcc-patches mailing list