Bug 66099 - _Pragma diagnostic 'ignored' in macro with strict-overflow not suppressing warning fully with -Werror
Summary: _Pragma diagnostic 'ignored' in macro with strict-overflow not suppressing wa...
Status: RESOLVED WONTFIX
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 5.1.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2015-05-10 22:24 UTC by Stefan H.
Modified: 2019-11-02 12:05 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2015-06-03 00:00:00


Attachments
Repro case with description (434 bytes, text/plain)
2015-05-10 22:24 UTC, Stefan H.
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Stefan H. 2015-05-10 22:24:10 UTC
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;
}
Comment 1 Stefan H. 2015-05-10 22:27:54 UTC
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.
Comment 2 Manuel López-Ibáñez 2015-06-03 08:53:42 UTC
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.
Comment 3 Luboš Luňák 2017-12-06 09:45:50 UTC
I've noticed that using -save-temps also avoids the bug (I've run into a similar problem).
Comment 4 Manuel López-Ibáñez 2019-11-02 12:05:56 UTC
Wstrict-overflow was deprecated in GCC 8 and nothing replaced it. So this bug cannot reproduced or fixed anymore.