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