c++: Fix up cp_parser_skip_to_pragma_eol [PR104623]
authorJakub Jelinek <jakub@redhat.com>
Tue, 15 Mar 2022 08:15:27 +0000 (09:15 +0100)
committerJakub Jelinek <jakub@redhat.com>
Tue, 15 Mar 2022 08:15:27 +0000 (09:15 +0100)
commitefd1582926f3278a5e57ebab7d87fc870f06ecea
treea806957e5961f4f7f3f12e77338abf217b3e4ba4
parenta2645cd8fb33b36d737b310e26f4c47401305c7b
c++: Fix up cp_parser_skip_to_pragma_eol [PR104623]

We ICE on the following testcase, because we tentatively parse it multiple
times and the erroneous attribute syntax results in
cp_parser_skip_to_end_of_statement, which when seeing CPP_PRAGMA (can be
any deferred one, OpenMP/OpenACC/ivdep etc.) it calls
cp_parser_skip_to_pragma_eol, which calls cp_lexer_purge_tokens_after.
That call purges all the tokens from CPP_PRAGMA until CPP_PRAGMA_EOL,
excluding the initial CPP_PRAGMA though (but including the final
CPP_PRAGMA_EOL).  This means the second time we parse this, we see
CPP_PRAGMA with no tokens after it from the pragma, most importantly
not the CPP_PRAGMA_EOL, so either if it is the last pragma in the TU,
we ICE, or if there are other pragmas we treat everything in between
as a pragma.

I've tried various things, including making the CPP_PRAGMA token
itself also purged, or changing the cp_parser_skip_to_end_of_statement
(and cp_parser_skip_to_end_of_block_or_statement) to call it with
NULL instead of token, so that this purging isn't done there,
but each patch resulted in lots of regressions.
But removing the purging altogether surprisingly doesn't regress anything,
and I think it is the right thing, if we e.g. parse tentatively, why can't
we parse the pragma multiple times or at least skip over it?

2022-03-15  Jakub Jelinek  <jakub@redhat.com>

PR c++/104623
* parser.cc (cp_parser_skip_to_pragma_eol): Don't purge any tokens.

* g++.dg/gomp/pr104623.C: New test.
gcc/cp/parser.cc
gcc/testsuite/g++.dg/gomp/pr104623.C [new file with mode: 0644]