@@ -278,8 +278,14 @@ void Lexer::formToken(tok Kind, const char *TokStart, bool MultilineString) {
278
278
Kind = tok::eof;
279
279
}
280
280
unsigned CommentLength = 0 ;
281
- if (RetainComments == CommentRetentionMode::AttachToNextToken && SeenComment)
282
- CommentLength = TokStart - LastCommentBlockStart;
281
+ if (RetainComments == CommentRetentionMode::AttachToNextToken) {
282
+ auto Iter = llvm::find_if (LeadingTrivia, [](const TriviaPiece &Piece) {
283
+ return Piece.isComment ();
284
+ });
285
+ for (auto End = LeadingTrivia.end (); Iter != End; Iter++) {
286
+ CommentLength += Iter->getTextLength ();
287
+ }
288
+ }
283
289
284
290
StringRef TokenText { TokStart, static_cast <size_t >(CurPtr - TokStart) };
285
291
@@ -2160,10 +2166,6 @@ void Lexer::lexImpl() {
2160
2166
NextToken.setAtStartOfLine (false );
2161
2167
}
2162
2168
2163
- // Remember where we started so that we can find the comment range.
2164
- LastCommentBlockStart = CurPtr;
2165
- SeenComment = false ;
2166
-
2167
2169
lexTrivia (LeadingTrivia, /* IsForTrailingTrivia */ false );
2168
2170
2169
2171
// Remember the start of the token so we can form the text range.
@@ -2253,14 +2255,12 @@ void Lexer::lexImpl() {
2253
2255
case ' /' :
2254
2256
if (CurPtr[0 ] == ' /' ) { // "//"
2255
2257
skipSlashSlashComment (/* EatNewline=*/ true );
2256
- SeenComment = true ;
2257
2258
assert (isKeepingComments () &&
2258
2259
" Non token comment should be eaten by lexTrivia as LeadingTrivia" );
2259
2260
return formToken (tok::comment, TokStart);
2260
2261
}
2261
2262
if (CurPtr[0 ] == ' *' ) { // "/*"
2262
2263
skipSlashStarComment ();
2263
- SeenComment = true ;
2264
2264
assert (isKeepingComments () &&
2265
2265
" Non token comment should be eaten by lexTrivia as LeadingTrivia" );
2266
2266
return formToken (tok::comment, TokStart);
@@ -2393,7 +2393,6 @@ void Lexer::lexTrivia(syntax::Trivia &Pieces, bool IsForTrailingTrivia) {
2393
2393
break ;
2394
2394
} else if (*CurPtr == ' /' ) {
2395
2395
// '// ...' comment.
2396
- SeenComment = true ;
2397
2396
bool isDocComment = CurPtr[1 ] == ' /' ;
2398
2397
skipSlashSlashComment (/* EatNewline=*/ false );
2399
2398
size_t Length = CurPtr - TriviaStart;
@@ -2403,7 +2402,6 @@ void Lexer::lexTrivia(syntax::Trivia &Pieces, bool IsForTrailingTrivia) {
2403
2402
goto Restart;
2404
2403
} else if (*CurPtr == ' *' ) {
2405
2404
// '/* ... */' comment.
2406
- SeenComment = true ;
2407
2405
bool isDocComment = CurPtr[1 ] == ' *' ;
2408
2406
skipSlashStarComment ();
2409
2407
size_t Length = CurPtr - TriviaStart;
0 commit comments