Skip to content

Commit 5626c72

Browse files
authored
Refactoring tool: Avoid re-tokenizing when collecting the available refactorings in a given source range. NFC (#11857)
1 parent f5ccc61 commit 5626c72

File tree

5 files changed

+46
-35
lines changed

5 files changed

+46
-35
lines changed

include/swift/Parse/Lexer.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,16 @@ class Lexer {
536536
bool tryLexConflictMarker();
537537
};
538538

539+
/// Given an ordered token \param Array , get the iterator pointing to the first
540+
/// token that is not before \param Loc .
541+
template<typename ArrayTy, typename Iterator = typename ArrayTy::iterator>
542+
Iterator token_lower_bound(ArrayTy &Array, SourceLoc Loc) {
543+
return std::lower_bound(Array.begin(), Array.end(), Loc,
544+
[](const Token &T, SourceLoc L) {
545+
return T.getLoc().getOpaquePointerValue() < L.getOpaquePointerValue();
546+
});
547+
}
548+
539549
} // end namespace swift
540550

541551
#endif

lib/IDE/Refactoring.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,15 +1257,10 @@ struct SimilarExprCollector: public SourceEntityWalker {
12571257

12581258
/// Find all tokens included by an expression.
12591259
llvm::ArrayRef<Token> getExprSlice(Expr *E) {
1260-
auto TokenComp = [&](const Token &LHS, SourceLoc Loc) {
1261-
return SM.isBeforeInBuffer(LHS.getLoc(), Loc);
1262-
};
12631260
SourceLoc StartLoc = E->getStartLoc();
12641261
SourceLoc EndLoc = E->getEndLoc();
1265-
auto StartIt = std::lower_bound(AllTokens.begin(), AllTokens.end(),
1266-
StartLoc, TokenComp);
1267-
auto EndIt = std::lower_bound(AllTokens.begin(), AllTokens.end(),
1268-
EndLoc, TokenComp);
1262+
auto StartIt = token_lower_bound(AllTokens, StartLoc);
1263+
auto EndIt = token_lower_bound(AllTokens, EndLoc);
12691264
assert(StartIt->getLoc() == StartLoc);
12701265
assert(EndIt->getLoc() == EndLoc);
12711266
return AllTokens.slice(StartIt - AllTokens.begin(), EndIt - StartIt + 1);
@@ -1333,16 +1328,16 @@ bool RefactoringActionExtractExprBase::performChange() {
13331328
Collector.walk(BS);
13341329

13351330
if (ExtractRepeated) {
1336-
unsigned BufferId = *TheFile->getBufferID();
1337-
13381331
// Tokenize the brace statement; all expressions should have their tokens
13391332
// in this array.
1340-
std::vector<Token> AllToks(tokenize(Ctx.LangOpts, SM, BufferId,
1341-
/*start offset*/SM.getLocOffsetInBuffer(BS->getStartLoc(), BufferId),
1342-
/*end offset*/SM.getLocOffsetInBuffer(BS->getEndLoc(), BufferId)));
1333+
auto AllTokens = TheFile->getAllTokens();
1334+
auto StartIt = token_lower_bound(AllTokens, BS->getStartLoc());
1335+
auto EndIt = token_lower_bound(AllTokens, BS->getEndLoc());
13431336

13441337
// Collect all expressions we are going to extract.
1345-
SimilarExprCollector(SM, SelectedExpr, AllToks, AllExpressions).walk(BS);
1338+
SimilarExprCollector(SM, SelectedExpr,
1339+
AllTokens.slice(StartIt - AllTokens.begin(), EndIt - StartIt + 1),
1340+
AllExpressions).walk(BS);
13461341
} else {
13471342
AllExpressions.insert(SelectedExpr);
13481343
}

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ struct RangeResolver::Implementation {
875875
};
876876

877877

878-
std::vector<Token> TokensInRange;
878+
ArrayRef<Token> TokensInRange;
879879
SourceLoc Start;
880880
SourceLoc End;
881881

@@ -1066,32 +1066,41 @@ struct RangeResolver::Implementation {
10661066
createInstance(SourceFile &File, unsigned StartOff, unsigned Length) {
10671067
SourceManager &SM = File.getASTContext().SourceMgr;
10681068
unsigned BufferId = File.getBufferID().getValue();
1069-
1070-
LangOptions Opts = File.getASTContext().LangOpts;
1071-
Opts.AttachCommentsToDecls = true;
1072-
std::vector<Token> AllTokens = tokenize(Opts, SM, BufferId, 0, 0, false);
1073-
auto TokenComp = [&](Token &LHS, SourceLoc Loc) {
1074-
return SM.isBeforeInBuffer(LHS.getLoc(), Loc);
1075-
};
1076-
1069+
auto AllTokens = File.getAllTokens();
10771070
SourceLoc StartRaw = SM.getLocForOffset(BufferId, StartOff);
10781071
SourceLoc EndRaw = SM.getLocForOffset(BufferId, StartOff + Length);
10791072

10801073
// This points to the first token after or on the start loc.
1081-
auto StartIt = std::lower_bound(AllTokens.begin(), AllTokens.end(), StartRaw,
1082-
TokenComp);
1074+
auto StartIt = token_lower_bound(AllTokens, StartRaw);
1075+
1076+
// Skip all the comments.
1077+
while(StartIt != AllTokens.end()) {
1078+
if (StartIt->getKind() != tok::comment)
1079+
break;
1080+
StartIt ++;
1081+
}
1082+
1083+
// Erroneous case.
1084+
if (StartIt == AllTokens.end())
1085+
return nullptr;
1086+
10831087
// This points to the first token after or on the end loc;
1084-
auto EndIt = std::lower_bound(AllTokens.begin(), AllTokens.end(), EndRaw,
1085-
TokenComp);
1088+
auto EndIt = token_lower_bound(AllTokens, EndRaw);
1089+
1090+
// Adjust end token to skip comments.
1091+
while (EndIt != AllTokens.begin()) {
1092+
EndIt --;
1093+
if (EndIt->getKind() != tok::comment)
1094+
break;
1095+
}
1096+
10861097
// Erroneous case.
1087-
if (StartIt == AllTokens.end() || EndIt == AllTokens.begin())
1098+
if (EndIt < StartIt)
10881099
return nullptr;
10891100

1090-
// The start token is inclusive.
10911101
unsigned StartIdx = StartIt - AllTokens.begin();
1092-
10931102
return std::unique_ptr<Implementation>(new Implementation(File,
1094-
llvm::makeArrayRef(AllTokens.data() + StartIdx, EndIt - StartIt)));
1103+
AllTokens.slice(StartIdx, EndIt - StartIt + 1)));
10951104
}
10961105

10971106
static std::unique_ptr<Implementation>

lib/Parse/Parser.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,7 @@ class TokenRecorder: public ConsumeTokenReceiver {
308308
llvm::DenseMap<const void*, tok> TokenKindChangeMap;
309309

310310
std::vector<Token>::iterator lower_bound(SourceLoc Loc) {
311-
return std::lower_bound(Bag.begin(), Bag.end(), Loc,
312-
[](const Token &T, SourceLoc L) {
313-
return T.getLoc().getOpaquePointerValue() < L.getOpaquePointerValue();
314-
});
311+
return token_lower_bound(Bag, Loc);
315312
}
316313

317314
std::vector<Token>::iterator lower_bound(Token Tok) {
@@ -400,7 +397,6 @@ class TokenRecorder: public ConsumeTokenReceiver {
400397
llvm::SmallVector<Token, 4> TokensToConsume;
401398
if (Tok.hasComment()) {
402399
relexComment(Tok.getCommentRange(), TokensToConsume);
403-
Tok.clearCommentLength();
404400
}
405401

406402
TokensToConsume.push_back(Tok);

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,6 +2669,7 @@ static int doPrintRangeInfo(const CompilerInvocation &InitInvok,
26692669
CompilerInvocation Invocation(InitInvok);
26702670
Invocation.addInputFilename(SourceFileName);
26712671
Invocation.getLangOptions().DisableAvailabilityChecking = false;
2672+
Invocation.getLangOptions().KeepTokensInSourceFile = true;
26722673

26732674
CompilerInstance CI;
26742675

0 commit comments

Comments
 (0)