Skip to content

[Parse] Fix empty platform condition crash #39444

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
Sep 29, 2021
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
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,9 @@ ERROR(unsupported_platform_condition_expression,none,
"unexpected platform condition "
"(expected 'os', 'arch', or 'swift')",
())
ERROR(platform_condition_expected_argument,none,
"expected argument to platform condition",
())
ERROR(platform_condition_expected_one_argument,none,
"expected only one argument to platform condition",
())
Expand Down
9 changes: 8 additions & 1 deletion lib/Parse/ParseIfConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ static llvm::VersionTuple getCanImportVersion(ArgumentList *args,

static Expr *getSingleSubExp(ArgumentList *args, StringRef kindName,
DiagnosticEngine *D) {
if (args->empty())
return nullptr;

if (auto *unary = args->getUnlabeledUnaryExpr())
return unary;

Expand Down Expand Up @@ -260,7 +263,11 @@ class ValidateIfConfigCondition :

Expr *Arg = getSingleSubExp(E->getArgs(), *KindName, &D);
if (!Arg) {
D.diagnose(E->getLoc(), diag::platform_condition_expected_one_argument);
if (E->getArgs()->empty()) {
D.diagnose(E->getLoc(), diag::platform_condition_expected_argument);
} else {
D.diagnose(E->getLoc(), diag::platform_condition_expected_one_argument);
}
return nullptr;
}
// '_compiler_version' '(' string-literal ')'
Expand Down
7 changes: 6 additions & 1 deletion test/Parse/ConditionalCompilation/basicParseErrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func fn_j() {}
#endif
fn_j() // OK

#if foo || bar || nonExistent() // expected-error {{expected only one argument to platform condition}}
#if foo || bar || nonExistent() // expected-error {{expected argument to platform condition}}
#endif

#if FOO = false
Expand Down Expand Up @@ -168,3 +168,8 @@ undefinedFunc() // expected-error {{cannot find 'undefinedFunc' in scope}}
#else
if true {}
#endif // OK

// rdar://83017601 Make sure we don't crash
#if canImport()
// expected-error@-1 {{expected argument to platform condition}}
#endif