From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id DB0113858403; Sat, 4 Dec 2021 17:48:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DB0113858403 From: "lhyatt at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/53431] C++ preprocessor ignores #pragma GCC diagnostic Date: Sat, 04 Dec 2021 17:48:45 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 4.7.0 X-Bugzilla-Keywords: diagnostic, documentation X-Bugzilla-Severity: minor X-Bugzilla-Who: lhyatt at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 04 Dec 2021 17:48:47 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D53431 Lewis Hyatt changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |lhyatt at gcc dot gnu.org --- Comment #44 from Lewis Hyatt --- 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 la= te; 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 determi= ning 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 a= ll cases, notably a change to the argument of an option (say something like, #pragma GCC diagnostic warning "-Wimplicit-fallthrough=3D4") 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 st= ate 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 simila= r 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 w= hen 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 outp= ut. I hope this looks workable, happy to adjust the patch as needed.=