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