Skip to content

Commit cd824a4

Browse files
committed
[clang][Syntax] Handle invalid source range in expandedTokens.
Differential Revision: https://reviews.llvm.org/D99934
1 parent 35bc756 commit cd824a4

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

clang/lib/Tooling/Syntax/Tokens.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,21 +189,29 @@ void TokenBuffer::indexExpandedTokens() {
189189
return;
190190
ExpandedTokIndex.reserve(ExpandedTokens.size());
191191
// Index ExpandedTokens for faster lookups by SourceLocation.
192-
for (size_t I = 0, E = ExpandedTokens.size(); I != E; ++I)
193-
ExpandedTokIndex[ExpandedTokens[I].location()] = I;
192+
for (size_t I = 0, E = ExpandedTokens.size(); I != E; ++I) {
193+
SourceLocation Loc = ExpandedTokens[I].location();
194+
if (Loc.isValid())
195+
ExpandedTokIndex[Loc] = I;
196+
}
194197
}
195198

196199
llvm::ArrayRef<syntax::Token> TokenBuffer::expandedTokens(SourceRange R) const {
200+
if (R.isInvalid())
201+
return {};
197202
if (!ExpandedTokIndex.empty()) {
198203
// Quick lookup if `R` is a token range.
199204
// This is a huge win since majority of the users use ranges provided by an
200205
// AST. Ranges in AST are token ranges from expanded token stream.
201206
const auto B = ExpandedTokIndex.find(R.getBegin());
202207
const auto E = ExpandedTokIndex.find(R.getEnd());
203208
if (B != ExpandedTokIndex.end() && E != ExpandedTokIndex.end()) {
209+
const Token *L = ExpandedTokens.data() + B->getSecond();
204210
// Add 1 to End to make a half-open range.
205-
return {ExpandedTokens.data() + B->getSecond(),
206-
ExpandedTokens.data() + E->getSecond() + 1};
211+
const Token *R = ExpandedTokens.data() + E->getSecond() + 1;
212+
if (L > R)
213+
return {};
214+
return {L, R};
207215
}
208216
}
209217
// Slow case. Use `isBeforeInTranslationUnit` to binary search for the

0 commit comments

Comments
 (0)