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