c-typeck.c (store_init_value): If initializing object with array type of unknown...
authorJakub Jelinek <jakub@redhat.com>
Fri, 28 Dec 2001 09:51:20 +0000 (10:51 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Fri, 28 Dec 2001 09:51:20 +0000 (10:51 +0100)
* c-typeck.c (store_init_value): If initializing object with array
type of unknown size by a compound literal, set object's size from
compound literal size.
* doc/extend.texi (Compound Literals): Adjust documentation.

* gcc.dg/gnu89-init-1.c: Adjust for the new behaviour, add some
additional tests.

From-SVN: r48343

gcc/ChangeLog
gcc/c-typeck.c
gcc/doc/extend.texi
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/gnu89-init-1.c

index 19958f4e5284b7c1eb3b50278c72aa070e412392..59dc83556778fb2f39aaf54bcb7bbe191c260b60 100644 (file)
@@ -1,3 +1,10 @@
+2001-12-28  Jakub Jelinek  <jakub@redhat.com>
+
+       * c-typeck.c (store_init_value): If initializing object with array
+       type of unknown size by a compound literal, set object's size from
+       compound literal size.
+       * doc/extend.texi (Compound Literals): Adjust documentation.
+
 2001-12-28  Richard Henderson  <rth@redhat.com>
 
        * real.c (etoe113, toe113): Ifndef INTEL_EXTENDED_IEEE_FORMAT.
index eacf4f5669143e8717d8406e1fff11e10c3a90ef..c2b4624ba2f7eaa384ca94ddf4f344391fa93ad6 100644 (file)
@@ -4495,6 +4495,33 @@ store_init_value (decl, init)
   /* ANSI wants warnings about out-of-range constant initializers.  */
   STRIP_TYPE_NOPS (value);
   constant_expression_warning (value);
+
+  /* Check if we need to set array size from compound literal size.  */
+  if (TREE_CODE (type) == ARRAY_TYPE
+      && TYPE_DOMAIN (type) == 0
+      && value != error_mark_node)
+    {
+      tree inside_init = init;
+
+      if (TREE_CODE (init) == NON_LVALUE_EXPR)
+       inside_init = TREE_OPERAND (init, 0);
+      inside_init = fold (inside_init);
+
+      if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
+       {
+         tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
+
+         if (TYPE_DOMAIN (TREE_TYPE (decl)))
+           {
+             /* For int foo[] = (int [3]){1}; we need to set array size
+                now since later on array initializer will be just the
+                brace enclosed list of the compound literal.  */
+             TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
+             layout_type (type);
+             layout_decl (decl, 0);
+           }
+       }
+    }
 }
 \f
 /* Methods for storing and printing names for error messages.  */
index a224770953b9cfffa916d5ddb0542ae078be7774..2e22c1b7d9b410ba72485d2b0853ba4f9f7b0cf9 100644 (file)
@@ -1598,8 +1598,7 @@ It is handled as if the object was initialized only with the bracket
 enclosed list if compound literal's and object types match.
 The initializer list of the compound literal must be constant.
 If the object being initialized has array type of unknown size, the size is
-determined by compound literal's initializer list, not by the size of the
-compound literal.
+determined by compound literal size.
 
 @example
 static struct foo x = (struct foo) @{1, 'a', 'b'@};
@@ -1612,7 +1611,7 @@ The above lines are equivalent to the following:
 @example
 static struct foo x = @{1, 'a', 'b'@};
 static int y[] = @{1, 2, 3@};
-static int z[] = @{1@};
+static int z[] = @{1, 0, 0@};
 @end example
 
 @node Designated Inits
index 24abc584bc2ff53517563c4fad99329ab28a4eeb..9cb21d407344bae259172e90676e4d900de38449 100644 (file)
@@ -1,3 +1,8 @@
+2001-12-28  Jakub Jelinek  <jakub@redhat.com>
+
+       * gcc.dg/gnu89-init-1.c: Adjust for the new behaviour, add some
+       additional tests.
+
 2001-12-27  Roger Sayle <roger@eyesopen.com>
 
        * gcc.c-torture/execute/string-opt-16.c: New testcase.
index dae61b57da5fb78a1fcbc7a8bb59d65f5a5540bc..e11a0a48c2acd764704a9f0b37a77a7f8b234f8d 100644 (file)
@@ -20,6 +20,8 @@ struct A a = (struct A) { .j = 6, .k[2] = 12 };
 struct B b = (struct B) { };
 int c[] = (int []) { [2] = 6, 7, 8 };
 int d[] = (int [3]) { 1 };
+int e[2] = (int []) { 1, 2 };
+int f[2] = (int [2]) { 1 };
 
 int main (void)
 {
@@ -29,9 +31,17 @@ int main (void)
     abort ();
   if (sizeof (c) != 5 * sizeof (int))
     abort ();
-  if (d[0] != 1)
+  if (d[0] != 1 || d[1] || d[2])
     abort ();
-  if (sizeof (d) != sizeof (int))
+  if (sizeof (d) != 3 * sizeof (int))
+    abort ();
+  if (e[0] != 1 || e[1] != 2)
+    abort ();
+  if (sizeof (e) != 2 * sizeof (int))
+    abort ();
+  if (f[0] != 1 || f[1])
+    abort ();
+  if (sizeof (f) != 2 * sizeof (int))
     abort ();
   exit (0);
 }