Created attachment 35517 [details] Repro case with description When compiling the attached sample with g++ 5.1.0 or 5.1.1 and -Werror the suppression of strict-overflow works for the approaches taken in the testing2 and testing3 functions but the analogous one in the TESTING macro will still produce a strict-overflow warning. This warning will strangely - even though -Werror is set - not result in an error though. With g++ 4.9.2 the diagnostic ignored seems to have no effect at all and a warning with resulting error is created in the TESTING macro. When compiling with gcc instead of g++ - as intended - none of the approaches leads to a warning or an error in any of the aforementioned versions. A related bug might be https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53469 . I found it when trying to create the repro sample for this bug. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53431 has the same g++ only behavior but - with my limited understanding of g++ internals - I wouldn't expect the strict-overflow warning to be created in that early processing stage causing that issue. Repro sample: // g++ -fstrict-overflow -Wstrict-overflow -Werror macro.c // gcc -fstrict-overflow -Wstrict-overflow -Werror macro.c // Under g++: // The TESTING macro still presents a strict-overflow warning // with gcc 5.1.0 and 5.1.1 even though it should be ignored. // With gcc 4.9.2 the ignore is completely ignored and the // compilation fails due to the incorrectly generated warning. // Under gcc: // Works as expected in all versions. #define TESTING(_Exp) { \ _Pragma("GCC diagnostic push") \ _Pragma("GCC diagnostic ignored \"-Wstrict-overflow\"") \ int b = _Exp ; \ _Pragma("GCC diagnostic pop") \ } void testing() { int i = 4; TESTING(i + 4 < i); } void testing2() { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wstrict-overflow" int j = 4; int b = j + 4 < j; #pragma GCC diagnostic pop } void testing3() { _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wstrict-overflow\"") int k = 4; int b = k + 4 < k; _Pragma("GCC diagnostic pop") } int main() { testing(); testing2(); testing3(); return 0; }
I actually meant to link https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66098 as a potentially related bug and not 53469. Sorry about that.
This is because the location of the warning is given at 21:19, which is not within the range affected by the #pragmas. Whether the warning should be given at the macro definition point or at the expansion point is a matter of debate (there are at least two PRs open about this, but I don't remember the numbers). Differences between C and C++ are spurious and may lead to the pragmas applying or not: cc1: /home/manuel/test2/pr66099.cc:21:3: error: assuming signed overflow does not occur when assuming that (X + c) < X is always false [-Werror=strict-overflow] TESTING(i + 4 < i); ^ cc1plus: /home/manuel/test2/pr66099.cc:21:19: error: assuming signed overflow does not occur when assuming that (X + c) < X is always false [-Werror=strict-overflow] TESTING(i + 4 < i); ^ Someone would need to track the locations in GDB and understand why they diverge. Unfortunately, this is not something that usual devs will spend time on given the amount of other things waiting to be fixed. If this is important to you, I would recommend to try to figure out the reason yourself. Once the problem is clear, it is far more likely that a GCC dev will produce a patch and fix it.
I've noticed that using -save-temps also avoids the bug (I've run into a similar problem).
Wstrict-overflow was deprecated in GCC 8 and nothing replaced it. So this bug cannot reproduced or fixed anymore.