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