Skip to content

Commit 62ac618

Browse files
committed
[Parse] small cleanup in parseBraceItems()
* Reorder 'if ... else if' branches to simplify conditions. * use 'append()' instead of `for { push_back() }`
1 parent d6bbf81 commit 62ac618

File tree

1 file changed

+30
-44
lines changed

1 file changed

+30
-44
lines changed

lib/Parse/ParseStmt.cpp

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
245245
}
246246

247247
ParserStatus BraceItemsStatus;
248-
SmallVector<Decl*, 8> TmpDecls;
249248

250249
bool PreviousHadSemi = true;
251250
while ((Kind == BraceItemListKind::TopLevelLibrary ||
@@ -292,57 +291,29 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
292291

293292
// Parse the decl, stmt, or expression.
294293
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)) {
294+
if (Tok.is(tok::pound_if)) {
315295
auto IfConfigResult = parseIfConfig(
316296
[&](SmallVectorImpl<ASTNode> &Elements, bool IsActive) {
317297
parseBraceItems(Elements, Kind, IsActive
318298
? BraceItemListKind::ActiveConditionalBlock
319299
: BraceItemListKind::InactiveConditionalBlock);
320300
});
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-
}
301+
if (auto ICD = IfConfigResult.getPtrOrNull()) {
302+
Result = ICD;
303+
// Add the #if block itself
304+
Entries.push_back(ICD);
333305

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);
306+
for (auto &Entry : ICD->getActiveClauseElements()) {
307+
if (Entry.is<Decl *>() && isa<IfConfigDecl>(Entry.get<Decl *>()))
308+
// Don't hoist nested '#if'.
309+
continue;
310+
Entries.push_back(Entry);
311+
if (Entry.is<Decl *>())
312+
Entry.get<Decl *>()->setEscapedFromIfConfig(true);
345313
}
314+
} else {
315+
NeedParseErrorRecovery = true;
316+
continue;
346317
}
347318
} else if (Tok.is(tok::pound_line)) {
348319
ParserStatus Status = parseLineDirective(true);
@@ -352,6 +323,21 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
352323
ParserStatus Status = parseLineDirective(false);
353324
BraceItemsStatus |= Status;
354325
NeedParseErrorRecovery = Status.isError();
326+
} else if (isStartOfDecl()) {
327+
SmallVector<Decl*, 8> TmpDecls;
328+
ParserResult<Decl> DeclResult =
329+
parseDecl(IsTopLevel ? PD_AllowTopLevel : PD_Default,
330+
[&](Decl *D) {TmpDecls.push_back(D);});
331+
if (DeclResult.isParseError()) {
332+
NeedParseErrorRecovery = true;
333+
if (DeclResult.hasCodeCompletion() && IsTopLevel &&
334+
isCodeCompletionFirstPass()) {
335+
consumeDecl(BeginParserPosition, None, IsTopLevel);
336+
return DeclResult;
337+
}
338+
}
339+
Result = DeclResult.getPtrOrNull();
340+
Entries.append(TmpDecls.begin(), TmpDecls.end());
355341
} else if (IsTopLevel) {
356342
// If this is a statement or expression at the top level of the module,
357343
// Parse it as a child of a TopLevelCodeDecl.

0 commit comments

Comments
 (0)