Skip to content

Commit 53fdf19

Browse files
committed
[Parser] Eliminate 'CommentRetentionMode::None' in Lexer
Lexer should always set `Token.CommentLength` correctly because it necessary for restoring to the token position with comments. Otherwise, 'Token::isAtStartOfLine()' might not correctly set.
1 parent 3bbd7da commit 53fdf19

File tree

11 files changed

+48
-47
lines changed

11 files changed

+48
-47
lines changed

include/swift/Parse/Lexer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ namespace swift {
3939
template<typename ...T> struct Diag;
4040

4141
enum class CommentRetentionMode {
42-
None,
4342
AttachToNextToken,
4443
ReturnAsTokens,
4544
};
@@ -185,7 +184,7 @@ class Lexer {
185184
const LangOptions &Options, const SourceManager &SourceMgr,
186185
unsigned BufferID, DiagnosticEngine *Diags, LexerMode LexMode,
187186
HashbangMode HashbangAllowed = HashbangMode::Disallowed,
188-
CommentRetentionMode RetainComments = CommentRetentionMode::None);
187+
CommentRetentionMode RetainComments = CommentRetentionMode::AttachToNextToken);
189188

190189
/// Create a lexer that scans a subrange of the source buffer.
191190
Lexer(const LangOptions &Options, const SourceManager &SourceMgr,

include/swift/Parse/Parser.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,10 @@ class Parser {
693693
return Context.LangOpts.EnableExperimentalConcurrency;
694694
}
695695

696+
bool shouldAttachCommentToDecl() {
697+
return Context.LangOpts.AttachCommentsToDecls;
698+
}
699+
696700
/// Returns true if a Swift declaration starts after the current token,
697701
/// otherwise returns false.
698702
bool isNextStartOfSwiftDecl() {

lib/IDE/Formatting.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static void widenOrSet(SourceRange &First, SourceRange Second) {
4949
static bool isFirstTokenOnLine(SourceManager &SM, SourceLoc Loc) {
5050
assert(Loc.isValid());
5151
SourceLoc LineStart = Lexer::getLocForStartOfLine(SM, Loc);
52-
CommentRetentionMode SkipComments = CommentRetentionMode::None;
52+
CommentRetentionMode SkipComments = CommentRetentionMode::AttachToNextToken;
5353
Token First = Lexer::getTokenAtLocation(SM, LineStart, SkipComments);
5454
return First.getLoc() == Loc;
5555
}
@@ -76,8 +76,8 @@ static std::optional<Token> getTokenAfter(SourceManager &SM, SourceLoc Loc,
7676
bool SkipComments = true) {
7777
assert(Loc.isValid());
7878
CommentRetentionMode Mode = SkipComments
79-
? CommentRetentionMode::None
80-
: CommentRetentionMode::ReturnAsTokens;
79+
? CommentRetentionMode::AttachToNextToken
80+
: CommentRetentionMode::ReturnAsTokens;
8181
assert(Lexer::getTokenAtLocation(SM, Loc, Mode).getLoc() == Loc);
8282
SourceLoc End = Lexer::getLocForEndOfToken(SM, Loc);
8383
Token Next = Lexer::getTokenAtLocation(SM, End, Mode);
@@ -814,8 +814,8 @@ class OutdentChecker: protected RangeWalker {
814814
} else if (R.isValid()) {
815815
// Check condition 2a.
816816
SourceLoc LineStart = Lexer::getLocForStartOfLine(SM, R);
817-
Token First = Lexer::getTokenAtLocation(SM, LineStart,
818-
CommentRetentionMode::None);
817+
Token First = Lexer::getTokenAtLocation(
818+
SM, LineStart, CommentRetentionMode::AttachToNextToken);
819819
IsOutdenting |= First.getLoc() == R;
820820
}
821821

lib/Parse/Lexer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ Token Lexer::getTokenAt(SourceLoc Loc) {
270270
"location from the wrong buffer");
271271

272272
Lexer L(LangOpts, SourceMgr, BufferID, getUnderlyingDiags(), LexMode,
273-
HashbangMode::Allowed, CommentRetentionMode::None);
273+
HashbangMode::Allowed, CommentRetentionMode::AttachToNextToken);
274274
L.restoreState(State(Loc));
275275
return L.peekNextToken();
276276
}
@@ -3029,7 +3029,7 @@ static SourceLoc getLocForStartOfTokenInBuf(SourceManager &SM,
30293029
LangOptions FakeLangOptions;
30303030

30313031
Lexer L(FakeLangOptions, SM, BufferID, nullptr, LexerMode::Swift,
3032-
HashbangMode::Allowed, CommentRetentionMode::None,
3032+
HashbangMode::Allowed, CommentRetentionMode::AttachToNextToken,
30333033
BufferStart, BufferEnd);
30343034

30353035
// Lex tokens until we find the token that contains the source location.

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6204,7 +6204,7 @@ ParserStatus Parser::parseDecl(bool IsAtStartOfLineOrPreviousHadSemi,
62046204

62056205
// Parse attributes.
62066206
DeclAttributes Attributes;
6207-
if (Tok.hasComment())
6207+
if (Tok.hasComment() && shouldAttachCommentToDecl())
62086208
Attributes.add(new (Context) RawDocCommentAttr(Tok.getCommentRange()));
62096209
ParserStatus AttrStatus = parseDeclAttributeList(
62106210
Attributes, IfConfigsAreDeclAttrs);

lib/Parse/ParseGeneric.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Parser::parseGenericParametersBeforeWhere(SourceLoc LAngleLoc,
5656

5757
// Parse attributes.
5858
DeclAttributes attributes;
59-
if (Tok.hasComment())
59+
if (Tok.hasComment() && shouldAttachCommentToDecl())
6060
attributes.add(new (Context) RawDocCommentAttr(Tok.getCommentRange()));
6161
parseDeclAttributeList(attributes);
6262

lib/Parse/Parser.cpp

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -341,21 +341,15 @@ Parser::Parser(unsigned BufferID, SourceFile &SF, SILParserStateBase *SIL,
341341
PersistentParserState *PersistentState)
342342
: Parser(BufferID, SF, &SF.getASTContext().Diags, SIL, PersistentState) {}
343343

344-
Parser::Parser(unsigned BufferID, SourceFile &SF, DiagnosticEngine* LexerDiags,
345-
SILParserStateBase *SIL,
346-
PersistentParserState *PersistentState)
347-
: Parser(
348-
std::unique_ptr<Lexer>(new Lexer(
349-
SF.getASTContext().LangOpts, SF.getASTContext().SourceMgr,
350-
BufferID, LexerDiags,
351-
sourceFileKindToLexerMode(SF.Kind),
352-
SF.Kind == SourceFileKind::Main
353-
? HashbangMode::Allowed
354-
: HashbangMode::Disallowed,
355-
SF.getASTContext().LangOpts.AttachCommentsToDecls
356-
? CommentRetentionMode::AttachToNextToken
357-
: CommentRetentionMode::None)),
358-
SF, SIL, PersistentState) {}
344+
Parser::Parser(unsigned BufferID, SourceFile &SF, DiagnosticEngine *LexerDiags,
345+
SILParserStateBase *SIL, PersistentParserState *PersistentState)
346+
: Parser(std::unique_ptr<Lexer>(new Lexer(
347+
SF.getASTContext().LangOpts, SF.getASTContext().SourceMgr,
348+
BufferID, LexerDiags, sourceFileKindToLexerMode(SF.Kind),
349+
SF.Kind == SourceFileKind::Main ? HashbangMode::Allowed
350+
: HashbangMode::Disallowed,
351+
CommentRetentionMode::AttachToNextToken)),
352+
SF, SIL, PersistentState) {}
359353

360354
namespace {
361355

@@ -1243,12 +1237,10 @@ ParserUnit::ParserUnit(SourceManager &SM, SourceFileKind SFKind,
12431237
: Impl(*new Implementation(SM, SFKind, BufferID, LangOptions(), "input")) {
12441238

12451239
std::unique_ptr<Lexer> Lex;
1246-
Lex.reset(new Lexer(Impl.LangOpts, SM,
1247-
BufferID, &Impl.Diags,
1248-
LexerMode::Swift,
1249-
HashbangMode::Allowed,
1250-
CommentRetentionMode::None,
1251-
Offset, EndOffset));
1240+
Lex.reset(new Lexer(Impl.LangOpts, SM, BufferID, &Impl.Diags,
1241+
LexerMode::Swift, HashbangMode::Allowed,
1242+
CommentRetentionMode::AttachToNextToken, Offset,
1243+
EndOffset));
12521244
Impl.TheParser.reset(new Parser(std::move(Lex), *Impl.SF, /*SIL=*/nullptr,
12531245
/*PersistentState=*/nullptr));
12541246
}

lib/Refactoring/Async/AsyncConverter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,8 @@ void AsyncConverter::addFuncDecl(const FuncDecl *FD) {
651651
LeftEndLoc = Lexer::getLocForEndOfToken(
652652
SM, Params->get(TopHandler.Index - 1)->getEndLoc());
653653
// Skip to the end of any comments
654-
Token Next =
655-
Lexer::getTokenAtLocation(SM, LeftEndLoc, CommentRetentionMode::None);
654+
Token Next = Lexer::getTokenAtLocation(
655+
SM, LeftEndLoc, CommentRetentionMode::AttachToNextToken);
656656
if (Next.getKind() != tok::NUM_TOKENS)
657657
LeftEndLoc = Next.getLoc();
658658
break;

lib/Sema/MiscDiagnostics.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,8 +2559,9 @@ class ImplicitSelfUsageChecker : public BaseDiagnosticWalker {
25592559
// insert 'self,'. If it wasn't a valid entry, then we will at least not
25602560
// be introducing any new errors/warnings...
25612561
const auto locAfterBracket = brackets.Start.getAdvancedLoc(1);
2562-
const auto nextAfterBracket = Lexer::getTokenAtLocation(
2563-
Ctx.SourceMgr, locAfterBracket, CommentRetentionMode::None);
2562+
const auto nextAfterBracket =
2563+
Lexer::getTokenAtLocation(Ctx.SourceMgr, locAfterBracket,
2564+
CommentRetentionMode::AttachToNextToken);
25642565
if (nextAfterBracket.getLoc() != brackets.End)
25652566
diag.fixItInsertAfter(brackets.Start, "self, ");
25662567
else
@@ -2581,8 +2582,8 @@ class ImplicitSelfUsageChecker : public BaseDiagnosticWalker {
25812582
// opening brace of the closure, we may need to pad the fix-it
25822583
// with a space.
25832584
const auto nextLoc = closureExpr->getLoc().getAdvancedLoc(1);
2584-
const auto next = Lexer::getTokenAtLocation(Ctx.SourceMgr, nextLoc,
2585-
CommentRetentionMode::None);
2585+
const auto next = Lexer::getTokenAtLocation(
2586+
Ctx.SourceMgr, nextLoc, CommentRetentionMode::AttachToNextToken);
25862587
std::string trailing = next.getLoc() == nextLoc ? " " : "";
25872588

25882589
diag.fixItInsertAfter(closureExpr->getLoc(), " [self] in" + trailing);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %target-typecheck-verify-swift -verify-ignore-unrelated
2+
3+
func test() {
4+
_ = "test"
5+
/* comment */ #if true
6+
#endif
7+
}

0 commit comments

Comments
 (0)