[clang] cd824a4 - [clang][Syntax] Handle invalid source range in expandedTokens.
Utkarsh Saxena via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 7 02:19:22 PDT 2021
Author: Utkarsh Saxena
Date: 2021-04-07T11:19:01+02:00
New Revision: cd824a48cccb000655498e2d52129adfda25f49b
URL: https://github.com/llvm/llvm-project/commit/cd824a48cccb000655498e2d52129adfda25f49b
DIFF: https://github.com/llvm/llvm-project/commit/cd824a48cccb000655498e2d52129adfda25f49b.diff
LOG: [clang][Syntax] Handle invalid source range in expandedTokens.
Differential Revision: https://reviews.llvm.org/D99934
Added:
Modified:
clang/lib/Tooling/Syntax/Tokens.cpp
Removed:
################################################################################
diff --git a/clang/lib/Tooling/Syntax/Tokens.cpp b/clang/lib/Tooling/Syntax/Tokens.cpp
index 2326e89ea48ba..8a31e776d0302 100644
--- a/clang/lib/Tooling/Syntax/Tokens.cpp
+++ b/clang/lib/Tooling/Syntax/Tokens.cpp
@@ -189,11 +189,16 @@ void TokenBuffer::indexExpandedTokens() {
return;
ExpandedTokIndex.reserve(ExpandedTokens.size());
// Index ExpandedTokens for faster lookups by SourceLocation.
- for (size_t I = 0, E = ExpandedTokens.size(); I != E; ++I)
- ExpandedTokIndex[ExpandedTokens[I].location()] = I;
+ for (size_t I = 0, E = ExpandedTokens.size(); I != E; ++I) {
+ SourceLocation Loc = ExpandedTokens[I].location();
+ if (Loc.isValid())
+ ExpandedTokIndex[Loc] = I;
+ }
}
llvm::ArrayRef<syntax::Token> TokenBuffer::expandedTokens(SourceRange R) const {
+ if (R.isInvalid())
+ return {};
if (!ExpandedTokIndex.empty()) {
// Quick lookup if `R` is a token range.
// This is a huge win since majority of the users use ranges provided by an
@@ -201,9 +206,12 @@ llvm::ArrayRef<syntax::Token> TokenBuffer::expandedTokens(SourceRange R) const {
const auto B = ExpandedTokIndex.find(R.getBegin());
const auto E = ExpandedTokIndex.find(R.getEnd());
if (B != ExpandedTokIndex.end() && E != ExpandedTokIndex.end()) {
+ const Token *L = ExpandedTokens.data() + B->getSecond();
// Add 1 to End to make a half-open range.
- return {ExpandedTokens.data() + B->getSecond(),
- ExpandedTokens.data() + E->getSecond() + 1};
+ const Token *R = ExpandedTokens.data() + E->getSecond() + 1;
+ if (L > R)
+ return {};
+ return {L, R};
}
}
// Slow case. Use `isBeforeInTranslationUnit` to binary search for the
More information about the cfe-commits
mailing list