clang API Documentation

InitPreprocessor.cpp

Go to the documentation of this file.
00001 //===--- InitPreprocessor.cpp - PP initialization code. ---------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the clang::InitializePreprocessor function.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/Frontend/Utils.h"
00015 #include "clang/Basic/MacroBuilder.h"
00016 #include "clang/Basic/TargetInfo.h"
00017 #include "clang/Frontend/FrontendDiagnostic.h"
00018 #include "clang/Frontend/FrontendOptions.h"
00019 #include "clang/Frontend/PreprocessorOptions.h"
00020 #include "clang/Lex/Preprocessor.h"
00021 #include "clang/Basic/FileManager.h"
00022 #include "clang/Basic/SourceManager.h"
00023 #include "llvm/ADT/APFloat.h"
00024 #include "llvm/Support/MemoryBuffer.h"
00025 #include "llvm/System/Path.h"
00026 using namespace clang;
00027 
00028 // Append a #define line to Buf for Macro.  Macro should be of the form XXX,
00029 // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
00030 // "#define XXX Y z W".  To get a #define with no value, use "XXX=".
00031 static void DefineBuiltinMacro(MacroBuilder &Builder, llvm::StringRef Macro,
00032                                Diagnostic &Diags) {
00033   std::pair<llvm::StringRef, llvm::StringRef> MacroPair = Macro.split('=');
00034   llvm::StringRef MacroName = MacroPair.first;
00035   llvm::StringRef MacroBody = MacroPair.second;
00036   if (MacroName.size() != Macro.size()) {
00037     // Per GCC -D semantics, the macro ends at \n if it exists.
00038     llvm::StringRef::size_type End = MacroBody.find_first_of("\n\r");
00039     if (End != llvm::StringRef::npos)
00040       Diags.Report(diag::warn_fe_macro_contains_embedded_newline)
00041         << MacroName;
00042     Builder.defineMacro(MacroName, MacroBody.substr(0, End));
00043   } else {
00044     // Push "macroname 1".
00045     Builder.defineMacro(Macro);
00046   }
00047 }
00048 
00049 std::string clang::NormalizeDashIncludePath(llvm::StringRef File) {
00050   // Implicit include paths should be resolved relative to the current
00051   // working directory first, and then use the regular header search
00052   // mechanism. The proper way to handle this is to have the
00053   // predefines buffer located at the current working directory, but
00054   // it has not file entry. For now, workaround this by using an
00055   // absolute path if we find the file here, and otherwise letting
00056   // header search handle it.
00057   llvm::sys::Path Path(File);
00058   Path.makeAbsolute();
00059   if (!Path.exists())
00060     Path = File;
00061 
00062   return Lexer::Stringify(Path.str());
00063 }
00064 
00065 /// AddImplicitInclude - Add an implicit #include of the specified file to the
00066 /// predefines buffer.
00067 static void AddImplicitInclude(MacroBuilder &Builder, llvm::StringRef File) {
00068   Builder.append("#include \"" +
00069                  llvm::Twine(NormalizeDashIncludePath(File)) + "\"");
00070 }
00071 
00072 static void AddImplicitIncludeMacros(MacroBuilder &Builder,
00073                                      llvm::StringRef File) {
00074   Builder.append("#__include_macros \"" +
00075                  llvm::Twine(NormalizeDashIncludePath(File)) + "\"");
00076   // Marker token to stop the __include_macros fetch loop.
00077   Builder.append("##"); // ##?
00078 }
00079 
00080 /// AddImplicitIncludePTH - Add an implicit #include using the original file
00081 ///  used to generate a PTH cache.
00082 static void AddImplicitIncludePTH(MacroBuilder &Builder, Preprocessor &PP,
00083                                   llvm::StringRef ImplicitIncludePTH) {
00084   PTHManager *P = PP.getPTHManager();
00085   assert(P && "No PTHManager.");
00086   const char *OriginalFile = P->getOriginalSourceFile();
00087 
00088   if (!OriginalFile) {
00089     PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header)
00090       << ImplicitIncludePTH;
00091     return;
00092   }
00093 
00094   AddImplicitInclude(Builder, OriginalFile);
00095 }
00096 
00097 /// PickFP - This is used to pick a value based on the FP semantics of the
00098 /// specified FP model.
00099 template <typename T>
00100 static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal,
00101                 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
00102                 T IEEEQuadVal) {
00103   if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle)
00104     return IEEESingleVal;
00105   if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble)
00106     return IEEEDoubleVal;
00107   if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended)
00108     return X87DoubleExtendedVal;
00109   if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble)
00110     return PPCDoubleDoubleVal;
00111   assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad);
00112   return IEEEQuadVal;
00113 }
00114 
00115 static void DefineFloatMacros(MacroBuilder &Builder, llvm::StringRef Prefix,
00116                               const llvm::fltSemantics *Sem) {
00117   const char *DenormMin, *Epsilon, *Max, *Min;
00118   DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324",
00119                      "3.64519953188247460253e-4951L",
00120                      "4.94065645841246544176568792868221e-324L",
00121                      "6.47517511943802511092443895822764655e-4966L");
00122   int Digits = PickFP(Sem, 6, 15, 18, 31, 33);
00123   Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16",
00124                    "1.08420217248550443401e-19L",
00125                    "4.94065645841246544176568792868221e-324L",
00126                    "1.92592994438723585305597794258492732e-34L");
00127   int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113);
00128   int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931);
00129   int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932);
00130   int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381);
00131   int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384);
00132   Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308",
00133                "3.36210314311209350626e-4932L",
00134                "2.00416836000897277799610805135016e-292L",
00135                "3.36210314311209350626267781732175260e-4932L");
00136   Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308",
00137                "1.18973149535723176502e+4932L",
00138                "1.79769313486231580793728971405301e+308L",
00139                "1.18973149535723176508575932662800702e+4932L");
00140 
00141   llvm::SmallString<32> DefPrefix;
00142   DefPrefix = "__";
00143   DefPrefix += Prefix;
00144   DefPrefix += "_";
00145 
00146   Builder.defineMacro(DefPrefix + "DENORM_MIN__", DenormMin);
00147   Builder.defineMacro(DefPrefix + "HAS_DENORM__");
00148   Builder.defineMacro(DefPrefix + "DIG__", llvm::Twine(Digits));
00149   Builder.defineMacro(DefPrefix + "EPSILON__", llvm::Twine(Epsilon));
00150   Builder.defineMacro(DefPrefix + "HAS_INFINITY__");
00151   Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__");
00152   Builder.defineMacro(DefPrefix + "MANT_DIG__", llvm::Twine(MantissaDigits));
00153 
00154   Builder.defineMacro(DefPrefix + "MAX_10_EXP__", llvm::Twine(Max10Exp));
00155   Builder.defineMacro(DefPrefix + "MAX_EXP__", llvm::Twine(MaxExp));
00156   Builder.defineMacro(DefPrefix + "MAX__", llvm::Twine(Max));
00157 
00158   Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+llvm::Twine(Min10Exp)+")");
00159   Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+llvm::Twine(MinExp)+")");
00160   Builder.defineMacro(DefPrefix + "MIN__", llvm::Twine(Min));
00161 }
00162 
00163 
00164 /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
00165 /// named MacroName with the max value for a type with width 'TypeWidth' a
00166 /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
00167 static void DefineTypeSize(llvm::StringRef MacroName, unsigned TypeWidth,
00168                            llvm::StringRef ValSuffix, bool isSigned,
00169                            MacroBuilder& Builder) {
00170   long long MaxVal;
00171   if (isSigned)
00172     MaxVal = (1LL << (TypeWidth - 1)) - 1;
00173   else
00174     MaxVal = ~0LL >> (64-TypeWidth);
00175 
00176   Builder.defineMacro(MacroName, llvm::Twine(MaxVal) + ValSuffix);
00177 }
00178 
00179 /// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
00180 /// the width, suffix, and signedness of the given type
00181 static void DefineTypeSize(llvm::StringRef MacroName, TargetInfo::IntType Ty,
00182                            const TargetInfo &TI, MacroBuilder &Builder) {
00183   DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty), 
00184                  TI.isTypeSigned(Ty), Builder);
00185 }
00186 
00187 static void DefineType(const llvm::Twine &MacroName, TargetInfo::IntType Ty,
00188                        MacroBuilder &Builder) {
00189   Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty));
00190 }
00191 
00192 static void DefineTypeWidth(llvm::StringRef MacroName, TargetInfo::IntType Ty,
00193                             const TargetInfo &TI, MacroBuilder &Builder) {
00194   Builder.defineMacro(MacroName, llvm::Twine(TI.getTypeWidth(Ty)));
00195 }
00196 
00197 static void DefineExactWidthIntType(TargetInfo::IntType Ty, 
00198                                const TargetInfo &TI, MacroBuilder &Builder) {
00199   int TypeWidth = TI.getTypeWidth(Ty);
00200   DefineType("__INT" + llvm::Twine(TypeWidth) + "_TYPE__", Ty, Builder);
00201 
00202   llvm::StringRef ConstSuffix(TargetInfo::getTypeConstantSuffix(Ty));
00203   if (!ConstSuffix.empty())
00204     Builder.defineMacro("__INT" + llvm::Twine(TypeWidth) + "_C_SUFFIX__",
00205                         ConstSuffix);
00206 }
00207 
00208 static void InitializePredefinedMacros(const TargetInfo &TI,
00209                                        const LangOptions &LangOpts,
00210                                        const FrontendOptions &FEOpts,
00211                                        MacroBuilder &Builder) {
00212   // Compiler version introspection macros.
00213   Builder.defineMacro("__llvm__");  // LLVM Backend
00214   Builder.defineMacro("__clang__"); // Clang Frontend
00215 
00216   // Currently claim to be compatible with GCC 4.2.1-5621.
00217   Builder.defineMacro("__GNUC_MINOR__", "2");
00218   Builder.defineMacro("__GNUC_PATCHLEVEL__", "1");
00219   Builder.defineMacro("__GNUC__", "4");
00220   Builder.defineMacro("__GXX_ABI_VERSION", "1002");
00221   Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible Clang Compiler\"");
00222 
00223   // Initialize language-specific preprocessor defines.
00224 
00225   // These should all be defined in the preprocessor according to the
00226   // current language configuration.
00227   if (!LangOpts.Microsoft)
00228     Builder.defineMacro("__STDC__");
00229   if (LangOpts.AsmPreprocessor)
00230     Builder.defineMacro("__ASSEMBLER__");
00231 
00232   if (!LangOpts.CPlusPlus) {
00233     if (LangOpts.C99)
00234       Builder.defineMacro("__STDC_VERSION__", "199901L");
00235     else if (!LangOpts.GNUMode && LangOpts.Digraphs)
00236       Builder.defineMacro("__STDC_VERSION__", "199409L");
00237   }
00238 
00239   // Standard conforming mode?
00240   if (!LangOpts.GNUMode)
00241     Builder.defineMacro("__STRICT_ANSI__");
00242 
00243   if (LangOpts.CPlusPlus0x)
00244     Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__");
00245 
00246   if (LangOpts.Freestanding)
00247     Builder.defineMacro("__STDC_HOSTED__", "0");
00248   else
00249     Builder.defineMacro("__STDC_HOSTED__");
00250 
00251   if (LangOpts.ObjC1) {
00252     Builder.defineMacro("__OBJC__");
00253     if (LangOpts.ObjCNonFragileABI) {
00254       Builder.defineMacro("__OBJC2__");
00255       Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS");
00256     }
00257 
00258     if (LangOpts.getGCMode() != LangOptions::NonGC)
00259       Builder.defineMacro("__OBJC_GC__");
00260 
00261     if (LangOpts.NeXTRuntime)
00262       Builder.defineMacro("__NEXT_RUNTIME__");
00263   }
00264 
00265   // darwin_constant_cfstrings controls this. This is also dependent
00266   // on other things like the runtime I believe.  This is set even for C code.
00267   Builder.defineMacro("__CONSTANT_CFSTRINGS__");
00268 
00269   if (LangOpts.ObjC2)
00270     Builder.defineMacro("OBJC_NEW_PROPERTIES");
00271 
00272   if (LangOpts.PascalStrings)
00273     Builder.defineMacro("__PASCAL_STRINGS__");
00274 
00275   if (LangOpts.Blocks) {
00276     Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))");
00277     Builder.defineMacro("__BLOCKS__");
00278   }
00279 
00280   if (LangOpts.Exceptions)
00281     Builder.defineMacro("__EXCEPTIONS");
00282   if (LangOpts.SjLjExceptions)
00283     Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__");
00284 
00285   if (LangOpts.CPlusPlus) {
00286     Builder.defineMacro("__DEPRECATED");
00287     Builder.defineMacro("__GNUG__", "4");
00288     Builder.defineMacro("__GXX_WEAK__");
00289     if (LangOpts.GNUMode)
00290       Builder.defineMacro("__cplusplus");
00291     else
00292       // C++ [cpp.predefined]p1:
00293       //   The name_ _cplusplusis defined to the value199711Lwhen compiling a
00294       //   C++ translation unit.
00295       Builder.defineMacro("__cplusplus", "199711L");
00296     Builder.defineMacro("__private_extern__", "extern");
00297     // Ugly hack to work with GNU libstdc++.
00298     Builder.defineMacro("_GNU_SOURCE");
00299   }
00300 
00301   if (LangOpts.Microsoft) {
00302     // Filter out some microsoft extensions when trying to parse in ms-compat
00303     // mode.
00304     Builder.defineMacro("__int8", "__INT8_TYPE__");
00305     Builder.defineMacro("__int16", "__INT16_TYPE__");
00306     Builder.defineMacro("__int32", "__INT32_TYPE__");
00307     Builder.defineMacro("__int64", "__INT64_TYPE__");
00308     // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however
00309     // VC++ appears to only like __FUNCTION__.
00310     Builder.defineMacro("__PRETTY_FUNCTION__", "__FUNCTION__");
00311     // Work around some issues with Visual C++ headerws.
00312     if (LangOpts.CPlusPlus) {
00313       // Since we define wchar_t in C++ mode.
00314       Builder.defineMacro("_WCHAR_T_DEFINED");
00315       Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED");
00316       // FIXME:  This should be temporary until we have a __pragma
00317       // solution, to avoid some errors flagged in VC++ headers.
00318       Builder.defineMacro("_CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES", "0");
00319     }
00320   }
00321 
00322   if (LangOpts.Optimize)
00323     Builder.defineMacro("__OPTIMIZE__");
00324   if (LangOpts.OptimizeSize)
00325     Builder.defineMacro("__OPTIMIZE_SIZE__");
00326 
00327   // Initialize target-specific preprocessor defines.
00328 
00329   // Define type sizing macros based on the target properties.
00330   assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
00331   Builder.defineMacro("__CHAR_BIT__", "8");
00332 
00333   DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Builder);
00334   DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder);
00335   DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder);
00336   DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder);
00337   DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder);
00338   DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder);
00339   DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder);
00340 
00341   DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder);
00342   DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder);
00343   DefineTypeWidth("__INTMAX_WIDTH__",  TI.getIntMaxType(), TI, Builder);
00344   DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder);
00345   DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder);
00346   DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder);
00347   DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder);
00348   DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder);
00349   DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder);
00350   DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder);
00351   DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder);
00352   DefineType("__WINT_TYPE__", TI.getWIntType(), Builder);
00353   DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder);
00354   DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder);
00355 
00356   DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat());
00357   DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat());
00358   DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat());
00359 
00360   // Define a __POINTER_WIDTH__ macro for stdint.h.
00361   Builder.defineMacro("__POINTER_WIDTH__",
00362                       llvm::Twine((int)TI.getPointerWidth(0)));
00363 
00364   if (!LangOpts.CharIsSigned)
00365     Builder.defineMacro("__CHAR_UNSIGNED__");
00366 
00367   // Define exact-width integer types for stdint.h
00368   Builder.defineMacro("__INT" + llvm::Twine(TI.getCharWidth()) + "_TYPE__",
00369                       "char");
00370 
00371   if (TI.getShortWidth() > TI.getCharWidth())
00372     DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder);
00373 
00374   if (TI.getIntWidth() > TI.getShortWidth())
00375     DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder);
00376 
00377   if (TI.getLongWidth() > TI.getIntWidth())
00378     DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder);
00379 
00380   if (TI.getLongLongWidth() > TI.getLongWidth())
00381     DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder);
00382 
00383   // Add __builtin_va_list typedef.
00384   Builder.append(TI.getVAListDeclaration());
00385 
00386   if (const char *Prefix = TI.getUserLabelPrefix())
00387     Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix);
00388 
00389   // Build configuration options.  FIXME: these should be controlled by
00390   // command line options or something.
00391   Builder.defineMacro("__FINITE_MATH_ONLY__", "0");
00392 
00393   if (LangOpts.GNUInline)
00394     Builder.defineMacro("__GNUC_GNU_INLINE__");
00395   else
00396     Builder.defineMacro("__GNUC_STDC_INLINE__");
00397 
00398   if (LangOpts.NoInline)
00399     Builder.defineMacro("__NO_INLINE__");
00400 
00401   if (unsigned PICLevel = LangOpts.PICLevel) {
00402     Builder.defineMacro("__PIC__", llvm::Twine(PICLevel));
00403     Builder.defineMacro("__pic__", llvm::Twine(PICLevel));
00404   }
00405 
00406   // Macros to control C99 numerics and <float.h>
00407   Builder.defineMacro("__FLT_EVAL_METHOD__", "0");
00408   Builder.defineMacro("__FLT_RADIX__", "2");
00409   int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36);
00410   Builder.defineMacro("__DECIMAL_DIG__", llvm::Twine(Dig));
00411 
00412   if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn)
00413     Builder.defineMacro("__SSP__");
00414   else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq)
00415     Builder.defineMacro("__SSP_ALL__", "2");
00416 
00417   if (FEOpts.ProgramAction == frontend::RewriteObjC)
00418     Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
00419   // Get other target #defines.
00420   TI.getTargetDefines(LangOpts, Builder);
00421 }
00422 
00423 // Initialize the remapping of files to alternative contents, e.g.,
00424 // those specified through other files.
00425 static void InitializeFileRemapping(Diagnostic &Diags,
00426                                     SourceManager &SourceMgr,
00427                                     FileManager &FileMgr,
00428                                     const PreprocessorOptions &InitOpts) {
00429   // Remap files in the source manager (with buffers).
00430   for (PreprocessorOptions::remapped_file_buffer_iterator
00431          Remap = InitOpts.remapped_file_buffer_begin(),
00432          RemapEnd = InitOpts.remapped_file_buffer_end();
00433        Remap != RemapEnd;
00434        ++Remap) {
00435     // Create the file entry for the file that we're mapping from.
00436     const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
00437                                                 Remap->second->getBufferSize(),
00438                                                        0);
00439     if (!FromFile) {
00440       Diags.Report(diag::err_fe_remap_missing_from_file)
00441         << Remap->first;
00442       delete Remap->second;
00443       continue;
00444     }
00445 
00446     // Override the contents of the "from" file with the contents of
00447     // the "to" file.
00448     SourceMgr.overrideFileContents(FromFile, Remap->second);
00449   }
00450 
00451   // Remap files in the source manager (with other files).
00452   for (PreprocessorOptions::remapped_file_iterator
00453        Remap = InitOpts.remapped_file_begin(),
00454        RemapEnd = InitOpts.remapped_file_end();
00455        Remap != RemapEnd;
00456        ++Remap) {
00457     // Find the file that we're mapping to.
00458     const FileEntry *ToFile = FileMgr.getFile(Remap->second);
00459     if (!ToFile) {
00460       Diags.Report(diag::err_fe_remap_missing_to_file)
00461       << Remap->first << Remap->second;
00462       continue;
00463     }
00464     
00465     // Create the file entry for the file that we're mapping from.
00466     const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
00467                                                        ToFile->getSize(),
00468                                                        0);
00469     if (!FromFile) {
00470       Diags.Report(diag::err_fe_remap_missing_from_file)
00471       << Remap->first;
00472       continue;
00473     }
00474     
00475     // Load the contents of the file we're mapping to.
00476     std::string ErrorStr;
00477     const llvm::MemoryBuffer *Buffer
00478     = llvm::MemoryBuffer::getFile(ToFile->getName(), &ErrorStr);
00479     if (!Buffer) {
00480       Diags.Report(diag::err_fe_error_opening)
00481         << Remap->second << ErrorStr;
00482       continue;
00483     }
00484     
00485     // Override the contents of the "from" file with the contents of
00486     // the "to" file.
00487     SourceMgr.overrideFileContents(FromFile, Buffer);
00488   }
00489 }
00490 
00491 /// InitializePreprocessor - Initialize the preprocessor getting it and the
00492 /// environment ready to process a single file. This returns true on error.
00493 ///
00494 void clang::InitializePreprocessor(Preprocessor &PP,
00495                                    const PreprocessorOptions &InitOpts,
00496                                    const HeaderSearchOptions &HSOpts,
00497                                    const FrontendOptions &FEOpts) {
00498   std::string PredefineBuffer;
00499   PredefineBuffer.reserve(4080);
00500   llvm::raw_string_ostream Predefines(PredefineBuffer);
00501   MacroBuilder Builder(Predefines);
00502 
00503   InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(),
00504                           PP.getFileManager(), InitOpts);
00505 
00506   Builder.append("# 1 \"<built-in>\" 3");
00507 
00508   // Install things like __POWERPC__, __GNUC__, etc into the macro table.
00509   if (InitOpts.UsePredefines)
00510     InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(),
00511                                FEOpts, Builder);
00512 
00513   // Add on the predefines from the driver.  Wrap in a #line directive to report
00514   // that they come from the command line.
00515   Builder.append("# 1 \"<command line>\" 1");
00516 
00517   // Process #define's and #undef's in the order they are given.
00518   for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
00519     if (InitOpts.Macros[i].second)  // isUndef
00520       Builder.undefineMacro(InitOpts.Macros[i].first);
00521     else
00522       DefineBuiltinMacro(Builder, InitOpts.Macros[i].first,
00523                          PP.getDiagnostics());
00524   }
00525 
00526   // If -imacros are specified, include them now.  These are processed before
00527   // any -include directives.
00528   for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
00529     AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i]);
00530 
00531   // Process -include directives.
00532   for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
00533     const std::string &Path = InitOpts.Includes[i];
00534     if (Path == InitOpts.ImplicitPTHInclude)
00535       AddImplicitIncludePTH(Builder, PP, Path);
00536     else
00537       AddImplicitInclude(Builder, Path);
00538   }
00539 
00540   // Exit the command line and go back to <built-in> (2 is LC_LEAVE).
00541   Builder.append("# 1 \"<built-in>\" 2");
00542 
00543   // Copy PredefinedBuffer into the Preprocessor.
00544   PP.setPredefines(Predefines.str());
00545 
00546   // Initialize the header search object.
00547   ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts,
00548                            PP.getLangOptions(),
00549                            PP.getTargetInfo().getTriple());
00550 }