(valid_compound_expr_initializer): New function.
authorJim Wilson <wilson@gcc.gnu.org>
Wed, 15 Sep 1993 02:03:22 +0000 (19:03 -0700)
committerJim Wilson <wilson@gcc.gnu.org>
Wed, 15 Sep 1993 02:03:22 +0000 (19:03 -0700)
(digest_init): Handle compound expressions as initializers when
pedantic.

From-SVN: r5323

gcc/c-typeck.c

index 7862221b0d000f8336e9e8dfb45b8c5b2830a14b..3dd7288a68a93ea391934a92efb4785f3ec28b23 100644 (file)
@@ -4579,6 +4579,32 @@ initializer_constant_valid_p (value, endtype)
 
   return 0;
 }
+
+/* If VALUE is a compound expr all of whose expressions are constant, then
+   return its value.  Otherwise, return error_mark_node.
+
+   This is for handling COMPOUND_EXPRs as initializer elements
+   which is allowed with a warning when -pedantic is specified.  */
+
+static tree
+valid_compound_expr_initializer (value, endtype)
+     tree value;
+     tree endtype;
+{
+  if (TREE_CODE (value) == COMPOUND_EXPR)
+    {
+      if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
+         == error_mark_node)
+       return error_mark_node;
+      return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
+                                             endtype);
+    }
+  else if (! TREE_CONSTANT (value)
+          && ! initializer_constant_valid_p (value, endtype))
+    return error_mark_node;
+  else
+    return value;
+}
 \f
 /* Perform appropriate conversions on the initial value of a variable,
    store it in the declaration DECL,
@@ -4969,7 +4995,25 @@ digest_init (type, init, require_constant, constructor_constant)
          && TREE_CODE (inside_init) == VAR_DECL)
        inside_init = decl_constant_value (inside_init);
 
-      if (require_constant && ! TREE_CONSTANT (inside_init))
+      /* Compound expressions can only occur here if -pedantic or
+        -pedantic-errors is specified.  In the later case, we always want
+        an error.  In the former case, we simply want a warning.  */
+      if (require_constant && pedantic
+         && TREE_CODE (inside_init) == COMPOUND_EXPR)
+       {
+         inside_init
+           = valid_compound_expr_initializer (inside_init,
+                                              TREE_TYPE (inside_init));
+         if (inside_init == error_mark_node)
+           error_init ("initializer element%s is not constant",
+                       " for `%s'", NULL);
+         else
+           pedwarn_init ("initializer element%s is not constant",
+                         " for `%s'", NULL);
+         if (flag_pedantic_errors)
+           inside_init = error_mark_node;
+       }
+      else if (require_constant && ! TREE_CONSTANT (inside_init))
        {
          error_init ("initializer element%s is not constant",
                      " for `%s'", NULL);