clang API Documentation
00001 //===--- InitPreprocessor.cpp - PP initialization code. ---------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the clang::InitializePreprocessor function. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/Basic/Version.h" 00015 #include "clang/Frontend/Utils.h" 00016 #include "clang/Basic/MacroBuilder.h" 00017 #include "clang/Basic/TargetInfo.h" 00018 #include "clang/Frontend/FrontendDiagnostic.h" 00019 #include "clang/Frontend/FrontendOptions.h" 00020 #include "clang/Frontend/PreprocessorOptions.h" 00021 #include "clang/Lex/HeaderSearch.h" 00022 #include "clang/Lex/Preprocessor.h" 00023 #include "clang/Basic/FileManager.h" 00024 #include "clang/Basic/SourceManager.h" 00025 #include "llvm/ADT/APFloat.h" 00026 #include "llvm/Support/FileSystem.h" 00027 #include "llvm/Support/MemoryBuffer.h" 00028 #include "llvm/Support/Path.h" 00029 using namespace clang; 00030 00031 // Append a #define line to Buf for Macro. Macro should be of the form XXX, 00032 // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit 00033 // "#define XXX Y z W". To get a #define with no value, use "XXX=". 00034 static void DefineBuiltinMacro(MacroBuilder &Builder, StringRef Macro, 00035 DiagnosticsEngine &Diags) { 00036 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 00037 StringRef MacroName = MacroPair.first; 00038 StringRef MacroBody = MacroPair.second; 00039 if (MacroName.size() != Macro.size()) { 00040 // Per GCC -D semantics, the macro ends at \n if it exists. 00041 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 00042 if (End != StringRef::npos) 00043 Diags.Report(diag::warn_fe_macro_contains_embedded_newline) 00044 << MacroName; 00045 Builder.defineMacro(MacroName, MacroBody.substr(0, End)); 00046 } else { 00047 // Push "macroname 1". 00048 Builder.defineMacro(Macro); 00049 } 00050 } 00051 00052 /// AddImplicitInclude - Add an implicit #include of the specified file to the 00053 /// predefines buffer. 00054 static void AddImplicitInclude(MacroBuilder &Builder, StringRef File, 00055 FileManager &FileMgr) { 00056 Builder.append(Twine("#include \"") + 00057 HeaderSearch::NormalizeDashIncludePath(File, FileMgr) + "\""); 00058 } 00059 00060 static void AddImplicitIncludeMacros(MacroBuilder &Builder, 00061 StringRef File, 00062 FileManager &FileMgr) { 00063 Builder.append(Twine("#__include_macros \"") + 00064 HeaderSearch::NormalizeDashIncludePath(File, FileMgr) + "\""); 00065 // Marker token to stop the __include_macros fetch loop. 00066 Builder.append("##"); // ##? 00067 } 00068 00069 /// AddImplicitIncludePTH - Add an implicit #include using the original file 00070 /// used to generate a PTH cache. 00071 static void AddImplicitIncludePTH(MacroBuilder &Builder, Preprocessor &PP, 00072 StringRef ImplicitIncludePTH) { 00073 PTHManager *P = PP.getPTHManager(); 00074 // Null check 'P' in the corner case where it couldn't be created. 00075 const char *OriginalFile = P ? P->getOriginalSourceFile() : 0; 00076 00077 if (!OriginalFile) { 00078 PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header) 00079 << ImplicitIncludePTH; 00080 return; 00081 } 00082 00083 AddImplicitInclude(Builder, OriginalFile, PP.getFileManager()); 00084 } 00085 00086 /// PickFP - This is used to pick a value based on the FP semantics of the 00087 /// specified FP model. 00088 template <typename T> 00089 static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal, 00090 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal, 00091 T IEEEQuadVal) { 00092 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle) 00093 return IEEESingleVal; 00094 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble) 00095 return IEEEDoubleVal; 00096 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended) 00097 return X87DoubleExtendedVal; 00098 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble) 00099 return PPCDoubleDoubleVal; 00100 assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad); 00101 return IEEEQuadVal; 00102 } 00103 00104 static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix, 00105 const llvm::fltSemantics *Sem) { 00106 const char *DenormMin, *Epsilon, *Max, *Min; 00107 DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324", 00108 "3.64519953188247460253e-4951L", 00109 "4.94065645841246544176568792868221e-324L", 00110 "6.47517511943802511092443895822764655e-4966L"); 00111 int Digits = PickFP(Sem, 6, 15, 18, 31, 33); 00112 Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16", 00113 "1.08420217248550443401e-19L", 00114 "4.94065645841246544176568792868221e-324L", 00115 "1.92592994438723585305597794258492732e-34L"); 00116 int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113); 00117 int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931); 00118 int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932); 00119 int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381); 00120 int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384); 00121 Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308", 00122 "3.36210314311209350626e-4932L", 00123 "2.00416836000897277799610805135016e-292L", 00124 "3.36210314311209350626267781732175260e-4932L"); 00125 Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308", 00126 "1.18973149535723176502e+4932L", 00127 "1.79769313486231580793728971405301e+308L", 00128 "1.18973149535723176508575932662800702e+4932L"); 00129 00130 SmallString<32> DefPrefix; 00131 DefPrefix = "__"; 00132 DefPrefix += Prefix; 00133 DefPrefix += "_"; 00134 00135 Builder.defineMacro(DefPrefix + "DENORM_MIN__", DenormMin); 00136 Builder.defineMacro(DefPrefix + "HAS_DENORM__"); 00137 Builder.defineMacro(DefPrefix + "DIG__", Twine(Digits)); 00138 Builder.defineMacro(DefPrefix + "EPSILON__", Twine(Epsilon)); 00139 Builder.defineMacro(DefPrefix + "HAS_INFINITY__"); 00140 Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__"); 00141 Builder.defineMacro(DefPrefix + "MANT_DIG__", Twine(MantissaDigits)); 00142 00143 Builder.defineMacro(DefPrefix + "MAX_10_EXP__", Twine(Max10Exp)); 00144 Builder.defineMacro(DefPrefix + "MAX_EXP__", Twine(MaxExp)); 00145 Builder.defineMacro(DefPrefix + "MAX__", Twine(Max)); 00146 00147 Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+Twine(Min10Exp)+")"); 00148 Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+Twine(MinExp)+")"); 00149 Builder.defineMacro(DefPrefix + "MIN__", Twine(Min)); 00150 } 00151 00152 00153 /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro 00154 /// named MacroName with the max value for a type with width 'TypeWidth' a 00155 /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL). 00156 static void DefineTypeSize(StringRef MacroName, unsigned TypeWidth, 00157 StringRef ValSuffix, bool isSigned, 00158 MacroBuilder &Builder) { 00159 llvm::APInt MaxVal = isSigned ? llvm::APInt::getSignedMaxValue(TypeWidth) 00160 : llvm::APInt::getMaxValue(TypeWidth); 00161 Builder.defineMacro(MacroName, MaxVal.toString(10, isSigned) + ValSuffix); 00162 } 00163 00164 /// DefineTypeSize - An overloaded helper that uses TargetInfo to determine 00165 /// the width, suffix, and signedness of the given type 00166 static void DefineTypeSize(StringRef MacroName, TargetInfo::IntType Ty, 00167 const TargetInfo &TI, MacroBuilder &Builder) { 00168 DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty), 00169 TI.isTypeSigned(Ty), Builder); 00170 } 00171 00172 static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty, 00173 MacroBuilder &Builder) { 00174 Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty)); 00175 } 00176 00177 static void DefineTypeWidth(StringRef MacroName, TargetInfo::IntType Ty, 00178 const TargetInfo &TI, MacroBuilder &Builder) { 00179 Builder.defineMacro(MacroName, Twine(TI.getTypeWidth(Ty))); 00180 } 00181 00182 static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth, 00183 const TargetInfo &TI, MacroBuilder &Builder) { 00184 Builder.defineMacro(MacroName, 00185 Twine(BitWidth / TI.getCharWidth())); 00186 } 00187 00188 static void DefineExactWidthIntType(TargetInfo::IntType Ty, 00189 const TargetInfo &TI, MacroBuilder &Builder) { 00190 int TypeWidth = TI.getTypeWidth(Ty); 00191 00192 // Use the target specified int64 type, when appropriate, so that [u]int64_t 00193 // ends up being defined in terms of the correct type. 00194 if (TypeWidth == 64) 00195 Ty = TI.getInt64Type(); 00196 00197 DefineType("__INT" + Twine(TypeWidth) + "_TYPE__", Ty, Builder); 00198 00199 StringRef ConstSuffix(TargetInfo::getTypeConstantSuffix(Ty)); 00200 if (!ConstSuffix.empty()) 00201 Builder.defineMacro("__INT" + Twine(TypeWidth) + "_C_SUFFIX__", 00202 ConstSuffix); 00203 } 00204 00205 /// Get the value the ATOMIC_*_LOCK_FREE macro should have for a type with 00206 /// the specified properties. 00207 static const char *getLockFreeValue(unsigned TypeWidth, unsigned TypeAlign, 00208 unsigned InlineWidth) { 00209 // Fully-aligned, power-of-2 sizes no larger than the inline 00210 // width will be inlined as lock-free operations. 00211 if (TypeWidth == TypeAlign && (TypeWidth & (TypeWidth - 1)) == 0 && 00212 TypeWidth <= InlineWidth) 00213 return "2"; // "always lock free" 00214 // We cannot be certain what operations the lib calls might be 00215 // able to implement as lock-free on future processors. 00216 return "1"; // "sometimes lock free" 00217 } 00218 00219 /// \brief Add definitions required for a smooth interaction between 00220 /// Objective-C++ automated reference counting and libstdc++ (4.2). 00221 static void AddObjCXXARCLibstdcxxDefines(const LangOptions &LangOpts, 00222 MacroBuilder &Builder) { 00223 Builder.defineMacro("_GLIBCXX_PREDEFINED_OBJC_ARC_IS_SCALAR"); 00224 00225 std::string Result; 00226 { 00227 // Provide specializations for the __is_scalar type trait so that 00228 // lifetime-qualified objects are not considered "scalar" types, which 00229 // libstdc++ uses as an indicator of the presence of trivial copy, assign, 00230 // default-construct, and destruct semantics (none of which hold for 00231 // lifetime-qualified objects in ARC). 00232 llvm::raw_string_ostream Out(Result); 00233 00234 Out << "namespace std {\n" 00235 << "\n" 00236 << "struct __true_type;\n" 00237 << "struct __false_type;\n" 00238 << "\n"; 00239 00240 Out << "template<typename _Tp> struct __is_scalar;\n" 00241 << "\n"; 00242 00243 Out << "template<typename _Tp>\n" 00244 << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> {\n" 00245 << " enum { __value = 0 };\n" 00246 << " typedef __false_type __type;\n" 00247 << "};\n" 00248 << "\n"; 00249 00250 if (LangOpts.ObjCRuntimeHasWeak) { 00251 Out << "template<typename _Tp>\n" 00252 << "struct __is_scalar<__attribute__((objc_ownership(weak))) _Tp> {\n" 00253 << " enum { __value = 0 };\n" 00254 << " typedef __false_type __type;\n" 00255 << "};\n" 00256 << "\n"; 00257 } 00258 00259 Out << "template<typename _Tp>\n" 00260 << "struct __is_scalar<__attribute__((objc_ownership(autoreleasing)))" 00261 << " _Tp> {\n" 00262 << " enum { __value = 0 };\n" 00263 << " typedef __false_type __type;\n" 00264 << "};\n" 00265 << "\n"; 00266 00267 Out << "}\n"; 00268 } 00269 Builder.append(Result); 00270 } 00271 00272 static void InitializeStandardPredefinedMacros(const TargetInfo &TI, 00273 const LangOptions &LangOpts, 00274 const FrontendOptions &FEOpts, 00275 MacroBuilder &Builder) { 00276 if (!LangOpts.MicrosoftMode && !LangOpts.TraditionalCPP) 00277 Builder.defineMacro("__STDC__"); 00278 if (LangOpts.Freestanding) 00279 Builder.defineMacro("__STDC_HOSTED__", "0"); 00280 else 00281 Builder.defineMacro("__STDC_HOSTED__"); 00282 00283 if (!LangOpts.CPlusPlus) { 00284 if (LangOpts.C11) 00285 Builder.defineMacro("__STDC_VERSION__", "201112L"); 00286 else if (LangOpts.C99) 00287 Builder.defineMacro("__STDC_VERSION__", "199901L"); 00288 else if (!LangOpts.GNUMode && LangOpts.Digraphs) 00289 Builder.defineMacro("__STDC_VERSION__", "199409L"); 00290 } else { 00291 // C++11 [cpp.predefined]p1: 00292 // The name __cplusplus is defined to the value 201103L when compiling a 00293 // C++ translation unit. 00294 if (LangOpts.CPlusPlus0x) 00295 Builder.defineMacro("__cplusplus", "201103L"); 00296 // C++03 [cpp.predefined]p1: 00297 // The name __cplusplus is defined to the value 199711L when compiling a 00298 // C++ translation unit. 00299 else 00300 Builder.defineMacro("__cplusplus", "199711L"); 00301 } 00302 00303 if (LangOpts.ObjC1) 00304 Builder.defineMacro("__OBJC__"); 00305 00306 // Not "standard" per se, but available even with the -undef flag. 00307 if (LangOpts.AsmPreprocessor) 00308 Builder.defineMacro("__ASSEMBLER__"); 00309 } 00310 00311 static void InitializePredefinedMacros(const TargetInfo &TI, 00312 const LangOptions &LangOpts, 00313 const FrontendOptions &FEOpts, 00314 MacroBuilder &Builder) { 00315 // Compiler version introspection macros. 00316 Builder.defineMacro("__llvm__"); // LLVM Backend 00317 Builder.defineMacro("__clang__"); // Clang Frontend 00318 #define TOSTR2(X) #X 00319 #define TOSTR(X) TOSTR2(X) 00320 Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR)); 00321 Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR)); 00322 #ifdef CLANG_VERSION_PATCHLEVEL 00323 Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL)); 00324 #else 00325 Builder.defineMacro("__clang_patchlevel__", "0"); 00326 #endif 00327 Builder.defineMacro("__clang_version__", 00328 "\"" CLANG_VERSION_STRING " (" 00329 + getClangFullRepositoryVersion() + ")\""); 00330 #undef TOSTR 00331 #undef TOSTR2 00332 if (!LangOpts.MicrosoftMode) { 00333 // Currently claim to be compatible with GCC 4.2.1-5621, but only if we're 00334 // not compiling for MSVC compatibility 00335 Builder.defineMacro("__GNUC_MINOR__", "2"); 00336 Builder.defineMacro("__GNUC_PATCHLEVEL__", "1"); 00337 Builder.defineMacro("__GNUC__", "4"); 00338 Builder.defineMacro("__GXX_ABI_VERSION", "1002"); 00339 } 00340 00341 // Define macros for the C11 / C++11 memory orderings 00342 Builder.defineMacro("__ATOMIC_RELAXED", "0"); 00343 Builder.defineMacro("__ATOMIC_CONSUME", "1"); 00344 Builder.defineMacro("__ATOMIC_ACQUIRE", "2"); 00345 Builder.defineMacro("__ATOMIC_RELEASE", "3"); 00346 Builder.defineMacro("__ATOMIC_ACQ_REL", "4"); 00347 Builder.defineMacro("__ATOMIC_SEQ_CST", "5"); 00348 00349 // Support for #pragma redefine_extname (Sun compatibility) 00350 Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1"); 00351 00352 // As sad as it is, enough software depends on the __VERSION__ for version 00353 // checks that it is necessary to report 4.2.1 (the base GCC version we claim 00354 // compatibility with) first. 00355 Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible " + 00356 Twine(getClangFullCPPVersion()) + "\""); 00357 00358 // Initialize language-specific preprocessor defines. 00359 00360 // Standard conforming mode? 00361 if (!LangOpts.GNUMode) 00362 Builder.defineMacro("__STRICT_ANSI__"); 00363 00364 if (LangOpts.CPlusPlus0x) 00365 Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__"); 00366 00367 if (LangOpts.ObjC1) { 00368 if (LangOpts.ObjCNonFragileABI) { 00369 Builder.defineMacro("__OBJC2__"); 00370 00371 if (LangOpts.ObjCExceptions) 00372 Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS"); 00373 } 00374 00375 if (LangOpts.getGC() != LangOptions::NonGC) 00376 Builder.defineMacro("__OBJC_GC__"); 00377 00378 if (LangOpts.NeXTRuntime) 00379 Builder.defineMacro("__NEXT_RUNTIME__"); 00380 } 00381 00382 // darwin_constant_cfstrings controls this. This is also dependent 00383 // on other things like the runtime I believe. This is set even for C code. 00384 if (!LangOpts.NoConstantCFStrings) 00385 Builder.defineMacro("__CONSTANT_CFSTRINGS__"); 00386 00387 if (LangOpts.ObjC2) 00388 Builder.defineMacro("OBJC_NEW_PROPERTIES"); 00389 00390 if (LangOpts.PascalStrings) 00391 Builder.defineMacro("__PASCAL_STRINGS__"); 00392 00393 if (LangOpts.Blocks) { 00394 Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))"); 00395 Builder.defineMacro("__BLOCKS__"); 00396 } 00397 00398 if (LangOpts.CXXExceptions) 00399 Builder.defineMacro("__EXCEPTIONS"); 00400 if (LangOpts.RTTI) 00401 Builder.defineMacro("__GXX_RTTI"); 00402 if (LangOpts.SjLjExceptions) 00403 Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__"); 00404 00405 if (LangOpts.Deprecated) 00406 Builder.defineMacro("__DEPRECATED"); 00407 00408 if (LangOpts.CPlusPlus) { 00409 Builder.defineMacro("__GNUG__", "4"); 00410 Builder.defineMacro("__GXX_WEAK__"); 00411 Builder.defineMacro("__private_extern__", "extern"); 00412 } 00413 00414 if (LangOpts.MicrosoftExt) { 00415 // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however 00416 // VC++ appears to only like __FUNCTION__. 00417 Builder.defineMacro("__PRETTY_FUNCTION__", "__FUNCTION__"); 00418 // Work around some issues with Visual C++ headerws. 00419 if (LangOpts.CPlusPlus) { 00420 // Since we define wchar_t in C++ mode. 00421 Builder.defineMacro("_WCHAR_T_DEFINED"); 00422 Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED"); 00423 // FIXME: Support Microsoft's __identifier extension in the lexer. 00424 Builder.append("#define __identifier(x) x"); 00425 Builder.append("class type_info;"); 00426 } 00427 00428 if (LangOpts.CPlusPlus0x) { 00429 Builder.defineMacro("_HAS_CHAR16_T_LANGUAGE_SUPPORT", "1"); 00430 } 00431 } 00432 00433 if (LangOpts.Optimize) 00434 Builder.defineMacro("__OPTIMIZE__"); 00435 if (LangOpts.OptimizeSize) 00436 Builder.defineMacro("__OPTIMIZE_SIZE__"); 00437 00438 if (LangOpts.FastMath) 00439 Builder.defineMacro("__FAST_MATH__"); 00440 00441 // Initialize target-specific preprocessor defines. 00442 00443 // Define type sizing macros based on the target properties. 00444 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far"); 00445 Builder.defineMacro("__CHAR_BIT__", "8"); 00446 00447 DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Builder); 00448 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder); 00449 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder); 00450 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder); 00451 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder); 00452 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder); 00453 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder); 00454 00455 DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder); 00456 DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder); 00457 DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder); 00458 DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder); 00459 DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder); 00460 DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder); 00461 DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder); 00462 DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder); 00463 DefineTypeSizeof("__SIZEOF_PTRDIFF_T__", 00464 TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder); 00465 DefineTypeSizeof("__SIZEOF_SIZE_T__", 00466 TI.getTypeWidth(TI.getSizeType()), TI, Builder); 00467 DefineTypeSizeof("__SIZEOF_WCHAR_T__", 00468 TI.getTypeWidth(TI.getWCharType()), TI, Builder); 00469 DefineTypeSizeof("__SIZEOF_WINT_T__", 00470 TI.getTypeWidth(TI.getWIntType()), TI, Builder); 00471 00472 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder); 00473 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder); 00474 DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder); 00475 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder); 00476 DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder); 00477 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder); 00478 DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder); 00479 DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder); 00480 DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder); 00481 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder); 00482 DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder); 00483 DefineType("__WINT_TYPE__", TI.getWIntType(), Builder); 00484 DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder); 00485 DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder); 00486 DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder); 00487 DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder); 00488 00489 DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat()); 00490 DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat()); 00491 DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat()); 00492 00493 // Define a __POINTER_WIDTH__ macro for stdint.h. 00494 Builder.defineMacro("__POINTER_WIDTH__", 00495 Twine((int)TI.getPointerWidth(0))); 00496 00497 if (!LangOpts.CharIsSigned) 00498 Builder.defineMacro("__CHAR_UNSIGNED__"); 00499 00500 if (!TargetInfo::isTypeSigned(TI.getWCharType())) 00501 Builder.defineMacro("__WCHAR_UNSIGNED__"); 00502 00503 if (!TargetInfo::isTypeSigned(TI.getWIntType())) 00504 Builder.defineMacro("__WINT_UNSIGNED__"); 00505 00506 // Define exact-width integer types for stdint.h 00507 Builder.defineMacro("__INT" + Twine(TI.getCharWidth()) + "_TYPE__", 00508 "char"); 00509 00510 if (TI.getShortWidth() > TI.getCharWidth()) 00511 DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder); 00512 00513 if (TI.getIntWidth() > TI.getShortWidth()) 00514 DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder); 00515 00516 if (TI.getLongWidth() > TI.getIntWidth()) 00517 DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder); 00518 00519 if (TI.getLongLongWidth() > TI.getLongWidth()) 00520 DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder); 00521 00522 // Add __builtin_va_list typedef. 00523 Builder.append(TI.getVAListDeclaration()); 00524 00525 if (const char *Prefix = TI.getUserLabelPrefix()) 00526 Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix); 00527 00528 // Build configuration options. FIXME: these should be controlled by 00529 // command line options or something. 00530 Builder.defineMacro("__FINITE_MATH_ONLY__", "0"); 00531 00532 if (LangOpts.GNUInline) 00533 Builder.defineMacro("__GNUC_GNU_INLINE__"); 00534 else 00535 Builder.defineMacro("__GNUC_STDC_INLINE__"); 00536 00537 // The value written by __atomic_test_and_set. 00538 // FIXME: This is target-dependent. 00539 Builder.defineMacro("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1"); 00540 00541 // Used by libstdc++ to implement ATOMIC_<foo>_LOCK_FREE. 00542 unsigned InlineWidthBits = TI.getMaxAtomicInlineWidth(); 00543 #define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \ 00544 Builder.defineMacro("__GCC_ATOMIC_" #TYPE "_LOCK_FREE", \ 00545 getLockFreeValue(TI.get##Type##Width(), \ 00546 TI.get##Type##Align(), \ 00547 InlineWidthBits)); 00548 DEFINE_LOCK_FREE_MACRO(BOOL, Bool); 00549 DEFINE_LOCK_FREE_MACRO(CHAR, Char); 00550 DEFINE_LOCK_FREE_MACRO(CHAR16_T, Char16); 00551 DEFINE_LOCK_FREE_MACRO(CHAR32_T, Char32); 00552 DEFINE_LOCK_FREE_MACRO(WCHAR_T, WChar); 00553 DEFINE_LOCK_FREE_MACRO(SHORT, Short); 00554 DEFINE_LOCK_FREE_MACRO(INT, Int); 00555 DEFINE_LOCK_FREE_MACRO(LONG, Long); 00556 DEFINE_LOCK_FREE_MACRO(LLONG, LongLong); 00557 Builder.defineMacro("__GCC_ATOMIC_POINTER_LOCK_FREE", 00558 getLockFreeValue(TI.getPointerWidth(0), 00559 TI.getPointerAlign(0), 00560 InlineWidthBits)); 00561 #undef DEFINE_LOCK_FREE_MACRO 00562 00563 if (LangOpts.NoInlineDefine) 00564 Builder.defineMacro("__NO_INLINE__"); 00565 00566 if (unsigned PICLevel = LangOpts.PICLevel) { 00567 Builder.defineMacro("__PIC__", Twine(PICLevel)); 00568 Builder.defineMacro("__pic__", Twine(PICLevel)); 00569 } 00570 if (unsigned PIELevel = LangOpts.PIELevel) { 00571 Builder.defineMacro("__PIE__", Twine(PIELevel)); 00572 Builder.defineMacro("__pie__", Twine(PIELevel)); 00573 } 00574 00575 // Macros to control C99 numerics and <float.h> 00576 Builder.defineMacro("__FLT_EVAL_METHOD__", Twine(TI.getFloatEvalMethod())); 00577 Builder.defineMacro("__FLT_RADIX__", "2"); 00578 int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36); 00579 Builder.defineMacro("__DECIMAL_DIG__", Twine(Dig)); 00580 00581 if (LangOpts.getStackProtector() == LangOptions::SSPOn) 00582 Builder.defineMacro("__SSP__"); 00583 else if (LangOpts.getStackProtector() == LangOptions::SSPReq) 00584 Builder.defineMacro("__SSP_ALL__", "2"); 00585 00586 if (FEOpts.ProgramAction == frontend::RewriteObjC) 00587 Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))"); 00588 00589 // Define a macro that exists only when using the static analyzer. 00590 if (FEOpts.ProgramAction == frontend::RunAnalysis) 00591 Builder.defineMacro("__clang_analyzer__"); 00592 00593 if (LangOpts.FastRelaxedMath) 00594 Builder.defineMacro("__FAST_RELAXED_MATH__"); 00595 00596 if (LangOpts.ObjCAutoRefCount) { 00597 Builder.defineMacro("__weak", "__attribute__((objc_ownership(weak)))"); 00598 Builder.defineMacro("__strong", "__attribute__((objc_ownership(strong)))"); 00599 Builder.defineMacro("__autoreleasing", 00600 "__attribute__((objc_ownership(autoreleasing)))"); 00601 Builder.defineMacro("__unsafe_unretained", 00602 "__attribute__((objc_ownership(none)))"); 00603 } 00604 00605 // Get other target #defines. 00606 TI.getTargetDefines(LangOpts, Builder); 00607 } 00608 00609 // Initialize the remapping of files to alternative contents, e.g., 00610 // those specified through other files. 00611 static void InitializeFileRemapping(DiagnosticsEngine &Diags, 00612 SourceManager &SourceMgr, 00613 FileManager &FileMgr, 00614 const PreprocessorOptions &InitOpts) { 00615 // Remap files in the source manager (with buffers). 00616 for (PreprocessorOptions::const_remapped_file_buffer_iterator 00617 Remap = InitOpts.remapped_file_buffer_begin(), 00618 RemapEnd = InitOpts.remapped_file_buffer_end(); 00619 Remap != RemapEnd; 00620 ++Remap) { 00621 // Create the file entry for the file that we're mapping from. 00622 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first, 00623 Remap->second->getBufferSize(), 00624 0); 00625 if (!FromFile) { 00626 Diags.Report(diag::err_fe_remap_missing_from_file) 00627 << Remap->first; 00628 if (!InitOpts.RetainRemappedFileBuffers) 00629 delete Remap->second; 00630 continue; 00631 } 00632 00633 // Override the contents of the "from" file with the contents of 00634 // the "to" file. 00635 SourceMgr.overrideFileContents(FromFile, Remap->second, 00636 InitOpts.RetainRemappedFileBuffers); 00637 } 00638 00639 // Remap files in the source manager (with other files). 00640 for (PreprocessorOptions::const_remapped_file_iterator 00641 Remap = InitOpts.remapped_file_begin(), 00642 RemapEnd = InitOpts.remapped_file_end(); 00643 Remap != RemapEnd; 00644 ++Remap) { 00645 // Find the file that we're mapping to. 00646 const FileEntry *ToFile = FileMgr.getFile(Remap->second); 00647 if (!ToFile) { 00648 Diags.Report(diag::err_fe_remap_missing_to_file) 00649 << Remap->first << Remap->second; 00650 continue; 00651 } 00652 00653 // Create the file entry for the file that we're mapping from. 00654 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first, 00655 ToFile->getSize(), 0); 00656 if (!FromFile) { 00657 Diags.Report(diag::err_fe_remap_missing_from_file) 00658 << Remap->first; 00659 continue; 00660 } 00661 00662 // Override the contents of the "from" file with the contents of 00663 // the "to" file. 00664 SourceMgr.overrideFileContents(FromFile, ToFile); 00665 } 00666 00667 SourceMgr.setOverridenFilesKeepOriginalName( 00668 InitOpts.RemappedFilesKeepOriginalName); 00669 } 00670 00671 /// InitializePreprocessor - Initialize the preprocessor getting it and the 00672 /// environment ready to process a single file. This returns true on error. 00673 /// 00674 void clang::InitializePreprocessor(Preprocessor &PP, 00675 const PreprocessorOptions &InitOpts, 00676 const HeaderSearchOptions &HSOpts, 00677 const FrontendOptions &FEOpts) { 00678 const LangOptions &LangOpts = PP.getLangOpts(); 00679 std::string PredefineBuffer; 00680 PredefineBuffer.reserve(4080); 00681 llvm::raw_string_ostream Predefines(PredefineBuffer); 00682 MacroBuilder Builder(Predefines); 00683 00684 InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(), 00685 PP.getFileManager(), InitOpts); 00686 00687 // Emit line markers for various builtin sections of the file. We don't do 00688 // this in asm preprocessor mode, because "# 4" is not a line marker directive 00689 // in this mode. 00690 if (!PP.getLangOpts().AsmPreprocessor) 00691 Builder.append("# 1 \"<built-in>\" 3"); 00692 00693 // Install things like __POWERPC__, __GNUC__, etc into the macro table. 00694 if (InitOpts.UsePredefines) { 00695 InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, Builder); 00696 00697 // Install definitions to make Objective-C++ ARC work well with various 00698 // C++ Standard Library implementations. 00699 if (LangOpts.ObjC1 && LangOpts.CPlusPlus && LangOpts.ObjCAutoRefCount) { 00700 switch (InitOpts.ObjCXXARCStandardLibrary) { 00701 case ARCXX_nolib: 00702 case ARCXX_libcxx: 00703 break; 00704 00705 case ARCXX_libstdcxx: 00706 AddObjCXXARCLibstdcxxDefines(LangOpts, Builder); 00707 break; 00708 } 00709 } 00710 } 00711 00712 // Even with predefines off, some macros are still predefined. 00713 // These should all be defined in the preprocessor according to the 00714 // current language configuration. 00715 InitializeStandardPredefinedMacros(PP.getTargetInfo(), PP.getLangOpts(), 00716 FEOpts, Builder); 00717 00718 // Add on the predefines from the driver. Wrap in a #line directive to report 00719 // that they come from the command line. 00720 if (!PP.getLangOpts().AsmPreprocessor) 00721 Builder.append("# 1 \"<command line>\" 1"); 00722 00723 // Process #define's and #undef's in the order they are given. 00724 for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) { 00725 if (InitOpts.Macros[i].second) // isUndef 00726 Builder.undefineMacro(InitOpts.Macros[i].first); 00727 else 00728 DefineBuiltinMacro(Builder, InitOpts.Macros[i].first, 00729 PP.getDiagnostics()); 00730 } 00731 00732 // If -imacros are specified, include them now. These are processed before 00733 // any -include directives. 00734 for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i) 00735 AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i], 00736 PP.getFileManager()); 00737 00738 // Process -include directives. 00739 for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) { 00740 const std::string &Path = InitOpts.Includes[i]; 00741 if (Path == InitOpts.ImplicitPTHInclude) 00742 AddImplicitIncludePTH(Builder, PP, Path); 00743 else 00744 AddImplicitInclude(Builder, Path, PP.getFileManager()); 00745 } 00746 00747 // Exit the command line and go back to <built-in> (2 is LC_LEAVE). 00748 if (!PP.getLangOpts().AsmPreprocessor) 00749 Builder.append("# 1 \"<built-in>\" 2"); 00750 00751 // Instruct the preprocessor to skip the preamble. 00752 PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first, 00753 InitOpts.PrecompiledPreambleBytes.second); 00754 00755 // Copy PredefinedBuffer into the Preprocessor. 00756 PP.setPredefines(Predefines.str()); 00757 00758 // Initialize the header search object. 00759 ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts, 00760 PP.getLangOpts(), 00761 PP.getTargetInfo().getTriple()); 00762 }