@@ -245,11 +245,9 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
245
245
}
246
246
247
247
ParserStatus BraceItemsStatus;
248
- SmallVector<Decl*, 8 > TmpDecls;
249
248
250
249
bool PreviousHadSemi = true ;
251
- while ((Kind == BraceItemListKind::TopLevelLibrary ||
252
- Tok.isNot (tok::r_brace)) &&
250
+ while ((IsTopLevel || Tok.isNot (tok::r_brace)) &&
253
251
Tok.isNot (tok::pound_endif) &&
254
252
Tok.isNot (tok::pound_elseif) &&
255
253
Tok.isNot (tok::pound_else) &&
@@ -266,9 +264,13 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
266
264
267
265
SyntaxParsingContext StmtContext (SyntaxContext, SyntaxContextKind::Stmt);
268
266
269
- if (Kind == BraceItemListKind::TopLevelLibrary &&
270
- skipExtraTopLevelRBraces ())
267
+ if (Tok.is (tok::r_brace)) {
268
+ assert (IsTopLevel);
269
+ diagnose (Tok, diag::extra_rbrace)
270
+ .fixItRemove (Tok.getLoc ());
271
+ consumeToken ();
271
272
continue ;
273
+ }
272
274
273
275
// Eat invalid tokens instead of allowing them to produce downstream errors.
274
276
if (consumeIf (tok::unknown))
@@ -292,57 +294,29 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
292
294
293
295
// Parse the decl, stmt, or expression.
294
296
PreviousHadSemi = false ;
295
- if (isStartOfDecl ()
296
- && Tok.isNot (
297
- tok::pound_if, tok::pound_sourceLocation, tok::pound_line)) {
298
- ParserResult<Decl> DeclResult =
299
- parseDecl (IsTopLevel ? PD_AllowTopLevel : PD_Default,
300
- [&](Decl *D) {TmpDecls.push_back (D);});
301
- if (DeclResult.isParseError ()) {
302
- NeedParseErrorRecovery = true ;
303
- if (DeclResult.hasCodeCompletion () && IsTopLevel &&
304
- isCodeCompletionFirstPass ()) {
305
- consumeDecl (BeginParserPosition, None, IsTopLevel);
306
- return DeclResult;
307
- }
308
- }
309
- Result = DeclResult.getPtrOrNull ();
310
-
311
- for (Decl *D : TmpDecls)
312
- Entries.push_back (D);
313
- TmpDecls.clear ();
314
- } else if (Tok.is (tok::pound_if)) {
297
+ if (Tok.is (tok::pound_if)) {
315
298
auto IfConfigResult = parseIfConfig (
316
299
[&](SmallVectorImpl<ASTNode> &Elements, bool IsActive) {
317
300
parseBraceItems (Elements, Kind, IsActive
318
301
? BraceItemListKind::ActiveConditionalBlock
319
302
: BraceItemListKind::InactiveConditionalBlock);
320
303
});
321
-
322
- if (IfConfigResult.isParseError ()) {
323
- NeedParseErrorRecovery = true ;
324
- continue ;
325
- }
326
-
327
- Result = IfConfigResult.get ();
328
-
329
- if (!Result) {
330
- NeedParseErrorRecovery = true ;
331
- continue ;
332
- }
304
+ if (auto ICD = IfConfigResult.getPtrOrNull ()) {
305
+ Result = ICD;
306
+ // Add the #if block itself
307
+ Entries.push_back (ICD);
333
308
334
- // Add the #if block itself
335
- Entries.push_back (Result);
336
-
337
- IfConfigDecl *ICD = cast<IfConfigDecl>(Result.get <Decl*>());
338
- for (auto &Entry : ICD->getActiveClauseElements ()) {
339
- if (Entry.is <Decl*>() && isa<IfConfigDecl>(Entry.get <Decl*>()))
340
- // Don't hoist nested '#if'.
341
- continue ;
342
- Entries.push_back (Entry);
343
- if (Entry.is <Decl*>()) {
344
- Entry.get <Decl*>()->setEscapedFromIfConfig (true );
309
+ for (auto &Entry : ICD->getActiveClauseElements ()) {
310
+ if (Entry.is <Decl *>() && isa<IfConfigDecl>(Entry.get <Decl *>()))
311
+ // Don't hoist nested '#if'.
312
+ continue ;
313
+ Entries.push_back (Entry);
314
+ if (Entry.is <Decl *>())
315
+ Entry.get <Decl *>()->setEscapedFromIfConfig (true );
345
316
}
317
+ } else {
318
+ NeedParseErrorRecovery = true ;
319
+ continue ;
346
320
}
347
321
} else if (Tok.is (tok::pound_line)) {
348
322
ParserStatus Status = parseLineDirective (true );
@@ -352,6 +326,21 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
352
326
ParserStatus Status = parseLineDirective (false );
353
327
BraceItemsStatus |= Status;
354
328
NeedParseErrorRecovery = Status.isError ();
329
+ } else if (isStartOfDecl ()) {
330
+ SmallVector<Decl*, 8 > TmpDecls;
331
+ ParserResult<Decl> DeclResult =
332
+ parseDecl (IsTopLevel ? PD_AllowTopLevel : PD_Default,
333
+ [&](Decl *D) {TmpDecls.push_back (D);});
334
+ if (DeclResult.isParseError ()) {
335
+ NeedParseErrorRecovery = true ;
336
+ if (DeclResult.hasCodeCompletion () && IsTopLevel &&
337
+ isCodeCompletionFirstPass ()) {
338
+ consumeDecl (BeginParserPosition, None, IsTopLevel);
339
+ return DeclResult;
340
+ }
341
+ }
342
+ Result = DeclResult.getPtrOrNull ();
343
+ Entries.append (TmpDecls.begin (), TmpDecls.end ());
355
344
} else if (IsTopLevel) {
356
345
// If this is a statement or expression at the top level of the module,
357
346
// Parse it as a child of a TopLevelCodeDecl.
0 commit comments