Skip to content

Commit a8c0136

Browse files
committed
[Lexer] Eliminate unnecessary calls to TriviaLexer::lexTrivia
If the lexer itself keeps track of where the first comment of a token starts, we can avoid parsing trivia into pieces.
1 parent b6746f0 commit a8c0136

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

include/swift/Parse/Lexer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ class Lexer {
129129
/// The StringRef points into the source buffer that is currently being lexed.
130130
StringRef TrailingTrivia;
131131

132+
/// The location at which the comment of the next token starts. \c nullptr if
133+
/// the next token doesn't have a comment.
134+
const char *CommentStart;
135+
132136
Lexer(const Lexer&) = delete;
133137
void operator=(const Lexer&) = delete;
134138

lib/Parse/Lexer.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,8 @@ void Lexer::formToken(tok Kind, const char *TokStart) {
283283
}
284284
unsigned CommentLength = 0;
285285
if (RetainComments == CommentRetentionMode::AttachToNextToken) {
286-
auto LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia);
287-
// 'CommentLength' here is the length from the *first* comment to the
288-
// token text (or its backtick if exist).
289-
auto Iter = llvm::find_if(LeadingTriviaPieces, [](const ParsedTriviaPiece &Piece) {
290-
return isCommentTriviaKind(Piece.getKind());
291-
});
292-
for (auto End = LeadingTriviaPieces.end(); Iter != End; Iter++) {
293-
CommentLength += Iter->getLength();
286+
if (CommentStart) {
287+
CommentLength = TokStart - CommentStart;
294288
}
295289
}
296290

@@ -2533,6 +2527,7 @@ Token Lexer::getTokenAtLocation(const SourceManager &SM, SourceLoc Loc,
25332527

25342528
StringRef Lexer::lexTrivia(bool IsForTrailingTrivia) {
25352529
const char *AllTriviaStart = CurPtr;
2530+
CommentStart = nullptr;
25362531

25372532
Restart:
25382533
const char *TriviaStart = CurPtr;
@@ -2562,10 +2557,16 @@ StringRef Lexer::lexTrivia(bool IsForTrailingTrivia) {
25622557
// Don't try to lex comments here if we are lexing comments as Tokens.
25632558
break;
25642559
} else if (*CurPtr == '/') {
2560+
if (CommentStart == nullptr) {
2561+
CommentStart = CurPtr - 1;
2562+
}
25652563
// '// ...' comment.
25662564
skipSlashSlashComment(/*EatNewline=*/false);
25672565
goto Restart;
25682566
} else if (*CurPtr == '*') {
2567+
if (CommentStart == nullptr) {
2568+
CommentStart = CurPtr - 1;
2569+
}
25692570
// '/* ... */' comment.
25702571
skipSlashStarComment();
25712572
goto Restart;

lib/Parse/ParseExpr.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,8 +2017,6 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {
20172017
SyntaxContext->addToken(CloseDelimiter, StringRef(), EntireTrailingTrivia);
20182018
} else {
20192019
// Without custom delimiter the quote owns trailing trivia.
2020-
auto EntireTrailingTriviaPieces =
2021-
TriviaLexer::lexTrivia(EntireTrailingTrivia);
20222020
SyntaxContext->addToken(CloseQuote, StringRef(), EntireTrailingTrivia);
20232021
}
20242022

0 commit comments

Comments
 (0)