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