blob: 9348ada91325096924c62a39eea0fb3d4dac4a3d [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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 Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
Andrew Scullcdfcccc2018-10-05 20:58:37 +01009// This file defines several macros, based on the current compiler. This allows
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020010// use of compiler-specific features in a way that remains portable. This header
11// can be included from either C or C++.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010012//
13//===----------------------------------------------------------------------===//
14
Andrew Scullcdfcccc2018-10-05 20:58:37 +010015#ifndef LLVM_SUPPORT_COMPILER_H
16#define LLVM_SUPPORT_COMPILER_H
17
18#include "llvm/Config/llvm-config.h"
19
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020020#ifdef __cplusplus
Andrew Scullcdfcccc2018-10-05 20:58:37 +010021#include <new>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020022#endif
Andrew Scullcdfcccc2018-10-05 20:58:37 +010023#include <stddef.h>
24
25#if defined(_MSC_VER)
26#include <sal.h>
27#endif
28
29#ifndef __has_feature
30# define __has_feature(x) 0
31#endif
32
33#ifndef __has_extension
34# define __has_extension(x) 0
35#endif
36
37#ifndef __has_attribute
38# define __has_attribute(x) 0
39#endif
40
Andrew Scullcdfcccc2018-10-05 20:58:37 +010041#ifndef __has_builtin
42# define __has_builtin(x) 0
43#endif
44
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020045// Only use __has_cpp_attribute in C++ mode. GCC defines __has_cpp_attribute in
46// C mode, but the :: in __has_cpp_attribute(scoped::attribute) is invalid.
47#ifndef LLVM_HAS_CPP_ATTRIBUTE
48#if defined(__cplusplus) && defined(__has_cpp_attribute)
49# define LLVM_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
50#else
51# define LLVM_HAS_CPP_ATTRIBUTE(x) 0
52#endif
53#endif
54
Andrew Scullcdfcccc2018-10-05 20:58:37 +010055/// \macro LLVM_GNUC_PREREQ
56/// Extend the default __GNUC_PREREQ even if glibc's features.h isn't
57/// available.
58#ifndef LLVM_GNUC_PREREQ
59# if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
60# define LLVM_GNUC_PREREQ(maj, min, patch) \
61 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
62 ((maj) << 20) + ((min) << 10) + (patch))
63# elif defined(__GNUC__) && defined(__GNUC_MINOR__)
64# define LLVM_GNUC_PREREQ(maj, min, patch) \
65 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
66# else
67# define LLVM_GNUC_PREREQ(maj, min, patch) 0
68# endif
69#endif
70
71/// \macro LLVM_MSC_PREREQ
72/// Is the compiler MSVC of at least the specified version?
73/// The common \param version values to check for are:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020074/// * 1910: VS2017, version 15.1 & 15.2
75/// * 1911: VS2017, version 15.3 & 15.4
76/// * 1912: VS2017, version 15.5
77/// * 1913: VS2017, version 15.6
78/// * 1914: VS2017, version 15.7
79/// * 1915: VS2017, version 15.8
80/// * 1916: VS2017, version 15.9
81/// * 1920: VS2019, version 16.0
82/// * 1921: VS2019, version 16.1
Andrew Scullcdfcccc2018-10-05 20:58:37 +010083#ifdef _MSC_VER
84#define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
85
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020086// We require at least MSVC 2017.
87#if !LLVM_MSC_PREREQ(1910)
88#error LLVM requires at least MSVC 2017.
Andrew Scullcdfcccc2018-10-05 20:58:37 +010089#endif
90
91#else
92#define LLVM_MSC_PREREQ(version) 0
93#endif
94
95/// Does the compiler support ref-qualifiers for *this?
96///
97/// Sadly, this is separate from just rvalue reference support because GCC
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020098/// and MSVC implemented this later than everything else. This appears to be
99/// corrected in MSVC 2019 but not MSVC 2017.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100100#if __has_feature(cxx_rvalue_references) || LLVM_GNUC_PREREQ(4, 8, 1)
101#define LLVM_HAS_RVALUE_REFERENCE_THIS 1
102#else
103#define LLVM_HAS_RVALUE_REFERENCE_THIS 0
104#endif
105
106/// Expands to '&' if ref-qualifiers for *this are supported.
107///
108/// This can be used to provide lvalue/rvalue overrides of member functions.
109/// The rvalue override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
110#if LLVM_HAS_RVALUE_REFERENCE_THIS
111#define LLVM_LVALUE_FUNCTION &
112#else
113#define LLVM_LVALUE_FUNCTION
114#endif
115
116/// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
117/// into a shared library, then the class should be private to the library and
118/// not accessible from outside it. Can also be used to mark variables and
119/// functions, making them private to any shared library they are linked into.
120/// On PE/COFF targets, library visibility is the default, so this isn't needed.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200121///
122/// LLVM_EXTERNAL_VISIBILITY - classes, functions, and variables marked with
123/// this attribute will be made public and visible outside of any shared library
124/// they are linked in to.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100125#if (__has_attribute(visibility) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
126 !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_WIN32)
127#define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200128#define LLVM_EXTERNAL_VISIBILITY __attribute__ ((visibility("default")))
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100129#else
130#define LLVM_LIBRARY_VISIBILITY
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200131#define LLVM_EXTERNAL_VISIBILITY
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100132#endif
133
134#if defined(__GNUC__)
135#define LLVM_PREFETCH(addr, rw, locality) __builtin_prefetch(addr, rw, locality)
136#else
137#define LLVM_PREFETCH(addr, rw, locality)
138#endif
139
140#if __has_attribute(used) || LLVM_GNUC_PREREQ(3, 1, 0)
141#define LLVM_ATTRIBUTE_USED __attribute__((__used__))
142#else
143#define LLVM_ATTRIBUTE_USED
144#endif
145
146/// LLVM_NODISCARD - Warn if a type or return value is discarded.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200147
148// Use the 'nodiscard' attribute in C++17 or newer mode.
149#if defined(__cplusplus) && __cplusplus > 201402L && LLVM_HAS_CPP_ATTRIBUTE(nodiscard)
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100150#define LLVM_NODISCARD [[nodiscard]]
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200151#elif LLVM_HAS_CPP_ATTRIBUTE(clang::warn_unused_result)
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100152#define LLVM_NODISCARD [[clang::warn_unused_result]]
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200153// Clang in C++14 mode claims that it has the 'nodiscard' attribute, but also
154// warns in the pedantic mode that 'nodiscard' is a C++17 extension (PR33518).
155// Use the 'nodiscard' attribute in C++14 mode only with GCC.
156// TODO: remove this workaround when PR33518 is resolved.
157#elif defined(__GNUC__) && LLVM_HAS_CPP_ATTRIBUTE(nodiscard)
158#define LLVM_NODISCARD [[nodiscard]]
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100159#else
160#define LLVM_NODISCARD
161#endif
162
Andrew Scull0372a572018-11-16 15:47:06 +0000163// Indicate that a non-static, non-const C++ member function reinitializes
164// the entire object to a known state, independent of the previous state of
165// the object.
166//
167// The clang-tidy check bugprone-use-after-move recognizes this attribute as a
168// marker that a moved-from object has left the indeterminate state and can be
169// reused.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200170#if LLVM_HAS_CPP_ATTRIBUTE(clang::reinitializes)
Andrew Scull0372a572018-11-16 15:47:06 +0000171#define LLVM_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
172#else
173#define LLVM_ATTRIBUTE_REINITIALIZES
174#endif
175
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100176// Some compilers warn about unused functions. When a function is sometimes
177// used or not depending on build settings (e.g. a function only called from
178// within "assert"), this attribute can be used to suppress such warnings.
179//
180// However, it shouldn't be used for unused *variables*, as those have a much
181// more portable solution:
182// (void)unused_var_name;
183// Prefer cast-to-void wherever it is sufficient.
184#if __has_attribute(unused) || LLVM_GNUC_PREREQ(3, 1, 0)
185#define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
186#else
187#define LLVM_ATTRIBUTE_UNUSED
188#endif
189
190// FIXME: Provide this for PE/COFF targets.
191#if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
192 (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_WIN32))
193#define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
194#else
195#define LLVM_ATTRIBUTE_WEAK
196#endif
197
198// Prior to clang 3.2, clang did not accept any spelling of
199// __has_attribute(const), so assume it is supported.
200#if defined(__clang__) || defined(__GNUC__)
201// aka 'CONST' but following LLVM Conventions.
202#define LLVM_READNONE __attribute__((__const__))
203#else
204#define LLVM_READNONE
205#endif
206
207#if __has_attribute(pure) || defined(__GNUC__)
208// aka 'PURE' but following LLVM Conventions.
209#define LLVM_READONLY __attribute__((__pure__))
210#else
211#define LLVM_READONLY
212#endif
213
214#if __has_builtin(__builtin_expect) || LLVM_GNUC_PREREQ(4, 0, 0)
215#define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
216#define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
217#else
218#define LLVM_LIKELY(EXPR) (EXPR)
219#define LLVM_UNLIKELY(EXPR) (EXPR)
220#endif
221
222/// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
223/// mark a method "not for inlining".
224#if __has_attribute(noinline) || LLVM_GNUC_PREREQ(3, 4, 0)
225#define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
226#elif defined(_MSC_VER)
227#define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
228#else
229#define LLVM_ATTRIBUTE_NOINLINE
230#endif
231
232/// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
233/// so, mark a method "always inline" because it is performance sensitive. GCC
234/// 3.4 supported this but is buggy in various cases and produces unimplemented
235/// errors, just use it in GCC 4.0 and later.
236#if __has_attribute(always_inline) || LLVM_GNUC_PREREQ(4, 0, 0)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200237#define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100238#elif defined(_MSC_VER)
239#define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
240#else
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200241#define LLVM_ATTRIBUTE_ALWAYS_INLINE inline
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100242#endif
243
244#ifdef __GNUC__
245#define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
246#elif defined(_MSC_VER)
247#define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
248#else
249#define LLVM_ATTRIBUTE_NORETURN
250#endif
251
252#if __has_attribute(returns_nonnull) || LLVM_GNUC_PREREQ(4, 9, 0)
253#define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
254#elif defined(_MSC_VER)
255#define LLVM_ATTRIBUTE_RETURNS_NONNULL _Ret_notnull_
256#else
257#define LLVM_ATTRIBUTE_RETURNS_NONNULL
258#endif
259
260/// \macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a
261/// pointer that does not alias any other valid pointer.
262#ifdef __GNUC__
263#define LLVM_ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
264#elif defined(_MSC_VER)
265#define LLVM_ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
266#else
267#define LLVM_ATTRIBUTE_RETURNS_NOALIAS
268#endif
269
270/// LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200271#if defined(__cplusplus) && __cplusplus > 201402L && LLVM_HAS_CPP_ATTRIBUTE(fallthrough)
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100272#define LLVM_FALLTHROUGH [[fallthrough]]
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200273#elif LLVM_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100274#define LLVM_FALLTHROUGH [[gnu::fallthrough]]
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200275#elif __has_attribute(fallthrough)
276#define LLVM_FALLTHROUGH __attribute__((fallthrough))
277#elif LLVM_HAS_CPP_ATTRIBUTE(clang::fallthrough)
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100278#define LLVM_FALLTHROUGH [[clang::fallthrough]]
279#else
280#define LLVM_FALLTHROUGH
281#endif
282
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100283/// LLVM_REQUIRE_CONSTANT_INITIALIZATION - Apply this to globals to ensure that
284/// they are constant initialized.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200285#if LLVM_HAS_CPP_ATTRIBUTE(clang::require_constant_initialization)
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100286#define LLVM_REQUIRE_CONSTANT_INITIALIZATION \
287 [[clang::require_constant_initialization]]
288#else
289#define LLVM_REQUIRE_CONSTANT_INITIALIZATION
290#endif
291
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200292/// LLVM_GSL_OWNER - Apply this to owning classes like SmallVector to enable
293/// lifetime warnings.
294#if LLVM_HAS_CPP_ATTRIBUTE(gsl::Owner)
295#define LLVM_GSL_OWNER [[gsl::Owner]]
296#else
297#define LLVM_GSL_OWNER
298#endif
299
300/// LLVM_GSL_POINTER - Apply this to non-owning classes like
301/// StringRef to enable lifetime warnings.
302#if LLVM_HAS_CPP_ATTRIBUTE(gsl::Pointer)
303#define LLVM_GSL_POINTER [[gsl::Pointer]]
304#else
305#define LLVM_GSL_POINTER
306#endif
307
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100308/// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
309/// pedantic diagnostics.
310#ifdef __GNUC__
311#define LLVM_EXTENSION __extension__
312#else
313#define LLVM_EXTENSION
314#endif
315
316// LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200317// This macro will be removed.
318// Use C++14's attribute instead: [[deprecated("message")]]
319#define LLVM_ATTRIBUTE_DEPRECATED(decl, message) [[deprecated(message)]] decl
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100320
321/// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
322/// to an expression which states that it is undefined behavior for the
323/// compiler to reach this point. Otherwise is not defined.
324#if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0)
325# define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
326#elif defined(_MSC_VER)
327# define LLVM_BUILTIN_UNREACHABLE __assume(false)
328#endif
329
330/// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
331/// which causes the program to exit abnormally.
332#if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0)
333# define LLVM_BUILTIN_TRAP __builtin_trap()
334#elif defined(_MSC_VER)
335// The __debugbreak intrinsic is supported by MSVC, does not require forward
336// declarations involving platform-specific typedefs (unlike RaiseException),
337// results in a call to vectored exception handlers, and encodes to a short
338// instruction that still causes the trapping behavior we want.
339# define LLVM_BUILTIN_TRAP __debugbreak()
340#else
341# define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
342#endif
343
344/// LLVM_BUILTIN_DEBUGTRAP - On compilers which support it, expands to
345/// an expression which causes the program to break while running
346/// under a debugger.
347#if __has_builtin(__builtin_debugtrap)
348# define LLVM_BUILTIN_DEBUGTRAP __builtin_debugtrap()
349#elif defined(_MSC_VER)
350// The __debugbreak intrinsic is supported by MSVC and breaks while
351// running under the debugger, and also supports invoking a debugger
352// when the OS is configured appropriately.
353# define LLVM_BUILTIN_DEBUGTRAP __debugbreak()
354#else
355// Just continue execution when built with compilers that have no
356// support. This is a debugging aid and not intended to force the
357// program to abort if encountered.
358# define LLVM_BUILTIN_DEBUGTRAP
359#endif
360
361/// \macro LLVM_ASSUME_ALIGNED
362/// Returns a pointer with an assumed alignment.
363#if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
364# define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
365#elif defined(LLVM_BUILTIN_UNREACHABLE)
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100366# define LLVM_ASSUME_ALIGNED(p, a) \
367 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
368#else
369# define LLVM_ASSUME_ALIGNED(p, a) (p)
370#endif
371
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100372/// \macro LLVM_PACKED
373/// Used to specify a packed structure.
374/// LLVM_PACKED(
375/// struct A {
376/// int i;
377/// int j;
378/// int k;
379/// long long l;
380/// });
381///
382/// LLVM_PACKED_START
383/// struct B {
384/// int i;
385/// int j;
386/// int k;
387/// long long l;
388/// };
389/// LLVM_PACKED_END
390#ifdef _MSC_VER
391# define LLVM_PACKED(d) __pragma(pack(push, 1)) d __pragma(pack(pop))
392# define LLVM_PACKED_START __pragma(pack(push, 1))
393# define LLVM_PACKED_END __pragma(pack(pop))
394#else
395# define LLVM_PACKED(d) d __attribute__((packed))
396# define LLVM_PACKED_START _Pragma("pack(push, 1)")
397# define LLVM_PACKED_END _Pragma("pack(pop)")
398#endif
399
400/// \macro LLVM_PTR_SIZE
401/// A constant integer equivalent to the value of sizeof(void*).
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200402/// Generally used in combination with alignas or when doing computation in the
403/// preprocessor.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100404#ifdef __SIZEOF_POINTER__
405# define LLVM_PTR_SIZE __SIZEOF_POINTER__
406#elif defined(_WIN64)
407# define LLVM_PTR_SIZE 8
408#elif defined(_WIN32)
409# define LLVM_PTR_SIZE 4
410#elif defined(_MSC_VER)
411# error "could not determine LLVM_PTR_SIZE as a constant int for MSVC"
412#else
413# define LLVM_PTR_SIZE sizeof(void *)
414#endif
415
416/// \macro LLVM_MEMORY_SANITIZER_BUILD
417/// Whether LLVM itself is built with MemorySanitizer instrumentation.
418#if __has_feature(memory_sanitizer)
419# define LLVM_MEMORY_SANITIZER_BUILD 1
420# include <sanitizer/msan_interface.h>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200421# define LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE __attribute__((no_sanitize_memory))
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100422#else
423# define LLVM_MEMORY_SANITIZER_BUILD 0
424# define __msan_allocated_memory(p, size)
425# define __msan_unpoison(p, size)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200426# define LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100427#endif
428
429/// \macro LLVM_ADDRESS_SANITIZER_BUILD
430/// Whether LLVM itself is built with AddressSanitizer instrumentation.
431#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
432# define LLVM_ADDRESS_SANITIZER_BUILD 1
433# include <sanitizer/asan_interface.h>
434#else
435# define LLVM_ADDRESS_SANITIZER_BUILD 0
436# define __asan_poison_memory_region(p, size)
437# define __asan_unpoison_memory_region(p, size)
438#endif
439
440/// \macro LLVM_THREAD_SANITIZER_BUILD
441/// Whether LLVM itself is built with ThreadSanitizer instrumentation.
442#if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
443# define LLVM_THREAD_SANITIZER_BUILD 1
444#else
445# define LLVM_THREAD_SANITIZER_BUILD 0
446#endif
447
448#if LLVM_THREAD_SANITIZER_BUILD
449// Thread Sanitizer is a tool that finds races in code.
450// See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
451// tsan detects these exact functions by name.
452#ifdef __cplusplus
453extern "C" {
454#endif
455void AnnotateHappensAfter(const char *file, int line, const volatile void *cv);
456void AnnotateHappensBefore(const char *file, int line, const volatile void *cv);
457void AnnotateIgnoreWritesBegin(const char *file, int line);
458void AnnotateIgnoreWritesEnd(const char *file, int line);
459#ifdef __cplusplus
460}
461#endif
462
463// This marker is used to define a happens-before arc. The race detector will
464// infer an arc from the begin to the end when they share the same pointer
465// argument.
466# define TsanHappensBefore(cv) AnnotateHappensBefore(__FILE__, __LINE__, cv)
467
468// This marker defines the destination of a happens-before arc.
469# define TsanHappensAfter(cv) AnnotateHappensAfter(__FILE__, __LINE__, cv)
470
471// Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
472# define TsanIgnoreWritesBegin() AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
473
474// Resume checking for racy writes.
475# define TsanIgnoreWritesEnd() AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
476#else
477# define TsanHappensBefore(cv)
478# define TsanHappensAfter(cv)
479# define TsanIgnoreWritesBegin()
480# define TsanIgnoreWritesEnd()
481#endif
482
483/// \macro LLVM_NO_SANITIZE
484/// Disable a particular sanitizer for a function.
485#if __has_attribute(no_sanitize)
486#define LLVM_NO_SANITIZE(KIND) __attribute__((no_sanitize(KIND)))
487#else
488#define LLVM_NO_SANITIZE(KIND)
489#endif
490
491/// Mark debug helper function definitions like dump() that should not be
492/// stripped from debug builds.
493/// Note that you should also surround dump() functions with
494/// `#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)` so they do always
495/// get stripped in release builds.
496// FIXME: Move this to a private config.h as it's not usable in public headers.
497#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
498#define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
499#else
500#define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
501#endif
502
503/// \macro LLVM_PRETTY_FUNCTION
504/// Gets a user-friendly looking function signature for the current scope
505/// using the best available method on each platform. The exact format of the
506/// resulting string is implementation specific and non-portable, so this should
507/// only be used, for example, for logging or diagnostics.
508#if defined(_MSC_VER)
509#define LLVM_PRETTY_FUNCTION __FUNCSIG__
510#elif defined(__GNUC__) || defined(__clang__)
511#define LLVM_PRETTY_FUNCTION __PRETTY_FUNCTION__
512#else
513#define LLVM_PRETTY_FUNCTION __func__
514#endif
515
516/// \macro LLVM_THREAD_LOCAL
517/// A thread-local storage specifier which can be used with globals,
518/// extern globals, and static globals.
519///
520/// This is essentially an extremely restricted analog to C++11's thread_local
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200521/// support. It uses thread_local if available, falling back on gcc __thread
522/// if not. __thread doesn't support many of the C++11 thread_local's
523/// features. You should only use this for PODs that you can statically
524/// initialize to some constant value. In almost all circumstances this is most
525/// appropriate for use with a pointer, integer, or small aggregation of
526/// pointers and integers.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100527#if LLVM_ENABLE_THREADS
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200528#if __has_feature(cxx_thread_local) || defined(_MSC_VER)
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100529#define LLVM_THREAD_LOCAL thread_local
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100530#else
531// Clang, GCC, and other compatible compilers used __thread prior to C++11 and
532// we only need the restricted functionality that provides.
533#define LLVM_THREAD_LOCAL __thread
534#endif
535#else // !LLVM_ENABLE_THREADS
536// If threading is disabled entirely, this compiles to nothing and you get
537// a normal global variable.
538#define LLVM_THREAD_LOCAL
539#endif
540
541/// \macro LLVM_ENABLE_EXCEPTIONS
542/// Whether LLVM is built with exception support.
543#if __has_feature(cxx_exceptions)
544#define LLVM_ENABLE_EXCEPTIONS 1
545#elif defined(__GNUC__) && defined(__EXCEPTIONS)
546#define LLVM_ENABLE_EXCEPTIONS 1
547#elif defined(_MSC_VER) && defined(_CPPUNWIND)
548#define LLVM_ENABLE_EXCEPTIONS 1
549#endif
550
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100551#endif