blob: 585599af971b491bcbe03b7e6b28384b0323321f [file] [log] [blame]
Chris Lattnere116ccf2009-04-21 05:40:52 +00001//===--- 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
Douglas Gregor4290fbd2010-04-30 02:51:06 +000014#include "clang/Basic/Version.h"
Daniel Dunbar8863b982009-11-07 04:20:15 +000015#include "clang/Frontend/Utils.h"
Chandler Carruth103b71c2010-01-20 06:13:02 +000016#include "clang/Basic/MacroBuilder.h"
Chris Lattnere116ccf2009-04-21 05:40:52 +000017#include "clang/Basic/TargetInfo.h"
Douglas Gregor2973c0e2009-12-02 16:32:41 +000018#include "clang/Frontend/FrontendDiagnostic.h"
Fariborz Jahanian7d957472010-01-13 18:51:17 +000019#include "clang/Frontend/FrontendOptions.h"
Daniel Dunbar8863b982009-11-07 04:20:15 +000020#include "clang/Frontend/PreprocessorOptions.h"
Chandler Carruthcb381ea2011-12-09 01:33:57 +000021#include "clang/Lex/HeaderSearch.h"
Chris Lattnere116ccf2009-04-21 05:40:52 +000022#include "clang/Lex/Preprocessor.h"
Douglas Gregor2973c0e2009-12-02 16:32:41 +000023#include "clang/Basic/FileManager.h"
24#include "clang/Basic/SourceManager.h"
Benjamin Kramera3d8ced2010-01-09 16:17:37 +000025#include "llvm/ADT/APFloat.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000026#include "llvm/Support/FileSystem.h"
Douglas Gregor2973c0e2009-12-02 16:32:41 +000027#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000028#include "llvm/Support/Path.h"
Chris Lattner47c06ee2009-11-02 21:48:09 +000029using namespace clang;
Chris Lattnere116ccf2009-04-21 05:40:52 +000030
31// Append a #define line to Buf for Macro. Macro should be of the form XXX,
32// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
33// "#define XXX Y z W". To get a #define with no value, use "XXX=".
Chris Lattner5f9e2722011-07-23 10:55:15 +000034static void DefineBuiltinMacro(MacroBuilder &Builder, StringRef Macro,
David Blaikied6471f72011-09-25 23:23:43 +000035 DiagnosticsEngine &Diags) {
Chris Lattner5f9e2722011-07-23 10:55:15 +000036 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
37 StringRef MacroName = MacroPair.first;
38 StringRef MacroBody = MacroPair.second;
Daniel Dunbar1ee4b9e2010-01-10 00:46:21 +000039 if (MacroName.size() != Macro.size()) {
Chris Lattnere116ccf2009-04-21 05:40:52 +000040 // Per GCC -D semantics, the macro ends at \n if it exists.
Chris Lattner5f9e2722011-07-23 10:55:15 +000041 StringRef::size_type End = MacroBody.find_first_of("\n\r");
42 if (End != StringRef::npos)
Daniel Dunbar1ee4b9e2010-01-10 00:46:21 +000043 Diags.Report(diag::warn_fe_macro_contains_embedded_newline)
Kovarththanan Rajaratnam8746e4e2010-01-09 09:27:11 +000044 << MacroName;
Benjamin Kramerb1b5b902010-01-09 17:43:21 +000045 Builder.defineMacro(MacroName, MacroBody.substr(0, End));
Chris Lattnere116ccf2009-04-21 05:40:52 +000046 } else {
47 // Push "macroname 1".
Benjamin Kramerb1b5b902010-01-09 17:43:21 +000048 Builder.defineMacro(Macro);
Chris Lattnere116ccf2009-04-21 05:40:52 +000049 }
Chris Lattnerdcdecf42009-05-15 16:08:43 +000050}
51
James Dennett809d1be2012-06-13 22:07:09 +000052/// AddImplicitInclude - Add an implicit \#include of the specified file to the
Chris Lattnere116ccf2009-04-21 05:40:52 +000053/// predefines buffer.
Chris Lattner5f9e2722011-07-23 10:55:15 +000054static void AddImplicitInclude(MacroBuilder &Builder, StringRef File,
Nick Lewycky277a6e72011-02-23 21:16:44 +000055 FileManager &FileMgr) {
Chandler Carruthcb381ea2011-12-09 01:33:57 +000056 Builder.append(Twine("#include \"") +
57 HeaderSearch::NormalizeDashIncludePath(File, FileMgr) + "\"");
Chris Lattnere116ccf2009-04-21 05:40:52 +000058}
59
Benjamin Kramera3d8ced2010-01-09 16:17:37 +000060static void AddImplicitIncludeMacros(MacroBuilder &Builder,
Chris Lattner5f9e2722011-07-23 10:55:15 +000061 StringRef File,
Nick Lewycky277a6e72011-02-23 21:16:44 +000062 FileManager &FileMgr) {
Chandler Carruthcb381ea2011-12-09 01:33:57 +000063 Builder.append(Twine("#__include_macros \"") +
64 HeaderSearch::NormalizeDashIncludePath(File, FileMgr) + "\"");
Chris Lattnere116ccf2009-04-21 05:40:52 +000065 // Marker token to stop the __include_macros fetch loop.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +000066 Builder.append("##"); // ##?
Chris Lattnere116ccf2009-04-21 05:40:52 +000067}
68
James Dennett809d1be2012-06-13 22:07:09 +000069/// AddImplicitIncludePTH - Add an implicit \#include using the original file
70/// used to generate a PTH cache.
Benjamin Kramera3d8ced2010-01-09 16:17:37 +000071static void AddImplicitIncludePTH(MacroBuilder &Builder, Preprocessor &PP,
Chris Lattner5f9e2722011-07-23 10:55:15 +000072 StringRef ImplicitIncludePTH) {
Chris Lattnere116ccf2009-04-21 05:40:52 +000073 PTHManager *P = PP.getPTHManager();
Ted Kremenek4ae4c912010-06-28 20:32:40 +000074 // Null check 'P' in the corner case where it couldn't be created.
75 const char *OriginalFile = P ? P->getOriginalSourceFile() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +000076
Chris Lattnere116ccf2009-04-21 05:40:52 +000077 if (!OriginalFile) {
Daniel Dunbarbaac1032009-12-03 09:14:12 +000078 PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header)
79 << ImplicitIncludePTH;
80 return;
Chris Lattnere116ccf2009-04-21 05:40:52 +000081 }
Mike Stump1eb44332009-09-09 15:08:12 +000082
Nick Lewycky277a6e72011-02-23 21:16:44 +000083 AddImplicitInclude(Builder, OriginalFile, PP.getFileManager());
Chris Lattnere116ccf2009-04-21 05:40:52 +000084}
85
86/// PickFP - This is used to pick a value based on the FP semantics of the
87/// specified FP model.
88template <typename T>
89static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal,
Eli Friedman2665a752009-05-23 03:50:01 +000090 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
91 T IEEEQuadVal) {
Duncan Sands63682f92009-06-03 14:28:20 +000092 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle)
Chris Lattnere116ccf2009-04-21 05:40:52 +000093 return IEEESingleVal;
Duncan Sands63682f92009-06-03 14:28:20 +000094 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble)
Chris Lattnere116ccf2009-04-21 05:40:52 +000095 return IEEEDoubleVal;
Duncan Sands63682f92009-06-03 14:28:20 +000096 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended)
Chris Lattnere116ccf2009-04-21 05:40:52 +000097 return X87DoubleExtendedVal;
Duncan Sands63682f92009-06-03 14:28:20 +000098 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble)
Eli Friedman2665a752009-05-23 03:50:01 +000099 return PPCDoubleDoubleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000100 assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad);
Eli Friedman2665a752009-05-23 03:50:01 +0000101 return IEEEQuadVal;
Chris Lattnere116ccf2009-04-21 05:40:52 +0000102}
103
Chris Lattner5f9e2722011-07-23 10:55:15 +0000104static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix,
Chris Lattnere116ccf2009-04-21 05:40:52 +0000105 const llvm::fltSemantics *Sem) {
106 const char *DenormMin, *Epsilon, *Max, *Min;
Mike Stump1eb44332009-09-09 15:08:12 +0000107 DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324",
Chris Lattnere116ccf2009-04-21 05:40:52 +0000108 "3.64519953188247460253e-4951L",
Eli Friedman2665a752009-05-23 03:50:01 +0000109 "4.94065645841246544176568792868221e-324L",
110 "6.47517511943802511092443895822764655e-4966L");
111 int Digits = PickFP(Sem, 6, 15, 18, 31, 33);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000112 Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16",
113 "1.08420217248550443401e-19L",
Eli Friedman2665a752009-05-23 03:50:01 +0000114 "4.94065645841246544176568792868221e-324L",
115 "1.92592994438723585305597794258492732e-34L");
Eli Friedman2665a752009-05-23 03:50:01 +0000116 int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113);
117 int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931);
118 int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932);
119 int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381);
120 int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000121 Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308",
122 "3.36210314311209350626e-4932L",
Eli Friedman2665a752009-05-23 03:50:01 +0000123 "2.00416836000897277799610805135016e-292L",
124 "3.36210314311209350626267781732175260e-4932L");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000125 Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308",
126 "1.18973149535723176502e+4932L",
Eli Friedman2665a752009-05-23 03:50:01 +0000127 "1.79769313486231580793728971405301e+308L",
128 "1.18973149535723176508575932662800702e+4932L");
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000130 SmallString<32> DefPrefix;
Daniel Dunbar066515f2010-01-20 06:09:53 +0000131 DefPrefix = "__";
132 DefPrefix += Prefix;
133 DefPrefix += "_";
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000134
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000135 Builder.defineMacro(DefPrefix + "DENORM_MIN__", DenormMin);
136 Builder.defineMacro(DefPrefix + "HAS_DENORM__");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000137 Builder.defineMacro(DefPrefix + "DIG__", Twine(Digits));
138 Builder.defineMacro(DefPrefix + "EPSILON__", Twine(Epsilon));
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000139 Builder.defineMacro(DefPrefix + "HAS_INFINITY__");
140 Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000141 Builder.defineMacro(DefPrefix + "MANT_DIG__", Twine(MantissaDigits));
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000142
Chris Lattner5f9e2722011-07-23 10:55:15 +0000143 Builder.defineMacro(DefPrefix + "MAX_10_EXP__", Twine(Max10Exp));
144 Builder.defineMacro(DefPrefix + "MAX_EXP__", Twine(MaxExp));
145 Builder.defineMacro(DefPrefix + "MAX__", Twine(Max));
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000146
Chris Lattner5f9e2722011-07-23 10:55:15 +0000147 Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+Twine(Min10Exp)+")");
148 Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+Twine(MinExp)+")");
149 Builder.defineMacro(DefPrefix + "MIN__", Twine(Min));
Chris Lattnere116ccf2009-04-21 05:40:52 +0000150}
151
152
153/// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
154/// named MacroName with the max value for a type with width 'TypeWidth' a
155/// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
Chris Lattner5f9e2722011-07-23 10:55:15 +0000156static void DefineTypeSize(StringRef MacroName, unsigned TypeWidth,
157 StringRef ValSuffix, bool isSigned,
Chris Lattnerd7f758c2011-02-24 06:54:56 +0000158 MacroBuilder &Builder) {
159 llvm::APInt MaxVal = isSigned ? llvm::APInt::getSignedMaxValue(TypeWidth)
160 : llvm::APInt::getMaxValue(TypeWidth);
161 Builder.defineMacro(MacroName, MaxVal.toString(10, isSigned) + ValSuffix);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000162}
163
Chris Lattner9099e7b2009-11-05 21:21:32 +0000164/// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
165/// the width, suffix, and signedness of the given type
Chris Lattner5f9e2722011-07-23 10:55:15 +0000166static void DefineTypeSize(StringRef MacroName, TargetInfo::IntType Ty,
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000167 const TargetInfo &TI, MacroBuilder &Builder) {
Chris Lattner9099e7b2009-11-05 21:21:32 +0000168 DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000169 TI.isTypeSigned(Ty), Builder);
Chris Lattner9099e7b2009-11-05 21:21:32 +0000170}
171
Chris Lattner5f9e2722011-07-23 10:55:15 +0000172static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty,
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000173 MacroBuilder &Builder) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000174 Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty));
Chris Lattnere116ccf2009-04-21 05:40:52 +0000175}
176
Chris Lattner5f9e2722011-07-23 10:55:15 +0000177static void DefineTypeWidth(StringRef MacroName, TargetInfo::IntType Ty,
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000178 const TargetInfo &TI, MacroBuilder &Builder) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000179 Builder.defineMacro(MacroName, Twine(TI.getTypeWidth(Ty)));
Ken Dyck186696b2009-11-18 13:52:57 +0000180}
181
Chris Lattner5f9e2722011-07-23 10:55:15 +0000182static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth,
Dan Gohmancfeac342010-05-28 00:27:15 +0000183 const TargetInfo &TI, MacroBuilder &Builder) {
184 Builder.defineMacro(MacroName,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000185 Twine(BitWidth / TI.getCharWidth()));
Dan Gohmancfeac342010-05-28 00:27:15 +0000186}
187
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000188static void DefineExactWidthIntType(TargetInfo::IntType Ty,
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000189 const TargetInfo &TI, MacroBuilder &Builder) {
Ken Dyckeef22ef2009-11-16 16:36:33 +0000190 int TypeWidth = TI.getTypeWidth(Ty);
Daniel Dunbar7f3a5452010-06-30 06:30:56 +0000191
192 // Use the target specified int64 type, when appropriate, so that [u]int64_t
193 // ends up being defined in terms of the correct type.
194 if (TypeWidth == 64)
195 Ty = TI.getInt64Type();
196
Chris Lattner5f9e2722011-07-23 10:55:15 +0000197 DefineType("__INT" + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
Ken Dyckeef22ef2009-11-16 16:36:33 +0000198
Chris Lattner5f9e2722011-07-23 10:55:15 +0000199 StringRef ConstSuffix(TargetInfo::getTypeConstantSuffix(Ty));
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000200 if (!ConstSuffix.empty())
Chris Lattner5f9e2722011-07-23 10:55:15 +0000201 Builder.defineMacro("__INT" + Twine(TypeWidth) + "_C_SUFFIX__",
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000202 ConstSuffix);
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000203}
Chris Lattnere116ccf2009-04-21 05:40:52 +0000204
Richard Smith2c39d712012-04-13 00:45:38 +0000205/// Get the value the ATOMIC_*_LOCK_FREE macro should have for a type with
206/// the specified properties.
207static const char *getLockFreeValue(unsigned TypeWidth, unsigned TypeAlign,
208 unsigned InlineWidth) {
209 // Fully-aligned, power-of-2 sizes no larger than the inline
210 // width will be inlined as lock-free operations.
211 if (TypeWidth == TypeAlign && (TypeWidth & (TypeWidth - 1)) == 0 &&
212 TypeWidth <= InlineWidth)
213 return "2"; // "always lock free"
214 // We cannot be certain what operations the lib calls might be
215 // able to implement as lock-free on future processors.
216 return "1"; // "sometimes lock free"
217}
218
John McCallf85e1932011-06-15 23:02:42 +0000219/// \brief Add definitions required for a smooth interaction between
John McCallf85e1932011-06-15 23:02:42 +0000220/// Objective-C++ automated reference counting and libstdc++ (4.2).
221static void AddObjCXXARCLibstdcxxDefines(const LangOptions &LangOpts,
222 MacroBuilder &Builder) {
223 Builder.defineMacro("_GLIBCXX_PREDEFINED_OBJC_ARC_IS_SCALAR");
224
225 std::string Result;
226 {
227 // Provide specializations for the __is_scalar type trait so that
228 // lifetime-qualified objects are not considered "scalar" types, which
229 // libstdc++ uses as an indicator of the presence of trivial copy, assign,
230 // default-construct, and destruct semantics (none of which hold for
231 // lifetime-qualified objects in ARC).
232 llvm::raw_string_ostream Out(Result);
233
234 Out << "namespace std {\n"
235 << "\n"
236 << "struct __true_type;\n"
237 << "struct __false_type;\n"
238 << "\n";
239
240 Out << "template<typename _Tp> struct __is_scalar;\n"
241 << "\n";
242
243 Out << "template<typename _Tp>\n"
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000244 << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> {\n"
John McCallf85e1932011-06-15 23:02:42 +0000245 << " enum { __value = 0 };\n"
246 << " typedef __false_type __type;\n"
247 << "};\n"
248 << "\n";
249
John McCall0a7dd782012-08-21 02:47:43 +0000250 if (LangOpts.ObjCARCWeak) {
John McCallf85e1932011-06-15 23:02:42 +0000251 Out << "template<typename _Tp>\n"
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000252 << "struct __is_scalar<__attribute__((objc_ownership(weak))) _Tp> {\n"
John McCallf85e1932011-06-15 23:02:42 +0000253 << " enum { __value = 0 };\n"
254 << " typedef __false_type __type;\n"
255 << "};\n"
256 << "\n";
257 }
258
259 Out << "template<typename _Tp>\n"
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000260 << "struct __is_scalar<__attribute__((objc_ownership(autoreleasing)))"
John McCallf85e1932011-06-15 23:02:42 +0000261 << " _Tp> {\n"
262 << " enum { __value = 0 };\n"
263 << " typedef __false_type __type;\n"
264 << "};\n"
265 << "\n";
266
267 Out << "}\n";
268 }
269 Builder.append(Result);
270}
271
Nick Lewyckya9c64412011-06-07 06:07:12 +0000272static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
273 const LangOptions &LangOpts,
274 const FrontendOptions &FEOpts,
275 MacroBuilder &Builder) {
Eli Friedman85037d92011-10-19 23:46:05 +0000276 if (!LangOpts.MicrosoftMode && !LangOpts.TraditionalCPP)
Nick Lewyckya9c64412011-06-07 06:07:12 +0000277 Builder.defineMacro("__STDC__");
278 if (LangOpts.Freestanding)
279 Builder.defineMacro("__STDC_HOSTED__", "0");
280 else
281 Builder.defineMacro("__STDC_HOSTED__");
282
283 if (!LangOpts.CPlusPlus) {
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000284 if (LangOpts.C11)
Benjamin Kramer430cf512011-12-23 17:00:41 +0000285 Builder.defineMacro("__STDC_VERSION__", "201112L");
Douglas Gregor5aa6dea2011-10-28 23:02:54 +0000286 else if (LangOpts.C99)
Nick Lewyckya9c64412011-06-07 06:07:12 +0000287 Builder.defineMacro("__STDC_VERSION__", "199901L");
288 else if (!LangOpts.GNUMode && LangOpts.Digraphs)
289 Builder.defineMacro("__STDC_VERSION__", "199409L");
290 } else {
Richard Smith05134482012-05-03 22:18:20 +0000291 // C++11 [cpp.predefined]p1:
292 // The name __cplusplus is defined to the value 201103L when compiling a
293 // C++ translation unit.
294 if (LangOpts.CPlusPlus0x)
295 Builder.defineMacro("__cplusplus", "201103L");
296 // C++03 [cpp.predefined]p1:
297 // The name __cplusplus is defined to the value 199711L when compiling a
298 // C++ translation unit.
299 else
300 Builder.defineMacro("__cplusplus", "199711L");
Nick Lewyckya9c64412011-06-07 06:07:12 +0000301 }
302
Nick Lewycky14648092011-06-10 20:56:43 +0000303 if (LangOpts.ObjC1)
304 Builder.defineMacro("__OBJC__");
305
Nick Lewyckya9c64412011-06-07 06:07:12 +0000306 // Not "standard" per se, but available even with the -undef flag.
307 if (LangOpts.AsmPreprocessor)
308 Builder.defineMacro("__ASSEMBLER__");
309}
310
Chris Lattnere116ccf2009-04-21 05:40:52 +0000311static void InitializePredefinedMacros(const TargetInfo &TI,
312 const LangOptions &LangOpts,
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000313 const FrontendOptions &FEOpts,
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000314 MacroBuilder &Builder) {
Chris Lattnere116ccf2009-04-21 05:40:52 +0000315 // Compiler version introspection macros.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000316 Builder.defineMacro("__llvm__"); // LLVM Backend
317 Builder.defineMacro("__clang__"); // Clang Frontend
Douglas Gregor4290fbd2010-04-30 02:51:06 +0000318#define TOSTR2(X) #X
319#define TOSTR(X) TOSTR2(X)
320 Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR));
321 Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR));
322#ifdef CLANG_VERSION_PATCHLEVEL
323 Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL));
324#else
325 Builder.defineMacro("__clang_patchlevel__", "0");
326#endif
327 Builder.defineMacro("__clang_version__",
Benjamin Kramerd7ee1942012-10-08 18:49:39 +0000328 "\"" CLANG_VERSION_STRING " "
329 + getClangFullRepositoryVersion() + "\"");
Douglas Gregor4290fbd2010-04-30 02:51:06 +0000330#undef TOSTR
331#undef TOSTR2
Aaron Ballman5b31d552012-03-10 22:21:14 +0000332 if (!LangOpts.MicrosoftMode) {
333 // Currently claim to be compatible with GCC 4.2.1-5621, but only if we're
334 // not compiling for MSVC compatibility
335 Builder.defineMacro("__GNUC_MINOR__", "2");
336 Builder.defineMacro("__GNUC_PATCHLEVEL__", "1");
337 Builder.defineMacro("__GNUC__", "4");
338 Builder.defineMacro("__GXX_ABI_VERSION", "1002");
339 }
Daniel Dunbarad1a4c62011-03-31 00:53:51 +0000340
David Chisnall7a7ee302012-01-16 17:27:18 +0000341 // Define macros for the C11 / C++11 memory orderings
342 Builder.defineMacro("__ATOMIC_RELAXED", "0");
343 Builder.defineMacro("__ATOMIC_CONSUME", "1");
344 Builder.defineMacro("__ATOMIC_ACQUIRE", "2");
345 Builder.defineMacro("__ATOMIC_RELEASE", "3");
346 Builder.defineMacro("__ATOMIC_ACQ_REL", "4");
347 Builder.defineMacro("__ATOMIC_SEQ_CST", "5");
348
David Chisnall5f3c1632012-02-18 16:12:34 +0000349 // Support for #pragma redefine_extname (Sun compatibility)
350 Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1");
351
Daniel Dunbarad1a4c62011-03-31 00:53:51 +0000352 // As sad as it is, enough software depends on the __VERSION__ for version
353 // checks that it is necessary to report 4.2.1 (the base GCC version we claim
354 // compatibility with) first.
355 Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible " +
Chris Lattner5f9e2722011-07-23 10:55:15 +0000356 Twine(getClangFullCPPVersion()) + "\"");
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Chris Lattnere116ccf2009-04-21 05:40:52 +0000358 // Initialize language-specific preprocessor defines.
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Chris Lattnere116ccf2009-04-21 05:40:52 +0000360 // Standard conforming mode?
361 if (!LangOpts.GNUMode)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000362 Builder.defineMacro("__STRICT_ANSI__");
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Chris Lattnere116ccf2009-04-21 05:40:52 +0000364 if (LangOpts.CPlusPlus0x)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000365 Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000366
Chris Lattnere116ccf2009-04-21 05:40:52 +0000367 if (LangOpts.ObjC1) {
John McCall260611a2012-06-20 06:18:46 +0000368 if (LangOpts.ObjCRuntime.isNonFragile()) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000369 Builder.defineMacro("__OBJC2__");
Douglas Gregoreced60c2011-09-12 15:17:19 +0000370
371 if (LangOpts.ObjCExceptions)
372 Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000373 }
374
Douglas Gregore289d812011-09-13 17:21:33 +0000375 if (LangOpts.getGC() != LangOptions::NonGC)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000376 Builder.defineMacro("__OBJC_GC__");
Mike Stump1eb44332009-09-09 15:08:12 +0000377
John McCall260611a2012-06-20 06:18:46 +0000378 if (LangOpts.ObjCRuntime.isNeXTFamily())
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000379 Builder.defineMacro("__NEXT_RUNTIME__");
Ted Kremenek3065cf92012-06-19 00:37:39 +0000380
381 Builder.defineMacro("IBOutlet", "__attribute__((iboutlet))");
382 Builder.defineMacro("IBOutletCollection(ClassName)",
383 "__attribute__((iboutletcollection(ClassName)))");
384 Builder.defineMacro("IBAction", "void)__attribute__((ibaction)");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000385 }
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Chris Lattnere116ccf2009-04-21 05:40:52 +0000387 // darwin_constant_cfstrings controls this. This is also dependent
388 // on other things like the runtime I believe. This is set even for C code.
Fariborz Jahanian16c3eae2011-07-05 16:00:59 +0000389 if (!LangOpts.NoConstantCFStrings)
390 Builder.defineMacro("__CONSTANT_CFSTRINGS__");
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Chris Lattnere116ccf2009-04-21 05:40:52 +0000392 if (LangOpts.ObjC2)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000393 Builder.defineMacro("OBJC_NEW_PROPERTIES");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000394
395 if (LangOpts.PascalStrings)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000396 Builder.defineMacro("__PASCAL_STRINGS__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000397
398 if (LangOpts.Blocks) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000399 Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))");
400 Builder.defineMacro("__BLOCKS__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000401 }
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Douglas Gregoreced60c2011-09-12 15:17:19 +0000403 if (LangOpts.CXXExceptions)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000404 Builder.defineMacro("__EXCEPTIONS");
Dan Gohmancfeac342010-05-28 00:27:15 +0000405 if (LangOpts.RTTI)
406 Builder.defineMacro("__GXX_RTTI");
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000407 if (LangOpts.SjLjExceptions)
408 Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__");
Rafael Espindolaf759df02009-10-01 13:33:33 +0000409
Chandler Carruthf8c247d2011-04-23 19:48:40 +0000410 if (LangOpts.Deprecated)
411 Builder.defineMacro("__DEPRECATED");
412
Chris Lattnere116ccf2009-04-21 05:40:52 +0000413 if (LangOpts.CPlusPlus) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000414 Builder.defineMacro("__GNUG__", "4");
415 Builder.defineMacro("__GXX_WEAK__");
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000416 Builder.defineMacro("__private_extern__", "extern");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000417 }
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Francois Pichet62ec1f22011-09-17 17:15:52 +0000419 if (LangOpts.MicrosoftExt) {
Steve Narofffdd6aaf2009-12-04 21:29:41 +0000420 // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however
421 // VC++ appears to only like __FUNCTION__.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000422 Builder.defineMacro("__PRETTY_FUNCTION__", "__FUNCTION__");
Abramo Bagnara5b86ffd2012-09-05 17:30:57 +0000423 // Work around some issues with Visual C++ headers.
424 if (LangOpts.WChar) {
425 // wchar_t supported as a keyword.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000426 Builder.defineMacro("_WCHAR_T_DEFINED");
427 Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED");
Abramo Bagnara5b86ffd2012-09-05 17:30:57 +0000428 }
429 if (LangOpts.CPlusPlus) {
Francois Pichet4b7e0482011-05-09 22:32:46 +0000430 // FIXME: Support Microsoft's __identifier extension in the lexer.
Francois Picheta23ae3f2011-05-07 17:47:38 +0000431 Builder.append("#define __identifier(x) x");
Douglas Gregor5c0ca522010-08-30 14:44:26 +0000432 Builder.append("class type_info;");
John Thompson24ee8042009-10-16 01:12:00 +0000433 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000434 }
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Chris Lattnere116ccf2009-04-21 05:40:52 +0000436 if (LangOpts.Optimize)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000437 Builder.defineMacro("__OPTIMIZE__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000438 if (LangOpts.OptimizeSize)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000439 Builder.defineMacro("__OPTIMIZE_SIZE__");
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Chandler Carruthba9186c2012-01-03 02:46:46 +0000441 if (LangOpts.FastMath)
442 Builder.defineMacro("__FAST_MATH__");
443
Chris Lattnere116ccf2009-04-21 05:40:52 +0000444 // Initialize target-specific preprocessor defines.
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Dylan Noblesmith5aeda882012-08-08 16:09:12 +0000446 // __BYTE_ORDER__ was added in GCC 4.6. It's analogous
447 // to the macro __BYTE_ORDER (no trailing underscores)
448 // from glibc's <endian.h> header.
Dylan Noblesmith3b198a92012-07-27 18:34:31 +0000449 // We don't support the PDP-11 as a target, but include
450 // the define so it can still be compared against.
451 Builder.defineMacro("__ORDER_LITTLE_ENDIAN__", "1234");
452 Builder.defineMacro("__ORDER_BIG_ENDIAN__", "4321");
453 Builder.defineMacro("__ORDER_PDP_ENDIAN__", "3412");
454 if (TI.isBigEndian())
455 Builder.defineMacro("__BYTE_ORDER__", "__ORDER_BIG_ENDIAN__");
456 else
457 Builder.defineMacro("__BYTE_ORDER__", "__ORDER_LITTLE_ENDIAN__");
458
Dylan Noblesmith6bd32912012-08-10 19:12:37 +0000459
460 if (TI.getPointerWidth(0) == 64 && TI.getLongWidth() == 64
461 && TI.getIntWidth() == 32) {
462 Builder.defineMacro("_LP64");
463 Builder.defineMacro("__LP64__");
464 }
465
Chris Lattnere116ccf2009-04-21 05:40:52 +0000466 // Define type sizing macros based on the target properties.
467 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000468 Builder.defineMacro("__CHAR_BIT__", "8");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000469
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000470 DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Builder);
471 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder);
472 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder);
473 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder);
474 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder);
475 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder);
476 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000477
Dan Gohmancfeac342010-05-28 00:27:15 +0000478 DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder);
479 DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder);
480 DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder);
481 DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder);
482 DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder);
483 DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder);
484 DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder);
485 DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder);
486 DefineTypeSizeof("__SIZEOF_PTRDIFF_T__",
487 TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder);
488 DefineTypeSizeof("__SIZEOF_SIZE_T__",
489 TI.getTypeWidth(TI.getSizeType()), TI, Builder);
490 DefineTypeSizeof("__SIZEOF_WCHAR_T__",
491 TI.getTypeWidth(TI.getWCharType()), TI, Builder);
492 DefineTypeSizeof("__SIZEOF_WINT_T__",
493 TI.getTypeWidth(TI.getWIntType()), TI, Builder);
494
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000495 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder);
496 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder);
497 DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder);
498 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder);
499 DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder);
500 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder);
501 DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder);
502 DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder);
503 DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder);
504 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder);
505 DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder);
506 DefineType("__WINT_TYPE__", TI.getWIntType(), Builder);
507 DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder);
508 DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder);
Dan Gohmancfeac342010-05-28 00:27:15 +0000509 DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder);
510 DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000512 DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat());
513 DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat());
514 DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000515
516 // Define a __POINTER_WIDTH__ macro for stdint.h.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000517 Builder.defineMacro("__POINTER_WIDTH__",
Chris Lattner5f9e2722011-07-23 10:55:15 +0000518 Twine((int)TI.getPointerWidth(0)));
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Eli Friedman15b91762009-06-05 07:05:05 +0000520 if (!LangOpts.CharIsSigned)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000521 Builder.defineMacro("__CHAR_UNSIGNED__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000522
James Molloya30d8602012-05-04 11:23:40 +0000523 if (!TargetInfo::isTypeSigned(TI.getWCharType()))
524 Builder.defineMacro("__WCHAR_UNSIGNED__");
525
Eli Friedman1cfeefd2011-04-21 05:45:45 +0000526 if (!TargetInfo::isTypeSigned(TI.getWIntType()))
527 Builder.defineMacro("__WINT_UNSIGNED__");
528
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000529 // Define exact-width integer types for stdint.h
Chris Lattner5f9e2722011-07-23 10:55:15 +0000530 Builder.defineMacro("__INT" + Twine(TI.getCharWidth()) + "_TYPE__",
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000531 "char");
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000533 if (TI.getShortWidth() > TI.getCharWidth())
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000534 DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000536 if (TI.getIntWidth() > TI.getShortWidth())
537 DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder);
538
539 if (TI.getLongWidth() > TI.getIntWidth())
540 DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder);
541
542 if (TI.getLongLongWidth() > TI.getLongWidth())
543 DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder);
544
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000545 if (const char *Prefix = TI.getUserLabelPrefix())
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000546 Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix);
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Bob Wilson455e72e2012-07-19 03:52:53 +0000548 if (LangOpts.FastMath || LangOpts.FiniteMathOnly)
549 Builder.defineMacro("__FINITE_MATH_ONLY__", "1");
550 else
551 Builder.defineMacro("__FINITE_MATH_ONLY__", "0");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000552
Chris Lattnere116ccf2009-04-21 05:40:52 +0000553 if (LangOpts.GNUInline)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000554 Builder.defineMacro("__GNUC_GNU_INLINE__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000555 else
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000556 Builder.defineMacro("__GNUC_STDC_INLINE__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000557
Richard Smith2c39d712012-04-13 00:45:38 +0000558 // The value written by __atomic_test_and_set.
559 // FIXME: This is target-dependent.
560 Builder.defineMacro("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1");
561
562 // Used by libstdc++ to implement ATOMIC_<foo>_LOCK_FREE.
563 unsigned InlineWidthBits = TI.getMaxAtomicInlineWidth();
564#define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \
565 Builder.defineMacro("__GCC_ATOMIC_" #TYPE "_LOCK_FREE", \
566 getLockFreeValue(TI.get##Type##Width(), \
567 TI.get##Type##Align(), \
568 InlineWidthBits));
569 DEFINE_LOCK_FREE_MACRO(BOOL, Bool);
570 DEFINE_LOCK_FREE_MACRO(CHAR, Char);
571 DEFINE_LOCK_FREE_MACRO(CHAR16_T, Char16);
572 DEFINE_LOCK_FREE_MACRO(CHAR32_T, Char32);
573 DEFINE_LOCK_FREE_MACRO(WCHAR_T, WChar);
574 DEFINE_LOCK_FREE_MACRO(SHORT, Short);
575 DEFINE_LOCK_FREE_MACRO(INT, Int);
576 DEFINE_LOCK_FREE_MACRO(LONG, Long);
577 DEFINE_LOCK_FREE_MACRO(LLONG, LongLong);
578 Builder.defineMacro("__GCC_ATOMIC_POINTER_LOCK_FREE",
579 getLockFreeValue(TI.getPointerWidth(0),
580 TI.getPointerAlign(0),
581 InlineWidthBits));
582#undef DEFINE_LOCK_FREE_MACRO
583
Daniel Dunbar7b140262012-03-09 15:39:08 +0000584 if (LangOpts.NoInlineDefine)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000585 Builder.defineMacro("__NO_INLINE__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000586
587 if (unsigned PICLevel = LangOpts.PICLevel) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000588 Builder.defineMacro("__PIC__", Twine(PICLevel));
589 Builder.defineMacro("__pic__", Twine(PICLevel));
Chris Lattnere116ccf2009-04-21 05:40:52 +0000590 }
Chandler Carruth5e219cf2012-04-08 16:40:35 +0000591 if (unsigned PIELevel = LangOpts.PIELevel) {
592 Builder.defineMacro("__PIE__", Twine(PIELevel));
593 Builder.defineMacro("__pie__", Twine(PIELevel));
594 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000595
596 // Macros to control C99 numerics and <float.h>
Benjamin Kramerb4066692011-12-28 15:47:06 +0000597 Builder.defineMacro("__FLT_EVAL_METHOD__", Twine(TI.getFloatEvalMethod()));
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000598 Builder.defineMacro("__FLT_RADIX__", "2");
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000599 int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000600 Builder.defineMacro("__DECIMAL_DIG__", Twine(Dig));
Bill Wendling45483f72009-06-28 07:36:13 +0000601
Douglas Gregore289d812011-09-13 17:21:33 +0000602 if (LangOpts.getStackProtector() == LangOptions::SSPOn)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000603 Builder.defineMacro("__SSP__");
Douglas Gregore289d812011-09-13 17:21:33 +0000604 else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000605 Builder.defineMacro("__SSP_ALL__", "2");
Bill Wendling45483f72009-06-28 07:36:13 +0000606
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000607 if (FEOpts.ProgramAction == frontend::RewriteObjC)
608 Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
Ted Kremenekf0af7772010-05-26 21:36:54 +0000609
610 // Define a macro that exists only when using the static analyzer.
611 if (FEOpts.ProgramAction == frontend::RunAnalysis)
612 Builder.defineMacro("__clang_analyzer__");
613
Peter Collingbournef0840822010-12-04 01:51:23 +0000614 if (LangOpts.FastRelaxedMath)
615 Builder.defineMacro("__FAST_RELAXED_MATH__");
616
John McCall098df7f2011-06-16 00:03:19 +0000617 if (LangOpts.ObjCAutoRefCount) {
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000618 Builder.defineMacro("__weak", "__attribute__((objc_ownership(weak)))");
619 Builder.defineMacro("__strong", "__attribute__((objc_ownership(strong)))");
John McCall098df7f2011-06-16 00:03:19 +0000620 Builder.defineMacro("__autoreleasing",
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000621 "__attribute__((objc_ownership(autoreleasing)))");
John McCall098df7f2011-06-16 00:03:19 +0000622 Builder.defineMacro("__unsafe_unretained",
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000623 "__attribute__((objc_ownership(none)))");
John McCall098df7f2011-06-16 00:03:19 +0000624 }
625
Chris Lattnere116ccf2009-04-21 05:40:52 +0000626 // Get other target #defines.
Benjamin Kramera9992772010-01-09 17:55:51 +0000627 TI.getTargetDefines(LangOpts, Builder);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000628}
629
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000630// Initialize the remapping of files to alternative contents, e.g.,
631// those specified through other files.
David Blaikied6471f72011-09-25 23:23:43 +0000632static void InitializeFileRemapping(DiagnosticsEngine &Diags,
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000633 SourceManager &SourceMgr,
634 FileManager &FileMgr,
635 const PreprocessorOptions &InitOpts) {
Douglas Gregor4db64a42010-01-23 00:14:00 +0000636 // Remap files in the source manager (with buffers).
Douglas Gregor44c181a2010-07-23 00:33:23 +0000637 for (PreprocessorOptions::const_remapped_file_buffer_iterator
Douglas Gregor4db64a42010-01-23 00:14:00 +0000638 Remap = InitOpts.remapped_file_buffer_begin(),
639 RemapEnd = InitOpts.remapped_file_buffer_end();
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000640 Remap != RemapEnd;
641 ++Remap) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000642 // Create the file entry for the file that we're mapping from.
643 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000644 Remap->second->getBufferSize(),
Chris Lattner39b49bc2010-11-23 08:35:12 +0000645 0);
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000646 if (!FromFile) {
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000647 Diags.Report(diag::err_fe_remap_missing_from_file)
648 << Remap->first;
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000649 if (!InitOpts.RetainRemappedFileBuffers)
650 delete Remap->second;
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000651 continue;
652 }
653
Douglas Gregor4db64a42010-01-23 00:14:00 +0000654 // Override the contents of the "from" file with the contents of
655 // the "to" file.
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000656 SourceMgr.overrideFileContents(FromFile, Remap->second,
657 InitOpts.RetainRemappedFileBuffers);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000658 }
659
660 // Remap files in the source manager (with other files).
Douglas Gregor44c181a2010-07-23 00:33:23 +0000661 for (PreprocessorOptions::const_remapped_file_iterator
662 Remap = InitOpts.remapped_file_begin(),
663 RemapEnd = InitOpts.remapped_file_end();
Douglas Gregor4db64a42010-01-23 00:14:00 +0000664 Remap != RemapEnd;
665 ++Remap) {
666 // Find the file that we're mapping to.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000667 const FileEntry *ToFile = FileMgr.getFile(Remap->second);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000668 if (!ToFile) {
669 Diags.Report(diag::err_fe_remap_missing_to_file)
670 << Remap->first << Remap->second;
671 continue;
672 }
673
674 // Create the file entry for the file that we're mapping from.
675 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000676 ToFile->getSize(), 0);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000677 if (!FromFile) {
678 Diags.Report(diag::err_fe_remap_missing_from_file)
679 << Remap->first;
680 continue;
681 }
682
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000683 // Override the contents of the "from" file with the contents of
684 // the "to" file.
Argyrios Kyrtzidis299a4a92011-03-08 23:35:24 +0000685 SourceMgr.overrideFileContents(FromFile, ToFile);
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000686 }
Argyrios Kyrtzidis299a4a92011-03-08 23:35:24 +0000687
688 SourceMgr.setOverridenFilesKeepOriginalName(
689 InitOpts.RemappedFilesKeepOriginalName);
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000690}
691
Chris Lattnere116ccf2009-04-21 05:40:52 +0000692/// InitializePreprocessor - Initialize the preprocessor getting it and the
693/// environment ready to process a single file. This returns true on error.
694///
Daniel Dunbar938963f2009-11-04 21:13:15 +0000695void clang::InitializePreprocessor(Preprocessor &PP,
Daniel Dunbar961c76e2009-11-11 21:44:42 +0000696 const PreprocessorOptions &InitOpts,
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000697 const HeaderSearchOptions &HSOpts,
698 const FrontendOptions &FEOpts) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000699 const LangOptions &LangOpts = PP.getLangOpts();
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000700 std::string PredefineBuffer;
701 PredefineBuffer.reserve(4080);
702 llvm::raw_string_ostream Predefines(PredefineBuffer);
703 MacroBuilder Builder(Predefines);
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000705 InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(),
Chris Lattner39b49bc2010-11-23 08:35:12 +0000706 PP.getFileManager(), InitOpts);
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000707
Chris Lattnerfcaa9e72010-04-26 22:08:10 +0000708 // Emit line markers for various builtin sections of the file. We don't do
709 // this in asm preprocessor mode, because "# 4" is not a line marker directive
710 // in this mode.
David Blaikie4e4d0842012-03-11 07:00:24 +0000711 if (!PP.getLangOpts().AsmPreprocessor)
Chris Lattnerfcaa9e72010-04-26 22:08:10 +0000712 Builder.append("# 1 \"<built-in>\" 3");
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Chris Lattnere116ccf2009-04-21 05:40:52 +0000714 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
John McCallf85e1932011-06-15 23:02:42 +0000715 if (InitOpts.UsePredefines) {
716 InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000717
John McCallf85e1932011-06-15 23:02:42 +0000718 // Install definitions to make Objective-C++ ARC work well with various
719 // C++ Standard Library implementations.
720 if (LangOpts.ObjC1 && LangOpts.CPlusPlus && LangOpts.ObjCAutoRefCount) {
721 switch (InitOpts.ObjCXXARCStandardLibrary) {
722 case ARCXX_nolib:
Douglas Gregored73b102011-09-27 22:48:19 +0000723 case ARCXX_libcxx:
John McCallf85e1932011-06-15 23:02:42 +0000724 break;
725
726 case ARCXX_libstdcxx:
727 AddObjCXXARCLibstdcxxDefines(LangOpts, Builder);
728 break;
729 }
730 }
731 }
732
Nick Lewyckya9c64412011-06-07 06:07:12 +0000733 // Even with predefines off, some macros are still predefined.
734 // These should all be defined in the preprocessor according to the
735 // current language configuration.
David Blaikie4e4d0842012-03-11 07:00:24 +0000736 InitializeStandardPredefinedMacros(PP.getTargetInfo(), PP.getLangOpts(),
Nick Lewyckya9c64412011-06-07 06:07:12 +0000737 FEOpts, Builder);
738
Chris Lattnere116ccf2009-04-21 05:40:52 +0000739 // Add on the predefines from the driver. Wrap in a #line directive to report
740 // that they come from the command line.
David Blaikie4e4d0842012-03-11 07:00:24 +0000741 if (!PP.getLangOpts().AsmPreprocessor)
Chris Lattnerfcaa9e72010-04-26 22:08:10 +0000742 Builder.append("# 1 \"<command line>\" 1");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000743
744 // Process #define's and #undef's in the order they are given.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000745 for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
746 if (InitOpts.Macros[i].second) // isUndef
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000747 Builder.undefineMacro(InitOpts.Macros[i].first);
Chris Lattner62f86c42009-04-21 06:00:24 +0000748 else
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000749 DefineBuiltinMacro(Builder, InitOpts.Macros[i].first,
Daniel Dunbar1ee4b9e2010-01-10 00:46:21 +0000750 PP.getDiagnostics());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000751 }
752
753 // If -imacros are specified, include them now. These are processed before
754 // any -include directives.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000755 for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
Nick Lewycky277a6e72011-02-23 21:16:44 +0000756 AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i],
757 PP.getFileManager());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000758
759 // Process -include directives.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000760 for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
761 const std::string &Path = InitOpts.Includes[i];
762 if (Path == InitOpts.ImplicitPTHInclude)
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000763 AddImplicitIncludePTH(Builder, PP, Path);
Chris Lattner62f86c42009-04-21 06:00:24 +0000764 else
Nick Lewycky277a6e72011-02-23 21:16:44 +0000765 AddImplicitInclude(Builder, Path, PP.getFileManager());
Eli Friedmanae96a962009-06-15 09:57:52 +0000766 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000767
Rafael Espindola0259d202009-12-01 18:28:16 +0000768 // Exit the command line and go back to <built-in> (2 is LC_LEAVE).
David Blaikie4e4d0842012-03-11 07:00:24 +0000769 if (!PP.getLangOpts().AsmPreprocessor)
Chris Lattnerfcaa9e72010-04-26 22:08:10 +0000770 Builder.append("# 1 \"<built-in>\" 2");
Rafael Espindola0259d202009-12-01 18:28:16 +0000771
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000772 // Instruct the preprocessor to skip the preamble.
773 PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first,
774 InitOpts.PrecompiledPreambleBytes.second);
775
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000776 // Copy PredefinedBuffer into the Preprocessor.
777 PP.setPredefines(Predefines.str());
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000778
Daniel Dunbar961c76e2009-11-11 21:44:42 +0000779 // Initialize the header search object.
780 ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts,
David Blaikie4e4d0842012-03-11 07:00:24 +0000781 PP.getLangOpts(),
Daniel Dunbar961c76e2009-11-11 21:44:42 +0000782 PP.getTargetInfo().getTriple());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000783}