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