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