blob: 3ec408a24f7dc4a76273f9667c7d10e14b640830 [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"
Chris Lattner2f5693f2009-04-21 05:40:52 +000023#include "llvm/ADT/STLExtras.h"
Douglas Gregor6ae34ab2009-12-02 16:32:41 +000024#include "llvm/Support/MemoryBuffer.h"
Chris Lattner2f5693f2009-04-21 05:40:52 +000025#include "llvm/System/Path.h"
Chris Lattner3fab58d2009-11-02 21:48:09 +000026using namespace clang;
Chris Lattner2f5693f2009-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 Dunbar6dac9352009-12-03 09:14:12 +000031static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
32 Diagnostic *Diags = 0) {
Kovarththanan Rajaratnam49c8da92010-01-07 18:11:14 +000033 const char Command[] = "#define ";
Chris Lattner2f5693f2009-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 Stump11289f42009-09-09 15:08:12 +000039
Chris Lattner2f5693f2009-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 Dunbar6dac9352009-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 Lattner2f5693f2009-04-21 05:40:52 +000046 } else {
47 End = Equal+strlen(Equal);
48 }
Mike Stump11289f42009-09-09 15:08:12 +000049
Chris Lattner2f5693f2009-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 Lattner4579b762009-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".
Kovarththanan Rajaratnam49c8da92010-01-07 18:11:14 +000064 const char Command[] = "#undef ";
Chris Lattner4579b762009-05-15 16:08:43 +000065 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 Dunbar732ef8a2009-11-11 23:58:53 +000070std::string clang::NormalizeDashIncludePath(llvm::StringRef File) {
Daniel Dunbar66cb0132009-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 Lattner2f5693f2009-04-21 05:40:52 +000078 llvm::sys::Path Path(File);
79 Path.makeAbsolute();
Daniel Dunbar66cb0132009-04-22 08:53:01 +000080 if (!Path.exists())
81 Path = File;
Mike Stump11289f42009-09-09 15:08:12 +000082
Daniel Dunbar732ef8a2009-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 Lattner2f5693f2009-04-21 05:40:52 +000090 // Escape double quotes etc.
91 Buf.push_back('"');
Daniel Dunbar732ef8a2009-11-11 23:58:53 +000092 std::string EscapedFile = NormalizeDashIncludePath(File);
Chris Lattner2f5693f2009-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 Stump11289f42009-09-09 15:08:12 +000099static void AddImplicitInclude(std::vector<char> &Buf,
Chris Lattner2f5693f2009-04-21 05:40:52 +0000100 const std::string &File) {
Kovarththanan Rajaratnam49c8da92010-01-07 18:11:14 +0000101 const char Command[] = "#include ";
102 Buf.insert(Buf.end(), Command, Command+strlen(Command));
Chris Lattner2f5693f2009-04-21 05:40:52 +0000103 AddQuotedIncludePath(Buf, File);
104 Buf.push_back('\n');
105}
106
107static void AddImplicitIncludeMacros(std::vector<char> &Buf,
108 const std::string &File) {
Kovarththanan Rajaratnam49c8da92010-01-07 18:11:14 +0000109 const char Command[] = "#__include_macros ";
110 Buf.insert(Buf.end(), Command, Command+strlen(Command));
Chris Lattner2f5693f2009-04-21 05:40:52 +0000111 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 Stump11289f42009-09-09 15:08:12 +0000120static void AddImplicitIncludePTH(std::vector<char> &Buf, Preprocessor &PP,
Chris Lattner2f5693f2009-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 Stump11289f42009-09-09 15:08:12 +0000125
Chris Lattner2f5693f2009-04-21 05:40:52 +0000126 if (!OriginalFile) {
Daniel Dunbar6dac9352009-12-03 09:14:12 +0000127 PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header)
128 << ImplicitIncludePTH;
129 return;
Chris Lattner2f5693f2009-04-21 05:40:52 +0000130 }
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) {
Chris Lattner2f5693f2009-04-21 05:40:52 +0000269 // Compiler version introspection macros.
270 DefineBuiltinMacro(Buf, "__llvm__=1"); // LLVM Backend
271 DefineBuiltinMacro(Buf, "__clang__=1"); // Clang Frontend
Mike Stump11289f42009-09-09 15:08:12 +0000272
Chris Lattner2f5693f2009-04-21 05:40:52 +0000273 // Currently claim to be compatible with GCC 4.2.1-5621.
Chris Lattner2f5693f2009-04-21 05:40:52 +0000274 DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2");
275 DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
276 DefineBuiltinMacro(Buf, "__GNUC__=4");
277 DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
278 DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 Compatible Clang Compiler\"");
Mike Stump11289f42009-09-09 15:08:12 +0000279
280
Chris Lattner2f5693f2009-04-21 05:40:52 +0000281 // Initialize language-specific preprocessor defines.
Mike Stump11289f42009-09-09 15:08:12 +0000282
Chris Lattner2f5693f2009-04-21 05:40:52 +0000283 // These should all be defined in the preprocessor according to the
284 // current language configuration.
285 if (!LangOpts.Microsoft)
286 DefineBuiltinMacro(Buf, "__STDC__=1");
287 if (LangOpts.AsmPreprocessor)
288 DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
Ryan Flynned73cac2009-07-21 00:07:02 +0000289
290 if (!LangOpts.CPlusPlus) {
291 if (LangOpts.C99)
292 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
293 else if (!LangOpts.GNUMode && LangOpts.Digraphs)
294 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
295 }
Chris Lattner2f5693f2009-04-21 05:40:52 +0000296
297 // Standard conforming mode?
298 if (!LangOpts.GNUMode)
299 DefineBuiltinMacro(Buf, "__STRICT_ANSI__=1");
Mike Stump11289f42009-09-09 15:08:12 +0000300
Chris Lattner2f5693f2009-04-21 05:40:52 +0000301 if (LangOpts.CPlusPlus0x)
302 DefineBuiltinMacro(Buf, "__GXX_EXPERIMENTAL_CXX0X__");
303
304 if (LangOpts.Freestanding)
305 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=0");
306 else
307 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
Mike Stump11289f42009-09-09 15:08:12 +0000308
Chris Lattner2f5693f2009-04-21 05:40:52 +0000309 if (LangOpts.ObjC1) {
310 DefineBuiltinMacro(Buf, "__OBJC__=1");
311 if (LangOpts.ObjCNonFragileABI) {
312 DefineBuiltinMacro(Buf, "__OBJC2__=1");
313 DefineBuiltinMacro(Buf, "OBJC_ZEROCOST_EXCEPTIONS=1");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000314 }
315
316 if (LangOpts.getGCMode() != LangOptions::NonGC)
317 DefineBuiltinMacro(Buf, "__OBJC_GC__=1");
Mike Stump11289f42009-09-09 15:08:12 +0000318
Chris Lattner2f5693f2009-04-21 05:40:52 +0000319 if (LangOpts.NeXTRuntime)
320 DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1");
321 }
Mike Stump11289f42009-09-09 15:08:12 +0000322
Chris Lattner2f5693f2009-04-21 05:40:52 +0000323 // darwin_constant_cfstrings controls this. This is also dependent
324 // on other things like the runtime I believe. This is set even for C code.
325 DefineBuiltinMacro(Buf, "__CONSTANT_CFSTRINGS__=1");
Mike Stump11289f42009-09-09 15:08:12 +0000326
Chris Lattner2f5693f2009-04-21 05:40:52 +0000327 if (LangOpts.ObjC2)
328 DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES");
329
330 if (LangOpts.PascalStrings)
331 DefineBuiltinMacro(Buf, "__PASCAL_STRINGS__");
332
333 if (LangOpts.Blocks) {
334 DefineBuiltinMacro(Buf, "__block=__attribute__((__blocks__(byref)))");
335 DefineBuiltinMacro(Buf, "__BLOCKS__=1");
336 }
Mike Stump11289f42009-09-09 15:08:12 +0000337
Rafael Espindola00a66572009-10-01 13:33:33 +0000338 if (LangOpts.Exceptions)
339 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
340
Chris Lattner2f5693f2009-04-21 05:40:52 +0000341 if (LangOpts.CPlusPlus) {
342 DefineBuiltinMacro(Buf, "__DEPRECATED=1");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000343 DefineBuiltinMacro(Buf, "__GNUG__=4");
344 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
Douglas Gregor3f4d51b2009-08-06 04:09:28 +0000345 if (LangOpts.GNUMode)
346 DefineBuiltinMacro(Buf, "__cplusplus=1");
Mike Stump11289f42009-09-09 15:08:12 +0000347 else
Douglas Gregor3f4d51b2009-08-06 04:09:28 +0000348 // C++ [cpp.predefined]p1:
Mike Stump11289f42009-09-09 15:08:12 +0000349 // The name_ _cplusplusis defined to the value199711Lwhen compiling a
Douglas Gregor3f4d51b2009-08-06 04:09:28 +0000350 // C++ translation unit.
351 DefineBuiltinMacro(Buf, "__cplusplus=199711L");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000352 DefineBuiltinMacro(Buf, "__private_extern__=extern");
Eli Friedman80110172009-08-27 22:01:41 +0000353 // Ugly hack to work with GNU libstdc++.
354 DefineBuiltinMacro(Buf, "_GNU_SOURCE=1");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000355 }
Mike Stump11289f42009-09-09 15:08:12 +0000356
Chris Lattner2f5693f2009-04-21 05:40:52 +0000357 if (LangOpts.Microsoft) {
John Thompson1faca9e2009-10-16 01:12:00 +0000358 // Filter out some microsoft extensions when trying to parse in ms-compat
359 // mode.
Chris Lattner2f5693f2009-04-21 05:40:52 +0000360 DefineBuiltinMacro(Buf, "__int8=__INT8_TYPE__");
361 DefineBuiltinMacro(Buf, "__int16=__INT16_TYPE__");
362 DefineBuiltinMacro(Buf, "__int32=__INT32_TYPE__");
363 DefineBuiltinMacro(Buf, "__int64=__INT64_TYPE__");
Steve Narofff0df20a2009-12-04 21:29:41 +0000364 // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however
365 // VC++ appears to only like __FUNCTION__.
366 DefineBuiltinMacro(Buf, "__PRETTY_FUNCTION__=__FUNCTION__");
John Thompson1faca9e2009-10-16 01:12:00 +0000367 // Work around some issues with Visual C++ headerws.
368 if (LangOpts.CPlusPlus) {
369 // Since we define wchar_t in C++ mode.
370 DefineBuiltinMacro(Buf, "_WCHAR_T_DEFINED=1");
371 DefineBuiltinMacro(Buf, "_NATIVE_WCHAR_T_DEFINED=1");
372 // FIXME: This should be temporary until we have a __pragma
373 // solution, to avoid some errors flagged in VC++ headers.
374 DefineBuiltinMacro(Buf, "_CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES=0");
375 }
Chris Lattner2f5693f2009-04-21 05:40:52 +0000376 }
Mike Stump11289f42009-09-09 15:08:12 +0000377
Chris Lattner2f5693f2009-04-21 05:40:52 +0000378 if (LangOpts.Optimize)
379 DefineBuiltinMacro(Buf, "__OPTIMIZE__=1");
380 if (LangOpts.OptimizeSize)
381 DefineBuiltinMacro(Buf, "__OPTIMIZE_SIZE__=1");
Mike Stump11289f42009-09-09 15:08:12 +0000382
Chris Lattner2f5693f2009-04-21 05:40:52 +0000383 // Initialize target-specific preprocessor defines.
Mike Stump11289f42009-09-09 15:08:12 +0000384
Chris Lattner2f5693f2009-04-21 05:40:52 +0000385 // Define type sizing macros based on the target properties.
386 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
387 DefineBuiltinMacro(Buf, "__CHAR_BIT__=8");
388
Chris Lattner2f5693f2009-04-21 05:40:52 +0000389 DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Buf);
Chris Lattnere4a8c642009-11-05 21:21:32 +0000390 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Buf);
391 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Buf);
392 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Buf);
393 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Buf);
Chris Lattner0fb5bbd2009-11-12 08:04:33 +0000394 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Buf);
Chris Lattnere4a8c642009-11-05 21:21:32 +0000395 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Buf);
Chris Lattner2f5693f2009-04-21 05:40:52 +0000396
Ken Dyck9b25f782009-11-19 13:18:59 +0000397 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Buf);
398 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Buf);
Ken Dyckc0c98292009-11-18 13:52:57 +0000399 DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Buf);
Ken Dyck9b25f782009-11-19 13:18:59 +0000400 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Buf);
Ken Dyck056efe02009-11-19 12:21:52 +0000401 DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Buf);
Ken Dyck9b25f782009-11-19 13:18:59 +0000402 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Buf);
Ken Dyck24cfcf12009-11-18 20:05:48 +0000403 DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Buf);
Chris Lattner2f5693f2009-04-21 05:40:52 +0000404 DefineType("__SIZE_TYPE__", TI.getSizeType(), Buf);
Ken Dyck57512862009-11-19 13:42:09 +0000405 DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Buf);
Chris Lattner2f5693f2009-04-21 05:40:52 +0000406 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Buf);
Ken Dyck0138b9e2009-11-19 15:47:58 +0000407 DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Buf);
Chris Lattner67204922009-10-21 04:59:34 +0000408 DefineType("__WINT_TYPE__", TI.getWIntType(), Buf);
Ken Dycka1f677c2009-11-19 14:16:57 +0000409 DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Buf);
Ken Dyckadc85112009-11-22 15:41:04 +0000410 DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Buf);
Mike Stump11289f42009-09-09 15:08:12 +0000411
Chris Lattner2f5693f2009-04-21 05:40:52 +0000412 DefineFloatMacros(Buf, "FLT", &TI.getFloatFormat());
413 DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat());
414 DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat());
415
416 // Define a __POINTER_WIDTH__ macro for stdint.h.
Kovarththanan Rajaratnamb53c7d42010-01-07 16:01:54 +0000417 char MacroBuf[60];
Chris Lattner2f5693f2009-04-21 05:40:52 +0000418 sprintf(MacroBuf, "__POINTER_WIDTH__=%d", (int)TI.getPointerWidth(0));
419 DefineBuiltinMacro(Buf, MacroBuf);
Mike Stump11289f42009-09-09 15:08:12 +0000420
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000421 if (!LangOpts.CharIsSigned)
Mike Stump11289f42009-09-09 15:08:12 +0000422 DefineBuiltinMacro(Buf, "__CHAR_UNSIGNED__");
Chris Lattner2f5693f2009-04-21 05:40:52 +0000423
Chris Lattner55c98772009-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 Stump11289f42009-09-09 15:08:12 +0000427
Chris Lattner55c98772009-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 Lattner2f5693f2009-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 Stump11289f42009-09-09 15:08:12 +0000446
Chris Lattner2f5693f2009-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 Stump11289f42009-09-09 15:08:12 +0000451
Chris Lattner2f5693f2009-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 Lattner2f5693f2009-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 Friedman57406b22009-05-23 03:50:01 +0000476 PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36));
Chris Lattner2f5693f2009-04-21 05:40:52 +0000477 DefineBuiltinMacro(Buf, MacroBuf);
Bill Wendlingd63bbad2009-06-28 07:36:13 +0000478
Bill Wendling18351072009-06-28 23:01:01 +0000479 if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn)
Bill Wendlingd63bbad2009-06-28 07:36:13 +0000480 DefineBuiltinMacro(Buf, "__SSP__=1");
Bill Wendling18351072009-06-28 23:01:01 +0000481 else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq)
Bill Wendlingd63bbad2009-06-28 07:36:13 +0000482 DefineBuiltinMacro(Buf, "__SSP_ALL__=2");
483
Chris Lattner2f5693f2009-04-21 05:40:52 +0000484 // Get other target #defines.
485 TI.getTargetDefines(LangOpts, Buf);
486}
487
Douglas Gregor6ae34ab2009-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 Gregor407e2122009-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 Gregor6ae34ab2009-12-02 16:32:41 +0000512 if (!FromFile) {
Douglas Gregor6ae34ab2009-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 Lattner2f5693f2009-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 Dunbar181aaee2009-11-04 21:13:15 +0000537void clang::InitializePreprocessor(Preprocessor &PP,
Daniel Dunbar2856ae442009-11-11 21:44:42 +0000538 const PreprocessorOptions &InitOpts,
539 const HeaderSearchOptions &HSOpts) {
Chris Lattner2f5693f2009-04-21 05:40:52 +0000540 std::vector<char> PredefineBuffer;
Mike Stump11289f42009-09-09 15:08:12 +0000541
Douglas Gregor6ae34ab2009-12-02 16:32:41 +0000542 InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(),
543 PP.getFileManager(), InitOpts);
544
Chris Lattnered462a82009-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 Stump11289f42009-09-09 15:08:12 +0000548
Chris Lattner2f5693f2009-04-21 05:40:52 +0000549 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
Daniel Dunbard6ea9022009-11-17 05:52:41 +0000550 if (InitOpts.UsePredefines)
Chris Lattnere9d7d782009-11-03 19:50:27 +0000551 InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(),
552 PredefineBuffer);
Mike Stump11289f42009-09-09 15:08:12 +0000553
Chris Lattner2f5693f2009-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 Lattnered462a82009-04-22 03:42:19 +0000556 LineDirective = "# 1 \"<command line>\" 1\n";
Chris Lattner2f5693f2009-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 Dunbard6ea9022009-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 Lattnere08c43a2009-04-21 06:00:24 +0000564 else
Daniel Dunbar6dac9352009-12-03 09:14:12 +0000565 DefineBuiltinMacro(PredefineBuffer, InitOpts.Macros[i].first.c_str(),
566 &PP.getDiagnostics());
Chris Lattner2f5693f2009-04-21 05:40:52 +0000567 }
568
569 // If -imacros are specified, include them now. These are processed before
570 // any -include directives.
Daniel Dunbard6ea9022009-11-17 05:52:41 +0000571 for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
572 AddImplicitIncludeMacros(PredefineBuffer, InitOpts.MacroIncludes[i]);
Chris Lattner2f5693f2009-04-21 05:40:52 +0000573
574 // Process -include directives.
Daniel Dunbard6ea9022009-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 Lattnere08c43a2009-04-21 06:00:24 +0000579 else
Daniel Dunbard6ea9022009-11-17 05:52:41 +0000580 AddImplicitInclude(PredefineBuffer, Path);
Eli Friedmanb1884552009-06-15 09:57:52 +0000581 }
Chris Lattner2f5693f2009-04-21 05:40:52 +0000582
Rafael Espindolaf8a04a12009-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 Lattner2f5693f2009-04-21 05:40:52 +0000588 // Null terminate PredefinedBuffer and add it.
589 PredefineBuffer.push_back(0);
590 PP.setPredefines(&PredefineBuffer[0]);
Daniel Dunbar2856ae442009-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 Lattner2f5693f2009-04-21 05:40:52 +0000596}