[Bug c++/53431] C++ preprocessor ignores #pragma GCC diagnostic

lhyatt at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Sat Dec 4 17:48:45 GMT 2021


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53431

Lewis Hyatt <lhyatt at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lhyatt at gcc dot gnu.org

--- Comment #44 from Lewis Hyatt <lhyatt at gcc dot gnu.org> ---
I have a patch for this submitted to the mailing list here:
https://gcc.gnu.org/pipermail/gcc-patches/2021-December/586191.html

Initially I tried resurrecting Manuel's patch from comment #10 and got it
working, however it led to other issues for C++. The problem is that, with that
change, the C++ frontend processes the pragmas too early, instead of too late;
it processes every #pragma GCC diagnostic in the file prior to handling any
tokens. This makes a large demand on the diagnostics infrastructure, namely
that it correctly handles every case of processing source lines and determining
which #pragmas are in effect at that time and which are for the future. This
infrastructure is in place and generally works, however it doesn't handle all
cases, notably a change to the argument of an option (say something like,
#pragma GCC diagnostic warning "-Wimplicit-fallthrough=4") is not tracked
properly (see also PR c/71603). The state of warning options is also often
queried directly by the frontend code, not making use of the diagnostics state
tracking facilities, and this also leads to warnings being generated or not
generated unexpectedly. So this patch leads to a lot of testcase failures, for
example, c-c++-common/Wshadow-1.c.

I think in the ideal case, it should be made to work correctly that all
diagnostic pragmas could be parsed prior to processing the tokens and
everything would work, but this seems like a large overhaul. I can try to
tackle improving that later, but for the purpose of resolving this PR, I
thought it was worth trying another approach, which is just to arrange that
every frontend does
indeed handle the #pragma when it needs to. My patch does things this way, it
adds the concept of an early pragma handler, which can be registered similar to
a normal one and runs in addition to it. The early handler for diagnostic
pragmas then filters for libcpp-generated diagnostics and processes those when
called. Then the C++ frontend, and gcc -E, are both modified to make use of it.
This asks much less of the state tracking, since only libcpp diagnostics are
handled early.

The C++ frontend changes are not too extensive, it basically notes when it sees
a CPP_PRAGMA_EOL token during initial parsing, and if so, it handles it
immediately. In order to reuse the existing handler for diagnostic pragmas, it
sets up the main lexer so that it is sufficiently prepared for pragma_lex() to
work, which is all that is needed.

For preprocessing mode, I needed to refactor the code for
handle_diagnostic_pragma a bit, since in preprocess-only mode, there is no C or
C++ parser available and the tokens need to be lexed directly from libcpp. I
also needed adjustments in c-ppoutput.c to make sure, as Manuel pointed out,
that we do still output the #pragma GCC diagnostic in the preprocessed output.

I hope this looks workable, happy to adjust the patch as needed.


More information about the Gcc-bugs mailing list