blob: 69680491319d41411abf71fea5f5801ba167cd03 [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"
Daniel Dunbar8863b982009-11-07 04:20:15 +000016#include "clang/Frontend/PreprocessorOptions.h"
Chris Lattnere116ccf2009-04-21 05:40:52 +000017#include "clang/Lex/Preprocessor.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/System/Path.h"
Chris Lattner47c06ee2009-11-02 21:48:09 +000021using namespace clang;
Chris Lattnere116ccf2009-04-21 05:40:52 +000022
23// Append a #define line to Buf for Macro. Macro should be of the form XXX,
24// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
25// "#define XXX Y z W". To get a #define with no value, use "XXX=".
Chris Lattner810dc542009-06-04 16:47:09 +000026static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro) {
27 const char *Command = "#define ";
Chris Lattnere116ccf2009-04-21 05:40:52 +000028 Buf.insert(Buf.end(), Command, Command+strlen(Command));
29 if (const char *Equal = strchr(Macro, '=')) {
30 // Turn the = into ' '.
31 Buf.insert(Buf.end(), Macro, Equal);
32 Buf.push_back(' ');
Mike Stump1eb44332009-09-09 15:08:12 +000033
Chris Lattnere116ccf2009-04-21 05:40:52 +000034 // Per GCC -D semantics, the macro ends at \n if it exists.
35 const char *End = strpbrk(Equal, "\n\r");
36 if (End) {
37 fprintf(stderr, "warning: macro '%s' contains embedded newline, text "
38 "after the newline is ignored.\n",
39 std::string(Macro, Equal).c_str());
40 } else {
41 End = Equal+strlen(Equal);
42 }
Mike Stump1eb44332009-09-09 15:08:12 +000043
Chris Lattnere116ccf2009-04-21 05:40:52 +000044 Buf.insert(Buf.end(), Equal+1, End);
45 } else {
46 // Push "macroname 1".
47 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
48 Buf.push_back(' ');
49 Buf.push_back('1');
50 }
51 Buf.push_back('\n');
52}
53
Chris Lattnerdcdecf42009-05-15 16:08:43 +000054// Append a #undef line to Buf for Macro. Macro should be of the form XXX
55// and we emit "#undef XXX".
56static void UndefineBuiltinMacro(std::vector<char> &Buf, const char *Macro) {
57 // Push "macroname".
58 const char *Command = "#undef ";
59 Buf.insert(Buf.end(), Command, Command+strlen(Command));
60 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
61 Buf.push_back('\n');
62}
63
Daniel Dunbarc7162932009-11-11 23:58:53 +000064std::string clang::NormalizeDashIncludePath(llvm::StringRef File) {
Daniel Dunbar1e7c6b62009-04-22 08:53:01 +000065 // Implicit include paths should be resolved relative to the current
66 // working directory first, and then use the regular header search
67 // mechanism. The proper way to handle this is to have the
68 // predefines buffer located at the current working directory, but
69 // it has not file entry. For now, workaround this by using an
70 // absolute path if we find the file here, and otherwise letting
71 // header search handle it.
Chris Lattnere116ccf2009-04-21 05:40:52 +000072 llvm::sys::Path Path(File);
73 Path.makeAbsolute();
Daniel Dunbar1e7c6b62009-04-22 08:53:01 +000074 if (!Path.exists())
75 Path = File;
Mike Stump1eb44332009-09-09 15:08:12 +000076
Daniel Dunbarc7162932009-11-11 23:58:53 +000077 return Lexer::Stringify(Path.str());
78}
79
80/// Add the quoted name of an implicit include file.
81static void AddQuotedIncludePath(std::vector<char> &Buf,
82 const std::string &File) {
83
Chris Lattnere116ccf2009-04-21 05:40:52 +000084 // Escape double quotes etc.
85 Buf.push_back('"');
Daniel Dunbarc7162932009-11-11 23:58:53 +000086 std::string EscapedFile = NormalizeDashIncludePath(File);
Chris Lattnere116ccf2009-04-21 05:40:52 +000087 Buf.insert(Buf.end(), EscapedFile.begin(), EscapedFile.end());
88 Buf.push_back('"');
89}
90
91/// AddImplicitInclude - Add an implicit #include of the specified file to the
92/// predefines buffer.
Mike Stump1eb44332009-09-09 15:08:12 +000093static void AddImplicitInclude(std::vector<char> &Buf,
Chris Lattnere116ccf2009-04-21 05:40:52 +000094 const std::string &File) {
95 const char *Inc = "#include ";
96 Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
97 AddQuotedIncludePath(Buf, File);
98 Buf.push_back('\n');
99}
100
101static void AddImplicitIncludeMacros(std::vector<char> &Buf,
102 const std::string &File) {
103 const char *Inc = "#__include_macros ";
104 Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
105 AddQuotedIncludePath(Buf, File);
106 Buf.push_back('\n');
107 // Marker token to stop the __include_macros fetch loop.
108 const char *Marker = "##\n"; // ##?
109 Buf.insert(Buf.end(), Marker, Marker+strlen(Marker));
110}
111
112/// AddImplicitIncludePTH - Add an implicit #include using the original file
113/// used to generate a PTH cache.
Mike Stump1eb44332009-09-09 15:08:12 +0000114static void AddImplicitIncludePTH(std::vector<char> &Buf, Preprocessor &PP,
Chris Lattnere116ccf2009-04-21 05:40:52 +0000115 const std::string& ImplicitIncludePTH) {
116 PTHManager *P = PP.getPTHManager();
117 assert(P && "No PTHManager.");
118 const char *OriginalFile = P->getOriginalSourceFile();
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Chris Lattnere116ccf2009-04-21 05:40:52 +0000120 if (!OriginalFile) {
121 assert(!ImplicitIncludePTH.empty());
122 fprintf(stderr, "error: PTH file '%s' does not designate an original "
123 "source header file for -include-pth\n",
124 ImplicitIncludePTH.c_str());
125 exit (1);
126 }
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Chris Lattnere116ccf2009-04-21 05:40:52 +0000128 AddImplicitInclude(Buf, OriginalFile);
129}
130
131/// PickFP - This is used to pick a value based on the FP semantics of the
132/// specified FP model.
133template <typename T>
134static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal,
Eli Friedman2665a752009-05-23 03:50:01 +0000135 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
136 T IEEEQuadVal) {
Duncan Sands63682f92009-06-03 14:28:20 +0000137 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000138 return IEEESingleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000139 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000140 return IEEEDoubleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000141 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000142 return X87DoubleExtendedVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000143 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble)
Eli Friedman2665a752009-05-23 03:50:01 +0000144 return PPCDoubleDoubleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000145 assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad);
Eli Friedman2665a752009-05-23 03:50:01 +0000146 return IEEEQuadVal;
Chris Lattnere116ccf2009-04-21 05:40:52 +0000147}
148
149static void DefineFloatMacros(std::vector<char> &Buf, const char *Prefix,
150 const llvm::fltSemantics *Sem) {
151 const char *DenormMin, *Epsilon, *Max, *Min;
Mike Stump1eb44332009-09-09 15:08:12 +0000152 DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324",
Chris Lattnere116ccf2009-04-21 05:40:52 +0000153 "3.64519953188247460253e-4951L",
Eli Friedman2665a752009-05-23 03:50:01 +0000154 "4.94065645841246544176568792868221e-324L",
155 "6.47517511943802511092443895822764655e-4966L");
156 int Digits = PickFP(Sem, 6, 15, 18, 31, 33);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000157 Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16",
158 "1.08420217248550443401e-19L",
Eli Friedman2665a752009-05-23 03:50:01 +0000159 "4.94065645841246544176568792868221e-324L",
160 "1.92592994438723585305597794258492732e-34L");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000161 int HasInifinity = 1, HasQuietNaN = 1;
Eli Friedman2665a752009-05-23 03:50:01 +0000162 int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113);
163 int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931);
164 int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932);
165 int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381);
166 int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000167 Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308",
168 "3.36210314311209350626e-4932L",
Eli Friedman2665a752009-05-23 03:50:01 +0000169 "2.00416836000897277799610805135016e-292L",
170 "3.36210314311209350626267781732175260e-4932L");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000171 Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308",
172 "1.18973149535723176502e+4932L",
Eli Friedman2665a752009-05-23 03:50:01 +0000173 "1.79769313486231580793728971405301e+308L",
174 "1.18973149535723176508575932662800702e+4932L");
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Eli Friedman2665a752009-05-23 03:50:01 +0000176 char MacroBuf[100];
Chris Lattnere116ccf2009-04-21 05:40:52 +0000177 sprintf(MacroBuf, "__%s_DENORM_MIN__=%s", Prefix, DenormMin);
178 DefineBuiltinMacro(Buf, MacroBuf);
179 sprintf(MacroBuf, "__%s_DIG__=%d", Prefix, Digits);
180 DefineBuiltinMacro(Buf, MacroBuf);
181 sprintf(MacroBuf, "__%s_EPSILON__=%s", Prefix, Epsilon);
182 DefineBuiltinMacro(Buf, MacroBuf);
183 sprintf(MacroBuf, "__%s_HAS_INFINITY__=%d", Prefix, HasInifinity);
184 DefineBuiltinMacro(Buf, MacroBuf);
185 sprintf(MacroBuf, "__%s_HAS_QUIET_NAN__=%d", Prefix, HasQuietNaN);
186 DefineBuiltinMacro(Buf, MacroBuf);
187 sprintf(MacroBuf, "__%s_MANT_DIG__=%d", Prefix, MantissaDigits);
188 DefineBuiltinMacro(Buf, MacroBuf);
189 sprintf(MacroBuf, "__%s_MAX_10_EXP__=%d", Prefix, Max10Exp);
190 DefineBuiltinMacro(Buf, MacroBuf);
191 sprintf(MacroBuf, "__%s_MAX_EXP__=%d", Prefix, MaxExp);
192 DefineBuiltinMacro(Buf, MacroBuf);
193 sprintf(MacroBuf, "__%s_MAX__=%s", Prefix, Max);
194 DefineBuiltinMacro(Buf, MacroBuf);
195 sprintf(MacroBuf, "__%s_MIN_10_EXP__=(%d)", Prefix, Min10Exp);
196 DefineBuiltinMacro(Buf, MacroBuf);
197 sprintf(MacroBuf, "__%s_MIN_EXP__=(%d)", Prefix, MinExp);
198 DefineBuiltinMacro(Buf, MacroBuf);
199 sprintf(MacroBuf, "__%s_MIN__=%s", Prefix, Min);
200 DefineBuiltinMacro(Buf, MacroBuf);
201 sprintf(MacroBuf, "__%s_HAS_DENORM__=1", Prefix);
202 DefineBuiltinMacro(Buf, MacroBuf);
203}
204
205
206/// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
207/// named MacroName with the max value for a type with width 'TypeWidth' a
208/// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
209static void DefineTypeSize(const char *MacroName, unsigned TypeWidth,
210 const char *ValSuffix, bool isSigned,
211 std::vector<char> &Buf) {
212 char MacroBuf[60];
213 long long MaxVal;
214 if (isSigned)
215 MaxVal = (1LL << (TypeWidth - 1)) - 1;
216 else
217 MaxVal = ~0LL >> (64-TypeWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Daniel Dunbarb8b844b2009-09-03 19:23:49 +0000219 // FIXME: Switch to using raw_ostream and avoid utostr().
220 sprintf(MacroBuf, "%s=%s%s", MacroName, llvm::utostr(MaxVal).c_str(),
221 ValSuffix);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000222 DefineBuiltinMacro(Buf, MacroBuf);
223}
224
Chris Lattner9099e7b2009-11-05 21:21:32 +0000225/// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
226/// the width, suffix, and signedness of the given type
227static void DefineTypeSize(const char *MacroName, TargetInfo::IntType Ty,
228 const TargetInfo &TI, std::vector<char> &Buf) {
229 DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
230 TI.isTypeSigned(Ty), Buf);
231}
232
Chris Lattnere116ccf2009-04-21 05:40:52 +0000233static void DefineType(const char *MacroName, TargetInfo::IntType Ty,
234 std::vector<char> &Buf) {
235 char MacroBuf[60];
236 sprintf(MacroBuf, "%s=%s", MacroName, TargetInfo::getTypeName(Ty));
237 DefineBuiltinMacro(Buf, MacroBuf);
238}
239
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000240static void DefineExactWidthIntType(TargetInfo::IntType Ty,
241 const TargetInfo &TI, std::vector<char> &Buf) {
242 char MacroBuf[60];
243 sprintf(MacroBuf, "__INT%d_TYPE__", TI.getTypeWidth(Ty));
244 DefineType(MacroBuf, Ty, Buf);
245}
Chris Lattnere116ccf2009-04-21 05:40:52 +0000246
247static void InitializePredefinedMacros(const TargetInfo &TI,
248 const LangOptions &LangOpts,
249 std::vector<char> &Buf) {
250 char MacroBuf[60];
251 // Compiler version introspection macros.
252 DefineBuiltinMacro(Buf, "__llvm__=1"); // LLVM Backend
253 DefineBuiltinMacro(Buf, "__clang__=1"); // Clang Frontend
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Chris Lattnere116ccf2009-04-21 05:40:52 +0000255 // Currently claim to be compatible with GCC 4.2.1-5621.
Chris Lattnere116ccf2009-04-21 05:40:52 +0000256 DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2");
257 DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
258 DefineBuiltinMacro(Buf, "__GNUC__=4");
259 DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
260 DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 Compatible Clang Compiler\"");
Mike Stump1eb44332009-09-09 15:08:12 +0000261
262
Chris Lattnere116ccf2009-04-21 05:40:52 +0000263 // Initialize language-specific preprocessor defines.
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Chris Lattnere116ccf2009-04-21 05:40:52 +0000265 // These should all be defined in the preprocessor according to the
266 // current language configuration.
267 if (!LangOpts.Microsoft)
268 DefineBuiltinMacro(Buf, "__STDC__=1");
269 if (LangOpts.AsmPreprocessor)
270 DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
Ryan Flynn07ef8042009-07-21 00:07:02 +0000271
272 if (!LangOpts.CPlusPlus) {
273 if (LangOpts.C99)
274 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
275 else if (!LangOpts.GNUMode && LangOpts.Digraphs)
276 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
277 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000278
279 // Standard conforming mode?
280 if (!LangOpts.GNUMode)
281 DefineBuiltinMacro(Buf, "__STRICT_ANSI__=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Chris Lattnere116ccf2009-04-21 05:40:52 +0000283 if (LangOpts.CPlusPlus0x)
284 DefineBuiltinMacro(Buf, "__GXX_EXPERIMENTAL_CXX0X__");
285
286 if (LangOpts.Freestanding)
287 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=0");
288 else
289 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Chris Lattnere116ccf2009-04-21 05:40:52 +0000291 if (LangOpts.ObjC1) {
292 DefineBuiltinMacro(Buf, "__OBJC__=1");
293 if (LangOpts.ObjCNonFragileABI) {
294 DefineBuiltinMacro(Buf, "__OBJC2__=1");
295 DefineBuiltinMacro(Buf, "OBJC_ZEROCOST_EXCEPTIONS=1");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000296 }
297
298 if (LangOpts.getGCMode() != LangOptions::NonGC)
299 DefineBuiltinMacro(Buf, "__OBJC_GC__=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Chris Lattnere116ccf2009-04-21 05:40:52 +0000301 if (LangOpts.NeXTRuntime)
302 DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1");
303 }
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Chris Lattnere116ccf2009-04-21 05:40:52 +0000305 // darwin_constant_cfstrings controls this. This is also dependent
306 // on other things like the runtime I believe. This is set even for C code.
307 DefineBuiltinMacro(Buf, "__CONSTANT_CFSTRINGS__=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Chris Lattnere116ccf2009-04-21 05:40:52 +0000309 if (LangOpts.ObjC2)
310 DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES");
311
312 if (LangOpts.PascalStrings)
313 DefineBuiltinMacro(Buf, "__PASCAL_STRINGS__");
314
315 if (LangOpts.Blocks) {
316 DefineBuiltinMacro(Buf, "__block=__attribute__((__blocks__(byref)))");
317 DefineBuiltinMacro(Buf, "__BLOCKS__=1");
318 }
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Rafael Espindolaf759df02009-10-01 13:33:33 +0000320 if (LangOpts.Exceptions)
321 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
322
Chris Lattnere116ccf2009-04-21 05:40:52 +0000323 if (LangOpts.CPlusPlus) {
324 DefineBuiltinMacro(Buf, "__DEPRECATED=1");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000325 DefineBuiltinMacro(Buf, "__GNUG__=4");
326 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
Douglas Gregor36dd1312009-08-06 04:09:28 +0000327 if (LangOpts.GNUMode)
328 DefineBuiltinMacro(Buf, "__cplusplus=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000329 else
Douglas Gregor36dd1312009-08-06 04:09:28 +0000330 // C++ [cpp.predefined]p1:
Mike Stump1eb44332009-09-09 15:08:12 +0000331 // The name_ _cplusplusis defined to the value199711Lwhen compiling a
Douglas Gregor36dd1312009-08-06 04:09:28 +0000332 // C++ translation unit.
333 DefineBuiltinMacro(Buf, "__cplusplus=199711L");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000334 DefineBuiltinMacro(Buf, "__private_extern__=extern");
Eli Friedman666479b2009-08-27 22:01:41 +0000335 // Ugly hack to work with GNU libstdc++.
336 DefineBuiltinMacro(Buf, "_GNU_SOURCE=1");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000337 }
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Chris Lattnere116ccf2009-04-21 05:40:52 +0000339 if (LangOpts.Microsoft) {
John Thompson24ee8042009-10-16 01:12:00 +0000340 // Filter out some microsoft extensions when trying to parse in ms-compat
341 // mode.
Chris Lattnere116ccf2009-04-21 05:40:52 +0000342 DefineBuiltinMacro(Buf, "__int8=__INT8_TYPE__");
343 DefineBuiltinMacro(Buf, "__int16=__INT16_TYPE__");
344 DefineBuiltinMacro(Buf, "__int32=__INT32_TYPE__");
345 DefineBuiltinMacro(Buf, "__int64=__INT64_TYPE__");
John Thompson24ee8042009-10-16 01:12:00 +0000346 // Work around some issues with Visual C++ headerws.
347 if (LangOpts.CPlusPlus) {
348 // Since we define wchar_t in C++ mode.
349 DefineBuiltinMacro(Buf, "_WCHAR_T_DEFINED=1");
350 DefineBuiltinMacro(Buf, "_NATIVE_WCHAR_T_DEFINED=1");
351 // FIXME: This should be temporary until we have a __pragma
352 // solution, to avoid some errors flagged in VC++ headers.
353 DefineBuiltinMacro(Buf, "_CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES=0");
354 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000355 }
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Chris Lattnere116ccf2009-04-21 05:40:52 +0000357 if (LangOpts.Optimize)
358 DefineBuiltinMacro(Buf, "__OPTIMIZE__=1");
359 if (LangOpts.OptimizeSize)
360 DefineBuiltinMacro(Buf, "__OPTIMIZE_SIZE__=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Chris Lattnere116ccf2009-04-21 05:40:52 +0000362 // Initialize target-specific preprocessor defines.
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Chris Lattnere116ccf2009-04-21 05:40:52 +0000364 // Define type sizing macros based on the target properties.
365 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
366 DefineBuiltinMacro(Buf, "__CHAR_BIT__=8");
367
Chris Lattnere116ccf2009-04-21 05:40:52 +0000368 DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Buf);
Chris Lattner9099e7b2009-11-05 21:21:32 +0000369 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Buf);
370 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Buf);
371 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Buf);
372 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Buf);
Chris Lattner91846462009-11-12 08:04:33 +0000373 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Buf);
Chris Lattner9099e7b2009-11-05 21:21:32 +0000374 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Buf);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000375
376 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Buf);
377 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Buf);
378 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Buf);
379 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Buf);
380 DefineType("__SIZE_TYPE__", TI.getSizeType(), Buf);
381 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Buf);
Chris Lattnere64ef802009-10-21 04:59:34 +0000382 DefineType("__WINT_TYPE__", TI.getWIntType(), Buf);
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Chris Lattnere116ccf2009-04-21 05:40:52 +0000384 DefineFloatMacros(Buf, "FLT", &TI.getFloatFormat());
385 DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat());
386 DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat());
387
388 // Define a __POINTER_WIDTH__ macro for stdint.h.
389 sprintf(MacroBuf, "__POINTER_WIDTH__=%d", (int)TI.getPointerWidth(0));
390 DefineBuiltinMacro(Buf, MacroBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Eli Friedman15b91762009-06-05 07:05:05 +0000392 if (!LangOpts.CharIsSigned)
Mike Stump1eb44332009-09-09 15:08:12 +0000393 DefineBuiltinMacro(Buf, "__CHAR_UNSIGNED__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000394
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000395 // Define exact-width integer types for stdint.h
396 sprintf(MacroBuf, "__INT%d_TYPE__=char", TI.getCharWidth());
397 DefineBuiltinMacro(Buf, MacroBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000399 if (TI.getShortWidth() > TI.getCharWidth())
400 DefineExactWidthIntType(TargetInfo::SignedShort, TI, Buf);
401
402 if (TI.getIntWidth() > TI.getShortWidth())
403 DefineExactWidthIntType(TargetInfo::SignedInt, TI, Buf);
404
405 if (TI.getLongWidth() > TI.getIntWidth())
406 DefineExactWidthIntType(TargetInfo::SignedLong, TI, Buf);
407
408 if (TI.getLongLongWidth() > TI.getLongWidth())
409 DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Buf);
410
Chris Lattnere116ccf2009-04-21 05:40:52 +0000411 // Add __builtin_va_list typedef.
412 {
413 const char *VAList = TI.getVAListDeclaration();
414 Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
415 Buf.push_back('\n');
416 }
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Chris Lattnere116ccf2009-04-21 05:40:52 +0000418 if (const char *Prefix = TI.getUserLabelPrefix()) {
419 sprintf(MacroBuf, "__USER_LABEL_PREFIX__=%s", Prefix);
420 DefineBuiltinMacro(Buf, MacroBuf);
421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Chris Lattnere116ccf2009-04-21 05:40:52 +0000423 // Build configuration options. FIXME: these should be controlled by
424 // command line options or something.
425 DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
426
Chris Lattnere116ccf2009-04-21 05:40:52 +0000427 if (LangOpts.GNUInline)
428 DefineBuiltinMacro(Buf, "__GNUC_GNU_INLINE__=1");
429 else
430 DefineBuiltinMacro(Buf, "__GNUC_STDC_INLINE__=1");
431
432 if (LangOpts.NoInline)
433 DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
434
435 if (unsigned PICLevel = LangOpts.PICLevel) {
436 sprintf(MacroBuf, "__PIC__=%d", PICLevel);
437 DefineBuiltinMacro(Buf, MacroBuf);
438
439 sprintf(MacroBuf, "__pic__=%d", PICLevel);
440 DefineBuiltinMacro(Buf, MacroBuf);
441 }
442
443 // Macros to control C99 numerics and <float.h>
444 DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0");
445 DefineBuiltinMacro(Buf, "__FLT_RADIX__=2");
446 sprintf(MacroBuf, "__DECIMAL_DIG__=%d",
Eli Friedman2665a752009-05-23 03:50:01 +0000447 PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36));
Chris Lattnere116ccf2009-04-21 05:40:52 +0000448 DefineBuiltinMacro(Buf, MacroBuf);
Bill Wendling45483f72009-06-28 07:36:13 +0000449
Bill Wendling4ebe3e42009-06-28 23:01:01 +0000450 if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn)
Bill Wendling45483f72009-06-28 07:36:13 +0000451 DefineBuiltinMacro(Buf, "__SSP__=1");
Bill Wendling4ebe3e42009-06-28 23:01:01 +0000452 else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq)
Bill Wendling45483f72009-06-28 07:36:13 +0000453 DefineBuiltinMacro(Buf, "__SSP_ALL__=2");
454
Chris Lattnere116ccf2009-04-21 05:40:52 +0000455 // Get other target #defines.
456 TI.getTargetDefines(LangOpts, Buf);
457}
458
459/// InitializePreprocessor - Initialize the preprocessor getting it and the
460/// environment ready to process a single file. This returns true on error.
461///
Daniel Dunbar938963f2009-11-04 21:13:15 +0000462void clang::InitializePreprocessor(Preprocessor &PP,
Daniel Dunbar961c76e2009-11-11 21:44:42 +0000463 const PreprocessorOptions &InitOpts,
464 const HeaderSearchOptions &HSOpts) {
Chris Lattnere116ccf2009-04-21 05:40:52 +0000465 std::vector<char> PredefineBuffer;
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Chris Lattner39360242009-04-22 03:42:19 +0000467 const char *LineDirective = "# 1 \"<built-in>\" 3\n";
468 PredefineBuffer.insert(PredefineBuffer.end(),
469 LineDirective, LineDirective+strlen(LineDirective));
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Chris Lattnere116ccf2009-04-21 05:40:52 +0000471 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
Daniel Dunbar468fe242009-11-04 21:13:02 +0000472 if (InitOpts.getUsePredefines())
Chris Lattnere6113de2009-11-03 19:50:27 +0000473 InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(),
474 PredefineBuffer);
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Chris Lattnere116ccf2009-04-21 05:40:52 +0000476 // Add on the predefines from the driver. Wrap in a #line directive to report
477 // that they come from the command line.
Chris Lattner39360242009-04-22 03:42:19 +0000478 LineDirective = "# 1 \"<command line>\" 1\n";
Chris Lattnere116ccf2009-04-21 05:40:52 +0000479 PredefineBuffer.insert(PredefineBuffer.end(),
480 LineDirective, LineDirective+strlen(LineDirective));
481
482 // Process #define's and #undef's in the order they are given.
Daniel Dunbar8863b982009-11-07 04:20:15 +0000483 for (PreprocessorOptions::macro_iterator I = InitOpts.macro_begin(),
Chris Lattnere116ccf2009-04-21 05:40:52 +0000484 E = InitOpts.macro_end(); I != E; ++I) {
Chris Lattner62f86c42009-04-21 06:00:24 +0000485 if (I->second) // isUndef
Chris Lattnerdcdecf42009-05-15 16:08:43 +0000486 UndefineBuiltinMacro(PredefineBuffer, I->first.c_str());
Chris Lattner62f86c42009-04-21 06:00:24 +0000487 else
488 DefineBuiltinMacro(PredefineBuffer, I->first.c_str());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000489 }
490
491 // If -imacros are specified, include them now. These are processed before
492 // any -include directives.
Daniel Dunbar8863b982009-11-07 04:20:15 +0000493 for (PreprocessorOptions::imacro_iterator I = InitOpts.imacro_begin(),
Chris Lattnere116ccf2009-04-21 05:40:52 +0000494 E = InitOpts.imacro_end(); I != E; ++I)
495 AddImplicitIncludeMacros(PredefineBuffer, *I);
496
497 // Process -include directives.
Daniel Dunbar8863b982009-11-07 04:20:15 +0000498 for (PreprocessorOptions::include_iterator I = InitOpts.include_begin(),
Chris Lattnere116ccf2009-04-21 05:40:52 +0000499 E = InitOpts.include_end(); I != E; ++I) {
Daniel Dunbarb6d1cc82009-11-10 23:53:43 +0000500 if (*I == InitOpts.getImplicitPTHInclude())
501 AddImplicitIncludePTH(PredefineBuffer, PP, *I);
Chris Lattner62f86c42009-04-21 06:00:24 +0000502 else
Daniel Dunbarb6d1cc82009-11-10 23:53:43 +0000503 AddImplicitInclude(PredefineBuffer, *I);
Eli Friedmanae96a962009-06-15 09:57:52 +0000504 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000505
506 // Null terminate PredefinedBuffer and add it.
507 PredefineBuffer.push_back(0);
508 PP.setPredefines(&PredefineBuffer[0]);
Daniel Dunbar961c76e2009-11-11 21:44:42 +0000509
510 // Initialize the header search object.
511 ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts,
512 PP.getLangOptions(),
513 PP.getTargetInfo().getTriple());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000514}