[clang] [Darwin][Driver][clang] arm64-apple-none-macho is missing the Apple macros from arm-apple-none-macho (PR #122427)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 9 22:54:45 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-arm
Author: Ian Anderson (ian-twilightcoder)
<details>
<summary>Changes</summary>
arm-apple-none-macho uses DarwinTargetInfo which provides several Apple specific macros. arm64-apple-none-macho however just uses the generic AArch64leTargetInfo and doesn't get any of those macros. It's not clear if everything from DarwinTargetInfo is desirable for arm64-apple-none-macho, so make an AppleMachOTargetInfo to hold the generic Apple macros and a few other basic things.
---
Full diff: https://github.com/llvm/llvm-project/pull/122427.diff
10 Files Affected:
- (modified) clang/lib/Basic/Targets.cpp (+8)
- (modified) clang/lib/Basic/Targets/AArch64.cpp (+20-3)
- (modified) clang/lib/Basic/Targets/AArch64.h (+13)
- (modified) clang/lib/Basic/Targets/ARM.cpp (+10)
- (modified) clang/lib/Basic/Targets/ARM.h (+11)
- (modified) clang/lib/Basic/Targets/OSTargets.cpp (+19-6)
- (modified) clang/lib/Basic/Targets/OSTargets.h (+30-8)
- (modified) clang/lib/Basic/Targets/X86.h (+8)
- (modified) clang/lib/Frontend/InitPreprocessor.cpp (-5)
- (modified) clang/test/Preprocessor/macho-embedded-predefines.c (+15)
``````````diff
diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index be5dedbe8044e2..f61beb98e0ac0e 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -135,11 +135,15 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
case llvm::Triple::aarch64_32:
if (Triple.isOSDarwin())
return std::make_unique<DarwinAArch64TargetInfo>(Triple, Opts);
+ else if (Triple.isAppleMachO())
+ return std::make_unique<AppleMachOAArch64TargetInfo>(Triple, Opts);
return nullptr;
case llvm::Triple::aarch64:
if (Triple.isOSDarwin())
return std::make_unique<DarwinAArch64TargetInfo>(Triple, Opts);
+ else if (Triple.isAppleMachO())
+ return std::make_unique<AppleMachOAArch64TargetInfo>(Triple, Opts);
switch (os) {
case llvm::Triple::FreeBSD:
@@ -243,6 +247,8 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
case llvm::Triple::thumbeb:
if (Triple.isOSDarwin())
return std::make_unique<DarwinARMTargetInfo>(Triple, Opts);
+ else if (Triple.isAppleMachO())
+ return std::make_unique<AppleMachOARMTargetInfo>(Triple, Opts);
switch (os) {
case llvm::Triple::Linux:
@@ -531,6 +537,8 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
case llvm::Triple::x86:
if (Triple.isOSDarwin())
return std::make_unique<DarwinI386TargetInfo>(Triple, Opts);
+ else if (Triple.isAppleMachO())
+ return std::make_unique<AppleMachOI386TargetInfo>(Triple, Opts);
switch (os) {
case llvm::Triple::Linux: {
diff --git a/clang/lib/Basic/Targets/AArch64.cpp b/clang/lib/Basic/Targets/AArch64.cpp
index 2b4b954d0c27ad..1bf58661d0efcd 100644
--- a/clang/lib/Basic/Targets/AArch64.cpp
+++ b/clang/lib/Basic/Targets/AArch64.cpp
@@ -1671,6 +1671,10 @@ MinGWARM64TargetInfo::MinGWARM64TargetInfo(const llvm::Triple &Triple,
TheCXXABI.set(TargetCXXABI::GenericAArch64);
}
+AppleMachOAArch64TargetInfo::AppleMachOAArch64TargetInfo(
+ const llvm::Triple &Triple, const TargetOptions &Opts)
+ : AppleMachOTargetInfo<AArch64leTargetInfo>(Triple, Opts) {}
+
DarwinAArch64TargetInfo::DarwinAArch64TargetInfo(const llvm::Triple &Triple,
const TargetOptions &Opts)
: DarwinTargetInfo<AArch64leTargetInfo>(Triple, Opts) {
@@ -1695,9 +1699,9 @@ DarwinAArch64TargetInfo::DarwinAArch64TargetInfo(const llvm::Triple &Triple,
TheCXXABI.set(TargetCXXABI::AppleARM64);
}
-void DarwinAArch64TargetInfo::getOSDefines(const LangOptions &Opts,
- const llvm::Triple &Triple,
- MacroBuilder &Builder) const {
+void clang::targets::getAppleMachOAArch64Defines(MacroBuilder &Builder,
+ const LangOptions &Opts,
+ const llvm::Triple &Triple) {
Builder.defineMacro("__AARCH64_SIMD__");
if (Triple.isArch32Bit())
Builder.defineMacro("__ARM64_ARCH_8_32__");
@@ -1710,7 +1714,20 @@ void DarwinAArch64TargetInfo::getOSDefines(const LangOptions &Opts,
if (Triple.isArm64e())
Builder.defineMacro("__arm64e__", "1");
+}
+void AppleMachOAArch64TargetInfo::getOSDefines(const LangOptions &Opts,
+ const llvm::Triple &Triple,
+ MacroBuilder &Builder) const {
+ getAppleMachOAArch64Defines(Builder, Opts, Triple);
+ AppleMachOTargetInfo<AArch64leTargetInfo>::getOSDefines(Opts, Triple,
+ Builder);
+}
+
+void DarwinAArch64TargetInfo::getOSDefines(const LangOptions &Opts,
+ const llvm::Triple &Triple,
+ MacroBuilder &Builder) const {
+ getAppleMachOAArch64Defines(Builder, Opts, Triple);
DarwinTargetInfo<AArch64leTargetInfo>::getOSDefines(Opts, Triple, Builder);
}
diff --git a/clang/lib/Basic/Targets/AArch64.h b/clang/lib/Basic/Targets/AArch64.h
index 4e927c0953b1fc..9b2acc26117238 100644
--- a/clang/lib/Basic/Targets/AArch64.h
+++ b/clang/lib/Basic/Targets/AArch64.h
@@ -306,6 +306,19 @@ class LLVM_LIBRARY_VISIBILITY AArch64beTargetInfo : public AArch64TargetInfo {
void setDataLayout() override;
};
+void getAppleMachOAArch64Defines(MacroBuilder &Builder, const LangOptions &Opts,
+ const llvm::Triple &Triple);
+
+class LLVM_LIBRARY_VISIBILITY AppleMachOAArch64TargetInfo
+ : public AppleMachOTargetInfo<AArch64leTargetInfo> {
+public:
+ AppleMachOAArch64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts);
+
+ protected:
+ void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
+ MacroBuilder &Builder) const override;
+};
+
class LLVM_LIBRARY_VISIBILITY DarwinAArch64TargetInfo
: public DarwinTargetInfo<AArch64leTargetInfo> {
public:
diff --git a/clang/lib/Basic/Targets/ARM.cpp b/clang/lib/Basic/Targets/ARM.cpp
index 370444057b4298..61ee26d8863832 100644
--- a/clang/lib/Basic/Targets/ARM.cpp
+++ b/clang/lib/Basic/Targets/ARM.cpp
@@ -1479,6 +1479,16 @@ void CygwinARMTargetInfo::getTargetDefines(const LangOptions &Opts,
Builder.defineMacro("_GNU_SOURCE");
}
+AppleMachOARMTargetInfo::AppleMachOARMTargetInfo(const llvm::Triple &Triple,
+ const TargetOptions &Opts)
+ : AppleMachOTargetInfo<ARMleTargetInfo>(Triple, Opts) {}
+
+void AppleMachOARMTargetInfo::getOSDefines(const LangOptions &Opts,
+ const llvm::Triple &Triple,
+ MacroBuilder &Builder) const {
+ getAppleMachODefines(Builder, Opts, Triple);
+}
+
DarwinARMTargetInfo::DarwinARMTargetInfo(const llvm::Triple &Triple,
const TargetOptions &Opts)
: DarwinTargetInfo<ARMleTargetInfo>(Triple, Opts) {
diff --git a/clang/lib/Basic/Targets/ARM.h b/clang/lib/Basic/Targets/ARM.h
index 55ecb99d82d8fb..fdb40c3d41918a 100644
--- a/clang/lib/Basic/Targets/ARM.h
+++ b/clang/lib/Basic/Targets/ARM.h
@@ -300,6 +300,17 @@ class LLVM_LIBRARY_VISIBILITY CygwinARMTargetInfo : public ARMleTargetInfo {
MacroBuilder &Builder) const override;
};
+class LLVM_LIBRARY_VISIBILITY AppleMachOARMTargetInfo
+ : public AppleMachOTargetInfo<ARMleTargetInfo> {
+protected:
+ void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
+ MacroBuilder &Builder) const override;
+
+public:
+ AppleMachOARMTargetInfo(const llvm::Triple &Triple,
+ const TargetOptions &Opts);
+};
+
class LLVM_LIBRARY_VISIBILITY DarwinARMTargetInfo
: public DarwinTargetInfo<ARMleTargetInfo> {
protected:
diff --git a/clang/lib/Basic/Targets/OSTargets.cpp b/clang/lib/Basic/Targets/OSTargets.cpp
index 6f98353fb8c2e4..c0c2a51f0dfeef 100644
--- a/clang/lib/Basic/Targets/OSTargets.cpp
+++ b/clang/lib/Basic/Targets/OSTargets.cpp
@@ -19,19 +19,17 @@ using namespace clang::targets;
namespace clang {
namespace targets {
-void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
- const llvm::Triple &Triple, StringRef &PlatformName,
- VersionTuple &PlatformMinVersion) {
+void getAppleMachODefines(MacroBuilder &Builder, const LangOptions &Opts,
+ const llvm::Triple &Triple) {
Builder.defineMacro("__APPLE_CC__", "6000");
Builder.defineMacro("__APPLE__");
- Builder.defineMacro("__STDC_NO_THREADS__");
// AddressSanitizer doesn't play well with source fortification, which is on
- // by default on Darwin.
+ // by default on Apple platforms.
if (Opts.Sanitize.has(SanitizerKind::Address))
Builder.defineMacro("_FORTIFY_SOURCE", "0");
- // Darwin defines __weak, __strong, and __unsafe_unretained even in C mode.
+ // Apple defines __weak, __strong, and __unsafe_unretained even in C mode.
if (!Opts.ObjC) {
// __weak is always defined, for use in blocks and with objc pointers.
Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
@@ -47,6 +45,21 @@ void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
if (Opts.POSIXThreads)
Builder.defineMacro("_REENTRANT");
+ // __MACH__ originally meant "will run in a Mach kernel based OS", but it has
+ // come to also mean "uses the Mach-O format". Notably libc++'s
+ // __configuration/platform.h and Swift's shims/Visibility.h check __MACH__ to
+ // figure out if they're using Mach-O.
+ Builder.defineMacro("__MACH__");
+}
+
+void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
+ const llvm::Triple &Triple, StringRef &PlatformName,
+ VersionTuple &PlatformMinVersion) {
+ getAppleMachODefines(Builder, Opts, Triple);
+
+ // Darwin's libc doesn't have threads.h
+ Builder.defineMacro("__STDC_NO_THREADS__");
+
// Get the platform type and version number from the triple.
VersionTuple OsVersion;
if (Triple.isMacOSX()) {
diff --git a/clang/lib/Basic/Targets/OSTargets.h b/clang/lib/Basic/Targets/OSTargets.h
index 53dd23c3129636..5343c473e37463 100644
--- a/clang/lib/Basic/Targets/OSTargets.h
+++ b/clang/lib/Basic/Targets/OSTargets.h
@@ -34,12 +34,39 @@ class LLVM_LIBRARY_VISIBILITY OSTargetInfo : public TgtInfo {
}
};
+void getAppleMachODefines(MacroBuilder &Builder, const LangOptions &Opts,
+ const llvm::Triple &Triple);
+
void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
const llvm::Triple &Triple, StringRef &PlatformName,
VersionTuple &PlatformMinVersion);
template <typename Target>
-class LLVM_LIBRARY_VISIBILITY DarwinTargetInfo : public OSTargetInfo<Target> {
+class LLVM_LIBRARY_VISIBILITY AppleMachOTargetInfo
+ : public OSTargetInfo<Target> {
+protected:
+ void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
+ MacroBuilder &Builder) const override {
+ getAppleMachODefines(Builder, Opts, Triple);
+ }
+
+public:
+ AppleMachOTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
+ : OSTargetInfo<Target>(Triple, Opts) {}
+
+ const char *getStaticInitSectionSpecifier() const override {
+ return "__TEXT,__StaticInit,regular,pure_instructions";
+ }
+
+ /// Apple Mach-O does not support protected visibility. Its "default" is very
+ /// similar to ELF's "protected"; Apple Mach-O requires a "weak" attribute on
+ /// declarations that can be dynamically replaced.
+ bool hasProtectedVisibility() const override { return false; }
+};
+
+template <typename Target>
+class LLVM_LIBRARY_VISIBILITY DarwinTargetInfo
+ : public AppleMachOTargetInfo<Target> {
protected:
void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
MacroBuilder &Builder) const override {
@@ -49,7 +76,7 @@ class LLVM_LIBRARY_VISIBILITY DarwinTargetInfo : public OSTargetInfo<Target> {
public:
DarwinTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
- : OSTargetInfo<Target>(Triple, Opts) {
+ : AppleMachOTargetInfo<Target>(Triple, Opts) {
// By default, no TLS, and we list permitted architecture/OS
// combinations.
this->TLSSupported = false;
@@ -82,14 +109,9 @@ class LLVM_LIBRARY_VISIBILITY DarwinTargetInfo : public OSTargetInfo<Target> {
const char *getStaticInitSectionSpecifier() const override {
// FIXME: We should return 0 when building kexts.
- return "__TEXT,__StaticInit,regular,pure_instructions";
+ return AppleMachOTargetInfo<Target>::getStaticInitSectionSpecifier();
}
- /// Darwin does not support protected visibility. Darwin's "default"
- /// is very similar to ELF's "protected"; Darwin requires a "weak"
- /// attribute on declarations that can be dynamically replaced.
- bool hasProtectedVisibility() const override { return false; }
-
unsigned getExnObjectAlignment() const override {
// Older versions of libc++abi guarantee an alignment of only 8-bytes for
// exception objects because of a bug in __cxa_exception that was
diff --git a/clang/lib/Basic/Targets/X86.h b/clang/lib/Basic/Targets/X86.h
index 35aceb1c58e142..2c200e64a3d84d 100644
--- a/clang/lib/Basic/Targets/X86.h
+++ b/clang/lib/Basic/Targets/X86.h
@@ -534,6 +534,14 @@ class LLVM_LIBRARY_VISIBILITY OpenBSDI386TargetInfo
}
};
+class LLVM_LIBRARY_VISIBILITY AppleMachOI386TargetInfo
+ : public AppleMachOTargetInfo<X86_32TargetInfo> {
+public:
+ AppleMachOI386TargetInfo(const llvm::Triple &Triple,
+ const TargetOptions &Opts)
+ : AppleMachOTargetInfo<X86_32TargetInfo>(Triple, Opts) {}
+};
+
class LLVM_LIBRARY_VISIBILITY DarwinI386TargetInfo
: public DarwinTargetInfo<X86_32TargetInfo> {
public:
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp
index 8eba766f21a640..29723b573e771a 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -1507,11 +1507,6 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
// ELF targets define __ELF__
if (TI.getTriple().isOSBinFormatELF())
Builder.defineMacro("__ELF__");
- else if (TI.getTriple().isAppleMachO())
- // Apple MachO targets define __MACH__ even when not using DarwinTargetInfo.
- // Hurd will also define this in some circumstances, but that's done in
- // HurdTargetInfo. Windows targets don't define this.
- Builder.defineMacro("__MACH__");
// Target OS macro definitions.
if (PPOpts.DefineTargetOSMacros) {
diff --git a/clang/test/Preprocessor/macho-embedded-predefines.c b/clang/test/Preprocessor/macho-embedded-predefines.c
index a7e5777a89a980..0fb4387fe6b18e 100644
--- a/clang/test/Preprocessor/macho-embedded-predefines.c
+++ b/clang/test/Preprocessor/macho-embedded-predefines.c
@@ -1,20 +1,35 @@
+// RUN: %clang_cc1 -E -dM -triple arm64-apple-none-macho -target-cpu generic %s | FileCheck %s -check-prefix CHECK-64
+
+// CHECK-64: #define __APPLE_CC__
+// CHECK-64: #define __APPLE__
+// CHECK-64: #define __ARM_64BIT_STATE 1
+// CHECK-64-NOT: __ENVIRONMENT_OS_VERSION_MIN_REQUIRED__
+// CHECK-64: #define __MACH__
+// CHECK-64-NOT: __STDC_NO_THREADS__
+
// RUN: %clang_cc1 -E -dM -triple thumbv7m-apple-unknown-macho -target-cpu cortex-m3 %s | FileCheck %s -check-prefix CHECK-7M
// CHECK-7M: #define __APPLE_CC__
// CHECK-7M: #define __APPLE__
// CHECK-7M: #define __ARM_ARCH_7M__
+// CHECK-7M-NOT: __ENVIRONMENT_OS_VERSION_MIN_REQUIRED__
// CHECK-7M: #define __MACH__
+// CHECK-7M: __STDC_NO_THREADS__
// RUN: %clang_cc1 -E -dM -triple thumbv7em-apple-unknown-macho -target-cpu cortex-m4 %s | FileCheck %s -check-prefix CHECK-7EM
// CHECK-7EM: #define __APPLE_CC__
// CHECK-7EM: #define __APPLE__
// CHECK-7EM: #define __ARM_ARCH_7EM__
+// CHECK-7EM-NOT: __ENVIRONMENT_OS_VERSION_MIN_REQUIRED__
// CHECK-7EM: #define __MACH__
+// CHECK-7EM: __STDC_NO_THREADS__
// RUN: %clang_cc1 -E -dM -triple thumbv6m-apple-unknown-macho -target-cpu cortex-m0 %s | FileCheck %s -check-prefix CHECK-6M
// CHECK-6M: #define __APPLE_CC__
// CHECK-6M: #define __APPLE__
// CHECK-6M: #define __ARM_ARCH_6M__
+// CHECK-6M-NOT: __ENVIRONMENT_OS_VERSION_MIN_REQUIRED__
// CHECK-6M: #define __MACH__
+// CHECK-6M: __STDC_NO_THREADS__
``````````
</details>
https://github.com/llvm/llvm-project/pull/122427
More information about the cfe-commits
mailing list