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