Skip to content

[Parser] Support "<" unary operator in #if swift() and #if compiler() expressions #17960

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
1 change: 1 addition & 0 deletions include/swift/Basic/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ class Version {
};

bool operator>=(const Version &lhs, const Version &rhs);
bool operator<(const Version &lhs, const Version &rhs);
bool operator==(const Version &lhs, const Version &rhs);
inline bool operator!=(const Version &lhs, const Version &rhs) {
return !(lhs == rhs);
Expand Down
6 changes: 5 additions & 1 deletion lib/Basic/Version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,11 @@ bool operator>=(const class Version &lhs,
return true;
}

bool operator<(const class Version &lhs, const class Version &rhs) {

return !(lhs >= rhs);
}

bool operator==(const class Version &lhs,
const class Version &rhs) {
auto n = std::max(lhs.size(), rhs.size());
Expand Down Expand Up @@ -446,4 +451,3 @@ std::string getSwiftRevision() {

} // end namespace version
} // end namespace swift

46 changes: 32 additions & 14 deletions lib/Parse/ParseIfConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace {
/// Get PlatformConditionKind from platform condition name.
static
Optional<PlatformConditionKind> getPlatformConditionKind(StringRef Name) {
return llvm::StringSwitch<llvm::Optional<PlatformConditionKind>>(Name)
return llvm::StringSwitch<Optional<PlatformConditionKind>>(Name)
.Case("os", PlatformConditionKind::OS)
.Case("arch", PlatformConditionKind::Arch)
.Case("_endian", PlatformConditionKind::Endianness)
Expand All @@ -53,6 +53,20 @@ static StringRef extractExprSource(SourceManager &SM, Expr *E) {
return SM.extractText(Range);
}

static bool isValidPrefixUnaryOperator(Optional<StringRef> UnaryOperator) {
return UnaryOperator != None &&
(UnaryOperator.getValue() == ">=" || UnaryOperator.getValue() == "<");
}

static bool isValidVersion(const version::Version &Version,
const version::Version &ExpectedVersion,
StringRef UnaryOperator) {
if (UnaryOperator == ">=")
return Version >= ExpectedVersion;
if (UnaryOperator == "<")
return Version < ExpectedVersion;
llvm_unreachable("unsupported unary operator");
}

/// The condition validator.
class ValidateIfConfigCondition :
Expand All @@ -63,7 +77,7 @@ class ValidateIfConfigCondition :
bool HasError;

/// Get the identifier string of the UnresolvedDeclRefExpr.
llvm::Optional<StringRef> getDeclRefStr(Expr *E, DeclRefKind Kind) {
Optional<StringRef> getDeclRefStr(Expr *E, DeclRefKind Kind) {
auto UDRE = dyn_cast<UnresolvedDeclRefExpr>(E);
if (!UDRE ||
!UDRE->hasName() ||
Expand Down Expand Up @@ -93,7 +107,7 @@ class ValidateIfConfigCondition :
Expr *foldSequence(Expr *LHS, ArrayRef<Expr*> &S, bool isRecurse = false) {
assert(!S.empty() && ((S.size() & 1) == 0));

auto getNextOperator = [&]() -> llvm::Optional<StringRef> {
auto getNextOperator = [&]() -> Optional<StringRef> {
assert((S.size() & 1) == 0);
while (!S.empty()) {
auto Name = getDeclRefStr(S[0], DeclRefKind::BinaryOperator);
Expand All @@ -117,7 +131,7 @@ class ValidateIfConfigCondition :
// If failed, it's not a sequence anymore.
return LHS;
Expr *Op = S[0];

// We will definitely be consuming at least one operator.
// Pull out the prospective RHS and slice off the first two elements.
Expr *RHS = validate(S[1]);
Expand Down Expand Up @@ -277,16 +291,16 @@ class ValidateIfConfigCondition :
return E;
}

// 'swift' '(' '>=' float-literal ( '.' integer-literal )* ')'
// 'compiler' '(' '>=' float-literal ( '.' integer-literal )* ')'
// 'swift' '(' ('>=' | '<') float-literal ( '.' integer-literal )* ')'
// 'compiler' '(' ('>=' | '<') float-literal ( '.' integer-literal )* ')'
if (*KindName == "swift" || *KindName == "compiler") {
auto PUE = dyn_cast<PrefixUnaryExpr>(Arg);
llvm::Optional<StringRef> PrefixName = PUE ?
getDeclRefStr(PUE->getFn(), DeclRefKind::PrefixOperator) : None;
if (!PrefixName || *PrefixName != ">=") {
D.diagnose(Arg->getLoc(),
diag::unsupported_platform_condition_argument,
"a unary comparison, such as '>=2.2'");
Optional<StringRef> PrefixName =
PUE ? getDeclRefStr(PUE->getFn(), DeclRefKind::PrefixOperator) : None;
if (!isValidPrefixUnaryOperator(PrefixName)) {
D.diagnose(
Arg->getLoc(), diag::unsupported_platform_condition_argument,
"a unary comparison '>=' or '<'; for example, '>=2.2' or '<2.2'");
return nullptr;
}
auto versionString = extractExprSource(Ctx.SourceMgr, PUE->getArg());
Expand Down Expand Up @@ -448,13 +462,17 @@ class EvaluateIfConfigCondition :
return thisVersion >= Val;
} else if ((KindName == "swift") || (KindName == "compiler")) {
auto PUE = cast<PrefixUnaryExpr>(Arg);
auto PrefixName = getDeclRefStr(PUE->getFn());
auto Str = extractExprSource(Ctx.SourceMgr, PUE->getArg());
auto Val = version::Version::parseVersionString(
Str, SourceLoc(), nullptr).getValue();
if (KindName == "swift") {
return Ctx.LangOpts.EffectiveLanguageVersion >= Val;
return isValidVersion(Ctx.LangOpts.EffectiveLanguageVersion, Val,
PrefixName);
} else if (KindName == "compiler") {
return version::Version::getCurrentLanguageVersion() >= Val;
auto currentLanguageVersion =
version::Version::getCurrentLanguageVersion();
return isValidVersion(currentLanguageVersion, Val, PrefixName);
} else {
llvm_unreachable("unsupported version conditional");
}
Expand Down
30 changes: 30 additions & 0 deletions test/Parse/ConditionalCompilation/compiler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -swift-version 4

#if !compiler(>=4.1)
// There should be no error here.
foo bar
#else
let _: Int = 1
#endif

#if compiler(<4.1)
// There should be no error here.
foo bar
#else
let _: Int = 1
#endif

#if (compiler(>=4.1))
let _: Int = 1
#else
// There should be no error here.
foo bar
#endif

#if !compiler(<4.1)
let _: Int = 1
#else
// There should be no error here.
foo bar
#endif
28 changes: 25 additions & 3 deletions test/Parse/ConditionalCompilation/language_version.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
asdf asdf asdf asdf
#endif

#if swift(<1.2)
#endif

#if swift(<4.2)
let a = 1
#else
let a = 2
#endif

#if swift(<1.0)
// This shouldn't emit any diagnostics.
asdf asdf asdf asdf
#endif

#if swift(>=1.2)

#if os(iOS)
Expand Down Expand Up @@ -34,13 +48,21 @@
%#^*&
#endif

#if swift(">=7.1") // expected-error {{unexpected platform condition argument: expected a unary comparison, such as '>=2.2'}}
#if !swift(<1000.0)
// This shouldn't emit any diagnostics.
%#^*&
#endif

#if swift(">=7.1") // expected-error {{unexpected platform condition argument: expected a unary comparison '>=' or '<'; for example, '>=2.2' or '<2.2'}}
#endif

#if swift("<7.1") // expected-error {{unexpected platform condition argument: expected a unary comparison '>=' or '<'; for example, '>=2.2' or '<2.2'}}
#endif

#if swift(">=2n.2") // expected-error {{unexpected platform condition argument: expected a unary comparison, such as '>=2.2'}}
#if swift(">=2n.2") // expected-error {{unexpected platform condition argument: expected a unary comparison '>=' or '<'; for example, '>=2.2' or '<2.2'}}
#endif

#if swift("") // expected-error {{unexpected platform condition argument: expected a unary comparison, such as '>=2.2'}}
#if swift("") // expected-error {{unexpected platform condition argument: expected a unary comparison '>=' or '<'; for example, '>=2.2' or '<2.2'}}
#endif

#if swift(>=2.2.1)
Expand Down