From: jsm28 Date: Tue, 28 Sep 2004 19:35:26 +0000 (+0000) Subject: PR c/16409 X-Git-Url: http://git.sourceforge.jp/view?p=pf3gnuchains%2Fgcc-fork.git;a=commitdiff_plain;h=5cbd179d875444116e92838078534bcf689ccff3 PR c/16409 * c-decl.c (start_decl): Check for initializing incomplete array of VLAs. (build_compound_literal): Check for TYPE being error_mark_node. * c-parse.in (primary): Check for VLA compound literals. testsuite: * gcc.dg/vla-init-2.c, gcc.dg/vla-init-3.c, gcc.dg/vla-init-4.c, gcc.dg/vla-init-5.c: New tests. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@88248 138bc75d-0d04-0410-961f-82ee72b054a4 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 1b3287528fc..2847a266704 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,11 @@ +2004-09-28 Joseph S. Myers + + PR c/16409 + * c-decl.c (start_decl): Check for initializing incomplete array + of VLAs. + (build_compound_literal): Check for TYPE being error_mark_node. + * c-parse.in (primary): Check for VLA compound literals. + 2004-09-28 Diego Novillo * tree-ssa-live.c (calculate_live_on_entry): Fix warnings diff --git a/gcc/c-decl.c b/gcc/c-decl.c index 3a2f5763b13..413787633dc 100644 --- a/gcc/c-decl.c +++ b/gcc/c-decl.c @@ -2986,6 +2986,15 @@ start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs, 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 + of VLAs themselves count as VLAs, it does not make + sense to permit them to be initialized given that + ordinary VLAs may not be initialized. */ + error ("variable-sized object may not be initialized"); + initialized = 0; + } } if (initialized) @@ -3416,9 +3425,14 @@ build_compound_literal (tree type, tree init) /* We do not use start_decl here because we have a type, not a declarator; and do not use finish_decl because the decl should be stored inside the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */ - tree decl = build_decl (VAR_DECL, NULL_TREE, type); + tree decl; tree complit; tree stmt; + + if (type == error_mark_node) + return error_mark_node; + + decl = build_decl (VAR_DECL, NULL_TREE, type); DECL_EXTERNAL (decl) = 0; TREE_PUBLIC (decl) = 0; TREE_STATIC (decl) = (current_scope == file_scope); diff --git a/gcc/c-parse.in b/gcc/c-parse.in index 6fcf66739f5..87dfc100748 100644 --- a/gcc/c-parse.in +++ b/gcc/c-parse.in @@ -677,6 +677,11 @@ primary: | '(' typename ')' '{' { start_init (NULL_TREE, NULL, 0); $$ = groktypename ($2); + if (C_TYPE_VARIABLE_SIZE ($$)) + { + error ("compound literal has variable size"); + $$ = error_mark_node; + } really_start_incremental_init ($$); } initlist_maybe_comma '}' %prec UNARY { struct c_expr init = pop_init_level (0); diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 29293815d8c..1b59e73d1ac 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2004-09-28 Joseph S. Myers + + PR c/16409 + * gcc.dg/vla-init-2.c, gcc.dg/vla-init-3.c, gcc.dg/vla-init-4.c, + gcc.dg/vla-init-5.c: New tests. + 2004-09-27 Joseph S. Myers PR c/13804 diff --git a/gcc/testsuite/gcc.dg/vla-init-2.c b/gcc/testsuite/gcc.dg/vla-init-2.c new file mode 100644 index 00000000000..395ddeb1607 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vla-init-2.c @@ -0,0 +1,10 @@ +/* Arrays of unknown size with element type a VLA type should not be + initialized (C99 isn't clear about whether such arrays are VLAs, + but this is the only reasonable interpretation). Bug 16409, first + testcase. */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +const int i = 1; +void foo() { char a[][i] = {""}; } /* { dg-error "error: variable-sized object may not be initialized" } */ +/* { dg-error "array size missing in 'a'" "extra error" { target *-*-* } 9 } */ diff --git a/gcc/testsuite/gcc.dg/vla-init-3.c b/gcc/testsuite/gcc.dg/vla-init-3.c new file mode 100644 index 00000000000..72bfe2095fd --- /dev/null +++ b/gcc/testsuite/gcc.dg/vla-init-3.c @@ -0,0 +1,9 @@ +/* Arrays of unknown size with element type a VLA type should not be + initialized (C99 isn't clear about whether such arrays are VLAs, + but this is the only reasonable interpretation). Bug 16409, second + testcase. */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +void foo(int i) { char a[][i] = {""}; } /* { dg-error "error: variable-sized object may not be initialized" } */ +/* { dg-error "array size missing in 'a'" "extra error" { target *-*-* } 8 } */ diff --git a/gcc/testsuite/gcc.dg/vla-init-4.c b/gcc/testsuite/gcc.dg/vla-init-4.c new file mode 100644 index 00000000000..0f0e332377d --- /dev/null +++ b/gcc/testsuite/gcc.dg/vla-init-4.c @@ -0,0 +1,7 @@ +/* Test for ICE on VLA compound literal. */ +/* Origin: Joseph Myers */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +const int i = 1; +void foo() { char *p = (char [i]){ "" }; } /* { dg-error "error: compound literal has variable size" } */ diff --git a/gcc/testsuite/gcc.dg/vla-init-5.c b/gcc/testsuite/gcc.dg/vla-init-5.c new file mode 100644 index 00000000000..b1455dbd3a0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vla-init-5.c @@ -0,0 +1,7 @@ +/* Test for ICE on incomplete-array-of-VLA compound literal. */ +/* Origin: Joseph Myers */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +const int i = 1; +void foo() { void *p = (char [][i]){ "" }; } /* { dg-error "error: compound literal has variable size" } */