示例#1
0
static void InitializePredefinedMacros(const TargetInfo &TI,
                                       const LangOptions &LangOpts,
                                       const FrontendOptions &FEOpts,
                                       MacroBuilder &Builder) {
  // Compiler version introspection macros.
  Builder.defineMacro("__llvm__");  // LLVM Backend
  Builder.defineMacro("__clang__"); // Clang Frontend
#define TOSTR2(X) #X
#define TOSTR(X) TOSTR2(X)
  Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR));
  Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR));
#ifdef CLANG_VERSION_PATCHLEVEL
  Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL));
#else
  Builder.defineMacro("__clang_patchlevel__", "0");
#endif
  Builder.defineMacro("__clang_version__", 
                      "\"" CLANG_VERSION_STRING " ("
                      + getClangFullRepositoryVersion() + ")\"");
#undef TOSTR
#undef TOSTR2
  if (!LangOpts.MicrosoftMode) {
    // Currently claim to be compatible with GCC 4.2.1-5621, but only if we're
    // not compiling for MSVC compatibility
    Builder.defineMacro("__GNUC_MINOR__", "2");
    Builder.defineMacro("__GNUC_PATCHLEVEL__", "1");
    Builder.defineMacro("__GNUC__", "4");
    Builder.defineMacro("__GXX_ABI_VERSION", "1002");
  }

  // Define macros for the C11 / C++11 memory orderings
  Builder.defineMacro("__ATOMIC_RELAXED", "0");
  Builder.defineMacro("__ATOMIC_CONSUME", "1");
  Builder.defineMacro("__ATOMIC_ACQUIRE", "2");
  Builder.defineMacro("__ATOMIC_RELEASE", "3");
  Builder.defineMacro("__ATOMIC_ACQ_REL", "4");
  Builder.defineMacro("__ATOMIC_SEQ_CST", "5");

  // Support for #pragma redefine_extname (Sun compatibility)
  Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1");

  // As sad as it is, enough software depends on the __VERSION__ for version
  // checks that it is necessary to report 4.2.1 (the base GCC version we claim
  // compatibility with) first.
  Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible " + 
                      Twine(getClangFullCPPVersion()) + "\"");

  // Initialize language-specific preprocessor defines.

  // Standard conforming mode?
  if (!LangOpts.GNUMode)
    Builder.defineMacro("__STRICT_ANSI__");

  if (LangOpts.CPlusPlus0x)
    Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__");

  if (LangOpts.ObjC1) {
    if (LangOpts.ObjCNonFragileABI) {
      Builder.defineMacro("__OBJC2__");
      
      if (LangOpts.ObjCExceptions)
        Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS");
    }

    if (LangOpts.getGC() != LangOptions::NonGC)
      Builder.defineMacro("__OBJC_GC__");

    if (LangOpts.NeXTRuntime)
      Builder.defineMacro("__NEXT_RUNTIME__");
  }

  // darwin_constant_cfstrings controls this. This is also dependent
  // on other things like the runtime I believe.  This is set even for C code.
  if (!LangOpts.NoConstantCFStrings)
      Builder.defineMacro("__CONSTANT_CFSTRINGS__");

  if (LangOpts.ObjC2)
    Builder.defineMacro("OBJC_NEW_PROPERTIES");

  if (LangOpts.PascalStrings)
    Builder.defineMacro("__PASCAL_STRINGS__");

  if (LangOpts.Blocks) {
    Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))");
    Builder.defineMacro("__BLOCKS__");
  }

  if (LangOpts.CXXExceptions)
    Builder.defineMacro("__EXCEPTIONS");
  if (LangOpts.RTTI)
    Builder.defineMacro("__GXX_RTTI");
  if (LangOpts.SjLjExceptions)
    Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__");

  if (LangOpts.Deprecated)
    Builder.defineMacro("__DEPRECATED");

  if (LangOpts.CPlusPlus) {
    Builder.defineMacro("__GNUG__", "4");
    Builder.defineMacro("__GXX_WEAK__");
    Builder.defineMacro("__private_extern__", "extern");
  }

  if (LangOpts.MicrosoftExt) {
    // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however
    // VC++ appears to only like __FUNCTION__.
    Builder.defineMacro("__PRETTY_FUNCTION__", "__FUNCTION__");
    // Work around some issues with Visual C++ headerws.
    if (LangOpts.CPlusPlus) {
      // Since we define wchar_t in C++ mode.
      Builder.defineMacro("_WCHAR_T_DEFINED");
      Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED");
      // FIXME: Support Microsoft's __identifier extension in the lexer.
      Builder.append("#define __identifier(x) x");
      Builder.append("class type_info;");
    }

    if (LangOpts.CPlusPlus0x) {
      Builder.defineMacro("_HAS_CHAR16_T_LANGUAGE_SUPPORT", "1");
    }
  }

  if (LangOpts.Optimize)
    Builder.defineMacro("__OPTIMIZE__");
  if (LangOpts.OptimizeSize)
    Builder.defineMacro("__OPTIMIZE_SIZE__");

  if (LangOpts.FastMath)
    Builder.defineMacro("__FAST_MATH__");

  // Initialize target-specific preprocessor defines.

  // Define type sizing macros based on the target properties.
  assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
  Builder.defineMacro("__CHAR_BIT__", "8");

  DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Builder);
  DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder);
  DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder);
  DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder);
  DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder);
  DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder);
  DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder);

  DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder);
  DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder);
  DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder);
  DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder);
  DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder);
  DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder);
  DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder);
  DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder);
  DefineTypeSizeof("__SIZEOF_PTRDIFF_T__",
                   TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder);
  DefineTypeSizeof("__SIZEOF_SIZE_T__",
                   TI.getTypeWidth(TI.getSizeType()), TI, Builder);
  DefineTypeSizeof("__SIZEOF_WCHAR_T__",
                   TI.getTypeWidth(TI.getWCharType()), TI, Builder);
  DefineTypeSizeof("__SIZEOF_WINT_T__",
                   TI.getTypeWidth(TI.getWIntType()), TI, Builder);

  DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder);
  DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder);
  DefineTypeWidth("__INTMAX_WIDTH__",  TI.getIntMaxType(), TI, Builder);
  DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder);
  DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder);
  DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder);
  DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder);
  DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder);
  DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder);
  DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder);
  DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder);
  DefineType("__WINT_TYPE__", TI.getWIntType(), Builder);
  DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder);
  DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder);
  DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder);
  DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder);

  DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat());
  DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat());
  DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat());

  // Define a __POINTER_WIDTH__ macro for stdint.h.
  Builder.defineMacro("__POINTER_WIDTH__",
                      Twine((int)TI.getPointerWidth(0)));

  if (!LangOpts.CharIsSigned)
    Builder.defineMacro("__CHAR_UNSIGNED__");

  if (!TargetInfo::isTypeSigned(TI.getWCharType()))
    Builder.defineMacro("__WCHAR_UNSIGNED__");

  if (!TargetInfo::isTypeSigned(TI.getWIntType()))
    Builder.defineMacro("__WINT_UNSIGNED__");

  // Define exact-width integer types for stdint.h
  Builder.defineMacro("__INT" + Twine(TI.getCharWidth()) + "_TYPE__",
                      "char");

  if (TI.getShortWidth() > TI.getCharWidth())
    DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder);

  if (TI.getIntWidth() > TI.getShortWidth())
    DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder);

  if (TI.getLongWidth() > TI.getIntWidth())
    DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder);

  if (TI.getLongLongWidth() > TI.getLongWidth())
    DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder);

  // Add __builtin_va_list typedef.
  Builder.append(TI.getVAListDeclaration());

  if (const char *Prefix = TI.getUserLabelPrefix())
    Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix);

  // Build configuration options.  FIXME: these should be controlled by
  // command line options or something.
  Builder.defineMacro("__FINITE_MATH_ONLY__", "0");

  if (LangOpts.GNUInline)
    Builder.defineMacro("__GNUC_GNU_INLINE__");
  else
    Builder.defineMacro("__GNUC_STDC_INLINE__");

  // The value written by __atomic_test_and_set.
  // FIXME: This is target-dependent.
  Builder.defineMacro("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1");

  // Used by libstdc++ to implement ATOMIC_<foo>_LOCK_FREE.
  unsigned InlineWidthBits = TI.getMaxAtomicInlineWidth();
#define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \
  Builder.defineMacro("__GCC_ATOMIC_" #TYPE "_LOCK_FREE", \
                      getLockFreeValue(TI.get##Type##Width(), \
                                       TI.get##Type##Align(), \
                                       InlineWidthBits));
  DEFINE_LOCK_FREE_MACRO(BOOL, Bool);
  DEFINE_LOCK_FREE_MACRO(CHAR, Char);
  DEFINE_LOCK_FREE_MACRO(CHAR16_T, Char16);
  DEFINE_LOCK_FREE_MACRO(CHAR32_T, Char32);
  DEFINE_LOCK_FREE_MACRO(WCHAR_T, WChar);
  DEFINE_LOCK_FREE_MACRO(SHORT, Short);
  DEFINE_LOCK_FREE_MACRO(INT, Int);
  DEFINE_LOCK_FREE_MACRO(LONG, Long);
  DEFINE_LOCK_FREE_MACRO(LLONG, LongLong);
  Builder.defineMacro("__GCC_ATOMIC_POINTER_LOCK_FREE",
                      getLockFreeValue(TI.getPointerWidth(0),
                                       TI.getPointerAlign(0),
                                       InlineWidthBits));
#undef DEFINE_LOCK_FREE_MACRO

  if (LangOpts.NoInlineDefine)
    Builder.defineMacro("__NO_INLINE__");

  if (unsigned PICLevel = LangOpts.PICLevel) {
    Builder.defineMacro("__PIC__", Twine(PICLevel));
    Builder.defineMacro("__pic__", Twine(PICLevel));
  }
  if (unsigned PIELevel = LangOpts.PIELevel) {
    Builder.defineMacro("__PIE__", Twine(PIELevel));
    Builder.defineMacro("__pie__", Twine(PIELevel));
  }

  // Macros to control C99 numerics and <float.h>
  Builder.defineMacro("__FLT_EVAL_METHOD__", Twine(TI.getFloatEvalMethod()));
  Builder.defineMacro("__FLT_RADIX__", "2");
  int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36);
  Builder.defineMacro("__DECIMAL_DIG__", Twine(Dig));

  if (LangOpts.getStackProtector() == LangOptions::SSPOn)
    Builder.defineMacro("__SSP__");
  else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
    Builder.defineMacro("__SSP_ALL__", "2");

  if (FEOpts.ProgramAction == frontend::RewriteObjC)
    Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");

  // Define a macro that exists only when using the static analyzer.
  if (FEOpts.ProgramAction == frontend::RunAnalysis)
    Builder.defineMacro("__clang_analyzer__");

  if (LangOpts.FastRelaxedMath)
    Builder.defineMacro("__FAST_RELAXED_MATH__");

  if (LangOpts.ObjCAutoRefCount) {
    Builder.defineMacro("__weak", "__attribute__((objc_ownership(weak)))");
    Builder.defineMacro("__strong", "__attribute__((objc_ownership(strong)))");
    Builder.defineMacro("__autoreleasing",
                        "__attribute__((objc_ownership(autoreleasing)))");
    Builder.defineMacro("__unsafe_unretained",
                        "__attribute__((objc_ownership(none)))");
  }

  // Get other target #defines.
  TI.getTargetDefines(LangOpts, Builder);
}
示例#2
0
static void InitializePredefinedMacros(const TargetInfo &TI,
                                       const LangOptions &LangOpts,
                                       std::vector<char> &Buf) {
  char MacroBuf[60];
  // Compiler version introspection macros.
  DefineBuiltinMacro(Buf, "__llvm__=1");   // LLVM Backend
  DefineBuiltinMacro(Buf, "__clang__=1");  // Clang Frontend

  // Currently claim to be compatible with GCC 4.2.1-5621.
  DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2");
  DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
  DefineBuiltinMacro(Buf, "__GNUC__=4");
  DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
  DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 Compatible Clang Compiler\"");


  // Initialize language-specific preprocessor defines.

  // These should all be defined in the preprocessor according to the
  // current language configuration.
  if (!LangOpts.Microsoft)
    DefineBuiltinMacro(Buf, "__STDC__=1");
  if (LangOpts.AsmPreprocessor)
    DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");

  if (!LangOpts.CPlusPlus) {
    if (LangOpts.C99)
      DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
    else if (!LangOpts.GNUMode && LangOpts.Digraphs)
      DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
  }

  // Standard conforming mode?
  if (!LangOpts.GNUMode)
    DefineBuiltinMacro(Buf, "__STRICT_ANSI__=1");

  if (LangOpts.CPlusPlus0x)
    DefineBuiltinMacro(Buf, "__GXX_EXPERIMENTAL_CXX0X__");

  if (LangOpts.Freestanding)
    DefineBuiltinMacro(Buf, "__STDC_HOSTED__=0");
  else
    DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");

  if (LangOpts.ObjC1) {
    DefineBuiltinMacro(Buf, "__OBJC__=1");
    if (LangOpts.ObjCNonFragileABI) {
      DefineBuiltinMacro(Buf, "__OBJC2__=1");
      DefineBuiltinMacro(Buf, "OBJC_ZEROCOST_EXCEPTIONS=1");
    }

    if (LangOpts.getGCMode() != LangOptions::NonGC)
      DefineBuiltinMacro(Buf, "__OBJC_GC__=1");

    if (LangOpts.NeXTRuntime)
      DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1");
  }

  // darwin_constant_cfstrings controls this. This is also dependent
  // on other things like the runtime I believe.  This is set even for C code.
  DefineBuiltinMacro(Buf, "__CONSTANT_CFSTRINGS__=1");

  if (LangOpts.ObjC2)
    DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES");

  if (LangOpts.PascalStrings)
    DefineBuiltinMacro(Buf, "__PASCAL_STRINGS__");

  if (LangOpts.Blocks) {
    DefineBuiltinMacro(Buf, "__block=__attribute__((__blocks__(byref)))");
    DefineBuiltinMacro(Buf, "__BLOCKS__=1");
  }

  if (LangOpts.Exceptions)
    DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");

  if (LangOpts.CPlusPlus) {
    DefineBuiltinMacro(Buf, "__DEPRECATED=1");
    DefineBuiltinMacro(Buf, "__GNUG__=4");
    DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
    if (LangOpts.GNUMode)
      DefineBuiltinMacro(Buf, "__cplusplus=1");
    else
      // C++ [cpp.predefined]p1:
      //   The name_ _cplusplusis defined to the value199711Lwhen compiling a
      //   C++ translation unit.
      DefineBuiltinMacro(Buf, "__cplusplus=199711L");
    DefineBuiltinMacro(Buf, "__private_extern__=extern");
    // Ugly hack to work with GNU libstdc++.
    DefineBuiltinMacro(Buf, "_GNU_SOURCE=1");
  }

  if (LangOpts.Microsoft) {
    // Filter out some microsoft extensions when trying to parse in ms-compat
    // mode.
    DefineBuiltinMacro(Buf, "__int8=__INT8_TYPE__");
    DefineBuiltinMacro(Buf, "__int16=__INT16_TYPE__");
    DefineBuiltinMacro(Buf, "__int32=__INT32_TYPE__");
    DefineBuiltinMacro(Buf, "__int64=__INT64_TYPE__");
    // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however
    // VC++ appears to only like __FUNCTION__.
    DefineBuiltinMacro(Buf, "__PRETTY_FUNCTION__=__FUNCTION__");
    // Work around some issues with Visual C++ headerws.
    if (LangOpts.CPlusPlus) {
      // Since we define wchar_t in C++ mode.
      DefineBuiltinMacro(Buf, "_WCHAR_T_DEFINED=1");
      DefineBuiltinMacro(Buf, "_NATIVE_WCHAR_T_DEFINED=1");
      // FIXME:  This should be temporary until we have a __pragma
      // solution, to avoid some errors flagged in VC++ headers.
      DefineBuiltinMacro(Buf, "_CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES=0");
    }
  }

  if (LangOpts.Optimize)
    DefineBuiltinMacro(Buf, "__OPTIMIZE__=1");
  if (LangOpts.OptimizeSize)
    DefineBuiltinMacro(Buf, "__OPTIMIZE_SIZE__=1");

  // Initialize target-specific preprocessor defines.

  // Define type sizing macros based on the target properties.
  assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
  DefineBuiltinMacro(Buf, "__CHAR_BIT__=8");

  DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Buf);
  DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Buf);
  DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Buf);
  DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Buf);
  DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Buf);
  DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Buf);
  DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Buf);

  DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Buf);
  DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Buf);
  DefineTypeWidth("__INTMAX_WIDTH__",  TI.getIntMaxType(), TI, Buf);
  DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Buf);
  DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Buf);
  DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Buf);
  DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Buf);
  DefineType("__SIZE_TYPE__", TI.getSizeType(), Buf);
  DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Buf);
  DefineType("__WCHAR_TYPE__", TI.getWCharType(), Buf);
  DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Buf);
  DefineType("__WINT_TYPE__", TI.getWIntType(), Buf);
  DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Buf);
  DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Buf);

  DefineFloatMacros(Buf, "FLT", &TI.getFloatFormat());
  DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat());
  DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat());

  // Define a __POINTER_WIDTH__ macro for stdint.h.
  sprintf(MacroBuf, "__POINTER_WIDTH__=%d", (int)TI.getPointerWidth(0));
  DefineBuiltinMacro(Buf, MacroBuf);

  if (!LangOpts.CharIsSigned)
    DefineBuiltinMacro(Buf, "__CHAR_UNSIGNED__");

  // Define exact-width integer types for stdint.h
  sprintf(MacroBuf, "__INT%d_TYPE__=char", TI.getCharWidth());
  DefineBuiltinMacro(Buf, MacroBuf);

  if (TI.getShortWidth() > TI.getCharWidth())
    DefineExactWidthIntType(TargetInfo::SignedShort, TI, Buf);
                      
  if (TI.getIntWidth() > TI.getShortWidth())
    DefineExactWidthIntType(TargetInfo::SignedInt, TI, Buf);
                              
  if (TI.getLongWidth() > TI.getIntWidth())
    DefineExactWidthIntType(TargetInfo::SignedLong, TI, Buf);
                                      
  if (TI.getLongLongWidth() > TI.getLongWidth())
    DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Buf);
  
  // Add __builtin_va_list typedef.
  {
    const char *VAList = TI.getVAListDeclaration();
    Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
    Buf.push_back('\n');
  }

  if (const char *Prefix = TI.getUserLabelPrefix()) {
    sprintf(MacroBuf, "__USER_LABEL_PREFIX__=%s", Prefix);
    DefineBuiltinMacro(Buf, MacroBuf);
  }

  // Build configuration options.  FIXME: these should be controlled by
  // command line options or something.
  DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");

  if (LangOpts.GNUInline)
    DefineBuiltinMacro(Buf, "__GNUC_GNU_INLINE__=1");
  else
    DefineBuiltinMacro(Buf, "__GNUC_STDC_INLINE__=1");

  if (LangOpts.NoInline)
    DefineBuiltinMacro(Buf, "__NO_INLINE__=1");

  if (unsigned PICLevel = LangOpts.PICLevel) {
    sprintf(MacroBuf, "__PIC__=%d", PICLevel);
    DefineBuiltinMacro(Buf, MacroBuf);

    sprintf(MacroBuf, "__pic__=%d", PICLevel);
    DefineBuiltinMacro(Buf, MacroBuf);
  }

  // Macros to control C99 numerics and <float.h>
  DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0");
  DefineBuiltinMacro(Buf, "__FLT_RADIX__=2");
  sprintf(MacroBuf, "__DECIMAL_DIG__=%d",
          PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36));
  DefineBuiltinMacro(Buf, MacroBuf);

  if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn)
    DefineBuiltinMacro(Buf, "__SSP__=1");
  else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq)
    DefineBuiltinMacro(Buf, "__SSP_ALL__=2");

  // Get other target #defines.
  TI.getTargetDefines(LangOpts, Buf);
}
示例#3
0
static void InitializePredefinedMacros(const TargetInfo &TI,
                                       const LangOptions &LangOpts,
                                       std::vector<char> &Buf) {
    char MacroBuf[60];
    // Compiler version introspection macros.
    DefineBuiltinMacro(Buf, "__llvm__=1");   // LLVM Backend
    DefineBuiltinMacro(Buf, "__clang__=1");  // Clang Frontend

    // Currently claim to be compatible with GCC 4.2.1-5621.
    DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2");
    DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
    DefineBuiltinMacro(Buf, "__GNUC__=4");
    DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
    DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 Compatible Clang Compiler\"");


    // Initialize language-specific preprocessor defines.

    // These should all be defined in the preprocessor according to the
    // current language configuration.
    if (!LangOpts.Microsoft)
        DefineBuiltinMacro(Buf, "__STDC__=1");
    if (LangOpts.AsmPreprocessor)
        DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");

    if (!LangOpts.CPlusPlus) {
        if (LangOpts.C99)
            DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
        else if (!LangOpts.GNUMode && LangOpts.Digraphs)
            DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
    }

    // Standard conforming mode?
    if (!LangOpts.GNUMode)
        DefineBuiltinMacro(Buf, "__STRICT_ANSI__=1");

    if (LangOpts.CPlusPlus0x)
        DefineBuiltinMacro(Buf, "__GXX_EXPERIMENTAL_CXX0X__");

    if (LangOpts.Freestanding)
        DefineBuiltinMacro(Buf, "__STDC_HOSTED__=0");
    else
        DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");

    if (LangOpts.ObjC1) {
        DefineBuiltinMacro(Buf, "__OBJC__=1");
        if (LangOpts.ObjCNonFragileABI) {
            DefineBuiltinMacro(Buf, "__OBJC2__=1");
            DefineBuiltinMacro(Buf, "OBJC_ZEROCOST_EXCEPTIONS=1");
            DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
        }

        if (LangOpts.getGCMode() != LangOptions::NonGC)
            DefineBuiltinMacro(Buf, "__OBJC_GC__=1");

        if (LangOpts.NeXTRuntime)
            DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1");
    }

    // darwin_constant_cfstrings controls this. This is also dependent
    // on other things like the runtime I believe.  This is set even for C code.
    DefineBuiltinMacro(Buf, "__CONSTANT_CFSTRINGS__=1");

    if (LangOpts.ObjC2)
        DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES");

    if (LangOpts.ObjCSenderDispatch)
        DefineBuiltinMacro(Buf, "__OBJC_SENDER_AWARE_DISPATCH__");

    if (LangOpts.PascalStrings)
        DefineBuiltinMacro(Buf, "__PASCAL_STRINGS__");

    if (LangOpts.Blocks) {
        DefineBuiltinMacro(Buf, "__block=__attribute__((__blocks__(byref)))");
        DefineBuiltinMacro(Buf, "__BLOCKS__=1");
    }

    if (LangOpts.CPlusPlus) {
        DefineBuiltinMacro(Buf, "__DEPRECATED=1");
        DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
        DefineBuiltinMacro(Buf, "__GNUG__=4");
        DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
        if (LangOpts.GNUMode)
            DefineBuiltinMacro(Buf, "__cplusplus=1");
        else
            // C++ [cpp.predefined]p1:
            //   The name_ _cplusplusis defined to the value199711Lwhen compiling a
            //   C++ translation unit.
            DefineBuiltinMacro(Buf, "__cplusplus=199711L");
        DefineBuiltinMacro(Buf, "__private_extern__=extern");
    }

    // Filter out some microsoft extensions when trying to parse in ms-compat
    // mode.
    if (LangOpts.Microsoft) {
        DefineBuiltinMacro(Buf, "__int8=__INT8_TYPE__");
        DefineBuiltinMacro(Buf, "__int16=__INT16_TYPE__");
        DefineBuiltinMacro(Buf, "__int32=__INT32_TYPE__");
        DefineBuiltinMacro(Buf, "__int64=__INT64_TYPE__");
    }

    if (LangOpts.Optimize)
        DefineBuiltinMacro(Buf, "__OPTIMIZE__=1");
    if (LangOpts.OptimizeSize)
        DefineBuiltinMacro(Buf, "__OPTIMIZE_SIZE__=1");

    // Initialize target-specific preprocessor defines.

    // Define type sizing macros based on the target properties.
    assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
    DefineBuiltinMacro(Buf, "__CHAR_BIT__=8");

    unsigned IntMaxWidth;
    const char *IntMaxSuffix;
    if (TI.getIntMaxType() == TargetInfo::SignedLongLong) {
        IntMaxWidth = TI.getLongLongWidth();
        IntMaxSuffix = "LL";
    } else if (TI.getIntMaxType() == TargetInfo::SignedLong) {
        IntMaxWidth = TI.getLongWidth();
        IntMaxSuffix = "L";
    } else {
        assert(TI.getIntMaxType() == TargetInfo::SignedInt);
        IntMaxWidth = TI.getIntWidth();
        IntMaxSuffix = "";
    }

    DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Buf);
    DefineTypeSize("__SHRT_MAX__", TI.getShortWidth(), "", true, Buf);
    DefineTypeSize("__INT_MAX__", TI.getIntWidth(), "", true, Buf);
    DefineTypeSize("__LONG_MAX__", TI.getLongWidth(), "L", true, Buf);
    DefineTypeSize("__LONG_LONG_MAX__", TI.getLongLongWidth(), "LL", true, Buf);
    DefineTypeSize("__WCHAR_MAX__", TI.getWCharWidth(), "", true, Buf);
    DefineTypeSize("__INTMAX_MAX__", IntMaxWidth, IntMaxSuffix, true, Buf);

    DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Buf);
    DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Buf);
    DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Buf);
    DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Buf);
    DefineType("__SIZE_TYPE__", TI.getSizeType(), Buf);
    DefineType("__WCHAR_TYPE__", TI.getWCharType(), Buf);
    // FIXME: TargetInfo hookize __WINT_TYPE__.
    DefineBuiltinMacro(Buf, "__WINT_TYPE__=int");

    DefineFloatMacros(Buf, "FLT", &TI.getFloatFormat());
    DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat());
    DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat());

    // Define a __POINTER_WIDTH__ macro for stdint.h.
    sprintf(MacroBuf, "__POINTER_WIDTH__=%d", (int)TI.getPointerWidth(0));
    DefineBuiltinMacro(Buf, MacroBuf);

    if (!LangOpts.CharIsSigned)
        DefineBuiltinMacro(Buf, "__CHAR_UNSIGNED__");

    // Define fixed-sized integer types for stdint.h
    assert(TI.getCharWidth() == 8 && "unsupported target types");
    assert(TI.getShortWidth() == 16 && "unsupported target types");
    DefineBuiltinMacro(Buf, "__INT8_TYPE__=char");
    DefineBuiltinMacro(Buf, "__INT16_TYPE__=short");

    if (TI.getIntWidth() == 32)
        DefineBuiltinMacro(Buf, "__INT32_TYPE__=int");
    else {
        assert(TI.getLongLongWidth() == 32 && "unsupported target types");
        DefineBuiltinMacro(Buf, "__INT32_TYPE__=long long");
    }

    // 16-bit targets doesn't necessarily have a 64-bit type.
    if (TI.getLongLongWidth() == 64)
        DefineType("__INT64_TYPE__", TI.getInt64Type(), Buf);

    // Add __builtin_va_list typedef.
    {
        const char *VAList = TI.getVAListDeclaration();
        Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
        Buf.push_back('\n');
    }

    if (const char *Prefix = TI.getUserLabelPrefix()) {
        sprintf(MacroBuf, "__USER_LABEL_PREFIX__=%s", Prefix);
        DefineBuiltinMacro(Buf, MacroBuf);
    }

    // Build configuration options.  FIXME: these should be controlled by
    // command line options or something.
    DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");

    if (LangOpts.GNUInline)
        DefineBuiltinMacro(Buf, "__GNUC_GNU_INLINE__=1");
    else
        DefineBuiltinMacro(Buf, "__GNUC_STDC_INLINE__=1");

    if (LangOpts.NoInline)
        DefineBuiltinMacro(Buf, "__NO_INLINE__=1");

    if (unsigned PICLevel = LangOpts.PICLevel) {
        sprintf(MacroBuf, "__PIC__=%d", PICLevel);
        DefineBuiltinMacro(Buf, MacroBuf);

        sprintf(MacroBuf, "__pic__=%d", PICLevel);
        DefineBuiltinMacro(Buf, MacroBuf);
    }

    // Macros to control C99 numerics and <float.h>
    DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0");
    DefineBuiltinMacro(Buf, "__FLT_RADIX__=2");
    sprintf(MacroBuf, "__DECIMAL_DIG__=%d",
            PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36));
    DefineBuiltinMacro(Buf, MacroBuf);

    if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn)
        DefineBuiltinMacro(Buf, "__SSP__=1");
    else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq)
        DefineBuiltinMacro(Buf, "__SSP_ALL__=2");

    // Get other target #defines.
    TI.getTargetDefines(LangOpts, Buf);
}