Skip to content

Skip escaped newlines before checking for whitespace in Lexer::getRawToken. #117548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/lib/Lex/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ bool Lexer::getRawToken(SourceLocation Loc, Token &Result,

const char *StrData = Buffer.data()+LocInfo.second;

if (!IgnoreWhiteSpace && isWhitespace(StrData[0]))
if (!IgnoreWhiteSpace && isWhitespace(SkipEscapedNewLines(StrData)[0]))
return true;

// Create a lexer starting at the beginning of this token.
Expand Down
32 changes: 32 additions & 0 deletions clang/unittests/Lex/LexerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,38 @@ TEST_F(LexerTest, RawAndNormalLexSameForLineComments) {
EXPECT_TRUE(ToksView.empty());
}

TEST_F(LexerTest, GetRawTokenOnEscapedNewLineChecksWhitespace) {
const llvm::StringLiteral Source = R"cc(
#define ONE \
1

int i = ONE;
)cc";
std::vector<Token> Toks =
CheckLex(Source, {tok::kw_int, tok::identifier, tok::equal,
tok::numeric_constant, tok::semi});

// Set up by getting the raw token for the `1` in the macro definition.
const Token &OneExpanded = Toks[3];
Token Tok;
ASSERT_FALSE(
Lexer::getRawToken(OneExpanded.getLocation(), Tok, SourceMgr, LangOpts));
// The `ONE`.
ASSERT_EQ(Tok.getKind(), tok::raw_identifier);
ASSERT_FALSE(
Lexer::getRawToken(SourceMgr.getSpellingLoc(OneExpanded.getLocation()),
Tok, SourceMgr, LangOpts));
// The `1` in the macro definition.
ASSERT_EQ(Tok.getKind(), tok::numeric_constant);

// Go back 4 characters: two spaces, one newline, and the backslash.
SourceLocation EscapedNewLineLoc = Tok.getLocation().getLocWithOffset(-4);
// Expect true (=failure) because the whitespace immediately after the
// escaped newline is not ignored.
EXPECT_TRUE(Lexer::getRawToken(EscapedNewLineLoc, Tok, SourceMgr, LangOpts,
/*IgnoreWhiteSpace=*/false));
}

TEST(LexerPreambleTest, PreambleBounds) {
std::vector<std::string> Cases = {
R"cc([[
Expand Down
Loading