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