Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable/20240723][Darwin][Driver][clang] arm64-apple-none-macho is missing the Apple macros from arm-apple-none-macho #9819

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions clang/lib/Basic/Targets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -534,6 +540,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: {
Expand Down
23 changes: 20 additions & 3 deletions clang/lib/Basic/Targets/AArch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,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) {
Expand All @@ -1676,9 +1680,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__");
Expand All @@ -1691,7 +1695,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);
}

Expand Down
14 changes: 14 additions & 0 deletions clang/lib/Basic/Targets/AArch64.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,20 @@ 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:
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/Basic/Targets/ARM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,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) {
Expand Down
11 changes: 11 additions & 0 deletions clang/lib/Basic/Targets/ARM.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
29 changes: 20 additions & 9 deletions clang/lib/Basic/Targets/OSTargets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ 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");

Expand All @@ -39,7 +37,7 @@ void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
Builder.defineMacro("__ptrauth_abi_version__",
llvm::utostr(Opts.PointerAuthABIVersion));

// 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)))");
Expand All @@ -55,6 +53,22 @@ 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 Apple Mach-O linking/symbol visibility semantics".
// Notably libc++'s __configuration/platform.h and Swift's shims/Visibility.h
// take __MACH__ for the more general meaning.
if (Triple.isAppleMachO() || Triple.isOSDarwin())
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()) {
Expand Down Expand Up @@ -122,9 +136,6 @@ void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
assert(OsVersion.getMinor().value_or(0) < 100 &&
OsVersion.getSubminor().value_or(0) < 100 && "Invalid version!");
Builder.defineMacro("__ENVIRONMENT_OS_VERSION_MIN_REQUIRED__", Str);

// Tell users about the kernel if there is one.
Builder.defineMacro("__MACH__");
}

PlatformMinVersion = OsVersion;
Expand Down
38 changes: 30 additions & 8 deletions clang/lib/Basic/Targets/OSTargets.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/Basic/Targets/X86.h
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,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:
Expand Down
5 changes: 0 additions & 5 deletions clang/lib/Frontend/InitPreprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1498,11 +1498,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) {
Expand Down
83 changes: 83 additions & 0 deletions clang/test/Preprocessor/darwin-predefines.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// RUN: %clang_cc1 -E -dM -triple arm64-apple-macosx15.0.0 -target-cpu apple-m1 %s | FileCheck %s -check-prefix CHECK-MACOSX

// CHECK-MACOSX: #define __APPLE_CC__
// CHECK-MACOSX: #define __APPLE__
// CHECK-MACOSX: #define __ARM_64BIT_STATE 1
// CHECK-MACOSX-NOT: #define __ENVIRONMENT_DRIVERKIT_VERSION_MIN_REQUIRED__
// CHECK-MACOSX-NOT: #define __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
// CHECK-MACOSX: #define __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ 150000
// CHECK-MACOSX: #define __ENVIRONMENT_OS_VERSION_MIN_REQUIRED__ 150000
// CHECK-MACOSX-NOT: #define __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__
// CHECK-MACOSX-NOT: #define __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__
// CHECK-MACOSX: #define __MACH__
// CHECK-MACOSX: #define __STDC_NO_THREADS__

// RUN: %clang_cc1 -E -dM -triple arm64-apple-ios18.0.0 -target-cpu apple-a7 %s | FileCheck %s -check-prefix CHECK-IOS

// CHECK-IOS: #define __APPLE_CC__
// CHECK-IOS: #define __APPLE__
// CHECK-IOS: #define __ARM_64BIT_STATE 1
// CHECK-IOS-NOT: #define __ENVIRONMENT_DRIVERKIT_VERSION_MIN_REQUIRED__
// CHECK-IOS: #define __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ 180000
// CHECK-IOS-NOT: #define __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
// CHECK-IOS: #define __ENVIRONMENT_OS_VERSION_MIN_REQUIRED__ 180000
// CHECK-IOS-NOT: #define __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__
// CHECK-IOS-NOT: #define __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__
// CHECK-IOS: #define __MACH__
// CHECK-IOS: #define __STDC_NO_THREADS__

// RUN: %clang_cc1 -E -dM -triple arm64-apple-watchos11.0.0 -target-cpu apple-s4 %s | FileCheck %s -check-prefix CHECK-WATCHOS

// CHECK-WATCHOS: #define __APPLE_CC__
// CHECK-WATCHOS: #define __APPLE__
// CHECK-WATCHOS: #define __ARM_64BIT_STATE 1
// CHECK-WATCHOS-NOT: #define __ENVIRONMENT_DRIVERKIT_VERSION_MIN_REQUIRED__
// CHECK-WATCHOS-NOT: #define __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
// CHECK-WATCHOS-NOT: #define __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
// CHECK-WATCHOS: #define __ENVIRONMENT_OS_VERSION_MIN_REQUIRED__ 110000
// CHECK-WATCHOS-NOT: #define __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__
// CHECK-WATCHOS: #define __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ 110000
// CHECK-WATCHOS: #define __MACH__
// CHECK-WATCHOS: #define __STDC_NO_THREADS__

// RUN: %clang_cc1 -E -dM -triple arm64-apple-tvos18.0.0 -target-cpu apple-a7 %s | FileCheck %s -check-prefix CHECK-TVOS

// CHECK-TVOS: #define __APPLE_CC__
// CHECK-TVOS: #define __APPLE__
// CHECK-TVOS: #define __ARM_64BIT_STATE 1
// CHECK-TVOS-NOT: #define __ENVIRONMENT_DRIVERKIT_VERSION_MIN_REQUIRED__
// CHECK-TVOS-NOT: #define __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
// CHECK-TVOS-NOT: #define __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
// CHECK-TVOS: #define __ENVIRONMENT_OS_VERSION_MIN_REQUIRED__ 180000
// CHECK-TVOS: #define __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ 180000
// CHECK-TVOS-NOT: #define __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__
// CHECK-TVOS: #define __MACH__
// CHECK-TVOS: #define __STDC_NO_THREADS__

// RUN: %clang_cc1 -E -dM -triple arm64-apple-driverkit24.0.0 -target-cpu apple-a7 %s | FileCheck %s -check-prefix CHECK-DRIVERKIT

// CHECK-DRIVERKIT: #define __APPLE_CC__
// CHECK-DRIVERKIT: #define __APPLE__
// CHECK-DRIVERKIT: #define __ARM_64BIT_STATE 1
// CHECK-DRIVERKIT: #define __ENVIRONMENT_DRIVERKIT_VERSION_MIN_REQUIRED__ 240000
// CHECK-DRIVERKIT-NOT: #define __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
// CHECK-DRIVERKIT-NOT: #define __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
// CHECK-DRIVERKIT: #define __ENVIRONMENT_OS_VERSION_MIN_REQUIRED__ 240000
// CHECK-DRIVERKIT-NOT: #define __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__
// CHECK-DRIVERKIT-NOT: #define __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__
// CHECK-DRIVERKIT: #define __MACH__
// CHECK-DRIVERKIT: #define __STDC_NO_THREADS__

// RUN: %clang_cc1 -E -dM -triple arm64-apple-xros2.0.0 -target-cpu apple-a12 %s | FileCheck %s -check-prefix CHECK-XROS

// CHECK-XROS: #define __APPLE_CC__
// CHECK-XROS: #define __APPLE__
// CHECK-XROS: #define __ARM_64BIT_STATE 1
// CHECK-XROS-NOT: #define __ENVIRONMENT_DRIVERKIT_VERSION_MIN_REQUIRED__
// CHECK-XROS-NOT: #define __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
// CHECK-XROS-NOT: #define __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
// CHECK-XROS: #define __ENVIRONMENT_OS_VERSION_MIN_REQUIRED__ 20000
// CHECK-XROS-NOT: #define __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__
// CHECK-XROS-NOT: #define __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__
// CHECK-XROS: #define __MACH__
// CHECK-XROS: #define __STDC_NO_THREADS__
Loading