InitPreprocessor.cpp revision 218893
1152615Sle//===--- InitPreprocessor.cpp - PP initialization code. ---------*- C++ -*-===// 2152615Sle// 3152615Sle// The LLVM Compiler Infrastructure 4152632Sle// 5152632Sle// This file is distributed under the University of Illinois Open Source 6152632Sle// License. See LICENSE.TXT for details. 7152632Sle// 8152632Sle//===----------------------------------------------------------------------===// 9152615Sle// 10152615Sle// This file implements the clang::InitializePreprocessor function. 11152615Sle// 12152615Sle//===----------------------------------------------------------------------===// 13152615Sle 14152615Sle#include "clang/Basic/Version.h" 15152615Sle#include "clang/Frontend/Utils.h" 16152615Sle#include "clang/Basic/MacroBuilder.h" 17152632Sle#include "clang/Basic/TargetInfo.h" 18152615Sle#include "clang/Frontend/FrontendDiagnostic.h" 19152615Sle#include "clang/Frontend/FrontendOptions.h" 20152615Sle#include "clang/Frontend/PreprocessorOptions.h" 21152615Sle#include "clang/Lex/Preprocessor.h" 22152615Sle#include "clang/Basic/FileManager.h" 23152615Sle#include "clang/Basic/SourceManager.h" 24152615Sle#include "llvm/ADT/APFloat.h" 25152615Sle#include "llvm/Support/FileSystem.h" 26152615Sle#include "llvm/Support/MemoryBuffer.h" 27152615Sle#include "llvm/Support/Path.h" 28152615Sleusing namespace clang; 29152615Sle 30152615Sle// Append a #define line to Buf for Macro. Macro should be of the form XXX, 31152615Sle// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit 32152615Sle// "#define XXX Y z W". To get a #define with no value, use "XXX=". 33152615Slestatic void DefineBuiltinMacro(MacroBuilder &Builder, llvm::StringRef Macro, 34152615Sle Diagnostic &Diags) { 35152615Sle std::pair<llvm::StringRef, llvm::StringRef> MacroPair = Macro.split('='); 36152615Sle llvm::StringRef MacroName = MacroPair.first; 37152615Sle llvm::StringRef MacroBody = MacroPair.second; 38152615Sle if (MacroName.size() != Macro.size()) { 39152615Sle // Per GCC -D semantics, the macro ends at \n if it exists. 40152615Sle llvm::StringRef::size_type End = MacroBody.find_first_of("\n\r"); 41152615Sle if (End != llvm::StringRef::npos) 42152615Sle Diags.Report(diag::warn_fe_macro_contains_embedded_newline) 43152615Sle << MacroName; 44152615Sle Builder.defineMacro(MacroName, MacroBody.substr(0, End)); 45152615Sle } else { 46152615Sle // Push "macroname 1". 47190507Slulf Builder.defineMacro(Macro); 48152615Sle } 49190507Slulf} 50152615Sle 51152615Slestd::string clang::NormalizeDashIncludePath(llvm::StringRef File) { 52152615Sle // Implicit include paths should be resolved relative to the current 53152615Sle // working directory first, and then use the regular header search 54185309Slulf // mechanism. The proper way to handle this is to have the 55185309Slulf // predefines buffer located at the current working directory, but 56185309Slulf // it has not file entry. For now, workaround this by using an 57185309Slulf // absolute path if we find the file here, and otherwise letting 58152615Sle // header search handle it. 59185309Slulf llvm::SmallString<128> Path(File); 60185309Slulf llvm::sys::fs::make_absolute(Path); 61185309Slulf bool exists; 62185309Slulf if (llvm::sys::fs::exists(Path.str(), exists) || !exists) 63152615Sle Path = File; 64152615Sle 65152615Sle return Lexer::Stringify(Path.str()); 66152615Sle} 67152615Sle 68152615Sle/// AddImplicitInclude - Add an implicit #include of the specified file to the 69152615Sle/// predefines buffer. 70152615Slestatic void AddImplicitInclude(MacroBuilder &Builder, llvm::StringRef File) { 71152615Sle Builder.append("#include \"" + 72190507Slulf llvm::Twine(NormalizeDashIncludePath(File)) + "\""); 73152615Sle} 74152615Sle 75152615Slestatic void AddImplicitIncludeMacros(MacroBuilder &Builder, 76152615Sle llvm::StringRef File) { 77152615Sle Builder.append("#__include_macros \"" + 78152615Sle llvm::Twine(NormalizeDashIncludePath(File)) + "\""); 79152615Sle // Marker token to stop the __include_macros fetch loop. 80152615Sle Builder.append("##"); // ##? 81152615Sle} 82152615Sle 83152615Sle/// AddImplicitIncludePTH - Add an implicit #include using the original file 84152615Sle/// used to generate a PTH cache. 85152615Slestatic void AddImplicitIncludePTH(MacroBuilder &Builder, Preprocessor &PP, 86152615Sle llvm::StringRef ImplicitIncludePTH) { 87197767Slulf PTHManager *P = PP.getPTHManager(); 88152615Sle // Null check 'P' in the corner case where it couldn't be created. 89152615Sle const char *OriginalFile = P ? P->getOriginalSourceFile() : 0; 90152615Sle 91152615Sle if (!OriginalFile) { 92152615Sle PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header) 93152615Sle << ImplicitIncludePTH; 94152615Sle return; 95152615Sle } 96190507Slulf 97152615Sle AddImplicitInclude(Builder, OriginalFile); 98152615Sle} 99152615Sle 100152615Sle/// PickFP - This is used to pick a value based on the FP semantics of the 101190507Slulf/// specified FP model. 102190507Slulftemplate <typename T> 103190507Slulfstatic T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal, 104152615Sle T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal, 105152615Sle T IEEEQuadVal) { 106152615Sle if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle) 107152615Sle return IEEESingleVal; 108152632Sle if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble) 109152615Sle return IEEEDoubleVal; 110152632Sle if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended) 111152632Sle return X87DoubleExtendedVal; 112190507Slulf if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble) 113152615Sle return PPCDoubleDoubleVal; 114190507Slulf assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad); 115152615Sle return IEEEQuadVal; 116190507Slulf} 117190507Slulf 118213318Slulfstatic void DefineFloatMacros(MacroBuilder &Builder, llvm::StringRef Prefix, 119190507Slulf const llvm::fltSemantics *Sem) { 120190507Slulf const char *DenormMin, *Epsilon, *Max, *Min; 121190507Slulf DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324", 122152615Sle "3.64519953188247460253e-4951L", 123152615Sle "4.94065645841246544176568792868221e-324L", 124213318Slulf "6.47517511943802511092443895822764655e-4966L"); 125190507Slulf int Digits = PickFP(Sem, 6, 15, 18, 31, 33); 126152615Sle Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16", 127190507Slulf "1.08420217248550443401e-19L", 128152615Sle "4.94065645841246544176568792868221e-324L", 129152615Sle "1.92592994438723585305597794258492732e-34L"); 130190507Slulf int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113); 131190507Slulf int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931); 132190507Slulf int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932); 133190507Slulf int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381); 134152615Sle int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384); 135152615Sle Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308", 136152615Sle "3.36210314311209350626e-4932L", 137152615Sle "2.00416836000897277799610805135016e-292L", 138152615Sle "3.36210314311209350626267781732175260e-4932L"); 139190507Slulf Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308", 140152615Sle "1.18973149535723176502e+4932L", 141190507Slulf "1.79769313486231580793728971405301e+308L", 142152615Sle "1.18973149535723176508575932662800702e+4932L"); 143190507Slulf 144152615Sle llvm::SmallString<32> DefPrefix; 145152615Sle DefPrefix = "__"; 146152615Sle DefPrefix += Prefix; 147152615Sle DefPrefix += "_"; 148197767Slulf 149152615Sle Builder.defineMacro(DefPrefix + "DENORM_MIN__", DenormMin); 150152615Sle Builder.defineMacro(DefPrefix + "HAS_DENORM__"); 151152615Sle Builder.defineMacro(DefPrefix + "DIG__", llvm::Twine(Digits)); 152152615Sle Builder.defineMacro(DefPrefix + "EPSILON__", llvm::Twine(Epsilon)); 153152615Sle Builder.defineMacro(DefPrefix + "HAS_INFINITY__"); 154152615Sle Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__"); 155152615Sle Builder.defineMacro(DefPrefix + "MANT_DIG__", llvm::Twine(MantissaDigits)); 156152615Sle 157152615Sle Builder.defineMacro(DefPrefix + "MAX_10_EXP__", llvm::Twine(Max10Exp)); 158152615Sle Builder.defineMacro(DefPrefix + "MAX_EXP__", llvm::Twine(MaxExp)); 159152615Sle Builder.defineMacro(DefPrefix + "MAX__", llvm::Twine(Max)); 160152632Sle 161152615Sle Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+llvm::Twine(Min10Exp)+")"); 162190507Slulf Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+llvm::Twine(MinExp)+")"); 163190507Slulf Builder.defineMacro(DefPrefix + "MIN__", llvm::Twine(Min)); 164190507Slulf} 165152615Sle 166152615Sle 167152615Sle/// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro 168190507Slulf/// named MacroName with the max value for a type with width 'TypeWidth' a 169152615Sle/// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL). 170152615Slestatic void DefineTypeSize(llvm::StringRef MacroName, unsigned TypeWidth, 171152615Sle llvm::StringRef ValSuffix, bool isSigned, 172152615Sle MacroBuilder& Builder) { 173152615Sle long long MaxVal; 174152615Sle if (isSigned) { 175152615Sle assert(TypeWidth != 1); 176152615Sle MaxVal = ~0ULL >> (65-TypeWidth); 177152615Sle } else 178190507Slulf MaxVal = ~0ULL >> (64-TypeWidth); 179152615Sle 180152615Sle Builder.defineMacro(MacroName, llvm::Twine(MaxVal) + ValSuffix); 181190507Slulf} 182152615Sle 183190507Slulf/// DefineTypeSize - An overloaded helper that uses TargetInfo to determine 184190507Slulf/// the width, suffix, and signedness of the given type 185190507Slulfstatic void DefineTypeSize(llvm::StringRef MacroName, TargetInfo::IntType Ty, 186190507Slulf const TargetInfo &TI, MacroBuilder &Builder) { 187152615Sle DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty), 188152615Sle TI.isTypeSigned(Ty), Builder); 189} 190 191static void DefineType(const llvm::Twine &MacroName, TargetInfo::IntType Ty, 192 MacroBuilder &Builder) { 193 Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty)); 194} 195 196static void DefineTypeWidth(llvm::StringRef MacroName, TargetInfo::IntType Ty, 197 const TargetInfo &TI, MacroBuilder &Builder) { 198 Builder.defineMacro(MacroName, llvm::Twine(TI.getTypeWidth(Ty))); 199} 200 201static void DefineTypeSizeof(llvm::StringRef MacroName, unsigned BitWidth, 202 const TargetInfo &TI, MacroBuilder &Builder) { 203 Builder.defineMacro(MacroName, 204 llvm::Twine(BitWidth / TI.getCharWidth())); 205} 206 207static void DefineExactWidthIntType(TargetInfo::IntType Ty, 208 const TargetInfo &TI, MacroBuilder &Builder) { 209 int TypeWidth = TI.getTypeWidth(Ty); 210 211 // Use the target specified int64 type, when appropriate, so that [u]int64_t 212 // ends up being defined in terms of the correct type. 213 if (TypeWidth == 64) 214 Ty = TI.getInt64Type(); 215 216 DefineType("__INT" + llvm::Twine(TypeWidth) + "_TYPE__", Ty, Builder); 217 218 llvm::StringRef ConstSuffix(TargetInfo::getTypeConstantSuffix(Ty)); 219 if (!ConstSuffix.empty()) 220 Builder.defineMacro("__INT" + llvm::Twine(TypeWidth) + "_C_SUFFIX__", 221 ConstSuffix); 222} 223 224static void InitializePredefinedMacros(const TargetInfo &TI, 225 const LangOptions &LangOpts, 226 const FrontendOptions &FEOpts, 227 MacroBuilder &Builder) { 228 // Compiler version introspection macros. 229 Builder.defineMacro("__llvm__"); // LLVM Backend 230 Builder.defineMacro("__clang__"); // Clang Frontend 231#define TOSTR2(X) #X 232#define TOSTR(X) TOSTR2(X) 233 Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR)); 234 Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR)); 235#ifdef CLANG_VERSION_PATCHLEVEL 236 Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL)); 237#else 238 Builder.defineMacro("__clang_patchlevel__", "0"); 239#endif 240 Builder.defineMacro("__clang_version__", 241 "\"" CLANG_VERSION_STRING " (" 242 + getClangFullRepositoryVersion() + ")\""); 243#undef TOSTR 244#undef TOSTR2 245 // Currently claim to be compatible with GCC 4.2.1-5621. 246 Builder.defineMacro("__GNUC_MINOR__", "2"); 247 Builder.defineMacro("__GNUC_PATCHLEVEL__", "1"); 248 Builder.defineMacro("__GNUC__", "4"); 249 Builder.defineMacro("__GXX_ABI_VERSION", "1002"); 250 Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible Clang Compiler\""); 251 252 // Initialize language-specific preprocessor defines. 253 254 // These should all be defined in the preprocessor according to the 255 // current language configuration. 256 if (!LangOpts.Microsoft) 257 Builder.defineMacro("__STDC__"); 258 if (LangOpts.AsmPreprocessor) 259 Builder.defineMacro("__ASSEMBLER__"); 260 261 if (!LangOpts.CPlusPlus) { 262 if (LangOpts.C99) 263 Builder.defineMacro("__STDC_VERSION__", "199901L"); 264 else if (!LangOpts.GNUMode && LangOpts.Digraphs) 265 Builder.defineMacro("__STDC_VERSION__", "199409L"); 266 } 267 268 // Standard conforming mode? 269 if (!LangOpts.GNUMode) 270 Builder.defineMacro("__STRICT_ANSI__"); 271 272 if (LangOpts.CPlusPlus0x) 273 Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__"); 274 275 if (LangOpts.Freestanding) 276 Builder.defineMacro("__STDC_HOSTED__", "0"); 277 else 278 Builder.defineMacro("__STDC_HOSTED__"); 279 280 if (LangOpts.ObjC1) { 281 Builder.defineMacro("__OBJC__"); 282 if (LangOpts.ObjCNonFragileABI) { 283 Builder.defineMacro("__OBJC2__"); 284 Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS"); 285 } 286 287 if (LangOpts.getGCMode() != LangOptions::NonGC) 288 Builder.defineMacro("__OBJC_GC__"); 289 290 if (LangOpts.NeXTRuntime) 291 Builder.defineMacro("__NEXT_RUNTIME__"); 292 } 293 294 // 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 Builder.defineMacro("__CONSTANT_CFSTRINGS__"); 297 298 if (LangOpts.ObjC2) 299 Builder.defineMacro("OBJC_NEW_PROPERTIES"); 300 301 if (LangOpts.PascalStrings) 302 Builder.defineMacro("__PASCAL_STRINGS__"); 303 304 if (LangOpts.Blocks) { 305 Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))"); 306 Builder.defineMacro("__BLOCKS__"); 307 } 308 309 if (LangOpts.Exceptions) 310 Builder.defineMacro("__EXCEPTIONS"); 311 if (LangOpts.RTTI) 312 Builder.defineMacro("__GXX_RTTI"); 313 if (LangOpts.SjLjExceptions) 314 Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__"); 315 316 if (LangOpts.CPlusPlus) { 317 Builder.defineMacro("__DEPRECATED"); 318 Builder.defineMacro("__GNUG__", "4"); 319 Builder.defineMacro("__GXX_WEAK__"); 320 if (LangOpts.GNUMode) 321 Builder.defineMacro("__cplusplus"); 322 else 323 // C++ [cpp.predefined]p1: 324 // The name_ _cplusplusis defined to the value 199711L when compiling a 325 // C++ translation unit. 326 Builder.defineMacro("__cplusplus", "199711L"); 327 Builder.defineMacro("__private_extern__", "extern"); 328 } 329 330 if (LangOpts.Microsoft) { 331 // Filter out some microsoft extensions when trying to parse in ms-compat 332 // mode. 333 Builder.defineMacro("__int8", "__INT8_TYPE__"); 334 Builder.defineMacro("__int16", "__INT16_TYPE__"); 335 Builder.defineMacro("__int32", "__INT32_TYPE__"); 336 Builder.defineMacro("__int64", "__INT64_TYPE__"); 337 // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however 338 // VC++ appears to only like __FUNCTION__. 339 Builder.defineMacro("__PRETTY_FUNCTION__", "__FUNCTION__"); 340 // Work around some issues with Visual C++ headerws. 341 if (LangOpts.CPlusPlus) { 342 // Since we define wchar_t in C++ mode. 343 Builder.defineMacro("_WCHAR_T_DEFINED"); 344 Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED"); 345 Builder.append("class type_info;"); 346 } 347 348 if (LangOpts.CPlusPlus0x) { 349 Builder.defineMacro("_HAS_CHAR16_T_LANGUAGE_SUPPORT", "1"); 350 } 351 } 352 353 if (LangOpts.Optimize) 354 Builder.defineMacro("__OPTIMIZE__"); 355 if (LangOpts.OptimizeSize) 356 Builder.defineMacro("__OPTIMIZE_SIZE__"); 357 358 // Initialize target-specific preprocessor defines. 359 360 // Define type sizing macros based on the target properties. 361 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far"); 362 Builder.defineMacro("__CHAR_BIT__", "8"); 363 364 DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Builder); 365 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder); 366 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder); 367 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder); 368 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder); 369 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder); 370 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder); 371 372 DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder); 373 DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder); 374 DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder); 375 DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder); 376 DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder); 377 DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder); 378 DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder); 379 DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder); 380 DefineTypeSizeof("__SIZEOF_PTRDIFF_T__", 381 TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder); 382 DefineTypeSizeof("__SIZEOF_SIZE_T__", 383 TI.getTypeWidth(TI.getSizeType()), TI, Builder); 384 DefineTypeSizeof("__SIZEOF_WCHAR_T__", 385 TI.getTypeWidth(TI.getWCharType()), TI, Builder); 386 DefineTypeSizeof("__SIZEOF_WINT_T__", 387 TI.getTypeWidth(TI.getWIntType()), TI, Builder); 388 389 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder); 390 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder); 391 DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder); 392 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder); 393 DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder); 394 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder); 395 DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder); 396 DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder); 397 DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder); 398 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder); 399 DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder); 400 DefineType("__WINT_TYPE__", TI.getWIntType(), Builder); 401 DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder); 402 DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder); 403 DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder); 404 DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder); 405 406 DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat()); 407 DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat()); 408 DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat()); 409 410 // Define a __POINTER_WIDTH__ macro for stdint.h. 411 Builder.defineMacro("__POINTER_WIDTH__", 412 llvm::Twine((int)TI.getPointerWidth(0))); 413 414 if (!LangOpts.CharIsSigned) 415 Builder.defineMacro("__CHAR_UNSIGNED__"); 416 417 // Define exact-width integer types for stdint.h 418 Builder.defineMacro("__INT" + llvm::Twine(TI.getCharWidth()) + "_TYPE__", 419 "char"); 420 421 if (TI.getShortWidth() > TI.getCharWidth()) 422 DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder); 423 424 if (TI.getIntWidth() > TI.getShortWidth()) 425 DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder); 426 427 if (TI.getLongWidth() > TI.getIntWidth()) 428 DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder); 429 430 if (TI.getLongLongWidth() > TI.getLongWidth()) 431 DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder); 432 433 // Add __builtin_va_list typedef. 434 Builder.append(TI.getVAListDeclaration()); 435 436 if (const char *Prefix = TI.getUserLabelPrefix()) 437 Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix); 438 439 // Build configuration options. FIXME: these should be controlled by 440 // command line options or something. 441 Builder.defineMacro("__FINITE_MATH_ONLY__", "0"); 442 443 if (LangOpts.GNUInline) 444 Builder.defineMacro("__GNUC_GNU_INLINE__"); 445 else 446 Builder.defineMacro("__GNUC_STDC_INLINE__"); 447 448 if (LangOpts.NoInline) 449 Builder.defineMacro("__NO_INLINE__"); 450 451 if (unsigned PICLevel = LangOpts.PICLevel) { 452 Builder.defineMacro("__PIC__", llvm::Twine(PICLevel)); 453 Builder.defineMacro("__pic__", llvm::Twine(PICLevel)); 454 } 455 456 // Macros to control C99 numerics and <float.h> 457 Builder.defineMacro("__FLT_EVAL_METHOD__", "0"); 458 Builder.defineMacro("__FLT_RADIX__", "2"); 459 int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36); 460 Builder.defineMacro("__DECIMAL_DIG__", llvm::Twine(Dig)); 461 462 if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn) 463 Builder.defineMacro("__SSP__"); 464 else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq) 465 Builder.defineMacro("__SSP_ALL__", "2"); 466 467 if (FEOpts.ProgramAction == frontend::RewriteObjC) 468 Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))"); 469 470 // Define a macro that exists only when using the static analyzer. 471 if (FEOpts.ProgramAction == frontend::RunAnalysis) 472 Builder.defineMacro("__clang_analyzer__"); 473 474 if (LangOpts.FastRelaxedMath) 475 Builder.defineMacro("__FAST_RELAXED_MATH__"); 476 477 // Get other target #defines. 478 TI.getTargetDefines(LangOpts, Builder); 479} 480 481// Initialize the remapping of files to alternative contents, e.g., 482// those specified through other files. 483static void InitializeFileRemapping(Diagnostic &Diags, 484 SourceManager &SourceMgr, 485 FileManager &FileMgr, 486 const PreprocessorOptions &InitOpts) { 487 // Remap files in the source manager (with buffers). 488 for (PreprocessorOptions::const_remapped_file_buffer_iterator 489 Remap = InitOpts.remapped_file_buffer_begin(), 490 RemapEnd = InitOpts.remapped_file_buffer_end(); 491 Remap != RemapEnd; 492 ++Remap) { 493 // Create the file entry for the file that we're mapping from. 494 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first, 495 Remap->second->getBufferSize(), 496 0); 497 if (!FromFile) { 498 Diags.Report(diag::err_fe_remap_missing_from_file) 499 << Remap->first; 500 if (!InitOpts.RetainRemappedFileBuffers) 501 delete Remap->second; 502 continue; 503 } 504 505 // Override the contents of the "from" file with the contents of 506 // the "to" file. 507 SourceMgr.overrideFileContents(FromFile, Remap->second, 508 InitOpts.RetainRemappedFileBuffers); 509 } 510 511 // Remap files in the source manager (with other files). 512 for (PreprocessorOptions::const_remapped_file_iterator 513 Remap = InitOpts.remapped_file_begin(), 514 RemapEnd = InitOpts.remapped_file_end(); 515 Remap != RemapEnd; 516 ++Remap) { 517 // Find the file that we're mapping to. 518 const FileEntry *ToFile = FileMgr.getFile(Remap->second); 519 if (!ToFile) { 520 Diags.Report(diag::err_fe_remap_missing_to_file) 521 << Remap->first << Remap->second; 522 continue; 523 } 524 525 // Create the file entry for the file that we're mapping from. 526 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first, 527 ToFile->getSize(), 0); 528 if (!FromFile) { 529 Diags.Report(diag::err_fe_remap_missing_from_file) 530 << Remap->first; 531 continue; 532 } 533 534 // Load the contents of the file we're mapping to. 535 std::string ErrorStr; 536 const llvm::MemoryBuffer *Buffer 537 = FileMgr.getBufferForFile(ToFile->getName(), &ErrorStr); 538 if (!Buffer) { 539 Diags.Report(diag::err_fe_error_opening) 540 << Remap->second << ErrorStr; 541 continue; 542 } 543 544 // Override the contents of the "from" file with the contents of 545 // the "to" file. 546 SourceMgr.overrideFileContents(FromFile, Buffer); 547 } 548} 549 550/// InitializePreprocessor - Initialize the preprocessor getting it and the 551/// environment ready to process a single file. This returns true on error. 552/// 553void clang::InitializePreprocessor(Preprocessor &PP, 554 const PreprocessorOptions &InitOpts, 555 const HeaderSearchOptions &HSOpts, 556 const FrontendOptions &FEOpts) { 557 std::string PredefineBuffer; 558 PredefineBuffer.reserve(4080); 559 llvm::raw_string_ostream Predefines(PredefineBuffer); 560 MacroBuilder Builder(Predefines); 561 562 InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(), 563 PP.getFileManager(), InitOpts); 564 565 // Emit line markers for various builtin sections of the file. We don't do 566 // this in asm preprocessor mode, because "# 4" is not a line marker directive 567 // in this mode. 568 if (!PP.getLangOptions().AsmPreprocessor) 569 Builder.append("# 1 \"<built-in>\" 3"); 570 571 // Install things like __POWERPC__, __GNUC__, etc into the macro table. 572 if (InitOpts.UsePredefines) 573 InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(), 574 FEOpts, Builder); 575 576 // Add on the predefines from the driver. Wrap in a #line directive to report 577 // that they come from the command line. 578 if (!PP.getLangOptions().AsmPreprocessor) 579 Builder.append("# 1 \"<command line>\" 1"); 580 581 // Process #define's and #undef's in the order they are given. 582 for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) { 583 if (InitOpts.Macros[i].second) // isUndef 584 Builder.undefineMacro(InitOpts.Macros[i].first); 585 else 586 DefineBuiltinMacro(Builder, InitOpts.Macros[i].first, 587 PP.getDiagnostics()); 588 } 589 590 // If -imacros are specified, include them now. These are processed before 591 // any -include directives. 592 for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i) 593 AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i]); 594 595 // Process -include directives. 596 for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) { 597 const std::string &Path = InitOpts.Includes[i]; 598 if (Path == InitOpts.ImplicitPTHInclude) 599 AddImplicitIncludePTH(Builder, PP, Path); 600 else 601 AddImplicitInclude(Builder, Path); 602 } 603 604 // Exit the command line and go back to <built-in> (2 is LC_LEAVE). 605 if (!PP.getLangOptions().AsmPreprocessor) 606 Builder.append("# 1 \"<built-in>\" 2"); 607 608 // Instruct the preprocessor to skip the preamble. 609 PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first, 610 InitOpts.PrecompiledPreambleBytes.second); 611 612 // Copy PredefinedBuffer into the Preprocessor. 613 PP.setPredefines(Predefines.str()); 614 615 // Initialize the header search object. 616 ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts, 617 PP.getLangOptions(), 618 PP.getTargetInfo().getTriple()); 619} 620