blob: 9c42993cf05d35ab8bdd9f34f682746aac12a38e [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"
22#include "llvm/ADT/STLExtras.h"
Douglas Gregor6ae34ab2009-12-02 16:32:41 +000023#include "llvm/Support/MemoryBuffer.h"
Chris Lattner2f5693f2009-04-21 05:40:52 +000024#include "llvm/System/Path.h"
Chris Lattner3fab58d2009-11-02 21:48:09 +000025using namespace clang;
Chris Lattner2f5693f2009-04-21 05:40:52 +000026
27// Append a #define line to Buf for Macro. Macro should be of the form XXX,
28// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
29// "#define XXX Y z W". To get a #define with no value, use "XXX=".
Chris Lattner5e4702d2009-06-04 16:47:09 +000030static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro) {
31 const char *Command = "#define ";
Chris Lattner2f5693f2009-04-21 05:40:52 +000032 Buf.insert(Buf.end(), Command, Command+strlen(Command));
33 if (const char *Equal = strchr(Macro, '=')) {
34 // Turn the = into ' '.
35 Buf.insert(Buf.end(), Macro, Equal);
36 Buf.push_back(' ');
Mike Stump11289f42009-09-09 15:08:12 +000037
Chris Lattner2f5693f2009-04-21 05:40:52 +000038 // Per GCC -D semantics, the macro ends at \n if it exists.
39 const char *End = strpbrk(Equal, "\n\r");
40 if (End) {
41 fprintf(stderr, "warning: macro '%s' contains embedded newline, text "
42 "after the newline is ignored.\n",
43 std::string(Macro, Equal).c_str());
44 } else {
45 End = Equal+strlen(Equal);
46 }
Mike Stump11289f42009-09-09 15:08:12 +000047
Chris Lattner2f5693f2009-04-21 05:40:52 +000048 Buf.insert(Buf.end(), Equal+1, End);
49 } else {
50 // Push "macroname 1".
51 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
52 Buf.push_back(' ');
53 Buf.push_back('1');
54 }
55 Buf.push_back('\n');
56}
57
Chris Lattner4579b762009-05-15 16:08:43 +000058// Append a #undef line to Buf for Macro. Macro should be of the form XXX
59// and we emit "#undef XXX".
60static void UndefineBuiltinMacro(std::vector<char> &Buf, const char *Macro) {
61 // Push "macroname".
62 const char *Command = "#undef ";
63 Buf.insert(Buf.end(), Command, Command+strlen(Command));
64 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
65 Buf.push_back('\n');
66}
67
Daniel Dunbar732ef8a2009-11-11 23:58:53 +000068std::string clang::NormalizeDashIncludePath(llvm::StringRef File) {
Daniel Dunbar66cb0132009-04-22 08:53:01 +000069 // Implicit include paths should be resolved relative to the current
70 // working directory first, and then use the regular header search
71 // mechanism. The proper way to handle this is to have the
72 // predefines buffer located at the current working directory, but
73 // it has not file entry. For now, workaround this by using an
74 // absolute path if we find the file here, and otherwise letting
75 // header search handle it.
Chris Lattner2f5693f2009-04-21 05:40:52 +000076 llvm::sys::Path Path(File);
77 Path.makeAbsolute();
Daniel Dunbar66cb0132009-04-22 08:53:01 +000078 if (!Path.exists())
79 Path = File;
Mike Stump11289f42009-09-09 15:08:12 +000080
Daniel Dunbar732ef8a2009-11-11 23:58:53 +000081 return Lexer::Stringify(Path.str());
82}
83
84/// Add the quoted name of an implicit include file.
85static void AddQuotedIncludePath(std::vector<char> &Buf,
86 const std::string &File) {
87
Chris Lattner2f5693f2009-04-21 05:40:52 +000088 // Escape double quotes etc.
89 Buf.push_back('"');
Daniel Dunbar732ef8a2009-11-11 23:58:53 +000090 std::string EscapedFile = NormalizeDashIncludePath(File);
Chris Lattner2f5693f2009-04-21 05:40:52 +000091 Buf.insert(Buf.end(), EscapedFile.begin(), EscapedFile.end());
92 Buf.push_back('"');
93}
94
95/// AddImplicitInclude - Add an implicit #include of the specified file to the
96/// predefines buffer.
Mike Stump11289f42009-09-09 15:08:12 +000097static void AddImplicitInclude(std::vector<char> &Buf,
Chris Lattner2f5693f2009-04-21 05:40:52 +000098 const std::string &File) {
99 const char *Inc = "#include ";
100 Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
101 AddQuotedIncludePath(Buf, File);
102 Buf.push_back('\n');
103}
104
105static void AddImplicitIncludeMacros(std::vector<char> &Buf,
106 const std::string &File) {
107 const char *Inc = "#__include_macros ";
108 Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
109 AddQuotedIncludePath(Buf, File);
110 Buf.push_back('\n');
111 // Marker token to stop the __include_macros fetch loop.
112 const char *Marker = "##\n"; // ##?
113 Buf.insert(Buf.end(), Marker, Marker+strlen(Marker));
114}
115
116/// AddImplicitIncludePTH - Add an implicit #include using the original file
117/// used to generate a PTH cache.
Mike Stump11289f42009-09-09 15:08:12 +0000118static void AddImplicitIncludePTH(std::vector<char> &Buf, Preprocessor &PP,
Chris Lattner2f5693f2009-04-21 05:40:52 +0000119 const std::string& ImplicitIncludePTH) {
120 PTHManager *P = PP.getPTHManager();
121 assert(P && "No PTHManager.");
122 const char *OriginalFile = P->getOriginalSourceFile();
Mike Stump11289f42009-09-09 15:08:12 +0000123
Chris Lattner2f5693f2009-04-21 05:40:52 +0000124 if (!OriginalFile) {
125 assert(!ImplicitIncludePTH.empty());
126 fprintf(stderr, "error: PTH file '%s' does not designate an original "
127 "source header file for -include-pth\n",
128 ImplicitIncludePTH.c_str());
129 exit (1);
130 }
Mike Stump11289f42009-09-09 15:08:12 +0000131
Chris Lattner2f5693f2009-04-21 05:40:52 +0000132 AddImplicitInclude(Buf, OriginalFile);
133}
134
135/// PickFP - This is used to pick a value based on the FP semantics of the
136/// specified FP model.
137template <typename T>
138static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal,
Eli Friedman57406b22009-05-23 03:50:01 +0000139 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
140 T IEEEQuadVal) {
Duncan Sandsf4063512009-06-03 14:28:20 +0000141 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle)
Chris Lattner2f5693f2009-04-21 05:40:52 +0000142 return IEEESingleVal;
Duncan Sandsf4063512009-06-03 14:28:20 +0000143 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble)
Chris Lattner2f5693f2009-04-21 05:40:52 +0000144 return IEEEDoubleVal;
Duncan Sandsf4063512009-06-03 14:28:20 +0000145 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended)
Chris Lattner2f5693f2009-04-21 05:40:52 +0000146 return X87DoubleExtendedVal;
Duncan Sandsf4063512009-06-03 14:28:20 +0000147 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble)
Eli Friedman57406b22009-05-23 03:50:01 +0000148 return PPCDoubleDoubleVal;
Duncan Sandsf4063512009-06-03 14:28:20 +0000149 assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad);
Eli Friedman57406b22009-05-23 03:50:01 +0000150 return IEEEQuadVal;
Chris Lattner2f5693f2009-04-21 05:40:52 +0000151}
152
153static void DefineFloatMacros(std::vector<char> &Buf, const char *Prefix,
154 const llvm::fltSemantics *Sem) {
155 const char *DenormMin, *Epsilon, *Max, *Min;
Mike Stump11289f42009-09-09 15:08:12 +0000156 DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324",
Chris Lattner2f5693f2009-04-21 05:40:52 +0000157 "3.64519953188247460253e-4951L",
Eli Friedman57406b22009-05-23 03:50:01 +0000158 "4.94065645841246544176568792868221e-324L",
159 "6.47517511943802511092443895822764655e-4966L");
160 int Digits = PickFP(Sem, 6, 15, 18, 31, 33);
Chris Lattner2f5693f2009-04-21 05:40:52 +0000161 Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16",
162 "1.08420217248550443401e-19L",
Eli Friedman57406b22009-05-23 03:50:01 +0000163 "4.94065645841246544176568792868221e-324L",
164 "1.92592994438723585305597794258492732e-34L");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000165 int HasInifinity = 1, HasQuietNaN = 1;
Eli Friedman57406b22009-05-23 03:50:01 +0000166 int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113);
167 int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931);
168 int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932);
169 int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381);
170 int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384);
Chris Lattner2f5693f2009-04-21 05:40:52 +0000171 Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308",
172 "3.36210314311209350626e-4932L",
Eli Friedman57406b22009-05-23 03:50:01 +0000173 "2.00416836000897277799610805135016e-292L",
174 "3.36210314311209350626267781732175260e-4932L");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000175 Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308",
176 "1.18973149535723176502e+4932L",
Eli Friedman57406b22009-05-23 03:50:01 +0000177 "1.79769313486231580793728971405301e+308L",
178 "1.18973149535723176508575932662800702e+4932L");
Mike Stump11289f42009-09-09 15:08:12 +0000179
Eli Friedman57406b22009-05-23 03:50:01 +0000180 char MacroBuf[100];
Chris Lattner2f5693f2009-04-21 05:40:52 +0000181 sprintf(MacroBuf, "__%s_DENORM_MIN__=%s", Prefix, DenormMin);
182 DefineBuiltinMacro(Buf, MacroBuf);
183 sprintf(MacroBuf, "__%s_DIG__=%d", Prefix, Digits);
184 DefineBuiltinMacro(Buf, MacroBuf);
185 sprintf(MacroBuf, "__%s_EPSILON__=%s", Prefix, Epsilon);
186 DefineBuiltinMacro(Buf, MacroBuf);
187 sprintf(MacroBuf, "__%s_HAS_INFINITY__=%d", Prefix, HasInifinity);
188 DefineBuiltinMacro(Buf, MacroBuf);
189 sprintf(MacroBuf, "__%s_HAS_QUIET_NAN__=%d", Prefix, HasQuietNaN);
190 DefineBuiltinMacro(Buf, MacroBuf);
191 sprintf(MacroBuf, "__%s_MANT_DIG__=%d", Prefix, MantissaDigits);
192 DefineBuiltinMacro(Buf, MacroBuf);
193 sprintf(MacroBuf, "__%s_MAX_10_EXP__=%d", Prefix, Max10Exp);
194 DefineBuiltinMacro(Buf, MacroBuf);
195 sprintf(MacroBuf, "__%s_MAX_EXP__=%d", Prefix, MaxExp);
196 DefineBuiltinMacro(Buf, MacroBuf);
197 sprintf(MacroBuf, "__%s_MAX__=%s", Prefix, Max);
198 DefineBuiltinMacro(Buf, MacroBuf);
199 sprintf(MacroBuf, "__%s_MIN_10_EXP__=(%d)", Prefix, Min10Exp);
200 DefineBuiltinMacro(Buf, MacroBuf);
201 sprintf(MacroBuf, "__%s_MIN_EXP__=(%d)", Prefix, MinExp);
202 DefineBuiltinMacro(Buf, MacroBuf);
203 sprintf(MacroBuf, "__%s_MIN__=%s", Prefix, Min);
204 DefineBuiltinMacro(Buf, MacroBuf);
205 sprintf(MacroBuf, "__%s_HAS_DENORM__=1", Prefix);
206 DefineBuiltinMacro(Buf, MacroBuf);
207}
208
209
210/// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
211/// named MacroName with the max value for a type with width 'TypeWidth' a
212/// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
213static void DefineTypeSize(const char *MacroName, unsigned TypeWidth,
214 const char *ValSuffix, bool isSigned,
215 std::vector<char> &Buf) {
216 char MacroBuf[60];
217 long long MaxVal;
218 if (isSigned)
219 MaxVal = (1LL << (TypeWidth - 1)) - 1;
220 else
221 MaxVal = ~0LL >> (64-TypeWidth);
Mike Stump11289f42009-09-09 15:08:12 +0000222
Daniel Dunbarfe07aec2009-09-03 19:23:49 +0000223 // FIXME: Switch to using raw_ostream and avoid utostr().
224 sprintf(MacroBuf, "%s=%s%s", MacroName, llvm::utostr(MaxVal).c_str(),
225 ValSuffix);
Chris Lattner2f5693f2009-04-21 05:40:52 +0000226 DefineBuiltinMacro(Buf, MacroBuf);
227}
228
Chris Lattnere4a8c642009-11-05 21:21:32 +0000229/// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
230/// the width, suffix, and signedness of the given type
231static void DefineTypeSize(const char *MacroName, TargetInfo::IntType Ty,
232 const TargetInfo &TI, std::vector<char> &Buf) {
233 DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
234 TI.isTypeSigned(Ty), Buf);
235}
236
Chris Lattner2f5693f2009-04-21 05:40:52 +0000237static void DefineType(const char *MacroName, TargetInfo::IntType Ty,
238 std::vector<char> &Buf) {
239 char MacroBuf[60];
240 sprintf(MacroBuf, "%s=%s", MacroName, TargetInfo::getTypeName(Ty));
241 DefineBuiltinMacro(Buf, MacroBuf);
242}
243
Ken Dyckc0c98292009-11-18 13:52:57 +0000244static void DefineTypeWidth(const char *MacroName, TargetInfo::IntType Ty,
245 const TargetInfo &TI, std::vector<char> &Buf) {
246 char MacroBuf[60];
247 sprintf(MacroBuf, "%s=%d", MacroName, TI.getTypeWidth(Ty));
248 DefineBuiltinMacro(Buf, MacroBuf);
249}
250
Chris Lattner55c98772009-11-12 08:08:27 +0000251static void DefineExactWidthIntType(TargetInfo::IntType Ty,
252 const TargetInfo &TI, std::vector<char> &Buf) {
253 char MacroBuf[60];
Ken Dyck2dc8d5f2009-11-16 16:36:33 +0000254 int TypeWidth = TI.getTypeWidth(Ty);
255 sprintf(MacroBuf, "__INT%d_TYPE__", TypeWidth);
Chris Lattner55c98772009-11-12 08:08:27 +0000256 DefineType(MacroBuf, Ty, Buf);
Ken Dyck2dc8d5f2009-11-16 16:36:33 +0000257
258
259 const char *ConstSuffix = TargetInfo::getTypeConstantSuffix(Ty);
260 if (strlen(ConstSuffix) > 0) {
261 sprintf(MacroBuf, "__INT%d_C_SUFFIX__=%s", TypeWidth, ConstSuffix);
262 DefineBuiltinMacro(Buf, MacroBuf);
263 }
Chris Lattner55c98772009-11-12 08:08:27 +0000264}
Chris Lattner2f5693f2009-04-21 05:40:52 +0000265
266static void InitializePredefinedMacros(const TargetInfo &TI,
267 const LangOptions &LangOpts,
268 std::vector<char> &Buf) {
269 char MacroBuf[60];
270 // Compiler version introspection macros.
271 DefineBuiltinMacro(Buf, "__llvm__=1"); // LLVM Backend
272 DefineBuiltinMacro(Buf, "__clang__=1"); // Clang Frontend
Mike Stump11289f42009-09-09 15:08:12 +0000273
Chris Lattner2f5693f2009-04-21 05:40:52 +0000274 // Currently claim to be compatible with GCC 4.2.1-5621.
Chris Lattner2f5693f2009-04-21 05:40:52 +0000275 DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2");
276 DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
277 DefineBuiltinMacro(Buf, "__GNUC__=4");
278 DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
279 DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 Compatible Clang Compiler\"");
Mike Stump11289f42009-09-09 15:08:12 +0000280
281
Chris Lattner2f5693f2009-04-21 05:40:52 +0000282 // Initialize language-specific preprocessor defines.
Mike Stump11289f42009-09-09 15:08:12 +0000283
Chris Lattner2f5693f2009-04-21 05:40:52 +0000284 // These should all be defined in the preprocessor according to the
285 // current language configuration.
286 if (!LangOpts.Microsoft)
287 DefineBuiltinMacro(Buf, "__STDC__=1");
288 if (LangOpts.AsmPreprocessor)
289 DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
Ryan Flynned73cac2009-07-21 00:07:02 +0000290
291 if (!LangOpts.CPlusPlus) {
292 if (LangOpts.C99)
293 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
294 else if (!LangOpts.GNUMode && LangOpts.Digraphs)
295 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
296 }
Chris Lattner2f5693f2009-04-21 05:40:52 +0000297
298 // Standard conforming mode?
299 if (!LangOpts.GNUMode)
300 DefineBuiltinMacro(Buf, "__STRICT_ANSI__=1");
Mike Stump11289f42009-09-09 15:08:12 +0000301
Chris Lattner2f5693f2009-04-21 05:40:52 +0000302 if (LangOpts.CPlusPlus0x)
303 DefineBuiltinMacro(Buf, "__GXX_EXPERIMENTAL_CXX0X__");
304
305 if (LangOpts.Freestanding)
306 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=0");
307 else
308 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
Mike Stump11289f42009-09-09 15:08:12 +0000309
Chris Lattner2f5693f2009-04-21 05:40:52 +0000310 if (LangOpts.ObjC1) {
311 DefineBuiltinMacro(Buf, "__OBJC__=1");
312 if (LangOpts.ObjCNonFragileABI) {
313 DefineBuiltinMacro(Buf, "__OBJC2__=1");
314 DefineBuiltinMacro(Buf, "OBJC_ZEROCOST_EXCEPTIONS=1");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000315 }
316
317 if (LangOpts.getGCMode() != LangOptions::NonGC)
318 DefineBuiltinMacro(Buf, "__OBJC_GC__=1");
Mike Stump11289f42009-09-09 15:08:12 +0000319
Chris Lattner2f5693f2009-04-21 05:40:52 +0000320 if (LangOpts.NeXTRuntime)
321 DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1");
322 }
Mike Stump11289f42009-09-09 15:08:12 +0000323
Chris Lattner2f5693f2009-04-21 05:40:52 +0000324 // darwin_constant_cfstrings controls this. This is also dependent
325 // on other things like the runtime I believe. This is set even for C code.
326 DefineBuiltinMacro(Buf, "__CONSTANT_CFSTRINGS__=1");
Mike Stump11289f42009-09-09 15:08:12 +0000327
Chris Lattner2f5693f2009-04-21 05:40:52 +0000328 if (LangOpts.ObjC2)
329 DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES");
330
331 if (LangOpts.PascalStrings)
332 DefineBuiltinMacro(Buf, "__PASCAL_STRINGS__");
333
334 if (LangOpts.Blocks) {
335 DefineBuiltinMacro(Buf, "__block=__attribute__((__blocks__(byref)))");
336 DefineBuiltinMacro(Buf, "__BLOCKS__=1");
337 }
Mike Stump11289f42009-09-09 15:08:12 +0000338
Rafael Espindola00a66572009-10-01 13:33:33 +0000339 if (LangOpts.Exceptions)
340 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
341
Chris Lattner2f5693f2009-04-21 05:40:52 +0000342 if (LangOpts.CPlusPlus) {
343 DefineBuiltinMacro(Buf, "__DEPRECATED=1");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000344 DefineBuiltinMacro(Buf, "__GNUG__=4");
345 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
Douglas Gregor3f4d51b2009-08-06 04:09:28 +0000346 if (LangOpts.GNUMode)
347 DefineBuiltinMacro(Buf, "__cplusplus=1");
Mike Stump11289f42009-09-09 15:08:12 +0000348 else
Douglas Gregor3f4d51b2009-08-06 04:09:28 +0000349 // C++ [cpp.predefined]p1:
Mike Stump11289f42009-09-09 15:08:12 +0000350 // The name_ _cplusplusis defined to the value199711Lwhen compiling a
Douglas Gregor3f4d51b2009-08-06 04:09:28 +0000351 // C++ translation unit.
352 DefineBuiltinMacro(Buf, "__cplusplus=199711L");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000353 DefineBuiltinMacro(Buf, "__private_extern__=extern");
Eli Friedman80110172009-08-27 22:01:41 +0000354 // Ugly hack to work with GNU libstdc++.
355 DefineBuiltinMacro(Buf, "_GNU_SOURCE=1");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000356 }
Mike Stump11289f42009-09-09 15:08:12 +0000357
Chris Lattner2f5693f2009-04-21 05:40:52 +0000358 if (LangOpts.Microsoft) {
John Thompson1faca9e2009-10-16 01:12:00 +0000359 // Filter out some microsoft extensions when trying to parse in ms-compat
360 // mode.
Chris Lattner2f5693f2009-04-21 05:40:52 +0000361 DefineBuiltinMacro(Buf, "__int8=__INT8_TYPE__");
362 DefineBuiltinMacro(Buf, "__int16=__INT16_TYPE__");
363 DefineBuiltinMacro(Buf, "__int32=__INT32_TYPE__");
364 DefineBuiltinMacro(Buf, "__int64=__INT64_TYPE__");
John Thompson1faca9e2009-10-16 01:12:00 +0000365 // Work around some issues with Visual C++ headerws.
366 if (LangOpts.CPlusPlus) {
367 // Since we define wchar_t in C++ mode.
368 DefineBuiltinMacro(Buf, "_WCHAR_T_DEFINED=1");
369 DefineBuiltinMacro(Buf, "_NATIVE_WCHAR_T_DEFINED=1");
370 // FIXME: This should be temporary until we have a __pragma
371 // solution, to avoid some errors flagged in VC++ headers.
372 DefineBuiltinMacro(Buf, "_CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES=0");
373 }
Chris Lattner2f5693f2009-04-21 05:40:52 +0000374 }
Mike Stump11289f42009-09-09 15:08:12 +0000375
Chris Lattner2f5693f2009-04-21 05:40:52 +0000376 if (LangOpts.Optimize)
377 DefineBuiltinMacro(Buf, "__OPTIMIZE__=1");
378 if (LangOpts.OptimizeSize)
379 DefineBuiltinMacro(Buf, "__OPTIMIZE_SIZE__=1");
Mike Stump11289f42009-09-09 15:08:12 +0000380
Chris Lattner2f5693f2009-04-21 05:40:52 +0000381 // Initialize target-specific preprocessor defines.
Mike Stump11289f42009-09-09 15:08:12 +0000382
Chris Lattner2f5693f2009-04-21 05:40:52 +0000383 // Define type sizing macros based on the target properties.
384 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
385 DefineBuiltinMacro(Buf, "__CHAR_BIT__=8");
386
Chris Lattner2f5693f2009-04-21 05:40:52 +0000387 DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Buf);
Chris Lattnere4a8c642009-11-05 21:21:32 +0000388 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Buf);
389 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Buf);
390 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Buf);
391 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Buf);
Chris Lattner0fb5bbd2009-11-12 08:04:33 +0000392 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Buf);
Chris Lattnere4a8c642009-11-05 21:21:32 +0000393 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Buf);
Chris Lattner2f5693f2009-04-21 05:40:52 +0000394
Ken Dyck9b25f782009-11-19 13:18:59 +0000395 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Buf);
396 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Buf);
Ken Dyckc0c98292009-11-18 13:52:57 +0000397 DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Buf);
Ken Dyck9b25f782009-11-19 13:18:59 +0000398 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Buf);
Ken Dyck056efe02009-11-19 12:21:52 +0000399 DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Buf);
Ken Dyck9b25f782009-11-19 13:18:59 +0000400 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Buf);
Ken Dyck24cfcf12009-11-18 20:05:48 +0000401 DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Buf);
Chris Lattner2f5693f2009-04-21 05:40:52 +0000402 DefineType("__SIZE_TYPE__", TI.getSizeType(), Buf);
Ken Dyck57512862009-11-19 13:42:09 +0000403 DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Buf);
Chris Lattner2f5693f2009-04-21 05:40:52 +0000404 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Buf);
Ken Dyck0138b9e2009-11-19 15:47:58 +0000405 DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Buf);
Chris Lattner67204922009-10-21 04:59:34 +0000406 DefineType("__WINT_TYPE__", TI.getWIntType(), Buf);
Ken Dycka1f677c2009-11-19 14:16:57 +0000407 DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Buf);
Ken Dyckadc85112009-11-22 15:41:04 +0000408 DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Buf);
Mike Stump11289f42009-09-09 15:08:12 +0000409
Chris Lattner2f5693f2009-04-21 05:40:52 +0000410 DefineFloatMacros(Buf, "FLT", &TI.getFloatFormat());
411 DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat());
412 DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat());
413
414 // Define a __POINTER_WIDTH__ macro for stdint.h.
415 sprintf(MacroBuf, "__POINTER_WIDTH__=%d", (int)TI.getPointerWidth(0));
416 DefineBuiltinMacro(Buf, MacroBuf);
Mike Stump11289f42009-09-09 15:08:12 +0000417
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000418 if (!LangOpts.CharIsSigned)
Mike Stump11289f42009-09-09 15:08:12 +0000419 DefineBuiltinMacro(Buf, "__CHAR_UNSIGNED__");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000420
Chris Lattner55c98772009-11-12 08:08:27 +0000421 // Define exact-width integer types for stdint.h
422 sprintf(MacroBuf, "__INT%d_TYPE__=char", TI.getCharWidth());
423 DefineBuiltinMacro(Buf, MacroBuf);
Mike Stump11289f42009-09-09 15:08:12 +0000424
Chris Lattner55c98772009-11-12 08:08:27 +0000425 if (TI.getShortWidth() > TI.getCharWidth())
426 DefineExactWidthIntType(TargetInfo::SignedShort, TI, Buf);
427
428 if (TI.getIntWidth() > TI.getShortWidth())
429 DefineExactWidthIntType(TargetInfo::SignedInt, TI, Buf);
430
431 if (TI.getLongWidth() > TI.getIntWidth())
432 DefineExactWidthIntType(TargetInfo::SignedLong, TI, Buf);
433
434 if (TI.getLongLongWidth() > TI.getLongWidth())
435 DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Buf);
436
Chris Lattner2f5693f2009-04-21 05:40:52 +0000437 // Add __builtin_va_list typedef.
438 {
439 const char *VAList = TI.getVAListDeclaration();
440 Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
441 Buf.push_back('\n');
442 }
Mike Stump11289f42009-09-09 15:08:12 +0000443
Chris Lattner2f5693f2009-04-21 05:40:52 +0000444 if (const char *Prefix = TI.getUserLabelPrefix()) {
445 sprintf(MacroBuf, "__USER_LABEL_PREFIX__=%s", Prefix);
446 DefineBuiltinMacro(Buf, MacroBuf);
447 }
Mike Stump11289f42009-09-09 15:08:12 +0000448
Chris Lattner2f5693f2009-04-21 05:40:52 +0000449 // Build configuration options. FIXME: these should be controlled by
450 // command line options or something.
451 DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
452
Chris Lattner2f5693f2009-04-21 05:40:52 +0000453 if (LangOpts.GNUInline)
454 DefineBuiltinMacro(Buf, "__GNUC_GNU_INLINE__=1");
455 else
456 DefineBuiltinMacro(Buf, "__GNUC_STDC_INLINE__=1");
457
458 if (LangOpts.NoInline)
459 DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
460
461 if (unsigned PICLevel = LangOpts.PICLevel) {
462 sprintf(MacroBuf, "__PIC__=%d", PICLevel);
463 DefineBuiltinMacro(Buf, MacroBuf);
464
465 sprintf(MacroBuf, "__pic__=%d", PICLevel);
466 DefineBuiltinMacro(Buf, MacroBuf);
467 }
468
469 // Macros to control C99 numerics and <float.h>
470 DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0");
471 DefineBuiltinMacro(Buf, "__FLT_RADIX__=2");
472 sprintf(MacroBuf, "__DECIMAL_DIG__=%d",
Eli Friedman57406b22009-05-23 03:50:01 +0000473 PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36));
Chris Lattner2f5693f2009-04-21 05:40:52 +0000474 DefineBuiltinMacro(Buf, MacroBuf);
Bill Wendlingd63bbad2009-06-28 07:36:13 +0000475
Bill Wendling18351072009-06-28 23:01:01 +0000476 if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn)
Bill Wendlingd63bbad2009-06-28 07:36:13 +0000477 DefineBuiltinMacro(Buf, "__SSP__=1");
Bill Wendling18351072009-06-28 23:01:01 +0000478 else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq)
Bill Wendlingd63bbad2009-06-28 07:36:13 +0000479 DefineBuiltinMacro(Buf, "__SSP_ALL__=2");
480
Chris Lattner2f5693f2009-04-21 05:40:52 +0000481 // Get other target #defines.
482 TI.getTargetDefines(LangOpts, Buf);
483}
484
Douglas Gregor6ae34ab2009-12-02 16:32:41 +0000485// Initialize the remapping of files to alternative contents, e.g.,
486// those specified through other files.
487static void InitializeFileRemapping(Diagnostic &Diags,
488 SourceManager &SourceMgr,
489 FileManager &FileMgr,
490 const PreprocessorOptions &InitOpts) {
491 // Remap files in the source manager.
492 for (PreprocessorOptions::remapped_file_iterator
493 Remap = InitOpts.remapped_file_begin(),
494 RemapEnd = InitOpts.remapped_file_end();
495 Remap != RemapEnd;
496 ++Remap) {
497 // Find the file that we're mapping to.
498 const FileEntry *ToFile = FileMgr.getFile(Remap->second);
499 if (!ToFile) {
500 Diags.Report(diag::err_fe_remap_missing_to_file)
501 << Remap->first << Remap->second;
502 continue;
503 }
504
505 // Find the file that we're mapping from.
506 const FileEntry *FromFile = FileMgr.getFile(Remap->first);
507 if (!FromFile) {
508 // FIXME: We could actually recover from this, by faking a
509 // FileEntry based on the "ToFile".
510 Diags.Report(diag::err_fe_remap_missing_from_file)
511 << Remap->first;
512 continue;
513 }
514
515 // Load the contents of the file we're mapping to.
516 std::string ErrorStr;
517 const llvm::MemoryBuffer *Buffer
518 = llvm::MemoryBuffer::getFile(ToFile->getName(), &ErrorStr);
519 if (!Buffer) {
520 Diags.Report(diag::err_fe_error_opening)
521 << Remap->second << ErrorStr;
522 continue;
523 }
524
525 // Override the contents of the "from" file with the contents of
526 // the "to" file.
527 SourceMgr.overrideFileContents(FromFile, Buffer);
528 }
529}
530
Chris Lattner2f5693f2009-04-21 05:40:52 +0000531/// InitializePreprocessor - Initialize the preprocessor getting it and the
532/// environment ready to process a single file. This returns true on error.
533///
Daniel Dunbar181aaee2009-11-04 21:13:15 +0000534void clang::InitializePreprocessor(Preprocessor &PP,
Daniel Dunbar2856ae442009-11-11 21:44:42 +0000535 const PreprocessorOptions &InitOpts,
536 const HeaderSearchOptions &HSOpts) {
Chris Lattner2f5693f2009-04-21 05:40:52 +0000537 std::vector<char> PredefineBuffer;
Mike Stump11289f42009-09-09 15:08:12 +0000538
Douglas Gregor6ae34ab2009-12-02 16:32:41 +0000539 InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(),
540 PP.getFileManager(), InitOpts);
541
Chris Lattnered462a82009-04-22 03:42:19 +0000542 const char *LineDirective = "# 1 \"<built-in>\" 3\n";
543 PredefineBuffer.insert(PredefineBuffer.end(),
544 LineDirective, LineDirective+strlen(LineDirective));
Mike Stump11289f42009-09-09 15:08:12 +0000545
Chris Lattner2f5693f2009-04-21 05:40:52 +0000546 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
Daniel Dunbard6ea9022009-11-17 05:52:41 +0000547 if (InitOpts.UsePredefines)
Chris Lattnere9d7d782009-11-03 19:50:27 +0000548 InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(),
549 PredefineBuffer);
Mike Stump11289f42009-09-09 15:08:12 +0000550
Chris Lattner2f5693f2009-04-21 05:40:52 +0000551 // Add on the predefines from the driver. Wrap in a #line directive to report
552 // that they come from the command line.
Chris Lattnered462a82009-04-22 03:42:19 +0000553 LineDirective = "# 1 \"<command line>\" 1\n";
Chris Lattner2f5693f2009-04-21 05:40:52 +0000554 PredefineBuffer.insert(PredefineBuffer.end(),
555 LineDirective, LineDirective+strlen(LineDirective));
556
557 // Process #define's and #undef's in the order they are given.
Daniel Dunbard6ea9022009-11-17 05:52:41 +0000558 for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
559 if (InitOpts.Macros[i].second) // isUndef
560 UndefineBuiltinMacro(PredefineBuffer, InitOpts.Macros[i].first.c_str());
Chris Lattnere08c43a2009-04-21 06:00:24 +0000561 else
Daniel Dunbard6ea9022009-11-17 05:52:41 +0000562 DefineBuiltinMacro(PredefineBuffer, InitOpts.Macros[i].first.c_str());
Chris Lattner2f5693f2009-04-21 05:40:52 +0000563 }
564
565 // If -imacros are specified, include them now. These are processed before
566 // any -include directives.
Daniel Dunbard6ea9022009-11-17 05:52:41 +0000567 for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
568 AddImplicitIncludeMacros(PredefineBuffer, InitOpts.MacroIncludes[i]);
Chris Lattner2f5693f2009-04-21 05:40:52 +0000569
570 // Process -include directives.
Daniel Dunbard6ea9022009-11-17 05:52:41 +0000571 for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
572 const std::string &Path = InitOpts.Includes[i];
573 if (Path == InitOpts.ImplicitPTHInclude)
574 AddImplicitIncludePTH(PredefineBuffer, PP, Path);
Chris Lattnere08c43a2009-04-21 06:00:24 +0000575 else
Daniel Dunbard6ea9022009-11-17 05:52:41 +0000576 AddImplicitInclude(PredefineBuffer, Path);
Eli Friedmanb1884552009-06-15 09:57:52 +0000577 }
Chris Lattner2f5693f2009-04-21 05:40:52 +0000578
Rafael Espindolaf8a04a12009-12-01 18:28:16 +0000579 // Exit the command line and go back to <built-in> (2 is LC_LEAVE).
580 LineDirective = "# 1 \"<built-in>\" 2\n";
581 PredefineBuffer.insert(PredefineBuffer.end(),
582 LineDirective, LineDirective+strlen(LineDirective));
583
Chris Lattner2f5693f2009-04-21 05:40:52 +0000584 // Null terminate PredefinedBuffer and add it.
585 PredefineBuffer.push_back(0);
586 PP.setPredefines(&PredefineBuffer[0]);
Daniel Dunbar2856ae442009-11-11 21:44:42 +0000587
588 // Initialize the header search object.
589 ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts,
590 PP.getLangOptions(),
591 PP.getTargetInfo().getTriple());
Chris Lattner2f5693f2009-04-21 05:40:52 +0000592}