Skip to content

Commit 5e55586

Browse files
omochirintaro
authored andcommitted
[Parse] Reduce branch by running lexTrivia always (swiftlang#15137)
1 parent a685272 commit 5e55586

File tree

2 files changed

+38
-47
lines changed

2 files changed

+38
-47
lines changed

include/swift/Parse/Lexer.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,10 @@ class Lexer {
195195
void lex(Token &Result, syntax::Trivia &LeadingTriviaResult,
196196
syntax::Trivia &TrailingTriviaResult) {
197197
Result = NextToken;
198-
LeadingTriviaResult = {LeadingTrivia};
199-
TrailingTriviaResult = {TrailingTrivia};
198+
if (TriviaRetention == TriviaRetentionMode::WithTrivia) {
199+
LeadingTriviaResult = {LeadingTrivia};
200+
TrailingTriviaResult = {TrailingTrivia};
201+
}
200202
if (Result.isNot(tok::eof))
201203
lexImpl();
202204
}

lib/Parse/Lexer.cpp

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,9 @@ void Lexer::formToken(tok Kind, const char *TokStart, bool MultilineString) {
283283

284284
StringRef TokenText { TokStart, static_cast<size_t>(CurPtr - TokStart) };
285285

286-
lexTrivia(TrailingTrivia, /* IsForTrailingTrivia */ true);
286+
if (TriviaRetention == TriviaRetentionMode::WithTrivia) {
287+
lexTrivia(TrailingTrivia, /* IsForTrailingTrivia */ true);
288+
}
287289

288290
NextToken.setToken(Kind, TokenText, CommentLength, MultilineString);
289291
}
@@ -292,11 +294,11 @@ void Lexer::formEscapedIdentifierToken(const char *TokStart) {
292294
assert(CurPtr - TokStart >= 3 && "escaped identifier must be longer than or equal 3 bytes");
293295
assert(TokStart[0] == '`' && "escaped identifier starts with backtick");
294296
assert(CurPtr[-1] == '`' && "escaped identifier ends with backtick");
295-
if (TriviaRetention == TriviaRetentionMode::WithTrivia) {
296-
LeadingTrivia.push_back(TriviaPiece::backtick());
297-
assert(TrailingTrivia.empty() && "TrailingTrivia is empty here");
298-
TrailingTrivia.push_back(TriviaPiece::backtick());
299-
}
297+
298+
LeadingTrivia.push_back(TriviaPiece::backtick());
299+
assert(TrailingTrivia.empty() && "TrailingTrivia is empty here");
300+
TrailingTrivia.push_back(TriviaPiece::backtick());
301+
300302
formToken(tok::identifier, TokStart);
301303
// If this token is at ArtificialEOF, it's forced to be tok::eof. Don't mark
302304
// this as escaped-identifier in this case.
@@ -2159,18 +2161,15 @@ void Lexer::lexImpl() {
21592161
assert(CurPtr >= BufferStart &&
21602162
CurPtr <= BufferEnd && "Current pointer out of range!");
21612163

2162-
if (TriviaRetention == TriviaRetentionMode::WithTrivia) {
2163-
LeadingTrivia.clear();
2164-
TrailingTrivia.clear();
2165-
}
2164+
LeadingTrivia.clear();
2165+
TrailingTrivia.clear();
2166+
21662167
if (CurPtr == BufferStart) {
21672168
if (BufferStart < ContentStart) {
21682169
size_t BOMLen = ContentStart - BufferStart;
21692170
assert(BOMLen == 3 && "UTF-8 BOM is 3 bytes");
2170-
if (TriviaRetention == TriviaRetentionMode::WithTrivia) {
2171-
// Add UTF-8 BOM to LeadingTrivia.
2172-
LeadingTrivia.push_back(TriviaPiece::garbageText({CurPtr, BOMLen}));
2173-
}
2171+
// Add UTF-8 BOM to LeadingTrivia.
2172+
LeadingTrivia.push_back(TriviaPiece::garbageText({CurPtr, BOMLen}));
21742173
CurPtr += BOMLen;
21752174
}
21762175
NextToken.setAtStartOfLine(true);
@@ -2182,7 +2181,6 @@ void Lexer::lexImpl() {
21822181
LastCommentBlockStart = CurPtr;
21832182
SeenComment = false;
21842183

2185-
Restart:
21862184
lexTrivia(LeadingTrivia, /* IsForTrailingTrivia */ false);
21872185

21882186
// Remember the start of the token so we can form the text range.
@@ -2198,24 +2196,23 @@ void Lexer::lexImpl() {
21982196
return lexOperatorIdentifier();
21992197

22002198
bool ShouldTokenize = lexUnknown(/*EmitDiagnosticsIfToken=*/true);
2201-
if (ShouldTokenize) {
2202-
return formToken(tok::unknown, TokStart);
2203-
}
2204-
goto Restart; // Skip presumed whitespace.
2199+
assert(
2200+
ShouldTokenize &&
2201+
"Invalid UTF-8 sequence should be eaten by lexTrivia as LeadingTrivia");
2202+
(void)ShouldTokenize;
2203+
return formToken(tok::unknown, TokStart);
22052204
}
22062205

22072206
case '\n':
22082207
case '\r':
2209-
assert(TriviaRetention != TriviaRetentionMode::WithTrivia &&
2210-
"newlines should be eaten by lexTrivia as LeadingTrivia");
2211-
NextToken.setAtStartOfLine(true);
2212-
goto Restart; // Skip whitespace.
2208+
llvm_unreachable("Newlines should be eaten by lexTrivia as LeadingTrivia");
22132209

22142210
case ' ':
22152211
case '\t':
22162212
case '\f':
22172213
case '\v':
2218-
goto Restart; // Skip whitespace.
2214+
llvm_unreachable(
2215+
"Whitespaces should be eaten by lexTrivia as LeadingTrivia");
22192216

22202217
case -1:
22212218
case -2:
@@ -2228,17 +2225,16 @@ void Lexer::lexImpl() {
22282225
case NulCharacterKind::CodeCompletion:
22292226
return formToken(tok::code_complete, TokStart);
22302227

2231-
case NulCharacterKind::Embedded:
2232-
// If this is a random nul character in the middle of a buffer, skip it as
2233-
// whitespace.
2234-
diagnoseEmbeddedNul(Diags, CurPtr-1);
2235-
goto Restart;
22362228
case NulCharacterKind::BufferEnd:
2237-
// Otherwise, this is the real end of the buffer. Put CurPtr back into
2238-
// buffer bounds.
2229+
// This is the real end of the buffer.
2230+
// Put CurPtr back into buffer bounds.
22392231
--CurPtr;
22402232
// Return EOF.
22412233
return formToken(tok::eof, TokStart);
2234+
2235+
case NulCharacterKind::Embedded:
2236+
llvm_unreachable(
2237+
"Embedded nul should be eaten by lexTrivia as LeadingTrivia");
22422238
}
22432239

22442240
case '@': return formToken(tok::at_sign, TokStart);
@@ -2275,16 +2271,16 @@ void Lexer::lexImpl() {
22752271
if (CurPtr[0] == '/') { // "//"
22762272
skipSlashSlashComment(/*EatNewline=*/true);
22772273
SeenComment = true;
2278-
if (isKeepingComments())
2279-
return formToken(tok::comment, TokStart);
2280-
goto Restart;
2274+
assert(isKeepingComments() &&
2275+
"Non token comment should be eaten by lexTrivia as LeadingTrivia");
2276+
return formToken(tok::comment, TokStart);
22812277
}
22822278
if (CurPtr[0] == '*') { // "/*"
22832279
skipSlashStarComment();
22842280
SeenComment = true;
2285-
if (isKeepingComments())
2286-
return formToken(tok::comment, TokStart);
2287-
goto Restart;
2281+
assert(isKeepingComments() &&
2282+
"Non token comment should be eaten by lexTrivia as LeadingTrivia");
2283+
return formToken(tok::comment, TokStart);
22882284
}
22892285
return lexOperatorIdentifier();
22902286
case '%':
@@ -2313,13 +2309,9 @@ void Lexer::lexImpl() {
23132309
case '<':
23142310
if (CurPtr[0] == '#')
23152311
return tryLexEditorPlaceholder();
2316-
else if (CurPtr[0] == '<' && tryLexConflictMarker(/*EatNewline=*/true))
2317-
goto Restart;
2318-
return lexOperatorIdentifier();
23192312

2313+
return lexOperatorIdentifier();
23202314
case '>':
2321-
if (CurPtr[0] == '>' && tryLexConflictMarker(/*EatNewline=*/true))
2322-
goto Restart;
23232315
return lexOperatorIdentifier();
23242316

23252317
case '=': case '-': case '+': case '*':
@@ -2378,9 +2370,6 @@ Token Lexer::getTokenAtLocation(const SourceManager &SM, SourceLoc Loc) {
23782370
}
23792371

23802372
void Lexer::lexTrivia(syntax::Trivia &Pieces, bool IsForTrailingTrivia) {
2381-
if (TriviaRetention == TriviaRetentionMode::WithoutTrivia)
2382-
return;
2383-
23842373
Restart:
23852374
const char *TriviaStart = CurPtr;
23862375

0 commit comments

Comments
 (0)