InitPreprocessor.cpp revision b2987d159a88ab0ee2e40c884eb4d77b42ab89b6
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"
26using 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=".
31static 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
49std::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.
67static void AddImplicitInclude(MacroBuilder &Builder, llvm::StringRef File) {
68  Builder.append("#include \"" +
69                 llvm::Twine(NormalizeDashIncludePath(File)) + "\"");
70}
71
72static 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.
82static 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.
99template <typename T>
100static 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
115static 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).
167static 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
181static 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
187static void DefineType(const llvm::Twine &MacroName, TargetInfo::IntType Ty,
188                       MacroBuilder &Builder) {
189  Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty));
190}
191
192static 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
197static 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
208static 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    // Ugly hack to work with GNU libstdc++.
298    Builder.defineMacro("_GNU_SOURCE");
299  }
300
301  if (LangOpts.Microsoft) {
302    // Filter out some microsoft extensions when trying to parse in ms-compat
303    // mode.
304    Builder.defineMacro("__int8", "__INT8_TYPE__");
305    Builder.defineMacro("__int16", "__INT16_TYPE__");
306    Builder.defineMacro("__int32", "__INT32_TYPE__");
307    Builder.defineMacro("__int64", "__INT64_TYPE__");
308    // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however
309    // VC++ appears to only like __FUNCTION__.
310    Builder.defineMacro("__PRETTY_FUNCTION__", "__FUNCTION__");
311    // Work around some issues with Visual C++ headerws.
312    if (LangOpts.CPlusPlus) {
313      // Since we define wchar_t in C++ mode.
314      Builder.defineMacro("_WCHAR_T_DEFINED");
315      Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED");
316      // FIXME:  This should be temporary until we have a __pragma
317      // solution, to avoid some errors flagged in VC++ headers.
318      Builder.defineMacro("_CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES", "0");
319    }
320  }
321
322  if (LangOpts.Optimize)
323    Builder.defineMacro("__OPTIMIZE__");
324  if (LangOpts.OptimizeSize)
325    Builder.defineMacro("__OPTIMIZE_SIZE__");
326
327  // Initialize target-specific preprocessor defines.
328
329  // Define type sizing macros based on the target properties.
330  assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
331  Builder.defineMacro("__CHAR_BIT__", "8");
332
333  DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Builder);
334  DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder);
335  DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder);
336  DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder);
337  DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder);
338  DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder);
339  DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder);
340
341  DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder);
342  DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder);
343  DefineTypeWidth("__INTMAX_WIDTH__",  TI.getIntMaxType(), TI, Builder);
344  DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder);
345  DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder);
346  DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder);
347  DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder);
348  DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder);
349  DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder);
350  DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder);
351  DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder);
352  DefineType("__WINT_TYPE__", TI.getWIntType(), Builder);
353  DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder);
354  DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder);
355
356  DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat());
357  DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat());
358  DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat());
359
360  // Define a __POINTER_WIDTH__ macro for stdint.h.
361  Builder.defineMacro("__POINTER_WIDTH__",
362                      llvm::Twine((int)TI.getPointerWidth(0)));
363
364  if (!LangOpts.CharIsSigned)
365    Builder.defineMacro("__CHAR_UNSIGNED__");
366
367  // Define exact-width integer types for stdint.h
368  Builder.defineMacro("__INT" + llvm::Twine(TI.getCharWidth()) + "_TYPE__",
369                      "char");
370
371  if (TI.getShortWidth() > TI.getCharWidth())
372    DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder);
373
374  if (TI.getIntWidth() > TI.getShortWidth())
375    DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder);
376
377  if (TI.getLongWidth() > TI.getIntWidth())
378    DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder);
379
380  if (TI.getLongLongWidth() > TI.getLongWidth())
381    DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder);
382
383  // Add __builtin_va_list typedef.
384  Builder.append(TI.getVAListDeclaration());
385
386  if (const char *Prefix = TI.getUserLabelPrefix())
387    Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix);
388
389  // Build configuration options.  FIXME: these should be controlled by
390  // command line options or something.
391  Builder.defineMacro("__FINITE_MATH_ONLY__", "0");
392
393  if (LangOpts.GNUInline)
394    Builder.defineMacro("__GNUC_GNU_INLINE__");
395  else
396    Builder.defineMacro("__GNUC_STDC_INLINE__");
397
398  if (LangOpts.NoInline)
399    Builder.defineMacro("__NO_INLINE__");
400
401  if (unsigned PICLevel = LangOpts.PICLevel) {
402    Builder.defineMacro("__PIC__", llvm::Twine(PICLevel));
403    Builder.defineMacro("__pic__", llvm::Twine(PICLevel));
404  }
405
406  // Macros to control C99 numerics and <float.h>
407  Builder.defineMacro("__FLT_EVAL_METHOD__", "0");
408  Builder.defineMacro("__FLT_RADIX__", "2");
409  int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36);
410  Builder.defineMacro("__DECIMAL_DIG__", llvm::Twine(Dig));
411
412  if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn)
413    Builder.defineMacro("__SSP__");
414  else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq)
415    Builder.defineMacro("__SSP_ALL__", "2");
416
417  if (FEOpts.ProgramAction == frontend::RewriteObjC)
418    Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
419  // Get other target #defines.
420  TI.getTargetDefines(LangOpts, Builder);
421}
422
423// Initialize the remapping of files to alternative contents, e.g.,
424// those specified through other files.
425static void InitializeFileRemapping(Diagnostic &Diags,
426                                    SourceManager &SourceMgr,
427                                    FileManager &FileMgr,
428                                    const PreprocessorOptions &InitOpts) {
429  // Remap files in the source manager (with buffers).
430  for (PreprocessorOptions::remapped_file_buffer_iterator
431         Remap = InitOpts.remapped_file_buffer_begin(),
432         RemapEnd = InitOpts.remapped_file_buffer_end();
433       Remap != RemapEnd;
434       ++Remap) {
435    // Create the file entry for the file that we're mapping from.
436    const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
437                                                Remap->second->getBufferSize(),
438                                                       0);
439    if (!FromFile) {
440      Diags.Report(diag::err_fe_remap_missing_from_file)
441        << Remap->first;
442      continue;
443    }
444
445    // Override the contents of the "from" file with the contents of
446    // the "to" file.
447    SourceMgr.overrideFileContents(FromFile, Remap->second);
448  }
449
450  // Remap files in the source manager (with other files).
451  for (PreprocessorOptions::remapped_file_iterator
452       Remap = InitOpts.remapped_file_begin(),
453       RemapEnd = InitOpts.remapped_file_end();
454       Remap != RemapEnd;
455       ++Remap) {
456    // Find the file that we're mapping to.
457    const FileEntry *ToFile = FileMgr.getFile(Remap->second);
458    if (!ToFile) {
459      Diags.Report(diag::err_fe_remap_missing_to_file)
460      << Remap->first << Remap->second;
461      continue;
462    }
463
464    // Create the file entry for the file that we're mapping from.
465    const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
466                                                       ToFile->getSize(),
467                                                       0);
468    if (!FromFile) {
469      Diags.Report(diag::err_fe_remap_missing_from_file)
470      << Remap->first;
471      continue;
472    }
473
474    // Load the contents of the file we're mapping to.
475    std::string ErrorStr;
476    const llvm::MemoryBuffer *Buffer
477    = llvm::MemoryBuffer::getFile(ToFile->getName(), &ErrorStr);
478    if (!Buffer) {
479      Diags.Report(diag::err_fe_error_opening)
480      << Remap->second << ErrorStr;
481      continue;
482    }
483
484    // Override the contents of the "from" file with the contents of
485    // the "to" file.
486    SourceMgr.overrideFileContents(FromFile, Buffer);
487  }
488}
489
490/// InitializePreprocessor - Initialize the preprocessor getting it and the
491/// environment ready to process a single file. This returns true on error.
492///
493void clang::InitializePreprocessor(Preprocessor &PP,
494                                   const PreprocessorOptions &InitOpts,
495                                   const HeaderSearchOptions &HSOpts,
496                                   const FrontendOptions &FEOpts) {
497  std::string PredefineBuffer;
498  PredefineBuffer.reserve(4080);
499  llvm::raw_string_ostream Predefines(PredefineBuffer);
500  MacroBuilder Builder(Predefines);
501
502  InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(),
503                          PP.getFileManager(), InitOpts);
504
505  Builder.append("# 1 \"<built-in>\" 3");
506
507  // Install things like __POWERPC__, __GNUC__, etc into the macro table.
508  if (InitOpts.UsePredefines)
509    InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(),
510                               FEOpts, Builder);
511
512  // Add on the predefines from the driver.  Wrap in a #line directive to report
513  // that they come from the command line.
514  Builder.append("# 1 \"<command line>\" 1");
515
516  // Process #define's and #undef's in the order they are given.
517  for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
518    if (InitOpts.Macros[i].second)  // isUndef
519      Builder.undefineMacro(InitOpts.Macros[i].first);
520    else
521      DefineBuiltinMacro(Builder, InitOpts.Macros[i].first,
522                         PP.getDiagnostics());
523  }
524
525  // If -imacros are specified, include them now.  These are processed before
526  // any -include directives.
527  for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
528    AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i]);
529
530  // Process -include directives.
531  for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
532    const std::string &Path = InitOpts.Includes[i];
533    if (Path == InitOpts.ImplicitPTHInclude)
534      AddImplicitIncludePTH(Builder, PP, Path);
535    else
536      AddImplicitInclude(Builder, Path);
537  }
538
539  // Exit the command line and go back to <built-in> (2 is LC_LEAVE).
540  Builder.append("# 1 \"<built-in>\" 2");
541
542  // Copy PredefinedBuffer into the Preprocessor.
543  PP.setPredefines(Predefines.str());
544
545  // Initialize the header search object.
546  ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts,
547                           PP.getLangOptions(),
548                           PP.getTargetInfo().getTriple());
549}
550