re PR c/39582 (bad errors for some uses of [*] arrays)
authorJoseph Myers <joseph@codesourcery.com>
Sat, 25 Apr 2009 21:19:09 +0000 (22:19 +0100)
committerJoseph Myers <jsm28@gcc.gnu.org>
Sat, 25 Apr 2009 21:19:09 +0000 (22:19 +0100)
PR c/39582
* c-typeck.c (c_expr_sizeof_type): Create a C_MAYBE_CONST_EXPR
with non-null C_MAYBE_CONST_EXPR_PRE if size of a variable-length
type is an integer constant.

testsuite:
* gcc.dg/vla-20.c: New test.

From-SVN: r146787

gcc/ChangeLog
gcc/c-typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/vla-20.c [new file with mode: 0644]

index 113cf11d30d70c3c08a0c84d19c148286efb9dba..b454cb6efae7c464c27a558a53ee8aab355bf6da 100644 (file)
@@ -1,3 +1,10 @@
+2009-04-25  Joseph Myers  <joseph@codesourcery.com>
+
+       PR c/39582
+       * c-typeck.c (c_expr_sizeof_type): Create a C_MAYBE_CONST_EXPR
+       with non-null C_MAYBE_CONST_EXPR_PRE if size of a variable-length
+       type is an integer constant.
+
 2009-04-25  Uros Bizjak  <ubizjak@gmail.com>
 
        PR target/39897
index 1cfab1f14f20770d1e6ba78ba6bfa8dfa5dad363..dc7d731ee02e944f8de182cf45f3f73c6bf8ea84 100644 (file)
@@ -2391,8 +2391,18 @@ c_expr_sizeof_type (struct c_type_name *t)
   ret.value = c_sizeof (type);
   ret.original_code = ERROR_MARK;
   ret.original_type = NULL;
-  if (type_expr && c_vla_type_p (type))
-    {
+  if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
+      && c_vla_type_p (type))
+    {
+      /* If the type is a [*] array, it is a VLA but is represented as
+        having a size of zero.  In such a case we must ensure that
+        the result of sizeof does not get folded to a constant by
+        c_fully_fold, because if the size is evaluated the result is
+        not constant and so constraints on zero or negative size
+        arrays must not be applied when this sizeof call is inside
+        another array declarator.  */
+      if (!type_expr)
+       type_expr = integer_zero_node;
       ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
                          type_expr, ret.value);
       C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
index c9cfd92fa3a1230a01f5259366c8f4c732291ec2..44c8c1ae91f4f58ab4dc0a480c5d0c5f62585e44 100644 (file)
@@ -1,3 +1,8 @@
+2009-04-25  Joseph Myers  <joseph@codesourcery.com>
+
+       PR c/39582
+       * gcc.dg/vla-20.c: New test.
+
 2009-04-25  Joseph Myers  <joseph@codesourcery.com>
 
        PR c/39564
diff --git a/gcc/testsuite/gcc.dg/vla-20.c b/gcc/testsuite/gcc.dg/vla-20.c
new file mode 100644 (file)
index 0000000..04d9ee7
--- /dev/null
@@ -0,0 +1,12 @@
+/* Test use of sizeof with [*] in type name: should not refer to
+   zero-size array.  PR 39582.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c99 -pedantic-errors" } */
+
+void foo11d(int x[sizeof(int *[*])]); /* { dg-warning "not in a declaration" } */
+
+/* Although the size is not constant, it may nevertheless appear in a
+   constant expression if not evaluated.  */
+
+void foo11e(int x[1 ? 0 : sizeof(int *[*])]); /* { dg-warning "not in a declaration" } */
+/* { dg-error "zero-size array" "correct zero size" { target *-*-* } 11 } */