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