Chris Lattner | e116ccf | 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 | |
Daniel Dunbar | 8863b98 | 2009-11-07 04:20:15 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/Utils.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "clang/Basic/FileManager.h" |
Chandler Carruth | 103b71c | 2010-01-20 06:13:02 +0000 | [diff] [blame] | 16 | #include "clang/Basic/MacroBuilder.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Version.h" |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/FrontendDiagnostic.h" |
Fariborz Jahanian | 7d95747 | 2010-01-13 18:51:17 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/FrontendOptions.h" |
Chandler Carruth | cb381ea | 2011-12-09 01:33:57 +0000 | [diff] [blame] | 22 | #include "clang/Lex/HeaderSearch.h" |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 14e71f0 | 2012-10-24 17:01:35 +0000 | [diff] [blame] | 24 | #include "clang/Lex/PreprocessorOptions.h" |
Douglas Gregor | 4ec429d | 2012-10-22 23:59:45 +0000 | [diff] [blame] | 25 | #include "clang/Serialization/ASTReader.h" |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/APFloat.h" |
Michael J. Spencer | fbfd180 | 2010-12-21 16:45:57 +0000 | [diff] [blame] | 27 | #include "llvm/Support/FileSystem.h" |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Path.h" |
Chris Lattner | 47c06ee | 2009-11-02 21:48:09 +0000 | [diff] [blame] | 30 | using namespace clang; |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 31 | |
Eli Friedman | 62c90e1 | 2013-08-28 20:35:38 +0000 | [diff] [blame] | 32 | static bool MacroBodyEndsInBackslash(StringRef MacroBody) { |
| 33 | while (!MacroBody.empty() && isWhitespace(MacroBody.back())) |
| 34 | MacroBody = MacroBody.drop_back(); |
| 35 | return !MacroBody.empty() && MacroBody.back() == '\\'; |
| 36 | } |
| 37 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 38 | // Append a #define line to Buf for Macro. Macro should be of the form XXX, |
| 39 | // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit |
| 40 | // "#define XXX Y z W". To get a #define with no value, use "XXX=". |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 41 | static void DefineBuiltinMacro(MacroBuilder &Builder, StringRef Macro, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 42 | DiagnosticsEngine &Diags) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 43 | std::pair<StringRef, StringRef> MacroPair = Macro.split('='); |
| 44 | StringRef MacroName = MacroPair.first; |
| 45 | StringRef MacroBody = MacroPair.second; |
Daniel Dunbar | 1ee4b9e | 2010-01-10 00:46:21 +0000 | [diff] [blame] | 46 | if (MacroName.size() != Macro.size()) { |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 47 | // Per GCC -D semantics, the macro ends at \n if it exists. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 48 | StringRef::size_type End = MacroBody.find_first_of("\n\r"); |
| 49 | if (End != StringRef::npos) |
Daniel Dunbar | 1ee4b9e | 2010-01-10 00:46:21 +0000 | [diff] [blame] | 50 | Diags.Report(diag::warn_fe_macro_contains_embedded_newline) |
Kovarththanan Rajaratnam | 8746e4e | 2010-01-09 09:27:11 +0000 | [diff] [blame] | 51 | << MacroName; |
Eli Friedman | 62c90e1 | 2013-08-28 20:35:38 +0000 | [diff] [blame] | 52 | MacroBody = MacroBody.substr(0, End); |
| 53 | // We handle macro bodies which end in a backslash by appending an extra |
| 54 | // backslash+newline. This makes sure we don't accidentally treat the |
| 55 | // backslash as a line continuation marker. |
| 56 | if (MacroBodyEndsInBackslash(MacroBody)) |
| 57 | Builder.defineMacro(MacroName, Twine(MacroBody) + "\\\n"); |
| 58 | else |
| 59 | Builder.defineMacro(MacroName, MacroBody); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 60 | } else { |
| 61 | // Push "macroname 1". |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 62 | Builder.defineMacro(Macro); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 63 | } |
Chris Lattner | dcdecf4 | 2009-05-15 16:08:43 +0000 | [diff] [blame] | 64 | } |
| 65 | |
James Dennett | 809d1be | 2012-06-13 22:07:09 +0000 | [diff] [blame] | 66 | /// AddImplicitInclude - Add an implicit \#include of the specified file to the |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 67 | /// predefines buffer. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 68 | static void AddImplicitInclude(MacroBuilder &Builder, StringRef File, |
Nick Lewycky | 277a6e7 | 2011-02-23 21:16:44 +0000 | [diff] [blame] | 69 | FileManager &FileMgr) { |
Chandler Carruth | cb381ea | 2011-12-09 01:33:57 +0000 | [diff] [blame] | 70 | Builder.append(Twine("#include \"") + |
| 71 | HeaderSearch::NormalizeDashIncludePath(File, FileMgr) + "\""); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 74 | static void AddImplicitIncludeMacros(MacroBuilder &Builder, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 75 | StringRef File, |
Nick Lewycky | 277a6e7 | 2011-02-23 21:16:44 +0000 | [diff] [blame] | 76 | FileManager &FileMgr) { |
Chandler Carruth | cb381ea | 2011-12-09 01:33:57 +0000 | [diff] [blame] | 77 | Builder.append(Twine("#__include_macros \"") + |
| 78 | HeaderSearch::NormalizeDashIncludePath(File, FileMgr) + "\""); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 79 | // Marker token to stop the __include_macros fetch loop. |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 80 | Builder.append("##"); // ##? |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 81 | } |
| 82 | |
James Dennett | 809d1be | 2012-06-13 22:07:09 +0000 | [diff] [blame] | 83 | /// AddImplicitIncludePTH - Add an implicit \#include using the original file |
| 84 | /// used to generate a PTH cache. |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 85 | static void AddImplicitIncludePTH(MacroBuilder &Builder, Preprocessor &PP, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 86 | StringRef ImplicitIncludePTH) { |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 87 | PTHManager *P = PP.getPTHManager(); |
Ted Kremenek | 4ae4c91 | 2010-06-28 20:32:40 +0000 | [diff] [blame] | 88 | // Null check 'P' in the corner case where it couldn't be created. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 89 | const char *OriginalFile = P ? P->getOriginalSourceFile() : nullptr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 91 | if (!OriginalFile) { |
Daniel Dunbar | baac103 | 2009-12-03 09:14:12 +0000 | [diff] [blame] | 92 | PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header) |
| 93 | << ImplicitIncludePTH; |
| 94 | return; |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 95 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
Nick Lewycky | 277a6e7 | 2011-02-23 21:16:44 +0000 | [diff] [blame] | 97 | AddImplicitInclude(Builder, OriginalFile, PP.getFileManager()); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Douglas Gregor | 4ec429d | 2012-10-22 23:59:45 +0000 | [diff] [blame] | 100 | /// \brief Add an implicit \#include using the original file used to generate |
| 101 | /// a PCH file. |
| 102 | static void AddImplicitIncludePCH(MacroBuilder &Builder, Preprocessor &PP, |
| 103 | StringRef ImplicitIncludePCH) { |
| 104 | std::string OriginalFile = |
| 105 | ASTReader::getOriginalSourceFile(ImplicitIncludePCH, PP.getFileManager(), |
| 106 | PP.getDiagnostics()); |
| 107 | if (OriginalFile.empty()) |
| 108 | return; |
| 109 | |
| 110 | AddImplicitInclude(Builder, OriginalFile, PP.getFileManager()); |
| 111 | } |
| 112 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 113 | /// PickFP - This is used to pick a value based on the FP semantics of the |
| 114 | /// specified FP model. |
| 115 | template <typename T> |
| 116 | static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal, |
Eli Friedman | 2665a75 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 117 | T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal, |
| 118 | T IEEEQuadVal) { |
Duncan Sands | 63682f9 | 2009-06-03 14:28:20 +0000 | [diff] [blame] | 119 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle) |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 120 | return IEEESingleVal; |
Duncan Sands | 63682f9 | 2009-06-03 14:28:20 +0000 | [diff] [blame] | 121 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble) |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 122 | return IEEEDoubleVal; |
Duncan Sands | 63682f9 | 2009-06-03 14:28:20 +0000 | [diff] [blame] | 123 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended) |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 124 | return X87DoubleExtendedVal; |
Duncan Sands | 63682f9 | 2009-06-03 14:28:20 +0000 | [diff] [blame] | 125 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble) |
Eli Friedman | 2665a75 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 126 | return PPCDoubleDoubleVal; |
Duncan Sands | 63682f9 | 2009-06-03 14:28:20 +0000 | [diff] [blame] | 127 | assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad); |
Eli Friedman | 2665a75 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 128 | return IEEEQuadVal; |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 131 | static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix, |
Eli Friedman | 75e8efe | 2012-11-10 00:20:38 +0000 | [diff] [blame] | 132 | const llvm::fltSemantics *Sem, StringRef Ext) { |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 133 | const char *DenormMin, *Epsilon, *Max, *Min; |
Eli Friedman | 75e8efe | 2012-11-10 00:20:38 +0000 | [diff] [blame] | 134 | DenormMin = PickFP(Sem, "1.40129846e-45", "4.9406564584124654e-324", |
| 135 | "3.64519953188247460253e-4951", |
| 136 | "4.94065645841246544176568792868221e-324", |
| 137 | "6.47517511943802511092443895822764655e-4966"); |
Eli Friedman | 2665a75 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 138 | int Digits = PickFP(Sem, 6, 15, 18, 31, 33); |
Eli Friedman | 75e8efe | 2012-11-10 00:20:38 +0000 | [diff] [blame] | 139 | Epsilon = PickFP(Sem, "1.19209290e-7", "2.2204460492503131e-16", |
| 140 | "1.08420217248550443401e-19", |
| 141 | "4.94065645841246544176568792868221e-324", |
| 142 | "1.92592994438723585305597794258492732e-34"); |
Eli Friedman | 2665a75 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 143 | int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113); |
| 144 | int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931); |
| 145 | int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932); |
| 146 | int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381); |
| 147 | int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384); |
Eli Friedman | 75e8efe | 2012-11-10 00:20:38 +0000 | [diff] [blame] | 148 | Min = PickFP(Sem, "1.17549435e-38", "2.2250738585072014e-308", |
| 149 | "3.36210314311209350626e-4932", |
| 150 | "2.00416836000897277799610805135016e-292", |
| 151 | "3.36210314311209350626267781732175260e-4932"); |
| 152 | Max = PickFP(Sem, "3.40282347e+38", "1.7976931348623157e+308", |
| 153 | "1.18973149535723176502e+4932", |
| 154 | "1.79769313486231580793728971405301e+308", |
| 155 | "1.18973149535723176508575932662800702e+4932"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 157 | SmallString<32> DefPrefix; |
Daniel Dunbar | 066515f | 2010-01-20 06:09:53 +0000 | [diff] [blame] | 158 | DefPrefix = "__"; |
| 159 | DefPrefix += Prefix; |
| 160 | DefPrefix += "_"; |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 161 | |
Eli Friedman | 75e8efe | 2012-11-10 00:20:38 +0000 | [diff] [blame] | 162 | Builder.defineMacro(DefPrefix + "DENORM_MIN__", Twine(DenormMin)+Ext); |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 163 | Builder.defineMacro(DefPrefix + "HAS_DENORM__"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 164 | Builder.defineMacro(DefPrefix + "DIG__", Twine(Digits)); |
Eli Friedman | 75e8efe | 2012-11-10 00:20:38 +0000 | [diff] [blame] | 165 | Builder.defineMacro(DefPrefix + "EPSILON__", Twine(Epsilon)+Ext); |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 166 | Builder.defineMacro(DefPrefix + "HAS_INFINITY__"); |
| 167 | Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 168 | Builder.defineMacro(DefPrefix + "MANT_DIG__", Twine(MantissaDigits)); |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 170 | Builder.defineMacro(DefPrefix + "MAX_10_EXP__", Twine(Max10Exp)); |
| 171 | Builder.defineMacro(DefPrefix + "MAX_EXP__", Twine(MaxExp)); |
Eli Friedman | 75e8efe | 2012-11-10 00:20:38 +0000 | [diff] [blame] | 172 | Builder.defineMacro(DefPrefix + "MAX__", Twine(Max)+Ext); |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 174 | Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+Twine(Min10Exp)+")"); |
| 175 | Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+Twine(MinExp)+")"); |
Eli Friedman | 75e8efe | 2012-11-10 00:20:38 +0000 | [diff] [blame] | 176 | Builder.defineMacro(DefPrefix + "MIN__", Twine(Min)+Ext); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | |
| 180 | /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro |
| 181 | /// named MacroName with the max value for a type with width 'TypeWidth' a |
| 182 | /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 183 | static void DefineTypeSize(StringRef MacroName, unsigned TypeWidth, |
| 184 | StringRef ValSuffix, bool isSigned, |
Chris Lattner | d7f758c | 2011-02-24 06:54:56 +0000 | [diff] [blame] | 185 | MacroBuilder &Builder) { |
| 186 | llvm::APInt MaxVal = isSigned ? llvm::APInt::getSignedMaxValue(TypeWidth) |
| 187 | : llvm::APInt::getMaxValue(TypeWidth); |
| 188 | Builder.defineMacro(MacroName, MaxVal.toString(10, isSigned) + ValSuffix); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Chris Lattner | 9099e7b | 2009-11-05 21:21:32 +0000 | [diff] [blame] | 191 | /// DefineTypeSize - An overloaded helper that uses TargetInfo to determine |
| 192 | /// the width, suffix, and signedness of the given type |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 193 | static void DefineTypeSize(StringRef MacroName, TargetInfo::IntType Ty, |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 194 | const TargetInfo &TI, MacroBuilder &Builder) { |
Chris Lattner | 9099e7b | 2009-11-05 21:21:32 +0000 | [diff] [blame] | 195 | DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty), |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 196 | TI.isTypeSigned(Ty), Builder); |
Chris Lattner | 9099e7b | 2009-11-05 21:21:32 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 199 | static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty, |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 200 | MacroBuilder &Builder) { |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 201 | Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty)); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 204 | static void DefineTypeWidth(StringRef MacroName, TargetInfo::IntType Ty, |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 205 | const TargetInfo &TI, MacroBuilder &Builder) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 206 | Builder.defineMacro(MacroName, Twine(TI.getTypeWidth(Ty))); |
Ken Dyck | 186696b | 2009-11-18 13:52:57 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 209 | static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth, |
Dan Gohman | cfeac34 | 2010-05-28 00:27:15 +0000 | [diff] [blame] | 210 | const TargetInfo &TI, MacroBuilder &Builder) { |
| 211 | Builder.defineMacro(MacroName, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 212 | Twine(BitWidth / TI.getCharWidth())); |
Dan Gohman | cfeac34 | 2010-05-28 00:27:15 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Chris Lattner | dcdd2a0 | 2009-11-12 08:08:27 +0000 | [diff] [blame] | 215 | static void DefineExactWidthIntType(TargetInfo::IntType Ty, |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 216 | const TargetInfo &TI, MacroBuilder &Builder) { |
Ken Dyck | eef22ef | 2009-11-16 16:36:33 +0000 | [diff] [blame] | 217 | int TypeWidth = TI.getTypeWidth(Ty); |
Daniel Dunbar | 7f3a545 | 2010-06-30 06:30:56 +0000 | [diff] [blame] | 218 | |
| 219 | // Use the target specified int64 type, when appropriate, so that [u]int64_t |
| 220 | // ends up being defined in terms of the correct type. |
| 221 | if (TypeWidth == 64) |
| 222 | Ty = TI.getInt64Type(); |
| 223 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 224 | DefineType("__INT" + Twine(TypeWidth) + "_TYPE__", Ty, Builder); |
Ken Dyck | eef22ef | 2009-11-16 16:36:33 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 226 | StringRef ConstSuffix(TargetInfo::getTypeConstantSuffix(Ty)); |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 227 | if (!ConstSuffix.empty()) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 228 | Builder.defineMacro("__INT" + Twine(TypeWidth) + "_C_SUFFIX__", |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 229 | ConstSuffix); |
Chris Lattner | dcdd2a0 | 2009-11-12 08:08:27 +0000 | [diff] [blame] | 230 | } |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 231 | |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 232 | /// Get the value the ATOMIC_*_LOCK_FREE macro should have for a type with |
| 233 | /// the specified properties. |
| 234 | static const char *getLockFreeValue(unsigned TypeWidth, unsigned TypeAlign, |
| 235 | unsigned InlineWidth) { |
| 236 | // Fully-aligned, power-of-2 sizes no larger than the inline |
| 237 | // width will be inlined as lock-free operations. |
| 238 | if (TypeWidth == TypeAlign && (TypeWidth & (TypeWidth - 1)) == 0 && |
| 239 | TypeWidth <= InlineWidth) |
| 240 | return "2"; // "always lock free" |
| 241 | // We cannot be certain what operations the lib calls might be |
| 242 | // able to implement as lock-free on future processors. |
| 243 | return "1"; // "sometimes lock free" |
| 244 | } |
| 245 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 246 | /// \brief Add definitions required for a smooth interaction between |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 247 | /// Objective-C++ automated reference counting and libstdc++ (4.2). |
| 248 | static void AddObjCXXARCLibstdcxxDefines(const LangOptions &LangOpts, |
| 249 | MacroBuilder &Builder) { |
| 250 | Builder.defineMacro("_GLIBCXX_PREDEFINED_OBJC_ARC_IS_SCALAR"); |
| 251 | |
| 252 | std::string Result; |
| 253 | { |
| 254 | // Provide specializations for the __is_scalar type trait so that |
| 255 | // lifetime-qualified objects are not considered "scalar" types, which |
| 256 | // libstdc++ uses as an indicator of the presence of trivial copy, assign, |
| 257 | // default-construct, and destruct semantics (none of which hold for |
| 258 | // lifetime-qualified objects in ARC). |
| 259 | llvm::raw_string_ostream Out(Result); |
| 260 | |
| 261 | Out << "namespace std {\n" |
| 262 | << "\n" |
| 263 | << "struct __true_type;\n" |
| 264 | << "struct __false_type;\n" |
| 265 | << "\n"; |
| 266 | |
| 267 | Out << "template<typename _Tp> struct __is_scalar;\n" |
| 268 | << "\n"; |
| 269 | |
| 270 | Out << "template<typename _Tp>\n" |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 271 | << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> {\n" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 272 | << " enum { __value = 0 };\n" |
| 273 | << " typedef __false_type __type;\n" |
| 274 | << "};\n" |
| 275 | << "\n"; |
| 276 | |
John McCall | 0a7dd78 | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 277 | if (LangOpts.ObjCARCWeak) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 278 | Out << "template<typename _Tp>\n" |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 279 | << "struct __is_scalar<__attribute__((objc_ownership(weak))) _Tp> {\n" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 280 | << " enum { __value = 0 };\n" |
| 281 | << " typedef __false_type __type;\n" |
| 282 | << "};\n" |
| 283 | << "\n"; |
| 284 | } |
| 285 | |
| 286 | Out << "template<typename _Tp>\n" |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 287 | << "struct __is_scalar<__attribute__((objc_ownership(autoreleasing)))" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 288 | << " _Tp> {\n" |
| 289 | << " enum { __value = 0 };\n" |
| 290 | << " typedef __false_type __type;\n" |
| 291 | << "};\n" |
| 292 | << "\n"; |
| 293 | |
| 294 | Out << "}\n"; |
| 295 | } |
| 296 | Builder.append(Result); |
| 297 | } |
| 298 | |
Nick Lewycky | a9c6441 | 2011-06-07 06:07:12 +0000 | [diff] [blame] | 299 | static void InitializeStandardPredefinedMacros(const TargetInfo &TI, |
| 300 | const LangOptions &LangOpts, |
| 301 | const FrontendOptions &FEOpts, |
| 302 | MacroBuilder &Builder) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 303 | if (!LangOpts.MSVCCompat && !LangOpts.TraditionalCPP) |
Nick Lewycky | a9c6441 | 2011-06-07 06:07:12 +0000 | [diff] [blame] | 304 | Builder.defineMacro("__STDC__"); |
| 305 | if (LangOpts.Freestanding) |
| 306 | Builder.defineMacro("__STDC_HOSTED__", "0"); |
| 307 | else |
| 308 | Builder.defineMacro("__STDC_HOSTED__"); |
| 309 | |
| 310 | if (!LangOpts.CPlusPlus) { |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 311 | if (LangOpts.C11) |
Benjamin Kramer | 430cf51 | 2011-12-23 17:00:41 +0000 | [diff] [blame] | 312 | Builder.defineMacro("__STDC_VERSION__", "201112L"); |
Douglas Gregor | 5aa6dea | 2011-10-28 23:02:54 +0000 | [diff] [blame] | 313 | else if (LangOpts.C99) |
Nick Lewycky | a9c6441 | 2011-06-07 06:07:12 +0000 | [diff] [blame] | 314 | Builder.defineMacro("__STDC_VERSION__", "199901L"); |
| 315 | else if (!LangOpts.GNUMode && LangOpts.Digraphs) |
| 316 | Builder.defineMacro("__STDC_VERSION__", "199409L"); |
| 317 | } else { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 318 | // C++1y [cpp.predefined]p1: |
| 319 | // The name __cplusplus is defined to the value 201402L when compiling a |
| 320 | // C++ translation unit. |
| 321 | if (LangOpts.CPlusPlus1y) |
| 322 | Builder.defineMacro("__cplusplus", "201402L"); |
Richard Smith | 0513448 | 2012-05-03 22:18:20 +0000 | [diff] [blame] | 323 | // C++11 [cpp.predefined]p1: |
| 324 | // The name __cplusplus is defined to the value 201103L when compiling a |
| 325 | // C++ translation unit. |
Richard Smith | 7f0ffb3 | 2013-05-07 19:32:56 +0000 | [diff] [blame] | 326 | else if (LangOpts.CPlusPlus11) |
Richard Smith | 0513448 | 2012-05-03 22:18:20 +0000 | [diff] [blame] | 327 | Builder.defineMacro("__cplusplus", "201103L"); |
| 328 | // C++03 [cpp.predefined]p1: |
| 329 | // The name __cplusplus is defined to the value 199711L when compiling a |
| 330 | // C++ translation unit. |
| 331 | else |
| 332 | Builder.defineMacro("__cplusplus", "199711L"); |
Nick Lewycky | a9c6441 | 2011-06-07 06:07:12 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Ed Schouten | 5ada7a5 | 2013-09-29 07:54:52 +0000 | [diff] [blame] | 335 | // In C11 these are environment macros. In C++11 they are only defined |
| 336 | // as part of <cuchar>. To prevent breakage when mixing C and C++ |
| 337 | // code, define these macros unconditionally. We can define them |
| 338 | // unconditionally, as Clang always uses UTF-16 and UTF-32 for 16-bit |
| 339 | // and 32-bit character literals. |
| 340 | Builder.defineMacro("__STDC_UTF_16__", "1"); |
| 341 | Builder.defineMacro("__STDC_UTF_32__", "1"); |
| 342 | |
Nick Lewycky | 1464809 | 2011-06-10 20:56:43 +0000 | [diff] [blame] | 343 | if (LangOpts.ObjC1) |
| 344 | Builder.defineMacro("__OBJC__"); |
| 345 | |
Nick Lewycky | a9c6441 | 2011-06-07 06:07:12 +0000 | [diff] [blame] | 346 | // Not "standard" per se, but available even with the -undef flag. |
| 347 | if (LangOpts.AsmPreprocessor) |
| 348 | Builder.defineMacro("__ASSEMBLER__"); |
| 349 | } |
| 350 | |
Bill Wendling | c78e5b3 | 2013-11-28 00:33:42 +0000 | [diff] [blame] | 351 | /// Initialize the predefined C++ language feature test macros defined in |
| 352 | /// ISO/IEC JTC1/SC22/WG21 (C++) SD-6: "SG10 Feature Test Recommendations". |
| 353 | static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts, |
| 354 | MacroBuilder &Builder) { |
| 355 | // C++11 features. |
| 356 | if (LangOpts.CPlusPlus11) { |
| 357 | Builder.defineMacro("__cpp_unicode_characters", "200704"); |
| 358 | Builder.defineMacro("__cpp_raw_strings", "200710"); |
| 359 | Builder.defineMacro("__cpp_unicode_literals", "200710"); |
| 360 | Builder.defineMacro("__cpp_user_defined_literals", "200809"); |
| 361 | Builder.defineMacro("__cpp_lambdas", "200907"); |
| 362 | Builder.defineMacro("__cpp_constexpr", |
| 363 | LangOpts.CPlusPlus1y ? "201304" : "200704"); |
| 364 | Builder.defineMacro("__cpp_static_assert", "200410"); |
| 365 | Builder.defineMacro("__cpp_decltype", "200707"); |
| 366 | Builder.defineMacro("__cpp_attributes", "200809"); |
| 367 | Builder.defineMacro("__cpp_rvalue_references", "200610"); |
| 368 | Builder.defineMacro("__cpp_variadic_templates", "200704"); |
| 369 | } |
| 370 | |
| 371 | // C++14 features. |
| 372 | if (LangOpts.CPlusPlus1y) { |
| 373 | Builder.defineMacro("__cpp_binary_literals", "201304"); |
| 374 | Builder.defineMacro("__cpp_init_captures", "201304"); |
| 375 | Builder.defineMacro("__cpp_generic_lambdas", "201304"); |
| 376 | Builder.defineMacro("__cpp_decltype_auto", "201304"); |
| 377 | Builder.defineMacro("__cpp_return_type_deduction", "201304"); |
| 378 | Builder.defineMacro("__cpp_aggregate_nsdmi", "201304"); |
| 379 | Builder.defineMacro("__cpp_variable_templates", "201304"); |
| 380 | } |
| 381 | } |
| 382 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 383 | static void InitializePredefinedMacros(const TargetInfo &TI, |
| 384 | const LangOptions &LangOpts, |
Fariborz Jahanian | 7d95747 | 2010-01-13 18:51:17 +0000 | [diff] [blame] | 385 | const FrontendOptions &FEOpts, |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 386 | MacroBuilder &Builder) { |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 387 | // Compiler version introspection macros. |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 388 | Builder.defineMacro("__llvm__"); // LLVM Backend |
| 389 | Builder.defineMacro("__clang__"); // Clang Frontend |
Douglas Gregor | 4290fbd | 2010-04-30 02:51:06 +0000 | [diff] [blame] | 390 | #define TOSTR2(X) #X |
| 391 | #define TOSTR(X) TOSTR2(X) |
| 392 | Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR)); |
| 393 | Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR)); |
| 394 | #ifdef CLANG_VERSION_PATCHLEVEL |
| 395 | Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL)); |
| 396 | #else |
| 397 | Builder.defineMacro("__clang_patchlevel__", "0"); |
| 398 | #endif |
| 399 | Builder.defineMacro("__clang_version__", |
Benjamin Kramer | d7ee194 | 2012-10-08 18:49:39 +0000 | [diff] [blame] | 400 | "\"" CLANG_VERSION_STRING " " |
| 401 | + getClangFullRepositoryVersion() + "\""); |
Douglas Gregor | 4290fbd | 2010-04-30 02:51:06 +0000 | [diff] [blame] | 402 | #undef TOSTR |
| 403 | #undef TOSTR2 |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 404 | if (!LangOpts.MSVCCompat) { |
Aaron Ballman | 5b31d55 | 2012-03-10 22:21:14 +0000 | [diff] [blame] | 405 | // Currently claim to be compatible with GCC 4.2.1-5621, but only if we're |
| 406 | // not compiling for MSVC compatibility |
| 407 | Builder.defineMacro("__GNUC_MINOR__", "2"); |
| 408 | Builder.defineMacro("__GNUC_PATCHLEVEL__", "1"); |
| 409 | Builder.defineMacro("__GNUC__", "4"); |
| 410 | Builder.defineMacro("__GXX_ABI_VERSION", "1002"); |
| 411 | } |
Daniel Dunbar | ad1a4c6 | 2011-03-31 00:53:51 +0000 | [diff] [blame] | 412 | |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 413 | // Define macros for the C11 / C++11 memory orderings |
| 414 | Builder.defineMacro("__ATOMIC_RELAXED", "0"); |
| 415 | Builder.defineMacro("__ATOMIC_CONSUME", "1"); |
| 416 | Builder.defineMacro("__ATOMIC_ACQUIRE", "2"); |
| 417 | Builder.defineMacro("__ATOMIC_RELEASE", "3"); |
| 418 | Builder.defineMacro("__ATOMIC_ACQ_REL", "4"); |
| 419 | Builder.defineMacro("__ATOMIC_SEQ_CST", "5"); |
| 420 | |
David Chisnall | 5f3c163 | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 421 | // Support for #pragma redefine_extname (Sun compatibility) |
| 422 | Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1"); |
| 423 | |
Daniel Dunbar | ad1a4c6 | 2011-03-31 00:53:51 +0000 | [diff] [blame] | 424 | // As sad as it is, enough software depends on the __VERSION__ for version |
| 425 | // checks that it is necessary to report 4.2.1 (the base GCC version we claim |
| 426 | // compatibility with) first. |
| 427 | Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible " + |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 428 | Twine(getClangFullCPPVersion()) + "\""); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 430 | // Initialize language-specific preprocessor defines. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 432 | // Standard conforming mode? |
| 433 | if (!LangOpts.GNUMode) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 434 | Builder.defineMacro("__STRICT_ANSI__"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 436 | if (LangOpts.CPlusPlus11) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 437 | Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 438 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 439 | if (LangOpts.ObjC1) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 440 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 441 | Builder.defineMacro("__OBJC2__"); |
Douglas Gregor | eced60c | 2011-09-12 15:17:19 +0000 | [diff] [blame] | 442 | |
| 443 | if (LangOpts.ObjCExceptions) |
| 444 | Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 447 | if (LangOpts.getGC() != LangOptions::NonGC) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 448 | Builder.defineMacro("__OBJC_GC__"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 449 | |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 450 | if (LangOpts.ObjCRuntime.isNeXTFamily()) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 451 | Builder.defineMacro("__NEXT_RUNTIME__"); |
Ted Kremenek | 3065cf9 | 2012-06-19 00:37:39 +0000 | [diff] [blame] | 452 | |
Benjamin Kramer | 585c84c | 2013-09-16 16:31:49 +0000 | [diff] [blame] | 453 | if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::ObjFW) { |
| 454 | VersionTuple tuple = LangOpts.ObjCRuntime.getVersion(); |
| 455 | |
| 456 | unsigned minor = 0; |
| 457 | if (tuple.getMinor().hasValue()) |
| 458 | minor = tuple.getMinor().getValue(); |
| 459 | |
| 460 | unsigned subminor = 0; |
| 461 | if (tuple.getSubminor().hasValue()) |
| 462 | subminor = tuple.getSubminor().getValue(); |
| 463 | |
| 464 | Builder.defineMacro("__OBJFW_RUNTIME_ABI__", |
| 465 | Twine(tuple.getMajor() * 10000 + minor * 100 + |
| 466 | subminor)); |
| 467 | } |
| 468 | |
Ted Kremenek | 3065cf9 | 2012-06-19 00:37:39 +0000 | [diff] [blame] | 469 | Builder.defineMacro("IBOutlet", "__attribute__((iboutlet))"); |
| 470 | Builder.defineMacro("IBOutletCollection(ClassName)", |
| 471 | "__attribute__((iboutletcollection(ClassName)))"); |
| 472 | Builder.defineMacro("IBAction", "void)__attribute__((ibaction)"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 473 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
Bill Wendling | c78e5b3 | 2013-11-28 00:33:42 +0000 | [diff] [blame] | 475 | if (LangOpts.CPlusPlus) |
| 476 | InitializeCPlusPlusFeatureTestMacros(LangOpts, Builder); |
| 477 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 478 | // darwin_constant_cfstrings controls this. This is also dependent |
| 479 | // on other things like the runtime I believe. This is set even for C code. |
Fariborz Jahanian | 16c3eae | 2011-07-05 16:00:59 +0000 | [diff] [blame] | 480 | if (!LangOpts.NoConstantCFStrings) |
| 481 | Builder.defineMacro("__CONSTANT_CFSTRINGS__"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 483 | if (LangOpts.ObjC2) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 484 | Builder.defineMacro("OBJC_NEW_PROPERTIES"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 485 | |
| 486 | if (LangOpts.PascalStrings) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 487 | Builder.defineMacro("__PASCAL_STRINGS__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 488 | |
| 489 | if (LangOpts.Blocks) { |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 490 | Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))"); |
| 491 | Builder.defineMacro("__BLOCKS__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 492 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | |
Douglas Gregor | eced60c | 2011-09-12 15:17:19 +0000 | [diff] [blame] | 494 | if (LangOpts.CXXExceptions) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 495 | Builder.defineMacro("__EXCEPTIONS"); |
Dan Gohman | cfeac34 | 2010-05-28 00:27:15 +0000 | [diff] [blame] | 496 | if (LangOpts.RTTI) |
| 497 | Builder.defineMacro("__GXX_RTTI"); |
Daniel Dunbar | b2987d1 | 2010-02-10 18:49:11 +0000 | [diff] [blame] | 498 | if (LangOpts.SjLjExceptions) |
| 499 | Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__"); |
Rafael Espindola | f759df0 | 2009-10-01 13:33:33 +0000 | [diff] [blame] | 500 | |
Chandler Carruth | f8c247d | 2011-04-23 19:48:40 +0000 | [diff] [blame] | 501 | if (LangOpts.Deprecated) |
| 502 | Builder.defineMacro("__DEPRECATED"); |
| 503 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 504 | if (LangOpts.CPlusPlus) { |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 505 | Builder.defineMacro("__GNUG__", "4"); |
| 506 | Builder.defineMacro("__GXX_WEAK__"); |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 507 | Builder.defineMacro("__private_extern__", "extern"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 508 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 510 | if (LangOpts.MicrosoftExt) { |
Abramo Bagnara | 5b86ffd | 2012-09-05 17:30:57 +0000 | [diff] [blame] | 511 | if (LangOpts.WChar) { |
| 512 | // wchar_t supported as a keyword. |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 513 | Builder.defineMacro("_WCHAR_T_DEFINED"); |
| 514 | Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED"); |
Abramo Bagnara | 5b86ffd | 2012-09-05 17:30:57 +0000 | [diff] [blame] | 515 | } |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 516 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 518 | if (LangOpts.Optimize) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 519 | Builder.defineMacro("__OPTIMIZE__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 520 | if (LangOpts.OptimizeSize) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 521 | Builder.defineMacro("__OPTIMIZE_SIZE__"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | |
Chandler Carruth | ba9186c | 2012-01-03 02:46:46 +0000 | [diff] [blame] | 523 | if (LangOpts.FastMath) |
| 524 | Builder.defineMacro("__FAST_MATH__"); |
| 525 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 526 | // Initialize target-specific preprocessor defines. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 527 | |
Dylan Noblesmith | 5aeda88 | 2012-08-08 16:09:12 +0000 | [diff] [blame] | 528 | // __BYTE_ORDER__ was added in GCC 4.6. It's analogous |
| 529 | // to the macro __BYTE_ORDER (no trailing underscores) |
| 530 | // from glibc's <endian.h> header. |
Dylan Noblesmith | 3b198a9 | 2012-07-27 18:34:31 +0000 | [diff] [blame] | 531 | // We don't support the PDP-11 as a target, but include |
| 532 | // the define so it can still be compared against. |
| 533 | Builder.defineMacro("__ORDER_LITTLE_ENDIAN__", "1234"); |
| 534 | Builder.defineMacro("__ORDER_BIG_ENDIAN__", "4321"); |
| 535 | Builder.defineMacro("__ORDER_PDP_ENDIAN__", "3412"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 536 | if (TI.isBigEndian()) { |
Dylan Noblesmith | 3b198a9 | 2012-07-27 18:34:31 +0000 | [diff] [blame] | 537 | Builder.defineMacro("__BYTE_ORDER__", "__ORDER_BIG_ENDIAN__"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 538 | Builder.defineMacro("__BIG_ENDIAN__"); |
| 539 | } else { |
Dylan Noblesmith | 3b198a9 | 2012-07-27 18:34:31 +0000 | [diff] [blame] | 540 | Builder.defineMacro("__BYTE_ORDER__", "__ORDER_LITTLE_ENDIAN__"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 541 | Builder.defineMacro("__LITTLE_ENDIAN__"); |
| 542 | } |
Dylan Noblesmith | 6bd3291 | 2012-08-10 19:12:37 +0000 | [diff] [blame] | 543 | |
| 544 | if (TI.getPointerWidth(0) == 64 && TI.getLongWidth() == 64 |
| 545 | && TI.getIntWidth() == 32) { |
| 546 | Builder.defineMacro("_LP64"); |
| 547 | Builder.defineMacro("__LP64__"); |
| 548 | } |
| 549 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 550 | // Define type sizing macros based on the target properties. |
| 551 | assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far"); |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 552 | Builder.defineMacro("__CHAR_BIT__", "8"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 553 | |
Stepan Dyatkovskiy | 7b7bef1 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 554 | DefineTypeSize("__SCHAR_MAX__", TargetInfo::SignedChar, TI, Builder); |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 555 | DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder); |
| 556 | DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder); |
| 557 | DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder); |
| 558 | DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder); |
| 559 | DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder); |
| 560 | DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder); |
Evgeniy Stepanov | edaf281 | 2013-03-28 08:36:54 +0000 | [diff] [blame] | 561 | DefineTypeSize("__SIZE_MAX__", TI.getSizeType(), TI, Builder); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 562 | |
Dan Gohman | cfeac34 | 2010-05-28 00:27:15 +0000 | [diff] [blame] | 563 | DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder); |
| 564 | DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder); |
| 565 | DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder); |
| 566 | DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder); |
| 567 | DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder); |
| 568 | DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder); |
| 569 | DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder); |
| 570 | DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder); |
| 571 | DefineTypeSizeof("__SIZEOF_PTRDIFF_T__", |
| 572 | TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder); |
| 573 | DefineTypeSizeof("__SIZEOF_SIZE_T__", |
| 574 | TI.getTypeWidth(TI.getSizeType()), TI, Builder); |
| 575 | DefineTypeSizeof("__SIZEOF_WCHAR_T__", |
| 576 | TI.getTypeWidth(TI.getWCharType()), TI, Builder); |
| 577 | DefineTypeSizeof("__SIZEOF_WINT_T__", |
| 578 | TI.getTypeWidth(TI.getWIntType()), TI, Builder); |
Richard Smith | 8426890 | 2012-11-29 05:41:51 +0000 | [diff] [blame] | 579 | if (TI.hasInt128Type()) |
| 580 | DefineTypeSizeof("__SIZEOF_INT128__", 128, TI, Builder); |
Dan Gohman | cfeac34 | 2010-05-28 00:27:15 +0000 | [diff] [blame] | 581 | |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 582 | DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder); |
| 583 | DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder); |
| 584 | DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder); |
| 585 | DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder); |
| 586 | DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder); |
| 587 | DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder); |
| 588 | DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder); |
| 589 | DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder); |
| 590 | DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder); |
| 591 | DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder); |
| 592 | DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder); |
| 593 | DefineType("__WINT_TYPE__", TI.getWIntType(), Builder); |
| 594 | DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder); |
| 595 | DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder); |
Dan Gohman | cfeac34 | 2010-05-28 00:27:15 +0000 | [diff] [blame] | 596 | DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder); |
| 597 | DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 598 | |
Eli Friedman | 75e8efe | 2012-11-10 00:20:38 +0000 | [diff] [blame] | 599 | DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat(), "F"); |
| 600 | DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat(), ""); |
| 601 | DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat(), "L"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 602 | |
| 603 | // Define a __POINTER_WIDTH__ macro for stdint.h. |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 604 | Builder.defineMacro("__POINTER_WIDTH__", |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 605 | Twine((int)TI.getPointerWidth(0))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 606 | |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 607 | if (!LangOpts.CharIsSigned) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 608 | Builder.defineMacro("__CHAR_UNSIGNED__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 609 | |
James Molloy | a30d860 | 2012-05-04 11:23:40 +0000 | [diff] [blame] | 610 | if (!TargetInfo::isTypeSigned(TI.getWCharType())) |
| 611 | Builder.defineMacro("__WCHAR_UNSIGNED__"); |
| 612 | |
Eli Friedman | 1cfeefd | 2011-04-21 05:45:45 +0000 | [diff] [blame] | 613 | if (!TargetInfo::isTypeSigned(TI.getWIntType())) |
| 614 | Builder.defineMacro("__WINT_UNSIGNED__"); |
| 615 | |
Chris Lattner | dcdd2a0 | 2009-11-12 08:08:27 +0000 | [diff] [blame] | 616 | // Define exact-width integer types for stdint.h |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 617 | Builder.defineMacro("__INT" + Twine(TI.getCharWidth()) + "_TYPE__", |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 618 | "char"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 619 | |
Chris Lattner | dcdd2a0 | 2009-11-12 08:08:27 +0000 | [diff] [blame] | 620 | if (TI.getShortWidth() > TI.getCharWidth()) |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 621 | DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 622 | |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 623 | if (TI.getIntWidth() > TI.getShortWidth()) |
| 624 | DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder); |
| 625 | |
| 626 | if (TI.getLongWidth() > TI.getIntWidth()) |
| 627 | DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder); |
| 628 | |
| 629 | if (TI.getLongLongWidth() > TI.getLongWidth()) |
| 630 | DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder); |
| 631 | |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 632 | if (const char *Prefix = TI.getUserLabelPrefix()) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 633 | Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | |
Bob Wilson | 455e72e | 2012-07-19 03:52:53 +0000 | [diff] [blame] | 635 | if (LangOpts.FastMath || LangOpts.FiniteMathOnly) |
| 636 | Builder.defineMacro("__FINITE_MATH_ONLY__", "1"); |
| 637 | else |
| 638 | Builder.defineMacro("__FINITE_MATH_ONLY__", "0"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 639 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 640 | if (LangOpts.GNUInline) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 641 | Builder.defineMacro("__GNUC_GNU_INLINE__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 642 | else |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 643 | Builder.defineMacro("__GNUC_STDC_INLINE__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 644 | |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 645 | // The value written by __atomic_test_and_set. |
| 646 | // FIXME: This is target-dependent. |
| 647 | Builder.defineMacro("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1"); |
| 648 | |
| 649 | // Used by libstdc++ to implement ATOMIC_<foo>_LOCK_FREE. |
| 650 | unsigned InlineWidthBits = TI.getMaxAtomicInlineWidth(); |
| 651 | #define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \ |
| 652 | Builder.defineMacro("__GCC_ATOMIC_" #TYPE "_LOCK_FREE", \ |
| 653 | getLockFreeValue(TI.get##Type##Width(), \ |
| 654 | TI.get##Type##Align(), \ |
| 655 | InlineWidthBits)); |
| 656 | DEFINE_LOCK_FREE_MACRO(BOOL, Bool); |
| 657 | DEFINE_LOCK_FREE_MACRO(CHAR, Char); |
| 658 | DEFINE_LOCK_FREE_MACRO(CHAR16_T, Char16); |
| 659 | DEFINE_LOCK_FREE_MACRO(CHAR32_T, Char32); |
| 660 | DEFINE_LOCK_FREE_MACRO(WCHAR_T, WChar); |
| 661 | DEFINE_LOCK_FREE_MACRO(SHORT, Short); |
| 662 | DEFINE_LOCK_FREE_MACRO(INT, Int); |
| 663 | DEFINE_LOCK_FREE_MACRO(LONG, Long); |
| 664 | DEFINE_LOCK_FREE_MACRO(LLONG, LongLong); |
| 665 | Builder.defineMacro("__GCC_ATOMIC_POINTER_LOCK_FREE", |
| 666 | getLockFreeValue(TI.getPointerWidth(0), |
| 667 | TI.getPointerAlign(0), |
| 668 | InlineWidthBits)); |
| 669 | #undef DEFINE_LOCK_FREE_MACRO |
| 670 | |
Daniel Dunbar | 7b14026 | 2012-03-09 15:39:08 +0000 | [diff] [blame] | 671 | if (LangOpts.NoInlineDefine) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 672 | Builder.defineMacro("__NO_INLINE__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 673 | |
| 674 | if (unsigned PICLevel = LangOpts.PICLevel) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 675 | Builder.defineMacro("__PIC__", Twine(PICLevel)); |
| 676 | Builder.defineMacro("__pic__", Twine(PICLevel)); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 677 | } |
Chandler Carruth | 5e219cf | 2012-04-08 16:40:35 +0000 | [diff] [blame] | 678 | if (unsigned PIELevel = LangOpts.PIELevel) { |
| 679 | Builder.defineMacro("__PIE__", Twine(PIELevel)); |
| 680 | Builder.defineMacro("__pie__", Twine(PIELevel)); |
| 681 | } |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 682 | |
| 683 | // Macros to control C99 numerics and <float.h> |
Benjamin Kramer | b406669 | 2011-12-28 15:47:06 +0000 | [diff] [blame] | 684 | Builder.defineMacro("__FLT_EVAL_METHOD__", Twine(TI.getFloatEvalMethod())); |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 685 | Builder.defineMacro("__FLT_RADIX__", "2"); |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 686 | int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 687 | Builder.defineMacro("__DECIMAL_DIG__", Twine(Dig)); |
Bill Wendling | 45483f7 | 2009-06-28 07:36:13 +0000 | [diff] [blame] | 688 | |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 689 | if (LangOpts.getStackProtector() == LangOptions::SSPOn) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 690 | Builder.defineMacro("__SSP__"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 691 | else if (LangOpts.getStackProtector() == LangOptions::SSPStrong) |
| 692 | Builder.defineMacro("__SSP_STRONG__", "2"); |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 693 | else if (LangOpts.getStackProtector() == LangOptions::SSPReq) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 694 | Builder.defineMacro("__SSP_ALL__", "3"); |
Bill Wendling | 45483f7 | 2009-06-28 07:36:13 +0000 | [diff] [blame] | 695 | |
Fariborz Jahanian | 7d95747 | 2010-01-13 18:51:17 +0000 | [diff] [blame] | 696 | if (FEOpts.ProgramAction == frontend::RewriteObjC) |
| 697 | Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))"); |
Ted Kremenek | f0af777 | 2010-05-26 21:36:54 +0000 | [diff] [blame] | 698 | |
| 699 | // Define a macro that exists only when using the static analyzer. |
| 700 | if (FEOpts.ProgramAction == frontend::RunAnalysis) |
| 701 | Builder.defineMacro("__clang_analyzer__"); |
| 702 | |
Peter Collingbourne | f084082 | 2010-12-04 01:51:23 +0000 | [diff] [blame] | 703 | if (LangOpts.FastRelaxedMath) |
| 704 | Builder.defineMacro("__FAST_RELAXED_MATH__"); |
| 705 | |
John McCall | 098df7f | 2011-06-16 00:03:19 +0000 | [diff] [blame] | 706 | if (LangOpts.ObjCAutoRefCount) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 707 | Builder.defineMacro("__weak", "__attribute__((objc_ownership(weak)))"); |
| 708 | Builder.defineMacro("__strong", "__attribute__((objc_ownership(strong)))"); |
John McCall | 098df7f | 2011-06-16 00:03:19 +0000 | [diff] [blame] | 709 | Builder.defineMacro("__autoreleasing", |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 710 | "__attribute__((objc_ownership(autoreleasing)))"); |
John McCall | 098df7f | 2011-06-16 00:03:19 +0000 | [diff] [blame] | 711 | Builder.defineMacro("__unsafe_unretained", |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 712 | "__attribute__((objc_ownership(none)))"); |
John McCall | 098df7f | 2011-06-16 00:03:19 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Douglas Gregor | 50a70cd | 2013-01-15 06:45:29 +0000 | [diff] [blame] | 715 | // OpenMP definition |
| 716 | if (LangOpts.OpenMP) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 717 | // OpenMP 2.2: |
Douglas Gregor | 50a70cd | 2013-01-15 06:45:29 +0000 | [diff] [blame] | 718 | // In implementations that support a preprocessor, the _OPENMP |
| 719 | // macro name is defined to have the decimal value yyyymm where |
| 720 | // yyyy and mm are the year and the month designations of the |
| 721 | // version of the OpenMP API that the implementation support. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 722 | Builder.defineMacro("_OPENMP", "201307"); |
Douglas Gregor | 50a70cd | 2013-01-15 06:45:29 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 725 | // Get other target #defines. |
Benjamin Kramer | a999277 | 2010-01-09 17:55:51 +0000 | [diff] [blame] | 726 | TI.getTargetDefines(LangOpts, Builder); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 729 | // Initialize the remapping of files to alternative contents, e.g., |
| 730 | // those specified through other files. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 731 | static void InitializeFileRemapping(DiagnosticsEngine &Diags, |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 732 | SourceManager &SourceMgr, |
| 733 | FileManager &FileMgr, |
| 734 | const PreprocessorOptions &InitOpts) { |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 735 | // Remap files in the source manager (with buffers). |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 736 | for (PreprocessorOptions::const_remapped_file_buffer_iterator |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 737 | Remap = InitOpts.remapped_file_buffer_begin(), |
| 738 | RemapEnd = InitOpts.remapped_file_buffer_end(); |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 739 | Remap != RemapEnd; |
| 740 | ++Remap) { |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 741 | // Create the file entry for the file that we're mapping from. |
| 742 | const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 743 | Remap->second->getBufferSize(), |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 744 | 0); |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 745 | if (!FromFile) { |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 746 | Diags.Report(diag::err_fe_remap_missing_from_file) |
| 747 | << Remap->first; |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 748 | if (!InitOpts.RetainRemappedFileBuffers) |
| 749 | delete Remap->second; |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 750 | continue; |
| 751 | } |
| 752 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 753 | // Override the contents of the "from" file with the contents of |
| 754 | // the "to" file. |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 755 | SourceMgr.overrideFileContents(FromFile, Remap->second, |
| 756 | InitOpts.RetainRemappedFileBuffers); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | // Remap files in the source manager (with other files). |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 760 | for (PreprocessorOptions::const_remapped_file_iterator |
| 761 | Remap = InitOpts.remapped_file_begin(), |
| 762 | RemapEnd = InitOpts.remapped_file_end(); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 763 | Remap != RemapEnd; |
| 764 | ++Remap) { |
| 765 | // Find the file that we're mapping to. |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 766 | const FileEntry *ToFile = FileMgr.getFile(Remap->second); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 767 | if (!ToFile) { |
| 768 | Diags.Report(diag::err_fe_remap_missing_to_file) |
| 769 | << Remap->first << Remap->second; |
| 770 | continue; |
| 771 | } |
| 772 | |
| 773 | // Create the file entry for the file that we're mapping from. |
| 774 | const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first, |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 775 | ToFile->getSize(), 0); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 776 | if (!FromFile) { |
| 777 | Diags.Report(diag::err_fe_remap_missing_from_file) |
| 778 | << Remap->first; |
| 779 | continue; |
| 780 | } |
| 781 | |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 782 | // Override the contents of the "from" file with the contents of |
| 783 | // the "to" file. |
Argyrios Kyrtzidis | 299a4a9 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 784 | SourceMgr.overrideFileContents(FromFile, ToFile); |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 785 | } |
Argyrios Kyrtzidis | 299a4a9 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 786 | |
| 787 | SourceMgr.setOverridenFilesKeepOriginalName( |
| 788 | InitOpts.RemappedFilesKeepOriginalName); |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 791 | /// InitializePreprocessor - Initialize the preprocessor getting it and the |
| 792 | /// environment ready to process a single file. This returns true on error. |
| 793 | /// |
Daniel Dunbar | 938963f | 2009-11-04 21:13:15 +0000 | [diff] [blame] | 794 | void clang::InitializePreprocessor(Preprocessor &PP, |
Daniel Dunbar | 961c76e | 2009-11-11 21:44:42 +0000 | [diff] [blame] | 795 | const PreprocessorOptions &InitOpts, |
Fariborz Jahanian | 7d95747 | 2010-01-13 18:51:17 +0000 | [diff] [blame] | 796 | const HeaderSearchOptions &HSOpts, |
| 797 | const FrontendOptions &FEOpts) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 798 | const LangOptions &LangOpts = PP.getLangOpts(); |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 799 | std::string PredefineBuffer; |
| 800 | PredefineBuffer.reserve(4080); |
| 801 | llvm::raw_string_ostream Predefines(PredefineBuffer); |
| 802 | MacroBuilder Builder(Predefines); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 803 | |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 804 | InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(), |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 805 | PP.getFileManager(), InitOpts); |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 806 | |
Chris Lattner | fcaa9e7 | 2010-04-26 22:08:10 +0000 | [diff] [blame] | 807 | // Emit line markers for various builtin sections of the file. We don't do |
| 808 | // this in asm preprocessor mode, because "# 4" is not a line marker directive |
| 809 | // in this mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 810 | if (!PP.getLangOpts().AsmPreprocessor) |
Chris Lattner | fcaa9e7 | 2010-04-26 22:08:10 +0000 | [diff] [blame] | 811 | Builder.append("# 1 \"<built-in>\" 3"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 812 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 813 | // Install things like __POWERPC__, __GNUC__, etc into the macro table. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 814 | if (InitOpts.UsePredefines) { |
| 815 | InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, Builder); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 817 | // Install definitions to make Objective-C++ ARC work well with various |
| 818 | // C++ Standard Library implementations. |
| 819 | if (LangOpts.ObjC1 && LangOpts.CPlusPlus && LangOpts.ObjCAutoRefCount) { |
| 820 | switch (InitOpts.ObjCXXARCStandardLibrary) { |
| 821 | case ARCXX_nolib: |
Douglas Gregor | ed73b10 | 2011-09-27 22:48:19 +0000 | [diff] [blame] | 822 | case ARCXX_libcxx: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 823 | break; |
| 824 | |
| 825 | case ARCXX_libstdcxx: |
| 826 | AddObjCXXARCLibstdcxxDefines(LangOpts, Builder); |
| 827 | break; |
| 828 | } |
| 829 | } |
| 830 | } |
| 831 | |
Nick Lewycky | a9c6441 | 2011-06-07 06:07:12 +0000 | [diff] [blame] | 832 | // Even with predefines off, some macros are still predefined. |
| 833 | // These should all be defined in the preprocessor according to the |
| 834 | // current language configuration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 835 | InitializeStandardPredefinedMacros(PP.getTargetInfo(), PP.getLangOpts(), |
Nick Lewycky | a9c6441 | 2011-06-07 06:07:12 +0000 | [diff] [blame] | 836 | FEOpts, Builder); |
| 837 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 838 | // Add on the predefines from the driver. Wrap in a #line directive to report |
| 839 | // that they come from the command line. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 840 | if (!PP.getLangOpts().AsmPreprocessor) |
Chris Lattner | fcaa9e7 | 2010-04-26 22:08:10 +0000 | [diff] [blame] | 841 | Builder.append("# 1 \"<command line>\" 1"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 842 | |
| 843 | // Process #define's and #undef's in the order they are given. |
Daniel Dunbar | 049d3a0 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 844 | for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) { |
| 845 | if (InitOpts.Macros[i].second) // isUndef |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 846 | Builder.undefineMacro(InitOpts.Macros[i].first); |
Chris Lattner | 62f86c4 | 2009-04-21 06:00:24 +0000 | [diff] [blame] | 847 | else |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 848 | DefineBuiltinMacro(Builder, InitOpts.Macros[i].first, |
Daniel Dunbar | 1ee4b9e | 2010-01-10 00:46:21 +0000 | [diff] [blame] | 849 | PP.getDiagnostics()); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | // If -imacros are specified, include them now. These are processed before |
| 853 | // any -include directives. |
Daniel Dunbar | 049d3a0 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 854 | for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i) |
Nick Lewycky | 277a6e7 | 2011-02-23 21:16:44 +0000 | [diff] [blame] | 855 | AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i], |
| 856 | PP.getFileManager()); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 857 | |
Argyrios Kyrtzidis | 3ad86fd | 2013-02-05 16:36:52 +0000 | [diff] [blame] | 858 | // Process -include-pch/-include-pth directives. |
| 859 | if (!InitOpts.ImplicitPCHInclude.empty()) |
| 860 | AddImplicitIncludePCH(Builder, PP, InitOpts.ImplicitPCHInclude); |
| 861 | if (!InitOpts.ImplicitPTHInclude.empty()) |
| 862 | AddImplicitIncludePTH(Builder, PP, InitOpts.ImplicitPTHInclude); |
| 863 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 864 | // Process -include directives. |
Daniel Dunbar | 049d3a0 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 865 | for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) { |
| 866 | const std::string &Path = InitOpts.Includes[i]; |
Argyrios Kyrtzidis | 3ad86fd | 2013-02-05 16:36:52 +0000 | [diff] [blame] | 867 | AddImplicitInclude(Builder, Path, PP.getFileManager()); |
Eli Friedman | ae96a96 | 2009-06-15 09:57:52 +0000 | [diff] [blame] | 868 | } |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 869 | |
Rafael Espindola | 0259d20 | 2009-12-01 18:28:16 +0000 | [diff] [blame] | 870 | // Exit the command line and go back to <built-in> (2 is LC_LEAVE). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 871 | if (!PP.getLangOpts().AsmPreprocessor) |
Chris Lattner | fcaa9e7 | 2010-04-26 22:08:10 +0000 | [diff] [blame] | 872 | Builder.append("# 1 \"<built-in>\" 2"); |
Rafael Espindola | 0259d20 | 2009-12-01 18:28:16 +0000 | [diff] [blame] | 873 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 874 | // Instruct the preprocessor to skip the preamble. |
| 875 | PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first, |
| 876 | InitOpts.PrecompiledPreambleBytes.second); |
| 877 | |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 878 | // Copy PredefinedBuffer into the Preprocessor. |
| 879 | PP.setPredefines(Predefines.str()); |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 880 | |
Daniel Dunbar | 961c76e | 2009-11-11 21:44:42 +0000 | [diff] [blame] | 881 | // Initialize the header search object. |
| 882 | ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 883 | PP.getLangOpts(), |
Daniel Dunbar | 961c76e | 2009-11-11 21:44:42 +0000 | [diff] [blame] | 884 | PP.getTargetInfo().getTriple()); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 885 | } |