InitPreprocessor.cpp 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  1. //===--- InitPreprocessor.cpp - PP initialization code. ---------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the clang::InitializePreprocessor function.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Basic/FileManager.h"
  13. #include "clang/Basic/MacroBuilder.h"
  14. #include "clang/Basic/SourceManager.h"
  15. #include "clang/Basic/SyncScope.h"
  16. #include "clang/Basic/TargetInfo.h"
  17. #include "clang/Basic/Version.h"
  18. #include "clang/Frontend/FrontendDiagnostic.h"
  19. #include "clang/Frontend/FrontendOptions.h"
  20. #include "clang/Frontend/Utils.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/IR/DataLayout.h"
  27. #include "llvm/IR/DerivedTypes.h"
  28. using namespace clang;
  29. static bool MacroBodyEndsInBackslash(StringRef MacroBody) {
  30. while (!MacroBody.empty() && isWhitespace(MacroBody.back()))
  31. MacroBody = MacroBody.drop_back();
  32. return !MacroBody.empty() && MacroBody.back() == '\\';
  33. }
  34. // Append a #define line to Buf for Macro. Macro should be of the form XXX,
  35. // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
  36. // "#define XXX Y z W". To get a #define with no value, use "XXX=".
  37. static void DefineBuiltinMacro(MacroBuilder &Builder, StringRef Macro,
  38. DiagnosticsEngine &Diags) {
  39. std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
  40. StringRef MacroName = MacroPair.first;
  41. StringRef MacroBody = MacroPair.second;
  42. if (MacroName.size() != Macro.size()) {
  43. // Per GCC -D semantics, the macro ends at \n if it exists.
  44. StringRef::size_type End = MacroBody.find_first_of("\n\r");
  45. if (End != StringRef::npos)
  46. Diags.Report(diag::warn_fe_macro_contains_embedded_newline)
  47. << MacroName;
  48. MacroBody = MacroBody.substr(0, End);
  49. // We handle macro bodies which end in a backslash by appending an extra
  50. // backslash+newline. This makes sure we don't accidentally treat the
  51. // backslash as a line continuation marker.
  52. if (MacroBodyEndsInBackslash(MacroBody))
  53. Builder.defineMacro(MacroName, Twine(MacroBody) + "\\\n");
  54. else
  55. Builder.defineMacro(MacroName, MacroBody);
  56. } else {
  57. // Push "macroname 1".
  58. Builder.defineMacro(Macro);
  59. }
  60. }
  61. /// AddImplicitInclude - Add an implicit \#include of the specified file to the
  62. /// predefines buffer.
  63. /// As these includes are generated by -include arguments the header search
  64. /// logic is going to search relatively to the current working directory.
  65. static void AddImplicitInclude(MacroBuilder &Builder, StringRef File) {
  66. Builder.append(Twine("#include \"") + File + "\"");
  67. }
  68. static void AddImplicitIncludeMacros(MacroBuilder &Builder, StringRef File) {
  69. Builder.append(Twine("#__include_macros \"") + File + "\"");
  70. // Marker token to stop the __include_macros fetch loop.
  71. Builder.append("##"); // ##?
  72. }
  73. /// Add an implicit \#include using the original file used to generate
  74. /// a PCH file.
  75. static void AddImplicitIncludePCH(MacroBuilder &Builder, Preprocessor &PP,
  76. const PCHContainerReader &PCHContainerRdr,
  77. StringRef ImplicitIncludePCH) {
  78. std::string OriginalFile = ASTReader::getOriginalSourceFile(
  79. std::string(ImplicitIncludePCH), PP.getFileManager(), PCHContainerRdr,
  80. PP.getDiagnostics());
  81. if (OriginalFile.empty())
  82. return;
  83. AddImplicitInclude(Builder, OriginalFile);
  84. }
  85. /// PickFP - This is used to pick a value based on the FP semantics of the
  86. /// specified FP model.
  87. template <typename T>
  88. static T PickFP(const llvm::fltSemantics *Sem, T IEEEHalfVal, T IEEESingleVal,
  89. T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
  90. T IEEEQuadVal) {
  91. if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEhalf())
  92. return IEEEHalfVal;
  93. if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle())
  94. return IEEESingleVal;
  95. if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble())
  96. return IEEEDoubleVal;
  97. if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended())
  98. return X87DoubleExtendedVal;
  99. if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble())
  100. return PPCDoubleDoubleVal;
  101. assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad());
  102. return IEEEQuadVal;
  103. }
  104. static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix,
  105. const llvm::fltSemantics *Sem, StringRef Ext) {
  106. const char *DenormMin, *Epsilon, *Max, *Min;
  107. DenormMin = PickFP(Sem, "5.9604644775390625e-8", "1.40129846e-45",
  108. "4.9406564584124654e-324", "3.64519953188247460253e-4951",
  109. "4.94065645841246544176568792868221e-324",
  110. "6.47517511943802511092443895822764655e-4966");
  111. int Digits = PickFP(Sem, 3, 6, 15, 18, 31, 33);
  112. int DecimalDigits = PickFP(Sem, 5, 9, 17, 21, 33, 36);
  113. Epsilon = PickFP(Sem, "9.765625e-4", "1.19209290e-7",
  114. "2.2204460492503131e-16", "1.08420217248550443401e-19",
  115. "4.94065645841246544176568792868221e-324",
  116. "1.92592994438723585305597794258492732e-34");
  117. int MantissaDigits = PickFP(Sem, 11, 24, 53, 64, 106, 113);
  118. int Min10Exp = PickFP(Sem, -4, -37, -307, -4931, -291, -4931);
  119. int Max10Exp = PickFP(Sem, 4, 38, 308, 4932, 308, 4932);
  120. int MinExp = PickFP(Sem, -13, -125, -1021, -16381, -968, -16381);
  121. int MaxExp = PickFP(Sem, 16, 128, 1024, 16384, 1024, 16384);
  122. Min = PickFP(Sem, "6.103515625e-5", "1.17549435e-38", "2.2250738585072014e-308",
  123. "3.36210314311209350626e-4932",
  124. "2.00416836000897277799610805135016e-292",
  125. "3.36210314311209350626267781732175260e-4932");
  126. Max = PickFP(Sem, "6.5504e+4", "3.40282347e+38", "1.7976931348623157e+308",
  127. "1.18973149535723176502e+4932",
  128. "1.79769313486231580793728971405301e+308",
  129. "1.18973149535723176508575932662800702e+4932");
  130. SmallString<32> DefPrefix;
  131. DefPrefix = "__";
  132. DefPrefix += Prefix;
  133. DefPrefix += "_";
  134. Builder.defineMacro(DefPrefix + "DENORM_MIN__", Twine(DenormMin)+Ext);
  135. Builder.defineMacro(DefPrefix + "HAS_DENORM__");
  136. Builder.defineMacro(DefPrefix + "DIG__", Twine(Digits));
  137. Builder.defineMacro(DefPrefix + "DECIMAL_DIG__", Twine(DecimalDigits));
  138. Builder.defineMacro(DefPrefix + "EPSILON__", Twine(Epsilon)+Ext);
  139. Builder.defineMacro(DefPrefix + "HAS_INFINITY__");
  140. Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__");
  141. Builder.defineMacro(DefPrefix + "MANT_DIG__", Twine(MantissaDigits));
  142. Builder.defineMacro(DefPrefix + "MAX_10_EXP__", Twine(Max10Exp));
  143. Builder.defineMacro(DefPrefix + "MAX_EXP__", Twine(MaxExp));
  144. Builder.defineMacro(DefPrefix + "MAX__", Twine(Max)+Ext);
  145. Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+Twine(Min10Exp)+")");
  146. Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+Twine(MinExp)+")");
  147. Builder.defineMacro(DefPrefix + "MIN__", Twine(Min)+Ext);
  148. }
  149. /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
  150. /// named MacroName with the max value for a type with width 'TypeWidth' a
  151. /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
  152. static void DefineTypeSize(const Twine &MacroName, unsigned TypeWidth,
  153. StringRef ValSuffix, bool isSigned,
  154. MacroBuilder &Builder) {
  155. llvm::APInt MaxVal = isSigned ? llvm::APInt::getSignedMaxValue(TypeWidth)
  156. : llvm::APInt::getMaxValue(TypeWidth);
  157. Builder.defineMacro(MacroName, toString(MaxVal, 10, isSigned) + ValSuffix);
  158. }
  159. /// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
  160. /// the width, suffix, and signedness of the given type
  161. static void DefineTypeSize(const Twine &MacroName, TargetInfo::IntType Ty,
  162. const TargetInfo &TI, MacroBuilder &Builder) {
  163. DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
  164. TI.isTypeSigned(Ty), Builder);
  165. }
  166. static void DefineFmt(const Twine &Prefix, TargetInfo::IntType Ty,
  167. const TargetInfo &TI, MacroBuilder &Builder) {
  168. bool IsSigned = TI.isTypeSigned(Ty);
  169. StringRef FmtModifier = TI.getTypeFormatModifier(Ty);
  170. for (const char *Fmt = IsSigned ? "di" : "ouxX"; *Fmt; ++Fmt) {
  171. Builder.defineMacro(Prefix + "_FMT" + Twine(*Fmt) + "__",
  172. Twine("\"") + FmtModifier + Twine(*Fmt) + "\"");
  173. }
  174. }
  175. static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty,
  176. MacroBuilder &Builder) {
  177. Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty));
  178. }
  179. static void DefineTypeWidth(const Twine &MacroName, TargetInfo::IntType Ty,
  180. const TargetInfo &TI, MacroBuilder &Builder) {
  181. Builder.defineMacro(MacroName, Twine(TI.getTypeWidth(Ty)));
  182. }
  183. static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth,
  184. const TargetInfo &TI, MacroBuilder &Builder) {
  185. Builder.defineMacro(MacroName,
  186. Twine(BitWidth / TI.getCharWidth()));
  187. }
  188. // This will generate a macro based on the prefix with `_MAX__` as the suffix
  189. // for the max value representable for the type, and a macro with a `_WIDTH__`
  190. // suffix for the width of the type.
  191. static void DefineTypeSizeAndWidth(const Twine &Prefix, TargetInfo::IntType Ty,
  192. const TargetInfo &TI,
  193. MacroBuilder &Builder) {
  194. DefineTypeSize(Prefix + "_MAX__", Ty, TI, Builder);
  195. DefineTypeWidth(Prefix + "_WIDTH__", Ty, TI, Builder);
  196. }
  197. static void DefineExactWidthIntType(TargetInfo::IntType Ty,
  198. const TargetInfo &TI,
  199. MacroBuilder &Builder) {
  200. int TypeWidth = TI.getTypeWidth(Ty);
  201. bool IsSigned = TI.isTypeSigned(Ty);
  202. // Use the target specified int64 type, when appropriate, so that [u]int64_t
  203. // ends up being defined in terms of the correct type.
  204. if (TypeWidth == 64)
  205. Ty = IsSigned ? TI.getInt64Type() : TI.getUInt64Type();
  206. // Use the target specified int16 type when appropriate. Some MCU targets
  207. // (such as AVR) have definition of [u]int16_t to [un]signed int.
  208. if (TypeWidth == 16)
  209. Ty = IsSigned ? TI.getInt16Type() : TI.getUInt16Type();
  210. const char *Prefix = IsSigned ? "__INT" : "__UINT";
  211. DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
  212. DefineFmt(Prefix + Twine(TypeWidth), Ty, TI, Builder);
  213. StringRef ConstSuffix(TI.getTypeConstantSuffix(Ty));
  214. Builder.defineMacro(Prefix + Twine(TypeWidth) + "_C_SUFFIX__", ConstSuffix);
  215. }
  216. static void DefineExactWidthIntTypeSize(TargetInfo::IntType Ty,
  217. const TargetInfo &TI,
  218. MacroBuilder &Builder) {
  219. int TypeWidth = TI.getTypeWidth(Ty);
  220. bool IsSigned = TI.isTypeSigned(Ty);
  221. // Use the target specified int64 type, when appropriate, so that [u]int64_t
  222. // ends up being defined in terms of the correct type.
  223. if (TypeWidth == 64)
  224. Ty = IsSigned ? TI.getInt64Type() : TI.getUInt64Type();
  225. // We don't need to define a _WIDTH macro for the exact-width types because
  226. // we already know the width.
  227. const char *Prefix = IsSigned ? "__INT" : "__UINT";
  228. DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder);
  229. }
  230. static void DefineLeastWidthIntType(unsigned TypeWidth, bool IsSigned,
  231. const TargetInfo &TI,
  232. MacroBuilder &Builder) {
  233. TargetInfo::IntType Ty = TI.getLeastIntTypeByWidth(TypeWidth, IsSigned);
  234. if (Ty == TargetInfo::NoInt)
  235. return;
  236. const char *Prefix = IsSigned ? "__INT_LEAST" : "__UINT_LEAST";
  237. DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
  238. // We only want the *_WIDTH macro for the signed types to avoid too many
  239. // predefined macros (the unsigned width and the signed width are identical.)
  240. if (IsSigned)
  241. DefineTypeSizeAndWidth(Prefix + Twine(TypeWidth), Ty, TI, Builder);
  242. else
  243. DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder);
  244. DefineFmt(Prefix + Twine(TypeWidth), Ty, TI, Builder);
  245. }
  246. static void DefineFastIntType(unsigned TypeWidth, bool IsSigned,
  247. const TargetInfo &TI, MacroBuilder &Builder) {
  248. // stdint.h currently defines the fast int types as equivalent to the least
  249. // types.
  250. TargetInfo::IntType Ty = TI.getLeastIntTypeByWidth(TypeWidth, IsSigned);
  251. if (Ty == TargetInfo::NoInt)
  252. return;
  253. const char *Prefix = IsSigned ? "__INT_FAST" : "__UINT_FAST";
  254. DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
  255. // We only want the *_WIDTH macro for the signed types to avoid too many
  256. // predefined macros (the unsigned width and the signed width are identical.)
  257. if (IsSigned)
  258. DefineTypeSizeAndWidth(Prefix + Twine(TypeWidth), Ty, TI, Builder);
  259. else
  260. DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder);
  261. DefineFmt(Prefix + Twine(TypeWidth), Ty, TI, Builder);
  262. }
  263. /// Get the value the ATOMIC_*_LOCK_FREE macro should have for a type with
  264. /// the specified properties.
  265. static const char *getLockFreeValue(unsigned TypeWidth, unsigned TypeAlign,
  266. unsigned InlineWidth) {
  267. // Fully-aligned, power-of-2 sizes no larger than the inline
  268. // width will be inlined as lock-free operations.
  269. if (TypeWidth == TypeAlign && (TypeWidth & (TypeWidth - 1)) == 0 &&
  270. TypeWidth <= InlineWidth)
  271. return "2"; // "always lock free"
  272. // We cannot be certain what operations the lib calls might be
  273. // able to implement as lock-free on future processors.
  274. return "1"; // "sometimes lock free"
  275. }
  276. /// Add definitions required for a smooth interaction between
  277. /// Objective-C++ automated reference counting and libstdc++ (4.2).
  278. static void AddObjCXXARCLibstdcxxDefines(const LangOptions &LangOpts,
  279. MacroBuilder &Builder) {
  280. Builder.defineMacro("_GLIBCXX_PREDEFINED_OBJC_ARC_IS_SCALAR");
  281. std::string Result;
  282. {
  283. // Provide specializations for the __is_scalar type trait so that
  284. // lifetime-qualified objects are not considered "scalar" types, which
  285. // libstdc++ uses as an indicator of the presence of trivial copy, assign,
  286. // default-construct, and destruct semantics (none of which hold for
  287. // lifetime-qualified objects in ARC).
  288. llvm::raw_string_ostream Out(Result);
  289. Out << "namespace std {\n"
  290. << "\n"
  291. << "struct __true_type;\n"
  292. << "struct __false_type;\n"
  293. << "\n";
  294. Out << "template<typename _Tp> struct __is_scalar;\n"
  295. << "\n";
  296. if (LangOpts.ObjCAutoRefCount) {
  297. Out << "template<typename _Tp>\n"
  298. << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> {\n"
  299. << " enum { __value = 0 };\n"
  300. << " typedef __false_type __type;\n"
  301. << "};\n"
  302. << "\n";
  303. }
  304. if (LangOpts.ObjCWeak) {
  305. Out << "template<typename _Tp>\n"
  306. << "struct __is_scalar<__attribute__((objc_ownership(weak))) _Tp> {\n"
  307. << " enum { __value = 0 };\n"
  308. << " typedef __false_type __type;\n"
  309. << "};\n"
  310. << "\n";
  311. }
  312. if (LangOpts.ObjCAutoRefCount) {
  313. Out << "template<typename _Tp>\n"
  314. << "struct __is_scalar<__attribute__((objc_ownership(autoreleasing)))"
  315. << " _Tp> {\n"
  316. << " enum { __value = 0 };\n"
  317. << " typedef __false_type __type;\n"
  318. << "};\n"
  319. << "\n";
  320. }
  321. Out << "}\n";
  322. }
  323. Builder.append(Result);
  324. }
  325. static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
  326. const LangOptions &LangOpts,
  327. const FrontendOptions &FEOpts,
  328. MacroBuilder &Builder) {
  329. // C++ [cpp.predefined]p1:
  330. // The following macro names shall be defined by the implementation:
  331. // -- __STDC__
  332. // [C++] Whether __STDC__ is predefined and if so, what its value is,
  333. // are implementation-defined.
  334. // (Removed in C++20.)
  335. if (!LangOpts.MSVCCompat && !LangOpts.TraditionalCPP)
  336. Builder.defineMacro("__STDC__");
  337. // -- __STDC_HOSTED__
  338. // The integer literal 1 if the implementation is a hosted
  339. // implementation or the integer literal 0 if it is not.
  340. if (LangOpts.Freestanding)
  341. Builder.defineMacro("__STDC_HOSTED__", "0");
  342. else
  343. Builder.defineMacro("__STDC_HOSTED__");
  344. // -- __STDC_VERSION__
  345. // [C++] Whether __STDC_VERSION__ is predefined and if so, what its
  346. // value is, are implementation-defined.
  347. // (Removed in C++20.)
  348. if (!LangOpts.CPlusPlus) {
  349. // FIXME: Use correct value for C23.
  350. if (LangOpts.C2x)
  351. Builder.defineMacro("__STDC_VERSION__", "202000L");
  352. else if (LangOpts.C17)
  353. Builder.defineMacro("__STDC_VERSION__", "201710L");
  354. else if (LangOpts.C11)
  355. Builder.defineMacro("__STDC_VERSION__", "201112L");
  356. else if (LangOpts.C99)
  357. Builder.defineMacro("__STDC_VERSION__", "199901L");
  358. else if (!LangOpts.GNUMode && LangOpts.Digraphs)
  359. Builder.defineMacro("__STDC_VERSION__", "199409L");
  360. } else {
  361. // -- __cplusplus
  362. // FIXME: Use correct value for C++23.
  363. if (LangOpts.CPlusPlus2b)
  364. Builder.defineMacro("__cplusplus", "202101L");
  365. // [C++20] The integer literal 202002L.
  366. else if (LangOpts.CPlusPlus20)
  367. Builder.defineMacro("__cplusplus", "202002L");
  368. // [C++17] The integer literal 201703L.
  369. else if (LangOpts.CPlusPlus17)
  370. Builder.defineMacro("__cplusplus", "201703L");
  371. // [C++14] The name __cplusplus is defined to the value 201402L when
  372. // compiling a C++ translation unit.
  373. else if (LangOpts.CPlusPlus14)
  374. Builder.defineMacro("__cplusplus", "201402L");
  375. // [C++11] The name __cplusplus is defined to the value 201103L when
  376. // compiling a C++ translation unit.
  377. else if (LangOpts.CPlusPlus11)
  378. Builder.defineMacro("__cplusplus", "201103L");
  379. // [C++03] The name __cplusplus is defined to the value 199711L when
  380. // compiling a C++ translation unit.
  381. else
  382. Builder.defineMacro("__cplusplus", "199711L");
  383. // -- __STDCPP_DEFAULT_NEW_ALIGNMENT__
  384. // [C++17] An integer literal of type std::size_t whose value is the
  385. // alignment guaranteed by a call to operator new(std::size_t)
  386. //
  387. // We provide this in all language modes, since it seems generally useful.
  388. Builder.defineMacro("__STDCPP_DEFAULT_NEW_ALIGNMENT__",
  389. Twine(TI.getNewAlign() / TI.getCharWidth()) +
  390. TI.getTypeConstantSuffix(TI.getSizeType()));
  391. // -- __STDCPP_­THREADS__
  392. // Defined, and has the value integer literal 1, if and only if a
  393. // program can have more than one thread of execution.
  394. if (LangOpts.getThreadModel() == LangOptions::ThreadModelKind::POSIX)
  395. Builder.defineMacro("__STDCPP_THREADS__", "1");
  396. }
  397. // In C11 these are environment macros. In C++11 they are only defined
  398. // as part of <cuchar>. To prevent breakage when mixing C and C++
  399. // code, define these macros unconditionally. We can define them
  400. // unconditionally, as Clang always uses UTF-16 and UTF-32 for 16-bit
  401. // and 32-bit character literals.
  402. Builder.defineMacro("__STDC_UTF_16__", "1");
  403. Builder.defineMacro("__STDC_UTF_32__", "1");
  404. if (LangOpts.ObjC)
  405. Builder.defineMacro("__OBJC__");
  406. // OpenCL v1.0/1.1 s6.9, v1.2/2.0 s6.10: Preprocessor Directives and Macros.
  407. if (LangOpts.OpenCL) {
  408. if (LangOpts.CPlusPlus) {
  409. switch (LangOpts.OpenCLCPlusPlusVersion) {
  410. case 100:
  411. Builder.defineMacro("__OPENCL_CPP_VERSION__", "100");
  412. break;
  413. case 202100:
  414. Builder.defineMacro("__OPENCL_CPP_VERSION__", "202100");
  415. break;
  416. default:
  417. llvm_unreachable("Unsupported C++ version for OpenCL");
  418. }
  419. Builder.defineMacro("__CL_CPP_VERSION_1_0__", "100");
  420. Builder.defineMacro("__CL_CPP_VERSION_2021__", "202100");
  421. } else {
  422. // OpenCL v1.0 and v1.1 do not have a predefined macro to indicate the
  423. // language standard with which the program is compiled. __OPENCL_VERSION__
  424. // is for the OpenCL version supported by the OpenCL device, which is not
  425. // necessarily the language standard with which the program is compiled.
  426. // A shared OpenCL header file requires a macro to indicate the language
  427. // standard. As a workaround, __OPENCL_C_VERSION__ is defined for
  428. // OpenCL v1.0 and v1.1.
  429. switch (LangOpts.OpenCLVersion) {
  430. case 100:
  431. Builder.defineMacro("__OPENCL_C_VERSION__", "100");
  432. break;
  433. case 110:
  434. Builder.defineMacro("__OPENCL_C_VERSION__", "110");
  435. break;
  436. case 120:
  437. Builder.defineMacro("__OPENCL_C_VERSION__", "120");
  438. break;
  439. case 200:
  440. Builder.defineMacro("__OPENCL_C_VERSION__", "200");
  441. break;
  442. case 300:
  443. Builder.defineMacro("__OPENCL_C_VERSION__", "300");
  444. break;
  445. default:
  446. llvm_unreachable("Unsupported OpenCL version");
  447. }
  448. }
  449. Builder.defineMacro("CL_VERSION_1_0", "100");
  450. Builder.defineMacro("CL_VERSION_1_1", "110");
  451. Builder.defineMacro("CL_VERSION_1_2", "120");
  452. Builder.defineMacro("CL_VERSION_2_0", "200");
  453. Builder.defineMacro("CL_VERSION_3_0", "300");
  454. if (TI.isLittleEndian())
  455. Builder.defineMacro("__ENDIAN_LITTLE__");
  456. if (LangOpts.FastRelaxedMath)
  457. Builder.defineMacro("__FAST_RELAXED_MATH__");
  458. }
  459. if (LangOpts.SYCLIsDevice || LangOpts.SYCLIsHost) {
  460. // SYCL Version is set to a value when building SYCL applications
  461. if (LangOpts.getSYCLVersion() == LangOptions::SYCL_2017)
  462. Builder.defineMacro("CL_SYCL_LANGUAGE_VERSION", "121");
  463. else if (LangOpts.getSYCLVersion() == LangOptions::SYCL_2020)
  464. Builder.defineMacro("SYCL_LANGUAGE_VERSION", "202001");
  465. }
  466. // Not "standard" per se, but available even with the -undef flag.
  467. if (LangOpts.AsmPreprocessor)
  468. Builder.defineMacro("__ASSEMBLER__");
  469. if (LangOpts.CUDA) {
  470. if (LangOpts.GPURelocatableDeviceCode)
  471. Builder.defineMacro("__CLANG_RDC__");
  472. if (!LangOpts.HIP)
  473. Builder.defineMacro("__CUDA__");
  474. }
  475. if (LangOpts.HIP) {
  476. Builder.defineMacro("__HIP__");
  477. Builder.defineMacro("__HIPCC__");
  478. Builder.defineMacro("__HIP_MEMORY_SCOPE_SINGLETHREAD", "1");
  479. Builder.defineMacro("__HIP_MEMORY_SCOPE_WAVEFRONT", "2");
  480. Builder.defineMacro("__HIP_MEMORY_SCOPE_WORKGROUP", "3");
  481. Builder.defineMacro("__HIP_MEMORY_SCOPE_AGENT", "4");
  482. Builder.defineMacro("__HIP_MEMORY_SCOPE_SYSTEM", "5");
  483. if (LangOpts.CUDAIsDevice)
  484. Builder.defineMacro("__HIP_DEVICE_COMPILE__");
  485. }
  486. }
  487. /// Initialize the predefined C++ language feature test macros defined in
  488. /// ISO/IEC JTC1/SC22/WG21 (C++) SD-6: "SG10 Feature Test Recommendations".
  489. static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts,
  490. MacroBuilder &Builder) {
  491. // C++98 features.
  492. if (LangOpts.RTTI)
  493. Builder.defineMacro("__cpp_rtti", "199711L");
  494. if (LangOpts.CXXExceptions)
  495. Builder.defineMacro("__cpp_exceptions", "199711L");
  496. // C++11 features.
  497. if (LangOpts.CPlusPlus11) {
  498. Builder.defineMacro("__cpp_unicode_characters", "200704L");
  499. Builder.defineMacro("__cpp_raw_strings", "200710L");
  500. Builder.defineMacro("__cpp_unicode_literals", "200710L");
  501. Builder.defineMacro("__cpp_user_defined_literals", "200809L");
  502. Builder.defineMacro("__cpp_lambdas", "200907L");
  503. Builder.defineMacro("__cpp_constexpr",
  504. LangOpts.CPlusPlus20 ? "201907L" :
  505. LangOpts.CPlusPlus17 ? "201603L" :
  506. LangOpts.CPlusPlus14 ? "201304L" : "200704");
  507. Builder.defineMacro("__cpp_constexpr_in_decltype", "201711L");
  508. Builder.defineMacro("__cpp_range_based_for",
  509. LangOpts.CPlusPlus17 ? "201603L" : "200907");
  510. Builder.defineMacro("__cpp_static_assert",
  511. LangOpts.CPlusPlus17 ? "201411L" : "200410");
  512. Builder.defineMacro("__cpp_decltype", "200707L");
  513. Builder.defineMacro("__cpp_attributes", "200809L");
  514. Builder.defineMacro("__cpp_rvalue_references", "200610L");
  515. Builder.defineMacro("__cpp_variadic_templates", "200704L");
  516. Builder.defineMacro("__cpp_initializer_lists", "200806L");
  517. Builder.defineMacro("__cpp_delegating_constructors", "200604L");
  518. Builder.defineMacro("__cpp_nsdmi", "200809L");
  519. Builder.defineMacro("__cpp_inheriting_constructors", "201511L");
  520. Builder.defineMacro("__cpp_ref_qualifiers", "200710L");
  521. Builder.defineMacro("__cpp_alias_templates", "200704L");
  522. }
  523. if (LangOpts.ThreadsafeStatics)
  524. Builder.defineMacro("__cpp_threadsafe_static_init", "200806L");
  525. // C++14 features.
  526. if (LangOpts.CPlusPlus14) {
  527. Builder.defineMacro("__cpp_binary_literals", "201304L");
  528. Builder.defineMacro("__cpp_digit_separators", "201309L");
  529. Builder.defineMacro("__cpp_init_captures",
  530. LangOpts.CPlusPlus20 ? "201803L" : "201304L");
  531. Builder.defineMacro("__cpp_generic_lambdas",
  532. LangOpts.CPlusPlus20 ? "201707L" : "201304L");
  533. Builder.defineMacro("__cpp_decltype_auto", "201304L");
  534. Builder.defineMacro("__cpp_return_type_deduction", "201304L");
  535. Builder.defineMacro("__cpp_aggregate_nsdmi", "201304L");
  536. Builder.defineMacro("__cpp_variable_templates", "201304L");
  537. }
  538. if (LangOpts.SizedDeallocation)
  539. Builder.defineMacro("__cpp_sized_deallocation", "201309L");
  540. // C++17 features.
  541. if (LangOpts.CPlusPlus17) {
  542. Builder.defineMacro("__cpp_hex_float", "201603L");
  543. Builder.defineMacro("__cpp_inline_variables", "201606L");
  544. Builder.defineMacro("__cpp_noexcept_function_type", "201510L");
  545. Builder.defineMacro("__cpp_capture_star_this", "201603L");
  546. Builder.defineMacro("__cpp_if_constexpr", "201606L");
  547. Builder.defineMacro("__cpp_deduction_guides", "201703L"); // (not latest)
  548. Builder.defineMacro("__cpp_template_auto", "201606L"); // (old name)
  549. Builder.defineMacro("__cpp_namespace_attributes", "201411L");
  550. Builder.defineMacro("__cpp_enumerator_attributes", "201411L");
  551. Builder.defineMacro("__cpp_nested_namespace_definitions", "201411L");
  552. Builder.defineMacro("__cpp_variadic_using", "201611L");
  553. Builder.defineMacro("__cpp_aggregate_bases", "201603L");
  554. Builder.defineMacro("__cpp_structured_bindings", "201606L");
  555. Builder.defineMacro("__cpp_nontype_template_args",
  556. "201411L"); // (not latest)
  557. Builder.defineMacro("__cpp_fold_expressions", "201603L");
  558. Builder.defineMacro("__cpp_guaranteed_copy_elision", "201606L");
  559. Builder.defineMacro("__cpp_nontype_template_parameter_auto", "201606L");
  560. }
  561. if (LangOpts.AlignedAllocation && !LangOpts.AlignedAllocationUnavailable)
  562. Builder.defineMacro("__cpp_aligned_new", "201606L");
  563. if (LangOpts.RelaxedTemplateTemplateArgs)
  564. Builder.defineMacro("__cpp_template_template_args", "201611L");
  565. // C++20 features.
  566. if (LangOpts.CPlusPlus20) {
  567. //Builder.defineMacro("__cpp_aggregate_paren_init", "201902L");
  568. Builder.defineMacro("__cpp_concepts", "201907L");
  569. Builder.defineMacro("__cpp_conditional_explicit", "201806L");
  570. //Builder.defineMacro("__cpp_consteval", "201811L");
  571. Builder.defineMacro("__cpp_constexpr_dynamic_alloc", "201907L");
  572. Builder.defineMacro("__cpp_constinit", "201907L");
  573. Builder.defineMacro("__cpp_impl_coroutine", "201902L");
  574. Builder.defineMacro("__cpp_designated_initializers", "201707L");
  575. Builder.defineMacro("__cpp_impl_three_way_comparison", "201907L");
  576. //Builder.defineMacro("__cpp_modules", "201907L");
  577. Builder.defineMacro("__cpp_using_enum", "201907L");
  578. }
  579. // C++2b features.
  580. if (LangOpts.CPlusPlus2b) {
  581. Builder.defineMacro("__cpp_implicit_move", "202011L");
  582. Builder.defineMacro("__cpp_size_t_suffix", "202011L");
  583. Builder.defineMacro("__cpp_if_consteval", "202106L");
  584. }
  585. if (LangOpts.Char8)
  586. Builder.defineMacro("__cpp_char8_t", "201811L");
  587. Builder.defineMacro("__cpp_impl_destroying_delete", "201806L");
  588. // TS features.
  589. if (LangOpts.Coroutines)
  590. Builder.defineMacro("__cpp_coroutines", "201703L");
  591. }
  592. /// InitializeOpenCLFeatureTestMacros - Define OpenCL macros based on target
  593. /// settings and language version
  594. void InitializeOpenCLFeatureTestMacros(const TargetInfo &TI,
  595. const LangOptions &Opts,
  596. MacroBuilder &Builder) {
  597. const llvm::StringMap<bool> &OpenCLFeaturesMap = TI.getSupportedOpenCLOpts();
  598. // FIXME: OpenCL options which affect language semantics/syntax
  599. // should be moved into LangOptions.
  600. auto defineOpenCLExtMacro = [&](llvm::StringRef Name, auto... OptArgs) {
  601. // Check if extension is supported by target and is available in this
  602. // OpenCL version
  603. if (TI.hasFeatureEnabled(OpenCLFeaturesMap, Name) &&
  604. OpenCLOptions::isOpenCLOptionAvailableIn(Opts, OptArgs...))
  605. Builder.defineMacro(Name);
  606. };
  607. #define OPENCL_GENERIC_EXTENSION(Ext, ...) \
  608. defineOpenCLExtMacro(#Ext, __VA_ARGS__);
  609. #include "clang/Basic/OpenCLExtensions.def"
  610. // Assume compiling for FULL profile
  611. Builder.defineMacro("__opencl_c_int64");
  612. }
  613. static void InitializePredefinedMacros(const TargetInfo &TI,
  614. const LangOptions &LangOpts,
  615. const FrontendOptions &FEOpts,
  616. const PreprocessorOptions &PPOpts,
  617. MacroBuilder &Builder) {
  618. // Compiler version introspection macros.
  619. Builder.defineMacro("__llvm__"); // LLVM Backend
  620. Builder.defineMacro("__clang__"); // Clang Frontend
  621. #define TOSTR2(X) #X
  622. #define TOSTR(X) TOSTR2(X)
  623. Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR));
  624. Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR));
  625. Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL));
  626. #undef TOSTR
  627. #undef TOSTR2
  628. Builder.defineMacro("__clang_version__",
  629. "\"" CLANG_VERSION_STRING " "
  630. + getClangFullRepositoryVersion() + "\"");
  631. if (LangOpts.GNUCVersion != 0) {
  632. // Major, minor, patch, are given two decimal places each, so 4.2.1 becomes
  633. // 40201.
  634. unsigned GNUCMajor = LangOpts.GNUCVersion / 100 / 100;
  635. unsigned GNUCMinor = LangOpts.GNUCVersion / 100 % 100;
  636. unsigned GNUCPatch = LangOpts.GNUCVersion % 100;
  637. Builder.defineMacro("__GNUC__", Twine(GNUCMajor));
  638. Builder.defineMacro("__GNUC_MINOR__", Twine(GNUCMinor));
  639. Builder.defineMacro("__GNUC_PATCHLEVEL__", Twine(GNUCPatch));
  640. Builder.defineMacro("__GXX_ABI_VERSION", "1002");
  641. if (LangOpts.CPlusPlus) {
  642. Builder.defineMacro("__GNUG__", Twine(GNUCMajor));
  643. Builder.defineMacro("__GXX_WEAK__");
  644. }
  645. }
  646. // Define macros for the C11 / C++11 memory orderings
  647. Builder.defineMacro("__ATOMIC_RELAXED", "0");
  648. Builder.defineMacro("__ATOMIC_CONSUME", "1");
  649. Builder.defineMacro("__ATOMIC_ACQUIRE", "2");
  650. Builder.defineMacro("__ATOMIC_RELEASE", "3");
  651. Builder.defineMacro("__ATOMIC_ACQ_REL", "4");
  652. Builder.defineMacro("__ATOMIC_SEQ_CST", "5");
  653. // Define macros for the OpenCL memory scope.
  654. // The values should match AtomicScopeOpenCLModel::ID enum.
  655. static_assert(
  656. static_cast<unsigned>(AtomicScopeOpenCLModel::WorkGroup) == 1 &&
  657. static_cast<unsigned>(AtomicScopeOpenCLModel::Device) == 2 &&
  658. static_cast<unsigned>(AtomicScopeOpenCLModel::AllSVMDevices) == 3 &&
  659. static_cast<unsigned>(AtomicScopeOpenCLModel::SubGroup) == 4,
  660. "Invalid OpenCL memory scope enum definition");
  661. Builder.defineMacro("__OPENCL_MEMORY_SCOPE_WORK_ITEM", "0");
  662. Builder.defineMacro("__OPENCL_MEMORY_SCOPE_WORK_GROUP", "1");
  663. Builder.defineMacro("__OPENCL_MEMORY_SCOPE_DEVICE", "2");
  664. Builder.defineMacro("__OPENCL_MEMORY_SCOPE_ALL_SVM_DEVICES", "3");
  665. Builder.defineMacro("__OPENCL_MEMORY_SCOPE_SUB_GROUP", "4");
  666. // Support for #pragma redefine_extname (Sun compatibility)
  667. Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1");
  668. // Previously this macro was set to a string aiming to achieve compatibility
  669. // with GCC 4.2.1. Now, just return the full Clang version
  670. Builder.defineMacro("__VERSION__", "\"" +
  671. Twine(getClangFullCPPVersion()) + "\"");
  672. // Initialize language-specific preprocessor defines.
  673. // Standard conforming mode?
  674. if (!LangOpts.GNUMode && !LangOpts.MSVCCompat)
  675. Builder.defineMacro("__STRICT_ANSI__");
  676. if (LangOpts.GNUCVersion && LangOpts.CPlusPlus11)
  677. Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__");
  678. if (LangOpts.ObjC) {
  679. if (LangOpts.ObjCRuntime.isNonFragile()) {
  680. Builder.defineMacro("__OBJC2__");
  681. if (LangOpts.ObjCExceptions)
  682. Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS");
  683. }
  684. if (LangOpts.getGC() != LangOptions::NonGC)
  685. Builder.defineMacro("__OBJC_GC__");
  686. if (LangOpts.ObjCRuntime.isNeXTFamily())
  687. Builder.defineMacro("__NEXT_RUNTIME__");
  688. if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::GNUstep) {
  689. auto version = LangOpts.ObjCRuntime.getVersion();
  690. std::string versionString = "1";
  691. // Don't rely on the tuple argument, because we can be asked to target
  692. // later ABIs than we actually support, so clamp these values to those
  693. // currently supported
  694. if (version >= VersionTuple(2, 0))
  695. Builder.defineMacro("__OBJC_GNUSTEP_RUNTIME_ABI__", "20");
  696. else
  697. Builder.defineMacro("__OBJC_GNUSTEP_RUNTIME_ABI__",
  698. "1" + Twine(std::min(8U, version.getMinor().getValueOr(0))));
  699. }
  700. if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::ObjFW) {
  701. VersionTuple tuple = LangOpts.ObjCRuntime.getVersion();
  702. unsigned minor = 0;
  703. if (tuple.getMinor().hasValue())
  704. minor = tuple.getMinor().getValue();
  705. unsigned subminor = 0;
  706. if (tuple.getSubminor().hasValue())
  707. subminor = tuple.getSubminor().getValue();
  708. Builder.defineMacro("__OBJFW_RUNTIME_ABI__",
  709. Twine(tuple.getMajor() * 10000 + minor * 100 +
  710. subminor));
  711. }
  712. Builder.defineMacro("IBOutlet", "__attribute__((iboutlet))");
  713. Builder.defineMacro("IBOutletCollection(ClassName)",
  714. "__attribute__((iboutletcollection(ClassName)))");
  715. Builder.defineMacro("IBAction", "void)__attribute__((ibaction)");
  716. Builder.defineMacro("IBInspectable", "");
  717. Builder.defineMacro("IB_DESIGNABLE", "");
  718. }
  719. // Define a macro that describes the Objective-C boolean type even for C
  720. // and C++ since BOOL can be used from non Objective-C code.
  721. Builder.defineMacro("__OBJC_BOOL_IS_BOOL",
  722. Twine(TI.useSignedCharForObjCBool() ? "0" : "1"));
  723. if (LangOpts.CPlusPlus)
  724. InitializeCPlusPlusFeatureTestMacros(LangOpts, Builder);
  725. // darwin_constant_cfstrings controls this. This is also dependent
  726. // on other things like the runtime I believe. This is set even for C code.
  727. if (!LangOpts.NoConstantCFStrings)
  728. Builder.defineMacro("__CONSTANT_CFSTRINGS__");
  729. if (LangOpts.ObjC)
  730. Builder.defineMacro("OBJC_NEW_PROPERTIES");
  731. if (LangOpts.PascalStrings)
  732. Builder.defineMacro("__PASCAL_STRINGS__");
  733. if (LangOpts.Blocks) {
  734. Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))");
  735. Builder.defineMacro("__BLOCKS__");
  736. }
  737. if (!LangOpts.MSVCCompat && LangOpts.Exceptions)
  738. Builder.defineMacro("__EXCEPTIONS");
  739. if (LangOpts.GNUCVersion && LangOpts.RTTI)
  740. Builder.defineMacro("__GXX_RTTI");
  741. if (LangOpts.hasSjLjExceptions())
  742. Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__");
  743. else if (LangOpts.hasSEHExceptions())
  744. Builder.defineMacro("__SEH__");
  745. else if (LangOpts.hasDWARFExceptions() &&
  746. (TI.getTriple().isThumb() || TI.getTriple().isARM()))
  747. Builder.defineMacro("__ARM_DWARF_EH__");
  748. if (LangOpts.Deprecated)
  749. Builder.defineMacro("__DEPRECATED");
  750. if (!LangOpts.MSVCCompat && LangOpts.CPlusPlus)
  751. Builder.defineMacro("__private_extern__", "extern");
  752. if (LangOpts.MicrosoftExt) {
  753. if (LangOpts.WChar) {
  754. // wchar_t supported as a keyword.
  755. Builder.defineMacro("_WCHAR_T_DEFINED");
  756. Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED");
  757. }
  758. }
  759. // Macros to help identify the narrow and wide character sets
  760. // FIXME: clang currently ignores -fexec-charset=. If this changes,
  761. // then this may need to be updated.
  762. Builder.defineMacro("__clang_literal_encoding__", "\"UTF-8\"");
  763. if (TI.getTypeWidth(TI.getWCharType()) >= 32) {
  764. // FIXME: 32-bit wchar_t signals UTF-32. This may change
  765. // if -fwide-exec-charset= is ever supported.
  766. Builder.defineMacro("__clang_wide_literal_encoding__", "\"UTF-32\"");
  767. } else {
  768. // FIXME: Less-than 32-bit wchar_t generally means UTF-16
  769. // (e.g., Windows, 32-bit IBM). This may need to be
  770. // updated if -fwide-exec-charset= is ever supported.
  771. Builder.defineMacro("__clang_wide_literal_encoding__", "\"UTF-16\"");
  772. }
  773. if (LangOpts.Optimize)
  774. Builder.defineMacro("__OPTIMIZE__");
  775. if (LangOpts.OptimizeSize)
  776. Builder.defineMacro("__OPTIMIZE_SIZE__");
  777. if (LangOpts.FastMath)
  778. Builder.defineMacro("__FAST_MATH__");
  779. // Initialize target-specific preprocessor defines.
  780. // __BYTE_ORDER__ was added in GCC 4.6. It's analogous
  781. // to the macro __BYTE_ORDER (no trailing underscores)
  782. // from glibc's <endian.h> header.
  783. // We don't support the PDP-11 as a target, but include
  784. // the define so it can still be compared against.
  785. Builder.defineMacro("__ORDER_LITTLE_ENDIAN__", "1234");
  786. Builder.defineMacro("__ORDER_BIG_ENDIAN__", "4321");
  787. Builder.defineMacro("__ORDER_PDP_ENDIAN__", "3412");
  788. if (TI.isBigEndian()) {
  789. Builder.defineMacro("__BYTE_ORDER__", "__ORDER_BIG_ENDIAN__");
  790. Builder.defineMacro("__BIG_ENDIAN__");
  791. } else {
  792. Builder.defineMacro("__BYTE_ORDER__", "__ORDER_LITTLE_ENDIAN__");
  793. Builder.defineMacro("__LITTLE_ENDIAN__");
  794. }
  795. if (TI.getPointerWidth(0) == 64 && TI.getLongWidth() == 64
  796. && TI.getIntWidth() == 32) {
  797. Builder.defineMacro("_LP64");
  798. Builder.defineMacro("__LP64__");
  799. }
  800. if (TI.getPointerWidth(0) == 32 && TI.getLongWidth() == 32
  801. && TI.getIntWidth() == 32) {
  802. Builder.defineMacro("_ILP32");
  803. Builder.defineMacro("__ILP32__");
  804. }
  805. // Define type sizing macros based on the target properties.
  806. assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
  807. Builder.defineMacro("__CHAR_BIT__", Twine(TI.getCharWidth()));
  808. Builder.defineMacro("__BOOL_WIDTH__", Twine(TI.getBoolWidth()));
  809. Builder.defineMacro("__SHRT_WIDTH__", Twine(TI.getShortWidth()));
  810. Builder.defineMacro("__INT_WIDTH__", Twine(TI.getIntWidth()));
  811. Builder.defineMacro("__LONG_WIDTH__", Twine(TI.getLongWidth()));
  812. Builder.defineMacro("__LLONG_WIDTH__", Twine(TI.getLongLongWidth()));
  813. size_t BitIntMaxWidth = TI.getMaxBitIntWidth();
  814. assert(BitIntMaxWidth <= llvm::IntegerType::MAX_INT_BITS &&
  815. "Target defined a max bit width larger than LLVM can support!");
  816. assert(BitIntMaxWidth >= TI.getLongLongWidth() &&
  817. "Target defined a max bit width smaller than the C standard allows!");
  818. Builder.defineMacro("__BITINT_MAXWIDTH__", Twine(BitIntMaxWidth));
  819. DefineTypeSize("__SCHAR_MAX__", TargetInfo::SignedChar, TI, Builder);
  820. DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder);
  821. DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder);
  822. DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder);
  823. DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder);
  824. DefineTypeSizeAndWidth("__WCHAR", TI.getWCharType(), TI, Builder);
  825. DefineTypeSizeAndWidth("__WINT", TI.getWIntType(), TI, Builder);
  826. DefineTypeSizeAndWidth("__INTMAX", TI.getIntMaxType(), TI, Builder);
  827. DefineTypeSizeAndWidth("__SIZE", TI.getSizeType(), TI, Builder);
  828. DefineTypeSizeAndWidth("__UINTMAX", TI.getUIntMaxType(), TI, Builder);
  829. DefineTypeSizeAndWidth("__PTRDIFF", TI.getPtrDiffType(0), TI, Builder);
  830. DefineTypeSizeAndWidth("__INTPTR", TI.getIntPtrType(), TI, Builder);
  831. DefineTypeSizeAndWidth("__UINTPTR", TI.getUIntPtrType(), TI, Builder);
  832. DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder);
  833. DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder);
  834. DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder);
  835. DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder);
  836. DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder);
  837. DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder);
  838. DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder);
  839. DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder);
  840. DefineTypeSizeof("__SIZEOF_PTRDIFF_T__",
  841. TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder);
  842. DefineTypeSizeof("__SIZEOF_SIZE_T__",
  843. TI.getTypeWidth(TI.getSizeType()), TI, Builder);
  844. DefineTypeSizeof("__SIZEOF_WCHAR_T__",
  845. TI.getTypeWidth(TI.getWCharType()), TI, Builder);
  846. DefineTypeSizeof("__SIZEOF_WINT_T__",
  847. TI.getTypeWidth(TI.getWIntType()), TI, Builder);
  848. if (TI.hasInt128Type())
  849. DefineTypeSizeof("__SIZEOF_INT128__", 128, TI, Builder);
  850. DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder);
  851. DefineFmt("__INTMAX", TI.getIntMaxType(), TI, Builder);
  852. Builder.defineMacro("__INTMAX_C_SUFFIX__",
  853. TI.getTypeConstantSuffix(TI.getIntMaxType()));
  854. DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder);
  855. DefineFmt("__UINTMAX", TI.getUIntMaxType(), TI, Builder);
  856. Builder.defineMacro("__UINTMAX_C_SUFFIX__",
  857. TI.getTypeConstantSuffix(TI.getUIntMaxType()));
  858. DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder);
  859. DefineFmt("__PTRDIFF", TI.getPtrDiffType(0), TI, Builder);
  860. DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder);
  861. DefineFmt("__INTPTR", TI.getIntPtrType(), TI, Builder);
  862. DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder);
  863. DefineFmt("__SIZE", TI.getSizeType(), TI, Builder);
  864. DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder);
  865. DefineType("__WINT_TYPE__", TI.getWIntType(), Builder);
  866. DefineTypeSizeAndWidth("__SIG_ATOMIC", TI.getSigAtomicType(), TI, Builder);
  867. DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder);
  868. DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder);
  869. DefineType("__UINTPTR_TYPE__", TI.getUIntPtrType(), Builder);
  870. DefineFmt("__UINTPTR", TI.getUIntPtrType(), TI, Builder);
  871. // The C standard requires the width of uintptr_t and intptr_t to be the same,
  872. // per 7.20.2.4p1. Same for intmax_t and uintmax_t, per 7.20.2.5p1.
  873. assert(TI.getTypeWidth(TI.getUIntPtrType()) ==
  874. TI.getTypeWidth(TI.getIntPtrType()) &&
  875. "uintptr_t and intptr_t have different widths?");
  876. assert(TI.getTypeWidth(TI.getUIntMaxType()) ==
  877. TI.getTypeWidth(TI.getIntMaxType()) &&
  878. "uintmax_t and intmax_t have different widths?");
  879. if (TI.hasFloat16Type())
  880. DefineFloatMacros(Builder, "FLT16", &TI.getHalfFormat(), "F16");
  881. DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat(), "F");
  882. DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat(), "");
  883. DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat(), "L");
  884. // Define a __POINTER_WIDTH__ macro for stdint.h.
  885. Builder.defineMacro("__POINTER_WIDTH__",
  886. Twine((int)TI.getPointerWidth(0)));
  887. // Define __BIGGEST_ALIGNMENT__ to be compatible with gcc.
  888. Builder.defineMacro("__BIGGEST_ALIGNMENT__",
  889. Twine(TI.getSuitableAlign() / TI.getCharWidth()) );
  890. if (!LangOpts.CharIsSigned)
  891. Builder.defineMacro("__CHAR_UNSIGNED__");
  892. if (!TargetInfo::isTypeSigned(TI.getWCharType()))
  893. Builder.defineMacro("__WCHAR_UNSIGNED__");
  894. if (!TargetInfo::isTypeSigned(TI.getWIntType()))
  895. Builder.defineMacro("__WINT_UNSIGNED__");
  896. // Define exact-width integer types for stdint.h
  897. DefineExactWidthIntType(TargetInfo::SignedChar, TI, Builder);
  898. if (TI.getShortWidth() > TI.getCharWidth())
  899. DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder);
  900. if (TI.getIntWidth() > TI.getShortWidth())
  901. DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder);
  902. if (TI.getLongWidth() > TI.getIntWidth())
  903. DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder);
  904. if (TI.getLongLongWidth() > TI.getLongWidth())
  905. DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder);
  906. DefineExactWidthIntType(TargetInfo::UnsignedChar, TI, Builder);
  907. DefineExactWidthIntTypeSize(TargetInfo::UnsignedChar, TI, Builder);
  908. DefineExactWidthIntTypeSize(TargetInfo::SignedChar, TI, Builder);
  909. if (TI.getShortWidth() > TI.getCharWidth()) {
  910. DefineExactWidthIntType(TargetInfo::UnsignedShort, TI, Builder);
  911. DefineExactWidthIntTypeSize(TargetInfo::UnsignedShort, TI, Builder);
  912. DefineExactWidthIntTypeSize(TargetInfo::SignedShort, TI, Builder);
  913. }
  914. if (TI.getIntWidth() > TI.getShortWidth()) {
  915. DefineExactWidthIntType(TargetInfo::UnsignedInt, TI, Builder);
  916. DefineExactWidthIntTypeSize(TargetInfo::UnsignedInt, TI, Builder);
  917. DefineExactWidthIntTypeSize(TargetInfo::SignedInt, TI, Builder);
  918. }
  919. if (TI.getLongWidth() > TI.getIntWidth()) {
  920. DefineExactWidthIntType(TargetInfo::UnsignedLong, TI, Builder);
  921. DefineExactWidthIntTypeSize(TargetInfo::UnsignedLong, TI, Builder);
  922. DefineExactWidthIntTypeSize(TargetInfo::SignedLong, TI, Builder);
  923. }
  924. if (TI.getLongLongWidth() > TI.getLongWidth()) {
  925. DefineExactWidthIntType(TargetInfo::UnsignedLongLong, TI, Builder);
  926. DefineExactWidthIntTypeSize(TargetInfo::UnsignedLongLong, TI, Builder);
  927. DefineExactWidthIntTypeSize(TargetInfo::SignedLongLong, TI, Builder);
  928. }
  929. DefineLeastWidthIntType(8, true, TI, Builder);
  930. DefineLeastWidthIntType(8, false, TI, Builder);
  931. DefineLeastWidthIntType(16, true, TI, Builder);
  932. DefineLeastWidthIntType(16, false, TI, Builder);
  933. DefineLeastWidthIntType(32, true, TI, Builder);
  934. DefineLeastWidthIntType(32, false, TI, Builder);
  935. DefineLeastWidthIntType(64, true, TI, Builder);
  936. DefineLeastWidthIntType(64, false, TI, Builder);
  937. DefineFastIntType(8, true, TI, Builder);
  938. DefineFastIntType(8, false, TI, Builder);
  939. DefineFastIntType(16, true, TI, Builder);
  940. DefineFastIntType(16, false, TI, Builder);
  941. DefineFastIntType(32, true, TI, Builder);
  942. DefineFastIntType(32, false, TI, Builder);
  943. DefineFastIntType(64, true, TI, Builder);
  944. DefineFastIntType(64, false, TI, Builder);
  945. Builder.defineMacro("__USER_LABEL_PREFIX__", TI.getUserLabelPrefix());
  946. if (!LangOpts.MathErrno)
  947. Builder.defineMacro("__NO_MATH_ERRNO__");
  948. if (LangOpts.FastMath || LangOpts.FiniteMathOnly)
  949. Builder.defineMacro("__FINITE_MATH_ONLY__", "1");
  950. else
  951. Builder.defineMacro("__FINITE_MATH_ONLY__", "0");
  952. if (LangOpts.GNUCVersion) {
  953. if (LangOpts.GNUInline || LangOpts.CPlusPlus)
  954. Builder.defineMacro("__GNUC_GNU_INLINE__");
  955. else
  956. Builder.defineMacro("__GNUC_STDC_INLINE__");
  957. // The value written by __atomic_test_and_set.
  958. // FIXME: This is target-dependent.
  959. Builder.defineMacro("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1");
  960. }
  961. auto addLockFreeMacros = [&](const llvm::Twine &Prefix) {
  962. // Used by libc++ and libstdc++ to implement ATOMIC_<foo>_LOCK_FREE.
  963. unsigned InlineWidthBits = TI.getMaxAtomicInlineWidth();
  964. #define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \
  965. Builder.defineMacro(Prefix + #TYPE "_LOCK_FREE", \
  966. getLockFreeValue(TI.get##Type##Width(), \
  967. TI.get##Type##Align(), \
  968. InlineWidthBits));
  969. DEFINE_LOCK_FREE_MACRO(BOOL, Bool);
  970. DEFINE_LOCK_FREE_MACRO(CHAR, Char);
  971. if (LangOpts.Char8)
  972. DEFINE_LOCK_FREE_MACRO(CHAR8_T, Char); // Treat char8_t like char.
  973. DEFINE_LOCK_FREE_MACRO(CHAR16_T, Char16);
  974. DEFINE_LOCK_FREE_MACRO(CHAR32_T, Char32);
  975. DEFINE_LOCK_FREE_MACRO(WCHAR_T, WChar);
  976. DEFINE_LOCK_FREE_MACRO(SHORT, Short);
  977. DEFINE_LOCK_FREE_MACRO(INT, Int);
  978. DEFINE_LOCK_FREE_MACRO(LONG, Long);
  979. DEFINE_LOCK_FREE_MACRO(LLONG, LongLong);
  980. Builder.defineMacro(Prefix + "POINTER_LOCK_FREE",
  981. getLockFreeValue(TI.getPointerWidth(0),
  982. TI.getPointerAlign(0),
  983. InlineWidthBits));
  984. #undef DEFINE_LOCK_FREE_MACRO
  985. };
  986. addLockFreeMacros("__CLANG_ATOMIC_");
  987. if (LangOpts.GNUCVersion)
  988. addLockFreeMacros("__GCC_ATOMIC_");
  989. if (LangOpts.NoInlineDefine)
  990. Builder.defineMacro("__NO_INLINE__");
  991. if (unsigned PICLevel = LangOpts.PICLevel) {
  992. Builder.defineMacro("__PIC__", Twine(PICLevel));
  993. Builder.defineMacro("__pic__", Twine(PICLevel));
  994. if (LangOpts.PIE) {
  995. Builder.defineMacro("__PIE__", Twine(PICLevel));
  996. Builder.defineMacro("__pie__", Twine(PICLevel));
  997. }
  998. }
  999. // Macros to control C99 numerics and <float.h>
  1000. Builder.defineMacro("__FLT_EVAL_METHOD__", Twine(TI.getFloatEvalMethod()));
  1001. Builder.defineMacro("__FLT_RADIX__", "2");
  1002. Builder.defineMacro("__DECIMAL_DIG__", "__LDBL_DECIMAL_DIG__");
  1003. if (LangOpts.getStackProtector() == LangOptions::SSPOn)
  1004. Builder.defineMacro("__SSP__");
  1005. else if (LangOpts.getStackProtector() == LangOptions::SSPStrong)
  1006. Builder.defineMacro("__SSP_STRONG__", "2");
  1007. else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
  1008. Builder.defineMacro("__SSP_ALL__", "3");
  1009. if (PPOpts.SetUpStaticAnalyzer)
  1010. Builder.defineMacro("__clang_analyzer__");
  1011. if (LangOpts.FastRelaxedMath)
  1012. Builder.defineMacro("__FAST_RELAXED_MATH__");
  1013. if (FEOpts.ProgramAction == frontend::RewriteObjC ||
  1014. LangOpts.getGC() != LangOptions::NonGC) {
  1015. Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
  1016. Builder.defineMacro("__strong", "__attribute__((objc_gc(strong)))");
  1017. Builder.defineMacro("__autoreleasing", "");
  1018. Builder.defineMacro("__unsafe_unretained", "");
  1019. } else if (LangOpts.ObjC) {
  1020. Builder.defineMacro("__weak", "__attribute__((objc_ownership(weak)))");
  1021. Builder.defineMacro("__strong", "__attribute__((objc_ownership(strong)))");
  1022. Builder.defineMacro("__autoreleasing",
  1023. "__attribute__((objc_ownership(autoreleasing)))");
  1024. Builder.defineMacro("__unsafe_unretained",
  1025. "__attribute__((objc_ownership(none)))");
  1026. }
  1027. // On Darwin, there are __double_underscored variants of the type
  1028. // nullability qualifiers.
  1029. if (TI.getTriple().isOSDarwin()) {
  1030. Builder.defineMacro("__nonnull", "_Nonnull");
  1031. Builder.defineMacro("__null_unspecified", "_Null_unspecified");
  1032. Builder.defineMacro("__nullable", "_Nullable");
  1033. }
  1034. // Add a macro to differentiate between regular iOS/tvOS/watchOS targets and
  1035. // the corresponding simulator targets.
  1036. if (TI.getTriple().isOSDarwin() && TI.getTriple().isSimulatorEnvironment())
  1037. Builder.defineMacro("__APPLE_EMBEDDED_SIMULATOR__", "1");
  1038. // OpenMP definition
  1039. // OpenMP 2.2:
  1040. // In implementations that support a preprocessor, the _OPENMP
  1041. // macro name is defined to have the decimal value yyyymm where
  1042. // yyyy and mm are the year and the month designations of the
  1043. // version of the OpenMP API that the implementation support.
  1044. if (!LangOpts.OpenMPSimd) {
  1045. switch (LangOpts.OpenMP) {
  1046. case 0:
  1047. break;
  1048. case 31:
  1049. Builder.defineMacro("_OPENMP", "201107");
  1050. break;
  1051. case 40:
  1052. Builder.defineMacro("_OPENMP", "201307");
  1053. break;
  1054. case 45:
  1055. Builder.defineMacro("_OPENMP", "201511");
  1056. break;
  1057. case 51:
  1058. Builder.defineMacro("_OPENMP", "202011");
  1059. break;
  1060. case 52:
  1061. Builder.defineMacro("_OPENMP", "202111");
  1062. break;
  1063. default:
  1064. // Default version is OpenMP 5.0
  1065. Builder.defineMacro("_OPENMP", "201811");
  1066. break;
  1067. }
  1068. }
  1069. // CUDA device path compilaton
  1070. if (LangOpts.CUDAIsDevice && !LangOpts.HIP) {
  1071. // The CUDA_ARCH value is set for the GPU target specified in the NVPTX
  1072. // backend's target defines.
  1073. Builder.defineMacro("__CUDA_ARCH__");
  1074. }
  1075. // We need to communicate this to our CUDA header wrapper, which in turn
  1076. // informs the proper CUDA headers of this choice.
  1077. if (LangOpts.CUDADeviceApproxTranscendentals || LangOpts.FastMath) {
  1078. Builder.defineMacro("__CLANG_CUDA_APPROX_TRANSCENDENTALS__");
  1079. }
  1080. // Define a macro indicating that the source file is being compiled with a
  1081. // SYCL device compiler which doesn't produce host binary.
  1082. if (LangOpts.SYCLIsDevice) {
  1083. Builder.defineMacro("__SYCL_DEVICE_ONLY__", "1");
  1084. }
  1085. // OpenCL definitions.
  1086. if (LangOpts.OpenCL) {
  1087. InitializeOpenCLFeatureTestMacros(TI, LangOpts, Builder);
  1088. if (TI.getTriple().isSPIR() || TI.getTriple().isSPIRV())
  1089. Builder.defineMacro("__IMAGE_SUPPORT__");
  1090. }
  1091. if (TI.hasInt128Type() && LangOpts.CPlusPlus && LangOpts.GNUMode) {
  1092. // For each extended integer type, g++ defines a macro mapping the
  1093. // index of the type (0 in this case) in some list of extended types
  1094. // to the type.
  1095. Builder.defineMacro("__GLIBCXX_TYPE_INT_N_0", "__int128");
  1096. Builder.defineMacro("__GLIBCXX_BITSIZE_INT_N_0", "128");
  1097. }
  1098. // Get other target #defines.
  1099. TI.getTargetDefines(LangOpts, Builder);
  1100. }
  1101. /// InitializePreprocessor - Initialize the preprocessor getting it and the
  1102. /// environment ready to process a single file. This returns true on error.
  1103. ///
  1104. void clang::InitializePreprocessor(
  1105. Preprocessor &PP, const PreprocessorOptions &InitOpts,
  1106. const PCHContainerReader &PCHContainerRdr,
  1107. const FrontendOptions &FEOpts) {
  1108. const LangOptions &LangOpts = PP.getLangOpts();
  1109. std::string PredefineBuffer;
  1110. PredefineBuffer.reserve(4080);
  1111. llvm::raw_string_ostream Predefines(PredefineBuffer);
  1112. MacroBuilder Builder(Predefines);
  1113. // Emit line markers for various builtin sections of the file. We don't do
  1114. // this in asm preprocessor mode, because "# 4" is not a line marker directive
  1115. // in this mode.
  1116. if (!PP.getLangOpts().AsmPreprocessor)
  1117. Builder.append("# 1 \"<built-in>\" 3");
  1118. // Install things like __POWERPC__, __GNUC__, etc into the macro table.
  1119. if (InitOpts.UsePredefines) {
  1120. // FIXME: This will create multiple definitions for most of the predefined
  1121. // macros. This is not the right way to handle this.
  1122. if ((LangOpts.CUDA || LangOpts.OpenMPIsDevice || LangOpts.SYCLIsDevice) &&
  1123. PP.getAuxTargetInfo())
  1124. InitializePredefinedMacros(*PP.getAuxTargetInfo(), LangOpts, FEOpts,
  1125. PP.getPreprocessorOpts(), Builder);
  1126. InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts,
  1127. PP.getPreprocessorOpts(), Builder);
  1128. // Install definitions to make Objective-C++ ARC work well with various
  1129. // C++ Standard Library implementations.
  1130. if (LangOpts.ObjC && LangOpts.CPlusPlus &&
  1131. (LangOpts.ObjCAutoRefCount || LangOpts.ObjCWeak)) {
  1132. switch (InitOpts.ObjCXXARCStandardLibrary) {
  1133. case ARCXX_nolib:
  1134. case ARCXX_libcxx:
  1135. break;
  1136. case ARCXX_libstdcxx:
  1137. AddObjCXXARCLibstdcxxDefines(LangOpts, Builder);
  1138. break;
  1139. }
  1140. }
  1141. }
  1142. // Even with predefines off, some macros are still predefined.
  1143. // These should all be defined in the preprocessor according to the
  1144. // current language configuration.
  1145. InitializeStandardPredefinedMacros(PP.getTargetInfo(), PP.getLangOpts(),
  1146. FEOpts, Builder);
  1147. // Add on the predefines from the driver. Wrap in a #line directive to report
  1148. // that they come from the command line.
  1149. if (!PP.getLangOpts().AsmPreprocessor)
  1150. Builder.append("# 1 \"<command line>\" 1");
  1151. // Process #define's and #undef's in the order they are given.
  1152. for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
  1153. if (InitOpts.Macros[i].second) // isUndef
  1154. Builder.undefineMacro(InitOpts.Macros[i].first);
  1155. else
  1156. DefineBuiltinMacro(Builder, InitOpts.Macros[i].first,
  1157. PP.getDiagnostics());
  1158. }
  1159. // Exit the command line and go back to <built-in> (2 is LC_LEAVE).
  1160. if (!PP.getLangOpts().AsmPreprocessor)
  1161. Builder.append("# 1 \"<built-in>\" 2");
  1162. // If -imacros are specified, include them now. These are processed before
  1163. // any -include directives.
  1164. for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
  1165. AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i]);
  1166. // Process -include-pch/-include-pth directives.
  1167. if (!InitOpts.ImplicitPCHInclude.empty())
  1168. AddImplicitIncludePCH(Builder, PP, PCHContainerRdr,
  1169. InitOpts.ImplicitPCHInclude);
  1170. // Process -include directives.
  1171. for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
  1172. const std::string &Path = InitOpts.Includes[i];
  1173. AddImplicitInclude(Builder, Path);
  1174. }
  1175. // Instruct the preprocessor to skip the preamble.
  1176. PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first,
  1177. InitOpts.PrecompiledPreambleBytes.second);
  1178. // Copy PredefinedBuffer into the Preprocessor.
  1179. PP.setPredefines(Predefines.str());
  1180. }