Skip to content

Commit c1e8bc4

Browse files
authored
Merge pull request #13620 from rintaro/parse-braceitems-reorder
[Parse] Small cleanups in parseBraceItems()
2 parents 8d7cc83 + 39746a9 commit c1e8bc4

File tree

4 files changed

+39
-69
lines changed

4 files changed

+39
-69
lines changed

include/swift/Parse/Parser.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -710,11 +710,6 @@ class Parser {
710710
/// Options that control the parsing of declarations.
711711
typedef OptionSet<ParseDeclFlags> ParseDeclOptions;
712712

713-
/// Skips the current token if it is '}', and emits a diagnostic.
714-
///
715-
/// \returns true if any tokens were skipped.
716-
bool skipExtraTopLevelRBraces();
717-
718713
void delayParseFromBeginningToHere(ParserPosition BeginParserPosition,
719714
ParseDeclOptions Flags);
720715
void consumeDecl(ParserPosition BeginParserPosition, ParseDeclOptions Flags,

lib/Parse/ParseDecl.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,6 @@ bool Parser::parseTopLevel() {
196196
// Parse the body of the file.
197197
SmallVector<ASTNode, 128> Items;
198198

199-
skipExtraTopLevelRBraces();
200-
201199
// If we are in SIL mode, and if the first token is the start of a sil
202200
// declaration, parse that one SIL function and return to the top level. This
203201
// allows type declarations and other things to be parsed, name bound, and
@@ -280,19 +278,6 @@ bool Parser::parseTopLevel() {
280278
return FoundTopLevelCodeToExecute;
281279
}
282280

283-
bool Parser::skipExtraTopLevelRBraces() {
284-
if (!Tok.is(tok::r_brace))
285-
return false;
286-
while (Tok.is(tok::r_brace)) {
287-
diagnose(Tok, diag::extra_rbrace)
288-
.fixItRemove(Tok.getLoc());
289-
consumeToken();
290-
}
291-
return true;
292-
}
293-
294-
295-
296281
static Optional<StringRef>
297282
getStringLiteralIfNotInterpolated(Parser &P, SourceLoc Loc, const Token &Tok,
298283
StringRef DiagText) {

lib/Parse/ParseStmt.cpp

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,9 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
245245
}
246246

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

250249
bool PreviousHadSemi = true;
251-
while ((Kind == BraceItemListKind::TopLevelLibrary ||
252-
Tok.isNot(tok::r_brace)) &&
250+
while ((IsTopLevel || Tok.isNot(tok::r_brace)) &&
253251
Tok.isNot(tok::pound_endif) &&
254252
Tok.isNot(tok::pound_elseif) &&
255253
Tok.isNot(tok::pound_else) &&
@@ -266,9 +264,13 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
266264

267265
SyntaxParsingContext StmtContext(SyntaxContext, SyntaxContextKind::Stmt);
268266

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();
271272
continue;
273+
}
272274

273275
// Eat invalid tokens instead of allowing them to produce downstream errors.
274276
if (consumeIf(tok::unknown))
@@ -292,57 +294,29 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
292294

293295
// Parse the decl, stmt, or expression.
294296
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)) {
315298
auto IfConfigResult = parseIfConfig(
316299
[&](SmallVectorImpl<ASTNode> &Elements, bool IsActive) {
317300
parseBraceItems(Elements, Kind, IsActive
318301
? BraceItemListKind::ActiveConditionalBlock
319302
: BraceItemListKind::InactiveConditionalBlock);
320303
});
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);
333308

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);
345316
}
317+
} else {
318+
NeedParseErrorRecovery = true;
319+
continue;
346320
}
347321
} else if (Tok.is(tok::pound_line)) {
348322
ParserStatus Status = parseLineDirective(true);
@@ -352,6 +326,21 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
352326
ParserStatus Status = parseLineDirective(false);
353327
BraceItemsStatus |= Status;
354328
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());
355344
} else if (IsTopLevel) {
356345
// If this is a statement or expression at the top level of the module,
357346
// Parse it as a child of a TopLevelCodeDecl.

test/Parse/ConditionalCompilation/pound-if-top-level-3.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
#if arch(x86_64)
44
// expected-error@+2{{expected '{' in protocol type}}
5-
// expected-error@+1{{expected #else or #endif at end of conditional compilation block}}
5+
// expected-error@+1{{extraneous '}' at top level}}
66
public protocol CS}
7+
// expected-error@+1{{expected #else or #endif at end of conditional compilation block}}

0 commit comments

Comments
 (0)