Bug 89038 - #pragma GCC diagnostic ignored "-Wunknown-pragmas" does not work
Summary: #pragma GCC diagnostic ignored "-Wunknown-pragmas" does not work
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.2.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2019-01-24 14:45 UTC by Maxim Ivanov
Modified: 2023-10-19 17:01 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.3.5, 4.4.7, 4.8.5, 4.9.4, 5.4.0, 6.4.0, 7.3.0, 8.2.0, 9.0
Last reconfirmed: 2019-01-24 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Maxim Ivanov 2019-01-24 14:45:23 UTC
Consider this test.cxx file:

-------------------------------8<--------------------------------
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"

// this gets silenced correctly (by line 2)
#pragma GCC diagnostic ignored "-Wfoobar-unknown-gcc-pragma"

// this still emits "-Wunknown-pragmas", despite line 3
#pragma clang diagnostic ignored "-Wfoobar-unknown-clang-pragma"

int main() {}

#pragma GCC diagnostic pop
-------------------------------8<--------------------------------

g++ -Wall -Werror test.cxx

test.cxx:9:0: error: ignoring #pragma clang diagnostic [-Werror=unknown-pragmas]
 #pragma clang diagnostic ignored "-Wfoobar-unknown-clang-pragma"
 

This happens despite the fact that "-Wunknown-pragmas" is explicitly silenced in line 3.

Adding -Wno-unknown-pragmas to the command line works as expected; however, I expect the in-source #pragma to work as well. But it doesn't.

g++ (GCC) 7.2.1 20180104 (Red Hat 7.2.1-5)

Bug #53431 seems closely related.
Comment 1 Martin Sebor 2019-01-24 18:04:43 UTC
Confirmed.  It works correctly in C but has never worked in C++ so it's not a regression.
Comment 2 Eric Gallager 2019-01-24 19:24:20 UTC
I think this is a dup of something but I can't remember the bug number right now; I'll search harder later...
Comment 3 Maxim Ivanov 2019-01-25 11:20:06 UTC
(In reply to Eric Gallager from comment #2)
> I think this is a dup of something but I can't remember the bug number right
> now; I'll search harder later...

Eric, take a look at bug #53431, I think that's what you meant. It's a pretty similar report about in-source pragmas not working in C++.

Further, that thread contains a WIP patch from Manuel López-Ibáñez, which he expressly abandoned while encouraging others to finish and merge it.

P.S.
I do harbor a hope to resolve this on my own, once I accumulate the necessary courage to allocate some time for a little GCC contribution. That's why I'm reporting a separate issue knowing it's a duplicate :)
Comment 4 Jonathan Wakely 2019-01-25 13:33:55 UTC
It is a dup of PR 53431, I see no reason to keep this one open as well.

The -Wunknown-pragmas warning is emitted by the preprocessor, and for C++ the preprocessor doesn't respect the diagnostic pragma.

*** This bug has been marked as a duplicate of bug 53431 ***
Comment 5 argothiel 2023-10-17 17:11:02 UTC
This issue still occurs in GCC 13.2 despite fixing PR53431:
https://godbolt.org/z/6xon1cTfh
Comment 6 Lewis Hyatt 2023-10-18 16:57:18 UTC
-Wunknown-pragmas is a special case because it is emitted during preprocessing, but not directly by the preprocessor (rather from a callback in C frontend), so in the options file, it is not tagged as a preprocessor warning, and hence wasn't fixed by the mechanism introduced for PR53431.

I'll submit a patch shortly:

diff --git a/gcc/c-family/c-pragma.cc b/gcc/c-family/c-pragma.cc
index 293311dd4ce..98dfb0f108b 100644
--- a/gcc/c-family/c-pragma.cc
+++ b/gcc/c-family/c-pragma.cc
@@ -963,7 +963,8 @@ handle_pragma_diagnostic_impl ()
   /* option_string + 1 to skip the initial '-' */
   unsigned int option_index = find_opt (data.option_str + 1, lang_mask);

-  if (early && !c_option_is_from_cpp_diagnostics (option_index))
+  if (early && !(c_option_is_from_cpp_diagnostics (option_index)
+                || option_index == OPT_Wunknown_pragmas))
     return;

   if (option_index == OPT_SPECIAL_unknown)
Comment 7 Marek Polacek 2023-10-19 12:45:21 UTC
Fix by Lewis: https://gcc.gnu.org/pipermail/gcc-patches/2023-October/633508.html
Comment 8 GCC Commits 2023-10-19 13:11:30 UTC
The master branch has been updated by Lewis Hyatt <lhyatt@gcc.gnu.org>:

https://gcc.gnu.org/g:19cc4b9d74940f29c961e2a5a8b1fa84992d3d30

commit r14-4748-g19cc4b9d74940f29c961e2a5a8b1fa84992d3d30
Author: Lewis Hyatt <lhyatt@gmail.com>
Date:   Wed Oct 18 12:37:08 2023 -0400

    c++: Make -Wunknown-pragmas controllable by #pragma GCC diagnostic [PR89038]
    
    As noted on the PR, commit r13-1544, the fix for PR53431, did not handle
    the specific case of -Wunknown-pragmas, because that warning is issued
    during preprocessing, but not by libcpp directly (it comes from the
    cb_def_pragma callback).  Address that by handling this pragma in
    addition to libcpp pragmas during the early pragma handler.
    
    gcc/c-family/ChangeLog:
    
            PR c++/89038
            * c-pragma.cc (handle_pragma_diagnostic_impl):  Handle
            -Wunknown-pragmas during early processing.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/89038
            * c-c++-common/cpp/Wunknown-pragmas-1.c: New test.
Comment 9 GCC Commits 2023-10-19 16:59:08 UTC
The releases/gcc-13 branch has been updated by Lewis Hyatt <lhyatt@gcc.gnu.org>:

https://gcc.gnu.org/g:7a1de35f9cdc13098375baa277496147be271dd3

commit r13-7964-g7a1de35f9cdc13098375baa277496147be271dd3
Author: Lewis Hyatt <lhyatt@gmail.com>
Date:   Wed Oct 18 12:37:08 2023 -0400

    c++: Make -Wunknown-pragmas controllable by #pragma GCC diagnostic [PR89038]
    
    As noted on the PR, commit r13-1544, the fix for PR53431, did not handle
    the specific case of -Wunknown-pragmas, because that warning is issued
    during preprocessing, but not by libcpp directly (it comes from the
    cb_def_pragma callback).  Address that by handling this pragma in
    addition to libcpp pragmas during the early pragma handler.
    
    gcc/c-family/ChangeLog:
    
            PR c++/89038
            * c-pragma.cc (handle_pragma_diagnostic_impl):  Handle
            -Wunknown-pragmas during early processing.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/89038
            * c-c++-common/cpp/Wunknown-pragmas-1.c: New test.
Comment 10 Lewis Hyatt 2023-10-19 17:01:34 UTC
Fixed for GCC 14 and 13.3.