Bug 19977 - overflow in non-static initializer should not be pedwarn
Summary: overflow in non-static initializer should not be pedwarn
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.0.0
: P2 normal
Target Milestone: 4.3.0
Assignee: Not yet assigned to anyone
Keywords: rejects-valid
Depends on:
Reported: 2005-02-15 18:47 UTC by Joseph S. Myers
Modified: 2007-01-06 09:36 UTC (History)
3 users (show)

See Also:
Known to work:
Known to fail: 3.3.3 3.2.3 3.4.0 4.0.0 3.0.4 2.95.3
Last reconfirmed: 2006-12-09 14:28:13


Note You need to log in before you can comment on or make changes to this bug.
Description Joseph S. Myers 2005-02-15 18:47:13 UTC
The test

#include <limits.h>

f (void)
  int c = INT_MAX + 1;

should yield only warnings, not an error, with -pedantic-errors.
Not a regression.
Comment 1 Andrew Pinski 2005-02-15 21:20:00 UTC
Comment 2 Manuel López-Ibáñez 2006-12-09 14:28:13 UTC
I believe that the culprit is a call to constant_expression_warning at c-typeck.c (store_init_value). The comment just above this call says "ANSI wants warnings about out-of-range constant initializers". However, if the out-of-range constant initialiser is the result of an operation, a warning has been emitted already by overflow_warning. If the out-of-range is the result of a value that overflowed when converted to the type of the variable, a warning should have been emitted by convert_and_check. 

So my question is, are we required to emit a special (pedantic) warning for: 

int c= INT_MAX + 1; 

different from the warning we already give for:

int c;
c = INT_MAX + 1;

Comment 3 Manuel López-Ibáñez 2006-12-10 17:17:56 UTC
This patch seems to fix this. I get:

XPASS: gcc.dg/overflow-warn-3.c constant (test for bogus messages, line 42)
XPASS: gcc.dg/overflow-warn-4.c constant (test for bogus messages, line 42)

and no new failures on the overflow testcases.

I still have to properly bootstrap and regression test it. Any thoughts?

Index: gcc/c-typeck.c
--- gcc/c-typeck.c      (revision 119686)
+++ gcc/c-typeck.c      (working copy)
@@ -4358,7 +4360,7 @@ store_init_value (tree decl, tree init)

   /* ANSI wants warnings about out-of-range constant initializers.  */
   STRIP_TYPE_NOPS (value);
-  constant_expression_warning (value);
+  if (TREE_STATIC (decl)) constant_expression_warning (value);

   /* Check if we need to set array size from compound literal size.  */
   if (TREE_CODE (type) == ARRAY_TYPE

Comment 4 patchapp@dberlin.org 2006-12-15 01:15:24 UTC
Subject: Bug number PR c/19977

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2006-12/msg01068.html
Comment 5 Manuel López-Ibáñez 2007-01-02 20:07:57 UTC
Subject: Bug 19977

Author: manu
Date: Tue Jan  2 20:07:44 2007
New Revision: 120355

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=120355
2007-01-02  Manuel Lopez-Ibanez <manu@gcc.gnu.org>

	PR c/19977
	* c-typeck.c (store_init_value): Don't emit pedantic overflow
	warning for non-static initializers.

	* gcc/testsuite/gcc.dg/overflow-warn-3.c: Remove XFAIL.
	* gcc/testsuite/gcc.dg/overflow-warn-4.c: Remove XFAIL.


Comment 6 Manuel López-Ibáñez 2007-01-06 09:36:53 UTC
Fixed in mainline.