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/Utils.h" 15 #include "clang/Basic/TargetInfo.h" 16 #include "clang/Frontend/FrontendDiagnostic.h" 17 #include "clang/Frontend/PreprocessorOptions.h" 18 #include "clang/Lex/Preprocessor.h" 19 #include "clang/Basic/FileManager.h" 20 #include "clang/Basic/SourceManager.h" 21 #include "llvm/ADT/StringExtras.h" 22 #include "llvm/ADT/StringMap.h" 23 #include "llvm/ADT/StringRef.h" 24 #include "llvm/ADT/STLExtras.h" 25 #include "llvm/Support/MemoryBuffer.h" 26 #include "llvm/System/Path.h" 27 using namespace clang; 28 29 // Append a #define line to Buf for Macro. Macro should be of the form XXX, 30 // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit 31 // "#define XXX Y z W". To get a #define with no value, use "XXX=". 32 static void DefineBuiltinMacro(std::vector<char> &Buf, llvm::StringRef Macro, 33 Diagnostic *Diags = 0) { 34 const char Command[] = "#define "; 35 Buf.insert(Buf.end(), Command, Command+strlen(Command)); 36 std::pair<llvm::StringRef, llvm::StringRef> MacroPair = Macro.split('='); 37 llvm::StringRef MacroName = MacroPair.first; 38 llvm::StringRef MacroBody = MacroPair.second; 39 if (!MacroBody.empty()) { 40 // Turn the = into ' '. 41 Buf.insert(Buf.end(), MacroName.begin(), MacroName.end()); 42 Buf.push_back(' '); 43 44 // Per GCC -D semantics, the macro ends at \n if it exists. 45 const char *End = strpbrk(MacroBody.data(), "\n\r"); 46 if (End) { 47 assert(Diags && "Unexpected macro with embedded newline!"); 48 Diags->Report(diag::warn_fe_macro_contains_embedded_newline) 49 << MacroName; 50 } else { 51 End = MacroBody.end(); 52 } 53 54 Buf.insert(Buf.end(), MacroBody.begin(), End); 55 } else { 56 // Push "macroname 1". 57 Buf.insert(Buf.end(), Macro.begin(), Macro.end()); 58 Buf.push_back(' '); 59 Buf.push_back('1'); 60 } 61 Buf.push_back('\n'); 62 } 63 64 // Append a #undef line to Buf for Macro. Macro should be of the form XXX 65 // and we emit "#undef XXX". 66 static void UndefineBuiltinMacro(std::vector<char> &Buf, llvm::StringRef Macro) { 67 // Push "macroname". 68 const char Command[] = "#undef "; 69 Buf.insert(Buf.end(), Command, Command+strlen(Command)); 70 Buf.insert(Buf.end(), Macro.begin(), Macro.end()); 71 Buf.push_back('\n'); 72 } 73 74 std::string clang::NormalizeDashIncludePath(llvm::StringRef File) { 75 // Implicit include paths should be resolved relative to the current 76 // working directory first, and then use the regular header search 77 // mechanism. The proper way to handle this is to have the 78 // predefines buffer located at the current working directory, but 79 // it has not file entry. For now, workaround this by using an 80 // absolute path if we find the file here, and otherwise letting 81 // header search handle it. 82 llvm::sys::Path Path(File); 83 Path.makeAbsolute(); 84 if (!Path.exists()) 85 Path = File; 86 87 return Lexer::Stringify(Path.str()); 88 } 89 90 /// Add the quoted name of an implicit include file. 91 static void AddQuotedIncludePath(std::vector<char> &Buf, 92 const std::string &File) { 93 94 // Escape double quotes etc. 95 Buf.push_back('"'); 96 std::string EscapedFile = NormalizeDashIncludePath(File); 97 Buf.insert(Buf.end(), EscapedFile.begin(), EscapedFile.end()); 98 Buf.push_back('"'); 99 } 100 101 /// AddImplicitInclude - Add an implicit #include of the specified file to the 102 /// predefines buffer. 103 static void AddImplicitInclude(std::vector<char> &Buf, 104 const std::string &File) { 105 const char Command[] = "#include "; 106 Buf.insert(Buf.end(), Command, Command+strlen(Command)); 107 AddQuotedIncludePath(Buf, File); 108 Buf.push_back('\n'); 109 } 110 111 static void AddImplicitIncludeMacros(std::vector<char> &Buf, 112 const std::string &File) { 113 const char Command[] = "#__include_macros "; 114 Buf.insert(Buf.end(), Command, Command+strlen(Command)); 115 AddQuotedIncludePath(Buf, File); 116 Buf.push_back('\n'); 117 // Marker token to stop the __include_macros fetch loop. 118 const char *Marker = "##\n"; // ##? 119 Buf.insert(Buf.end(), Marker, Marker+strlen(Marker)); 120 } 121 122 /// AddImplicitIncludePTH - Add an implicit #include using the original file 123 /// used to generate a PTH cache. 124 static void AddImplicitIncludePTH(std::vector<char> &Buf, Preprocessor &PP, 125 const std::string& ImplicitIncludePTH) { 126 PTHManager *P = PP.getPTHManager(); 127 assert(P && "No PTHManager."); 128 const char *OriginalFile = P->getOriginalSourceFile(); 129 130 if (!OriginalFile) { 131 PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header) 132 << ImplicitIncludePTH; 133 return; 134 } 135 136 AddImplicitInclude(Buf, OriginalFile); 137 } 138 139 /// PickFP - This is used to pick a value based on the FP semantics of the 140 /// specified FP model. 141 template <typename T> 142 static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal, 143 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal, 144 T IEEEQuadVal) { 145 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle) 146 return IEEESingleVal; 147 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble) 148 return IEEEDoubleVal; 149 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended) 150 return X87DoubleExtendedVal; 151 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble) 152 return PPCDoubleDoubleVal; 153 assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad); 154 return IEEEQuadVal; 155 } 156 157 static void DefineFloatMacros(std::vector<char> &Buf, const char *Prefix, 158 const llvm::fltSemantics *Sem) { 159 const char *DenormMin, *Epsilon, *Max, *Min; 160 DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324", 161 "3.64519953188247460253e-4951L", 162 "4.94065645841246544176568792868221e-324L", 163 "6.47517511943802511092443895822764655e-4966L"); 164 int Digits = PickFP(Sem, 6, 15, 18, 31, 33); 165 Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16", 166 "1.08420217248550443401e-19L", 167 "4.94065645841246544176568792868221e-324L", 168 "1.92592994438723585305597794258492732e-34L"); 169 int HasInifinity = 1, HasQuietNaN = 1; 170 int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113); 171 int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931); 172 int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932); 173 int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381); 174 int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384); 175 Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308", 176 "3.36210314311209350626e-4932L", 177 "2.00416836000897277799610805135016e-292L", 178 "3.36210314311209350626267781732175260e-4932L"); 179 Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308", 180 "1.18973149535723176502e+4932L", 181 "1.79769313486231580793728971405301e+308L", 182 "1.18973149535723176508575932662800702e+4932L"); 183 184 char MacroBuf[100]; 185 sprintf(MacroBuf, "__%s_DENORM_MIN__=%s", Prefix, DenormMin); 186 DefineBuiltinMacro(Buf, MacroBuf); 187 sprintf(MacroBuf, "__%s_DIG__=%d", Prefix, Digits); 188 DefineBuiltinMacro(Buf, MacroBuf); 189 sprintf(MacroBuf, "__%s_EPSILON__=%s", Prefix, Epsilon); 190 DefineBuiltinMacro(Buf, MacroBuf); 191 sprintf(MacroBuf, "__%s_HAS_INFINITY__=%d", Prefix, HasInifinity); 192 DefineBuiltinMacro(Buf, MacroBuf); 193 sprintf(MacroBuf, "__%s_HAS_QUIET_NAN__=%d", Prefix, HasQuietNaN); 194 DefineBuiltinMacro(Buf, MacroBuf); 195 sprintf(MacroBuf, "__%s_MANT_DIG__=%d", Prefix, MantissaDigits); 196 DefineBuiltinMacro(Buf, MacroBuf); 197 sprintf(MacroBuf, "__%s_MAX_10_EXP__=%d", Prefix, Max10Exp); 198 DefineBuiltinMacro(Buf, MacroBuf); 199 sprintf(MacroBuf, "__%s_MAX_EXP__=%d", Prefix, MaxExp); 200 DefineBuiltinMacro(Buf, MacroBuf); 201 sprintf(MacroBuf, "__%s_MAX__=%s", Prefix, Max); 202 DefineBuiltinMacro(Buf, MacroBuf); 203 sprintf(MacroBuf, "__%s_MIN_10_EXP__=(%d)", Prefix, Min10Exp); 204 DefineBuiltinMacro(Buf, MacroBuf); 205 sprintf(MacroBuf, "__%s_MIN_EXP__=(%d)", Prefix, MinExp); 206 DefineBuiltinMacro(Buf, MacroBuf); 207 sprintf(MacroBuf, "__%s_MIN__=%s", Prefix, Min); 208 DefineBuiltinMacro(Buf, MacroBuf); 209 sprintf(MacroBuf, "__%s_HAS_DENORM__=1", Prefix); 210 DefineBuiltinMacro(Buf, MacroBuf); 211 } 212 213 214 /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro 215 /// named MacroName with the max value for a type with width 'TypeWidth' a 216 /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL). 217 static void DefineTypeSize(const char *MacroName, unsigned TypeWidth, 218 const char *ValSuffix, bool isSigned, 219 std::vector<char> &Buf) { 220 char MacroBuf[60]; 221 long long MaxVal; 222 if (isSigned) 223 MaxVal = (1LL << (TypeWidth - 1)) - 1; 224 else 225 MaxVal = ~0LL >> (64-TypeWidth); 226 227 // FIXME: Switch to using raw_ostream and avoid utostr(). 228 sprintf(MacroBuf, "%s=%s%s", MacroName, llvm::utostr(MaxVal).c_str(), 229 ValSuffix); 230 DefineBuiltinMacro(Buf, MacroBuf); 231 } 232 233 /// DefineTypeSize - An overloaded helper that uses TargetInfo to determine 234 /// the width, suffix, and signedness of the given type 235 static void DefineTypeSize(const char *MacroName, TargetInfo::IntType Ty, 236 const TargetInfo &TI, std::vector<char> &Buf) { 237 DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty), 238 TI.isTypeSigned(Ty), Buf); 239 } 240 241 static void DefineType(const char *MacroName, TargetInfo::IntType Ty, 242 std::vector<char> &Buf) { 243 char MacroBuf[60]; 244 sprintf(MacroBuf, "%s=%s", MacroName, TargetInfo::getTypeName(Ty)); 245 DefineBuiltinMacro(Buf, MacroBuf); 246 } 247 248 static void DefineTypeWidth(const char *MacroName, TargetInfo::IntType Ty, 249 const TargetInfo &TI, std::vector<char> &Buf) { 250 char MacroBuf[60]; 251 sprintf(MacroBuf, "%s=%d", MacroName, TI.getTypeWidth(Ty)); 252 DefineBuiltinMacro(Buf, MacroBuf); 253 } 254 255 static void DefineExactWidthIntType(TargetInfo::IntType Ty, 256 const TargetInfo &TI, std::vector<char> &Buf) { 257 char MacroBuf[60]; 258 int TypeWidth = TI.getTypeWidth(Ty); 259 sprintf(MacroBuf, "__INT%d_TYPE__", TypeWidth); 260 DefineType(MacroBuf, Ty, Buf); 261 262 263 const char *ConstSuffix = TargetInfo::getTypeConstantSuffix(Ty); 264 if (strlen(ConstSuffix) > 0) { 265 sprintf(MacroBuf, "__INT%d_C_SUFFIX__=%s", TypeWidth, ConstSuffix); 266 DefineBuiltinMacro(Buf, MacroBuf); 267 } 268 } 269 270 static void InitializePredefinedMacros(const TargetInfo &TI, 271 const LangOptions &LangOpts, 272 std::vector<char> &Buf) { 273 // Compiler version introspection macros. 274 DefineBuiltinMacro(Buf, "__llvm__=1"); // LLVM Backend 275 DefineBuiltinMacro(Buf, "__clang__=1"); // Clang Frontend 276 277 // Currently claim to be compatible with GCC 4.2.1-5621. 278 DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2"); 279 DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1"); 280 DefineBuiltinMacro(Buf, "__GNUC__=4"); 281 DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002"); 282 DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 Compatible Clang Compiler\""); 283 284 285 // Initialize language-specific preprocessor defines. 286 287 // These should all be defined in the preprocessor according to the 288 // current language configuration. 289 if (!LangOpts.Microsoft) 290 DefineBuiltinMacro(Buf, "__STDC__=1"); 291 if (LangOpts.AsmPreprocessor) 292 DefineBuiltinMacro(Buf, "__ASSEMBLER__=1"); 293 294 if (!LangOpts.CPlusPlus) { 295 if (LangOpts.C99) 296 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L"); 297 else if (!LangOpts.GNUMode && LangOpts.Digraphs) 298 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L"); 299 } 300 301 // Standard conforming mode? 302 if (!LangOpts.GNUMode) 303 DefineBuiltinMacro(Buf, "__STRICT_ANSI__=1"); 304 305 if (LangOpts.CPlusPlus0x) 306 DefineBuiltinMacro(Buf, "__GXX_EXPERIMENTAL_CXX0X__"); 307 308 if (LangOpts.Freestanding) 309 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=0"); 310 else 311 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1"); 312 313 if (LangOpts.ObjC1) { 314 DefineBuiltinMacro(Buf, "__OBJC__=1"); 315 if (LangOpts.ObjCNonFragileABI) { 316 DefineBuiltinMacro(Buf, "__OBJC2__=1"); 317 DefineBuiltinMacro(Buf, "OBJC_ZEROCOST_EXCEPTIONS=1"); 318 } 319 320 if (LangOpts.getGCMode() != LangOptions::NonGC) 321 DefineBuiltinMacro(Buf, "__OBJC_GC__=1"); 322 323 if (LangOpts.NeXTRuntime) 324 DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1"); 325 } 326 327 // darwin_constant_cfstrings controls this. This is also dependent 328 // on other things like the runtime I believe. This is set even for C code. 329 DefineBuiltinMacro(Buf, "__CONSTANT_CFSTRINGS__=1"); 330 331 if (LangOpts.ObjC2) 332 DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES"); 333 334 if (LangOpts.PascalStrings) 335 DefineBuiltinMacro(Buf, "__PASCAL_STRINGS__"); 336 337 if (LangOpts.Blocks) { 338 DefineBuiltinMacro(Buf, "__block=__attribute__((__blocks__(byref)))"); 339 DefineBuiltinMacro(Buf, "__BLOCKS__=1"); 340 } 341 342 if (LangOpts.Exceptions) 343 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1"); 344 345 if (LangOpts.CPlusPlus) { 346 DefineBuiltinMacro(Buf, "__DEPRECATED=1"); 347 DefineBuiltinMacro(Buf, "__GNUG__=4"); 348 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1"); 349 if (LangOpts.GNUMode) 350 DefineBuiltinMacro(Buf, "__cplusplus=1"); 351 else 352 // C++ [cpp.predefined]p1: 353 // The name_ _cplusplusis defined to the value199711Lwhen compiling a 354 // C++ translation unit. 355 DefineBuiltinMacro(Buf, "__cplusplus=199711L"); 356 DefineBuiltinMacro(Buf, "__private_extern__=extern"); 357 // Ugly hack to work with GNU libstdc++. 358 DefineBuiltinMacro(Buf, "_GNU_SOURCE=1"); 359 } 360 361 if (LangOpts.Microsoft) { 362 // Filter out some microsoft extensions when trying to parse in ms-compat 363 // mode. 364 DefineBuiltinMacro(Buf, "__int8=__INT8_TYPE__"); 365 DefineBuiltinMacro(Buf, "__int16=__INT16_TYPE__"); 366 DefineBuiltinMacro(Buf, "__int32=__INT32_TYPE__"); 367 DefineBuiltinMacro(Buf, "__int64=__INT64_TYPE__"); 368 // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however 369 // VC++ appears to only like __FUNCTION__. 370 DefineBuiltinMacro(Buf, "__PRETTY_FUNCTION__=__FUNCTION__"); 371 // Work around some issues with Visual C++ headerws. 372 if (LangOpts.CPlusPlus) { 373 // Since we define wchar_t in C++ mode. 374 DefineBuiltinMacro(Buf, "_WCHAR_T_DEFINED=1"); 375 DefineBuiltinMacro(Buf, "_NATIVE_WCHAR_T_DEFINED=1"); 376 // FIXME: This should be temporary until we have a __pragma 377 // solution, to avoid some errors flagged in VC++ headers. 378 DefineBuiltinMacro(Buf, "_CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES=0"); 379 } 380 } 381 382 if (LangOpts.Optimize) 383 DefineBuiltinMacro(Buf, "__OPTIMIZE__=1"); 384 if (LangOpts.OptimizeSize) 385 DefineBuiltinMacro(Buf, "__OPTIMIZE_SIZE__=1"); 386 387 // Initialize target-specific preprocessor defines. 388 389 // Define type sizing macros based on the target properties. 390 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far"); 391 DefineBuiltinMacro(Buf, "__CHAR_BIT__=8"); 392 393 DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Buf); 394 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Buf); 395 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Buf); 396 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Buf); 397 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Buf); 398 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Buf); 399 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Buf); 400 401 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Buf); 402 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Buf); 403 DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Buf); 404 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Buf); 405 DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Buf); 406 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Buf); 407 DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Buf); 408 DefineType("__SIZE_TYPE__", TI.getSizeType(), Buf); 409 DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Buf); 410 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Buf); 411 DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Buf); 412 DefineType("__WINT_TYPE__", TI.getWIntType(), Buf); 413 DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Buf); 414 DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Buf); 415 416 DefineFloatMacros(Buf, "FLT", &TI.getFloatFormat()); 417 DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat()); 418 DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat()); 419 420 // Define a __POINTER_WIDTH__ macro for stdint.h. 421 char MacroBuf[60]; 422 sprintf(MacroBuf, "__POINTER_WIDTH__=%d", (int)TI.getPointerWidth(0)); 423 DefineBuiltinMacro(Buf, MacroBuf); 424 425 if (!LangOpts.CharIsSigned) 426 DefineBuiltinMacro(Buf, "__CHAR_UNSIGNED__"); 427 428 // Define exact-width integer types for stdint.h 429 sprintf(MacroBuf, "__INT%d_TYPE__=char", TI.getCharWidth()); 430 DefineBuiltinMacro(Buf, MacroBuf); 431 432 if (TI.getShortWidth() > TI.getCharWidth()) 433 DefineExactWidthIntType(TargetInfo::SignedShort, TI, Buf); 434 435 if (TI.getIntWidth() > TI.getShortWidth()) 436 DefineExactWidthIntType(TargetInfo::SignedInt, TI, Buf); 437 438 if (TI.getLongWidth() > TI.getIntWidth()) 439 DefineExactWidthIntType(TargetInfo::SignedLong, TI, Buf); 440 441 if (TI.getLongLongWidth() > TI.getLongWidth()) 442 DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Buf); 443 444 // Add __builtin_va_list typedef. 445 { 446 const char *VAList = TI.getVAListDeclaration(); 447 Buf.insert(Buf.end(), VAList, VAList+strlen(VAList)); 448 Buf.push_back('\n'); 449 } 450 451 if (const char *Prefix = TI.getUserLabelPrefix()) { 452 sprintf(MacroBuf, "__USER_LABEL_PREFIX__=%s", Prefix); 453 DefineBuiltinMacro(Buf, MacroBuf); 454 } 455 456 // Build configuration options. FIXME: these should be controlled by 457 // command line options or something. 458 DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0"); 459 460 if (LangOpts.GNUInline) 461 DefineBuiltinMacro(Buf, "__GNUC_GNU_INLINE__=1"); 462 else 463 DefineBuiltinMacro(Buf, "__GNUC_STDC_INLINE__=1"); 464 465 if (LangOpts.NoInline) 466 DefineBuiltinMacro(Buf, "__NO_INLINE__=1"); 467 468 if (unsigned PICLevel = LangOpts.PICLevel) { 469 sprintf(MacroBuf, "__PIC__=%d", PICLevel); 470 DefineBuiltinMacro(Buf, MacroBuf); 471 472 sprintf(MacroBuf, "__pic__=%d", PICLevel); 473 DefineBuiltinMacro(Buf, MacroBuf); 474 } 475 476 // Macros to control C99 numerics and <float.h> 477 DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0"); 478 DefineBuiltinMacro(Buf, "__FLT_RADIX__=2"); 479 sprintf(MacroBuf, "__DECIMAL_DIG__=%d", 480 PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36)); 481 DefineBuiltinMacro(Buf, MacroBuf); 482 483 if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn) 484 DefineBuiltinMacro(Buf, "__SSP__=1"); 485 else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq) 486 DefineBuiltinMacro(Buf, "__SSP_ALL__=2"); 487 488 // Get other target #defines. 489 TI.getTargetDefines(LangOpts, Buf); 490 } 491 492 // Initialize the remapping of files to alternative contents, e.g., 493 // those specified through other files. 494 static void InitializeFileRemapping(Diagnostic &Diags, 495 SourceManager &SourceMgr, 496 FileManager &FileMgr, 497 const PreprocessorOptions &InitOpts) { 498 // Remap files in the source manager. 499 for (PreprocessorOptions::remapped_file_iterator 500 Remap = InitOpts.remapped_file_begin(), 501 RemapEnd = InitOpts.remapped_file_end(); 502 Remap != RemapEnd; 503 ++Remap) { 504 // Find the file that we're mapping to. 505 const FileEntry *ToFile = FileMgr.getFile(Remap->second); 506 if (!ToFile) { 507 Diags.Report(diag::err_fe_remap_missing_to_file) 508 << Remap->first << Remap->second; 509 continue; 510 } 511 512 // Create the file entry for the file that we're mapping from. 513 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first, 514 ToFile->getSize(), 515 0); 516 if (!FromFile) { 517 Diags.Report(diag::err_fe_remap_missing_from_file) 518 << Remap->first; 519 continue; 520 } 521 522 // Load the contents of the file we're mapping to. 523 std::string ErrorStr; 524 const llvm::MemoryBuffer *Buffer 525 = llvm::MemoryBuffer::getFile(ToFile->getName(), &ErrorStr); 526 if (!Buffer) { 527 Diags.Report(diag::err_fe_error_opening) 528 << Remap->second << ErrorStr; 529 continue; 530 } 531 532 // Override the contents of the "from" file with the contents of 533 // the "to" file. 534 SourceMgr.overrideFileContents(FromFile, Buffer); 535 } 536 } 537 538 /// InitializePreprocessor - Initialize the preprocessor getting it and the 539 /// environment ready to process a single file. This returns true on error. 540 /// 541 void clang::InitializePreprocessor(Preprocessor &PP, 542 const PreprocessorOptions &InitOpts, 543 const HeaderSearchOptions &HSOpts) { 544 std::vector<char> PredefineBuffer; 545 546 InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(), 547 PP.getFileManager(), InitOpts); 548 549 const char *LineDirective = "# 1 \"<built-in>\" 3\n"; 550 PredefineBuffer.insert(PredefineBuffer.end(), 551 LineDirective, LineDirective+strlen(LineDirective)); 552 553 // Install things like __POWERPC__, __GNUC__, etc into the macro table. 554 if (InitOpts.UsePredefines) 555 InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(), 556 PredefineBuffer); 557 558 // Add on the predefines from the driver. Wrap in a #line directive to report 559 // that they come from the command line. 560 LineDirective = "# 1 \"<command line>\" 1\n"; 561 PredefineBuffer.insert(PredefineBuffer.end(), 562 LineDirective, LineDirective+strlen(LineDirective)); 563 564 // Process #define's and #undef's in the order they are given. 565 for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) { 566 if (InitOpts.Macros[i].second) // isUndef 567 UndefineBuiltinMacro(PredefineBuffer, InitOpts.Macros[i].first.c_str()); 568 else 569 DefineBuiltinMacro(PredefineBuffer, InitOpts.Macros[i].first.c_str(), 570 &PP.getDiagnostics()); 571 } 572 573 // If -imacros are specified, include them now. These are processed before 574 // any -include directives. 575 for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i) 576 AddImplicitIncludeMacros(PredefineBuffer, InitOpts.MacroIncludes[i]); 577 578 // Process -include directives. 579 for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) { 580 const std::string &Path = InitOpts.Includes[i]; 581 if (Path == InitOpts.ImplicitPTHInclude) 582 AddImplicitIncludePTH(PredefineBuffer, PP, Path); 583 else 584 AddImplicitInclude(PredefineBuffer, Path); 585 } 586 587 // Exit the command line and go back to <built-in> (2 is LC_LEAVE). 588 LineDirective = "# 1 \"<built-in>\" 2\n"; 589 PredefineBuffer.insert(PredefineBuffer.end(), 590 LineDirective, LineDirective+strlen(LineDirective)); 591 592 // Null terminate PredefinedBuffer and add it. 593 PredefineBuffer.push_back(0); 594 PP.setPredefines(&PredefineBuffer[0]); 595 596 // Initialize the header search object. 597 ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts, 598 PP.getLangOptions(), 599 PP.getTargetInfo().getTriple()); 600 } 601