Skip to content

Commit 7183f9c

Browse files
committed
Add conversion operator between version::Version and clang::VersionTuple
1 parent 42c1a6c commit 7183f9c

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

include/swift/Basic/Version.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/ADT/Optional.h"
2525
#include "llvm/ADT/SmallVector.h"
2626
#include "llvm/ADT/StringRef.h"
27+
#include "clang/Basic/VersionTuple.h"
2728
#include <string>
2829

2930
namespace swift {
@@ -89,6 +90,10 @@ class Version {
8990
return Components.empty();
9091
}
9192

93+
/// Convert to a (maximum-4-element) clang::VersionTuple, truncating
94+
/// away any 5th component that might be in this version.
95+
operator clang::VersionTuple() const;
96+
9297
/// Return whether this version is a valid Swift language version number
9398
/// to set the compiler to using -swift-version; this is not the same as
9499
/// the set of Swift versions that have ever existed, just those that we

lib/Basic/Version.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,31 @@ Version::preprocessorDefinition(StringRef macroName,
285285
return define;
286286
}
287287

288+
Version::operator clang::VersionTuple() const
289+
{
290+
switch (Components.size()) {
291+
case 0:
292+
return clang::VersionTuple();
293+
case 1:
294+
return clang::VersionTuple((unsigned)Components[0]);
295+
case 2:
296+
return clang::VersionTuple((unsigned)Components[0],
297+
(unsigned)Components[1]);
298+
case 3:
299+
return clang::VersionTuple((unsigned)Components[0],
300+
(unsigned)Components[1],
301+
(unsigned)Components[2]);
302+
case 4:
303+
case 5:
304+
return clang::VersionTuple((unsigned)Components[0],
305+
(unsigned)Components[1],
306+
(unsigned)Components[2],
307+
(unsigned)Components[3]);
308+
default:
309+
llvm_unreachable("swift::version::Version with 6 or more components");
310+
}
311+
}
312+
288313
bool
289314
Version::isValidEffectiveLanguageVersion() const
290315
{

0 commit comments

Comments
 (0)