From: Lewis Hyatt Date: Wed, 1 Nov 2023 17:01:12 +0000 (-0400) Subject: preprocessor: Reinitialize frontend parser after loading a PCH [PR112319] X-Git-Tag: basepoints/gcc-15~4303 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5d4abd9219dfa53b52b341255e99139bb6cad302;p=thirdparty%2Fgcc.git preprocessor: Reinitialize frontend parser after loading a PCH [PR112319] Since r14-2893, the frontend parser object needs to exist when running in preprocess-only mode, because pragma_lex() is now called in that mode and needs to make use of it. This is handled by calling c_init_preprocess() at startup. If -fpch-preprocess is in effect (commonly, because of -save-temps), a PCH file may be loaded during preprocessing, in which case the parser will be destroyed, causing the issue noted in the PR. Resolve it by reinitializing the frontend parser after loading the PCH. gcc/c-family/ChangeLog: PR pch/112319 * c-ppoutput.cc (cb_read_pch): Reinitialize the frontend parser after loading a PCH. gcc/testsuite/ChangeLog: PR pch/112319 * g++.dg/pch/pr112319.C: New test. * g++.dg/pch/pr112319.Hs: New test. * gcc.dg/pch/pr112319.c: New test. * gcc.dg/pch/pr112319.hs: New test. --- diff --git a/gcc/c-family/c-ppoutput.cc b/gcc/c-family/c-ppoutput.cc index 4aa2bef2c0fd..4f973767976e 100644 --- a/gcc/c-family/c-ppoutput.cc +++ b/gcc/c-family/c-ppoutput.cc @@ -862,4 +862,9 @@ cb_read_pch (cpp_reader *pfile, const char *name, fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name); print.src_line++; + + /* The process of reading the PCH has destroyed the frontend parser, + so ask the frontend to reinitialize it, in case we need it to + process any #pragma directives encountered while preprocessing. */ + c_init_preprocess (); } diff --git a/gcc/testsuite/g++.dg/pch/pr112319.C b/gcc/testsuite/g++.dg/pch/pr112319.C new file mode 100644 index 000000000000..9e0457e8aec4 --- /dev/null +++ b/gcc/testsuite/g++.dg/pch/pr112319.C @@ -0,0 +1,5 @@ +/* { dg-additional-options "-Wpragmas -save-temps" } */ +#include "pr112319.H" +#pragma GCC diagnostic error "-Wpragmas" +#pragma GCC diagnostic ignored "oops" /* { dg-error "oops" } */ +/* { dg-regexp {[^[:space:]]*: some warnings being treated as errors} } */ diff --git a/gcc/testsuite/g++.dg/pch/pr112319.Hs b/gcc/testsuite/g++.dg/pch/pr112319.Hs new file mode 100644 index 000000000000..3b6178bfae0f --- /dev/null +++ b/gcc/testsuite/g++.dg/pch/pr112319.Hs @@ -0,0 +1 @@ +/* This space intentionally left blank. */ diff --git a/gcc/testsuite/gcc.dg/pch/pr112319.c b/gcc/testsuite/gcc.dg/pch/pr112319.c new file mode 100644 index 000000000000..043881463c51 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pch/pr112319.c @@ -0,0 +1,5 @@ +/* { dg-additional-options "-Wpragmas -save-temps" } */ +#include "pr112319.h" +#pragma GCC diagnostic error "-Wpragmas" +#pragma GCC diagnostic ignored "oops" /* { dg-error "oops" } */ +/* { dg-regexp {[^[:space:]]*: some warnings being treated as errors} } */ diff --git a/gcc/testsuite/gcc.dg/pch/pr112319.hs b/gcc/testsuite/gcc.dg/pch/pr112319.hs new file mode 100644 index 000000000000..3b6178bfae0f --- /dev/null +++ b/gcc/testsuite/gcc.dg/pch/pr112319.hs @@ -0,0 +1 @@ +/* This space intentionally left blank. */