InitPreprocessor.cpp 26 KB

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