Skip to content

Commit 633c6c0

Browse files
hokeintstellar
authored andcommitted
[Lex] Fix a crash in updateConsecutiveMacroArgTokens.
Fixes #60722. Differential Revision: https://reviews.llvm.org/D144054 (cherry picked from commit 341dd60)
1 parent e0044a6 commit 633c6c0

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

clang/lib/Lex/TokenLexer.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,8 +1020,16 @@ static void updateConsecutiveMacroArgTokens(SourceManager &SM,
10201020
SourceLocation Limit =
10211021
SM.getComposedLoc(BeginFID, SM.getFileIDSize(BeginFID));
10221022
Partition = All.take_while([&](const Token &T) {
1023-
return T.getLocation() >= BeginLoc && T.getLocation() < Limit &&
1024-
NearLast(T.getLocation());
1023+
// NOTE: the Limit is included! The lexer recovery only ever inserts a
1024+
// single token past the end of the FileID, specifically the ) when a
1025+
// macro-arg containing a comma should be guarded by parentheses.
1026+
//
1027+
// It is safe to include the Limit here because SourceManager allocates
1028+
// FileSize + 1 for each SLocEntry.
1029+
//
1030+
// See https://github.com/llvm/llvm-project/issues/60722.
1031+
return T.getLocation() >= BeginLoc && T.getLocation() <= Limit
1032+
&& NearLast(T.getLocation());
10251033
});
10261034
}
10271035
assert(!Partition.empty());
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %clang -cc1 -fsyntax-only -verify %s 2>&1
2+
3+
#define X(val2) Y(val2++) // expected-note {{macro 'X' defined here}}
4+
#define Y(expression) expression ;
5+
6+
void foo() {
7+
// https://github.com/llvm/llvm-project/issues/60722:
8+
//
9+
// - Due to to the error recovery, the lexer inserts a pair of () around the
10+
// macro argument int{,}, so we will see [(, int, {, ,, }, )] tokens.
11+
// - however, the size of file id for the macro argument only takes account
12+
// the written tokens int{,} , and the extra inserted ) token points to the
13+
// Limit source location which triggered an empty Partition violation.
14+
X(int{,}); // expected-error {{too many arguments provided to function-like macro invocation}} \
15+
expected-error {{expected expression}} \
16+
expected-note {{parentheses are required around macro argument containing braced initializer list}}
17+
}

0 commit comments

Comments
 (0)