Bug 49705 - -Wstrict-overflow should not diagnose unevaluated expressions
Summary: -Wstrict-overflow should not diagnose unevaluated expressions
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.6.1
: P3 normal
Target Milestone: ---
Assignee: Ian Lance Taylor
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2011-07-11 18:17 UTC by Paul Eggert
Modified: 2011-09-21 15:33 UTC (History)
3 users (show)

See Also:
Host: x86_64-unknown-linux-gnu
Target: x86_64-unknown-linux-gnu
Build: x86_64-unknown-linux-gnu
Known to work:
Known to fail:
Last reconfirmed: 2011-07-12 08:57:17


Attachments
Possible patch (1011 bytes, patch)
2011-07-12 14:25 UTC, Ian Lance Taylor
Details | Diff
longer, more-realistic test case (463 bytes, text/x-csrc)
2011-07-12 16:30 UTC, Paul Eggert
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Paul Eggert 2011-07-11 18:17:58 UTC
I ran into this problem when compiling a test version of Emacs.
Here's a stripped-down version of the problem:

   int
   check_image_width (int width)
   {
     return (1 || width <= width + 2);
   }

When compiled with "gcc -c -Wstrict-overflow -O2 t.c" the diagnostic is:

   t.c:4:3: warning: assuming signed overflow does not occur when assuming that (X + c) >= X is always true [-Wstrict-overflow]

This diagnostic is wrong, as the expression in question (width <=
width + 2) is unreachable.  In the original source code the
expression is unreachable because the code is carefully checking for
integer overflow.

GCC should not diagnose potential integer overflow in unreachable
expressions, as that is a common idiom in code that is working correctly.
Comment 1 Richard Biener 2011-07-12 08:57:17 UTC
Confirmed.  And very difficult to fix.
Comment 2 Ian Lance Taylor 2011-07-12 14:25:13 UTC
Very difficult to fix in the general case but easy to fix in this particular case.  When the warning is issued, c_inhibit_evaluation_warnings is 1.  All we need to do is, when setting c_inhibit_evaluation_warnings non-zero, also call fold_defer_overflow_warnings.
Comment 3 Ian Lance Taylor 2011-07-12 14:25:56 UTC
Created attachment 24744 [details]
Possible patch

Here is a possible patch to fix this test case.
Comment 4 Paul Eggert 2011-07-12 16:30:44 UTC
Created attachment 24747 [details]
longer, more-realistic test case

Thanks for looking into the problem.  If it helps, attached is a longer test
case that more-closely corresponds to the original problem that I had
with the Emacs source code.
Comment 5 Ian Lance Taylor 2011-07-12 16:42:07 UTC
My proposed patch also fixes the larger test case.  Without the patch I see this:

strict-overflow-bug.c: In function ‘check_image_width’:
strict-overflow-bug.c:14:4: warning: assuming signed overflow does not occur when assuming that (X + c) >= X is always true [-Wstrict-overflow]

With the patch I see no warning.  This is compiling with -O2 -Wstrict-overflow.
Comment 6 Ian Lance Taylor 2011-07-12 22:47:33 UTC
Patch tested and sent for approval.

http://gcc.gnu.org/ml/gcc-patches/2011-07/msg00971.html
Comment 7 ian@gcc.gnu.org 2011-07-21 21:30:26 UTC
Author: ian
Date: Thu Jul 21 21:30:24 2011
New Revision: 176591

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=176591
Log:
gcc/c-family:
	PR middle-end/49705
	* c-common.c (c_disable_warnings): New static function.
	(c_enable_warnings): New static function.
	(c_fully_fold_internal): Change local unused_p to bool.  Call
	c_disable_warnings and c_enable_warnings rather than change
	c_inhibit_evaluation_warnings.
gcc/testsuite:
	PR middle-end/49705
	* gcc.dg/pr49705.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/pr49705.c
Modified:
    trunk/gcc/c-family/ChangeLog
    trunk/gcc/c-family/c-common.c
    trunk/gcc/testsuite/ChangeLog
Comment 8 Ian Lance Taylor 2011-07-21 21:32:01 UTC
Fixed in mainline.
Comment 9 Manuel López-Ibáñez 2011-09-21 15:33:24 UTC
(In reply to comment #8)
> Fixed in mainline.

So closing.