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