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/Preprocessor.h" 22 #include "clang/Basic/FileManager.h" 23 #include "clang/Basic/SourceManager.h" 24 #include "llvm/ADT/APFloat.h" 25 #include "llvm/Support/FileSystem.h" 26 #include "llvm/Support/MemoryBuffer.h" 27 #include "llvm/Support/Path.h" 28 using namespace clang; 29 30 // Append a #define line to Buf for Macro. Macro should be of the form XXX, 31 // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit 32 // "#define XXX Y z W". To get a #define with no value, use "XXX=". 33 static void DefineBuiltinMacro(MacroBuilder &Builder, llvm::StringRef Macro, 34 Diagnostic &Diags) { 35 std::pair<llvm::StringRef, llvm::StringRef> MacroPair = Macro.split('='); 36 llvm::StringRef MacroName = MacroPair.first; 37 llvm::StringRef MacroBody = MacroPair.second; 38 if (MacroName.size() != Macro.size()) { 39 // Per GCC -D semantics, the macro ends at \n if it exists. 40 llvm::StringRef::size_type End = MacroBody.find_first_of("\n\r"); 41 if (End != llvm::StringRef::npos) 42 Diags.Report(diag::warn_fe_macro_contains_embedded_newline) 43 << MacroName; 44 Builder.defineMacro(MacroName, MacroBody.substr(0, End)); 45 } else { 46 // Push "macroname 1". 47 Builder.defineMacro(Macro); 48 } 49 } 50 51 std::string clang::NormalizeDashIncludePath(llvm::StringRef File, 52 FileManager &FileMgr) { 53 // Implicit include paths should be resolved relative to the current 54 // working directory first, and then use the regular header search 55 // mechanism. The proper way to handle this is to have the 56 // predefines buffer located at the current working directory, but 57 // it has no file entry. For now, workaround this by using an 58 // absolute path if we find the file here, and otherwise letting 59 // header search handle it. 60 llvm::SmallString<128> Path(File); 61 llvm::sys::fs::make_absolute(Path); 62 bool exists; 63 if (llvm::sys::fs::exists(Path.str(), exists) || !exists) 64 Path = File; 65 else if (exists) 66 FileMgr.getFile(File); 67 68 return Lexer::Stringify(Path.str()); 69 } 70 71 /// AddImplicitInclude - Add an implicit #include of the specified file to the 72 /// predefines buffer. 73 static void AddImplicitInclude(MacroBuilder &Builder, llvm::StringRef File, 74 FileManager &FileMgr) { 75 Builder.append("#include \"" + 76 llvm::Twine(NormalizeDashIncludePath(File, FileMgr)) + "\""); 77 } 78 79 static void AddImplicitIncludeMacros(MacroBuilder &Builder, 80 llvm::StringRef File, 81 FileManager &FileMgr) { 82 Builder.append("#__include_macros \"" + 83 llvm::Twine(NormalizeDashIncludePath(File, FileMgr)) + "\""); 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 llvm::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() : 0; 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, PP.getFileManager()); 103 } 104 105 /// PickFP - This is used to pick a value based on the FP semantics of the 106 /// specified FP model. 107 template <typename T> 108 static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal, 109 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal, 110 T IEEEQuadVal) { 111 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle) 112 return IEEESingleVal; 113 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble) 114 return IEEEDoubleVal; 115 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended) 116 return X87DoubleExtendedVal; 117 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble) 118 return PPCDoubleDoubleVal; 119 assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad); 120 return IEEEQuadVal; 121 } 122 123 static void DefineFloatMacros(MacroBuilder &Builder, llvm::StringRef Prefix, 124 const llvm::fltSemantics *Sem) { 125 const char *DenormMin, *Epsilon, *Max, *Min; 126 DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324", 127 "3.64519953188247460253e-4951L", 128 "4.94065645841246544176568792868221e-324L", 129 "6.47517511943802511092443895822764655e-4966L"); 130 int Digits = PickFP(Sem, 6, 15, 18, 31, 33); 131 Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16", 132 "1.08420217248550443401e-19L", 133 "4.94065645841246544176568792868221e-324L", 134 "1.92592994438723585305597794258492732e-34L"); 135 int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113); 136 int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931); 137 int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932); 138 int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381); 139 int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384); 140 Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308", 141 "3.36210314311209350626e-4932L", 142 "2.00416836000897277799610805135016e-292L", 143 "3.36210314311209350626267781732175260e-4932L"); 144 Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308", 145 "1.18973149535723176502e+4932L", 146 "1.79769313486231580793728971405301e+308L", 147 "1.18973149535723176508575932662800702e+4932L"); 148 149 llvm::SmallString<32> DefPrefix; 150 DefPrefix = "__"; 151 DefPrefix += Prefix; 152 DefPrefix += "_"; 153 154 Builder.defineMacro(DefPrefix + "DENORM_MIN__", DenormMin); 155 Builder.defineMacro(DefPrefix + "HAS_DENORM__"); 156 Builder.defineMacro(DefPrefix + "DIG__", llvm::Twine(Digits)); 157 Builder.defineMacro(DefPrefix + "EPSILON__", llvm::Twine(Epsilon)); 158 Builder.defineMacro(DefPrefix + "HAS_INFINITY__"); 159 Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__"); 160 Builder.defineMacro(DefPrefix + "MANT_DIG__", llvm::Twine(MantissaDigits)); 161 162 Builder.defineMacro(DefPrefix + "MAX_10_EXP__", llvm::Twine(Max10Exp)); 163 Builder.defineMacro(DefPrefix + "MAX_EXP__", llvm::Twine(MaxExp)); 164 Builder.defineMacro(DefPrefix + "MAX__", llvm::Twine(Max)); 165 166 Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+llvm::Twine(Min10Exp)+")"); 167 Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+llvm::Twine(MinExp)+")"); 168 Builder.defineMacro(DefPrefix + "MIN__", llvm::Twine(Min)); 169 } 170 171 172 /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro 173 /// named MacroName with the max value for a type with width 'TypeWidth' a 174 /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL). 175 static void DefineTypeSize(llvm::StringRef MacroName, unsigned TypeWidth, 176 llvm::StringRef ValSuffix, bool isSigned, 177 MacroBuilder &Builder) { 178 llvm::APInt MaxVal = isSigned ? llvm::APInt::getSignedMaxValue(TypeWidth) 179 : llvm::APInt::getMaxValue(TypeWidth); 180 Builder.defineMacro(MacroName, MaxVal.toString(10, isSigned) + ValSuffix); 181 } 182 183 /// DefineTypeSize - An overloaded helper that uses TargetInfo to determine 184 /// the width, suffix, and signedness of the given type 185 static void DefineTypeSize(llvm::StringRef MacroName, TargetInfo::IntType Ty, 186 const TargetInfo &TI, MacroBuilder &Builder) { 187 DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty), 188 TI.isTypeSigned(Ty), Builder); 189 } 190 191 static void DefineType(const llvm::Twine &MacroName, TargetInfo::IntType Ty, 192 MacroBuilder &Builder) { 193 Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty)); 194 } 195 196 static void DefineTypeWidth(llvm::StringRef MacroName, TargetInfo::IntType Ty, 197 const TargetInfo &TI, MacroBuilder &Builder) { 198 Builder.defineMacro(MacroName, llvm::Twine(TI.getTypeWidth(Ty))); 199 } 200 201 static void DefineTypeSizeof(llvm::StringRef MacroName, unsigned BitWidth, 202 const TargetInfo &TI, MacroBuilder &Builder) { 203 Builder.defineMacro(MacroName, 204 llvm::Twine(BitWidth / TI.getCharWidth())); 205 } 206 207 static void DefineExactWidthIntType(TargetInfo::IntType Ty, 208 const TargetInfo &TI, MacroBuilder &Builder) { 209 int TypeWidth = TI.getTypeWidth(Ty); 210 211 // Use the target specified int64 type, when appropriate, so that [u]int64_t 212 // ends up being defined in terms of the correct type. 213 if (TypeWidth == 64) 214 Ty = TI.getInt64Type(); 215 216 DefineType("__INT" + llvm::Twine(TypeWidth) + "_TYPE__", Ty, Builder); 217 218 llvm::StringRef ConstSuffix(TargetInfo::getTypeConstantSuffix(Ty)); 219 if (!ConstSuffix.empty()) 220 Builder.defineMacro("__INT" + llvm::Twine(TypeWidth) + "_C_SUFFIX__", 221 ConstSuffix); 222 } 223 224 static void InitializePredefinedMacros(const TargetInfo &TI, 225 const LangOptions &LangOpts, 226 const FrontendOptions &FEOpts, 227 MacroBuilder &Builder) { 228 // Compiler version introspection macros. 229 Builder.defineMacro("__llvm__"); // LLVM Backend 230 Builder.defineMacro("__clang__"); // Clang Frontend 231 #define TOSTR2(X) #X 232 #define TOSTR(X) TOSTR2(X) 233 Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR)); 234 Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR)); 235 #ifdef CLANG_VERSION_PATCHLEVEL 236 Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL)); 237 #else 238 Builder.defineMacro("__clang_patchlevel__", "0"); 239 #endif 240 Builder.defineMacro("__clang_version__", 241 "\"" CLANG_VERSION_STRING " (" 242 + getClangFullRepositoryVersion() + ")\""); 243 #undef TOSTR 244 #undef TOSTR2 245 // Currently claim to be compatible with GCC 4.2.1-5621. 246 Builder.defineMacro("__GNUC_MINOR__", "2"); 247 Builder.defineMacro("__GNUC_PATCHLEVEL__", "1"); 248 Builder.defineMacro("__GNUC__", "4"); 249 Builder.defineMacro("__GXX_ABI_VERSION", "1002"); 250 251 // As sad as it is, enough software depends on the __VERSION__ for version 252 // checks that it is necessary to report 4.2.1 (the base GCC version we claim 253 // compatibility with) first. 254 Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible " + 255 llvm::Twine(getClangFullCPPVersion()) + "\""); 256 257 // Initialize language-specific preprocessor defines. 258 259 // These should all be defined in the preprocessor according to the 260 // current language configuration. 261 if (!LangOpts.Microsoft && !LangOpts.TraditionalCPP) 262 Builder.defineMacro("__STDC__"); 263 if (LangOpts.AsmPreprocessor) 264 Builder.defineMacro("__ASSEMBLER__"); 265 266 if (!LangOpts.CPlusPlus) { 267 if (LangOpts.C99) 268 Builder.defineMacro("__STDC_VERSION__", "199901L"); 269 else if (!LangOpts.GNUMode && LangOpts.Digraphs) 270 Builder.defineMacro("__STDC_VERSION__", "199409L"); 271 } 272 273 // Standard conforming mode? 274 if (!LangOpts.GNUMode) 275 Builder.defineMacro("__STRICT_ANSI__"); 276 277 if (LangOpts.CPlusPlus0x) 278 Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__"); 279 280 if (LangOpts.Freestanding) 281 Builder.defineMacro("__STDC_HOSTED__", "0"); 282 else 283 Builder.defineMacro("__STDC_HOSTED__"); 284 285 if (LangOpts.ObjC1) { 286 Builder.defineMacro("__OBJC__"); 287 if (LangOpts.ObjCNonFragileABI) { 288 Builder.defineMacro("__OBJC2__"); 289 Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS"); 290 } 291 292 if (LangOpts.getGCMode() != LangOptions::NonGC) 293 Builder.defineMacro("__OBJC_GC__"); 294 295 if (LangOpts.NeXTRuntime) 296 Builder.defineMacro("__NEXT_RUNTIME__"); 297 } 298 299 // darwin_constant_cfstrings controls this. This is also dependent 300 // on other things like the runtime I believe. This is set even for C code. 301 Builder.defineMacro("__CONSTANT_CFSTRINGS__"); 302 303 if (LangOpts.ObjC2) 304 Builder.defineMacro("OBJC_NEW_PROPERTIES"); 305 306 if (LangOpts.PascalStrings) 307 Builder.defineMacro("__PASCAL_STRINGS__"); 308 309 if (LangOpts.Blocks) { 310 Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))"); 311 Builder.defineMacro("__BLOCKS__"); 312 } 313 314 if (LangOpts.Exceptions) 315 Builder.defineMacro("__EXCEPTIONS"); 316 if (LangOpts.RTTI) 317 Builder.defineMacro("__GXX_RTTI"); 318 if (LangOpts.SjLjExceptions) 319 Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__"); 320 321 if (LangOpts.Deprecated) 322 Builder.defineMacro("__DEPRECATED"); 323 324 if (LangOpts.CPlusPlus) { 325 Builder.defineMacro("__GNUG__", "4"); 326 Builder.defineMacro("__GXX_WEAK__"); 327 if (LangOpts.GNUMode) 328 Builder.defineMacro("__cplusplus"); 329 else 330 // C++ [cpp.predefined]p1: 331 // The name_ _cplusplusis defined to the value 199711L when compiling a 332 // C++ translation unit. 333 Builder.defineMacro("__cplusplus", "199711L"); 334 Builder.defineMacro("__private_extern__", "extern"); 335 } 336 337 if (LangOpts.Microsoft) { 338 // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however 339 // VC++ appears to only like __FUNCTION__. 340 Builder.defineMacro("__PRETTY_FUNCTION__", "__FUNCTION__"); 341 // Work around some issues with Visual C++ headerws. 342 if (LangOpts.CPlusPlus) { 343 // Since we define wchar_t in C++ mode. 344 Builder.defineMacro("_WCHAR_T_DEFINED"); 345 Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED"); 346 Builder.append("#define __identifier(x) x"); 347 Builder.append("class type_info;"); 348 } 349 350 if (LangOpts.CPlusPlus0x) { 351 Builder.defineMacro("_HAS_CHAR16_T_LANGUAGE_SUPPORT", "1"); 352 } 353 } 354 355 if (LangOpts.Optimize) 356 Builder.defineMacro("__OPTIMIZE__"); 357 if (LangOpts.OptimizeSize) 358 Builder.defineMacro("__OPTIMIZE_SIZE__"); 359 360 // Initialize target-specific preprocessor defines. 361 362 // Define type sizing macros based on the target properties. 363 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far"); 364 Builder.defineMacro("__CHAR_BIT__", "8"); 365 366 DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Builder); 367 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder); 368 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder); 369 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder); 370 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder); 371 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder); 372 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder); 373 374 DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder); 375 DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder); 376 DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder); 377 DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder); 378 DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder); 379 DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder); 380 DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder); 381 DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder); 382 DefineTypeSizeof("__SIZEOF_PTRDIFF_T__", 383 TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder); 384 DefineTypeSizeof("__SIZEOF_SIZE_T__", 385 TI.getTypeWidth(TI.getSizeType()), TI, Builder); 386 DefineTypeSizeof("__SIZEOF_WCHAR_T__", 387 TI.getTypeWidth(TI.getWCharType()), TI, Builder); 388 DefineTypeSizeof("__SIZEOF_WINT_T__", 389 TI.getTypeWidth(TI.getWIntType()), TI, Builder); 390 391 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder); 392 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder); 393 DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder); 394 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder); 395 DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder); 396 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder); 397 DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder); 398 DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder); 399 DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder); 400 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder); 401 DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder); 402 DefineType("__WINT_TYPE__", TI.getWIntType(), Builder); 403 DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder); 404 DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder); 405 DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder); 406 DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder); 407 408 DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat()); 409 DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat()); 410 DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat()); 411 412 // Define a __POINTER_WIDTH__ macro for stdint.h. 413 Builder.defineMacro("__POINTER_WIDTH__", 414 llvm::Twine((int)TI.getPointerWidth(0))); 415 416 if (!LangOpts.CharIsSigned) 417 Builder.defineMacro("__CHAR_UNSIGNED__"); 418 419 if (!TargetInfo::isTypeSigned(TI.getWIntType())) 420 Builder.defineMacro("__WINT_UNSIGNED__"); 421 422 // Define exact-width integer types for stdint.h 423 Builder.defineMacro("__INT" + llvm::Twine(TI.getCharWidth()) + "_TYPE__", 424 "char"); 425 426 if (TI.getShortWidth() > TI.getCharWidth()) 427 DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder); 428 429 if (TI.getIntWidth() > TI.getShortWidth()) 430 DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder); 431 432 if (TI.getLongWidth() > TI.getIntWidth()) 433 DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder); 434 435 if (TI.getLongLongWidth() > TI.getLongWidth()) 436 DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder); 437 438 // Add __builtin_va_list typedef. 439 Builder.append(TI.getVAListDeclaration()); 440 441 if (const char *Prefix = TI.getUserLabelPrefix()) 442 Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix); 443 444 // Build configuration options. FIXME: these should be controlled by 445 // command line options or something. 446 Builder.defineMacro("__FINITE_MATH_ONLY__", "0"); 447 448 if (LangOpts.GNUInline) 449 Builder.defineMacro("__GNUC_GNU_INLINE__"); 450 else 451 Builder.defineMacro("__GNUC_STDC_INLINE__"); 452 453 if (LangOpts.NoInline) 454 Builder.defineMacro("__NO_INLINE__"); 455 456 if (unsigned PICLevel = LangOpts.PICLevel) { 457 Builder.defineMacro("__PIC__", llvm::Twine(PICLevel)); 458 Builder.defineMacro("__pic__", llvm::Twine(PICLevel)); 459 } 460 461 // Macros to control C99 numerics and <float.h> 462 Builder.defineMacro("__FLT_EVAL_METHOD__", "0"); 463 Builder.defineMacro("__FLT_RADIX__", "2"); 464 int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36); 465 Builder.defineMacro("__DECIMAL_DIG__", llvm::Twine(Dig)); 466 467 if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn) 468 Builder.defineMacro("__SSP__"); 469 else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq) 470 Builder.defineMacro("__SSP_ALL__", "2"); 471 472 if (FEOpts.ProgramAction == frontend::RewriteObjC) 473 Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))"); 474 475 // Define a macro that exists only when using the static analyzer. 476 if (FEOpts.ProgramAction == frontend::RunAnalysis) 477 Builder.defineMacro("__clang_analyzer__"); 478 479 if (LangOpts.FastRelaxedMath) 480 Builder.defineMacro("__FAST_RELAXED_MATH__"); 481 482 // Get other target #defines. 483 TI.getTargetDefines(LangOpts, Builder); 484 } 485 486 // Initialize the remapping of files to alternative contents, e.g., 487 // those specified through other files. 488 static void InitializeFileRemapping(Diagnostic &Diags, 489 SourceManager &SourceMgr, 490 FileManager &FileMgr, 491 const PreprocessorOptions &InitOpts) { 492 // Remap files in the source manager (with buffers). 493 for (PreprocessorOptions::const_remapped_file_buffer_iterator 494 Remap = InitOpts.remapped_file_buffer_begin(), 495 RemapEnd = InitOpts.remapped_file_buffer_end(); 496 Remap != RemapEnd; 497 ++Remap) { 498 // Create the file entry for the file that we're mapping from. 499 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first, 500 Remap->second->getBufferSize(), 501 0); 502 if (!FromFile) { 503 Diags.Report(diag::err_fe_remap_missing_from_file) 504 << Remap->first; 505 if (!InitOpts.RetainRemappedFileBuffers) 506 delete Remap->second; 507 continue; 508 } 509 510 // Override the contents of the "from" file with the contents of 511 // the "to" file. 512 SourceMgr.overrideFileContents(FromFile, Remap->second, 513 InitOpts.RetainRemappedFileBuffers); 514 } 515 516 // Remap files in the source manager (with other files). 517 for (PreprocessorOptions::const_remapped_file_iterator 518 Remap = InitOpts.remapped_file_begin(), 519 RemapEnd = InitOpts.remapped_file_end(); 520 Remap != RemapEnd; 521 ++Remap) { 522 // Find the file that we're mapping to. 523 const FileEntry *ToFile = FileMgr.getFile(Remap->second); 524 if (!ToFile) { 525 Diags.Report(diag::err_fe_remap_missing_to_file) 526 << Remap->first << Remap->second; 527 continue; 528 } 529 530 // Create the file entry for the file that we're mapping from. 531 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first, 532 ToFile->getSize(), 0); 533 if (!FromFile) { 534 Diags.Report(diag::err_fe_remap_missing_from_file) 535 << Remap->first; 536 continue; 537 } 538 539 // Override the contents of the "from" file with the contents of 540 // the "to" file. 541 SourceMgr.overrideFileContents(FromFile, ToFile); 542 } 543 544 SourceMgr.setOverridenFilesKeepOriginalName( 545 InitOpts.RemappedFilesKeepOriginalName); 546 } 547 548 /// InitializePreprocessor - Initialize the preprocessor getting it and the 549 /// environment ready to process a single file. This returns true on error. 550 /// 551 void clang::InitializePreprocessor(Preprocessor &PP, 552 const PreprocessorOptions &InitOpts, 553 const HeaderSearchOptions &HSOpts, 554 const FrontendOptions &FEOpts) { 555 std::string PredefineBuffer; 556 PredefineBuffer.reserve(4080); 557 llvm::raw_string_ostream Predefines(PredefineBuffer); 558 MacroBuilder Builder(Predefines); 559 560 InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(), 561 PP.getFileManager(), InitOpts); 562 563 // Emit line markers for various builtin sections of the file. We don't do 564 // this in asm preprocessor mode, because "# 4" is not a line marker directive 565 // in this mode. 566 if (!PP.getLangOptions().AsmPreprocessor) 567 Builder.append("# 1 \"<built-in>\" 3"); 568 569 // Install things like __POWERPC__, __GNUC__, etc into the macro table. 570 if (InitOpts.UsePredefines) 571 InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(), 572 FEOpts, Builder); 573 574 // Add on the predefines from the driver. Wrap in a #line directive to report 575 // that they come from the command line. 576 if (!PP.getLangOptions().AsmPreprocessor) 577 Builder.append("# 1 \"<command line>\" 1"); 578 579 // Process #define's and #undef's in the order they are given. 580 for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) { 581 if (InitOpts.Macros[i].second) // isUndef 582 Builder.undefineMacro(InitOpts.Macros[i].first); 583 else 584 DefineBuiltinMacro(Builder, InitOpts.Macros[i].first, 585 PP.getDiagnostics()); 586 } 587 588 // If -imacros are specified, include them now. These are processed before 589 // any -include directives. 590 for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i) 591 AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i], 592 PP.getFileManager()); 593 594 // Process -include directives. 595 for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) { 596 const std::string &Path = InitOpts.Includes[i]; 597 if (Path == InitOpts.ImplicitPTHInclude) 598 AddImplicitIncludePTH(Builder, PP, Path); 599 else 600 AddImplicitInclude(Builder, Path, PP.getFileManager()); 601 } 602 603 // Exit the command line and go back to <built-in> (2 is LC_LEAVE). 604 if (!PP.getLangOptions().AsmPreprocessor) 605 Builder.append("# 1 \"<built-in>\" 2"); 606 607 // Instruct the preprocessor to skip the preamble. 608 PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first, 609 InitOpts.PrecompiledPreambleBytes.second); 610 611 // Copy PredefinedBuffer into the Preprocessor. 612 PP.setPredefines(Predefines.str()); 613 614 // Initialize the header search object. 615 ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts, 616 PP.getLangOptions(), 617 PP.getTargetInfo().getTriple()); 618 } 619