Skip to content

Commit 1d98e2d

Browse files
committed
[Lexer] Don't include backtick length into comment length
Although backtick is a kind of trivia piece, Token::getCommentRange doesn't take it into account. Invalid Token::getCommentRange used to cause compiler crash. rdar://problem/42492793 https://bugs.swift.org/browse/SR-8315
1 parent d40059e commit 1d98e2d

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

lib/Parse/Lexer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,16 @@ void Lexer::formToken(tok Kind, const char *TokStart, bool MultilineString) {
279279
}
280280
unsigned CommentLength = 0;
281281
if (RetainComments == CommentRetentionMode::AttachToNextToken) {
282+
// 'CommentLength' here is the length from the *first* comment to the
283+
// token text (or its backtick if exist).
282284
auto Iter = llvm::find_if(LeadingTrivia, [](const TriviaPiece &Piece) {
283285
return Piece.isComment();
284286
});
285287
for (auto End = LeadingTrivia.end(); Iter != End; Iter++) {
288+
if (Iter->getKind() == TriviaKind::Backtick)
289+
// Since Token::getCommentRange() doesn't take backtick into account,
290+
// we cannot include length of backtick.
291+
break;
286292
CommentLength += Iter->getTextLength();
287293
}
288294
}

validation-test/IDE/crashers_2/0021-lexer-commentlength.swift

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*comment*/`init`()
2+
foo();/*comment*/`var`()
3+
4+
// RUN: %target-swift-ide-test -syntax-coloring -source-filename %s

0 commit comments

Comments
 (0)