1193323Sed//===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file defines several macros, based on the current compiler. This allows 11193323Sed// use of compiler-specific features in a way that remains portable. 12193323Sed// 13193323Sed//===----------------------------------------------------------------------===// 14193323Sed 15193323Sed#ifndef LLVM_SUPPORT_COMPILER_H 16193323Sed#define LLVM_SUPPORT_COMPILER_H 17193323Sed 18249423Sdim#include "llvm/Config/llvm-config.h" 19249423Sdim 20218893Sdim#ifndef __has_feature 21218893Sdim# define __has_feature(x) 0 22218893Sdim#endif 23218893Sdim 24263508Sdim#ifndef __has_attribute 25263508Sdim# define __has_attribute(x) 0 26263508Sdim#endif 27263508Sdim 28263508Sdim#ifndef __has_builtin 29263508Sdim# define __has_builtin(x) 0 30263508Sdim#endif 31263508Sdim 32263508Sdim/// \macro __GNUC_PREREQ 33263508Sdim/// \brief Defines __GNUC_PREREQ if glibc's features.h isn't available. 34263508Sdim#ifndef __GNUC_PREREQ 35263508Sdim# if defined(__GNUC__) && defined(__GNUC_MINOR__) 36263508Sdim# define __GNUC_PREREQ(maj, min) \ 37263508Sdim ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) 38263508Sdim# else 39263508Sdim# define __GNUC_PREREQ(maj, min) 0 40263508Sdim# endif 41263508Sdim#endif 42263508Sdim 43249423Sdim/// \brief Does the compiler support r-value references? 44239462Sdim/// This implies that <utility> provides the one-argument std::move; it 45239462Sdim/// does not imply the existence of any other C++ library features. 46239462Sdim#if (__has_feature(cxx_rvalue_references) \ 47239462Sdim || defined(__GXX_EXPERIMENTAL_CXX0X__) \ 48243830Sdim || (defined(_MSC_VER) && _MSC_VER >= 1600)) 49249423Sdim#define LLVM_HAS_RVALUE_REFERENCES 1 50239462Sdim#else 51249423Sdim#define LLVM_HAS_RVALUE_REFERENCES 0 52239462Sdim#endif 53239462Sdim 54249423Sdim/// \brief Does the compiler support r-value reference *this? 55249423Sdim/// 56249423Sdim/// Sadly, this is separate from just r-value reference support because GCC 57249423Sdim/// implemented everything but this thus far. No release of GCC yet has support 58249423Sdim/// for this feature so it is enabled with Clang only. 59249423Sdim/// FIXME: This should change to a version check when GCC grows support for it. 60249423Sdim#if __has_feature(cxx_rvalue_references) 61249423Sdim#define LLVM_HAS_RVALUE_REFERENCE_THIS 1 62249423Sdim#else 63249423Sdim#define LLVM_HAS_RVALUE_REFERENCE_THIS 0 64249423Sdim#endif 65249423Sdim 66249423Sdim/// \macro LLVM_HAS_CXX11_TYPETRAITS 67249423Sdim/// \brief Does the compiler have the C++11 type traits. 68249423Sdim/// 69249423Sdim/// #include <type_traits> 70249423Sdim/// 71249423Sdim/// * enable_if 72249423Sdim/// * {true,false}_type 73249423Sdim/// * is_constructible 74249423Sdim/// * etc... 75249423Sdim#if defined(__GXX_EXPERIMENTAL_CXX0X__) \ 76249423Sdim || (defined(_MSC_VER) && _MSC_VER >= 1700) 77249423Sdim#define LLVM_HAS_CXX11_TYPETRAITS 1 78249423Sdim#else 79249423Sdim#define LLVM_HAS_CXX11_TYPETRAITS 0 80249423Sdim#endif 81249423Sdim 82249423Sdim/// \macro LLVM_HAS_CXX11_STDLIB 83249423Sdim/// \brief Does the compiler have the C++11 standard library. 84249423Sdim/// 85249423Sdim/// Implies LLVM_HAS_RVALUE_REFERENCES, LLVM_HAS_CXX11_TYPETRAITS 86249423Sdim#if defined(__GXX_EXPERIMENTAL_CXX0X__) \ 87249423Sdim || (defined(_MSC_VER) && _MSC_VER >= 1700) 88249423Sdim#define LLVM_HAS_CXX11_STDLIB 1 89249423Sdim#else 90249423Sdim#define LLVM_HAS_CXX11_STDLIB 0 91249423Sdim#endif 92249423Sdim 93249423Sdim/// \macro LLVM_HAS_VARIADIC_TEMPLATES 94249423Sdim/// \brief Does this compiler support variadic templates. 95249423Sdim/// 96249423Sdim/// Implies LLVM_HAS_RVALUE_REFERENCES and the existence of std::forward. 97249423Sdim#if __has_feature(cxx_variadic_templates) 98249423Sdim# define LLVM_HAS_VARIADIC_TEMPLATES 1 99249423Sdim#else 100249423Sdim# define LLVM_HAS_VARIADIC_TEMPLATES 0 101249423Sdim#endif 102249423Sdim 103239462Sdim/// llvm_move - Expands to ::std::move if the compiler supports 104239462Sdim/// r-value references; otherwise, expands to the argument. 105249423Sdim#if LLVM_HAS_RVALUE_REFERENCES 106239462Sdim#define llvm_move(value) (::std::move(value)) 107239462Sdim#else 108239462Sdim#define llvm_move(value) (value) 109239462Sdim#endif 110239462Sdim 111249423Sdim/// Expands to '&' if r-value references are supported. 112249423Sdim/// 113249423Sdim/// This can be used to provide l-value/r-value overrides of member functions. 114249423Sdim/// The r-value override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS 115249423Sdim#if LLVM_HAS_RVALUE_REFERENCE_THIS 116249423Sdim#define LLVM_LVALUE_FUNCTION & 117249423Sdim#else 118249423Sdim#define LLVM_LVALUE_FUNCTION 119249423Sdim#endif 120249423Sdim 121239462Sdim/// LLVM_DELETED_FUNCTION - Expands to = delete if the compiler supports it. 122239462Sdim/// Use to mark functions as uncallable. Member functions with this should 123243830Sdim/// be declared private so that some behavior is kept in C++03 mode. 124239462Sdim/// 125239462Sdim/// class DontCopy { 126239462Sdim/// private: 127239462Sdim/// DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION; 128239462Sdim/// DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION; 129239462Sdim/// public: 130239462Sdim/// ... 131239462Sdim/// }; 132239462Sdim#if (__has_feature(cxx_deleted_functions) \ 133239462Sdim || defined(__GXX_EXPERIMENTAL_CXX0X__)) 134239462Sdim // No version of MSVC currently supports this. 135239462Sdim#define LLVM_DELETED_FUNCTION = delete 136239462Sdim#else 137239462Sdim#define LLVM_DELETED_FUNCTION 138239462Sdim#endif 139239462Sdim 140243830Sdim/// LLVM_FINAL - Expands to 'final' if the compiler supports it. 141243830Sdim/// Use to mark classes or virtual methods as final. 142249423Sdim#if __has_feature(cxx_override_control) \ 143249423Sdim || (defined(_MSC_VER) && _MSC_VER >= 1700) 144243830Sdim#define LLVM_FINAL final 145243830Sdim#else 146243830Sdim#define LLVM_FINAL 147243830Sdim#endif 148243830Sdim 149243830Sdim/// LLVM_OVERRIDE - Expands to 'override' if the compiler supports it. 150243830Sdim/// Use to mark virtual methods as overriding a base class method. 151249423Sdim#if __has_feature(cxx_override_control) \ 152249423Sdim || (defined(_MSC_VER) && _MSC_VER >= 1700) 153243830Sdim#define LLVM_OVERRIDE override 154243830Sdim#else 155243830Sdim#define LLVM_OVERRIDE 156243830Sdim#endif 157243830Sdim 158249423Sdim#if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__) 159249423Sdim# define LLVM_CONSTEXPR constexpr 160249423Sdim#else 161249423Sdim# define LLVM_CONSTEXPR 162249423Sdim#endif 163249423Sdim 164208599Srdivacky/// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked 165208599Srdivacky/// into a shared library, then the class should be private to the library and 166208599Srdivacky/// not accessible from outside it. Can also be used to mark variables and 167208599Srdivacky/// functions, making them private to any shared library they are linked into. 168263508Sdim/// On PE/COFF targets, library visibility is the default, so this isn't needed. 169263508Sdim#if (__has_attribute(visibility) || __GNUC_PREREQ(4, 0)) && \ 170263508Sdim !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32) 171208599Srdivacky#define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden"))) 172193323Sed#else 173208599Srdivacky#define LLVM_LIBRARY_VISIBILITY 174193323Sed#endif 175193323Sed 176263508Sdim#if __has_attribute(used) || __GNUC_PREREQ(3, 1) 177218893Sdim#define LLVM_ATTRIBUTE_USED __attribute__((__used__)) 178193323Sed#else 179218893Sdim#define LLVM_ATTRIBUTE_USED 180193323Sed#endif 181193323Sed 182263508Sdim#if __has_attribute(warn_unused_result) || __GNUC_PREREQ(3, 4) 183263508Sdim#define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__)) 184263508Sdim#else 185263508Sdim#define LLVM_ATTRIBUTE_UNUSED_RESULT 186263508Sdim#endif 187263508Sdim 188218893Sdim// Some compilers warn about unused functions. When a function is sometimes 189218893Sdim// used or not depending on build settings (e.g. a function only called from 190218893Sdim// within "assert"), this attribute can be used to suppress such warnings. 191218893Sdim// 192218893Sdim// However, it shouldn't be used for unused *variables*, as those have a much 193218893Sdim// more portable solution: 194218893Sdim// (void)unused_var_name; 195218893Sdim// Prefer cast-to-void wherever it is sufficient. 196263508Sdim#if __has_attribute(unused) || __GNUC_PREREQ(3, 1) 197218893Sdim#define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__)) 198201360Srdivacky#else 199218893Sdim#define LLVM_ATTRIBUTE_UNUSED 200201360Srdivacky#endif 201201360Srdivacky 202263508Sdim// FIXME: Provide this for PE/COFF targets. 203263508Sdim#if (__has_attribute(weak) || __GNUC_PREREQ(4, 0)) && \ 204263508Sdim (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)) 205234353Sdim#define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__)) 206199481Srdivacky#else 207234353Sdim#define LLVM_ATTRIBUTE_WEAK 208199481Srdivacky#endif 209199481Srdivacky 210263508Sdim// Prior to clang 3.2, clang did not accept any spelling of 211263508Sdim// __has_attribute(const), so assume it is supported. 212263508Sdim#if defined(__clang__) || defined(__GNUC__) 213263508Sdim// aka 'CONST' but following LLVM Conventions. 214234353Sdim#define LLVM_READNONE __attribute__((__const__)) 215199481Srdivacky#else 216234353Sdim#define LLVM_READNONE 217199481Srdivacky#endif 218199481Srdivacky 219263508Sdim#if __has_attribute(pure) || defined(__GNUC__) 220263508Sdim// aka 'PURE' but following LLVM Conventions. 221234353Sdim#define LLVM_READONLY __attribute__((__pure__)) 222234353Sdim#else 223234353Sdim#define LLVM_READONLY 224234353Sdim#endif 225234353Sdim 226263508Sdim#if __has_builtin(__builtin_expect) || __GNUC_PREREQ(4, 0) 227243830Sdim#define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true) 228243830Sdim#define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false) 229193323Sed#else 230243830Sdim#define LLVM_LIKELY(EXPR) (EXPR) 231243830Sdim#define LLVM_UNLIKELY(EXPR) (EXPR) 232193323Sed#endif 233193323Sed 234193323Sed// C++ doesn't support 'extern template' of template specializations. GCC does, 235193323Sed// but requires __extension__ before it. In the header, use this: 236193323Sed// EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>); 237193323Sed// in the .cpp file, use this: 238193323Sed// TEMPLATE_INSTANTIATION(class foo<bar>); 239193323Sed#ifdef __GNUC__ 240193323Sed#define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X 241193323Sed#define TEMPLATE_INSTANTIATION(X) template X 242193323Sed#else 243193323Sed#define EXTERN_TEMPLATE_INSTANTIATION(X) 244193323Sed#define TEMPLATE_INSTANTIATION(X) 245193323Sed#endif 246193323Sed 247249423Sdim/// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so, 248249423Sdim/// mark a method "not for inlining". 249263508Sdim#if __has_attribute(noinline) || __GNUC_PREREQ(3, 4) 250218893Sdim#define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline)) 251199481Srdivacky#elif defined(_MSC_VER) 252218893Sdim#define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline) 253193323Sed#else 254218893Sdim#define LLVM_ATTRIBUTE_NOINLINE 255193323Sed#endif 256193323Sed 257249423Sdim/// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do 258249423Sdim/// so, mark a method "always inline" because it is performance sensitive. GCC 259249423Sdim/// 3.4 supported this but is buggy in various cases and produces unimplemented 260249423Sdim/// errors, just use it in GCC 4.0 and later. 261263508Sdim#if __has_attribute(always_inline) || __GNUC_PREREQ(4, 0) 262239462Sdim#define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline)) 263218893Sdim#elif defined(_MSC_VER) 264218893Sdim#define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline 265200581Srdivacky#else 266218893Sdim#define LLVM_ATTRIBUTE_ALWAYS_INLINE 267200581Srdivacky#endif 268200581Srdivacky 269198090Srdivacky#ifdef __GNUC__ 270218893Sdim#define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn)) 271199481Srdivacky#elif defined(_MSC_VER) 272218893Sdim#define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn) 273198090Srdivacky#else 274218893Sdim#define LLVM_ATTRIBUTE_NORETURN 275193323Sed#endif 276198090Srdivacky 277249423Sdim/// LLVM_EXTENSION - Support compilers where we have a keyword to suppress 278249423Sdim/// pedantic diagnostics. 279234353Sdim#ifdef __GNUC__ 280234353Sdim#define LLVM_EXTENSION __extension__ 281234353Sdim#else 282234353Sdim#define LLVM_EXTENSION 283234353Sdim#endif 284234353Sdim 285218893Sdim// LLVM_ATTRIBUTE_DEPRECATED(decl, "message") 286218893Sdim#if __has_feature(attribute_deprecated_with_message) 287218893Sdim# define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \ 288218893Sdim decl __attribute__((deprecated(message))) 289218893Sdim#elif defined(__GNUC__) 290218893Sdim# define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \ 291218893Sdim decl __attribute__((deprecated)) 292218893Sdim#elif defined(_MSC_VER) 293218893Sdim# define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \ 294218893Sdim __declspec(deprecated(message)) decl 295218893Sdim#else 296218893Sdim# define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \ 297218893Sdim decl 298198090Srdivacky#endif 299218893Sdim 300249423Sdim/// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands 301249423Sdim/// to an expression which states that it is undefined behavior for the 302249423Sdim/// compiler to reach this point. Otherwise is not defined. 303263508Sdim#if __has_builtin(__builtin_unreachable) || __GNUC_PREREQ(4, 5) 304221345Sdim# define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable() 305249423Sdim#elif defined(_MSC_VER) 306249423Sdim# define LLVM_BUILTIN_UNREACHABLE __assume(false) 307218893Sdim#endif 308221345Sdim 309249423Sdim/// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression 310249423Sdim/// which causes the program to exit abnormally. 311263508Sdim#if __has_builtin(__builtin_trap) || __GNUC_PREREQ(4, 3) 312243830Sdim# define LLVM_BUILTIN_TRAP __builtin_trap() 313243830Sdim#else 314243830Sdim# define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0 315221345Sdim#endif 316243830Sdim 317249423Sdim/// \macro LLVM_ASSUME_ALIGNED 318249423Sdim/// \brief Returns a pointer with an assumed alignment. 319263508Sdim#if __has_builtin(__builtin_assume_aligned) && __GNUC_PREREQ(4, 7) 320249423Sdim# define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a) 321249423Sdim#elif defined(LLVM_BUILTIN_UNREACHABLE) 322263508Sdim// As of today, clang does not support __builtin_assume_aligned. 323249423Sdim# define LLVM_ASSUME_ALIGNED(p, a) \ 324249423Sdim (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p))) 325249423Sdim#else 326249423Sdim# define LLVM_ASSUME_ALIGNED(p, a) (p) 327243830Sdim#endif 328249423Sdim 329249423Sdim/// \macro LLVM_FUNCTION_NAME 330249423Sdim/// \brief Expands to __func__ on compilers which support it. Otherwise, 331249423Sdim/// expands to a compiler-dependent replacement. 332249423Sdim#if defined(_MSC_VER) 333249423Sdim# define LLVM_FUNCTION_NAME __FUNCTION__ 334249423Sdim#else 335249423Sdim# define LLVM_FUNCTION_NAME __func__ 336249423Sdim#endif 337249423Sdim 338249423Sdim#if defined(HAVE_SANITIZER_MSAN_INTERFACE_H) 339249423Sdim# include <sanitizer/msan_interface.h> 340249423Sdim#else 341249423Sdim# define __msan_allocated_memory(p, size) 342249423Sdim# define __msan_unpoison(p, size) 343249423Sdim#endif 344249423Sdim 345249423Sdim/// \macro LLVM_MEMORY_SANITIZER_BUILD 346249423Sdim/// \brief Whether LLVM itself is built with MemorySanitizer instrumentation. 347249423Sdim#if __has_feature(memory_sanitizer) 348249423Sdim# define LLVM_MEMORY_SANITIZER_BUILD 1 349249423Sdim#else 350249423Sdim# define LLVM_MEMORY_SANITIZER_BUILD 0 351249423Sdim#endif 352249423Sdim 353249423Sdim/// \macro LLVM_ADDRESS_SANITIZER_BUILD 354249423Sdim/// \brief Whether LLVM itself is built with AddressSanitizer instrumentation. 355249423Sdim#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) 356249423Sdim# define LLVM_ADDRESS_SANITIZER_BUILD 1 357249423Sdim#else 358249423Sdim# define LLVM_ADDRESS_SANITIZER_BUILD 0 359249423Sdim#endif 360249423Sdim 361249423Sdim/// \macro LLVM_IS_UNALIGNED_ACCESS_FAST 362249423Sdim/// \brief Is unaligned memory access fast on the host machine. 363249423Sdim/// 364249423Sdim/// Don't specialize on alignment for platforms where unaligned memory accesses 365249423Sdim/// generates the same code as aligned memory accesses for common types. 366249423Sdim#if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \ 367249423Sdim defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \ 368249423Sdim defined(_X86_) || defined(__i386) || defined(__i386__) 369249423Sdim# define LLVM_IS_UNALIGNED_ACCESS_FAST 1 370249423Sdim#else 371249423Sdim# define LLVM_IS_UNALIGNED_ACCESS_FAST 0 372249423Sdim#endif 373249423Sdim 374249423Sdim/// \macro LLVM_EXPLICIT 375249423Sdim/// \brief Expands to explicit on compilers which support explicit conversion 376249423Sdim/// operators. Otherwise expands to nothing. 377249423Sdim#if (__has_feature(cxx_explicit_conversions) \ 378249423Sdim || defined(__GXX_EXPERIMENTAL_CXX0X__)) 379249423Sdim#define LLVM_EXPLICIT explicit 380249423Sdim#else 381249423Sdim#define LLVM_EXPLICIT 382249423Sdim#endif 383249423Sdim 384249423Sdim/// \macro LLVM_STATIC_ASSERT 385249423Sdim/// \brief Expands to C/C++'s static_assert on compilers which support it. 386249423Sdim#if __has_feature(cxx_static_assert) 387249423Sdim# define LLVM_STATIC_ASSERT(expr, msg) static_assert(expr, msg) 388249423Sdim#elif __has_feature(c_static_assert) 389249423Sdim# define LLVM_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg) 390249423Sdim#else 391249423Sdim# define LLVM_STATIC_ASSERT(expr, msg) 392249423Sdim#endif 393249423Sdim 394263508Sdim/// \macro LLVM_ENUM_INT_TYPE 395263508Sdim/// \brief Expands to colon followed by the given integral type on compilers 396263508Sdim/// which support C++11 strong enums. This can be used to make enums unsigned 397263508Sdim/// with MSVC. 398263508Sdim#if __has_feature(cxx_strong_enums) 399263508Sdim# define LLVM_ENUM_INT_TYPE(intty) : intty 400263508Sdim#elif defined(_MSC_VER) && _MSC_VER >= 1600 // Added in MSVC 2010. 401263508Sdim# define LLVM_ENUM_INT_TYPE(intty) : intty 402263508Sdim#else 403263508Sdim# define LLVM_ENUM_INT_TYPE(intty) 404249423Sdim#endif 405263508Sdim 406263508Sdim/// \brief Does the compiler support generalized initializers (using braced 407263508Sdim/// lists and std::initializer_list). 408263508Sdim#if __has_feature(cxx_generalized_initializers) 409263508Sdim#define LLVM_HAS_INITIALIZER_LISTS 1 410263508Sdim#else 411263508Sdim#define LLVM_HAS_INITIALIZER_LISTS 0 412263508Sdim#endif 413263508Sdim 414263508Sdim#endif 415