LLVM 24.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#if __has_attribute(warn_unused)
242#define LLVM_ATTRIBUTE_WARN_UNUSED __attribute__((warn_unused))
243#else
244#define LLVM_ATTRIBUTE_WARN_UNUSED
245#endif
246
247// Only enabled for clang:
248// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99587
249// GCC may produce "warning: 'retain' attribute ignored" (despite
250// __has_attribute(retain) being 1).
251#if defined(__clang__) && __has_attribute(retain)
252#define LLVM_ATTRIBUTE_RETAIN __attribute__((__retain__))
253#else
254#define LLVM_ATTRIBUTE_RETAIN
255#endif
256
257// For deprecations that are not a simple rename, use [[deprecated(MSG)]]
258// instead.
259#if defined(__clang__)
260#define LLVM_DEPRECATED_WITH_FIXIT(MSG, FIX) \
261 __attribute__((deprecated(MSG, FIX)))
262#else
263#define LLVM_DEPRECATED_WITH_FIXIT(MSG, FIX) [[deprecated(MSG)]]
264#endif
265
266// clang-format off
267#if defined(__clang__) || defined(__GNUC__)
268#define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH \
269 _Pragma("GCC diagnostic push") \
270 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
271#define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP \
272 _Pragma("GCC diagnostic pop")
273#elif defined(_MSC_VER)
274#define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH \
275 _Pragma("warning(push)") \
276 _Pragma("warning(disable : 4996)")
277#define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP \
278 _Pragma("warning(pop)")
279#else
280#define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH
281#define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP
282#endif
283// clang-format on
284
285// Indicate that a non-static, non-const C++ member function reinitializes
286// the entire object to a known state, independent of the previous state of
287// the object.
288//
289// The clang-tidy check bugprone-use-after-move recognizes this attribute as a
290// marker that a moved-from object has left the indeterminate state and can be
291// reused.
292#if LLVM_HAS_CPP_ATTRIBUTE(clang::reinitializes)
293#define LLVM_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
294#else
295#define LLVM_ATTRIBUTE_REINITIALIZES
296#endif
297
298// Some compilers warn about unused functions. When a function is sometimes
299// used or not depending on build settings (e.g. a function only called from
300// within "assert"), this attribute can be used to suppress such warnings.
301//
302// However, it shouldn't be used for unused *variables*, as those have a much
303// more portable solution:
304// (void)unused_var_name;
305// Prefer cast-to-void wherever it is sufficient.
306#if __has_attribute(unused)
307#define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
308#else
309#define LLVM_ATTRIBUTE_UNUSED
310#endif
311
312// FIXME: Provide this for PE/COFF targets.
313#if __has_attribute(weak) && !defined(__MINGW32__) && !defined(__CYGWIN__) && \
314 !defined(_WIN32)
315#define LLVM_HAS_ATTRIBUTE_WEAK 1
316#else
317#define LLVM_HAS_ATTRIBUTE_WEAK 0
318#endif
319#if LLVM_HAS_ATTRIBUTE_WEAK
320#define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
321#else
322#define LLVM_ATTRIBUTE_WEAK
323#endif
324
325// Prior to clang 3.2, clang did not accept any spelling of
326// __has_attribute(const), so assume it is supported.
327#if defined(__clang__) || defined(__GNUC__)
328// aka 'CONST' but following LLVM Conventions.
329#define LLVM_READNONE __attribute__((__const__))
330#else
331#define LLVM_READNONE
332#endif
333
334#if __has_attribute(pure) || defined(__GNUC__)
335// aka 'PURE' but following LLVM Conventions.
336#define LLVM_READONLY __attribute__((__pure__))
337#else
338#define LLVM_READONLY
339#endif
340
341#if __has_attribute(minsize)
342#define LLVM_ATTRIBUTE_MINSIZE __attribute__((minsize))
343#else
344#define LLVM_ATTRIBUTE_MINSIZE
345#endif
346
347#if __has_builtin(__builtin_expect) || defined(__GNUC__)
348#define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
349#define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
350#else
351#define LLVM_LIKELY(EXPR) (EXPR)
352#define LLVM_UNLIKELY(EXPR) (EXPR)
353#endif
354
355/// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
356/// mark a method "not for inlining".
357#if __has_attribute(noinline)
358#define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
359#elif defined(_MSC_VER)
360#define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
361#else
362#define LLVM_ATTRIBUTE_NOINLINE
363#endif
364
365/// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
366/// so, mark a method "always inline" because it is performance sensitive.
367#if __has_attribute(always_inline)
368#define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
369#elif defined(_MSC_VER)
370#define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
371#else
372#define LLVM_ATTRIBUTE_ALWAYS_INLINE inline
373#endif
374
375/// LLVM_ATTRIBUTE_ALWAYS_INLINE_UNLESS_DEBUG - Like
376/// LLVM_ATTRIBUTE_ALWAYS_INLINE but disabled in debug builds to avoid stack
377/// overflow with deep recursion.
378#if defined(NDEBUG)
379#define LLVM_ATTRIBUTE_ALWAYS_INLINE_UNLESS_DEBUG LLVM_ATTRIBUTE_ALWAYS_INLINE
380#else
381#define LLVM_ATTRIBUTE_ALWAYS_INLINE_UNLESS_DEBUG inline
382#endif
383
384/// LLVM_ATTRIBUTE_NO_DEBUG - On compilers where we have a directive to do
385/// so, mark a method "no debug" because debug info makes the debugger
386/// experience worse.
387#if __has_attribute(nodebug)
388#define LLVM_ATTRIBUTE_NODEBUG __attribute__((nodebug))
389#else
390#define LLVM_ATTRIBUTE_NODEBUG
391#endif
392
393#if __has_attribute(returns_nonnull)
394#define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
395#elif defined(_MSC_VER)
396#define LLVM_ATTRIBUTE_RETURNS_NONNULL _Ret_notnull_
397#else
398#define LLVM_ATTRIBUTE_RETURNS_NONNULL
399#endif
400
401/// LLVM_ATTRIBUTE_RESTRICT - Annotates a pointer to tell the compiler that
402/// it is not aliased in the current scope.
403#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER)
404#define LLVM_ATTRIBUTE_RESTRICT __restrict
405#else
406#define LLVM_ATTRIBUTE_RESTRICT
407#endif
408
409/// \macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a
410/// pointer that does not alias any other valid pointer.
411#ifdef __GNUC__
412#define LLVM_ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
413#elif defined(_MSC_VER)
414#define LLVM_ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
415#else
416#define LLVM_ATTRIBUTE_RETURNS_NOALIAS
417#endif
418
419/// LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
420#if defined(__cplusplus) && __cplusplus > 201402L && LLVM_HAS_CPP_ATTRIBUTE(fallthrough)
421#define LLVM_FALLTHROUGH [[fallthrough]]
422#elif LLVM_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
423#define LLVM_FALLTHROUGH [[gnu::fallthrough]]
424#elif __has_attribute(fallthrough)
425#define LLVM_FALLTHROUGH __attribute__((fallthrough))
426#elif LLVM_HAS_CPP_ATTRIBUTE(clang::fallthrough)
427#define LLVM_FALLTHROUGH [[clang::fallthrough]]
428#else
429#define LLVM_FALLTHROUGH
430#endif
431
432/// LLVM_REQUIRE_CONSTANT_INITIALIZATION - Apply this to globals to ensure that
433/// they are constant initialized.
434#if LLVM_HAS_CPP_ATTRIBUTE(clang::require_constant_initialization)
435#define LLVM_REQUIRE_CONSTANT_INITIALIZATION \
436 [[clang::require_constant_initialization]]
437#else
438#define LLVM_REQUIRE_CONSTANT_INITIALIZATION
439#endif
440
441/// LLVM_GSL_OWNER - Apply this to owning classes like SmallVector to enable
442/// lifetime warnings.
443#if LLVM_HAS_CPP_ATTRIBUTE(gsl::Owner)
444#define LLVM_GSL_OWNER [[gsl::Owner]]
445#else
446#define LLVM_GSL_OWNER
447#endif
448
449/// LLVM_GSL_POINTER - Apply this to non-owning classes like
450/// StringRef to enable lifetime warnings.
451#if LLVM_HAS_CPP_ATTRIBUTE(gsl::Pointer)
452#define LLVM_GSL_POINTER [[gsl::Pointer]]
453#else
454#define LLVM_GSL_POINTER
455#endif
456
457#if LLVM_HAS_CPP_ATTRIBUTE(clang::lifetimebound)
458#define LLVM_LIFETIME_BOUND [[clang::lifetimebound]]
459#else
460#define LLVM_LIFETIME_BOUND
461#endif
462
463#if LLVM_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L
464#define LLVM_CTOR_NODISCARD [[nodiscard]]
465#else
466#define LLVM_CTOR_NODISCARD
467#endif
468
469// Macro to suppress the MSVC warning C4848:
470// "support for attribute [[msvc::no_unique_address]] in C++17 and earlier
471// is a vendor extension".
472// This warning is removed in versions >= 19.43.
473#if !defined(_MSC_VER) || _MSC_VER >= 1943 || defined(__clang__)
474#define LLVM_SUPPRESS_MSVC_ATTR_IS_VENDOR_EXT_PUSH
475#define LLVM_SUPPRESS_MSVC_ATTR_IS_VENDOR_EXT_POP
476#else // MSVC < 19.43
477#define LLVM_SUPPRESS_MSVC_ATTR_IS_VENDOR_EXT_PUSH \
478 _Pragma("warning(push)") _Pragma("warning(disable : 4848)")
479#define LLVM_SUPPRESS_MSVC_ATTR_IS_VENDOR_EXT_POP _Pragma("warning(pop)")
480#endif
481
482#if LLVM_HAS_CPP_ATTRIBUTE(no_unique_address)
483#define LLVM_NO_UNIQUE_ADDRESS [[no_unique_address]]
484#elif LLVM_HAS_CPP_ATTRIBUTE(msvc::no_unique_address)
485#define LLVM_NO_UNIQUE_ADDRESS \
486 LLVM_SUPPRESS_MSVC_ATTR_IS_VENDOR_EXT_PUSH \
487 [[msvc::no_unique_address]] LLVM_SUPPRESS_MSVC_ATTR_IS_VENDOR_EXT_POP
488#else
489#define LLVM_NO_UNIQUE_ADDRESS
490#endif
491
492/// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
493/// pedantic diagnostics.
494#ifdef __GNUC__
495#define LLVM_EXTENSION __extension__
496#else
497#define LLVM_EXTENSION
498#endif
499
500/// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
501/// to an expression which states that it is undefined behavior for the
502/// compiler to reach this point. Otherwise is not defined.
503///
504/// '#else' is intentionally left out so that other macro logic (e.g.,
505/// LLVM_ASSUME_ALIGNED and llvm_unreachable()) can detect whether
506/// LLVM_BUILTIN_UNREACHABLE has a definition.
507#if __has_builtin(__builtin_unreachable) || defined(__GNUC__)
508# define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
509#elif defined(_MSC_VER)
510# define LLVM_BUILTIN_UNREACHABLE __assume(false)
511#endif
512
513/// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
514/// which causes the program to exit abnormally.
515#if __has_builtin(__builtin_trap) || defined(__GNUC__)
516# define LLVM_BUILTIN_TRAP __builtin_trap()
517#elif defined(_MSC_VER)
518// The __debugbreak intrinsic is supported by MSVC, does not require forward
519// declarations involving platform-specific typedefs (unlike RaiseException),
520// results in a call to vectored exception handlers, and encodes to a short
521// instruction that still causes the trapping behavior we want.
522# define LLVM_BUILTIN_TRAP __debugbreak()
523#else
524# define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
525#endif
526
527/// LLVM_BUILTIN_DEBUGTRAP - On compilers which support it, expands to
528/// an expression which causes the program to break while running
529/// under a debugger.
530#if __has_builtin(__builtin_debugtrap)
531# define LLVM_BUILTIN_DEBUGTRAP __builtin_debugtrap()
532#elif defined(_MSC_VER)
533// The __debugbreak intrinsic is supported by MSVC and breaks while
534// running under the debugger, and also supports invoking a debugger
535// when the OS is configured appropriately.
536# define LLVM_BUILTIN_DEBUGTRAP __debugbreak()
537#else
538// Just continue execution when built with compilers that have no
539// support. This is a debugging aid and not intended to force the
540// program to abort if encountered.
541# define LLVM_BUILTIN_DEBUGTRAP
542#endif
543
544/// \macro LLVM_ASSUME_ALIGNED
545/// Returns a pointer with an assumed alignment.
546#if __has_builtin(__builtin_assume_aligned) || defined(__GNUC__)
547# define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
548#elif defined(LLVM_BUILTIN_UNREACHABLE)
549# define LLVM_ASSUME_ALIGNED(p, a) \
550 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
551#else
552# define LLVM_ASSUME_ALIGNED(p, a) (p)
553#endif
554
555/// \macro LLVM_PACKED
556/// Used to specify a packed structure.
557/// LLVM_PACKED(
558/// struct A {
559/// int i;
560/// int j;
561/// int k;
562/// long long l;
563/// });
564///
565/// LLVM_PACKED_START
566/// struct B {
567/// int i;
568/// int j;
569/// int k;
570/// long long l;
571/// };
572/// LLVM_PACKED_END
573#ifdef _MSC_VER
574# define LLVM_PACKED(d) __pragma(pack(push, 1)) d __pragma(pack(pop))
575# define LLVM_PACKED_START __pragma(pack(push, 1))
576# define LLVM_PACKED_END __pragma(pack(pop))
577#else
578# define LLVM_PACKED(d) d __attribute__((packed))
579# define LLVM_PACKED_START _Pragma("pack(push, 1)")
580# define LLVM_PACKED_END _Pragma("pack(pop)")
581#endif
582
583/// \macro LLVM_MEMORY_SANITIZER_BUILD
584/// Whether LLVM itself is built with MemorySanitizer instrumentation.
585#if __has_feature(memory_sanitizer)
586# define LLVM_MEMORY_SANITIZER_BUILD 1
587# include <sanitizer/msan_interface.h>
588# define LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE __attribute__((no_sanitize_memory))
589#else
590# define LLVM_MEMORY_SANITIZER_BUILD 0
591# define __msan_allocated_memory(p, size)
592# define __msan_unpoison(p, size)
593# define LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE
594#endif
595
596/// \macro LLVM_ADDRESS_SANITIZER_BUILD
597/// Whether LLVM itself is built with AddressSanitizer instrumentation.
598#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
599# define LLVM_ADDRESS_SANITIZER_BUILD 1
600#if __has_include(<sanitizer/asan_interface.h>)
601# include <sanitizer/asan_interface.h>
602#else
603// These declarations exist to support ASan with MSVC. If MSVC eventually ships
604// asan_interface.h in their headers, then we can remove this.
605#ifdef __cplusplus
606extern "C" {
607#endif
608void __asan_poison_memory_region(void const volatile *addr, size_t size);
609void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
610#ifdef __cplusplus
611} // extern "C"
612#endif
613#endif
614#else
615# define LLVM_ADDRESS_SANITIZER_BUILD 0
616# define __asan_poison_memory_region(p, size)
617# define __asan_unpoison_memory_region(p, size)
618#endif
619
620/// \macro LLVM_HWADDRESS_SANITIZER_BUILD
621/// Whether LLVM itself is built with HWAddressSanitizer instrumentation.
622#if __has_feature(hwaddress_sanitizer)
623#define LLVM_HWADDRESS_SANITIZER_BUILD 1
624#else
625#define LLVM_HWADDRESS_SANITIZER_BUILD 0
626#endif
627
628/// \macro LLVM_THREAD_SANITIZER_BUILD
629/// Whether LLVM itself is built with ThreadSanitizer instrumentation.
630#if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
631# define LLVM_THREAD_SANITIZER_BUILD 1
632#else
633# define LLVM_THREAD_SANITIZER_BUILD 0
634#endif
635
636#if LLVM_THREAD_SANITIZER_BUILD
637// Thread Sanitizer is a tool that finds races in code.
638// See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
639// tsan detects these exact functions by name.
640#ifdef __cplusplus
641extern "C" {
642#endif
643void AnnotateHappensAfter(const char *file, int line, const volatile void *cv);
644void AnnotateHappensBefore(const char *file, int line, const volatile void *cv);
645void AnnotateIgnoreWritesBegin(const char *file, int line);
646void AnnotateIgnoreWritesEnd(const char *file, int line);
647#ifdef __cplusplus
648}
649#endif
650
651// This marker is used to define a happens-before arc. The race detector will
652// infer an arc from the begin to the end when they share the same pointer
653// argument.
654# define TsanHappensBefore(cv) AnnotateHappensBefore(__FILE__, __LINE__, cv)
655
656// This marker defines the destination of a happens-before arc.
657# define TsanHappensAfter(cv) AnnotateHappensAfter(__FILE__, __LINE__, cv)
658
659// Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
660# define TsanIgnoreWritesBegin() AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
661
662// Resume checking for racy writes.
663# define TsanIgnoreWritesEnd() AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
664#else
665# define TsanHappensBefore(cv)
666# define TsanHappensAfter(cv)
667# define TsanIgnoreWritesBegin()
668# define TsanIgnoreWritesEnd()
669#endif
670
671/// \macro LLVM_NO_SANITIZE
672/// Disable a particular sanitizer for a function.
673#if __has_attribute(no_sanitize)
674#define LLVM_NO_SANITIZE(KIND) __attribute__((no_sanitize(KIND)))
675#else
676#define LLVM_NO_SANITIZE(KIND)
677#endif
678
679/// Mark debug helper function definitions like dump() that should not be
680/// stripped from debug builds.
681/// Note that you should also surround dump() functions with
682/// `#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)` so they do always
683/// get stripped in release builds.
684// FIXME: Move this to a private config.h as it's not usable in public headers.
685#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
686#define LLVM_DUMP_METHOD \
687 LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED LLVM_ATTRIBUTE_RETAIN
688#else
689#define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
690#endif
691
692/// \macro LLVM_PRETTY_FUNCTION
693/// Gets a user-friendly looking function signature for the current scope
694/// using the best available method on each platform. The exact format of the
695/// resulting string is implementation specific and non-portable, so this should
696/// only be used, for example, for logging or diagnostics.
697#if defined(_MSC_VER)
698#define LLVM_PRETTY_FUNCTION __FUNCSIG__
699#elif defined(__GNUC__) || defined(__clang__)
700#define LLVM_PRETTY_FUNCTION __PRETTY_FUNCTION__
701#else
702#define LLVM_PRETTY_FUNCTION __func__
703#endif
704
705/// \macro LLVM_THREAD_LOCAL
706/// A thread-local storage specifier which can be used with globals,
707/// extern globals, and static globals.
708///
709/// This is essentially an extremely restricted analog to C++11's thread_local
710/// support. It uses thread_local if available, falling back on gcc __thread
711/// if not. __thread doesn't support many of the C++11 thread_local's
712/// features. You should only use this for PODs that you can statically
713/// initialize to some constant value. In almost all circumstances this is most
714/// appropriate for use with a pointer, integer, or small aggregation of
715/// pointers and integers.
716#if LLVM_ENABLE_THREADS
717#if __has_feature(cxx_thread_local) || defined(_MSC_VER)
718#define LLVM_THREAD_LOCAL thread_local
719#else
720// Clang, GCC, and other compatible compilers used __thread prior to C++11 and
721// we only need the restricted functionality that provides.
722#define LLVM_THREAD_LOCAL __thread
723#endif
724#else // !LLVM_ENABLE_THREADS
725// If threading is disabled entirely, this compiles to nothing and you get
726// a normal global variable.
727#define LLVM_THREAD_LOCAL
728#endif
729
730/// \macro LLVM_ENABLE_EXCEPTIONS
731/// Whether LLVM is built with exception support.
732#if __has_feature(cxx_exceptions)
733#define LLVM_ENABLE_EXCEPTIONS 1
734#elif defined(__GNUC__) && defined(__EXCEPTIONS)
735#define LLVM_ENABLE_EXCEPTIONS 1
736#elif defined(_MSC_VER) && defined(_CPPUNWIND)
737#define LLVM_ENABLE_EXCEPTIONS 1
738#endif
739
740/// \macro LLVM_NO_PROFILE_INSTRUMENT_FUNCTION
741/// Disable the profile instrument for a function.
742#if __has_attribute(no_profile_instrument_function)
743#define LLVM_NO_PROFILE_INSTRUMENT_FUNCTION \
744 __attribute__((no_profile_instrument_function))
745#else
746#define LLVM_NO_PROFILE_INSTRUMENT_FUNCTION
747#endif
748
749/// \macro LLVM_PREFERRED_TYPE
750/// Adjust type of bit-field in debug info.
751#if __has_attribute(preferred_type)
752#define LLVM_PREFERRED_TYPE(T) __attribute__((preferred_type(T)))
753#else
754#define LLVM_PREFERRED_TYPE(T)
755#endif
756
757#if LLVM_HAS_CPP_ATTRIBUTE(clang::ptrauth_vtable_pointer) && \
758 (defined(__PTRAUTH__) || __has_feature(ptrauth_calls))
759#define LLVM_MOVABLE_POLYMORPHIC_TYPE \
760 [[clang::ptrauth_vtable_pointer(default_key, no_address_discrimination, \
761 default_extra_discrimination)]]
762#else
763#define LLVM_MOVABLE_POLYMORPHIC_TYPE
764#endif
765
766/// \macro LLVM_VIRTUAL_ANCHOR_FUNCTION
767/// This macro is used to adhere to LLVM's policy that each class with a vtable
768/// must have at least one out-of-line virtual function. This macro allows us
769/// to declare such a function in `final` classes without triggering a warning.
770// clang-format off
771// Autoformatting makes this look awful.
772#if defined(__clang__)
773 // Make sure this is only parsed if __clang__ is defined
774 #if __has_warning("-Wunnecessary-virtual-specifier")
775 #define LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION() \
776 _Pragma("clang diagnostic push") \
777 _Pragma("clang diagnostic ignored \"-Wunnecessary-virtual-specifier\"") \
778 virtual void anchor() \
779 _Pragma("clang diagnostic pop")
780 #else // __has_warning
781 #define LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION() \
782 virtual void anchor()
783 #endif
784#else // defined(__clang__)
785 #define LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION() \
786 virtual void anchor()
787#endif
788// clang-format on
789
790/// \macro LLVM_IS_X86
791/// Whether the target architecture is x86 / x86-64.
792#if defined(__x86_64__) || defined(__i386__)
793#define LLVM_IS_X86 1
794#else
795#define LLVM_IS_X86 0
796#endif
797
798/// \macro LLVM_TARGET_SSE42
799/// Function attribute to compile a function with SSE4.2 enabled.
800#if defined(__has_attribute) && __has_attribute(target)
801#define LLVM_TARGET_SSE42 __attribute__((target("sse4.2")))
802#else
803#define LLVM_TARGET_SSE42
804#endif
805
806#if __has_builtin(__builtin_cpu_supports) && \
807 (defined(__linux__) || defined(__APPLE__))
808#define LLVM_CPU_SUPPORTS(feature) __builtin_cpu_supports(feature)
809#else
810#define LLVM_CPU_SUPPORTS(feature) 0
811#endif
812
813/// \macro LLVM_CPU_SUPPORTS_SSE42
814/// Expands to true if the runtime cpu supports SSE4.2, or if compiled with
815/// SSE4.2 enabled.
816#if defined(__SSE4_2__)
817#define LLVM_CPU_SUPPORTS_SSE42 1
818#else
819#define LLVM_CPU_SUPPORTS_SSE42 LLVM_CPU_SUPPORTS("sse4.2")
820#endif
821
822#endif
#define __asan_poison_memory_region(p, size)
Definition Compiler.h:616
#define __asan_unpoison_memory_region(p, size)
Definition Compiler.h:617
dot regions Print regions of function to dot file(with no function bodies)"