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.
Confirmed. It works correctly in C but has never worked in C++ so it's not a regression.
I think this is a dup of something but I can't remember the bug number right now; I'll search harder later...
(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 :)
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 ***
This issue still occurs in GCC 13.2 despite fixing PR53431: https://godbolt.org/z/6xon1cTfh
-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)
Fix by Lewis: https://gcc.gnu.org/pipermail/gcc-patches/2023-October/633508.html
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.
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.
Fixed for GCC 14 and 13.3.