InitPreprocessor.cpp 59 KB

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