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/Frontend/Utils.h" 00015 #include "clang/Basic/FileManager.h" 00016 #include "clang/Basic/MacroBuilder.h" 00017 #include "clang/Basic/SourceManager.h" 00018 #include "clang/Basic/TargetInfo.h" 00019 #include "clang/Basic/Version.h" 00020 #include "clang/Frontend/FrontendDiagnostic.h" 00021 #include "clang/Frontend/FrontendOptions.h" 00022 #include "clang/Lex/HeaderSearch.h" 00023 #include "clang/Lex/Preprocessor.h" 00024 #include "clang/Lex/PreprocessorOptions.h" 00025 #include "clang/Serialization/ASTReader.h" 00026 #include "llvm/ADT/APFloat.h" 00027 #include "llvm/Support/FileSystem.h" 00028 #include "llvm/Support/MemoryBuffer.h" 00029 #include "llvm/Support/Path.h" 00030 using namespace clang; 00031 00032 static bool MacroBodyEndsInBackslash(StringRef MacroBody) { 00033 while (!MacroBody.empty() && isWhitespace(MacroBody.back())) 00034 MacroBody = MacroBody.drop_back(); 00035 return !MacroBody.empty() && MacroBody.back() == '\\'; 00036 } 00037 00038 // Append a #define line to Buf for Macro. Macro should be of the form XXX, 00039 // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit 00040 // "#define XXX Y z W". To get a #define with no value, use "XXX=". 00041 static void DefineBuiltinMacro(MacroBuilder &Builder, StringRef Macro, 00042 DiagnosticsEngine &Diags) { 00043 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 00044 StringRef MacroName = MacroPair.first; 00045 StringRef MacroBody = MacroPair.second; 00046 if (MacroName.size() != Macro.size()) { 00047 // Per GCC -D semantics, the macro ends at \n if it exists. 00048 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 00049 if (End != StringRef::npos) 00050 Diags.Report(diag::warn_fe_macro_contains_embedded_newline) 00051 << MacroName; 00052 MacroBody = MacroBody.substr(0, End); 00053 // We handle macro bodies which end in a backslash by appending an extra 00054 // backslash+newline. This makes sure we don't accidentally treat the 00055 // backslash as a line continuation marker. 00056 if (MacroBodyEndsInBackslash(MacroBody)) 00057 Builder.defineMacro(MacroName, Twine(MacroBody) + "\\\n"); 00058 else 00059 Builder.defineMacro(MacroName, MacroBody); 00060 } else { 00061 // Push "macroname 1". 00062 Builder.defineMacro(Macro); 00063 } 00064 } 00065 00066 /// AddImplicitInclude - Add an implicit \#include of the specified file to the 00067 /// predefines buffer. 00068 /// As these includes are generated by -include arguments the header search 00069 /// logic is going to search relatively to the current working directory. 00070 static void AddImplicitInclude(MacroBuilder &Builder, StringRef File) { 00071 Builder.append(Twine("#include \"") + File + "\""); 00072 } 00073 00074 static void AddImplicitIncludeMacros(MacroBuilder &Builder, StringRef File) { 00075 Builder.append(Twine("#__include_macros \"") + File + "\""); 00076 // Marker token to stop the __include_macros fetch loop. 00077 Builder.append("##"); // ##? 00078 } 00079 00080 /// AddImplicitIncludePTH - Add an implicit \#include using the original file 00081 /// used to generate a PTH cache. 00082 static void AddImplicitIncludePTH(MacroBuilder &Builder, Preprocessor &PP, 00083 StringRef ImplicitIncludePTH) { 00084 PTHManager *P = PP.getPTHManager(); 00085 // Null check 'P' in the corner case where it couldn't be created. 00086 const char *OriginalFile = P ? P->getOriginalSourceFile() : nullptr; 00087 00088 if (!OriginalFile) { 00089 PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header) 00090 << ImplicitIncludePTH; 00091 return; 00092 } 00093 00094 AddImplicitInclude(Builder, OriginalFile); 00095 } 00096 00097 /// \brief Add an implicit \#include using the original file used to generate 00098 /// a PCH file. 00099 static void AddImplicitIncludePCH(MacroBuilder &Builder, Preprocessor &PP, 00100 StringRef ImplicitIncludePCH) { 00101 std::string OriginalFile = 00102 ASTReader::getOriginalSourceFile(ImplicitIncludePCH, PP.getFileManager(), 00103 PP.getDiagnostics()); 00104 if (OriginalFile.empty()) 00105 return; 00106 00107 AddImplicitInclude(Builder, OriginalFile); 00108 } 00109 00110 /// PickFP - This is used to pick a value based on the FP semantics of the 00111 /// specified FP model. 00112 template <typename T> 00113 static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal, 00114 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal, 00115 T IEEEQuadVal) { 00116 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle) 00117 return IEEESingleVal; 00118 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble) 00119 return IEEEDoubleVal; 00120 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended) 00121 return X87DoubleExtendedVal; 00122 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble) 00123 return PPCDoubleDoubleVal; 00124 assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad); 00125 return IEEEQuadVal; 00126 } 00127 00128 static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix, 00129 const llvm::fltSemantics *Sem, StringRef Ext) { 00130 const char *DenormMin, *Epsilon, *Max, *Min; 00131 DenormMin = PickFP(Sem, "1.40129846e-45", "4.9406564584124654e-324", 00132 "3.64519953188247460253e-4951", 00133 "4.94065645841246544176568792868221e-324", 00134 "6.47517511943802511092443895822764655e-4966"); 00135 int Digits = PickFP(Sem, 6, 15, 18, 31, 33); 00136 Epsilon = PickFP(Sem, "1.19209290e-7", "2.2204460492503131e-16", 00137 "1.08420217248550443401e-19", 00138 "4.94065645841246544176568792868221e-324", 00139 "1.92592994438723585305597794258492732e-34"); 00140 int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113); 00141 int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931); 00142 int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932); 00143 int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381); 00144 int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384); 00145 Min = PickFP(Sem, "1.17549435e-38", "2.2250738585072014e-308", 00146 "3.36210314311209350626e-4932", 00147 "2.00416836000897277799610805135016e-292", 00148 "3.36210314311209350626267781732175260e-4932"); 00149 Max = PickFP(Sem, "3.40282347e+38", "1.7976931348623157e+308", 00150 "1.18973149535723176502e+4932", 00151 "1.79769313486231580793728971405301e+308", 00152 "1.18973149535723176508575932662800702e+4932"); 00153 00154 SmallString<32> DefPrefix; 00155 DefPrefix = "__"; 00156 DefPrefix += Prefix; 00157 DefPrefix += "_"; 00158 00159 Builder.defineMacro(DefPrefix + "DENORM_MIN__", Twine(DenormMin)+Ext); 00160 Builder.defineMacro(DefPrefix + "HAS_DENORM__"); 00161 Builder.defineMacro(DefPrefix + "DIG__", Twine(Digits)); 00162 Builder.defineMacro(DefPrefix + "EPSILON__", Twine(Epsilon)+Ext); 00163 Builder.defineMacro(DefPrefix + "HAS_INFINITY__"); 00164 Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__"); 00165 Builder.defineMacro(DefPrefix + "MANT_DIG__", Twine(MantissaDigits)); 00166 00167 Builder.defineMacro(DefPrefix + "MAX_10_EXP__", Twine(Max10Exp)); 00168 Builder.defineMacro(DefPrefix + "MAX_EXP__", Twine(MaxExp)); 00169 Builder.defineMacro(DefPrefix + "MAX__", Twine(Max)+Ext); 00170 00171 Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+Twine(Min10Exp)+")"); 00172 Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+Twine(MinExp)+")"); 00173 Builder.defineMacro(DefPrefix + "MIN__", Twine(Min)+Ext); 00174 } 00175 00176 00177 /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro 00178 /// named MacroName with the max value for a type with width 'TypeWidth' a 00179 /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL). 00180 static void DefineTypeSize(const Twine &MacroName, unsigned TypeWidth, 00181 StringRef ValSuffix, bool isSigned, 00182 MacroBuilder &Builder) { 00183 llvm::APInt MaxVal = isSigned ? llvm::APInt::getSignedMaxValue(TypeWidth) 00184 : llvm::APInt::getMaxValue(TypeWidth); 00185 Builder.defineMacro(MacroName, MaxVal.toString(10, isSigned) + ValSuffix); 00186 } 00187 00188 /// DefineTypeSize - An overloaded helper that uses TargetInfo to determine 00189 /// the width, suffix, and signedness of the given type 00190 static void DefineTypeSize(const Twine &MacroName, TargetInfo::IntType Ty, 00191 const TargetInfo &TI, MacroBuilder &Builder) { 00192 DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty), 00193 TI.isTypeSigned(Ty), Builder); 00194 } 00195 00196 static void DefineFmt(const Twine &Prefix, TargetInfo::IntType Ty, 00197 const TargetInfo &TI, MacroBuilder &Builder) { 00198 bool IsSigned = TI.isTypeSigned(Ty); 00199 StringRef FmtModifier = TI.getTypeFormatModifier(Ty); 00200 for (const char *Fmt = IsSigned ? "di" : "ouxX"; *Fmt; ++Fmt) { 00201 Builder.defineMacro(Prefix + "_FMT" + Twine(*Fmt) + "__", 00202 Twine("\"") + FmtModifier + Twine(*Fmt) + "\""); 00203 } 00204 } 00205 00206 static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty, 00207 MacroBuilder &Builder) { 00208 Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty)); 00209 } 00210 00211 static void DefineTypeWidth(StringRef MacroName, TargetInfo::IntType Ty, 00212 const TargetInfo &TI, MacroBuilder &Builder) { 00213 Builder.defineMacro(MacroName, Twine(TI.getTypeWidth(Ty))); 00214 } 00215 00216 static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth, 00217 const TargetInfo &TI, MacroBuilder &Builder) { 00218 Builder.defineMacro(MacroName, 00219 Twine(BitWidth / TI.getCharWidth())); 00220 } 00221 00222 static void DefineExactWidthIntType(TargetInfo::IntType Ty, 00223 const TargetInfo &TI, 00224 MacroBuilder &Builder) { 00225 int TypeWidth = TI.getTypeWidth(Ty); 00226 bool IsSigned = TI.isTypeSigned(Ty); 00227 00228 // Use the target specified int64 type, when appropriate, so that [u]int64_t 00229 // ends up being defined in terms of the correct type. 00230 if (TypeWidth == 64) 00231 Ty = IsSigned ? TI.getInt64Type() : TI.getUInt64Type(); 00232 00233 const char *Prefix = IsSigned ? "__INT" : "__UINT"; 00234 00235 DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder); 00236 DefineFmt(Prefix + Twine(TypeWidth), Ty, TI, Builder); 00237 00238 StringRef ConstSuffix(TI.getTypeConstantSuffix(Ty)); 00239 Builder.defineMacro(Prefix + Twine(TypeWidth) + "_C_SUFFIX__", ConstSuffix); 00240 } 00241 00242 static void DefineExactWidthIntTypeSize(TargetInfo::IntType Ty, 00243 const TargetInfo &TI, 00244 MacroBuilder &Builder) { 00245 int TypeWidth = TI.getTypeWidth(Ty); 00246 bool IsSigned = TI.isTypeSigned(Ty); 00247 00248 // Use the target specified int64 type, when appropriate, so that [u]int64_t 00249 // ends up being defined in terms of the correct type. 00250 if (TypeWidth == 64) 00251 Ty = IsSigned ? TI.getInt64Type() : TI.getUInt64Type(); 00252 00253 const char *Prefix = IsSigned ? "__INT" : "__UINT"; 00254 DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder); 00255 } 00256 00257 static void DefineLeastWidthIntType(unsigned TypeWidth, bool IsSigned, 00258 const TargetInfo &TI, 00259 MacroBuilder &Builder) { 00260 TargetInfo::IntType Ty = TI.getLeastIntTypeByWidth(TypeWidth, IsSigned); 00261 if (Ty == TargetInfo::NoInt) 00262 return; 00263 00264 const char *Prefix = IsSigned ? "__INT_LEAST" : "__UINT_LEAST"; 00265 DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder); 00266 DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder); 00267 DefineFmt(Prefix + Twine(TypeWidth), Ty, TI, Builder); 00268 } 00269 00270 static void DefineFastIntType(unsigned TypeWidth, bool IsSigned, 00271 const TargetInfo &TI, MacroBuilder &Builder) { 00272 // stdint.h currently defines the fast int types as equivalent to the least 00273 // types. 00274 TargetInfo::IntType Ty = TI.getLeastIntTypeByWidth(TypeWidth, IsSigned); 00275 if (Ty == TargetInfo::NoInt) 00276 return; 00277 00278 const char *Prefix = IsSigned ? "__INT_FAST" : "__UINT_FAST"; 00279 DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder); 00280 DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder); 00281 00282 DefineFmt(Prefix + Twine(TypeWidth), Ty, TI, Builder); 00283 } 00284 00285 00286 /// Get the value the ATOMIC_*_LOCK_FREE macro should have for a type with 00287 /// the specified properties. 00288 static const char *getLockFreeValue(unsigned TypeWidth, unsigned TypeAlign, 00289 unsigned InlineWidth) { 00290 // Fully-aligned, power-of-2 sizes no larger than the inline 00291 // width will be inlined as lock-free operations. 00292 if (TypeWidth == TypeAlign && (TypeWidth & (TypeWidth - 1)) == 0 && 00293 TypeWidth <= InlineWidth) 00294 return "2"; // "always lock free" 00295 // We cannot be certain what operations the lib calls might be 00296 // able to implement as lock-free on future processors. 00297 return "1"; // "sometimes lock free" 00298 } 00299 00300 /// \brief Add definitions required for a smooth interaction between 00301 /// Objective-C++ automated reference counting and libstdc++ (4.2). 00302 static void AddObjCXXARCLibstdcxxDefines(const LangOptions &LangOpts, 00303 MacroBuilder &Builder) { 00304 Builder.defineMacro("_GLIBCXX_PREDEFINED_OBJC_ARC_IS_SCALAR"); 00305 00306 std::string Result; 00307 { 00308 // Provide specializations for the __is_scalar type trait so that 00309 // lifetime-qualified objects are not considered "scalar" types, which 00310 // libstdc++ uses as an indicator of the presence of trivial copy, assign, 00311 // default-construct, and destruct semantics (none of which hold for 00312 // lifetime-qualified objects in ARC). 00313 llvm::raw_string_ostream Out(Result); 00314 00315 Out << "namespace std {\n" 00316 << "\n" 00317 << "struct __true_type;\n" 00318 << "struct __false_type;\n" 00319 << "\n"; 00320 00321 Out << "template<typename _Tp> struct __is_scalar;\n" 00322 << "\n"; 00323 00324 Out << "template<typename _Tp>\n" 00325 << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> {\n" 00326 << " enum { __value = 0 };\n" 00327 << " typedef __false_type __type;\n" 00328 << "};\n" 00329 << "\n"; 00330 00331 if (LangOpts.ObjCARCWeak) { 00332 Out << "template<typename _Tp>\n" 00333 << "struct __is_scalar<__attribute__((objc_ownership(weak))) _Tp> {\n" 00334 << " enum { __value = 0 };\n" 00335 << " typedef __false_type __type;\n" 00336 << "};\n" 00337 << "\n"; 00338 } 00339 00340 Out << "template<typename _Tp>\n" 00341 << "struct __is_scalar<__attribute__((objc_ownership(autoreleasing)))" 00342 << " _Tp> {\n" 00343 << " enum { __value = 0 };\n" 00344 << " typedef __false_type __type;\n" 00345 << "};\n" 00346 << "\n"; 00347 00348 Out << "}\n"; 00349 } 00350 Builder.append(Result); 00351 } 00352 00353 static void InitializeStandardPredefinedMacros(const TargetInfo &TI, 00354 const LangOptions &LangOpts, 00355 const FrontendOptions &FEOpts, 00356 MacroBuilder &Builder) { 00357 if (!LangOpts.MSVCCompat && !LangOpts.TraditionalCPP) 00358 Builder.defineMacro("__STDC__"); 00359 if (LangOpts.Freestanding) 00360 Builder.defineMacro("__STDC_HOSTED__", "0"); 00361 else 00362 Builder.defineMacro("__STDC_HOSTED__"); 00363 00364 if (!LangOpts.CPlusPlus) { 00365 if (LangOpts.C11) 00366 Builder.defineMacro("__STDC_VERSION__", "201112L"); 00367 else if (LangOpts.C99) 00368 Builder.defineMacro("__STDC_VERSION__", "199901L"); 00369 else if (!LangOpts.GNUMode && LangOpts.Digraphs) 00370 Builder.defineMacro("__STDC_VERSION__", "199409L"); 00371 } else { 00372 // FIXME: Use correct value for C++17. 00373 if (LangOpts.CPlusPlus1z) 00374 Builder.defineMacro("__cplusplus", "201406L"); 00375 // C++1y [cpp.predefined]p1: 00376 // The name __cplusplus is defined to the value 201402L when compiling a 00377 // C++ translation unit. 00378 else if (LangOpts.CPlusPlus14) 00379 Builder.defineMacro("__cplusplus", "201402L"); 00380 // C++11 [cpp.predefined]p1: 00381 // The name __cplusplus is defined to the value 201103L when compiling a 00382 // C++ translation unit. 00383 else if (LangOpts.CPlusPlus11) 00384 Builder.defineMacro("__cplusplus", "201103L"); 00385 // C++03 [cpp.predefined]p1: 00386 // The name __cplusplus is defined to the value 199711L when compiling a 00387 // C++ translation unit. 00388 else 00389 Builder.defineMacro("__cplusplus", "199711L"); 00390 } 00391 00392 // In C11 these are environment macros. In C++11 they are only defined 00393 // as part of <cuchar>. To prevent breakage when mixing C and C++ 00394 // code, define these macros unconditionally. We can define them 00395 // unconditionally, as Clang always uses UTF-16 and UTF-32 for 16-bit 00396 // and 32-bit character literals. 00397 Builder.defineMacro("__STDC_UTF_16__", "1"); 00398 Builder.defineMacro("__STDC_UTF_32__", "1"); 00399 00400 if (LangOpts.ObjC1) 00401 Builder.defineMacro("__OBJC__"); 00402 00403 // Not "standard" per se, but available even with the -undef flag. 00404 if (LangOpts.AsmPreprocessor) 00405 Builder.defineMacro("__ASSEMBLER__"); 00406 } 00407 00408 /// Initialize the predefined C++ language feature test macros defined in 00409 /// ISO/IEC JTC1/SC22/WG21 (C++) SD-6: "SG10 Feature Test Recommendations". 00410 static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts, 00411 MacroBuilder &Builder) { 00412 // C++98 features. 00413 if (LangOpts.RTTI) 00414 Builder.defineMacro("__cpp_rtti", "199711"); 00415 if (LangOpts.CXXExceptions) 00416 Builder.defineMacro("__cpp_exceptions", "199711"); 00417 00418 // C++11 features. 00419 if (LangOpts.CPlusPlus11) { 00420 Builder.defineMacro("__cpp_unicode_characters", "200704"); 00421 Builder.defineMacro("__cpp_raw_strings", "200710"); 00422 Builder.defineMacro("__cpp_unicode_literals", "200710"); 00423 Builder.defineMacro("__cpp_user_defined_literals", "200809"); 00424 Builder.defineMacro("__cpp_lambdas", "200907"); 00425 Builder.defineMacro("__cpp_constexpr", 00426 LangOpts.CPlusPlus14 ? "201304" : "200704"); 00427 Builder.defineMacro("__cpp_range_based_for", "200907"); 00428 Builder.defineMacro("__cpp_static_assert", "200410"); 00429 Builder.defineMacro("__cpp_decltype", "200707"); 00430 Builder.defineMacro("__cpp_attributes", "200809"); 00431 Builder.defineMacro("__cpp_rvalue_references", "200610"); 00432 Builder.defineMacro("__cpp_variadic_templates", "200704"); 00433 Builder.defineMacro("__cpp_initializer_lists", "200806"); 00434 Builder.defineMacro("__cpp_delegating_constructors", "200604"); 00435 Builder.defineMacro("__cpp_nsdmi", "200809"); 00436 Builder.defineMacro("__cpp_inheriting_constructors", "200802"); 00437 Builder.defineMacro("__cpp_ref_qualifiers", "200710"); 00438 Builder.defineMacro("__cpp_alias_templates", "200704"); 00439 } 00440 00441 // C++14 features. 00442 if (LangOpts.CPlusPlus14) { 00443 Builder.defineMacro("__cpp_binary_literals", "201304"); 00444 Builder.defineMacro("__cpp_digit_separators", "201309"); 00445 Builder.defineMacro("__cpp_init_captures", "201304"); 00446 Builder.defineMacro("__cpp_generic_lambdas", "201304"); 00447 Builder.defineMacro("__cpp_decltype_auto", "201304"); 00448 Builder.defineMacro("__cpp_return_type_deduction", "201304"); 00449 Builder.defineMacro("__cpp_aggregate_nsdmi", "201304"); 00450 Builder.defineMacro("__cpp_variable_templates", "201304"); 00451 } 00452 if (LangOpts.SizedDeallocation) 00453 Builder.defineMacro("__cpp_sized_deallocation", "201309"); 00454 } 00455 00456 static void InitializePredefinedMacros(const TargetInfo &TI, 00457 const LangOptions &LangOpts, 00458 const FrontendOptions &FEOpts, 00459 MacroBuilder &Builder) { 00460 // Compiler version introspection macros. 00461 Builder.defineMacro("__llvm__"); // LLVM Backend 00462 Builder.defineMacro("__clang__"); // Clang Frontend 00463 #define TOSTR2(X) #X 00464 #define TOSTR(X) TOSTR2(X) 00465 Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR)); 00466 Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR)); 00467 #ifdef CLANG_VERSION_PATCHLEVEL 00468 Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL)); 00469 #else 00470 Builder.defineMacro("__clang_patchlevel__", "0"); 00471 #endif 00472 Builder.defineMacro("__clang_version__", 00473 "\"" CLANG_VERSION_STRING " " 00474 + getClangFullRepositoryVersion() + "\""); 00475 #undef TOSTR 00476 #undef TOSTR2 00477 if (!LangOpts.MSVCCompat) { 00478 // Currently claim to be compatible with GCC 4.2.1-5621, but only if we're 00479 // not compiling for MSVC compatibility 00480 Builder.defineMacro("__GNUC_MINOR__", "2"); 00481 Builder.defineMacro("__GNUC_PATCHLEVEL__", "1"); 00482 Builder.defineMacro("__GNUC__", "4"); 00483 Builder.defineMacro("__GXX_ABI_VERSION", "1002"); 00484 } 00485 00486 // Define macros for the C11 / C++11 memory orderings 00487 Builder.defineMacro("__ATOMIC_RELAXED", "0"); 00488 Builder.defineMacro("__ATOMIC_CONSUME", "1"); 00489 Builder.defineMacro("__ATOMIC_ACQUIRE", "2"); 00490 Builder.defineMacro("__ATOMIC_RELEASE", "3"); 00491 Builder.defineMacro("__ATOMIC_ACQ_REL", "4"); 00492 Builder.defineMacro("__ATOMIC_SEQ_CST", "5"); 00493 00494 // Support for #pragma redefine_extname (Sun compatibility) 00495 Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1"); 00496 00497 // As sad as it is, enough software depends on the __VERSION__ for version 00498 // checks that it is necessary to report 4.2.1 (the base GCC version we claim 00499 // compatibility with) first. 00500 Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible " + 00501 Twine(getClangFullCPPVersion()) + "\""); 00502 00503 // Initialize language-specific preprocessor defines. 00504 00505 // Standard conforming mode? 00506 if (!LangOpts.GNUMode && !LangOpts.MSVCCompat) 00507 Builder.defineMacro("__STRICT_ANSI__"); 00508 00509 if (!LangOpts.MSVCCompat && LangOpts.CPlusPlus11) 00510 Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__"); 00511 00512 if (LangOpts.ObjC1) { 00513 if (LangOpts.ObjCRuntime.isNonFragile()) { 00514 Builder.defineMacro("__OBJC2__"); 00515 00516 if (LangOpts.ObjCExceptions) 00517 Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS"); 00518 } 00519 00520 if (LangOpts.getGC() != LangOptions::NonGC) 00521 Builder.defineMacro("__OBJC_GC__"); 00522 00523 if (LangOpts.ObjCRuntime.isNeXTFamily()) 00524 Builder.defineMacro("__NEXT_RUNTIME__"); 00525 00526 if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::ObjFW) { 00527 VersionTuple tuple = LangOpts.ObjCRuntime.getVersion(); 00528 00529 unsigned minor = 0; 00530 if (tuple.getMinor().hasValue()) 00531 minor = tuple.getMinor().getValue(); 00532 00533 unsigned subminor = 0; 00534 if (tuple.getSubminor().hasValue()) 00535 subminor = tuple.getSubminor().getValue(); 00536 00537 Builder.defineMacro("__OBJFW_RUNTIME_ABI__", 00538 Twine(tuple.getMajor() * 10000 + minor * 100 + 00539 subminor)); 00540 } 00541 00542 Builder.defineMacro("IBOutlet", "__attribute__((iboutlet))"); 00543 Builder.defineMacro("IBOutletCollection(ClassName)", 00544 "__attribute__((iboutletcollection(ClassName)))"); 00545 Builder.defineMacro("IBAction", "void)__attribute__((ibaction)"); 00546 Builder.defineMacro("IBInspectable", ""); 00547 Builder.defineMacro("IB_DESIGNABLE", ""); 00548 } 00549 00550 if (LangOpts.CPlusPlus) 00551 InitializeCPlusPlusFeatureTestMacros(LangOpts, Builder); 00552 00553 // darwin_constant_cfstrings controls this. This is also dependent 00554 // on other things like the runtime I believe. This is set even for C code. 00555 if (!LangOpts.NoConstantCFStrings) 00556 Builder.defineMacro("__CONSTANT_CFSTRINGS__"); 00557 00558 if (LangOpts.ObjC2) 00559 Builder.defineMacro("OBJC_NEW_PROPERTIES"); 00560 00561 if (LangOpts.PascalStrings) 00562 Builder.defineMacro("__PASCAL_STRINGS__"); 00563 00564 if (LangOpts.Blocks) { 00565 Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))"); 00566 Builder.defineMacro("__BLOCKS__"); 00567 } 00568 00569 if (!LangOpts.MSVCCompat && LangOpts.Exceptions) 00570 Builder.defineMacro("__EXCEPTIONS"); 00571 if (!LangOpts.MSVCCompat && LangOpts.RTTI) 00572 Builder.defineMacro("__GXX_RTTI"); 00573 if (LangOpts.SjLjExceptions) 00574 Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__"); 00575 00576 if (LangOpts.Deprecated) 00577 Builder.defineMacro("__DEPRECATED"); 00578 00579 if (!LangOpts.MSVCCompat && LangOpts.CPlusPlus) { 00580 Builder.defineMacro("__GNUG__", "4"); 00581 Builder.defineMacro("__GXX_WEAK__"); 00582 Builder.defineMacro("__private_extern__", "extern"); 00583 } 00584 00585 if (LangOpts.MicrosoftExt) { 00586 if (LangOpts.WChar) { 00587 // wchar_t supported as a keyword. 00588 Builder.defineMacro("_WCHAR_T_DEFINED"); 00589 Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED"); 00590 } 00591 } 00592 00593 if (LangOpts.Optimize) 00594 Builder.defineMacro("__OPTIMIZE__"); 00595 if (LangOpts.OptimizeSize) 00596 Builder.defineMacro("__OPTIMIZE_SIZE__"); 00597 00598 if (LangOpts.FastMath) 00599 Builder.defineMacro("__FAST_MATH__"); 00600 00601 // Initialize target-specific preprocessor defines. 00602 00603 // __BYTE_ORDER__ was added in GCC 4.6. It's analogous 00604 // to the macro __BYTE_ORDER (no trailing underscores) 00605 // from glibc's <endian.h> header. 00606 // We don't support the PDP-11 as a target, but include 00607 // the define so it can still be compared against. 00608 Builder.defineMacro("__ORDER_LITTLE_ENDIAN__", "1234"); 00609 Builder.defineMacro("__ORDER_BIG_ENDIAN__", "4321"); 00610 Builder.defineMacro("__ORDER_PDP_ENDIAN__", "3412"); 00611 if (TI.isBigEndian()) { 00612 Builder.defineMacro("__BYTE_ORDER__", "__ORDER_BIG_ENDIAN__"); 00613 Builder.defineMacro("__BIG_ENDIAN__"); 00614 } else { 00615 Builder.defineMacro("__BYTE_ORDER__", "__ORDER_LITTLE_ENDIAN__"); 00616 Builder.defineMacro("__LITTLE_ENDIAN__"); 00617 } 00618 00619 if (TI.getPointerWidth(0) == 64 && TI.getLongWidth() == 64 00620 && TI.getIntWidth() == 32) { 00621 Builder.defineMacro("_LP64"); 00622 Builder.defineMacro("__LP64__"); 00623 } 00624 00625 if (TI.getPointerWidth(0) == 32 && TI.getLongWidth() == 32 00626 && TI.getIntWidth() == 32) { 00627 Builder.defineMacro("_ILP32"); 00628 Builder.defineMacro("__ILP32__"); 00629 } 00630 00631 // Define type sizing macros based on the target properties. 00632 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far"); 00633 Builder.defineMacro("__CHAR_BIT__", "8"); 00634 00635 DefineTypeSize("__SCHAR_MAX__", TargetInfo::SignedChar, TI, Builder); 00636 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder); 00637 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder); 00638 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder); 00639 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder); 00640 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder); 00641 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder); 00642 DefineTypeSize("__SIZE_MAX__", TI.getSizeType(), TI, Builder); 00643 00644 DefineTypeSize("__UINTMAX_MAX__", TI.getUIntMaxType(), TI, Builder); 00645 DefineTypeSize("__PTRDIFF_MAX__", TI.getPtrDiffType(0), TI, Builder); 00646 DefineTypeSize("__INTPTR_MAX__", TI.getIntPtrType(), TI, Builder); 00647 DefineTypeSize("__UINTPTR_MAX__", TI.getUIntPtrType(), TI, Builder); 00648 00649 DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder); 00650 DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder); 00651 DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder); 00652 DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder); 00653 DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder); 00654 DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder); 00655 DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder); 00656 DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder); 00657 DefineTypeSizeof("__SIZEOF_PTRDIFF_T__", 00658 TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder); 00659 DefineTypeSizeof("__SIZEOF_SIZE_T__", 00660 TI.getTypeWidth(TI.getSizeType()), TI, Builder); 00661 DefineTypeSizeof("__SIZEOF_WCHAR_T__", 00662 TI.getTypeWidth(TI.getWCharType()), TI, Builder); 00663 DefineTypeSizeof("__SIZEOF_WINT_T__", 00664 TI.getTypeWidth(TI.getWIntType()), TI, Builder); 00665 if (TI.hasInt128Type()) 00666 DefineTypeSizeof("__SIZEOF_INT128__", 128, TI, Builder); 00667 00668 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder); 00669 DefineFmt("__INTMAX", TI.getIntMaxType(), TI, Builder); 00670 Builder.defineMacro("__INTMAX_C_SUFFIX__", 00671 TI.getTypeConstantSuffix(TI.getIntMaxType())); 00672 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder); 00673 DefineFmt("__UINTMAX", TI.getUIntMaxType(), TI, Builder); 00674 Builder.defineMacro("__UINTMAX_C_SUFFIX__", 00675 TI.getTypeConstantSuffix(TI.getUIntMaxType())); 00676 DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder); 00677 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder); 00678 DefineFmt("__PTRDIFF", TI.getPtrDiffType(0), TI, Builder); 00679 DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder); 00680 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder); 00681 DefineFmt("__INTPTR", TI.getIntPtrType(), TI, Builder); 00682 DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder); 00683 DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder); 00684 DefineFmt("__SIZE", TI.getSizeType(), TI, Builder); 00685 DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder); 00686 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder); 00687 DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder); 00688 DefineType("__WINT_TYPE__", TI.getWIntType(), Builder); 00689 DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder); 00690 DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder); 00691 DefineTypeSize("__SIG_ATOMIC_MAX__", TI.getSigAtomicType(), TI, Builder); 00692 DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder); 00693 DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder); 00694 00695 DefineTypeWidth("__UINTMAX_WIDTH__", TI.getUIntMaxType(), TI, Builder); 00696 DefineType("__UINTPTR_TYPE__", TI.getUIntPtrType(), Builder); 00697 DefineFmt("__UINTPTR", TI.getUIntPtrType(), TI, Builder); 00698 DefineTypeWidth("__UINTPTR_WIDTH__", TI.getUIntPtrType(), TI, Builder); 00699 00700 DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat(), "F"); 00701 DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat(), ""); 00702 DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat(), "L"); 00703 00704 // Define a __POINTER_WIDTH__ macro for stdint.h. 00705 Builder.defineMacro("__POINTER_WIDTH__", 00706 Twine((int)TI.getPointerWidth(0))); 00707 00708 if (!LangOpts.CharIsSigned) 00709 Builder.defineMacro("__CHAR_UNSIGNED__"); 00710 00711 if (!TargetInfo::isTypeSigned(TI.getWCharType())) 00712 Builder.defineMacro("__WCHAR_UNSIGNED__"); 00713 00714 if (!TargetInfo::isTypeSigned(TI.getWIntType())) 00715 Builder.defineMacro("__WINT_UNSIGNED__"); 00716 00717 // Define exact-width integer types for stdint.h 00718 DefineExactWidthIntType(TargetInfo::SignedChar, TI, Builder); 00719 00720 if (TI.getShortWidth() > TI.getCharWidth()) 00721 DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder); 00722 00723 if (TI.getIntWidth() > TI.getShortWidth()) 00724 DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder); 00725 00726 if (TI.getLongWidth() > TI.getIntWidth()) 00727 DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder); 00728 00729 if (TI.getLongLongWidth() > TI.getLongWidth()) 00730 DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder); 00731 00732 DefineExactWidthIntType(TargetInfo::UnsignedChar, TI, Builder); 00733 DefineExactWidthIntTypeSize(TargetInfo::UnsignedChar, TI, Builder); 00734 DefineExactWidthIntTypeSize(TargetInfo::SignedChar, TI, Builder); 00735 00736 if (TI.getShortWidth() > TI.getCharWidth()) { 00737 DefineExactWidthIntType(TargetInfo::UnsignedShort, TI, Builder); 00738 DefineExactWidthIntTypeSize(TargetInfo::UnsignedShort, TI, Builder); 00739 DefineExactWidthIntTypeSize(TargetInfo::SignedShort, TI, Builder); 00740 } 00741 00742 if (TI.getIntWidth() > TI.getShortWidth()) { 00743 DefineExactWidthIntType(TargetInfo::UnsignedInt, TI, Builder); 00744 DefineExactWidthIntTypeSize(TargetInfo::UnsignedInt, TI, Builder); 00745 DefineExactWidthIntTypeSize(TargetInfo::SignedInt, TI, Builder); 00746 } 00747 00748 if (TI.getLongWidth() > TI.getIntWidth()) { 00749 DefineExactWidthIntType(TargetInfo::UnsignedLong, TI, Builder); 00750 DefineExactWidthIntTypeSize(TargetInfo::UnsignedLong, TI, Builder); 00751 DefineExactWidthIntTypeSize(TargetInfo::SignedLong, TI, Builder); 00752 } 00753 00754 if (TI.getLongLongWidth() > TI.getLongWidth()) { 00755 DefineExactWidthIntType(TargetInfo::UnsignedLongLong, TI, Builder); 00756 DefineExactWidthIntTypeSize(TargetInfo::UnsignedLongLong, TI, Builder); 00757 DefineExactWidthIntTypeSize(TargetInfo::SignedLongLong, TI, Builder); 00758 } 00759 00760 DefineLeastWidthIntType(8, true, TI, Builder); 00761 DefineLeastWidthIntType(8, false, TI, Builder); 00762 DefineLeastWidthIntType(16, true, TI, Builder); 00763 DefineLeastWidthIntType(16, false, TI, Builder); 00764 DefineLeastWidthIntType(32, true, TI, Builder); 00765 DefineLeastWidthIntType(32, false, TI, Builder); 00766 DefineLeastWidthIntType(64, true, TI, Builder); 00767 DefineLeastWidthIntType(64, false, TI, Builder); 00768 00769 DefineFastIntType(8, true, TI, Builder); 00770 DefineFastIntType(8, false, TI, Builder); 00771 DefineFastIntType(16, true, TI, Builder); 00772 DefineFastIntType(16, false, TI, Builder); 00773 DefineFastIntType(32, true, TI, Builder); 00774 DefineFastIntType(32, false, TI, Builder); 00775 DefineFastIntType(64, true, TI, Builder); 00776 DefineFastIntType(64, false, TI, Builder); 00777 00778 if (const char *Prefix = TI.getUserLabelPrefix()) 00779 Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix); 00780 00781 if (LangOpts.FastMath || LangOpts.FiniteMathOnly) 00782 Builder.defineMacro("__FINITE_MATH_ONLY__", "1"); 00783 else 00784 Builder.defineMacro("__FINITE_MATH_ONLY__", "0"); 00785 00786 if (!LangOpts.MSVCCompat) { 00787 if (LangOpts.GNUInline) 00788 Builder.defineMacro("__GNUC_GNU_INLINE__"); 00789 else 00790 Builder.defineMacro("__GNUC_STDC_INLINE__"); 00791 00792 // The value written by __atomic_test_and_set. 00793 // FIXME: This is target-dependent. 00794 Builder.defineMacro("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1"); 00795 00796 // Used by libstdc++ to implement ATOMIC_<foo>_LOCK_FREE. 00797 unsigned InlineWidthBits = TI.getMaxAtomicInlineWidth(); 00798 #define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \ 00799 Builder.defineMacro("__GCC_ATOMIC_" #TYPE "_LOCK_FREE", \ 00800 getLockFreeValue(TI.get##Type##Width(), \ 00801 TI.get##Type##Align(), \ 00802 InlineWidthBits)); 00803 DEFINE_LOCK_FREE_MACRO(BOOL, Bool); 00804 DEFINE_LOCK_FREE_MACRO(CHAR, Char); 00805 DEFINE_LOCK_FREE_MACRO(CHAR16_T, Char16); 00806 DEFINE_LOCK_FREE_MACRO(CHAR32_T, Char32); 00807 DEFINE_LOCK_FREE_MACRO(WCHAR_T, WChar); 00808 DEFINE_LOCK_FREE_MACRO(SHORT, Short); 00809 DEFINE_LOCK_FREE_MACRO(INT, Int); 00810 DEFINE_LOCK_FREE_MACRO(LONG, Long); 00811 DEFINE_LOCK_FREE_MACRO(LLONG, LongLong); 00812 Builder.defineMacro("__GCC_ATOMIC_POINTER_LOCK_FREE", 00813 getLockFreeValue(TI.getPointerWidth(0), 00814 TI.getPointerAlign(0), 00815 InlineWidthBits)); 00816 #undef DEFINE_LOCK_FREE_MACRO 00817 } 00818 00819 if (LangOpts.NoInlineDefine) 00820 Builder.defineMacro("__NO_INLINE__"); 00821 00822 if (unsigned PICLevel = LangOpts.PICLevel) { 00823 Builder.defineMacro("__PIC__", Twine(PICLevel)); 00824 Builder.defineMacro("__pic__", Twine(PICLevel)); 00825 } 00826 if (unsigned PIELevel = LangOpts.PIELevel) { 00827 Builder.defineMacro("__PIE__", Twine(PIELevel)); 00828 Builder.defineMacro("__pie__", Twine(PIELevel)); 00829 } 00830 00831 // Macros to control C99 numerics and <float.h> 00832 Builder.defineMacro("__FLT_EVAL_METHOD__", Twine(TI.getFloatEvalMethod())); 00833 Builder.defineMacro("__FLT_RADIX__", "2"); 00834 int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36); 00835 Builder.defineMacro("__DECIMAL_DIG__", Twine(Dig)); 00836 00837 if (LangOpts.getStackProtector() == LangOptions::SSPOn) 00838 Builder.defineMacro("__SSP__"); 00839 else if (LangOpts.getStackProtector() == LangOptions::SSPStrong) 00840 Builder.defineMacro("__SSP_STRONG__", "2"); 00841 else if (LangOpts.getStackProtector() == LangOptions::SSPReq) 00842 Builder.defineMacro("__SSP_ALL__", "3"); 00843 00844 if (FEOpts.ProgramAction == frontend::RewriteObjC) 00845 Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))"); 00846 00847 // Define a macro that exists only when using the static analyzer. 00848 if (FEOpts.ProgramAction == frontend::RunAnalysis) 00849 Builder.defineMacro("__clang_analyzer__"); 00850 00851 if (LangOpts.FastRelaxedMath) 00852 Builder.defineMacro("__FAST_RELAXED_MATH__"); 00853 00854 if (LangOpts.ObjCAutoRefCount) { 00855 Builder.defineMacro("__weak", "__attribute__((objc_ownership(weak)))"); 00856 Builder.defineMacro("__strong", "__attribute__((objc_ownership(strong)))"); 00857 Builder.defineMacro("__autoreleasing", 00858 "__attribute__((objc_ownership(autoreleasing)))"); 00859 Builder.defineMacro("__unsafe_unretained", 00860 "__attribute__((objc_ownership(none)))"); 00861 } 00862 00863 // OpenMP definition 00864 if (LangOpts.OpenMP) { 00865 // OpenMP 2.2: 00866 // In implementations that support a preprocessor, the _OPENMP 00867 // macro name is defined to have the decimal value yyyymm where 00868 // yyyy and mm are the year and the month designations of the 00869 // version of the OpenMP API that the implementation support. 00870 Builder.defineMacro("_OPENMP", "201307"); 00871 } 00872 00873 // Get other target #defines. 00874 TI.getTargetDefines(LangOpts, Builder); 00875 } 00876 00877 /// InitializePreprocessor - Initialize the preprocessor getting it and the 00878 /// environment ready to process a single file. This returns true on error. 00879 /// 00880 void clang::InitializePreprocessor(Preprocessor &PP, 00881 const PreprocessorOptions &InitOpts, 00882 const FrontendOptions &FEOpts) { 00883 const LangOptions &LangOpts = PP.getLangOpts(); 00884 std::string PredefineBuffer; 00885 PredefineBuffer.reserve(4080); 00886 llvm::raw_string_ostream Predefines(PredefineBuffer); 00887 MacroBuilder Builder(Predefines); 00888 00889 // Emit line markers for various builtin sections of the file. We don't do 00890 // this in asm preprocessor mode, because "# 4" is not a line marker directive 00891 // in this mode. 00892 if (!PP.getLangOpts().AsmPreprocessor) 00893 Builder.append("# 1 \"<built-in>\" 3"); 00894 00895 // Install things like __POWERPC__, __GNUC__, etc into the macro table. 00896 if (InitOpts.UsePredefines) { 00897 InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, Builder); 00898 00899 // Install definitions to make Objective-C++ ARC work well with various 00900 // C++ Standard Library implementations. 00901 if (LangOpts.ObjC1 && LangOpts.CPlusPlus && LangOpts.ObjCAutoRefCount) { 00902 switch (InitOpts.ObjCXXARCStandardLibrary) { 00903 case ARCXX_nolib: 00904 case ARCXX_libcxx: 00905 break; 00906 00907 case ARCXX_libstdcxx: 00908 AddObjCXXARCLibstdcxxDefines(LangOpts, Builder); 00909 break; 00910 } 00911 } 00912 } 00913 00914 // Even with predefines off, some macros are still predefined. 00915 // These should all be defined in the preprocessor according to the 00916 // current language configuration. 00917 InitializeStandardPredefinedMacros(PP.getTargetInfo(), PP.getLangOpts(), 00918 FEOpts, Builder); 00919 00920 // Add on the predefines from the driver. Wrap in a #line directive to report 00921 // that they come from the command line. 00922 if (!PP.getLangOpts().AsmPreprocessor) 00923 Builder.append("# 1 \"<command line>\" 1"); 00924 00925 // Process #define's and #undef's in the order they are given. 00926 for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) { 00927 if (InitOpts.Macros[i].second) // isUndef 00928 Builder.undefineMacro(InitOpts.Macros[i].first); 00929 else 00930 DefineBuiltinMacro(Builder, InitOpts.Macros[i].first, 00931 PP.getDiagnostics()); 00932 } 00933 00934 // If -imacros are specified, include them now. These are processed before 00935 // any -include directives. 00936 for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i) 00937 AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i]); 00938 00939 // Process -include-pch/-include-pth directives. 00940 if (!InitOpts.ImplicitPCHInclude.empty()) 00941 AddImplicitIncludePCH(Builder, PP, InitOpts.ImplicitPCHInclude); 00942 if (!InitOpts.ImplicitPTHInclude.empty()) 00943 AddImplicitIncludePTH(Builder, PP, InitOpts.ImplicitPTHInclude); 00944 00945 // Process -include directives. 00946 for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) { 00947 const std::string &Path = InitOpts.Includes[i]; 00948 AddImplicitInclude(Builder, Path); 00949 } 00950 00951 // Exit the command line and go back to <built-in> (2 is LC_LEAVE). 00952 if (!PP.getLangOpts().AsmPreprocessor) 00953 Builder.append("# 1 \"<built-in>\" 2"); 00954 00955 // Instruct the preprocessor to skip the preamble. 00956 PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first, 00957 InitOpts.PrecompiledPreambleBytes.second); 00958 00959 // Copy PredefinedBuffer into the Preprocessor. 00960 PP.setPredefines(Predefines.str()); 00961 }