blob: ad70727c05e6a6ec6a07524aa1e3d71390e436de [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
Chris Lattnere116ccf2009-04-21 05:40:52 +000064/// Add the quoted name of an implicit include file.
Mike Stump1eb44332009-09-09 15:08:12 +000065static void AddQuotedIncludePath(std::vector<char> &Buf,
Chris Lattnere116ccf2009-04-21 05:40:52 +000066 const std::string &File) {
Daniel Dunbar1e7c6b62009-04-22 08:53:01 +000067 // Implicit include paths should be resolved relative to the current
68 // working directory first, and then use the regular header search
69 // mechanism. The proper way to handle this is to have the
70 // predefines buffer located at the current working directory, but
71 // it has not file entry. For now, workaround this by using an
72 // absolute path if we find the file here, and otherwise letting
73 // header search handle it.
Chris Lattnere116ccf2009-04-21 05:40:52 +000074 llvm::sys::Path Path(File);
75 Path.makeAbsolute();
Daniel Dunbar1e7c6b62009-04-22 08:53:01 +000076 if (!Path.exists())
77 Path = File;
Mike Stump1eb44332009-09-09 15:08:12 +000078
Chris Lattnere116ccf2009-04-21 05:40:52 +000079 // Escape double quotes etc.
80 Buf.push_back('"');
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000081 std::string EscapedFile = Lexer::Stringify(Path.str());
Chris Lattnere116ccf2009-04-21 05:40:52 +000082 Buf.insert(Buf.end(), EscapedFile.begin(), EscapedFile.end());
83 Buf.push_back('"');
84}
85
86/// AddImplicitInclude - Add an implicit #include of the specified file to the
87/// predefines buffer.
Mike Stump1eb44332009-09-09 15:08:12 +000088static void AddImplicitInclude(std::vector<char> &Buf,
Chris Lattnere116ccf2009-04-21 05:40:52 +000089 const std::string &File) {
90 const char *Inc = "#include ";
91 Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
92 AddQuotedIncludePath(Buf, File);
93 Buf.push_back('\n');
94}
95
96static void AddImplicitIncludeMacros(std::vector<char> &Buf,
97 const std::string &File) {
98 const char *Inc = "#__include_macros ";
99 Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
100 AddQuotedIncludePath(Buf, File);
101 Buf.push_back('\n');
102 // Marker token to stop the __include_macros fetch loop.
103 const char *Marker = "##\n"; // ##?
104 Buf.insert(Buf.end(), Marker, Marker+strlen(Marker));
105}
106
107/// AddImplicitIncludePTH - Add an implicit #include using the original file
108/// used to generate a PTH cache.
Mike Stump1eb44332009-09-09 15:08:12 +0000109static void AddImplicitIncludePTH(std::vector<char> &Buf, Preprocessor &PP,
Chris Lattnere116ccf2009-04-21 05:40:52 +0000110 const std::string& ImplicitIncludePTH) {
111 PTHManager *P = PP.getPTHManager();
112 assert(P && "No PTHManager.");
113 const char *OriginalFile = P->getOriginalSourceFile();
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Chris Lattnere116ccf2009-04-21 05:40:52 +0000115 if (!OriginalFile) {
116 assert(!ImplicitIncludePTH.empty());
117 fprintf(stderr, "error: PTH file '%s' does not designate an original "
118 "source header file for -include-pth\n",
119 ImplicitIncludePTH.c_str());
120 exit (1);
121 }
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Chris Lattnere116ccf2009-04-21 05:40:52 +0000123 AddImplicitInclude(Buf, OriginalFile);
124}
125
126/// PickFP - This is used to pick a value based on the FP semantics of the
127/// specified FP model.
128template <typename T>
129static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal,
Eli Friedman2665a752009-05-23 03:50:01 +0000130 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
131 T IEEEQuadVal) {
Duncan Sands63682f92009-06-03 14:28:20 +0000132 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000133 return IEEESingleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000134 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000135 return IEEEDoubleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000136 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000137 return X87DoubleExtendedVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000138 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble)
Eli Friedman2665a752009-05-23 03:50:01 +0000139 return PPCDoubleDoubleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000140 assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad);
Eli Friedman2665a752009-05-23 03:50:01 +0000141 return IEEEQuadVal;
Chris Lattnere116ccf2009-04-21 05:40:52 +0000142}
143
144static void DefineFloatMacros(std::vector<char> &Buf, const char *Prefix,
145 const llvm::fltSemantics *Sem) {
146 const char *DenormMin, *Epsilon, *Max, *Min;
Mike Stump1eb44332009-09-09 15:08:12 +0000147 DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324",
Chris Lattnere116ccf2009-04-21 05:40:52 +0000148 "3.64519953188247460253e-4951L",
Eli Friedman2665a752009-05-23 03:50:01 +0000149 "4.94065645841246544176568792868221e-324L",
150 "6.47517511943802511092443895822764655e-4966L");
151 int Digits = PickFP(Sem, 6, 15, 18, 31, 33);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000152 Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16",
153 "1.08420217248550443401e-19L",
Eli Friedman2665a752009-05-23 03:50:01 +0000154 "4.94065645841246544176568792868221e-324L",
155 "1.92592994438723585305597794258492732e-34L");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000156 int HasInifinity = 1, HasQuietNaN = 1;
Eli Friedman2665a752009-05-23 03:50:01 +0000157 int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113);
158 int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931);
159 int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932);
160 int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381);
161 int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000162 Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308",
163 "3.36210314311209350626e-4932L",
Eli Friedman2665a752009-05-23 03:50:01 +0000164 "2.00416836000897277799610805135016e-292L",
165 "3.36210314311209350626267781732175260e-4932L");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000166 Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308",
167 "1.18973149535723176502e+4932L",
Eli Friedman2665a752009-05-23 03:50:01 +0000168 "1.79769313486231580793728971405301e+308L",
169 "1.18973149535723176508575932662800702e+4932L");
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Eli Friedman2665a752009-05-23 03:50:01 +0000171 char MacroBuf[100];
Chris Lattnere116ccf2009-04-21 05:40:52 +0000172 sprintf(MacroBuf, "__%s_DENORM_MIN__=%s", Prefix, DenormMin);
173 DefineBuiltinMacro(Buf, MacroBuf);
174 sprintf(MacroBuf, "__%s_DIG__=%d", Prefix, Digits);
175 DefineBuiltinMacro(Buf, MacroBuf);
176 sprintf(MacroBuf, "__%s_EPSILON__=%s", Prefix, Epsilon);
177 DefineBuiltinMacro(Buf, MacroBuf);
178 sprintf(MacroBuf, "__%s_HAS_INFINITY__=%d", Prefix, HasInifinity);
179 DefineBuiltinMacro(Buf, MacroBuf);
180 sprintf(MacroBuf, "__%s_HAS_QUIET_NAN__=%d", Prefix, HasQuietNaN);
181 DefineBuiltinMacro(Buf, MacroBuf);
182 sprintf(MacroBuf, "__%s_MANT_DIG__=%d", Prefix, MantissaDigits);
183 DefineBuiltinMacro(Buf, MacroBuf);
184 sprintf(MacroBuf, "__%s_MAX_10_EXP__=%d", Prefix, Max10Exp);
185 DefineBuiltinMacro(Buf, MacroBuf);
186 sprintf(MacroBuf, "__%s_MAX_EXP__=%d", Prefix, MaxExp);
187 DefineBuiltinMacro(Buf, MacroBuf);
188 sprintf(MacroBuf, "__%s_MAX__=%s", Prefix, Max);
189 DefineBuiltinMacro(Buf, MacroBuf);
190 sprintf(MacroBuf, "__%s_MIN_10_EXP__=(%d)", Prefix, Min10Exp);
191 DefineBuiltinMacro(Buf, MacroBuf);
192 sprintf(MacroBuf, "__%s_MIN_EXP__=(%d)", Prefix, MinExp);
193 DefineBuiltinMacro(Buf, MacroBuf);
194 sprintf(MacroBuf, "__%s_MIN__=%s", Prefix, Min);
195 DefineBuiltinMacro(Buf, MacroBuf);
196 sprintf(MacroBuf, "__%s_HAS_DENORM__=1", Prefix);
197 DefineBuiltinMacro(Buf, MacroBuf);
198}
199
200
201/// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
202/// named MacroName with the max value for a type with width 'TypeWidth' a
203/// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
204static void DefineTypeSize(const char *MacroName, unsigned TypeWidth,
205 const char *ValSuffix, bool isSigned,
206 std::vector<char> &Buf) {
207 char MacroBuf[60];
208 long long MaxVal;
209 if (isSigned)
210 MaxVal = (1LL << (TypeWidth - 1)) - 1;
211 else
212 MaxVal = ~0LL >> (64-TypeWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Daniel Dunbarb8b844b2009-09-03 19:23:49 +0000214 // FIXME: Switch to using raw_ostream and avoid utostr().
215 sprintf(MacroBuf, "%s=%s%s", MacroName, llvm::utostr(MaxVal).c_str(),
216 ValSuffix);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000217 DefineBuiltinMacro(Buf, MacroBuf);
218}
219
Chris Lattner9099e7b2009-11-05 21:21:32 +0000220/// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
221/// the width, suffix, and signedness of the given type
222static void DefineTypeSize(const char *MacroName, TargetInfo::IntType Ty,
223 const TargetInfo &TI, std::vector<char> &Buf) {
224 DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
225 TI.isTypeSigned(Ty), Buf);
226}
227
Chris Lattnere116ccf2009-04-21 05:40:52 +0000228static void DefineType(const char *MacroName, TargetInfo::IntType Ty,
229 std::vector<char> &Buf) {
230 char MacroBuf[60];
231 sprintf(MacroBuf, "%s=%s", MacroName, TargetInfo::getTypeName(Ty));
232 DefineBuiltinMacro(Buf, MacroBuf);
233}
234
235
236static void InitializePredefinedMacros(const TargetInfo &TI,
237 const LangOptions &LangOpts,
238 std::vector<char> &Buf) {
239 char MacroBuf[60];
240 // Compiler version introspection macros.
241 DefineBuiltinMacro(Buf, "__llvm__=1"); // LLVM Backend
242 DefineBuiltinMacro(Buf, "__clang__=1"); // Clang Frontend
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Chris Lattnere116ccf2009-04-21 05:40:52 +0000244 // Currently claim to be compatible with GCC 4.2.1-5621.
Chris Lattnere116ccf2009-04-21 05:40:52 +0000245 DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2");
246 DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
247 DefineBuiltinMacro(Buf, "__GNUC__=4");
248 DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
249 DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 Compatible Clang Compiler\"");
Mike Stump1eb44332009-09-09 15:08:12 +0000250
251
Chris Lattnere116ccf2009-04-21 05:40:52 +0000252 // Initialize language-specific preprocessor defines.
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Chris Lattnere116ccf2009-04-21 05:40:52 +0000254 // These should all be defined in the preprocessor according to the
255 // current language configuration.
256 if (!LangOpts.Microsoft)
257 DefineBuiltinMacro(Buf, "__STDC__=1");
258 if (LangOpts.AsmPreprocessor)
259 DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
Ryan Flynn07ef8042009-07-21 00:07:02 +0000260
261 if (!LangOpts.CPlusPlus) {
262 if (LangOpts.C99)
263 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
264 else if (!LangOpts.GNUMode && LangOpts.Digraphs)
265 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
266 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000267
268 // Standard conforming mode?
269 if (!LangOpts.GNUMode)
270 DefineBuiltinMacro(Buf, "__STRICT_ANSI__=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Chris Lattnere116ccf2009-04-21 05:40:52 +0000272 if (LangOpts.CPlusPlus0x)
273 DefineBuiltinMacro(Buf, "__GXX_EXPERIMENTAL_CXX0X__");
274
275 if (LangOpts.Freestanding)
276 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=0");
277 else
278 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Chris Lattnere116ccf2009-04-21 05:40:52 +0000280 if (LangOpts.ObjC1) {
281 DefineBuiltinMacro(Buf, "__OBJC__=1");
282 if (LangOpts.ObjCNonFragileABI) {
283 DefineBuiltinMacro(Buf, "__OBJC2__=1");
284 DefineBuiltinMacro(Buf, "OBJC_ZEROCOST_EXCEPTIONS=1");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000285 }
286
287 if (LangOpts.getGCMode() != LangOptions::NonGC)
288 DefineBuiltinMacro(Buf, "__OBJC_GC__=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Chris Lattnere116ccf2009-04-21 05:40:52 +0000290 if (LangOpts.NeXTRuntime)
291 DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1");
292 }
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Chris Lattnere116ccf2009-04-21 05:40:52 +0000294 // darwin_constant_cfstrings controls this. This is also dependent
295 // on other things like the runtime I believe. This is set even for C code.
296 DefineBuiltinMacro(Buf, "__CONSTANT_CFSTRINGS__=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Chris Lattnere116ccf2009-04-21 05:40:52 +0000298 if (LangOpts.ObjC2)
299 DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES");
300
301 if (LangOpts.PascalStrings)
302 DefineBuiltinMacro(Buf, "__PASCAL_STRINGS__");
303
304 if (LangOpts.Blocks) {
305 DefineBuiltinMacro(Buf, "__block=__attribute__((__blocks__(byref)))");
306 DefineBuiltinMacro(Buf, "__BLOCKS__=1");
307 }
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Rafael Espindolaf759df02009-10-01 13:33:33 +0000309 if (LangOpts.Exceptions)
310 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
311
Chris Lattnere116ccf2009-04-21 05:40:52 +0000312 if (LangOpts.CPlusPlus) {
313 DefineBuiltinMacro(Buf, "__DEPRECATED=1");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000314 DefineBuiltinMacro(Buf, "__GNUG__=4");
315 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
Douglas Gregor36dd1312009-08-06 04:09:28 +0000316 if (LangOpts.GNUMode)
317 DefineBuiltinMacro(Buf, "__cplusplus=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000318 else
Douglas Gregor36dd1312009-08-06 04:09:28 +0000319 // C++ [cpp.predefined]p1:
Mike Stump1eb44332009-09-09 15:08:12 +0000320 // The name_ _cplusplusis defined to the value199711Lwhen compiling a
Douglas Gregor36dd1312009-08-06 04:09:28 +0000321 // C++ translation unit.
322 DefineBuiltinMacro(Buf, "__cplusplus=199711L");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000323 DefineBuiltinMacro(Buf, "__private_extern__=extern");
Eli Friedman666479b2009-08-27 22:01:41 +0000324 // Ugly hack to work with GNU libstdc++.
325 DefineBuiltinMacro(Buf, "_GNU_SOURCE=1");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000326 }
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Chris Lattnere116ccf2009-04-21 05:40:52 +0000328 if (LangOpts.Microsoft) {
John Thompson24ee8042009-10-16 01:12:00 +0000329 // Filter out some microsoft extensions when trying to parse in ms-compat
330 // mode.
Chris Lattnere116ccf2009-04-21 05:40:52 +0000331 DefineBuiltinMacro(Buf, "__int8=__INT8_TYPE__");
332 DefineBuiltinMacro(Buf, "__int16=__INT16_TYPE__");
333 DefineBuiltinMacro(Buf, "__int32=__INT32_TYPE__");
334 DefineBuiltinMacro(Buf, "__int64=__INT64_TYPE__");
John Thompson24ee8042009-10-16 01:12:00 +0000335 // Work around some issues with Visual C++ headerws.
336 if (LangOpts.CPlusPlus) {
337 // Since we define wchar_t in C++ mode.
338 DefineBuiltinMacro(Buf, "_WCHAR_T_DEFINED=1");
339 DefineBuiltinMacro(Buf, "_NATIVE_WCHAR_T_DEFINED=1");
340 // FIXME: This should be temporary until we have a __pragma
341 // solution, to avoid some errors flagged in VC++ headers.
342 DefineBuiltinMacro(Buf, "_CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES=0");
343 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattnere116ccf2009-04-21 05:40:52 +0000346 if (LangOpts.Optimize)
347 DefineBuiltinMacro(Buf, "__OPTIMIZE__=1");
348 if (LangOpts.OptimizeSize)
349 DefineBuiltinMacro(Buf, "__OPTIMIZE_SIZE__=1");
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Chris Lattnere116ccf2009-04-21 05:40:52 +0000351 // Initialize target-specific preprocessor defines.
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Chris Lattnere116ccf2009-04-21 05:40:52 +0000353 // Define type sizing macros based on the target properties.
354 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
355 DefineBuiltinMacro(Buf, "__CHAR_BIT__=8");
356
Chris Lattnere116ccf2009-04-21 05:40:52 +0000357 DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Buf);
Chris Lattner9099e7b2009-11-05 21:21:32 +0000358 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Buf);
359 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Buf);
360 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Buf);
361 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Buf);
362 // FIXME: TI.getWCharWidth() and TI.getTypeWidth(TI.getWCharType()) return
363 // different values on PIC16 and MSP430. TargetInfo needs to be corrected
364 // and the following line substituted for the one below it.
365 // DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Buf);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000366 DefineTypeSize("__WCHAR_MAX__", TI.getWCharWidth(), "", true, Buf);
Chris Lattner9099e7b2009-11-05 21:21:32 +0000367 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Buf);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000368
369 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Buf);
370 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Buf);
371 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Buf);
372 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Buf);
373 DefineType("__SIZE_TYPE__", TI.getSizeType(), Buf);
374 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Buf);
Chris Lattnere64ef802009-10-21 04:59:34 +0000375 DefineType("__WINT_TYPE__", TI.getWIntType(), Buf);
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Chris Lattnere116ccf2009-04-21 05:40:52 +0000377 DefineFloatMacros(Buf, "FLT", &TI.getFloatFormat());
378 DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat());
379 DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat());
380
381 // Define a __POINTER_WIDTH__ macro for stdint.h.
382 sprintf(MacroBuf, "__POINTER_WIDTH__=%d", (int)TI.getPointerWidth(0));
383 DefineBuiltinMacro(Buf, MacroBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Eli Friedman15b91762009-06-05 07:05:05 +0000385 if (!LangOpts.CharIsSigned)
Mike Stump1eb44332009-09-09 15:08:12 +0000386 DefineBuiltinMacro(Buf, "__CHAR_UNSIGNED__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000387
388 // Define fixed-sized integer types for stdint.h
389 assert(TI.getCharWidth() == 8 && "unsupported target types");
390 assert(TI.getShortWidth() == 16 && "unsupported target types");
391 DefineBuiltinMacro(Buf, "__INT8_TYPE__=char");
Chris Lattner9099e7b2009-11-05 21:21:32 +0000392 DefineType("__INT16_TYPE__", TargetInfo::SignedShort, Buf);
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Chris Lattner9099e7b2009-11-05 21:21:32 +0000394 TargetInfo::IntType Int32Type;
Chris Lattnere116ccf2009-04-21 05:40:52 +0000395 if (TI.getIntWidth() == 32)
Chris Lattner9099e7b2009-11-05 21:21:32 +0000396 Int32Type = TargetInfo::SignedInt;
Chris Lattnere116ccf2009-04-21 05:40:52 +0000397 else {
398 assert(TI.getLongLongWidth() == 32 && "unsupported target types");
Chris Lattner9099e7b2009-11-05 21:21:32 +0000399 Int32Type = TargetInfo::SignedLongLong;
Chris Lattnere116ccf2009-04-21 05:40:52 +0000400 }
Chris Lattner9099e7b2009-11-05 21:21:32 +0000401 DefineType("__INT32_TYPE__", Int32Type, Buf);
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Chris Lattnere116ccf2009-04-21 05:40:52 +0000403 // 16-bit targets doesn't necessarily have a 64-bit type.
404 if (TI.getLongLongWidth() == 64)
Eli Friedman3c7b6e42009-07-01 03:36:11 +0000405 DefineType("__INT64_TYPE__", TI.getInt64Type(), Buf);
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Chris Lattnere116ccf2009-04-21 05:40:52 +0000407 // Add __builtin_va_list typedef.
408 {
409 const char *VAList = TI.getVAListDeclaration();
410 Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
411 Buf.push_back('\n');
412 }
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Chris Lattnere116ccf2009-04-21 05:40:52 +0000414 if (const char *Prefix = TI.getUserLabelPrefix()) {
415 sprintf(MacroBuf, "__USER_LABEL_PREFIX__=%s", Prefix);
416 DefineBuiltinMacro(Buf, MacroBuf);
417 }
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Chris Lattnere116ccf2009-04-21 05:40:52 +0000419 // Build configuration options. FIXME: these should be controlled by
420 // command line options or something.
421 DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
422
Chris Lattnere116ccf2009-04-21 05:40:52 +0000423 if (LangOpts.GNUInline)
424 DefineBuiltinMacro(Buf, "__GNUC_GNU_INLINE__=1");
425 else
426 DefineBuiltinMacro(Buf, "__GNUC_STDC_INLINE__=1");
427
428 if (LangOpts.NoInline)
429 DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
430
431 if (unsigned PICLevel = LangOpts.PICLevel) {
432 sprintf(MacroBuf, "__PIC__=%d", PICLevel);
433 DefineBuiltinMacro(Buf, MacroBuf);
434
435 sprintf(MacroBuf, "__pic__=%d", PICLevel);
436 DefineBuiltinMacro(Buf, MacroBuf);
437 }
438
439 // Macros to control C99 numerics and <float.h>
440 DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0");
441 DefineBuiltinMacro(Buf, "__FLT_RADIX__=2");
442 sprintf(MacroBuf, "__DECIMAL_DIG__=%d",
Eli Friedman2665a752009-05-23 03:50:01 +0000443 PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36));
Chris Lattnere116ccf2009-04-21 05:40:52 +0000444 DefineBuiltinMacro(Buf, MacroBuf);
Bill Wendling45483f72009-06-28 07:36:13 +0000445
Bill Wendling4ebe3e42009-06-28 23:01:01 +0000446 if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn)
Bill Wendling45483f72009-06-28 07:36:13 +0000447 DefineBuiltinMacro(Buf, "__SSP__=1");
Bill Wendling4ebe3e42009-06-28 23:01:01 +0000448 else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq)
Bill Wendling45483f72009-06-28 07:36:13 +0000449 DefineBuiltinMacro(Buf, "__SSP_ALL__=2");
450
Chris Lattnere116ccf2009-04-21 05:40:52 +0000451 // Get other target #defines.
452 TI.getTargetDefines(LangOpts, Buf);
453}
454
455/// InitializePreprocessor - Initialize the preprocessor getting it and the
456/// environment ready to process a single file. This returns true on error.
457///
Daniel Dunbar938963f2009-11-04 21:13:15 +0000458void clang::InitializePreprocessor(Preprocessor &PP,
Daniel Dunbar961c76e2009-11-11 21:44:42 +0000459 const PreprocessorOptions &InitOpts,
460 const HeaderSearchOptions &HSOpts) {
Chris Lattnere116ccf2009-04-21 05:40:52 +0000461 std::vector<char> PredefineBuffer;
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Chris Lattner39360242009-04-22 03:42:19 +0000463 const char *LineDirective = "# 1 \"<built-in>\" 3\n";
464 PredefineBuffer.insert(PredefineBuffer.end(),
465 LineDirective, LineDirective+strlen(LineDirective));
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Chris Lattnere116ccf2009-04-21 05:40:52 +0000467 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
Daniel Dunbar468fe242009-11-04 21:13:02 +0000468 if (InitOpts.getUsePredefines())
Chris Lattnere6113de2009-11-03 19:50:27 +0000469 InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(),
470 PredefineBuffer);
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Chris Lattnere116ccf2009-04-21 05:40:52 +0000472 // Add on the predefines from the driver. Wrap in a #line directive to report
473 // that they come from the command line.
Chris Lattner39360242009-04-22 03:42:19 +0000474 LineDirective = "# 1 \"<command line>\" 1\n";
Chris Lattnere116ccf2009-04-21 05:40:52 +0000475 PredefineBuffer.insert(PredefineBuffer.end(),
476 LineDirective, LineDirective+strlen(LineDirective));
477
478 // Process #define's and #undef's in the order they are given.
Daniel Dunbar8863b982009-11-07 04:20:15 +0000479 for (PreprocessorOptions::macro_iterator I = InitOpts.macro_begin(),
Chris Lattnere116ccf2009-04-21 05:40:52 +0000480 E = InitOpts.macro_end(); I != E; ++I) {
Chris Lattner62f86c42009-04-21 06:00:24 +0000481 if (I->second) // isUndef
Chris Lattnerdcdecf42009-05-15 16:08:43 +0000482 UndefineBuiltinMacro(PredefineBuffer, I->first.c_str());
Chris Lattner62f86c42009-04-21 06:00:24 +0000483 else
484 DefineBuiltinMacro(PredefineBuffer, I->first.c_str());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000485 }
486
487 // If -imacros are specified, include them now. These are processed before
488 // any -include directives.
Daniel Dunbar8863b982009-11-07 04:20:15 +0000489 for (PreprocessorOptions::imacro_iterator I = InitOpts.imacro_begin(),
Chris Lattnere116ccf2009-04-21 05:40:52 +0000490 E = InitOpts.imacro_end(); I != E; ++I)
491 AddImplicitIncludeMacros(PredefineBuffer, *I);
492
493 // Process -include directives.
Daniel Dunbar8863b982009-11-07 04:20:15 +0000494 for (PreprocessorOptions::include_iterator I = InitOpts.include_begin(),
Chris Lattnere116ccf2009-04-21 05:40:52 +0000495 E = InitOpts.include_end(); I != E; ++I) {
Daniel Dunbarb6d1cc82009-11-10 23:53:43 +0000496 if (*I == InitOpts.getImplicitPTHInclude())
497 AddImplicitIncludePTH(PredefineBuffer, PP, *I);
Chris Lattner62f86c42009-04-21 06:00:24 +0000498 else
Daniel Dunbarb6d1cc82009-11-10 23:53:43 +0000499 AddImplicitInclude(PredefineBuffer, *I);
Eli Friedmanae96a962009-06-15 09:57:52 +0000500 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000501
502 // Null terminate PredefinedBuffer and add it.
503 PredefineBuffer.push_back(0);
504 PP.setPredefines(&PredefineBuffer[0]);
Daniel Dunbar961c76e2009-11-11 21:44:42 +0000505
506 // Initialize the header search object.
507 ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts,
508 PP.getLangOptions(),
509 PP.getTargetInfo().getTriple());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000510}