summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjsm28 <jsm28@138bc75d-0d04-0410-961f-82ee72b054a4>2004-09-28 19:35:26 +0000
committerjsm28 <jsm28@138bc75d-0d04-0410-961f-82ee72b054a4>2004-09-28 19:35:26 +0000
commit5cbd179d875444116e92838078534bcf689ccff3 (patch)
tree5fc1cb98a75ae0a4738fa2fd992b53aee46095a7
parent0ecedb6fdf659f79d6d35c973874a5c502982ed3 (diff)
downloadgcc-5cbd179d875444116e92838078534bcf689ccff3.tar.gz
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
-rw-r--r--gcc/ChangeLog8
-rw-r--r--gcc/c-decl.c16
-rw-r--r--gcc/c-parse.in5
-rw-r--r--gcc/testsuite/ChangeLog6
-rw-r--r--gcc/testsuite/gcc.dg/vla-init-2.c10
-rw-r--r--gcc/testsuite/gcc.dg/vla-init-3.c9
-rw-r--r--gcc/testsuite/gcc.dg/vla-init-4.c7
-rw-r--r--gcc/testsuite/gcc.dg/vla-init-5.c7
8 files changed, 67 insertions, 1 deletions
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 <jsm@polyomino.org.uk>
+
+ 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 <dnovillo@redhat.com>
* 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);
$<ttype>$ = groktypename ($2);
+ if (C_TYPE_VARIABLE_SIZE ($<ttype>$))
+ {
+ error ("compound literal has variable size");
+ $<ttype>$ = error_mark_node;
+ }
really_start_incremental_init ($<ttype>$); }
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 <jsm@polyomino.org.uk>
+
+ 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 <jsm@polyomino.org.uk>
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 <jsm@polyomino.org.uk> */
+/* { 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 <jsm@polyomino.org.uk> */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+const int i = 1;
+void foo() { void *p = (char [][i]){ "" }; } /* { dg-error "error: compound literal has variable size" } */