blob: d0a87bd888ed63c5fef991e2093b452b2b9a7782 [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"
Chris Lattnere116ccf2009-04-21 05:40:52 +000021#include "clang/Lex/Preprocessor.h"
Douglas Gregor2973c0e2009-12-02 16:32:41 +000022#include "clang/Basic/FileManager.h"
23#include "clang/Basic/SourceManager.h"
Benjamin Kramera3d8ced2010-01-09 16:17:37 +000024#include "llvm/ADT/APFloat.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000025#include "llvm/Support/FileSystem.h"
Douglas Gregor2973c0e2009-12-02 16:32:41 +000026#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000027#include "llvm/Support/Path.h"
Chris Lattner47c06ee2009-11-02 21:48:09 +000028using namespace clang;
Chris Lattnere116ccf2009-04-21 05:40:52 +000029
30// Append a #define line to Buf for Macro. Macro should be of the form XXX,
31// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
32// "#define XXX Y z W". To get a #define with no value, use "XXX=".
Chris Lattner5f9e2722011-07-23 10:55:15 +000033static void DefineBuiltinMacro(MacroBuilder &Builder, StringRef Macro,
David Blaikied6471f72011-09-25 23:23:43 +000034 DiagnosticsEngine &Diags) {
Chris Lattner5f9e2722011-07-23 10:55:15 +000035 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
36 StringRef MacroName = MacroPair.first;
37 StringRef MacroBody = MacroPair.second;
Daniel Dunbar1ee4b9e2010-01-10 00:46:21 +000038 if (MacroName.size() != Macro.size()) {
Chris Lattnere116ccf2009-04-21 05:40:52 +000039 // Per GCC -D semantics, the macro ends at \n if it exists.
Chris Lattner5f9e2722011-07-23 10:55:15 +000040 StringRef::size_type End = MacroBody.find_first_of("\n\r");
41 if (End != StringRef::npos)
Daniel Dunbar1ee4b9e2010-01-10 00:46:21 +000042 Diags.Report(diag::warn_fe_macro_contains_embedded_newline)
Kovarththanan Rajaratnam8746e4e2010-01-09 09:27:11 +000043 << MacroName;
Benjamin Kramerb1b5b902010-01-09 17:43:21 +000044 Builder.defineMacro(MacroName, MacroBody.substr(0, End));
Chris Lattnere116ccf2009-04-21 05:40:52 +000045 } else {
46 // Push "macroname 1".
Benjamin Kramerb1b5b902010-01-09 17:43:21 +000047 Builder.defineMacro(Macro);
Chris Lattnere116ccf2009-04-21 05:40:52 +000048 }
Chris Lattnerdcdecf42009-05-15 16:08:43 +000049}
50
Chris Lattner5f9e2722011-07-23 10:55:15 +000051std::string clang::NormalizeDashIncludePath(StringRef File,
Nick Lewycky277a6e72011-02-23 21:16:44 +000052 FileManager &FileMgr) {
Daniel Dunbar1e7c6b62009-04-22 08:53:01 +000053 // Implicit include paths should be resolved relative to the current
54 // working directory first, and then use the regular header search
55 // mechanism. The proper way to handle this is to have the
56 // predefines buffer located at the current working directory, but
Nick Lewycky277a6e72011-02-23 21:16:44 +000057 // it has no file entry. For now, workaround this by using an
Daniel Dunbar1e7c6b62009-04-22 08:53:01 +000058 // absolute path if we find the file here, and otherwise letting
59 // header search handle it.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000060 llvm::SmallString<128> Path(File);
61 llvm::sys::fs::make_absolute(Path);
62 bool exists;
63 if (llvm::sys::fs::exists(Path.str(), exists) || !exists)
Daniel Dunbar1e7c6b62009-04-22 08:53:01 +000064 Path = File;
Nick Lewycky277a6e72011-02-23 21:16:44 +000065 else if (exists)
66 FileMgr.getFile(File);
Mike Stump1eb44332009-09-09 15:08:12 +000067
Daniel Dunbarc7162932009-11-11 23:58:53 +000068 return Lexer::Stringify(Path.str());
69}
70
Chris Lattnere116ccf2009-04-21 05:40:52 +000071/// AddImplicitInclude - Add an implicit #include of the specified file to the
72/// predefines buffer.
Chris Lattner5f9e2722011-07-23 10:55:15 +000073static void AddImplicitInclude(MacroBuilder &Builder, StringRef File,
Nick Lewycky277a6e72011-02-23 21:16:44 +000074 FileManager &FileMgr) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +000075 Builder.append("#include \"" +
Chris Lattner5f9e2722011-07-23 10:55:15 +000076 Twine(NormalizeDashIncludePath(File, FileMgr)) + "\"");
Chris Lattnere116ccf2009-04-21 05:40:52 +000077}
78
Benjamin Kramera3d8ced2010-01-09 16:17:37 +000079static void AddImplicitIncludeMacros(MacroBuilder &Builder,
Chris Lattner5f9e2722011-07-23 10:55:15 +000080 StringRef File,
Nick Lewycky277a6e72011-02-23 21:16:44 +000081 FileManager &FileMgr) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +000082 Builder.append("#__include_macros \"" +
Chris Lattner5f9e2722011-07-23 10:55:15 +000083 Twine(NormalizeDashIncludePath(File, FileMgr)) + "\"");
Chris Lattnere116ccf2009-04-21 05:40:52 +000084 // Marker token to stop the __include_macros fetch loop.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +000085 Builder.append("##"); // ##?
Chris Lattnere116ccf2009-04-21 05:40:52 +000086}
87
88/// AddImplicitIncludePTH - Add an implicit #include using the original file
89/// used to generate a PTH cache.
Benjamin Kramera3d8ced2010-01-09 16:17:37 +000090static void AddImplicitIncludePTH(MacroBuilder &Builder, Preprocessor &PP,
Chris Lattner5f9e2722011-07-23 10:55:15 +000091 StringRef ImplicitIncludePTH) {
Chris Lattnere116ccf2009-04-21 05:40:52 +000092 PTHManager *P = PP.getPTHManager();
Ted Kremenek4ae4c912010-06-28 20:32:40 +000093 // Null check 'P' in the corner case where it couldn't be created.
94 const char *OriginalFile = P ? P->getOriginalSourceFile() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +000095
Chris Lattnere116ccf2009-04-21 05:40:52 +000096 if (!OriginalFile) {
Daniel Dunbarbaac1032009-12-03 09:14:12 +000097 PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header)
98 << ImplicitIncludePTH;
99 return;
Chris Lattnere116ccf2009-04-21 05:40:52 +0000100 }
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Nick Lewycky277a6e72011-02-23 21:16:44 +0000102 AddImplicitInclude(Builder, OriginalFile, PP.getFileManager());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000103}
104
105/// PickFP - This is used to pick a value based on the FP semantics of the
106/// specified FP model.
107template <typename T>
108static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal,
Eli Friedman2665a752009-05-23 03:50:01 +0000109 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
110 T IEEEQuadVal) {
Duncan Sands63682f92009-06-03 14:28:20 +0000111 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000112 return IEEESingleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000113 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000114 return IEEEDoubleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000115 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000116 return X87DoubleExtendedVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000117 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble)
Eli Friedman2665a752009-05-23 03:50:01 +0000118 return PPCDoubleDoubleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000119 assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad);
Eli Friedman2665a752009-05-23 03:50:01 +0000120 return IEEEQuadVal;
Chris Lattnere116ccf2009-04-21 05:40:52 +0000121}
122
Chris Lattner5f9e2722011-07-23 10:55:15 +0000123static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix,
Chris Lattnere116ccf2009-04-21 05:40:52 +0000124 const llvm::fltSemantics *Sem) {
125 const char *DenormMin, *Epsilon, *Max, *Min;
Mike Stump1eb44332009-09-09 15:08:12 +0000126 DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324",
Chris Lattnere116ccf2009-04-21 05:40:52 +0000127 "3.64519953188247460253e-4951L",
Eli Friedman2665a752009-05-23 03:50:01 +0000128 "4.94065645841246544176568792868221e-324L",
129 "6.47517511943802511092443895822764655e-4966L");
130 int Digits = PickFP(Sem, 6, 15, 18, 31, 33);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000131 Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16",
132 "1.08420217248550443401e-19L",
Eli Friedman2665a752009-05-23 03:50:01 +0000133 "4.94065645841246544176568792868221e-324L",
134 "1.92592994438723585305597794258492732e-34L");
Eli Friedman2665a752009-05-23 03:50:01 +0000135 int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113);
136 int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931);
137 int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932);
138 int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381);
139 int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000140 Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308",
141 "3.36210314311209350626e-4932L",
Eli Friedman2665a752009-05-23 03:50:01 +0000142 "2.00416836000897277799610805135016e-292L",
143 "3.36210314311209350626267781732175260e-4932L");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000144 Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308",
145 "1.18973149535723176502e+4932L",
Eli Friedman2665a752009-05-23 03:50:01 +0000146 "1.79769313486231580793728971405301e+308L",
147 "1.18973149535723176508575932662800702e+4932L");
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Daniel Dunbar066515f2010-01-20 06:09:53 +0000149 llvm::SmallString<32> DefPrefix;
150 DefPrefix = "__";
151 DefPrefix += Prefix;
152 DefPrefix += "_";
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000153
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000154 Builder.defineMacro(DefPrefix + "DENORM_MIN__", DenormMin);
155 Builder.defineMacro(DefPrefix + "HAS_DENORM__");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000156 Builder.defineMacro(DefPrefix + "DIG__", Twine(Digits));
157 Builder.defineMacro(DefPrefix + "EPSILON__", Twine(Epsilon));
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000158 Builder.defineMacro(DefPrefix + "HAS_INFINITY__");
159 Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000160 Builder.defineMacro(DefPrefix + "MANT_DIG__", Twine(MantissaDigits));
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000161
Chris Lattner5f9e2722011-07-23 10:55:15 +0000162 Builder.defineMacro(DefPrefix + "MAX_10_EXP__", Twine(Max10Exp));
163 Builder.defineMacro(DefPrefix + "MAX_EXP__", Twine(MaxExp));
164 Builder.defineMacro(DefPrefix + "MAX__", Twine(Max));
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000165
Chris Lattner5f9e2722011-07-23 10:55:15 +0000166 Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+Twine(Min10Exp)+")");
167 Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+Twine(MinExp)+")");
168 Builder.defineMacro(DefPrefix + "MIN__", Twine(Min));
Chris Lattnere116ccf2009-04-21 05:40:52 +0000169}
170
171
172/// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
173/// named MacroName with the max value for a type with width 'TypeWidth' a
174/// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
Chris Lattner5f9e2722011-07-23 10:55:15 +0000175static void DefineTypeSize(StringRef MacroName, unsigned TypeWidth,
176 StringRef ValSuffix, bool isSigned,
Chris Lattnerd7f758c2011-02-24 06:54:56 +0000177 MacroBuilder &Builder) {
178 llvm::APInt MaxVal = isSigned ? llvm::APInt::getSignedMaxValue(TypeWidth)
179 : llvm::APInt::getMaxValue(TypeWidth);
180 Builder.defineMacro(MacroName, MaxVal.toString(10, isSigned) + ValSuffix);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000181}
182
Chris Lattner9099e7b2009-11-05 21:21:32 +0000183/// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
184/// the width, suffix, and signedness of the given type
Chris Lattner5f9e2722011-07-23 10:55:15 +0000185static void DefineTypeSize(StringRef MacroName, TargetInfo::IntType Ty,
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000186 const TargetInfo &TI, MacroBuilder &Builder) {
Chris Lattner9099e7b2009-11-05 21:21:32 +0000187 DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000188 TI.isTypeSigned(Ty), Builder);
Chris Lattner9099e7b2009-11-05 21:21:32 +0000189}
190
Chris Lattner5f9e2722011-07-23 10:55:15 +0000191static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty,
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000192 MacroBuilder &Builder) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000193 Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty));
Chris Lattnere116ccf2009-04-21 05:40:52 +0000194}
195
Chris Lattner5f9e2722011-07-23 10:55:15 +0000196static void DefineTypeWidth(StringRef MacroName, TargetInfo::IntType Ty,
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000197 const TargetInfo &TI, MacroBuilder &Builder) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000198 Builder.defineMacro(MacroName, Twine(TI.getTypeWidth(Ty)));
Ken Dyck186696b2009-11-18 13:52:57 +0000199}
200
Chris Lattner5f9e2722011-07-23 10:55:15 +0000201static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth,
Dan Gohmancfeac342010-05-28 00:27:15 +0000202 const TargetInfo &TI, MacroBuilder &Builder) {
203 Builder.defineMacro(MacroName,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000204 Twine(BitWidth / TI.getCharWidth()));
Dan Gohmancfeac342010-05-28 00:27:15 +0000205}
206
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000207static void DefineExactWidthIntType(TargetInfo::IntType Ty,
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000208 const TargetInfo &TI, MacroBuilder &Builder) {
Ken Dyckeef22ef2009-11-16 16:36:33 +0000209 int TypeWidth = TI.getTypeWidth(Ty);
Daniel Dunbar7f3a5452010-06-30 06:30:56 +0000210
211 // Use the target specified int64 type, when appropriate, so that [u]int64_t
212 // ends up being defined in terms of the correct type.
213 if (TypeWidth == 64)
214 Ty = TI.getInt64Type();
215
Chris Lattner5f9e2722011-07-23 10:55:15 +0000216 DefineType("__INT" + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
Ken Dyckeef22ef2009-11-16 16:36:33 +0000217
Chris Lattner5f9e2722011-07-23 10:55:15 +0000218 StringRef ConstSuffix(TargetInfo::getTypeConstantSuffix(Ty));
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000219 if (!ConstSuffix.empty())
Chris Lattner5f9e2722011-07-23 10:55:15 +0000220 Builder.defineMacro("__INT" + Twine(TypeWidth) + "_C_SUFFIX__",
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000221 ConstSuffix);
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000222}
Chris Lattnere116ccf2009-04-21 05:40:52 +0000223
John McCallf85e1932011-06-15 23:02:42 +0000224/// \brief Add definitions required for a smooth interaction between
John McCallf85e1932011-06-15 23:02:42 +0000225/// Objective-C++ automated reference counting and libstdc++ (4.2).
226static void AddObjCXXARCLibstdcxxDefines(const LangOptions &LangOpts,
227 MacroBuilder &Builder) {
228 Builder.defineMacro("_GLIBCXX_PREDEFINED_OBJC_ARC_IS_SCALAR");
229
230 std::string Result;
231 {
232 // Provide specializations for the __is_scalar type trait so that
233 // lifetime-qualified objects are not considered "scalar" types, which
234 // libstdc++ uses as an indicator of the presence of trivial copy, assign,
235 // default-construct, and destruct semantics (none of which hold for
236 // lifetime-qualified objects in ARC).
237 llvm::raw_string_ostream Out(Result);
238
239 Out << "namespace std {\n"
240 << "\n"
241 << "struct __true_type;\n"
242 << "struct __false_type;\n"
243 << "\n";
244
245 Out << "template<typename _Tp> struct __is_scalar;\n"
246 << "\n";
247
248 Out << "template<typename _Tp>\n"
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000249 << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> {\n"
John McCallf85e1932011-06-15 23:02:42 +0000250 << " enum { __value = 0 };\n"
251 << " typedef __false_type __type;\n"
252 << "};\n"
253 << "\n";
254
John McCall9f084a32011-07-06 00:26:06 +0000255 if (LangOpts.ObjCRuntimeHasWeak) {
John McCallf85e1932011-06-15 23:02:42 +0000256 Out << "template<typename _Tp>\n"
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000257 << "struct __is_scalar<__attribute__((objc_ownership(weak))) _Tp> {\n"
John McCallf85e1932011-06-15 23:02:42 +0000258 << " enum { __value = 0 };\n"
259 << " typedef __false_type __type;\n"
260 << "};\n"
261 << "\n";
262 }
263
264 Out << "template<typename _Tp>\n"
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000265 << "struct __is_scalar<__attribute__((objc_ownership(autoreleasing)))"
John McCallf85e1932011-06-15 23:02:42 +0000266 << " _Tp> {\n"
267 << " enum { __value = 0 };\n"
268 << " typedef __false_type __type;\n"
269 << "};\n"
270 << "\n";
271
272 Out << "}\n";
273 }
274 Builder.append(Result);
275}
276
Nick Lewyckya9c64412011-06-07 06:07:12 +0000277static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
278 const LangOptions &LangOpts,
279 const FrontendOptions &FEOpts,
280 MacroBuilder &Builder) {
Eli Friedman85037d92011-10-19 23:46:05 +0000281 if (!LangOpts.MicrosoftMode && !LangOpts.TraditionalCPP)
Nick Lewyckya9c64412011-06-07 06:07:12 +0000282 Builder.defineMacro("__STDC__");
283 if (LangOpts.Freestanding)
284 Builder.defineMacro("__STDC_HOSTED__", "0");
285 else
286 Builder.defineMacro("__STDC_HOSTED__");
287
288 if (!LangOpts.CPlusPlus) {
Douglas Gregor5aa6dea2011-10-28 23:02:54 +0000289 // FIXME: C1x doesn't have a defined version number yet, so pick something
290 // that is the minimum possible according to their placeholder scheme
291 // 201ymmL.
292 if (LangOpts.C1X)
293 Builder.defineMacro("__STDC_VERSION__", "201001L");
294 else if (LangOpts.C99)
Nick Lewyckya9c64412011-06-07 06:07:12 +0000295 Builder.defineMacro("__STDC_VERSION__", "199901L");
296 else if (!LangOpts.GNUMode && LangOpts.Digraphs)
297 Builder.defineMacro("__STDC_VERSION__", "199409L");
298 } else {
299 if (LangOpts.GNUMode)
300 Builder.defineMacro("__cplusplus");
Douglas Gregor06788992011-06-20 15:00:58 +0000301 else {
302 // C++0x [cpp.predefined]p1:
303 // The name_ _cplusplus is defined to the value 201103L when compiling a
304 // C++ translation unit.
305 if (LangOpts.CPlusPlus0x)
306 Builder.defineMacro("__cplusplus", "201103L");
307 // C++03 [cpp.predefined]p1:
Nick Lewyckya9c64412011-06-07 06:07:12 +0000308 // The name_ _cplusplus is defined to the value 199711L when compiling a
309 // C++ translation unit.
Douglas Gregor06788992011-06-20 15:00:58 +0000310 else
311 Builder.defineMacro("__cplusplus", "199711L");
312 }
Nick Lewyckya9c64412011-06-07 06:07:12 +0000313 }
314
Nick Lewycky14648092011-06-10 20:56:43 +0000315 if (LangOpts.ObjC1)
316 Builder.defineMacro("__OBJC__");
317
Nick Lewyckya9c64412011-06-07 06:07:12 +0000318 // Not "standard" per se, but available even with the -undef flag.
319 if (LangOpts.AsmPreprocessor)
320 Builder.defineMacro("__ASSEMBLER__");
321}
322
Chris Lattnere116ccf2009-04-21 05:40:52 +0000323static void InitializePredefinedMacros(const TargetInfo &TI,
324 const LangOptions &LangOpts,
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000325 const FrontendOptions &FEOpts,
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000326 MacroBuilder &Builder) {
Chris Lattnere116ccf2009-04-21 05:40:52 +0000327 // Compiler version introspection macros.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000328 Builder.defineMacro("__llvm__"); // LLVM Backend
329 Builder.defineMacro("__clang__"); // Clang Frontend
Douglas Gregor4290fbd2010-04-30 02:51:06 +0000330#define TOSTR2(X) #X
331#define TOSTR(X) TOSTR2(X)
332 Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR));
333 Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR));
334#ifdef CLANG_VERSION_PATCHLEVEL
335 Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL));
336#else
337 Builder.defineMacro("__clang_patchlevel__", "0");
338#endif
339 Builder.defineMacro("__clang_version__",
340 "\"" CLANG_VERSION_STRING " ("
341 + getClangFullRepositoryVersion() + ")\"");
342#undef TOSTR
343#undef TOSTR2
Chris Lattnere116ccf2009-04-21 05:40:52 +0000344 // Currently claim to be compatible with GCC 4.2.1-5621.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000345 Builder.defineMacro("__GNUC_MINOR__", "2");
346 Builder.defineMacro("__GNUC_PATCHLEVEL__", "1");
347 Builder.defineMacro("__GNUC__", "4");
348 Builder.defineMacro("__GXX_ABI_VERSION", "1002");
Daniel Dunbarad1a4c62011-03-31 00:53:51 +0000349
350 // As sad as it is, enough software depends on the __VERSION__ for version
351 // checks that it is necessary to report 4.2.1 (the base GCC version we claim
352 // compatibility with) first.
353 Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible " +
Chris Lattner5f9e2722011-07-23 10:55:15 +0000354 Twine(getClangFullCPPVersion()) + "\"");
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Chris Lattnere116ccf2009-04-21 05:40:52 +0000356 // Initialize language-specific preprocessor defines.
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Chris Lattnere116ccf2009-04-21 05:40:52 +0000358 // Standard conforming mode?
359 if (!LangOpts.GNUMode)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000360 Builder.defineMacro("__STRICT_ANSI__");
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Chris Lattnere116ccf2009-04-21 05:40:52 +0000362 if (LangOpts.CPlusPlus0x)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000363 Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000364
Chris Lattnere116ccf2009-04-21 05:40:52 +0000365 if (LangOpts.ObjC1) {
Chris Lattnere116ccf2009-04-21 05:40:52 +0000366 if (LangOpts.ObjCNonFragileABI) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000367 Builder.defineMacro("__OBJC2__");
Douglas Gregoreced60c2011-09-12 15:17:19 +0000368
369 if (LangOpts.ObjCExceptions)
370 Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000371 }
372
Douglas Gregore289d812011-09-13 17:21:33 +0000373 if (LangOpts.getGC() != LangOptions::NonGC)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000374 Builder.defineMacro("__OBJC_GC__");
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Chris Lattnere116ccf2009-04-21 05:40:52 +0000376 if (LangOpts.NeXTRuntime)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000377 Builder.defineMacro("__NEXT_RUNTIME__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000378 }
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Chris Lattnere116ccf2009-04-21 05:40:52 +0000380 // darwin_constant_cfstrings controls this. This is also dependent
381 // on other things like the runtime I believe. This is set even for C code.
Fariborz Jahanian16c3eae2011-07-05 16:00:59 +0000382 if (!LangOpts.NoConstantCFStrings)
383 Builder.defineMacro("__CONSTANT_CFSTRINGS__");
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Chris Lattnere116ccf2009-04-21 05:40:52 +0000385 if (LangOpts.ObjC2)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000386 Builder.defineMacro("OBJC_NEW_PROPERTIES");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000387
388 if (LangOpts.PascalStrings)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000389 Builder.defineMacro("__PASCAL_STRINGS__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000390
391 if (LangOpts.Blocks) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000392 Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))");
393 Builder.defineMacro("__BLOCKS__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Douglas Gregoreced60c2011-09-12 15:17:19 +0000396 if (LangOpts.CXXExceptions)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000397 Builder.defineMacro("__EXCEPTIONS");
Dan Gohmancfeac342010-05-28 00:27:15 +0000398 if (LangOpts.RTTI)
399 Builder.defineMacro("__GXX_RTTI");
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000400 if (LangOpts.SjLjExceptions)
401 Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__");
Rafael Espindolaf759df02009-10-01 13:33:33 +0000402
Chandler Carruthf8c247d2011-04-23 19:48:40 +0000403 if (LangOpts.Deprecated)
404 Builder.defineMacro("__DEPRECATED");
405
Chris Lattnere116ccf2009-04-21 05:40:52 +0000406 if (LangOpts.CPlusPlus) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000407 Builder.defineMacro("__GNUG__", "4");
408 Builder.defineMacro("__GXX_WEAK__");
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000409 Builder.defineMacro("__private_extern__", "extern");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000410 }
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Francois Pichet62ec1f22011-09-17 17:15:52 +0000412 if (LangOpts.MicrosoftExt) {
Steve Narofffdd6aaf2009-12-04 21:29:41 +0000413 // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however
414 // VC++ appears to only like __FUNCTION__.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000415 Builder.defineMacro("__PRETTY_FUNCTION__", "__FUNCTION__");
John Thompson24ee8042009-10-16 01:12:00 +0000416 // Work around some issues with Visual C++ headerws.
417 if (LangOpts.CPlusPlus) {
418 // Since we define wchar_t in C++ mode.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000419 Builder.defineMacro("_WCHAR_T_DEFINED");
420 Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED");
Francois Pichet4b7e0482011-05-09 22:32:46 +0000421 // FIXME: Support Microsoft's __identifier extension in the lexer.
Francois Picheta23ae3f2011-05-07 17:47:38 +0000422 Builder.append("#define __identifier(x) x");
Douglas Gregor5c0ca522010-08-30 14:44:26 +0000423 Builder.append("class type_info;");
John Thompson24ee8042009-10-16 01:12:00 +0000424 }
Steven Watanabe343d65c2010-09-05 23:16:22 +0000425
426 if (LangOpts.CPlusPlus0x) {
427 Builder.defineMacro("_HAS_CHAR16_T_LANGUAGE_SUPPORT", "1");
428 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000429 }
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Chris Lattnere116ccf2009-04-21 05:40:52 +0000431 if (LangOpts.Optimize)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000432 Builder.defineMacro("__OPTIMIZE__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000433 if (LangOpts.OptimizeSize)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000434 Builder.defineMacro("__OPTIMIZE_SIZE__");
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Chris Lattnere116ccf2009-04-21 05:40:52 +0000436 // Initialize target-specific preprocessor defines.
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Chris Lattnere116ccf2009-04-21 05:40:52 +0000438 // Define type sizing macros based on the target properties.
439 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000440 Builder.defineMacro("__CHAR_BIT__", "8");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000441
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000442 DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Builder);
443 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder);
444 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder);
445 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder);
446 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder);
447 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder);
448 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000449
Dan Gohmancfeac342010-05-28 00:27:15 +0000450 DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder);
451 DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder);
452 DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder);
453 DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder);
454 DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder);
455 DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder);
456 DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder);
457 DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder);
458 DefineTypeSizeof("__SIZEOF_PTRDIFF_T__",
459 TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder);
460 DefineTypeSizeof("__SIZEOF_SIZE_T__",
461 TI.getTypeWidth(TI.getSizeType()), TI, Builder);
462 DefineTypeSizeof("__SIZEOF_WCHAR_T__",
463 TI.getTypeWidth(TI.getWCharType()), TI, Builder);
464 DefineTypeSizeof("__SIZEOF_WINT_T__",
465 TI.getTypeWidth(TI.getWIntType()), TI, Builder);
466
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000467 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder);
468 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder);
469 DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder);
470 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder);
471 DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder);
472 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder);
473 DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder);
474 DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder);
475 DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder);
476 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder);
477 DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder);
478 DefineType("__WINT_TYPE__", TI.getWIntType(), Builder);
479 DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder);
480 DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder);
Dan Gohmancfeac342010-05-28 00:27:15 +0000481 DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder);
482 DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000484 DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat());
485 DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat());
486 DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000487
488 // Define a __POINTER_WIDTH__ macro for stdint.h.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000489 Builder.defineMacro("__POINTER_WIDTH__",
Chris Lattner5f9e2722011-07-23 10:55:15 +0000490 Twine((int)TI.getPointerWidth(0)));
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Eli Friedman15b91762009-06-05 07:05:05 +0000492 if (!LangOpts.CharIsSigned)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000493 Builder.defineMacro("__CHAR_UNSIGNED__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000494
Eli Friedman1cfeefd2011-04-21 05:45:45 +0000495 if (!TargetInfo::isTypeSigned(TI.getWIntType()))
496 Builder.defineMacro("__WINT_UNSIGNED__");
497
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000498 // Define exact-width integer types for stdint.h
Chris Lattner5f9e2722011-07-23 10:55:15 +0000499 Builder.defineMacro("__INT" + Twine(TI.getCharWidth()) + "_TYPE__",
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000500 "char");
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000502 if (TI.getShortWidth() > TI.getCharWidth())
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000503 DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000505 if (TI.getIntWidth() > TI.getShortWidth())
506 DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder);
507
508 if (TI.getLongWidth() > TI.getIntWidth())
509 DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder);
510
511 if (TI.getLongLongWidth() > TI.getLongWidth())
512 DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder);
513
514 // Add __builtin_va_list typedef.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000515 Builder.append(TI.getVAListDeclaration());
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000516
517 if (const char *Prefix = TI.getUserLabelPrefix())
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000518 Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix);
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Chris Lattnere116ccf2009-04-21 05:40:52 +0000520 // Build configuration options. FIXME: these should be controlled by
521 // command line options or something.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000522 Builder.defineMacro("__FINITE_MATH_ONLY__", "0");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000523
Chris Lattnere116ccf2009-04-21 05:40:52 +0000524 if (LangOpts.GNUInline)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000525 Builder.defineMacro("__GNUC_GNU_INLINE__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000526 else
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000527 Builder.defineMacro("__GNUC_STDC_INLINE__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000528
529 if (LangOpts.NoInline)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000530 Builder.defineMacro("__NO_INLINE__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000531
532 if (unsigned PICLevel = LangOpts.PICLevel) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000533 Builder.defineMacro("__PIC__", Twine(PICLevel));
534 Builder.defineMacro("__pic__", Twine(PICLevel));
Chris Lattnere116ccf2009-04-21 05:40:52 +0000535 }
536
537 // Macros to control C99 numerics and <float.h>
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000538 Builder.defineMacro("__FLT_EVAL_METHOD__", "0");
539 Builder.defineMacro("__FLT_RADIX__", "2");
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000540 int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000541 Builder.defineMacro("__DECIMAL_DIG__", Twine(Dig));
Bill Wendling45483f72009-06-28 07:36:13 +0000542
Douglas Gregore289d812011-09-13 17:21:33 +0000543 if (LangOpts.getStackProtector() == LangOptions::SSPOn)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000544 Builder.defineMacro("__SSP__");
Douglas Gregore289d812011-09-13 17:21:33 +0000545 else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000546 Builder.defineMacro("__SSP_ALL__", "2");
Bill Wendling45483f72009-06-28 07:36:13 +0000547
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000548 if (FEOpts.ProgramAction == frontend::RewriteObjC)
549 Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
Ted Kremenekf0af7772010-05-26 21:36:54 +0000550
551 // Define a macro that exists only when using the static analyzer.
552 if (FEOpts.ProgramAction == frontend::RunAnalysis)
553 Builder.defineMacro("__clang_analyzer__");
554
Peter Collingbournef0840822010-12-04 01:51:23 +0000555 if (LangOpts.FastRelaxedMath)
556 Builder.defineMacro("__FAST_RELAXED_MATH__");
557
John McCall098df7f2011-06-16 00:03:19 +0000558 if (LangOpts.ObjCAutoRefCount) {
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000559 Builder.defineMacro("__weak", "__attribute__((objc_ownership(weak)))");
560 Builder.defineMacro("__strong", "__attribute__((objc_ownership(strong)))");
John McCall098df7f2011-06-16 00:03:19 +0000561 Builder.defineMacro("__autoreleasing",
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000562 "__attribute__((objc_ownership(autoreleasing)))");
John McCall098df7f2011-06-16 00:03:19 +0000563 Builder.defineMacro("__unsafe_unretained",
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000564 "__attribute__((objc_ownership(none)))");
John McCall098df7f2011-06-16 00:03:19 +0000565 }
566
Chris Lattnere116ccf2009-04-21 05:40:52 +0000567 // Get other target #defines.
Benjamin Kramera9992772010-01-09 17:55:51 +0000568 TI.getTargetDefines(LangOpts, Builder);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000569}
570
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000571// Initialize the remapping of files to alternative contents, e.g.,
572// those specified through other files.
David Blaikied6471f72011-09-25 23:23:43 +0000573static void InitializeFileRemapping(DiagnosticsEngine &Diags,
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000574 SourceManager &SourceMgr,
575 FileManager &FileMgr,
576 const PreprocessorOptions &InitOpts) {
Douglas Gregor4db64a42010-01-23 00:14:00 +0000577 // Remap files in the source manager (with buffers).
Douglas Gregor44c181a2010-07-23 00:33:23 +0000578 for (PreprocessorOptions::const_remapped_file_buffer_iterator
Douglas Gregor4db64a42010-01-23 00:14:00 +0000579 Remap = InitOpts.remapped_file_buffer_begin(),
580 RemapEnd = InitOpts.remapped_file_buffer_end();
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000581 Remap != RemapEnd;
582 ++Remap) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000583 // Create the file entry for the file that we're mapping from.
584 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000585 Remap->second->getBufferSize(),
Chris Lattner39b49bc2010-11-23 08:35:12 +0000586 0);
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000587 if (!FromFile) {
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000588 Diags.Report(diag::err_fe_remap_missing_from_file)
589 << Remap->first;
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000590 if (!InitOpts.RetainRemappedFileBuffers)
591 delete Remap->second;
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000592 continue;
593 }
594
Douglas Gregor4db64a42010-01-23 00:14:00 +0000595 // Override the contents of the "from" file with the contents of
596 // the "to" file.
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000597 SourceMgr.overrideFileContents(FromFile, Remap->second,
598 InitOpts.RetainRemappedFileBuffers);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000599 }
600
601 // Remap files in the source manager (with other files).
Douglas Gregor44c181a2010-07-23 00:33:23 +0000602 for (PreprocessorOptions::const_remapped_file_iterator
603 Remap = InitOpts.remapped_file_begin(),
604 RemapEnd = InitOpts.remapped_file_end();
Douglas Gregor4db64a42010-01-23 00:14:00 +0000605 Remap != RemapEnd;
606 ++Remap) {
607 // Find the file that we're mapping to.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000608 const FileEntry *ToFile = FileMgr.getFile(Remap->second);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000609 if (!ToFile) {
610 Diags.Report(diag::err_fe_remap_missing_to_file)
611 << Remap->first << Remap->second;
612 continue;
613 }
614
615 // Create the file entry for the file that we're mapping from.
616 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000617 ToFile->getSize(), 0);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000618 if (!FromFile) {
619 Diags.Report(diag::err_fe_remap_missing_from_file)
620 << Remap->first;
621 continue;
622 }
623
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000624 // Override the contents of the "from" file with the contents of
625 // the "to" file.
Argyrios Kyrtzidis299a4a92011-03-08 23:35:24 +0000626 SourceMgr.overrideFileContents(FromFile, ToFile);
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000627 }
Argyrios Kyrtzidis299a4a92011-03-08 23:35:24 +0000628
629 SourceMgr.setOverridenFilesKeepOriginalName(
630 InitOpts.RemappedFilesKeepOriginalName);
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000631}
632
Chris Lattnere116ccf2009-04-21 05:40:52 +0000633/// InitializePreprocessor - Initialize the preprocessor getting it and the
634/// environment ready to process a single file. This returns true on error.
635///
Daniel Dunbar938963f2009-11-04 21:13:15 +0000636void clang::InitializePreprocessor(Preprocessor &PP,
Daniel Dunbar961c76e2009-11-11 21:44:42 +0000637 const PreprocessorOptions &InitOpts,
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000638 const HeaderSearchOptions &HSOpts,
639 const FrontendOptions &FEOpts) {
John McCallf85e1932011-06-15 23:02:42 +0000640 const LangOptions &LangOpts = PP.getLangOptions();
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000641 std::string PredefineBuffer;
642 PredefineBuffer.reserve(4080);
643 llvm::raw_string_ostream Predefines(PredefineBuffer);
644 MacroBuilder Builder(Predefines);
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000646 InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(),
Chris Lattner39b49bc2010-11-23 08:35:12 +0000647 PP.getFileManager(), InitOpts);
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000648
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000649 // Specify whether the preprocessor should replace #include/#import with
650 // module imports when plausible.
651 PP.setAutoModuleImport(InitOpts.AutoModuleImport);
652
Chris Lattnerfcaa9e72010-04-26 22:08:10 +0000653 // Emit line markers for various builtin sections of the file. We don't do
654 // this in asm preprocessor mode, because "# 4" is not a line marker directive
655 // in this mode.
656 if (!PP.getLangOptions().AsmPreprocessor)
657 Builder.append("# 1 \"<built-in>\" 3");
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Chris Lattnere116ccf2009-04-21 05:40:52 +0000659 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
John McCallf85e1932011-06-15 23:02:42 +0000660 if (InitOpts.UsePredefines) {
661 InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000662
John McCallf85e1932011-06-15 23:02:42 +0000663 // Install definitions to make Objective-C++ ARC work well with various
664 // C++ Standard Library implementations.
665 if (LangOpts.ObjC1 && LangOpts.CPlusPlus && LangOpts.ObjCAutoRefCount) {
666 switch (InitOpts.ObjCXXARCStandardLibrary) {
667 case ARCXX_nolib:
Douglas Gregored73b102011-09-27 22:48:19 +0000668 case ARCXX_libcxx:
John McCallf85e1932011-06-15 23:02:42 +0000669 break;
670
671 case ARCXX_libstdcxx:
672 AddObjCXXARCLibstdcxxDefines(LangOpts, Builder);
673 break;
674 }
675 }
676 }
677
Nick Lewyckya9c64412011-06-07 06:07:12 +0000678 // Even with predefines off, some macros are still predefined.
679 // These should all be defined in the preprocessor according to the
680 // current language configuration.
681 InitializeStandardPredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(),
682 FEOpts, Builder);
683
Chris Lattnere116ccf2009-04-21 05:40:52 +0000684 // Add on the predefines from the driver. Wrap in a #line directive to report
685 // that they come from the command line.
Chris Lattnerfcaa9e72010-04-26 22:08:10 +0000686 if (!PP.getLangOptions().AsmPreprocessor)
687 Builder.append("# 1 \"<command line>\" 1");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000688
689 // Process #define's and #undef's in the order they are given.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000690 for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
691 if (InitOpts.Macros[i].second) // isUndef
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000692 Builder.undefineMacro(InitOpts.Macros[i].first);
Chris Lattner62f86c42009-04-21 06:00:24 +0000693 else
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000694 DefineBuiltinMacro(Builder, InitOpts.Macros[i].first,
Daniel Dunbar1ee4b9e2010-01-10 00:46:21 +0000695 PP.getDiagnostics());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000696 }
697
698 // If -imacros are specified, include them now. These are processed before
699 // any -include directives.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000700 for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
Nick Lewycky277a6e72011-02-23 21:16:44 +0000701 AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i],
702 PP.getFileManager());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000703
704 // Process -include directives.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000705 for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
706 const std::string &Path = InitOpts.Includes[i];
707 if (Path == InitOpts.ImplicitPTHInclude)
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000708 AddImplicitIncludePTH(Builder, PP, Path);
Chris Lattner62f86c42009-04-21 06:00:24 +0000709 else
Nick Lewycky277a6e72011-02-23 21:16:44 +0000710 AddImplicitInclude(Builder, Path, PP.getFileManager());
Eli Friedmanae96a962009-06-15 09:57:52 +0000711 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000712
Rafael Espindola0259d202009-12-01 18:28:16 +0000713 // Exit the command line and go back to <built-in> (2 is LC_LEAVE).
Chris Lattnerfcaa9e72010-04-26 22:08:10 +0000714 if (!PP.getLangOptions().AsmPreprocessor)
715 Builder.append("# 1 \"<built-in>\" 2");
Rafael Espindola0259d202009-12-01 18:28:16 +0000716
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000717 // Instruct the preprocessor to skip the preamble.
718 PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first,
719 InitOpts.PrecompiledPreambleBytes.second);
720
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000721 // Copy PredefinedBuffer into the Preprocessor.
722 PP.setPredefines(Predefines.str());
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000723
Daniel Dunbar961c76e2009-11-11 21:44:42 +0000724 // Initialize the header search object.
725 ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts,
726 PP.getLangOptions(),
727 PP.getTargetInfo().getTriple());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000728}