blob: aa05b34ef5cac166e4f8a9d2a892dcb15157bc64 [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=".
Chris Lattner810dc542009-06-04 16:47:09 +000031static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro) {
32 const char *Command = "#define ";
Chris Lattnere116ccf2009-04-21 05:40:52 +000033 Buf.insert(Buf.end(), Command, Command+strlen(Command));
34 if (const char *Equal = strchr(Macro, '=')) {
35 // Turn the = into ' '.
36 Buf.insert(Buf.end(), Macro, Equal);
37 Buf.push_back(' ');
Mike Stump1eb44332009-09-09 15:08:12 +000038
Chris Lattnere116ccf2009-04-21 05:40:52 +000039 // Per GCC -D semantics, the macro ends at \n if it exists.
40 const char *End = strpbrk(Equal, "\n\r");
41 if (End) {
42 fprintf(stderr, "warning: macro '%s' contains embedded newline, text "
43 "after the newline is ignored.\n",
44 std::string(Macro, Equal).c_str());
45 } else {
46 End = Equal+strlen(Equal);
47 }
Mike Stump1eb44332009-09-09 15:08:12 +000048
Chris Lattnere116ccf2009-04-21 05:40:52 +000049 Buf.insert(Buf.end(), Equal+1, End);
50 } else {
51 // Push "macroname 1".
52 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
53 Buf.push_back(' ');
54 Buf.push_back('1');
55 }
56 Buf.push_back('\n');
57}
58
Chris Lattnerdcdecf42009-05-15 16:08:43 +000059// Append a #undef line to Buf for Macro. Macro should be of the form XXX
60// and we emit "#undef XXX".
61static void UndefineBuiltinMacro(std::vector<char> &Buf, const char *Macro) {
62 // Push "macroname".
63 const char *Command = "#undef ";
64 Buf.insert(Buf.end(), Command, Command+strlen(Command));
65 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
66 Buf.push_back('\n');
67}
68
Daniel Dunbarc7162932009-11-11 23:58:53 +000069std::string clang::NormalizeDashIncludePath(llvm::StringRef File) {
Daniel Dunbar1e7c6b62009-04-22 08:53:01 +000070 // Implicit include paths should be resolved relative to the current
71 // working directory first, and then use the regular header search
72 // mechanism. The proper way to handle this is to have the
73 // predefines buffer located at the current working directory, but
74 // it has not file entry. For now, workaround this by using an
75 // absolute path if we find the file here, and otherwise letting
76 // header search handle it.
Chris Lattnere116ccf2009-04-21 05:40:52 +000077 llvm::sys::Path Path(File);
78 Path.makeAbsolute();
Daniel Dunbar1e7c6b62009-04-22 08:53:01 +000079 if (!Path.exists())
80 Path = File;
Mike Stump1eb44332009-09-09 15:08:12 +000081
Daniel Dunbarc7162932009-11-11 23:58:53 +000082 return Lexer::Stringify(Path.str());
83}
84
85/// Add the quoted name of an implicit include file.
86static void AddQuotedIncludePath(std::vector<char> &Buf,
87 const std::string &File) {
88
Chris Lattnere116ccf2009-04-21 05:40:52 +000089 // Escape double quotes etc.
90 Buf.push_back('"');
Daniel Dunbarc7162932009-11-11 23:58:53 +000091 std::string EscapedFile = NormalizeDashIncludePath(File);
Chris Lattnere116ccf2009-04-21 05:40:52 +000092 Buf.insert(Buf.end(), EscapedFile.begin(), EscapedFile.end());
93 Buf.push_back('"');
94}
95
96/// AddImplicitInclude - Add an implicit #include of the specified file to the
97/// predefines buffer.
Mike Stump1eb44332009-09-09 15:08:12 +000098static void AddImplicitInclude(std::vector<char> &Buf,
Chris Lattnere116ccf2009-04-21 05:40:52 +000099 const std::string &File) {
100 const char *Inc = "#include ";
101 Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
102 AddQuotedIncludePath(Buf, File);
103 Buf.push_back('\n');
104}
105
106static void AddImplicitIncludeMacros(std::vector<char> &Buf,
107 const std::string &File) {
108 const char *Inc = "#__include_macros ";
109 Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
110 AddQuotedIncludePath(Buf, File);
111 Buf.push_back('\n');
112 // Marker token to stop the __include_macros fetch loop.
113 const char *Marker = "##\n"; // ##?
114 Buf.insert(Buf.end(), Marker, Marker+strlen(Marker));
115}
116
117/// AddImplicitIncludePTH - Add an implicit #include using the original file
118/// used to generate a PTH cache.
Mike Stump1eb44332009-09-09 15:08:12 +0000119static void AddImplicitIncludePTH(std::vector<char> &Buf, Preprocessor &PP,
Chris Lattnere116ccf2009-04-21 05:40:52 +0000120 const std::string& ImplicitIncludePTH) {
121 PTHManager *P = PP.getPTHManager();
122 assert(P && "No PTHManager.");
123 const char *OriginalFile = P->getOriginalSourceFile();
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Chris Lattnere116ccf2009-04-21 05:40:52 +0000125 if (!OriginalFile) {
126 assert(!ImplicitIncludePTH.empty());
127 fprintf(stderr, "error: PTH file '%s' does not designate an original "
128 "source header file for -include-pth\n",
129 ImplicitIncludePTH.c_str());
130 exit (1);
131 }
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Chris Lattnere116ccf2009-04-21 05:40:52 +0000133 AddImplicitInclude(Buf, OriginalFile);
134}
135
136/// PickFP - This is used to pick a value based on the FP semantics of the
137/// specified FP model.
138template <typename T>
139static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal,
Eli Friedman2665a752009-05-23 03:50:01 +0000140 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
141 T IEEEQuadVal) {
Duncan Sands63682f92009-06-03 14:28:20 +0000142 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000143 return IEEESingleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000144 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000145 return IEEEDoubleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000146 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000147 return X87DoubleExtendedVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000148 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble)
Eli Friedman2665a752009-05-23 03:50:01 +0000149 return PPCDoubleDoubleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000150 assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad);
Eli Friedman2665a752009-05-23 03:50:01 +0000151 return IEEEQuadVal;
Chris Lattnere116ccf2009-04-21 05:40:52 +0000152}
153
154static void DefineFloatMacros(std::vector<char> &Buf, const char *Prefix,
155 const llvm::fltSemantics *Sem) {
156 const char *DenormMin, *Epsilon, *Max, *Min;
Mike Stump1eb44332009-09-09 15:08:12 +0000157 DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324",
Chris Lattnere116ccf2009-04-21 05:40:52 +0000158 "3.64519953188247460253e-4951L",
Eli Friedman2665a752009-05-23 03:50:01 +0000159 "4.94065645841246544176568792868221e-324L",
160 "6.47517511943802511092443895822764655e-4966L");
161 int Digits = PickFP(Sem, 6, 15, 18, 31, 33);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000162 Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16",
163 "1.08420217248550443401e-19L",
Eli Friedman2665a752009-05-23 03:50:01 +0000164 "4.94065645841246544176568792868221e-324L",
165 "1.92592994438723585305597794258492732e-34L");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000166 int HasInifinity = 1, HasQuietNaN = 1;
Eli Friedman2665a752009-05-23 03:50:01 +0000167 int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113);
168 int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931);
169 int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932);
170 int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381);
171 int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000172 Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308",
173 "3.36210314311209350626e-4932L",
Eli Friedman2665a752009-05-23 03:50:01 +0000174 "2.00416836000897277799610805135016e-292L",
175 "3.36210314311209350626267781732175260e-4932L");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000176 Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308",
177 "1.18973149535723176502e+4932L",
Eli Friedman2665a752009-05-23 03:50:01 +0000178 "1.79769313486231580793728971405301e+308L",
179 "1.18973149535723176508575932662800702e+4932L");
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Eli Friedman2665a752009-05-23 03:50:01 +0000181 char MacroBuf[100];
Chris Lattnere116ccf2009-04-21 05:40:52 +0000182 sprintf(MacroBuf, "__%s_DENORM_MIN__=%s", Prefix, DenormMin);
183 DefineBuiltinMacro(Buf, MacroBuf);
184 sprintf(MacroBuf, "__%s_DIG__=%d", Prefix, Digits);
185 DefineBuiltinMacro(Buf, MacroBuf);
186 sprintf(MacroBuf, "__%s_EPSILON__=%s", Prefix, Epsilon);
187 DefineBuiltinMacro(Buf, MacroBuf);
188 sprintf(MacroBuf, "__%s_HAS_INFINITY__=%d", Prefix, HasInifinity);
189 DefineBuiltinMacro(Buf, MacroBuf);
190 sprintf(MacroBuf, "__%s_HAS_QUIET_NAN__=%d", Prefix, HasQuietNaN);
191 DefineBuiltinMacro(Buf, MacroBuf);
192 sprintf(MacroBuf, "__%s_MANT_DIG__=%d", Prefix, MantissaDigits);
193 DefineBuiltinMacro(Buf, MacroBuf);
194 sprintf(MacroBuf, "__%s_MAX_10_EXP__=%d", Prefix, Max10Exp);
195 DefineBuiltinMacro(Buf, MacroBuf);
196 sprintf(MacroBuf, "__%s_MAX_EXP__=%d", Prefix, MaxExp);
197 DefineBuiltinMacro(Buf, MacroBuf);
198 sprintf(MacroBuf, "__%s_MAX__=%s", Prefix, Max);
199 DefineBuiltinMacro(Buf, MacroBuf);
200 sprintf(MacroBuf, "__%s_MIN_10_EXP__=(%d)", Prefix, Min10Exp);
201 DefineBuiltinMacro(Buf, MacroBuf);
202 sprintf(MacroBuf, "__%s_MIN_EXP__=(%d)", Prefix, MinExp);
203 DefineBuiltinMacro(Buf, MacroBuf);
204 sprintf(MacroBuf, "__%s_MIN__=%s", Prefix, Min);
205 DefineBuiltinMacro(Buf, MacroBuf);
206 sprintf(MacroBuf, "__%s_HAS_DENORM__=1", Prefix);
207 DefineBuiltinMacro(Buf, MacroBuf);
208}
209
210
211/// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
212/// named MacroName with the max value for a type with width 'TypeWidth' a
213/// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
214static void DefineTypeSize(const char *MacroName, unsigned TypeWidth,
215 const char *ValSuffix, bool isSigned,
216 std::vector<char> &Buf) {
217 char MacroBuf[60];
218 long long MaxVal;
219 if (isSigned)
220 MaxVal = (1LL << (TypeWidth - 1)) - 1;
221 else
222 MaxVal = ~0LL >> (64-TypeWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Daniel Dunbarb8b844b2009-09-03 19:23:49 +0000224 // FIXME: Switch to using raw_ostream and avoid utostr().
225 sprintf(MacroBuf, "%s=%s%s", MacroName, llvm::utostr(MaxVal).c_str(),
226 ValSuffix);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000227 DefineBuiltinMacro(Buf, MacroBuf);
228}
229
Chris Lattner9099e7b2009-11-05 21:21:32 +0000230/// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
231/// the width, suffix, and signedness of the given type
232static void DefineTypeSize(const char *MacroName, TargetInfo::IntType Ty,
233 const TargetInfo &TI, std::vector<char> &Buf) {
234 DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
235 TI.isTypeSigned(Ty), Buf);
236}
237
Chris Lattnere116ccf2009-04-21 05:40:52 +0000238static void DefineType(const char *MacroName, TargetInfo::IntType Ty,
239 std::vector<char> &Buf) {
240 char MacroBuf[60];
241 sprintf(MacroBuf, "%s=%s", MacroName, TargetInfo::getTypeName(Ty));
242 DefineBuiltinMacro(Buf, MacroBuf);
243}
244
Ken Dyck186696b2009-11-18 13:52:57 +0000245static void DefineTypeWidth(const char *MacroName, TargetInfo::IntType Ty,
246 const TargetInfo &TI, std::vector<char> &Buf) {
247 char MacroBuf[60];
248 sprintf(MacroBuf, "%s=%d", MacroName, TI.getTypeWidth(Ty));
249 DefineBuiltinMacro(Buf, MacroBuf);
250}
251
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000252static void DefineExactWidthIntType(TargetInfo::IntType Ty,
253 const TargetInfo &TI, std::vector<char> &Buf) {
254 char MacroBuf[60];
Ken Dyckeef22ef2009-11-16 16:36:33 +0000255 int TypeWidth = TI.getTypeWidth(Ty);
256 sprintf(MacroBuf, "__INT%d_TYPE__", TypeWidth);
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000257 DefineType(MacroBuf, Ty, Buf);
Ken Dyckeef22ef2009-11-16 16:36:33 +0000258
259
260 const char *ConstSuffix = TargetInfo::getTypeConstantSuffix(Ty);
261 if (strlen(ConstSuffix) > 0) {
262 sprintf(MacroBuf, "__INT%d_C_SUFFIX__=%s", TypeWidth, ConstSuffix);
263 DefineBuiltinMacro(Buf, MacroBuf);
264 }
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000265}
Chris Lattnere116ccf2009-04-21 05:40:52 +0000266
267static void InitializePredefinedMacros(const TargetInfo &TI,
268 const LangOptions &LangOpts,
269 std::vector<char> &Buf) {
270 char MacroBuf[60];
271 // Compiler version introspection macros.
272 DefineBuiltinMacro(Buf, "__llvm__=1"); // LLVM Backend
273 DefineBuiltinMacro(Buf, "__clang__=1"); // Clang Frontend
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Chris Lattnere116ccf2009-04-21 05:40:52 +0000275 // Currently claim to be compatible with GCC 4.2.1-5621.
Chris Lattnere116ccf2009-04-21 05:40:52 +0000276 DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2");
277 DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
278 DefineBuiltinMacro(Buf, "__GNUC__=4");
279 DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
280 DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 Compatible Clang Compiler\"");
Mike Stump1eb44332009-09-09 15:08:12 +0000281
282
Chris Lattnere116ccf2009-04-21 05:40:52 +0000283 // Initialize language-specific preprocessor defines.
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Chris Lattnere116ccf2009-04-21 05:40:52 +0000285 // These should all be defined in the preprocessor according to the
286 // current language configuration.
287 if (!LangOpts.Microsoft)
288 DefineBuiltinMacro(Buf, "__STDC__=1");
289 if (LangOpts.AsmPreprocessor)
290 DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
Ryan Flynn07ef8042009-07-21 00:07:02 +0000291
292 if (!LangOpts.CPlusPlus) {
293 if (LangOpts.C99)
294 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
295 else if (!LangOpts.GNUMode && LangOpts.Digraphs)
296 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
297 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000298
299 // Standard conforming mode?
300 if (!LangOpts.GNUMode)
301 DefineBuiltinMacro(Buf, "__STRICT_ANSI__=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Chris Lattnere116ccf2009-04-21 05:40:52 +0000303 if (LangOpts.CPlusPlus0x)
304 DefineBuiltinMacro(Buf, "__GXX_EXPERIMENTAL_CXX0X__");
305
306 if (LangOpts.Freestanding)
307 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=0");
308 else
309 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Chris Lattnere116ccf2009-04-21 05:40:52 +0000311 if (LangOpts.ObjC1) {
312 DefineBuiltinMacro(Buf, "__OBJC__=1");
313 if (LangOpts.ObjCNonFragileABI) {
314 DefineBuiltinMacro(Buf, "__OBJC2__=1");
315 DefineBuiltinMacro(Buf, "OBJC_ZEROCOST_EXCEPTIONS=1");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000316 }
317
318 if (LangOpts.getGCMode() != LangOptions::NonGC)
319 DefineBuiltinMacro(Buf, "__OBJC_GC__=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Chris Lattnere116ccf2009-04-21 05:40:52 +0000321 if (LangOpts.NeXTRuntime)
322 DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1");
323 }
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Chris Lattnere116ccf2009-04-21 05:40:52 +0000325 // darwin_constant_cfstrings controls this. This is also dependent
326 // on other things like the runtime I believe. This is set even for C code.
327 DefineBuiltinMacro(Buf, "__CONSTANT_CFSTRINGS__=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Chris Lattnere116ccf2009-04-21 05:40:52 +0000329 if (LangOpts.ObjC2)
330 DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES");
331
332 if (LangOpts.PascalStrings)
333 DefineBuiltinMacro(Buf, "__PASCAL_STRINGS__");
334
335 if (LangOpts.Blocks) {
336 DefineBuiltinMacro(Buf, "__block=__attribute__((__blocks__(byref)))");
337 DefineBuiltinMacro(Buf, "__BLOCKS__=1");
338 }
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Rafael Espindolaf759df02009-10-01 13:33:33 +0000340 if (LangOpts.Exceptions)
341 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
342
Chris Lattnere116ccf2009-04-21 05:40:52 +0000343 if (LangOpts.CPlusPlus) {
344 DefineBuiltinMacro(Buf, "__DEPRECATED=1");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000345 DefineBuiltinMacro(Buf, "__GNUG__=4");
346 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
Douglas Gregor36dd1312009-08-06 04:09:28 +0000347 if (LangOpts.GNUMode)
348 DefineBuiltinMacro(Buf, "__cplusplus=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000349 else
Douglas Gregor36dd1312009-08-06 04:09:28 +0000350 // C++ [cpp.predefined]p1:
Mike Stump1eb44332009-09-09 15:08:12 +0000351 // The name_ _cplusplusis defined to the value199711Lwhen compiling a
Douglas Gregor36dd1312009-08-06 04:09:28 +0000352 // C++ translation unit.
353 DefineBuiltinMacro(Buf, "__cplusplus=199711L");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000354 DefineBuiltinMacro(Buf, "__private_extern__=extern");
Eli Friedman666479b2009-08-27 22:01:41 +0000355 // Ugly hack to work with GNU libstdc++.
356 DefineBuiltinMacro(Buf, "_GNU_SOURCE=1");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000357 }
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Chris Lattnere116ccf2009-04-21 05:40:52 +0000359 if (LangOpts.Microsoft) {
John Thompson24ee8042009-10-16 01:12:00 +0000360 // Filter out some microsoft extensions when trying to parse in ms-compat
361 // mode.
Chris Lattnere116ccf2009-04-21 05:40:52 +0000362 DefineBuiltinMacro(Buf, "__int8=__INT8_TYPE__");
363 DefineBuiltinMacro(Buf, "__int16=__INT16_TYPE__");
364 DefineBuiltinMacro(Buf, "__int32=__INT32_TYPE__");
365 DefineBuiltinMacro(Buf, "__int64=__INT64_TYPE__");
John Thompson24ee8042009-10-16 01:12:00 +0000366 // Work around some issues with Visual C++ headerws.
367 if (LangOpts.CPlusPlus) {
368 // Since we define wchar_t in C++ mode.
369 DefineBuiltinMacro(Buf, "_WCHAR_T_DEFINED=1");
370 DefineBuiltinMacro(Buf, "_NATIVE_WCHAR_T_DEFINED=1");
371 // FIXME: This should be temporary until we have a __pragma
372 // solution, to avoid some errors flagged in VC++ headers.
373 DefineBuiltinMacro(Buf, "_CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES=0");
374 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000375 }
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Chris Lattnere116ccf2009-04-21 05:40:52 +0000377 if (LangOpts.Optimize)
378 DefineBuiltinMacro(Buf, "__OPTIMIZE__=1");
379 if (LangOpts.OptimizeSize)
380 DefineBuiltinMacro(Buf, "__OPTIMIZE_SIZE__=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Chris Lattnere116ccf2009-04-21 05:40:52 +0000382 // Initialize target-specific preprocessor defines.
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Chris Lattnere116ccf2009-04-21 05:40:52 +0000384 // Define type sizing macros based on the target properties.
385 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
386 DefineBuiltinMacro(Buf, "__CHAR_BIT__=8");
387
Chris Lattnere116ccf2009-04-21 05:40:52 +0000388 DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Buf);
Chris Lattner9099e7b2009-11-05 21:21:32 +0000389 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Buf);
390 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Buf);
391 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Buf);
392 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Buf);
Chris Lattner91846462009-11-12 08:04:33 +0000393 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Buf);
Chris Lattner9099e7b2009-11-05 21:21:32 +0000394 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Buf);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000395
Ken Dyck8241d732009-11-19 13:18:59 +0000396 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Buf);
397 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Buf);
Ken Dyck186696b2009-11-18 13:52:57 +0000398 DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Buf);
Ken Dyck8241d732009-11-19 13:18:59 +0000399 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Buf);
Ken Dyckd00c7512009-11-19 12:21:52 +0000400 DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Buf);
Ken Dyck8241d732009-11-19 13:18:59 +0000401 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Buf);
Ken Dyckd896e1a2009-11-18 20:05:48 +0000402 DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Buf);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000403 DefineType("__SIZE_TYPE__", TI.getSizeType(), Buf);
Ken Dyck7484e5d2009-11-19 13:42:09 +0000404 DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Buf);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000405 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Buf);
Ken Dyck63e65612009-11-19 15:47:58 +0000406 DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Buf);
Chris Lattnere64ef802009-10-21 04:59:34 +0000407 DefineType("__WINT_TYPE__", TI.getWIntType(), Buf);
Ken Dyck7635d212009-11-19 14:16:57 +0000408 DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Buf);
Ken Dyck3e945c82009-11-22 15:41:04 +0000409 DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Buf);
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Chris Lattnere116ccf2009-04-21 05:40:52 +0000411 DefineFloatMacros(Buf, "FLT", &TI.getFloatFormat());
412 DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat());
413 DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat());
414
415 // Define a __POINTER_WIDTH__ macro for stdint.h.
416 sprintf(MacroBuf, "__POINTER_WIDTH__=%d", (int)TI.getPointerWidth(0));
417 DefineBuiltinMacro(Buf, MacroBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Eli Friedman15b91762009-06-05 07:05:05 +0000419 if (!LangOpts.CharIsSigned)
Mike Stump1eb44332009-09-09 15:08:12 +0000420 DefineBuiltinMacro(Buf, "__CHAR_UNSIGNED__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000421
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000422 // Define exact-width integer types for stdint.h
423 sprintf(MacroBuf, "__INT%d_TYPE__=char", TI.getCharWidth());
424 DefineBuiltinMacro(Buf, MacroBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000426 if (TI.getShortWidth() > TI.getCharWidth())
427 DefineExactWidthIntType(TargetInfo::SignedShort, TI, Buf);
428
429 if (TI.getIntWidth() > TI.getShortWidth())
430 DefineExactWidthIntType(TargetInfo::SignedInt, TI, Buf);
431
432 if (TI.getLongWidth() > TI.getIntWidth())
433 DefineExactWidthIntType(TargetInfo::SignedLong, TI, Buf);
434
435 if (TI.getLongLongWidth() > TI.getLongWidth())
436 DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Buf);
437
Chris Lattnere116ccf2009-04-21 05:40:52 +0000438 // Add __builtin_va_list typedef.
439 {
440 const char *VAList = TI.getVAListDeclaration();
441 Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
442 Buf.push_back('\n');
443 }
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Chris Lattnere116ccf2009-04-21 05:40:52 +0000445 if (const char *Prefix = TI.getUserLabelPrefix()) {
446 sprintf(MacroBuf, "__USER_LABEL_PREFIX__=%s", Prefix);
447 DefineBuiltinMacro(Buf, MacroBuf);
448 }
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Chris Lattnere116ccf2009-04-21 05:40:52 +0000450 // Build configuration options. FIXME: these should be controlled by
451 // command line options or something.
452 DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
453
Chris Lattnere116ccf2009-04-21 05:40:52 +0000454 if (LangOpts.GNUInline)
455 DefineBuiltinMacro(Buf, "__GNUC_GNU_INLINE__=1");
456 else
457 DefineBuiltinMacro(Buf, "__GNUC_STDC_INLINE__=1");
458
459 if (LangOpts.NoInline)
460 DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
461
462 if (unsigned PICLevel = LangOpts.PICLevel) {
463 sprintf(MacroBuf, "__PIC__=%d", PICLevel);
464 DefineBuiltinMacro(Buf, MacroBuf);
465
466 sprintf(MacroBuf, "__pic__=%d", PICLevel);
467 DefineBuiltinMacro(Buf, MacroBuf);
468 }
469
470 // Macros to control C99 numerics and <float.h>
471 DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0");
472 DefineBuiltinMacro(Buf, "__FLT_RADIX__=2");
473 sprintf(MacroBuf, "__DECIMAL_DIG__=%d",
Eli Friedman2665a752009-05-23 03:50:01 +0000474 PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36));
Chris Lattnere116ccf2009-04-21 05:40:52 +0000475 DefineBuiltinMacro(Buf, MacroBuf);
Bill Wendling45483f72009-06-28 07:36:13 +0000476
Bill Wendling4ebe3e42009-06-28 23:01:01 +0000477 if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn)
Bill Wendling45483f72009-06-28 07:36:13 +0000478 DefineBuiltinMacro(Buf, "__SSP__=1");
Bill Wendling4ebe3e42009-06-28 23:01:01 +0000479 else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq)
Bill Wendling45483f72009-06-28 07:36:13 +0000480 DefineBuiltinMacro(Buf, "__SSP_ALL__=2");
481
Chris Lattnere116ccf2009-04-21 05:40:52 +0000482 // Get other target #defines.
483 TI.getTargetDefines(LangOpts, Buf);
484}
485
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000486// Initialize the remapping of files to alternative contents, e.g.,
487// those specified through other files.
488static void InitializeFileRemapping(Diagnostic &Diags,
489 SourceManager &SourceMgr,
490 FileManager &FileMgr,
491 const PreprocessorOptions &InitOpts) {
492 // Remap files in the source manager.
493 for (PreprocessorOptions::remapped_file_iterator
494 Remap = InitOpts.remapped_file_begin(),
495 RemapEnd = InitOpts.remapped_file_end();
496 Remap != RemapEnd;
497 ++Remap) {
498 // Find the file that we're mapping to.
499 const FileEntry *ToFile = FileMgr.getFile(Remap->second);
500 if (!ToFile) {
501 Diags.Report(diag::err_fe_remap_missing_to_file)
502 << Remap->first << Remap->second;
503 continue;
504 }
505
Douglas Gregor057e5672009-12-02 18:12:28 +0000506 // Create the file entry for the file that we're mapping from.
507 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
508 ToFile->getSize(),
509 0);
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000510 if (!FromFile) {
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000511 Diags.Report(diag::err_fe_remap_missing_from_file)
512 << Remap->first;
513 continue;
514 }
515
516 // Load the contents of the file we're mapping to.
517 std::string ErrorStr;
518 const llvm::MemoryBuffer *Buffer
519 = llvm::MemoryBuffer::getFile(ToFile->getName(), &ErrorStr);
520 if (!Buffer) {
521 Diags.Report(diag::err_fe_error_opening)
522 << Remap->second << ErrorStr;
523 continue;
524 }
525
526 // Override the contents of the "from" file with the contents of
527 // the "to" file.
528 SourceMgr.overrideFileContents(FromFile, Buffer);
529 }
530}
531
Chris Lattnere116ccf2009-04-21 05:40:52 +0000532/// InitializePreprocessor - Initialize the preprocessor getting it and the
533/// environment ready to process a single file. This returns true on error.
534///
Daniel Dunbar938963f2009-11-04 21:13:15 +0000535void clang::InitializePreprocessor(Preprocessor &PP,
Daniel Dunbar961c76e2009-11-11 21:44:42 +0000536 const PreprocessorOptions &InitOpts,
537 const HeaderSearchOptions &HSOpts) {
Chris Lattnere116ccf2009-04-21 05:40:52 +0000538 std::vector<char> PredefineBuffer;
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000540 InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(),
541 PP.getFileManager(), InitOpts);
542
Chris Lattner39360242009-04-22 03:42:19 +0000543 const char *LineDirective = "# 1 \"<built-in>\" 3\n";
544 PredefineBuffer.insert(PredefineBuffer.end(),
545 LineDirective, LineDirective+strlen(LineDirective));
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Chris Lattnere116ccf2009-04-21 05:40:52 +0000547 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000548 if (InitOpts.UsePredefines)
Chris Lattnere6113de2009-11-03 19:50:27 +0000549 InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(),
550 PredefineBuffer);
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Chris Lattnere116ccf2009-04-21 05:40:52 +0000552 // Add on the predefines from the driver. Wrap in a #line directive to report
553 // that they come from the command line.
Chris Lattner39360242009-04-22 03:42:19 +0000554 LineDirective = "# 1 \"<command line>\" 1\n";
Chris Lattnere116ccf2009-04-21 05:40:52 +0000555 PredefineBuffer.insert(PredefineBuffer.end(),
556 LineDirective, LineDirective+strlen(LineDirective));
557
558 // Process #define's and #undef's in the order they are given.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000559 for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
560 if (InitOpts.Macros[i].second) // isUndef
561 UndefineBuiltinMacro(PredefineBuffer, InitOpts.Macros[i].first.c_str());
Chris Lattner62f86c42009-04-21 06:00:24 +0000562 else
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000563 DefineBuiltinMacro(PredefineBuffer, InitOpts.Macros[i].first.c_str());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000564 }
565
566 // If -imacros are specified, include them now. These are processed before
567 // any -include directives.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000568 for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
569 AddImplicitIncludeMacros(PredefineBuffer, InitOpts.MacroIncludes[i]);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000570
571 // Process -include directives.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000572 for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
573 const std::string &Path = InitOpts.Includes[i];
574 if (Path == InitOpts.ImplicitPTHInclude)
575 AddImplicitIncludePTH(PredefineBuffer, PP, Path);
Chris Lattner62f86c42009-04-21 06:00:24 +0000576 else
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000577 AddImplicitInclude(PredefineBuffer, Path);
Eli Friedmanae96a962009-06-15 09:57:52 +0000578 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000579
Rafael Espindola0259d202009-12-01 18:28:16 +0000580 // Exit the command line and go back to <built-in> (2 is LC_LEAVE).
581 LineDirective = "# 1 \"<built-in>\" 2\n";
582 PredefineBuffer.insert(PredefineBuffer.end(),
583 LineDirective, LineDirective+strlen(LineDirective));
584
Chris Lattnere116ccf2009-04-21 05:40:52 +0000585 // Null terminate PredefinedBuffer and add it.
586 PredefineBuffer.push_back(0);
587 PP.setPredefines(&PredefineBuffer[0]);
Daniel Dunbar961c76e2009-11-11 21:44:42 +0000588
589 // Initialize the header search object.
590 ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts,
591 PP.getLangOptions(),
592 PP.getTargetInfo().getTriple());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000593}