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