Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 1 | //===--- 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 | |
| 14 | #include "clang/Frontend/InitPreprocessor.h" |
| 15 | #include "clang/Basic/TargetInfo.h" |
| 16 | #include "clang/Lex/Preprocessor.h" |
| 17 | #include "llvm/ADT/StringExtras.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | #include "llvm/System/Path.h" |
| 20 | |
| 21 | namespace clang { |
| 22 | |
| 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=". |
| 26 | static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro, |
| 27 | const char *Command = "#define ") { |
| 28 | 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(' '); |
| 33 | |
| 34 | // 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 | } |
| 43 | |
| 44 | 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 | |
| 54 | /// Add the quoted name of an implicit include file. |
| 55 | static void AddQuotedIncludePath(std::vector<char> &Buf, |
| 56 | const std::string &File) { |
| 57 | // Implicit include paths are relative to the current working |
| 58 | // directory; resolve them now instead of using the normal machinery |
| 59 | // (which would look relative to the input file). |
| 60 | llvm::sys::Path Path(File); |
| 61 | Path.makeAbsolute(); |
| 62 | |
| 63 | // Escape double quotes etc. |
| 64 | Buf.push_back('"'); |
| 65 | std::string EscapedFile = Lexer::Stringify(Path.toString()); |
| 66 | Buf.insert(Buf.end(), EscapedFile.begin(), EscapedFile.end()); |
| 67 | Buf.push_back('"'); |
| 68 | } |
| 69 | |
| 70 | /// AddImplicitInclude - Add an implicit #include of the specified file to the |
| 71 | /// predefines buffer. |
| 72 | static void AddImplicitInclude(std::vector<char> &Buf, |
| 73 | const std::string &File) { |
| 74 | const char *Inc = "#include "; |
| 75 | Buf.insert(Buf.end(), Inc, Inc+strlen(Inc)); |
| 76 | AddQuotedIncludePath(Buf, File); |
| 77 | Buf.push_back('\n'); |
| 78 | } |
| 79 | |
| 80 | static void AddImplicitIncludeMacros(std::vector<char> &Buf, |
| 81 | const std::string &File) { |
| 82 | const char *Inc = "#__include_macros "; |
| 83 | Buf.insert(Buf.end(), Inc, Inc+strlen(Inc)); |
| 84 | AddQuotedIncludePath(Buf, File); |
| 85 | Buf.push_back('\n'); |
| 86 | // Marker token to stop the __include_macros fetch loop. |
| 87 | const char *Marker = "##\n"; // ##? |
| 88 | Buf.insert(Buf.end(), Marker, Marker+strlen(Marker)); |
| 89 | } |
| 90 | |
| 91 | /// AddImplicitIncludePTH - Add an implicit #include using the original file |
| 92 | /// used to generate a PTH cache. |
| 93 | static void AddImplicitIncludePTH(std::vector<char> &Buf, Preprocessor &PP, |
| 94 | const std::string& ImplicitIncludePTH) { |
| 95 | PTHManager *P = PP.getPTHManager(); |
| 96 | assert(P && "No PTHManager."); |
| 97 | const char *OriginalFile = P->getOriginalSourceFile(); |
| 98 | |
| 99 | if (!OriginalFile) { |
| 100 | assert(!ImplicitIncludePTH.empty()); |
| 101 | fprintf(stderr, "error: PTH file '%s' does not designate an original " |
| 102 | "source header file for -include-pth\n", |
| 103 | ImplicitIncludePTH.c_str()); |
| 104 | exit (1); |
| 105 | } |
| 106 | |
| 107 | AddImplicitInclude(Buf, OriginalFile); |
| 108 | } |
| 109 | |
| 110 | /// PickFP - This is used to pick a value based on the FP semantics of the |
| 111 | /// specified FP model. |
| 112 | template <typename T> |
| 113 | static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal, |
| 114 | T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal) { |
| 115 | if (Sem == &llvm::APFloat::IEEEsingle) |
| 116 | return IEEESingleVal; |
| 117 | if (Sem == &llvm::APFloat::IEEEdouble) |
| 118 | return IEEEDoubleVal; |
| 119 | if (Sem == &llvm::APFloat::x87DoubleExtended) |
| 120 | return X87DoubleExtendedVal; |
| 121 | assert(Sem == &llvm::APFloat::PPCDoubleDouble); |
| 122 | return PPCDoubleDoubleVal; |
| 123 | } |
| 124 | |
| 125 | static void DefineFloatMacros(std::vector<char> &Buf, const char *Prefix, |
| 126 | const llvm::fltSemantics *Sem) { |
| 127 | const char *DenormMin, *Epsilon, *Max, *Min; |
| 128 | DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324", |
| 129 | "3.64519953188247460253e-4951L", |
| 130 | "4.94065645841246544176568792868221e-324L"); |
| 131 | int Digits = PickFP(Sem, 6, 15, 18, 31); |
| 132 | Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16", |
| 133 | "1.08420217248550443401e-19L", |
| 134 | "4.94065645841246544176568792868221e-324L"); |
| 135 | int HasInifinity = 1, HasQuietNaN = 1; |
| 136 | int MantissaDigits = PickFP(Sem, 24, 53, 64, 106); |
| 137 | int Min10Exp = PickFP(Sem, -37, -307, -4931, -291); |
| 138 | int Max10Exp = PickFP(Sem, 38, 308, 4932, 308); |
| 139 | int MinExp = PickFP(Sem, -125, -1021, -16381, -968); |
| 140 | int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024); |
| 141 | Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308", |
| 142 | "3.36210314311209350626e-4932L", |
| 143 | "2.00416836000897277799610805135016e-292L"); |
| 144 | Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308", |
| 145 | "1.18973149535723176502e+4932L", |
| 146 | "1.79769313486231580793728971405301e+308L"); |
| 147 | |
| 148 | char MacroBuf[60]; |
| 149 | sprintf(MacroBuf, "__%s_DENORM_MIN__=%s", Prefix, DenormMin); |
| 150 | DefineBuiltinMacro(Buf, MacroBuf); |
| 151 | sprintf(MacroBuf, "__%s_DIG__=%d", Prefix, Digits); |
| 152 | DefineBuiltinMacro(Buf, MacroBuf); |
| 153 | sprintf(MacroBuf, "__%s_EPSILON__=%s", Prefix, Epsilon); |
| 154 | DefineBuiltinMacro(Buf, MacroBuf); |
| 155 | sprintf(MacroBuf, "__%s_HAS_INFINITY__=%d", Prefix, HasInifinity); |
| 156 | DefineBuiltinMacro(Buf, MacroBuf); |
| 157 | sprintf(MacroBuf, "__%s_HAS_QUIET_NAN__=%d", Prefix, HasQuietNaN); |
| 158 | DefineBuiltinMacro(Buf, MacroBuf); |
| 159 | sprintf(MacroBuf, "__%s_MANT_DIG__=%d", Prefix, MantissaDigits); |
| 160 | DefineBuiltinMacro(Buf, MacroBuf); |
| 161 | sprintf(MacroBuf, "__%s_MAX_10_EXP__=%d", Prefix, Max10Exp); |
| 162 | DefineBuiltinMacro(Buf, MacroBuf); |
| 163 | sprintf(MacroBuf, "__%s_MAX_EXP__=%d", Prefix, MaxExp); |
| 164 | DefineBuiltinMacro(Buf, MacroBuf); |
| 165 | sprintf(MacroBuf, "__%s_MAX__=%s", Prefix, Max); |
| 166 | DefineBuiltinMacro(Buf, MacroBuf); |
| 167 | sprintf(MacroBuf, "__%s_MIN_10_EXP__=(%d)", Prefix, Min10Exp); |
| 168 | DefineBuiltinMacro(Buf, MacroBuf); |
| 169 | sprintf(MacroBuf, "__%s_MIN_EXP__=(%d)", Prefix, MinExp); |
| 170 | DefineBuiltinMacro(Buf, MacroBuf); |
| 171 | sprintf(MacroBuf, "__%s_MIN__=%s", Prefix, Min); |
| 172 | DefineBuiltinMacro(Buf, MacroBuf); |
| 173 | sprintf(MacroBuf, "__%s_HAS_DENORM__=1", Prefix); |
| 174 | DefineBuiltinMacro(Buf, MacroBuf); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro |
| 179 | /// named MacroName with the max value for a type with width 'TypeWidth' a |
| 180 | /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL). |
| 181 | static void DefineTypeSize(const char *MacroName, unsigned TypeWidth, |
| 182 | const char *ValSuffix, bool isSigned, |
| 183 | std::vector<char> &Buf) { |
| 184 | char MacroBuf[60]; |
| 185 | long long MaxVal; |
| 186 | if (isSigned) |
| 187 | MaxVal = (1LL << (TypeWidth - 1)) - 1; |
| 188 | else |
| 189 | MaxVal = ~0LL >> (64-TypeWidth); |
| 190 | |
| 191 | sprintf(MacroBuf, "%s=%llu%s", MacroName, MaxVal, ValSuffix); |
| 192 | DefineBuiltinMacro(Buf, MacroBuf); |
| 193 | } |
| 194 | |
| 195 | static void DefineType(const char *MacroName, TargetInfo::IntType Ty, |
| 196 | std::vector<char> &Buf) { |
| 197 | char MacroBuf[60]; |
| 198 | sprintf(MacroBuf, "%s=%s", MacroName, TargetInfo::getTypeName(Ty)); |
| 199 | DefineBuiltinMacro(Buf, MacroBuf); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | static void InitializePredefinedMacros(const TargetInfo &TI, |
| 204 | const LangOptions &LangOpts, |
| 205 | std::vector<char> &Buf) { |
| 206 | char MacroBuf[60]; |
| 207 | // Compiler version introspection macros. |
| 208 | DefineBuiltinMacro(Buf, "__llvm__=1"); // LLVM Backend |
| 209 | DefineBuiltinMacro(Buf, "__clang__=1"); // Clang Frontend |
| 210 | |
| 211 | // Currently claim to be compatible with GCC 4.2.1-5621. |
| 212 | DefineBuiltinMacro(Buf, "__APPLE_CC__=5621"); |
| 213 | DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2"); |
| 214 | DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1"); |
| 215 | DefineBuiltinMacro(Buf, "__GNUC__=4"); |
| 216 | DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002"); |
| 217 | DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 Compatible Clang Compiler\""); |
| 218 | |
| 219 | |
| 220 | // Initialize language-specific preprocessor defines. |
| 221 | |
| 222 | // These should all be defined in the preprocessor according to the |
| 223 | // current language configuration. |
| 224 | if (!LangOpts.Microsoft) |
| 225 | DefineBuiltinMacro(Buf, "__STDC__=1"); |
| 226 | if (LangOpts.AsmPreprocessor) |
| 227 | DefineBuiltinMacro(Buf, "__ASSEMBLER__=1"); |
| 228 | if (LangOpts.C99 && !LangOpts.CPlusPlus) |
| 229 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L"); |
| 230 | else if (0) // STDC94 ? |
| 231 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L"); |
| 232 | |
| 233 | // Standard conforming mode? |
| 234 | if (!LangOpts.GNUMode) |
| 235 | DefineBuiltinMacro(Buf, "__STRICT_ANSI__=1"); |
| 236 | |
| 237 | if (LangOpts.CPlusPlus0x) |
| 238 | DefineBuiltinMacro(Buf, "__GXX_EXPERIMENTAL_CXX0X__"); |
| 239 | |
| 240 | if (LangOpts.Freestanding) |
| 241 | DefineBuiltinMacro(Buf, "__STDC_HOSTED__=0"); |
| 242 | else |
| 243 | DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1"); |
| 244 | |
| 245 | if (LangOpts.ObjC1) { |
| 246 | DefineBuiltinMacro(Buf, "__OBJC__=1"); |
| 247 | if (LangOpts.ObjCNonFragileABI) { |
| 248 | DefineBuiltinMacro(Buf, "__OBJC2__=1"); |
| 249 | DefineBuiltinMacro(Buf, "OBJC_ZEROCOST_EXCEPTIONS=1"); |
| 250 | DefineBuiltinMacro(Buf, "__EXCEPTIONS=1"); |
| 251 | } |
| 252 | |
| 253 | if (LangOpts.getGCMode() != LangOptions::NonGC) |
| 254 | DefineBuiltinMacro(Buf, "__OBJC_GC__=1"); |
| 255 | |
| 256 | if (LangOpts.NeXTRuntime) |
| 257 | DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1"); |
| 258 | } |
| 259 | |
| 260 | // darwin_constant_cfstrings controls this. This is also dependent |
| 261 | // on other things like the runtime I believe. This is set even for C code. |
| 262 | DefineBuiltinMacro(Buf, "__CONSTANT_CFSTRINGS__=1"); |
| 263 | |
| 264 | if (LangOpts.ObjC2) |
| 265 | DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES"); |
| 266 | |
| 267 | if (LangOpts.PascalStrings) |
| 268 | DefineBuiltinMacro(Buf, "__PASCAL_STRINGS__"); |
| 269 | |
| 270 | if (LangOpts.Blocks) { |
| 271 | DefineBuiltinMacro(Buf, "__block=__attribute__((__blocks__(byref)))"); |
| 272 | DefineBuiltinMacro(Buf, "__BLOCKS__=1"); |
| 273 | } |
| 274 | |
| 275 | if (LangOpts.CPlusPlus) { |
| 276 | DefineBuiltinMacro(Buf, "__DEPRECATED=1"); |
| 277 | DefineBuiltinMacro(Buf, "__EXCEPTIONS=1"); |
| 278 | DefineBuiltinMacro(Buf, "__GNUG__=4"); |
| 279 | DefineBuiltinMacro(Buf, "__GXX_WEAK__=1"); |
| 280 | DefineBuiltinMacro(Buf, "__cplusplus=1"); |
| 281 | DefineBuiltinMacro(Buf, "__private_extern__=extern"); |
| 282 | } |
| 283 | |
| 284 | // Filter out some microsoft extensions when trying to parse in ms-compat |
| 285 | // mode. |
| 286 | if (LangOpts.Microsoft) { |
| 287 | DefineBuiltinMacro(Buf, "_cdecl=__cdecl"); |
| 288 | DefineBuiltinMacro(Buf, "__int8=__INT8_TYPE__"); |
| 289 | DefineBuiltinMacro(Buf, "__int16=__INT16_TYPE__"); |
| 290 | DefineBuiltinMacro(Buf, "__int32=__INT32_TYPE__"); |
| 291 | DefineBuiltinMacro(Buf, "__int64=__INT64_TYPE__"); |
| 292 | } |
| 293 | |
| 294 | if (LangOpts.Optimize) |
| 295 | DefineBuiltinMacro(Buf, "__OPTIMIZE__=1"); |
| 296 | if (LangOpts.OptimizeSize) |
| 297 | DefineBuiltinMacro(Buf, "__OPTIMIZE_SIZE__=1"); |
| 298 | |
| 299 | // Initialize target-specific preprocessor defines. |
| 300 | |
| 301 | // Define type sizing macros based on the target properties. |
| 302 | assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far"); |
| 303 | DefineBuiltinMacro(Buf, "__CHAR_BIT__=8"); |
| 304 | |
| 305 | unsigned IntMaxWidth; |
| 306 | const char *IntMaxSuffix; |
| 307 | if (TI.getIntMaxType() == TargetInfo::SignedLongLong) { |
| 308 | IntMaxWidth = TI.getLongLongWidth(); |
| 309 | IntMaxSuffix = "LL"; |
| 310 | } else if (TI.getIntMaxType() == TargetInfo::SignedLong) { |
| 311 | IntMaxWidth = TI.getLongWidth(); |
| 312 | IntMaxSuffix = "L"; |
| 313 | } else { |
| 314 | assert(TI.getIntMaxType() == TargetInfo::SignedInt); |
| 315 | IntMaxWidth = TI.getIntWidth(); |
| 316 | IntMaxSuffix = ""; |
| 317 | } |
| 318 | |
| 319 | DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Buf); |
| 320 | DefineTypeSize("__SHRT_MAX__", TI.getShortWidth(), "", true, Buf); |
| 321 | DefineTypeSize("__INT_MAX__", TI.getIntWidth(), "", true, Buf); |
| 322 | DefineTypeSize("__LONG_MAX__", TI.getLongWidth(), "L", true, Buf); |
| 323 | DefineTypeSize("__LONG_LONG_MAX__", TI.getLongLongWidth(), "LL", true, Buf); |
| 324 | DefineTypeSize("__WCHAR_MAX__", TI.getWCharWidth(), "", true, Buf); |
| 325 | DefineTypeSize("__INTMAX_MAX__", IntMaxWidth, IntMaxSuffix, true, Buf); |
| 326 | |
| 327 | DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Buf); |
| 328 | DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Buf); |
| 329 | DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Buf); |
| 330 | DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Buf); |
| 331 | DefineType("__SIZE_TYPE__", TI.getSizeType(), Buf); |
| 332 | DefineType("__WCHAR_TYPE__", TI.getWCharType(), Buf); |
| 333 | // FIXME: TargetInfo hookize __WINT_TYPE__. |
| 334 | DefineBuiltinMacro(Buf, "__WINT_TYPE__=int"); |
| 335 | |
| 336 | DefineFloatMacros(Buf, "FLT", &TI.getFloatFormat()); |
| 337 | DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat()); |
| 338 | DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat()); |
| 339 | |
| 340 | // Define a __POINTER_WIDTH__ macro for stdint.h. |
| 341 | sprintf(MacroBuf, "__POINTER_WIDTH__=%d", (int)TI.getPointerWidth(0)); |
| 342 | DefineBuiltinMacro(Buf, MacroBuf); |
| 343 | |
| 344 | if (!TI.isCharSigned()) |
| 345 | DefineBuiltinMacro(Buf, "__CHAR_UNSIGNED__"); |
| 346 | |
| 347 | // Define fixed-sized integer types for stdint.h |
| 348 | assert(TI.getCharWidth() == 8 && "unsupported target types"); |
| 349 | assert(TI.getShortWidth() == 16 && "unsupported target types"); |
| 350 | DefineBuiltinMacro(Buf, "__INT8_TYPE__=char"); |
| 351 | DefineBuiltinMacro(Buf, "__INT16_TYPE__=short"); |
| 352 | |
| 353 | if (TI.getIntWidth() == 32) |
| 354 | DefineBuiltinMacro(Buf, "__INT32_TYPE__=int"); |
| 355 | else { |
| 356 | assert(TI.getLongLongWidth() == 32 && "unsupported target types"); |
| 357 | DefineBuiltinMacro(Buf, "__INT32_TYPE__=long long"); |
| 358 | } |
| 359 | |
| 360 | // 16-bit targets doesn't necessarily have a 64-bit type. |
| 361 | if (TI.getLongLongWidth() == 64) |
| 362 | DefineBuiltinMacro(Buf, "__INT64_TYPE__=long long"); |
| 363 | |
| 364 | // Add __builtin_va_list typedef. |
| 365 | { |
| 366 | const char *VAList = TI.getVAListDeclaration(); |
| 367 | Buf.insert(Buf.end(), VAList, VAList+strlen(VAList)); |
| 368 | Buf.push_back('\n'); |
| 369 | } |
| 370 | |
| 371 | if (const char *Prefix = TI.getUserLabelPrefix()) { |
| 372 | sprintf(MacroBuf, "__USER_LABEL_PREFIX__=%s", Prefix); |
| 373 | DefineBuiltinMacro(Buf, MacroBuf); |
| 374 | } |
| 375 | |
| 376 | // Build configuration options. FIXME: these should be controlled by |
| 377 | // command line options or something. |
| 378 | DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0"); |
| 379 | |
| 380 | if (LangOpts.Static) |
| 381 | DefineBuiltinMacro(Buf, "__STATIC__=1"); |
| 382 | else |
| 383 | DefineBuiltinMacro(Buf, "__DYNAMIC__=1"); |
| 384 | |
| 385 | if (LangOpts.GNUInline) |
| 386 | DefineBuiltinMacro(Buf, "__GNUC_GNU_INLINE__=1"); |
| 387 | else |
| 388 | DefineBuiltinMacro(Buf, "__GNUC_STDC_INLINE__=1"); |
| 389 | |
| 390 | if (LangOpts.NoInline) |
| 391 | DefineBuiltinMacro(Buf, "__NO_INLINE__=1"); |
| 392 | |
| 393 | if (unsigned PICLevel = LangOpts.PICLevel) { |
| 394 | sprintf(MacroBuf, "__PIC__=%d", PICLevel); |
| 395 | DefineBuiltinMacro(Buf, MacroBuf); |
| 396 | |
| 397 | sprintf(MacroBuf, "__pic__=%d", PICLevel); |
| 398 | DefineBuiltinMacro(Buf, MacroBuf); |
| 399 | } |
| 400 | |
| 401 | // Macros to control C99 numerics and <float.h> |
| 402 | DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0"); |
| 403 | DefineBuiltinMacro(Buf, "__FLT_RADIX__=2"); |
| 404 | sprintf(MacroBuf, "__DECIMAL_DIG__=%d", |
| 405 | PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33)); |
| 406 | DefineBuiltinMacro(Buf, MacroBuf); |
| 407 | |
| 408 | // Get other target #defines. |
| 409 | TI.getTargetDefines(LangOpts, Buf); |
| 410 | } |
| 411 | |
| 412 | /// InitializePreprocessor - Initialize the preprocessor getting it and the |
| 413 | /// environment ready to process a single file. This returns true on error. |
| 414 | /// |
| 415 | bool InitializePreprocessor(Preprocessor &PP, |
| 416 | const std::string &InFile, |
| 417 | const PreprocessorInitOptions& InitOpts) { |
| 418 | std::vector<char> PredefineBuffer; |
| 419 | |
Chris Lattner | 3936024 | 2009-04-22 03:42:19 +0000 | [diff] [blame^] | 420 | const char *LineDirective = "# 1 \"<built-in>\" 3\n"; |
| 421 | PredefineBuffer.insert(PredefineBuffer.end(), |
| 422 | LineDirective, LineDirective+strlen(LineDirective)); |
| 423 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 424 | // Install things like __POWERPC__, __GNUC__, etc into the macro table. |
| 425 | InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(), |
| 426 | PredefineBuffer); |
| 427 | |
| 428 | // Add on the predefines from the driver. Wrap in a #line directive to report |
| 429 | // that they come from the command line. |
Chris Lattner | 3936024 | 2009-04-22 03:42:19 +0000 | [diff] [blame^] | 430 | LineDirective = "# 1 \"<command line>\" 1\n"; |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 431 | PredefineBuffer.insert(PredefineBuffer.end(), |
| 432 | LineDirective, LineDirective+strlen(LineDirective)); |
| 433 | |
| 434 | // Process #define's and #undef's in the order they are given. |
| 435 | for (PreprocessorInitOptions::macro_iterator I = InitOpts.macro_begin(), |
| 436 | E = InitOpts.macro_end(); I != E; ++I) { |
Chris Lattner | 62f86c4 | 2009-04-21 06:00:24 +0000 | [diff] [blame] | 437 | if (I->second) // isUndef |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 438 | DefineBuiltinMacro(PredefineBuffer, I->first.c_str(), "#undef "); |
Chris Lattner | 62f86c4 | 2009-04-21 06:00:24 +0000 | [diff] [blame] | 439 | else |
| 440 | DefineBuiltinMacro(PredefineBuffer, I->first.c_str()); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | // If -imacros are specified, include them now. These are processed before |
| 444 | // any -include directives. |
| 445 | for (PreprocessorInitOptions::imacro_iterator I = InitOpts.imacro_begin(), |
| 446 | E = InitOpts.imacro_end(); I != E; ++I) |
| 447 | AddImplicitIncludeMacros(PredefineBuffer, *I); |
| 448 | |
| 449 | // Process -include directives. |
| 450 | for (PreprocessorInitOptions::include_iterator I = InitOpts.include_begin(), |
| 451 | E = InitOpts.include_end(); I != E; ++I) { |
Chris Lattner | 62f86c4 | 2009-04-21 06:00:24 +0000 | [diff] [blame] | 452 | if (I->second) // isPTH |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 453 | AddImplicitIncludePTH(PredefineBuffer, PP, I->first); |
Chris Lattner | 62f86c4 | 2009-04-21 06:00:24 +0000 | [diff] [blame] | 454 | else |
| 455 | AddImplicitInclude(PredefineBuffer, I->first); |
Chris Lattner | 62f86c4 | 2009-04-21 06:00:24 +0000 | [diff] [blame] | 456 | } |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 457 | |
Chris Lattner | 3936024 | 2009-04-22 03:42:19 +0000 | [diff] [blame^] | 458 | LineDirective = "# 2 \"<built-in>\" 2 3\n"; |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 459 | PredefineBuffer.insert(PredefineBuffer.end(), |
| 460 | LineDirective, LineDirective+strlen(LineDirective)); |
| 461 | |
| 462 | // Null terminate PredefinedBuffer and add it. |
| 463 | PredefineBuffer.push_back(0); |
| 464 | PP.setPredefines(&PredefineBuffer[0]); |
| 465 | |
| 466 | // Once we've read this, we're done. |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | } // namespace clang |