From 2b6da65c33c24de56b95b42c5dd2c771c425ef7c Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Fri, 24 Apr 2009 18:24:32 +0100 Subject: [PATCH] c-typeck.c (set_init_index): Allow array designators that are not integer constant expressions with a... * c-typeck.c (set_init_index): Allow array designators that are not integer constant expressions with a pedwarn if pedantic. testsuite: * gcc.dg/array-const-1.c, gcc.dg/array-const-2.c, gcc.dg/array-const-3.c: New tests. From-SVN: r146741 --- gcc/ChangeLog | 5 +++++ gcc/c-typeck.c | 18 ++++++++++++++++++ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/array-const-1.c | 8 ++++++++ gcc/testsuite/gcc.dg/array-const-2.c | 9 +++++++++ gcc/testsuite/gcc.dg/array-const-3.c | 9 +++++++++ 6 files changed, 54 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/array-const-1.c create mode 100644 gcc/testsuite/gcc.dg/array-const-2.c create mode 100644 gcc/testsuite/gcc.dg/array-const-3.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index a48db46dcd5d..51d848d755b9 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2009-04-24 Joseph Myers + + * c-typeck.c (set_init_index): Allow array designators that are + not integer constant expressions with a pedwarn if pedantic. + 2009-04-24 Bernd Schmidt * simplify-rtx.c (simplify_binary_operation_1, case AND): Result is diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index c7ea3284ac03..1cfab1f14f20 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -6421,6 +6421,24 @@ set_init_index (tree first, tree last) return; } + 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) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index b4ec0dd5cac8..083a7e3efa55 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2009-04-24 Joseph Myers + + * gcc.dg/array-const-1.c, gcc.dg/array-const-2.c, + gcc.dg/array-const-3.c: New tests. + 2009-04-24 Ian Lance Taylor * gcc.dg/Wcxx-compat-4.c: New testcase. diff --git a/gcc/testsuite/gcc.dg/array-const-1.c b/gcc/testsuite/gcc.dg/array-const-1.c new file mode 100644 index 000000000000..0e0c46224cf0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/array-const-1.c @@ -0,0 +1,8 @@ +/* Test for array designators not integer constant expressions but + folding to integer constants (used in Linux kernel, + ). */ +/* { dg-do compile } */ +/* { dg-options "-std=gnu99" } */ + +extern int i; +int a[] = { [1 ? 1 : i] = 0 }; diff --git a/gcc/testsuite/gcc.dg/array-const-2.c b/gcc/testsuite/gcc.dg/array-const-2.c new file mode 100644 index 000000000000..f6e2bd5bbfa0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/array-const-2.c @@ -0,0 +1,9 @@ +/* Test for array designators not integer constant expressions but + folding to integer constants (used in Linux kernel, + ). */ +/* { 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 } */ diff --git a/gcc/testsuite/gcc.dg/array-const-3.c b/gcc/testsuite/gcc.dg/array-const-3.c new file mode 100644 index 000000000000..5bb215cbd0fc --- /dev/null +++ b/gcc/testsuite/gcc.dg/array-const-3.c @@ -0,0 +1,9 @@ +/* Test for array designators not integer constant expressions but + folding to integer constants (used in Linux kernel, + ). */ +/* { 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 } */