OSDN Git Service

PR c/16409
authorjsm28 <jsm28@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 28 Sep 2004 19:35:26 +0000 (19:35 +0000)
committerjsm28 <jsm28@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 28 Sep 2004 19:35:26 +0000 (19:35 +0000)
* 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

gcc/ChangeLog
gcc/c-decl.c
gcc/c-parse.in
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/vla-init-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/vla-init-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/vla-init-4.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/vla-init-5.c [new file with mode: 0644]

index 1b32875..2847a26 100644 (file)
@@ -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
 2004-09-28  Diego Novillo  <dnovillo@redhat.com>
 
        * tree-ssa-live.c (calculate_live_on_entry): Fix warnings
index 3a2f576..4137876 100644 (file)
@@ -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;
          }
            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)
       }
 
   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.  */
   /* 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;
   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);
   DECL_EXTERNAL (decl) = 0;
   TREE_PUBLIC (decl) = 0;
   TREE_STATIC (decl) = (current_scope == file_scope);
index 6fcf667..87dfc10 100644 (file)
@@ -677,6 +677,11 @@ primary:
        | '(' typename ')' '{'
                { start_init (NULL_TREE, NULL, 0);
                  $<ttype>$ = groktypename ($2);
        | '(' 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);
                  really_start_incremental_init ($<ttype>$); }
          initlist_maybe_comma '}'  %prec UNARY
                { struct c_expr init = pop_init_level (0);
index 2929381..1b59e73 100644 (file)
@@ -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
 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 (file)
index 0000000..395ddeb
--- /dev/null
@@ -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 (file)
index 0000000..72bfe20
--- /dev/null
@@ -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 (file)
index 0000000..0f0e332
--- /dev/null
@@ -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 (file)
index 0000000..b1455db
--- /dev/null
@@ -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" } */