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