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" |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 22 | #include "clang/Basic/FileManager.h" |
| 23 | #include "clang/Basic/SourceManager.h" |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/APFloat.h" |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Path.h" |
Chris Lattner | 47c06ee | 2009-11-02 21:48:09 +0000 | [diff] [blame] | 27 | using namespace clang; |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 28 | |
| 29 | // Append a #define line to Buf for Macro. Macro should be of the form XXX, |
| 30 | // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit |
| 31 | // "#define XXX Y z W". To get a #define with no value, use "XXX=". |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 32 | static void DefineBuiltinMacro(MacroBuilder &Builder, llvm::StringRef Macro, |
Daniel Dunbar | 1ee4b9e | 2010-01-10 00:46:21 +0000 | [diff] [blame] | 33 | Diagnostic &Diags) { |
Kovarththanan Rajaratnam | 8746e4e | 2010-01-09 09:27:11 +0000 | [diff] [blame] | 34 | std::pair<llvm::StringRef, llvm::StringRef> MacroPair = Macro.split('='); |
| 35 | llvm::StringRef MacroName = MacroPair.first; |
| 36 | llvm::StringRef MacroBody = MacroPair.second; |
Daniel Dunbar | 1ee4b9e | 2010-01-10 00:46:21 +0000 | [diff] [blame] | 37 | if (MacroName.size() != Macro.size()) { |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 38 | // Per GCC -D semantics, the macro ends at \n if it exists. |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 39 | llvm::StringRef::size_type End = MacroBody.find_first_of("\n\r"); |
Daniel Dunbar | 1ee4b9e | 2010-01-10 00:46:21 +0000 | [diff] [blame] | 40 | if (End != llvm::StringRef::npos) |
| 41 | Diags.Report(diag::warn_fe_macro_contains_embedded_newline) |
Kovarththanan Rajaratnam | 8746e4e | 2010-01-09 09:27:11 +0000 | [diff] [blame] | 42 | << MacroName; |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 43 | Builder.defineMacro(MacroName, MacroBody.substr(0, End)); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 44 | } else { |
| 45 | // Push "macroname 1". |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 46 | Builder.defineMacro(Macro); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 47 | } |
Chris Lattner | dcdecf4 | 2009-05-15 16:08:43 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Daniel Dunbar | c716293 | 2009-11-11 23:58:53 +0000 | [diff] [blame] | 50 | std::string clang::NormalizeDashIncludePath(llvm::StringRef File) { |
Daniel Dunbar | 1e7c6b6 | 2009-04-22 08:53:01 +0000 | [diff] [blame] | 51 | // Implicit include paths should be resolved relative to the current |
| 52 | // working directory first, and then use the regular header search |
| 53 | // mechanism. The proper way to handle this is to have the |
| 54 | // predefines buffer located at the current working directory, but |
| 55 | // it has not file entry. For now, workaround this by using an |
| 56 | // absolute path if we find the file here, and otherwise letting |
| 57 | // header search handle it. |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 58 | llvm::sys::Path Path(File); |
| 59 | Path.makeAbsolute(); |
Daniel Dunbar | 1e7c6b6 | 2009-04-22 08:53:01 +0000 | [diff] [blame] | 60 | if (!Path.exists()) |
| 61 | Path = File; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Daniel Dunbar | c716293 | 2009-11-11 23:58:53 +0000 | [diff] [blame] | 63 | return Lexer::Stringify(Path.str()); |
| 64 | } |
| 65 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 66 | /// AddImplicitInclude - Add an implicit #include of the specified file to the |
| 67 | /// predefines buffer. |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 68 | static void AddImplicitInclude(MacroBuilder &Builder, llvm::StringRef File) { |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 69 | Builder.append("#include \"" + |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 70 | llvm::Twine(NormalizeDashIncludePath(File)) + "\""); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 73 | static void AddImplicitIncludeMacros(MacroBuilder &Builder, |
| 74 | llvm::StringRef File) { |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 75 | Builder.append("#__include_macros \"" + |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 76 | llvm::Twine(NormalizeDashIncludePath(File)) + "\""); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 77 | // Marker token to stop the __include_macros fetch loop. |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 78 | Builder.append("##"); // ##? |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /// AddImplicitIncludePTH - Add an implicit #include using the original file |
| 82 | /// used to generate a PTH cache. |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 83 | static void AddImplicitIncludePTH(MacroBuilder &Builder, Preprocessor &PP, |
| 84 | llvm::StringRef ImplicitIncludePTH) { |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 85 | PTHManager *P = PP.getPTHManager(); |
Ted Kremenek | 4ae4c91 | 2010-06-28 20:32:40 +0000 | [diff] [blame] | 86 | // Null check 'P' in the corner case where it couldn't be created. |
| 87 | const char *OriginalFile = P ? P->getOriginalSourceFile() : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 89 | if (!OriginalFile) { |
Daniel Dunbar | baac103 | 2009-12-03 09:14:12 +0000 | [diff] [blame] | 90 | PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header) |
| 91 | << ImplicitIncludePTH; |
| 92 | return; |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 93 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 95 | AddImplicitInclude(Builder, OriginalFile); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /// PickFP - This is used to pick a value based on the FP semantics of the |
| 99 | /// specified FP model. |
| 100 | template <typename T> |
| 101 | static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal, |
Eli Friedman | 2665a75 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 102 | T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal, |
| 103 | T IEEEQuadVal) { |
Duncan Sands | 63682f9 | 2009-06-03 14:28:20 +0000 | [diff] [blame] | 104 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle) |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 105 | return IEEESingleVal; |
Duncan Sands | 63682f9 | 2009-06-03 14:28:20 +0000 | [diff] [blame] | 106 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble) |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 107 | return IEEEDoubleVal; |
Duncan Sands | 63682f9 | 2009-06-03 14:28:20 +0000 | [diff] [blame] | 108 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended) |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 109 | return X87DoubleExtendedVal; |
Duncan Sands | 63682f9 | 2009-06-03 14:28:20 +0000 | [diff] [blame] | 110 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble) |
Eli Friedman | 2665a75 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 111 | return PPCDoubleDoubleVal; |
Duncan Sands | 63682f9 | 2009-06-03 14:28:20 +0000 | [diff] [blame] | 112 | assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad); |
Eli Friedman | 2665a75 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 113 | return IEEEQuadVal; |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 116 | static void DefineFloatMacros(MacroBuilder &Builder, llvm::StringRef Prefix, |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 117 | const llvm::fltSemantics *Sem) { |
| 118 | const char *DenormMin, *Epsilon, *Max, *Min; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324", |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 120 | "3.64519953188247460253e-4951L", |
Eli Friedman | 2665a75 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 121 | "4.94065645841246544176568792868221e-324L", |
| 122 | "6.47517511943802511092443895822764655e-4966L"); |
| 123 | int Digits = PickFP(Sem, 6, 15, 18, 31, 33); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 124 | Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16", |
| 125 | "1.08420217248550443401e-19L", |
Eli Friedman | 2665a75 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 126 | "4.94065645841246544176568792868221e-324L", |
| 127 | "1.92592994438723585305597794258492732e-34L"); |
Eli Friedman | 2665a75 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 128 | int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113); |
| 129 | int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931); |
| 130 | int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932); |
| 131 | int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381); |
| 132 | int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 133 | Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308", |
| 134 | "3.36210314311209350626e-4932L", |
Eli Friedman | 2665a75 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 135 | "2.00416836000897277799610805135016e-292L", |
| 136 | "3.36210314311209350626267781732175260e-4932L"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 137 | Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308", |
| 138 | "1.18973149535723176502e+4932L", |
Eli Friedman | 2665a75 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 139 | "1.79769313486231580793728971405301e+308L", |
| 140 | "1.18973149535723176508575932662800702e+4932L"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | |
Daniel Dunbar | 066515f | 2010-01-20 06:09:53 +0000 | [diff] [blame] | 142 | llvm::SmallString<32> DefPrefix; |
| 143 | DefPrefix = "__"; |
| 144 | DefPrefix += Prefix; |
| 145 | DefPrefix += "_"; |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 146 | |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 147 | Builder.defineMacro(DefPrefix + "DENORM_MIN__", DenormMin); |
| 148 | Builder.defineMacro(DefPrefix + "HAS_DENORM__"); |
| 149 | Builder.defineMacro(DefPrefix + "DIG__", llvm::Twine(Digits)); |
| 150 | Builder.defineMacro(DefPrefix + "EPSILON__", llvm::Twine(Epsilon)); |
| 151 | Builder.defineMacro(DefPrefix + "HAS_INFINITY__"); |
| 152 | Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__"); |
| 153 | Builder.defineMacro(DefPrefix + "MANT_DIG__", llvm::Twine(MantissaDigits)); |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 154 | |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 155 | Builder.defineMacro(DefPrefix + "MAX_10_EXP__", llvm::Twine(Max10Exp)); |
| 156 | Builder.defineMacro(DefPrefix + "MAX_EXP__", llvm::Twine(MaxExp)); |
| 157 | Builder.defineMacro(DefPrefix + "MAX__", llvm::Twine(Max)); |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 158 | |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 159 | Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+llvm::Twine(Min10Exp)+")"); |
| 160 | Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+llvm::Twine(MinExp)+")"); |
| 161 | Builder.defineMacro(DefPrefix + "MIN__", llvm::Twine(Min)); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
| 165 | /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro |
| 166 | /// named MacroName with the max value for a type with width 'TypeWidth' a |
| 167 | /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL). |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 168 | static void DefineTypeSize(llvm::StringRef MacroName, unsigned TypeWidth, |
| 169 | llvm::StringRef ValSuffix, bool isSigned, |
| 170 | MacroBuilder& Builder) { |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 171 | long long MaxVal; |
Chris Lattner | 8074ab3 | 2010-08-03 16:48:42 +0000 | [diff] [blame] | 172 | if (isSigned) { |
| 173 | assert(TypeWidth != 1); |
| 174 | MaxVal = ~0ULL >> (65-TypeWidth); |
| 175 | } else |
Eli Friedman | 1d83243 | 2010-08-03 17:34:19 +0000 | [diff] [blame] | 176 | MaxVal = ~0ULL >> (64-TypeWidth); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 178 | Builder.defineMacro(MacroName, llvm::Twine(MaxVal) + ValSuffix); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Chris Lattner | 9099e7b | 2009-11-05 21:21:32 +0000 | [diff] [blame] | 181 | /// DefineTypeSize - An overloaded helper that uses TargetInfo to determine |
| 182 | /// the width, suffix, and signedness of the given type |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 183 | static void DefineTypeSize(llvm::StringRef MacroName, TargetInfo::IntType Ty, |
| 184 | const TargetInfo &TI, MacroBuilder &Builder) { |
Chris Lattner | 9099e7b | 2009-11-05 21:21:32 +0000 | [diff] [blame] | 185 | DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty), |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 186 | TI.isTypeSigned(Ty), Builder); |
Chris Lattner | 9099e7b | 2009-11-05 21:21:32 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 189 | static void DefineType(const llvm::Twine &MacroName, TargetInfo::IntType Ty, |
| 190 | MacroBuilder &Builder) { |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 191 | Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty)); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 194 | static void DefineTypeWidth(llvm::StringRef MacroName, TargetInfo::IntType Ty, |
| 195 | const TargetInfo &TI, MacroBuilder &Builder) { |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 196 | Builder.defineMacro(MacroName, llvm::Twine(TI.getTypeWidth(Ty))); |
Ken Dyck | 186696b | 2009-11-18 13:52:57 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Dan Gohman | cfeac34 | 2010-05-28 00:27:15 +0000 | [diff] [blame] | 199 | static void DefineTypeSizeof(llvm::StringRef MacroName, unsigned BitWidth, |
| 200 | const TargetInfo &TI, MacroBuilder &Builder) { |
| 201 | Builder.defineMacro(MacroName, |
| 202 | llvm::Twine(BitWidth / TI.getCharWidth())); |
| 203 | } |
| 204 | |
Chris Lattner | dcdd2a0 | 2009-11-12 08:08:27 +0000 | [diff] [blame] | 205 | static void DefineExactWidthIntType(TargetInfo::IntType Ty, |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 206 | const TargetInfo &TI, MacroBuilder &Builder) { |
Ken Dyck | eef22ef | 2009-11-16 16:36:33 +0000 | [diff] [blame] | 207 | int TypeWidth = TI.getTypeWidth(Ty); |
Daniel Dunbar | 7f3a545 | 2010-06-30 06:30:56 +0000 | [diff] [blame] | 208 | |
| 209 | // Use the target specified int64 type, when appropriate, so that [u]int64_t |
| 210 | // ends up being defined in terms of the correct type. |
| 211 | if (TypeWidth == 64) |
| 212 | Ty = TI.getInt64Type(); |
| 213 | |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 214 | DefineType("__INT" + llvm::Twine(TypeWidth) + "_TYPE__", Ty, Builder); |
Ken Dyck | eef22ef | 2009-11-16 16:36:33 +0000 | [diff] [blame] | 215 | |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 216 | llvm::StringRef ConstSuffix(TargetInfo::getTypeConstantSuffix(Ty)); |
| 217 | if (!ConstSuffix.empty()) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 218 | Builder.defineMacro("__INT" + llvm::Twine(TypeWidth) + "_C_SUFFIX__", |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 219 | ConstSuffix); |
Chris Lattner | dcdd2a0 | 2009-11-12 08:08:27 +0000 | [diff] [blame] | 220 | } |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 221 | |
| 222 | static void InitializePredefinedMacros(const TargetInfo &TI, |
| 223 | const LangOptions &LangOpts, |
Fariborz Jahanian | 7d95747 | 2010-01-13 18:51:17 +0000 | [diff] [blame] | 224 | const FrontendOptions &FEOpts, |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 225 | MacroBuilder &Builder) { |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 226 | // Compiler version introspection macros. |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 227 | Builder.defineMacro("__llvm__"); // LLVM Backend |
| 228 | Builder.defineMacro("__clang__"); // Clang Frontend |
Douglas Gregor | 4290fbd | 2010-04-30 02:51:06 +0000 | [diff] [blame] | 229 | #define TOSTR2(X) #X |
| 230 | #define TOSTR(X) TOSTR2(X) |
| 231 | Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR)); |
| 232 | Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR)); |
| 233 | #ifdef CLANG_VERSION_PATCHLEVEL |
| 234 | Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL)); |
| 235 | #else |
| 236 | Builder.defineMacro("__clang_patchlevel__", "0"); |
| 237 | #endif |
| 238 | Builder.defineMacro("__clang_version__", |
| 239 | "\"" CLANG_VERSION_STRING " (" |
| 240 | + getClangFullRepositoryVersion() + ")\""); |
| 241 | #undef TOSTR |
| 242 | #undef TOSTR2 |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 243 | // Currently claim to be compatible with GCC 4.2.1-5621. |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 244 | Builder.defineMacro("__GNUC_MINOR__", "2"); |
| 245 | Builder.defineMacro("__GNUC_PATCHLEVEL__", "1"); |
| 246 | Builder.defineMacro("__GNUC__", "4"); |
| 247 | Builder.defineMacro("__GXX_ABI_VERSION", "1002"); |
| 248 | Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible Clang Compiler\""); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 250 | // Initialize language-specific preprocessor defines. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 252 | // These should all be defined in the preprocessor according to the |
| 253 | // current language configuration. |
| 254 | if (!LangOpts.Microsoft) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 255 | Builder.defineMacro("__STDC__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 256 | if (LangOpts.AsmPreprocessor) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 257 | Builder.defineMacro("__ASSEMBLER__"); |
Ryan Flynn | 07ef804 | 2009-07-21 00:07:02 +0000 | [diff] [blame] | 258 | |
| 259 | if (!LangOpts.CPlusPlus) { |
| 260 | if (LangOpts.C99) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 261 | Builder.defineMacro("__STDC_VERSION__", "199901L"); |
Ryan Flynn | 07ef804 | 2009-07-21 00:07:02 +0000 | [diff] [blame] | 262 | else if (!LangOpts.GNUMode && LangOpts.Digraphs) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 263 | Builder.defineMacro("__STDC_VERSION__", "199409L"); |
Ryan Flynn | 07ef804 | 2009-07-21 00:07:02 +0000 | [diff] [blame] | 264 | } |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 265 | |
| 266 | // Standard conforming mode? |
| 267 | if (!LangOpts.GNUMode) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 268 | Builder.defineMacro("__STRICT_ANSI__"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 269 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 270 | if (LangOpts.CPlusPlus0x) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 271 | Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 272 | |
| 273 | if (LangOpts.Freestanding) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 274 | Builder.defineMacro("__STDC_HOSTED__", "0"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 275 | else |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 276 | Builder.defineMacro("__STDC_HOSTED__"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 278 | if (LangOpts.ObjC1) { |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 279 | Builder.defineMacro("__OBJC__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 280 | if (LangOpts.ObjCNonFragileABI) { |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 281 | Builder.defineMacro("__OBJC2__"); |
| 282 | Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | if (LangOpts.getGCMode() != LangOptions::NonGC) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 286 | Builder.defineMacro("__OBJC_GC__"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 288 | if (LangOpts.NeXTRuntime) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 289 | Builder.defineMacro("__NEXT_RUNTIME__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 290 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 292 | // darwin_constant_cfstrings controls this. This is also dependent |
| 293 | // on other things like the runtime I believe. This is set even for C code. |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 294 | Builder.defineMacro("__CONSTANT_CFSTRINGS__"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 296 | if (LangOpts.ObjC2) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 297 | Builder.defineMacro("OBJC_NEW_PROPERTIES"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 298 | |
| 299 | if (LangOpts.PascalStrings) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 300 | Builder.defineMacro("__PASCAL_STRINGS__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 301 | |
| 302 | if (LangOpts.Blocks) { |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 303 | Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))"); |
| 304 | Builder.defineMacro("__BLOCKS__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 305 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | |
Rafael Espindola | f759df0 | 2009-10-01 13:33:33 +0000 | [diff] [blame] | 307 | if (LangOpts.Exceptions) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 308 | Builder.defineMacro("__EXCEPTIONS"); |
Dan Gohman | cfeac34 | 2010-05-28 00:27:15 +0000 | [diff] [blame] | 309 | if (LangOpts.RTTI) |
| 310 | Builder.defineMacro("__GXX_RTTI"); |
Daniel Dunbar | b2987d1 | 2010-02-10 18:49:11 +0000 | [diff] [blame] | 311 | if (LangOpts.SjLjExceptions) |
| 312 | Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__"); |
Rafael Espindola | f759df0 | 2009-10-01 13:33:33 +0000 | [diff] [blame] | 313 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 314 | if (LangOpts.CPlusPlus) { |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 315 | Builder.defineMacro("__DEPRECATED"); |
| 316 | Builder.defineMacro("__GNUG__", "4"); |
| 317 | Builder.defineMacro("__GXX_WEAK__"); |
Douglas Gregor | 36dd131 | 2009-08-06 04:09:28 +0000 | [diff] [blame] | 318 | if (LangOpts.GNUMode) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 319 | Builder.defineMacro("__cplusplus"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | else |
Douglas Gregor | 36dd131 | 2009-08-06 04:09:28 +0000 | [diff] [blame] | 321 | // C++ [cpp.predefined]p1: |
Michael J. Spencer | 13cfedb | 2010-08-21 06:05:06 +0000 | [diff] [blame] | 322 | // The name_ _cplusplusis defined to the value 199711L when compiling a |
Douglas Gregor | 36dd131 | 2009-08-06 04:09:28 +0000 | [diff] [blame] | 323 | // C++ translation unit. |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 324 | Builder.defineMacro("__cplusplus", "199711L"); |
| 325 | Builder.defineMacro("__private_extern__", "extern"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 326 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 327 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 328 | if (LangOpts.Microsoft) { |
John Thompson | 24ee804 | 2009-10-16 01:12:00 +0000 | [diff] [blame] | 329 | // Filter out some microsoft extensions when trying to parse in ms-compat |
| 330 | // mode. |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 331 | Builder.defineMacro("__int8", "__INT8_TYPE__"); |
| 332 | Builder.defineMacro("__int16", "__INT16_TYPE__"); |
| 333 | Builder.defineMacro("__int32", "__INT32_TYPE__"); |
| 334 | Builder.defineMacro("__int64", "__INT64_TYPE__"); |
Steve Naroff | fdd6aaf | 2009-12-04 21:29:41 +0000 | [diff] [blame] | 335 | // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however |
| 336 | // VC++ appears to only like __FUNCTION__. |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 337 | Builder.defineMacro("__PRETTY_FUNCTION__", "__FUNCTION__"); |
John Thompson | 24ee804 | 2009-10-16 01:12:00 +0000 | [diff] [blame] | 338 | // Work around some issues with Visual C++ headerws. |
| 339 | if (LangOpts.CPlusPlus) { |
| 340 | // Since we define wchar_t in C++ mode. |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 341 | Builder.defineMacro("_WCHAR_T_DEFINED"); |
| 342 | Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED"); |
Douglas Gregor | 5c0ca52 | 2010-08-30 14:44:26 +0000 | [diff] [blame] | 343 | Builder.append("class type_info;"); |
John Thompson | 24ee804 | 2009-10-16 01:12:00 +0000 | [diff] [blame] | 344 | } |
Steven Watanabe | 343d65c | 2010-09-05 23:16:22 +0000 | [diff] [blame] | 345 | |
| 346 | if (LangOpts.CPlusPlus0x) { |
| 347 | Builder.defineMacro("_HAS_CHAR16_T_LANGUAGE_SUPPORT", "1"); |
| 348 | } |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 349 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 351 | if (LangOpts.Optimize) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 352 | Builder.defineMacro("__OPTIMIZE__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 353 | if (LangOpts.OptimizeSize) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 354 | Builder.defineMacro("__OPTIMIZE_SIZE__"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 356 | // Initialize target-specific preprocessor defines. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 358 | // Define type sizing macros based on the target properties. |
| 359 | assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far"); |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 360 | Builder.defineMacro("__CHAR_BIT__", "8"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 361 | |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 362 | DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Builder); |
| 363 | DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder); |
| 364 | DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder); |
| 365 | DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder); |
| 366 | DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder); |
| 367 | DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder); |
| 368 | DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 369 | |
Dan Gohman | cfeac34 | 2010-05-28 00:27:15 +0000 | [diff] [blame] | 370 | DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder); |
| 371 | DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder); |
| 372 | DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder); |
| 373 | DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder); |
| 374 | DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder); |
| 375 | DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder); |
| 376 | DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder); |
| 377 | DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder); |
| 378 | DefineTypeSizeof("__SIZEOF_PTRDIFF_T__", |
| 379 | TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder); |
| 380 | DefineTypeSizeof("__SIZEOF_SIZE_T__", |
| 381 | TI.getTypeWidth(TI.getSizeType()), TI, Builder); |
| 382 | DefineTypeSizeof("__SIZEOF_WCHAR_T__", |
| 383 | TI.getTypeWidth(TI.getWCharType()), TI, Builder); |
| 384 | DefineTypeSizeof("__SIZEOF_WINT_T__", |
| 385 | TI.getTypeWidth(TI.getWIntType()), TI, Builder); |
| 386 | |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 387 | DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder); |
| 388 | DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder); |
| 389 | DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder); |
| 390 | DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder); |
| 391 | DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder); |
| 392 | DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder); |
| 393 | DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder); |
| 394 | DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder); |
| 395 | DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder); |
| 396 | DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder); |
| 397 | DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder); |
| 398 | DefineType("__WINT_TYPE__", TI.getWIntType(), Builder); |
| 399 | DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder); |
| 400 | DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder); |
Dan Gohman | cfeac34 | 2010-05-28 00:27:15 +0000 | [diff] [blame] | 401 | DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder); |
| 402 | DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 403 | |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 404 | DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat()); |
| 405 | DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat()); |
| 406 | DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat()); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 407 | |
| 408 | // Define a __POINTER_WIDTH__ macro for stdint.h. |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 409 | Builder.defineMacro("__POINTER_WIDTH__", |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 410 | llvm::Twine((int)TI.getPointerWidth(0))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 412 | if (!LangOpts.CharIsSigned) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 413 | Builder.defineMacro("__CHAR_UNSIGNED__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 414 | |
Chris Lattner | dcdd2a0 | 2009-11-12 08:08:27 +0000 | [diff] [blame] | 415 | // Define exact-width integer types for stdint.h |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 416 | Builder.defineMacro("__INT" + llvm::Twine(TI.getCharWidth()) + "_TYPE__", |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 417 | "char"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | |
Chris Lattner | dcdd2a0 | 2009-11-12 08:08:27 +0000 | [diff] [blame] | 419 | if (TI.getShortWidth() > TI.getCharWidth()) |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 420 | DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 422 | if (TI.getIntWidth() > TI.getShortWidth()) |
| 423 | DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder); |
| 424 | |
| 425 | if (TI.getLongWidth() > TI.getIntWidth()) |
| 426 | DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder); |
| 427 | |
| 428 | if (TI.getLongLongWidth() > TI.getLongWidth()) |
| 429 | DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder); |
| 430 | |
| 431 | // Add __builtin_va_list typedef. |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 432 | Builder.append(TI.getVAListDeclaration()); |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 433 | |
| 434 | if (const char *Prefix = TI.getUserLabelPrefix()) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 435 | Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix); |
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 | // Build configuration options. FIXME: these should be controlled by |
| 438 | // command line options or something. |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 439 | Builder.defineMacro("__FINITE_MATH_ONLY__", "0"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 440 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 441 | if (LangOpts.GNUInline) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 442 | Builder.defineMacro("__GNUC_GNU_INLINE__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 443 | else |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 444 | Builder.defineMacro("__GNUC_STDC_INLINE__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 445 | |
| 446 | if (LangOpts.NoInline) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 447 | Builder.defineMacro("__NO_INLINE__"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 448 | |
| 449 | if (unsigned PICLevel = LangOpts.PICLevel) { |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 450 | Builder.defineMacro("__PIC__", llvm::Twine(PICLevel)); |
| 451 | Builder.defineMacro("__pic__", llvm::Twine(PICLevel)); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | // Macros to control C99 numerics and <float.h> |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 455 | Builder.defineMacro("__FLT_EVAL_METHOD__", "0"); |
| 456 | Builder.defineMacro("__FLT_RADIX__", "2"); |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 457 | int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36); |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 458 | Builder.defineMacro("__DECIMAL_DIG__", llvm::Twine(Dig)); |
Bill Wendling | 45483f7 | 2009-06-28 07:36:13 +0000 | [diff] [blame] | 459 | |
Bill Wendling | 4ebe3e4 | 2009-06-28 23:01:01 +0000 | [diff] [blame] | 460 | if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 461 | Builder.defineMacro("__SSP__"); |
Bill Wendling | 4ebe3e4 | 2009-06-28 23:01:01 +0000 | [diff] [blame] | 462 | else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq) |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 463 | Builder.defineMacro("__SSP_ALL__", "2"); |
Bill Wendling | 45483f7 | 2009-06-28 07:36:13 +0000 | [diff] [blame] | 464 | |
Fariborz Jahanian | 7d95747 | 2010-01-13 18:51:17 +0000 | [diff] [blame] | 465 | if (FEOpts.ProgramAction == frontend::RewriteObjC) |
| 466 | Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))"); |
Ted Kremenek | f0af777 | 2010-05-26 21:36:54 +0000 | [diff] [blame] | 467 | |
| 468 | // Define a macro that exists only when using the static analyzer. |
| 469 | if (FEOpts.ProgramAction == frontend::RunAnalysis) |
| 470 | Builder.defineMacro("__clang_analyzer__"); |
| 471 | |
Peter Collingbourne | f084082 | 2010-12-04 01:51:23 +0000 | [diff] [blame^] | 472 | if (LangOpts.FastRelaxedMath) |
| 473 | Builder.defineMacro("__FAST_RELAXED_MATH__"); |
| 474 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 475 | // Get other target #defines. |
Benjamin Kramer | a999277 | 2010-01-09 17:55:51 +0000 | [diff] [blame] | 476 | TI.getTargetDefines(LangOpts, Builder); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 479 | // Initialize the remapping of files to alternative contents, e.g., |
| 480 | // those specified through other files. |
| 481 | static void InitializeFileRemapping(Diagnostic &Diags, |
| 482 | SourceManager &SourceMgr, |
| 483 | FileManager &FileMgr, |
| 484 | const PreprocessorOptions &InitOpts) { |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 485 | // Remap files in the source manager (with buffers). |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 486 | for (PreprocessorOptions::const_remapped_file_buffer_iterator |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 487 | Remap = InitOpts.remapped_file_buffer_begin(), |
| 488 | RemapEnd = InitOpts.remapped_file_buffer_end(); |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 489 | Remap != RemapEnd; |
| 490 | ++Remap) { |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 491 | // Create the file entry for the file that we're mapping from. |
| 492 | const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 493 | Remap->second->getBufferSize(), |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 494 | 0); |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 495 | if (!FromFile) { |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 496 | Diags.Report(diag::err_fe_remap_missing_from_file) |
| 497 | << Remap->first; |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 498 | if (!InitOpts.RetainRemappedFileBuffers) |
| 499 | delete Remap->second; |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 500 | continue; |
| 501 | } |
| 502 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 503 | // Override the contents of the "from" file with the contents of |
| 504 | // the "to" file. |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 505 | SourceMgr.overrideFileContents(FromFile, Remap->second, |
| 506 | InitOpts.RetainRemappedFileBuffers); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | // Remap files in the source manager (with other files). |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 510 | for (PreprocessorOptions::const_remapped_file_iterator |
| 511 | Remap = InitOpts.remapped_file_begin(), |
| 512 | RemapEnd = InitOpts.remapped_file_end(); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 513 | Remap != RemapEnd; |
| 514 | ++Remap) { |
| 515 | // Find the file that we're mapping to. |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 516 | const FileEntry *ToFile = FileMgr.getFile(Remap->second); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 517 | if (!ToFile) { |
| 518 | Diags.Report(diag::err_fe_remap_missing_to_file) |
| 519 | << Remap->first << Remap->second; |
| 520 | continue; |
| 521 | } |
| 522 | |
| 523 | // Create the file entry for the file that we're mapping from. |
| 524 | const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first, |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 525 | ToFile->getSize(), 0); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 526 | if (!FromFile) { |
| 527 | Diags.Report(diag::err_fe_remap_missing_from_file) |
| 528 | << Remap->first; |
| 529 | continue; |
| 530 | } |
| 531 | |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 532 | // Load the contents of the file we're mapping to. |
| 533 | std::string ErrorStr; |
| 534 | const llvm::MemoryBuffer *Buffer |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 535 | = FileMgr.getBufferForFile(ToFile->getName(), &ErrorStr); |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 536 | if (!Buffer) { |
| 537 | Diags.Report(diag::err_fe_error_opening) |
Douglas Gregor | c8dfe5e | 2010-02-27 01:32:48 +0000 | [diff] [blame] | 538 | << Remap->second << ErrorStr; |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 539 | continue; |
| 540 | } |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 541 | |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 542 | // Override the contents of the "from" file with the contents of |
| 543 | // the "to" file. |
| 544 | SourceMgr.overrideFileContents(FromFile, Buffer); |
| 545 | } |
| 546 | } |
| 547 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 548 | /// InitializePreprocessor - Initialize the preprocessor getting it and the |
| 549 | /// environment ready to process a single file. This returns true on error. |
| 550 | /// |
Daniel Dunbar | 938963f | 2009-11-04 21:13:15 +0000 | [diff] [blame] | 551 | void clang::InitializePreprocessor(Preprocessor &PP, |
Daniel Dunbar | 961c76e | 2009-11-11 21:44:42 +0000 | [diff] [blame] | 552 | const PreprocessorOptions &InitOpts, |
Fariborz Jahanian | 7d95747 | 2010-01-13 18:51:17 +0000 | [diff] [blame] | 553 | const HeaderSearchOptions &HSOpts, |
| 554 | const FrontendOptions &FEOpts) { |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 555 | std::string PredefineBuffer; |
| 556 | PredefineBuffer.reserve(4080); |
| 557 | llvm::raw_string_ostream Predefines(PredefineBuffer); |
| 558 | MacroBuilder Builder(Predefines); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 559 | |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 560 | InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(), |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 561 | PP.getFileManager(), InitOpts); |
Douglas Gregor | 2973c0e | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 562 | |
Chris Lattner | fcaa9e7 | 2010-04-26 22:08:10 +0000 | [diff] [blame] | 563 | // Emit line markers for various builtin sections of the file. We don't do |
| 564 | // this in asm preprocessor mode, because "# 4" is not a line marker directive |
| 565 | // in this mode. |
| 566 | if (!PP.getLangOptions().AsmPreprocessor) |
| 567 | Builder.append("# 1 \"<built-in>\" 3"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 568 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 569 | // Install things like __POWERPC__, __GNUC__, etc into the macro table. |
Daniel Dunbar | 049d3a0 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 570 | if (InitOpts.UsePredefines) |
Chris Lattner | e6113de | 2009-11-03 19:50:27 +0000 | [diff] [blame] | 571 | InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(), |
Fariborz Jahanian | 7d95747 | 2010-01-13 18:51:17 +0000 | [diff] [blame] | 572 | FEOpts, Builder); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 573 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 574 | // Add on the predefines from the driver. Wrap in a #line directive to report |
| 575 | // that they come from the command line. |
Chris Lattner | fcaa9e7 | 2010-04-26 22:08:10 +0000 | [diff] [blame] | 576 | if (!PP.getLangOptions().AsmPreprocessor) |
| 577 | Builder.append("# 1 \"<command line>\" 1"); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 578 | |
| 579 | // Process #define's and #undef's in the order they are given. |
Daniel Dunbar | 049d3a0 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 580 | for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) { |
| 581 | if (InitOpts.Macros[i].second) // isUndef |
Benjamin Kramer | b1b5b90 | 2010-01-09 17:43:21 +0000 | [diff] [blame] | 582 | Builder.undefineMacro(InitOpts.Macros[i].first); |
Chris Lattner | 62f86c4 | 2009-04-21 06:00:24 +0000 | [diff] [blame] | 583 | else |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 584 | DefineBuiltinMacro(Builder, InitOpts.Macros[i].first, |
Daniel Dunbar | 1ee4b9e | 2010-01-10 00:46:21 +0000 | [diff] [blame] | 585 | PP.getDiagnostics()); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | // If -imacros are specified, include them now. These are processed before |
| 589 | // any -include directives. |
Daniel Dunbar | 049d3a0 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 590 | for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i) |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 591 | AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i]); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 592 | |
| 593 | // Process -include directives. |
Daniel Dunbar | 049d3a0 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 594 | for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) { |
| 595 | const std::string &Path = InitOpts.Includes[i]; |
| 596 | if (Path == InitOpts.ImplicitPTHInclude) |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 597 | AddImplicitIncludePTH(Builder, PP, Path); |
Chris Lattner | 62f86c4 | 2009-04-21 06:00:24 +0000 | [diff] [blame] | 598 | else |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 599 | AddImplicitInclude(Builder, Path); |
Eli Friedman | ae96a96 | 2009-06-15 09:57:52 +0000 | [diff] [blame] | 600 | } |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 601 | |
Rafael Espindola | 0259d20 | 2009-12-01 18:28:16 +0000 | [diff] [blame] | 602 | // Exit the command line and go back to <built-in> (2 is LC_LEAVE). |
Chris Lattner | fcaa9e7 | 2010-04-26 22:08:10 +0000 | [diff] [blame] | 603 | if (!PP.getLangOptions().AsmPreprocessor) |
| 604 | Builder.append("# 1 \"<built-in>\" 2"); |
Rafael Espindola | 0259d20 | 2009-12-01 18:28:16 +0000 | [diff] [blame] | 605 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 606 | // Instruct the preprocessor to skip the preamble. |
| 607 | PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first, |
| 608 | InitOpts.PrecompiledPreambleBytes.second); |
| 609 | |
Benjamin Kramer | a3d8ced | 2010-01-09 16:17:37 +0000 | [diff] [blame] | 610 | // Copy PredefinedBuffer into the Preprocessor. |
| 611 | PP.setPredefines(Predefines.str()); |
Daniel Dunbar | 961c76e | 2009-11-11 21:44:42 +0000 | [diff] [blame] | 612 | |
| 613 | // Initialize the header search object. |
| 614 | ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts, |
| 615 | PP.getLangOptions(), |
| 616 | PP.getTargetInfo().getTriple()); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 617 | } |