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 = ASTReader::getOriginalSourceFile( 84 std::string(ImplicitIncludePCH), PP.getFileManager(), PCHContainerRdr, 85 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 // C++ [cpp.predefined]p1: 348 // The following macro names shall be defined by the implementation: 349 350 // -- __STDC__ 351 // [C++] Whether __STDC__ is predefined and if so, what its value is, 352 // are implementation-defined. 353 // (Removed in C++20.) 354 if (!LangOpts.MSVCCompat && !LangOpts.TraditionalCPP) 355 Builder.defineMacro("__STDC__"); 356 // -- __STDC_HOSTED__ 357 // The integer literal 1 if the implementation is a hosted 358 // implementation or the integer literal 0 if it is not. 359 if (LangOpts.Freestanding) 360 Builder.defineMacro("__STDC_HOSTED__", "0"); 361 else 362 Builder.defineMacro("__STDC_HOSTED__"); 363 364 // -- __STDC_VERSION__ 365 // [C++] Whether __STDC_VERSION__ is predefined and if so, what its 366 // value is, are implementation-defined. 367 // (Removed in C++20.) 368 if (!LangOpts.CPlusPlus) { 369 if (LangOpts.C17) 370 Builder.defineMacro("__STDC_VERSION__", "201710L"); 371 else if (LangOpts.C11) 372 Builder.defineMacro("__STDC_VERSION__", "201112L"); 373 else if (LangOpts.C99) 374 Builder.defineMacro("__STDC_VERSION__", "199901L"); 375 else if (!LangOpts.GNUMode && LangOpts.Digraphs) 376 Builder.defineMacro("__STDC_VERSION__", "199409L"); 377 } else { 378 // -- __cplusplus 379 // [C++20] The integer literal 202002L. 380 if (LangOpts.CPlusPlus20) 381 Builder.defineMacro("__cplusplus", "202002L"); 382 // [C++17] The integer literal 201703L. 383 else if (LangOpts.CPlusPlus17) 384 Builder.defineMacro("__cplusplus", "201703L"); 385 // [C++14] The name __cplusplus is defined to the value 201402L when 386 // compiling a C++ translation unit. 387 else if (LangOpts.CPlusPlus14) 388 Builder.defineMacro("__cplusplus", "201402L"); 389 // [C++11] The name __cplusplus is defined to the value 201103L when 390 // compiling a C++ translation unit. 391 else if (LangOpts.CPlusPlus11) 392 Builder.defineMacro("__cplusplus", "201103L"); 393 // [C++03] The name __cplusplus is defined to the value 199711L when 394 // compiling a C++ translation unit. 395 else 396 Builder.defineMacro("__cplusplus", "199711L"); 397 398 // -- __STDCPP_DEFAULT_NEW_ALIGNMENT__ 399 // [C++17] An integer literal of type std::size_t whose value is the 400 // alignment guaranteed by a call to operator new(std::size_t) 401 // 402 // We provide this in all language modes, since it seems generally useful. 403 Builder.defineMacro("__STDCPP_DEFAULT_NEW_ALIGNMENT__", 404 Twine(TI.getNewAlign() / TI.getCharWidth()) + 405 TI.getTypeConstantSuffix(TI.getSizeType())); 406 } 407 408 // In C11 these are environment macros. In C++11 they are only defined 409 // as part of <cuchar>. To prevent breakage when mixing C and C++ 410 // code, define these macros unconditionally. We can define them 411 // unconditionally, as Clang always uses UTF-16 and UTF-32 for 16-bit 412 // and 32-bit character literals. 413 Builder.defineMacro("__STDC_UTF_16__", "1"); 414 Builder.defineMacro("__STDC_UTF_32__", "1"); 415 416 if (LangOpts.ObjC) 417 Builder.defineMacro("__OBJC__"); 418 419 // OpenCL v1.0/1.1 s6.9, v1.2/2.0 s6.10: Preprocessor Directives and Macros. 420 if (LangOpts.OpenCL) { 421 if (LangOpts.CPlusPlus) { 422 if (LangOpts.OpenCLCPlusPlusVersion == 100) 423 Builder.defineMacro("__OPENCL_CPP_VERSION__", "100"); 424 else 425 llvm_unreachable("Unsupported C++ version for OpenCL"); 426 Builder.defineMacro("__CL_CPP_VERSION_1_0__", "100"); 427 } else { 428 // OpenCL v1.0 and v1.1 do not have a predefined macro to indicate the 429 // language standard with which the program is compiled. __OPENCL_VERSION__ 430 // is for the OpenCL version supported by the OpenCL device, which is not 431 // necessarily the language standard with which the program is compiled. 432 // A shared OpenCL header file requires a macro to indicate the language 433 // standard. As a workaround, __OPENCL_C_VERSION__ is defined for 434 // OpenCL v1.0 and v1.1. 435 switch (LangOpts.OpenCLVersion) { 436 case 100: 437 Builder.defineMacro("__OPENCL_C_VERSION__", "100"); 438 break; 439 case 110: 440 Builder.defineMacro("__OPENCL_C_VERSION__", "110"); 441 break; 442 case 120: 443 Builder.defineMacro("__OPENCL_C_VERSION__", "120"); 444 break; 445 case 200: 446 Builder.defineMacro("__OPENCL_C_VERSION__", "200"); 447 break; 448 default: 449 llvm_unreachable("Unsupported OpenCL version"); 450 } 451 } 452 Builder.defineMacro("CL_VERSION_1_0", "100"); 453 Builder.defineMacro("CL_VERSION_1_1", "110"); 454 Builder.defineMacro("CL_VERSION_1_2", "120"); 455 Builder.defineMacro("CL_VERSION_2_0", "200"); 456 457 if (TI.isLittleEndian()) 458 Builder.defineMacro("__ENDIAN_LITTLE__"); 459 460 if (LangOpts.FastRelaxedMath) 461 Builder.defineMacro("__FAST_RELAXED_MATH__"); 462 } 463 464 if (LangOpts.SYCL) { 465 // SYCL Version is set to a value when building SYCL applications 466 if (LangOpts.SYCLVersion == 2017) 467 Builder.defineMacro("CL_SYCL_LANGUAGE_VERSION", "121"); 468 } 469 470 // Not "standard" per se, but available even with the -undef flag. 471 if (LangOpts.AsmPreprocessor) 472 Builder.defineMacro("__ASSEMBLER__"); 473 if (LangOpts.CUDA && !LangOpts.HIP) 474 Builder.defineMacro("__CUDA__"); 475 if (LangOpts.HIP) { 476 Builder.defineMacro("__HIP__"); 477 Builder.defineMacro("__HIPCC__"); 478 if (LangOpts.CUDAIsDevice) 479 Builder.defineMacro("__HIP_DEVICE_COMPILE__"); 480 } 481 } 482 483 /// Initialize the predefined C++ language feature test macros defined in 484 /// ISO/IEC JTC1/SC22/WG21 (C++) SD-6: "SG10 Feature Test Recommendations". 485 static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts, 486 MacroBuilder &Builder) { 487 // C++98 features. 488 if (LangOpts.RTTI) 489 Builder.defineMacro("__cpp_rtti", "199711L"); 490 if (LangOpts.CXXExceptions) 491 Builder.defineMacro("__cpp_exceptions", "199711L"); 492 493 // C++11 features. 494 if (LangOpts.CPlusPlus11) { 495 Builder.defineMacro("__cpp_unicode_characters", "200704L"); 496 Builder.defineMacro("__cpp_raw_strings", "200710L"); 497 Builder.defineMacro("__cpp_unicode_literals", "200710L"); 498 Builder.defineMacro("__cpp_user_defined_literals", "200809L"); 499 Builder.defineMacro("__cpp_lambdas", "200907L"); 500 Builder.defineMacro("__cpp_constexpr", 501 LangOpts.CPlusPlus20 ? "201907L" : 502 LangOpts.CPlusPlus17 ? "201603L" : 503 LangOpts.CPlusPlus14 ? "201304L" : "200704"); 504 Builder.defineMacro("__cpp_constexpr_in_decltype", "201711L"); 505 Builder.defineMacro("__cpp_range_based_for", 506 LangOpts.CPlusPlus17 ? "201603L" : "200907"); 507 Builder.defineMacro("__cpp_static_assert", 508 LangOpts.CPlusPlus17 ? "201411L" : "200410"); 509 Builder.defineMacro("__cpp_decltype", "200707L"); 510 Builder.defineMacro("__cpp_attributes", "200809L"); 511 Builder.defineMacro("__cpp_rvalue_references", "200610L"); 512 Builder.defineMacro("__cpp_variadic_templates", "200704L"); 513 Builder.defineMacro("__cpp_initializer_lists", "200806L"); 514 Builder.defineMacro("__cpp_delegating_constructors", "200604L"); 515 Builder.defineMacro("__cpp_nsdmi", "200809L"); 516 Builder.defineMacro("__cpp_inheriting_constructors", "201511L"); 517 Builder.defineMacro("__cpp_ref_qualifiers", "200710L"); 518 Builder.defineMacro("__cpp_alias_templates", "200704L"); 519 } 520 if (LangOpts.ThreadsafeStatics) 521 Builder.defineMacro("__cpp_threadsafe_static_init", "200806L"); 522 523 // C++14 features. 524 if (LangOpts.CPlusPlus14) { 525 Builder.defineMacro("__cpp_binary_literals", "201304L"); 526 Builder.defineMacro("__cpp_digit_separators", "201309L"); 527 Builder.defineMacro("__cpp_init_captures", 528 LangOpts.CPlusPlus20 ? "201803L" : "201304L"); 529 Builder.defineMacro("__cpp_generic_lambdas", 530 LangOpts.CPlusPlus20 ? "201707L" : "201304L"); 531 Builder.defineMacro("__cpp_decltype_auto", "201304L"); 532 Builder.defineMacro("__cpp_return_type_deduction", "201304L"); 533 Builder.defineMacro("__cpp_aggregate_nsdmi", "201304L"); 534 Builder.defineMacro("__cpp_variable_templates", "201304L"); 535 } 536 if (LangOpts.SizedDeallocation) 537 Builder.defineMacro("__cpp_sized_deallocation", "201309L"); 538 539 // C++17 features. 540 if (LangOpts.CPlusPlus17) { 541 Builder.defineMacro("__cpp_hex_float", "201603L"); 542 Builder.defineMacro("__cpp_inline_variables", "201606L"); 543 Builder.defineMacro("__cpp_noexcept_function_type", "201510L"); 544 Builder.defineMacro("__cpp_capture_star_this", "201603L"); 545 Builder.defineMacro("__cpp_if_constexpr", "201606L"); 546 Builder.defineMacro("__cpp_deduction_guides", "201703L"); // (not latest) 547 Builder.defineMacro("__cpp_template_auto", "201606L"); // (old name) 548 Builder.defineMacro("__cpp_namespace_attributes", "201411L"); 549 Builder.defineMacro("__cpp_enumerator_attributes", "201411L"); 550 Builder.defineMacro("__cpp_nested_namespace_definitions", "201411L"); 551 Builder.defineMacro("__cpp_variadic_using", "201611L"); 552 Builder.defineMacro("__cpp_aggregate_bases", "201603L"); 553 Builder.defineMacro("__cpp_structured_bindings", "201606L"); 554 Builder.defineMacro("__cpp_nontype_template_args", 555 "201411L"); // (not latest) 556 Builder.defineMacro("__cpp_fold_expressions", "201603L"); 557 Builder.defineMacro("__cpp_guaranteed_copy_elision", "201606L"); 558 Builder.defineMacro("__cpp_nontype_template_parameter_auto", "201606L"); 559 } 560 if (LangOpts.AlignedAllocation && !LangOpts.AlignedAllocationUnavailable) 561 Builder.defineMacro("__cpp_aligned_new", "201606L"); 562 if (LangOpts.RelaxedTemplateTemplateArgs) 563 Builder.defineMacro("__cpp_template_template_args", "201611L"); 564 565 // C++20 features. 566 if (LangOpts.CPlusPlus20) { 567 //Builder.defineMacro("__cpp_aggregate_paren_init", "201902L"); 568 Builder.defineMacro("__cpp_concepts", "201907L"); 569 Builder.defineMacro("__cpp_conditional_explicit", "201806L"); 570 //Builder.defineMacro("__cpp_consteval", "201811L"); 571 Builder.defineMacro("__cpp_constexpr_dynamic_alloc", "201907L"); 572 Builder.defineMacro("__cpp_constinit", "201907L"); 573 //Builder.defineMacro("__cpp_coroutines", "201902L"); 574 Builder.defineMacro("__cpp_designated_initializers", "201707L"); 575 Builder.defineMacro("__cpp_impl_three_way_comparison", "201907L"); 576 //Builder.defineMacro("__cpp_modules", "201907L"); 577 //Builder.defineMacro("__cpp_using_enum", "201907L"); 578 } 579 if (LangOpts.Char8) 580 Builder.defineMacro("__cpp_char8_t", "201811L"); 581 Builder.defineMacro("__cpp_impl_destroying_delete", "201806L"); 582 583 // TS features. 584 if (LangOpts.Coroutines) 585 Builder.defineMacro("__cpp_coroutines", "201703L"); 586 } 587 588 static void InitializePredefinedMacros(const TargetInfo &TI, 589 const LangOptions &LangOpts, 590 const FrontendOptions &FEOpts, 591 const PreprocessorOptions &PPOpts, 592 MacroBuilder &Builder) { 593 // Compiler version introspection macros. 594 Builder.defineMacro("__llvm__"); // LLVM Backend 595 Builder.defineMacro("__clang__"); // Clang Frontend 596 #define TOSTR2(X) #X 597 #define TOSTR(X) TOSTR2(X) 598 Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR)); 599 Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR)); 600 Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL)); 601 #undef TOSTR 602 #undef TOSTR2 603 Builder.defineMacro("__clang_version__", 604 "\"" CLANG_VERSION_STRING " " 605 + getClangFullRepositoryVersion() + "\""); 606 607 if (LangOpts.GNUCVersion != 0) { 608 // Major, minor, patch, are given two decimal places each, so 4.2.1 becomes 609 // 40201. 610 unsigned GNUCMajor = LangOpts.GNUCVersion / 100 / 100; 611 unsigned GNUCMinor = LangOpts.GNUCVersion / 100 % 100; 612 unsigned GNUCPatch = LangOpts.GNUCVersion % 100; 613 Builder.defineMacro("__GNUC__", Twine(GNUCMajor)); 614 Builder.defineMacro("__GNUC_MINOR__", Twine(GNUCMinor)); 615 Builder.defineMacro("__GNUC_PATCHLEVEL__", Twine(GNUCPatch)); 616 Builder.defineMacro("__GXX_ABI_VERSION", "1002"); 617 618 if (LangOpts.CPlusPlus) { 619 Builder.defineMacro("__GNUG__", Twine(GNUCMajor)); 620 Builder.defineMacro("__GXX_WEAK__"); 621 } 622 } 623 624 // Define macros for the C11 / C++11 memory orderings 625 Builder.defineMacro("__ATOMIC_RELAXED", "0"); 626 Builder.defineMacro("__ATOMIC_CONSUME", "1"); 627 Builder.defineMacro("__ATOMIC_ACQUIRE", "2"); 628 Builder.defineMacro("__ATOMIC_RELEASE", "3"); 629 Builder.defineMacro("__ATOMIC_ACQ_REL", "4"); 630 Builder.defineMacro("__ATOMIC_SEQ_CST", "5"); 631 632 // Define macros for the OpenCL memory scope. 633 // The values should match AtomicScopeOpenCLModel::ID enum. 634 static_assert( 635 static_cast<unsigned>(AtomicScopeOpenCLModel::WorkGroup) == 1 && 636 static_cast<unsigned>(AtomicScopeOpenCLModel::Device) == 2 && 637 static_cast<unsigned>(AtomicScopeOpenCLModel::AllSVMDevices) == 3 && 638 static_cast<unsigned>(AtomicScopeOpenCLModel::SubGroup) == 4, 639 "Invalid OpenCL memory scope enum definition"); 640 Builder.defineMacro("__OPENCL_MEMORY_SCOPE_WORK_ITEM", "0"); 641 Builder.defineMacro("__OPENCL_MEMORY_SCOPE_WORK_GROUP", "1"); 642 Builder.defineMacro("__OPENCL_MEMORY_SCOPE_DEVICE", "2"); 643 Builder.defineMacro("__OPENCL_MEMORY_SCOPE_ALL_SVM_DEVICES", "3"); 644 Builder.defineMacro("__OPENCL_MEMORY_SCOPE_SUB_GROUP", "4"); 645 646 // Support for #pragma redefine_extname (Sun compatibility) 647 Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1"); 648 649 // Previously this macro was set to a string aiming to achieve compatibility 650 // with GCC 4.2.1. Now, just return the full Clang version 651 Builder.defineMacro("__VERSION__", "\"" + 652 Twine(getClangFullCPPVersion()) + "\""); 653 654 // Initialize language-specific preprocessor defines. 655 656 // Standard conforming mode? 657 if (!LangOpts.GNUMode && !LangOpts.MSVCCompat) 658 Builder.defineMacro("__STRICT_ANSI__"); 659 660 if (LangOpts.GNUCVersion && LangOpts.CPlusPlus11) 661 Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__"); 662 663 if (LangOpts.ObjC) { 664 if (LangOpts.ObjCRuntime.isNonFragile()) { 665 Builder.defineMacro("__OBJC2__"); 666 667 if (LangOpts.ObjCExceptions) 668 Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS"); 669 } 670 671 if (LangOpts.getGC() != LangOptions::NonGC) 672 Builder.defineMacro("__OBJC_GC__"); 673 674 if (LangOpts.ObjCRuntime.isNeXTFamily()) 675 Builder.defineMacro("__NEXT_RUNTIME__"); 676 677 if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::GNUstep) { 678 auto version = LangOpts.ObjCRuntime.getVersion(); 679 std::string versionString = "1"; 680 // Don't rely on the tuple argument, because we can be asked to target 681 // later ABIs than we actually support, so clamp these values to those 682 // currently supported 683 if (version >= VersionTuple(2, 0)) 684 Builder.defineMacro("__OBJC_GNUSTEP_RUNTIME_ABI__", "20"); 685 else 686 Builder.defineMacro("__OBJC_GNUSTEP_RUNTIME_ABI__", 687 "1" + Twine(std::min(8U, version.getMinor().getValueOr(0)))); 688 } 689 690 if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::ObjFW) { 691 VersionTuple tuple = LangOpts.ObjCRuntime.getVersion(); 692 693 unsigned minor = 0; 694 if (tuple.getMinor().hasValue()) 695 minor = tuple.getMinor().getValue(); 696 697 unsigned subminor = 0; 698 if (tuple.getSubminor().hasValue()) 699 subminor = tuple.getSubminor().getValue(); 700 701 Builder.defineMacro("__OBJFW_RUNTIME_ABI__", 702 Twine(tuple.getMajor() * 10000 + minor * 100 + 703 subminor)); 704 } 705 706 Builder.defineMacro("IBOutlet", "__attribute__((iboutlet))"); 707 Builder.defineMacro("IBOutletCollection(ClassName)", 708 "__attribute__((iboutletcollection(ClassName)))"); 709 Builder.defineMacro("IBAction", "void)__attribute__((ibaction)"); 710 Builder.defineMacro("IBInspectable", ""); 711 Builder.defineMacro("IB_DESIGNABLE", ""); 712 } 713 714 // Define a macro that describes the Objective-C boolean type even for C 715 // and C++ since BOOL can be used from non Objective-C code. 716 Builder.defineMacro("__OBJC_BOOL_IS_BOOL", 717 Twine(TI.useSignedCharForObjCBool() ? "0" : "1")); 718 719 if (LangOpts.CPlusPlus) 720 InitializeCPlusPlusFeatureTestMacros(LangOpts, Builder); 721 722 // darwin_constant_cfstrings controls this. This is also dependent 723 // on other things like the runtime I believe. This is set even for C code. 724 if (!LangOpts.NoConstantCFStrings) 725 Builder.defineMacro("__CONSTANT_CFSTRINGS__"); 726 727 if (LangOpts.ObjC) 728 Builder.defineMacro("OBJC_NEW_PROPERTIES"); 729 730 if (LangOpts.PascalStrings) 731 Builder.defineMacro("__PASCAL_STRINGS__"); 732 733 if (LangOpts.Blocks) { 734 Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))"); 735 Builder.defineMacro("__BLOCKS__"); 736 } 737 738 if (!LangOpts.MSVCCompat && LangOpts.Exceptions) 739 Builder.defineMacro("__EXCEPTIONS"); 740 if (LangOpts.GNUCVersion && LangOpts.RTTI) 741 Builder.defineMacro("__GXX_RTTI"); 742 743 if (LangOpts.SjLjExceptions) 744 Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__"); 745 else if (LangOpts.SEHExceptions) 746 Builder.defineMacro("__SEH__"); 747 else if (LangOpts.DWARFExceptions && 748 (TI.getTriple().isThumb() || TI.getTriple().isARM())) 749 Builder.defineMacro("__ARM_DWARF_EH__"); 750 751 if (LangOpts.Deprecated) 752 Builder.defineMacro("__DEPRECATED"); 753 754 if (!LangOpts.MSVCCompat && LangOpts.CPlusPlus) 755 Builder.defineMacro("__private_extern__", "extern"); 756 757 if (LangOpts.MicrosoftExt) { 758 if (LangOpts.WChar) { 759 // wchar_t supported as a keyword. 760 Builder.defineMacro("_WCHAR_T_DEFINED"); 761 Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED"); 762 } 763 } 764 765 if (LangOpts.Optimize) 766 Builder.defineMacro("__OPTIMIZE__"); 767 if (LangOpts.OptimizeSize) 768 Builder.defineMacro("__OPTIMIZE_SIZE__"); 769 770 if (LangOpts.FastMath) 771 Builder.defineMacro("__FAST_MATH__"); 772 773 // Initialize target-specific preprocessor defines. 774 775 // __BYTE_ORDER__ was added in GCC 4.6. It's analogous 776 // to the macro __BYTE_ORDER (no trailing underscores) 777 // from glibc's <endian.h> header. 778 // We don't support the PDP-11 as a target, but include 779 // the define so it can still be compared against. 780 Builder.defineMacro("__ORDER_LITTLE_ENDIAN__", "1234"); 781 Builder.defineMacro("__ORDER_BIG_ENDIAN__", "4321"); 782 Builder.defineMacro("__ORDER_PDP_ENDIAN__", "3412"); 783 if (TI.isBigEndian()) { 784 Builder.defineMacro("__BYTE_ORDER__", "__ORDER_BIG_ENDIAN__"); 785 Builder.defineMacro("__BIG_ENDIAN__"); 786 } else { 787 Builder.defineMacro("__BYTE_ORDER__", "__ORDER_LITTLE_ENDIAN__"); 788 Builder.defineMacro("__LITTLE_ENDIAN__"); 789 } 790 791 if (TI.getPointerWidth(0) == 64 && TI.getLongWidth() == 64 792 && TI.getIntWidth() == 32) { 793 Builder.defineMacro("_LP64"); 794 Builder.defineMacro("__LP64__"); 795 } 796 797 if (TI.getPointerWidth(0) == 32 && TI.getLongWidth() == 32 798 && TI.getIntWidth() == 32) { 799 Builder.defineMacro("_ILP32"); 800 Builder.defineMacro("__ILP32__"); 801 } 802 803 // Define type sizing macros based on the target properties. 804 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far"); 805 Builder.defineMacro("__CHAR_BIT__", Twine(TI.getCharWidth())); 806 807 DefineTypeSize("__SCHAR_MAX__", TargetInfo::SignedChar, TI, Builder); 808 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder); 809 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder); 810 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder); 811 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder); 812 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder); 813 DefineTypeSize("__WINT_MAX__", TI.getWIntType(), TI, Builder); 814 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder); 815 DefineTypeSize("__SIZE_MAX__", TI.getSizeType(), TI, Builder); 816 817 DefineTypeSize("__UINTMAX_MAX__", TI.getUIntMaxType(), TI, Builder); 818 DefineTypeSize("__PTRDIFF_MAX__", TI.getPtrDiffType(0), TI, Builder); 819 DefineTypeSize("__INTPTR_MAX__", TI.getIntPtrType(), TI, Builder); 820 DefineTypeSize("__UINTPTR_MAX__", TI.getUIntPtrType(), TI, Builder); 821 822 DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder); 823 DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder); 824 DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder); 825 DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder); 826 DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder); 827 DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder); 828 DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder); 829 DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder); 830 DefineTypeSizeof("__SIZEOF_PTRDIFF_T__", 831 TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder); 832 DefineTypeSizeof("__SIZEOF_SIZE_T__", 833 TI.getTypeWidth(TI.getSizeType()), TI, Builder); 834 DefineTypeSizeof("__SIZEOF_WCHAR_T__", 835 TI.getTypeWidth(TI.getWCharType()), TI, Builder); 836 DefineTypeSizeof("__SIZEOF_WINT_T__", 837 TI.getTypeWidth(TI.getWIntType()), TI, Builder); 838 if (TI.hasInt128Type()) 839 DefineTypeSizeof("__SIZEOF_INT128__", 128, TI, Builder); 840 841 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder); 842 DefineFmt("__INTMAX", TI.getIntMaxType(), TI, Builder); 843 Builder.defineMacro("__INTMAX_C_SUFFIX__", 844 TI.getTypeConstantSuffix(TI.getIntMaxType())); 845 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder); 846 DefineFmt("__UINTMAX", TI.getUIntMaxType(), TI, Builder); 847 Builder.defineMacro("__UINTMAX_C_SUFFIX__", 848 TI.getTypeConstantSuffix(TI.getUIntMaxType())); 849 DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder); 850 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder); 851 DefineFmt("__PTRDIFF", TI.getPtrDiffType(0), TI, Builder); 852 DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder); 853 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder); 854 DefineFmt("__INTPTR", TI.getIntPtrType(), TI, Builder); 855 DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder); 856 DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder); 857 DefineFmt("__SIZE", TI.getSizeType(), TI, Builder); 858 DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder); 859 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder); 860 DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder); 861 DefineType("__WINT_TYPE__", TI.getWIntType(), Builder); 862 DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder); 863 DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder); 864 DefineTypeSize("__SIG_ATOMIC_MAX__", TI.getSigAtomicType(), TI, Builder); 865 DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder); 866 DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder); 867 868 DefineTypeWidth("__UINTMAX_WIDTH__", TI.getUIntMaxType(), TI, Builder); 869 DefineType("__UINTPTR_TYPE__", TI.getUIntPtrType(), Builder); 870 DefineFmt("__UINTPTR", TI.getUIntPtrType(), TI, Builder); 871 DefineTypeWidth("__UINTPTR_WIDTH__", TI.getUIntPtrType(), TI, Builder); 872 873 if (TI.hasFloat16Type()) 874 DefineFloatMacros(Builder, "FLT16", &TI.getHalfFormat(), "F16"); 875 DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat(), "F"); 876 DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat(), ""); 877 DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat(), "L"); 878 879 // Define a __POINTER_WIDTH__ macro for stdint.h. 880 Builder.defineMacro("__POINTER_WIDTH__", 881 Twine((int)TI.getPointerWidth(0))); 882 883 // Define __BIGGEST_ALIGNMENT__ to be compatible with gcc. 884 Builder.defineMacro("__BIGGEST_ALIGNMENT__", 885 Twine(TI.getSuitableAlign() / TI.getCharWidth()) ); 886 887 if (!LangOpts.CharIsSigned) 888 Builder.defineMacro("__CHAR_UNSIGNED__"); 889 890 if (!TargetInfo::isTypeSigned(TI.getWCharType())) 891 Builder.defineMacro("__WCHAR_UNSIGNED__"); 892 893 if (!TargetInfo::isTypeSigned(TI.getWIntType())) 894 Builder.defineMacro("__WINT_UNSIGNED__"); 895 896 // Define exact-width integer types for stdint.h 897 DefineExactWidthIntType(TargetInfo::SignedChar, TI, Builder); 898 899 if (TI.getShortWidth() > TI.getCharWidth()) 900 DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder); 901 902 if (TI.getIntWidth() > TI.getShortWidth()) 903 DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder); 904 905 if (TI.getLongWidth() > TI.getIntWidth()) 906 DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder); 907 908 if (TI.getLongLongWidth() > TI.getLongWidth()) 909 DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder); 910 911 DefineExactWidthIntType(TargetInfo::UnsignedChar, TI, Builder); 912 DefineExactWidthIntTypeSize(TargetInfo::UnsignedChar, TI, Builder); 913 DefineExactWidthIntTypeSize(TargetInfo::SignedChar, TI, Builder); 914 915 if (TI.getShortWidth() > TI.getCharWidth()) { 916 DefineExactWidthIntType(TargetInfo::UnsignedShort, TI, Builder); 917 DefineExactWidthIntTypeSize(TargetInfo::UnsignedShort, TI, Builder); 918 DefineExactWidthIntTypeSize(TargetInfo::SignedShort, TI, Builder); 919 } 920 921 if (TI.getIntWidth() > TI.getShortWidth()) { 922 DefineExactWidthIntType(TargetInfo::UnsignedInt, TI, Builder); 923 DefineExactWidthIntTypeSize(TargetInfo::UnsignedInt, TI, Builder); 924 DefineExactWidthIntTypeSize(TargetInfo::SignedInt, TI, Builder); 925 } 926 927 if (TI.getLongWidth() > TI.getIntWidth()) { 928 DefineExactWidthIntType(TargetInfo::UnsignedLong, TI, Builder); 929 DefineExactWidthIntTypeSize(TargetInfo::UnsignedLong, TI, Builder); 930 DefineExactWidthIntTypeSize(TargetInfo::SignedLong, TI, Builder); 931 } 932 933 if (TI.getLongLongWidth() > TI.getLongWidth()) { 934 DefineExactWidthIntType(TargetInfo::UnsignedLongLong, TI, Builder); 935 DefineExactWidthIntTypeSize(TargetInfo::UnsignedLongLong, TI, Builder); 936 DefineExactWidthIntTypeSize(TargetInfo::SignedLongLong, TI, Builder); 937 } 938 939 DefineLeastWidthIntType(8, true, TI, Builder); 940 DefineLeastWidthIntType(8, false, TI, Builder); 941 DefineLeastWidthIntType(16, true, TI, Builder); 942 DefineLeastWidthIntType(16, false, TI, Builder); 943 DefineLeastWidthIntType(32, true, TI, Builder); 944 DefineLeastWidthIntType(32, false, TI, Builder); 945 DefineLeastWidthIntType(64, true, TI, Builder); 946 DefineLeastWidthIntType(64, false, TI, Builder); 947 948 DefineFastIntType(8, true, TI, Builder); 949 DefineFastIntType(8, false, TI, Builder); 950 DefineFastIntType(16, true, TI, Builder); 951 DefineFastIntType(16, false, TI, Builder); 952 DefineFastIntType(32, true, TI, Builder); 953 DefineFastIntType(32, false, TI, Builder); 954 DefineFastIntType(64, true, TI, Builder); 955 DefineFastIntType(64, false, TI, Builder); 956 957 char UserLabelPrefix[2] = {TI.getDataLayout().getGlobalPrefix(), 0}; 958 Builder.defineMacro("__USER_LABEL_PREFIX__", UserLabelPrefix); 959 960 if (LangOpts.FastMath || LangOpts.FiniteMathOnly) 961 Builder.defineMacro("__FINITE_MATH_ONLY__", "1"); 962 else 963 Builder.defineMacro("__FINITE_MATH_ONLY__", "0"); 964 965 if (LangOpts.GNUCVersion) { 966 if (LangOpts.GNUInline || LangOpts.CPlusPlus) 967 Builder.defineMacro("__GNUC_GNU_INLINE__"); 968 else 969 Builder.defineMacro("__GNUC_STDC_INLINE__"); 970 971 // The value written by __atomic_test_and_set. 972 // FIXME: This is target-dependent. 973 Builder.defineMacro("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1"); 974 } 975 976 auto addLockFreeMacros = [&](const llvm::Twine &Prefix) { 977 // Used by libc++ and libstdc++ to implement ATOMIC_<foo>_LOCK_FREE. 978 unsigned InlineWidthBits = TI.getMaxAtomicInlineWidth(); 979 #define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \ 980 Builder.defineMacro(Prefix + #TYPE "_LOCK_FREE", \ 981 getLockFreeValue(TI.get##Type##Width(), \ 982 TI.get##Type##Align(), \ 983 InlineWidthBits)); 984 DEFINE_LOCK_FREE_MACRO(BOOL, Bool); 985 DEFINE_LOCK_FREE_MACRO(CHAR, Char); 986 if (LangOpts.Char8) 987 DEFINE_LOCK_FREE_MACRO(CHAR8_T, Char); // Treat char8_t like char. 988 DEFINE_LOCK_FREE_MACRO(CHAR16_T, Char16); 989 DEFINE_LOCK_FREE_MACRO(CHAR32_T, Char32); 990 DEFINE_LOCK_FREE_MACRO(WCHAR_T, WChar); 991 DEFINE_LOCK_FREE_MACRO(SHORT, Short); 992 DEFINE_LOCK_FREE_MACRO(INT, Int); 993 DEFINE_LOCK_FREE_MACRO(LONG, Long); 994 DEFINE_LOCK_FREE_MACRO(LLONG, LongLong); 995 Builder.defineMacro(Prefix + "POINTER_LOCK_FREE", 996 getLockFreeValue(TI.getPointerWidth(0), 997 TI.getPointerAlign(0), 998 InlineWidthBits)); 999 #undef DEFINE_LOCK_FREE_MACRO 1000 }; 1001 addLockFreeMacros("__CLANG_ATOMIC_"); 1002 if (LangOpts.GNUCVersion) 1003 addLockFreeMacros("__GCC_ATOMIC_"); 1004 1005 if (LangOpts.NoInlineDefine) 1006 Builder.defineMacro("__NO_INLINE__"); 1007 1008 if (unsigned PICLevel = LangOpts.PICLevel) { 1009 Builder.defineMacro("__PIC__", Twine(PICLevel)); 1010 Builder.defineMacro("__pic__", Twine(PICLevel)); 1011 if (LangOpts.PIE) { 1012 Builder.defineMacro("__PIE__", Twine(PICLevel)); 1013 Builder.defineMacro("__pie__", Twine(PICLevel)); 1014 } 1015 } 1016 1017 // Macros to control C99 numerics and <float.h> 1018 Builder.defineMacro("__FLT_EVAL_METHOD__", Twine(TI.getFloatEvalMethod())); 1019 Builder.defineMacro("__FLT_RADIX__", "2"); 1020 Builder.defineMacro("__DECIMAL_DIG__", "__LDBL_DECIMAL_DIG__"); 1021 1022 if (LangOpts.getStackProtector() == LangOptions::SSPOn) 1023 Builder.defineMacro("__SSP__"); 1024 else if (LangOpts.getStackProtector() == LangOptions::SSPStrong) 1025 Builder.defineMacro("__SSP_STRONG__", "2"); 1026 else if (LangOpts.getStackProtector() == LangOptions::SSPReq) 1027 Builder.defineMacro("__SSP_ALL__", "3"); 1028 1029 if (PPOpts.SetUpStaticAnalyzer) 1030 Builder.defineMacro("__clang_analyzer__"); 1031 1032 if (LangOpts.FastRelaxedMath) 1033 Builder.defineMacro("__FAST_RELAXED_MATH__"); 1034 1035 if (FEOpts.ProgramAction == frontend::RewriteObjC || 1036 LangOpts.getGC() != LangOptions::NonGC) { 1037 Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))"); 1038 Builder.defineMacro("__strong", "__attribute__((objc_gc(strong)))"); 1039 Builder.defineMacro("__autoreleasing", ""); 1040 Builder.defineMacro("__unsafe_unretained", ""); 1041 } else if (LangOpts.ObjC) { 1042 Builder.defineMacro("__weak", "__attribute__((objc_ownership(weak)))"); 1043 Builder.defineMacro("__strong", "__attribute__((objc_ownership(strong)))"); 1044 Builder.defineMacro("__autoreleasing", 1045 "__attribute__((objc_ownership(autoreleasing)))"); 1046 Builder.defineMacro("__unsafe_unretained", 1047 "__attribute__((objc_ownership(none)))"); 1048 } 1049 1050 // On Darwin, there are __double_underscored variants of the type 1051 // nullability qualifiers. 1052 if (TI.getTriple().isOSDarwin()) { 1053 Builder.defineMacro("__nonnull", "_Nonnull"); 1054 Builder.defineMacro("__null_unspecified", "_Null_unspecified"); 1055 Builder.defineMacro("__nullable", "_Nullable"); 1056 } 1057 1058 // Add a macro to differentiate between regular iOS/tvOS/watchOS targets and 1059 // the corresponding simulator targets. 1060 if (TI.getTriple().isOSDarwin() && TI.getTriple().isSimulatorEnvironment()) 1061 Builder.defineMacro("__APPLE_EMBEDDED_SIMULATOR__", "1"); 1062 1063 // OpenMP definition 1064 // OpenMP 2.2: 1065 // In implementations that support a preprocessor, the _OPENMP 1066 // macro name is defined to have the decimal value yyyymm where 1067 // yyyy and mm are the year and the month designations of the 1068 // version of the OpenMP API that the implementation support. 1069 if (!LangOpts.OpenMPSimd) { 1070 switch (LangOpts.OpenMP) { 1071 case 0: 1072 break; 1073 case 31: 1074 Builder.defineMacro("_OPENMP", "201107"); 1075 break; 1076 case 40: 1077 Builder.defineMacro("_OPENMP", "201307"); 1078 break; 1079 case 45: 1080 Builder.defineMacro("_OPENMP", "201511"); 1081 break; 1082 default: 1083 // Default version is OpenMP 5.0 1084 Builder.defineMacro("_OPENMP", "201811"); 1085 break; 1086 } 1087 } 1088 1089 // CUDA device path compilaton 1090 if (LangOpts.CUDAIsDevice && !LangOpts.HIP) { 1091 // The CUDA_ARCH value is set for the GPU target specified in the NVPTX 1092 // backend's target defines. 1093 Builder.defineMacro("__CUDA_ARCH__"); 1094 } 1095 1096 // We need to communicate this to our CUDA header wrapper, which in turn 1097 // informs the proper CUDA headers of this choice. 1098 if (LangOpts.CUDADeviceApproxTranscendentals || LangOpts.FastMath) { 1099 Builder.defineMacro("__CLANG_CUDA_APPROX_TRANSCENDENTALS__"); 1100 } 1101 1102 // Define a macro indicating that the source file is being compiled with a 1103 // SYCL device compiler which doesn't produce host binary. 1104 if (LangOpts.SYCLIsDevice) { 1105 Builder.defineMacro("__SYCL_DEVICE_ONLY__", "1"); 1106 } 1107 1108 // OpenCL definitions. 1109 if (LangOpts.OpenCL) { 1110 #define OPENCLEXT(Ext) \ 1111 if (TI.getSupportedOpenCLOpts().isSupported(#Ext, LangOpts)) \ 1112 Builder.defineMacro(#Ext); 1113 #include "clang/Basic/OpenCLExtensions.def" 1114 1115 if (TI.getTriple().isSPIR()) 1116 Builder.defineMacro("__IMAGE_SUPPORT__"); 1117 } 1118 1119 if (TI.hasInt128Type() && LangOpts.CPlusPlus && LangOpts.GNUMode) { 1120 // For each extended integer type, g++ defines a macro mapping the 1121 // index of the type (0 in this case) in some list of extended types 1122 // to the type. 1123 Builder.defineMacro("__GLIBCXX_TYPE_INT_N_0", "__int128"); 1124 Builder.defineMacro("__GLIBCXX_BITSIZE_INT_N_0", "128"); 1125 } 1126 1127 // Get other target #defines. 1128 TI.getTargetDefines(LangOpts, Builder); 1129 } 1130 1131 /// InitializePreprocessor - Initialize the preprocessor getting it and the 1132 /// environment ready to process a single file. This returns true on error. 1133 /// 1134 void clang::InitializePreprocessor( 1135 Preprocessor &PP, const PreprocessorOptions &InitOpts, 1136 const PCHContainerReader &PCHContainerRdr, 1137 const FrontendOptions &FEOpts) { 1138 const LangOptions &LangOpts = PP.getLangOpts(); 1139 std::string PredefineBuffer; 1140 PredefineBuffer.reserve(4080); 1141 llvm::raw_string_ostream Predefines(PredefineBuffer); 1142 MacroBuilder Builder(Predefines); 1143 1144 // Emit line markers for various builtin sections of the file. We don't do 1145 // this in asm preprocessor mode, because "# 4" is not a line marker directive 1146 // in this mode. 1147 if (!PP.getLangOpts().AsmPreprocessor) 1148 Builder.append("# 1 \"<built-in>\" 3"); 1149 1150 // Install things like __POWERPC__, __GNUC__, etc into the macro table. 1151 if (InitOpts.UsePredefines) { 1152 // FIXME: This will create multiple definitions for most of the predefined 1153 // macros. This is not the right way to handle this. 1154 if ((LangOpts.CUDA || LangOpts.OpenMPIsDevice || LangOpts.SYCLIsDevice) && 1155 PP.getAuxTargetInfo()) 1156 InitializePredefinedMacros(*PP.getAuxTargetInfo(), LangOpts, FEOpts, 1157 PP.getPreprocessorOpts(), Builder); 1158 1159 InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, 1160 PP.getPreprocessorOpts(), Builder); 1161 1162 // Install definitions to make Objective-C++ ARC work well with various 1163 // C++ Standard Library implementations. 1164 if (LangOpts.ObjC && LangOpts.CPlusPlus && 1165 (LangOpts.ObjCAutoRefCount || LangOpts.ObjCWeak)) { 1166 switch (InitOpts.ObjCXXARCStandardLibrary) { 1167 case ARCXX_nolib: 1168 case ARCXX_libcxx: 1169 break; 1170 1171 case ARCXX_libstdcxx: 1172 AddObjCXXARCLibstdcxxDefines(LangOpts, Builder); 1173 break; 1174 } 1175 } 1176 } 1177 1178 // Even with predefines off, some macros are still predefined. 1179 // These should all be defined in the preprocessor according to the 1180 // current language configuration. 1181 InitializeStandardPredefinedMacros(PP.getTargetInfo(), PP.getLangOpts(), 1182 FEOpts, Builder); 1183 1184 // Add on the predefines from the driver. Wrap in a #line directive to report 1185 // that they come from the command line. 1186 if (!PP.getLangOpts().AsmPreprocessor) 1187 Builder.append("# 1 \"<command line>\" 1"); 1188 1189 // Process #define's and #undef's in the order they are given. 1190 for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) { 1191 if (InitOpts.Macros[i].second) // isUndef 1192 Builder.undefineMacro(InitOpts.Macros[i].first); 1193 else 1194 DefineBuiltinMacro(Builder, InitOpts.Macros[i].first, 1195 PP.getDiagnostics()); 1196 } 1197 1198 // Exit the command line and go back to <built-in> (2 is LC_LEAVE). 1199 if (!PP.getLangOpts().AsmPreprocessor) 1200 Builder.append("# 1 \"<built-in>\" 2"); 1201 1202 // If -imacros are specified, include them now. These are processed before 1203 // any -include directives. 1204 for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i) 1205 AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i]); 1206 1207 // Process -include-pch/-include-pth directives. 1208 if (!InitOpts.ImplicitPCHInclude.empty()) 1209 AddImplicitIncludePCH(Builder, PP, PCHContainerRdr, 1210 InitOpts.ImplicitPCHInclude); 1211 1212 // Process -include directives. 1213 for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) { 1214 const std::string &Path = InitOpts.Includes[i]; 1215 AddImplicitInclude(Builder, Path); 1216 } 1217 1218 // Instruct the preprocessor to skip the preamble. 1219 PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first, 1220 InitOpts.PrecompiledPreambleBytes.second); 1221 1222 // Copy PredefinedBuffer into the Preprocessor. 1223 PP.setPredefines(Predefines.str()); 1224 } 1225