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