1 //===--- InitPreprocessor.cpp - PP initialization code. ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the clang::InitializePreprocessor function.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Basic/FileManager.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/PTHManager.h"
25 #include "clang/Lex/Preprocessor.h"
26 #include "clang/Lex/PreprocessorOptions.h"
27 #include "clang/Serialization/ASTReader.h"
28 #include "llvm/ADT/APFloat.h"
29 using namespace clang;
31 static bool MacroBodyEndsInBackslash(StringRef MacroBody) {
32 while (!MacroBody.empty() && isWhitespace(MacroBody.back()))
33 MacroBody = MacroBody.drop_back();
34 return !MacroBody.empty() && MacroBody.back() == '\\';
37 // Append a #define line to Buf for Macro. Macro should be of the form XXX,
38 // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
39 // "#define XXX Y z W". To get a #define with no value, use "XXX=".
40 static void DefineBuiltinMacro(MacroBuilder &Builder, StringRef Macro,
41 DiagnosticsEngine &Diags) {
42 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
43 StringRef MacroName = MacroPair.first;
44 StringRef MacroBody = MacroPair.second;
45 if (MacroName.size() != Macro.size()) {
46 // Per GCC -D semantics, the macro ends at \n if it exists.
47 StringRef::size_type End = MacroBody.find_first_of("\n\r");
48 if (End != StringRef::npos)
49 Diags.Report(diag::warn_fe_macro_contains_embedded_newline)
51 MacroBody = MacroBody.substr(0, End);
52 // We handle macro bodies which end in a backslash by appending an extra
53 // backslash+newline. This makes sure we don't accidentally treat the
54 // backslash as a line continuation marker.
55 if (MacroBodyEndsInBackslash(MacroBody))
56 Builder.defineMacro(MacroName, Twine(MacroBody) + "\\\n");
58 Builder.defineMacro(MacroName, MacroBody);
60 // Push "macroname 1".
61 Builder.defineMacro(Macro);
65 /// AddImplicitInclude - Add an implicit \#include of the specified file to the
66 /// predefines buffer.
67 /// As these includes are generated by -include arguments the header search
68 /// logic is going to search relatively to the current working directory.
69 static void AddImplicitInclude(MacroBuilder &Builder, StringRef File) {
70 Builder.append(Twine("#include \"") + File + "\"");
73 static void AddImplicitIncludeMacros(MacroBuilder &Builder, StringRef File) {
74 Builder.append(Twine("#__include_macros \"") + File + "\"");
75 // Marker token to stop the __include_macros fetch loop.
76 Builder.append("##"); // ##?
79 /// AddImplicitIncludePTH - Add an implicit \#include using the original file
80 /// used to generate a PTH cache.
81 static void AddImplicitIncludePTH(MacroBuilder &Builder, Preprocessor &PP,
82 StringRef ImplicitIncludePTH) {
83 PTHManager *P = PP.getPTHManager();
84 // Null check 'P' in the corner case where it couldn't be created.
85 const char *OriginalFile = P ? P->getOriginalSourceFile() : nullptr;
88 PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header)
89 << ImplicitIncludePTH;
93 AddImplicitInclude(Builder, OriginalFile);
96 /// Add an implicit \#include using the original file used to generate
98 static void AddImplicitIncludePCH(MacroBuilder &Builder, Preprocessor &PP,
99 const PCHContainerReader &PCHContainerRdr,
100 StringRef ImplicitIncludePCH) {
101 std::string OriginalFile =
102 ASTReader::getOriginalSourceFile(ImplicitIncludePCH, PP.getFileManager(),
103 PCHContainerRdr, PP.getDiagnostics());
104 if (OriginalFile.empty())
107 AddImplicitInclude(Builder, OriginalFile);
110 /// PickFP - This is used to pick a value based on the FP semantics of the
111 /// specified FP model.
112 template <typename T>
113 static T PickFP(const llvm::fltSemantics *Sem, T IEEEHalfVal, T IEEESingleVal,
114 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
116 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEhalf())
118 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle())
119 return IEEESingleVal;
120 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble())
121 return IEEEDoubleVal;
122 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended())
123 return X87DoubleExtendedVal;
124 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble())
125 return PPCDoubleDoubleVal;
126 assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad());
130 static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix,
131 const llvm::fltSemantics *Sem, StringRef Ext) {
132 const char *DenormMin, *Epsilon, *Max, *Min;
133 DenormMin = PickFP(Sem, "5.9604644775390625e-8", "1.40129846e-45",
134 "4.9406564584124654e-324", "3.64519953188247460253e-4951",
135 "4.94065645841246544176568792868221e-324",
136 "6.47517511943802511092443895822764655e-4966");
137 int Digits = PickFP(Sem, 3, 6, 15, 18, 31, 33);
138 int DecimalDigits = PickFP(Sem, 5, 9, 17, 21, 33, 36);
139 Epsilon = PickFP(Sem, "9.765625e-4", "1.19209290e-7",
140 "2.2204460492503131e-16", "1.08420217248550443401e-19",
141 "4.94065645841246544176568792868221e-324",
142 "1.92592994438723585305597794258492732e-34");
143 int MantissaDigits = PickFP(Sem, 11, 24, 53, 64, 106, 113);
144 int Min10Exp = PickFP(Sem, -13, -37, -307, -4931, -291, -4931);
145 int Max10Exp = PickFP(Sem, 4, 38, 308, 4932, 308, 4932);
146 int MinExp = PickFP(Sem, -14, -125, -1021, -16381, -968, -16381);
147 int MaxExp = PickFP(Sem, 15, 128, 1024, 16384, 1024, 16384);
148 Min = PickFP(Sem, "6.103515625e-5", "1.17549435e-38", "2.2250738585072014e-308",
149 "3.36210314311209350626e-4932",
150 "2.00416836000897277799610805135016e-292",
151 "3.36210314311209350626267781732175260e-4932");
152 Max = PickFP(Sem, "6.5504e+4", "3.40282347e+38", "1.7976931348623157e+308",
153 "1.18973149535723176502e+4932",
154 "1.79769313486231580793728971405301e+308",
155 "1.18973149535723176508575932662800702e+4932");
157 SmallString<32> DefPrefix;
162 Builder.defineMacro(DefPrefix + "DENORM_MIN__", Twine(DenormMin)+Ext);
163 Builder.defineMacro(DefPrefix + "HAS_DENORM__");
164 Builder.defineMacro(DefPrefix + "DIG__", Twine(Digits));
165 Builder.defineMacro(DefPrefix + "DECIMAL_DIG__", Twine(DecimalDigits));
166 Builder.defineMacro(DefPrefix + "EPSILON__", Twine(Epsilon)+Ext);
167 Builder.defineMacro(DefPrefix + "HAS_INFINITY__");
168 Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__");
169 Builder.defineMacro(DefPrefix + "MANT_DIG__", Twine(MantissaDigits));
171 Builder.defineMacro(DefPrefix + "MAX_10_EXP__", Twine(Max10Exp));
172 Builder.defineMacro(DefPrefix + "MAX_EXP__", Twine(MaxExp));
173 Builder.defineMacro(DefPrefix + "MAX__", Twine(Max)+Ext);
175 Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+Twine(Min10Exp)+")");
176 Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+Twine(MinExp)+")");
177 Builder.defineMacro(DefPrefix + "MIN__", Twine(Min)+Ext);
181 /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
182 /// named MacroName with the max value for a type with width 'TypeWidth' a
183 /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
184 static void DefineTypeSize(const Twine &MacroName, unsigned TypeWidth,
185 StringRef ValSuffix, bool isSigned,
186 MacroBuilder &Builder) {
187 llvm::APInt MaxVal = isSigned ? llvm::APInt::getSignedMaxValue(TypeWidth)
188 : llvm::APInt::getMaxValue(TypeWidth);
189 Builder.defineMacro(MacroName, MaxVal.toString(10, isSigned) + ValSuffix);
192 /// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
193 /// the width, suffix, and signedness of the given type
194 static void DefineTypeSize(const Twine &MacroName, TargetInfo::IntType Ty,
195 const TargetInfo &TI, MacroBuilder &Builder) {
196 DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
197 TI.isTypeSigned(Ty), Builder);
200 static void DefineFmt(const Twine &Prefix, TargetInfo::IntType Ty,
201 const TargetInfo &TI, MacroBuilder &Builder) {
202 bool IsSigned = TI.isTypeSigned(Ty);
203 StringRef FmtModifier = TI.getTypeFormatModifier(Ty);
204 for (const char *Fmt = IsSigned ? "di" : "ouxX"; *Fmt; ++Fmt) {
205 Builder.defineMacro(Prefix + "_FMT" + Twine(*Fmt) + "__",
206 Twine("\"") + FmtModifier + Twine(*Fmt) + "\"");
210 static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty,
211 MacroBuilder &Builder) {
212 Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty));
215 static void DefineTypeWidth(StringRef MacroName, TargetInfo::IntType Ty,
216 const TargetInfo &TI, MacroBuilder &Builder) {
217 Builder.defineMacro(MacroName, Twine(TI.getTypeWidth(Ty)));
220 static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth,
221 const TargetInfo &TI, MacroBuilder &Builder) {
222 Builder.defineMacro(MacroName,
223 Twine(BitWidth / TI.getCharWidth()));
226 static void DefineExactWidthIntType(TargetInfo::IntType Ty,
227 const TargetInfo &TI,
228 MacroBuilder &Builder) {
229 int TypeWidth = TI.getTypeWidth(Ty);
230 bool IsSigned = TI.isTypeSigned(Ty);
232 // Use the target specified int64 type, when appropriate, so that [u]int64_t
233 // ends up being defined in terms of the correct type.
235 Ty = IsSigned ? TI.getInt64Type() : TI.getUInt64Type();
237 const char *Prefix = IsSigned ? "__INT" : "__UINT";
239 DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
240 DefineFmt(Prefix + Twine(TypeWidth), Ty, TI, Builder);
242 StringRef ConstSuffix(TI.getTypeConstantSuffix(Ty));
243 Builder.defineMacro(Prefix + Twine(TypeWidth) + "_C_SUFFIX__", ConstSuffix);
246 static void DefineExactWidthIntTypeSize(TargetInfo::IntType Ty,
247 const TargetInfo &TI,
248 MacroBuilder &Builder) {
249 int TypeWidth = TI.getTypeWidth(Ty);
250 bool IsSigned = TI.isTypeSigned(Ty);
252 // Use the target specified int64 type, when appropriate, so that [u]int64_t
253 // ends up being defined in terms of the correct type.
255 Ty = IsSigned ? TI.getInt64Type() : TI.getUInt64Type();
257 const char *Prefix = IsSigned ? "__INT" : "__UINT";
258 DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder);
261 static void DefineLeastWidthIntType(unsigned TypeWidth, bool IsSigned,
262 const TargetInfo &TI,
263 MacroBuilder &Builder) {
264 TargetInfo::IntType Ty = TI.getLeastIntTypeByWidth(TypeWidth, IsSigned);
265 if (Ty == TargetInfo::NoInt)
268 const char *Prefix = IsSigned ? "__INT_LEAST" : "__UINT_LEAST";
269 DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
270 DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder);
271 DefineFmt(Prefix + Twine(TypeWidth), Ty, TI, Builder);
274 static void DefineFastIntType(unsigned TypeWidth, bool IsSigned,
275 const TargetInfo &TI, MacroBuilder &Builder) {
276 // stdint.h currently defines the fast int types as equivalent to the least
278 TargetInfo::IntType Ty = TI.getLeastIntTypeByWidth(TypeWidth, IsSigned);
279 if (Ty == TargetInfo::NoInt)
282 const char *Prefix = IsSigned ? "__INT_FAST" : "__UINT_FAST";
283 DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
284 DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder);
286 DefineFmt(Prefix + Twine(TypeWidth), Ty, TI, Builder);
290 /// Get the value the ATOMIC_*_LOCK_FREE macro should have for a type with
291 /// the specified properties.
292 static const char *getLockFreeValue(unsigned TypeWidth, unsigned TypeAlign,
293 unsigned InlineWidth) {
294 // Fully-aligned, power-of-2 sizes no larger than the inline
295 // width will be inlined as lock-free operations.
296 if (TypeWidth == TypeAlign && (TypeWidth & (TypeWidth - 1)) == 0 &&
297 TypeWidth <= InlineWidth)
298 return "2"; // "always lock free"
299 // We cannot be certain what operations the lib calls might be
300 // able to implement as lock-free on future processors.
301 return "1"; // "sometimes lock free"
304 /// Add definitions required for a smooth interaction between
305 /// Objective-C++ automated reference counting and libstdc++ (4.2).
306 static void AddObjCXXARCLibstdcxxDefines(const LangOptions &LangOpts,
307 MacroBuilder &Builder) {
308 Builder.defineMacro("_GLIBCXX_PREDEFINED_OBJC_ARC_IS_SCALAR");
312 // Provide specializations for the __is_scalar type trait so that
313 // lifetime-qualified objects are not considered "scalar" types, which
314 // libstdc++ uses as an indicator of the presence of trivial copy, assign,
315 // default-construct, and destruct semantics (none of which hold for
316 // lifetime-qualified objects in ARC).
317 llvm::raw_string_ostream Out(Result);
319 Out << "namespace std {\n"
321 << "struct __true_type;\n"
322 << "struct __false_type;\n"
325 Out << "template<typename _Tp> struct __is_scalar;\n"
328 if (LangOpts.ObjCAutoRefCount) {
329 Out << "template<typename _Tp>\n"
330 << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> {\n"
331 << " enum { __value = 0 };\n"
332 << " typedef __false_type __type;\n"
337 if (LangOpts.ObjCWeak) {
338 Out << "template<typename _Tp>\n"
339 << "struct __is_scalar<__attribute__((objc_ownership(weak))) _Tp> {\n"
340 << " enum { __value = 0 };\n"
341 << " typedef __false_type __type;\n"
346 if (LangOpts.ObjCAutoRefCount) {
347 Out << "template<typename _Tp>\n"
348 << "struct __is_scalar<__attribute__((objc_ownership(autoreleasing)))"
350 << " enum { __value = 0 };\n"
351 << " typedef __false_type __type;\n"
358 Builder.append(Result);
361 static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
362 const LangOptions &LangOpts,
363 const FrontendOptions &FEOpts,
364 MacroBuilder &Builder) {
365 if (!LangOpts.MSVCCompat && !LangOpts.TraditionalCPP)
366 Builder.defineMacro("__STDC__");
367 if (LangOpts.Freestanding)
368 Builder.defineMacro("__STDC_HOSTED__", "0");
370 Builder.defineMacro("__STDC_HOSTED__");
372 if (!LangOpts.CPlusPlus) {
374 Builder.defineMacro("__STDC_VERSION__", "201710L");
375 else if (LangOpts.C11)
376 Builder.defineMacro("__STDC_VERSION__", "201112L");
377 else if (LangOpts.C99)
378 Builder.defineMacro("__STDC_VERSION__", "199901L");
379 else if (!LangOpts.GNUMode && LangOpts.Digraphs)
380 Builder.defineMacro("__STDC_VERSION__", "199409L");
382 // FIXME: Use correct value for C++20.
383 if (LangOpts.CPlusPlus2a)
384 Builder.defineMacro("__cplusplus", "201707L");
385 // C++17 [cpp.predefined]p1:
386 // The name __cplusplus is defined to the value 201703L when compiling a
387 // C++ translation unit.
388 else if (LangOpts.CPlusPlus17)
389 Builder.defineMacro("__cplusplus", "201703L");
390 // C++1y [cpp.predefined]p1:
391 // The name __cplusplus is defined to the value 201402L when compiling a
392 // C++ translation unit.
393 else if (LangOpts.CPlusPlus14)
394 Builder.defineMacro("__cplusplus", "201402L");
395 // C++11 [cpp.predefined]p1:
396 // The name __cplusplus is defined to the value 201103L when compiling a
397 // C++ translation unit.
398 else if (LangOpts.CPlusPlus11)
399 Builder.defineMacro("__cplusplus", "201103L");
400 // C++03 [cpp.predefined]p1:
401 // The name __cplusplus is defined to the value 199711L when compiling a
402 // C++ translation unit.
404 Builder.defineMacro("__cplusplus", "199711L");
406 // C++1z [cpp.predefined]p1:
407 // An integer literal of type std::size_t whose value is the alignment
408 // guaranteed by a call to operator new(std::size_t)
410 // We provide this in all language modes, since it seems generally useful.
411 Builder.defineMacro("__STDCPP_DEFAULT_NEW_ALIGNMENT__",
412 Twine(TI.getNewAlign() / TI.getCharWidth()) +
413 TI.getTypeConstantSuffix(TI.getSizeType()));
416 // In C11 these are environment macros. In C++11 they are only defined
417 // as part of <cuchar>. To prevent breakage when mixing C and C++
418 // code, define these macros unconditionally. We can define them
419 // unconditionally, as Clang always uses UTF-16 and UTF-32 for 16-bit
420 // and 32-bit character literals.
421 Builder.defineMacro("__STDC_UTF_16__", "1");
422 Builder.defineMacro("__STDC_UTF_32__", "1");
425 Builder.defineMacro("__OBJC__");
427 // OpenCL v1.0/1.1 s6.9, v1.2/2.0 s6.10: Preprocessor Directives and Macros.
428 if (LangOpts.OpenCL) {
429 if (LangOpts.CPlusPlus) {
430 if (LangOpts.OpenCLCPlusPlusVersion == 100)
431 Builder.defineMacro("__OPENCL_CPP_VERSION__", "100");
433 llvm_unreachable("Unsupported OpenCL C++ version");
434 Builder.defineMacro("__CL_CPP_VERSION_1_0__", "100");
436 // OpenCL v1.0 and v1.1 do not have a predefined macro to indicate the
437 // language standard with which the program is compiled. __OPENCL_VERSION__
438 // is for the OpenCL version supported by the OpenCL device, which is not
439 // necessarily the language standard with which the program is compiled.
440 // A shared OpenCL header file requires a macro to indicate the language
441 // standard. As a workaround, __OPENCL_C_VERSION__ is defined for
442 // OpenCL v1.0 and v1.1.
443 switch (LangOpts.OpenCLVersion) {
445 Builder.defineMacro("__OPENCL_C_VERSION__", "100");
448 Builder.defineMacro("__OPENCL_C_VERSION__", "110");
451 Builder.defineMacro("__OPENCL_C_VERSION__", "120");
454 Builder.defineMacro("__OPENCL_C_VERSION__", "200");
457 llvm_unreachable("Unsupported OpenCL version");
459 Builder.defineMacro("CL_VERSION_1_0", "100");
460 Builder.defineMacro("CL_VERSION_1_1", "110");
461 Builder.defineMacro("CL_VERSION_1_2", "120");
462 Builder.defineMacro("CL_VERSION_2_0", "200");
464 if (TI.isLittleEndian())
465 Builder.defineMacro("__ENDIAN_LITTLE__");
467 if (LangOpts.FastRelaxedMath)
468 Builder.defineMacro("__FAST_RELAXED_MATH__");
471 // Not "standard" per se, but available even with the -undef flag.
472 if (LangOpts.AsmPreprocessor)
473 Builder.defineMacro("__ASSEMBLER__");
474 if (LangOpts.CUDA && !LangOpts.HIP)
475 Builder.defineMacro("__CUDA__");
477 Builder.defineMacro("__HIP__");
478 Builder.defineMacro("__HIPCC__");
479 if (LangOpts.CUDAIsDevice)
480 Builder.defineMacro("__HIP_DEVICE_COMPILE__");
484 /// Initialize the predefined C++ language feature test macros defined in
485 /// ISO/IEC JTC1/SC22/WG21 (C++) SD-6: "SG10 Feature Test Recommendations".
486 static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts,
487 MacroBuilder &Builder) {
490 Builder.defineMacro("__cpp_rtti", "199711L");
491 if (LangOpts.CXXExceptions)
492 Builder.defineMacro("__cpp_exceptions", "199711L");
495 if (LangOpts.CPlusPlus11) {
496 Builder.defineMacro("__cpp_unicode_characters", "200704L");
497 Builder.defineMacro("__cpp_raw_strings", "200710L");
498 Builder.defineMacro("__cpp_unicode_literals", "200710L");
499 Builder.defineMacro("__cpp_user_defined_literals", "200809L");
500 Builder.defineMacro("__cpp_lambdas", "200907L");
501 Builder.defineMacro("__cpp_constexpr",
502 LangOpts.CPlusPlus17 ? "201603L" :
503 LangOpts.CPlusPlus14 ? "201304L" : "200704");
504 Builder.defineMacro("__cpp_range_based_for",
505 LangOpts.CPlusPlus17 ? "201603L" : "200907");
506 Builder.defineMacro("__cpp_static_assert",
507 LangOpts.CPlusPlus17 ? "201411L" : "200410");
508 Builder.defineMacro("__cpp_decltype", "200707L");
509 Builder.defineMacro("__cpp_attributes", "200809L");
510 Builder.defineMacro("__cpp_rvalue_references", "200610L");
511 Builder.defineMacro("__cpp_variadic_templates", "200704L");
512 Builder.defineMacro("__cpp_initializer_lists", "200806L");
513 Builder.defineMacro("__cpp_delegating_constructors", "200604L");
514 Builder.defineMacro("__cpp_nsdmi", "200809L");
515 Builder.defineMacro("__cpp_inheriting_constructors", "201511L");
516 Builder.defineMacro("__cpp_ref_qualifiers", "200710L");
517 Builder.defineMacro("__cpp_alias_templates", "200704L");
519 if (LangOpts.ThreadsafeStatics)
520 Builder.defineMacro("__cpp_threadsafe_static_init", "200806L");
523 if (LangOpts.CPlusPlus14) {
524 Builder.defineMacro("__cpp_binary_literals", "201304L");
525 Builder.defineMacro("__cpp_digit_separators", "201309L");
526 Builder.defineMacro("__cpp_init_captures", "201304L");
527 Builder.defineMacro("__cpp_generic_lambdas", "201304L");
528 Builder.defineMacro("__cpp_decltype_auto", "201304L");
529 Builder.defineMacro("__cpp_return_type_deduction", "201304L");
530 Builder.defineMacro("__cpp_aggregate_nsdmi", "201304L");
531 Builder.defineMacro("__cpp_variable_templates", "201304L");
533 if (LangOpts.SizedDeallocation)
534 Builder.defineMacro("__cpp_sized_deallocation", "201309L");
537 if (LangOpts.CPlusPlus17) {
538 Builder.defineMacro("__cpp_hex_float", "201603L");
539 Builder.defineMacro("__cpp_inline_variables", "201606L");
540 Builder.defineMacro("__cpp_noexcept_function_type", "201510L");
541 Builder.defineMacro("__cpp_capture_star_this", "201603L");
542 Builder.defineMacro("__cpp_if_constexpr", "201606L");
543 Builder.defineMacro("__cpp_deduction_guides", "201703L");
544 Builder.defineMacro("__cpp_template_auto", "201606L"); // (old name)
545 Builder.defineMacro("__cpp_namespace_attributes", "201411L");
546 Builder.defineMacro("__cpp_enumerator_attributes", "201411L");
547 Builder.defineMacro("__cpp_nested_namespace_definitions", "201411L");
548 Builder.defineMacro("__cpp_variadic_using", "201611L");
549 Builder.defineMacro("__cpp_aggregate_bases", "201603L");
550 Builder.defineMacro("__cpp_structured_bindings", "201606L");
551 Builder.defineMacro("__cpp_nontype_template_args", "201411L");
552 Builder.defineMacro("__cpp_fold_expressions", "201603L");
553 Builder.defineMacro("__cpp_guaranteed_copy_elision", "201606L");
554 Builder.defineMacro("__cpp_nontype_template_parameter_auto", "201606L");
556 if (LangOpts.AlignedAllocation && !LangOpts.AlignedAllocationUnavailable)
557 Builder.defineMacro("__cpp_aligned_new", "201606L");
558 if (LangOpts.RelaxedTemplateTemplateArgs)
559 Builder.defineMacro("__cpp_template_template_args", "201611L");
562 if (LangOpts.ConceptsTS)
563 Builder.defineMacro("__cpp_experimental_concepts", "1L");
564 if (LangOpts.CoroutinesTS)
565 Builder.defineMacro("__cpp_coroutines", "201703L");
567 // Potential future breaking changes.
569 Builder.defineMacro("__cpp_char8_t", "201803L");
572 static void InitializePredefinedMacros(const TargetInfo &TI,
573 const LangOptions &LangOpts,
574 const FrontendOptions &FEOpts,
575 MacroBuilder &Builder) {
576 // Compiler version introspection macros.
577 Builder.defineMacro("__llvm__"); // LLVM Backend
578 Builder.defineMacro("__clang__"); // Clang Frontend
580 #define TOSTR(X) TOSTR2(X)
581 Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR));
582 Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR));
583 Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL));
586 Builder.defineMacro("__clang_version__",
587 "\"" CLANG_VERSION_STRING " "
588 + getClangFullRepositoryVersion() + "\"");
589 if (!LangOpts.MSVCCompat) {
590 // Currently claim to be compatible with GCC 4.2.1-5621, but only if we're
591 // not compiling for MSVC compatibility
592 Builder.defineMacro("__GNUC_MINOR__", "2");
593 Builder.defineMacro("__GNUC_PATCHLEVEL__", "1");
594 Builder.defineMacro("__GNUC__", "4");
595 Builder.defineMacro("__GXX_ABI_VERSION", "1002");
598 // Define macros for the C11 / C++11 memory orderings
599 Builder.defineMacro("__ATOMIC_RELAXED", "0");
600 Builder.defineMacro("__ATOMIC_CONSUME", "1");
601 Builder.defineMacro("__ATOMIC_ACQUIRE", "2");
602 Builder.defineMacro("__ATOMIC_RELEASE", "3");
603 Builder.defineMacro("__ATOMIC_ACQ_REL", "4");
604 Builder.defineMacro("__ATOMIC_SEQ_CST", "5");
606 // Define macros for the OpenCL memory scope.
607 // The values should match AtomicScopeOpenCLModel::ID enum.
609 static_cast<unsigned>(AtomicScopeOpenCLModel::WorkGroup) == 1 &&
610 static_cast<unsigned>(AtomicScopeOpenCLModel::Device) == 2 &&
611 static_cast<unsigned>(AtomicScopeOpenCLModel::AllSVMDevices) == 3 &&
612 static_cast<unsigned>(AtomicScopeOpenCLModel::SubGroup) == 4,
613 "Invalid OpenCL memory scope enum definition");
614 Builder.defineMacro("__OPENCL_MEMORY_SCOPE_WORK_ITEM", "0");
615 Builder.defineMacro("__OPENCL_MEMORY_SCOPE_WORK_GROUP", "1");
616 Builder.defineMacro("__OPENCL_MEMORY_SCOPE_DEVICE", "2");
617 Builder.defineMacro("__OPENCL_MEMORY_SCOPE_ALL_SVM_DEVICES", "3");
618 Builder.defineMacro("__OPENCL_MEMORY_SCOPE_SUB_GROUP", "4");
620 // Support for #pragma redefine_extname (Sun compatibility)
621 Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1");
623 // As sad as it is, enough software depends on the __VERSION__ for version
624 // checks that it is necessary to report 4.2.1 (the base GCC version we claim
625 // compatibility with) first.
626 Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible " +
627 Twine(getClangFullCPPVersion()) + "\"");
629 // Initialize language-specific preprocessor defines.
631 // Standard conforming mode?
632 if (!LangOpts.GNUMode && !LangOpts.MSVCCompat)
633 Builder.defineMacro("__STRICT_ANSI__");
635 if (!LangOpts.MSVCCompat && LangOpts.CPlusPlus11)
636 Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__");
638 if (LangOpts.ObjC1) {
639 if (LangOpts.ObjCRuntime.isNonFragile()) {
640 Builder.defineMacro("__OBJC2__");
642 if (LangOpts.ObjCExceptions)
643 Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS");
646 if (LangOpts.getGC() != LangOptions::NonGC)
647 Builder.defineMacro("__OBJC_GC__");
649 if (LangOpts.ObjCRuntime.isNeXTFamily())
650 Builder.defineMacro("__NEXT_RUNTIME__");
652 if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::GNUstep) {
653 auto version = LangOpts.ObjCRuntime.getVersion();
654 std::string versionString = "1";
655 // Don't rely on the tuple argument, because we can be asked to target
656 // later ABIs than we actually support, so clamp these values to those
657 // currently supported
658 if (version >= VersionTuple(2, 0))
659 Builder.defineMacro("__OBJC_GNUSTEP_RUNTIME_ABI__", "20");
661 Builder.defineMacro("__OBJC_GNUSTEP_RUNTIME_ABI__",
662 "1" + Twine(std::min(8U, version.getMinor().getValueOr(0))));
665 if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::ObjFW) {
666 VersionTuple tuple = LangOpts.ObjCRuntime.getVersion();
669 if (tuple.getMinor().hasValue())
670 minor = tuple.getMinor().getValue();
672 unsigned subminor = 0;
673 if (tuple.getSubminor().hasValue())
674 subminor = tuple.getSubminor().getValue();
676 Builder.defineMacro("__OBJFW_RUNTIME_ABI__",
677 Twine(tuple.getMajor() * 10000 + minor * 100 +
681 Builder.defineMacro("IBOutlet", "__attribute__((iboutlet))");
682 Builder.defineMacro("IBOutletCollection(ClassName)",
683 "__attribute__((iboutletcollection(ClassName)))");
684 Builder.defineMacro("IBAction", "void)__attribute__((ibaction)");
685 Builder.defineMacro("IBInspectable", "");
686 Builder.defineMacro("IB_DESIGNABLE", "");
689 // Define a macro that describes the Objective-C boolean type even for C
690 // and C++ since BOOL can be used from non Objective-C code.
691 Builder.defineMacro("__OBJC_BOOL_IS_BOOL",
692 Twine(TI.useSignedCharForObjCBool() ? "0" : "1"));
694 if (LangOpts.CPlusPlus)
695 InitializeCPlusPlusFeatureTestMacros(LangOpts, Builder);
697 // darwin_constant_cfstrings controls this. This is also dependent
698 // on other things like the runtime I believe. This is set even for C code.
699 if (!LangOpts.NoConstantCFStrings)
700 Builder.defineMacro("__CONSTANT_CFSTRINGS__");
703 Builder.defineMacro("OBJC_NEW_PROPERTIES");
705 if (LangOpts.PascalStrings)
706 Builder.defineMacro("__PASCAL_STRINGS__");
708 if (LangOpts.Blocks) {
709 Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))");
710 Builder.defineMacro("__BLOCKS__");
713 if (!LangOpts.MSVCCompat && LangOpts.Exceptions)
714 Builder.defineMacro("__EXCEPTIONS");
715 if (!LangOpts.MSVCCompat && LangOpts.RTTI)
716 Builder.defineMacro("__GXX_RTTI");
718 if (LangOpts.SjLjExceptions)
719 Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__");
720 else if (LangOpts.SEHExceptions)
721 Builder.defineMacro("__SEH__");
722 else if (LangOpts.DWARFExceptions &&
723 (TI.getTriple().isThumb() || TI.getTriple().isARM()))
724 Builder.defineMacro("__ARM_DWARF_EH__");
726 if (LangOpts.Deprecated)
727 Builder.defineMacro("__DEPRECATED");
729 if (!LangOpts.MSVCCompat && LangOpts.CPlusPlus) {
730 Builder.defineMacro("__GNUG__", "4");
731 Builder.defineMacro("__GXX_WEAK__");
732 Builder.defineMacro("__private_extern__", "extern");
735 if (LangOpts.MicrosoftExt) {
736 if (LangOpts.WChar) {
737 // wchar_t supported as a keyword.
738 Builder.defineMacro("_WCHAR_T_DEFINED");
739 Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED");
743 if (LangOpts.Optimize)
744 Builder.defineMacro("__OPTIMIZE__");
745 if (LangOpts.OptimizeSize)
746 Builder.defineMacro("__OPTIMIZE_SIZE__");
748 if (LangOpts.FastMath)
749 Builder.defineMacro("__FAST_MATH__");
751 // Initialize target-specific preprocessor defines.
753 // __BYTE_ORDER__ was added in GCC 4.6. It's analogous
754 // to the macro __BYTE_ORDER (no trailing underscores)
755 // from glibc's <endian.h> header.
756 // We don't support the PDP-11 as a target, but include
757 // the define so it can still be compared against.
758 Builder.defineMacro("__ORDER_LITTLE_ENDIAN__", "1234");
759 Builder.defineMacro("__ORDER_BIG_ENDIAN__", "4321");
760 Builder.defineMacro("__ORDER_PDP_ENDIAN__", "3412");
761 if (TI.isBigEndian()) {
762 Builder.defineMacro("__BYTE_ORDER__", "__ORDER_BIG_ENDIAN__");
763 Builder.defineMacro("__BIG_ENDIAN__");
765 Builder.defineMacro("__BYTE_ORDER__", "__ORDER_LITTLE_ENDIAN__");
766 Builder.defineMacro("__LITTLE_ENDIAN__");
769 if (TI.getPointerWidth(0) == 64 && TI.getLongWidth() == 64
770 && TI.getIntWidth() == 32) {
771 Builder.defineMacro("_LP64");
772 Builder.defineMacro("__LP64__");
775 if (TI.getPointerWidth(0) == 32 && TI.getLongWidth() == 32
776 && TI.getIntWidth() == 32) {
777 Builder.defineMacro("_ILP32");
778 Builder.defineMacro("__ILP32__");
781 // Define type sizing macros based on the target properties.
782 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
783 Builder.defineMacro("__CHAR_BIT__", Twine(TI.getCharWidth()));
785 DefineTypeSize("__SCHAR_MAX__", TargetInfo::SignedChar, TI, Builder);
786 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder);
787 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder);
788 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder);
789 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder);
790 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder);
791 DefineTypeSize("__WINT_MAX__", TI.getWIntType(), TI, Builder);
792 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder);
793 DefineTypeSize("__SIZE_MAX__", TI.getSizeType(), TI, Builder);
795 DefineTypeSize("__UINTMAX_MAX__", TI.getUIntMaxType(), TI, Builder);
796 DefineTypeSize("__PTRDIFF_MAX__", TI.getPtrDiffType(0), TI, Builder);
797 DefineTypeSize("__INTPTR_MAX__", TI.getIntPtrType(), TI, Builder);
798 DefineTypeSize("__UINTPTR_MAX__", TI.getUIntPtrType(), TI, Builder);
800 DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder);
801 DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder);
802 DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder);
803 DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder);
804 DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder);
805 DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder);
806 DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder);
807 DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder);
808 DefineTypeSizeof("__SIZEOF_PTRDIFF_T__",
809 TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder);
810 DefineTypeSizeof("__SIZEOF_SIZE_T__",
811 TI.getTypeWidth(TI.getSizeType()), TI, Builder);
812 DefineTypeSizeof("__SIZEOF_WCHAR_T__",
813 TI.getTypeWidth(TI.getWCharType()), TI, Builder);
814 DefineTypeSizeof("__SIZEOF_WINT_T__",
815 TI.getTypeWidth(TI.getWIntType()), TI, Builder);
816 if (TI.hasInt128Type())
817 DefineTypeSizeof("__SIZEOF_INT128__", 128, TI, Builder);
819 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder);
820 DefineFmt("__INTMAX", TI.getIntMaxType(), TI, Builder);
821 Builder.defineMacro("__INTMAX_C_SUFFIX__",
822 TI.getTypeConstantSuffix(TI.getIntMaxType()));
823 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder);
824 DefineFmt("__UINTMAX", TI.getUIntMaxType(), TI, Builder);
825 Builder.defineMacro("__UINTMAX_C_SUFFIX__",
826 TI.getTypeConstantSuffix(TI.getUIntMaxType()));
827 DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder);
828 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder);
829 DefineFmt("__PTRDIFF", TI.getPtrDiffType(0), TI, Builder);
830 DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder);
831 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder);
832 DefineFmt("__INTPTR", TI.getIntPtrType(), TI, Builder);
833 DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder);
834 DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder);
835 DefineFmt("__SIZE", TI.getSizeType(), TI, Builder);
836 DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder);
837 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder);
838 DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder);
839 DefineType("__WINT_TYPE__", TI.getWIntType(), Builder);
840 DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder);
841 DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder);
842 DefineTypeSize("__SIG_ATOMIC_MAX__", TI.getSigAtomicType(), TI, Builder);
843 DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder);
844 DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder);
846 DefineTypeWidth("__UINTMAX_WIDTH__", TI.getUIntMaxType(), TI, Builder);
847 DefineType("__UINTPTR_TYPE__", TI.getUIntPtrType(), Builder);
848 DefineFmt("__UINTPTR", TI.getUIntPtrType(), TI, Builder);
849 DefineTypeWidth("__UINTPTR_WIDTH__", TI.getUIntPtrType(), TI, Builder);
851 DefineFloatMacros(Builder, "FLT16", &TI.getHalfFormat(), "F16");
852 DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat(), "F");
853 DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat(), "");
854 DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat(), "L");
856 // Define a __POINTER_WIDTH__ macro for stdint.h.
857 Builder.defineMacro("__POINTER_WIDTH__",
858 Twine((int)TI.getPointerWidth(0)));
860 // Define __BIGGEST_ALIGNMENT__ to be compatible with gcc.
861 Builder.defineMacro("__BIGGEST_ALIGNMENT__",
862 Twine(TI.getSuitableAlign() / TI.getCharWidth()) );
864 if (!LangOpts.CharIsSigned)
865 Builder.defineMacro("__CHAR_UNSIGNED__");
867 if (!TargetInfo::isTypeSigned(TI.getWCharType()))
868 Builder.defineMacro("__WCHAR_UNSIGNED__");
870 if (!TargetInfo::isTypeSigned(TI.getWIntType()))
871 Builder.defineMacro("__WINT_UNSIGNED__");
873 // Define exact-width integer types for stdint.h
874 DefineExactWidthIntType(TargetInfo::SignedChar, TI, Builder);
876 if (TI.getShortWidth() > TI.getCharWidth())
877 DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder);
879 if (TI.getIntWidth() > TI.getShortWidth())
880 DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder);
882 if (TI.getLongWidth() > TI.getIntWidth())
883 DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder);
885 if (TI.getLongLongWidth() > TI.getLongWidth())
886 DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder);
888 DefineExactWidthIntType(TargetInfo::UnsignedChar, TI, Builder);
889 DefineExactWidthIntTypeSize(TargetInfo::UnsignedChar, TI, Builder);
890 DefineExactWidthIntTypeSize(TargetInfo::SignedChar, TI, Builder);
892 if (TI.getShortWidth() > TI.getCharWidth()) {
893 DefineExactWidthIntType(TargetInfo::UnsignedShort, TI, Builder);
894 DefineExactWidthIntTypeSize(TargetInfo::UnsignedShort, TI, Builder);
895 DefineExactWidthIntTypeSize(TargetInfo::SignedShort, TI, Builder);
898 if (TI.getIntWidth() > TI.getShortWidth()) {
899 DefineExactWidthIntType(TargetInfo::UnsignedInt, TI, Builder);
900 DefineExactWidthIntTypeSize(TargetInfo::UnsignedInt, TI, Builder);
901 DefineExactWidthIntTypeSize(TargetInfo::SignedInt, TI, Builder);
904 if (TI.getLongWidth() > TI.getIntWidth()) {
905 DefineExactWidthIntType(TargetInfo::UnsignedLong, TI, Builder);
906 DefineExactWidthIntTypeSize(TargetInfo::UnsignedLong, TI, Builder);
907 DefineExactWidthIntTypeSize(TargetInfo::SignedLong, TI, Builder);
910 if (TI.getLongLongWidth() > TI.getLongWidth()) {
911 DefineExactWidthIntType(TargetInfo::UnsignedLongLong, TI, Builder);
912 DefineExactWidthIntTypeSize(TargetInfo::UnsignedLongLong, TI, Builder);
913 DefineExactWidthIntTypeSize(TargetInfo::SignedLongLong, TI, Builder);
916 DefineLeastWidthIntType(8, true, TI, Builder);
917 DefineLeastWidthIntType(8, false, TI, Builder);
918 DefineLeastWidthIntType(16, true, TI, Builder);
919 DefineLeastWidthIntType(16, false, TI, Builder);
920 DefineLeastWidthIntType(32, true, TI, Builder);
921 DefineLeastWidthIntType(32, false, TI, Builder);
922 DefineLeastWidthIntType(64, true, TI, Builder);
923 DefineLeastWidthIntType(64, false, TI, Builder);
925 DefineFastIntType(8, true, TI, Builder);
926 DefineFastIntType(8, false, TI, Builder);
927 DefineFastIntType(16, true, TI, Builder);
928 DefineFastIntType(16, false, TI, Builder);
929 DefineFastIntType(32, true, TI, Builder);
930 DefineFastIntType(32, false, TI, Builder);
931 DefineFastIntType(64, true, TI, Builder);
932 DefineFastIntType(64, false, TI, Builder);
934 char UserLabelPrefix[2] = {TI.getDataLayout().getGlobalPrefix(), 0};
935 Builder.defineMacro("__USER_LABEL_PREFIX__", UserLabelPrefix);
937 if (LangOpts.FastMath || LangOpts.FiniteMathOnly)
938 Builder.defineMacro("__FINITE_MATH_ONLY__", "1");
940 Builder.defineMacro("__FINITE_MATH_ONLY__", "0");
942 if (!LangOpts.MSVCCompat) {
943 if (LangOpts.GNUInline || LangOpts.CPlusPlus)
944 Builder.defineMacro("__GNUC_GNU_INLINE__");
946 Builder.defineMacro("__GNUC_STDC_INLINE__");
948 // The value written by __atomic_test_and_set.
949 // FIXME: This is target-dependent.
950 Builder.defineMacro("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1");
953 auto addLockFreeMacros = [&](const llvm::Twine &Prefix) {
954 // Used by libc++ and libstdc++ to implement ATOMIC_<foo>_LOCK_FREE.
955 unsigned InlineWidthBits = TI.getMaxAtomicInlineWidth();
956 #define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \
957 Builder.defineMacro(Prefix + #TYPE "_LOCK_FREE", \
958 getLockFreeValue(TI.get##Type##Width(), \
959 TI.get##Type##Align(), \
961 DEFINE_LOCK_FREE_MACRO(BOOL, Bool);
962 DEFINE_LOCK_FREE_MACRO(CHAR, Char);
964 DEFINE_LOCK_FREE_MACRO(CHAR8_T, Char); // Treat char8_t like char.
965 DEFINE_LOCK_FREE_MACRO(CHAR16_T, Char16);
966 DEFINE_LOCK_FREE_MACRO(CHAR32_T, Char32);
967 DEFINE_LOCK_FREE_MACRO(WCHAR_T, WChar);
968 DEFINE_LOCK_FREE_MACRO(SHORT, Short);
969 DEFINE_LOCK_FREE_MACRO(INT, Int);
970 DEFINE_LOCK_FREE_MACRO(LONG, Long);
971 DEFINE_LOCK_FREE_MACRO(LLONG, LongLong);
972 Builder.defineMacro(Prefix + "POINTER_LOCK_FREE",
973 getLockFreeValue(TI.getPointerWidth(0),
974 TI.getPointerAlign(0),
976 #undef DEFINE_LOCK_FREE_MACRO
978 addLockFreeMacros("__CLANG_ATOMIC_");
979 if (!LangOpts.MSVCCompat)
980 addLockFreeMacros("__GCC_ATOMIC_");
982 if (LangOpts.NoInlineDefine)
983 Builder.defineMacro("__NO_INLINE__");
985 if (unsigned PICLevel = LangOpts.PICLevel) {
986 Builder.defineMacro("__PIC__", Twine(PICLevel));
987 Builder.defineMacro("__pic__", Twine(PICLevel));
989 Builder.defineMacro("__PIE__", Twine(PICLevel));
990 Builder.defineMacro("__pie__", Twine(PICLevel));
994 // Macros to control C99 numerics and <float.h>
995 Builder.defineMacro("__FLT_EVAL_METHOD__", Twine(TI.getFloatEvalMethod()));
996 Builder.defineMacro("__FLT_RADIX__", "2");
997 Builder.defineMacro("__DECIMAL_DIG__", "__LDBL_DECIMAL_DIG__");
999 if (LangOpts.getStackProtector() == LangOptions::SSPOn)
1000 Builder.defineMacro("__SSP__");
1001 else if (LangOpts.getStackProtector() == LangOptions::SSPStrong)
1002 Builder.defineMacro("__SSP_STRONG__", "2");
1003 else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
1004 Builder.defineMacro("__SSP_ALL__", "3");
1006 // Define a macro that exists only when using the static analyzer.
1007 if (FEOpts.ProgramAction == frontend::RunAnalysis)
1008 Builder.defineMacro("__clang_analyzer__");
1010 if (LangOpts.FastRelaxedMath)
1011 Builder.defineMacro("__FAST_RELAXED_MATH__");
1013 if (FEOpts.ProgramAction == frontend::RewriteObjC ||
1014 LangOpts.getGC() != LangOptions::NonGC) {
1015 Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
1016 Builder.defineMacro("__strong", "__attribute__((objc_gc(strong)))");
1017 Builder.defineMacro("__autoreleasing", "");
1018 Builder.defineMacro("__unsafe_unretained", "");
1019 } else if (LangOpts.ObjC1) {
1020 Builder.defineMacro("__weak", "__attribute__((objc_ownership(weak)))");
1021 Builder.defineMacro("__strong", "__attribute__((objc_ownership(strong)))");
1022 Builder.defineMacro("__autoreleasing",
1023 "__attribute__((objc_ownership(autoreleasing)))");
1024 Builder.defineMacro("__unsafe_unretained",
1025 "__attribute__((objc_ownership(none)))");
1028 // On Darwin, there are __double_underscored variants of the type
1029 // nullability qualifiers.
1030 if (TI.getTriple().isOSDarwin()) {
1031 Builder.defineMacro("__nonnull", "_Nonnull");
1032 Builder.defineMacro("__null_unspecified", "_Null_unspecified");
1033 Builder.defineMacro("__nullable", "_Nullable");
1036 // Add a macro to differentiate between regular iOS/tvOS/watchOS targets and
1037 // the corresponding simulator targets.
1038 if (TI.getTriple().isOSDarwin() && TI.getTriple().isSimulatorEnvironment())
1039 Builder.defineMacro("__APPLE_EMBEDDED_SIMULATOR__", "1");
1041 // OpenMP definition
1043 // In implementations that support a preprocessor, the _OPENMP
1044 // macro name is defined to have the decimal value yyyymm where
1045 // yyyy and mm are the year and the month designations of the
1046 // version of the OpenMP API that the implementation support.
1047 if (!LangOpts.OpenMPSimd) {
1048 switch (LangOpts.OpenMP) {
1052 Builder.defineMacro("_OPENMP", "201307");
1055 Builder.defineMacro("_OPENMP", "201511");
1058 // Default version is OpenMP 3.1
1059 Builder.defineMacro("_OPENMP", "201107");
1064 // CUDA device path compilaton
1065 if (LangOpts.CUDAIsDevice && !LangOpts.HIP) {
1066 // The CUDA_ARCH value is set for the GPU target specified in the NVPTX
1067 // backend's target defines.
1068 Builder.defineMacro("__CUDA_ARCH__");
1071 // We need to communicate this to our CUDA header wrapper, which in turn
1072 // informs the proper CUDA headers of this choice.
1073 if (LangOpts.CUDADeviceApproxTranscendentals || LangOpts.FastMath) {
1074 Builder.defineMacro("__CLANG_CUDA_APPROX_TRANSCENDENTALS__");
1077 // OpenCL definitions.
1078 if (LangOpts.OpenCL) {
1079 #define OPENCLEXT(Ext) \
1080 if (TI.getSupportedOpenCLOpts().isSupported(#Ext, \
1081 LangOpts.OpenCLVersion)) \
1082 Builder.defineMacro(#Ext);
1083 #include "clang/Basic/OpenCLExtensions.def"
1085 auto Arch = TI.getTriple().getArch();
1086 if (Arch == llvm::Triple::spir || Arch == llvm::Triple::spir64)
1087 Builder.defineMacro("__IMAGE_SUPPORT__");
1090 if (TI.hasInt128Type() && LangOpts.CPlusPlus && LangOpts.GNUMode) {
1091 // For each extended integer type, g++ defines a macro mapping the
1092 // index of the type (0 in this case) in some list of extended types
1094 Builder.defineMacro("__GLIBCXX_TYPE_INT_N_0", "__int128");
1095 Builder.defineMacro("__GLIBCXX_BITSIZE_INT_N_0", "128");
1098 // Get other target #defines.
1099 TI.getTargetDefines(LangOpts, Builder);
1102 /// InitializePreprocessor - Initialize the preprocessor getting it and the
1103 /// environment ready to process a single file. This returns true on error.
1105 void clang::InitializePreprocessor(
1106 Preprocessor &PP, const PreprocessorOptions &InitOpts,
1107 const PCHContainerReader &PCHContainerRdr,
1108 const FrontendOptions &FEOpts) {
1109 const LangOptions &LangOpts = PP.getLangOpts();
1110 std::string PredefineBuffer;
1111 PredefineBuffer.reserve(4080);
1112 llvm::raw_string_ostream Predefines(PredefineBuffer);
1113 MacroBuilder Builder(Predefines);
1115 // Emit line markers for various builtin sections of the file. We don't do
1116 // this in asm preprocessor mode, because "# 4" is not a line marker directive
1118 if (!PP.getLangOpts().AsmPreprocessor)
1119 Builder.append("# 1 \"<built-in>\" 3");
1121 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
1122 if (InitOpts.UsePredefines) {
1123 // FIXME: This will create multiple definitions for most of the predefined
1124 // macros. This is not the right way to handle this.
1125 if ((LangOpts.CUDA || LangOpts.OpenMPIsDevice) && PP.getAuxTargetInfo())
1126 InitializePredefinedMacros(*PP.getAuxTargetInfo(), LangOpts, FEOpts,
1129 InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, Builder);
1131 // Install definitions to make Objective-C++ ARC work well with various
1132 // C++ Standard Library implementations.
1133 if (LangOpts.ObjC1 && LangOpts.CPlusPlus &&
1134 (LangOpts.ObjCAutoRefCount || LangOpts.ObjCWeak)) {
1135 switch (InitOpts.ObjCXXARCStandardLibrary) {
1140 case ARCXX_libstdcxx:
1141 AddObjCXXARCLibstdcxxDefines(LangOpts, Builder);
1147 // Even with predefines off, some macros are still predefined.
1148 // These should all be defined in the preprocessor according to the
1149 // current language configuration.
1150 InitializeStandardPredefinedMacros(PP.getTargetInfo(), PP.getLangOpts(),
1153 // Add on the predefines from the driver. Wrap in a #line directive to report
1154 // that they come from the command line.
1155 if (!PP.getLangOpts().AsmPreprocessor)
1156 Builder.append("# 1 \"<command line>\" 1");
1158 // Process #define's and #undef's in the order they are given.
1159 for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
1160 if (InitOpts.Macros[i].second) // isUndef
1161 Builder.undefineMacro(InitOpts.Macros[i].first);
1163 DefineBuiltinMacro(Builder, InitOpts.Macros[i].first,
1164 PP.getDiagnostics());
1167 // Exit the command line and go back to <built-in> (2 is LC_LEAVE).
1168 if (!PP.getLangOpts().AsmPreprocessor)
1169 Builder.append("# 1 \"<built-in>\" 2");
1171 // If -imacros are specified, include them now. These are processed before
1172 // any -include directives.
1173 for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
1174 AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i]);
1176 // Process -include-pch/-include-pth directives.
1177 if (!InitOpts.ImplicitPCHInclude.empty())
1178 AddImplicitIncludePCH(Builder, PP, PCHContainerRdr,
1179 InitOpts.ImplicitPCHInclude);
1180 if (!InitOpts.ImplicitPTHInclude.empty())
1181 AddImplicitIncludePTH(Builder, PP, InitOpts.ImplicitPTHInclude);
1183 // Process -include directives.
1184 for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
1185 const std::string &Path = InitOpts.Includes[i];
1186 AddImplicitInclude(Builder, Path);
1189 // Instruct the preprocessor to skip the preamble.
1190 PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first,
1191 InitOpts.PrecompiledPreambleBytes.second);
1193 // Copy PredefinedBuffer into the Preprocessor.
1194 PP.setPredefines(Predefines.str());