Skip to content

Commit

Permalink
Use VersionTuple for parsing versions in Triple. This makes it possib…
Browse files Browse the repository at this point in the history
…le to distinguish between "16" and "16.0" after parsing, which previously was not possible.

See also android/ndk#1455.

Differential Revision: https://reviews.llvm.org/D114163
  • Loading branch information
jfgoog committed Nov 30, 2021
1 parent c9ad356 commit 1e82864
Show file tree
Hide file tree
Showing 23 changed files with 292 additions and 377 deletions.
4 changes: 1 addition & 3 deletions clang/lib/ARCMigrate/ARCMT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,7 @@ static bool HasARCRuntime(CompilerInvocation &origCI) {
return triple.getOSMajorVersion() >= 11;

if (triple.getOS() == llvm::Triple::MacOSX) {
unsigned Major, Minor, Micro;
triple.getOSVersion(Major, Minor, Micro);
return Major > 10 || (Major == 10 && Minor >= 7);
return triple.getOSVersion() >= VersionTuple(10, 7);
}

return false;
Expand Down
72 changes: 36 additions & 36 deletions clang/lib/Basic/Targets/OSTargets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
Builder.defineMacro("_REENTRANT");

// Get the platform type and version number from the triple.
unsigned Maj, Min, Rev;
VersionTuple OsVersion;
if (Triple.isMacOSX()) {
Triple.getMacOSXVersion(Maj, Min, Rev);
Triple.getMacOSXVersion(OsVersion);
PlatformName = "macos";
} else {
Triple.getOSVersion(Maj, Min, Rev);
OsVersion = Triple.getOSVersion();
PlatformName = llvm::Triple::getOSTypeName(Triple.getOS());
if (PlatformName == "ios" && Triple.isMacCatalystEnvironment())
PlatformName = "maccatalyst";
Expand All @@ -63,29 +63,29 @@ void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
// generating code for Win32 ABI. No need to emit
// __ENVIRONMENT_XX_OS_VERSION_MIN_REQUIRED__.
if (PlatformName == "win32") {
PlatformMinVersion = VersionTuple(Maj, Min, Rev);
PlatformMinVersion = OsVersion;
return;
}

// Set the appropriate OS version define.
if (Triple.isiOS()) {
assert(Maj < 100 && Min < 100 && Rev < 100 && "Invalid version!");
assert(OsVersion < VersionTuple(100) && "Invalid version!");
char Str[7];
if (Maj < 10) {
Str[0] = '0' + Maj;
Str[1] = '0' + (Min / 10);
Str[2] = '0' + (Min % 10);
Str[3] = '0' + (Rev / 10);
Str[4] = '0' + (Rev % 10);
if (OsVersion.getMajor() < 10) {
Str[0] = '0' + OsVersion.getMajor();
Str[1] = '0' + (OsVersion.getMinor().getValueOr(0) / 10);
Str[2] = '0' + (OsVersion.getMinor().getValueOr(0) % 10);
Str[3] = '0' + (OsVersion.getSubminor().getValueOr(0) / 10);
Str[4] = '0' + (OsVersion.getSubminor().getValueOr(0) % 10);
Str[5] = '\0';
} else {
// Handle versions >= 10.
Str[0] = '0' + (Maj / 10);
Str[1] = '0' + (Maj % 10);
Str[2] = '0' + (Min / 10);
Str[3] = '0' + (Min % 10);
Str[4] = '0' + (Rev / 10);
Str[5] = '0' + (Rev % 10);
Str[0] = '0' + (OsVersion.getMajor() / 10);
Str[1] = '0' + (OsVersion.getMajor() % 10);
Str[2] = '0' + (OsVersion.getMinor().getValueOr(0) / 10);
Str[3] = '0' + (OsVersion.getMinor().getValueOr(0) % 10);
Str[4] = '0' + (OsVersion.getSubminor().getValueOr(0) / 10);
Str[5] = '0' + (OsVersion.getSubminor().getValueOr(0) % 10);
Str[6] = '\0';
}
if (Triple.isTvOS())
Expand All @@ -95,36 +95,36 @@ void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
Str);

} else if (Triple.isWatchOS()) {
assert(Maj < 10 && Min < 100 && Rev < 100 && "Invalid version!");
assert(OsVersion < VersionTuple(10) && "Invalid version!");
char Str[6];
Str[0] = '0' + Maj;
Str[1] = '0' + (Min / 10);
Str[2] = '0' + (Min % 10);
Str[3] = '0' + (Rev / 10);
Str[4] = '0' + (Rev % 10);
Str[0] = '0' + OsVersion.getMajor();
Str[1] = '0' + (OsVersion.getMinor().getValueOr(0) / 10);
Str[2] = '0' + (OsVersion.getMinor().getValueOr(0) % 10);
Str[3] = '0' + (OsVersion.getSubminor().getValueOr(0) / 10);
Str[4] = '0' + (OsVersion.getSubminor().getValueOr(0) % 10);
Str[5] = '\0';
Builder.defineMacro("__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__", Str);
} else if (Triple.isMacOSX()) {
// Note that the Driver allows versions which aren't representable in the
// define (because we only get a single digit for the minor and micro
// revision numbers). So, we limit them to the maximum representable
// version.
assert(Maj < 100 && Min < 100 && Rev < 100 && "Invalid version!");
assert(OsVersion < VersionTuple(100) && "Invalid version!");
char Str[7];
if (Maj < 10 || (Maj == 10 && Min < 10)) {
Str[0] = '0' + (Maj / 10);
Str[1] = '0' + (Maj % 10);
Str[2] = '0' + std::min(Min, 9U);
Str[3] = '0' + std::min(Rev, 9U);
if (OsVersion < VersionTuple(10, 10)) {
Str[0] = '0' + (OsVersion.getMajor() / 10);
Str[1] = '0' + (OsVersion.getMajor() % 10);
Str[2] = '0' + std::min(OsVersion.getMinor().getValueOr(0), 9U);
Str[3] = '0' + std::min(OsVersion.getSubminor().getValueOr(0), 9U);
Str[4] = '\0';
} else {
// Handle versions > 10.9.
Str[0] = '0' + (Maj / 10);
Str[1] = '0' + (Maj % 10);
Str[2] = '0' + (Min / 10);
Str[3] = '0' + (Min % 10);
Str[4] = '0' + (Rev / 10);
Str[5] = '0' + (Rev % 10);
Str[0] = '0' + (OsVersion.getMajor() / 10);
Str[1] = '0' + (OsVersion.getMajor() % 10);
Str[2] = '0' + (OsVersion.getMinor().getValueOr(0) / 10);
Str[3] = '0' + (OsVersion.getMinor().getValueOr(0) % 10);
Str[4] = '0' + (OsVersion.getSubminor().getValueOr(0) / 10);
Str[5] = '0' + (OsVersion.getSubminor().getValueOr(0) % 10);
Str[6] = '\0';
}
Builder.defineMacro("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__", Str);
Expand All @@ -134,7 +134,7 @@ void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
if (Triple.isOSDarwin())
Builder.defineMacro("__MACH__");

PlatformMinVersion = VersionTuple(Maj, Min, Rev);
PlatformMinVersion = OsVersion;
}

static void addMinGWDefines(const llvm::Triple &Triple, const LangOptions &Opts,
Expand Down
48 changes: 27 additions & 21 deletions clang/lib/Basic/Targets/OSTargets.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ class LLVM_LIBRARY_VISIBILITY DarwinTargetInfo : public OSTargetInfo<Target> {
return 64;
}

unsigned Major, Minor, Micro;
T.getOSVersion(Major, Minor, Micro);
if (llvm::VersionTuple(Major, Minor, Micro) < MinVersion)
if (T.getOSVersion() < MinVersion)
return 64;
return OSTargetInfo<Target>::getExnObjectAlignment();
}
Expand Down Expand Up @@ -294,7 +292,7 @@ class LLVM_LIBRARY_VISIBILITY HaikuTargetInfo : public OSTargetInfo<Target> {
Builder.defineMacro("__HAIKU__");
Builder.defineMacro("__ELF__");
DefineStd(Builder, "unix", Opts);
if (this->HasFloat128)
if (this->HasFloat128)
Builder.defineMacro("__FLOAT128__");
}

Expand Down Expand Up @@ -376,10 +374,9 @@ class LLVM_LIBRARY_VISIBILITY LinuxTargetInfo : public OSTargetInfo<Target> {
Builder.defineMacro("__ELF__");
if (Triple.isAndroid()) {
Builder.defineMacro("__ANDROID__", "1");
unsigned Maj, Min, Rev;
Triple.getEnvironmentVersion(Maj, Min, Rev);
this->PlatformName = "android";
this->PlatformMinVersion = VersionTuple(Maj, Min, Rev);
this->PlatformMinVersion = Triple.getEnvironmentVersion();
const unsigned Maj = this->PlatformMinVersion.getMajor();
if (Maj) {
Builder.defineMacro("__ANDROID_MIN_SDK_VERSION__", Twine(Maj));
// This historical but ambiguous name for the minSdkVersion macro. Keep
Expand Down Expand Up @@ -693,23 +690,32 @@ class AIXTargetInfo : public OSTargetInfo<Target> {
if (Opts.EnableAIXExtendedAltivecABI)
Builder.defineMacro("__EXTABI__");

unsigned Major, Minor, Micro;
Triple.getOSVersion(Major, Minor, Micro);
VersionTuple OsVersion = Triple.getOSVersion();

// Define AIX OS-Version Macros.
// Includes logic for legacy versions of AIX; no specific intent to support.
std::pair<int, int> OsVersion = {Major, Minor};
if (OsVersion >= std::make_pair(3, 2)) Builder.defineMacro("_AIX32");
if (OsVersion >= std::make_pair(4, 1)) Builder.defineMacro("_AIX41");
if (OsVersion >= std::make_pair(4, 3)) Builder.defineMacro("_AIX43");
if (OsVersion >= std::make_pair(5, 0)) Builder.defineMacro("_AIX50");
if (OsVersion >= std::make_pair(5, 1)) Builder.defineMacro("_AIX51");
if (OsVersion >= std::make_pair(5, 2)) Builder.defineMacro("_AIX52");
if (OsVersion >= std::make_pair(5, 3)) Builder.defineMacro("_AIX53");
if (OsVersion >= std::make_pair(6, 1)) Builder.defineMacro("_AIX61");
if (OsVersion >= std::make_pair(7, 1)) Builder.defineMacro("_AIX71");
if (OsVersion >= std::make_pair(7, 2)) Builder.defineMacro("_AIX72");
if (OsVersion >= std::make_pair(7, 3)) Builder.defineMacro("_AIX73");
if (OsVersion >= VersionTuple(3, 2))
Builder.defineMacro("_AIX32");
if (OsVersion >= VersionTuple(4, 1))
Builder.defineMacro("_AIX41");
if (OsVersion >= VersionTuple(4, 3))
Builder.defineMacro("_AIX43");
if (OsVersion >= VersionTuple(5, 0))
Builder.defineMacro("_AIX50");
if (OsVersion >= VersionTuple(5, 1))
Builder.defineMacro("_AIX51");
if (OsVersion >= VersionTuple(5, 2))
Builder.defineMacro("_AIX52");
if (OsVersion >= VersionTuple(5, 3))
Builder.defineMacro("_AIX53");
if (OsVersion >= VersionTuple(6, 1))
Builder.defineMacro("_AIX61");
if (OsVersion >= VersionTuple(7, 1))
Builder.defineMacro("_AIX71");
if (OsVersion >= VersionTuple(7, 2))
Builder.defineMacro("_AIX72");
if (OsVersion >= VersionTuple(7, 3))
Builder.defineMacro("_AIX73");

// FIXME: Do not define _LONG_LONG when -fno-long-long is specified.
Builder.defineMacro("_LONG_LONG");
Expand Down
5 changes: 2 additions & 3 deletions clang/lib/Basic/Targets/X86.h
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,9 @@ class LLVM_LIBRARY_VISIBILITY NetBSDI386TargetInfo
: NetBSDTargetInfo<X86_32TargetInfo>(Triple, Opts) {}

unsigned getFloatEvalMethod() const override {
unsigned Major, Minor, Micro;
getTriple().getOSVersion(Major, Minor, Micro);
VersionTuple OsVersion = getTriple().getOSVersion();
// New NetBSD uses the default rounding mode.
if (Major >= 7 || (Major == 6 && Minor == 99 && Micro >= 26) || Major == 0)
if (OsVersion >= VersionTuple(6, 99, 26) || OsVersion.getMajor() == 0)
return X86_32TargetInfo::getFloatEvalMethod();
// NetBSD before 6.99.26 defaults to "double" rounding.
return 1;
Expand Down
40 changes: 18 additions & 22 deletions clang/lib/Driver/ToolChains/Darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1412,8 +1412,8 @@ static std::string getSystemOrSDKMacOSVersion(StringRef MacOSSDKVersion) {
llvm::Triple SystemTriple(llvm::sys::getProcessTriple());
if (!SystemTriple.isMacOSX())
return std::string(MacOSSDKVersion);
SystemTriple.getMacOSXVersion(Major, Minor, Micro);
VersionTuple SystemVersion(Major, Minor, Micro);
VersionTuple SystemVersion;
SystemTriple.getMacOSXVersion(SystemVersion);
bool HadExtra;
if (!Driver::GetReleaseVersion(MacOSSDKVersion, Major, Minor, Micro,
HadExtra))
Expand Down Expand Up @@ -1554,12 +1554,10 @@ struct DarwinPlatform {
const Optional<DarwinSDKInfo> &SDKInfo) {
DarwinPlatform Result(TargetArg, getPlatformFromOS(TT.getOS()), OSVersion,
A);
unsigned Major, Minor, Micro;
TT.getOSVersion(Major, Minor, Micro);
if (Major == 0)
VersionTuple OsVersion = TT.getOSVersion();
if (OsVersion.getMajor() == 0)
Result.HasOSVersion = false;
Result.setEnvironment(TT.getEnvironment(),
VersionTuple(Major, Minor, Micro), SDKInfo);
Result.setEnvironment(TT.getEnvironment(), OsVersion, SDKInfo);
return Result;
}
static DarwinPlatform
Expand Down Expand Up @@ -1805,7 +1803,7 @@ inferDeploymentTargetFromSDK(DerivedArgList &Args,

std::string getOSVersion(llvm::Triple::OSType OS, const llvm::Triple &Triple,
const Driver &TheDriver) {
unsigned Major, Minor, Micro;
VersionTuple OsVersion;
llvm::Triple SystemTriple(llvm::sys::getProcessTriple());
switch (OS) {
case llvm::Triple::Darwin:
Expand All @@ -1814,32 +1812,32 @@ std::string getOSVersion(llvm::Triple::OSType OS, const llvm::Triple &Triple,
// macos, use the host triple to infer OS version.
if (Triple.isMacOSX() && SystemTriple.isMacOSX() &&
!Triple.getOSMajorVersion())
SystemTriple.getMacOSXVersion(Major, Minor, Micro);
else if (!Triple.getMacOSXVersion(Major, Minor, Micro))
SystemTriple.getMacOSXVersion(OsVersion);
else if (!Triple.getMacOSXVersion(OsVersion))
TheDriver.Diag(diag::err_drv_invalid_darwin_version)
<< Triple.getOSName();
break;
case llvm::Triple::IOS:
if (Triple.isMacCatalystEnvironment() && !Triple.getOSMajorVersion()) {
Major = 13;
Minor = 1;
Micro = 0;
OsVersion = VersionTuple(13, 1);
} else
Triple.getiOSVersion(Major, Minor, Micro);
OsVersion = Triple.getiOSVersion();
break;
case llvm::Triple::TvOS:
Triple.getOSVersion(Major, Minor, Micro);
OsVersion = Triple.getOSVersion();
break;
case llvm::Triple::WatchOS:
Triple.getWatchOSVersion(Major, Minor, Micro);
OsVersion = Triple.getWatchOSVersion();
break;
default:
llvm_unreachable("Unexpected OS type");
break;
}

std::string OSVersion;
llvm::raw_string_ostream(OSVersion) << Major << '.' << Minor << '.' << Micro;
llvm::raw_string_ostream(OSVersion)
<< OsVersion.getMajor() << '.' << OsVersion.getMinor().getValueOr(0)
<< '.' << OsVersion.getSubminor().getValueOr(0);
return OSVersion;
}

Expand Down Expand Up @@ -1909,15 +1907,13 @@ getDeploymentTargetFromMTargetOSArg(DerivedArgList &Args,
return None;
}

unsigned Major, Minor, Micro;
TT.getOSVersion(Major, Minor, Micro);
if (!Major) {
VersionTuple Version = TT.getOSVersion();
if (!Version.getMajor()) {
TheDriver.Diag(diag::err_drv_invalid_version_number)
<< A->getAsString(Args);
return None;
}
return DarwinPlatform::createFromMTargetOS(TT.getOS(),
VersionTuple(Major, Minor, Micro),
return DarwinPlatform::createFromMTargetOS(TT.getOS(), Version,
TT.getEnvironment(), A, SDKInfo);
}

Expand Down
13 changes: 5 additions & 8 deletions clang/lib/Driver/ToolChains/Linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,11 @@ Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
// Android sysroots contain a library directory for each supported OS
// version as well as some unversioned libraries in the usual multiarch
// directory.
unsigned Major;
unsigned Minor;
unsigned Micro;
Triple.getEnvironmentVersion(Major, Minor, Micro);
addPathIfExists(D,
SysRoot + "/usr/lib/" + MultiarchTriple + "/" +
llvm::to_string(Major),
Paths);
addPathIfExists(
D,
SysRoot + "/usr/lib/" + MultiarchTriple + "/" +
llvm::to_string(Triple.getEnvironmentVersion().getMajor()),
Paths);
}

addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
Expand Down
10 changes: 1 addition & 9 deletions clang/lib/Driver/ToolChains/MSVC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1194,14 +1194,6 @@ bool MSVCToolChain::getUniversalCRTLibraryPath(const ArgList &Args,
return true;
}

static VersionTuple getMSVCVersionFromTriple(const llvm::Triple &Triple) {
unsigned Major, Minor, Micro;
Triple.getEnvironmentVersion(Major, Minor, Micro);
if (Major || Minor || Micro)
return VersionTuple(Major, Minor, Micro);
return VersionTuple();
}

static VersionTuple getMSVCVersionFromExe(const std::string &BinDir) {
VersionTuple Version;
#ifdef _WIN32
Expand Down Expand Up @@ -1374,7 +1366,7 @@ VersionTuple MSVCToolChain::computeMSVCVersion(const Driver *D,
bool IsWindowsMSVC = getTriple().isWindowsMSVCEnvironment();
VersionTuple MSVT = ToolChain::computeMSVCVersion(D, Args);
if (MSVT.empty())
MSVT = getMSVCVersionFromTriple(getTriple());
MSVT = getTriple().getEnvironmentVersion();
if (MSVT.empty() && IsWindowsMSVC)
MSVT = getMSVCVersionFromExe(getSubDirectoryPath(SubDirectoryType::Bin));
if (MSVT.empty() &&
Expand Down
Loading

0 comments on commit 1e82864

Please sign in to comment.