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 18252723Sdim#include "llvm/Config/llvm-config.h" 19252723Sdim 20218893Sdim#ifndef __has_feature 21218893Sdim# define __has_feature(x) 0 22218893Sdim#endif 23218893Sdim 24263509Sdim#ifndef __has_attribute 25263509Sdim# define __has_attribute(x) 0 26263509Sdim#endif 27263509Sdim 28263509Sdim#ifndef __has_builtin 29263509Sdim# define __has_builtin(x) 0 30263509Sdim#endif 31263509Sdim 32263509Sdim/// \macro __GNUC_PREREQ 33263509Sdim/// \brief Defines __GNUC_PREREQ if glibc's features.h isn't available. 34263509Sdim#ifndef __GNUC_PREREQ 35263509Sdim# if defined(__GNUC__) && defined(__GNUC_MINOR__) 36263509Sdim# define __GNUC_PREREQ(maj, min) \ 37263509Sdim ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) 38263509Sdim# else 39263509Sdim# define __GNUC_PREREQ(maj, min) 0 40263509Sdim# endif 41263509Sdim#endif 42263509Sdim 43252723Sdim/// \brief Does the compiler support r-value references? 44245431Sdim/// This implies that <utility> provides the one-argument std::move; it 45245431Sdim/// does not imply the existence of any other C++ library features. 46245431Sdim#if (__has_feature(cxx_rvalue_references) \ 47245431Sdim || defined(__GXX_EXPERIMENTAL_CXX0X__) \ 48245431Sdim || (defined(_MSC_VER) && _MSC_VER >= 1600)) 49252723Sdim#define LLVM_HAS_RVALUE_REFERENCES 1 50245431Sdim#else 51252723Sdim#define LLVM_HAS_RVALUE_REFERENCES 0 52245431Sdim#endif 53245431Sdim 54252723Sdim/// \brief Does the compiler support r-value reference *this? 55252723Sdim/// 56252723Sdim/// Sadly, this is separate from just r-value reference support because GCC 57252723Sdim/// implemented everything but this thus far. No release of GCC yet has support 58252723Sdim/// for this feature so it is enabled with Clang only. 59252723Sdim/// FIXME: This should change to a version check when GCC grows support for it. 60252723Sdim#if __has_feature(cxx_rvalue_references) 61252723Sdim#define LLVM_HAS_RVALUE_REFERENCE_THIS 1 62252723Sdim#else 63252723Sdim#define LLVM_HAS_RVALUE_REFERENCE_THIS 0 64252723Sdim#endif 65252723Sdim 66252723Sdim/// \macro LLVM_HAS_CXX11_TYPETRAITS 67252723Sdim/// \brief Does the compiler have the C++11 type traits. 68252723Sdim/// 69252723Sdim/// #include <type_traits> 70252723Sdim/// 71252723Sdim/// * enable_if 72252723Sdim/// * {true,false}_type 73252723Sdim/// * is_constructible 74252723Sdim/// * etc... 75252723Sdim#if defined(__GXX_EXPERIMENTAL_CXX0X__) \ 76252723Sdim || (defined(_MSC_VER) && _MSC_VER >= 1700) 77252723Sdim#define LLVM_HAS_CXX11_TYPETRAITS 1 78252723Sdim#else 79252723Sdim#define LLVM_HAS_CXX11_TYPETRAITS 0 80252723Sdim#endif 81252723Sdim 82252723Sdim/// \macro LLVM_HAS_CXX11_STDLIB 83252723Sdim/// \brief Does the compiler have the C++11 standard library. 84252723Sdim/// 85252723Sdim/// Implies LLVM_HAS_RVALUE_REFERENCES, LLVM_HAS_CXX11_TYPETRAITS 86252723Sdim#if defined(__GXX_EXPERIMENTAL_CXX0X__) \ 87252723Sdim || (defined(_MSC_VER) && _MSC_VER >= 1700) 88252723Sdim#define LLVM_HAS_CXX11_STDLIB 1 89252723Sdim#else 90252723Sdim#define LLVM_HAS_CXX11_STDLIB 0 91252723Sdim#endif 92252723Sdim 93252723Sdim/// \macro LLVM_HAS_VARIADIC_TEMPLATES 94252723Sdim/// \brief Does this compiler support variadic templates. 95252723Sdim/// 96252723Sdim/// Implies LLVM_HAS_RVALUE_REFERENCES and the existence of std::forward. 97252723Sdim#if __has_feature(cxx_variadic_templates) 98252723Sdim# define LLVM_HAS_VARIADIC_TEMPLATES 1 99252723Sdim#else 100252723Sdim# define LLVM_HAS_VARIADIC_TEMPLATES 0 101252723Sdim#endif 102252723Sdim 103245431Sdim/// llvm_move - Expands to ::std::move if the compiler supports 104245431Sdim/// r-value references; otherwise, expands to the argument. 105252723Sdim#if LLVM_HAS_RVALUE_REFERENCES 106245431Sdim#define llvm_move(value) (::std::move(value)) 107245431Sdim#else 108245431Sdim#define llvm_move(value) (value) 109245431Sdim#endif 110245431Sdim 111252723Sdim/// Expands to '&' if r-value references are supported. 112252723Sdim/// 113252723Sdim/// This can be used to provide l-value/r-value overrides of member functions. 114252723Sdim/// The r-value override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS 115252723Sdim#if LLVM_HAS_RVALUE_REFERENCE_THIS 116252723Sdim#define LLVM_LVALUE_FUNCTION & 117252723Sdim#else 118252723Sdim#define LLVM_LVALUE_FUNCTION 119252723Sdim#endif 120252723Sdim 121245431Sdim/// LLVM_DELETED_FUNCTION - Expands to = delete if the compiler supports it. 122245431Sdim/// Use to mark functions as uncallable. Member functions with this should 123245431Sdim/// be declared private so that some behavior is kept in C++03 mode. 124245431Sdim/// 125245431Sdim/// class DontCopy { 126245431Sdim/// private: 127245431Sdim/// DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION; 128245431Sdim/// DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION; 129245431Sdim/// public: 130245431Sdim/// ... 131245431Sdim/// }; 132245431Sdim#if (__has_feature(cxx_deleted_functions) \ 133245431Sdim || defined(__GXX_EXPERIMENTAL_CXX0X__)) 134245431Sdim // No version of MSVC currently supports this. 135245431Sdim#define LLVM_DELETED_FUNCTION = delete 136245431Sdim#else 137245431Sdim#define LLVM_DELETED_FUNCTION 138245431Sdim#endif 139245431Sdim 140245431Sdim/// LLVM_FINAL - Expands to 'final' if the compiler supports it. 141245431Sdim/// Use to mark classes or virtual methods as final. 142252723Sdim#if __has_feature(cxx_override_control) \ 143252723Sdim || (defined(_MSC_VER) && _MSC_VER >= 1700) 144245431Sdim#define LLVM_FINAL final 145245431Sdim#else 146245431Sdim#define LLVM_FINAL 147245431Sdim#endif 148245431Sdim 149245431Sdim/// LLVM_OVERRIDE - Expands to 'override' if the compiler supports it. 150245431Sdim/// Use to mark virtual methods as overriding a base class method. 151252723Sdim#if __has_feature(cxx_override_control) \ 152252723Sdim || (defined(_MSC_VER) && _MSC_VER >= 1700) 153245431Sdim#define LLVM_OVERRIDE override 154245431Sdim#else 155245431Sdim#define LLVM_OVERRIDE 156245431Sdim#endif 157245431Sdim 158252723Sdim#if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__) 159252723Sdim# define LLVM_CONSTEXPR constexpr 160252723Sdim#else 161252723Sdim# define LLVM_CONSTEXPR 162252723Sdim#endif 163252723Sdim 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. 168263509Sdim/// On PE/COFF targets, library visibility is the default, so this isn't needed. 169263509Sdim#if (__has_attribute(visibility) || __GNUC_PREREQ(4, 0)) && \ 170263509Sdim !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 176263509Sdim#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 182263509Sdim#if __has_attribute(warn_unused_result) || __GNUC_PREREQ(3, 4) 183263509Sdim#define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__)) 184263509Sdim#else 185263509Sdim#define LLVM_ATTRIBUTE_UNUSED_RESULT 186263509Sdim#endif 187263509Sdim 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. 196263509Sdim#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 202263509Sdim// FIXME: Provide this for PE/COFF targets. 203263509Sdim#if (__has_attribute(weak) || __GNUC_PREREQ(4, 0)) && \ 204263509Sdim (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)) 205235633Sdim#define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__)) 206199481Srdivacky#else 207235633Sdim#define LLVM_ATTRIBUTE_WEAK 208199481Srdivacky#endif 209199481Srdivacky 210263509Sdim// Prior to clang 3.2, clang did not accept any spelling of 211263509Sdim// __has_attribute(const), so assume it is supported. 212263509Sdim#if defined(__clang__) || defined(__GNUC__) 213263509Sdim// aka 'CONST' but following LLVM Conventions. 214235633Sdim#define LLVM_READNONE __attribute__((__const__)) 215199481Srdivacky#else 216235633Sdim#define LLVM_READNONE 217199481Srdivacky#endif 218199481Srdivacky 219263509Sdim#if __has_attribute(pure) || defined(__GNUC__) 220263509Sdim// aka 'PURE' but following LLVM Conventions. 221235633Sdim#define LLVM_READONLY __attribute__((__pure__)) 222235633Sdim#else 223235633Sdim#define LLVM_READONLY 224235633Sdim#endif 225235633Sdim 226263509Sdim#if __has_builtin(__builtin_expect) || __GNUC_PREREQ(4, 0) 227245431Sdim#define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true) 228245431Sdim#define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false) 229193323Sed#else 230245431Sdim#define LLVM_LIKELY(EXPR) (EXPR) 231245431Sdim#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 247252723Sdim/// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so, 248252723Sdim/// mark a method "not for inlining". 249263509Sdim#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 257252723Sdim/// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do 258252723Sdim/// so, mark a method "always inline" because it is performance sensitive. GCC 259252723Sdim/// 3.4 supported this but is buggy in various cases and produces unimplemented 260252723Sdim/// errors, just use it in GCC 4.0 and later. 261263509Sdim#if __has_attribute(always_inline) || __GNUC_PREREQ(4, 0) 262245431Sdim#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 277252723Sdim/// LLVM_EXTENSION - Support compilers where we have a keyword to suppress 278252723Sdim/// pedantic diagnostics. 279235633Sdim#ifdef __GNUC__ 280235633Sdim#define LLVM_EXTENSION __extension__ 281235633Sdim#else 282235633Sdim#define LLVM_EXTENSION 283235633Sdim#endif 284235633Sdim 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 300252723Sdim/// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands 301252723Sdim/// to an expression which states that it is undefined behavior for the 302252723Sdim/// compiler to reach this point. Otherwise is not defined. 303263509Sdim#if __has_builtin(__builtin_unreachable) || __GNUC_PREREQ(4, 5) 304221345Sdim# define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable() 305252723Sdim#elif defined(_MSC_VER) 306252723Sdim# define LLVM_BUILTIN_UNREACHABLE __assume(false) 307218893Sdim#endif 308221345Sdim 309252723Sdim/// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression 310252723Sdim/// which causes the program to exit abnormally. 311263509Sdim#if __has_builtin(__builtin_trap) || __GNUC_PREREQ(4, 3) 312245431Sdim# define LLVM_BUILTIN_TRAP __builtin_trap() 313245431Sdim#else 314245431Sdim# define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0 315221345Sdim#endif 316245431Sdim 317252723Sdim/// \macro LLVM_ASSUME_ALIGNED 318252723Sdim/// \brief Returns a pointer with an assumed alignment. 319263509Sdim#if __has_builtin(__builtin_assume_aligned) && __GNUC_PREREQ(4, 7) 320252723Sdim# define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a) 321252723Sdim#elif defined(LLVM_BUILTIN_UNREACHABLE) 322263509Sdim// As of today, clang does not support __builtin_assume_aligned. 323252723Sdim# define LLVM_ASSUME_ALIGNED(p, a) \ 324252723Sdim (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p))) 325252723Sdim#else 326252723Sdim# define LLVM_ASSUME_ALIGNED(p, a) (p) 327245431Sdim#endif 328252723Sdim 329252723Sdim/// \macro LLVM_FUNCTION_NAME 330252723Sdim/// \brief Expands to __func__ on compilers which support it. Otherwise, 331252723Sdim/// expands to a compiler-dependent replacement. 332252723Sdim#if defined(_MSC_VER) 333252723Sdim# define LLVM_FUNCTION_NAME __FUNCTION__ 334252723Sdim#else 335252723Sdim# define LLVM_FUNCTION_NAME __func__ 336252723Sdim#endif 337252723Sdim 338252723Sdim#if defined(HAVE_SANITIZER_MSAN_INTERFACE_H) 339252723Sdim# include <sanitizer/msan_interface.h> 340252723Sdim#else 341252723Sdim# define __msan_allocated_memory(p, size) 342252723Sdim# define __msan_unpoison(p, size) 343252723Sdim#endif 344252723Sdim 345252723Sdim/// \macro LLVM_MEMORY_SANITIZER_BUILD 346252723Sdim/// \brief Whether LLVM itself is built with MemorySanitizer instrumentation. 347252723Sdim#if __has_feature(memory_sanitizer) 348252723Sdim# define LLVM_MEMORY_SANITIZER_BUILD 1 349252723Sdim#else 350252723Sdim# define LLVM_MEMORY_SANITIZER_BUILD 0 351252723Sdim#endif 352252723Sdim 353252723Sdim/// \macro LLVM_ADDRESS_SANITIZER_BUILD 354252723Sdim/// \brief Whether LLVM itself is built with AddressSanitizer instrumentation. 355252723Sdim#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) 356252723Sdim# define LLVM_ADDRESS_SANITIZER_BUILD 1 357252723Sdim#else 358252723Sdim# define LLVM_ADDRESS_SANITIZER_BUILD 0 359252723Sdim#endif 360252723Sdim 361252723Sdim/// \macro LLVM_IS_UNALIGNED_ACCESS_FAST 362252723Sdim/// \brief Is unaligned memory access fast on the host machine. 363252723Sdim/// 364252723Sdim/// Don't specialize on alignment for platforms where unaligned memory accesses 365252723Sdim/// generates the same code as aligned memory accesses for common types. 366252723Sdim#if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \ 367252723Sdim defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \ 368252723Sdim defined(_X86_) || defined(__i386) || defined(__i386__) 369252723Sdim# define LLVM_IS_UNALIGNED_ACCESS_FAST 1 370252723Sdim#else 371252723Sdim# define LLVM_IS_UNALIGNED_ACCESS_FAST 0 372252723Sdim#endif 373252723Sdim 374252723Sdim/// \macro LLVM_EXPLICIT 375252723Sdim/// \brief Expands to explicit on compilers which support explicit conversion 376252723Sdim/// operators. Otherwise expands to nothing. 377252723Sdim#if (__has_feature(cxx_explicit_conversions) \ 378252723Sdim || defined(__GXX_EXPERIMENTAL_CXX0X__)) 379252723Sdim#define LLVM_EXPLICIT explicit 380252723Sdim#else 381252723Sdim#define LLVM_EXPLICIT 382252723Sdim#endif 383252723Sdim 384252723Sdim/// \macro LLVM_STATIC_ASSERT 385252723Sdim/// \brief Expands to C/C++'s static_assert on compilers which support it. 386252723Sdim#if __has_feature(cxx_static_assert) 387252723Sdim# define LLVM_STATIC_ASSERT(expr, msg) static_assert(expr, msg) 388252723Sdim#elif __has_feature(c_static_assert) 389252723Sdim# define LLVM_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg) 390252723Sdim#else 391252723Sdim# define LLVM_STATIC_ASSERT(expr, msg) 392252723Sdim#endif 393252723Sdim 394263509Sdim/// \macro LLVM_ENUM_INT_TYPE 395263509Sdim/// \brief Expands to colon followed by the given integral type on compilers 396263509Sdim/// which support C++11 strong enums. This can be used to make enums unsigned 397263509Sdim/// with MSVC. 398263509Sdim#if __has_feature(cxx_strong_enums) 399263509Sdim# define LLVM_ENUM_INT_TYPE(intty) : intty 400263509Sdim#elif defined(_MSC_VER) && _MSC_VER >= 1600 // Added in MSVC 2010. 401263509Sdim# define LLVM_ENUM_INT_TYPE(intty) : intty 402263509Sdim#else 403263509Sdim# define LLVM_ENUM_INT_TYPE(intty) 404252723Sdim#endif 405263509Sdim 406263509Sdim/// \brief Does the compiler support generalized initializers (using braced 407263509Sdim/// lists and std::initializer_list). 408263509Sdim#if __has_feature(cxx_generalized_initializers) 409263509Sdim#define LLVM_HAS_INITIALIZER_LISTS 1 410263509Sdim#else 411263509Sdim#define LLVM_HAS_INITIALIZER_LISTS 0 412263509Sdim#endif 413263509Sdim 414263509Sdim#endif 415