Skip to content

Commit 44bf526

Browse files
committed
[gardening] Avoid copying source locs to a new vector, make a wrapper for token iterator. NFC
1 parent 784f18f commit 44bf526

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,21 +1846,40 @@ class FormatWalker: public ide::SourceEntityWalker {
18461846
SourceLoc FoundSibling;
18471847
SourceManager &SM;
18481848
std::vector<Token> &Tokens;
1849-
std::vector<SourceLoc> TokenLocs;
18501849
SourceLoc &TargetLoc;
18511850
TokenIt TI;
18521851
bool NeedExtraIndentation;
18531852

1853+
class SourceLocIterator : public std::iterator<std::input_iterator_tag,
1854+
SourceLoc>
1855+
{
1856+
TokenIt It;
1857+
public:
1858+
SourceLocIterator(TokenIt It) :It(It) {}
1859+
SourceLocIterator(const SourceLocIterator& mit) : It(mit.It) {}
1860+
SourceLocIterator& operator++() {++It; return *this;}
1861+
SourceLocIterator operator++(int) {
1862+
SourceLocIterator tmp(*this);
1863+
operator++();
1864+
return tmp;
1865+
}
1866+
bool operator==(const SourceLocIterator& rhs) {return It==rhs.It;}
1867+
bool operator!=(const SourceLocIterator& rhs) {return It!=rhs.It;}
1868+
SourceLoc operator*() {return It->getLoc();}
1869+
};
1870+
18541871
void adjustTokenIteratorToImmediateAfter(SourceLoc End) {
1855-
auto Lower = std::lower_bound(TokenLocs.begin(), TokenLocs.end(), End,
1872+
SourceLocIterator LocBegin(Tokens.begin());
1873+
SourceLocIterator LocEnd(Tokens.end());
1874+
auto Lower = std::lower_bound(LocBegin, LocEnd, End,
18561875
[&](SourceLoc L, SourceLoc R) {
18571876
return SM.isBeforeInBuffer(L, R);
18581877
});
18591878
if (*Lower == End) {
18601879
Lower ++;
18611880
}
18621881
TI = Tokens.begin();
1863-
std::advance(TI, Lower - TokenLocs.begin());
1882+
std::advance(TI, std::distance(LocBegin, Lower));
18641883
}
18651884

18661885
bool isImmediateAfterSeparator(SourceLoc End, tok Separator) {
@@ -1892,11 +1911,7 @@ class FormatWalker: public ide::SourceEntityWalker {
18921911
SiblingCollector(SourceManager &SM, std::vector<Token> &Tokens,
18931912
SourceLoc &TargetLoc) : SM(SM), Tokens(Tokens),
18941913
TargetLoc(TargetLoc), TI(Tokens.begin()),
1895-
NeedExtraIndentation(false) {
1896-
for (Token &T : Tokens) {
1897-
TokenLocs.push_back(T.getLoc());
1898-
}
1899-
}
1914+
NeedExtraIndentation(false) {}
19001915

19011916
void collect(ASTNode Node) {
19021917
if (FoundSibling.isValid())

0 commit comments

Comments
 (0)