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_REQUIRE_CONSTANT_INITIALIZATION - Apply this to globals to ensure that 247 /// they are constant initialized. 248 #if __has_cpp_attribute(clang::require_constant_initialization) 249 #define LLVM_REQUIRE_CONSTANT_INITIALIZATION \ 250 [[clang::require_constant_initialization]] 251 #else 252 #define LLVM_REQUIRE_CONSTANT_INITIALIZATION 253 #endif 254 255 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress 256 /// pedantic diagnostics. 257 #ifdef __GNUC__ 258 #define LLVM_EXTENSION __extension__ 259 #else 260 #define LLVM_EXTENSION 261 #endif 262 263 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message") 264 #if __has_feature(attribute_deprecated_with_message) 265 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \ 266 decl __attribute__((deprecated(message))) 267 #elif defined(__GNUC__) 268 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \ 269 decl __attribute__((deprecated)) 270 #elif defined(_MSC_VER) 271 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \ 272 __declspec(deprecated(message)) decl 273 #else 274 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \ 275 decl 276 #endif 277 278 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands 279 /// to an expression which states that it is undefined behavior for the 280 /// compiler to reach this point. Otherwise is not defined. 281 #if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0) 282 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable() 283 #elif defined(_MSC_VER) 284 # define LLVM_BUILTIN_UNREACHABLE __assume(false) 285 #endif 286 287 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression 288 /// which causes the program to exit abnormally. 289 #if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0) 290 # define LLVM_BUILTIN_TRAP __builtin_trap() 291 #elif defined(_MSC_VER) 292 // The __debugbreak intrinsic is supported by MSVC, does not require forward 293 // declarations involving platform-specific typedefs (unlike RaiseException), 294 // results in a call to vectored exception handlers, and encodes to a short 295 // instruction that still causes the trapping behavior we want. 296 # define LLVM_BUILTIN_TRAP __debugbreak() 297 #else 298 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0 299 #endif 300 301 /// LLVM_BUILTIN_DEBUGTRAP - On compilers which support it, expands to 302 /// an expression which causes the program to break while running 303 /// under a debugger. 304 #if __has_builtin(__builtin_debugtrap) 305 # define LLVM_BUILTIN_DEBUGTRAP __builtin_debugtrap() 306 #elif defined(_MSC_VER) 307 // The __debugbreak intrinsic is supported by MSVC and breaks while 308 // running under the debugger, and also supports invoking a debugger 309 // when the OS is configured appropriately. 310 # define LLVM_BUILTIN_DEBUGTRAP __debugbreak() 311 #else 312 // Just continue execution when built with compilers that have no 313 // support. This is a debugging aid and not intended to force the 314 // program to abort if encountered. 315 # define LLVM_BUILTIN_DEBUGTRAP 316 #endif 317 318 /// \macro LLVM_ASSUME_ALIGNED 319 /// \brief Returns a pointer with an assumed alignment. 320 #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0) 321 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a) 322 #elif defined(LLVM_BUILTIN_UNREACHABLE) 323 // As of today, clang does not support __builtin_assume_aligned. 324 # define LLVM_ASSUME_ALIGNED(p, a) \ 325 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p))) 326 #else 327 # define LLVM_ASSUME_ALIGNED(p, a) (p) 328 #endif 329 330 /// \macro LLVM_ALIGNAS 331 /// \brief Used to specify a minimum alignment for a structure or variable. 332 #if __GNUC__ && !__has_feature(cxx_alignas) && !LLVM_GNUC_PREREQ(4, 8, 1) 333 # define LLVM_ALIGNAS(x) __attribute__((aligned(x))) 334 #else 335 # define LLVM_ALIGNAS(x) alignas(x) 336 #endif 337 338 /// \macro LLVM_PACKED 339 /// \brief Used to specify a packed structure. 340 /// LLVM_PACKED( 341 /// struct A { 342 /// int i; 343 /// int j; 344 /// int k; 345 /// long long l; 346 /// }); 347 /// 348 /// LLVM_PACKED_START 349 /// struct B { 350 /// int i; 351 /// int j; 352 /// int k; 353 /// long long l; 354 /// }; 355 /// LLVM_PACKED_END 356 #ifdef _MSC_VER 357 # define LLVM_PACKED(d) __pragma(pack(push, 1)) d __pragma(pack(pop)) 358 # define LLVM_PACKED_START __pragma(pack(push, 1)) 359 # define LLVM_PACKED_END __pragma(pack(pop)) 360 #else 361 # define LLVM_PACKED(d) d __attribute__((packed)) 362 # define LLVM_PACKED_START _Pragma("pack(push, 1)") 363 # define LLVM_PACKED_END _Pragma("pack(pop)") 364 #endif 365 366 /// \macro LLVM_PTR_SIZE 367 /// \brief A constant integer equivalent to the value of sizeof(void*). 368 /// Generally used in combination with LLVM_ALIGNAS or when doing computation in 369 /// the preprocessor. 370 #ifdef __SIZEOF_POINTER__ 371 # define LLVM_PTR_SIZE __SIZEOF_POINTER__ 372 #elif defined(_WIN64) 373 # define LLVM_PTR_SIZE 8 374 #elif defined(_WIN32) 375 # define LLVM_PTR_SIZE 4 376 #elif defined(_MSC_VER) 377 # error "could not determine LLVM_PTR_SIZE as a constant int for MSVC" 378 #else 379 # define LLVM_PTR_SIZE sizeof(void *) 380 #endif 381 382 /// \macro LLVM_MEMORY_SANITIZER_BUILD 383 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation. 384 #if __has_feature(memory_sanitizer) 385 # define LLVM_MEMORY_SANITIZER_BUILD 1 386 # include <sanitizer/msan_interface.h> 387 #else 388 # define LLVM_MEMORY_SANITIZER_BUILD 0 389 # define __msan_allocated_memory(p, size) 390 # define __msan_unpoison(p, size) 391 #endif 392 393 /// \macro LLVM_ADDRESS_SANITIZER_BUILD 394 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation. 395 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) 396 # define LLVM_ADDRESS_SANITIZER_BUILD 1 397 # include <sanitizer/asan_interface.h> 398 #else 399 # define LLVM_ADDRESS_SANITIZER_BUILD 0 400 # define __asan_poison_memory_region(p, size) 401 # define __asan_unpoison_memory_region(p, size) 402 #endif 403 404 /// \macro LLVM_THREAD_SANITIZER_BUILD 405 /// \brief Whether LLVM itself is built with ThreadSanitizer instrumentation. 406 #if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__) 407 # define LLVM_THREAD_SANITIZER_BUILD 1 408 #else 409 # define LLVM_THREAD_SANITIZER_BUILD 0 410 #endif 411 412 #if LLVM_THREAD_SANITIZER_BUILD 413 // Thread Sanitizer is a tool that finds races in code. 414 // See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations . 415 // tsan detects these exact functions by name. 416 #ifdef __cplusplus 417 extern "C" { 418 #endif 419 void AnnotateHappensAfter(const char *file, int line, const volatile void *cv); 420 void AnnotateHappensBefore(const char *file, int line, const volatile void *cv); 421 void AnnotateIgnoreWritesBegin(const char *file, int line); 422 void AnnotateIgnoreWritesEnd(const char *file, int line); 423 #ifdef __cplusplus 424 } 425 #endif 426 427 // This marker is used to define a happens-before arc. The race detector will 428 // infer an arc from the begin to the end when they share the same pointer 429 // argument. 430 # define TsanHappensBefore(cv) AnnotateHappensBefore(__FILE__, __LINE__, cv) 431 432 // This marker defines the destination of a happens-before arc. 433 # define TsanHappensAfter(cv) AnnotateHappensAfter(__FILE__, __LINE__, cv) 434 435 // Ignore any races on writes between here and the next TsanIgnoreWritesEnd. 436 # define TsanIgnoreWritesBegin() AnnotateIgnoreWritesBegin(__FILE__, __LINE__) 437 438 // Resume checking for racy writes. 439 # define TsanIgnoreWritesEnd() AnnotateIgnoreWritesEnd(__FILE__, __LINE__) 440 #else 441 # define TsanHappensBefore(cv) 442 # define TsanHappensAfter(cv) 443 # define TsanIgnoreWritesBegin() 444 # define TsanIgnoreWritesEnd() 445 #endif 446 447 /// \macro LLVM_NO_SANITIZE 448 /// \brief Disable a particular sanitizer for a function. 449 #if __has_attribute(no_sanitize) 450 #define LLVM_NO_SANITIZE(KIND) __attribute__((no_sanitize(KIND))) 451 #else 452 #define LLVM_NO_SANITIZE(KIND) 453 #endif 454 455 /// \brief Mark debug helper function definitions like dump() that should not be 456 /// stripped from debug builds. 457 // FIXME: Move this to a private config.h as it's not usable in public headers. 458 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 459 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED 460 #else 461 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE 462 #endif 463 464 /// \macro LLVM_PRETTY_FUNCTION 465 /// \brief Gets a user-friendly looking function signature for the current scope 466 /// using the best available method on each platform. The exact format of the 467 /// resulting string is implementation specific and non-portable, so this should 468 /// only be used, for example, for logging or diagnostics. 469 #if defined(_MSC_VER) 470 #define LLVM_PRETTY_FUNCTION __FUNCSIG__ 471 #elif defined(__GNUC__) || defined(__clang__) 472 #define LLVM_PRETTY_FUNCTION __PRETTY_FUNCTION__ 473 #else 474 #define LLVM_PRETTY_FUNCTION __func__ 475 #endif 476 477 /// \macro LLVM_THREAD_LOCAL 478 /// \brief A thread-local storage specifier which can be used with globals, 479 /// extern globals, and static globals. 480 /// 481 /// This is essentially an extremely restricted analog to C++11's thread_local 482 /// support, and uses that when available. However, it falls back on 483 /// platform-specific or vendor-provided extensions when necessary. These 484 /// extensions don't support many of the C++11 thread_local's features. You 485 /// should only use this for PODs that you can statically initialize to 486 /// some constant value. In almost all circumstances this is most appropriate 487 /// for use with a pointer, integer, or small aggregation of pointers and 488 /// integers. 489 #if LLVM_ENABLE_THREADS 490 #if __has_feature(cxx_thread_local) 491 #define LLVM_THREAD_LOCAL thread_local 492 #elif defined(_MSC_VER) 493 // MSVC supports this with a __declspec. 494 #define LLVM_THREAD_LOCAL __declspec(thread) 495 #else 496 // Clang, GCC, and other compatible compilers used __thread prior to C++11 and 497 // we only need the restricted functionality that provides. 498 #define LLVM_THREAD_LOCAL __thread 499 #endif 500 #else // !LLVM_ENABLE_THREADS 501 // If threading is disabled entirely, this compiles to nothing and you get 502 // a normal global variable. 503 #define LLVM_THREAD_LOCAL 504 #endif 505 506 #endif 507