preprocessor: Fix cpp_avoid_paste for digit separators
authorJoseph Myers <joseph@codesourcery.com>
Tue, 11 May 2021 18:54:32 +0000 (18:54 +0000)
committerJoseph Myers <joseph@codesourcery.com>
Tue, 11 May 2021 18:54:32 +0000 (18:54 +0000)
commit3e3fdf3d5217e5a2d075ca399b557b2e886dcd18
tree70b8026d4dfe436fc83dd55fd8f7f6cb11020e52
parent21dfb22920ce32fcf336eac4513fa44de28819e0
preprocessor: Fix cpp_avoid_paste for digit separators

The libcpp function cpp_avoid_paste is used to insert whitespace in
preprocessed output where needed to avoid two consecutive
preprocessing tokens, that logically (e.g. when stringized) do not
have whitespace between them, from being incorrectly lexed as one when
the preprocessed input is reread by a compiler.

This fails to allow for digit separators, so meaning that invalid
code, that has a CPP_NUMBER (from a macro expansion) followed by a
character literal, can result in preprocessed output with a valid use
of digit separators, so that required syntax errors do not occur when
compiling with -save-temps.  Fix this by handling that case in
cpp_avoid_paste (as with other cases in cpp_avoid_paste, this doesn't
try to check whether the language version in use supports digit
separators; it's always OK to have unnecessary whitespace in
preprocessed output).

Note: there are other cases, with various kinds of wide character or
string literal following a CPP_NUMBER, where spurious pasting of
preprocessing tokens can occur but the sequence of tokens remains
invalid both before and after that pasting.  Maybe cpp_avoid_paste
should also handle those cases (and similar cases after a CPP_NAME),
to ensure the sequence of preprocessing tokens in preprocessed output
is exactly right, whether or not it affects whether syntax errors
occur.  This patch only addresses the case with digit separators where
invalid code can fail to be diagnosed without the space inserted.

Bootstrapped with no regressions for x86_64-pc-linux-gnu.

libcpp/
* lex.c (cpp_avoid_paste): Do not allow pasting CPP_NUMBER with
CPP_CHAR.

gcc/testsuite/
* g++.dg/cpp1y/digit-sep-paste.C, gcc.dg/c2x-digit-separators-3.c:
New tests.
gcc/testsuite/g++.dg/cpp1y/digit-sep-paste.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/c2x-digit-separators-3.c [new file with mode: 0644]
libcpp/lex.c