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 
MacroBodyEndsInBackslash(StringRef MacroBody)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=".
DefineBuiltinMacro(MacroBuilder & Builder,StringRef Macro,DiagnosticsEngine & Diags)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.
AddImplicitInclude(MacroBuilder & Builder,StringRef File)70 static void AddImplicitInclude(MacroBuilder &Builder, StringRef File) {
71   Builder.append(Twine("#include \"") + File + "\"");
72 }
73 
AddImplicitIncludeMacros(MacroBuilder & Builder,StringRef File)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.
AddImplicitIncludePTH(MacroBuilder & Builder,Preprocessor & PP,StringRef ImplicitIncludePTH)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.
AddImplicitIncludePCH(MacroBuilder & Builder,Preprocessor & PP,StringRef ImplicitIncludePCH)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>
PickFP(const llvm::fltSemantics * Sem,T IEEESingleVal,T IEEEDoubleVal,T X87DoubleExtendedVal,T PPCDoubleDoubleVal,T IEEEQuadVal)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 
DefineFloatMacros(MacroBuilder & Builder,StringRef Prefix,const llvm::fltSemantics * Sem,StringRef Ext)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).
DefineTypeSize(const Twine & MacroName,unsigned TypeWidth,StringRef ValSuffix,bool isSigned,MacroBuilder & Builder)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
DefineTypeSize(const Twine & MacroName,TargetInfo::IntType Ty,const TargetInfo & TI,MacroBuilder & Builder)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 
DefineFmt(const Twine & Prefix,TargetInfo::IntType Ty,const TargetInfo & TI,MacroBuilder & Builder)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 
DefineType(const Twine & MacroName,TargetInfo::IntType Ty,MacroBuilder & Builder)208 static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty,
209                        MacroBuilder &Builder) {
210   Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty));
211 }
212 
DefineTypeWidth(StringRef MacroName,TargetInfo::IntType Ty,const TargetInfo & TI,MacroBuilder & Builder)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 
DefineTypeSizeof(StringRef MacroName,unsigned BitWidth,const TargetInfo & TI,MacroBuilder & Builder)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 
DefineExactWidthIntType(TargetInfo::IntType Ty,const TargetInfo & TI,MacroBuilder & Builder)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 
DefineExactWidthIntTypeSize(TargetInfo::IntType Ty,const TargetInfo & TI,MacroBuilder & Builder)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 
DefineLeastWidthIntType(unsigned TypeWidth,bool IsSigned,const TargetInfo & TI,MacroBuilder & Builder)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 
DefineFastIntType(unsigned TypeWidth,bool IsSigned,const TargetInfo & TI,MacroBuilder & Builder)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.
getLockFreeValue(unsigned TypeWidth,unsigned TypeAlign,unsigned InlineWidth)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).
AddObjCXXARCLibstdcxxDefines(const LangOptions & LangOpts,MacroBuilder & Builder)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 
InitializeStandardPredefinedMacros(const TargetInfo & TI,const LangOptions & LangOpts,const FrontendOptions & FEOpts,MacroBuilder & Builder)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".
InitializeCPlusPlusFeatureTestMacros(const LangOptions & LangOpts,MacroBuilder & Builder)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 }
457 
InitializePredefinedMacros(const TargetInfo & TI,const LangOptions & LangOpts,const FrontendOptions & FEOpts,MacroBuilder & Builder)458 static void InitializePredefinedMacros(const TargetInfo &TI,
459                                        const LangOptions &LangOpts,
460                                        const FrontendOptions &FEOpts,
461                                        MacroBuilder &Builder) {
462   // Compiler version introspection macros.
463   Builder.defineMacro("__llvm__");  // LLVM Backend
464   Builder.defineMacro("__clang__"); // Clang Frontend
465 #define TOSTR2(X) #X
466 #define TOSTR(X) TOSTR2(X)
467   Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR));
468   Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR));
469 #ifdef CLANG_VERSION_PATCHLEVEL
470   Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL));
471 #else
472   Builder.defineMacro("__clang_patchlevel__", "0");
473 #endif
474   Builder.defineMacro("__clang_version__",
475                       "\"" CLANG_VERSION_STRING " "
476                       + getClangFullRepositoryVersion() + "\"");
477 #undef TOSTR
478 #undef TOSTR2
479   if (!LangOpts.MSVCCompat) {
480     // Currently claim to be compatible with GCC 4.2.1-5621, but only if we're
481     // not compiling for MSVC compatibility
482     Builder.defineMacro("__GNUC_MINOR__", "2");
483     Builder.defineMacro("__GNUC_PATCHLEVEL__", "1");
484     Builder.defineMacro("__GNUC__", "4");
485     Builder.defineMacro("__GXX_ABI_VERSION", "1002");
486   }
487 
488   // Define macros for the C11 / C++11 memory orderings
489   Builder.defineMacro("__ATOMIC_RELAXED", "0");
490   Builder.defineMacro("__ATOMIC_CONSUME", "1");
491   Builder.defineMacro("__ATOMIC_ACQUIRE", "2");
492   Builder.defineMacro("__ATOMIC_RELEASE", "3");
493   Builder.defineMacro("__ATOMIC_ACQ_REL", "4");
494   Builder.defineMacro("__ATOMIC_SEQ_CST", "5");
495 
496   // Support for #pragma redefine_extname (Sun compatibility)
497   Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1");
498 
499   // As sad as it is, enough software depends on the __VERSION__ for version
500   // checks that it is necessary to report 4.2.1 (the base GCC version we claim
501   // compatibility with) first.
502   Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible " +
503                       Twine(getClangFullCPPVersion()) + "\"");
504 
505   // Initialize language-specific preprocessor defines.
506 
507   // Standard conforming mode?
508   if (!LangOpts.GNUMode && !LangOpts.MSVCCompat)
509     Builder.defineMacro("__STRICT_ANSI__");
510 
511   if (!LangOpts.MSVCCompat && LangOpts.CPlusPlus11)
512     Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__");
513 
514   if (LangOpts.ObjC1) {
515     if (LangOpts.ObjCRuntime.isNonFragile()) {
516       Builder.defineMacro("__OBJC2__");
517 
518       if (LangOpts.ObjCExceptions)
519         Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS");
520     }
521 
522     if (LangOpts.getGC() != LangOptions::NonGC)
523       Builder.defineMacro("__OBJC_GC__");
524 
525     if (LangOpts.ObjCRuntime.isNeXTFamily())
526       Builder.defineMacro("__NEXT_RUNTIME__");
527 
528     if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::ObjFW) {
529       VersionTuple tuple = LangOpts.ObjCRuntime.getVersion();
530 
531       unsigned minor = 0;
532       if (tuple.getMinor().hasValue())
533         minor = tuple.getMinor().getValue();
534 
535       unsigned subminor = 0;
536       if (tuple.getSubminor().hasValue())
537         subminor = tuple.getSubminor().getValue();
538 
539       Builder.defineMacro("__OBJFW_RUNTIME_ABI__",
540                           Twine(tuple.getMajor() * 10000 + minor * 100 +
541                                 subminor));
542     }
543 
544     Builder.defineMacro("IBOutlet", "__attribute__((iboutlet))");
545     Builder.defineMacro("IBOutletCollection(ClassName)",
546                         "__attribute__((iboutletcollection(ClassName)))");
547     Builder.defineMacro("IBAction", "void)__attribute__((ibaction)");
548     Builder.defineMacro("IBInspectable", "");
549     Builder.defineMacro("IB_DESIGNABLE", "");
550   }
551 
552   if (LangOpts.CPlusPlus)
553     InitializeCPlusPlusFeatureTestMacros(LangOpts, Builder);
554 
555   // darwin_constant_cfstrings controls this. This is also dependent
556   // on other things like the runtime I believe.  This is set even for C code.
557   if (!LangOpts.NoConstantCFStrings)
558       Builder.defineMacro("__CONSTANT_CFSTRINGS__");
559 
560   if (LangOpts.ObjC2)
561     Builder.defineMacro("OBJC_NEW_PROPERTIES");
562 
563   if (LangOpts.PascalStrings)
564     Builder.defineMacro("__PASCAL_STRINGS__");
565 
566   if (LangOpts.Blocks) {
567     Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))");
568     Builder.defineMacro("__BLOCKS__");
569   }
570 
571   if (!LangOpts.MSVCCompat && LangOpts.Exceptions)
572     Builder.defineMacro("__EXCEPTIONS");
573   if (!LangOpts.MSVCCompat && LangOpts.RTTI)
574     Builder.defineMacro("__GXX_RTTI");
575   if (LangOpts.SjLjExceptions)
576     Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__");
577 
578   if (LangOpts.Deprecated)
579     Builder.defineMacro("__DEPRECATED");
580 
581   if (!LangOpts.MSVCCompat && LangOpts.CPlusPlus) {
582     Builder.defineMacro("__GNUG__", "4");
583     Builder.defineMacro("__GXX_WEAK__");
584     Builder.defineMacro("__private_extern__", "extern");
585   }
586 
587   if (LangOpts.MicrosoftExt) {
588     if (LangOpts.WChar) {
589       // wchar_t supported as a keyword.
590       Builder.defineMacro("_WCHAR_T_DEFINED");
591       Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED");
592     }
593   }
594 
595   if (LangOpts.Optimize)
596     Builder.defineMacro("__OPTIMIZE__");
597   if (LangOpts.OptimizeSize)
598     Builder.defineMacro("__OPTIMIZE_SIZE__");
599 
600   if (LangOpts.FastMath)
601     Builder.defineMacro("__FAST_MATH__");
602 
603   // Initialize target-specific preprocessor defines.
604 
605   // __BYTE_ORDER__ was added in GCC 4.6. It's analogous
606   // to the macro __BYTE_ORDER (no trailing underscores)
607   // from glibc's <endian.h> header.
608   // We don't support the PDP-11 as a target, but include
609   // the define so it can still be compared against.
610   Builder.defineMacro("__ORDER_LITTLE_ENDIAN__", "1234");
611   Builder.defineMacro("__ORDER_BIG_ENDIAN__",    "4321");
612   Builder.defineMacro("__ORDER_PDP_ENDIAN__",    "3412");
613   if (TI.isBigEndian()) {
614     Builder.defineMacro("__BYTE_ORDER__", "__ORDER_BIG_ENDIAN__");
615     Builder.defineMacro("__BIG_ENDIAN__");
616   } else {
617     Builder.defineMacro("__BYTE_ORDER__", "__ORDER_LITTLE_ENDIAN__");
618     Builder.defineMacro("__LITTLE_ENDIAN__");
619   }
620 
621   if (TI.getPointerWidth(0) == 64 && TI.getLongWidth() == 64
622       && TI.getIntWidth() == 32) {
623     Builder.defineMacro("_LP64");
624     Builder.defineMacro("__LP64__");
625   }
626 
627   if (TI.getPointerWidth(0) == 32 && TI.getLongWidth() == 32
628       && TI.getIntWidth() == 32) {
629     Builder.defineMacro("_ILP32");
630     Builder.defineMacro("__ILP32__");
631   }
632 
633   // Define type sizing macros based on the target properties.
634   assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
635   Builder.defineMacro("__CHAR_BIT__", "8");
636 
637   DefineTypeSize("__SCHAR_MAX__", TargetInfo::SignedChar, TI, Builder);
638   DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder);
639   DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder);
640   DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder);
641   DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder);
642   DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder);
643   DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder);
644   DefineTypeSize("__SIZE_MAX__", TI.getSizeType(), TI, Builder);
645 
646   DefineTypeSize("__UINTMAX_MAX__", TI.getUIntMaxType(), TI, Builder);
647   DefineTypeSize("__PTRDIFF_MAX__", TI.getPtrDiffType(0), TI, Builder);
648   DefineTypeSize("__INTPTR_MAX__", TI.getIntPtrType(), TI, Builder);
649   DefineTypeSize("__UINTPTR_MAX__", TI.getUIntPtrType(), TI, Builder);
650 
651   DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder);
652   DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder);
653   DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder);
654   DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder);
655   DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder);
656   DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder);
657   DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder);
658   DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder);
659   DefineTypeSizeof("__SIZEOF_PTRDIFF_T__",
660                    TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder);
661   DefineTypeSizeof("__SIZEOF_SIZE_T__",
662                    TI.getTypeWidth(TI.getSizeType()), TI, Builder);
663   DefineTypeSizeof("__SIZEOF_WCHAR_T__",
664                    TI.getTypeWidth(TI.getWCharType()), TI, Builder);
665   DefineTypeSizeof("__SIZEOF_WINT_T__",
666                    TI.getTypeWidth(TI.getWIntType()), TI, Builder);
667   if (TI.hasInt128Type())
668     DefineTypeSizeof("__SIZEOF_INT128__", 128, TI, Builder);
669 
670   DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder);
671   DefineFmt("__INTMAX", TI.getIntMaxType(), TI, Builder);
672   Builder.defineMacro("__INTMAX_C_SUFFIX__",
673                       TI.getTypeConstantSuffix(TI.getIntMaxType()));
674   DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder);
675   DefineFmt("__UINTMAX", TI.getUIntMaxType(), TI, Builder);
676   Builder.defineMacro("__UINTMAX_C_SUFFIX__",
677                       TI.getTypeConstantSuffix(TI.getUIntMaxType()));
678   DefineTypeWidth("__INTMAX_WIDTH__",  TI.getIntMaxType(), TI, Builder);
679   DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder);
680   DefineFmt("__PTRDIFF", TI.getPtrDiffType(0), TI, Builder);
681   DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder);
682   DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder);
683   DefineFmt("__INTPTR", TI.getIntPtrType(), TI, Builder);
684   DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder);
685   DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder);
686   DefineFmt("__SIZE", TI.getSizeType(), TI, Builder);
687   DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder);
688   DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder);
689   DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder);
690   DefineType("__WINT_TYPE__", TI.getWIntType(), Builder);
691   DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder);
692   DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder);
693   DefineTypeSize("__SIG_ATOMIC_MAX__", TI.getSigAtomicType(), TI, Builder);
694   DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder);
695   DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder);
696 
697   DefineTypeWidth("__UINTMAX_WIDTH__",  TI.getUIntMaxType(), TI, Builder);
698   DefineType("__UINTPTR_TYPE__", TI.getUIntPtrType(), Builder);
699   DefineFmt("__UINTPTR", TI.getUIntPtrType(), TI, Builder);
700   DefineTypeWidth("__UINTPTR_WIDTH__", TI.getUIntPtrType(), TI, Builder);
701 
702   DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat(), "F");
703   DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat(), "");
704   DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat(), "L");
705 
706   // Define a __POINTER_WIDTH__ macro for stdint.h.
707   Builder.defineMacro("__POINTER_WIDTH__",
708                       Twine((int)TI.getPointerWidth(0)));
709 
710   // Define __BIGGEST_ALIGNMENT__ to be compatible with gcc.
711   Builder.defineMacro("__BIGGEST_ALIGNMENT__",
712                       Twine(TI.getSuitableAlign() / TI.getCharWidth()) );
713 
714   if (!LangOpts.CharIsSigned)
715     Builder.defineMacro("__CHAR_UNSIGNED__");
716 
717   if (!TargetInfo::isTypeSigned(TI.getWCharType()))
718     Builder.defineMacro("__WCHAR_UNSIGNED__");
719 
720   if (!TargetInfo::isTypeSigned(TI.getWIntType()))
721     Builder.defineMacro("__WINT_UNSIGNED__");
722 
723   // Define exact-width integer types for stdint.h
724   DefineExactWidthIntType(TargetInfo::SignedChar, TI, Builder);
725 
726   if (TI.getShortWidth() > TI.getCharWidth())
727     DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder);
728 
729   if (TI.getIntWidth() > TI.getShortWidth())
730     DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder);
731 
732   if (TI.getLongWidth() > TI.getIntWidth())
733     DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder);
734 
735   if (TI.getLongLongWidth() > TI.getLongWidth())
736     DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder);
737 
738   DefineExactWidthIntType(TargetInfo::UnsignedChar, TI, Builder);
739   DefineExactWidthIntTypeSize(TargetInfo::UnsignedChar, TI, Builder);
740   DefineExactWidthIntTypeSize(TargetInfo::SignedChar, TI, Builder);
741 
742   if (TI.getShortWidth() > TI.getCharWidth()) {
743     DefineExactWidthIntType(TargetInfo::UnsignedShort, TI, Builder);
744     DefineExactWidthIntTypeSize(TargetInfo::UnsignedShort, TI, Builder);
745     DefineExactWidthIntTypeSize(TargetInfo::SignedShort, TI, Builder);
746   }
747 
748   if (TI.getIntWidth() > TI.getShortWidth()) {
749     DefineExactWidthIntType(TargetInfo::UnsignedInt, TI, Builder);
750     DefineExactWidthIntTypeSize(TargetInfo::UnsignedInt, TI, Builder);
751     DefineExactWidthIntTypeSize(TargetInfo::SignedInt, TI, Builder);
752   }
753 
754   if (TI.getLongWidth() > TI.getIntWidth()) {
755     DefineExactWidthIntType(TargetInfo::UnsignedLong, TI, Builder);
756     DefineExactWidthIntTypeSize(TargetInfo::UnsignedLong, TI, Builder);
757     DefineExactWidthIntTypeSize(TargetInfo::SignedLong, TI, Builder);
758   }
759 
760   if (TI.getLongLongWidth() > TI.getLongWidth()) {
761     DefineExactWidthIntType(TargetInfo::UnsignedLongLong, TI, Builder);
762     DefineExactWidthIntTypeSize(TargetInfo::UnsignedLongLong, TI, Builder);
763     DefineExactWidthIntTypeSize(TargetInfo::SignedLongLong, TI, Builder);
764   }
765 
766   DefineLeastWidthIntType(8, true, TI, Builder);
767   DefineLeastWidthIntType(8, false, TI, Builder);
768   DefineLeastWidthIntType(16, true, TI, Builder);
769   DefineLeastWidthIntType(16, false, TI, Builder);
770   DefineLeastWidthIntType(32, true, TI, Builder);
771   DefineLeastWidthIntType(32, false, TI, Builder);
772   DefineLeastWidthIntType(64, true, TI, Builder);
773   DefineLeastWidthIntType(64, false, TI, Builder);
774 
775   DefineFastIntType(8, true, TI, Builder);
776   DefineFastIntType(8, false, TI, Builder);
777   DefineFastIntType(16, true, TI, Builder);
778   DefineFastIntType(16, false, TI, Builder);
779   DefineFastIntType(32, true, TI, Builder);
780   DefineFastIntType(32, false, TI, Builder);
781   DefineFastIntType(64, true, TI, Builder);
782   DefineFastIntType(64, false, TI, Builder);
783 
784   if (const char *Prefix = TI.getUserLabelPrefix())
785     Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix);
786 
787   if (LangOpts.FastMath || LangOpts.FiniteMathOnly)
788     Builder.defineMacro("__FINITE_MATH_ONLY__", "1");
789   else
790     Builder.defineMacro("__FINITE_MATH_ONLY__", "0");
791 
792   if (!LangOpts.MSVCCompat) {
793     if (LangOpts.GNUInline)
794       Builder.defineMacro("__GNUC_GNU_INLINE__");
795     else
796       Builder.defineMacro("__GNUC_STDC_INLINE__");
797 
798     // The value written by __atomic_test_and_set.
799     // FIXME: This is target-dependent.
800     Builder.defineMacro("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1");
801 
802     // Used by libstdc++ to implement ATOMIC_<foo>_LOCK_FREE.
803     unsigned InlineWidthBits = TI.getMaxAtomicInlineWidth();
804 #define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \
805     Builder.defineMacro("__GCC_ATOMIC_" #TYPE "_LOCK_FREE", \
806                         getLockFreeValue(TI.get##Type##Width(), \
807                                          TI.get##Type##Align(), \
808                                          InlineWidthBits));
809     DEFINE_LOCK_FREE_MACRO(BOOL, Bool);
810     DEFINE_LOCK_FREE_MACRO(CHAR, Char);
811     DEFINE_LOCK_FREE_MACRO(CHAR16_T, Char16);
812     DEFINE_LOCK_FREE_MACRO(CHAR32_T, Char32);
813     DEFINE_LOCK_FREE_MACRO(WCHAR_T, WChar);
814     DEFINE_LOCK_FREE_MACRO(SHORT, Short);
815     DEFINE_LOCK_FREE_MACRO(INT, Int);
816     DEFINE_LOCK_FREE_MACRO(LONG, Long);
817     DEFINE_LOCK_FREE_MACRO(LLONG, LongLong);
818     Builder.defineMacro("__GCC_ATOMIC_POINTER_LOCK_FREE",
819                         getLockFreeValue(TI.getPointerWidth(0),
820                                          TI.getPointerAlign(0),
821                                          InlineWidthBits));
822 #undef DEFINE_LOCK_FREE_MACRO
823   }
824 
825   if (LangOpts.NoInlineDefine)
826     Builder.defineMacro("__NO_INLINE__");
827 
828   if (unsigned PICLevel = LangOpts.PICLevel) {
829     Builder.defineMacro("__PIC__", Twine(PICLevel));
830     Builder.defineMacro("__pic__", Twine(PICLevel));
831   }
832   if (unsigned PIELevel = LangOpts.PIELevel) {
833     Builder.defineMacro("__PIE__", Twine(PIELevel));
834     Builder.defineMacro("__pie__", Twine(PIELevel));
835   }
836 
837   // Macros to control C99 numerics and <float.h>
838   Builder.defineMacro("__FLT_EVAL_METHOD__", Twine(TI.getFloatEvalMethod()));
839   Builder.defineMacro("__FLT_RADIX__", "2");
840   Builder.defineMacro("__DECIMAL_DIG__", "__LDBL_DECIMAL_DIG__");
841 
842   if (LangOpts.getStackProtector() == LangOptions::SSPOn)
843     Builder.defineMacro("__SSP__");
844   else if (LangOpts.getStackProtector() == LangOptions::SSPStrong)
845     Builder.defineMacro("__SSP_STRONG__", "2");
846   else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
847     Builder.defineMacro("__SSP_ALL__", "3");
848 
849   if (FEOpts.ProgramAction == frontend::RewriteObjC)
850     Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
851 
852   // Define a macro that exists only when using the static analyzer.
853   if (FEOpts.ProgramAction == frontend::RunAnalysis)
854     Builder.defineMacro("__clang_analyzer__");
855 
856   if (LangOpts.FastRelaxedMath)
857     Builder.defineMacro("__FAST_RELAXED_MATH__");
858 
859   if (LangOpts.ObjCAutoRefCount) {
860     Builder.defineMacro("__weak", "__attribute__((objc_ownership(weak)))");
861     Builder.defineMacro("__strong", "__attribute__((objc_ownership(strong)))");
862     Builder.defineMacro("__autoreleasing",
863                         "__attribute__((objc_ownership(autoreleasing)))");
864     Builder.defineMacro("__unsafe_unretained",
865                         "__attribute__((objc_ownership(none)))");
866   }
867 
868   // OpenMP definition
869   if (LangOpts.OpenMP) {
870     // OpenMP 2.2:
871     //   In implementations that support a preprocessor, the _OPENMP
872     //   macro name is defined to have the decimal value yyyymm where
873     //   yyyy and mm are the year and the month designations of the
874     //   version of the OpenMP API that the implementation support.
875     Builder.defineMacro("_OPENMP", "201307");
876   }
877 
878   // CUDA device path compilaton
879   if (LangOpts.CUDAIsDevice) {
880     // The CUDA_ARCH value is set for the GPU target specified in the NVPTX
881     // backend's target defines.
882     Builder.defineMacro("__CUDA_ARCH__");
883   }
884 
885   // Get other target #defines.
886   TI.getTargetDefines(LangOpts, Builder);
887 }
888 
889 /// InitializePreprocessor - Initialize the preprocessor getting it and the
890 /// environment ready to process a single file. This returns true on error.
891 ///
InitializePreprocessor(Preprocessor & PP,const PreprocessorOptions & InitOpts,const FrontendOptions & FEOpts)892 void clang::InitializePreprocessor(Preprocessor &PP,
893                                    const PreprocessorOptions &InitOpts,
894                                    const FrontendOptions &FEOpts) {
895   const LangOptions &LangOpts = PP.getLangOpts();
896   std::string PredefineBuffer;
897   PredefineBuffer.reserve(4080);
898   llvm::raw_string_ostream Predefines(PredefineBuffer);
899   MacroBuilder Builder(Predefines);
900 
901   // Emit line markers for various builtin sections of the file.  We don't do
902   // this in asm preprocessor mode, because "# 4" is not a line marker directive
903   // in this mode.
904   if (!PP.getLangOpts().AsmPreprocessor)
905     Builder.append("# 1 \"<built-in>\" 3");
906 
907   // Install things like __POWERPC__, __GNUC__, etc into the macro table.
908   if (InitOpts.UsePredefines) {
909     InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, Builder);
910 
911     // Install definitions to make Objective-C++ ARC work well with various
912     // C++ Standard Library implementations.
913     if (LangOpts.ObjC1 && LangOpts.CPlusPlus && LangOpts.ObjCAutoRefCount) {
914       switch (InitOpts.ObjCXXARCStandardLibrary) {
915       case ARCXX_nolib:
916         case ARCXX_libcxx:
917         break;
918 
919       case ARCXX_libstdcxx:
920         AddObjCXXARCLibstdcxxDefines(LangOpts, Builder);
921         break;
922       }
923     }
924   }
925 
926   // Even with predefines off, some macros are still predefined.
927   // These should all be defined in the preprocessor according to the
928   // current language configuration.
929   InitializeStandardPredefinedMacros(PP.getTargetInfo(), PP.getLangOpts(),
930                                      FEOpts, Builder);
931 
932   // Add on the predefines from the driver.  Wrap in a #line directive to report
933   // that they come from the command line.
934   if (!PP.getLangOpts().AsmPreprocessor)
935     Builder.append("# 1 \"<command line>\" 1");
936 
937   // Process #define's and #undef's in the order they are given.
938   for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
939     if (InitOpts.Macros[i].second)  // isUndef
940       Builder.undefineMacro(InitOpts.Macros[i].first);
941     else
942       DefineBuiltinMacro(Builder, InitOpts.Macros[i].first,
943                          PP.getDiagnostics());
944   }
945 
946   // If -imacros are specified, include them now.  These are processed before
947   // any -include directives.
948   for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
949     AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i]);
950 
951   // Process -include-pch/-include-pth directives.
952   if (!InitOpts.ImplicitPCHInclude.empty())
953     AddImplicitIncludePCH(Builder, PP, InitOpts.ImplicitPCHInclude);
954   if (!InitOpts.ImplicitPTHInclude.empty())
955     AddImplicitIncludePTH(Builder, PP, InitOpts.ImplicitPTHInclude);
956 
957   // Process -include directives.
958   for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
959     const std::string &Path = InitOpts.Includes[i];
960     AddImplicitInclude(Builder, Path);
961   }
962 
963   // Exit the command line and go back to <built-in> (2 is LC_LEAVE).
964   if (!PP.getLangOpts().AsmPreprocessor)
965     Builder.append("# 1 \"<built-in>\" 2");
966 
967   // Instruct the preprocessor to skip the preamble.
968   PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first,
969                              InitOpts.PrecompiledPreambleBytes.second);
970 
971   // Copy PredefinedBuffer into the Preprocessor.
972   PP.setPredefines(Predefines.str());
973 }
974