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