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 #if __has_attribute(warn_unused_result) || __GNUC_PREREQ(3, 4) 183 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__)) 184 #else 185 #define LLVM_ATTRIBUTE_UNUSED_RESULT 186 #endif 187 188 // Some compilers warn about unused functions. When a function is sometimes 189 // used or not depending on build settings (e.g. a function only called from 190 // within "assert"), this attribute can be used to suppress such warnings. 191 // 192 // However, it shouldn't be used for unused *variables*, as those have a much 193 // more portable solution: 194 // (void)unused_var_name; 195 // Prefer cast-to-void wherever it is sufficient. 196 #if __has_attribute(unused) || __GNUC_PREREQ(3, 1) 197 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__)) 198 #else 199 #define LLVM_ATTRIBUTE_UNUSED 200 #endif 201 202 // FIXME: Provide this for PE/COFF targets. 203 #if (__has_attribute(weak) || __GNUC_PREREQ(4, 0)) && \ 204 (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)) 205 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__)) 206 #else 207 #define LLVM_ATTRIBUTE_WEAK 208 #endif 209 210 // Prior to clang 3.2, clang did not accept any spelling of 211 // __has_attribute(const), so assume it is supported. 212 #if defined(__clang__) || defined(__GNUC__) 213 // aka 'CONST' but following LLVM Conventions. 214 #define LLVM_READNONE __attribute__((__const__)) 215 #else 216 #define LLVM_READNONE 217 #endif 218 219 #if __has_attribute(pure) || defined(__GNUC__) 220 // aka 'PURE' but following LLVM Conventions. 221 #define LLVM_READONLY __attribute__((__pure__)) 222 #else 223 #define LLVM_READONLY 224 #endif 225 226 #if __has_builtin(__builtin_expect) || __GNUC_PREREQ(4, 0) 227 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true) 228 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false) 229 #else 230 #define LLVM_LIKELY(EXPR) (EXPR) 231 #define LLVM_UNLIKELY(EXPR) (EXPR) 232 #endif 233 234 // C++ doesn't support 'extern template' of template specializations. GCC does, 235 // but requires __extension__ before it. In the header, use this: 236 // EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>); 237 // in the .cpp file, use this: 238 // TEMPLATE_INSTANTIATION(class foo<bar>); 239 #ifdef __GNUC__ 240 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X 241 #define TEMPLATE_INSTANTIATION(X) template X 242 #else 243 #define EXTERN_TEMPLATE_INSTANTIATION(X) 244 #define TEMPLATE_INSTANTIATION(X) 245 #endif 246 247 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so, 248 /// mark a method "not for inlining". 249 #if __has_attribute(noinline) || __GNUC_PREREQ(3, 4) 250 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline)) 251 #elif defined(_MSC_VER) 252 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline) 253 #else 254 #define LLVM_ATTRIBUTE_NOINLINE 255 #endif 256 257 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do 258 /// so, mark a method "always inline" because it is performance sensitive. GCC 259 /// 3.4 supported this but is buggy in various cases and produces unimplemented 260 /// errors, just use it in GCC 4.0 and later. 261 #if __has_attribute(always_inline) || __GNUC_PREREQ(4, 0) 262 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline)) 263 #elif defined(_MSC_VER) 264 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline 265 #else 266 #define LLVM_ATTRIBUTE_ALWAYS_INLINE 267 #endif 268 269 #ifdef __GNUC__ 270 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn)) 271 #elif defined(_MSC_VER) 272 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn) 273 #else 274 #define LLVM_ATTRIBUTE_NORETURN 275 #endif 276 277 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress 278 /// pedantic diagnostics. 279 #ifdef __GNUC__ 280 #define LLVM_EXTENSION __extension__ 281 #else 282 #define LLVM_EXTENSION 283 #endif 284 285 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message") 286 #if __has_feature(attribute_deprecated_with_message) 287 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \ 288 decl __attribute__((deprecated(message))) 289 #elif defined(__GNUC__) 290 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \ 291 decl __attribute__((deprecated)) 292 #elif defined(_MSC_VER) 293 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \ 294 __declspec(deprecated(message)) decl 295 #else 296 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \ 297 decl 298 #endif 299 300 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands 301 /// to an expression which states that it is undefined behavior for the 302 /// compiler to reach this point. Otherwise is not defined. 303 #if __has_builtin(__builtin_unreachable) || __GNUC_PREREQ(4, 5) 304 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable() 305 #elif defined(_MSC_VER) 306 # define LLVM_BUILTIN_UNREACHABLE __assume(false) 307 #endif 308 309 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression 310 /// which causes the program to exit abnormally. 311 #if __has_builtin(__builtin_trap) || __GNUC_PREREQ(4, 3) 312 # define LLVM_BUILTIN_TRAP __builtin_trap() 313 #else 314 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0 315 #endif 316 317 /// \macro LLVM_ASSUME_ALIGNED 318 /// \brief Returns a pointer with an assumed alignment. 319 #if __has_builtin(__builtin_assume_aligned) && __GNUC_PREREQ(4, 7) 320 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a) 321 #elif defined(LLVM_BUILTIN_UNREACHABLE) 322 // As of today, clang does not support __builtin_assume_aligned. 323 # define LLVM_ASSUME_ALIGNED(p, a) \ 324 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p))) 325 #else 326 # define LLVM_ASSUME_ALIGNED(p, a) (p) 327 #endif 328 329 /// \macro LLVM_FUNCTION_NAME 330 /// \brief Expands to __func__ on compilers which support it. Otherwise, 331 /// expands to a compiler-dependent replacement. 332 #if defined(_MSC_VER) 333 # define LLVM_FUNCTION_NAME __FUNCTION__ 334 #else 335 # define LLVM_FUNCTION_NAME __func__ 336 #endif 337 338 #if defined(HAVE_SANITIZER_MSAN_INTERFACE_H) 339 # include <sanitizer/msan_interface.h> 340 #else 341 # define __msan_allocated_memory(p, size) 342 # define __msan_unpoison(p, size) 343 #endif 344 345 /// \macro LLVM_MEMORY_SANITIZER_BUILD 346 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation. 347 #if __has_feature(memory_sanitizer) 348 # define LLVM_MEMORY_SANITIZER_BUILD 1 349 #else 350 # define LLVM_MEMORY_SANITIZER_BUILD 0 351 #endif 352 353 /// \macro LLVM_ADDRESS_SANITIZER_BUILD 354 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation. 355 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) 356 # define LLVM_ADDRESS_SANITIZER_BUILD 1 357 #else 358 # define LLVM_ADDRESS_SANITIZER_BUILD 0 359 #endif 360 361 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST 362 /// \brief Is unaligned memory access fast on the host machine. 363 /// 364 /// Don't specialize on alignment for platforms where unaligned memory accesses 365 /// generates the same code as aligned memory accesses for common types. 366 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \ 367 defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \ 368 defined(_X86_) || defined(__i386) || defined(__i386__) 369 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1 370 #else 371 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0 372 #endif 373 374 /// \macro LLVM_EXPLICIT 375 /// \brief Expands to explicit on compilers which support explicit conversion 376 /// operators. Otherwise expands to nothing. 377 #if (__has_feature(cxx_explicit_conversions) \ 378 || defined(__GXX_EXPERIMENTAL_CXX0X__)) 379 #define LLVM_EXPLICIT explicit 380 #else 381 #define LLVM_EXPLICIT 382 #endif 383 384 /// \macro LLVM_STATIC_ASSERT 385 /// \brief Expands to C/C++'s static_assert on compilers which support it. 386 #if __has_feature(cxx_static_assert) 387 # define LLVM_STATIC_ASSERT(expr, msg) static_assert(expr, msg) 388 #elif __has_feature(c_static_assert) 389 # define LLVM_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg) 390 #else 391 # define LLVM_STATIC_ASSERT(expr, msg) 392 #endif 393 394 /// \macro LLVM_ENUM_INT_TYPE 395 /// \brief Expands to colon followed by the given integral type on compilers 396 /// which support C++11 strong enums. This can be used to make enums unsigned 397 /// with MSVC. 398 #if __has_feature(cxx_strong_enums) 399 # define LLVM_ENUM_INT_TYPE(intty) : intty 400 #elif defined(_MSC_VER) && _MSC_VER >= 1600 // Added in MSVC 2010. 401 # define LLVM_ENUM_INT_TYPE(intty) : intty 402 #else 403 # define LLVM_ENUM_INT_TYPE(intty) 404 #endif 405 406 /// \brief Does the compiler support generalized initializers (using braced 407 /// lists and std::initializer_list). 408 #if __has_feature(cxx_generalized_initializers) 409 #define LLVM_HAS_INITIALIZER_LISTS 1 410 #else 411 #define LLVM_HAS_INITIALIZER_LISTS 0 412 #endif 413 414 #endif 415