Skip to content

Commit 5571e5c

Browse files
committed
[Lexer] Clear trivia at the top of lexImpl()
To make sure we only parse trivia for the current token.
1 parent 9b32c62 commit 5571e5c

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

include/swift/Parse/Lexer.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,8 @@ class Lexer {
233233
Result = NextToken;
234234
LeadingTriviaResult = {LeadingTrivia};
235235
TrailingTriviaResult = {TrailingTrivia};
236-
if (Result.isNot(tok::eof)) {
237-
LeadingTrivia.clear();
238-
TrailingTrivia.clear();
236+
if (Result.isNot(tok::eof))
239237
lexImpl();
240-
}
241238
}
242239

243240
void lex(Token &Result) {
@@ -288,8 +285,6 @@ class Lexer {
288285
void restoreState(State S, bool enableDiagnostics = false) {
289286
assert(S.isValid());
290287
CurPtr = getBufferPtrForSourceLoc(S.Loc);
291-
LeadingTrivia.clear();
292-
TrailingTrivia.clear();
293288
// Don't reemit diagnostics while readvancing the lexer.
294289
llvm::SaveAndRestore<DiagnosticEngine*>
295290
D(Diags, enableDiagnostics ? Diags : nullptr);

lib/Parse/Lexer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,6 +2058,10 @@ void Lexer::lexImpl() {
20582058
assert(CurPtr >= BufferStart &&
20592059
CurPtr <= BufferEnd && "Current pointer out of range!");
20602060

2061+
if (TriviaRetention == TriviaRetentionMode::WithTrivia) {
2062+
LeadingTrivia.clear();
2063+
TrailingTrivia.clear();
2064+
}
20612065
NextToken.setAtStartOfLine(CurPtr == BufferStart);
20622066

20632067
// Remember where we started so that we can find the comment range.

0 commit comments

Comments
 (0)