blob: 456d624f738579795503779eabc9628e99ae9bee [file] [log] [blame]
Chris Lattner2f5693f2009-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
Daniel Dunbar00f8a392009-11-07 04:20:15 +000014#include "clang/Frontend/Utils.h"
Chris Lattner2f5693f2009-04-21 05:40:52 +000015#include "clang/Basic/TargetInfo.h"
Douglas Gregor6ae34ab2009-12-02 16:32:41 +000016#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar00f8a392009-11-07 04:20:15 +000017#include "clang/Frontend/PreprocessorOptions.h"
Chris Lattner2f5693f2009-04-21 05:40:52 +000018#include "clang/Lex/Preprocessor.h"
Douglas Gregor6ae34ab2009-12-02 16:32:41 +000019#include "clang/Basic/FileManager.h"
20#include "clang/Basic/SourceManager.h"
Chris Lattner2f5693f2009-04-21 05:40:52 +000021#include "llvm/ADT/StringExtras.h"
Douglas Gregor407e2122009-12-02 18:12:28 +000022#include "llvm/ADT/StringMap.h"
Kovarththanan Rajaratnam2f146192010-01-09 09:27:11 +000023#include "llvm/ADT/StringRef.h"
Chris Lattner2f5693f2009-04-21 05:40:52 +000024#include "llvm/ADT/STLExtras.h"
Douglas Gregor6ae34ab2009-12-02 16:32:41 +000025#include "llvm/Support/MemoryBuffer.h"
Chris Lattner2f5693f2009-04-21 05:40:52 +000026#include "llvm/System/Path.h"
Chris Lattner3fab58d2009-11-02 21:48:09 +000027using namespace clang;
Chris Lattner2f5693f2009-04-21 05:40:52 +000028
29// Append a #define line to Buf for Macro. Macro should be of the form XXX,
30// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
31// "#define XXX Y z W". To get a #define with no value, use "XXX=".
Kovarththanan Rajaratnam2f146192010-01-09 09:27:11 +000032static void DefineBuiltinMacro(std::vector<char> &Buf, llvm::StringRef Macro,
Daniel Dunbar6dac9352009-12-03 09:14:12 +000033 Diagnostic *Diags = 0) {
Kovarththanan Rajaratnam49c8da92010-01-07 18:11:14 +000034 const char Command[] = "#define ";
Chris Lattner2f5693f2009-04-21 05:40:52 +000035 Buf.insert(Buf.end(), Command, Command+strlen(Command));
Kovarththanan Rajaratnam2f146192010-01-09 09:27:11 +000036 std::pair<llvm::StringRef, llvm::StringRef> MacroPair = Macro.split('=');
37 llvm::StringRef MacroName = MacroPair.first;
38 llvm::StringRef MacroBody = MacroPair.second;
39 if (!MacroBody.empty()) {
Chris Lattner2f5693f2009-04-21 05:40:52 +000040 // Turn the = into ' '.
Kovarththanan Rajaratnam2f146192010-01-09 09:27:11 +000041 Buf.insert(Buf.end(), MacroName.begin(), MacroName.end());
Chris Lattner2f5693f2009-04-21 05:40:52 +000042 Buf.push_back(' ');
Mike Stump11289f42009-09-09 15:08:12 +000043
Chris Lattner2f5693f2009-04-21 05:40:52 +000044 // Per GCC -D semantics, the macro ends at \n if it exists.
Kovarththanan Rajaratnam2f146192010-01-09 09:27:11 +000045 const char *End = strpbrk(MacroBody.data(), "\n\r");
Chris Lattner2f5693f2009-04-21 05:40:52 +000046 if (End) {
Daniel Dunbar6dac9352009-12-03 09:14:12 +000047 assert(Diags && "Unexpected macro with embedded newline!");
48 Diags->Report(diag::warn_fe_macro_contains_embedded_newline)
Kovarththanan Rajaratnam2f146192010-01-09 09:27:11 +000049 << MacroName;
Chris Lattner2f5693f2009-04-21 05:40:52 +000050 } else {
Kovarththanan Rajaratnam2f146192010-01-09 09:27:11 +000051 End = MacroBody.end();
Chris Lattner2f5693f2009-04-21 05:40:52 +000052 }
Mike Stump11289f42009-09-09 15:08:12 +000053
Kovarththanan Rajaratnam2f146192010-01-09 09:27:11 +000054 Buf.insert(Buf.end(), MacroBody.begin(), End);
Chris Lattner2f5693f2009-04-21 05:40:52 +000055 } else {
56 // Push "macroname 1".
Kovarththanan Rajaratnam2f146192010-01-09 09:27:11 +000057 Buf.insert(Buf.end(), Macro.begin(), Macro.end());
Chris Lattner2f5693f2009-04-21 05:40:52 +000058 Buf.push_back(' ');
59 Buf.push_back('1');
60 }
61 Buf.push_back('\n');
62}
63
Chris Lattner4579b762009-05-15 16:08:43 +000064// Append a #undef line to Buf for Macro. Macro should be of the form XXX
65// and we emit "#undef XXX".
66static void UndefineBuiltinMacro(std::vector<char> &Buf, const char *Macro) {
67 // Push "macroname".
Kovarththanan Rajaratnam49c8da92010-01-07 18:11:14 +000068 const char Command[] = "#undef ";
Chris Lattner4579b762009-05-15 16:08:43 +000069 Buf.insert(Buf.end(), Command, Command+strlen(Command));
70 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
71 Buf.push_back('\n');
72}
73
Daniel Dunbar732ef8a2009-11-11 23:58:53 +000074std::string clang::NormalizeDashIncludePath(llvm::StringRef File) {
Daniel Dunbar66cb0132009-04-22 08:53:01 +000075 // Implicit include paths should be resolved relative to the current
76 // working directory first, and then use the regular header search
77 // mechanism. The proper way to handle this is to have the
78 // predefines buffer located at the current working directory, but
79 // it has not file entry. For now, workaround this by using an
80 // absolute path if we find the file here, and otherwise letting
81 // header search handle it.
Chris Lattner2f5693f2009-04-21 05:40:52 +000082 llvm::sys::Path Path(File);
83 Path.makeAbsolute();
Daniel Dunbar66cb0132009-04-22 08:53:01 +000084 if (!Path.exists())
85 Path = File;
Mike Stump11289f42009-09-09 15:08:12 +000086
Daniel Dunbar732ef8a2009-11-11 23:58:53 +000087 return Lexer::Stringify(Path.str());
88}
89
90/// Add the quoted name of an implicit include file.
91static void AddQuotedIncludePath(std::vector<char> &Buf,
92 const std::string &File) {
93
Chris Lattner2f5693f2009-04-21 05:40:52 +000094 // Escape double quotes etc.
95 Buf.push_back('"');
Daniel Dunbar732ef8a2009-11-11 23:58:53 +000096 std::string EscapedFile = NormalizeDashIncludePath(File);
Chris Lattner2f5693f2009-04-21 05:40:52 +000097 Buf.insert(Buf.end(), EscapedFile.begin(), EscapedFile.end());
98 Buf.push_back('"');
99}
100
101/// AddImplicitInclude - Add an implicit #include of the specified file to the
102/// predefines buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000103static void AddImplicitInclude(std::vector<char> &Buf,
Chris Lattner2f5693f2009-04-21 05:40:52 +0000104 const std::string &File) {
Kovarththanan Rajaratnam49c8da92010-01-07 18:11:14 +0000105 const char Command[] = "#include ";
106 Buf.insert(Buf.end(), Command, Command+strlen(Command));
Chris Lattner2f5693f2009-04-21 05:40:52 +0000107 AddQuotedIncludePath(Buf, File);
108 Buf.push_back('\n');
109}
110
111static void AddImplicitIncludeMacros(std::vector<char> &Buf,
112 const std::string &File) {
Kovarththanan Rajaratnam49c8da92010-01-07 18:11:14 +0000113 const char Command[] = "#__include_macros ";
114 Buf.insert(Buf.end(), Command, Command+strlen(Command));
Chris Lattner2f5693f2009-04-21 05:40:52 +0000115 AddQuotedIncludePath(Buf, File);
116 Buf.push_back('\n');
117 // Marker token to stop the __include_macros fetch loop.
118 const char *Marker = "##\n"; // ##?
119 Buf.insert(Buf.end(), Marker, Marker+strlen(Marker));
120}
121
122/// AddImplicitIncludePTH - Add an implicit #include using the original file
123/// used to generate a PTH cache.
Mike Stump11289f42009-09-09 15:08:12 +0000124static void AddImplicitIncludePTH(std::vector<char> &Buf, Preprocessor &PP,
Chris Lattner2f5693f2009-04-21 05:40:52 +0000125 const std::string& ImplicitIncludePTH) {
126 PTHManager *P = PP.getPTHManager();
127 assert(P && "No PTHManager.");
128 const char *OriginalFile = P->getOriginalSourceFile();
Mike Stump11289f42009-09-09 15:08:12 +0000129
Chris Lattner2f5693f2009-04-21 05:40:52 +0000130 if (!OriginalFile) {
Daniel Dunbar6dac9352009-12-03 09:14:12 +0000131 PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header)
132 << ImplicitIncludePTH;
133 return;
Chris Lattner2f5693f2009-04-21 05:40:52 +0000134 }
Mike Stump11289f42009-09-09 15:08:12 +0000135
Chris Lattner2f5693f2009-04-21 05:40:52 +0000136 AddImplicitInclude(Buf, OriginalFile);
137}
138
139/// PickFP - This is used to pick a value based on the FP semantics of the
140/// specified FP model.
141template <typename T>
142static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal,
Eli Friedman57406b22009-05-23 03:50:01 +0000143 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
144 T IEEEQuadVal) {
Duncan Sandsf4063512009-06-03 14:28:20 +0000145 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle)
Chris Lattner2f5693f2009-04-21 05:40:52 +0000146 return IEEESingleVal;
Duncan Sandsf4063512009-06-03 14:28:20 +0000147 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble)
Chris Lattner2f5693f2009-04-21 05:40:52 +0000148 return IEEEDoubleVal;
Duncan Sandsf4063512009-06-03 14:28:20 +0000149 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended)
Chris Lattner2f5693f2009-04-21 05:40:52 +0000150 return X87DoubleExtendedVal;
Duncan Sandsf4063512009-06-03 14:28:20 +0000151 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble)
Eli Friedman57406b22009-05-23 03:50:01 +0000152 return PPCDoubleDoubleVal;
Duncan Sandsf4063512009-06-03 14:28:20 +0000153 assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad);
Eli Friedman57406b22009-05-23 03:50:01 +0000154 return IEEEQuadVal;
Chris Lattner2f5693f2009-04-21 05:40:52 +0000155}
156
157static void DefineFloatMacros(std::vector<char> &Buf, const char *Prefix,
158 const llvm::fltSemantics *Sem) {
159 const char *DenormMin, *Epsilon, *Max, *Min;
Mike Stump11289f42009-09-09 15:08:12 +0000160 DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324",
Chris Lattner2f5693f2009-04-21 05:40:52 +0000161 "3.64519953188247460253e-4951L",
Eli Friedman57406b22009-05-23 03:50:01 +0000162 "4.94065645841246544176568792868221e-324L",
163 "6.47517511943802511092443895822764655e-4966L");
164 int Digits = PickFP(Sem, 6, 15, 18, 31, 33);
Chris Lattner2f5693f2009-04-21 05:40:52 +0000165 Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16",
166 "1.08420217248550443401e-19L",
Eli Friedman57406b22009-05-23 03:50:01 +0000167 "4.94065645841246544176568792868221e-324L",
168 "1.92592994438723585305597794258492732e-34L");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000169 int HasInifinity = 1, HasQuietNaN = 1;
Eli Friedman57406b22009-05-23 03:50:01 +0000170 int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113);
171 int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931);
172 int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932);
173 int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381);
174 int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384);
Chris Lattner2f5693f2009-04-21 05:40:52 +0000175 Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308",
176 "3.36210314311209350626e-4932L",
Eli Friedman57406b22009-05-23 03:50:01 +0000177 "2.00416836000897277799610805135016e-292L",
178 "3.36210314311209350626267781732175260e-4932L");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000179 Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308",
180 "1.18973149535723176502e+4932L",
Eli Friedman57406b22009-05-23 03:50:01 +0000181 "1.79769313486231580793728971405301e+308L",
182 "1.18973149535723176508575932662800702e+4932L");
Mike Stump11289f42009-09-09 15:08:12 +0000183
Eli Friedman57406b22009-05-23 03:50:01 +0000184 char MacroBuf[100];
Chris Lattner2f5693f2009-04-21 05:40:52 +0000185 sprintf(MacroBuf, "__%s_DENORM_MIN__=%s", Prefix, DenormMin);
186 DefineBuiltinMacro(Buf, MacroBuf);
187 sprintf(MacroBuf, "__%s_DIG__=%d", Prefix, Digits);
188 DefineBuiltinMacro(Buf, MacroBuf);
189 sprintf(MacroBuf, "__%s_EPSILON__=%s", Prefix, Epsilon);
190 DefineBuiltinMacro(Buf, MacroBuf);
191 sprintf(MacroBuf, "__%s_HAS_INFINITY__=%d", Prefix, HasInifinity);
192 DefineBuiltinMacro(Buf, MacroBuf);
193 sprintf(MacroBuf, "__%s_HAS_QUIET_NAN__=%d", Prefix, HasQuietNaN);
194 DefineBuiltinMacro(Buf, MacroBuf);
195 sprintf(MacroBuf, "__%s_MANT_DIG__=%d", Prefix, MantissaDigits);
196 DefineBuiltinMacro(Buf, MacroBuf);
197 sprintf(MacroBuf, "__%s_MAX_10_EXP__=%d", Prefix, Max10Exp);
198 DefineBuiltinMacro(Buf, MacroBuf);
199 sprintf(MacroBuf, "__%s_MAX_EXP__=%d", Prefix, MaxExp);
200 DefineBuiltinMacro(Buf, MacroBuf);
201 sprintf(MacroBuf, "__%s_MAX__=%s", Prefix, Max);
202 DefineBuiltinMacro(Buf, MacroBuf);
203 sprintf(MacroBuf, "__%s_MIN_10_EXP__=(%d)", Prefix, Min10Exp);
204 DefineBuiltinMacro(Buf, MacroBuf);
205 sprintf(MacroBuf, "__%s_MIN_EXP__=(%d)", Prefix, MinExp);
206 DefineBuiltinMacro(Buf, MacroBuf);
207 sprintf(MacroBuf, "__%s_MIN__=%s", Prefix, Min);
208 DefineBuiltinMacro(Buf, MacroBuf);
209 sprintf(MacroBuf, "__%s_HAS_DENORM__=1", Prefix);
210 DefineBuiltinMacro(Buf, MacroBuf);
211}
212
213
214/// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
215/// named MacroName with the max value for a type with width 'TypeWidth' a
216/// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
217static void DefineTypeSize(const char *MacroName, unsigned TypeWidth,
218 const char *ValSuffix, bool isSigned,
219 std::vector<char> &Buf) {
220 char MacroBuf[60];
221 long long MaxVal;
222 if (isSigned)
223 MaxVal = (1LL << (TypeWidth - 1)) - 1;
224 else
225 MaxVal = ~0LL >> (64-TypeWidth);
Mike Stump11289f42009-09-09 15:08:12 +0000226
Daniel Dunbarfe07aec2009-09-03 19:23:49 +0000227 // FIXME: Switch to using raw_ostream and avoid utostr().
228 sprintf(MacroBuf, "%s=%s%s", MacroName, llvm::utostr(MaxVal).c_str(),
229 ValSuffix);
Chris Lattner2f5693f2009-04-21 05:40:52 +0000230 DefineBuiltinMacro(Buf, MacroBuf);
231}
232
Chris Lattnere4a8c642009-11-05 21:21:32 +0000233/// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
234/// the width, suffix, and signedness of the given type
235static void DefineTypeSize(const char *MacroName, TargetInfo::IntType Ty,
236 const TargetInfo &TI, std::vector<char> &Buf) {
237 DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
238 TI.isTypeSigned(Ty), Buf);
239}
240
Chris Lattner2f5693f2009-04-21 05:40:52 +0000241static void DefineType(const char *MacroName, TargetInfo::IntType Ty,
242 std::vector<char> &Buf) {
243 char MacroBuf[60];
244 sprintf(MacroBuf, "%s=%s", MacroName, TargetInfo::getTypeName(Ty));
245 DefineBuiltinMacro(Buf, MacroBuf);
246}
247
Ken Dyckc0c98292009-11-18 13:52:57 +0000248static void DefineTypeWidth(const char *MacroName, TargetInfo::IntType Ty,
249 const TargetInfo &TI, std::vector<char> &Buf) {
250 char MacroBuf[60];
251 sprintf(MacroBuf, "%s=%d", MacroName, TI.getTypeWidth(Ty));
252 DefineBuiltinMacro(Buf, MacroBuf);
253}
254
Chris Lattner55c98772009-11-12 08:08:27 +0000255static void DefineExactWidthIntType(TargetInfo::IntType Ty,
256 const TargetInfo &TI, std::vector<char> &Buf) {
257 char MacroBuf[60];
Ken Dyck2dc8d5f2009-11-16 16:36:33 +0000258 int TypeWidth = TI.getTypeWidth(Ty);
259 sprintf(MacroBuf, "__INT%d_TYPE__", TypeWidth);
Chris Lattner55c98772009-11-12 08:08:27 +0000260 DefineType(MacroBuf, Ty, Buf);
Ken Dyck2dc8d5f2009-11-16 16:36:33 +0000261
262
263 const char *ConstSuffix = TargetInfo::getTypeConstantSuffix(Ty);
264 if (strlen(ConstSuffix) > 0) {
265 sprintf(MacroBuf, "__INT%d_C_SUFFIX__=%s", TypeWidth, ConstSuffix);
266 DefineBuiltinMacro(Buf, MacroBuf);
267 }
Chris Lattner55c98772009-11-12 08:08:27 +0000268}
Chris Lattner2f5693f2009-04-21 05:40:52 +0000269
270static void InitializePredefinedMacros(const TargetInfo &TI,
271 const LangOptions &LangOpts,
272 std::vector<char> &Buf) {
Chris Lattner2f5693f2009-04-21 05:40:52 +0000273 // Compiler version introspection macros.
274 DefineBuiltinMacro(Buf, "__llvm__=1"); // LLVM Backend
275 DefineBuiltinMacro(Buf, "__clang__=1"); // Clang Frontend
Mike Stump11289f42009-09-09 15:08:12 +0000276
Chris Lattner2f5693f2009-04-21 05:40:52 +0000277 // Currently claim to be compatible with GCC 4.2.1-5621.
Chris Lattner2f5693f2009-04-21 05:40:52 +0000278 DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2");
279 DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
280 DefineBuiltinMacro(Buf, "__GNUC__=4");
281 DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
282 DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 Compatible Clang Compiler\"");
Mike Stump11289f42009-09-09 15:08:12 +0000283
284
Chris Lattner2f5693f2009-04-21 05:40:52 +0000285 // Initialize language-specific preprocessor defines.
Mike Stump11289f42009-09-09 15:08:12 +0000286
Chris Lattner2f5693f2009-04-21 05:40:52 +0000287 // These should all be defined in the preprocessor according to the
288 // current language configuration.
289 if (!LangOpts.Microsoft)
290 DefineBuiltinMacro(Buf, "__STDC__=1");
291 if (LangOpts.AsmPreprocessor)
292 DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
Ryan Flynned73cac2009-07-21 00:07:02 +0000293
294 if (!LangOpts.CPlusPlus) {
295 if (LangOpts.C99)
296 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
297 else if (!LangOpts.GNUMode && LangOpts.Digraphs)
298 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
299 }
Chris Lattner2f5693f2009-04-21 05:40:52 +0000300
301 // Standard conforming mode?
302 if (!LangOpts.GNUMode)
303 DefineBuiltinMacro(Buf, "__STRICT_ANSI__=1");
Mike Stump11289f42009-09-09 15:08:12 +0000304
Chris Lattner2f5693f2009-04-21 05:40:52 +0000305 if (LangOpts.CPlusPlus0x)
306 DefineBuiltinMacro(Buf, "__GXX_EXPERIMENTAL_CXX0X__");
307
308 if (LangOpts.Freestanding)
309 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=0");
310 else
311 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
Mike Stump11289f42009-09-09 15:08:12 +0000312
Chris Lattner2f5693f2009-04-21 05:40:52 +0000313 if (LangOpts.ObjC1) {
314 DefineBuiltinMacro(Buf, "__OBJC__=1");
315 if (LangOpts.ObjCNonFragileABI) {
316 DefineBuiltinMacro(Buf, "__OBJC2__=1");
317 DefineBuiltinMacro(Buf, "OBJC_ZEROCOST_EXCEPTIONS=1");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000318 }
319
320 if (LangOpts.getGCMode() != LangOptions::NonGC)
321 DefineBuiltinMacro(Buf, "__OBJC_GC__=1");
Mike Stump11289f42009-09-09 15:08:12 +0000322
Chris Lattner2f5693f2009-04-21 05:40:52 +0000323 if (LangOpts.NeXTRuntime)
324 DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1");
325 }
Mike Stump11289f42009-09-09 15:08:12 +0000326
Chris Lattner2f5693f2009-04-21 05:40:52 +0000327 // darwin_constant_cfstrings controls this. This is also dependent
328 // on other things like the runtime I believe. This is set even for C code.
329 DefineBuiltinMacro(Buf, "__CONSTANT_CFSTRINGS__=1");
Mike Stump11289f42009-09-09 15:08:12 +0000330
Chris Lattner2f5693f2009-04-21 05:40:52 +0000331 if (LangOpts.ObjC2)
332 DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES");
333
334 if (LangOpts.PascalStrings)
335 DefineBuiltinMacro(Buf, "__PASCAL_STRINGS__");
336
337 if (LangOpts.Blocks) {
338 DefineBuiltinMacro(Buf, "__block=__attribute__((__blocks__(byref)))");
339 DefineBuiltinMacro(Buf, "__BLOCKS__=1");
340 }
Mike Stump11289f42009-09-09 15:08:12 +0000341
Rafael Espindola00a66572009-10-01 13:33:33 +0000342 if (LangOpts.Exceptions)
343 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
344
Chris Lattner2f5693f2009-04-21 05:40:52 +0000345 if (LangOpts.CPlusPlus) {
346 DefineBuiltinMacro(Buf, "__DEPRECATED=1");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000347 DefineBuiltinMacro(Buf, "__GNUG__=4");
348 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
Douglas Gregor3f4d51b2009-08-06 04:09:28 +0000349 if (LangOpts.GNUMode)
350 DefineBuiltinMacro(Buf, "__cplusplus=1");
Mike Stump11289f42009-09-09 15:08:12 +0000351 else
Douglas Gregor3f4d51b2009-08-06 04:09:28 +0000352 // C++ [cpp.predefined]p1:
Mike Stump11289f42009-09-09 15:08:12 +0000353 // The name_ _cplusplusis defined to the value199711Lwhen compiling a
Douglas Gregor3f4d51b2009-08-06 04:09:28 +0000354 // C++ translation unit.
355 DefineBuiltinMacro(Buf, "__cplusplus=199711L");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000356 DefineBuiltinMacro(Buf, "__private_extern__=extern");
Eli Friedman80110172009-08-27 22:01:41 +0000357 // Ugly hack to work with GNU libstdc++.
358 DefineBuiltinMacro(Buf, "_GNU_SOURCE=1");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000359 }
Mike Stump11289f42009-09-09 15:08:12 +0000360
Chris Lattner2f5693f2009-04-21 05:40:52 +0000361 if (LangOpts.Microsoft) {
John Thompson1faca9e2009-10-16 01:12:00 +0000362 // Filter out some microsoft extensions when trying to parse in ms-compat
363 // mode.
Chris Lattner2f5693f2009-04-21 05:40:52 +0000364 DefineBuiltinMacro(Buf, "__int8=__INT8_TYPE__");
365 DefineBuiltinMacro(Buf, "__int16=__INT16_TYPE__");
366 DefineBuiltinMacro(Buf, "__int32=__INT32_TYPE__");
367 DefineBuiltinMacro(Buf, "__int64=__INT64_TYPE__");
Steve Narofff0df20a2009-12-04 21:29:41 +0000368 // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however
369 // VC++ appears to only like __FUNCTION__.
370 DefineBuiltinMacro(Buf, "__PRETTY_FUNCTION__=__FUNCTION__");
John Thompson1faca9e2009-10-16 01:12:00 +0000371 // Work around some issues with Visual C++ headerws.
372 if (LangOpts.CPlusPlus) {
373 // Since we define wchar_t in C++ mode.
374 DefineBuiltinMacro(Buf, "_WCHAR_T_DEFINED=1");
375 DefineBuiltinMacro(Buf, "_NATIVE_WCHAR_T_DEFINED=1");
376 // FIXME: This should be temporary until we have a __pragma
377 // solution, to avoid some errors flagged in VC++ headers.
378 DefineBuiltinMacro(Buf, "_CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES=0");
379 }
Chris Lattner2f5693f2009-04-21 05:40:52 +0000380 }
Mike Stump11289f42009-09-09 15:08:12 +0000381
Chris Lattner2f5693f2009-04-21 05:40:52 +0000382 if (LangOpts.Optimize)
383 DefineBuiltinMacro(Buf, "__OPTIMIZE__=1");
384 if (LangOpts.OptimizeSize)
385 DefineBuiltinMacro(Buf, "__OPTIMIZE_SIZE__=1");
Mike Stump11289f42009-09-09 15:08:12 +0000386
Chris Lattner2f5693f2009-04-21 05:40:52 +0000387 // Initialize target-specific preprocessor defines.
Mike Stump11289f42009-09-09 15:08:12 +0000388
Chris Lattner2f5693f2009-04-21 05:40:52 +0000389 // Define type sizing macros based on the target properties.
390 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
391 DefineBuiltinMacro(Buf, "__CHAR_BIT__=8");
392
Chris Lattner2f5693f2009-04-21 05:40:52 +0000393 DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Buf);
Chris Lattnere4a8c642009-11-05 21:21:32 +0000394 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Buf);
395 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Buf);
396 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Buf);
397 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Buf);
Chris Lattner0fb5bbd2009-11-12 08:04:33 +0000398 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Buf);
Chris Lattnere4a8c642009-11-05 21:21:32 +0000399 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Buf);
Chris Lattner2f5693f2009-04-21 05:40:52 +0000400
Ken Dyck9b25f782009-11-19 13:18:59 +0000401 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Buf);
402 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Buf);
Ken Dyckc0c98292009-11-18 13:52:57 +0000403 DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Buf);
Ken Dyck9b25f782009-11-19 13:18:59 +0000404 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Buf);
Ken Dyck056efe02009-11-19 12:21:52 +0000405 DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Buf);
Ken Dyck9b25f782009-11-19 13:18:59 +0000406 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Buf);
Ken Dyck24cfcf12009-11-18 20:05:48 +0000407 DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Buf);
Chris Lattner2f5693f2009-04-21 05:40:52 +0000408 DefineType("__SIZE_TYPE__", TI.getSizeType(), Buf);
Ken Dyck57512862009-11-19 13:42:09 +0000409 DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Buf);
Chris Lattner2f5693f2009-04-21 05:40:52 +0000410 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Buf);
Ken Dyck0138b9e2009-11-19 15:47:58 +0000411 DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Buf);
Chris Lattner67204922009-10-21 04:59:34 +0000412 DefineType("__WINT_TYPE__", TI.getWIntType(), Buf);
Ken Dycka1f677c2009-11-19 14:16:57 +0000413 DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Buf);
Ken Dyckadc85112009-11-22 15:41:04 +0000414 DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Buf);
Mike Stump11289f42009-09-09 15:08:12 +0000415
Chris Lattner2f5693f2009-04-21 05:40:52 +0000416 DefineFloatMacros(Buf, "FLT", &TI.getFloatFormat());
417 DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat());
418 DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat());
419
420 // Define a __POINTER_WIDTH__ macro for stdint.h.
Kovarththanan Rajaratnamb53c7d42010-01-07 16:01:54 +0000421 char MacroBuf[60];
Chris Lattner2f5693f2009-04-21 05:40:52 +0000422 sprintf(MacroBuf, "__POINTER_WIDTH__=%d", (int)TI.getPointerWidth(0));
423 DefineBuiltinMacro(Buf, MacroBuf);
Mike Stump11289f42009-09-09 15:08:12 +0000424
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000425 if (!LangOpts.CharIsSigned)
Mike Stump11289f42009-09-09 15:08:12 +0000426 DefineBuiltinMacro(Buf, "__CHAR_UNSIGNED__");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000427
Chris Lattner55c98772009-11-12 08:08:27 +0000428 // Define exact-width integer types for stdint.h
429 sprintf(MacroBuf, "__INT%d_TYPE__=char", TI.getCharWidth());
430 DefineBuiltinMacro(Buf, MacroBuf);
Mike Stump11289f42009-09-09 15:08:12 +0000431
Chris Lattner55c98772009-11-12 08:08:27 +0000432 if (TI.getShortWidth() > TI.getCharWidth())
433 DefineExactWidthIntType(TargetInfo::SignedShort, TI, Buf);
434
435 if (TI.getIntWidth() > TI.getShortWidth())
436 DefineExactWidthIntType(TargetInfo::SignedInt, TI, Buf);
437
438 if (TI.getLongWidth() > TI.getIntWidth())
439 DefineExactWidthIntType(TargetInfo::SignedLong, TI, Buf);
440
441 if (TI.getLongLongWidth() > TI.getLongWidth())
442 DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Buf);
443
Chris Lattner2f5693f2009-04-21 05:40:52 +0000444 // Add __builtin_va_list typedef.
445 {
446 const char *VAList = TI.getVAListDeclaration();
447 Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
448 Buf.push_back('\n');
449 }
Mike Stump11289f42009-09-09 15:08:12 +0000450
Chris Lattner2f5693f2009-04-21 05:40:52 +0000451 if (const char *Prefix = TI.getUserLabelPrefix()) {
452 sprintf(MacroBuf, "__USER_LABEL_PREFIX__=%s", Prefix);
453 DefineBuiltinMacro(Buf, MacroBuf);
454 }
Mike Stump11289f42009-09-09 15:08:12 +0000455
Chris Lattner2f5693f2009-04-21 05:40:52 +0000456 // Build configuration options. FIXME: these should be controlled by
457 // command line options or something.
458 DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
459
Chris Lattner2f5693f2009-04-21 05:40:52 +0000460 if (LangOpts.GNUInline)
461 DefineBuiltinMacro(Buf, "__GNUC_GNU_INLINE__=1");
462 else
463 DefineBuiltinMacro(Buf, "__GNUC_STDC_INLINE__=1");
464
465 if (LangOpts.NoInline)
466 DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
467
468 if (unsigned PICLevel = LangOpts.PICLevel) {
469 sprintf(MacroBuf, "__PIC__=%d", PICLevel);
470 DefineBuiltinMacro(Buf, MacroBuf);
471
472 sprintf(MacroBuf, "__pic__=%d", PICLevel);
473 DefineBuiltinMacro(Buf, MacroBuf);
474 }
475
476 // Macros to control C99 numerics and <float.h>
477 DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0");
478 DefineBuiltinMacro(Buf, "__FLT_RADIX__=2");
479 sprintf(MacroBuf, "__DECIMAL_DIG__=%d",
Eli Friedman57406b22009-05-23 03:50:01 +0000480 PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36));
Chris Lattner2f5693f2009-04-21 05:40:52 +0000481 DefineBuiltinMacro(Buf, MacroBuf);
Bill Wendlingd63bbad2009-06-28 07:36:13 +0000482
Bill Wendling18351072009-06-28 23:01:01 +0000483 if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn)
Bill Wendlingd63bbad2009-06-28 07:36:13 +0000484 DefineBuiltinMacro(Buf, "__SSP__=1");
Bill Wendling18351072009-06-28 23:01:01 +0000485 else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq)
Bill Wendlingd63bbad2009-06-28 07:36:13 +0000486 DefineBuiltinMacro(Buf, "__SSP_ALL__=2");
487
Chris Lattner2f5693f2009-04-21 05:40:52 +0000488 // Get other target #defines.
489 TI.getTargetDefines(LangOpts, Buf);
490}
491
Douglas Gregor6ae34ab2009-12-02 16:32:41 +0000492// Initialize the remapping of files to alternative contents, e.g.,
493// those specified through other files.
494static void InitializeFileRemapping(Diagnostic &Diags,
495 SourceManager &SourceMgr,
496 FileManager &FileMgr,
497 const PreprocessorOptions &InitOpts) {
498 // Remap files in the source manager.
499 for (PreprocessorOptions::remapped_file_iterator
500 Remap = InitOpts.remapped_file_begin(),
501 RemapEnd = InitOpts.remapped_file_end();
502 Remap != RemapEnd;
503 ++Remap) {
504 // Find the file that we're mapping to.
505 const FileEntry *ToFile = FileMgr.getFile(Remap->second);
506 if (!ToFile) {
507 Diags.Report(diag::err_fe_remap_missing_to_file)
508 << Remap->first << Remap->second;
509 continue;
510 }
511
Douglas Gregor407e2122009-12-02 18:12:28 +0000512 // Create the file entry for the file that we're mapping from.
513 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
514 ToFile->getSize(),
515 0);
Douglas Gregor6ae34ab2009-12-02 16:32:41 +0000516 if (!FromFile) {
Douglas Gregor6ae34ab2009-12-02 16:32:41 +0000517 Diags.Report(diag::err_fe_remap_missing_from_file)
518 << Remap->first;
519 continue;
520 }
521
522 // Load the contents of the file we're mapping to.
523 std::string ErrorStr;
524 const llvm::MemoryBuffer *Buffer
525 = llvm::MemoryBuffer::getFile(ToFile->getName(), &ErrorStr);
526 if (!Buffer) {
527 Diags.Report(diag::err_fe_error_opening)
528 << Remap->second << ErrorStr;
529 continue;
530 }
531
532 // Override the contents of the "from" file with the contents of
533 // the "to" file.
534 SourceMgr.overrideFileContents(FromFile, Buffer);
535 }
536}
537
Chris Lattner2f5693f2009-04-21 05:40:52 +0000538/// InitializePreprocessor - Initialize the preprocessor getting it and the
539/// environment ready to process a single file. This returns true on error.
540///
Daniel Dunbar181aaee2009-11-04 21:13:15 +0000541void clang::InitializePreprocessor(Preprocessor &PP,
Daniel Dunbar2856ae442009-11-11 21:44:42 +0000542 const PreprocessorOptions &InitOpts,
543 const HeaderSearchOptions &HSOpts) {
Chris Lattner2f5693f2009-04-21 05:40:52 +0000544 std::vector<char> PredefineBuffer;
Mike Stump11289f42009-09-09 15:08:12 +0000545
Douglas Gregor6ae34ab2009-12-02 16:32:41 +0000546 InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(),
547 PP.getFileManager(), InitOpts);
548
Chris Lattnered462a82009-04-22 03:42:19 +0000549 const char *LineDirective = "# 1 \"<built-in>\" 3\n";
550 PredefineBuffer.insert(PredefineBuffer.end(),
551 LineDirective, LineDirective+strlen(LineDirective));
Mike Stump11289f42009-09-09 15:08:12 +0000552
Chris Lattner2f5693f2009-04-21 05:40:52 +0000553 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
Daniel Dunbard6ea9022009-11-17 05:52:41 +0000554 if (InitOpts.UsePredefines)
Chris Lattnere9d7d782009-11-03 19:50:27 +0000555 InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(),
556 PredefineBuffer);
Mike Stump11289f42009-09-09 15:08:12 +0000557
Chris Lattner2f5693f2009-04-21 05:40:52 +0000558 // Add on the predefines from the driver. Wrap in a #line directive to report
559 // that they come from the command line.
Chris Lattnered462a82009-04-22 03:42:19 +0000560 LineDirective = "# 1 \"<command line>\" 1\n";
Chris Lattner2f5693f2009-04-21 05:40:52 +0000561 PredefineBuffer.insert(PredefineBuffer.end(),
562 LineDirective, LineDirective+strlen(LineDirective));
563
564 // Process #define's and #undef's in the order they are given.
Daniel Dunbard6ea9022009-11-17 05:52:41 +0000565 for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
566 if (InitOpts.Macros[i].second) // isUndef
567 UndefineBuiltinMacro(PredefineBuffer, InitOpts.Macros[i].first.c_str());
Chris Lattnere08c43a2009-04-21 06:00:24 +0000568 else
Daniel Dunbar6dac9352009-12-03 09:14:12 +0000569 DefineBuiltinMacro(PredefineBuffer, InitOpts.Macros[i].first.c_str(),
570 &PP.getDiagnostics());
Chris Lattner2f5693f2009-04-21 05:40:52 +0000571 }
572
573 // If -imacros are specified, include them now. These are processed before
574 // any -include directives.
Daniel Dunbard6ea9022009-11-17 05:52:41 +0000575 for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
576 AddImplicitIncludeMacros(PredefineBuffer, InitOpts.MacroIncludes[i]);
Chris Lattner2f5693f2009-04-21 05:40:52 +0000577
578 // Process -include directives.
Daniel Dunbard6ea9022009-11-17 05:52:41 +0000579 for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
580 const std::string &Path = InitOpts.Includes[i];
581 if (Path == InitOpts.ImplicitPTHInclude)
582 AddImplicitIncludePTH(PredefineBuffer, PP, Path);
Chris Lattnere08c43a2009-04-21 06:00:24 +0000583 else
Daniel Dunbard6ea9022009-11-17 05:52:41 +0000584 AddImplicitInclude(PredefineBuffer, Path);
Eli Friedmanb1884552009-06-15 09:57:52 +0000585 }
Chris Lattner2f5693f2009-04-21 05:40:52 +0000586
Rafael Espindolaf8a04a12009-12-01 18:28:16 +0000587 // Exit the command line and go back to <built-in> (2 is LC_LEAVE).
588 LineDirective = "# 1 \"<built-in>\" 2\n";
589 PredefineBuffer.insert(PredefineBuffer.end(),
590 LineDirective, LineDirective+strlen(LineDirective));
591
Chris Lattner2f5693f2009-04-21 05:40:52 +0000592 // Null terminate PredefinedBuffer and add it.
593 PredefineBuffer.push_back(0);
594 PP.setPredefines(&PredefineBuffer[0]);
Daniel Dunbar2856ae442009-11-11 21:44:42 +0000595
596 // Initialize the header search object.
597 ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts,
598 PP.getLangOptions(),
599 PP.getTargetInfo().getTriple());
Chris Lattner2f5693f2009-04-21 05:40:52 +0000600}