[clang] 60a33de - [clang][darwin] Use consistent version define stringifying logic for different Darwin OSes
Alex Lorenz via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 29 14:22:46 PST 2022
Author: Alex Lorenz
Date: 2022-11-29T14:22:34-08:00
New Revision: 60a33ded751c86fff9ac1c4bdd2b341fbe4b0649
URL: https://github.com/llvm/llvm-project/commit/60a33ded751c86fff9ac1c4bdd2b341fbe4b0649
DIFF: https://github.com/llvm/llvm-project/commit/60a33ded751c86fff9ac1c4bdd2b341fbe4b0649.diff
LOG: [clang][darwin] Use consistent version define stringifying logic for different Darwin OSes
Added:
Modified:
clang/lib/Basic/Targets/OSTargets.cpp
clang/test/Frontend/darwin-version.c
Removed:
################################################################################
diff --git a/clang/lib/Basic/Targets/OSTargets.cpp b/clang/lib/Basic/Targets/OSTargets.cpp
index d11b9f5dbba5a..33a5b500e2d1c 100644
--- a/clang/lib/Basic/Targets/OSTargets.cpp
+++ b/clang/lib/Basic/Targets/OSTargets.cpp
@@ -67,48 +67,23 @@ void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
return;
}
- // Set the appropriate OS version define.
- if (Triple.isiOS()) {
- assert(OsVersion < VersionTuple(100) && "Invalid version!");
- char Str[7];
- if (OsVersion.getMajor() < 10) {
- Str[0] = '0' + OsVersion.getMajor();
- Str[1] = '0' + (OsVersion.getMinor().value_or(0) / 10);
- Str[2] = '0' + (OsVersion.getMinor().value_or(0) % 10);
- Str[3] = '0' + (OsVersion.getSubminor().value_or(0) / 10);
- Str[4] = '0' + (OsVersion.getSubminor().value_or(0) % 10);
- Str[5] = '\0';
- } else {
- // Handle versions >= 10.
- Str[0] = '0' + (OsVersion.getMajor() / 10);
- Str[1] = '0' + (OsVersion.getMajor() % 10);
- Str[2] = '0' + (OsVersion.getMinor().value_or(0) / 10);
- Str[3] = '0' + (OsVersion.getMinor().value_or(0) % 10);
- Str[4] = '0' + (OsVersion.getSubminor().value_or(0) / 10);
- Str[5] = '0' + (OsVersion.getSubminor().value_or(0) % 10);
- Str[6] = '\0';
- }
- if (Triple.isTvOS())
- Builder.defineMacro("__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__", Str);
- else
- Builder.defineMacro("__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__",
- Str);
-
- } else if (Triple.isWatchOS()) {
- assert(OsVersion < VersionTuple(10) && "Invalid version!");
- char Str[6];
+ assert(OsVersion < VersionTuple(100) && "Invalid version!");
+ char Str[7];
+ if (Triple.isMacOSX() && OsVersion < VersionTuple(10, 10)) {
+ Str[0] = '0' + (OsVersion.getMajor() / 10);
+ Str[1] = '0' + (OsVersion.getMajor() % 10);
+ Str[2] = '0' + std::min(OsVersion.getMinor().value_or(0), 9U);
+ Str[3] = '0' + std::min(OsVersion.getSubminor().value_or(0), 9U);
+ Str[4] = '\0';
+ } else if (!Triple.isMacOSX() && OsVersion.getMajor() < 10) {
Str[0] = '0' + OsVersion.getMajor();
Str[1] = '0' + (OsVersion.getMinor().value_or(0) / 10);
Str[2] = '0' + (OsVersion.getMinor().value_or(0) % 10);
Str[3] = '0' + (OsVersion.getSubminor().value_or(0) / 10);
Str[4] = '0' + (OsVersion.getSubminor().value_or(0) % 10);
Str[5] = '\0';
- Builder.defineMacro("__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__", Str);
- } else if (Triple.isDriverKit()) {
- assert(OsVersion.getMajor() < 100 &&
- OsVersion.getMinor().value_or(0) < 100 &&
- OsVersion.getSubminor().value_or(0) < 100 && "Invalid version!");
- char Str[7];
+ } else {
+ // Handle versions >= 10.
Str[0] = '0' + (OsVersion.getMajor() / 10);
Str[1] = '0' + (OsVersion.getMajor() % 10);
Str[2] = '0' + (OsVersion.getMinor().value_or(0) / 10);
@@ -116,30 +91,20 @@ void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
Str[4] = '0' + (OsVersion.getSubminor().value_or(0) / 10);
Str[5] = '0' + (OsVersion.getSubminor().value_or(0) % 10);
Str[6] = '\0';
+ }
+
+ // Set the appropriate OS version define.
+ if (Triple.isTvOS()) {
+ Builder.defineMacro("__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__", Str);
+ } else if (Triple.isiOS()) {
+ Builder.defineMacro("__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__", Str);
+ } else if (Triple.isWatchOS()) {
+ Builder.defineMacro("__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__", Str);
+ } else if (Triple.isDriverKit()) {
+ assert(OsVersion.getMinor().value_or(0) < 100 &&
+ OsVersion.getSubminor().value_or(0) < 100 && "Invalid version!");
Builder.defineMacro("__ENVIRONMENT_DRIVERKIT_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(OsVersion < VersionTuple(100) && "Invalid version!");
- char Str[7];
- if (OsVersion < VersionTuple(10, 10)) {
- Str[0] = '0' + (OsVersion.getMajor() / 10);
- Str[1] = '0' + (OsVersion.getMajor() % 10);
- Str[2] = '0' + std::min(OsVersion.getMinor().value_or(0), 9U);
- Str[3] = '0' + std::min(OsVersion.getSubminor().value_or(0), 9U);
- Str[4] = '\0';
- } else {
- // Handle versions > 10.9.
- Str[0] = '0' + (OsVersion.getMajor() / 10);
- Str[1] = '0' + (OsVersion.getMajor() % 10);
- Str[2] = '0' + (OsVersion.getMinor().value_or(0) / 10);
- Str[3] = '0' + (OsVersion.getMinor().value_or(0) % 10);
- Str[4] = '0' + (OsVersion.getSubminor().value_or(0) / 10);
- Str[5] = '0' + (OsVersion.getSubminor().value_or(0) % 10);
- Str[6] = '\0';
- }
Builder.defineMacro("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__", Str);
}
diff --git a/clang/test/Frontend/darwin-version.c b/clang/test/Frontend/darwin-version.c
index 4f38c90c15e73..67f087feafe25 100644
--- a/clang/test/Frontend/darwin-version.c
+++ b/clang/test/Frontend/darwin-version.c
@@ -56,3 +56,9 @@
// RUN: grep '__ENVIRONMENT_DRIVERKIT_VERSION_MIN_REQUIRED__' %t | grep '1900' | count 1
// RUN: not grep '__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__' %t
// RUN: not grep '__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__' %t
+
+// RUN: %clang_cc1 -triple arm64-apple-ios99.99.99 -dM -E %s | FileCheck --check-prefix=IOS99 %s
+// IOS99: __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ 999999
+
+// RUN: %clang_cc1 -triple arm64-apple-watchos99.9 -dM -E %s | FileCheck --check-prefix=WATCHOS99 %s
+// WATCHOS99: __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ 990900
More information about the cfe-commits
mailing list