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