@@ -283,7 +283,9 @@ void Lexer::formToken(tok Kind, const char *TokStart, bool MultilineString) {
283
283
284
284
StringRef TokenText { TokStart, static_cast <size_t >(CurPtr - TokStart) };
285
285
286
- lexTrivia (TrailingTrivia, /* IsForTrailingTrivia */ true );
286
+ if (TriviaRetention == TriviaRetentionMode::WithTrivia) {
287
+ lexTrivia (TrailingTrivia, /* IsForTrailingTrivia */ true );
288
+ }
287
289
288
290
NextToken.setToken (Kind, TokenText, CommentLength, MultilineString);
289
291
}
@@ -292,11 +294,11 @@ void Lexer::formEscapedIdentifierToken(const char *TokStart) {
292
294
assert (CurPtr - TokStart >= 3 && " escaped identifier must be longer than or equal 3 bytes" );
293
295
assert (TokStart[0 ] == ' `' && " escaped identifier starts with backtick" );
294
296
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
+
300
302
formToken (tok::identifier, TokStart);
301
303
// If this token is at ArtificialEOF, it's forced to be tok::eof. Don't mark
302
304
// this as escaped-identifier in this case.
@@ -2159,18 +2161,15 @@ void Lexer::lexImpl() {
2159
2161
assert (CurPtr >= BufferStart &&
2160
2162
CurPtr <= BufferEnd && " Current pointer out of range!" );
2161
2163
2162
- if (TriviaRetention == TriviaRetentionMode::WithTrivia) {
2163
- LeadingTrivia.clear ();
2164
- TrailingTrivia.clear ();
2165
- }
2164
+ LeadingTrivia.clear ();
2165
+ TrailingTrivia.clear ();
2166
+
2166
2167
if (CurPtr == BufferStart) {
2167
2168
if (BufferStart < ContentStart) {
2168
2169
size_t BOMLen = ContentStart - BufferStart;
2169
2170
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}));
2174
2173
CurPtr += BOMLen;
2175
2174
}
2176
2175
NextToken.setAtStartOfLine (true );
@@ -2182,7 +2181,6 @@ void Lexer::lexImpl() {
2182
2181
LastCommentBlockStart = CurPtr;
2183
2182
SeenComment = false ;
2184
2183
2185
- Restart:
2186
2184
lexTrivia (LeadingTrivia, /* IsForTrailingTrivia */ false );
2187
2185
2188
2186
// Remember the start of the token so we can form the text range.
@@ -2198,24 +2196,23 @@ void Lexer::lexImpl() {
2198
2196
return lexOperatorIdentifier ();
2199
2197
2200
2198
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);
2205
2204
}
2206
2205
2207
2206
case ' \n ' :
2208
2207
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" );
2213
2209
2214
2210
case ' ' :
2215
2211
case ' \t ' :
2216
2212
case ' \f ' :
2217
2213
case ' \v ' :
2218
- goto Restart; // Skip whitespace.
2214
+ llvm_unreachable (
2215
+ " Whitespaces should be eaten by lexTrivia as LeadingTrivia" );
2219
2216
2220
2217
case -1 :
2221
2218
case -2 :
@@ -2228,17 +2225,16 @@ void Lexer::lexImpl() {
2228
2225
case NulCharacterKind::CodeCompletion:
2229
2226
return formToken (tok::code_complete, TokStart);
2230
2227
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;
2236
2228
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.
2239
2231
--CurPtr;
2240
2232
// Return EOF.
2241
2233
return formToken (tok::eof, TokStart);
2234
+
2235
+ case NulCharacterKind::Embedded:
2236
+ llvm_unreachable (
2237
+ " Embedded nul should be eaten by lexTrivia as LeadingTrivia" );
2242
2238
}
2243
2239
2244
2240
case ' @' : return formToken (tok::at_sign, TokStart);
@@ -2275,16 +2271,16 @@ void Lexer::lexImpl() {
2275
2271
if (CurPtr[0 ] == ' /' ) { // "//"
2276
2272
skipSlashSlashComment (/* EatNewline=*/ true );
2277
2273
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) ;
2281
2277
}
2282
2278
if (CurPtr[0 ] == ' *' ) { // "/*"
2283
2279
skipSlashStarComment ();
2284
2280
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) ;
2288
2284
}
2289
2285
return lexOperatorIdentifier ();
2290
2286
case ' %' :
@@ -2313,13 +2309,9 @@ void Lexer::lexImpl() {
2313
2309
case ' <' :
2314
2310
if (CurPtr[0 ] == ' #' )
2315
2311
return tryLexEditorPlaceholder ();
2316
- else if (CurPtr[0 ] == ' <' && tryLexConflictMarker (/* EatNewline=*/ true ))
2317
- goto Restart;
2318
- return lexOperatorIdentifier ();
2319
2312
2313
+ return lexOperatorIdentifier ();
2320
2314
case ' >' :
2321
- if (CurPtr[0 ] == ' >' && tryLexConflictMarker (/* EatNewline=*/ true ))
2322
- goto Restart;
2323
2315
return lexOperatorIdentifier ();
2324
2316
2325
2317
case ' =' : case ' -' : case ' +' : case ' *' :
@@ -2378,9 +2370,6 @@ Token Lexer::getTokenAtLocation(const SourceManager &SM, SourceLoc Loc) {
2378
2370
}
2379
2371
2380
2372
void Lexer::lexTrivia (syntax::Trivia &Pieces, bool IsForTrailingTrivia) {
2381
- if (TriviaRetention == TriviaRetentionMode::WithoutTrivia)
2382
- return ;
2383
-
2384
2373
Restart:
2385
2374
const char *TriviaStart = CurPtr;
2386
2375
0 commit comments