From 8bd9a00f4349ebcd65223e3dcdfe83867e417287 Mon Sep 17 00:00:00 2001 From: Nathan Sidwell Date: Tue, 3 Nov 2020 09:06:09 -0800 Subject: [PATCH] cpplib: EOF in pragmas This patch moves the generation of PRAGMA_EOF earlier, to when we set need_line, rather than when we try and get the next line. It also prevents peeking past a PRAGMA token. libcpp/ * lex.c (cpp_peek_token): Do not peek past CPP_PRAGMA. (_cpp_lex_direct): Handle EOF in pragma when setting need_line, not when needing a line. --- libcpp/lex.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/libcpp/lex.c b/libcpp/lex.c index 1d522030a3c..f58a8828124 100644 --- a/libcpp/lex.c +++ b/libcpp/lex.c @@ -2554,6 +2554,15 @@ cpp_peek_token (cpp_reader *pfile, int index) index--; break; } + else if (peektok->type == CPP_PRAGMA) + { + /* Don't peek past a pragma. */ + if (peektok == &pfile->directive_result) + /* Save the pragma in the buffer. */ + *pfile->cur_token++ = *peektok; + index--; + break; + } } while (index--); @@ -2757,14 +2766,7 @@ _cpp_lex_direct (cpp_reader *pfile) buffer = pfile->buffer; if (buffer->need_line) { - if (pfile->state.in_deferred_pragma) - { - result->type = CPP_PRAGMA_EOL; - pfile->state.in_deferred_pragma = false; - if (!pfile->state.pragma_allow_expansion) - pfile->state.prevent_expansion--; - return result; - } + gcc_assert (!pfile->state.in_deferred_pragma); if (!_cpp_get_fresh_line (pfile)) { result->type = CPP_EOF; @@ -2829,6 +2831,19 @@ _cpp_lex_direct (cpp_reader *pfile) && !CPP_OPTION (pfile, traditional))) CPP_INCREMENT_LINE (pfile, 0); buffer->need_line = true; + if (pfile->state.in_deferred_pragma) + { + /* Produce the PRAGMA_EOL on this line. File reading + ensures there is always a \n at end of the buffer, thus + in a deferred pragma we always see CPP_PRAGMA_EOL before + any CPP_EOF. */ + result->type = CPP_PRAGMA_EOL; + result->flags &= ~PREV_WHITE; + pfile->state.in_deferred_pragma = false; + if (!pfile->state.pragma_allow_expansion) + pfile->state.prevent_expansion--; + return result; + } goto fresh_line; case '0': case '1': case '2': case '3': case '4': -- 2.30.2