xref: /llvm-project/clang/lib/Frontend/InitPreprocessor.cpp (revision 24d59d14ba1b65b8d7931b06a7c59589a0742032)
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/FileManager.h"
16 #include "clang/Basic/MacroBuilder.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/Basic/TargetInfo.h"
19 #include "clang/Basic/Version.h"
20 #include "clang/Frontend/FrontendDiagnostic.h"
21 #include "clang/Frontend/FrontendOptions.h"
22 #include "clang/Lex/HeaderSearch.h"
23 #include "clang/Lex/Preprocessor.h"
24 #include "clang/Lex/PreprocessorOptions.h"
25 #include "clang/Serialization/ASTReader.h"
26 #include "llvm/ADT/APFloat.h"
27 #include "llvm/Support/FileSystem.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/Path.h"
30 using namespace clang;
31 
32 static bool MacroBodyEndsInBackslash(StringRef MacroBody) {
33   while (!MacroBody.empty() && isWhitespace(MacroBody.back()))
34     MacroBody = MacroBody.drop_back();
35   return !MacroBody.empty() && MacroBody.back() == '\\';
36 }
37 
38 // Append a #define line to Buf for Macro.  Macro should be of the form XXX,
39 // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
40 // "#define XXX Y z W".  To get a #define with no value, use "XXX=".
41 static void DefineBuiltinMacro(MacroBuilder &Builder, StringRef Macro,
42                                DiagnosticsEngine &Diags) {
43   std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
44   StringRef MacroName = MacroPair.first;
45   StringRef MacroBody = MacroPair.second;
46   if (MacroName.size() != Macro.size()) {
47     // Per GCC -D semantics, the macro ends at \n if it exists.
48     StringRef::size_type End = MacroBody.find_first_of("\n\r");
49     if (End != StringRef::npos)
50       Diags.Report(diag::warn_fe_macro_contains_embedded_newline)
51         << MacroName;
52     MacroBody = MacroBody.substr(0, End);
53     // We handle macro bodies which end in a backslash by appending an extra
54     // backslash+newline.  This makes sure we don't accidentally treat the
55     // backslash as a line continuation marker.
56     if (MacroBodyEndsInBackslash(MacroBody))
57       Builder.defineMacro(MacroName, Twine(MacroBody) + "\\\n");
58     else
59       Builder.defineMacro(MacroName, MacroBody);
60   } else {
61     // Push "macroname 1".
62     Builder.defineMacro(Macro);
63   }
64 }
65 
66 /// AddImplicitInclude - Add an implicit \#include of the specified file to the
67 /// predefines buffer.
68 /// As these includes are generated by -include arguments the header search
69 /// logic is going to search relatively to the current working directory.
70 static void AddImplicitInclude(MacroBuilder &Builder, StringRef File) {
71   Builder.append(Twine("#include \"") + File + "\"");
72 }
73 
74 static void AddImplicitIncludeMacros(MacroBuilder &Builder, StringRef File) {
75   Builder.append(Twine("#__include_macros \"") + 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                                   StringRef ImplicitIncludePTH) {
84   PTHManager *P = PP.getPTHManager();
85   // Null check 'P' in the corner case where it couldn't be created.
86   const char *OriginalFile = P ? P->getOriginalSourceFile() : nullptr;
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 /// \brief Add an implicit \#include using the original file used to generate
98 /// a PCH file.
99 static void AddImplicitIncludePCH(MacroBuilder &Builder, Preprocessor &PP,
100                                   StringRef ImplicitIncludePCH) {
101   std::string OriginalFile =
102     ASTReader::getOriginalSourceFile(ImplicitIncludePCH, PP.getFileManager(),
103                                      PP.getDiagnostics());
104   if (OriginalFile.empty())
105     return;
106 
107   AddImplicitInclude(Builder, OriginalFile);
108 }
109 
110 /// PickFP - This is used to pick a value based on the FP semantics of the
111 /// specified FP model.
112 template <typename T>
113 static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal,
114                 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
115                 T IEEEQuadVal) {
116   if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle)
117     return IEEESingleVal;
118   if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble)
119     return IEEEDoubleVal;
120   if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended)
121     return X87DoubleExtendedVal;
122   if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble)
123     return PPCDoubleDoubleVal;
124   assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad);
125   return IEEEQuadVal;
126 }
127 
128 static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix,
129                               const llvm::fltSemantics *Sem, StringRef Ext) {
130   const char *DenormMin, *Epsilon, *Max, *Min;
131   DenormMin = PickFP(Sem, "1.40129846e-45", "4.9406564584124654e-324",
132                      "3.64519953188247460253e-4951",
133                      "4.94065645841246544176568792868221e-324",
134                      "6.47517511943802511092443895822764655e-4966");
135   int Digits = PickFP(Sem, 6, 15, 18, 31, 33);
136   int DecimalDigits = PickFP(Sem, 9, 17, 21, 33, 36);
137   Epsilon = PickFP(Sem, "1.19209290e-7", "2.2204460492503131e-16",
138                    "1.08420217248550443401e-19",
139                    "4.94065645841246544176568792868221e-324",
140                    "1.92592994438723585305597794258492732e-34");
141   int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113);
142   int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931);
143   int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932);
144   int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381);
145   int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384);
146   Min = PickFP(Sem, "1.17549435e-38", "2.2250738585072014e-308",
147                "3.36210314311209350626e-4932",
148                "2.00416836000897277799610805135016e-292",
149                "3.36210314311209350626267781732175260e-4932");
150   Max = PickFP(Sem, "3.40282347e+38", "1.7976931348623157e+308",
151                "1.18973149535723176502e+4932",
152                "1.79769313486231580793728971405301e+308",
153                "1.18973149535723176508575932662800702e+4932");
154 
155   SmallString<32> DefPrefix;
156   DefPrefix = "__";
157   DefPrefix += Prefix;
158   DefPrefix += "_";
159 
160   Builder.defineMacro(DefPrefix + "DENORM_MIN__", Twine(DenormMin)+Ext);
161   Builder.defineMacro(DefPrefix + "HAS_DENORM__");
162   Builder.defineMacro(DefPrefix + "DIG__", Twine(Digits));
163   Builder.defineMacro(DefPrefix + "DECIMAL_DIG__", Twine(DecimalDigits));
164   Builder.defineMacro(DefPrefix + "EPSILON__", Twine(Epsilon)+Ext);
165   Builder.defineMacro(DefPrefix + "HAS_INFINITY__");
166   Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__");
167   Builder.defineMacro(DefPrefix + "MANT_DIG__", Twine(MantissaDigits));
168 
169   Builder.defineMacro(DefPrefix + "MAX_10_EXP__", Twine(Max10Exp));
170   Builder.defineMacro(DefPrefix + "MAX_EXP__", Twine(MaxExp));
171   Builder.defineMacro(DefPrefix + "MAX__", Twine(Max)+Ext);
172 
173   Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+Twine(Min10Exp)+")");
174   Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+Twine(MinExp)+")");
175   Builder.defineMacro(DefPrefix + "MIN__", Twine(Min)+Ext);
176 }
177 
178 
179 /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
180 /// named MacroName with the max value for a type with width 'TypeWidth' a
181 /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
182 static void DefineTypeSize(const Twine &MacroName, unsigned TypeWidth,
183                            StringRef ValSuffix, bool isSigned,
184                            MacroBuilder &Builder) {
185   llvm::APInt MaxVal = isSigned ? llvm::APInt::getSignedMaxValue(TypeWidth)
186                                 : llvm::APInt::getMaxValue(TypeWidth);
187   Builder.defineMacro(MacroName, MaxVal.toString(10, isSigned) + ValSuffix);
188 }
189 
190 /// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
191 /// the width, suffix, and signedness of the given type
192 static void DefineTypeSize(const Twine &MacroName, TargetInfo::IntType Ty,
193                            const TargetInfo &TI, MacroBuilder &Builder) {
194   DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
195                  TI.isTypeSigned(Ty), Builder);
196 }
197 
198 static void DefineFmt(const Twine &Prefix, TargetInfo::IntType Ty,
199                       const TargetInfo &TI, MacroBuilder &Builder) {
200   bool IsSigned = TI.isTypeSigned(Ty);
201   StringRef FmtModifier = TI.getTypeFormatModifier(Ty);
202   for (const char *Fmt = IsSigned ? "di" : "ouxX"; *Fmt; ++Fmt) {
203     Builder.defineMacro(Prefix + "_FMT" + Twine(*Fmt) + "__",
204                         Twine("\"") + FmtModifier + Twine(*Fmt) + "\"");
205   }
206 }
207 
208 static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty,
209                        MacroBuilder &Builder) {
210   Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty));
211 }
212 
213 static void DefineTypeWidth(StringRef MacroName, TargetInfo::IntType Ty,
214                             const TargetInfo &TI, MacroBuilder &Builder) {
215   Builder.defineMacro(MacroName, Twine(TI.getTypeWidth(Ty)));
216 }
217 
218 static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth,
219                              const TargetInfo &TI, MacroBuilder &Builder) {
220   Builder.defineMacro(MacroName,
221                       Twine(BitWidth / TI.getCharWidth()));
222 }
223 
224 static void DefineExactWidthIntType(TargetInfo::IntType Ty,
225                                     const TargetInfo &TI,
226                                     MacroBuilder &Builder) {
227   int TypeWidth = TI.getTypeWidth(Ty);
228   bool IsSigned = TI.isTypeSigned(Ty);
229 
230   // Use the target specified int64 type, when appropriate, so that [u]int64_t
231   // ends up being defined in terms of the correct type.
232   if (TypeWidth == 64)
233     Ty = IsSigned ? TI.getInt64Type() : TI.getUInt64Type();
234 
235   const char *Prefix = IsSigned ? "__INT" : "__UINT";
236 
237   DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
238   DefineFmt(Prefix + Twine(TypeWidth), Ty, TI, Builder);
239 
240   StringRef ConstSuffix(TI.getTypeConstantSuffix(Ty));
241   Builder.defineMacro(Prefix + Twine(TypeWidth) + "_C_SUFFIX__", ConstSuffix);
242 }
243 
244 static void DefineExactWidthIntTypeSize(TargetInfo::IntType Ty,
245                                         const TargetInfo &TI,
246                                         MacroBuilder &Builder) {
247   int TypeWidth = TI.getTypeWidth(Ty);
248   bool IsSigned = TI.isTypeSigned(Ty);
249 
250   // Use the target specified int64 type, when appropriate, so that [u]int64_t
251   // ends up being defined in terms of the correct type.
252   if (TypeWidth == 64)
253     Ty = IsSigned ? TI.getInt64Type() : TI.getUInt64Type();
254 
255   const char *Prefix = IsSigned ? "__INT" : "__UINT";
256   DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder);
257 }
258 
259 static void DefineLeastWidthIntType(unsigned TypeWidth, bool IsSigned,
260                                     const TargetInfo &TI,
261                                     MacroBuilder &Builder) {
262   TargetInfo::IntType Ty = TI.getLeastIntTypeByWidth(TypeWidth, IsSigned);
263   if (Ty == TargetInfo::NoInt)
264     return;
265 
266   const char *Prefix = IsSigned ? "__INT_LEAST" : "__UINT_LEAST";
267   DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
268   DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder);
269   DefineFmt(Prefix + Twine(TypeWidth), Ty, TI, Builder);
270 }
271 
272 static void DefineFastIntType(unsigned TypeWidth, bool IsSigned,
273                               const TargetInfo &TI, MacroBuilder &Builder) {
274   // stdint.h currently defines the fast int types as equivalent to the least
275   // types.
276   TargetInfo::IntType Ty = TI.getLeastIntTypeByWidth(TypeWidth, IsSigned);
277   if (Ty == TargetInfo::NoInt)
278     return;
279 
280   const char *Prefix = IsSigned ? "__INT_FAST" : "__UINT_FAST";
281   DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
282   DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder);
283 
284   DefineFmt(Prefix + Twine(TypeWidth), Ty, TI, Builder);
285 }
286 
287 
288 /// Get the value the ATOMIC_*_LOCK_FREE macro should have for a type with
289 /// the specified properties.
290 static const char *getLockFreeValue(unsigned TypeWidth, unsigned TypeAlign,
291                                     unsigned InlineWidth) {
292   // Fully-aligned, power-of-2 sizes no larger than the inline
293   // width will be inlined as lock-free operations.
294   if (TypeWidth == TypeAlign && (TypeWidth & (TypeWidth - 1)) == 0 &&
295       TypeWidth <= InlineWidth)
296     return "2"; // "always lock free"
297   // We cannot be certain what operations the lib calls might be
298   // able to implement as lock-free on future processors.
299   return "1"; // "sometimes lock free"
300 }
301 
302 /// \brief Add definitions required for a smooth interaction between
303 /// Objective-C++ automated reference counting and libstdc++ (4.2).
304 static void AddObjCXXARCLibstdcxxDefines(const LangOptions &LangOpts,
305                                          MacroBuilder &Builder) {
306   Builder.defineMacro("_GLIBCXX_PREDEFINED_OBJC_ARC_IS_SCALAR");
307 
308   std::string Result;
309   {
310     // Provide specializations for the __is_scalar type trait so that
311     // lifetime-qualified objects are not considered "scalar" types, which
312     // libstdc++ uses as an indicator of the presence of trivial copy, assign,
313     // default-construct, and destruct semantics (none of which hold for
314     // lifetime-qualified objects in ARC).
315     llvm::raw_string_ostream Out(Result);
316 
317     Out << "namespace std {\n"
318         << "\n"
319         << "struct __true_type;\n"
320         << "struct __false_type;\n"
321         << "\n";
322 
323     Out << "template<typename _Tp> struct __is_scalar;\n"
324         << "\n";
325 
326     Out << "template<typename _Tp>\n"
327         << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> {\n"
328         << "  enum { __value = 0 };\n"
329         << "  typedef __false_type __type;\n"
330         << "};\n"
331         << "\n";
332 
333     if (LangOpts.ObjCARCWeak) {
334       Out << "template<typename _Tp>\n"
335           << "struct __is_scalar<__attribute__((objc_ownership(weak))) _Tp> {\n"
336           << "  enum { __value = 0 };\n"
337           << "  typedef __false_type __type;\n"
338           << "};\n"
339           << "\n";
340     }
341 
342     Out << "template<typename _Tp>\n"
343         << "struct __is_scalar<__attribute__((objc_ownership(autoreleasing)))"
344         << " _Tp> {\n"
345         << "  enum { __value = 0 };\n"
346         << "  typedef __false_type __type;\n"
347         << "};\n"
348         << "\n";
349 
350     Out << "}\n";
351   }
352   Builder.append(Result);
353 }
354 
355 static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
356                                                const LangOptions &LangOpts,
357                                                const FrontendOptions &FEOpts,
358                                                MacroBuilder &Builder) {
359   if (!LangOpts.MSVCCompat && !LangOpts.TraditionalCPP)
360     Builder.defineMacro("__STDC__");
361   if (LangOpts.Freestanding)
362     Builder.defineMacro("__STDC_HOSTED__", "0");
363   else
364     Builder.defineMacro("__STDC_HOSTED__");
365 
366   if (!LangOpts.CPlusPlus) {
367     if (LangOpts.C11)
368       Builder.defineMacro("__STDC_VERSION__", "201112L");
369     else if (LangOpts.C99)
370       Builder.defineMacro("__STDC_VERSION__", "199901L");
371     else if (!LangOpts.GNUMode && LangOpts.Digraphs)
372       Builder.defineMacro("__STDC_VERSION__", "199409L");
373   } else {
374     // FIXME: Use correct value for C++17.
375     if (LangOpts.CPlusPlus1z)
376       Builder.defineMacro("__cplusplus", "201406L");
377     // C++1y [cpp.predefined]p1:
378     //   The name __cplusplus is defined to the value 201402L when compiling a
379     //   C++ translation unit.
380     else if (LangOpts.CPlusPlus14)
381       Builder.defineMacro("__cplusplus", "201402L");
382     // C++11 [cpp.predefined]p1:
383     //   The name __cplusplus is defined to the value 201103L when compiling a
384     //   C++ translation unit.
385     else if (LangOpts.CPlusPlus11)
386       Builder.defineMacro("__cplusplus", "201103L");
387     // C++03 [cpp.predefined]p1:
388     //   The name __cplusplus is defined to the value 199711L when compiling a
389     //   C++ translation unit.
390     else
391       Builder.defineMacro("__cplusplus", "199711L");
392   }
393 
394   // In C11 these are environment macros. In C++11 they are only defined
395   // as part of <cuchar>. To prevent breakage when mixing C and C++
396   // code, define these macros unconditionally. We can define them
397   // unconditionally, as Clang always uses UTF-16 and UTF-32 for 16-bit
398   // and 32-bit character literals.
399   Builder.defineMacro("__STDC_UTF_16__", "1");
400   Builder.defineMacro("__STDC_UTF_32__", "1");
401 
402   if (LangOpts.ObjC1)
403     Builder.defineMacro("__OBJC__");
404 
405   // Not "standard" per se, but available even with the -undef flag.
406   if (LangOpts.AsmPreprocessor)
407     Builder.defineMacro("__ASSEMBLER__");
408 }
409 
410 /// Initialize the predefined C++ language feature test macros defined in
411 /// ISO/IEC JTC1/SC22/WG21 (C++) SD-6: "SG10 Feature Test Recommendations".
412 static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts,
413                                                  MacroBuilder &Builder) {
414   // C++98 features.
415   if (LangOpts.RTTI)
416     Builder.defineMacro("__cpp_rtti", "199711");
417   if (LangOpts.CXXExceptions)
418     Builder.defineMacro("__cpp_exceptions", "199711");
419 
420   // C++11 features.
421   if (LangOpts.CPlusPlus11) {
422     Builder.defineMacro("__cpp_unicode_characters", "200704");
423     Builder.defineMacro("__cpp_raw_strings", "200710");
424     Builder.defineMacro("__cpp_unicode_literals", "200710");
425     Builder.defineMacro("__cpp_user_defined_literals", "200809");
426     Builder.defineMacro("__cpp_lambdas", "200907");
427     Builder.defineMacro("__cpp_constexpr",
428                         LangOpts.CPlusPlus14 ? "201304" : "200704");
429     Builder.defineMacro("__cpp_range_based_for", "200907");
430     Builder.defineMacro("__cpp_static_assert", "200410");
431     Builder.defineMacro("__cpp_decltype", "200707");
432     Builder.defineMacro("__cpp_attributes", "200809");
433     Builder.defineMacro("__cpp_rvalue_references", "200610");
434     Builder.defineMacro("__cpp_variadic_templates", "200704");
435     Builder.defineMacro("__cpp_initializer_lists", "200806");
436     Builder.defineMacro("__cpp_delegating_constructors", "200604");
437     Builder.defineMacro("__cpp_nsdmi", "200809");
438     Builder.defineMacro("__cpp_inheriting_constructors", "200802");
439     Builder.defineMacro("__cpp_ref_qualifiers", "200710");
440     Builder.defineMacro("__cpp_alias_templates", "200704");
441   }
442 
443   // C++14 features.
444   if (LangOpts.CPlusPlus14) {
445     Builder.defineMacro("__cpp_binary_literals", "201304");
446     Builder.defineMacro("__cpp_digit_separators", "201309");
447     Builder.defineMacro("__cpp_init_captures", "201304");
448     Builder.defineMacro("__cpp_generic_lambdas", "201304");
449     Builder.defineMacro("__cpp_decltype_auto", "201304");
450     Builder.defineMacro("__cpp_return_type_deduction", "201304");
451     Builder.defineMacro("__cpp_aggregate_nsdmi", "201304");
452     Builder.defineMacro("__cpp_variable_templates", "201304");
453   }
454   if (LangOpts.SizedDeallocation)
455     Builder.defineMacro("__cpp_sized_deallocation", "201309");
456   if (LangOpts.ConceptsTS)
457     Builder.defineMacro("__cpp_experimental_concepts", "1");
458 }
459 
460 static void InitializePredefinedMacros(const TargetInfo &TI,
461                                        const LangOptions &LangOpts,
462                                        const FrontendOptions &FEOpts,
463                                        MacroBuilder &Builder) {
464   // Compiler version introspection macros.
465   Builder.defineMacro("__llvm__");  // LLVM Backend
466   Builder.defineMacro("__clang__"); // Clang Frontend
467 #define TOSTR2(X) #X
468 #define TOSTR(X) TOSTR2(X)
469   Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR));
470   Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR));
471 #ifdef CLANG_VERSION_PATCHLEVEL
472   Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL));
473 #else
474   Builder.defineMacro("__clang_patchlevel__", "0");
475 #endif
476   Builder.defineMacro("__clang_version__",
477                       "\"" CLANG_VERSION_STRING " "
478                       + getClangFullRepositoryVersion() + "\"");
479 #undef TOSTR
480 #undef TOSTR2
481   if (!LangOpts.MSVCCompat) {
482     // Currently claim to be compatible with GCC 4.2.1-5621, but only if we're
483     // not compiling for MSVC compatibility
484     Builder.defineMacro("__GNUC_MINOR__", "2");
485     Builder.defineMacro("__GNUC_PATCHLEVEL__", "1");
486     Builder.defineMacro("__GNUC__", "4");
487     Builder.defineMacro("__GXX_ABI_VERSION", "1002");
488   }
489 
490   // Define macros for the C11 / C++11 memory orderings
491   Builder.defineMacro("__ATOMIC_RELAXED", "0");
492   Builder.defineMacro("__ATOMIC_CONSUME", "1");
493   Builder.defineMacro("__ATOMIC_ACQUIRE", "2");
494   Builder.defineMacro("__ATOMIC_RELEASE", "3");
495   Builder.defineMacro("__ATOMIC_ACQ_REL", "4");
496   Builder.defineMacro("__ATOMIC_SEQ_CST", "5");
497 
498   // Support for #pragma redefine_extname (Sun compatibility)
499   Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1");
500 
501   // As sad as it is, enough software depends on the __VERSION__ for version
502   // checks that it is necessary to report 4.2.1 (the base GCC version we claim
503   // compatibility with) first.
504   Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible " +
505                       Twine(getClangFullCPPVersion()) + "\"");
506 
507   // Initialize language-specific preprocessor defines.
508 
509   // Standard conforming mode?
510   if (!LangOpts.GNUMode && !LangOpts.MSVCCompat)
511     Builder.defineMacro("__STRICT_ANSI__");
512 
513   if (!LangOpts.MSVCCompat && LangOpts.CPlusPlus11)
514     Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__");
515 
516   if (LangOpts.ObjC1) {
517     if (LangOpts.ObjCRuntime.isNonFragile()) {
518       Builder.defineMacro("__OBJC2__");
519 
520       if (LangOpts.ObjCExceptions)
521         Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS");
522     }
523 
524     if (LangOpts.getGC() != LangOptions::NonGC)
525       Builder.defineMacro("__OBJC_GC__");
526 
527     if (LangOpts.ObjCRuntime.isNeXTFamily())
528       Builder.defineMacro("__NEXT_RUNTIME__");
529 
530     if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::ObjFW) {
531       VersionTuple tuple = LangOpts.ObjCRuntime.getVersion();
532 
533       unsigned minor = 0;
534       if (tuple.getMinor().hasValue())
535         minor = tuple.getMinor().getValue();
536 
537       unsigned subminor = 0;
538       if (tuple.getSubminor().hasValue())
539         subminor = tuple.getSubminor().getValue();
540 
541       Builder.defineMacro("__OBJFW_RUNTIME_ABI__",
542                           Twine(tuple.getMajor() * 10000 + minor * 100 +
543                                 subminor));
544     }
545 
546     Builder.defineMacro("IBOutlet", "__attribute__((iboutlet))");
547     Builder.defineMacro("IBOutletCollection(ClassName)",
548                         "__attribute__((iboutletcollection(ClassName)))");
549     Builder.defineMacro("IBAction", "void)__attribute__((ibaction)");
550     Builder.defineMacro("IBInspectable", "");
551     Builder.defineMacro("IB_DESIGNABLE", "");
552   }
553 
554   if (LangOpts.CPlusPlus)
555     InitializeCPlusPlusFeatureTestMacros(LangOpts, Builder);
556 
557   // darwin_constant_cfstrings controls this. This is also dependent
558   // on other things like the runtime I believe.  This is set even for C code.
559   if (!LangOpts.NoConstantCFStrings)
560       Builder.defineMacro("__CONSTANT_CFSTRINGS__");
561 
562   if (LangOpts.ObjC2)
563     Builder.defineMacro("OBJC_NEW_PROPERTIES");
564 
565   if (LangOpts.PascalStrings)
566     Builder.defineMacro("__PASCAL_STRINGS__");
567 
568   if (LangOpts.Blocks) {
569     Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))");
570     Builder.defineMacro("__BLOCKS__");
571   }
572 
573   if (!LangOpts.MSVCCompat && LangOpts.Exceptions)
574     Builder.defineMacro("__EXCEPTIONS");
575   if (!LangOpts.MSVCCompat && LangOpts.RTTI)
576     Builder.defineMacro("__GXX_RTTI");
577   if (LangOpts.SjLjExceptions)
578     Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__");
579 
580   if (LangOpts.Deprecated)
581     Builder.defineMacro("__DEPRECATED");
582 
583   if (!LangOpts.MSVCCompat && LangOpts.CPlusPlus) {
584     Builder.defineMacro("__GNUG__", "4");
585     Builder.defineMacro("__GXX_WEAK__");
586     Builder.defineMacro("__private_extern__", "extern");
587   }
588 
589   if (LangOpts.MicrosoftExt) {
590     if (LangOpts.WChar) {
591       // wchar_t supported as a keyword.
592       Builder.defineMacro("_WCHAR_T_DEFINED");
593       Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED");
594     }
595   }
596 
597   if (LangOpts.Optimize)
598     Builder.defineMacro("__OPTIMIZE__");
599   if (LangOpts.OptimizeSize)
600     Builder.defineMacro("__OPTIMIZE_SIZE__");
601 
602   if (LangOpts.FastMath)
603     Builder.defineMacro("__FAST_MATH__");
604 
605   // Initialize target-specific preprocessor defines.
606 
607   // __BYTE_ORDER__ was added in GCC 4.6. It's analogous
608   // to the macro __BYTE_ORDER (no trailing underscores)
609   // from glibc's <endian.h> header.
610   // We don't support the PDP-11 as a target, but include
611   // the define so it can still be compared against.
612   Builder.defineMacro("__ORDER_LITTLE_ENDIAN__", "1234");
613   Builder.defineMacro("__ORDER_BIG_ENDIAN__",    "4321");
614   Builder.defineMacro("__ORDER_PDP_ENDIAN__",    "3412");
615   if (TI.isBigEndian()) {
616     Builder.defineMacro("__BYTE_ORDER__", "__ORDER_BIG_ENDIAN__");
617     Builder.defineMacro("__BIG_ENDIAN__");
618   } else {
619     Builder.defineMacro("__BYTE_ORDER__", "__ORDER_LITTLE_ENDIAN__");
620     Builder.defineMacro("__LITTLE_ENDIAN__");
621   }
622 
623   if (TI.getPointerWidth(0) == 64 && TI.getLongWidth() == 64
624       && TI.getIntWidth() == 32) {
625     Builder.defineMacro("_LP64");
626     Builder.defineMacro("__LP64__");
627   }
628 
629   if (TI.getPointerWidth(0) == 32 && TI.getLongWidth() == 32
630       && TI.getIntWidth() == 32) {
631     Builder.defineMacro("_ILP32");
632     Builder.defineMacro("__ILP32__");
633   }
634 
635   // Define type sizing macros based on the target properties.
636   assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
637   Builder.defineMacro("__CHAR_BIT__", "8");
638 
639   DefineTypeSize("__SCHAR_MAX__", TargetInfo::SignedChar, TI, Builder);
640   DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder);
641   DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder);
642   DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder);
643   DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder);
644   DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder);
645   DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder);
646   DefineTypeSize("__SIZE_MAX__", TI.getSizeType(), TI, Builder);
647 
648   DefineTypeSize("__UINTMAX_MAX__", TI.getUIntMaxType(), TI, Builder);
649   DefineTypeSize("__PTRDIFF_MAX__", TI.getPtrDiffType(0), TI, Builder);
650   DefineTypeSize("__INTPTR_MAX__", TI.getIntPtrType(), TI, Builder);
651   DefineTypeSize("__UINTPTR_MAX__", TI.getUIntPtrType(), TI, Builder);
652 
653   DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder);
654   DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder);
655   DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder);
656   DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder);
657   DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder);
658   DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder);
659   DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder);
660   DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder);
661   DefineTypeSizeof("__SIZEOF_PTRDIFF_T__",
662                    TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder);
663   DefineTypeSizeof("__SIZEOF_SIZE_T__",
664                    TI.getTypeWidth(TI.getSizeType()), TI, Builder);
665   DefineTypeSizeof("__SIZEOF_WCHAR_T__",
666                    TI.getTypeWidth(TI.getWCharType()), TI, Builder);
667   DefineTypeSizeof("__SIZEOF_WINT_T__",
668                    TI.getTypeWidth(TI.getWIntType()), TI, Builder);
669   if (TI.hasInt128Type())
670     DefineTypeSizeof("__SIZEOF_INT128__", 128, TI, Builder);
671 
672   DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder);
673   DefineFmt("__INTMAX", TI.getIntMaxType(), TI, Builder);
674   Builder.defineMacro("__INTMAX_C_SUFFIX__",
675                       TI.getTypeConstantSuffix(TI.getIntMaxType()));
676   DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder);
677   DefineFmt("__UINTMAX", TI.getUIntMaxType(), TI, Builder);
678   Builder.defineMacro("__UINTMAX_C_SUFFIX__",
679                       TI.getTypeConstantSuffix(TI.getUIntMaxType()));
680   DefineTypeWidth("__INTMAX_WIDTH__",  TI.getIntMaxType(), TI, Builder);
681   DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder);
682   DefineFmt("__PTRDIFF", TI.getPtrDiffType(0), TI, Builder);
683   DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder);
684   DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder);
685   DefineFmt("__INTPTR", TI.getIntPtrType(), TI, Builder);
686   DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder);
687   DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder);
688   DefineFmt("__SIZE", TI.getSizeType(), TI, Builder);
689   DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder);
690   DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder);
691   DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder);
692   DefineType("__WINT_TYPE__", TI.getWIntType(), Builder);
693   DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder);
694   DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder);
695   DefineTypeSize("__SIG_ATOMIC_MAX__", TI.getSigAtomicType(), TI, Builder);
696   DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder);
697   DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder);
698 
699   DefineTypeWidth("__UINTMAX_WIDTH__",  TI.getUIntMaxType(), TI, Builder);
700   DefineType("__UINTPTR_TYPE__", TI.getUIntPtrType(), Builder);
701   DefineFmt("__UINTPTR", TI.getUIntPtrType(), TI, Builder);
702   DefineTypeWidth("__UINTPTR_WIDTH__", TI.getUIntPtrType(), TI, Builder);
703 
704   DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat(), "F");
705   DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat(), "");
706   DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat(), "L");
707 
708   // Define a __POINTER_WIDTH__ macro for stdint.h.
709   Builder.defineMacro("__POINTER_WIDTH__",
710                       Twine((int)TI.getPointerWidth(0)));
711 
712   // Define __BIGGEST_ALIGNMENT__ to be compatible with gcc.
713   Builder.defineMacro("__BIGGEST_ALIGNMENT__",
714                       Twine(TI.getSuitableAlign() / TI.getCharWidth()) );
715 
716   if (!LangOpts.CharIsSigned)
717     Builder.defineMacro("__CHAR_UNSIGNED__");
718 
719   if (!TargetInfo::isTypeSigned(TI.getWCharType()))
720     Builder.defineMacro("__WCHAR_UNSIGNED__");
721 
722   if (!TargetInfo::isTypeSigned(TI.getWIntType()))
723     Builder.defineMacro("__WINT_UNSIGNED__");
724 
725   // Define exact-width integer types for stdint.h
726   DefineExactWidthIntType(TargetInfo::SignedChar, TI, Builder);
727 
728   if (TI.getShortWidth() > TI.getCharWidth())
729     DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder);
730 
731   if (TI.getIntWidth() > TI.getShortWidth())
732     DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder);
733 
734   if (TI.getLongWidth() > TI.getIntWidth())
735     DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder);
736 
737   if (TI.getLongLongWidth() > TI.getLongWidth())
738     DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder);
739 
740   DefineExactWidthIntType(TargetInfo::UnsignedChar, TI, Builder);
741   DefineExactWidthIntTypeSize(TargetInfo::UnsignedChar, TI, Builder);
742   DefineExactWidthIntTypeSize(TargetInfo::SignedChar, TI, Builder);
743 
744   if (TI.getShortWidth() > TI.getCharWidth()) {
745     DefineExactWidthIntType(TargetInfo::UnsignedShort, TI, Builder);
746     DefineExactWidthIntTypeSize(TargetInfo::UnsignedShort, TI, Builder);
747     DefineExactWidthIntTypeSize(TargetInfo::SignedShort, TI, Builder);
748   }
749 
750   if (TI.getIntWidth() > TI.getShortWidth()) {
751     DefineExactWidthIntType(TargetInfo::UnsignedInt, TI, Builder);
752     DefineExactWidthIntTypeSize(TargetInfo::UnsignedInt, TI, Builder);
753     DefineExactWidthIntTypeSize(TargetInfo::SignedInt, TI, Builder);
754   }
755 
756   if (TI.getLongWidth() > TI.getIntWidth()) {
757     DefineExactWidthIntType(TargetInfo::UnsignedLong, TI, Builder);
758     DefineExactWidthIntTypeSize(TargetInfo::UnsignedLong, TI, Builder);
759     DefineExactWidthIntTypeSize(TargetInfo::SignedLong, TI, Builder);
760   }
761 
762   if (TI.getLongLongWidth() > TI.getLongWidth()) {
763     DefineExactWidthIntType(TargetInfo::UnsignedLongLong, TI, Builder);
764     DefineExactWidthIntTypeSize(TargetInfo::UnsignedLongLong, TI, Builder);
765     DefineExactWidthIntTypeSize(TargetInfo::SignedLongLong, TI, Builder);
766   }
767 
768   DefineLeastWidthIntType(8, true, TI, Builder);
769   DefineLeastWidthIntType(8, false, TI, Builder);
770   DefineLeastWidthIntType(16, true, TI, Builder);
771   DefineLeastWidthIntType(16, false, TI, Builder);
772   DefineLeastWidthIntType(32, true, TI, Builder);
773   DefineLeastWidthIntType(32, false, TI, Builder);
774   DefineLeastWidthIntType(64, true, TI, Builder);
775   DefineLeastWidthIntType(64, false, TI, Builder);
776 
777   DefineFastIntType(8, true, TI, Builder);
778   DefineFastIntType(8, false, TI, Builder);
779   DefineFastIntType(16, true, TI, Builder);
780   DefineFastIntType(16, false, TI, Builder);
781   DefineFastIntType(32, true, TI, Builder);
782   DefineFastIntType(32, false, TI, Builder);
783   DefineFastIntType(64, true, TI, Builder);
784   DefineFastIntType(64, false, TI, Builder);
785 
786   if (const char *Prefix = TI.getUserLabelPrefix())
787     Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix);
788 
789   if (LangOpts.FastMath || LangOpts.FiniteMathOnly)
790     Builder.defineMacro("__FINITE_MATH_ONLY__", "1");
791   else
792     Builder.defineMacro("__FINITE_MATH_ONLY__", "0");
793 
794   if (!LangOpts.MSVCCompat) {
795     if (LangOpts.GNUInline || LangOpts.CPlusPlus)
796       Builder.defineMacro("__GNUC_GNU_INLINE__");
797     else
798       Builder.defineMacro("__GNUC_STDC_INLINE__");
799 
800     // The value written by __atomic_test_and_set.
801     // FIXME: This is target-dependent.
802     Builder.defineMacro("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1");
803 
804     // Used by libstdc++ to implement ATOMIC_<foo>_LOCK_FREE.
805     unsigned InlineWidthBits = TI.getMaxAtomicInlineWidth();
806 #define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \
807     Builder.defineMacro("__GCC_ATOMIC_" #TYPE "_LOCK_FREE", \
808                         getLockFreeValue(TI.get##Type##Width(), \
809                                          TI.get##Type##Align(), \
810                                          InlineWidthBits));
811     DEFINE_LOCK_FREE_MACRO(BOOL, Bool);
812     DEFINE_LOCK_FREE_MACRO(CHAR, Char);
813     DEFINE_LOCK_FREE_MACRO(CHAR16_T, Char16);
814     DEFINE_LOCK_FREE_MACRO(CHAR32_T, Char32);
815     DEFINE_LOCK_FREE_MACRO(WCHAR_T, WChar);
816     DEFINE_LOCK_FREE_MACRO(SHORT, Short);
817     DEFINE_LOCK_FREE_MACRO(INT, Int);
818     DEFINE_LOCK_FREE_MACRO(LONG, Long);
819     DEFINE_LOCK_FREE_MACRO(LLONG, LongLong);
820     Builder.defineMacro("__GCC_ATOMIC_POINTER_LOCK_FREE",
821                         getLockFreeValue(TI.getPointerWidth(0),
822                                          TI.getPointerAlign(0),
823                                          InlineWidthBits));
824 #undef DEFINE_LOCK_FREE_MACRO
825   }
826 
827   if (LangOpts.NoInlineDefine)
828     Builder.defineMacro("__NO_INLINE__");
829 
830   if (unsigned PICLevel = LangOpts.PICLevel) {
831     Builder.defineMacro("__PIC__", Twine(PICLevel));
832     Builder.defineMacro("__pic__", Twine(PICLevel));
833   }
834   if (unsigned PIELevel = LangOpts.PIELevel) {
835     Builder.defineMacro("__PIE__", Twine(PIELevel));
836     Builder.defineMacro("__pie__", Twine(PIELevel));
837   }
838 
839   // Macros to control C99 numerics and <float.h>
840   Builder.defineMacro("__FLT_EVAL_METHOD__", Twine(TI.getFloatEvalMethod()));
841   Builder.defineMacro("__FLT_RADIX__", "2");
842   Builder.defineMacro("__DECIMAL_DIG__", "__LDBL_DECIMAL_DIG__");
843 
844   if (LangOpts.getStackProtector() == LangOptions::SSPOn)
845     Builder.defineMacro("__SSP__");
846   else if (LangOpts.getStackProtector() == LangOptions::SSPStrong)
847     Builder.defineMacro("__SSP_STRONG__", "2");
848   else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
849     Builder.defineMacro("__SSP_ALL__", "3");
850 
851   if (FEOpts.ProgramAction == frontend::RewriteObjC)
852     Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
853 
854   // Define a macro that exists only when using the static analyzer.
855   if (FEOpts.ProgramAction == frontend::RunAnalysis)
856     Builder.defineMacro("__clang_analyzer__");
857 
858   if (LangOpts.FastRelaxedMath)
859     Builder.defineMacro("__FAST_RELAXED_MATH__");
860 
861   if (LangOpts.ObjCAutoRefCount) {
862     Builder.defineMacro("__weak", "__attribute__((objc_ownership(weak)))");
863     Builder.defineMacro("__strong", "__attribute__((objc_ownership(strong)))");
864     Builder.defineMacro("__autoreleasing",
865                         "__attribute__((objc_ownership(autoreleasing)))");
866     Builder.defineMacro("__unsafe_unretained",
867                         "__attribute__((objc_ownership(none)))");
868   }
869 
870   // OpenMP definition
871   if (LangOpts.OpenMP) {
872     // OpenMP 2.2:
873     //   In implementations that support a preprocessor, the _OPENMP
874     //   macro name is defined to have the decimal value yyyymm where
875     //   yyyy and mm are the year and the month designations of the
876     //   version of the OpenMP API that the implementation support.
877     Builder.defineMacro("_OPENMP", "201307");
878   }
879 
880   // CUDA device path compilaton
881   if (LangOpts.CUDAIsDevice) {
882     // The CUDA_ARCH value is set for the GPU target specified in the NVPTX
883     // backend's target defines.
884     Builder.defineMacro("__CUDA_ARCH__");
885   }
886 
887   // Get other target #defines.
888   TI.getTargetDefines(LangOpts, Builder);
889 }
890 
891 /// InitializePreprocessor - Initialize the preprocessor getting it and the
892 /// environment ready to process a single file. This returns true on error.
893 ///
894 void clang::InitializePreprocessor(Preprocessor &PP,
895                                    const PreprocessorOptions &InitOpts,
896                                    const FrontendOptions &FEOpts) {
897   const LangOptions &LangOpts = PP.getLangOpts();
898   std::string PredefineBuffer;
899   PredefineBuffer.reserve(4080);
900   llvm::raw_string_ostream Predefines(PredefineBuffer);
901   MacroBuilder Builder(Predefines);
902 
903   // Emit line markers for various builtin sections of the file.  We don't do
904   // this in asm preprocessor mode, because "# 4" is not a line marker directive
905   // in this mode.
906   if (!PP.getLangOpts().AsmPreprocessor)
907     Builder.append("# 1 \"<built-in>\" 3");
908 
909   // Install things like __POWERPC__, __GNUC__, etc into the macro table.
910   if (InitOpts.UsePredefines) {
911     InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, Builder);
912 
913     // Install definitions to make Objective-C++ ARC work well with various
914     // C++ Standard Library implementations.
915     if (LangOpts.ObjC1 && LangOpts.CPlusPlus && LangOpts.ObjCAutoRefCount) {
916       switch (InitOpts.ObjCXXARCStandardLibrary) {
917       case ARCXX_nolib:
918         case ARCXX_libcxx:
919         break;
920 
921       case ARCXX_libstdcxx:
922         AddObjCXXARCLibstdcxxDefines(LangOpts, Builder);
923         break;
924       }
925     }
926   }
927 
928   // Even with predefines off, some macros are still predefined.
929   // These should all be defined in the preprocessor according to the
930   // current language configuration.
931   InitializeStandardPredefinedMacros(PP.getTargetInfo(), PP.getLangOpts(),
932                                      FEOpts, Builder);
933 
934   // Add on the predefines from the driver.  Wrap in a #line directive to report
935   // that they come from the command line.
936   if (!PP.getLangOpts().AsmPreprocessor)
937     Builder.append("# 1 \"<command line>\" 1");
938 
939   // Process #define's and #undef's in the order they are given.
940   for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
941     if (InitOpts.Macros[i].second)  // isUndef
942       Builder.undefineMacro(InitOpts.Macros[i].first);
943     else
944       DefineBuiltinMacro(Builder, InitOpts.Macros[i].first,
945                          PP.getDiagnostics());
946   }
947 
948   // If -imacros are specified, include them now.  These are processed before
949   // any -include directives.
950   for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
951     AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i]);
952 
953   // Process -include-pch/-include-pth directives.
954   if (!InitOpts.ImplicitPCHInclude.empty())
955     AddImplicitIncludePCH(Builder, PP, InitOpts.ImplicitPCHInclude);
956   if (!InitOpts.ImplicitPTHInclude.empty())
957     AddImplicitIncludePTH(Builder, PP, InitOpts.ImplicitPTHInclude);
958 
959   // Process -include directives.
960   for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
961     const std::string &Path = InitOpts.Includes[i];
962     AddImplicitInclude(Builder, Path);
963   }
964 
965   // Exit the command line and go back to <built-in> (2 is LC_LEAVE).
966   if (!PP.getLangOpts().AsmPreprocessor)
967     Builder.append("# 1 \"<built-in>\" 2");
968 
969   // Instruct the preprocessor to skip the preamble.
970   PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first,
971                              InitOpts.PrecompiledPreambleBytes.second);
972 
973   // Copy PredefinedBuffer into the Preprocessor.
974   PP.setPredefines(Predefines.str());
975 }
976