Skip to content

[Parse] Improve '#if' block parsing #7382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ ERROR(incomplete_conditional_compilation_directive,none,
"incomplete condition in conditional compilation directive", ())
ERROR(extra_tokens_conditional_compilation_directive,none,
"extra tokens following conditional compilation directive", ())
ERROR(unexpected_rbrace_in_conditional_compilation_block,none,
"unexpected '}' in conditional compilation block", ())

ERROR(sourceLocation_expected,none,
"expected '%0' in #sourceLocation directive", (StringRef))
Expand Down
20 changes: 10 additions & 10 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3016,6 +3016,7 @@ ParserResult<IfConfigDecl> Parser::parseDeclIfConfig(ParseDeclOptions Flags) {
ConfigState.setConditionActive(!foundActive);
} else {
// Evaluate the condition.
llvm::SaveAndRestore<bool> S(InPoundIfEnvironment, true);
ParserResult<Expr> Result = parseExprSequence(diag::expected_expr,
/*isBasic*/true,
/*isForDirective*/true);
Expand Down Expand Up @@ -3045,19 +3046,18 @@ ParserResult<IfConfigDecl> Parser::parseDeclIfConfig(ParseDeclOptions Flags) {
if (ConfigState.shouldParse()) {
ParserStatus Status;
bool PreviousHadSemi = true;
while (Tok.isNot(tok::pound_else, tok::pound_endif, tok::pound_elseif)) {
SourceLoc StartLoc = Tok.getLoc();
Status |= parseDeclItem(*this, PreviousHadSemi, Flags,
[&](Decl *D) {Decls.push_back(D);});
if (StartLoc == Tok.getLoc()) {
assert(Status.isError() && "no progress without error?");
while (Tok.isNot(tok::pound_else, tok::pound_endif, tok::pound_elseif,
tok::eof)) {
if (Tok.is(tok::r_brace)) {
diagnose(Tok.getLoc(),
diag::unexpected_rbrace_in_conditional_compilation_block);
// If we see '}', following declarations don't look like belong to
// the current decl context; skip them.
skipUntilConditionalBlockClose();
break;
}
if (Tok.isAny(tok::eof)) {
diagnose(Tok, diag::expected_close_to_if_directive);
break;
}
Status |= parseDeclItem(*this, PreviousHadSemi, Flags,
[&](Decl *D) {Decls.push_back(D);});
}
} else {
DiagnosticTransaction DT(Diags);
Expand Down
2 changes: 1 addition & 1 deletion lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,6 @@ Parser::classifyConditionalCompilationExpr(Expr *condition,
ParserResult<Stmt> Parser::parseStmtIfConfig(BraceItemListKind Kind) {
StructureMarkerRAII ParsingDecl(*this, Tok.getLoc(),
StructureMarkerKind::IfConfig);
llvm::SaveAndRestore<bool> S(InPoundIfEnvironment, true);
SmallVector<IfConfigStmtClause, 4> Clauses;

bool foundActive = false;
Expand All @@ -1785,6 +1784,7 @@ ParserResult<Stmt> Parser::parseStmtIfConfig(BraceItemListKind Kind) {
ConfigState.setConditionActive(!foundActive);
} else {
// Evaluate the condition.
llvm::SaveAndRestore<bool> S(InPoundIfEnvironment, true);
ParserResult<Expr> Result = parseExprSequence(diag::expected_expr,
/*basic*/true,
/*isForDirective*/true);
Expand Down
7 changes: 7 additions & 0 deletions test/Parse/ConditionalCompilation/decl_parse_errors-2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %target-typecheck-verify-swift

class C2 { // expected-note {{to match this opening '{'}}
#if FOO
func foo() {}
// expected-error @+2 {{expected '}' in class}}
// expected-error @+1 {{expected #else or #endif at end of conditional compilation block}}
6 changes: 3 additions & 3 deletions test/Parse/ConditionalCompilation/decl_parse_errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ lazy
var val3: Int = 0;
#line

class C { // expected-note 2 {{in declaration of 'C'}} expected-note {{to match this opening '{'}}
class C { // expected-note {{to match this opening '{'}}

#if os(iOS)
func foo() {}
} // expected-error{{expected declaration}}
} // expected-error{{unexpected '}' in conditional compilation block}}
#else
func bar() {}
func baz() {}
} // expected-error{{expected declaration}}
} // expected-error{{unexpected '}' in conditional compilation block}}
#endif
// expected-error@+1{{expected '}' in class}}
7 changes: 7 additions & 0 deletions test/Parse/ConditionalCompilation/language_version.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@
#endif

#if swift(>=2.2.1)
_ = 2.2.1 // expected-error {{expected named member of numeric literal}}
#endif

class C {
#if swift(>=2.2.1)
let val = 2.2.1 // expected-error {{expected named member of numeric literal}}
#endif
}

#if swift(>=2.0, *) // expected-error {{expected only one argument to platform condition}}
#endif

Expand Down