Skip to content

Commit 5fdc5f3

Browse files
committed
NFC: Clean up TokenRecorder a little
Rename `Bag` to `Tokens`, and query the SourceManager from the ASTContext instead of storing it directly.
1 parent 22c1058 commit 5fdc5f3

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

lib/Parse/Parser.cpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -412,18 +412,17 @@ namespace {
412412
/// underlying corrected token stream.
413413
class TokenRecorder: public ConsumeTokenReceiver {
414414
ASTContext &Ctx;
415-
SourceManager &SM;
415+
unsigned BufferID;
416416

417417
// Token list ordered by their appearance in the source file.
418-
std::vector<Token> &Bag;
419-
unsigned BufferID;
418+
std::vector<Token> &Tokens;
420419

421420
// Registered token kind change. These changes are regiestered before the
422421
// token is consumed, so we need to keep track of them here.
423422
llvm::DenseMap<const void*, tok> TokenKindChangeMap;
424423

425424
std::vector<Token>::iterator lower_bound(SourceLoc Loc) {
426-
return token_lower_bound(Bag, Loc);
425+
return token_lower_bound(Tokens, Loc);
427426
}
428427

429428
std::vector<Token>::iterator lower_bound(Token Tok) {
@@ -432,9 +431,9 @@ class TokenRecorder: public ConsumeTokenReceiver {
432431

433432
void relexComment(CharSourceRange CommentRange,
434433
llvm::SmallVectorImpl<Token> &Scratch) {
435-
Lexer L(Ctx.LangOpts, Ctx.SourceMgr, BufferID, nullptr, LexerMode::Swift,
436-
HashbangMode::Disallowed,
437-
CommentRetentionMode::ReturnAsTokens,
434+
auto &SM = Ctx.SourceMgr;
435+
Lexer L(Ctx.LangOpts, SM, BufferID, nullptr, LexerMode::Swift,
436+
HashbangMode::Disallowed, CommentRetentionMode::ReturnAsTokens,
438437
TriviaRetentionMode::WithoutTrivia,
439438
SM.getLocOffsetInBuffer(CommentRange.getStart(), BufferID),
440439
SM.getLocOffsetInBuffer(CommentRange.getEnd(), BufferID));
@@ -449,19 +448,18 @@ class TokenRecorder: public ConsumeTokenReceiver {
449448
}
450449

451450
public:
452-
TokenRecorder(SourceFile &SF, unsigned BufferID):
453-
Ctx(SF.getASTContext()),
454-
SM(SF.getASTContext().SourceMgr),
455-
Bag(SF.getTokenVector()),
456-
BufferID(BufferID) {};
451+
TokenRecorder(SourceFile &SF, unsigned BufferID)
452+
: Ctx(SF.getASTContext()), BufferID(BufferID),
453+
Tokens(SF.getTokenVector()) {}
457454

458455
void finalize() override {
456+
auto &SM = Ctx.SourceMgr;
459457

460458
// We should consume the comments at the end of the file that don't attach
461459
// to any tokens.
462460
SourceLoc TokEndLoc;
463-
if (!Bag.empty()) {
464-
Token Last = Bag.back();
461+
if (!Tokens.empty()) {
462+
Token Last = Tokens.back();
465463
TokEndLoc = Last.getLoc().getAdvancedLoc(Last.getLength());
466464
} else {
467465

@@ -473,14 +471,14 @@ class TokenRecorder: public ConsumeTokenReceiver {
473471
SM.getRangeForBuffer(BufferID).getEnd()),
474472
Scratch);
475473
// Accept these orphaned comments.
476-
Bag.insert(Bag.end(), Scratch.begin(), Scratch.end());
474+
Tokens.insert(Tokens.end(), Scratch.begin(), Scratch.end());
477475
}
478476

479477
void registerTokenKindChange(SourceLoc Loc, tok NewKind) override {
480478
// If a token with the same location is already in the bag, update its kind.
481479
auto Pos = lower_bound(Loc);
482-
if (Pos != Bag.end() && Pos->getLoc().getOpaquePointerValue() ==
483-
Loc.getOpaquePointerValue()) {
480+
if (Pos != Tokens.end() &&
481+
Pos->getLoc().getOpaquePointerValue() == Loc.getOpaquePointerValue()) {
484482
Pos->setKind(NewKind);
485483
return;
486484
}
@@ -496,8 +494,8 @@ class TokenRecorder: public ConsumeTokenReceiver {
496494

497495
// If a token with the same location is already in the bag, skip this token.
498496
auto Pos = lower_bound(Tok);
499-
if (Pos != Bag.end() && Pos->getLoc().getOpaquePointerValue() ==
500-
Tok.getLoc().getOpaquePointerValue()) {
497+
if (Pos != Tokens.end() && Pos->getLoc().getOpaquePointerValue() ==
498+
Tok.getLoc().getOpaquePointerValue()) {
501499
return;
502500
}
503501

@@ -516,7 +514,7 @@ class TokenRecorder: public ConsumeTokenReceiver {
516514
}
517515

518516
TokensToConsume.push_back(Tok);
519-
Bag.insert(Pos, TokensToConsume.begin(), TokensToConsume.end());
517+
Tokens.insert(Pos, TokensToConsume.begin(), TokensToConsume.end());
520518
}
521519
};
522520
} // End of an anonymous namespace.

0 commit comments

Comments
 (0)