Skip to content

[Parse] Support #if swift(<4.1) #14503

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -152,6 +152,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
5 changes: 5 additions & 0 deletions 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
11 changes: 8 additions & 3 deletions lib/Parse/ParseIfConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@ class ValidateIfConfigCondition :
auto PUE = dyn_cast<PrefixUnaryExpr>(Arg);
llvm::Optional<StringRef> PrefixName = PUE ?
getDeclRefStr(PUE->getFn(), DeclRefKind::PrefixOperator) : None;
if (!PrefixName || *PrefixName != ">=") {
if (!PrefixName || (*PrefixName != ">=" && *PrefixName != "<")) {
D.diagnose(Arg->getLoc(),
diag::unsupported_platform_condition_argument,
"a unary comparison, such as '>=2.2'");
"a unary comparison, such as '>=2.2' or '<4.1'");
return nullptr;
}
auto versionString = extractExprSource(Ctx.SourceMgr, PUE->getArg());
Expand Down Expand Up @@ -447,11 +447,16 @@ class EvaluateIfConfigCondition :
return thisVersion >= Val;
} else if (KindName == "swift") {
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();
auto thisVersion = Ctx.LangOpts.EffectiveLanguageVersion;
return thisVersion >= Val;
if (PrefixName == ">=") {
return thisVersion >= Val;
} else if (PrefixName == "<") {
return thisVersion < Val;
}
} else if (KindName == "canImport") {
auto Str = extractExprSource(Ctx.SourceMgr, Arg);
return Ctx.canImportModule({ Ctx.getIdentifier(Str) , E->getLoc() });
Expand Down
43 changes: 43 additions & 0 deletions test/Parse/ConditionalCompilation/language_version_explicit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,46 @@
let c = 1
#endif
// NOTE: ...the next time the version goes up.

#if swift(<4)
// This shouldn't emit any diagnostics.
asdf asdf asdf asdf
#else
let a = 1
#endif

#if !swift(<4)
let b = 1
#else
// This shouldn't emit any diagnostics.
asdf asdf asdf asdf
#endif

#if swift(<4.0)
// This shouldn't emit any diagnostics.
asdf asdf asdf asdf
#else
let c = 1
#endif

#if swift(<4.0.0)
// This shouldn't emit any diagnostics.
asdf asdf asdf asdf
#else
let d = 1
#endif

#if swift(<4.0.1)
// This shouldn't emit any diagnostics.
asdf asdf asdf asdf
#else
let e = 1
#endif

#if swift(<99)
let f = 1
#else
// This shouldn't emit any diagnostics.
asdf asdf asdf asdf
#endif