@@ -243,18 +243,17 @@ class Lexer {
243
243
// / there.
244
244
State getStateForBeginningOfToken (const Token &Tok,
245
245
const syntax::Trivia &LeadingTrivia = {}) const {
246
- // If trivia parsing mode, start position of trivia is the position we want
247
- // to restore.
248
- if (TriviaRetention == TriviaRetentionMode::WithTrivia)
249
- return State (Tok.getLoc ().getAdvancedLoc (-LeadingTrivia.getTextLength ()));
250
246
251
247
// If the token has a comment attached to it, rewind to before the comment,
252
248
// not just the start of the token. This ensures that we will re-lex and
253
249
// reattach the comment to the token if rewound to this state.
254
250
SourceLoc TokStart = Tok.getCommentStart ();
255
251
if (TokStart.isInvalid ())
256
252
TokStart = Tok.getLoc ();
257
- return getStateForBeginningOfTokenLoc (TokStart);
253
+ auto S = getStateForBeginningOfTokenLoc (TokStart);
254
+ if (TriviaRetention == TriviaRetentionMode::WithTrivia)
255
+ S.LeadingTrivia = LeadingTrivia.Pieces ;
256
+ return S;
258
257
}
259
258
260
259
State getStateForEndOfTokenLoc (SourceLoc Loc) const {
@@ -273,7 +272,13 @@ class Lexer {
273
272
// Don't reemit diagnostics while readvancing the lexer.
274
273
llvm::SaveAndRestore<DiagnosticEngine*>
275
274
D (Diags, enableDiagnostics ? Diags : nullptr );
275
+
276
276
lexImpl ();
277
+
278
+ // Restore Trivia.
279
+ if (TriviaRetention == TriviaRetentionMode::WithTrivia)
280
+ if (auto <rivia = S.LeadingTrivia )
281
+ LeadingTrivia = std::move (*LTrivia);
277
282
}
278
283
279
284
// / \brief Restore the lexer state to a given state that is located before
0 commit comments