Home | History | Annotate | Download | only in Support
      1 //===-- llvm/Support/Compiler.h - Compiler abstraction support --*- 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 defines several macros, based on the current compiler.  This allows
     11 // use of compiler-specific features in a way that remains portable.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_SUPPORT_COMPILER_H
     16 #define LLVM_SUPPORT_COMPILER_H
     17 
     18 #include "llvm/Config/llvm-config.h"
     19 
     20 #ifndef __has_feature
     21 # define __has_feature(x) 0
     22 #endif
     23 
     24 #ifndef __has_extension
     25 # define __has_extension(x) 0
     26 #endif
     27 
     28 #ifndef __has_attribute
     29 # define __has_attribute(x) 0
     30 #endif
     31 
     32 #ifndef __has_builtin
     33 # define __has_builtin(x) 0
     34 #endif
     35 
     36 /// \macro __GNUC_PREREQ
     37 /// \brief Defines __GNUC_PREREQ if glibc's features.h isn't available.
     38 #ifndef __GNUC_PREREQ
     39 # if defined(__GNUC__) && defined(__GNUC_MINOR__)
     40 #  define __GNUC_PREREQ(maj, min) \
     41     ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
     42 # else
     43 #  define __GNUC_PREREQ(maj, min) 0
     44 # endif
     45 #endif
     46 
     47 /// \macro LLVM_MSC_PREREQ
     48 /// \brief Is the compiler MSVC of at least the specified version?
     49 /// The common \param version values to check for are:
     50 ///  * 1700: Microsoft Visual Studio 2012 / 11.0
     51 ///  * 1800: Microsoft Visual Studio 2013 / 12.0
     52 #ifdef _MSC_VER
     53 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
     54 
     55 // We require at least MSVC 2012.
     56 #if !LLVM_MSC_PREREQ(1700)
     57 #error LLVM requires at least MSVC 2012.
     58 #endif
     59 
     60 #else
     61 #define LLVM_MSC_PREREQ(version) 0
     62 #endif
     63 
     64 #ifndef _MSC_VER
     65 #define LLVM_NOEXCEPT noexcept
     66 #else
     67 #define LLVM_NOEXCEPT
     68 #endif
     69 
     70 /// \brief Does the compiler support r-value reference *this?
     71 ///
     72 /// Sadly, this is separate from just r-value reference support because GCC
     73 /// implemented everything but this thus far. No release of GCC yet has support
     74 /// for this feature so it is enabled with Clang only.
     75 /// FIXME: This should change to a version check when GCC grows support for it.
     76 #if __has_feature(cxx_rvalue_references)
     77 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
     78 #else
     79 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
     80 #endif
     81 
     82 /// \macro LLVM_HAS_VARIADIC_TEMPLATES
     83 /// \brief Does this compiler support variadic templates.
     84 ///
     85 /// Implies LLVM_HAS_RVALUE_REFERENCES and the existence of std::forward.
     86 #if __has_feature(cxx_variadic_templates) || LLVM_MSC_PREREQ(1800)
     87 # define LLVM_HAS_VARIADIC_TEMPLATES 1
     88 #else
     89 # define LLVM_HAS_VARIADIC_TEMPLATES 0
     90 #endif
     91 
     92 /// Expands to '&' if r-value references are supported.
     93 ///
     94 /// This can be used to provide l-value/r-value overrides of member functions.
     95 /// The r-value override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
     96 #if LLVM_HAS_RVALUE_REFERENCE_THIS
     97 #define LLVM_LVALUE_FUNCTION &
     98 #else
     99 #define LLVM_LVALUE_FUNCTION
    100 #endif
    101 
    102 /// LLVM_DELETED_FUNCTION - Expands to = delete if the compiler supports it.
    103 /// Use to mark functions as uncallable. Member functions with this should
    104 /// be declared private so that some behavior is kept in C++03 mode.
    105 ///
    106 /// class DontCopy {
    107 /// private:
    108 ///   DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
    109 ///   DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
    110 /// public:
    111 ///   ...
    112 /// };
    113 #if __has_feature(cxx_deleted_functions) || \
    114     defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
    115 #define LLVM_DELETED_FUNCTION = delete
    116 #else
    117 #define LLVM_DELETED_FUNCTION
    118 #endif
    119 
    120 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
    121 # define LLVM_CONSTEXPR constexpr
    122 #else
    123 # define LLVM_CONSTEXPR
    124 #endif
    125 
    126 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
    127 /// into a shared library, then the class should be private to the library and
    128 /// not accessible from outside it.  Can also be used to mark variables and
    129 /// functions, making them private to any shared library they are linked into.
    130 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
    131 #if (__has_attribute(visibility) || __GNUC_PREREQ(4, 0)) &&                    \
    132     !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
    133 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
    134 #else
    135 #define LLVM_LIBRARY_VISIBILITY
    136 #endif
    137 
    138 #if __has_attribute(used) || __GNUC_PREREQ(3, 1)
    139 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
    140 #else
    141 #define LLVM_ATTRIBUTE_USED
    142 #endif
    143 
    144 #if __has_attribute(warn_unused_result) || __GNUC_PREREQ(3, 4)
    145 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
    146 #else
    147 #define LLVM_ATTRIBUTE_UNUSED_RESULT
    148 #endif
    149 
    150 // Some compilers warn about unused functions. When a function is sometimes
    151 // used or not depending on build settings (e.g. a function only called from
    152 // within "assert"), this attribute can be used to suppress such warnings.
    153 //
    154 // However, it shouldn't be used for unused *variables*, as those have a much
    155 // more portable solution:
    156 //   (void)unused_var_name;
    157 // Prefer cast-to-void wherever it is sufficient.
    158 #if __has_attribute(unused) || __GNUC_PREREQ(3, 1)
    159 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
    160 #else
    161 #define LLVM_ATTRIBUTE_UNUSED
    162 #endif
    163 
    164 // FIXME: Provide this for PE/COFF targets.
    165 #if (__has_attribute(weak) || __GNUC_PREREQ(4, 0)) &&                          \
    166     (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
    167 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
    168 #else
    169 #define LLVM_ATTRIBUTE_WEAK
    170 #endif
    171 
    172 // Prior to clang 3.2, clang did not accept any spelling of
    173 // __has_attribute(const), so assume it is supported.
    174 #if defined(__clang__) || defined(__GNUC__)
    175 // aka 'CONST' but following LLVM Conventions.
    176 #define LLVM_READNONE __attribute__((__const__))
    177 #else
    178 #define LLVM_READNONE
    179 #endif
    180 
    181 #if __has_attribute(pure) || defined(__GNUC__)
    182 // aka 'PURE' but following LLVM Conventions.
    183 #define LLVM_READONLY __attribute__((__pure__))
    184 #else
    185 #define LLVM_READONLY
    186 #endif
    187 
    188 #if __has_builtin(__builtin_expect) || __GNUC_PREREQ(4, 0)
    189 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
    190 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
    191 #else
    192 #define LLVM_LIKELY(EXPR) (EXPR)
    193 #define LLVM_UNLIKELY(EXPR) (EXPR)
    194 #endif
    195 
    196 // C++ doesn't support 'extern template' of template specializations.  GCC does,
    197 // but requires __extension__ before it.  In the header, use this:
    198 //   EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
    199 // in the .cpp file, use this:
    200 //   TEMPLATE_INSTANTIATION(class foo<bar>);
    201 #ifdef __GNUC__
    202 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
    203 #define TEMPLATE_INSTANTIATION(X) template X
    204 #else
    205 #define EXTERN_TEMPLATE_INSTANTIATION(X)
    206 #define TEMPLATE_INSTANTIATION(X)
    207 #endif
    208 
    209 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
    210 /// mark a method "not for inlining".
    211 #if __has_attribute(noinline) || __GNUC_PREREQ(3, 4)
    212 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
    213 #elif defined(_MSC_VER)
    214 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
    215 #else
    216 #define LLVM_ATTRIBUTE_NOINLINE
    217 #endif
    218 
    219 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
    220 /// so, mark a method "always inline" because it is performance sensitive. GCC
    221 /// 3.4 supported this but is buggy in various cases and produces unimplemented
    222 /// errors, just use it in GCC 4.0 and later.
    223 #if __has_attribute(always_inline) || __GNUC_PREREQ(4, 0)
    224 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
    225 #elif defined(_MSC_VER)
    226 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
    227 #else
    228 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
    229 #endif
    230 
    231 #ifdef __GNUC__
    232 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
    233 #elif defined(_MSC_VER)
    234 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
    235 #else
    236 #define LLVM_ATTRIBUTE_NORETURN
    237 #endif
    238 
    239 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
    240 /// pedantic diagnostics.
    241 #ifdef __GNUC__
    242 #define LLVM_EXTENSION __extension__
    243 #else
    244 #define LLVM_EXTENSION
    245 #endif
    246 
    247 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
    248 #if __has_feature(attribute_deprecated_with_message)
    249 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
    250   decl __attribute__((deprecated(message)))
    251 #elif defined(__GNUC__)
    252 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
    253   decl __attribute__((deprecated))
    254 #elif defined(_MSC_VER)
    255 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
    256   __declspec(deprecated(message)) decl
    257 #else
    258 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
    259   decl
    260 #endif
    261 
    262 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
    263 /// to an expression which states that it is undefined behavior for the
    264 /// compiler to reach this point.  Otherwise is not defined.
    265 #if __has_builtin(__builtin_unreachable) || __GNUC_PREREQ(4, 5)
    266 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
    267 #elif defined(_MSC_VER)
    268 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
    269 #endif
    270 
    271 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
    272 /// which causes the program to exit abnormally.
    273 #if __has_builtin(__builtin_trap) || __GNUC_PREREQ(4, 3)
    274 # define LLVM_BUILTIN_TRAP __builtin_trap()
    275 #else
    276 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
    277 #endif
    278 
    279 /// \macro LLVM_ASSUME_ALIGNED
    280 /// \brief Returns a pointer with an assumed alignment.
    281 #if __has_builtin(__builtin_assume_aligned) && __GNUC_PREREQ(4, 7)
    282 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
    283 #elif defined(LLVM_BUILTIN_UNREACHABLE)
    284 // As of today, clang does not support __builtin_assume_aligned.
    285 # define LLVM_ASSUME_ALIGNED(p, a) \
    286            (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
    287 #else
    288 # define LLVM_ASSUME_ALIGNED(p, a) (p)
    289 #endif
    290 
    291 /// \macro LLVM_FUNCTION_NAME
    292 /// \brief Expands to __func__ on compilers which support it.  Otherwise,
    293 /// expands to a compiler-dependent replacement.
    294 #if defined(_MSC_VER)
    295 # define LLVM_FUNCTION_NAME __FUNCTION__
    296 #else
    297 # define LLVM_FUNCTION_NAME __func__
    298 #endif
    299 
    300 /// \macro LLVM_MEMORY_SANITIZER_BUILD
    301 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
    302 #if __has_feature(memory_sanitizer)
    303 # define LLVM_MEMORY_SANITIZER_BUILD 1
    304 # include <sanitizer/msan_interface.h>
    305 #else
    306 # define LLVM_MEMORY_SANITIZER_BUILD 0
    307 # define __msan_allocated_memory(p, size)
    308 # define __msan_unpoison(p, size)
    309 #endif
    310 
    311 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
    312 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
    313 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
    314 # define LLVM_ADDRESS_SANITIZER_BUILD 1
    315 #else
    316 # define LLVM_ADDRESS_SANITIZER_BUILD 0
    317 #endif
    318 
    319 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
    320 /// \brief Is unaligned memory access fast on the host machine.
    321 ///
    322 /// Don't specialize on alignment for platforms where unaligned memory accesses
    323 /// generates the same code as aligned memory accesses for common types.
    324 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
    325     defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
    326     defined(_X86_) || defined(__i386) || defined(__i386__)
    327 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
    328 #else
    329 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
    330 #endif
    331 
    332 /// \macro LLVM_EXPLICIT
    333 /// \brief Expands to explicit on compilers which support explicit conversion
    334 /// operators. Otherwise expands to nothing.
    335 #if __has_feature(cxx_explicit_conversions) || \
    336     defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
    337 #define LLVM_EXPLICIT explicit
    338 #else
    339 #define LLVM_EXPLICIT
    340 #endif
    341 
    342 /// \brief Does the compiler support generalized initializers (using braced
    343 /// lists and std::initializer_list).  While clang may claim it supports general
    344 /// initializers, if we're using MSVC's headers, we might not have a usable
    345 /// std::initializer list type from the STL.  Disable this for now.
    346 #if __has_feature(cxx_generalized_initializers) && !defined(_MSC_VER)
    347 #define LLVM_HAS_INITIALIZER_LISTS 1
    348 #else
    349 #define LLVM_HAS_INITIALIZER_LISTS 0
    350 #endif
    351 
    352 /// \brief Mark debug helper function definitions like dump() that should not be
    353 /// stripped from debug builds.
    354 // FIXME: Move this to a private config.h as it's not usable in public headers.
    355 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
    356 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
    357 #else
    358 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
    359 #endif
    360 
    361 #endif
    362