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/Frontend/Utils.h" 15 #include "clang/Basic/FileManager.h" 16 #include "clang/Basic/MacroBuilder.h" 17 #include "clang/Basic/SourceManager.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/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/Support/FileSystem.h" 28 #include "llvm/Support/MemoryBuffer.h" 29 #include "llvm/Support/Path.h" 30 using namespace clang; 31 32 static bool MacroBodyEndsInBackslash(StringRef MacroBody) { 33 while (!MacroBody.empty() && isWhitespace(MacroBody.back())) 34 MacroBody = MacroBody.drop_back(); 35 return !MacroBody.empty() && MacroBody.back() == '\\'; 36 } 37 38 // Append a #define line to Buf for Macro. Macro should be of the form XXX, 39 // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit 40 // "#define XXX Y z W". To get a #define with no value, use "XXX=". 41 static void DefineBuiltinMacro(MacroBuilder &Builder, StringRef Macro, 42 DiagnosticsEngine &Diags) { 43 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 44 StringRef MacroName = MacroPair.first; 45 StringRef MacroBody = MacroPair.second; 46 if (MacroName.size() != Macro.size()) { 47 // Per GCC -D semantics, the macro ends at \n if it exists. 48 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 49 if (End != StringRef::npos) 50 Diags.Report(diag::warn_fe_macro_contains_embedded_newline) 51 << MacroName; 52 MacroBody = MacroBody.substr(0, End); 53 // We handle macro bodies which end in a backslash by appending an extra 54 // backslash+newline. This makes sure we don't accidentally treat the 55 // backslash as a line continuation marker. 56 if (MacroBodyEndsInBackslash(MacroBody)) 57 Builder.defineMacro(MacroName, Twine(MacroBody) + "\\\n"); 58 else 59 Builder.defineMacro(MacroName, MacroBody); 60 } else { 61 // Push "macroname 1". 62 Builder.defineMacro(Macro); 63 } 64 } 65 66 /// AddImplicitInclude - Add an implicit \#include of the specified file to the 67 /// predefines buffer. 68 static void AddImplicitInclude(MacroBuilder &Builder, StringRef File, 69 FileManager &FileMgr) { 70 Builder.append(Twine("#include \"") + 71 HeaderSearch::NormalizeDashIncludePath(File, FileMgr) + "\""); 72 } 73 74 static void AddImplicitIncludeMacros(MacroBuilder &Builder, 75 StringRef File, 76 FileManager &FileMgr) { 77 Builder.append(Twine("#__include_macros \"") + 78 HeaderSearch::NormalizeDashIncludePath(File, FileMgr) + "\""); 79 // Marker token to stop the __include_macros fetch loop. 80 Builder.append("##"); // ##? 81 } 82 83 /// AddImplicitIncludePTH - Add an implicit \#include using the original file 84 /// used to generate a PTH cache. 85 static void AddImplicitIncludePTH(MacroBuilder &Builder, Preprocessor &PP, 86 StringRef ImplicitIncludePTH) { 87 PTHManager *P = PP.getPTHManager(); 88 // Null check 'P' in the corner case where it couldn't be created. 89 const char *OriginalFile = P ? P->getOriginalSourceFile() : nullptr; 90 91 if (!OriginalFile) { 92 PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header) 93 << ImplicitIncludePTH; 94 return; 95 } 96 97 AddImplicitInclude(Builder, OriginalFile, PP.getFileManager()); 98 } 99 100 /// \brief Add an implicit \#include using the original file used to generate 101 /// a PCH file. 102 static void AddImplicitIncludePCH(MacroBuilder &Builder, Preprocessor &PP, 103 StringRef ImplicitIncludePCH) { 104 std::string OriginalFile = 105 ASTReader::getOriginalSourceFile(ImplicitIncludePCH, PP.getFileManager(), 106 PP.getDiagnostics()); 107 if (OriginalFile.empty()) 108 return; 109 110 AddImplicitInclude(Builder, OriginalFile, PP.getFileManager()); 111 } 112 113 /// PickFP - This is used to pick a value based on the FP semantics of the 114 /// specified FP model. 115 template <typename T> 116 static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal, 117 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal, 118 T IEEEQuadVal) { 119 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle) 120 return IEEESingleVal; 121 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble) 122 return IEEEDoubleVal; 123 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended) 124 return X87DoubleExtendedVal; 125 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble) 126 return PPCDoubleDoubleVal; 127 assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad); 128 return IEEEQuadVal; 129 } 130 131 static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix, 132 const llvm::fltSemantics *Sem, StringRef Ext) { 133 const char *DenormMin, *Epsilon, *Max, *Min; 134 DenormMin = PickFP(Sem, "1.40129846e-45", "4.9406564584124654e-324", 135 "3.64519953188247460253e-4951", 136 "4.94065645841246544176568792868221e-324", 137 "6.47517511943802511092443895822764655e-4966"); 138 int Digits = PickFP(Sem, 6, 15, 18, 31, 33); 139 Epsilon = PickFP(Sem, "1.19209290e-7", "2.2204460492503131e-16", 140 "1.08420217248550443401e-19", 141 "4.94065645841246544176568792868221e-324", 142 "1.92592994438723585305597794258492732e-34"); 143 int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113); 144 int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931); 145 int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932); 146 int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381); 147 int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384); 148 Min = PickFP(Sem, "1.17549435e-38", "2.2250738585072014e-308", 149 "3.36210314311209350626e-4932", 150 "2.00416836000897277799610805135016e-292", 151 "3.36210314311209350626267781732175260e-4932"); 152 Max = PickFP(Sem, "3.40282347e+38", "1.7976931348623157e+308", 153 "1.18973149535723176502e+4932", 154 "1.79769313486231580793728971405301e+308", 155 "1.18973149535723176508575932662800702e+4932"); 156 157 SmallString<32> DefPrefix; 158 DefPrefix = "__"; 159 DefPrefix += Prefix; 160 DefPrefix += "_"; 161 162 Builder.defineMacro(DefPrefix + "DENORM_MIN__", Twine(DenormMin)+Ext); 163 Builder.defineMacro(DefPrefix + "HAS_DENORM__"); 164 Builder.defineMacro(DefPrefix + "DIG__", Twine(Digits)); 165 Builder.defineMacro(DefPrefix + "EPSILON__", Twine(Epsilon)+Ext); 166 Builder.defineMacro(DefPrefix + "HAS_INFINITY__"); 167 Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__"); 168 Builder.defineMacro(DefPrefix + "MANT_DIG__", Twine(MantissaDigits)); 169 170 Builder.defineMacro(DefPrefix + "MAX_10_EXP__", Twine(Max10Exp)); 171 Builder.defineMacro(DefPrefix + "MAX_EXP__", Twine(MaxExp)); 172 Builder.defineMacro(DefPrefix + "MAX__", Twine(Max)+Ext); 173 174 Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+Twine(Min10Exp)+")"); 175 Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+Twine(MinExp)+")"); 176 Builder.defineMacro(DefPrefix + "MIN__", Twine(Min)+Ext); 177 } 178 179 180 /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro 181 /// named MacroName with the max value for a type with width 'TypeWidth' a 182 /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL). 183 static void DefineTypeSize(const Twine &MacroName, unsigned TypeWidth, 184 StringRef ValSuffix, bool isSigned, 185 MacroBuilder &Builder) { 186 llvm::APInt MaxVal = isSigned ? llvm::APInt::getSignedMaxValue(TypeWidth) 187 : llvm::APInt::getMaxValue(TypeWidth); 188 Builder.defineMacro(MacroName, MaxVal.toString(10, isSigned) + ValSuffix); 189 } 190 191 /// DefineTypeSize - An overloaded helper that uses TargetInfo to determine 192 /// the width, suffix, and signedness of the given type 193 static void DefineTypeSize(const Twine &MacroName, TargetInfo::IntType Ty, 194 const TargetInfo &TI, MacroBuilder &Builder) { 195 DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty), 196 TI.isTypeSigned(Ty), Builder); 197 } 198 199 static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty, 200 MacroBuilder &Builder) { 201 Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty)); 202 } 203 204 static void DefineTypeWidth(StringRef MacroName, TargetInfo::IntType Ty, 205 const TargetInfo &TI, MacroBuilder &Builder) { 206 Builder.defineMacro(MacroName, Twine(TI.getTypeWidth(Ty))); 207 } 208 209 static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth, 210 const TargetInfo &TI, MacroBuilder &Builder) { 211 Builder.defineMacro(MacroName, 212 Twine(BitWidth / TI.getCharWidth())); 213 } 214 215 static void DefineExactWidthIntType(TargetInfo::IntType Ty, 216 const TargetInfo &TI, 217 MacroBuilder &Builder) { 218 int TypeWidth = TI.getTypeWidth(Ty); 219 bool IsSigned = TI.isTypeSigned(Ty); 220 221 // Use the target specified int64 type, when appropriate, so that [u]int64_t 222 // ends up being defined in terms of the correct type. 223 if (TypeWidth == 64) 224 Ty = IsSigned ? TI.getInt64Type() : TI.getIntTypeByWidth(64, false); 225 226 const char *Prefix = IsSigned ? "__INT" : "__UINT"; 227 228 DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder); 229 230 StringRef ConstSuffix(TargetInfo::getTypeConstantSuffix(Ty)); 231 if (!ConstSuffix.empty()) 232 Builder.defineMacro(Prefix + Twine(TypeWidth) + "_C_SUFFIX__", ConstSuffix); 233 234 } 235 236 static void DefineExactWidthIntTypeSize(TargetInfo::IntType Ty, 237 const TargetInfo &TI, 238 MacroBuilder &Builder) { 239 int TypeWidth = TI.getTypeWidth(Ty); 240 bool IsSigned = TI.isTypeSigned(Ty); 241 242 // Use the target specified int64 type, when appropriate, so that [u]int64_t 243 // ends up being defined in terms of the correct type. 244 if (TypeWidth == 64) 245 Ty = IsSigned ? TI.getInt64Type() : TI.getIntTypeByWidth(64, false); 246 247 const char *Prefix = IsSigned ? "__INT" : "__UINT"; 248 DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder); 249 } 250 251 static void DefineLeastWidthIntType(unsigned TypeWidth, bool IsSigned, 252 const TargetInfo &TI, 253 MacroBuilder &Builder) { 254 TargetInfo::IntType Ty = TI.getLeastIntTypeByWidth(TypeWidth, IsSigned); 255 if (Ty == TargetInfo::NoInt) 256 return; 257 258 const char *Prefix = IsSigned ? "__INT_LEAST" : "__UINT_LEAST"; 259 DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder); 260 DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder); 261 } 262 263 static void DefineFastIntType(unsigned TypeWidth, bool IsSigned, 264 const TargetInfo &TI, MacroBuilder &Builder) { 265 // stdint.h currently defines the fast int types as equivalent to the least 266 // types. 267 TargetInfo::IntType Ty = TI.getLeastIntTypeByWidth(TypeWidth, IsSigned); 268 if (Ty == TargetInfo::NoInt) 269 return; 270 271 const char *Prefix = IsSigned ? "__INT_FAST" : "__UINT_FAST"; 272 DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder); 273 DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder); 274 } 275 276 277 /// Get the value the ATOMIC_*_LOCK_FREE macro should have for a type with 278 /// the specified properties. 279 static const char *getLockFreeValue(unsigned TypeWidth, unsigned TypeAlign, 280 unsigned InlineWidth) { 281 // Fully-aligned, power-of-2 sizes no larger than the inline 282 // width will be inlined as lock-free operations. 283 if (TypeWidth == TypeAlign && (TypeWidth & (TypeWidth - 1)) == 0 && 284 TypeWidth <= InlineWidth) 285 return "2"; // "always lock free" 286 // We cannot be certain what operations the lib calls might be 287 // able to implement as lock-free on future processors. 288 return "1"; // "sometimes lock free" 289 } 290 291 /// \brief Add definitions required for a smooth interaction between 292 /// Objective-C++ automated reference counting and libstdc++ (4.2). 293 static void AddObjCXXARCLibstdcxxDefines(const LangOptions &LangOpts, 294 MacroBuilder &Builder) { 295 Builder.defineMacro("_GLIBCXX_PREDEFINED_OBJC_ARC_IS_SCALAR"); 296 297 std::string Result; 298 { 299 // Provide specializations for the __is_scalar type trait so that 300 // lifetime-qualified objects are not considered "scalar" types, which 301 // libstdc++ uses as an indicator of the presence of trivial copy, assign, 302 // default-construct, and destruct semantics (none of which hold for 303 // lifetime-qualified objects in ARC). 304 llvm::raw_string_ostream Out(Result); 305 306 Out << "namespace std {\n" 307 << "\n" 308 << "struct __true_type;\n" 309 << "struct __false_type;\n" 310 << "\n"; 311 312 Out << "template<typename _Tp> struct __is_scalar;\n" 313 << "\n"; 314 315 Out << "template<typename _Tp>\n" 316 << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> {\n" 317 << " enum { __value = 0 };\n" 318 << " typedef __false_type __type;\n" 319 << "};\n" 320 << "\n"; 321 322 if (LangOpts.ObjCARCWeak) { 323 Out << "template<typename _Tp>\n" 324 << "struct __is_scalar<__attribute__((objc_ownership(weak))) _Tp> {\n" 325 << " enum { __value = 0 };\n" 326 << " typedef __false_type __type;\n" 327 << "};\n" 328 << "\n"; 329 } 330 331 Out << "template<typename _Tp>\n" 332 << "struct __is_scalar<__attribute__((objc_ownership(autoreleasing)))" 333 << " _Tp> {\n" 334 << " enum { __value = 0 };\n" 335 << " typedef __false_type __type;\n" 336 << "};\n" 337 << "\n"; 338 339 Out << "}\n"; 340 } 341 Builder.append(Result); 342 } 343 344 static void InitializeStandardPredefinedMacros(const TargetInfo &TI, 345 const LangOptions &LangOpts, 346 const FrontendOptions &FEOpts, 347 MacroBuilder &Builder) { 348 if (!LangOpts.MSVCCompat && !LangOpts.TraditionalCPP) 349 Builder.defineMacro("__STDC__"); 350 if (LangOpts.Freestanding) 351 Builder.defineMacro("__STDC_HOSTED__", "0"); 352 else 353 Builder.defineMacro("__STDC_HOSTED__"); 354 355 if (!LangOpts.CPlusPlus) { 356 if (LangOpts.C11) 357 Builder.defineMacro("__STDC_VERSION__", "201112L"); 358 else if (LangOpts.C99) 359 Builder.defineMacro("__STDC_VERSION__", "199901L"); 360 else if (!LangOpts.GNUMode && LangOpts.Digraphs) 361 Builder.defineMacro("__STDC_VERSION__", "199409L"); 362 } else { 363 // FIXME: Use correct value for C++17. 364 if (LangOpts.CPlusPlus1z) 365 Builder.defineMacro("__cplusplus", "201406L"); 366 // C++1y [cpp.predefined]p1: 367 // The name __cplusplus is defined to the value 201402L when compiling a 368 // C++ translation unit. 369 else if (LangOpts.CPlusPlus1y) 370 Builder.defineMacro("__cplusplus", "201402L"); 371 // C++11 [cpp.predefined]p1: 372 // The name __cplusplus is defined to the value 201103L when compiling a 373 // C++ translation unit. 374 else if (LangOpts.CPlusPlus11) 375 Builder.defineMacro("__cplusplus", "201103L"); 376 // C++03 [cpp.predefined]p1: 377 // The name __cplusplus is defined to the value 199711L when compiling a 378 // C++ translation unit. 379 else 380 Builder.defineMacro("__cplusplus", "199711L"); 381 } 382 383 // In C11 these are environment macros. In C++11 they are only defined 384 // as part of <cuchar>. To prevent breakage when mixing C and C++ 385 // code, define these macros unconditionally. We can define them 386 // unconditionally, as Clang always uses UTF-16 and UTF-32 for 16-bit 387 // and 32-bit character literals. 388 Builder.defineMacro("__STDC_UTF_16__", "1"); 389 Builder.defineMacro("__STDC_UTF_32__", "1"); 390 391 if (LangOpts.ObjC1) 392 Builder.defineMacro("__OBJC__"); 393 394 // Not "standard" per se, but available even with the -undef flag. 395 if (LangOpts.AsmPreprocessor) 396 Builder.defineMacro("__ASSEMBLER__"); 397 } 398 399 /// Initialize the predefined C++ language feature test macros defined in 400 /// ISO/IEC JTC1/SC22/WG21 (C++) SD-6: "SG10 Feature Test Recommendations". 401 static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts, 402 MacroBuilder &Builder) { 403 // C++11 features. 404 if (LangOpts.CPlusPlus11) { 405 Builder.defineMacro("__cpp_unicode_characters", "200704"); 406 Builder.defineMacro("__cpp_raw_strings", "200710"); 407 Builder.defineMacro("__cpp_unicode_literals", "200710"); 408 Builder.defineMacro("__cpp_user_defined_literals", "200809"); 409 Builder.defineMacro("__cpp_lambdas", "200907"); 410 Builder.defineMacro("__cpp_constexpr", 411 LangOpts.CPlusPlus1y ? "201304" : "200704"); 412 Builder.defineMacro("__cpp_static_assert", "200410"); 413 Builder.defineMacro("__cpp_decltype", "200707"); 414 Builder.defineMacro("__cpp_attributes", "200809"); 415 Builder.defineMacro("__cpp_rvalue_references", "200610"); 416 Builder.defineMacro("__cpp_variadic_templates", "200704"); 417 } 418 419 // C++14 features. 420 if (LangOpts.CPlusPlus1y) { 421 Builder.defineMacro("__cpp_binary_literals", "201304"); 422 Builder.defineMacro("__cpp_init_captures", "201304"); 423 Builder.defineMacro("__cpp_generic_lambdas", "201304"); 424 Builder.defineMacro("__cpp_decltype_auto", "201304"); 425 Builder.defineMacro("__cpp_return_type_deduction", "201304"); 426 Builder.defineMacro("__cpp_aggregate_nsdmi", "201304"); 427 Builder.defineMacro("__cpp_variable_templates", "201304"); 428 } 429 } 430 431 static void InitializePredefinedMacros(const TargetInfo &TI, 432 const LangOptions &LangOpts, 433 const FrontendOptions &FEOpts, 434 MacroBuilder &Builder) { 435 // Compiler version introspection macros. 436 Builder.defineMacro("__llvm__"); // LLVM Backend 437 Builder.defineMacro("__clang__"); // Clang Frontend 438 #define TOSTR2(X) #X 439 #define TOSTR(X) TOSTR2(X) 440 Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR)); 441 Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR)); 442 #ifdef CLANG_VERSION_PATCHLEVEL 443 Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL)); 444 #else 445 Builder.defineMacro("__clang_patchlevel__", "0"); 446 #endif 447 Builder.defineMacro("__clang_version__", 448 "\"" CLANG_VERSION_STRING " " 449 + getClangFullRepositoryVersion() + "\""); 450 #undef TOSTR 451 #undef TOSTR2 452 if (!LangOpts.MSVCCompat) { 453 // Currently claim to be compatible with GCC 4.2.1-5621, but only if we're 454 // not compiling for MSVC compatibility 455 Builder.defineMacro("__GNUC_MINOR__", "2"); 456 Builder.defineMacro("__GNUC_PATCHLEVEL__", "1"); 457 Builder.defineMacro("__GNUC__", "4"); 458 Builder.defineMacro("__GXX_ABI_VERSION", "1002"); 459 } 460 461 // Define macros for the C11 / C++11 memory orderings 462 Builder.defineMacro("__ATOMIC_RELAXED", "0"); 463 Builder.defineMacro("__ATOMIC_CONSUME", "1"); 464 Builder.defineMacro("__ATOMIC_ACQUIRE", "2"); 465 Builder.defineMacro("__ATOMIC_RELEASE", "3"); 466 Builder.defineMacro("__ATOMIC_ACQ_REL", "4"); 467 Builder.defineMacro("__ATOMIC_SEQ_CST", "5"); 468 469 // Support for #pragma redefine_extname (Sun compatibility) 470 Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1"); 471 472 // As sad as it is, enough software depends on the __VERSION__ for version 473 // checks that it is necessary to report 4.2.1 (the base GCC version we claim 474 // compatibility with) first. 475 Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible " + 476 Twine(getClangFullCPPVersion()) + "\""); 477 478 // Initialize language-specific preprocessor defines. 479 480 // Standard conforming mode? 481 if (!LangOpts.GNUMode && !LangOpts.MSVCCompat) 482 Builder.defineMacro("__STRICT_ANSI__"); 483 484 if (LangOpts.CPlusPlus11) 485 Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__"); 486 487 if (LangOpts.ObjC1) { 488 if (LangOpts.ObjCRuntime.isNonFragile()) { 489 Builder.defineMacro("__OBJC2__"); 490 491 if (LangOpts.ObjCExceptions) 492 Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS"); 493 } 494 495 if (LangOpts.getGC() != LangOptions::NonGC) 496 Builder.defineMacro("__OBJC_GC__"); 497 498 if (LangOpts.ObjCRuntime.isNeXTFamily()) 499 Builder.defineMacro("__NEXT_RUNTIME__"); 500 501 if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::ObjFW) { 502 VersionTuple tuple = LangOpts.ObjCRuntime.getVersion(); 503 504 unsigned minor = 0; 505 if (tuple.getMinor().hasValue()) 506 minor = tuple.getMinor().getValue(); 507 508 unsigned subminor = 0; 509 if (tuple.getSubminor().hasValue()) 510 subminor = tuple.getSubminor().getValue(); 511 512 Builder.defineMacro("__OBJFW_RUNTIME_ABI__", 513 Twine(tuple.getMajor() * 10000 + minor * 100 + 514 subminor)); 515 } 516 517 Builder.defineMacro("IBOutlet", "__attribute__((iboutlet))"); 518 Builder.defineMacro("IBOutletCollection(ClassName)", 519 "__attribute__((iboutletcollection(ClassName)))"); 520 Builder.defineMacro("IBAction", "void)__attribute__((ibaction)"); 521 } 522 523 if (LangOpts.CPlusPlus) 524 InitializeCPlusPlusFeatureTestMacros(LangOpts, Builder); 525 526 // darwin_constant_cfstrings controls this. This is also dependent 527 // on other things like the runtime I believe. This is set even for C code. 528 if (!LangOpts.NoConstantCFStrings) 529 Builder.defineMacro("__CONSTANT_CFSTRINGS__"); 530 531 if (LangOpts.ObjC2) 532 Builder.defineMacro("OBJC_NEW_PROPERTIES"); 533 534 if (LangOpts.PascalStrings) 535 Builder.defineMacro("__PASCAL_STRINGS__"); 536 537 if (LangOpts.Blocks) { 538 Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))"); 539 Builder.defineMacro("__BLOCKS__"); 540 } 541 542 if (!LangOpts.MSVCCompat && LangOpts.CXXExceptions) 543 Builder.defineMacro("__EXCEPTIONS"); 544 if (LangOpts.RTTI) 545 Builder.defineMacro("__GXX_RTTI"); 546 if (LangOpts.SjLjExceptions) 547 Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__"); 548 549 if (LangOpts.Deprecated) 550 Builder.defineMacro("__DEPRECATED"); 551 552 if (LangOpts.CPlusPlus) { 553 Builder.defineMacro("__GNUG__", "4"); 554 Builder.defineMacro("__GXX_WEAK__"); 555 Builder.defineMacro("__private_extern__", "extern"); 556 } 557 558 if (LangOpts.MicrosoftExt) { 559 if (LangOpts.WChar) { 560 // wchar_t supported as a keyword. 561 Builder.defineMacro("_WCHAR_T_DEFINED"); 562 Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED"); 563 } 564 } 565 566 if (LangOpts.Optimize) 567 Builder.defineMacro("__OPTIMIZE__"); 568 if (LangOpts.OptimizeSize) 569 Builder.defineMacro("__OPTIMIZE_SIZE__"); 570 571 if (LangOpts.FastMath) 572 Builder.defineMacro("__FAST_MATH__"); 573 574 // Initialize target-specific preprocessor defines. 575 576 // __BYTE_ORDER__ was added in GCC 4.6. It's analogous 577 // to the macro __BYTE_ORDER (no trailing underscores) 578 // from glibc's <endian.h> header. 579 // We don't support the PDP-11 as a target, but include 580 // the define so it can still be compared against. 581 Builder.defineMacro("__ORDER_LITTLE_ENDIAN__", "1234"); 582 Builder.defineMacro("__ORDER_BIG_ENDIAN__", "4321"); 583 Builder.defineMacro("__ORDER_PDP_ENDIAN__", "3412"); 584 if (TI.isBigEndian()) { 585 Builder.defineMacro("__BYTE_ORDER__", "__ORDER_BIG_ENDIAN__"); 586 Builder.defineMacro("__BIG_ENDIAN__"); 587 } else { 588 Builder.defineMacro("__BYTE_ORDER__", "__ORDER_LITTLE_ENDIAN__"); 589 Builder.defineMacro("__LITTLE_ENDIAN__"); 590 } 591 592 if (TI.getPointerWidth(0) == 64 && TI.getLongWidth() == 64 593 && TI.getIntWidth() == 32) { 594 Builder.defineMacro("_LP64"); 595 Builder.defineMacro("__LP64__"); 596 } 597 598 // Define type sizing macros based on the target properties. 599 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far"); 600 Builder.defineMacro("__CHAR_BIT__", "8"); 601 602 DefineTypeSize("__SCHAR_MAX__", TargetInfo::SignedChar, TI, Builder); 603 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder); 604 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder); 605 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder); 606 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder); 607 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder); 608 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder); 609 DefineTypeSize("__SIZE_MAX__", TI.getSizeType(), TI, Builder); 610 611 if (!LangOpts.MSVCCompat) { 612 DefineTypeSize("__UINTMAX_MAX__", TI.getUIntMaxType(), TI, Builder); 613 DefineTypeSize("__PTRDIFF_MAX__", TI.getPtrDiffType(0), TI, Builder); 614 DefineTypeSize("__INTPTR_MAX__", TI.getIntPtrType(), TI, Builder); 615 DefineTypeSize("__UINTPTR_MAX__", TI.getUIntPtrType(), TI, Builder); 616 } 617 618 DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder); 619 DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder); 620 DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder); 621 DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder); 622 DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder); 623 DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder); 624 DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder); 625 DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder); 626 DefineTypeSizeof("__SIZEOF_PTRDIFF_T__", 627 TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder); 628 DefineTypeSizeof("__SIZEOF_SIZE_T__", 629 TI.getTypeWidth(TI.getSizeType()), TI, Builder); 630 DefineTypeSizeof("__SIZEOF_WCHAR_T__", 631 TI.getTypeWidth(TI.getWCharType()), TI, Builder); 632 DefineTypeSizeof("__SIZEOF_WINT_T__", 633 TI.getTypeWidth(TI.getWIntType()), TI, Builder); 634 if (TI.hasInt128Type()) 635 DefineTypeSizeof("__SIZEOF_INT128__", 128, TI, Builder); 636 637 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder); 638 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder); 639 DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder); 640 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder); 641 DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder); 642 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder); 643 DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder); 644 DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder); 645 DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder); 646 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder); 647 DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder); 648 DefineType("__WINT_TYPE__", TI.getWIntType(), Builder); 649 DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder); 650 DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder); 651 DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder); 652 DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder); 653 654 if (!LangOpts.MSVCCompat) { 655 DefineTypeWidth("__UINTMAX_WIDTH__", TI.getUIntMaxType(), TI, Builder); 656 DefineType("__UINTPTR_TYPE__", TI.getUIntPtrType(), Builder); 657 DefineTypeWidth("__UINTPTR_WIDTH__", TI.getUIntPtrType(), TI, Builder); 658 } 659 660 DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat(), "F"); 661 DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat(), ""); 662 DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat(), "L"); 663 664 // Define a __POINTER_WIDTH__ macro for stdint.h. 665 Builder.defineMacro("__POINTER_WIDTH__", 666 Twine((int)TI.getPointerWidth(0))); 667 668 if (!LangOpts.CharIsSigned) 669 Builder.defineMacro("__CHAR_UNSIGNED__"); 670 671 if (!TargetInfo::isTypeSigned(TI.getWCharType())) 672 Builder.defineMacro("__WCHAR_UNSIGNED__"); 673 674 if (!TargetInfo::isTypeSigned(TI.getWIntType())) 675 Builder.defineMacro("__WINT_UNSIGNED__"); 676 677 // Define exact-width integer types for stdint.h 678 Builder.defineMacro("__INT" + Twine(TI.getCharWidth()) + "_TYPE__", 679 "char"); 680 681 if (TI.getShortWidth() > TI.getCharWidth()) 682 DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder); 683 684 if (TI.getIntWidth() > TI.getShortWidth()) 685 DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder); 686 687 if (TI.getLongWidth() > TI.getIntWidth()) 688 DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder); 689 690 if (TI.getLongLongWidth() > TI.getLongWidth()) 691 DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder); 692 693 if (!LangOpts.MSVCCompat) { 694 DefineExactWidthIntType(TargetInfo::UnsignedChar, TI, Builder); 695 DefineExactWidthIntTypeSize(TargetInfo::UnsignedChar, TI, Builder); 696 DefineExactWidthIntTypeSize(TargetInfo::SignedChar, TI, Builder); 697 698 if (TI.getShortWidth() > TI.getCharWidth()) { 699 DefineExactWidthIntType(TargetInfo::UnsignedShort, TI, Builder); 700 DefineExactWidthIntTypeSize(TargetInfo::UnsignedShort, TI, Builder); 701 DefineExactWidthIntTypeSize(TargetInfo::SignedShort, TI, Builder); 702 } 703 704 if (TI.getIntWidth() > TI.getShortWidth()) { 705 DefineExactWidthIntType(TargetInfo::UnsignedInt, TI, Builder); 706 DefineExactWidthIntTypeSize(TargetInfo::UnsignedInt, TI, Builder); 707 DefineExactWidthIntTypeSize(TargetInfo::SignedInt, TI, Builder); 708 } 709 710 if (TI.getLongWidth() > TI.getIntWidth()) { 711 DefineExactWidthIntType(TargetInfo::UnsignedLong, TI, Builder); 712 DefineExactWidthIntTypeSize(TargetInfo::UnsignedLong, TI, Builder); 713 DefineExactWidthIntTypeSize(TargetInfo::SignedLong, TI, Builder); 714 } 715 716 if (TI.getLongLongWidth() > TI.getLongWidth()) { 717 DefineExactWidthIntType(TargetInfo::UnsignedLongLong, TI, Builder); 718 DefineExactWidthIntTypeSize(TargetInfo::UnsignedLongLong, TI, Builder); 719 DefineExactWidthIntTypeSize(TargetInfo::SignedLongLong, TI, Builder); 720 } 721 722 DefineLeastWidthIntType(8, true, TI, Builder); 723 DefineLeastWidthIntType(8, false, TI, Builder); 724 DefineLeastWidthIntType(16, true, TI, Builder); 725 DefineLeastWidthIntType(16, false, TI, Builder); 726 DefineLeastWidthIntType(32, true, TI, Builder); 727 DefineLeastWidthIntType(32, false, TI, Builder); 728 DefineLeastWidthIntType(64, true, TI, Builder); 729 DefineLeastWidthIntType(64, false, TI, Builder); 730 731 DefineFastIntType(8, true, TI, Builder); 732 DefineFastIntType(8, false, TI, Builder); 733 DefineFastIntType(16, true, TI, Builder); 734 DefineFastIntType(16, false, TI, Builder); 735 DefineFastIntType(32, true, TI, Builder); 736 DefineFastIntType(32, false, TI, Builder); 737 DefineFastIntType(64, true, TI, Builder); 738 DefineFastIntType(64, false, TI, Builder); 739 } 740 741 if (const char *Prefix = TI.getUserLabelPrefix()) 742 Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix); 743 744 if (LangOpts.FastMath || LangOpts.FiniteMathOnly) 745 Builder.defineMacro("__FINITE_MATH_ONLY__", "1"); 746 else 747 Builder.defineMacro("__FINITE_MATH_ONLY__", "0"); 748 749 if (LangOpts.GNUInline) 750 Builder.defineMacro("__GNUC_GNU_INLINE__"); 751 else 752 Builder.defineMacro("__GNUC_STDC_INLINE__"); 753 754 // The value written by __atomic_test_and_set. 755 // FIXME: This is target-dependent. 756 Builder.defineMacro("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1"); 757 758 // Used by libstdc++ to implement ATOMIC_<foo>_LOCK_FREE. 759 unsigned InlineWidthBits = TI.getMaxAtomicInlineWidth(); 760 #define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \ 761 Builder.defineMacro("__GCC_ATOMIC_" #TYPE "_LOCK_FREE", \ 762 getLockFreeValue(TI.get##Type##Width(), \ 763 TI.get##Type##Align(), \ 764 InlineWidthBits)); 765 DEFINE_LOCK_FREE_MACRO(BOOL, Bool); 766 DEFINE_LOCK_FREE_MACRO(CHAR, Char); 767 DEFINE_LOCK_FREE_MACRO(CHAR16_T, Char16); 768 DEFINE_LOCK_FREE_MACRO(CHAR32_T, Char32); 769 DEFINE_LOCK_FREE_MACRO(WCHAR_T, WChar); 770 DEFINE_LOCK_FREE_MACRO(SHORT, Short); 771 DEFINE_LOCK_FREE_MACRO(INT, Int); 772 DEFINE_LOCK_FREE_MACRO(LONG, Long); 773 DEFINE_LOCK_FREE_MACRO(LLONG, LongLong); 774 Builder.defineMacro("__GCC_ATOMIC_POINTER_LOCK_FREE", 775 getLockFreeValue(TI.getPointerWidth(0), 776 TI.getPointerAlign(0), 777 InlineWidthBits)); 778 #undef DEFINE_LOCK_FREE_MACRO 779 780 if (LangOpts.NoInlineDefine) 781 Builder.defineMacro("__NO_INLINE__"); 782 783 if (unsigned PICLevel = LangOpts.PICLevel) { 784 Builder.defineMacro("__PIC__", Twine(PICLevel)); 785 Builder.defineMacro("__pic__", Twine(PICLevel)); 786 } 787 if (unsigned PIELevel = LangOpts.PIELevel) { 788 Builder.defineMacro("__PIE__", Twine(PIELevel)); 789 Builder.defineMacro("__pie__", Twine(PIELevel)); 790 } 791 792 // Macros to control C99 numerics and <float.h> 793 Builder.defineMacro("__FLT_EVAL_METHOD__", Twine(TI.getFloatEvalMethod())); 794 Builder.defineMacro("__FLT_RADIX__", "2"); 795 int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36); 796 Builder.defineMacro("__DECIMAL_DIG__", Twine(Dig)); 797 798 if (LangOpts.getStackProtector() == LangOptions::SSPOn) 799 Builder.defineMacro("__SSP__"); 800 else if (LangOpts.getStackProtector() == LangOptions::SSPStrong) 801 Builder.defineMacro("__SSP_STRONG__", "2"); 802 else if (LangOpts.getStackProtector() == LangOptions::SSPReq) 803 Builder.defineMacro("__SSP_ALL__", "3"); 804 805 if (FEOpts.ProgramAction == frontend::RewriteObjC) 806 Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))"); 807 808 // Define a macro that exists only when using the static analyzer. 809 if (FEOpts.ProgramAction == frontend::RunAnalysis) 810 Builder.defineMacro("__clang_analyzer__"); 811 812 if (LangOpts.FastRelaxedMath) 813 Builder.defineMacro("__FAST_RELAXED_MATH__"); 814 815 if (LangOpts.ObjCAutoRefCount) { 816 Builder.defineMacro("__weak", "__attribute__((objc_ownership(weak)))"); 817 Builder.defineMacro("__strong", "__attribute__((objc_ownership(strong)))"); 818 Builder.defineMacro("__autoreleasing", 819 "__attribute__((objc_ownership(autoreleasing)))"); 820 Builder.defineMacro("__unsafe_unretained", 821 "__attribute__((objc_ownership(none)))"); 822 } 823 824 // OpenMP definition 825 if (LangOpts.OpenMP) { 826 // OpenMP 2.2: 827 // In implementations that support a preprocessor, the _OPENMP 828 // macro name is defined to have the decimal value yyyymm where 829 // yyyy and mm are the year and the month designations of the 830 // version of the OpenMP API that the implementation support. 831 Builder.defineMacro("_OPENMP", "201307"); 832 } 833 834 // Get other target #defines. 835 TI.getTargetDefines(LangOpts, Builder); 836 } 837 838 /// InitializePreprocessor - Initialize the preprocessor getting it and the 839 /// environment ready to process a single file. This returns true on error. 840 /// 841 void clang::InitializePreprocessor(Preprocessor &PP, 842 const PreprocessorOptions &InitOpts, 843 const FrontendOptions &FEOpts) { 844 const LangOptions &LangOpts = PP.getLangOpts(); 845 std::string PredefineBuffer; 846 PredefineBuffer.reserve(4080); 847 llvm::raw_string_ostream Predefines(PredefineBuffer); 848 MacroBuilder Builder(Predefines); 849 850 // Emit line markers for various builtin sections of the file. We don't do 851 // this in asm preprocessor mode, because "# 4" is not a line marker directive 852 // in this mode. 853 if (!PP.getLangOpts().AsmPreprocessor) 854 Builder.append("# 1 \"<built-in>\" 3"); 855 856 // Install things like __POWERPC__, __GNUC__, etc into the macro table. 857 if (InitOpts.UsePredefines) { 858 InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, Builder); 859 860 // Install definitions to make Objective-C++ ARC work well with various 861 // C++ Standard Library implementations. 862 if (LangOpts.ObjC1 && LangOpts.CPlusPlus && LangOpts.ObjCAutoRefCount) { 863 switch (InitOpts.ObjCXXARCStandardLibrary) { 864 case ARCXX_nolib: 865 case ARCXX_libcxx: 866 break; 867 868 case ARCXX_libstdcxx: 869 AddObjCXXARCLibstdcxxDefines(LangOpts, Builder); 870 break; 871 } 872 } 873 } 874 875 // Even with predefines off, some macros are still predefined. 876 // These should all be defined in the preprocessor according to the 877 // current language configuration. 878 InitializeStandardPredefinedMacros(PP.getTargetInfo(), PP.getLangOpts(), 879 FEOpts, Builder); 880 881 // Add on the predefines from the driver. Wrap in a #line directive to report 882 // that they come from the command line. 883 if (!PP.getLangOpts().AsmPreprocessor) 884 Builder.append("# 1 \"<command line>\" 1"); 885 886 // Process #define's and #undef's in the order they are given. 887 for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) { 888 if (InitOpts.Macros[i].second) // isUndef 889 Builder.undefineMacro(InitOpts.Macros[i].first); 890 else 891 DefineBuiltinMacro(Builder, InitOpts.Macros[i].first, 892 PP.getDiagnostics()); 893 } 894 895 // If -imacros are specified, include them now. These are processed before 896 // any -include directives. 897 for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i) 898 AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i], 899 PP.getFileManager()); 900 901 // Process -include-pch/-include-pth directives. 902 if (!InitOpts.ImplicitPCHInclude.empty()) 903 AddImplicitIncludePCH(Builder, PP, InitOpts.ImplicitPCHInclude); 904 if (!InitOpts.ImplicitPTHInclude.empty()) 905 AddImplicitIncludePTH(Builder, PP, InitOpts.ImplicitPTHInclude); 906 907 // Process -include directives. 908 for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) { 909 const std::string &Path = InitOpts.Includes[i]; 910 AddImplicitInclude(Builder, Path, PP.getFileManager()); 911 } 912 913 // Exit the command line and go back to <built-in> (2 is LC_LEAVE). 914 if (!PP.getLangOpts().AsmPreprocessor) 915 Builder.append("# 1 \"<built-in>\" 2"); 916 917 // Instruct the preprocessor to skip the preamble. 918 PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first, 919 InitOpts.PrecompiledPreambleBytes.second); 920 921 // Copy PredefinedBuffer into the Preprocessor. 922 PP.setPredefines(Predefines.str()); 923 } 924