Skip to content

Commit e0cd1c8

Browse files
committed
[Parse] Support #if swift(<4.1)
1 parent 643adf5 commit e0cd1c8

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

include/swift/Basic/Version.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class Version {
152152
};
153153

154154
bool operator>=(const Version &lhs, const Version &rhs);
155+
bool operator<(const Version &lhs, const Version &rhs);
155156
bool operator==(const Version &lhs, const Version &rhs);
156157
inline bool operator!=(const Version &lhs, const Version &rhs) {
157158
return !(lhs == rhs);

lib/Basic/Version.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,11 @@ bool operator>=(const class Version &lhs,
384384
return true;
385385
}
386386

387+
bool operator<(const class Version &lhs,
388+
const class Version &rhs) {
389+
return !(lhs >= rhs);
390+
}
391+
387392
bool operator==(const class Version &lhs,
388393
const class Version &rhs) {
389394
auto n = std::max(lhs.size(), rhs.size());

lib/Parse/ParseIfConfig.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,10 @@ class ValidateIfConfigCondition :
282282
auto PUE = dyn_cast<PrefixUnaryExpr>(Arg);
283283
llvm::Optional<StringRef> PrefixName = PUE ?
284284
getDeclRefStr(PUE->getFn(), DeclRefKind::PrefixOperator) : None;
285-
if (!PrefixName || *PrefixName != ">=") {
285+
if (!PrefixName || (*PrefixName != ">=" && *PrefixName != "<")) {
286286
D.diagnose(Arg->getLoc(),
287287
diag::unsupported_platform_condition_argument,
288-
"a unary comparison, such as '>=2.2'");
288+
"a unary comparison, such as '>=2.2' or '<4.1'");
289289
return nullptr;
290290
}
291291
auto versionString = extractExprSource(Ctx.SourceMgr, PUE->getArg());
@@ -447,11 +447,16 @@ class EvaluateIfConfigCondition :
447447
return thisVersion >= Val;
448448
} else if (KindName == "swift") {
449449
auto PUE = cast<PrefixUnaryExpr>(Arg);
450+
auto PrefixName = getDeclRefStr(PUE->getFn());
450451
auto Str = extractExprSource(Ctx.SourceMgr, PUE->getArg());
451452
auto Val = version::Version::parseVersionString(
452453
Str, SourceLoc(), nullptr).getValue();
453454
auto thisVersion = Ctx.LangOpts.EffectiveLanguageVersion;
454-
return thisVersion >= Val;
455+
if (PrefixName == ">=") {
456+
return thisVersion >= Val;
457+
} else if (PrefixName == "<") {
458+
return thisVersion < Val;
459+
}
455460
} else if (KindName == "canImport") {
456461
auto Str = extractExprSource(Ctx.SourceMgr, Arg);
457462
return Ctx.canImportModule({ Ctx.getIdentifier(Str) , E->getLoc() });

test/Parse/ConditionalCompilation/language_version_explicit.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,46 @@
3737
let c = 1
3838
#endif
3939
// NOTE: ...the next time the version goes up.
40+
41+
#if swift(<4)
42+
// This shouldn't emit any diagnostics.
43+
asdf asdf asdf asdf
44+
#else
45+
let a = 1
46+
#endif
47+
48+
#if !swift(<4)
49+
let b = 1
50+
#else
51+
// This shouldn't emit any diagnostics.
52+
asdf asdf asdf asdf
53+
#endif
54+
55+
#if swift(<4.0)
56+
// This shouldn't emit any diagnostics.
57+
asdf asdf asdf asdf
58+
#else
59+
let c = 1
60+
#endif
61+
62+
#if swift(<4.0.0)
63+
// This shouldn't emit any diagnostics.
64+
asdf asdf asdf asdf
65+
#else
66+
let d = 1
67+
#endif
68+
69+
#if swift(<4.0.1)
70+
// This shouldn't emit any diagnostics.
71+
asdf asdf asdf asdf
72+
#else
73+
let e = 1
74+
#endif
75+
76+
#if swift(<99)
77+
let f = 1
78+
#else
79+
// This shouldn't emit any diagnostics.
80+
asdf asdf asdf asdf
81+
#endif
82+

0 commit comments

Comments
 (0)