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_extension 25 # define __has_extension(x) 0 26 #endif 27 28 #ifndef __has_attribute 29 # define __has_attribute(x) 0 30 #endif 31 32 #ifndef __has_builtin 33 # define __has_builtin(x) 0 34 #endif 35 36 /// \macro LLVM_GNUC_PREREQ 37 /// \brief Extend the default __GNUC_PREREQ even if glibc's features.h isn't 38 /// available. 39 #ifndef LLVM_GNUC_PREREQ 40 # if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) 41 # define LLVM_GNUC_PREREQ(maj, min, patch) \ 42 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \ 43 ((maj) << 20) + ((min) << 10) + (patch)) 44 # elif defined(__GNUC__) && defined(__GNUC_MINOR__) 45 # define LLVM_GNUC_PREREQ(maj, min, patch) \ 46 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10)) 47 # else 48 # define LLVM_GNUC_PREREQ(maj, min, patch) 0 49 # endif 50 #endif 51 52 /// \macro LLVM_MSC_PREREQ 53 /// \brief Is the compiler MSVC of at least the specified version? 54 /// The common \param version values to check for are: 55 /// * 1800: Microsoft Visual Studio 2013 / 12.0 56 /// * 1900: Microsoft Visual Studio 2015 / 14.0 57 #ifdef _MSC_VER 58 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version)) 59 60 // We require at least MSVC 2013. 61 #if !LLVM_MSC_PREREQ(1800) 62 #error LLVM requires at least MSVC 2013. 63 #endif 64 65 #else 66 #define LLVM_MSC_PREREQ(version) 0 67 #endif 68 69 #if !defined(_MSC_VER) || defined(__clang__) || LLVM_MSC_PREREQ(1900) 70 #define LLVM_NOEXCEPT noexcept 71 #else 72 #define LLVM_NOEXCEPT throw() 73 #endif 74 75 /// \brief Does the compiler support ref-qualifiers for *this? 76 /// 77 /// Sadly, this is separate from just rvalue reference support because GCC 78 /// and MSVC implemented this later than everything else. 79 #if __has_feature(cxx_rvalue_references) || LLVM_GNUC_PREREQ(4, 8, 1) 80 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1 81 #else 82 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0 83 #endif 84 85 /// Expands to '&' if ref-qualifiers for *this are supported. 86 /// 87 /// This can be used to provide lvalue/rvalue overrides of member functions. 88 /// The rvalue override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS 89 #if LLVM_HAS_RVALUE_REFERENCE_THIS 90 #define LLVM_LVALUE_FUNCTION & 91 #else 92 #define LLVM_LVALUE_FUNCTION 93 #endif 94 95 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__) 96 # define LLVM_CONSTEXPR constexpr 97 #else 98 # define LLVM_CONSTEXPR 99 #endif 100 101 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked 102 /// into a shared library, then the class should be private to the library and 103 /// not accessible from outside it. Can also be used to mark variables and 104 /// functions, making them private to any shared library they are linked into. 105 /// On PE/COFF targets, library visibility is the default, so this isn't needed. 106 #if (__has_attribute(visibility) || LLVM_GNUC_PREREQ(4, 0, 0)) && \ 107 !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32) 108 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden"))) 109 #else 110 #define LLVM_LIBRARY_VISIBILITY 111 #endif 112 113 #if __has_attribute(sentinel) || LLVM_GNUC_PREREQ(3, 0, 0) 114 #define LLVM_END_WITH_NULL __attribute__((sentinel)) 115 #else 116 #define LLVM_END_WITH_NULL 117 #endif 118 119 #if __has_attribute(used) || LLVM_GNUC_PREREQ(3, 1, 0) 120 #define LLVM_ATTRIBUTE_USED __attribute__((__used__)) 121 #else 122 #define LLVM_ATTRIBUTE_USED 123 #endif 124 125 #if __has_attribute(warn_unused_result) || LLVM_GNUC_PREREQ(3, 4, 0) 126 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__)) 127 #else 128 #define LLVM_ATTRIBUTE_UNUSED_RESULT 129 #endif 130 131 // Some compilers warn about unused functions. When a function is sometimes 132 // used or not depending on build settings (e.g. a function only called from 133 // within "assert"), this attribute can be used to suppress such warnings. 134 // 135 // However, it shouldn't be used for unused *variables*, as those have a much 136 // more portable solution: 137 // (void)unused_var_name; 138 // Prefer cast-to-void wherever it is sufficient. 139 #if __has_attribute(unused) || LLVM_GNUC_PREREQ(3, 1, 0) 140 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__)) 141 #else 142 #define LLVM_ATTRIBUTE_UNUSED 143 #endif 144 145 // FIXME: Provide this for PE/COFF targets. 146 #if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) && \ 147 (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)) 148 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__)) 149 #else 150 #define LLVM_ATTRIBUTE_WEAK 151 #endif 152 153 // Prior to clang 3.2, clang did not accept any spelling of 154 // __has_attribute(const), so assume it is supported. 155 #if defined(__clang__) || defined(__GNUC__) 156 // aka 'CONST' but following LLVM Conventions. 157 #define LLVM_READNONE __attribute__((__const__)) 158 #else 159 #define LLVM_READNONE 160 #endif 161 162 #if __has_attribute(pure) || defined(__GNUC__) 163 // aka 'PURE' but following LLVM Conventions. 164 #define LLVM_READONLY __attribute__((__pure__)) 165 #else 166 #define LLVM_READONLY 167 #endif 168 169 #if __has_builtin(__builtin_expect) || LLVM_GNUC_PREREQ(4, 0, 0) 170 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true) 171 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false) 172 #else 173 #define LLVM_LIKELY(EXPR) (EXPR) 174 #define LLVM_UNLIKELY(EXPR) (EXPR) 175 #endif 176 177 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so, 178 /// mark a method "not for inlining". 179 #if __has_attribute(noinline) || LLVM_GNUC_PREREQ(3, 4, 0) 180 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline)) 181 #elif defined(_MSC_VER) 182 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline) 183 #else 184 #define LLVM_ATTRIBUTE_NOINLINE 185 #endif 186 187 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do 188 /// so, mark a method "always inline" because it is performance sensitive. GCC 189 /// 3.4 supported this but is buggy in various cases and produces unimplemented 190 /// errors, just use it in GCC 4.0 and later. 191 #if __has_attribute(always_inline) || LLVM_GNUC_PREREQ(4, 0, 0) 192 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline)) 193 #elif defined(_MSC_VER) 194 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline 195 #else 196 #define LLVM_ATTRIBUTE_ALWAYS_INLINE 197 #endif 198 199 #ifdef __GNUC__ 200 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn)) 201 #elif defined(_MSC_VER) 202 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn) 203 #else 204 #define LLVM_ATTRIBUTE_NORETURN 205 #endif 206 207 #if __has_attribute(returns_nonnull) || LLVM_GNUC_PREREQ(4, 9, 0) 208 #define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull)) 209 #else 210 #define LLVM_ATTRIBUTE_RETURNS_NONNULL 211 #endif 212 213 /// \macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a 214 /// pointer that does not alias any other valid pointer. 215 #ifdef __GNUC__ 216 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__)) 217 #elif defined(_MSC_VER) 218 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict) 219 #else 220 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS 221 #endif 222 223 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress 224 /// pedantic diagnostics. 225 #ifdef __GNUC__ 226 #define LLVM_EXTENSION __extension__ 227 #else 228 #define LLVM_EXTENSION 229 #endif 230 231 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message") 232 #if __has_feature(attribute_deprecated_with_message) 233 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \ 234 decl __attribute__((deprecated(message))) 235 #elif defined(__GNUC__) 236 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \ 237 decl __attribute__((deprecated)) 238 #elif defined(_MSC_VER) 239 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \ 240 __declspec(deprecated(message)) decl 241 #else 242 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \ 243 decl 244 #endif 245 246 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands 247 /// to an expression which states that it is undefined behavior for the 248 /// compiler to reach this point. Otherwise is not defined. 249 #if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0) 250 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable() 251 #elif defined(_MSC_VER) 252 # define LLVM_BUILTIN_UNREACHABLE __assume(false) 253 #endif 254 255 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression 256 /// which causes the program to exit abnormally. 257 #if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0) 258 # define LLVM_BUILTIN_TRAP __builtin_trap() 259 #elif defined(_MSC_VER) 260 // The __debugbreak intrinsic is supported by MSVC, does not require forward 261 // declarations involving platform-specific typedefs (unlike RaiseException), 262 // results in a call to vectored exception handlers, and encodes to a short 263 // instruction that still causes the trapping behavior we want. 264 # define LLVM_BUILTIN_TRAP __debugbreak() 265 #else 266 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0 267 #endif 268 269 /// \macro LLVM_ASSUME_ALIGNED 270 /// \brief Returns a pointer with an assumed alignment. 271 #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0) 272 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a) 273 #elif defined(LLVM_BUILTIN_UNREACHABLE) 274 // As of today, clang does not support __builtin_assume_aligned. 275 # define LLVM_ASSUME_ALIGNED(p, a) \ 276 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p))) 277 #else 278 # define LLVM_ASSUME_ALIGNED(p, a) (p) 279 #endif 280 281 /// \macro LLVM_ALIGNAS 282 /// \brief Used to specify a minimum alignment for a structure or variable. The 283 /// alignment must be a constant integer. Use LLVM_PTR_SIZE to compute 284 /// alignments in terms of the size of a pointer. 285 /// 286 /// Note that __declspec(align) has special quirks, it's not legal to pass a 287 /// structure with __declspec(align) as a formal parameter. 288 #ifdef _MSC_VER 289 # define LLVM_ALIGNAS(x) __declspec(align(x)) 290 #elif __GNUC__ && !__has_feature(cxx_alignas) && !LLVM_GNUC_PREREQ(4, 8, 0) 291 # define LLVM_ALIGNAS(x) __attribute__((aligned(x))) 292 #else 293 # define LLVM_ALIGNAS(x) alignas(x) 294 #endif 295 296 /// \macro LLVM_PACKED 297 /// \brief Used to specify a packed structure. 298 /// LLVM_PACKED( 299 /// struct A { 300 /// int i; 301 /// int j; 302 /// int k; 303 /// long long l; 304 /// }); 305 /// 306 /// LLVM_PACKED_START 307 /// struct B { 308 /// int i; 309 /// int j; 310 /// int k; 311 /// long long l; 312 /// }; 313 /// LLVM_PACKED_END 314 #ifdef _MSC_VER 315 # define LLVM_PACKED(d) __pragma(pack(push, 1)) d __pragma(pack(pop)) 316 # define LLVM_PACKED_START __pragma(pack(push, 1)) 317 # define LLVM_PACKED_END __pragma(pack(pop)) 318 #else 319 # define LLVM_PACKED(d) d __attribute__((packed)) 320 # define LLVM_PACKED_START _Pragma("pack(push, 1)") 321 # define LLVM_PACKED_END _Pragma("pack(pop)") 322 #endif 323 324 /// \macro LLVM_PTR_SIZE 325 /// \brief A constant integer equivalent to the value of sizeof(void*). 326 /// Generally used in combination with LLVM_ALIGNAS or when doing computation in 327 /// the preprocessor. 328 #ifdef __SIZEOF_POINTER__ 329 # define LLVM_PTR_SIZE __SIZEOF_POINTER__ 330 #elif defined(_WIN64) 331 # define LLVM_PTR_SIZE 8 332 #elif defined(_WIN32) 333 # define LLVM_PTR_SIZE 4 334 #elif defined(_MSC_VER) 335 # error "could not determine LLVM_PTR_SIZE as a constant int for MSVC" 336 #else 337 # define LLVM_PTR_SIZE sizeof(void *) 338 #endif 339 340 /// \macro LLVM_FUNCTION_NAME 341 /// \brief Expands to __func__ on compilers which support it. Otherwise, 342 /// expands to a compiler-dependent replacement. 343 #if defined(_MSC_VER) 344 # define LLVM_FUNCTION_NAME __FUNCTION__ 345 #else 346 # define LLVM_FUNCTION_NAME __func__ 347 #endif 348 349 /// \macro LLVM_MEMORY_SANITIZER_BUILD 350 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation. 351 #if __has_feature(memory_sanitizer) 352 # define LLVM_MEMORY_SANITIZER_BUILD 1 353 # include <sanitizer/msan_interface.h> 354 #else 355 # define LLVM_MEMORY_SANITIZER_BUILD 0 356 # define __msan_allocated_memory(p, size) 357 # define __msan_unpoison(p, size) 358 #endif 359 360 /// \macro LLVM_ADDRESS_SANITIZER_BUILD 361 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation. 362 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) 363 # define LLVM_ADDRESS_SANITIZER_BUILD 1 364 # include <sanitizer/asan_interface.h> 365 #else 366 # define LLVM_ADDRESS_SANITIZER_BUILD 0 367 # define __asan_poison_memory_region(p, size) 368 # define __asan_unpoison_memory_region(p, size) 369 #endif 370 371 /// \macro LLVM_THREAD_SANITIZER_BUILD 372 /// \brief Whether LLVM itself is built with ThreadSanitizer instrumentation. 373 #if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__) 374 # define LLVM_THREAD_SANITIZER_BUILD 1 375 #else 376 # define LLVM_THREAD_SANITIZER_BUILD 0 377 #endif 378 379 #if LLVM_THREAD_SANITIZER_BUILD 380 // Thread Sanitizer is a tool that finds races in code. 381 // See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations . 382 // tsan detects these exact functions by name. 383 extern "C" { 384 void AnnotateHappensAfter(const char *file, int line, const volatile void *cv); 385 void AnnotateHappensBefore(const char *file, int line, const volatile void *cv); 386 void AnnotateIgnoreWritesBegin(const char *file, int line); 387 void AnnotateIgnoreWritesEnd(const char *file, int line); 388 } 389 390 // This marker is used to define a happens-before arc. The race detector will 391 // infer an arc from the begin to the end when they share the same pointer 392 // argument. 393 # define TsanHappensBefore(cv) AnnotateHappensBefore(__FILE__, __LINE__, cv) 394 395 // This marker defines the destination of a happens-before arc. 396 # define TsanHappensAfter(cv) AnnotateHappensAfter(__FILE__, __LINE__, cv) 397 398 // Ignore any races on writes between here and the next TsanIgnoreWritesEnd. 399 # define TsanIgnoreWritesBegin() AnnotateIgnoreWritesBegin(__FILE__, __LINE__) 400 401 // Resume checking for racy writes. 402 # define TsanIgnoreWritesEnd() AnnotateIgnoreWritesEnd(__FILE__, __LINE__) 403 #else 404 # define TsanHappensBefore(cv) 405 # define TsanHappensAfter(cv) 406 # define TsanIgnoreWritesBegin() 407 # define TsanIgnoreWritesEnd() 408 #endif 409 410 /// \brief Mark debug helper function definitions like dump() that should not be 411 /// stripped from debug builds. 412 // FIXME: Move this to a private config.h as it's not usable in public headers. 413 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 414 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED 415 #else 416 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE 417 #endif 418 419 /// \macro LLVM_THREAD_LOCAL 420 /// \brief A thread-local storage specifier which can be used with globals, 421 /// extern globals, and static globals. 422 /// 423 /// This is essentially an extremely restricted analog to C++11's thread_local 424 /// support, and uses that when available. However, it falls back on 425 /// platform-specific or vendor-provided extensions when necessary. These 426 /// extensions don't support many of the C++11 thread_local's features. You 427 /// should only use this for PODs that you can statically initialize to 428 /// some constant value. In almost all circumstances this is most appropriate 429 /// for use with a pointer, integer, or small aggregation of pointers and 430 /// integers. 431 #if LLVM_ENABLE_THREADS 432 #if __has_feature(cxx_thread_local) 433 #define LLVM_THREAD_LOCAL thread_local 434 #elif defined(_MSC_VER) 435 // MSVC supports this with a __declspec. 436 #define LLVM_THREAD_LOCAL __declspec(thread) 437 #else 438 // Clang, GCC, and other compatible compilers used __thread prior to C++11 and 439 // we only need the restricted functionality that provides. 440 #define LLVM_THREAD_LOCAL __thread 441 #endif 442 #else // !LLVM_ENABLE_THREADS 443 // If threading is disabled entirely, this compiles to nothing and you get 444 // a normal global variable. 445 #define LLVM_THREAD_LOCAL 446 #endif 447 448 #endif 449