LLVM 23.0.0git
Compiler.h
Go to the documentation of this file.
1//===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
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. This header
11// can be included from either C or C++.
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#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_builtin
39# define __has_builtin(x) 0
40#endif
41
42#ifndef __has_warning
43# define __has_warning(x) 0
44#endif
45
46// Only use __has_cpp_attribute in C++ mode. GCC defines __has_cpp_attribute in
47// C mode, but the :: in __has_cpp_attribute(scoped::attribute) is invalid.
48#ifndef LLVM_HAS_CPP_ATTRIBUTE
49#if defined(__cplusplus) && defined(__has_cpp_attribute)
50# define LLVM_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
51#else
52# define LLVM_HAS_CPP_ATTRIBUTE(x) 0
53#endif
54#endif
55
56/// \macro LLVM_GNUC_PREREQ
57/// Extend the default __GNUC_PREREQ even if glibc's features.h isn't
58/// available.
59#ifndef LLVM_GNUC_PREREQ
60# if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
61# define LLVM_GNUC_PREREQ(maj, min, patch) \
62 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
63 ((maj) << 20) + ((min) << 10) + (patch))
64# elif defined(__GNUC__) && defined(__GNUC_MINOR__)
65# define LLVM_GNUC_PREREQ(maj, min, patch) \
66 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
67# else
68# define LLVM_GNUC_PREREQ(maj, min, patch) 0
69# endif
70#endif
71
72/// \macro LLVM_MSC_PREREQ
73/// Is the compiler MSVC of at least the specified version?
74/// The common \param version values to check for are:
75/// * 1910: VS2017, version 15.1 & 15.2
76/// * 1911: VS2017, version 15.3 & 15.4
77/// * 1912: VS2017, version 15.5
78/// * 1913: VS2017, version 15.6
79/// * 1914: VS2017, version 15.7
80/// * 1915: VS2017, version 15.8
81/// * 1916: VS2017, version 15.9
82/// * 1920: VS2019, version 16.0
83/// * 1921: VS2019, version 16.1
84/// * 1922: VS2019, version 16.2
85/// * 1923: VS2019, version 16.3
86/// * 1924: VS2019, version 16.4
87/// * 1925: VS2019, version 16.5
88/// * 1926: VS2019, version 16.6
89/// * 1927: VS2019, version 16.7
90/// * 1928: VS2019, version 16.8 + 16.9
91/// * 1929: VS2019, version 16.10 + 16.11
92/// * 1930: VS2022, version 17.0
93#ifdef _MSC_VER
94#define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
95
96// We require at least VS 2019.
97#if !defined(LLVM_FORCE_USE_OLD_TOOLCHAIN)
98#if !LLVM_MSC_PREREQ(1920)
99#error LLVM requires at least VS 2019.
100#endif
101#endif
102
103#else
104#define LLVM_MSC_PREREQ(version) 0
105#endif
106
107/// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
108/// into a shared library, then the class should be private to the library and
109/// not accessible from outside it. Can also be used to mark variables and
110/// functions, making them private to any shared library they are linked into.
111/// On PE/COFF targets, library visibility is the default, so this isn't needed.
112///
113/// LLVM_EXTERNAL_VISIBILITY - classes, functions, and variables marked with
114/// this attribute will be made public and visible outside of any shared library
115/// they are linked in to.
116
117#if LLVM_HAS_CPP_ATTRIBUTE(gnu::visibility) && defined(__GNUC__) && \
118 !defined(__clang__)
119#define LLVM_ATTRIBUTE_VISIBILITY_HIDDEN [[gnu::visibility("hidden")]]
120#define LLVM_ATTRIBUTE_VISIBILITY_DEFAULT [[gnu::visibility("default")]]
121#elif __has_attribute(visibility)
122#define LLVM_ATTRIBUTE_VISIBILITY_HIDDEN __attribute__((visibility("hidden")))
123#define LLVM_ATTRIBUTE_VISIBILITY_DEFAULT __attribute__((visibility("default")))
124#else
125#define LLVM_ATTRIBUTE_VISIBILITY_HIDDEN
126#define LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
127#endif
128
129#if defined(LLVM_BUILD_LLVM_DYLIB) || defined(LLVM_BUILD_SHARED_LIBS)
130#define LLVM_EXTERNAL_VISIBILITY LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
131#else
132#define LLVM_EXTERNAL_VISIBILITY
133#endif
134
135#if (!(defined(_WIN32) || defined(__CYGWIN__)) || \
136 ((defined(__MINGW32__) || defined(__CYGWIN__)) && defined(__clang__)))
137#define LLVM_LIBRARY_VISIBILITY LLVM_ATTRIBUTE_VISIBILITY_HIDDEN
138// Clang compilers older then 15 do not support gnu style attributes on
139// namespaces.
140#if defined(__clang__) && __clang_major__ < 15
141#define LLVM_LIBRARY_VISIBILITY_NAMESPACE [[gnu::visibility("hidden")]]
142#else
143#define LLVM_LIBRARY_VISIBILITY_NAMESPACE LLVM_ATTRIBUTE_VISIBILITY_HIDDEN
144#endif
145#define LLVM_ALWAYS_EXPORT LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
146#elif defined(_WIN32)
147#define LLVM_ALWAYS_EXPORT __declspec(dllexport)
148#define LLVM_LIBRARY_VISIBILITY
149#define LLVM_LIBRARY_VISIBILITY_NAMESPACE
150#else
151#define LLVM_LIBRARY_VISIBILITY
152#define LLVM_ALWAYS_EXPORT
153#define LLVM_LIBRARY_VISIBILITY_NAMESPACE
154#endif
155
156/// LLVM_ABI is the main export/visibility macro to mark something as explicitly
157/// exported when llvm is built as a shared library with everything else that is
158/// unannotated will have internal visibility.
159///
160/// LLVM_ABI_EXPORT is for the special case for things like plugin symbol
161/// declarations or definitions where we don't want the macro to be switching
162/// between dllexport and dllimport on windows based on what codebase is being
163/// built, it will only be dllexport. For non windows platforms this macro
164/// behaves the same as LLVM_ABI.
165///
166/// LLVM_EXPORT_TEMPLATE is used on explicit template instantiations in source
167/// files that were declared extern in a header. This macro is only set as a
168/// compiler export attribute on windows, on other platforms it does nothing.
169///
170/// LLVM_TEMPLATE_ABI is for annotating extern template declarations in headers
171/// for both functions and classes. On windows its turned in to dllimport for
172/// library consumers, for other platforms its a default visibility attribute.
173///
174/// LLVM_ABI_FOR_TEST is for annotating symbols that are exported from a
175/// library-internal header solely so that unit tests can link against them.
176/// Symbols in LLVM's public headers are part of the LLVM public interface and
177/// should use LLVM_ABI. LLVM_ABI_FOR_TEST is reserved for internal headers,
178/// whose symbols could be conditionally excluded when not building tests in the
179/// future.
180///
181#ifndef LLVM_ABI_GENERATING_ANNOTATIONS
182// Marker to add to classes or functions in public headers that should not have
183// export macros added to them by the clang tool
184#define LLVM_ABI_NOT_EXPORTED
185// TODO(https://github.com/llvm/llvm-project/issues/145406): eliminate need for
186// two preprocessor definitions to gate LLVM_ABI macro definitions.
187#if defined(LLVM_ENABLE_LLVM_EXPORT_ANNOTATIONS) && !defined(LLVM_BUILD_STATIC)
188#if defined(_WIN32) && !defined(__MINGW32__)
189#if defined(LLVM_EXPORTS)
190#define LLVM_ABI __declspec(dllexport)
191#define LLVM_TEMPLATE_ABI
192#define LLVM_EXPORT_TEMPLATE __declspec(dllexport)
193#else
194#define LLVM_ABI __declspec(dllimport)
195#define LLVM_TEMPLATE_ABI __declspec(dllimport)
196#define LLVM_EXPORT_TEMPLATE
197#endif
198#define LLVM_ABI_EXPORT __declspec(dllexport)
199#elif __has_attribute(visibility)
200#if defined(__ELF__) || defined(__MINGW32__) || defined(_AIX) || \
201 defined(__MVS__) || defined(__CYGWIN__)
202#define LLVM_ABI __attribute__((visibility("default")))
203#define LLVM_TEMPLATE_ABI LLVM_ABI
204#define LLVM_EXPORT_TEMPLATE
205#define LLVM_ABI_EXPORT LLVM_ABI
206#elif defined(__MACH__) || defined(__WASM__) || defined(__EMSCRIPTEN__)
207#define LLVM_ABI __attribute__((visibility("default")))
208#define LLVM_TEMPLATE_ABI
209#define LLVM_EXPORT_TEMPLATE
210#define LLVM_ABI_EXPORT LLVM_ABI
211#endif
212#endif
213#endif
214#if !defined(LLVM_ABI)
215#define LLVM_ABI
216#define LLVM_TEMPLATE_ABI
217#define LLVM_EXPORT_TEMPLATE
218#define LLVM_ABI_EXPORT
219#endif
220#define LLVM_ABI_FOR_TEST LLVM_ABI
221#endif
222
223#if defined(__GNUC__)
224#define LLVM_PREFETCH(addr, rw, locality) __builtin_prefetch(addr, rw, locality)
225#else
226#define LLVM_PREFETCH(addr, rw, locality)
227#endif
228
229#if __has_attribute(uninitialized)
230#define LLVM_ATTRIBUTE_UNINITIALIZED __attribute__((uninitialized))
231#else
232#define LLVM_ATTRIBUTE_UNINITIALIZED
233#endif
234
235#if __has_attribute(used)
236#define LLVM_ATTRIBUTE_USED __attribute__((__used__))
237#else
238#define LLVM_ATTRIBUTE_USED
239#endif
240
241// Only enabled for clang:
242// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99587
243// GCC may produce "warning: 'retain' attribute ignored" (despite
244// __has_attribute(retain) being 1).
245#if defined(__clang__) && __has_attribute(retain)
246#define LLVM_ATTRIBUTE_RETAIN __attribute__((__retain__))
247#else
248#define LLVM_ATTRIBUTE_RETAIN
249#endif
250
251#if defined(__clang__)
252#define LLVM_DEPRECATED(MSG, FIX) __attribute__((deprecated(MSG, FIX)))
253#else
254#define LLVM_DEPRECATED(MSG, FIX) [[deprecated(MSG)]]
255#endif
256
257// clang-format off
258#if defined(__clang__) || defined(__GNUC__)
259#define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH \
260 _Pragma("GCC diagnostic push") \
261 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
262#define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP \
263 _Pragma("GCC diagnostic pop")
264#elif defined(_MSC_VER)
265#define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH \
266 _Pragma("warning(push)") \
267 _Pragma("warning(disable : 4996)")
268#define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP \
269 _Pragma("warning(pop)")
270#else
271#define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH
272#define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP
273#endif
274// clang-format on
275
276// Indicate that a non-static, non-const C++ member function reinitializes
277// the entire object to a known state, independent of the previous state of
278// the object.
279//
280// The clang-tidy check bugprone-use-after-move recognizes this attribute as a
281// marker that a moved-from object has left the indeterminate state and can be
282// reused.
283#if LLVM_HAS_CPP_ATTRIBUTE(clang::reinitializes)
284#define LLVM_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
285#else
286#define LLVM_ATTRIBUTE_REINITIALIZES
287#endif
288
289// Some compilers warn about unused functions. When a function is sometimes
290// used or not depending on build settings (e.g. a function only called from
291// within "assert"), this attribute can be used to suppress such warnings.
292//
293// However, it shouldn't be used for unused *variables*, as those have a much
294// more portable solution:
295// (void)unused_var_name;
296// Prefer cast-to-void wherever it is sufficient.
297#if __has_attribute(unused)
298#define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
299#else
300#define LLVM_ATTRIBUTE_UNUSED
301#endif
302
303// FIXME: Provide this for PE/COFF targets.
304#if __has_attribute(weak) && !defined(__MINGW32__) && !defined(__CYGWIN__) && \
305 !defined(_WIN32)
306#define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
307#else
308#define LLVM_ATTRIBUTE_WEAK
309#endif
310
311// Prior to clang 3.2, clang did not accept any spelling of
312// __has_attribute(const), so assume it is supported.
313#if defined(__clang__) || defined(__GNUC__)
314// aka 'CONST' but following LLVM Conventions.
315#define LLVM_READNONE __attribute__((__const__))
316#else
317#define LLVM_READNONE
318#endif
319
320#if __has_attribute(pure) || defined(__GNUC__)
321// aka 'PURE' but following LLVM Conventions.
322#define LLVM_READONLY __attribute__((__pure__))
323#else
324#define LLVM_READONLY
325#endif
326
327#if __has_attribute(minsize)
328#define LLVM_ATTRIBUTE_MINSIZE __attribute__((minsize))
329#else
330#define LLVM_ATTRIBUTE_MINSIZE
331#endif
332
333#if __has_builtin(__builtin_expect) || defined(__GNUC__)
334#define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
335#define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
336#else
337#define LLVM_LIKELY(EXPR) (EXPR)
338#define LLVM_UNLIKELY(EXPR) (EXPR)
339#endif
340
341/// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
342/// mark a method "not for inlining".
343#if __has_attribute(noinline)
344#define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
345#elif defined(_MSC_VER)
346#define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
347#else
348#define LLVM_ATTRIBUTE_NOINLINE
349#endif
350
351/// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
352/// so, mark a method "always inline" because it is performance sensitive.
353#if __has_attribute(always_inline)
354#define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
355#elif defined(_MSC_VER)
356#define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
357#else
358#define LLVM_ATTRIBUTE_ALWAYS_INLINE inline
359#endif
360
361/// LLVM_ATTRIBUTE_ALWAYS_INLINE_UNLESS_DEBUG - Like
362/// LLVM_ATTRIBUTE_ALWAYS_INLINE but disabled in debug builds to avoid stack
363/// overflow with deep recursion.
364#if defined(NDEBUG)
365#define LLVM_ATTRIBUTE_ALWAYS_INLINE_UNLESS_DEBUG LLVM_ATTRIBUTE_ALWAYS_INLINE
366#else
367#define LLVM_ATTRIBUTE_ALWAYS_INLINE_UNLESS_DEBUG inline
368#endif
369
370/// LLVM_ATTRIBUTE_NO_DEBUG - On compilers where we have a directive to do
371/// so, mark a method "no debug" because debug info makes the debugger
372/// experience worse.
373#if __has_attribute(nodebug)
374#define LLVM_ATTRIBUTE_NODEBUG __attribute__((nodebug))
375#else
376#define LLVM_ATTRIBUTE_NODEBUG
377#endif
378
379#if __has_attribute(returns_nonnull)
380#define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
381#elif defined(_MSC_VER)
382#define LLVM_ATTRIBUTE_RETURNS_NONNULL _Ret_notnull_
383#else
384#define LLVM_ATTRIBUTE_RETURNS_NONNULL
385#endif
386
387/// LLVM_ATTRIBUTE_RESTRICT - Annotates a pointer to tell the compiler that
388/// it is not aliased in the current scope.
389#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER)
390#define LLVM_ATTRIBUTE_RESTRICT __restrict
391#else
392#define LLVM_ATTRIBUTE_RESTRICT
393#endif
394
395/// \macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a
396/// pointer that does not alias any other valid pointer.
397#ifdef __GNUC__
398#define LLVM_ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
399#elif defined(_MSC_VER)
400#define LLVM_ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
401#else
402#define LLVM_ATTRIBUTE_RETURNS_NOALIAS
403#endif
404
405/// LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
406#if defined(__cplusplus) && __cplusplus > 201402L && LLVM_HAS_CPP_ATTRIBUTE(fallthrough)
407#define LLVM_FALLTHROUGH [[fallthrough]]
408#elif LLVM_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
409#define LLVM_FALLTHROUGH [[gnu::fallthrough]]
410#elif __has_attribute(fallthrough)
411#define LLVM_FALLTHROUGH __attribute__((fallthrough))
412#elif LLVM_HAS_CPP_ATTRIBUTE(clang::fallthrough)
413#define LLVM_FALLTHROUGH [[clang::fallthrough]]
414#else
415#define LLVM_FALLTHROUGH
416#endif
417
418/// LLVM_REQUIRE_CONSTANT_INITIALIZATION - Apply this to globals to ensure that
419/// they are constant initialized.
420#if LLVM_HAS_CPP_ATTRIBUTE(clang::require_constant_initialization)
421#define LLVM_REQUIRE_CONSTANT_INITIALIZATION \
422 [[clang::require_constant_initialization]]
423#else
424#define LLVM_REQUIRE_CONSTANT_INITIALIZATION
425#endif
426
427/// LLVM_GSL_OWNER - Apply this to owning classes like SmallVector to enable
428/// lifetime warnings.
429#if LLVM_HAS_CPP_ATTRIBUTE(gsl::Owner)
430#define LLVM_GSL_OWNER [[gsl::Owner]]
431#else
432#define LLVM_GSL_OWNER
433#endif
434
435/// LLVM_GSL_POINTER - Apply this to non-owning classes like
436/// StringRef to enable lifetime warnings.
437#if LLVM_HAS_CPP_ATTRIBUTE(gsl::Pointer)
438#define LLVM_GSL_POINTER [[gsl::Pointer]]
439#else
440#define LLVM_GSL_POINTER
441#endif
442
443#if LLVM_HAS_CPP_ATTRIBUTE(clang::lifetimebound)
444#define LLVM_LIFETIME_BOUND [[clang::lifetimebound]]
445#else
446#define LLVM_LIFETIME_BOUND
447#endif
448
449#if LLVM_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L
450#define LLVM_CTOR_NODISCARD [[nodiscard]]
451#else
452#define LLVM_CTOR_NODISCARD
453#endif
454
455// Macro to suppress the MSVC warning C4848:
456// "support for attribute [[msvc::no_unique_address]] in C++17 and earlier
457// is a vendor extension".
458// This warning is removed in versions >= 19.43.
459#if !defined(_MSC_VER) || _MSC_VER >= 1943 || defined(__clang__)
460#define LLVM_SUPPRESS_MSVC_ATTR_IS_VENDOR_EXT_PUSH
461#define LLVM_SUPPRESS_MSVC_ATTR_IS_VENDOR_EXT_POP
462#else // MSVC < 19.43
463#define LLVM_SUPPRESS_MSVC_ATTR_IS_VENDOR_EXT_PUSH \
464 _Pragma("warning(push)") _Pragma("warning(disable : 4848)")
465#define LLVM_SUPPRESS_MSVC_ATTR_IS_VENDOR_EXT_POP _Pragma("warning(pop)")
466#endif
467
468#if LLVM_HAS_CPP_ATTRIBUTE(no_unique_address)
469#define LLVM_NO_UNIQUE_ADDRESS [[no_unique_address]]
470#elif LLVM_HAS_CPP_ATTRIBUTE(msvc::no_unique_address)
471#define LLVM_NO_UNIQUE_ADDRESS \
472 LLVM_SUPPRESS_MSVC_ATTR_IS_VENDOR_EXT_PUSH \
473 [[msvc::no_unique_address]] LLVM_SUPPRESS_MSVC_ATTR_IS_VENDOR_EXT_POP
474#else
475#define LLVM_NO_UNIQUE_ADDRESS
476#endif
477
478/// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
479/// pedantic diagnostics.
480#ifdef __GNUC__
481#define LLVM_EXTENSION __extension__
482#else
483#define LLVM_EXTENSION
484#endif
485
486/// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
487/// to an expression which states that it is undefined behavior for the
488/// compiler to reach this point. Otherwise is not defined.
489///
490/// '#else' is intentionally left out so that other macro logic (e.g.,
491/// LLVM_ASSUME_ALIGNED and llvm_unreachable()) can detect whether
492/// LLVM_BUILTIN_UNREACHABLE has a definition.
493#if __has_builtin(__builtin_unreachable) || defined(__GNUC__)
494# define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
495#elif defined(_MSC_VER)
496# define LLVM_BUILTIN_UNREACHABLE __assume(false)
497#endif
498
499/// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
500/// which causes the program to exit abnormally.
501#if __has_builtin(__builtin_trap) || defined(__GNUC__)
502# define LLVM_BUILTIN_TRAP __builtin_trap()
503#elif defined(_MSC_VER)
504// The __debugbreak intrinsic is supported by MSVC, does not require forward
505// declarations involving platform-specific typedefs (unlike RaiseException),
506// results in a call to vectored exception handlers, and encodes to a short
507// instruction that still causes the trapping behavior we want.
508# define LLVM_BUILTIN_TRAP __debugbreak()
509#else
510# define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
511#endif
512
513/// LLVM_BUILTIN_DEBUGTRAP - On compilers which support it, expands to
514/// an expression which causes the program to break while running
515/// under a debugger.
516#if __has_builtin(__builtin_debugtrap)
517# define LLVM_BUILTIN_DEBUGTRAP __builtin_debugtrap()
518#elif defined(_MSC_VER)
519// The __debugbreak intrinsic is supported by MSVC and breaks while
520// running under the debugger, and also supports invoking a debugger
521// when the OS is configured appropriately.
522# define LLVM_BUILTIN_DEBUGTRAP __debugbreak()
523#else
524// Just continue execution when built with compilers that have no
525// support. This is a debugging aid and not intended to force the
526// program to abort if encountered.
527# define LLVM_BUILTIN_DEBUGTRAP
528#endif
529
530/// \macro LLVM_ASSUME_ALIGNED
531/// Returns a pointer with an assumed alignment.
532#if __has_builtin(__builtin_assume_aligned) || defined(__GNUC__)
533# define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
534#elif defined(LLVM_BUILTIN_UNREACHABLE)
535# define LLVM_ASSUME_ALIGNED(p, a) \
536 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
537#else
538# define LLVM_ASSUME_ALIGNED(p, a) (p)
539#endif
540
541/// \macro LLVM_PACKED
542/// Used to specify a packed structure.
543/// LLVM_PACKED(
544/// struct A {
545/// int i;
546/// int j;
547/// int k;
548/// long long l;
549/// });
550///
551/// LLVM_PACKED_START
552/// struct B {
553/// int i;
554/// int j;
555/// int k;
556/// long long l;
557/// };
558/// LLVM_PACKED_END
559#ifdef _MSC_VER
560# define LLVM_PACKED(d) __pragma(pack(push, 1)) d __pragma(pack(pop))
561# define LLVM_PACKED_START __pragma(pack(push, 1))
562# define LLVM_PACKED_END __pragma(pack(pop))
563#else
564# define LLVM_PACKED(d) d __attribute__((packed))
565# define LLVM_PACKED_START _Pragma("pack(push, 1)")
566# define LLVM_PACKED_END _Pragma("pack(pop)")
567#endif
568
569/// \macro LLVM_MEMORY_SANITIZER_BUILD
570/// Whether LLVM itself is built with MemorySanitizer instrumentation.
571#if __has_feature(memory_sanitizer)
572# define LLVM_MEMORY_SANITIZER_BUILD 1
573# include <sanitizer/msan_interface.h>
574# define LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE __attribute__((no_sanitize_memory))
575#else
576# define LLVM_MEMORY_SANITIZER_BUILD 0
577# define __msan_allocated_memory(p, size)
578# define __msan_unpoison(p, size)
579# define LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE
580#endif
581
582/// \macro LLVM_ADDRESS_SANITIZER_BUILD
583/// Whether LLVM itself is built with AddressSanitizer instrumentation.
584#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
585# define LLVM_ADDRESS_SANITIZER_BUILD 1
586#if __has_include(<sanitizer/asan_interface.h>)
587# include <sanitizer/asan_interface.h>
588#else
589// These declarations exist to support ASan with MSVC. If MSVC eventually ships
590// asan_interface.h in their headers, then we can remove this.
591#ifdef __cplusplus
592extern "C" {
593#endif
594void __asan_poison_memory_region(void const volatile *addr, size_t size);
595void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
596#ifdef __cplusplus
597} // extern "C"
598#endif
599#endif
600#else
601# define LLVM_ADDRESS_SANITIZER_BUILD 0
602# define __asan_poison_memory_region(p, size)
603# define __asan_unpoison_memory_region(p, size)
604#endif
605
606/// \macro LLVM_HWADDRESS_SANITIZER_BUILD
607/// Whether LLVM itself is built with HWAddressSanitizer instrumentation.
608#if __has_feature(hwaddress_sanitizer)
609#define LLVM_HWADDRESS_SANITIZER_BUILD 1
610#else
611#define LLVM_HWADDRESS_SANITIZER_BUILD 0
612#endif
613
614/// \macro LLVM_THREAD_SANITIZER_BUILD
615/// Whether LLVM itself is built with ThreadSanitizer instrumentation.
616#if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
617# define LLVM_THREAD_SANITIZER_BUILD 1
618#else
619# define LLVM_THREAD_SANITIZER_BUILD 0
620#endif
621
622#if LLVM_THREAD_SANITIZER_BUILD
623// Thread Sanitizer is a tool that finds races in code.
624// See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
625// tsan detects these exact functions by name.
626#ifdef __cplusplus
627extern "C" {
628#endif
629void AnnotateHappensAfter(const char *file, int line, const volatile void *cv);
630void AnnotateHappensBefore(const char *file, int line, const volatile void *cv);
631void AnnotateIgnoreWritesBegin(const char *file, int line);
632void AnnotateIgnoreWritesEnd(const char *file, int line);
633#ifdef __cplusplus
634}
635#endif
636
637// This marker is used to define a happens-before arc. The race detector will
638// infer an arc from the begin to the end when they share the same pointer
639// argument.
640# define TsanHappensBefore(cv) AnnotateHappensBefore(__FILE__, __LINE__, cv)
641
642// This marker defines the destination of a happens-before arc.
643# define TsanHappensAfter(cv) AnnotateHappensAfter(__FILE__, __LINE__, cv)
644
645// Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
646# define TsanIgnoreWritesBegin() AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
647
648// Resume checking for racy writes.
649# define TsanIgnoreWritesEnd() AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
650#else
651# define TsanHappensBefore(cv)
652# define TsanHappensAfter(cv)
653# define TsanIgnoreWritesBegin()
654# define TsanIgnoreWritesEnd()
655#endif
656
657/// \macro LLVM_NO_SANITIZE
658/// Disable a particular sanitizer for a function.
659#if __has_attribute(no_sanitize)
660#define LLVM_NO_SANITIZE(KIND) __attribute__((no_sanitize(KIND)))
661#else
662#define LLVM_NO_SANITIZE(KIND)
663#endif
664
665/// Mark debug helper function definitions like dump() that should not be
666/// stripped from debug builds.
667/// Note that you should also surround dump() functions with
668/// `#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)` so they do always
669/// get stripped in release builds.
670// FIXME: Move this to a private config.h as it's not usable in public headers.
671#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
672#define LLVM_DUMP_METHOD \
673 LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED LLVM_ATTRIBUTE_RETAIN
674#else
675#define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
676#endif
677
678/// \macro LLVM_PRETTY_FUNCTION
679/// Gets a user-friendly looking function signature for the current scope
680/// using the best available method on each platform. The exact format of the
681/// resulting string is implementation specific and non-portable, so this should
682/// only be used, for example, for logging or diagnostics.
683#if defined(_MSC_VER)
684#define LLVM_PRETTY_FUNCTION __FUNCSIG__
685#elif defined(__GNUC__) || defined(__clang__)
686#define LLVM_PRETTY_FUNCTION __PRETTY_FUNCTION__
687#else
688#define LLVM_PRETTY_FUNCTION __func__
689#endif
690
691/// \macro LLVM_THREAD_LOCAL
692/// A thread-local storage specifier which can be used with globals,
693/// extern globals, and static globals.
694///
695/// This is essentially an extremely restricted analog to C++11's thread_local
696/// support. It uses thread_local if available, falling back on gcc __thread
697/// if not. __thread doesn't support many of the C++11 thread_local's
698/// features. You should only use this for PODs that you can statically
699/// initialize to some constant value. In almost all circumstances this is most
700/// appropriate for use with a pointer, integer, or small aggregation of
701/// pointers and integers.
702#if LLVM_ENABLE_THREADS
703#if __has_feature(cxx_thread_local) || defined(_MSC_VER)
704#define LLVM_THREAD_LOCAL thread_local
705#else
706// Clang, GCC, and other compatible compilers used __thread prior to C++11 and
707// we only need the restricted functionality that provides.
708#define LLVM_THREAD_LOCAL __thread
709#endif
710#else // !LLVM_ENABLE_THREADS
711// If threading is disabled entirely, this compiles to nothing and you get
712// a normal global variable.
713#define LLVM_THREAD_LOCAL
714#endif
715
716/// \macro LLVM_ENABLE_EXCEPTIONS
717/// Whether LLVM is built with exception support.
718#if __has_feature(cxx_exceptions)
719#define LLVM_ENABLE_EXCEPTIONS 1
720#elif defined(__GNUC__) && defined(__EXCEPTIONS)
721#define LLVM_ENABLE_EXCEPTIONS 1
722#elif defined(_MSC_VER) && defined(_CPPUNWIND)
723#define LLVM_ENABLE_EXCEPTIONS 1
724#endif
725
726/// \macro LLVM_NO_PROFILE_INSTRUMENT_FUNCTION
727/// Disable the profile instrument for a function.
728#if __has_attribute(no_profile_instrument_function)
729#define LLVM_NO_PROFILE_INSTRUMENT_FUNCTION \
730 __attribute__((no_profile_instrument_function))
731#else
732#define LLVM_NO_PROFILE_INSTRUMENT_FUNCTION
733#endif
734
735/// \macro LLVM_PREFERRED_TYPE
736/// Adjust type of bit-field in debug info.
737#if __has_attribute(preferred_type)
738#define LLVM_PREFERRED_TYPE(T) __attribute__((preferred_type(T)))
739#else
740#define LLVM_PREFERRED_TYPE(T)
741#endif
742
743#if LLVM_HAS_CPP_ATTRIBUTE(clang::ptrauth_vtable_pointer) && \
744 (defined(__PTRAUTH__) || __has_feature(ptrauth_calls))
745#define LLVM_MOVABLE_POLYMORPHIC_TYPE \
746 [[clang::ptrauth_vtable_pointer(default_key, no_address_discrimination, \
747 default_extra_discrimination)]]
748#else
749#define LLVM_MOVABLE_POLYMORPHIC_TYPE
750#endif
751
752/// \macro LLVM_VIRTUAL_ANCHOR_FUNCTION
753/// This macro is used to adhere to LLVM's policy that each class with a vtable
754/// must have at least one out-of-line virtual function. This macro allows us
755/// to declare such a function in `final` classes without triggering a warning.
756// clang-format off
757// Autoformatting makes this look awful.
758#if defined(__clang__)
759 // Make sure this is only parsed if __clang__ is defined
760 #if __has_warning("-Wunnecessary-virtual-specifier")
761 #define LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION() \
762 _Pragma("clang diagnostic push") \
763 _Pragma("clang diagnostic ignored \"-Wunnecessary-virtual-specifier\"") \
764 virtual void anchor() \
765 _Pragma("clang diagnostic pop")
766 #else // __has_warning
767 #define LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION() \
768 virtual void anchor()
769 #endif
770#else // defined(__clang__)
771 #define LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION() \
772 virtual void anchor()
773#endif
774// clang-format on
775
776#endif
#define __asan_poison_memory_region(p, size)
Definition Compiler.h:602
#define __asan_unpoison_memory_region(p, size)
Definition Compiler.h:603
dot regions Print regions of function to dot file(with no function bodies)"