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/Version.h" 15 #include "clang/Frontend/Utils.h" 16 #include "clang/Basic/MacroBuilder.h" 17 #include "clang/Basic/TargetInfo.h" 18 #include "clang/Frontend/FrontendDiagnostic.h" 19 #include "clang/Frontend/FrontendOptions.h" 20 #include "clang/Frontend/PreprocessorOptions.h" 21 #include "clang/Lex/HeaderSearch.h" 22 #include "clang/Lex/Preprocessor.h" 23 #include "clang/Basic/FileManager.h" 24 #include "clang/Basic/SourceManager.h" 25 #include "llvm/ADT/APFloat.h" 26 #include "llvm/Support/FileSystem.h" 27 #include "llvm/Support/MemoryBuffer.h" 28 #include "llvm/Support/Path.h" 29 using namespace clang; 30 31 // Append a #define line to Buf for Macro. Macro should be of the form XXX, 32 // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit 33 // "#define XXX Y z W". To get a #define with no value, use "XXX=". 34 static void DefineBuiltinMacro(MacroBuilder &Builder, StringRef Macro, 35 DiagnosticsEngine &Diags) { 36 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 37 StringRef MacroName = MacroPair.first; 38 StringRef MacroBody = MacroPair.second; 39 if (MacroName.size() != Macro.size()) { 40 // Per GCC -D semantics, the macro ends at \n if it exists. 41 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 42 if (End != StringRef::npos) 43 Diags.Report(diag::warn_fe_macro_contains_embedded_newline) 44 << MacroName; 45 Builder.defineMacro(MacroName, MacroBody.substr(0, End)); 46 } else { 47 // Push "macroname 1". 48 Builder.defineMacro(Macro); 49 } 50 } 51 52 /// AddImplicitInclude - Add an implicit \#include of the specified file to the 53 /// predefines buffer. 54 static void AddImplicitInclude(MacroBuilder &Builder, StringRef File, 55 FileManager &FileMgr) { 56 Builder.append(Twine("#include \"") + 57 HeaderSearch::NormalizeDashIncludePath(File, FileMgr) + "\""); 58 } 59 60 static void AddImplicitIncludeMacros(MacroBuilder &Builder, 61 StringRef File, 62 FileManager &FileMgr) { 63 Builder.append(Twine("#__include_macros \"") + 64 HeaderSearch::NormalizeDashIncludePath(File, FileMgr) + "\""); 65 // Marker token to stop the __include_macros fetch loop. 66 Builder.append("##"); // ##? 67 } 68 69 /// AddImplicitIncludePTH - Add an implicit \#include using the original file 70 /// used to generate a PTH cache. 71 static void AddImplicitIncludePTH(MacroBuilder &Builder, Preprocessor &PP, 72 StringRef ImplicitIncludePTH) { 73 PTHManager *P = PP.getPTHManager(); 74 // Null check 'P' in the corner case where it couldn't be created. 75 const char *OriginalFile = P ? P->getOriginalSourceFile() : 0; 76 77 if (!OriginalFile) { 78 PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header) 79 << ImplicitIncludePTH; 80 return; 81 } 82 83 AddImplicitInclude(Builder, OriginalFile, PP.getFileManager()); 84 } 85 86 /// PickFP - This is used to pick a value based on the FP semantics of the 87 /// specified FP model. 88 template <typename T> 89 static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal, 90 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal, 91 T IEEEQuadVal) { 92 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle) 93 return IEEESingleVal; 94 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble) 95 return IEEEDoubleVal; 96 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended) 97 return X87DoubleExtendedVal; 98 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble) 99 return PPCDoubleDoubleVal; 100 assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad); 101 return IEEEQuadVal; 102 } 103 104 static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix, 105 const llvm::fltSemantics *Sem) { 106 const char *DenormMin, *Epsilon, *Max, *Min; 107 DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324", 108 "3.64519953188247460253e-4951L", 109 "4.94065645841246544176568792868221e-324L", 110 "6.47517511943802511092443895822764655e-4966L"); 111 int Digits = PickFP(Sem, 6, 15, 18, 31, 33); 112 Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16", 113 "1.08420217248550443401e-19L", 114 "4.94065645841246544176568792868221e-324L", 115 "1.92592994438723585305597794258492732e-34L"); 116 int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113); 117 int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931); 118 int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932); 119 int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381); 120 int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384); 121 Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308", 122 "3.36210314311209350626e-4932L", 123 "2.00416836000897277799610805135016e-292L", 124 "3.36210314311209350626267781732175260e-4932L"); 125 Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308", 126 "1.18973149535723176502e+4932L", 127 "1.79769313486231580793728971405301e+308L", 128 "1.18973149535723176508575932662800702e+4932L"); 129 130 SmallString<32> DefPrefix; 131 DefPrefix = "__"; 132 DefPrefix += Prefix; 133 DefPrefix += "_"; 134 135 Builder.defineMacro(DefPrefix + "DENORM_MIN__", DenormMin); 136 Builder.defineMacro(DefPrefix + "HAS_DENORM__"); 137 Builder.defineMacro(DefPrefix + "DIG__", Twine(Digits)); 138 Builder.defineMacro(DefPrefix + "EPSILON__", Twine(Epsilon)); 139 Builder.defineMacro(DefPrefix + "HAS_INFINITY__"); 140 Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__"); 141 Builder.defineMacro(DefPrefix + "MANT_DIG__", Twine(MantissaDigits)); 142 143 Builder.defineMacro(DefPrefix + "MAX_10_EXP__", Twine(Max10Exp)); 144 Builder.defineMacro(DefPrefix + "MAX_EXP__", Twine(MaxExp)); 145 Builder.defineMacro(DefPrefix + "MAX__", Twine(Max)); 146 147 Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+Twine(Min10Exp)+")"); 148 Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+Twine(MinExp)+")"); 149 Builder.defineMacro(DefPrefix + "MIN__", Twine(Min)); 150 } 151 152 153 /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro 154 /// named MacroName with the max value for a type with width 'TypeWidth' a 155 /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL). 156 static void DefineTypeSize(StringRef MacroName, unsigned TypeWidth, 157 StringRef ValSuffix, bool isSigned, 158 MacroBuilder &Builder) { 159 llvm::APInt MaxVal = isSigned ? llvm::APInt::getSignedMaxValue(TypeWidth) 160 : llvm::APInt::getMaxValue(TypeWidth); 161 Builder.defineMacro(MacroName, MaxVal.toString(10, isSigned) + ValSuffix); 162 } 163 164 /// DefineTypeSize - An overloaded helper that uses TargetInfo to determine 165 /// the width, suffix, and signedness of the given type 166 static void DefineTypeSize(StringRef MacroName, TargetInfo::IntType Ty, 167 const TargetInfo &TI, MacroBuilder &Builder) { 168 DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty), 169 TI.isTypeSigned(Ty), Builder); 170 } 171 172 static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty, 173 MacroBuilder &Builder) { 174 Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty)); 175 } 176 177 static void DefineTypeWidth(StringRef MacroName, TargetInfo::IntType Ty, 178 const TargetInfo &TI, MacroBuilder &Builder) { 179 Builder.defineMacro(MacroName, Twine(TI.getTypeWidth(Ty))); 180 } 181 182 static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth, 183 const TargetInfo &TI, MacroBuilder &Builder) { 184 Builder.defineMacro(MacroName, 185 Twine(BitWidth / TI.getCharWidth())); 186 } 187 188 static void DefineExactWidthIntType(TargetInfo::IntType Ty, 189 const TargetInfo &TI, MacroBuilder &Builder) { 190 int TypeWidth = TI.getTypeWidth(Ty); 191 192 // Use the target specified int64 type, when appropriate, so that [u]int64_t 193 // ends up being defined in terms of the correct type. 194 if (TypeWidth == 64) 195 Ty = TI.getInt64Type(); 196 197 DefineType("__INT" + Twine(TypeWidth) + "_TYPE__", Ty, Builder); 198 199 StringRef ConstSuffix(TargetInfo::getTypeConstantSuffix(Ty)); 200 if (!ConstSuffix.empty()) 201 Builder.defineMacro("__INT" + Twine(TypeWidth) + "_C_SUFFIX__", 202 ConstSuffix); 203 } 204 205 /// Get the value the ATOMIC_*_LOCK_FREE macro should have for a type with 206 /// the specified properties. 207 static const char *getLockFreeValue(unsigned TypeWidth, unsigned TypeAlign, 208 unsigned InlineWidth) { 209 // Fully-aligned, power-of-2 sizes no larger than the inline 210 // width will be inlined as lock-free operations. 211 if (TypeWidth == TypeAlign && (TypeWidth & (TypeWidth - 1)) == 0 && 212 TypeWidth <= InlineWidth) 213 return "2"; // "always lock free" 214 // We cannot be certain what operations the lib calls might be 215 // able to implement as lock-free on future processors. 216 return "1"; // "sometimes lock free" 217 } 218 219 /// \brief Add definitions required for a smooth interaction between 220 /// Objective-C++ automated reference counting and libstdc++ (4.2). 221 static void AddObjCXXARCLibstdcxxDefines(const LangOptions &LangOpts, 222 MacroBuilder &Builder) { 223 Builder.defineMacro("_GLIBCXX_PREDEFINED_OBJC_ARC_IS_SCALAR"); 224 225 std::string Result; 226 { 227 // Provide specializations for the __is_scalar type trait so that 228 // lifetime-qualified objects are not considered "scalar" types, which 229 // libstdc++ uses as an indicator of the presence of trivial copy, assign, 230 // default-construct, and destruct semantics (none of which hold for 231 // lifetime-qualified objects in ARC). 232 llvm::raw_string_ostream Out(Result); 233 234 Out << "namespace std {\n" 235 << "\n" 236 << "struct __true_type;\n" 237 << "struct __false_type;\n" 238 << "\n"; 239 240 Out << "template<typename _Tp> struct __is_scalar;\n" 241 << "\n"; 242 243 Out << "template<typename _Tp>\n" 244 << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> {\n" 245 << " enum { __value = 0 };\n" 246 << " typedef __false_type __type;\n" 247 << "};\n" 248 << "\n"; 249 250 if (LangOpts.ObjCRuntimeHasWeak) { 251 Out << "template<typename _Tp>\n" 252 << "struct __is_scalar<__attribute__((objc_ownership(weak))) _Tp> {\n" 253 << " enum { __value = 0 };\n" 254 << " typedef __false_type __type;\n" 255 << "};\n" 256 << "\n"; 257 } 258 259 Out << "template<typename _Tp>\n" 260 << "struct __is_scalar<__attribute__((objc_ownership(autoreleasing)))" 261 << " _Tp> {\n" 262 << " enum { __value = 0 };\n" 263 << " typedef __false_type __type;\n" 264 << "};\n" 265 << "\n"; 266 267 Out << "}\n"; 268 } 269 Builder.append(Result); 270 } 271 272 static void InitializeStandardPredefinedMacros(const TargetInfo &TI, 273 const LangOptions &LangOpts, 274 const FrontendOptions &FEOpts, 275 MacroBuilder &Builder) { 276 if (!LangOpts.MicrosoftMode && !LangOpts.TraditionalCPP) 277 Builder.defineMacro("__STDC__"); 278 if (LangOpts.Freestanding) 279 Builder.defineMacro("__STDC_HOSTED__", "0"); 280 else 281 Builder.defineMacro("__STDC_HOSTED__"); 282 283 if (!LangOpts.CPlusPlus) { 284 if (LangOpts.C11) 285 Builder.defineMacro("__STDC_VERSION__", "201112L"); 286 else if (LangOpts.C99) 287 Builder.defineMacro("__STDC_VERSION__", "199901L"); 288 else if (!LangOpts.GNUMode && LangOpts.Digraphs) 289 Builder.defineMacro("__STDC_VERSION__", "199409L"); 290 } else { 291 // C++11 [cpp.predefined]p1: 292 // The name __cplusplus is defined to the value 201103L when compiling a 293 // C++ translation unit. 294 if (LangOpts.CPlusPlus0x) 295 Builder.defineMacro("__cplusplus", "201103L"); 296 // C++03 [cpp.predefined]p1: 297 // The name __cplusplus is defined to the value 199711L when compiling a 298 // C++ translation unit. 299 else 300 Builder.defineMacro("__cplusplus", "199711L"); 301 } 302 303 if (LangOpts.ObjC1) 304 Builder.defineMacro("__OBJC__"); 305 306 // Not "standard" per se, but available even with the -undef flag. 307 if (LangOpts.AsmPreprocessor) 308 Builder.defineMacro("__ASSEMBLER__"); 309 } 310 311 static void InitializePredefinedMacros(const TargetInfo &TI, 312 const LangOptions &LangOpts, 313 const FrontendOptions &FEOpts, 314 MacroBuilder &Builder) { 315 // Compiler version introspection macros. 316 Builder.defineMacro("__llvm__"); // LLVM Backend 317 Builder.defineMacro("__clang__"); // Clang Frontend 318 #define TOSTR2(X) #X 319 #define TOSTR(X) TOSTR2(X) 320 Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR)); 321 Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR)); 322 #ifdef CLANG_VERSION_PATCHLEVEL 323 Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL)); 324 #else 325 Builder.defineMacro("__clang_patchlevel__", "0"); 326 #endif 327 Builder.defineMacro("__clang_version__", 328 "\"" CLANG_VERSION_STRING " (" 329 + getClangFullRepositoryVersion() + ")\""); 330 #undef TOSTR 331 #undef TOSTR2 332 if (!LangOpts.MicrosoftMode) { 333 // Currently claim to be compatible with GCC 4.2.1-5621, but only if we're 334 // not compiling for MSVC compatibility 335 Builder.defineMacro("__GNUC_MINOR__", "2"); 336 Builder.defineMacro("__GNUC_PATCHLEVEL__", "1"); 337 Builder.defineMacro("__GNUC__", "4"); 338 Builder.defineMacro("__GXX_ABI_VERSION", "1002"); 339 } 340 341 // Define macros for the C11 / C++11 memory orderings 342 Builder.defineMacro("__ATOMIC_RELAXED", "0"); 343 Builder.defineMacro("__ATOMIC_CONSUME", "1"); 344 Builder.defineMacro("__ATOMIC_ACQUIRE", "2"); 345 Builder.defineMacro("__ATOMIC_RELEASE", "3"); 346 Builder.defineMacro("__ATOMIC_ACQ_REL", "4"); 347 Builder.defineMacro("__ATOMIC_SEQ_CST", "5"); 348 349 // Support for #pragma redefine_extname (Sun compatibility) 350 Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1"); 351 352 // As sad as it is, enough software depends on the __VERSION__ for version 353 // checks that it is necessary to report 4.2.1 (the base GCC version we claim 354 // compatibility with) first. 355 Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible " + 356 Twine(getClangFullCPPVersion()) + "\""); 357 358 // Initialize language-specific preprocessor defines. 359 360 // Standard conforming mode? 361 if (!LangOpts.GNUMode) 362 Builder.defineMacro("__STRICT_ANSI__"); 363 364 if (LangOpts.CPlusPlus0x) 365 Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__"); 366 367 if (LangOpts.ObjC1) { 368 if (LangOpts.ObjCNonFragileABI) { 369 Builder.defineMacro("__OBJC2__"); 370 371 if (LangOpts.ObjCExceptions) 372 Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS"); 373 } 374 375 if (LangOpts.getGC() != LangOptions::NonGC) 376 Builder.defineMacro("__OBJC_GC__"); 377 378 if (LangOpts.NeXTRuntime) 379 Builder.defineMacro("__NEXT_RUNTIME__"); 380 } 381 382 // darwin_constant_cfstrings controls this. This is also dependent 383 // on other things like the runtime I believe. This is set even for C code. 384 if (!LangOpts.NoConstantCFStrings) 385 Builder.defineMacro("__CONSTANT_CFSTRINGS__"); 386 387 if (LangOpts.ObjC2) 388 Builder.defineMacro("OBJC_NEW_PROPERTIES"); 389 390 if (LangOpts.PascalStrings) 391 Builder.defineMacro("__PASCAL_STRINGS__"); 392 393 if (LangOpts.Blocks) { 394 Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))"); 395 Builder.defineMacro("__BLOCKS__"); 396 } 397 398 if (LangOpts.CXXExceptions) 399 Builder.defineMacro("__EXCEPTIONS"); 400 if (LangOpts.RTTI) 401 Builder.defineMacro("__GXX_RTTI"); 402 if (LangOpts.SjLjExceptions) 403 Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__"); 404 405 if (LangOpts.Deprecated) 406 Builder.defineMacro("__DEPRECATED"); 407 408 if (LangOpts.CPlusPlus) { 409 Builder.defineMacro("__GNUG__", "4"); 410 Builder.defineMacro("__GXX_WEAK__"); 411 Builder.defineMacro("__private_extern__", "extern"); 412 } 413 414 if (LangOpts.MicrosoftExt) { 415 // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however 416 // VC++ appears to only like __FUNCTION__. 417 Builder.defineMacro("__PRETTY_FUNCTION__", "__FUNCTION__"); 418 // Work around some issues with Visual C++ headerws. 419 if (LangOpts.CPlusPlus) { 420 // Since we define wchar_t in C++ mode. 421 Builder.defineMacro("_WCHAR_T_DEFINED"); 422 Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED"); 423 // FIXME: Support Microsoft's __identifier extension in the lexer. 424 Builder.append("#define __identifier(x) x"); 425 Builder.append("class type_info;"); 426 } 427 428 if (LangOpts.CPlusPlus0x) { 429 Builder.defineMacro("_HAS_CHAR16_T_LANGUAGE_SUPPORT", "1"); 430 } 431 } 432 433 if (LangOpts.Optimize) 434 Builder.defineMacro("__OPTIMIZE__"); 435 if (LangOpts.OptimizeSize) 436 Builder.defineMacro("__OPTIMIZE_SIZE__"); 437 438 if (LangOpts.FastMath) 439 Builder.defineMacro("__FAST_MATH__"); 440 441 // Initialize target-specific preprocessor defines. 442 443 // Define type sizing macros based on the target properties. 444 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far"); 445 Builder.defineMacro("__CHAR_BIT__", "8"); 446 447 DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Builder); 448 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder); 449 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder); 450 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder); 451 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder); 452 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder); 453 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder); 454 455 DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder); 456 DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder); 457 DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder); 458 DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder); 459 DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder); 460 DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder); 461 DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder); 462 DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder); 463 DefineTypeSizeof("__SIZEOF_PTRDIFF_T__", 464 TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder); 465 DefineTypeSizeof("__SIZEOF_SIZE_T__", 466 TI.getTypeWidth(TI.getSizeType()), TI, Builder); 467 DefineTypeSizeof("__SIZEOF_WCHAR_T__", 468 TI.getTypeWidth(TI.getWCharType()), TI, Builder); 469 DefineTypeSizeof("__SIZEOF_WINT_T__", 470 TI.getTypeWidth(TI.getWIntType()), TI, Builder); 471 472 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder); 473 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder); 474 DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder); 475 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder); 476 DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder); 477 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder); 478 DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder); 479 DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder); 480 DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder); 481 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder); 482 DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder); 483 DefineType("__WINT_TYPE__", TI.getWIntType(), Builder); 484 DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder); 485 DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder); 486 DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder); 487 DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder); 488 489 DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat()); 490 DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat()); 491 DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat()); 492 493 // Define a __POINTER_WIDTH__ macro for stdint.h. 494 Builder.defineMacro("__POINTER_WIDTH__", 495 Twine((int)TI.getPointerWidth(0))); 496 497 if (!LangOpts.CharIsSigned) 498 Builder.defineMacro("__CHAR_UNSIGNED__"); 499 500 if (!TargetInfo::isTypeSigned(TI.getWCharType())) 501 Builder.defineMacro("__WCHAR_UNSIGNED__"); 502 503 if (!TargetInfo::isTypeSigned(TI.getWIntType())) 504 Builder.defineMacro("__WINT_UNSIGNED__"); 505 506 // Define exact-width integer types for stdint.h 507 Builder.defineMacro("__INT" + Twine(TI.getCharWidth()) + "_TYPE__", 508 "char"); 509 510 if (TI.getShortWidth() > TI.getCharWidth()) 511 DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder); 512 513 if (TI.getIntWidth() > TI.getShortWidth()) 514 DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder); 515 516 if (TI.getLongWidth() > TI.getIntWidth()) 517 DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder); 518 519 if (TI.getLongLongWidth() > TI.getLongWidth()) 520 DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder); 521 522 if (const char *Prefix = TI.getUserLabelPrefix()) 523 Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix); 524 525 // Build configuration options. FIXME: these should be controlled by 526 // command line options or something. 527 Builder.defineMacro("__FINITE_MATH_ONLY__", "0"); 528 529 if (LangOpts.GNUInline) 530 Builder.defineMacro("__GNUC_GNU_INLINE__"); 531 else 532 Builder.defineMacro("__GNUC_STDC_INLINE__"); 533 534 // The value written by __atomic_test_and_set. 535 // FIXME: This is target-dependent. 536 Builder.defineMacro("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1"); 537 538 // Used by libstdc++ to implement ATOMIC_<foo>_LOCK_FREE. 539 unsigned InlineWidthBits = TI.getMaxAtomicInlineWidth(); 540 #define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \ 541 Builder.defineMacro("__GCC_ATOMIC_" #TYPE "_LOCK_FREE", \ 542 getLockFreeValue(TI.get##Type##Width(), \ 543 TI.get##Type##Align(), \ 544 InlineWidthBits)); 545 DEFINE_LOCK_FREE_MACRO(BOOL, Bool); 546 DEFINE_LOCK_FREE_MACRO(CHAR, Char); 547 DEFINE_LOCK_FREE_MACRO(CHAR16_T, Char16); 548 DEFINE_LOCK_FREE_MACRO(CHAR32_T, Char32); 549 DEFINE_LOCK_FREE_MACRO(WCHAR_T, WChar); 550 DEFINE_LOCK_FREE_MACRO(SHORT, Short); 551 DEFINE_LOCK_FREE_MACRO(INT, Int); 552 DEFINE_LOCK_FREE_MACRO(LONG, Long); 553 DEFINE_LOCK_FREE_MACRO(LLONG, LongLong); 554 Builder.defineMacro("__GCC_ATOMIC_POINTER_LOCK_FREE", 555 getLockFreeValue(TI.getPointerWidth(0), 556 TI.getPointerAlign(0), 557 InlineWidthBits)); 558 #undef DEFINE_LOCK_FREE_MACRO 559 560 if (LangOpts.NoInlineDefine) 561 Builder.defineMacro("__NO_INLINE__"); 562 563 if (unsigned PICLevel = LangOpts.PICLevel) { 564 Builder.defineMacro("__PIC__", Twine(PICLevel)); 565 Builder.defineMacro("__pic__", Twine(PICLevel)); 566 } 567 if (unsigned PIELevel = LangOpts.PIELevel) { 568 Builder.defineMacro("__PIE__", Twine(PIELevel)); 569 Builder.defineMacro("__pie__", Twine(PIELevel)); 570 } 571 572 // Macros to control C99 numerics and <float.h> 573 Builder.defineMacro("__FLT_EVAL_METHOD__", Twine(TI.getFloatEvalMethod())); 574 Builder.defineMacro("__FLT_RADIX__", "2"); 575 int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36); 576 Builder.defineMacro("__DECIMAL_DIG__", Twine(Dig)); 577 578 if (LangOpts.getStackProtector() == LangOptions::SSPOn) 579 Builder.defineMacro("__SSP__"); 580 else if (LangOpts.getStackProtector() == LangOptions::SSPReq) 581 Builder.defineMacro("__SSP_ALL__", "2"); 582 583 if (FEOpts.ProgramAction == frontend::RewriteObjC) 584 Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))"); 585 586 // Define a macro that exists only when using the static analyzer. 587 if (FEOpts.ProgramAction == frontend::RunAnalysis) 588 Builder.defineMacro("__clang_analyzer__"); 589 590 if (LangOpts.FastRelaxedMath) 591 Builder.defineMacro("__FAST_RELAXED_MATH__"); 592 593 if (LangOpts.ObjCAutoRefCount) { 594 Builder.defineMacro("__weak", "__attribute__((objc_ownership(weak)))"); 595 Builder.defineMacro("__strong", "__attribute__((objc_ownership(strong)))"); 596 Builder.defineMacro("__autoreleasing", 597 "__attribute__((objc_ownership(autoreleasing)))"); 598 Builder.defineMacro("__unsafe_unretained", 599 "__attribute__((objc_ownership(none)))"); 600 } 601 602 // Get other target #defines. 603 TI.getTargetDefines(LangOpts, Builder); 604 } 605 606 // Initialize the remapping of files to alternative contents, e.g., 607 // those specified through other files. 608 static void InitializeFileRemapping(DiagnosticsEngine &Diags, 609 SourceManager &SourceMgr, 610 FileManager &FileMgr, 611 const PreprocessorOptions &InitOpts) { 612 // Remap files in the source manager (with buffers). 613 for (PreprocessorOptions::const_remapped_file_buffer_iterator 614 Remap = InitOpts.remapped_file_buffer_begin(), 615 RemapEnd = InitOpts.remapped_file_buffer_end(); 616 Remap != RemapEnd; 617 ++Remap) { 618 // Create the file entry for the file that we're mapping from. 619 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first, 620 Remap->second->getBufferSize(), 621 0); 622 if (!FromFile) { 623 Diags.Report(diag::err_fe_remap_missing_from_file) 624 << Remap->first; 625 if (!InitOpts.RetainRemappedFileBuffers) 626 delete Remap->second; 627 continue; 628 } 629 630 // Override the contents of the "from" file with the contents of 631 // the "to" file. 632 SourceMgr.overrideFileContents(FromFile, Remap->second, 633 InitOpts.RetainRemappedFileBuffers); 634 } 635 636 // Remap files in the source manager (with other files). 637 for (PreprocessorOptions::const_remapped_file_iterator 638 Remap = InitOpts.remapped_file_begin(), 639 RemapEnd = InitOpts.remapped_file_end(); 640 Remap != RemapEnd; 641 ++Remap) { 642 // Find the file that we're mapping to. 643 const FileEntry *ToFile = FileMgr.getFile(Remap->second); 644 if (!ToFile) { 645 Diags.Report(diag::err_fe_remap_missing_to_file) 646 << Remap->first << Remap->second; 647 continue; 648 } 649 650 // Create the file entry for the file that we're mapping from. 651 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first, 652 ToFile->getSize(), 0); 653 if (!FromFile) { 654 Diags.Report(diag::err_fe_remap_missing_from_file) 655 << Remap->first; 656 continue; 657 } 658 659 // Override the contents of the "from" file with the contents of 660 // the "to" file. 661 SourceMgr.overrideFileContents(FromFile, ToFile); 662 } 663 664 SourceMgr.setOverridenFilesKeepOriginalName( 665 InitOpts.RemappedFilesKeepOriginalName); 666 } 667 668 /// InitializePreprocessor - Initialize the preprocessor getting it and the 669 /// environment ready to process a single file. This returns true on error. 670 /// 671 void clang::InitializePreprocessor(Preprocessor &PP, 672 const PreprocessorOptions &InitOpts, 673 const HeaderSearchOptions &HSOpts, 674 const FrontendOptions &FEOpts) { 675 const LangOptions &LangOpts = PP.getLangOpts(); 676 std::string PredefineBuffer; 677 PredefineBuffer.reserve(4080); 678 llvm::raw_string_ostream Predefines(PredefineBuffer); 679 MacroBuilder Builder(Predefines); 680 681 InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(), 682 PP.getFileManager(), InitOpts); 683 684 // Emit line markers for various builtin sections of the file. We don't do 685 // this in asm preprocessor mode, because "# 4" is not a line marker directive 686 // in this mode. 687 if (!PP.getLangOpts().AsmPreprocessor) 688 Builder.append("# 1 \"<built-in>\" 3"); 689 690 // Install things like __POWERPC__, __GNUC__, etc into the macro table. 691 if (InitOpts.UsePredefines) { 692 InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, Builder); 693 694 // Install definitions to make Objective-C++ ARC work well with various 695 // C++ Standard Library implementations. 696 if (LangOpts.ObjC1 && LangOpts.CPlusPlus && LangOpts.ObjCAutoRefCount) { 697 switch (InitOpts.ObjCXXARCStandardLibrary) { 698 case ARCXX_nolib: 699 case ARCXX_libcxx: 700 break; 701 702 case ARCXX_libstdcxx: 703 AddObjCXXARCLibstdcxxDefines(LangOpts, Builder); 704 break; 705 } 706 } 707 } 708 709 // Even with predefines off, some macros are still predefined. 710 // These should all be defined in the preprocessor according to the 711 // current language configuration. 712 InitializeStandardPredefinedMacros(PP.getTargetInfo(), PP.getLangOpts(), 713 FEOpts, Builder); 714 715 // Add on the predefines from the driver. Wrap in a #line directive to report 716 // that they come from the command line. 717 if (!PP.getLangOpts().AsmPreprocessor) 718 Builder.append("# 1 \"<command line>\" 1"); 719 720 // Process #define's and #undef's in the order they are given. 721 for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) { 722 if (InitOpts.Macros[i].second) // isUndef 723 Builder.undefineMacro(InitOpts.Macros[i].first); 724 else 725 DefineBuiltinMacro(Builder, InitOpts.Macros[i].first, 726 PP.getDiagnostics()); 727 } 728 729 // If -imacros are specified, include them now. These are processed before 730 // any -include directives. 731 for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i) 732 AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i], 733 PP.getFileManager()); 734 735 // Process -include directives. 736 for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) { 737 const std::string &Path = InitOpts.Includes[i]; 738 if (Path == InitOpts.ImplicitPTHInclude) 739 AddImplicitIncludePTH(Builder, PP, Path); 740 else 741 AddImplicitInclude(Builder, Path, PP.getFileManager()); 742 } 743 744 // Exit the command line and go back to <built-in> (2 is LC_LEAVE). 745 if (!PP.getLangOpts().AsmPreprocessor) 746 Builder.append("# 1 \"<built-in>\" 2"); 747 748 // Instruct the preprocessor to skip the preamble. 749 PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first, 750 InitOpts.PrecompiledPreambleBytes.second); 751 752 // Copy PredefinedBuffer into the Preprocessor. 753 PP.setPredefines(Predefines.str()); 754 755 // Initialize the header search object. 756 ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts, 757 PP.getLangOpts(), 758 PP.getTargetInfo().getTriple()); 759 } 760