InitPreprocessor.cpp 42 KB

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