InitPreprocessor.cpp 46 KB

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