blob: c1fc92d3b0c9cb36fab9a2110332e2d6617960fe [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
Daniel Dunbar8863b982009-11-07 04:20:15 +000014#include "clang/Frontend/Utils.h"
Chris Lattnere116ccf2009-04-21 05:40:52 +000015#include "clang/Basic/TargetInfo.h"
Douglas Gregor2973c0e2009-12-02 16:32:41 +000016#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar8863b982009-11-07 04:20:15 +000017#include "clang/Frontend/PreprocessorOptions.h"
Chris Lattnere116ccf2009-04-21 05:40:52 +000018#include "clang/Lex/Preprocessor.h"
Douglas Gregor2973c0e2009-12-02 16:32:41 +000019#include "clang/Basic/FileManager.h"
20#include "clang/Basic/SourceManager.h"
Chris Lattnere116ccf2009-04-21 05:40:52 +000021#include "llvm/ADT/StringExtras.h"
Douglas Gregor057e5672009-12-02 18:12:28 +000022#include "llvm/ADT/StringMap.h"
Chris Lattnere116ccf2009-04-21 05:40:52 +000023#include "llvm/ADT/STLExtras.h"
Douglas Gregor2973c0e2009-12-02 16:32:41 +000024#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere116ccf2009-04-21 05:40:52 +000025#include "llvm/System/Path.h"
Chris Lattner47c06ee2009-11-02 21:48:09 +000026using namespace clang;
Chris Lattnere116ccf2009-04-21 05:40:52 +000027
28// Append a #define line to Buf for Macro. Macro should be of the form XXX,
29// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
30// "#define XXX Y z W". To get a #define with no value, use "XXX=".
Daniel Dunbarbaac1032009-12-03 09:14:12 +000031static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
32 Diagnostic *Diags = 0) {
Chris Lattner810dc542009-06-04 16:47:09 +000033 const char *Command = "#define ";
Chris Lattnere116ccf2009-04-21 05:40:52 +000034 Buf.insert(Buf.end(), Command, Command+strlen(Command));
35 if (const char *Equal = strchr(Macro, '=')) {
36 // Turn the = into ' '.
37 Buf.insert(Buf.end(), Macro, Equal);
38 Buf.push_back(' ');
Mike Stump1eb44332009-09-09 15:08:12 +000039
Chris Lattnere116ccf2009-04-21 05:40:52 +000040 // Per GCC -D semantics, the macro ends at \n if it exists.
41 const char *End = strpbrk(Equal, "\n\r");
42 if (End) {
Daniel Dunbarbaac1032009-12-03 09:14:12 +000043 assert(Diags && "Unexpected macro with embedded newline!");
44 Diags->Report(diag::warn_fe_macro_contains_embedded_newline)
45 << std::string(Macro, Equal);
Chris Lattnere116ccf2009-04-21 05:40:52 +000046 } else {
47 End = Equal+strlen(Equal);
48 }
Mike Stump1eb44332009-09-09 15:08:12 +000049
Chris Lattnere116ccf2009-04-21 05:40:52 +000050 Buf.insert(Buf.end(), Equal+1, End);
51 } else {
52 // Push "macroname 1".
53 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
54 Buf.push_back(' ');
55 Buf.push_back('1');
56 }
57 Buf.push_back('\n');
58}
59
Chris Lattnerdcdecf42009-05-15 16:08:43 +000060// Append a #undef line to Buf for Macro. Macro should be of the form XXX
61// and we emit "#undef XXX".
62static void UndefineBuiltinMacro(std::vector<char> &Buf, const char *Macro) {
63 // Push "macroname".
64 const char *Command = "#undef ";
65 Buf.insert(Buf.end(), Command, Command+strlen(Command));
66 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
67 Buf.push_back('\n');
68}
69
Daniel Dunbarc7162932009-11-11 23:58:53 +000070std::string clang::NormalizeDashIncludePath(llvm::StringRef File) {
Daniel Dunbar1e7c6b62009-04-22 08:53:01 +000071 // Implicit include paths should be resolved relative to the current
72 // working directory first, and then use the regular header search
73 // mechanism. The proper way to handle this is to have the
74 // predefines buffer located at the current working directory, but
75 // it has not file entry. For now, workaround this by using an
76 // absolute path if we find the file here, and otherwise letting
77 // header search handle it.
Chris Lattnere116ccf2009-04-21 05:40:52 +000078 llvm::sys::Path Path(File);
79 Path.makeAbsolute();
Daniel Dunbar1e7c6b62009-04-22 08:53:01 +000080 if (!Path.exists())
81 Path = File;
Mike Stump1eb44332009-09-09 15:08:12 +000082
Daniel Dunbarc7162932009-11-11 23:58:53 +000083 return Lexer::Stringify(Path.str());
84}
85
86/// Add the quoted name of an implicit include file.
87static void AddQuotedIncludePath(std::vector<char> &Buf,
88 const std::string &File) {
89
Chris Lattnere116ccf2009-04-21 05:40:52 +000090 // Escape double quotes etc.
91 Buf.push_back('"');
Daniel Dunbarc7162932009-11-11 23:58:53 +000092 std::string EscapedFile = NormalizeDashIncludePath(File);
Chris Lattnere116ccf2009-04-21 05:40:52 +000093 Buf.insert(Buf.end(), EscapedFile.begin(), EscapedFile.end());
94 Buf.push_back('"');
95}
96
97/// AddImplicitInclude - Add an implicit #include of the specified file to the
98/// predefines buffer.
Mike Stump1eb44332009-09-09 15:08:12 +000099static void AddImplicitInclude(std::vector<char> &Buf,
Chris Lattnere116ccf2009-04-21 05:40:52 +0000100 const std::string &File) {
101 const char *Inc = "#include ";
102 Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
103 AddQuotedIncludePath(Buf, File);
104 Buf.push_back('\n');
105}
106
107static void AddImplicitIncludeMacros(std::vector<char> &Buf,
108 const std::string &File) {
109 const char *Inc = "#__include_macros ";
110 Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
111 AddQuotedIncludePath(Buf, File);
112 Buf.push_back('\n');
113 // Marker token to stop the __include_macros fetch loop.
114 const char *Marker = "##\n"; // ##?
115 Buf.insert(Buf.end(), Marker, Marker+strlen(Marker));
116}
117
118/// AddImplicitIncludePTH - Add an implicit #include using the original file
119/// used to generate a PTH cache.
Mike Stump1eb44332009-09-09 15:08:12 +0000120static void AddImplicitIncludePTH(std::vector<char> &Buf, Preprocessor &PP,
Chris Lattnere116ccf2009-04-21 05:40:52 +0000121 const std::string& ImplicitIncludePTH) {
122 PTHManager *P = PP.getPTHManager();
123 assert(P && "No PTHManager.");
124 const char *OriginalFile = P->getOriginalSourceFile();
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Chris Lattnere116ccf2009-04-21 05:40:52 +0000126 if (!OriginalFile) {
Daniel Dunbarbaac1032009-12-03 09:14:12 +0000127 PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header)
128 << ImplicitIncludePTH;
129 return;
Chris Lattnere116ccf2009-04-21 05:40:52 +0000130 }
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Chris Lattnere116ccf2009-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 Friedman2665a752009-05-23 03:50:01 +0000139 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
140 T IEEEQuadVal) {
Duncan Sands63682f92009-06-03 14:28:20 +0000141 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000142 return IEEESingleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000143 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000144 return IEEEDoubleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000145 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000146 return X87DoubleExtendedVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000147 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble)
Eli Friedman2665a752009-05-23 03:50:01 +0000148 return PPCDoubleDoubleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000149 assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad);
Eli Friedman2665a752009-05-23 03:50:01 +0000150 return IEEEQuadVal;
Chris Lattnere116ccf2009-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 Stump1eb44332009-09-09 15:08:12 +0000156 DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324",
Chris Lattnere116ccf2009-04-21 05:40:52 +0000157 "3.64519953188247460253e-4951L",
Eli Friedman2665a752009-05-23 03:50:01 +0000158 "4.94065645841246544176568792868221e-324L",
159 "6.47517511943802511092443895822764655e-4966L");
160 int Digits = PickFP(Sem, 6, 15, 18, 31, 33);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000161 Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16",
162 "1.08420217248550443401e-19L",
Eli Friedman2665a752009-05-23 03:50:01 +0000163 "4.94065645841246544176568792868221e-324L",
164 "1.92592994438723585305597794258492732e-34L");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000165 int HasInifinity = 1, HasQuietNaN = 1;
Eli Friedman2665a752009-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 Lattnere116ccf2009-04-21 05:40:52 +0000171 Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308",
172 "3.36210314311209350626e-4932L",
Eli Friedman2665a752009-05-23 03:50:01 +0000173 "2.00416836000897277799610805135016e-292L",
174 "3.36210314311209350626267781732175260e-4932L");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000175 Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308",
176 "1.18973149535723176502e+4932L",
Eli Friedman2665a752009-05-23 03:50:01 +0000177 "1.79769313486231580793728971405301e+308L",
178 "1.18973149535723176508575932662800702e+4932L");
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Eli Friedman2665a752009-05-23 03:50:01 +0000180 char MacroBuf[100];
Chris Lattnere116ccf2009-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 Stump1eb44332009-09-09 15:08:12 +0000222
Daniel Dunbarb8b844b2009-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 Lattnere116ccf2009-04-21 05:40:52 +0000226 DefineBuiltinMacro(Buf, MacroBuf);
227}
228
Chris Lattner9099e7b2009-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 Lattnere116ccf2009-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 Dyck186696b2009-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 Lattnerdcdd2a02009-11-12 08:08:27 +0000251static void DefineExactWidthIntType(TargetInfo::IntType Ty,
252 const TargetInfo &TI, std::vector<char> &Buf) {
253 char MacroBuf[60];
Ken Dyckeef22ef2009-11-16 16:36:33 +0000254 int TypeWidth = TI.getTypeWidth(Ty);
255 sprintf(MacroBuf, "__INT%d_TYPE__", TypeWidth);
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000256 DefineType(MacroBuf, Ty, Buf);
Ken Dyckeef22ef2009-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 Lattnerdcdd2a02009-11-12 08:08:27 +0000264}
Chris Lattnere116ccf2009-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 Stump1eb44332009-09-09 15:08:12 +0000273
Chris Lattnere116ccf2009-04-21 05:40:52 +0000274 // Currently claim to be compatible with GCC 4.2.1-5621.
Chris Lattnere116ccf2009-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 Stump1eb44332009-09-09 15:08:12 +0000280
281
Chris Lattnere116ccf2009-04-21 05:40:52 +0000282 // Initialize language-specific preprocessor defines.
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Chris Lattnere116ccf2009-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 Flynn07ef8042009-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 Lattnere116ccf2009-04-21 05:40:52 +0000297
298 // Standard conforming mode?
299 if (!LangOpts.GNUMode)
300 DefineBuiltinMacro(Buf, "__STRICT_ANSI__=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Chris Lattnere116ccf2009-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 Stump1eb44332009-09-09 15:08:12 +0000309
Chris Lattnere116ccf2009-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 Lattnere116ccf2009-04-21 05:40:52 +0000315 }
316
317 if (LangOpts.getGCMode() != LangOptions::NonGC)
318 DefineBuiltinMacro(Buf, "__OBJC_GC__=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Chris Lattnere116ccf2009-04-21 05:40:52 +0000320 if (LangOpts.NeXTRuntime)
321 DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1");
322 }
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Chris Lattnere116ccf2009-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 Stump1eb44332009-09-09 15:08:12 +0000327
Chris Lattnere116ccf2009-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 Stump1eb44332009-09-09 15:08:12 +0000338
Rafael Espindolaf759df02009-10-01 13:33:33 +0000339 if (LangOpts.Exceptions)
340 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
341
Chris Lattnere116ccf2009-04-21 05:40:52 +0000342 if (LangOpts.CPlusPlus) {
343 DefineBuiltinMacro(Buf, "__DEPRECATED=1");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000344 DefineBuiltinMacro(Buf, "__GNUG__=4");
345 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
Douglas Gregor36dd1312009-08-06 04:09:28 +0000346 if (LangOpts.GNUMode)
347 DefineBuiltinMacro(Buf, "__cplusplus=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000348 else
Douglas Gregor36dd1312009-08-06 04:09:28 +0000349 // C++ [cpp.predefined]p1:
Mike Stump1eb44332009-09-09 15:08:12 +0000350 // The name_ _cplusplusis defined to the value199711Lwhen compiling a
Douglas Gregor36dd1312009-08-06 04:09:28 +0000351 // C++ translation unit.
352 DefineBuiltinMacro(Buf, "__cplusplus=199711L");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000353 DefineBuiltinMacro(Buf, "__private_extern__=extern");
Eli Friedman666479b2009-08-27 22:01:41 +0000354 // Ugly hack to work with GNU libstdc++.
355 DefineBuiltinMacro(Buf, "_GNU_SOURCE=1");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Chris Lattnere116ccf2009-04-21 05:40:52 +0000358 if (LangOpts.Microsoft) {
John Thompson24ee8042009-10-16 01:12:00 +0000359 // Filter out some microsoft extensions when trying to parse in ms-compat
360 // mode.
Chris Lattnere116ccf2009-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__");
Steve Narofffdd6aaf2009-12-04 21:29:41 +0000365 // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however
366 // VC++ appears to only like __FUNCTION__.
367 DefineBuiltinMacro(Buf, "__PRETTY_FUNCTION__=__FUNCTION__");
John Thompson24ee8042009-10-16 01:12:00 +0000368 // Work around some issues with Visual C++ headerws.
369 if (LangOpts.CPlusPlus) {
370 // Since we define wchar_t in C++ mode.
371 DefineBuiltinMacro(Buf, "_WCHAR_T_DEFINED=1");
372 DefineBuiltinMacro(Buf, "_NATIVE_WCHAR_T_DEFINED=1");
373 // FIXME: This should be temporary until we have a __pragma
374 // solution, to avoid some errors flagged in VC++ headers.
375 DefineBuiltinMacro(Buf, "_CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES=0");
376 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000377 }
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Chris Lattnere116ccf2009-04-21 05:40:52 +0000379 if (LangOpts.Optimize)
380 DefineBuiltinMacro(Buf, "__OPTIMIZE__=1");
381 if (LangOpts.OptimizeSize)
382 DefineBuiltinMacro(Buf, "__OPTIMIZE_SIZE__=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Chris Lattnere116ccf2009-04-21 05:40:52 +0000384 // Initialize target-specific preprocessor defines.
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Chris Lattnere116ccf2009-04-21 05:40:52 +0000386 // Define type sizing macros based on the target properties.
387 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
388 DefineBuiltinMacro(Buf, "__CHAR_BIT__=8");
389
Chris Lattnere116ccf2009-04-21 05:40:52 +0000390 DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Buf);
Chris Lattner9099e7b2009-11-05 21:21:32 +0000391 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Buf);
392 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Buf);
393 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Buf);
394 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Buf);
Chris Lattner91846462009-11-12 08:04:33 +0000395 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Buf);
Chris Lattner9099e7b2009-11-05 21:21:32 +0000396 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Buf);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000397
Ken Dyck8241d732009-11-19 13:18:59 +0000398 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Buf);
399 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Buf);
Ken Dyck186696b2009-11-18 13:52:57 +0000400 DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Buf);
Ken Dyck8241d732009-11-19 13:18:59 +0000401 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Buf);
Ken Dyckd00c7512009-11-19 12:21:52 +0000402 DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Buf);
Ken Dyck8241d732009-11-19 13:18:59 +0000403 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Buf);
Ken Dyckd896e1a2009-11-18 20:05:48 +0000404 DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Buf);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000405 DefineType("__SIZE_TYPE__", TI.getSizeType(), Buf);
Ken Dyck7484e5d2009-11-19 13:42:09 +0000406 DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Buf);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000407 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Buf);
Ken Dyck63e65612009-11-19 15:47:58 +0000408 DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Buf);
Chris Lattnere64ef802009-10-21 04:59:34 +0000409 DefineType("__WINT_TYPE__", TI.getWIntType(), Buf);
Ken Dyck7635d212009-11-19 14:16:57 +0000410 DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Buf);
Ken Dyck3e945c82009-11-22 15:41:04 +0000411 DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Buf);
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Chris Lattnere116ccf2009-04-21 05:40:52 +0000413 DefineFloatMacros(Buf, "FLT", &TI.getFloatFormat());
414 DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat());
415 DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat());
416
417 // Define a __POINTER_WIDTH__ macro for stdint.h.
418 sprintf(MacroBuf, "__POINTER_WIDTH__=%d", (int)TI.getPointerWidth(0));
419 DefineBuiltinMacro(Buf, MacroBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Eli Friedman15b91762009-06-05 07:05:05 +0000421 if (!LangOpts.CharIsSigned)
Mike Stump1eb44332009-09-09 15:08:12 +0000422 DefineBuiltinMacro(Buf, "__CHAR_UNSIGNED__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000423
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000424 // Define exact-width integer types for stdint.h
425 sprintf(MacroBuf, "__INT%d_TYPE__=char", TI.getCharWidth());
426 DefineBuiltinMacro(Buf, MacroBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000428 if (TI.getShortWidth() > TI.getCharWidth())
429 DefineExactWidthIntType(TargetInfo::SignedShort, TI, Buf);
430
431 if (TI.getIntWidth() > TI.getShortWidth())
432 DefineExactWidthIntType(TargetInfo::SignedInt, TI, Buf);
433
434 if (TI.getLongWidth() > TI.getIntWidth())
435 DefineExactWidthIntType(TargetInfo::SignedLong, TI, Buf);
436
437 if (TI.getLongLongWidth() > TI.getLongWidth())
438 DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Buf);
439
Chris Lattnere116ccf2009-04-21 05:40:52 +0000440 // Add __builtin_va_list typedef.
441 {
442 const char *VAList = TI.getVAListDeclaration();
443 Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
444 Buf.push_back('\n');
445 }
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Chris Lattnere116ccf2009-04-21 05:40:52 +0000447 if (const char *Prefix = TI.getUserLabelPrefix()) {
448 sprintf(MacroBuf, "__USER_LABEL_PREFIX__=%s", Prefix);
449 DefineBuiltinMacro(Buf, MacroBuf);
450 }
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Chris Lattnere116ccf2009-04-21 05:40:52 +0000452 // Build configuration options. FIXME: these should be controlled by
453 // command line options or something.
454 DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
455
Chris Lattnere116ccf2009-04-21 05:40:52 +0000456 if (LangOpts.GNUInline)
457 DefineBuiltinMacro(Buf, "__GNUC_GNU_INLINE__=1");
458 else
459 DefineBuiltinMacro(Buf, "__GNUC_STDC_INLINE__=1");
460
461 if (LangOpts.NoInline)
462 DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
463
464 if (unsigned PICLevel = LangOpts.PICLevel) {
465 sprintf(MacroBuf, "__PIC__=%d", PICLevel);
466 DefineBuiltinMacro(Buf, MacroBuf);
467
468 sprintf(MacroBuf, "__pic__=%d", PICLevel);
469 DefineBuiltinMacro(Buf, MacroBuf);
470 }
471
472 // Macros to control C99 numerics and <float.h>
473 DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0");
474 DefineBuiltinMacro(Buf, "__FLT_RADIX__=2");
475 sprintf(MacroBuf, "__DECIMAL_DIG__=%d",
Eli Friedman2665a752009-05-23 03:50:01 +0000476 PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36));
Chris Lattnere116ccf2009-04-21 05:40:52 +0000477 DefineBuiltinMacro(Buf, MacroBuf);
Bill Wendling45483f72009-06-28 07:36:13 +0000478
Bill Wendling4ebe3e42009-06-28 23:01:01 +0000479 if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn)
Bill Wendling45483f72009-06-28 07:36:13 +0000480 DefineBuiltinMacro(Buf, "__SSP__=1");
Bill Wendling4ebe3e42009-06-28 23:01:01 +0000481 else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq)
Bill Wendling45483f72009-06-28 07:36:13 +0000482 DefineBuiltinMacro(Buf, "__SSP_ALL__=2");
483
Chris Lattnere116ccf2009-04-21 05:40:52 +0000484 // Get other target #defines.
485 TI.getTargetDefines(LangOpts, Buf);
486}
487
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000488// Initialize the remapping of files to alternative contents, e.g.,
489// those specified through other files.
490static void InitializeFileRemapping(Diagnostic &Diags,
491 SourceManager &SourceMgr,
492 FileManager &FileMgr,
493 const PreprocessorOptions &InitOpts) {
494 // Remap files in the source manager.
495 for (PreprocessorOptions::remapped_file_iterator
496 Remap = InitOpts.remapped_file_begin(),
497 RemapEnd = InitOpts.remapped_file_end();
498 Remap != RemapEnd;
499 ++Remap) {
500 // Find the file that we're mapping to.
501 const FileEntry *ToFile = FileMgr.getFile(Remap->second);
502 if (!ToFile) {
503 Diags.Report(diag::err_fe_remap_missing_to_file)
504 << Remap->first << Remap->second;
505 continue;
506 }
507
Douglas Gregor057e5672009-12-02 18:12:28 +0000508 // Create the file entry for the file that we're mapping from.
509 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
510 ToFile->getSize(),
511 0);
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000512 if (!FromFile) {
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000513 Diags.Report(diag::err_fe_remap_missing_from_file)
514 << Remap->first;
515 continue;
516 }
517
518 // Load the contents of the file we're mapping to.
519 std::string ErrorStr;
520 const llvm::MemoryBuffer *Buffer
521 = llvm::MemoryBuffer::getFile(ToFile->getName(), &ErrorStr);
522 if (!Buffer) {
523 Diags.Report(diag::err_fe_error_opening)
524 << Remap->second << ErrorStr;
525 continue;
526 }
527
528 // Override the contents of the "from" file with the contents of
529 // the "to" file.
530 SourceMgr.overrideFileContents(FromFile, Buffer);
531 }
532}
533
Chris Lattnere116ccf2009-04-21 05:40:52 +0000534/// InitializePreprocessor - Initialize the preprocessor getting it and the
535/// environment ready to process a single file. This returns true on error.
536///
Daniel Dunbar938963f2009-11-04 21:13:15 +0000537void clang::InitializePreprocessor(Preprocessor &PP,
Daniel Dunbar961c76e2009-11-11 21:44:42 +0000538 const PreprocessorOptions &InitOpts,
539 const HeaderSearchOptions &HSOpts) {
Chris Lattnere116ccf2009-04-21 05:40:52 +0000540 std::vector<char> PredefineBuffer;
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000542 InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(),
543 PP.getFileManager(), InitOpts);
544
Chris Lattner39360242009-04-22 03:42:19 +0000545 const char *LineDirective = "# 1 \"<built-in>\" 3\n";
546 PredefineBuffer.insert(PredefineBuffer.end(),
547 LineDirective, LineDirective+strlen(LineDirective));
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Chris Lattnere116ccf2009-04-21 05:40:52 +0000549 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000550 if (InitOpts.UsePredefines)
Chris Lattnere6113de2009-11-03 19:50:27 +0000551 InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(),
552 PredefineBuffer);
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Chris Lattnere116ccf2009-04-21 05:40:52 +0000554 // Add on the predefines from the driver. Wrap in a #line directive to report
555 // that they come from the command line.
Chris Lattner39360242009-04-22 03:42:19 +0000556 LineDirective = "# 1 \"<command line>\" 1\n";
Chris Lattnere116ccf2009-04-21 05:40:52 +0000557 PredefineBuffer.insert(PredefineBuffer.end(),
558 LineDirective, LineDirective+strlen(LineDirective));
559
560 // Process #define's and #undef's in the order they are given.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000561 for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
562 if (InitOpts.Macros[i].second) // isUndef
563 UndefineBuiltinMacro(PredefineBuffer, InitOpts.Macros[i].first.c_str());
Chris Lattner62f86c42009-04-21 06:00:24 +0000564 else
Daniel Dunbarbaac1032009-12-03 09:14:12 +0000565 DefineBuiltinMacro(PredefineBuffer, InitOpts.Macros[i].first.c_str(),
566 &PP.getDiagnostics());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000567 }
568
569 // If -imacros are specified, include them now. These are processed before
570 // any -include directives.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000571 for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
572 AddImplicitIncludeMacros(PredefineBuffer, InitOpts.MacroIncludes[i]);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000573
574 // Process -include directives.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000575 for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
576 const std::string &Path = InitOpts.Includes[i];
577 if (Path == InitOpts.ImplicitPTHInclude)
578 AddImplicitIncludePTH(PredefineBuffer, PP, Path);
Chris Lattner62f86c42009-04-21 06:00:24 +0000579 else
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000580 AddImplicitInclude(PredefineBuffer, Path);
Eli Friedmanae96a962009-06-15 09:57:52 +0000581 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000582
Rafael Espindola0259d202009-12-01 18:28:16 +0000583 // Exit the command line and go back to <built-in> (2 is LC_LEAVE).
584 LineDirective = "# 1 \"<built-in>\" 2\n";
585 PredefineBuffer.insert(PredefineBuffer.end(),
586 LineDirective, LineDirective+strlen(LineDirective));
587
Chris Lattnere116ccf2009-04-21 05:40:52 +0000588 // Null terminate PredefinedBuffer and add it.
589 PredefineBuffer.push_back(0);
590 PP.setPredefines(&PredefineBuffer[0]);
Daniel Dunbar961c76e2009-11-11 21:44:42 +0000591
592 // Initialize the header search object.
593 ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts,
594 PP.getLangOptions(),
595 PP.getTargetInfo().getTriple());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000596}