Bug 55578

Summary: Disabling warnings inside macro definition doesn't work
Product: gcc Reporter: Ruben Adamyan <ruboam>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: dodji, dpsicilia, egallager, federico, ferdnyc, lhyatt, lloda, vz-gcc, webrown.cpp
Priority: P3 Keywords: diagnostic
Version: 4.6.2   
Target Milestone: ---   
See Also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53431
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91669
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90400
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69558
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91285
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91932
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2012-12-09 00:00:00

Description Ruben Adamyan 2012-12-03 18:41:19 UTC
When compiling following code with just -Wall option I'm getting below mentioned warning.

#define FF() \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wunused-variable\"") \
{int x;} \
_Pragma("GCC diagnostic pop")

int main()
{
  FF();
  return 0;
}

In function 'int main()':
warning: unused variable 'x' [-Wunused-variable]

But when I also specify -no-integrated-cpp or -save-temps options the warning doesn't show up.

It looks like when preprocessor and compiler work in one shop the warning doesn't get disabled. BTW this happens for any warning not just with unused-variable one.

GCC version is: 4.6.2
Command line is: gcc <file-name> -Wall
System is: Linux  2.6.18-238.el5 #1 SMP Sun Dec 19 14:22:44 EST 2010 x86_64 x86_64 x86_64 GNU/Linux
Comment 1 Paolo Carlini 2012-12-05 13:07:38 UTC
Maybe Dodji can have a look.
Comment 2 Andrew Pinski 2012-12-09 00:31:40 UTC
Confirmed.  It only happens with the C++ front-end though.  The exact same code does not warn with the C front-end.  I think this might be the pre-ahead of time tokenizer that happens for the C++ front-end but does not happen for C.
Comment 3 Eric Gallager 2019-03-14 19:10:36 UTC
*** Bug 89718 has been marked as a duplicate of this bug. ***
Comment 4 Eric Gallager 2019-03-14 19:27:04 UTC
(In reply to Andrew Pinski from comment #2)
> Confirmed.  It only happens with the C++ front-end though.  The exact same
> code does not warn with the C front-end.  I think this might be the
> pre-ahead of time tokenizer that happens for the C++ front-end but does not
> happen for C.

so related to bug 53431 then
Comment 5 Eric Gallager 2019-09-17 01:59:12 UTC
(In reply to Eric Gallager from comment #4)
> (In reply to Andrew Pinski from comment #2)
> > Confirmed.  It only happens with the C++ front-end though.  The exact same
> > code does not warn with the C front-end.  I think this might be the
> > pre-ahead of time tokenizer that happens for the C++ front-end but does not
> > happen for C.
> 
> so related to bug 53431 then

and bug 91669, bug 90400, bug 69558, bug 91285, etc. We should probably choose one of the bugs in this web of related bugs and choose one to use as the base bug and mark the rest as duplicates, but idk which one...
Comment 6 Eric Gallager 2019-09-17 02:00:38 UTC
dang it, typoed one of the "See Also" entries...
Comment 7 FeRD 2019-09-20 10:47:39 UTC
(In reply to Eric Gallager from comment #5)
> We should probably
> choose one of the bugs in this web of related bugs and choose one to use as
> the base bug and mark the rest as duplicates, but idk which one...

If you're taking votes from the "peanut gallery", I vote this one, as it's the only one that even hints at a poorly-disseminated fact: That -no-integrated-cpp serves as an easy workaround for this issue on affected versions, at the slight expense of some additional compile time.

In our own CMake project, we've added the following config, to great effect:

#### Work around a GCC < 9 bug with handling of _Pragma() in macros
#### See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55578
if ((${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") AND
    (${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS "9.0.0"))
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -no-integrated-cpp")
endif()
Comment 8 Vadim Zeitlin 2021-04-26 09:34:20 UTC
For the record: with gcc 11 this bug now affects the existing code using pragmas to locally suppress -Wsuggest-override, i.e. the warning is not suppressed any longer, even though it used to work in all versions since gcc 5, so not only this bug is still present, but it got worse in the latest version.
Comment 9 Lewis Hyatt 2022-10-04 00:37:37 UTC
This was fixed by r9-1926 (see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69558#c25) and is a dupe of that aspect of PR69558. I would suggest to mark this one resolved, however comment 8 brought up that there was potentially a change in behavior in GCC 11. Do you have an example test case that doesn't behave as you expect? If not, I would suggest to close this one, there has been a lot of progress on pragma locations and I believe most such issues are now fixed on GCC 13 master branch.
Comment 10 Vadim Zeitlin 2022-10-04 16:04:36 UTC
There definitely was a change in behaviour in gcc 11 because I had to make this change

https://github.com/wxWidgets/wxWidgets/commit/95c98a0b5ff71caca6654327bf341719c6587766

to avoid getting warnings with it that we didn't get with the previous versions.

The idea there is that we define some macros whose expansion contains declarations of overridden virtual functions, which inevitably results in -Wsuggest-override when used in user classes using or not using "override" for their other functions, so we have to disable this warning locally. wxWARNING_SUPPRESS_MISSING_OVERRIDE, which expands to

    _Pragma("GCC diagnostic push")
    _Pragma("GCC diagnostic ignored \"-Wsuggest-override\"")

worked for this until gcc 11 but not with it.
Comment 11 Lewis Hyatt 2022-10-05 22:08:36 UTC
(In reply to Vadim Zeitlin from comment #10)
> There definitely was a change in behaviour in gcc 11 because I had to make
> this change
> 
> https://github.com/wxWidgets/wxWidgets/commit/
> 95c98a0b5ff71caca6654327bf341719c6587766

Thanks for that. I made a bisectable test that compiles allheaders.cpp (without your GCC 11-specific workaround in place) and counts the number of -Wsuggest-override warnings, which does indeed change in GCC 11.1 vs older and newer versions. The bisect confirmed that this issue was the same as that of PR100796. It existed only in GCC 11.1, as it was introduced by r11-7179 and was fixed by r12-1538, which was also backported to GCC 11.2.

This particular issue only materializes with a sufficiently large source file, so I don't think you should find any "reasonably" sized testcase to reproduce it. All basic examples I tried work fine in 11.1 as well. As far as I can tell, I think your workaround should be needed only for sufficiently large translation units (approx 100000 lines or more), not for typical size user code, and I think it should be needed only for 11.1 and no other version. If you know otherwise, please advise... otherwise, I think this is resolved.
Comment 12 Vadim Zeitlin 2022-10-06 01:31:05 UTC
Thanks for looking at this! I'm happy to hear that the problem is fixed in 11.2, but I'm probably not going to change our code anyhow, especially as we're going to finally drop support for C++98 very soon and so will be able to just use "override" unconditionally anyhow.

I.e. from my point of view there is no real problem any more, I just replied here to give more information about the problem in case it could be useful.
Comment 13 Lewis Hyatt 2022-10-06 01:58:58 UTC
(In reply to Vadim Zeitlin from comment #12)
> Thanks for looking at this! I'm happy to hear that the problem is fixed in
> 11.2, but I'm probably not going to change our code anyhow, especially as
> we're going to finally drop support for C++98 very soon and so will be able
> to just use "override" unconditionally anyhow.
> 
> I.e. from my point of view there is no real problem any more, I just replied
> here to give more information about the problem in case it could be useful.

Thanks, yes it was useful. Hopefully, issues with warning suppression will be increasingly rare going forward.