From 6ccb2a4a4bbd43dfbcf9ac56f0f0f7e614874f0c Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Wed, 1 Apr 2009 15:41:06 +0100 Subject: [PATCH] re PR c/39605 ("error: variable-size type declared outside of any function" is issued twice) PR c/39605 * c-decl.c (grokdeclarator): Pedwarn for file-scope array declarator whose size is not an integer constant expression but folds to an integer constant, then treat it as a constant subsequently. testsuite: * gcc.dg/vla-17.c, gcc.dg/vla-18.c: New tests. * gcc.dg/pr25682.c: Update expected diagnostics. From-SVN: r145405 --- gcc/ChangeLog | 8 ++++++++ gcc/c-decl.c | 12 +++++++++++- gcc/testsuite/ChangeLog | 6 ++++++ gcc/testsuite/gcc.dg/pr25682.c | 8 ++++---- gcc/testsuite/gcc.dg/vla-17.c | 9 +++++++++ gcc/testsuite/gcc.dg/vla-18.c | 9 +++++++++ 6 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/vla-17.c create mode 100644 gcc/testsuite/gcc.dg/vla-18.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index f4b1e568f5a..1eebf64a509 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,11 @@ +2009-04-01 Joseph Myers + + PR c/39605 + * c-decl.c (grokdeclarator): Pedwarn for file-scope array + declarator whose size is not an integer constant expression but + folds to an integer constant, then treat it as a constant + subsequently. + 2009-04-01 Richard Guenther * fold-const.c (fold_plusminus_mult_expr): Do not fold diff --git a/gcc/c-decl.c b/gcc/c-decl.c index 3c1c2773f20..9202feb4b94 100644 --- a/gcc/c-decl.c +++ b/gcc/c-decl.c @@ -4408,7 +4408,17 @@ grokdeclarator (const struct c_declarator *declarator, not an integer constant expression. */ if (!size_int_const) { - this_size_varies = size_varies = 1; + /* If this is a file scope declaration of an + ordinary identifier, this is invalid code; + diagnosing it here and not subsequently + treating the type as variable-length avoids + more confusing diagnostics later. */ + if ((decl_context == NORMAL || decl_context == FIELD) + && current_scope == file_scope) + pedwarn (input_location, 0, + "variably modified %qs at file scope", name); + else + this_size_varies = size_varies = 1; warn_variable_length_array (orig_name, size); } } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 5462bead4bd..16d93400179 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2009-04-01 Joseph Myers + + PR c/39605 + * gcc.dg/vla-17.c, gcc.dg/vla-18.c: New tests. + * gcc.dg/pr25682.c: Update expected diagnostics. + 2009-04-01 Richard Guenther * gcc.dg/fold-plusmult-2.c: New testcase. diff --git a/gcc/testsuite/gcc.dg/pr25682.c b/gcc/testsuite/gcc.dg/pr25682.c index c99b891f8fa..8c51d82403a 100644 --- a/gcc/testsuite/gcc.dg/pr25682.c +++ b/gcc/testsuite/gcc.dg/pr25682.c @@ -10,10 +10,10 @@ struct S int b; }; -char c[(char *) &((struct S *) 0)->b - (char *) 0]; /* { dg-error "variable-size" } */ -char d[(__SIZE_TYPE__) &((struct S *) 8)->b]; /* { dg-error "variable-size" } */ -char e[sizeof (c) == __builtin_offsetof (struct S, b) ? 1 : -1]; /* { dg-error "variably modified" } */ -char f[sizeof (d) == __builtin_offsetof (struct S, b) + 8 ? 1 : -1]; /* { dg-error "variably modified" } */ +char c[(char *) &((struct S *) 0)->b - (char *) 0]; /* { dg-warning "variably modified" } */ +char d[(__SIZE_TYPE__) &((struct S *) 8)->b]; /* { dg-warning "variably modified" } */ +char e[sizeof (c) == __builtin_offsetof (struct S, b) ? 1 : -1]; +char f[sizeof (d) == __builtin_offsetof (struct S, b) + 8 ? 1 : -1]; extern void bar (char *, char *); diff --git a/gcc/testsuite/gcc.dg/vla-17.c b/gcc/testsuite/gcc.dg/vla-17.c new file mode 100644 index 00000000000..07d39300417 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vla-17.c @@ -0,0 +1,9 @@ +/* Test diagnostics for VLA whose size folds to an integer constant at + file scope. PR 39605. */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +#define FIRST ((void*)0x80) +#define LAST ((void*)0x86) + +static int b[LAST-FIRST]; /* { dg-warning "variably modified 'b' at file scope" } */ diff --git a/gcc/testsuite/gcc.dg/vla-18.c b/gcc/testsuite/gcc.dg/vla-18.c new file mode 100644 index 00000000000..c60069cfd86 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vla-18.c @@ -0,0 +1,9 @@ +/* Test diagnostics for VLA whose size folds to an integer constant at + file scope; the diagnostic should be a pedwarn. PR 39605. */ +/* { dg-do compile } */ +/* { dg-options "-std=c99 -pedantic-errors" } */ + +#define FIRST ((char*)0x80) +#define LAST ((char*)0x86) + +static int b[LAST-FIRST]; /* { dg-error "variably modified 'b' at file scope" } */