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