aboutsummaryrefslogtreecommitdiff
path: root/libcpp/lex.cc
diff options
context:
space:
mode:
authorLewis Hyatt <lhyatt@gmail.com>2022-06-15 18:06:53 -0400
committerLewis Hyatt <lhyatt@gmail.com>2022-11-05 10:23:20 -0400
commit3ad2167bbac8ae83b1e91305b105ab5287bdac55 (patch)
treed6dbb86c53a6eded2fb209819e466b6ef1200ddc /libcpp/lex.cc
parentb83f01d0057578ebc1785f858fbfd46cdc210560 (diff)
downloadgcc-3ad2167bbac8ae83b1e91305b105ab5287bdac55.zip
gcc-3ad2167bbac8ae83b1e91305b105ab5287bdac55.tar.gz
gcc-3ad2167bbac8ae83b1e91305b105ab5287bdac55.tar.bz2
c++: libcpp: Support raw strings with newlines in directives [PR55971]
It's not currently possible to use a C++11 raw string containing a newline as part of the definition of a macro, or in any other preprocessing directive, such as: #define X R"(two lines)" #error R"(this error has two lines)" Add support for that by relaxing the conditions under which _cpp_get_fresh_line() refuses to get a new line. For the case of lexing a raw string, it's OK to do so as long as there is another line within the current buffer. The code in cpp_get_fresh_line() was refactored into a new function get_fresh_line_impl(), so that the new logic is applied only when processing a raw string and not any other times. libcpp/ChangeLog: PR preprocessor/55971 * lex.cc (get_fresh_line_impl): New function refactoring the code from... (_cpp_get_fresh_line): ...here. (lex_raw_string): Use the new version of get_fresh_line_impl() to support raw strings containing new lines when processing a directive. gcc/testsuite/ChangeLog: PR preprocessor/55971 * c-c++-common/raw-string-directive-1.c: New test. * c-c++-common/raw-string-directive-2.c: New test. gcc/c-family/ChangeLog: PR preprocessor/55971 * c-ppoutput.cc (adjust_for_newlines): Update comment.
Diffstat (limited to 'libcpp/lex.cc')
-rw-r--r--libcpp/lex.cc41
1 files changed, 32 insertions, 9 deletions
diff --git a/libcpp/lex.cc b/libcpp/lex.cc
index cc12a52d..b110792 100644
--- a/libcpp/lex.cc
+++ b/libcpp/lex.cc
@@ -1076,6 +1076,9 @@ _cpp_clean_line (cpp_reader *pfile)
buffer->next_line = s + 1;
}
+template <bool lexing_raw_string>
+static bool get_fresh_line_impl (cpp_reader *pfile);
+
/* Return true if the trigraph indicated by NOTE should be warned
about in a comment. */
static bool
@@ -2695,9 +2698,8 @@ lex_raw_string (cpp_reader *pfile, cpp_token *token, const uchar *base)
{
pos--;
pfile->buffer->cur = pos;
- if (pfile->state.in_directive
- || (pfile->state.parsing_args
- && pfile->buffer->next_line >= pfile->buffer->rlimit))
+ if ((pfile->state.in_directive || pfile->state.parsing_args)
+ && pfile->buffer->next_line >= pfile->buffer->rlimit)
{
cpp_error_with_line (pfile, CPP_DL_ERROR, token->src_loc, 0,
"unterminated raw string");
@@ -2712,7 +2714,7 @@ lex_raw_string (cpp_reader *pfile, cpp_token *token, const uchar *base)
CPP_INCREMENT_LINE (pfile, 0);
pfile->buffer->need_line = true;
- if (!_cpp_get_fresh_line (pfile))
+ if (!get_fresh_line_impl<true> (pfile))
{
/* We ran out of file and failed to get a line. */
location_t src_loc = token->src_loc;
@@ -2724,8 +2726,15 @@ lex_raw_string (cpp_reader *pfile, cpp_token *token, const uchar *base)
_cpp_release_buff (pfile, accum.first);
cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
"unterminated raw string");
- /* Now pop the buffer that _cpp_get_fresh_line did not. */
+
+ /* Now pop the buffer that get_fresh_line_impl() did not. Popping
+ is not safe if processing a directive, however this cannot
+ happen as we already checked above that a line would be
+ available, and get_fresh_line_impl() can't fail in this
+ case. */
+ gcc_assert (!pfile->state.in_directive);
_cpp_pop_buffer (pfile);
+
return;
}
@@ -3659,11 +3668,14 @@ _cpp_lex_token (cpp_reader *pfile)
}
/* Returns true if a fresh line has been loaded. */
-bool
-_cpp_get_fresh_line (cpp_reader *pfile)
+template <bool lexing_raw_string>
+static bool
+get_fresh_line_impl (cpp_reader *pfile)
{
- /* We can't get a new line until we leave the current directive. */
- if (pfile->state.in_directive)
+ /* We can't get a new line until we leave the current directive, unless we
+ are lexing a raw string, in which case it will be OK as long as we don't
+ pop the current buffer. */
+ if (!lexing_raw_string && pfile->state.in_directive)
return false;
for (;;)
@@ -3679,6 +3691,10 @@ _cpp_get_fresh_line (cpp_reader *pfile)
return true;
}
+ /* We can't change buffers until we leave the current directive. */
+ if (lexing_raw_string && pfile->state.in_directive)
+ return false;
+
/* First, get out of parsing arguments state. */
if (pfile->state.parsing_args)
return false;
@@ -3706,6 +3722,13 @@ _cpp_get_fresh_line (cpp_reader *pfile)
}
}
+bool
+_cpp_get_fresh_line (cpp_reader *pfile)
+{
+ return get_fresh_line_impl<false> (pfile);
+}
+
+
#define IF_NEXT_IS(CHAR, THEN_TYPE, ELSE_TYPE) \
do \
{ \