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