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.
Confirmed. And very difficult to fix.
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.
Created attachment 24744 [details] Possible patch Here is a possible patch to fix this test case.
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.
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.
Patch tested and sent for approval. http://gcc.gnu.org/ml/gcc-patches/2011-07/msg00971.html
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
Fixed in mainline.
(In reply to comment #8) > Fixed in mainline. So closing.