Skip to content

Commit 3b15873

Browse files
committed
[clang][PP] Add extension to predefine target OS macros (llvm#74676)
Add an extension feature `define-target-os-macros` that enables clang to provide definitions of common TARGET_OS_* conditional macros. The extension is enabled in the Darwin toolchain driver. (cherry picked from commit 6e1f191)
1 parent c3a1207 commit 3b15873

File tree

9 files changed

+327
-0
lines changed

9 files changed

+327
-0
lines changed

clang/include/clang/Basic/Features.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ FEATURE(blocks, LangOpts.Blocks)
9090
FEATURE(c_thread_safety_attributes, true)
9191
FEATURE(cxx_exceptions, LangOpts.CXXExceptions)
9292
FEATURE(cxx_rtti, LangOpts.RTTI &&LangOpts.RTTIData)
93+
EXTENSION(define_target_os_macros,
94+
PP.getPreprocessorOpts().DefineTargetOSMacros)
9395
FEATURE(enumerator_attributes, true)
9496
FEATURE(generalized_swift_name, true)
9597
FEATURE(nullability, true)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===--- TargetOSMacros.def - Target OS macros ------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file specifies the predefined TARGET_OS_* conditional macros.
10+
// A target macro `Name` should be defined if `Predicate` evaluates to true.
11+
// The macro expects `const llvm::Triple &Triple` and the class `llvm::Triple`
12+
// to be available for the predicate.
13+
//
14+
//===----------------------------------------------------------------------===//
15+
16+
#ifndef TARGET_OS
17+
#define TARGET_OS(Name, Predicate)
18+
#endif
19+
20+
// Windows targets.
21+
TARGET_OS(TARGET_OS_WIN32, Triple.isOSWindows())
22+
TARGET_OS(TARGET_OS_WINDOWS, Triple.isOSWindows())
23+
24+
// Linux target.
25+
TARGET_OS(TARGET_OS_LINUX, Triple.isOSLinux())
26+
27+
// Unix target.
28+
TARGET_OS(TARGET_OS_UNIX, Triple.isOSNetBSD() ||
29+
Triple.isOSFreeBSD() ||
30+
Triple.isOSOpenBSD() ||
31+
Triple.isOSSolaris())
32+
33+
// Apple (Mac) targets.
34+
TARGET_OS(TARGET_OS_MAC, Triple.isOSDarwin())
35+
TARGET_OS(TARGET_OS_OSX, Triple.isMacOSX())
36+
TARGET_OS(TARGET_OS_IPHONE, Triple.isiOS() || Triple.isTvOS() ||
37+
Triple.isWatchOS())
38+
// Triple::isiOS() also includes tvOS
39+
TARGET_OS(TARGET_OS_IOS, Triple.getOS() == llvm::Triple::IOS)
40+
TARGET_OS(TARGET_OS_TV, Triple.isTvOS())
41+
TARGET_OS(TARGET_OS_WATCH, Triple.isWatchOS())
42+
TARGET_OS(TARGET_OS_DRIVERKIT, Triple.isDriverKit())
43+
TARGET_OS(TARGET_OS_MACCATALYST, Triple.isMacCatalystEnvironment())
44+
TARGET_OS(TARGET_OS_SIMULATOR, Triple.isSimulatorEnvironment())
45+
46+
// Deprecated Apple target conditionals.
47+
TARGET_OS(TARGET_OS_EMBEDDED, (Triple.isiOS() || Triple.isTvOS() \
48+
|| Triple.isWatchOS()) \
49+
&& !Triple.isMacCatalystEnvironment() \
50+
&& !Triple.isSimulatorEnvironment())
51+
TARGET_OS(TARGET_OS_NANO, Triple.isWatchOS())
52+
TARGET_OS(TARGET_IPHONE_SIMULATOR, Triple.isSimulatorEnvironment())
53+
TARGET_OS(TARGET_OS_UIKITFORMAC, Triple.isMacCatalystEnvironment())
54+
55+
#undef TARGET_OS

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,9 @@ def fansi_escape_codes : Flag<["-"], "fansi-escape-codes">, Group<f_Group>,
15441544
def fcomment_block_commands : CommaJoined<["-"], "fcomment-block-commands=">, Group<f_clang_Group>, Flags<[CC1Option]>,
15451545
HelpText<"Treat each comma separated argument in <arg> as a documentation comment block command">,
15461546
MetaVarName<"<arg>">, MarshallingInfoStringVector<LangOpts<"CommentOpts.BlockCommandNames">>;
1547+
defm define_target_os_macros : OptInCC1FFlag<"define-target-os-macros",
1548+
"Enable", "Disable", " predefined target OS macros",
1549+
[CoreOption, CC1Option]>;
15471550
def fparse_all_comments : Flag<["-"], "fparse-all-comments">, Group<f_clang_Group>, Flags<[CC1Option]>,
15481551
MarshallingInfoFlag<LangOpts<"CommentOpts.ParseAllComments">>;
15491552
def frecord_command_line : Flag<["-"], "frecord-command-line">,

clang/include/clang/Lex/PreprocessorOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ class PreprocessorOptions {
8888
/// predefines.
8989
bool UsePredefines = true;
9090

91+
/// Indicates whether to predefine target OS macros.
92+
bool DefineTargetOSMacros = false;
93+
9194
/// Whether we should maintain a detailed record of all macro
9295
/// definitions and expansions.
9396
bool DetailedRecord = false;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,9 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
14051405
CmdArgs.push_back("-source-date-epoch");
14061406
CmdArgs.push_back(Args.MakeArgString(Epoch));
14071407
}
1408+
1409+
Args.addOptInFlag(CmdArgs, options::OPT_fdefine_target_os_macros,
1410+
options::OPT_fno_define_target_os_macros);
14081411
}
14091412

14101413
// FIXME: Move to target hook.

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3078,6 +3078,10 @@ void Darwin::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
30783078
// to fix the same problem with C++ headers, and is generally fragile.
30793079
if (!sdkSupportsBuiltinModules(TargetPlatform, SDKInfo))
30803080
CC1Args.push_back("-fbuiltin-headers-in-system-modules");
3081+
3082+
if (!DriverArgs.hasArgNoClaim(options::OPT_fdefine_target_os_macros,
3083+
options::OPT_fno_define_target_os_macros))
3084+
CC1Args.push_back("-fdefine-target-os-macros");
30813085
}
30823086

30833087
void Darwin::addClangCC1ASTargetOptions(

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4706,6 +4706,9 @@ static void GeneratePreprocessorArgs(const PreprocessorOptions &Opts,
47064706
if (Opts.SourceDateEpoch)
47074707
GenerateArg(Consumer, OPT_source_date_epoch, Twine(*Opts.SourceDateEpoch));
47084708

4709+
if (Opts.DefineTargetOSMacros)
4710+
GenerateArg(Consumer, OPT_fdefine_target_os_macros);
4711+
47094712
// Don't handle LexEditorPlaceholders. It is implied by the action that is
47104713
// generated elsewhere.
47114714
}
@@ -4804,6 +4807,10 @@ static bool ParsePreprocessorArgs(PreprocessorOptions &Opts, ArgList &Args,
48044807
if (isStrictlyPreprocessorAction(Action))
48054808
Opts.LexEditorPlaceholders = false;
48064809

4810+
Opts.DefineTargetOSMacros =
4811+
Args.hasFlag(OPT_fdefine_target_os_macros,
4812+
OPT_fno_define_target_os_macros, Opts.DefineTargetOSMacros);
4813+
48074814
return Diags.getNumErrors() == NumErrorsBefore;
48084815
}
48094816

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,15 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
13291329
if (TI.getTriple().isOSBinFormatELF())
13301330
Builder.defineMacro("__ELF__");
13311331

1332+
// Target OS macro definitions.
1333+
if (PPOpts.DefineTargetOSMacros) {
1334+
const llvm::Triple &Triple = TI.getTriple();
1335+
#define TARGET_OS(Name, Predicate) \
1336+
Builder.defineMacro(#Name, (Predicate) ? "1" : "0");
1337+
#include "clang/Basic/TargetOSMacros.def"
1338+
#undef TARGET_OS
1339+
}
1340+
13321341
// Get other target #defines.
13331342
TI.getTargetDefines(LangOpts, Builder);
13341343
}
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
// RUN: %clang -### --target=arm64-apple-darwin %s 2>&1 | FileCheck %s --check-prefix=DARWIN-DEFAULT
2+
// DARWIN-DEFAULT: "-fdefine-target-os-macros"
3+
4+
// RUN: %clang -### --target=arm-none-linux-gnu %s 2>&1 | FileCheck %s --check-prefix=NON-DARWIN-DEFAULT
5+
// RUN: %clang -### --target=x86_64-pc-win32 %s 2>&1 | FileCheck %s --check-prefix=NON-DARWIN-DEFAULT
6+
// NON-DARWIN-DEFAULT-NOT: "-fdefine-target-os-macros"
7+
8+
// RUN: %clang -dM -E --target=arm64-apple-macos %s 2>&1 \
9+
// RUN: | FileCheck %s -DMAC=1 \
10+
// RUN: -DOSX=1 \
11+
// RUN: -DIPHONE=0 \
12+
// RUN: -DIOS=0 \
13+
// RUN: -DTV=0 \
14+
// RUN: -DWATCH=0 \
15+
// RUN: -DDRIVERKIT=0 \
16+
// RUN: -DMACCATALYST=0 \
17+
// RUN: -DEMBEDDED=0 \
18+
// RUN: -DSIMULATOR=0 \
19+
// RUN: -DWINDOWS=0 \
20+
// RUN: -DLINUX=0 \
21+
// RUN: -DUNIX=0
22+
23+
// RUN: %clang -dM -E --target=arm64-apple-ios %s 2>&1 \
24+
// RUN: | FileCheck %s -DMAC=1 \
25+
// RUN: -DOSX=0 \
26+
// RUN: -DIPHONE=1 \
27+
// RUN: -DIOS=1 \
28+
// RUN: -DTV=0 \
29+
// RUN: -DWATCH=0 \
30+
// RUN: -DDRIVERKIT=0 \
31+
// RUN: -DMACCATALYST=0 \
32+
// RUN: -DEMBEDDED=1 \
33+
// RUN: -DSIMULATOR=0 \
34+
// RUN: -DWINDOWS=0 \
35+
// RUN: -DLINUX=0 \
36+
// RUN: -DUNIX=0
37+
38+
// RUN: %clang -dM -E --target=arm64-apple-ios-macabi %s 2>&1 \
39+
// RUN: | FileCheck %s -DMAC=1 \
40+
// RUN: -DOSX=0 \
41+
// RUN: -DIPHONE=1 \
42+
// RUN: -DIOS=1 \
43+
// RUN: -DTV=0 \
44+
// RUN: -DWATCH=0 \
45+
// RUN: -DDRIVERKIT=0 \
46+
// RUN: -DMACCATALYST=1 \
47+
// RUN: -DEMBEDDED=0 \
48+
// RUN: -DSIMULATOR=0 \
49+
// RUN: -DWINDOWS=0 \
50+
// RUN: -DLINUX=0 \
51+
// RUN: -DUNIX=0
52+
53+
// RUN: %clang -dM -E --target=arm64-apple-ios-simulator %s 2>&1 \
54+
// RUN: | FileCheck %s -DMAC=1 \
55+
// RUN: -DOSX=0 \
56+
// RUN: -DIPHONE=1 \
57+
// RUN: -DIOS=1 \
58+
// RUN: -DTV=0 \
59+
// RUN: -DWATCH=0 \
60+
// RUN: -DDRIVERKIT=0 \
61+
// RUN: -DMACCATALYST=0 \
62+
// RUN: -DEMBEDDED=0 \
63+
// RUN: -DSIMULATOR=1 \
64+
// RUN: -DWINDOWS=0 \
65+
// RUN: -DLINUX=0 \
66+
// RUN: -DUNIX=0
67+
68+
// RUN: %clang -dM -E --target=arm64-apple-tvos %s 2>&1 \
69+
// RUN: | FileCheck %s -DMAC=1 \
70+
// RUN: -DOSX=0 \
71+
// RUN: -DIPHONE=1 \
72+
// RUN: -DIOS=0 \
73+
// RUN: -DTV=1 \
74+
// RUN: -DWATCH=0 \
75+
// RUN: -DDRIVERKIT=0 \
76+
// RUN: -DMACCATALYST=0 \
77+
// RUN: -DEMBEDDED=1 \
78+
// RUN: -DSIMULATOR=0 \
79+
// RUN: -DWINDOWS=0 \
80+
// RUN: -DLINUX=0 \
81+
// RUN: -DUNIX=0
82+
83+
// RUN: %clang -dM -E --target=arm64-apple-tvos-simulator %s 2>&1 \
84+
// RUN: | FileCheck %s -DMAC=1 \
85+
// RUN: -DOSX=0 \
86+
// RUN: -DIPHONE=1 \
87+
// RUN: -DIOS=0 \
88+
// RUN: -DTV=1 \
89+
// RUN: -DWATCH=0 \
90+
// RUN: -DDRIVERKIT=0 \
91+
// RUN: -DMACCATALYST=0 \
92+
// RUN: -DEMBEDDED=0 \
93+
// RUN: -DSIMULATOR=1 \
94+
// RUN: -DWINDOWS=0 \
95+
// RUN: -DLINUX=0 \
96+
// RUN: -DUNIX=0
97+
98+
// RUN: %clang -dM -E --target=arm64-apple-watchos %s 2>&1 \
99+
// RUN: | FileCheck %s -DMAC=1 \
100+
// RUN: -DOSX=0 \
101+
// RUN: -DIPHONE=1 \
102+
// RUN: -DIOS=0 \
103+
// RUN: -DTV=0 \
104+
// RUN: -DWATCH=1 \
105+
// RUN: -DDRIVERKIT=0 \
106+
// RUN: -DMACCATALYST=0 \
107+
// RUN: -DEMBEDDED=1 \
108+
// RUN: -DSIMULATOR=0 \
109+
// RUN: -DWINDOWS=0 \
110+
// RUN: -DLINUX=0 \
111+
// RUN: -DUNIX=0
112+
113+
// RUN: %clang -dM -E --target=arm64-apple-watchos-simulator %s 2>&1 \
114+
// RUN: | FileCheck %s -DMAC=1 \
115+
// RUN: -DOSX=0 \
116+
// RUN: -DIPHONE=1 \
117+
// RUN: -DIOS=0 \
118+
// RUN: -DTV=0 \
119+
// RUN: -DWATCH=1 \
120+
// RUN: -DDRIVERKIT=0 \
121+
// RUN: -DMACCATALYST=0 \
122+
// RUN: -DEMBEDDED=0 \
123+
// RUN: -DSIMULATOR=1 \
124+
// RUN: -DWINDOWS=0 \
125+
// RUN: -DLINUX=0 \
126+
// RUN: -DUNIX=0
127+
128+
// RUN: %clang -dM -E --target=arm64-apple-driverkit %s 2>&1 \
129+
// RUN: | FileCheck %s -DMAC=1 \
130+
// RUN: -DOSX=0 \
131+
// RUN: -DIPHONE=0 \
132+
// RUN: -DIOS=0 \
133+
// RUN: -DTV=0 \
134+
// RUN: -DWATCH=0 \
135+
// RUN: -DDRIVERKIT=1 \
136+
// RUN: -DMACCATALYST=0 \
137+
// RUN: -DEMBEDDED=0 \
138+
// RUN: -DSIMULATOR=0 \
139+
// RUN: -DWINDOWS=0 \
140+
// RUN: -DLINUX=0 \
141+
// RUN: -DUNIX=0
142+
143+
// RUN: %clang -dM -E --target=x86_64-pc-linux-gnu \
144+
// RUN: -fdefine-target-os-macros %s 2>&1 \
145+
// RUN: | FileCheck %s -DMAC=0 \
146+
// RUN: -DOSX=0 \
147+
// RUN: -DIPHONE=0 \
148+
// RUN: -DIOS=0 \
149+
// RUN: -DTV=0 \
150+
// RUN: -DWATCH=0 \
151+
// RUN: -DDRIVERKIT=0 \
152+
// RUN: -DMACCATALYST=0 \
153+
// RUN: -DEMBEDDED=0 \
154+
// RUN: -DSIMULATOR=0 \
155+
// RUN: -DWINDOWS=0 \
156+
// RUN: -DLINUX=1 \
157+
// RUN: -DUNIX=0
158+
159+
// RUN: %clang -dM -E --target=x86_64-pc-win32 \
160+
// RUN: -fdefine-target-os-macros %s 2>&1 \
161+
// RUN: | FileCheck %s -DMAC=0 \
162+
// RUN: -DOSX=0 \
163+
// RUN: -DIPHONE=0 \
164+
// RUN: -DIOS=0 \
165+
// RUN: -DTV=0 \
166+
// RUN: -DWATCH=0 \
167+
// RUN: -DDRIVERKIT=0 \
168+
// RUN: -DMACCATALYST=0 \
169+
// RUN: -DEMBEDDED=0 \
170+
// RUN: -DSIMULATOR=0 \
171+
// RUN: -DWINDOWS=1 \
172+
// RUN: -DLINUX=0 \
173+
// RUN: -DUNIX=0
174+
175+
// RUN: %clang -dM -E --target=x86_64-pc-windows-gnu \
176+
// RUN: -fdefine-target-os-macros %s 2>&1 \
177+
// RUN: | FileCheck %s -DMAC=0 \
178+
// RUN: -DOSX=0 \
179+
// RUN: -DIPHONE=0 \
180+
// RUN: -DIOS=0 \
181+
// RUN: -DTV=0 \
182+
// RUN: -DWATCH=0 \
183+
// RUN: -DDRIVERKIT=0 \
184+
// RUN: -DMACCATALYST=0 \
185+
// RUN: -DEMBEDDED=0 \
186+
// RUN: -DSIMULATOR=0 \
187+
// RUN: -DWINDOWS=1 \
188+
// RUN: -DLINUX=0 \
189+
// RUN: -DUNIX=0
190+
191+
// RUN: %clang -dM -E --target=sparc-none-solaris \
192+
// RUN: -fdefine-target-os-macros %s 2>&1 \
193+
// RUN: | FileCheck %s -DMAC=0 \
194+
// RUN: -DOSX=0 \
195+
// RUN: -DIPHONE=0 \
196+
// RUN: -DIOS=0 \
197+
// RUN: -DTV=0 \
198+
// RUN: -DWATCH=0 \
199+
// RUN: -DDRIVERKIT=0 \
200+
// RUN: -DMACCATALYST=0 \
201+
// RUN: -DEMBEDDED=0 \
202+
// RUN: -DSIMULATOR=0 \
203+
// RUN: -DWINDOWS=0 \
204+
// RUN: -DLINUX=0 \
205+
// RUN: -DUNIX=1
206+
207+
// RUN: %clang -dM -E --target=arm64-apple-macos \
208+
// RUN: -fno-define-target-os-macros %s 2>&1 \
209+
// RUN: | FileCheck %s --check-prefix=NEG
210+
211+
// RUN: %clang -dM -E --target=arm64-apple-macos \
212+
// RUN: -fdefine-target-os-macros \
213+
// RUN: -fno-define-target-os-macros %s 2>&1 \
214+
// RUN: | FileCheck %s --check-prefix=NEG
215+
216+
// RUN: %clang -dM -E --target=x86_64-pc-windows \
217+
// RUN: -fdefine-target-os-macros \
218+
// RUN: -fno-define-target-os-macros %s 2>&1 \
219+
// RUN: | FileCheck %s --check-prefix=NEG
220+
221+
// NEG-NOT: #define TARGET_OS_
222+
223+
// CHECK-DAG: #define TARGET_OS_MAC [[MAC]]
224+
// CHECK-DAG: #define TARGET_OS_OSX [[OSX]]
225+
// CHECK-DAG: #define TARGET_OS_IPHONE [[IPHONE]]
226+
// CHECK-DAG: #define TARGET_OS_IOS [[IOS]]
227+
// CHECK-DAG: #define TARGET_OS_TV [[TV]]
228+
// CHECK-DAG: #define TARGET_OS_WATCH [[WATCH]]
229+
// CHECK-DAG: #define TARGET_OS_DRIVERKIT [[DRIVERKIT]]
230+
// CHECK-DAG: #define TARGET_OS_MACCATALYST [[MACCATALYST]]
231+
// CHECK-DAG: #define TARGET_OS_SIMULATOR [[SIMULATOR]]
232+
// Deprecated
233+
// CHECK-DAG: #define TARGET_OS_EMBEDDED [[EMBEDDED]]
234+
// CHECK-DAG: #define TARGET_OS_NANO [[WATCH]]
235+
// CHECK-DAG: #define TARGET_IPHONE_SIMULATOR [[SIMULATOR]]
236+
// CHECK-DAG: #define TARGET_OS_UIKITFORMAC [[MACCATALYST]]
237+
// Non-darwin OSes
238+
// CHECK-DAG: #define TARGET_OS_WIN32 [[WINDOWS]]
239+
// CHECK-DAG: #define TARGET_OS_WINDOWS [[WINDOWS]]
240+
// CHECK-DAG: #define TARGET_OS_LINUX [[LINUX]]
241+
// CHECK-DAG: #define TARGET_OS_UNIX [[UNIX]]

0 commit comments

Comments
 (0)