1 | //===--- InitPreprocessor.cpp - PP initialization code. ---------*- 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 implements the clang::InitializePreprocessor function. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "clang/Basic/FileManager.h" |
14 | #include "clang/Basic/HLSLRuntime.h" |
15 | #include "clang/Basic/MacroBuilder.h" |
16 | #include "clang/Basic/SourceManager.h" |
17 | #include "clang/Basic/SyncScope.h" |
18 | #include "clang/Basic/TargetInfo.h" |
19 | #include "clang/Basic/Version.h" |
20 | #include "clang/Frontend/FrontendDiagnostic.h" |
21 | #include "clang/Frontend/FrontendOptions.h" |
22 | #include "clang/Frontend/Utils.h" |
23 | #include "clang/Lex/HeaderSearch.h" |
24 | #include "clang/Lex/Preprocessor.h" |
25 | #include "clang/Lex/PreprocessorOptions.h" |
26 | #include "clang/Serialization/ASTReader.h" |
27 | #include "llvm/ADT/APFloat.h" |
28 | #include "llvm/IR/DataLayout.h" |
29 | #include "llvm/IR/DerivedTypes.h" |
30 | using namespace clang; |
31 | |
32 | static bool MacroBodyEndsInBackslash(StringRef MacroBody) { |
33 | while (!MacroBody.empty() && isWhitespace(c: MacroBody.back())) |
34 | MacroBody = MacroBody.drop_back(); |
35 | return !MacroBody.empty() && MacroBody.back() == '\\'; |
36 | } |
37 | |
38 | // Append a #define line to Buf for Macro. Macro should be of the form XXX, |
39 | // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit |
40 | // "#define XXX Y z W". To get a #define with no value, use "XXX=". |
41 | static void DefineBuiltinMacro(MacroBuilder &Builder, StringRef Macro, |
42 | DiagnosticsEngine &Diags) { |
43 | std::pair<StringRef, StringRef> MacroPair = Macro.split(Separator: '='); |
44 | StringRef MacroName = MacroPair.first; |
45 | StringRef MacroBody = MacroPair.second; |
46 | if (MacroName.size() != Macro.size()) { |
47 | // Per GCC -D semantics, the macro ends at \n if it exists. |
48 | StringRef::size_type End = MacroBody.find_first_of(Chars: "\n\r" ); |
49 | if (End != StringRef::npos) |
50 | Diags.Report(diag::warn_fe_macro_contains_embedded_newline) |
51 | << MacroName; |
52 | MacroBody = MacroBody.substr(Start: 0, N: End); |
53 | // We handle macro bodies which end in a backslash by appending an extra |
54 | // backslash+newline. This makes sure we don't accidentally treat the |
55 | // backslash as a line continuation marker. |
56 | if (MacroBodyEndsInBackslash(MacroBody)) |
57 | Builder.defineMacro(Name: MacroName, Value: Twine(MacroBody) + "\\\n" ); |
58 | else |
59 | Builder.defineMacro(Name: MacroName, Value: MacroBody); |
60 | } else { |
61 | // Push "macroname 1". |
62 | Builder.defineMacro(Name: Macro); |
63 | } |
64 | } |
65 | |
66 | /// AddImplicitInclude - Add an implicit \#include of the specified file to the |
67 | /// predefines buffer. |
68 | /// As these includes are generated by -include arguments the header search |
69 | /// logic is going to search relatively to the current working directory. |
70 | static void AddImplicitInclude(MacroBuilder &Builder, StringRef File) { |
71 | Builder.append(Str: Twine("#include \"" ) + File + "\"" ); |
72 | } |
73 | |
74 | static void AddImplicitIncludeMacros(MacroBuilder &Builder, StringRef File) { |
75 | Builder.append(Str: Twine("#__include_macros \"" ) + File + "\"" ); |
76 | // Marker token to stop the __include_macros fetch loop. |
77 | Builder.append(Str: "##" ); // ##? |
78 | } |
79 | |
80 | /// Add an implicit \#include using the original file used to generate |
81 | /// a PCH file. |
82 | static void AddImplicitIncludePCH(MacroBuilder &Builder, Preprocessor &PP, |
83 | const PCHContainerReader &PCHContainerRdr, |
84 | StringRef ImplicitIncludePCH) { |
85 | std::string OriginalFile = ASTReader::getOriginalSourceFile( |
86 | ASTFileName: std::string(ImplicitIncludePCH), FileMgr&: PP.getFileManager(), PCHContainerRdr, |
87 | Diags&: PP.getDiagnostics()); |
88 | if (OriginalFile.empty()) |
89 | return; |
90 | |
91 | AddImplicitInclude(Builder, File: OriginalFile); |
92 | } |
93 | |
94 | /// PickFP - This is used to pick a value based on the FP semantics of the |
95 | /// specified FP model. |
96 | template <typename T> |
97 | static T PickFP(const llvm::fltSemantics *Sem, T IEEEHalfVal, T IEEESingleVal, |
98 | T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal, |
99 | T IEEEQuadVal) { |
100 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEhalf()) |
101 | return IEEEHalfVal; |
102 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle()) |
103 | return IEEESingleVal; |
104 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble()) |
105 | return IEEEDoubleVal; |
106 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended()) |
107 | return X87DoubleExtendedVal; |
108 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble()) |
109 | return PPCDoubleDoubleVal; |
110 | assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad()); |
111 | return IEEEQuadVal; |
112 | } |
113 | |
114 | static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix, |
115 | const llvm::fltSemantics *Sem, StringRef Ext) { |
116 | const char *DenormMin, *Epsilon, *Max, *Min; |
117 | DenormMin = PickFP(Sem, IEEEHalfVal: "5.9604644775390625e-8" , IEEESingleVal: "1.40129846e-45" , |
118 | IEEEDoubleVal: "4.9406564584124654e-324" , X87DoubleExtendedVal: "3.64519953188247460253e-4951" , |
119 | PPCDoubleDoubleVal: "4.94065645841246544176568792868221e-324" , |
120 | IEEEQuadVal: "6.47517511943802511092443895822764655e-4966" ); |
121 | int Digits = PickFP(Sem, IEEEHalfVal: 3, IEEESingleVal: 6, IEEEDoubleVal: 15, X87DoubleExtendedVal: 18, PPCDoubleDoubleVal: 31, IEEEQuadVal: 33); |
122 | int DecimalDigits = PickFP(Sem, IEEEHalfVal: 5, IEEESingleVal: 9, IEEEDoubleVal: 17, X87DoubleExtendedVal: 21, PPCDoubleDoubleVal: 33, IEEEQuadVal: 36); |
123 | Epsilon = PickFP(Sem, IEEEHalfVal: "9.765625e-4" , IEEESingleVal: "1.19209290e-7" , |
124 | IEEEDoubleVal: "2.2204460492503131e-16" , X87DoubleExtendedVal: "1.08420217248550443401e-19" , |
125 | PPCDoubleDoubleVal: "4.94065645841246544176568792868221e-324" , |
126 | IEEEQuadVal: "1.92592994438723585305597794258492732e-34" ); |
127 | int MantissaDigits = PickFP(Sem, IEEEHalfVal: 11, IEEESingleVal: 24, IEEEDoubleVal: 53, X87DoubleExtendedVal: 64, PPCDoubleDoubleVal: 106, IEEEQuadVal: 113); |
128 | int Min10Exp = PickFP(Sem, IEEEHalfVal: -4, IEEESingleVal: -37, IEEEDoubleVal: -307, X87DoubleExtendedVal: -4931, PPCDoubleDoubleVal: -291, IEEEQuadVal: -4931); |
129 | int Max10Exp = PickFP(Sem, IEEEHalfVal: 4, IEEESingleVal: 38, IEEEDoubleVal: 308, X87DoubleExtendedVal: 4932, PPCDoubleDoubleVal: 308, IEEEQuadVal: 4932); |
130 | int MinExp = PickFP(Sem, IEEEHalfVal: -13, IEEESingleVal: -125, IEEEDoubleVal: -1021, X87DoubleExtendedVal: -16381, PPCDoubleDoubleVal: -968, IEEEQuadVal: -16381); |
131 | int MaxExp = PickFP(Sem, IEEEHalfVal: 16, IEEESingleVal: 128, IEEEDoubleVal: 1024, X87DoubleExtendedVal: 16384, PPCDoubleDoubleVal: 1024, IEEEQuadVal: 16384); |
132 | Min = PickFP(Sem, IEEEHalfVal: "6.103515625e-5" , IEEESingleVal: "1.17549435e-38" , IEEEDoubleVal: "2.2250738585072014e-308" , |
133 | X87DoubleExtendedVal: "3.36210314311209350626e-4932" , |
134 | PPCDoubleDoubleVal: "2.00416836000897277799610805135016e-292" , |
135 | IEEEQuadVal: "3.36210314311209350626267781732175260e-4932" ); |
136 | Max = PickFP(Sem, IEEEHalfVal: "6.5504e+4" , IEEESingleVal: "3.40282347e+38" , IEEEDoubleVal: "1.7976931348623157e+308" , |
137 | X87DoubleExtendedVal: "1.18973149535723176502e+4932" , |
138 | PPCDoubleDoubleVal: "1.79769313486231580793728971405301e+308" , |
139 | IEEEQuadVal: "1.18973149535723176508575932662800702e+4932" ); |
140 | |
141 | SmallString<32> DefPrefix; |
142 | DefPrefix = "__" ; |
143 | DefPrefix += Prefix; |
144 | DefPrefix += "_" ; |
145 | |
146 | Builder.defineMacro(Name: DefPrefix + "DENORM_MIN__" , Value: Twine(DenormMin)+Ext); |
147 | Builder.defineMacro(Name: DefPrefix + "HAS_DENORM__" ); |
148 | Builder.defineMacro(Name: DefPrefix + "DIG__" , Value: Twine(Digits)); |
149 | Builder.defineMacro(Name: DefPrefix + "DECIMAL_DIG__" , Value: Twine(DecimalDigits)); |
150 | Builder.defineMacro(Name: DefPrefix + "EPSILON__" , Value: Twine(Epsilon)+Ext); |
151 | Builder.defineMacro(Name: DefPrefix + "HAS_INFINITY__" ); |
152 | Builder.defineMacro(Name: DefPrefix + "HAS_QUIET_NAN__" ); |
153 | Builder.defineMacro(Name: DefPrefix + "MANT_DIG__" , Value: Twine(MantissaDigits)); |
154 | |
155 | Builder.defineMacro(Name: DefPrefix + "MAX_10_EXP__" , Value: Twine(Max10Exp)); |
156 | Builder.defineMacro(Name: DefPrefix + "MAX_EXP__" , Value: Twine(MaxExp)); |
157 | Builder.defineMacro(Name: DefPrefix + "MAX__" , Value: Twine(Max)+Ext); |
158 | |
159 | Builder.defineMacro(Name: DefPrefix + "MIN_10_EXP__" ,Value: "(" +Twine(Min10Exp)+")" ); |
160 | Builder.defineMacro(Name: DefPrefix + "MIN_EXP__" , Value: "(" +Twine(MinExp)+")" ); |
161 | Builder.defineMacro(Name: DefPrefix + "MIN__" , Value: Twine(Min)+Ext); |
162 | } |
163 | |
164 | |
165 | /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro |
166 | /// named MacroName with the max value for a type with width 'TypeWidth' a |
167 | /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL). |
168 | static void DefineTypeSize(const Twine &MacroName, unsigned TypeWidth, |
169 | StringRef ValSuffix, bool isSigned, |
170 | MacroBuilder &Builder) { |
171 | llvm::APInt MaxVal = isSigned ? llvm::APInt::getSignedMaxValue(numBits: TypeWidth) |
172 | : llvm::APInt::getMaxValue(numBits: TypeWidth); |
173 | Builder.defineMacro(Name: MacroName, Value: toString(I: MaxVal, Radix: 10, Signed: isSigned) + ValSuffix); |
174 | } |
175 | |
176 | /// DefineTypeSize - An overloaded helper that uses TargetInfo to determine |
177 | /// the width, suffix, and signedness of the given type |
178 | static void DefineTypeSize(const Twine &MacroName, TargetInfo::IntType Ty, |
179 | const TargetInfo &TI, MacroBuilder &Builder) { |
180 | DefineTypeSize(MacroName, TypeWidth: TI.getTypeWidth(T: Ty), ValSuffix: TI.getTypeConstantSuffix(T: Ty), |
181 | isSigned: TI.isTypeSigned(T: Ty), Builder); |
182 | } |
183 | |
184 | static void DefineFmt(const LangOptions &LangOpts, const Twine &Prefix, |
185 | TargetInfo::IntType Ty, const TargetInfo &TI, |
186 | MacroBuilder &Builder) { |
187 | StringRef FmtModifier = TI.getTypeFormatModifier(T: Ty); |
188 | auto Emitter = [&](char Fmt) { |
189 | Builder.defineMacro(Name: Prefix + "_FMT" + Twine(Fmt) + "__" , |
190 | Value: Twine("\"" ) + FmtModifier + Twine(Fmt) + "\"" ); |
191 | }; |
192 | bool IsSigned = TI.isTypeSigned(T: Ty); |
193 | llvm::for_each(Range: StringRef(IsSigned ? "di" : "ouxX" ), F: Emitter); |
194 | |
195 | // C23 added the b and B modifiers for printing binary output of unsigned |
196 | // integers. Conditionally define those if compiling in C23 mode. |
197 | if (LangOpts.C23 && !IsSigned) |
198 | llvm::for_each(Range: StringRef("bB" ), F: Emitter); |
199 | } |
200 | |
201 | static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty, |
202 | MacroBuilder &Builder) { |
203 | Builder.defineMacro(Name: MacroName, Value: TargetInfo::getTypeName(T: Ty)); |
204 | } |
205 | |
206 | static void DefineTypeWidth(const Twine &MacroName, TargetInfo::IntType Ty, |
207 | const TargetInfo &TI, MacroBuilder &Builder) { |
208 | Builder.defineMacro(Name: MacroName, Value: Twine(TI.getTypeWidth(T: Ty))); |
209 | } |
210 | |
211 | static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth, |
212 | const TargetInfo &TI, MacroBuilder &Builder) { |
213 | Builder.defineMacro(Name: MacroName, |
214 | Value: Twine(BitWidth / TI.getCharWidth())); |
215 | } |
216 | |
217 | // This will generate a macro based on the prefix with `_MAX__` as the suffix |
218 | // for the max value representable for the type, and a macro with a `_WIDTH__` |
219 | // suffix for the width of the type. |
220 | static void DefineTypeSizeAndWidth(const Twine &Prefix, TargetInfo::IntType Ty, |
221 | const TargetInfo &TI, |
222 | MacroBuilder &Builder) { |
223 | DefineTypeSize(MacroName: Prefix + "_MAX__" , Ty, TI, Builder); |
224 | DefineTypeWidth(MacroName: Prefix + "_WIDTH__" , Ty, TI, Builder); |
225 | } |
226 | |
227 | static void DefineExactWidthIntType(const LangOptions &LangOpts, |
228 | TargetInfo::IntType Ty, |
229 | const TargetInfo &TI, |
230 | MacroBuilder &Builder) { |
231 | int TypeWidth = TI.getTypeWidth(T: Ty); |
232 | bool IsSigned = TI.isTypeSigned(T: Ty); |
233 | |
234 | // Use the target specified int64 type, when appropriate, so that [u]int64_t |
235 | // ends up being defined in terms of the correct type. |
236 | if (TypeWidth == 64) |
237 | Ty = IsSigned ? TI.getInt64Type() : TI.getUInt64Type(); |
238 | |
239 | // Use the target specified int16 type when appropriate. Some MCU targets |
240 | // (such as AVR) have definition of [u]int16_t to [un]signed int. |
241 | if (TypeWidth == 16) |
242 | Ty = IsSigned ? TI.getInt16Type() : TI.getUInt16Type(); |
243 | |
244 | const char *Prefix = IsSigned ? "__INT" : "__UINT" ; |
245 | |
246 | DefineType(MacroName: Prefix + Twine(TypeWidth) + "_TYPE__" , Ty, Builder); |
247 | DefineFmt(LangOpts, Prefix: Prefix + Twine(TypeWidth), Ty, TI, Builder); |
248 | |
249 | StringRef ConstSuffix(TI.getTypeConstantSuffix(T: Ty)); |
250 | Builder.defineMacro(Name: Prefix + Twine(TypeWidth) + "_C_SUFFIX__" , Value: ConstSuffix); |
251 | } |
252 | |
253 | static void DefineExactWidthIntTypeSize(TargetInfo::IntType Ty, |
254 | const TargetInfo &TI, |
255 | MacroBuilder &Builder) { |
256 | int TypeWidth = TI.getTypeWidth(T: Ty); |
257 | bool IsSigned = TI.isTypeSigned(T: Ty); |
258 | |
259 | // Use the target specified int64 type, when appropriate, so that [u]int64_t |
260 | // ends up being defined in terms of the correct type. |
261 | if (TypeWidth == 64) |
262 | Ty = IsSigned ? TI.getInt64Type() : TI.getUInt64Type(); |
263 | |
264 | // We don't need to define a _WIDTH macro for the exact-width types because |
265 | // we already know the width. |
266 | const char *Prefix = IsSigned ? "__INT" : "__UINT" ; |
267 | DefineTypeSize(MacroName: Prefix + Twine(TypeWidth) + "_MAX__" , Ty, TI, Builder); |
268 | } |
269 | |
270 | static void DefineLeastWidthIntType(const LangOptions &LangOpts, |
271 | unsigned TypeWidth, bool IsSigned, |
272 | const TargetInfo &TI, |
273 | MacroBuilder &Builder) { |
274 | TargetInfo::IntType Ty = TI.getLeastIntTypeByWidth(BitWidth: TypeWidth, IsSigned); |
275 | if (Ty == TargetInfo::NoInt) |
276 | return; |
277 | |
278 | const char *Prefix = IsSigned ? "__INT_LEAST" : "__UINT_LEAST" ; |
279 | DefineType(MacroName: Prefix + Twine(TypeWidth) + "_TYPE__" , Ty, Builder); |
280 | // We only want the *_WIDTH macro for the signed types to avoid too many |
281 | // predefined macros (the unsigned width and the signed width are identical.) |
282 | if (IsSigned) |
283 | DefineTypeSizeAndWidth(Prefix: Prefix + Twine(TypeWidth), Ty, TI, Builder); |
284 | else |
285 | DefineTypeSize(MacroName: Prefix + Twine(TypeWidth) + "_MAX__" , Ty, TI, Builder); |
286 | DefineFmt(LangOpts, Prefix: Prefix + Twine(TypeWidth), Ty, TI, Builder); |
287 | } |
288 | |
289 | static void DefineFastIntType(const LangOptions &LangOpts, unsigned TypeWidth, |
290 | bool IsSigned, const TargetInfo &TI, |
291 | MacroBuilder &Builder) { |
292 | // stdint.h currently defines the fast int types as equivalent to the least |
293 | // types. |
294 | TargetInfo::IntType Ty = TI.getLeastIntTypeByWidth(BitWidth: TypeWidth, IsSigned); |
295 | if (Ty == TargetInfo::NoInt) |
296 | return; |
297 | |
298 | const char *Prefix = IsSigned ? "__INT_FAST" : "__UINT_FAST" ; |
299 | DefineType(MacroName: Prefix + Twine(TypeWidth) + "_TYPE__" , Ty, Builder); |
300 | // We only want the *_WIDTH macro for the signed types to avoid too many |
301 | // predefined macros (the unsigned width and the signed width are identical.) |
302 | if (IsSigned) |
303 | DefineTypeSizeAndWidth(Prefix: Prefix + Twine(TypeWidth), Ty, TI, Builder); |
304 | else |
305 | DefineTypeSize(MacroName: Prefix + Twine(TypeWidth) + "_MAX__" , Ty, TI, Builder); |
306 | DefineFmt(LangOpts, Prefix: Prefix + Twine(TypeWidth), Ty, TI, Builder); |
307 | } |
308 | |
309 | |
310 | /// Get the value the ATOMIC_*_LOCK_FREE macro should have for a type with |
311 | /// the specified properties. |
312 | static const char *getLockFreeValue(unsigned TypeWidth, const TargetInfo &TI) { |
313 | // Fully-aligned, power-of-2 sizes no larger than the inline |
314 | // width will be inlined as lock-free operations. |
315 | // Note: we do not need to check alignment since _Atomic(T) is always |
316 | // appropriately-aligned in clang. |
317 | if (TI.hasBuiltinAtomic(AtomicSizeInBits: TypeWidth, AlignmentInBits: TypeWidth)) |
318 | return "2" ; // "always lock free" |
319 | // We cannot be certain what operations the lib calls might be |
320 | // able to implement as lock-free on future processors. |
321 | return "1" ; // "sometimes lock free" |
322 | } |
323 | |
324 | /// Add definitions required for a smooth interaction between |
325 | /// Objective-C++ automated reference counting and libstdc++ (4.2). |
326 | static void AddObjCXXARCLibstdcxxDefines(const LangOptions &LangOpts, |
327 | MacroBuilder &Builder) { |
328 | Builder.defineMacro(Name: "_GLIBCXX_PREDEFINED_OBJC_ARC_IS_SCALAR" ); |
329 | |
330 | std::string Result; |
331 | { |
332 | // Provide specializations for the __is_scalar type trait so that |
333 | // lifetime-qualified objects are not considered "scalar" types, which |
334 | // libstdc++ uses as an indicator of the presence of trivial copy, assign, |
335 | // default-construct, and destruct semantics (none of which hold for |
336 | // lifetime-qualified objects in ARC). |
337 | llvm::raw_string_ostream Out(Result); |
338 | |
339 | Out << "namespace std {\n" |
340 | << "\n" |
341 | << "struct __true_type;\n" |
342 | << "struct __false_type;\n" |
343 | << "\n" ; |
344 | |
345 | Out << "template<typename _Tp> struct __is_scalar;\n" |
346 | << "\n" ; |
347 | |
348 | if (LangOpts.ObjCAutoRefCount) { |
349 | Out << "template<typename _Tp>\n" |
350 | << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> {\n" |
351 | << " enum { __value = 0 };\n" |
352 | << " typedef __false_type __type;\n" |
353 | << "};\n" |
354 | << "\n" ; |
355 | } |
356 | |
357 | if (LangOpts.ObjCWeak) { |
358 | Out << "template<typename _Tp>\n" |
359 | << "struct __is_scalar<__attribute__((objc_ownership(weak))) _Tp> {\n" |
360 | << " enum { __value = 0 };\n" |
361 | << " typedef __false_type __type;\n" |
362 | << "};\n" |
363 | << "\n" ; |
364 | } |
365 | |
366 | if (LangOpts.ObjCAutoRefCount) { |
367 | Out << "template<typename _Tp>\n" |
368 | << "struct __is_scalar<__attribute__((objc_ownership(autoreleasing)))" |
369 | << " _Tp> {\n" |
370 | << " enum { __value = 0 };\n" |
371 | << " typedef __false_type __type;\n" |
372 | << "};\n" |
373 | << "\n" ; |
374 | } |
375 | |
376 | Out << "}\n" ; |
377 | } |
378 | Builder.append(Str: Result); |
379 | } |
380 | |
381 | static void InitializeStandardPredefinedMacros(const TargetInfo &TI, |
382 | const LangOptions &LangOpts, |
383 | const FrontendOptions &FEOpts, |
384 | MacroBuilder &Builder) { |
385 | if (LangOpts.HLSL) { |
386 | Builder.defineMacro(Name: "__hlsl_clang" ); |
387 | // HLSL Version |
388 | Builder.defineMacro(Name: "__HLSL_VERSION" , |
389 | Value: Twine((unsigned)LangOpts.getHLSLVersion())); |
390 | |
391 | if (LangOpts.NativeHalfType) |
392 | Builder.defineMacro(Name: "__HLSL_ENABLE_16_BIT" , |
393 | Value: Twine((unsigned)LangOpts.getHLSLVersion())); |
394 | |
395 | // Shader target information |
396 | // "enums" for shader stages |
397 | Builder.defineMacro(Name: "__SHADER_STAGE_VERTEX" , |
398 | Value: Twine((uint32_t)ShaderStage::Vertex)); |
399 | Builder.defineMacro(Name: "__SHADER_STAGE_PIXEL" , |
400 | Value: Twine((uint32_t)ShaderStage::Pixel)); |
401 | Builder.defineMacro(Name: "__SHADER_STAGE_GEOMETRY" , |
402 | Value: Twine((uint32_t)ShaderStage::Geometry)); |
403 | Builder.defineMacro(Name: "__SHADER_STAGE_HULL" , |
404 | Value: Twine((uint32_t)ShaderStage::Hull)); |
405 | Builder.defineMacro(Name: "__SHADER_STAGE_DOMAIN" , |
406 | Value: Twine((uint32_t)ShaderStage::Domain)); |
407 | Builder.defineMacro(Name: "__SHADER_STAGE_COMPUTE" , |
408 | Value: Twine((uint32_t)ShaderStage::Compute)); |
409 | Builder.defineMacro(Name: "__SHADER_STAGE_AMPLIFICATION" , |
410 | Value: Twine((uint32_t)ShaderStage::Amplification)); |
411 | Builder.defineMacro(Name: "__SHADER_STAGE_MESH" , |
412 | Value: Twine((uint32_t)ShaderStage::Mesh)); |
413 | Builder.defineMacro(Name: "__SHADER_STAGE_LIBRARY" , |
414 | Value: Twine((uint32_t)ShaderStage::Library)); |
415 | // The current shader stage itself |
416 | uint32_t StageInteger = static_cast<uint32_t>( |
417 | hlsl::getStageFromEnvironment(E: TI.getTriple().getEnvironment())); |
418 | |
419 | Builder.defineMacro(Name: "__SHADER_TARGET_STAGE" , Value: Twine(StageInteger)); |
420 | // Add target versions |
421 | if (TI.getTriple().getOS() == llvm::Triple::ShaderModel) { |
422 | VersionTuple Version = TI.getTriple().getOSVersion(); |
423 | Builder.defineMacro(Name: "__SHADER_TARGET_MAJOR" , Value: Twine(Version.getMajor())); |
424 | unsigned Minor = Version.getMinor().value_or(u: 0); |
425 | Builder.defineMacro(Name: "__SHADER_TARGET_MINOR" , Value: Twine(Minor)); |
426 | } |
427 | return; |
428 | } |
429 | // C++ [cpp.predefined]p1: |
430 | // The following macro names shall be defined by the implementation: |
431 | |
432 | // -- __STDC__ |
433 | // [C++] Whether __STDC__ is predefined and if so, what its value is, |
434 | // are implementation-defined. |
435 | // (Removed in C++20.) |
436 | if (!LangOpts.MSVCCompat && !LangOpts.TraditionalCPP) |
437 | Builder.defineMacro(Name: "__STDC__" ); |
438 | // -- __STDC_HOSTED__ |
439 | // The integer literal 1 if the implementation is a hosted |
440 | // implementation or the integer literal 0 if it is not. |
441 | if (LangOpts.Freestanding) |
442 | Builder.defineMacro(Name: "__STDC_HOSTED__" , Value: "0" ); |
443 | else |
444 | Builder.defineMacro(Name: "__STDC_HOSTED__" ); |
445 | |
446 | // -- __STDC_VERSION__ |
447 | // [C++] Whether __STDC_VERSION__ is predefined and if so, what its |
448 | // value is, are implementation-defined. |
449 | // (Removed in C++20.) |
450 | if (!LangOpts.CPlusPlus) { |
451 | if (LangOpts.C23) |
452 | Builder.defineMacro(Name: "__STDC_VERSION__" , Value: "202311L" ); |
453 | else if (LangOpts.C17) |
454 | Builder.defineMacro(Name: "__STDC_VERSION__" , Value: "201710L" ); |
455 | else if (LangOpts.C11) |
456 | Builder.defineMacro(Name: "__STDC_VERSION__" , Value: "201112L" ); |
457 | else if (LangOpts.C99) |
458 | Builder.defineMacro(Name: "__STDC_VERSION__" , Value: "199901L" ); |
459 | else if (!LangOpts.GNUMode && LangOpts.Digraphs) |
460 | Builder.defineMacro(Name: "__STDC_VERSION__" , Value: "199409L" ); |
461 | } else { |
462 | // -- __cplusplus |
463 | if (LangOpts.CPlusPlus26) |
464 | // FIXME: Use correct value for C++26. |
465 | Builder.defineMacro(Name: "__cplusplus" , Value: "202400L" ); |
466 | else if (LangOpts.CPlusPlus23) |
467 | Builder.defineMacro(Name: "__cplusplus" , Value: "202302L" ); |
468 | // [C++20] The integer literal 202002L. |
469 | else if (LangOpts.CPlusPlus20) |
470 | Builder.defineMacro(Name: "__cplusplus" , Value: "202002L" ); |
471 | // [C++17] The integer literal 201703L. |
472 | else if (LangOpts.CPlusPlus17) |
473 | Builder.defineMacro(Name: "__cplusplus" , Value: "201703L" ); |
474 | // [C++14] The name __cplusplus is defined to the value 201402L when |
475 | // compiling a C++ translation unit. |
476 | else if (LangOpts.CPlusPlus14) |
477 | Builder.defineMacro(Name: "__cplusplus" , Value: "201402L" ); |
478 | // [C++11] The name __cplusplus is defined to the value 201103L when |
479 | // compiling a C++ translation unit. |
480 | else if (LangOpts.CPlusPlus11) |
481 | Builder.defineMacro(Name: "__cplusplus" , Value: "201103L" ); |
482 | // [C++03] The name __cplusplus is defined to the value 199711L when |
483 | // compiling a C++ translation unit. |
484 | else |
485 | Builder.defineMacro(Name: "__cplusplus" , Value: "199711L" ); |
486 | |
487 | // -- __STDCPP_DEFAULT_NEW_ALIGNMENT__ |
488 | // [C++17] An integer literal of type std::size_t whose value is the |
489 | // alignment guaranteed by a call to operator new(std::size_t) |
490 | // |
491 | // We provide this in all language modes, since it seems generally useful. |
492 | Builder.defineMacro(Name: "__STDCPP_DEFAULT_NEW_ALIGNMENT__" , |
493 | Value: Twine(TI.getNewAlign() / TI.getCharWidth()) + |
494 | TI.getTypeConstantSuffix(T: TI.getSizeType())); |
495 | |
496 | // -- __STDCPP_ÂTHREADS__ |
497 | // Defined, and has the value integer literal 1, if and only if a |
498 | // program can have more than one thread of execution. |
499 | if (LangOpts.getThreadModel() == LangOptions::ThreadModelKind::POSIX) |
500 | Builder.defineMacro(Name: "__STDCPP_THREADS__" , Value: "1" ); |
501 | } |
502 | |
503 | // In C11 these are environment macros. In C++11 they are only defined |
504 | // as part of <cuchar>. To prevent breakage when mixing C and C++ |
505 | // code, define these macros unconditionally. We can define them |
506 | // unconditionally, as Clang always uses UTF-16 and UTF-32 for 16-bit |
507 | // and 32-bit character literals. |
508 | Builder.defineMacro(Name: "__STDC_UTF_16__" , Value: "1" ); |
509 | Builder.defineMacro(Name: "__STDC_UTF_32__" , Value: "1" ); |
510 | |
511 | if (LangOpts.ObjC) |
512 | Builder.defineMacro(Name: "__OBJC__" ); |
513 | |
514 | // OpenCL v1.0/1.1 s6.9, v1.2/2.0 s6.10: Preprocessor Directives and Macros. |
515 | if (LangOpts.OpenCL) { |
516 | if (LangOpts.CPlusPlus) { |
517 | switch (LangOpts.OpenCLCPlusPlusVersion) { |
518 | case 100: |
519 | Builder.defineMacro(Name: "__OPENCL_CPP_VERSION__" , Value: "100" ); |
520 | break; |
521 | case 202100: |
522 | Builder.defineMacro(Name: "__OPENCL_CPP_VERSION__" , Value: "202100" ); |
523 | break; |
524 | default: |
525 | llvm_unreachable("Unsupported C++ version for OpenCL" ); |
526 | } |
527 | Builder.defineMacro(Name: "__CL_CPP_VERSION_1_0__" , Value: "100" ); |
528 | Builder.defineMacro(Name: "__CL_CPP_VERSION_2021__" , Value: "202100" ); |
529 | } else { |
530 | // OpenCL v1.0 and v1.1 do not have a predefined macro to indicate the |
531 | // language standard with which the program is compiled. __OPENCL_VERSION__ |
532 | // is for the OpenCL version supported by the OpenCL device, which is not |
533 | // necessarily the language standard with which the program is compiled. |
534 | // A shared OpenCL header file requires a macro to indicate the language |
535 | // standard. As a workaround, __OPENCL_C_VERSION__ is defined for |
536 | // OpenCL v1.0 and v1.1. |
537 | switch (LangOpts.OpenCLVersion) { |
538 | case 100: |
539 | Builder.defineMacro(Name: "__OPENCL_C_VERSION__" , Value: "100" ); |
540 | break; |
541 | case 110: |
542 | Builder.defineMacro(Name: "__OPENCL_C_VERSION__" , Value: "110" ); |
543 | break; |
544 | case 120: |
545 | Builder.defineMacro(Name: "__OPENCL_C_VERSION__" , Value: "120" ); |
546 | break; |
547 | case 200: |
548 | Builder.defineMacro(Name: "__OPENCL_C_VERSION__" , Value: "200" ); |
549 | break; |
550 | case 300: |
551 | Builder.defineMacro(Name: "__OPENCL_C_VERSION__" , Value: "300" ); |
552 | break; |
553 | default: |
554 | llvm_unreachable("Unsupported OpenCL version" ); |
555 | } |
556 | } |
557 | Builder.defineMacro(Name: "CL_VERSION_1_0" , Value: "100" ); |
558 | Builder.defineMacro(Name: "CL_VERSION_1_1" , Value: "110" ); |
559 | Builder.defineMacro(Name: "CL_VERSION_1_2" , Value: "120" ); |
560 | Builder.defineMacro(Name: "CL_VERSION_2_0" , Value: "200" ); |
561 | Builder.defineMacro(Name: "CL_VERSION_3_0" , Value: "300" ); |
562 | |
563 | if (TI.isLittleEndian()) |
564 | Builder.defineMacro(Name: "__ENDIAN_LITTLE__" ); |
565 | |
566 | if (LangOpts.FastRelaxedMath) |
567 | Builder.defineMacro(Name: "__FAST_RELAXED_MATH__" ); |
568 | } |
569 | |
570 | if (LangOpts.SYCLIsDevice || LangOpts.SYCLIsHost) { |
571 | // SYCL Version is set to a value when building SYCL applications |
572 | if (LangOpts.getSYCLVersion() == LangOptions::SYCL_2017) |
573 | Builder.defineMacro(Name: "CL_SYCL_LANGUAGE_VERSION" , Value: "121" ); |
574 | else if (LangOpts.getSYCLVersion() == LangOptions::SYCL_2020) |
575 | Builder.defineMacro(Name: "SYCL_LANGUAGE_VERSION" , Value: "202001" ); |
576 | } |
577 | |
578 | // Not "standard" per se, but available even with the -undef flag. |
579 | if (LangOpts.AsmPreprocessor) |
580 | Builder.defineMacro(Name: "__ASSEMBLER__" ); |
581 | if (LangOpts.CUDA) { |
582 | if (LangOpts.GPURelocatableDeviceCode) |
583 | Builder.defineMacro(Name: "__CLANG_RDC__" ); |
584 | if (!LangOpts.HIP) |
585 | Builder.defineMacro(Name: "__CUDA__" ); |
586 | if (LangOpts.GPUDefaultStream == |
587 | LangOptions::GPUDefaultStreamKind::PerThread) |
588 | Builder.defineMacro(Name: "CUDA_API_PER_THREAD_DEFAULT_STREAM" ); |
589 | } |
590 | if (LangOpts.HIP) { |
591 | Builder.defineMacro(Name: "__HIP__" ); |
592 | Builder.defineMacro(Name: "__HIPCC__" ); |
593 | Builder.defineMacro(Name: "__HIP_MEMORY_SCOPE_SINGLETHREAD" , Value: "1" ); |
594 | Builder.defineMacro(Name: "__HIP_MEMORY_SCOPE_WAVEFRONT" , Value: "2" ); |
595 | Builder.defineMacro(Name: "__HIP_MEMORY_SCOPE_WORKGROUP" , Value: "3" ); |
596 | Builder.defineMacro(Name: "__HIP_MEMORY_SCOPE_AGENT" , Value: "4" ); |
597 | Builder.defineMacro(Name: "__HIP_MEMORY_SCOPE_SYSTEM" , Value: "5" ); |
598 | if (LangOpts.HIPStdPar) { |
599 | Builder.defineMacro(Name: "__HIPSTDPAR__" ); |
600 | if (LangOpts.HIPStdParInterposeAlloc) |
601 | Builder.defineMacro(Name: "__HIPSTDPAR_INTERPOSE_ALLOC__" ); |
602 | } |
603 | if (LangOpts.CUDAIsDevice) { |
604 | Builder.defineMacro(Name: "__HIP_DEVICE_COMPILE__" ); |
605 | if (!TI.hasHIPImageSupport()) { |
606 | Builder.defineMacro(Name: "__HIP_NO_IMAGE_SUPPORT__" , Value: "1" ); |
607 | // Deprecated. |
608 | Builder.defineMacro(Name: "__HIP_NO_IMAGE_SUPPORT" , Value: "1" ); |
609 | } |
610 | } |
611 | if (LangOpts.GPUDefaultStream == |
612 | LangOptions::GPUDefaultStreamKind::PerThread) { |
613 | Builder.defineMacro(Name: "__HIP_API_PER_THREAD_DEFAULT_STREAM__" ); |
614 | // Deprecated. |
615 | Builder.defineMacro(Name: "HIP_API_PER_THREAD_DEFAULT_STREAM" ); |
616 | } |
617 | } |
618 | |
619 | if (LangOpts.OpenACC) { |
620 | // FIXME: When we have full support for OpenACC, we should set this to the |
621 | // version we support. Until then, set as '1' by default, but provide a |
622 | // temporary mechanism for users to override this so real-world examples can |
623 | // be tested against. |
624 | if (!LangOpts.OpenACCMacroOverride.empty()) |
625 | Builder.defineMacro(Name: "_OPENACC" , Value: LangOpts.OpenACCMacroOverride); |
626 | else |
627 | Builder.defineMacro(Name: "_OPENACC" , Value: "1" ); |
628 | } |
629 | } |
630 | |
631 | /// Initialize the predefined C++ language feature test macros defined in |
632 | /// ISO/IEC JTC1/SC22/WG21 (C++) SD-6: "SG10 Feature Test Recommendations". |
633 | static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts, |
634 | MacroBuilder &Builder) { |
635 | // C++98 features. |
636 | if (LangOpts.RTTI) |
637 | Builder.defineMacro(Name: "__cpp_rtti" , Value: "199711L" ); |
638 | if (LangOpts.CXXExceptions) |
639 | Builder.defineMacro(Name: "__cpp_exceptions" , Value: "199711L" ); |
640 | |
641 | // C++11 features. |
642 | if (LangOpts.CPlusPlus11) { |
643 | Builder.defineMacro(Name: "__cpp_unicode_characters" , Value: "200704L" ); |
644 | Builder.defineMacro(Name: "__cpp_raw_strings" , Value: "200710L" ); |
645 | Builder.defineMacro(Name: "__cpp_unicode_literals" , Value: "200710L" ); |
646 | Builder.defineMacro(Name: "__cpp_user_defined_literals" , Value: "200809L" ); |
647 | Builder.defineMacro(Name: "__cpp_lambdas" , Value: "200907L" ); |
648 | Builder.defineMacro(Name: "__cpp_constexpr" , Value: LangOpts.CPlusPlus26 ? "202306L" |
649 | : LangOpts.CPlusPlus23 ? "202211L" |
650 | : LangOpts.CPlusPlus20 ? "201907L" |
651 | : LangOpts.CPlusPlus17 ? "201603L" |
652 | : LangOpts.CPlusPlus14 ? "201304L" |
653 | : "200704" ); |
654 | Builder.defineMacro(Name: "__cpp_constexpr_in_decltype" , Value: "201711L" ); |
655 | Builder.defineMacro(Name: "__cpp_range_based_for" , |
656 | Value: LangOpts.CPlusPlus23 ? "202211L" |
657 | : LangOpts.CPlusPlus17 ? "201603L" |
658 | : "200907" ); |
659 | Builder.defineMacro(Name: "__cpp_static_assert" , Value: LangOpts.CPlusPlus26 ? "202306L" |
660 | : LangOpts.CPlusPlus17 |
661 | ? "201411L" |
662 | : "200410" ); |
663 | Builder.defineMacro(Name: "__cpp_decltype" , Value: "200707L" ); |
664 | Builder.defineMacro(Name: "__cpp_attributes" , Value: "200809L" ); |
665 | Builder.defineMacro(Name: "__cpp_rvalue_references" , Value: "200610L" ); |
666 | Builder.defineMacro(Name: "__cpp_variadic_templates" , Value: "200704L" ); |
667 | Builder.defineMacro(Name: "__cpp_initializer_lists" , Value: "200806L" ); |
668 | Builder.defineMacro(Name: "__cpp_delegating_constructors" , Value: "200604L" ); |
669 | Builder.defineMacro(Name: "__cpp_nsdmi" , Value: "200809L" ); |
670 | Builder.defineMacro(Name: "__cpp_inheriting_constructors" , Value: "201511L" ); |
671 | Builder.defineMacro(Name: "__cpp_ref_qualifiers" , Value: "200710L" ); |
672 | Builder.defineMacro(Name: "__cpp_alias_templates" , Value: "200704L" ); |
673 | } |
674 | if (LangOpts.ThreadsafeStatics) |
675 | Builder.defineMacro(Name: "__cpp_threadsafe_static_init" , Value: "200806L" ); |
676 | |
677 | // C++14 features. |
678 | if (LangOpts.CPlusPlus14) { |
679 | Builder.defineMacro(Name: "__cpp_binary_literals" , Value: "201304L" ); |
680 | Builder.defineMacro(Name: "__cpp_digit_separators" , Value: "201309L" ); |
681 | Builder.defineMacro(Name: "__cpp_init_captures" , |
682 | Value: LangOpts.CPlusPlus20 ? "201803L" : "201304L" ); |
683 | Builder.defineMacro(Name: "__cpp_generic_lambdas" , |
684 | Value: LangOpts.CPlusPlus20 ? "201707L" : "201304L" ); |
685 | Builder.defineMacro(Name: "__cpp_decltype_auto" , Value: "201304L" ); |
686 | Builder.defineMacro(Name: "__cpp_return_type_deduction" , Value: "201304L" ); |
687 | Builder.defineMacro(Name: "__cpp_aggregate_nsdmi" , Value: "201304L" ); |
688 | Builder.defineMacro(Name: "__cpp_variable_templates" , Value: "201304L" ); |
689 | } |
690 | if (LangOpts.SizedDeallocation) |
691 | Builder.defineMacro(Name: "__cpp_sized_deallocation" , Value: "201309L" ); |
692 | |
693 | // C++17 features. |
694 | if (LangOpts.CPlusPlus17) { |
695 | Builder.defineMacro(Name: "__cpp_hex_float" , Value: "201603L" ); |
696 | Builder.defineMacro(Name: "__cpp_inline_variables" , Value: "201606L" ); |
697 | Builder.defineMacro(Name: "__cpp_noexcept_function_type" , Value: "201510L" ); |
698 | Builder.defineMacro(Name: "__cpp_capture_star_this" , Value: "201603L" ); |
699 | Builder.defineMacro(Name: "__cpp_if_constexpr" , Value: "201606L" ); |
700 | Builder.defineMacro(Name: "__cpp_deduction_guides" , Value: "201703L" ); // (not latest) |
701 | Builder.defineMacro(Name: "__cpp_template_auto" , Value: "201606L" ); // (old name) |
702 | Builder.defineMacro(Name: "__cpp_namespace_attributes" , Value: "201411L" ); |
703 | Builder.defineMacro(Name: "__cpp_enumerator_attributes" , Value: "201411L" ); |
704 | Builder.defineMacro(Name: "__cpp_nested_namespace_definitions" , Value: "201411L" ); |
705 | Builder.defineMacro(Name: "__cpp_variadic_using" , Value: "201611L" ); |
706 | Builder.defineMacro(Name: "__cpp_aggregate_bases" , Value: "201603L" ); |
707 | Builder.defineMacro(Name: "__cpp_structured_bindings" , Value: "201606L" ); |
708 | Builder.defineMacro(Name: "__cpp_nontype_template_args" , |
709 | Value: "201411L" ); // (not latest) |
710 | Builder.defineMacro(Name: "__cpp_fold_expressions" , Value: "201603L" ); |
711 | Builder.defineMacro(Name: "__cpp_guaranteed_copy_elision" , Value: "201606L" ); |
712 | Builder.defineMacro(Name: "__cpp_nontype_template_parameter_auto" , Value: "201606L" ); |
713 | } |
714 | if (LangOpts.AlignedAllocation && !LangOpts.AlignedAllocationUnavailable) |
715 | Builder.defineMacro(Name: "__cpp_aligned_new" , Value: "201606L" ); |
716 | if (LangOpts.RelaxedTemplateTemplateArgs) |
717 | Builder.defineMacro(Name: "__cpp_template_template_args" , Value: "201611L" ); |
718 | |
719 | // C++20 features. |
720 | if (LangOpts.CPlusPlus20) { |
721 | Builder.defineMacro(Name: "__cpp_aggregate_paren_init" , Value: "201902L" ); |
722 | |
723 | Builder.defineMacro(Name: "__cpp_concepts" , Value: "202002" ); |
724 | Builder.defineMacro(Name: "__cpp_conditional_explicit" , Value: "201806L" ); |
725 | Builder.defineMacro(Name: "__cpp_consteval" , Value: "202211L" ); |
726 | Builder.defineMacro(Name: "__cpp_constexpr_dynamic_alloc" , Value: "201907L" ); |
727 | Builder.defineMacro(Name: "__cpp_constinit" , Value: "201907L" ); |
728 | Builder.defineMacro(Name: "__cpp_impl_coroutine" , Value: "201902L" ); |
729 | Builder.defineMacro(Name: "__cpp_designated_initializers" , Value: "201707L" ); |
730 | Builder.defineMacro(Name: "__cpp_impl_three_way_comparison" , Value: "201907L" ); |
731 | //Builder.defineMacro("__cpp_modules", "201907L"); |
732 | Builder.defineMacro(Name: "__cpp_using_enum" , Value: "201907L" ); |
733 | } |
734 | // C++23 features. |
735 | if (LangOpts.CPlusPlus23) { |
736 | Builder.defineMacro(Name: "__cpp_implicit_move" , Value: "202207L" ); |
737 | Builder.defineMacro(Name: "__cpp_size_t_suffix" , Value: "202011L" ); |
738 | Builder.defineMacro(Name: "__cpp_if_consteval" , Value: "202106L" ); |
739 | Builder.defineMacro(Name: "__cpp_multidimensional_subscript" , Value: "202211L" ); |
740 | Builder.defineMacro(Name: "__cpp_auto_cast" , Value: "202110L" ); |
741 | } |
742 | |
743 | // We provide those C++23 features as extensions in earlier language modes, so |
744 | // we also define their feature test macros. |
745 | if (LangOpts.CPlusPlus11) |
746 | Builder.defineMacro(Name: "__cpp_static_call_operator" , Value: "202207L" ); |
747 | Builder.defineMacro(Name: "__cpp_named_character_escapes" , Value: "202207L" ); |
748 | Builder.defineMacro(Name: "__cpp_placeholder_variables" , Value: "202306L" ); |
749 | |
750 | // C++26 features supported in earlier language modes. |
751 | Builder.defineMacro(Name: "__cpp_deleted_function" , Value: "202403L" ); |
752 | |
753 | if (LangOpts.Char8) |
754 | Builder.defineMacro(Name: "__cpp_char8_t" , Value: "202207L" ); |
755 | Builder.defineMacro(Name: "__cpp_impl_destroying_delete" , Value: "201806L" ); |
756 | } |
757 | |
758 | /// InitializeOpenCLFeatureTestMacros - Define OpenCL macros based on target |
759 | /// settings and language version |
760 | void InitializeOpenCLFeatureTestMacros(const TargetInfo &TI, |
761 | const LangOptions &Opts, |
762 | MacroBuilder &Builder) { |
763 | const llvm::StringMap<bool> &OpenCLFeaturesMap = TI.getSupportedOpenCLOpts(); |
764 | // FIXME: OpenCL options which affect language semantics/syntax |
765 | // should be moved into LangOptions. |
766 | auto defineOpenCLExtMacro = [&](llvm::StringRef Name, auto... OptArgs) { |
767 | // Check if extension is supported by target and is available in this |
768 | // OpenCL version |
769 | if (TI.hasFeatureEnabled(Features: OpenCLFeaturesMap, Name) && |
770 | OpenCLOptions::isOpenCLOptionAvailableIn(Opts, OptArgs...)) |
771 | Builder.defineMacro(Name); |
772 | }; |
773 | #define OPENCL_GENERIC_EXTENSION(Ext, ...) \ |
774 | defineOpenCLExtMacro(#Ext, __VA_ARGS__); |
775 | #include "clang/Basic/OpenCLExtensions.def" |
776 | |
777 | // Assume compiling for FULL profile |
778 | Builder.defineMacro(Name: "__opencl_c_int64" ); |
779 | } |
780 | |
781 | llvm::SmallString<32> ConstructFixedPointLiteral(llvm::APFixedPoint Val, |
782 | llvm::StringRef Suffix) { |
783 | if (Val.isSigned() && Val == llvm::APFixedPoint::getMin(Sema: Val.getSemantics())) { |
784 | // When representing the min value of a signed fixed point type in source |
785 | // code, we cannot simply write `-<lowest value>`. For example, the min |
786 | // value of a `short _Fract` cannot be written as `-1.0hr`. This is because |
787 | // the parser will read this (and really any negative numerical literal) as |
788 | // a UnaryOperator that owns a FixedPointLiteral with a positive value |
789 | // rather than just a FixedPointLiteral with a negative value. Compiling |
790 | // `-1.0hr` results in an overflow to the maximal value of that fixed point |
791 | // type. The correct way to represent a signed min value is to instead split |
792 | // it into two halves, like `(-0.5hr-0.5hr)` which is what the standard |
793 | // defines SFRACT_MIN as. |
794 | llvm::SmallString<32> Literal; |
795 | Literal.push_back(Elt: '('); |
796 | llvm::SmallString<32> HalfStr = |
797 | ConstructFixedPointLiteral(Val: Val.shr(Amt: 1), Suffix); |
798 | Literal += HalfStr; |
799 | Literal += HalfStr; |
800 | Literal.push_back(Elt: ')'); |
801 | return Literal; |
802 | } |
803 | |
804 | llvm::SmallString<32> Str(Val.toString()); |
805 | Str += Suffix; |
806 | return Str; |
807 | } |
808 | |
809 | void DefineFixedPointMacros(const TargetInfo &TI, MacroBuilder &Builder, |
810 | llvm::StringRef TypeName, llvm::StringRef Suffix, |
811 | unsigned Width, unsigned Scale, bool Signed) { |
812 | // Saturation doesn't affect the size or scale of a fixed point type, so we |
813 | // don't need it here. |
814 | llvm::FixedPointSemantics FXSema( |
815 | Width, Scale, Signed, /*IsSaturated=*/false, |
816 | !Signed && TI.doUnsignedFixedPointTypesHavePadding()); |
817 | llvm::SmallString<32> MacroPrefix("__" ); |
818 | MacroPrefix += TypeName; |
819 | Builder.defineMacro(Name: MacroPrefix + "_EPSILON__" , |
820 | Value: ConstructFixedPointLiteral( |
821 | Val: llvm::APFixedPoint::getEpsilon(Sema: FXSema), Suffix)); |
822 | Builder.defineMacro(Name: MacroPrefix + "_FBIT__" , Value: Twine(Scale)); |
823 | Builder.defineMacro( |
824 | Name: MacroPrefix + "_MAX__" , |
825 | Value: ConstructFixedPointLiteral(Val: llvm::APFixedPoint::getMax(Sema: FXSema), Suffix)); |
826 | |
827 | // ISO/IEC TR 18037:2008 doesn't specify MIN macros for unsigned types since |
828 | // they're all just zero. |
829 | if (Signed) |
830 | Builder.defineMacro( |
831 | Name: MacroPrefix + "_MIN__" , |
832 | Value: ConstructFixedPointLiteral(Val: llvm::APFixedPoint::getMin(Sema: FXSema), Suffix)); |
833 | } |
834 | |
835 | static void InitializePredefinedMacros(const TargetInfo &TI, |
836 | const LangOptions &LangOpts, |
837 | const FrontendOptions &FEOpts, |
838 | const PreprocessorOptions &PPOpts, |
839 | MacroBuilder &Builder) { |
840 | // Compiler version introspection macros. |
841 | Builder.defineMacro(Name: "__llvm__" ); // LLVM Backend |
842 | Builder.defineMacro(Name: "__clang__" ); // Clang Frontend |
843 | #define TOSTR2(X) #X |
844 | #define TOSTR(X) TOSTR2(X) |
845 | Builder.defineMacro(Name: "__clang_major__" , TOSTR(CLANG_VERSION_MAJOR)); |
846 | Builder.defineMacro(Name: "__clang_minor__" , TOSTR(CLANG_VERSION_MINOR)); |
847 | Builder.defineMacro(Name: "__clang_patchlevel__" , TOSTR(CLANG_VERSION_PATCHLEVEL)); |
848 | #undef TOSTR |
849 | #undef TOSTR2 |
850 | Builder.defineMacro(Name: "__clang_version__" , |
851 | Value: "\"" CLANG_VERSION_STRING " " |
852 | + getClangFullRepositoryVersion() + "\"" ); |
853 | |
854 | if (LangOpts.GNUCVersion != 0) { |
855 | // Major, minor, patch, are given two decimal places each, so 4.2.1 becomes |
856 | // 40201. |
857 | unsigned GNUCMajor = LangOpts.GNUCVersion / 100 / 100; |
858 | unsigned GNUCMinor = LangOpts.GNUCVersion / 100 % 100; |
859 | unsigned GNUCPatch = LangOpts.GNUCVersion % 100; |
860 | Builder.defineMacro(Name: "__GNUC__" , Value: Twine(GNUCMajor)); |
861 | Builder.defineMacro(Name: "__GNUC_MINOR__" , Value: Twine(GNUCMinor)); |
862 | Builder.defineMacro(Name: "__GNUC_PATCHLEVEL__" , Value: Twine(GNUCPatch)); |
863 | Builder.defineMacro(Name: "__GXX_ABI_VERSION" , Value: "1002" ); |
864 | |
865 | if (LangOpts.CPlusPlus) { |
866 | Builder.defineMacro(Name: "__GNUG__" , Value: Twine(GNUCMajor)); |
867 | Builder.defineMacro(Name: "__GXX_WEAK__" ); |
868 | } |
869 | } |
870 | |
871 | // Define macros for the C11 / C++11 memory orderings |
872 | Builder.defineMacro(Name: "__ATOMIC_RELAXED" , Value: "0" ); |
873 | Builder.defineMacro(Name: "__ATOMIC_CONSUME" , Value: "1" ); |
874 | Builder.defineMacro(Name: "__ATOMIC_ACQUIRE" , Value: "2" ); |
875 | Builder.defineMacro(Name: "__ATOMIC_RELEASE" , Value: "3" ); |
876 | Builder.defineMacro(Name: "__ATOMIC_ACQ_REL" , Value: "4" ); |
877 | Builder.defineMacro(Name: "__ATOMIC_SEQ_CST" , Value: "5" ); |
878 | |
879 | // Define macros for the clang atomic scopes. |
880 | Builder.defineMacro(Name: "__MEMORY_SCOPE_SYSTEM" , Value: "0" ); |
881 | Builder.defineMacro(Name: "__MEMORY_SCOPE_DEVICE" , Value: "1" ); |
882 | Builder.defineMacro(Name: "__MEMORY_SCOPE_WRKGRP" , Value: "2" ); |
883 | Builder.defineMacro(Name: "__MEMORY_SCOPE_WVFRNT" , Value: "3" ); |
884 | Builder.defineMacro(Name: "__MEMORY_SCOPE_SINGLE" , Value: "4" ); |
885 | |
886 | // Define macros for the OpenCL memory scope. |
887 | // The values should match AtomicScopeOpenCLModel::ID enum. |
888 | static_assert( |
889 | static_cast<unsigned>(AtomicScopeOpenCLModel::WorkGroup) == 1 && |
890 | static_cast<unsigned>(AtomicScopeOpenCLModel::Device) == 2 && |
891 | static_cast<unsigned>(AtomicScopeOpenCLModel::AllSVMDevices) == 3 && |
892 | static_cast<unsigned>(AtomicScopeOpenCLModel::SubGroup) == 4, |
893 | "Invalid OpenCL memory scope enum definition" ); |
894 | Builder.defineMacro(Name: "__OPENCL_MEMORY_SCOPE_WORK_ITEM" , Value: "0" ); |
895 | Builder.defineMacro(Name: "__OPENCL_MEMORY_SCOPE_WORK_GROUP" , Value: "1" ); |
896 | Builder.defineMacro(Name: "__OPENCL_MEMORY_SCOPE_DEVICE" , Value: "2" ); |
897 | Builder.defineMacro(Name: "__OPENCL_MEMORY_SCOPE_ALL_SVM_DEVICES" , Value: "3" ); |
898 | Builder.defineMacro(Name: "__OPENCL_MEMORY_SCOPE_SUB_GROUP" , Value: "4" ); |
899 | |
900 | // Define macros for floating-point data classes, used in __builtin_isfpclass. |
901 | Builder.defineMacro(Name: "__FPCLASS_SNAN" , Value: "0x0001" ); |
902 | Builder.defineMacro(Name: "__FPCLASS_QNAN" , Value: "0x0002" ); |
903 | Builder.defineMacro(Name: "__FPCLASS_NEGINF" , Value: "0x0004" ); |
904 | Builder.defineMacro(Name: "__FPCLASS_NEGNORMAL" , Value: "0x0008" ); |
905 | Builder.defineMacro(Name: "__FPCLASS_NEGSUBNORMAL" , Value: "0x0010" ); |
906 | Builder.defineMacro(Name: "__FPCLASS_NEGZERO" , Value: "0x0020" ); |
907 | Builder.defineMacro(Name: "__FPCLASS_POSZERO" , Value: "0x0040" ); |
908 | Builder.defineMacro(Name: "__FPCLASS_POSSUBNORMAL" , Value: "0x0080" ); |
909 | Builder.defineMacro(Name: "__FPCLASS_POSNORMAL" , Value: "0x0100" ); |
910 | Builder.defineMacro(Name: "__FPCLASS_POSINF" , Value: "0x0200" ); |
911 | |
912 | // Support for #pragma redefine_extname (Sun compatibility) |
913 | Builder.defineMacro(Name: "__PRAGMA_REDEFINE_EXTNAME" , Value: "1" ); |
914 | |
915 | // Previously this macro was set to a string aiming to achieve compatibility |
916 | // with GCC 4.2.1. Now, just return the full Clang version |
917 | Builder.defineMacro(Name: "__VERSION__" , Value: "\"" + |
918 | Twine(getClangFullCPPVersion()) + "\"" ); |
919 | |
920 | // Initialize language-specific preprocessor defines. |
921 | |
922 | // Standard conforming mode? |
923 | if (!LangOpts.GNUMode && !LangOpts.MSVCCompat) |
924 | Builder.defineMacro(Name: "__STRICT_ANSI__" ); |
925 | |
926 | if (LangOpts.GNUCVersion && LangOpts.CPlusPlus11) |
927 | Builder.defineMacro(Name: "__GXX_EXPERIMENTAL_CXX0X__" ); |
928 | |
929 | if (LangOpts.ObjC) { |
930 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
931 | Builder.defineMacro(Name: "__OBJC2__" ); |
932 | |
933 | if (LangOpts.ObjCExceptions) |
934 | Builder.defineMacro(Name: "OBJC_ZEROCOST_EXCEPTIONS" ); |
935 | } |
936 | |
937 | if (LangOpts.getGC() != LangOptions::NonGC) |
938 | Builder.defineMacro(Name: "__OBJC_GC__" ); |
939 | |
940 | if (LangOpts.ObjCRuntime.isNeXTFamily()) |
941 | Builder.defineMacro(Name: "__NEXT_RUNTIME__" ); |
942 | |
943 | if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::GNUstep) { |
944 | auto version = LangOpts.ObjCRuntime.getVersion(); |
945 | std::string versionString = "1" ; |
946 | // Don't rely on the tuple argument, because we can be asked to target |
947 | // later ABIs than we actually support, so clamp these values to those |
948 | // currently supported |
949 | if (version >= VersionTuple(2, 0)) |
950 | Builder.defineMacro(Name: "__OBJC_GNUSTEP_RUNTIME_ABI__" , Value: "20" ); |
951 | else |
952 | Builder.defineMacro( |
953 | Name: "__OBJC_GNUSTEP_RUNTIME_ABI__" , |
954 | Value: "1" + Twine(std::min(a: 8U, b: version.getMinor().value_or(u: 0)))); |
955 | } |
956 | |
957 | if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::ObjFW) { |
958 | VersionTuple tuple = LangOpts.ObjCRuntime.getVersion(); |
959 | unsigned minor = tuple.getMinor().value_or(u: 0); |
960 | unsigned subminor = tuple.getSubminor().value_or(u: 0); |
961 | Builder.defineMacro(Name: "__OBJFW_RUNTIME_ABI__" , |
962 | Value: Twine(tuple.getMajor() * 10000 + minor * 100 + |
963 | subminor)); |
964 | } |
965 | |
966 | Builder.defineMacro(Name: "IBOutlet" , Value: "__attribute__((iboutlet))" ); |
967 | Builder.defineMacro(Name: "IBOutletCollection(ClassName)" , |
968 | Value: "__attribute__((iboutletcollection(ClassName)))" ); |
969 | Builder.defineMacro(Name: "IBAction" , Value: "void)__attribute__((ibaction)" ); |
970 | Builder.defineMacro(Name: "IBInspectable" , Value: "" ); |
971 | Builder.defineMacro(Name: "IB_DESIGNABLE" , Value: "" ); |
972 | } |
973 | |
974 | // Define a macro that describes the Objective-C boolean type even for C |
975 | // and C++ since BOOL can be used from non Objective-C code. |
976 | Builder.defineMacro(Name: "__OBJC_BOOL_IS_BOOL" , |
977 | Value: Twine(TI.useSignedCharForObjCBool() ? "0" : "1" )); |
978 | |
979 | if (LangOpts.CPlusPlus) |
980 | InitializeCPlusPlusFeatureTestMacros(LangOpts, Builder); |
981 | |
982 | // darwin_constant_cfstrings controls this. This is also dependent |
983 | // on other things like the runtime I believe. This is set even for C code. |
984 | if (!LangOpts.NoConstantCFStrings) |
985 | Builder.defineMacro(Name: "__CONSTANT_CFSTRINGS__" ); |
986 | |
987 | if (LangOpts.ObjC) |
988 | Builder.defineMacro(Name: "OBJC_NEW_PROPERTIES" ); |
989 | |
990 | if (LangOpts.PascalStrings) |
991 | Builder.defineMacro(Name: "__PASCAL_STRINGS__" ); |
992 | |
993 | if (LangOpts.Blocks) { |
994 | Builder.defineMacro(Name: "__block" , Value: "__attribute__((__blocks__(byref)))" ); |
995 | Builder.defineMacro(Name: "__BLOCKS__" ); |
996 | } |
997 | |
998 | if (!LangOpts.MSVCCompat && LangOpts.Exceptions) |
999 | Builder.defineMacro(Name: "__EXCEPTIONS" ); |
1000 | if (LangOpts.GNUCVersion && LangOpts.RTTI) |
1001 | Builder.defineMacro(Name: "__GXX_RTTI" ); |
1002 | |
1003 | if (LangOpts.hasSjLjExceptions()) |
1004 | Builder.defineMacro(Name: "__USING_SJLJ_EXCEPTIONS__" ); |
1005 | else if (LangOpts.hasSEHExceptions()) |
1006 | Builder.defineMacro(Name: "__SEH__" ); |
1007 | else if (LangOpts.hasDWARFExceptions() && |
1008 | (TI.getTriple().isThumb() || TI.getTriple().isARM())) |
1009 | Builder.defineMacro(Name: "__ARM_DWARF_EH__" ); |
1010 | |
1011 | if (LangOpts.Deprecated) |
1012 | Builder.defineMacro(Name: "__DEPRECATED" ); |
1013 | |
1014 | if (!LangOpts.MSVCCompat && LangOpts.CPlusPlus) |
1015 | Builder.defineMacro(Name: "__private_extern__" , Value: "extern" ); |
1016 | |
1017 | if (LangOpts.MicrosoftExt) { |
1018 | if (LangOpts.WChar) { |
1019 | // wchar_t supported as a keyword. |
1020 | Builder.defineMacro(Name: "_WCHAR_T_DEFINED" ); |
1021 | Builder.defineMacro(Name: "_NATIVE_WCHAR_T_DEFINED" ); |
1022 | } |
1023 | } |
1024 | |
1025 | // Macros to help identify the narrow and wide character sets |
1026 | // FIXME: clang currently ignores -fexec-charset=. If this changes, |
1027 | // then this may need to be updated. |
1028 | Builder.defineMacro(Name: "__clang_literal_encoding__" , Value: "\"UTF-8\"" ); |
1029 | if (TI.getTypeWidth(T: TI.getWCharType()) >= 32) { |
1030 | // FIXME: 32-bit wchar_t signals UTF-32. This may change |
1031 | // if -fwide-exec-charset= is ever supported. |
1032 | Builder.defineMacro(Name: "__clang_wide_literal_encoding__" , Value: "\"UTF-32\"" ); |
1033 | } else { |
1034 | // FIXME: Less-than 32-bit wchar_t generally means UTF-16 |
1035 | // (e.g., Windows, 32-bit IBM). This may need to be |
1036 | // updated if -fwide-exec-charset= is ever supported. |
1037 | Builder.defineMacro(Name: "__clang_wide_literal_encoding__" , Value: "\"UTF-16\"" ); |
1038 | } |
1039 | |
1040 | if (LangOpts.Optimize) |
1041 | Builder.defineMacro(Name: "__OPTIMIZE__" ); |
1042 | if (LangOpts.OptimizeSize) |
1043 | Builder.defineMacro(Name: "__OPTIMIZE_SIZE__" ); |
1044 | |
1045 | if (LangOpts.FastMath) |
1046 | Builder.defineMacro(Name: "__FAST_MATH__" ); |
1047 | |
1048 | // Initialize target-specific preprocessor defines. |
1049 | |
1050 | // __BYTE_ORDER__ was added in GCC 4.6. It's analogous |
1051 | // to the macro __BYTE_ORDER (no trailing underscores) |
1052 | // from glibc's <endian.h> header. |
1053 | // We don't support the PDP-11 as a target, but include |
1054 | // the define so it can still be compared against. |
1055 | Builder.defineMacro(Name: "__ORDER_LITTLE_ENDIAN__" , Value: "1234" ); |
1056 | Builder.defineMacro(Name: "__ORDER_BIG_ENDIAN__" , Value: "4321" ); |
1057 | Builder.defineMacro(Name: "__ORDER_PDP_ENDIAN__" , Value: "3412" ); |
1058 | if (TI.isBigEndian()) { |
1059 | Builder.defineMacro(Name: "__BYTE_ORDER__" , Value: "__ORDER_BIG_ENDIAN__" ); |
1060 | Builder.defineMacro(Name: "__BIG_ENDIAN__" ); |
1061 | } else { |
1062 | Builder.defineMacro(Name: "__BYTE_ORDER__" , Value: "__ORDER_LITTLE_ENDIAN__" ); |
1063 | Builder.defineMacro(Name: "__LITTLE_ENDIAN__" ); |
1064 | } |
1065 | |
1066 | if (TI.getPointerWidth(AddrSpace: LangAS::Default) == 64 && TI.getLongWidth() == 64 && |
1067 | TI.getIntWidth() == 32) { |
1068 | Builder.defineMacro(Name: "_LP64" ); |
1069 | Builder.defineMacro(Name: "__LP64__" ); |
1070 | } |
1071 | |
1072 | if (TI.getPointerWidth(AddrSpace: LangAS::Default) == 32 && TI.getLongWidth() == 32 && |
1073 | TI.getIntWidth() == 32) { |
1074 | Builder.defineMacro(Name: "_ILP32" ); |
1075 | Builder.defineMacro(Name: "__ILP32__" ); |
1076 | } |
1077 | |
1078 | // Define type sizing macros based on the target properties. |
1079 | assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far" ); |
1080 | Builder.defineMacro(Name: "__CHAR_BIT__" , Value: Twine(TI.getCharWidth())); |
1081 | |
1082 | Builder.defineMacro(Name: "__BOOL_WIDTH__" , Value: Twine(TI.getBoolWidth())); |
1083 | Builder.defineMacro(Name: "__SHRT_WIDTH__" , Value: Twine(TI.getShortWidth())); |
1084 | Builder.defineMacro(Name: "__INT_WIDTH__" , Value: Twine(TI.getIntWidth())); |
1085 | Builder.defineMacro(Name: "__LONG_WIDTH__" , Value: Twine(TI.getLongWidth())); |
1086 | Builder.defineMacro(Name: "__LLONG_WIDTH__" , Value: Twine(TI.getLongLongWidth())); |
1087 | |
1088 | size_t BitIntMaxWidth = TI.getMaxBitIntWidth(); |
1089 | assert(BitIntMaxWidth <= llvm::IntegerType::MAX_INT_BITS && |
1090 | "Target defined a max bit width larger than LLVM can support!" ); |
1091 | assert(BitIntMaxWidth >= TI.getLongLongWidth() && |
1092 | "Target defined a max bit width smaller than the C standard allows!" ); |
1093 | Builder.defineMacro(Name: "__BITINT_MAXWIDTH__" , Value: Twine(BitIntMaxWidth)); |
1094 | |
1095 | DefineTypeSize(MacroName: "__SCHAR_MAX__" , Ty: TargetInfo::SignedChar, TI, Builder); |
1096 | DefineTypeSize(MacroName: "__SHRT_MAX__" , Ty: TargetInfo::SignedShort, TI, Builder); |
1097 | DefineTypeSize(MacroName: "__INT_MAX__" , Ty: TargetInfo::SignedInt, TI, Builder); |
1098 | DefineTypeSize(MacroName: "__LONG_MAX__" , Ty: TargetInfo::SignedLong, TI, Builder); |
1099 | DefineTypeSize(MacroName: "__LONG_LONG_MAX__" , Ty: TargetInfo::SignedLongLong, TI, Builder); |
1100 | DefineTypeSizeAndWidth(Prefix: "__WCHAR" , Ty: TI.getWCharType(), TI, Builder); |
1101 | DefineTypeSizeAndWidth(Prefix: "__WINT" , Ty: TI.getWIntType(), TI, Builder); |
1102 | DefineTypeSizeAndWidth(Prefix: "__INTMAX" , Ty: TI.getIntMaxType(), TI, Builder); |
1103 | DefineTypeSizeAndWidth(Prefix: "__SIZE" , Ty: TI.getSizeType(), TI, Builder); |
1104 | |
1105 | DefineTypeSizeAndWidth(Prefix: "__UINTMAX" , Ty: TI.getUIntMaxType(), TI, Builder); |
1106 | DefineTypeSizeAndWidth(Prefix: "__PTRDIFF" , Ty: TI.getPtrDiffType(AddrSpace: LangAS::Default), TI, |
1107 | Builder); |
1108 | DefineTypeSizeAndWidth(Prefix: "__INTPTR" , Ty: TI.getIntPtrType(), TI, Builder); |
1109 | DefineTypeSizeAndWidth(Prefix: "__UINTPTR" , Ty: TI.getUIntPtrType(), TI, Builder); |
1110 | |
1111 | DefineTypeSizeof(MacroName: "__SIZEOF_DOUBLE__" , BitWidth: TI.getDoubleWidth(), TI, Builder); |
1112 | DefineTypeSizeof(MacroName: "__SIZEOF_FLOAT__" , BitWidth: TI.getFloatWidth(), TI, Builder); |
1113 | DefineTypeSizeof(MacroName: "__SIZEOF_INT__" , BitWidth: TI.getIntWidth(), TI, Builder); |
1114 | DefineTypeSizeof(MacroName: "__SIZEOF_LONG__" , BitWidth: TI.getLongWidth(), TI, Builder); |
1115 | DefineTypeSizeof(MacroName: "__SIZEOF_LONG_DOUBLE__" ,BitWidth: TI.getLongDoubleWidth(),TI,Builder); |
1116 | DefineTypeSizeof(MacroName: "__SIZEOF_LONG_LONG__" , BitWidth: TI.getLongLongWidth(), TI, Builder); |
1117 | DefineTypeSizeof(MacroName: "__SIZEOF_POINTER__" , BitWidth: TI.getPointerWidth(AddrSpace: LangAS::Default), |
1118 | TI, Builder); |
1119 | DefineTypeSizeof(MacroName: "__SIZEOF_SHORT__" , BitWidth: TI.getShortWidth(), TI, Builder); |
1120 | DefineTypeSizeof(MacroName: "__SIZEOF_PTRDIFF_T__" , |
1121 | BitWidth: TI.getTypeWidth(T: TI.getPtrDiffType(AddrSpace: LangAS::Default)), TI, |
1122 | Builder); |
1123 | DefineTypeSizeof(MacroName: "__SIZEOF_SIZE_T__" , |
1124 | BitWidth: TI.getTypeWidth(T: TI.getSizeType()), TI, Builder); |
1125 | DefineTypeSizeof(MacroName: "__SIZEOF_WCHAR_T__" , |
1126 | BitWidth: TI.getTypeWidth(T: TI.getWCharType()), TI, Builder); |
1127 | DefineTypeSizeof(MacroName: "__SIZEOF_WINT_T__" , |
1128 | BitWidth: TI.getTypeWidth(T: TI.getWIntType()), TI, Builder); |
1129 | if (TI.hasInt128Type()) |
1130 | DefineTypeSizeof(MacroName: "__SIZEOF_INT128__" , BitWidth: 128, TI, Builder); |
1131 | |
1132 | DefineType(MacroName: "__INTMAX_TYPE__" , Ty: TI.getIntMaxType(), Builder); |
1133 | DefineFmt(LangOpts, Prefix: "__INTMAX" , Ty: TI.getIntMaxType(), TI, Builder); |
1134 | Builder.defineMacro(Name: "__INTMAX_C_SUFFIX__" , |
1135 | Value: TI.getTypeConstantSuffix(T: TI.getIntMaxType())); |
1136 | DefineType(MacroName: "__UINTMAX_TYPE__" , Ty: TI.getUIntMaxType(), Builder); |
1137 | DefineFmt(LangOpts, Prefix: "__UINTMAX" , Ty: TI.getUIntMaxType(), TI, Builder); |
1138 | Builder.defineMacro(Name: "__UINTMAX_C_SUFFIX__" , |
1139 | Value: TI.getTypeConstantSuffix(T: TI.getUIntMaxType())); |
1140 | DefineType(MacroName: "__PTRDIFF_TYPE__" , Ty: TI.getPtrDiffType(AddrSpace: LangAS::Default), Builder); |
1141 | DefineFmt(LangOpts, Prefix: "__PTRDIFF" , Ty: TI.getPtrDiffType(AddrSpace: LangAS::Default), TI, |
1142 | Builder); |
1143 | DefineType(MacroName: "__INTPTR_TYPE__" , Ty: TI.getIntPtrType(), Builder); |
1144 | DefineFmt(LangOpts, Prefix: "__INTPTR" , Ty: TI.getIntPtrType(), TI, Builder); |
1145 | DefineType(MacroName: "__SIZE_TYPE__" , Ty: TI.getSizeType(), Builder); |
1146 | DefineFmt(LangOpts, Prefix: "__SIZE" , Ty: TI.getSizeType(), TI, Builder); |
1147 | DefineType(MacroName: "__WCHAR_TYPE__" , Ty: TI.getWCharType(), Builder); |
1148 | DefineType(MacroName: "__WINT_TYPE__" , Ty: TI.getWIntType(), Builder); |
1149 | DefineTypeSizeAndWidth(Prefix: "__SIG_ATOMIC" , Ty: TI.getSigAtomicType(), TI, Builder); |
1150 | DefineType(MacroName: "__CHAR16_TYPE__" , Ty: TI.getChar16Type(), Builder); |
1151 | DefineType(MacroName: "__CHAR32_TYPE__" , Ty: TI.getChar32Type(), Builder); |
1152 | |
1153 | DefineType(MacroName: "__UINTPTR_TYPE__" , Ty: TI.getUIntPtrType(), Builder); |
1154 | DefineFmt(LangOpts, Prefix: "__UINTPTR" , Ty: TI.getUIntPtrType(), TI, Builder); |
1155 | |
1156 | // The C standard requires the width of uintptr_t and intptr_t to be the same, |
1157 | // per 7.20.2.4p1. Same for intmax_t and uintmax_t, per 7.20.2.5p1. |
1158 | assert(TI.getTypeWidth(TI.getUIntPtrType()) == |
1159 | TI.getTypeWidth(TI.getIntPtrType()) && |
1160 | "uintptr_t and intptr_t have different widths?" ); |
1161 | assert(TI.getTypeWidth(TI.getUIntMaxType()) == |
1162 | TI.getTypeWidth(TI.getIntMaxType()) && |
1163 | "uintmax_t and intmax_t have different widths?" ); |
1164 | |
1165 | if (LangOpts.FixedPoint) { |
1166 | // Each unsigned type has the same width as their signed type. |
1167 | DefineFixedPointMacros(TI, Builder, TypeName: "SFRACT" , Suffix: "HR" , Width: TI.getShortFractWidth(), |
1168 | Scale: TI.getShortFractScale(), /*Signed=*/true); |
1169 | DefineFixedPointMacros(TI, Builder, TypeName: "USFRACT" , Suffix: "UHR" , |
1170 | Width: TI.getShortFractWidth(), |
1171 | Scale: TI.getUnsignedShortFractScale(), /*Signed=*/false); |
1172 | DefineFixedPointMacros(TI, Builder, TypeName: "FRACT" , Suffix: "R" , Width: TI.getFractWidth(), |
1173 | Scale: TI.getFractScale(), /*Signed=*/true); |
1174 | DefineFixedPointMacros(TI, Builder, TypeName: "UFRACT" , Suffix: "UR" , Width: TI.getFractWidth(), |
1175 | Scale: TI.getUnsignedFractScale(), /*Signed=*/false); |
1176 | DefineFixedPointMacros(TI, Builder, TypeName: "LFRACT" , Suffix: "LR" , Width: TI.getLongFractWidth(), |
1177 | Scale: TI.getLongFractScale(), /*Signed=*/true); |
1178 | DefineFixedPointMacros(TI, Builder, TypeName: "ULFRACT" , Suffix: "ULR" , |
1179 | Width: TI.getLongFractWidth(), |
1180 | Scale: TI.getUnsignedLongFractScale(), /*Signed=*/false); |
1181 | DefineFixedPointMacros(TI, Builder, TypeName: "SACCUM" , Suffix: "HK" , Width: TI.getShortAccumWidth(), |
1182 | Scale: TI.getShortAccumScale(), /*Signed=*/true); |
1183 | DefineFixedPointMacros(TI, Builder, TypeName: "USACCUM" , Suffix: "UHK" , |
1184 | Width: TI.getShortAccumWidth(), |
1185 | Scale: TI.getUnsignedShortAccumScale(), /*Signed=*/false); |
1186 | DefineFixedPointMacros(TI, Builder, TypeName: "ACCUM" , Suffix: "K" , Width: TI.getAccumWidth(), |
1187 | Scale: TI.getAccumScale(), /*Signed=*/true); |
1188 | DefineFixedPointMacros(TI, Builder, TypeName: "UACCUM" , Suffix: "UK" , Width: TI.getAccumWidth(), |
1189 | Scale: TI.getUnsignedAccumScale(), /*Signed=*/false); |
1190 | DefineFixedPointMacros(TI, Builder, TypeName: "LACCUM" , Suffix: "LK" , Width: TI.getLongAccumWidth(), |
1191 | Scale: TI.getLongAccumScale(), /*Signed=*/true); |
1192 | DefineFixedPointMacros(TI, Builder, TypeName: "ULACCUM" , Suffix: "ULK" , |
1193 | Width: TI.getLongAccumWidth(), |
1194 | Scale: TI.getUnsignedLongAccumScale(), /*Signed=*/false); |
1195 | |
1196 | Builder.defineMacro(Name: "__SACCUM_IBIT__" , Value: Twine(TI.getShortAccumIBits())); |
1197 | Builder.defineMacro(Name: "__USACCUM_IBIT__" , |
1198 | Value: Twine(TI.getUnsignedShortAccumIBits())); |
1199 | Builder.defineMacro(Name: "__ACCUM_IBIT__" , Value: Twine(TI.getAccumIBits())); |
1200 | Builder.defineMacro(Name: "__UACCUM_IBIT__" , Value: Twine(TI.getUnsignedAccumIBits())); |
1201 | Builder.defineMacro(Name: "__LACCUM_IBIT__" , Value: Twine(TI.getLongAccumIBits())); |
1202 | Builder.defineMacro(Name: "__ULACCUM_IBIT__" , |
1203 | Value: Twine(TI.getUnsignedLongAccumIBits())); |
1204 | } |
1205 | |
1206 | if (TI.hasFloat16Type()) |
1207 | DefineFloatMacros(Builder, Prefix: "FLT16" , Sem: &TI.getHalfFormat(), Ext: "F16" ); |
1208 | DefineFloatMacros(Builder, Prefix: "FLT" , Sem: &TI.getFloatFormat(), Ext: "F" ); |
1209 | DefineFloatMacros(Builder, Prefix: "DBL" , Sem: &TI.getDoubleFormat(), Ext: "" ); |
1210 | DefineFloatMacros(Builder, Prefix: "LDBL" , Sem: &TI.getLongDoubleFormat(), Ext: "L" ); |
1211 | |
1212 | // Define a __POINTER_WIDTH__ macro for stdint.h. |
1213 | Builder.defineMacro(Name: "__POINTER_WIDTH__" , |
1214 | Value: Twine((int)TI.getPointerWidth(AddrSpace: LangAS::Default))); |
1215 | |
1216 | // Define __BIGGEST_ALIGNMENT__ to be compatible with gcc. |
1217 | Builder.defineMacro(Name: "__BIGGEST_ALIGNMENT__" , |
1218 | Value: Twine(TI.getSuitableAlign() / TI.getCharWidth()) ); |
1219 | |
1220 | if (!LangOpts.CharIsSigned) |
1221 | Builder.defineMacro(Name: "__CHAR_UNSIGNED__" ); |
1222 | |
1223 | if (!TargetInfo::isTypeSigned(T: TI.getWCharType())) |
1224 | Builder.defineMacro(Name: "__WCHAR_UNSIGNED__" ); |
1225 | |
1226 | if (!TargetInfo::isTypeSigned(T: TI.getWIntType())) |
1227 | Builder.defineMacro(Name: "__WINT_UNSIGNED__" ); |
1228 | |
1229 | // Define exact-width integer types for stdint.h |
1230 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::SignedChar, TI, Builder); |
1231 | |
1232 | if (TI.getShortWidth() > TI.getCharWidth()) |
1233 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::SignedShort, TI, Builder); |
1234 | |
1235 | if (TI.getIntWidth() > TI.getShortWidth()) |
1236 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::SignedInt, TI, Builder); |
1237 | |
1238 | if (TI.getLongWidth() > TI.getIntWidth()) |
1239 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::SignedLong, TI, Builder); |
1240 | |
1241 | if (TI.getLongLongWidth() > TI.getLongWidth()) |
1242 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::SignedLongLong, TI, Builder); |
1243 | |
1244 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::UnsignedChar, TI, Builder); |
1245 | DefineExactWidthIntTypeSize(Ty: TargetInfo::UnsignedChar, TI, Builder); |
1246 | DefineExactWidthIntTypeSize(Ty: TargetInfo::SignedChar, TI, Builder); |
1247 | |
1248 | if (TI.getShortWidth() > TI.getCharWidth()) { |
1249 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::UnsignedShort, TI, Builder); |
1250 | DefineExactWidthIntTypeSize(Ty: TargetInfo::UnsignedShort, TI, Builder); |
1251 | DefineExactWidthIntTypeSize(Ty: TargetInfo::SignedShort, TI, Builder); |
1252 | } |
1253 | |
1254 | if (TI.getIntWidth() > TI.getShortWidth()) { |
1255 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::UnsignedInt, TI, Builder); |
1256 | DefineExactWidthIntTypeSize(Ty: TargetInfo::UnsignedInt, TI, Builder); |
1257 | DefineExactWidthIntTypeSize(Ty: TargetInfo::SignedInt, TI, Builder); |
1258 | } |
1259 | |
1260 | if (TI.getLongWidth() > TI.getIntWidth()) { |
1261 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::UnsignedLong, TI, Builder); |
1262 | DefineExactWidthIntTypeSize(Ty: TargetInfo::UnsignedLong, TI, Builder); |
1263 | DefineExactWidthIntTypeSize(Ty: TargetInfo::SignedLong, TI, Builder); |
1264 | } |
1265 | |
1266 | if (TI.getLongLongWidth() > TI.getLongWidth()) { |
1267 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::UnsignedLongLong, TI, |
1268 | Builder); |
1269 | DefineExactWidthIntTypeSize(Ty: TargetInfo::UnsignedLongLong, TI, Builder); |
1270 | DefineExactWidthIntTypeSize(Ty: TargetInfo::SignedLongLong, TI, Builder); |
1271 | } |
1272 | |
1273 | DefineLeastWidthIntType(LangOpts, TypeWidth: 8, IsSigned: true, TI, Builder); |
1274 | DefineLeastWidthIntType(LangOpts, TypeWidth: 8, IsSigned: false, TI, Builder); |
1275 | DefineLeastWidthIntType(LangOpts, TypeWidth: 16, IsSigned: true, TI, Builder); |
1276 | DefineLeastWidthIntType(LangOpts, TypeWidth: 16, IsSigned: false, TI, Builder); |
1277 | DefineLeastWidthIntType(LangOpts, TypeWidth: 32, IsSigned: true, TI, Builder); |
1278 | DefineLeastWidthIntType(LangOpts, TypeWidth: 32, IsSigned: false, TI, Builder); |
1279 | DefineLeastWidthIntType(LangOpts, TypeWidth: 64, IsSigned: true, TI, Builder); |
1280 | DefineLeastWidthIntType(LangOpts, TypeWidth: 64, IsSigned: false, TI, Builder); |
1281 | |
1282 | DefineFastIntType(LangOpts, TypeWidth: 8, IsSigned: true, TI, Builder); |
1283 | DefineFastIntType(LangOpts, TypeWidth: 8, IsSigned: false, TI, Builder); |
1284 | DefineFastIntType(LangOpts, TypeWidth: 16, IsSigned: true, TI, Builder); |
1285 | DefineFastIntType(LangOpts, TypeWidth: 16, IsSigned: false, TI, Builder); |
1286 | DefineFastIntType(LangOpts, TypeWidth: 32, IsSigned: true, TI, Builder); |
1287 | DefineFastIntType(LangOpts, TypeWidth: 32, IsSigned: false, TI, Builder); |
1288 | DefineFastIntType(LangOpts, TypeWidth: 64, IsSigned: true, TI, Builder); |
1289 | DefineFastIntType(LangOpts, TypeWidth: 64, IsSigned: false, TI, Builder); |
1290 | |
1291 | Builder.defineMacro(Name: "__USER_LABEL_PREFIX__" , Value: TI.getUserLabelPrefix()); |
1292 | |
1293 | if (!LangOpts.MathErrno) |
1294 | Builder.defineMacro(Name: "__NO_MATH_ERRNO__" ); |
1295 | |
1296 | if (LangOpts.FastMath || LangOpts.FiniteMathOnly) |
1297 | Builder.defineMacro(Name: "__FINITE_MATH_ONLY__" , Value: "1" ); |
1298 | else |
1299 | Builder.defineMacro(Name: "__FINITE_MATH_ONLY__" , Value: "0" ); |
1300 | |
1301 | if (LangOpts.GNUCVersion) { |
1302 | if (LangOpts.GNUInline || LangOpts.CPlusPlus) |
1303 | Builder.defineMacro(Name: "__GNUC_GNU_INLINE__" ); |
1304 | else |
1305 | Builder.defineMacro(Name: "__GNUC_STDC_INLINE__" ); |
1306 | |
1307 | // The value written by __atomic_test_and_set. |
1308 | // FIXME: This is target-dependent. |
1309 | Builder.defineMacro(Name: "__GCC_ATOMIC_TEST_AND_SET_TRUEVAL" , Value: "1" ); |
1310 | } |
1311 | |
1312 | auto addLockFreeMacros = [&](const llvm::Twine &Prefix) { |
1313 | // Used by libc++ and libstdc++ to implement ATOMIC_<foo>_LOCK_FREE. |
1314 | #define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \ |
1315 | Builder.defineMacro(Prefix + #TYPE "_LOCK_FREE", \ |
1316 | getLockFreeValue(TI.get##Type##Width(), TI)); |
1317 | DEFINE_LOCK_FREE_MACRO(BOOL, Bool); |
1318 | DEFINE_LOCK_FREE_MACRO(CHAR, Char); |
1319 | if (LangOpts.Char8) |
1320 | DEFINE_LOCK_FREE_MACRO(CHAR8_T, Char); // Treat char8_t like char. |
1321 | DEFINE_LOCK_FREE_MACRO(CHAR16_T, Char16); |
1322 | DEFINE_LOCK_FREE_MACRO(CHAR32_T, Char32); |
1323 | DEFINE_LOCK_FREE_MACRO(WCHAR_T, WChar); |
1324 | DEFINE_LOCK_FREE_MACRO(SHORT, Short); |
1325 | DEFINE_LOCK_FREE_MACRO(INT, Int); |
1326 | DEFINE_LOCK_FREE_MACRO(LONG, Long); |
1327 | DEFINE_LOCK_FREE_MACRO(LLONG, LongLong); |
1328 | Builder.defineMacro( |
1329 | Name: Prefix + "POINTER_LOCK_FREE" , |
1330 | Value: getLockFreeValue(TypeWidth: TI.getPointerWidth(AddrSpace: LangAS::Default), TI)); |
1331 | #undef DEFINE_LOCK_FREE_MACRO |
1332 | }; |
1333 | addLockFreeMacros("__CLANG_ATOMIC_" ); |
1334 | if (LangOpts.GNUCVersion) |
1335 | addLockFreeMacros("__GCC_ATOMIC_" ); |
1336 | |
1337 | if (LangOpts.NoInlineDefine) |
1338 | Builder.defineMacro(Name: "__NO_INLINE__" ); |
1339 | |
1340 | if (unsigned PICLevel = LangOpts.PICLevel) { |
1341 | Builder.defineMacro(Name: "__PIC__" , Value: Twine(PICLevel)); |
1342 | Builder.defineMacro(Name: "__pic__" , Value: Twine(PICLevel)); |
1343 | if (LangOpts.PIE) { |
1344 | Builder.defineMacro(Name: "__PIE__" , Value: Twine(PICLevel)); |
1345 | Builder.defineMacro(Name: "__pie__" , Value: Twine(PICLevel)); |
1346 | } |
1347 | } |
1348 | |
1349 | // Macros to control C99 numerics and <float.h> |
1350 | Builder.defineMacro(Name: "__FLT_RADIX__" , Value: "2" ); |
1351 | Builder.defineMacro(Name: "__DECIMAL_DIG__" , Value: "__LDBL_DECIMAL_DIG__" ); |
1352 | |
1353 | if (LangOpts.getStackProtector() == LangOptions::SSPOn) |
1354 | Builder.defineMacro(Name: "__SSP__" ); |
1355 | else if (LangOpts.getStackProtector() == LangOptions::SSPStrong) |
1356 | Builder.defineMacro(Name: "__SSP_STRONG__" , Value: "2" ); |
1357 | else if (LangOpts.getStackProtector() == LangOptions::SSPReq) |
1358 | Builder.defineMacro(Name: "__SSP_ALL__" , Value: "3" ); |
1359 | |
1360 | if (PPOpts.SetUpStaticAnalyzer) |
1361 | Builder.defineMacro(Name: "__clang_analyzer__" ); |
1362 | |
1363 | if (LangOpts.FastRelaxedMath) |
1364 | Builder.defineMacro(Name: "__FAST_RELAXED_MATH__" ); |
1365 | |
1366 | if (FEOpts.ProgramAction == frontend::RewriteObjC || |
1367 | LangOpts.getGC() != LangOptions::NonGC) { |
1368 | Builder.defineMacro(Name: "__weak" , Value: "__attribute__((objc_gc(weak)))" ); |
1369 | Builder.defineMacro(Name: "__strong" , Value: "__attribute__((objc_gc(strong)))" ); |
1370 | Builder.defineMacro(Name: "__autoreleasing" , Value: "" ); |
1371 | Builder.defineMacro(Name: "__unsafe_unretained" , Value: "" ); |
1372 | } else if (LangOpts.ObjC) { |
1373 | Builder.defineMacro(Name: "__weak" , Value: "__attribute__((objc_ownership(weak)))" ); |
1374 | Builder.defineMacro(Name: "__strong" , Value: "__attribute__((objc_ownership(strong)))" ); |
1375 | Builder.defineMacro(Name: "__autoreleasing" , |
1376 | Value: "__attribute__((objc_ownership(autoreleasing)))" ); |
1377 | Builder.defineMacro(Name: "__unsafe_unretained" , |
1378 | Value: "__attribute__((objc_ownership(none)))" ); |
1379 | } |
1380 | |
1381 | // On Darwin, there are __double_underscored variants of the type |
1382 | // nullability qualifiers. |
1383 | if (TI.getTriple().isOSDarwin()) { |
1384 | Builder.defineMacro(Name: "__nonnull" , Value: "_Nonnull" ); |
1385 | Builder.defineMacro(Name: "__null_unspecified" , Value: "_Null_unspecified" ); |
1386 | Builder.defineMacro(Name: "__nullable" , Value: "_Nullable" ); |
1387 | } |
1388 | |
1389 | // Add a macro to differentiate between regular iOS/tvOS/watchOS targets and |
1390 | // the corresponding simulator targets. |
1391 | if (TI.getTriple().isOSDarwin() && TI.getTriple().isSimulatorEnvironment()) |
1392 | Builder.defineMacro(Name: "__APPLE_EMBEDDED_SIMULATOR__" , Value: "1" ); |
1393 | |
1394 | // OpenMP definition |
1395 | // OpenMP 2.2: |
1396 | // In implementations that support a preprocessor, the _OPENMP |
1397 | // macro name is defined to have the decimal value yyyymm where |
1398 | // yyyy and mm are the year and the month designations of the |
1399 | // version of the OpenMP API that the implementation support. |
1400 | if (!LangOpts.OpenMPSimd) { |
1401 | switch (LangOpts.OpenMP) { |
1402 | case 0: |
1403 | break; |
1404 | case 31: |
1405 | Builder.defineMacro(Name: "_OPENMP" , Value: "201107" ); |
1406 | break; |
1407 | case 40: |
1408 | Builder.defineMacro(Name: "_OPENMP" , Value: "201307" ); |
1409 | break; |
1410 | case 45: |
1411 | Builder.defineMacro(Name: "_OPENMP" , Value: "201511" ); |
1412 | break; |
1413 | case 50: |
1414 | Builder.defineMacro(Name: "_OPENMP" , Value: "201811" ); |
1415 | break; |
1416 | case 52: |
1417 | Builder.defineMacro(Name: "_OPENMP" , Value: "202111" ); |
1418 | break; |
1419 | default: // case 51: |
1420 | // Default version is OpenMP 5.1 |
1421 | Builder.defineMacro(Name: "_OPENMP" , Value: "202011" ); |
1422 | break; |
1423 | } |
1424 | } |
1425 | |
1426 | // CUDA device path compilaton |
1427 | if (LangOpts.CUDAIsDevice && !LangOpts.HIP) { |
1428 | // The CUDA_ARCH value is set for the GPU target specified in the NVPTX |
1429 | // backend's target defines. |
1430 | Builder.defineMacro(Name: "__CUDA_ARCH__" ); |
1431 | } |
1432 | |
1433 | // We need to communicate this to our CUDA/HIP header wrapper, which in turn |
1434 | // informs the proper CUDA/HIP headers of this choice. |
1435 | if (LangOpts.GPUDeviceApproxTranscendentals) |
1436 | Builder.defineMacro(Name: "__CLANG_GPU_APPROX_TRANSCENDENTALS__" ); |
1437 | |
1438 | // Define a macro indicating that the source file is being compiled with a |
1439 | // SYCL device compiler which doesn't produce host binary. |
1440 | if (LangOpts.SYCLIsDevice) { |
1441 | Builder.defineMacro(Name: "__SYCL_DEVICE_ONLY__" , Value: "1" ); |
1442 | } |
1443 | |
1444 | // OpenCL definitions. |
1445 | if (LangOpts.OpenCL) { |
1446 | InitializeOpenCLFeatureTestMacros(TI, Opts: LangOpts, Builder); |
1447 | |
1448 | if (TI.getTriple().isSPIR() || TI.getTriple().isSPIRV()) |
1449 | Builder.defineMacro(Name: "__IMAGE_SUPPORT__" ); |
1450 | } |
1451 | |
1452 | if (TI.hasInt128Type() && LangOpts.CPlusPlus && LangOpts.GNUMode) { |
1453 | // For each extended integer type, g++ defines a macro mapping the |
1454 | // index of the type (0 in this case) in some list of extended types |
1455 | // to the type. |
1456 | Builder.defineMacro(Name: "__GLIBCXX_TYPE_INT_N_0" , Value: "__int128" ); |
1457 | Builder.defineMacro(Name: "__GLIBCXX_BITSIZE_INT_N_0" , Value: "128" ); |
1458 | } |
1459 | |
1460 | // ELF targets define __ELF__ |
1461 | if (TI.getTriple().isOSBinFormatELF()) |
1462 | Builder.defineMacro(Name: "__ELF__" ); |
1463 | |
1464 | // Target OS macro definitions. |
1465 | if (PPOpts.DefineTargetOSMacros) { |
1466 | const llvm::Triple &Triple = TI.getTriple(); |
1467 | #define TARGET_OS(Name, Predicate) \ |
1468 | Builder.defineMacro(#Name, (Predicate) ? "1" : "0"); |
1469 | #include "clang/Basic/TargetOSMacros.def" |
1470 | #undef TARGET_OS |
1471 | } |
1472 | |
1473 | // Get other target #defines. |
1474 | TI.getTargetDefines(Opts: LangOpts, Builder); |
1475 | } |
1476 | |
1477 | static void InitializePGOProfileMacros(const CodeGenOptions &CodeGenOpts, |
1478 | MacroBuilder &Builder) { |
1479 | if (CodeGenOpts.hasProfileInstr()) |
1480 | Builder.defineMacro(Name: "__LLVM_INSTR_PROFILE_GENERATE" ); |
1481 | |
1482 | if (CodeGenOpts.hasProfileIRUse() || CodeGenOpts.hasProfileClangUse()) |
1483 | Builder.defineMacro(Name: "__LLVM_INSTR_PROFILE_USE" ); |
1484 | } |
1485 | |
1486 | /// InitializePreprocessor - Initialize the preprocessor getting it and the |
1487 | /// environment ready to process a single file. |
1488 | void clang::InitializePreprocessor(Preprocessor &PP, |
1489 | const PreprocessorOptions &InitOpts, |
1490 | const PCHContainerReader &PCHContainerRdr, |
1491 | const FrontendOptions &FEOpts, |
1492 | const CodeGenOptions &CodeGenOpts) { |
1493 | const LangOptions &LangOpts = PP.getLangOpts(); |
1494 | std::string PredefineBuffer; |
1495 | PredefineBuffer.reserve(res: 4080); |
1496 | llvm::raw_string_ostream Predefines(PredefineBuffer); |
1497 | MacroBuilder Builder(Predefines); |
1498 | |
1499 | // Emit line markers for various builtin sections of the file. The 3 here |
1500 | // marks <built-in> as being a system header, which suppresses warnings when |
1501 | // the same macro is defined multiple times. |
1502 | Builder.append(Str: "# 1 \"<built-in>\" 3" ); |
1503 | |
1504 | // Install things like __POWERPC__, __GNUC__, etc into the macro table. |
1505 | if (InitOpts.UsePredefines) { |
1506 | // FIXME: This will create multiple definitions for most of the predefined |
1507 | // macros. This is not the right way to handle this. |
1508 | if ((LangOpts.CUDA || LangOpts.OpenMPIsTargetDevice || |
1509 | LangOpts.SYCLIsDevice) && |
1510 | PP.getAuxTargetInfo()) |
1511 | InitializePredefinedMacros(TI: *PP.getAuxTargetInfo(), LangOpts, FEOpts, |
1512 | PPOpts: PP.getPreprocessorOpts(), Builder); |
1513 | |
1514 | InitializePredefinedMacros(TI: PP.getTargetInfo(), LangOpts, FEOpts, |
1515 | PPOpts: PP.getPreprocessorOpts(), Builder); |
1516 | |
1517 | // Install definitions to make Objective-C++ ARC work well with various |
1518 | // C++ Standard Library implementations. |
1519 | if (LangOpts.ObjC && LangOpts.CPlusPlus && |
1520 | (LangOpts.ObjCAutoRefCount || LangOpts.ObjCWeak)) { |
1521 | switch (InitOpts.ObjCXXARCStandardLibrary) { |
1522 | case ARCXX_nolib: |
1523 | case ARCXX_libcxx: |
1524 | break; |
1525 | |
1526 | case ARCXX_libstdcxx: |
1527 | AddObjCXXARCLibstdcxxDefines(LangOpts, Builder); |
1528 | break; |
1529 | } |
1530 | } |
1531 | } |
1532 | |
1533 | // Even with predefines off, some macros are still predefined. |
1534 | // These should all be defined in the preprocessor according to the |
1535 | // current language configuration. |
1536 | InitializeStandardPredefinedMacros(TI: PP.getTargetInfo(), LangOpts: PP.getLangOpts(), |
1537 | FEOpts, Builder); |
1538 | |
1539 | // The PGO instrumentation profile macros are driven by options |
1540 | // -fprofile[-instr]-generate/-fcs-profile-generate/-fprofile[-instr]-use, |
1541 | // hence they are not guarded by InitOpts.UsePredefines. |
1542 | InitializePGOProfileMacros(CodeGenOpts, Builder); |
1543 | |
1544 | // Add on the predefines from the driver. Wrap in a #line directive to report |
1545 | // that they come from the command line. |
1546 | Builder.append(Str: "# 1 \"<command line>\" 1" ); |
1547 | |
1548 | // Process #define's and #undef's in the order they are given. |
1549 | for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) { |
1550 | if (InitOpts.Macros[i].second) // isUndef |
1551 | Builder.undefineMacro(Name: InitOpts.Macros[i].first); |
1552 | else |
1553 | DefineBuiltinMacro(Builder, Macro: InitOpts.Macros[i].first, |
1554 | Diags&: PP.getDiagnostics()); |
1555 | } |
1556 | |
1557 | // Exit the command line and go back to <built-in> (2 is LC_LEAVE). |
1558 | Builder.append(Str: "# 1 \"<built-in>\" 2" ); |
1559 | |
1560 | // If -imacros are specified, include them now. These are processed before |
1561 | // any -include directives. |
1562 | for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i) |
1563 | AddImplicitIncludeMacros(Builder, File: InitOpts.MacroIncludes[i]); |
1564 | |
1565 | // Process -include-pch/-include-pth directives. |
1566 | if (!InitOpts.ImplicitPCHInclude.empty()) |
1567 | AddImplicitIncludePCH(Builder, PP, PCHContainerRdr, |
1568 | ImplicitIncludePCH: InitOpts.ImplicitPCHInclude); |
1569 | |
1570 | // Process -include directives. |
1571 | for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) { |
1572 | const std::string &Path = InitOpts.Includes[i]; |
1573 | AddImplicitInclude(Builder, File: Path); |
1574 | } |
1575 | |
1576 | // Instruct the preprocessor to skip the preamble. |
1577 | PP.setSkipMainFilePreamble(Bytes: InitOpts.PrecompiledPreambleBytes.first, |
1578 | StartOfLine: InitOpts.PrecompiledPreambleBytes.second); |
1579 | |
1580 | // Copy PredefinedBuffer into the Preprocessor. |
1581 | PP.setPredefines(std::move(PredefineBuffer)); |
1582 | } |
1583 | |