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