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