Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 1 | //===--- InitPreprocessor.cpp - PP initialization code. ---------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the clang::InitializePreprocessor function. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | 00f8a39 | 2009-11-07 04:20:15 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/Utils.h" |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 15 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | 6ae34ab | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 00f8a39 | 2009-11-07 04:20:15 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/PreprocessorOptions.h" |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 6ae34ab | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 19 | #include "clang/Basic/FileManager.h" |
| 20 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringMap.h" |
Kovarththanan Rajaratnam | 2f14619 | 2010-01-09 09:27:11 +0000 | [diff] [blame^] | 23 | #include "llvm/ADT/StringRef.h" |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 6ae34ab | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 26 | #include "llvm/System/Path.h" |
Chris Lattner | 3fab58d | 2009-11-02 21:48:09 +0000 | [diff] [blame] | 27 | using namespace clang; |
Chris Lattner | 2f5693f | 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=". |
Kovarththanan Rajaratnam | 2f14619 | 2010-01-09 09:27:11 +0000 | [diff] [blame^] | 32 | static void DefineBuiltinMacro(std::vector<char> &Buf, llvm::StringRef Macro, |
Daniel Dunbar | 6dac935 | 2009-12-03 09:14:12 +0000 | [diff] [blame] | 33 | Diagnostic *Diags = 0) { |
Kovarththanan Rajaratnam | 49c8da9 | 2010-01-07 18:11:14 +0000 | [diff] [blame] | 34 | const char Command[] = "#define "; |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 35 | Buf.insert(Buf.end(), Command, Command+strlen(Command)); |
Kovarththanan Rajaratnam | 2f14619 | 2010-01-09 09:27:11 +0000 | [diff] [blame^] | 36 | std::pair<llvm::StringRef, llvm::StringRef> MacroPair = Macro.split('='); |
| 37 | llvm::StringRef MacroName = MacroPair.first; |
| 38 | llvm::StringRef MacroBody = MacroPair.second; |
| 39 | if (!MacroBody.empty()) { |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 40 | // Turn the = into ' '. |
Kovarththanan Rajaratnam | 2f14619 | 2010-01-09 09:27:11 +0000 | [diff] [blame^] | 41 | Buf.insert(Buf.end(), MacroName.begin(), MacroName.end()); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 42 | Buf.push_back(' '); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 44 | // Per GCC -D semantics, the macro ends at \n if it exists. |
Kovarththanan Rajaratnam | 2f14619 | 2010-01-09 09:27:11 +0000 | [diff] [blame^] | 45 | const char *End = strpbrk(MacroBody.data(), "\n\r"); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 46 | if (End) { |
Daniel Dunbar | 6dac935 | 2009-12-03 09:14:12 +0000 | [diff] [blame] | 47 | assert(Diags && "Unexpected macro with embedded newline!"); |
| 48 | Diags->Report(diag::warn_fe_macro_contains_embedded_newline) |
Kovarththanan Rajaratnam | 2f14619 | 2010-01-09 09:27:11 +0000 | [diff] [blame^] | 49 | << MacroName; |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 50 | } else { |
Kovarththanan Rajaratnam | 2f14619 | 2010-01-09 09:27:11 +0000 | [diff] [blame^] | 51 | End = MacroBody.end(); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 52 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | |
Kovarththanan Rajaratnam | 2f14619 | 2010-01-09 09:27:11 +0000 | [diff] [blame^] | 54 | Buf.insert(Buf.end(), MacroBody.begin(), End); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 55 | } else { |
| 56 | // Push "macroname 1". |
Kovarththanan Rajaratnam | 2f14619 | 2010-01-09 09:27:11 +0000 | [diff] [blame^] | 57 | Buf.insert(Buf.end(), Macro.begin(), Macro.end()); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 58 | Buf.push_back(' '); |
| 59 | Buf.push_back('1'); |
| 60 | } |
| 61 | Buf.push_back('\n'); |
| 62 | } |
| 63 | |
Chris Lattner | 4579b76 | 2009-05-15 16:08:43 +0000 | [diff] [blame] | 64 | // Append a #undef line to Buf for Macro. Macro should be of the form XXX |
| 65 | // and we emit "#undef XXX". |
| 66 | static void UndefineBuiltinMacro(std::vector<char> &Buf, const char *Macro) { |
| 67 | // Push "macroname". |
Kovarththanan Rajaratnam | 49c8da9 | 2010-01-07 18:11:14 +0000 | [diff] [blame] | 68 | const char Command[] = "#undef "; |
Chris Lattner | 4579b76 | 2009-05-15 16:08:43 +0000 | [diff] [blame] | 69 | Buf.insert(Buf.end(), Command, Command+strlen(Command)); |
| 70 | Buf.insert(Buf.end(), Macro, Macro+strlen(Macro)); |
| 71 | Buf.push_back('\n'); |
| 72 | } |
| 73 | |
Daniel Dunbar | 732ef8a | 2009-11-11 23:58:53 +0000 | [diff] [blame] | 74 | std::string clang::NormalizeDashIncludePath(llvm::StringRef File) { |
Daniel Dunbar | 66cb013 | 2009-04-22 08:53:01 +0000 | [diff] [blame] | 75 | // Implicit include paths should be resolved relative to the current |
| 76 | // working directory first, and then use the regular header search |
| 77 | // mechanism. The proper way to handle this is to have the |
| 78 | // predefines buffer located at the current working directory, but |
| 79 | // it has not file entry. For now, workaround this by using an |
| 80 | // absolute path if we find the file here, and otherwise letting |
| 81 | // header search handle it. |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 82 | llvm::sys::Path Path(File); |
| 83 | Path.makeAbsolute(); |
Daniel Dunbar | 66cb013 | 2009-04-22 08:53:01 +0000 | [diff] [blame] | 84 | if (!Path.exists()) |
| 85 | Path = File; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
Daniel Dunbar | 732ef8a | 2009-11-11 23:58:53 +0000 | [diff] [blame] | 87 | return Lexer::Stringify(Path.str()); |
| 88 | } |
| 89 | |
| 90 | /// Add the quoted name of an implicit include file. |
| 91 | static void AddQuotedIncludePath(std::vector<char> &Buf, |
| 92 | const std::string &File) { |
| 93 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 94 | // Escape double quotes etc. |
| 95 | Buf.push_back('"'); |
Daniel Dunbar | 732ef8a | 2009-11-11 23:58:53 +0000 | [diff] [blame] | 96 | std::string EscapedFile = NormalizeDashIncludePath(File); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 97 | Buf.insert(Buf.end(), EscapedFile.begin(), EscapedFile.end()); |
| 98 | Buf.push_back('"'); |
| 99 | } |
| 100 | |
| 101 | /// AddImplicitInclude - Add an implicit #include of the specified file to the |
| 102 | /// predefines buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | static void AddImplicitInclude(std::vector<char> &Buf, |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 104 | const std::string &File) { |
Kovarththanan Rajaratnam | 49c8da9 | 2010-01-07 18:11:14 +0000 | [diff] [blame] | 105 | const char Command[] = "#include "; |
| 106 | Buf.insert(Buf.end(), Command, Command+strlen(Command)); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 107 | AddQuotedIncludePath(Buf, File); |
| 108 | Buf.push_back('\n'); |
| 109 | } |
| 110 | |
| 111 | static void AddImplicitIncludeMacros(std::vector<char> &Buf, |
| 112 | const std::string &File) { |
Kovarththanan Rajaratnam | 49c8da9 | 2010-01-07 18:11:14 +0000 | [diff] [blame] | 113 | const char Command[] = "#__include_macros "; |
| 114 | Buf.insert(Buf.end(), Command, Command+strlen(Command)); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 115 | AddQuotedIncludePath(Buf, File); |
| 116 | Buf.push_back('\n'); |
| 117 | // Marker token to stop the __include_macros fetch loop. |
| 118 | const char *Marker = "##\n"; // ##? |
| 119 | Buf.insert(Buf.end(), Marker, Marker+strlen(Marker)); |
| 120 | } |
| 121 | |
| 122 | /// AddImplicitIncludePTH - Add an implicit #include using the original file |
| 123 | /// used to generate a PTH cache. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | static void AddImplicitIncludePTH(std::vector<char> &Buf, Preprocessor &PP, |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 125 | const std::string& ImplicitIncludePTH) { |
| 126 | PTHManager *P = PP.getPTHManager(); |
| 127 | assert(P && "No PTHManager."); |
| 128 | const char *OriginalFile = P->getOriginalSourceFile(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 130 | if (!OriginalFile) { |
Daniel Dunbar | 6dac935 | 2009-12-03 09:14:12 +0000 | [diff] [blame] | 131 | PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header) |
| 132 | << ImplicitIncludePTH; |
| 133 | return; |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 134 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 136 | AddImplicitInclude(Buf, OriginalFile); |
| 137 | } |
| 138 | |
| 139 | /// PickFP - This is used to pick a value based on the FP semantics of the |
| 140 | /// specified FP model. |
| 141 | template <typename T> |
| 142 | static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal, |
Eli Friedman | 57406b2 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 143 | T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal, |
| 144 | T IEEEQuadVal) { |
Duncan Sands | f406351 | 2009-06-03 14:28:20 +0000 | [diff] [blame] | 145 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle) |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 146 | return IEEESingleVal; |
Duncan Sands | f406351 | 2009-06-03 14:28:20 +0000 | [diff] [blame] | 147 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble) |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 148 | return IEEEDoubleVal; |
Duncan Sands | f406351 | 2009-06-03 14:28:20 +0000 | [diff] [blame] | 149 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended) |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 150 | return X87DoubleExtendedVal; |
Duncan Sands | f406351 | 2009-06-03 14:28:20 +0000 | [diff] [blame] | 151 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble) |
Eli Friedman | 57406b2 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 152 | return PPCDoubleDoubleVal; |
Duncan Sands | f406351 | 2009-06-03 14:28:20 +0000 | [diff] [blame] | 153 | assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad); |
Eli Friedman | 57406b2 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 154 | return IEEEQuadVal; |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static void DefineFloatMacros(std::vector<char> &Buf, const char *Prefix, |
| 158 | const llvm::fltSemantics *Sem) { |
| 159 | const char *DenormMin, *Epsilon, *Max, *Min; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324", |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 161 | "3.64519953188247460253e-4951L", |
Eli Friedman | 57406b2 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 162 | "4.94065645841246544176568792868221e-324L", |
| 163 | "6.47517511943802511092443895822764655e-4966L"); |
| 164 | int Digits = PickFP(Sem, 6, 15, 18, 31, 33); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 165 | Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16", |
| 166 | "1.08420217248550443401e-19L", |
Eli Friedman | 57406b2 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 167 | "4.94065645841246544176568792868221e-324L", |
| 168 | "1.92592994438723585305597794258492732e-34L"); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 169 | int HasInifinity = 1, HasQuietNaN = 1; |
Eli Friedman | 57406b2 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 170 | int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113); |
| 171 | int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931); |
| 172 | int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932); |
| 173 | int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381); |
| 174 | int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 175 | Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308", |
| 176 | "3.36210314311209350626e-4932L", |
Eli Friedman | 57406b2 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 177 | "2.00416836000897277799610805135016e-292L", |
| 178 | "3.36210314311209350626267781732175260e-4932L"); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 179 | Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308", |
| 180 | "1.18973149535723176502e+4932L", |
Eli Friedman | 57406b2 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 181 | "1.79769313486231580793728971405301e+308L", |
| 182 | "1.18973149535723176508575932662800702e+4932L"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
Eli Friedman | 57406b2 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 184 | char MacroBuf[100]; |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 185 | sprintf(MacroBuf, "__%s_DENORM_MIN__=%s", Prefix, DenormMin); |
| 186 | DefineBuiltinMacro(Buf, MacroBuf); |
| 187 | sprintf(MacroBuf, "__%s_DIG__=%d", Prefix, Digits); |
| 188 | DefineBuiltinMacro(Buf, MacroBuf); |
| 189 | sprintf(MacroBuf, "__%s_EPSILON__=%s", Prefix, Epsilon); |
| 190 | DefineBuiltinMacro(Buf, MacroBuf); |
| 191 | sprintf(MacroBuf, "__%s_HAS_INFINITY__=%d", Prefix, HasInifinity); |
| 192 | DefineBuiltinMacro(Buf, MacroBuf); |
| 193 | sprintf(MacroBuf, "__%s_HAS_QUIET_NAN__=%d", Prefix, HasQuietNaN); |
| 194 | DefineBuiltinMacro(Buf, MacroBuf); |
| 195 | sprintf(MacroBuf, "__%s_MANT_DIG__=%d", Prefix, MantissaDigits); |
| 196 | DefineBuiltinMacro(Buf, MacroBuf); |
| 197 | sprintf(MacroBuf, "__%s_MAX_10_EXP__=%d", Prefix, Max10Exp); |
| 198 | DefineBuiltinMacro(Buf, MacroBuf); |
| 199 | sprintf(MacroBuf, "__%s_MAX_EXP__=%d", Prefix, MaxExp); |
| 200 | DefineBuiltinMacro(Buf, MacroBuf); |
| 201 | sprintf(MacroBuf, "__%s_MAX__=%s", Prefix, Max); |
| 202 | DefineBuiltinMacro(Buf, MacroBuf); |
| 203 | sprintf(MacroBuf, "__%s_MIN_10_EXP__=(%d)", Prefix, Min10Exp); |
| 204 | DefineBuiltinMacro(Buf, MacroBuf); |
| 205 | sprintf(MacroBuf, "__%s_MIN_EXP__=(%d)", Prefix, MinExp); |
| 206 | DefineBuiltinMacro(Buf, MacroBuf); |
| 207 | sprintf(MacroBuf, "__%s_MIN__=%s", Prefix, Min); |
| 208 | DefineBuiltinMacro(Buf, MacroBuf); |
| 209 | sprintf(MacroBuf, "__%s_HAS_DENORM__=1", Prefix); |
| 210 | DefineBuiltinMacro(Buf, MacroBuf); |
| 211 | } |
| 212 | |
| 213 | |
| 214 | /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro |
| 215 | /// named MacroName with the max value for a type with width 'TypeWidth' a |
| 216 | /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL). |
| 217 | static void DefineTypeSize(const char *MacroName, unsigned TypeWidth, |
| 218 | const char *ValSuffix, bool isSigned, |
| 219 | std::vector<char> &Buf) { |
| 220 | char MacroBuf[60]; |
| 221 | long long MaxVal; |
| 222 | if (isSigned) |
| 223 | MaxVal = (1LL << (TypeWidth - 1)) - 1; |
| 224 | else |
| 225 | MaxVal = ~0LL >> (64-TypeWidth); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | |
Daniel Dunbar | fe07aec | 2009-09-03 19:23:49 +0000 | [diff] [blame] | 227 | // FIXME: Switch to using raw_ostream and avoid utostr(). |
| 228 | sprintf(MacroBuf, "%s=%s%s", MacroName, llvm::utostr(MaxVal).c_str(), |
| 229 | ValSuffix); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 230 | DefineBuiltinMacro(Buf, MacroBuf); |
| 231 | } |
| 232 | |
Chris Lattner | e4a8c64 | 2009-11-05 21:21:32 +0000 | [diff] [blame] | 233 | /// DefineTypeSize - An overloaded helper that uses TargetInfo to determine |
| 234 | /// the width, suffix, and signedness of the given type |
| 235 | static void DefineTypeSize(const char *MacroName, TargetInfo::IntType Ty, |
| 236 | const TargetInfo &TI, std::vector<char> &Buf) { |
| 237 | DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty), |
| 238 | TI.isTypeSigned(Ty), Buf); |
| 239 | } |
| 240 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 241 | static void DefineType(const char *MacroName, TargetInfo::IntType Ty, |
| 242 | std::vector<char> &Buf) { |
| 243 | char MacroBuf[60]; |
| 244 | sprintf(MacroBuf, "%s=%s", MacroName, TargetInfo::getTypeName(Ty)); |
| 245 | DefineBuiltinMacro(Buf, MacroBuf); |
| 246 | } |
| 247 | |
Ken Dyck | c0c9829 | 2009-11-18 13:52:57 +0000 | [diff] [blame] | 248 | static void DefineTypeWidth(const char *MacroName, TargetInfo::IntType Ty, |
| 249 | const TargetInfo &TI, std::vector<char> &Buf) { |
| 250 | char MacroBuf[60]; |
| 251 | sprintf(MacroBuf, "%s=%d", MacroName, TI.getTypeWidth(Ty)); |
| 252 | DefineBuiltinMacro(Buf, MacroBuf); |
| 253 | } |
| 254 | |
Chris Lattner | 55c9877 | 2009-11-12 08:08:27 +0000 | [diff] [blame] | 255 | static void DefineExactWidthIntType(TargetInfo::IntType Ty, |
| 256 | const TargetInfo &TI, std::vector<char> &Buf) { |
| 257 | char MacroBuf[60]; |
Ken Dyck | 2dc8d5f | 2009-11-16 16:36:33 +0000 | [diff] [blame] | 258 | int TypeWidth = TI.getTypeWidth(Ty); |
| 259 | sprintf(MacroBuf, "__INT%d_TYPE__", TypeWidth); |
Chris Lattner | 55c9877 | 2009-11-12 08:08:27 +0000 | [diff] [blame] | 260 | DefineType(MacroBuf, Ty, Buf); |
Ken Dyck | 2dc8d5f | 2009-11-16 16:36:33 +0000 | [diff] [blame] | 261 | |
| 262 | |
| 263 | const char *ConstSuffix = TargetInfo::getTypeConstantSuffix(Ty); |
| 264 | if (strlen(ConstSuffix) > 0) { |
| 265 | sprintf(MacroBuf, "__INT%d_C_SUFFIX__=%s", TypeWidth, ConstSuffix); |
| 266 | DefineBuiltinMacro(Buf, MacroBuf); |
| 267 | } |
Chris Lattner | 55c9877 | 2009-11-12 08:08:27 +0000 | [diff] [blame] | 268 | } |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 269 | |
| 270 | static void InitializePredefinedMacros(const TargetInfo &TI, |
| 271 | const LangOptions &LangOpts, |
| 272 | std::vector<char> &Buf) { |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 273 | // Compiler version introspection macros. |
| 274 | DefineBuiltinMacro(Buf, "__llvm__=1"); // LLVM Backend |
| 275 | DefineBuiltinMacro(Buf, "__clang__=1"); // Clang Frontend |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 277 | // Currently claim to be compatible with GCC 4.2.1-5621. |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 278 | DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2"); |
| 279 | DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1"); |
| 280 | DefineBuiltinMacro(Buf, "__GNUC__=4"); |
| 281 | DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002"); |
| 282 | DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 Compatible Clang Compiler\""); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
| 284 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 285 | // Initialize language-specific preprocessor defines. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 287 | // These should all be defined in the preprocessor according to the |
| 288 | // current language configuration. |
| 289 | if (!LangOpts.Microsoft) |
| 290 | DefineBuiltinMacro(Buf, "__STDC__=1"); |
| 291 | if (LangOpts.AsmPreprocessor) |
| 292 | DefineBuiltinMacro(Buf, "__ASSEMBLER__=1"); |
Ryan Flynn | ed73cac | 2009-07-21 00:07:02 +0000 | [diff] [blame] | 293 | |
| 294 | if (!LangOpts.CPlusPlus) { |
| 295 | if (LangOpts.C99) |
| 296 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L"); |
| 297 | else if (!LangOpts.GNUMode && LangOpts.Digraphs) |
| 298 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L"); |
| 299 | } |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 300 | |
| 301 | // Standard conforming mode? |
| 302 | if (!LangOpts.GNUMode) |
| 303 | DefineBuiltinMacro(Buf, "__STRICT_ANSI__=1"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 305 | if (LangOpts.CPlusPlus0x) |
| 306 | DefineBuiltinMacro(Buf, "__GXX_EXPERIMENTAL_CXX0X__"); |
| 307 | |
| 308 | if (LangOpts.Freestanding) |
| 309 | DefineBuiltinMacro(Buf, "__STDC_HOSTED__=0"); |
| 310 | else |
| 311 | DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 313 | if (LangOpts.ObjC1) { |
| 314 | DefineBuiltinMacro(Buf, "__OBJC__=1"); |
| 315 | if (LangOpts.ObjCNonFragileABI) { |
| 316 | DefineBuiltinMacro(Buf, "__OBJC2__=1"); |
| 317 | DefineBuiltinMacro(Buf, "OBJC_ZEROCOST_EXCEPTIONS=1"); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | if (LangOpts.getGCMode() != LangOptions::NonGC) |
| 321 | DefineBuiltinMacro(Buf, "__OBJC_GC__=1"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 323 | if (LangOpts.NeXTRuntime) |
| 324 | DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1"); |
| 325 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 327 | // darwin_constant_cfstrings controls this. This is also dependent |
| 328 | // on other things like the runtime I believe. This is set even for C code. |
| 329 | DefineBuiltinMacro(Buf, "__CONSTANT_CFSTRINGS__=1"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 331 | if (LangOpts.ObjC2) |
| 332 | DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES"); |
| 333 | |
| 334 | if (LangOpts.PascalStrings) |
| 335 | DefineBuiltinMacro(Buf, "__PASCAL_STRINGS__"); |
| 336 | |
| 337 | if (LangOpts.Blocks) { |
| 338 | DefineBuiltinMacro(Buf, "__block=__attribute__((__blocks__(byref)))"); |
| 339 | DefineBuiltinMacro(Buf, "__BLOCKS__=1"); |
| 340 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | |
Rafael Espindola | 00a6657 | 2009-10-01 13:33:33 +0000 | [diff] [blame] | 342 | if (LangOpts.Exceptions) |
| 343 | DefineBuiltinMacro(Buf, "__EXCEPTIONS=1"); |
| 344 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 345 | if (LangOpts.CPlusPlus) { |
| 346 | DefineBuiltinMacro(Buf, "__DEPRECATED=1"); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 347 | DefineBuiltinMacro(Buf, "__GNUG__=4"); |
| 348 | DefineBuiltinMacro(Buf, "__GXX_WEAK__=1"); |
Douglas Gregor | 3f4d51b | 2009-08-06 04:09:28 +0000 | [diff] [blame] | 349 | if (LangOpts.GNUMode) |
| 350 | DefineBuiltinMacro(Buf, "__cplusplus=1"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | else |
Douglas Gregor | 3f4d51b | 2009-08-06 04:09:28 +0000 | [diff] [blame] | 352 | // C++ [cpp.predefined]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | // The name_ _cplusplusis defined to the value199711Lwhen compiling a |
Douglas Gregor | 3f4d51b | 2009-08-06 04:09:28 +0000 | [diff] [blame] | 354 | // C++ translation unit. |
| 355 | DefineBuiltinMacro(Buf, "__cplusplus=199711L"); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 356 | DefineBuiltinMacro(Buf, "__private_extern__=extern"); |
Eli Friedman | 8011017 | 2009-08-27 22:01:41 +0000 | [diff] [blame] | 357 | // Ugly hack to work with GNU libstdc++. |
| 358 | DefineBuiltinMacro(Buf, "_GNU_SOURCE=1"); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 359 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 361 | if (LangOpts.Microsoft) { |
John Thompson | 1faca9e | 2009-10-16 01:12:00 +0000 | [diff] [blame] | 362 | // Filter out some microsoft extensions when trying to parse in ms-compat |
| 363 | // mode. |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 364 | DefineBuiltinMacro(Buf, "__int8=__INT8_TYPE__"); |
| 365 | DefineBuiltinMacro(Buf, "__int16=__INT16_TYPE__"); |
| 366 | DefineBuiltinMacro(Buf, "__int32=__INT32_TYPE__"); |
| 367 | DefineBuiltinMacro(Buf, "__int64=__INT64_TYPE__"); |
Steve Naroff | f0df20a | 2009-12-04 21:29:41 +0000 | [diff] [blame] | 368 | // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however |
| 369 | // VC++ appears to only like __FUNCTION__. |
| 370 | DefineBuiltinMacro(Buf, "__PRETTY_FUNCTION__=__FUNCTION__"); |
John Thompson | 1faca9e | 2009-10-16 01:12:00 +0000 | [diff] [blame] | 371 | // Work around some issues with Visual C++ headerws. |
| 372 | if (LangOpts.CPlusPlus) { |
| 373 | // Since we define wchar_t in C++ mode. |
| 374 | DefineBuiltinMacro(Buf, "_WCHAR_T_DEFINED=1"); |
| 375 | DefineBuiltinMacro(Buf, "_NATIVE_WCHAR_T_DEFINED=1"); |
| 376 | // FIXME: This should be temporary until we have a __pragma |
| 377 | // solution, to avoid some errors flagged in VC++ headers. |
| 378 | DefineBuiltinMacro(Buf, "_CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES=0"); |
| 379 | } |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 380 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 381 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 382 | if (LangOpts.Optimize) |
| 383 | DefineBuiltinMacro(Buf, "__OPTIMIZE__=1"); |
| 384 | if (LangOpts.OptimizeSize) |
| 385 | DefineBuiltinMacro(Buf, "__OPTIMIZE_SIZE__=1"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 387 | // Initialize target-specific preprocessor defines. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 389 | // Define type sizing macros based on the target properties. |
| 390 | assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far"); |
| 391 | DefineBuiltinMacro(Buf, "__CHAR_BIT__=8"); |
| 392 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 393 | DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Buf); |
Chris Lattner | e4a8c64 | 2009-11-05 21:21:32 +0000 | [diff] [blame] | 394 | DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Buf); |
| 395 | DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Buf); |
| 396 | DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Buf); |
| 397 | DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Buf); |
Chris Lattner | 0fb5bbd | 2009-11-12 08:04:33 +0000 | [diff] [blame] | 398 | DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Buf); |
Chris Lattner | e4a8c64 | 2009-11-05 21:21:32 +0000 | [diff] [blame] | 399 | DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Buf); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 400 | |
Ken Dyck | 9b25f78 | 2009-11-19 13:18:59 +0000 | [diff] [blame] | 401 | DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Buf); |
| 402 | DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Buf); |
Ken Dyck | c0c9829 | 2009-11-18 13:52:57 +0000 | [diff] [blame] | 403 | DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Buf); |
Ken Dyck | 9b25f78 | 2009-11-19 13:18:59 +0000 | [diff] [blame] | 404 | DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Buf); |
Ken Dyck | 056efe0 | 2009-11-19 12:21:52 +0000 | [diff] [blame] | 405 | DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Buf); |
Ken Dyck | 9b25f78 | 2009-11-19 13:18:59 +0000 | [diff] [blame] | 406 | DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Buf); |
Ken Dyck | 24cfcf1 | 2009-11-18 20:05:48 +0000 | [diff] [blame] | 407 | DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Buf); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 408 | DefineType("__SIZE_TYPE__", TI.getSizeType(), Buf); |
Ken Dyck | 5751286 | 2009-11-19 13:42:09 +0000 | [diff] [blame] | 409 | DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Buf); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 410 | DefineType("__WCHAR_TYPE__", TI.getWCharType(), Buf); |
Ken Dyck | 0138b9e | 2009-11-19 15:47:58 +0000 | [diff] [blame] | 411 | DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Buf); |
Chris Lattner | 6720492 | 2009-10-21 04:59:34 +0000 | [diff] [blame] | 412 | DefineType("__WINT_TYPE__", TI.getWIntType(), Buf); |
Ken Dyck | a1f677c | 2009-11-19 14:16:57 +0000 | [diff] [blame] | 413 | DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Buf); |
Ken Dyck | adc8511 | 2009-11-22 15:41:04 +0000 | [diff] [blame] | 414 | DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 415 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 416 | DefineFloatMacros(Buf, "FLT", &TI.getFloatFormat()); |
| 417 | DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat()); |
| 418 | DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat()); |
| 419 | |
| 420 | // Define a __POINTER_WIDTH__ macro for stdint.h. |
Kovarththanan Rajaratnam | b53c7d4 | 2010-01-07 16:01:54 +0000 | [diff] [blame] | 421 | char MacroBuf[60]; |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 422 | sprintf(MacroBuf, "__POINTER_WIDTH__=%d", (int)TI.getPointerWidth(0)); |
| 423 | DefineBuiltinMacro(Buf, MacroBuf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | |
Eli Friedman | 9ffd4a9 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 425 | if (!LangOpts.CharIsSigned) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | DefineBuiltinMacro(Buf, "__CHAR_UNSIGNED__"); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 427 | |
Chris Lattner | 55c9877 | 2009-11-12 08:08:27 +0000 | [diff] [blame] | 428 | // Define exact-width integer types for stdint.h |
| 429 | sprintf(MacroBuf, "__INT%d_TYPE__=char", TI.getCharWidth()); |
| 430 | DefineBuiltinMacro(Buf, MacroBuf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 55c9877 | 2009-11-12 08:08:27 +0000 | [diff] [blame] | 432 | if (TI.getShortWidth() > TI.getCharWidth()) |
| 433 | DefineExactWidthIntType(TargetInfo::SignedShort, TI, Buf); |
| 434 | |
| 435 | if (TI.getIntWidth() > TI.getShortWidth()) |
| 436 | DefineExactWidthIntType(TargetInfo::SignedInt, TI, Buf); |
| 437 | |
| 438 | if (TI.getLongWidth() > TI.getIntWidth()) |
| 439 | DefineExactWidthIntType(TargetInfo::SignedLong, TI, Buf); |
| 440 | |
| 441 | if (TI.getLongLongWidth() > TI.getLongWidth()) |
| 442 | DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Buf); |
| 443 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 444 | // Add __builtin_va_list typedef. |
| 445 | { |
| 446 | const char *VAList = TI.getVAListDeclaration(); |
| 447 | Buf.insert(Buf.end(), VAList, VAList+strlen(VAList)); |
| 448 | Buf.push_back('\n'); |
| 449 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 451 | if (const char *Prefix = TI.getUserLabelPrefix()) { |
| 452 | sprintf(MacroBuf, "__USER_LABEL_PREFIX__=%s", Prefix); |
| 453 | DefineBuiltinMacro(Buf, MacroBuf); |
| 454 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 456 | // Build configuration options. FIXME: these should be controlled by |
| 457 | // command line options or something. |
| 458 | DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0"); |
| 459 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 460 | if (LangOpts.GNUInline) |
| 461 | DefineBuiltinMacro(Buf, "__GNUC_GNU_INLINE__=1"); |
| 462 | else |
| 463 | DefineBuiltinMacro(Buf, "__GNUC_STDC_INLINE__=1"); |
| 464 | |
| 465 | if (LangOpts.NoInline) |
| 466 | DefineBuiltinMacro(Buf, "__NO_INLINE__=1"); |
| 467 | |
| 468 | if (unsigned PICLevel = LangOpts.PICLevel) { |
| 469 | sprintf(MacroBuf, "__PIC__=%d", PICLevel); |
| 470 | DefineBuiltinMacro(Buf, MacroBuf); |
| 471 | |
| 472 | sprintf(MacroBuf, "__pic__=%d", PICLevel); |
| 473 | DefineBuiltinMacro(Buf, MacroBuf); |
| 474 | } |
| 475 | |
| 476 | // Macros to control C99 numerics and <float.h> |
| 477 | DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0"); |
| 478 | DefineBuiltinMacro(Buf, "__FLT_RADIX__=2"); |
| 479 | sprintf(MacroBuf, "__DECIMAL_DIG__=%d", |
Eli Friedman | 57406b2 | 2009-05-23 03:50:01 +0000 | [diff] [blame] | 480 | PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36)); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 481 | DefineBuiltinMacro(Buf, MacroBuf); |
Bill Wendling | d63bbad | 2009-06-28 07:36:13 +0000 | [diff] [blame] | 482 | |
Bill Wendling | 1835107 | 2009-06-28 23:01:01 +0000 | [diff] [blame] | 483 | if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn) |
Bill Wendling | d63bbad | 2009-06-28 07:36:13 +0000 | [diff] [blame] | 484 | DefineBuiltinMacro(Buf, "__SSP__=1"); |
Bill Wendling | 1835107 | 2009-06-28 23:01:01 +0000 | [diff] [blame] | 485 | else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq) |
Bill Wendling | d63bbad | 2009-06-28 07:36:13 +0000 | [diff] [blame] | 486 | DefineBuiltinMacro(Buf, "__SSP_ALL__=2"); |
| 487 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 488 | // Get other target #defines. |
| 489 | TI.getTargetDefines(LangOpts, Buf); |
| 490 | } |
| 491 | |
Douglas Gregor | 6ae34ab | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 492 | // Initialize the remapping of files to alternative contents, e.g., |
| 493 | // those specified through other files. |
| 494 | static void InitializeFileRemapping(Diagnostic &Diags, |
| 495 | SourceManager &SourceMgr, |
| 496 | FileManager &FileMgr, |
| 497 | const PreprocessorOptions &InitOpts) { |
| 498 | // Remap files in the source manager. |
| 499 | for (PreprocessorOptions::remapped_file_iterator |
| 500 | Remap = InitOpts.remapped_file_begin(), |
| 501 | RemapEnd = InitOpts.remapped_file_end(); |
| 502 | Remap != RemapEnd; |
| 503 | ++Remap) { |
| 504 | // Find the file that we're mapping to. |
| 505 | const FileEntry *ToFile = FileMgr.getFile(Remap->second); |
| 506 | if (!ToFile) { |
| 507 | Diags.Report(diag::err_fe_remap_missing_to_file) |
| 508 | << Remap->first << Remap->second; |
| 509 | continue; |
| 510 | } |
| 511 | |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 512 | // Create the file entry for the file that we're mapping from. |
| 513 | const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first, |
| 514 | ToFile->getSize(), |
| 515 | 0); |
Douglas Gregor | 6ae34ab | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 516 | if (!FromFile) { |
Douglas Gregor | 6ae34ab | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 517 | Diags.Report(diag::err_fe_remap_missing_from_file) |
| 518 | << Remap->first; |
| 519 | continue; |
| 520 | } |
| 521 | |
| 522 | // Load the contents of the file we're mapping to. |
| 523 | std::string ErrorStr; |
| 524 | const llvm::MemoryBuffer *Buffer |
| 525 | = llvm::MemoryBuffer::getFile(ToFile->getName(), &ErrorStr); |
| 526 | if (!Buffer) { |
| 527 | Diags.Report(diag::err_fe_error_opening) |
| 528 | << Remap->second << ErrorStr; |
| 529 | continue; |
| 530 | } |
| 531 | |
| 532 | // Override the contents of the "from" file with the contents of |
| 533 | // the "to" file. |
| 534 | SourceMgr.overrideFileContents(FromFile, Buffer); |
| 535 | } |
| 536 | } |
| 537 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 538 | /// InitializePreprocessor - Initialize the preprocessor getting it and the |
| 539 | /// environment ready to process a single file. This returns true on error. |
| 540 | /// |
Daniel Dunbar | 181aaee | 2009-11-04 21:13:15 +0000 | [diff] [blame] | 541 | void clang::InitializePreprocessor(Preprocessor &PP, |
Daniel Dunbar | 2856ae44 | 2009-11-11 21:44:42 +0000 | [diff] [blame] | 542 | const PreprocessorOptions &InitOpts, |
| 543 | const HeaderSearchOptions &HSOpts) { |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 544 | std::vector<char> PredefineBuffer; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | |
Douglas Gregor | 6ae34ab | 2009-12-02 16:32:41 +0000 | [diff] [blame] | 546 | InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(), |
| 547 | PP.getFileManager(), InitOpts); |
| 548 | |
Chris Lattner | ed462a8 | 2009-04-22 03:42:19 +0000 | [diff] [blame] | 549 | const char *LineDirective = "# 1 \"<built-in>\" 3\n"; |
| 550 | PredefineBuffer.insert(PredefineBuffer.end(), |
| 551 | LineDirective, LineDirective+strlen(LineDirective)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 553 | // Install things like __POWERPC__, __GNUC__, etc into the macro table. |
Daniel Dunbar | d6ea902 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 554 | if (InitOpts.UsePredefines) |
Chris Lattner | e9d7d78 | 2009-11-03 19:50:27 +0000 | [diff] [blame] | 555 | InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(), |
| 556 | PredefineBuffer); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 558 | // Add on the predefines from the driver. Wrap in a #line directive to report |
| 559 | // that they come from the command line. |
Chris Lattner | ed462a8 | 2009-04-22 03:42:19 +0000 | [diff] [blame] | 560 | LineDirective = "# 1 \"<command line>\" 1\n"; |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 561 | PredefineBuffer.insert(PredefineBuffer.end(), |
| 562 | LineDirective, LineDirective+strlen(LineDirective)); |
| 563 | |
| 564 | // Process #define's and #undef's in the order they are given. |
Daniel Dunbar | d6ea902 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 565 | for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) { |
| 566 | if (InitOpts.Macros[i].second) // isUndef |
| 567 | UndefineBuiltinMacro(PredefineBuffer, InitOpts.Macros[i].first.c_str()); |
Chris Lattner | e08c43a | 2009-04-21 06:00:24 +0000 | [diff] [blame] | 568 | else |
Daniel Dunbar | 6dac935 | 2009-12-03 09:14:12 +0000 | [diff] [blame] | 569 | DefineBuiltinMacro(PredefineBuffer, InitOpts.Macros[i].first.c_str(), |
| 570 | &PP.getDiagnostics()); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | // If -imacros are specified, include them now. These are processed before |
| 574 | // any -include directives. |
Daniel Dunbar | d6ea902 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 575 | for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i) |
| 576 | AddImplicitIncludeMacros(PredefineBuffer, InitOpts.MacroIncludes[i]); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 577 | |
| 578 | // Process -include directives. |
Daniel Dunbar | d6ea902 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 579 | for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) { |
| 580 | const std::string &Path = InitOpts.Includes[i]; |
| 581 | if (Path == InitOpts.ImplicitPTHInclude) |
| 582 | AddImplicitIncludePTH(PredefineBuffer, PP, Path); |
Chris Lattner | e08c43a | 2009-04-21 06:00:24 +0000 | [diff] [blame] | 583 | else |
Daniel Dunbar | d6ea902 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 584 | AddImplicitInclude(PredefineBuffer, Path); |
Eli Friedman | b188455 | 2009-06-15 09:57:52 +0000 | [diff] [blame] | 585 | } |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 586 | |
Rafael Espindola | f8a04a1 | 2009-12-01 18:28:16 +0000 | [diff] [blame] | 587 | // Exit the command line and go back to <built-in> (2 is LC_LEAVE). |
| 588 | LineDirective = "# 1 \"<built-in>\" 2\n"; |
| 589 | PredefineBuffer.insert(PredefineBuffer.end(), |
| 590 | LineDirective, LineDirective+strlen(LineDirective)); |
| 591 | |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 592 | // Null terminate PredefinedBuffer and add it. |
| 593 | PredefineBuffer.push_back(0); |
| 594 | PP.setPredefines(&PredefineBuffer[0]); |
Daniel Dunbar | 2856ae44 | 2009-11-11 21:44:42 +0000 | [diff] [blame] | 595 | |
| 596 | // Initialize the header search object. |
| 597 | ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts, |
| 598 | PP.getLangOptions(), |
| 599 | PP.getTargetInfo().getTriple()); |
Chris Lattner | 2f5693f | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 600 | } |