Skip to content

[Parse] Fix crash in conditional compilation parsing #7331

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
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
14 changes: 6 additions & 8 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ ERROR(extra_rbrace,none,
ERROR(structure_overflow,Fatal,
"structure nesting level exceeded maximum of %0", (unsigned))

ERROR(expected_close_to_if_directive,none,
"expected #else or #endif at end of conditional compilation block", ())
ERROR(expected_close_after_else_directive,none,
"further conditions after #else are unreachable", ())
ERROR(unexpected_conditional_compilation_block_terminator,none,
"unexpected conditional compilation block terminator", ())
ERROR(expected_conditional_compilation_expression,none,
"expected a condition to follow %select{#if|#elseif}0", (bool))
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", ())

Expand Down Expand Up @@ -828,12 +832,6 @@ ERROR(expected_expr_assignment,none,
ERROR(expected_rbrace_in_brace_stmt,none,
"expected '}' at end of brace statement", ())

/// #if Statement
ERROR(expected_close_to_if_directive,none,
"expected #else or #endif at end of conditional compilation block", ())
ERROR(expected_close_after_else_directive,none,
"further conditions after #else are unreachable", ())

/// Associatedtype Statement
ERROR(typealias_inside_protocol_without_type,none,
"typealias is missing an assigned type; use 'associatedtype' to define an associated type requirement", ())
Expand Down
5 changes: 0 additions & 5 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3015,11 +3015,6 @@ ParserResult<IfConfigDecl> Parser::parseDeclIfConfig(ParseDeclOptions Flags) {
if (isElse) {
ConfigState.setConditionActive(!foundActive);
} else {
if (Tok.isAtStartOfLine()) {
diagnose(ClauseLoc, diag::expected_conditional_compilation_expression,
!Clauses.empty());
}

// Evaluate the condition.
ParserResult<Expr> Result = parseExprSequence(diag::expected_expr,
/*isBasic*/true,
Expand Down
21 changes: 12 additions & 9 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ ParserResult<Expr> Parser::parseExprSequence(Diag<> Message,
}
}
SequencedExprs.push_back(Primary.get());

if (isForConditionalDirective && Tok.isAtStartOfLine())
break;

parse_operator:
switch (Tok.getKind()) {
Expand Down Expand Up @@ -320,17 +323,17 @@ ParserResult<Expr> Parser::parseExprSequence(Diag<> Message,
}
}
done:

if (SequencedExprs.empty()) {
if (isForConditionalDirective) {
diagnose(startLoc, diag::expected_close_to_if_directive);
return makeParserError();
} else {
// If we had semantic errors, just fail here.
assert(!SequencedExprs.empty());
}

// For conditional directives, we stop parsing after a line break.
if (isForConditionalDirective && (SequencedExprs.size() & 1) == 0) {
diagnose(getEndOfPreviousLoc(),
diag::incomplete_conditional_compilation_directive);
return makeParserError();
}

// If we had semantic errors, just fail here.
assert(!SequencedExprs.empty());

// If we saw no operators, don't build a sequence.
if (SequencedExprs.size() == 1) {
auto Result = makeParserResult(SequencedExprs[0]);
Expand Down
5 changes: 0 additions & 5 deletions lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1784,11 +1784,6 @@ ParserResult<Stmt> Parser::parseStmtIfConfig(BraceItemListKind Kind) {
if (isElse) {
ConfigState.setConditionActive(!foundActive);
} else {
if (Tok.isAtStartOfLine()) {
diagnose(ClauseLoc, diag::expected_conditional_compilation_expression,
!Clauses.empty());
}

// Evaluate the condition.
ParserResult<Expr> Result = parseExprSequence(diag::expected_expr,
/*basic*/true,
Expand Down
11 changes: 11 additions & 0 deletions test/Parse/ConditionalCompilation/basicParseErrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ func h() {}

#endif


// <https://twitter.com/practicalswift/status/829066902869786625>
#if // expected-error {{incomplete condition in conditional compilation directive}}
#if 0 == // expected-error {{incomplete condition in conditional compilation directive}}
#if0= // expected-error {{incomplete condition in conditional compilation directive}} expected-error {{'=' must have consistent whitespace on both sides}}
class Foo {
#if // expected-error {{incomplete condition in conditional compilation directive}}
#if 0 == // expected-error {{incomplete condition in conditional compilation directive}}
#if0= // expected-error {{incomplete condition in conditional compilation directive}} expected-error {{'=' must have consistent whitespace on both sides}}
}

struct S {
#if FOO
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

// With a space next to the '#if'
#if
// expected-error@-1 {{expected a condition to follow #if}}
class C {} //expected-error {{expected #else or #endif at end of conditional compilation block}}
// expected-error@-1 {{incomplete condition in conditional compilation directive}}
class C {}
#endif // expected-error {{unexpected conditional compilation block terminator}}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// No space next to the '#if'

#if
// expected-error@-1 {{expected a condition to follow #if}}
class D {} // expected-error {{expected #else or #endif at end of conditional compilation block}}
// expected-error@-1 {{incomplete condition in conditional compilation directive}}
class D {}
#endif
// expected-error@-1 {{unexpected conditional compilation block terminator}}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// RUN: not --crash %target-swift-frontend %s -emit-ir
// RUN: not %target-swift-frontend %s -emit-ir
// REQUIRES: asserts
#if0 *