InitPreprocessor.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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/Frontend/Utils.h"
  14. #include "clang/Basic/FileManager.h"
  15. #include "clang/Basic/MacroBuilder.h"
  16. #include "clang/Basic/SourceManager.h"
  17. #include "clang/Basic/TargetInfo.h"
  18. #include "clang/Basic/Version.h"
  19. #include "clang/Frontend/FrontendDiagnostic.h"
  20. #include "clang/Frontend/FrontendOptions.h"
  21. #include "clang/Lex/HeaderSearch.h"
  22. #include "clang/Lex/Preprocessor.h"
  23. #include "clang/Lex/PreprocessorOptions.h"
  24. #include "clang/Serialization/ASTReader.h"
  25. #include "llvm/ADT/APFloat.h"
  26. #include "llvm/Support/FileSystem.h"
  27. #include "llvm/Support/MemoryBuffer.h"
  28. #include "llvm/Support/Path.h"
  29. using namespace clang;
  30. static bool MacroBodyEndsInBackslash(StringRef MacroBody) {
  31. while (!MacroBody.empty() && isWhitespace(MacroBody.back()))
  32. MacroBody = MacroBody.drop_back();
  33. return !MacroBody.empty() && MacroBody.back() == '\\';
  34. }
  35. // Append a #define line to Buf for Macro. Macro should be of the form XXX,
  36. // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
  37. // "#define XXX Y z W". To get a #define with no value, use "XXX=".
  38. static void DefineBuiltinMacro(MacroBuilder &Builder, StringRef Macro,
  39. DiagnosticsEngine &Diags) {
  40. std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
  41. StringRef MacroName = MacroPair.first;
  42. StringRef MacroBody = MacroPair.second;
  43. if (MacroName.size() != Macro.size()) {
  44. // Per GCC -D semantics, the macro ends at \n if it exists.
  45. StringRef::size_type End = MacroBody.find_first_of("\n\r");
  46. if (End != StringRef::npos)
  47. Diags.Report(diag::warn_fe_macro_contains_embedded_newline)
  48. << MacroName;
  49. MacroBody = MacroBody.substr(0, End);
  50. // We handle macro bodies which end in a backslash by appending an extra
  51. // backslash+newline. This makes sure we don't accidentally treat the
  52. // backslash as a line continuation marker.
  53. if (MacroBodyEndsInBackslash(MacroBody))
  54. Builder.defineMacro(MacroName, Twine(MacroBody) + "\\\n");
  55. else
  56. Builder.defineMacro(MacroName, MacroBody);
  57. } else {
  58. // Push "macroname 1".
  59. Builder.defineMacro(Macro);
  60. }
  61. }
  62. /// AddImplicitInclude - Add an implicit \#include of the specified file to the
  63. /// predefines buffer.
  64. static void AddImplicitInclude(MacroBuilder &Builder, StringRef File,
  65. FileManager &FileMgr) {
  66. Builder.append(Twine("#include \"") +
  67. HeaderSearch::NormalizeDashIncludePath(File, FileMgr) + "\"");
  68. }
  69. static void AddImplicitIncludeMacros(MacroBuilder &Builder,
  70. StringRef File,
  71. FileManager &FileMgr) {
  72. Builder.append(Twine("#__include_macros \"") +
  73. HeaderSearch::NormalizeDashIncludePath(File, FileMgr) + "\"");
  74. // Marker token to stop the __include_macros fetch loop.
  75. Builder.append("##"); // ##?
  76. }
  77. /// AddImplicitIncludePTH - Add an implicit \#include using the original file
  78. /// used to generate a PTH cache.
  79. static void AddImplicitIncludePTH(MacroBuilder &Builder, Preprocessor &PP,
  80. StringRef ImplicitIncludePTH) {
  81. PTHManager *P = PP.getPTHManager();
  82. // Null check 'P' in the corner case where it couldn't be created.
  83. const char *OriginalFile = P ? P->getOriginalSourceFile() : nullptr;
  84. if (!OriginalFile) {
  85. PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header)
  86. << ImplicitIncludePTH;
  87. return;
  88. }
  89. AddImplicitInclude(Builder, OriginalFile, PP.getFileManager());
  90. }
  91. /// \brief Add an implicit \#include using the original file used to generate
  92. /// a PCH file.
  93. static void AddImplicitIncludePCH(MacroBuilder &Builder, Preprocessor &PP,
  94. StringRef ImplicitIncludePCH) {
  95. std::string OriginalFile =
  96. ASTReader::getOriginalSourceFile(ImplicitIncludePCH, PP.getFileManager(),
  97. PP.getDiagnostics());
  98. if (OriginalFile.empty())
  99. return;
  100. AddImplicitInclude(Builder, OriginalFile, PP.getFileManager());
  101. }
  102. /// PickFP - This is used to pick a value based on the FP semantics of the
  103. /// specified FP model.
  104. template <typename T>
  105. static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal,
  106. T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
  107. T IEEEQuadVal) {
  108. if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle)
  109. return IEEESingleVal;
  110. if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble)
  111. return IEEEDoubleVal;
  112. if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended)
  113. return X87DoubleExtendedVal;
  114. if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble)
  115. return PPCDoubleDoubleVal;
  116. assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad);
  117. return IEEEQuadVal;
  118. }
  119. static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix,
  120. const llvm::fltSemantics *Sem, StringRef Ext) {
  121. const char *DenormMin, *Epsilon, *Max, *Min;
  122. DenormMin = PickFP(Sem, "1.40129846e-45", "4.9406564584124654e-324",
  123. "3.64519953188247460253e-4951",
  124. "4.94065645841246544176568792868221e-324",
  125. "6.47517511943802511092443895822764655e-4966");
  126. int Digits = PickFP(Sem, 6, 15, 18, 31, 33);
  127. Epsilon = PickFP(Sem, "1.19209290e-7", "2.2204460492503131e-16",
  128. "1.08420217248550443401e-19",
  129. "4.94065645841246544176568792868221e-324",
  130. "1.92592994438723585305597794258492732e-34");
  131. int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113);
  132. int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931);
  133. int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932);
  134. int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381);
  135. int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384);
  136. Min = PickFP(Sem, "1.17549435e-38", "2.2250738585072014e-308",
  137. "3.36210314311209350626e-4932",
  138. "2.00416836000897277799610805135016e-292",
  139. "3.36210314311209350626267781732175260e-4932");
  140. Max = PickFP(Sem, "3.40282347e+38", "1.7976931348623157e+308",
  141. "1.18973149535723176502e+4932",
  142. "1.79769313486231580793728971405301e+308",
  143. "1.18973149535723176508575932662800702e+4932");
  144. SmallString<32> DefPrefix;
  145. DefPrefix = "__";
  146. DefPrefix += Prefix;
  147. DefPrefix += "_";
  148. Builder.defineMacro(DefPrefix + "DENORM_MIN__", Twine(DenormMin)+Ext);
  149. Builder.defineMacro(DefPrefix + "HAS_DENORM__");
  150. Builder.defineMacro(DefPrefix + "DIG__", Twine(Digits));
  151. Builder.defineMacro(DefPrefix + "EPSILON__", Twine(Epsilon)+Ext);
  152. Builder.defineMacro(DefPrefix + "HAS_INFINITY__");
  153. Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__");
  154. Builder.defineMacro(DefPrefix + "MANT_DIG__", Twine(MantissaDigits));
  155. Builder.defineMacro(DefPrefix + "MAX_10_EXP__", Twine(Max10Exp));
  156. Builder.defineMacro(DefPrefix + "MAX_EXP__", Twine(MaxExp));
  157. Builder.defineMacro(DefPrefix + "MAX__", Twine(Max)+Ext);
  158. Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+Twine(Min10Exp)+")");
  159. Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+Twine(MinExp)+")");
  160. Builder.defineMacro(DefPrefix + "MIN__", Twine(Min)+Ext);
  161. }
  162. /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
  163. /// named MacroName with the max value for a type with width 'TypeWidth' a
  164. /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
  165. static void DefineTypeSize(const Twine &MacroName, unsigned TypeWidth,
  166. StringRef ValSuffix, bool isSigned,
  167. MacroBuilder &Builder) {
  168. llvm::APInt MaxVal = isSigned ? llvm::APInt::getSignedMaxValue(TypeWidth)
  169. : llvm::APInt::getMaxValue(TypeWidth);
  170. Builder.defineMacro(MacroName, MaxVal.toString(10, isSigned) + ValSuffix);
  171. }
  172. /// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
  173. /// the width, suffix, and signedness of the given type
  174. static void DefineTypeSize(const Twine &MacroName, TargetInfo::IntType Ty,
  175. const TargetInfo &TI, MacroBuilder &Builder) {
  176. DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
  177. TI.isTypeSigned(Ty), Builder);
  178. }
  179. static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty,
  180. MacroBuilder &Builder) {
  181. Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty));
  182. }
  183. static void DefineTypeWidth(StringRef MacroName, TargetInfo::IntType Ty,
  184. const TargetInfo &TI, MacroBuilder &Builder) {
  185. Builder.defineMacro(MacroName, Twine(TI.getTypeWidth(Ty)));
  186. }
  187. static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth,
  188. const TargetInfo &TI, MacroBuilder &Builder) {
  189. Builder.defineMacro(MacroName,
  190. Twine(BitWidth / TI.getCharWidth()));
  191. }
  192. static void DefineExactWidthIntType(TargetInfo::IntType Ty,
  193. const TargetInfo &TI,
  194. MacroBuilder &Builder) {
  195. int TypeWidth = TI.getTypeWidth(Ty);
  196. bool IsSigned = TI.isTypeSigned(Ty);
  197. // Use the target specified int64 type, when appropriate, so that [u]int64_t
  198. // ends up being defined in terms of the correct type.
  199. if (TypeWidth == 64)
  200. Ty = IsSigned ? TI.getInt64Type() : TI.getIntTypeByWidth(64, false);
  201. const char *Prefix = IsSigned ? "__INT" : "__UINT";
  202. DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
  203. StringRef ConstSuffix(TargetInfo::getTypeConstantSuffix(Ty));
  204. if (!ConstSuffix.empty())
  205. Builder.defineMacro(Prefix + Twine(TypeWidth) + "_C_SUFFIX__", ConstSuffix);
  206. }
  207. static void DefineExactWidthIntTypeSize(TargetInfo::IntType Ty,
  208. const TargetInfo &TI,
  209. MacroBuilder &Builder) {
  210. int TypeWidth = TI.getTypeWidth(Ty);
  211. bool IsSigned = TI.isTypeSigned(Ty);
  212. // Use the target specified int64 type, when appropriate, so that [u]int64_t
  213. // ends up being defined in terms of the correct type.
  214. if (TypeWidth == 64)
  215. Ty = IsSigned ? TI.getInt64Type() : TI.getIntTypeByWidth(64, false);
  216. const char *Prefix = IsSigned ? "__INT" : "__UINT";
  217. DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder);
  218. }
  219. static void DefineLeastWidthIntType(unsigned TypeWidth, bool IsSigned,
  220. const TargetInfo &TI,
  221. MacroBuilder &Builder) {
  222. TargetInfo::IntType Ty = TI.getLeastIntTypeByWidth(TypeWidth, IsSigned);
  223. if (Ty == TargetInfo::NoInt)
  224. return;
  225. const char *Prefix = IsSigned ? "__INT_LEAST" : "__UINT_LEAST";
  226. DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
  227. DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder);
  228. }
  229. static void DefineFastIntType(unsigned TypeWidth, bool IsSigned,
  230. const TargetInfo &TI, MacroBuilder &Builder) {
  231. // stdint.h currently defines the fast int types as equivalent to the least
  232. // types.
  233. TargetInfo::IntType Ty = TI.getLeastIntTypeByWidth(TypeWidth, IsSigned);
  234. if (Ty == TargetInfo::NoInt)
  235. return;
  236. const char *Prefix = IsSigned ? "__INT_FAST" : "__UINT_FAST";
  237. DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
  238. DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder);
  239. }
  240. /// Get the value the ATOMIC_*_LOCK_FREE macro should have for a type with
  241. /// the specified properties.
  242. static const char *getLockFreeValue(unsigned TypeWidth, unsigned TypeAlign,
  243. unsigned InlineWidth) {
  244. // Fully-aligned, power-of-2 sizes no larger than the inline
  245. // width will be inlined as lock-free operations.
  246. if (TypeWidth == TypeAlign && (TypeWidth & (TypeWidth - 1)) == 0 &&
  247. TypeWidth <= InlineWidth)
  248. return "2"; // "always lock free"
  249. // We cannot be certain what operations the lib calls might be
  250. // able to implement as lock-free on future processors.
  251. return "1"; // "sometimes lock free"
  252. }
  253. /// \brief Add definitions required for a smooth interaction between
  254. /// Objective-C++ automated reference counting and libstdc++ (4.2).
  255. static void AddObjCXXARCLibstdcxxDefines(const LangOptions &LangOpts,
  256. MacroBuilder &Builder) {
  257. Builder.defineMacro("_GLIBCXX_PREDEFINED_OBJC_ARC_IS_SCALAR");
  258. std::string Result;
  259. {
  260. // Provide specializations for the __is_scalar type trait so that
  261. // lifetime-qualified objects are not considered "scalar" types, which
  262. // libstdc++ uses as an indicator of the presence of trivial copy, assign,
  263. // default-construct, and destruct semantics (none of which hold for
  264. // lifetime-qualified objects in ARC).
  265. llvm::raw_string_ostream Out(Result);
  266. Out << "namespace std {\n"
  267. << "\n"
  268. << "struct __true_type;\n"
  269. << "struct __false_type;\n"
  270. << "\n";
  271. Out << "template<typename _Tp> struct __is_scalar;\n"
  272. << "\n";
  273. Out << "template<typename _Tp>\n"
  274. << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> {\n"
  275. << " enum { __value = 0 };\n"
  276. << " typedef __false_type __type;\n"
  277. << "};\n"
  278. << "\n";
  279. if (LangOpts.ObjCARCWeak) {
  280. Out << "template<typename _Tp>\n"
  281. << "struct __is_scalar<__attribute__((objc_ownership(weak))) _Tp> {\n"
  282. << " enum { __value = 0 };\n"
  283. << " typedef __false_type __type;\n"
  284. << "};\n"
  285. << "\n";
  286. }
  287. Out << "template<typename _Tp>\n"
  288. << "struct __is_scalar<__attribute__((objc_ownership(autoreleasing)))"
  289. << " _Tp> {\n"
  290. << " enum { __value = 0 };\n"
  291. << " typedef __false_type __type;\n"
  292. << "};\n"
  293. << "\n";
  294. Out << "}\n";
  295. }
  296. Builder.append(Result);
  297. }
  298. static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
  299. const LangOptions &LangOpts,
  300. const FrontendOptions &FEOpts,
  301. MacroBuilder &Builder) {
  302. if (!LangOpts.MSVCCompat && !LangOpts.TraditionalCPP)
  303. Builder.defineMacro("__STDC__");
  304. if (LangOpts.Freestanding)
  305. Builder.defineMacro("__STDC_HOSTED__", "0");
  306. else
  307. Builder.defineMacro("__STDC_HOSTED__");
  308. if (!LangOpts.CPlusPlus) {
  309. if (LangOpts.C11)
  310. Builder.defineMacro("__STDC_VERSION__", "201112L");
  311. else if (LangOpts.C99)
  312. Builder.defineMacro("__STDC_VERSION__", "199901L");
  313. else if (!LangOpts.GNUMode && LangOpts.Digraphs)
  314. Builder.defineMacro("__STDC_VERSION__", "199409L");
  315. } else {
  316. // FIXME: Use correct value for C++17.
  317. if (LangOpts.CPlusPlus1z)
  318. Builder.defineMacro("__cplusplus", "201406L");
  319. // C++1y [cpp.predefined]p1:
  320. // The name __cplusplus is defined to the value 201402L when compiling a
  321. // C++ translation unit.
  322. else if (LangOpts.CPlusPlus1y)
  323. Builder.defineMacro("__cplusplus", "201402L");
  324. // C++11 [cpp.predefined]p1:
  325. // The name __cplusplus is defined to the value 201103L when compiling a
  326. // C++ translation unit.
  327. else if (LangOpts.CPlusPlus11)
  328. Builder.defineMacro("__cplusplus", "201103L");
  329. // C++03 [cpp.predefined]p1:
  330. // The name __cplusplus is defined to the value 199711L when compiling a
  331. // C++ translation unit.
  332. else
  333. Builder.defineMacro("__cplusplus", "199711L");
  334. }
  335. // In C11 these are environment macros. In C++11 they are only defined
  336. // as part of <cuchar>. To prevent breakage when mixing C and C++
  337. // code, define these macros unconditionally. We can define them
  338. // unconditionally, as Clang always uses UTF-16 and UTF-32 for 16-bit
  339. // and 32-bit character literals.
  340. Builder.defineMacro("__STDC_UTF_16__", "1");
  341. Builder.defineMacro("__STDC_UTF_32__", "1");
  342. if (LangOpts.ObjC1)
  343. Builder.defineMacro("__OBJC__");
  344. // Not "standard" per se, but available even with the -undef flag.
  345. if (LangOpts.AsmPreprocessor)
  346. Builder.defineMacro("__ASSEMBLER__");
  347. }
  348. /// Initialize the predefined C++ language feature test macros defined in
  349. /// ISO/IEC JTC1/SC22/WG21 (C++) SD-6: "SG10 Feature Test Recommendations".
  350. static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts,
  351. MacroBuilder &Builder) {
  352. // C++11 features.
  353. if (LangOpts.CPlusPlus11) {
  354. Builder.defineMacro("__cpp_unicode_characters", "200704");
  355. Builder.defineMacro("__cpp_raw_strings", "200710");
  356. Builder.defineMacro("__cpp_unicode_literals", "200710");
  357. Builder.defineMacro("__cpp_user_defined_literals", "200809");
  358. Builder.defineMacro("__cpp_lambdas", "200907");
  359. Builder.defineMacro("__cpp_constexpr",
  360. LangOpts.CPlusPlus1y ? "201304" : "200704");
  361. Builder.defineMacro("__cpp_static_assert", "200410");
  362. Builder.defineMacro("__cpp_decltype", "200707");
  363. Builder.defineMacro("__cpp_attributes", "200809");
  364. Builder.defineMacro("__cpp_rvalue_references", "200610");
  365. Builder.defineMacro("__cpp_variadic_templates", "200704");
  366. }
  367. // C++14 features.
  368. if (LangOpts.CPlusPlus1y) {
  369. Builder.defineMacro("__cpp_binary_literals", "201304");
  370. Builder.defineMacro("__cpp_init_captures", "201304");
  371. Builder.defineMacro("__cpp_generic_lambdas", "201304");
  372. Builder.defineMacro("__cpp_decltype_auto", "201304");
  373. Builder.defineMacro("__cpp_return_type_deduction", "201304");
  374. Builder.defineMacro("__cpp_aggregate_nsdmi", "201304");
  375. Builder.defineMacro("__cpp_variable_templates", "201304");
  376. }
  377. }
  378. static void InitializePredefinedMacros(const TargetInfo &TI,
  379. const LangOptions &LangOpts,
  380. const FrontendOptions &FEOpts,
  381. MacroBuilder &Builder) {
  382. // Compiler version introspection macros.
  383. Builder.defineMacro("__llvm__"); // LLVM Backend
  384. Builder.defineMacro("__clang__"); // Clang Frontend
  385. #define TOSTR2(X) #X
  386. #define TOSTR(X) TOSTR2(X)
  387. Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR));
  388. Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR));
  389. #ifdef CLANG_VERSION_PATCHLEVEL
  390. Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL));
  391. #else
  392. Builder.defineMacro("__clang_patchlevel__", "0");
  393. #endif
  394. Builder.defineMacro("__clang_version__",
  395. "\"" CLANG_VERSION_STRING " "
  396. + getClangFullRepositoryVersion() + "\"");
  397. #undef TOSTR
  398. #undef TOSTR2
  399. if (!LangOpts.MSVCCompat) {
  400. // Currently claim to be compatible with GCC 4.2.1-5621, but only if we're
  401. // not compiling for MSVC compatibility
  402. Builder.defineMacro("__GNUC_MINOR__", "2");
  403. Builder.defineMacro("__GNUC_PATCHLEVEL__", "1");
  404. Builder.defineMacro("__GNUC__", "4");
  405. Builder.defineMacro("__GXX_ABI_VERSION", "1002");
  406. }
  407. // Define macros for the C11 / C++11 memory orderings
  408. Builder.defineMacro("__ATOMIC_RELAXED", "0");
  409. Builder.defineMacro("__ATOMIC_CONSUME", "1");
  410. Builder.defineMacro("__ATOMIC_ACQUIRE", "2");
  411. Builder.defineMacro("__ATOMIC_RELEASE", "3");
  412. Builder.defineMacro("__ATOMIC_ACQ_REL", "4");
  413. Builder.defineMacro("__ATOMIC_SEQ_CST", "5");
  414. // Support for #pragma redefine_extname (Sun compatibility)
  415. Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1");
  416. // As sad as it is, enough software depends on the __VERSION__ for version
  417. // checks that it is necessary to report 4.2.1 (the base GCC version we claim
  418. // compatibility with) first.
  419. Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible " +
  420. Twine(getClangFullCPPVersion()) + "\"");
  421. // Initialize language-specific preprocessor defines.
  422. // Standard conforming mode?
  423. if (!LangOpts.GNUMode && !LangOpts.MSVCCompat)
  424. Builder.defineMacro("__STRICT_ANSI__");
  425. if (LangOpts.CPlusPlus11)
  426. Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__");
  427. if (LangOpts.ObjC1) {
  428. if (LangOpts.ObjCRuntime.isNonFragile()) {
  429. Builder.defineMacro("__OBJC2__");
  430. if (LangOpts.ObjCExceptions)
  431. Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS");
  432. }
  433. if (LangOpts.getGC() != LangOptions::NonGC)
  434. Builder.defineMacro("__OBJC_GC__");
  435. if (LangOpts.ObjCRuntime.isNeXTFamily())
  436. Builder.defineMacro("__NEXT_RUNTIME__");
  437. if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::ObjFW) {
  438. VersionTuple tuple = LangOpts.ObjCRuntime.getVersion();
  439. unsigned minor = 0;
  440. if (tuple.getMinor().hasValue())
  441. minor = tuple.getMinor().getValue();
  442. unsigned subminor = 0;
  443. if (tuple.getSubminor().hasValue())
  444. subminor = tuple.getSubminor().getValue();
  445. Builder.defineMacro("__OBJFW_RUNTIME_ABI__",
  446. Twine(tuple.getMajor() * 10000 + minor * 100 +
  447. subminor));
  448. }
  449. Builder.defineMacro("IBOutlet", "__attribute__((iboutlet))");
  450. Builder.defineMacro("IBOutletCollection(ClassName)",
  451. "__attribute__((iboutletcollection(ClassName)))");
  452. Builder.defineMacro("IBAction", "void)__attribute__((ibaction)");
  453. }
  454. if (LangOpts.CPlusPlus)
  455. InitializeCPlusPlusFeatureTestMacros(LangOpts, Builder);
  456. // darwin_constant_cfstrings controls this. This is also dependent
  457. // on other things like the runtime I believe. This is set even for C code.
  458. if (!LangOpts.NoConstantCFStrings)
  459. Builder.defineMacro("__CONSTANT_CFSTRINGS__");
  460. if (LangOpts.ObjC2)
  461. Builder.defineMacro("OBJC_NEW_PROPERTIES");
  462. if (LangOpts.PascalStrings)
  463. Builder.defineMacro("__PASCAL_STRINGS__");
  464. if (LangOpts.Blocks) {
  465. Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))");
  466. Builder.defineMacro("__BLOCKS__");
  467. }
  468. if (!LangOpts.MSVCCompat && LangOpts.CXXExceptions)
  469. Builder.defineMacro("__EXCEPTIONS");
  470. if (LangOpts.RTTI)
  471. Builder.defineMacro("__GXX_RTTI");
  472. if (LangOpts.SjLjExceptions)
  473. Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__");
  474. if (LangOpts.Deprecated)
  475. Builder.defineMacro("__DEPRECATED");
  476. if (LangOpts.CPlusPlus) {
  477. Builder.defineMacro("__GNUG__", "4");
  478. Builder.defineMacro("__GXX_WEAK__");
  479. Builder.defineMacro("__private_extern__", "extern");
  480. }
  481. if (LangOpts.MicrosoftExt) {
  482. if (LangOpts.WChar) {
  483. // wchar_t supported as a keyword.
  484. Builder.defineMacro("_WCHAR_T_DEFINED");
  485. Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED");
  486. }
  487. }
  488. if (LangOpts.Optimize)
  489. Builder.defineMacro("__OPTIMIZE__");
  490. if (LangOpts.OptimizeSize)
  491. Builder.defineMacro("__OPTIMIZE_SIZE__");
  492. if (LangOpts.FastMath)
  493. Builder.defineMacro("__FAST_MATH__");
  494. // Initialize target-specific preprocessor defines.
  495. // __BYTE_ORDER__ was added in GCC 4.6. It's analogous
  496. // to the macro __BYTE_ORDER (no trailing underscores)
  497. // from glibc's <endian.h> header.
  498. // We don't support the PDP-11 as a target, but include
  499. // the define so it can still be compared against.
  500. Builder.defineMacro("__ORDER_LITTLE_ENDIAN__", "1234");
  501. Builder.defineMacro("__ORDER_BIG_ENDIAN__", "4321");
  502. Builder.defineMacro("__ORDER_PDP_ENDIAN__", "3412");
  503. if (TI.isBigEndian()) {
  504. Builder.defineMacro("__BYTE_ORDER__", "__ORDER_BIG_ENDIAN__");
  505. Builder.defineMacro("__BIG_ENDIAN__");
  506. } else {
  507. Builder.defineMacro("__BYTE_ORDER__", "__ORDER_LITTLE_ENDIAN__");
  508. Builder.defineMacro("__LITTLE_ENDIAN__");
  509. }
  510. if (TI.getPointerWidth(0) == 64 && TI.getLongWidth() == 64
  511. && TI.getIntWidth() == 32) {
  512. Builder.defineMacro("_LP64");
  513. Builder.defineMacro("__LP64__");
  514. }
  515. // Define type sizing macros based on the target properties.
  516. assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
  517. Builder.defineMacro("__CHAR_BIT__", "8");
  518. DefineTypeSize("__SCHAR_MAX__", TargetInfo::SignedChar, TI, Builder);
  519. DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder);
  520. DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder);
  521. DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder);
  522. DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder);
  523. DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder);
  524. DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder);
  525. DefineTypeSize("__SIZE_MAX__", TI.getSizeType(), TI, Builder);
  526. if (!LangOpts.MSVCCompat) {
  527. DefineTypeSize("__UINTMAX_MAX__", TI.getUIntMaxType(), TI, Builder);
  528. DefineTypeSize("__PTRDIFF_MAX__", TI.getPtrDiffType(0), TI, Builder);
  529. DefineTypeSize("__INTPTR_MAX__", TI.getIntPtrType(), TI, Builder);
  530. DefineTypeSize("__UINTPTR_MAX__", TI.getUIntPtrType(), TI, Builder);
  531. }
  532. DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder);
  533. DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder);
  534. DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder);
  535. DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder);
  536. DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder);
  537. DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder);
  538. DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder);
  539. DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder);
  540. DefineTypeSizeof("__SIZEOF_PTRDIFF_T__",
  541. TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder);
  542. DefineTypeSizeof("__SIZEOF_SIZE_T__",
  543. TI.getTypeWidth(TI.getSizeType()), TI, Builder);
  544. DefineTypeSizeof("__SIZEOF_WCHAR_T__",
  545. TI.getTypeWidth(TI.getWCharType()), TI, Builder);
  546. DefineTypeSizeof("__SIZEOF_WINT_T__",
  547. TI.getTypeWidth(TI.getWIntType()), TI, Builder);
  548. if (TI.hasInt128Type())
  549. DefineTypeSizeof("__SIZEOF_INT128__", 128, TI, Builder);
  550. DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder);
  551. DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder);
  552. DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder);
  553. DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder);
  554. DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder);
  555. DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder);
  556. DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder);
  557. DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder);
  558. DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder);
  559. DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder);
  560. DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder);
  561. DefineType("__WINT_TYPE__", TI.getWIntType(), Builder);
  562. DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder);
  563. DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder);
  564. DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder);
  565. DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder);
  566. if (!LangOpts.MSVCCompat) {
  567. DefineTypeWidth("__UINTMAX_WIDTH__", TI.getUIntMaxType(), TI, Builder);
  568. DefineType("__UINTPTR_TYPE__", TI.getUIntPtrType(), Builder);
  569. DefineTypeWidth("__UINTPTR_WIDTH__", TI.getUIntPtrType(), TI, Builder);
  570. }
  571. DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat(), "F");
  572. DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat(), "");
  573. DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat(), "L");
  574. // Define a __POINTER_WIDTH__ macro for stdint.h.
  575. Builder.defineMacro("__POINTER_WIDTH__",
  576. Twine((int)TI.getPointerWidth(0)));
  577. if (!LangOpts.CharIsSigned)
  578. Builder.defineMacro("__CHAR_UNSIGNED__");
  579. if (!TargetInfo::isTypeSigned(TI.getWCharType()))
  580. Builder.defineMacro("__WCHAR_UNSIGNED__");
  581. if (!TargetInfo::isTypeSigned(TI.getWIntType()))
  582. Builder.defineMacro("__WINT_UNSIGNED__");
  583. // Define exact-width integer types for stdint.h
  584. Builder.defineMacro("__INT" + Twine(TI.getCharWidth()) + "_TYPE__",
  585. "char");
  586. if (TI.getShortWidth() > TI.getCharWidth())
  587. DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder);
  588. if (TI.getIntWidth() > TI.getShortWidth())
  589. DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder);
  590. if (TI.getLongWidth() > TI.getIntWidth())
  591. DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder);
  592. if (TI.getLongLongWidth() > TI.getLongWidth())
  593. DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder);
  594. if (!LangOpts.MSVCCompat) {
  595. DefineExactWidthIntType(TargetInfo::UnsignedChar, TI, Builder);
  596. DefineExactWidthIntTypeSize(TargetInfo::UnsignedChar, TI, Builder);
  597. DefineExactWidthIntTypeSize(TargetInfo::SignedChar, TI, Builder);
  598. if (TI.getShortWidth() > TI.getCharWidth()) {
  599. DefineExactWidthIntType(TargetInfo::UnsignedShort, TI, Builder);
  600. DefineExactWidthIntTypeSize(TargetInfo::UnsignedShort, TI, Builder);
  601. DefineExactWidthIntTypeSize(TargetInfo::SignedShort, TI, Builder);
  602. }
  603. if (TI.getIntWidth() > TI.getShortWidth()) {
  604. DefineExactWidthIntType(TargetInfo::UnsignedInt, TI, Builder);
  605. DefineExactWidthIntTypeSize(TargetInfo::UnsignedInt, TI, Builder);
  606. DefineExactWidthIntTypeSize(TargetInfo::SignedInt, TI, Builder);
  607. }
  608. if (TI.getLongWidth() > TI.getIntWidth()) {
  609. DefineExactWidthIntType(TargetInfo::UnsignedLong, TI, Builder);
  610. DefineExactWidthIntTypeSize(TargetInfo::UnsignedLong, TI, Builder);
  611. DefineExactWidthIntTypeSize(TargetInfo::SignedLong, TI, Builder);
  612. }
  613. if (TI.getLongLongWidth() > TI.getLongWidth()) {
  614. DefineExactWidthIntType(TargetInfo::UnsignedLongLong, TI, Builder);
  615. DefineExactWidthIntTypeSize(TargetInfo::UnsignedLongLong, TI, Builder);
  616. DefineExactWidthIntTypeSize(TargetInfo::SignedLongLong, TI, Builder);
  617. }
  618. DefineLeastWidthIntType(8, true, TI, Builder);
  619. DefineLeastWidthIntType(8, false, TI, Builder);
  620. DefineLeastWidthIntType(16, true, TI, Builder);
  621. DefineLeastWidthIntType(16, false, TI, Builder);
  622. DefineLeastWidthIntType(32, true, TI, Builder);
  623. DefineLeastWidthIntType(32, false, TI, Builder);
  624. DefineLeastWidthIntType(64, true, TI, Builder);
  625. DefineLeastWidthIntType(64, false, TI, Builder);
  626. DefineFastIntType(8, true, TI, Builder);
  627. DefineFastIntType(8, false, TI, Builder);
  628. DefineFastIntType(16, true, TI, Builder);
  629. DefineFastIntType(16, false, TI, Builder);
  630. DefineFastIntType(32, true, TI, Builder);
  631. DefineFastIntType(32, false, TI, Builder);
  632. DefineFastIntType(64, true, TI, Builder);
  633. DefineFastIntType(64, false, TI, Builder);
  634. }
  635. if (const char *Prefix = TI.getUserLabelPrefix())
  636. Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix);
  637. if (LangOpts.FastMath || LangOpts.FiniteMathOnly)
  638. Builder.defineMacro("__FINITE_MATH_ONLY__", "1");
  639. else
  640. Builder.defineMacro("__FINITE_MATH_ONLY__", "0");
  641. if (LangOpts.GNUInline)
  642. Builder.defineMacro("__GNUC_GNU_INLINE__");
  643. else
  644. Builder.defineMacro("__GNUC_STDC_INLINE__");
  645. // The value written by __atomic_test_and_set.
  646. // FIXME: This is target-dependent.
  647. Builder.defineMacro("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1");
  648. // Used by libstdc++ to implement ATOMIC_<foo>_LOCK_FREE.
  649. unsigned InlineWidthBits = TI.getMaxAtomicInlineWidth();
  650. #define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \
  651. Builder.defineMacro("__GCC_ATOMIC_" #TYPE "_LOCK_FREE", \
  652. getLockFreeValue(TI.get##Type##Width(), \
  653. TI.get##Type##Align(), \
  654. InlineWidthBits));
  655. DEFINE_LOCK_FREE_MACRO(BOOL, Bool);
  656. DEFINE_LOCK_FREE_MACRO(CHAR, Char);
  657. DEFINE_LOCK_FREE_MACRO(CHAR16_T, Char16);
  658. DEFINE_LOCK_FREE_MACRO(CHAR32_T, Char32);
  659. DEFINE_LOCK_FREE_MACRO(WCHAR_T, WChar);
  660. DEFINE_LOCK_FREE_MACRO(SHORT, Short);
  661. DEFINE_LOCK_FREE_MACRO(INT, Int);
  662. DEFINE_LOCK_FREE_MACRO(LONG, Long);
  663. DEFINE_LOCK_FREE_MACRO(LLONG, LongLong);
  664. Builder.defineMacro("__GCC_ATOMIC_POINTER_LOCK_FREE",
  665. getLockFreeValue(TI.getPointerWidth(0),
  666. TI.getPointerAlign(0),
  667. InlineWidthBits));
  668. #undef DEFINE_LOCK_FREE_MACRO
  669. if (LangOpts.NoInlineDefine)
  670. Builder.defineMacro("__NO_INLINE__");
  671. if (unsigned PICLevel = LangOpts.PICLevel) {
  672. Builder.defineMacro("__PIC__", Twine(PICLevel));
  673. Builder.defineMacro("__pic__", Twine(PICLevel));
  674. }
  675. if (unsigned PIELevel = LangOpts.PIELevel) {
  676. Builder.defineMacro("__PIE__", Twine(PIELevel));
  677. Builder.defineMacro("__pie__", Twine(PIELevel));
  678. }
  679. // Macros to control C99 numerics and <float.h>
  680. Builder.defineMacro("__FLT_EVAL_METHOD__", Twine(TI.getFloatEvalMethod()));
  681. Builder.defineMacro("__FLT_RADIX__", "2");
  682. int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36);
  683. Builder.defineMacro("__DECIMAL_DIG__", Twine(Dig));
  684. if (LangOpts.getStackProtector() == LangOptions::SSPOn)
  685. Builder.defineMacro("__SSP__");
  686. else if (LangOpts.getStackProtector() == LangOptions::SSPStrong)
  687. Builder.defineMacro("__SSP_STRONG__", "2");
  688. else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
  689. Builder.defineMacro("__SSP_ALL__", "3");
  690. if (FEOpts.ProgramAction == frontend::RewriteObjC)
  691. Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
  692. // Define a macro that exists only when using the static analyzer.
  693. if (FEOpts.ProgramAction == frontend::RunAnalysis)
  694. Builder.defineMacro("__clang_analyzer__");
  695. if (LangOpts.FastRelaxedMath)
  696. Builder.defineMacro("__FAST_RELAXED_MATH__");
  697. if (LangOpts.ObjCAutoRefCount) {
  698. Builder.defineMacro("__weak", "__attribute__((objc_ownership(weak)))");
  699. Builder.defineMacro("__strong", "__attribute__((objc_ownership(strong)))");
  700. Builder.defineMacro("__autoreleasing",
  701. "__attribute__((objc_ownership(autoreleasing)))");
  702. Builder.defineMacro("__unsafe_unretained",
  703. "__attribute__((objc_ownership(none)))");
  704. }
  705. // OpenMP definition
  706. if (LangOpts.OpenMP) {
  707. // OpenMP 2.2:
  708. // In implementations that support a preprocessor, the _OPENMP
  709. // macro name is defined to have the decimal value yyyymm where
  710. // yyyy and mm are the year and the month designations of the
  711. // version of the OpenMP API that the implementation support.
  712. Builder.defineMacro("_OPENMP", "201307");
  713. }
  714. // Get other target #defines.
  715. TI.getTargetDefines(LangOpts, Builder);
  716. }
  717. /// InitializePreprocessor - Initialize the preprocessor getting it and the
  718. /// environment ready to process a single file. This returns true on error.
  719. ///
  720. void clang::InitializePreprocessor(Preprocessor &PP,
  721. const PreprocessorOptions &InitOpts,
  722. const FrontendOptions &FEOpts) {
  723. const LangOptions &LangOpts = PP.getLangOpts();
  724. std::string PredefineBuffer;
  725. PredefineBuffer.reserve(4080);
  726. llvm::raw_string_ostream Predefines(PredefineBuffer);
  727. MacroBuilder Builder(Predefines);
  728. // Emit line markers for various builtin sections of the file. We don't do
  729. // this in asm preprocessor mode, because "# 4" is not a line marker directive
  730. // in this mode.
  731. if (!PP.getLangOpts().AsmPreprocessor)
  732. Builder.append("# 1 \"<built-in>\" 3");
  733. // Install things like __POWERPC__, __GNUC__, etc into the macro table.
  734. if (InitOpts.UsePredefines) {
  735. InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, Builder);
  736. // Install definitions to make Objective-C++ ARC work well with various
  737. // C++ Standard Library implementations.
  738. if (LangOpts.ObjC1 && LangOpts.CPlusPlus && LangOpts.ObjCAutoRefCount) {
  739. switch (InitOpts.ObjCXXARCStandardLibrary) {
  740. case ARCXX_nolib:
  741. case ARCXX_libcxx:
  742. break;
  743. case ARCXX_libstdcxx:
  744. AddObjCXXARCLibstdcxxDefines(LangOpts, Builder);
  745. break;
  746. }
  747. }
  748. }
  749. // Even with predefines off, some macros are still predefined.
  750. // These should all be defined in the preprocessor according to the
  751. // current language configuration.
  752. InitializeStandardPredefinedMacros(PP.getTargetInfo(), PP.getLangOpts(),
  753. FEOpts, Builder);
  754. // Add on the predefines from the driver. Wrap in a #line directive to report
  755. // that they come from the command line.
  756. if (!PP.getLangOpts().AsmPreprocessor)
  757. Builder.append("# 1 \"<command line>\" 1");
  758. // Process #define's and #undef's in the order they are given.
  759. for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
  760. if (InitOpts.Macros[i].second) // isUndef
  761. Builder.undefineMacro(InitOpts.Macros[i].first);
  762. else
  763. DefineBuiltinMacro(Builder, InitOpts.Macros[i].first,
  764. PP.getDiagnostics());
  765. }
  766. // If -imacros are specified, include them now. These are processed before
  767. // any -include directives.
  768. for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
  769. AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i],
  770. PP.getFileManager());
  771. // Process -include-pch/-include-pth directives.
  772. if (!InitOpts.ImplicitPCHInclude.empty())
  773. AddImplicitIncludePCH(Builder, PP, InitOpts.ImplicitPCHInclude);
  774. if (!InitOpts.ImplicitPTHInclude.empty())
  775. AddImplicitIncludePTH(Builder, PP, InitOpts.ImplicitPTHInclude);
  776. // Process -include directives.
  777. for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
  778. const std::string &Path = InitOpts.Includes[i];
  779. AddImplicitInclude(Builder, Path, PP.getFileManager());
  780. }
  781. // Exit the command line and go back to <built-in> (2 is LC_LEAVE).
  782. if (!PP.getLangOpts().AsmPreprocessor)
  783. Builder.append("# 1 \"<built-in>\" 2");
  784. // Instruct the preprocessor to skip the preamble.
  785. PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first,
  786. InitOpts.PrecompiledPreambleBytes.second);
  787. // Copy PredefinedBuffer into the Preprocessor.
  788. PP.setPredefines(Predefines.str());
  789. }