Skip to content

Commit d5ec182

Browse files
committed
Basic: Change isSwiftVersionAtLeast() to take optional minor version number
I want to be able to do an isSwiftVersionAtLeast(4, 2) check.
1 parent 028a112 commit d5ec182

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

include/swift/AST/ASTContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,8 @@ class ASTContext final {
880880
/// This is usually the check you want; for example, when introducing
881881
/// a new language feature which is only visible in Swift 5, you would
882882
/// check for isSwiftVersionAtLeast(5).
883-
bool isSwiftVersionAtLeast(unsigned major) const {
884-
return LangOpts.isSwiftVersionAtLeast(major);
883+
bool isSwiftVersionAtLeast(unsigned major, unsigned minor = 0) const {
884+
return LangOpts.isSwiftVersionAtLeast(major, minor);
885885
}
886886

887887
private:

include/swift/Basic/LangOptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ namespace swift {
342342
/// This is usually the check you want; for example, when introducing
343343
/// a new language feature which is only visible in Swift 5, you would
344344
/// check for isSwiftVersionAtLeast(5).
345-
bool isSwiftVersionAtLeast(unsigned major) const {
346-
return EffectiveLanguageVersion.isVersionAtLeast(major);
345+
bool isSwiftVersionAtLeast(unsigned major, unsigned minor = 0) const {
346+
return EffectiveLanguageVersion.isVersionAtLeast(major, minor);
347347
}
348348

349349
/// Returns true if the given platform condition argument represents

include/swift/Basic/Version.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,17 @@ class Version {
112112

113113
/// Whether this version is greater than or equal to the given major version
114114
/// number.
115-
bool isVersionAtLeast(unsigned major) const {
116-
return !empty() && Components[0] >= major;
115+
bool isVersionAtLeast(unsigned major, unsigned minor = 0) const {
116+
switch (size()) {
117+
case 0:
118+
return false;
119+
case 1:
120+
return ((Components[0] == major && 0 == minor) ||
121+
(Components[0] > major));
122+
default:
123+
return ((Components[0] == major && Components[1] >= minor) ||
124+
(Components[0] > major));
125+
}
117126
}
118127

119128
/// Return this Version struct with minor and sub-minor components stripped

0 commit comments

Comments
 (0)