1 //===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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++.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_COMPILER_H
16 #define LLVM_SUPPORT_COMPILER_H
18 #include "llvm/Config/llvm-config.h"
30 # define __has_feature(x) 0
33 #ifndef __has_extension
34 # define __has_extension(x) 0
37 #ifndef __has_attribute
38 # define __has_attribute(x) 0
42 # define __has_builtin(x) 0
45 // Only use __has_cpp_attribute in C++ mode. GCC defines __has_cpp_attribute in
46 // C mode, but the :: in __has_cpp_attribute(scoped::attribute) is invalid.
47 #ifndef LLVM_HAS_CPP_ATTRIBUTE
48 #if defined(__cplusplus) && defined(__has_cpp_attribute)
49 # define LLVM_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
51 # define LLVM_HAS_CPP_ATTRIBUTE(x) 0
55 /// \macro LLVM_GNUC_PREREQ
56 /// Extend the default __GNUC_PREREQ even if glibc's features.h isn't
58 #ifndef LLVM_GNUC_PREREQ
59 # if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
60 # define LLVM_GNUC_PREREQ(maj, min, patch) \
61 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
62 ((maj) << 20) + ((min) << 10) + (patch))
63 # elif defined(__GNUC__) && defined(__GNUC_MINOR__)
64 # define LLVM_GNUC_PREREQ(maj, min, patch) \
65 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
67 # define LLVM_GNUC_PREREQ(maj, min, patch) 0
71 /// \macro LLVM_MSC_PREREQ
72 /// Is the compiler MSVC of at least the specified version?
73 /// The common \param version values to check for are:
74 /// * 1910: VS2017, version 15.1 & 15.2
75 /// * 1911: VS2017, version 15.3 & 15.4
76 /// * 1912: VS2017, version 15.5
77 /// * 1913: VS2017, version 15.6
78 /// * 1914: VS2017, version 15.7
79 /// * 1915: VS2017, version 15.8
80 /// * 1916: VS2017, version 15.9
81 /// * 1920: VS2019, version 16.0
82 /// * 1921: VS2019, version 16.1
84 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
86 // We require at least MSVC 2017.
87 #if !LLVM_MSC_PREREQ(1910)
88 #error LLVM requires at least MSVC 2017.
92 #define LLVM_MSC_PREREQ(version) 0
95 /// Does the compiler support ref-qualifiers for *this?
97 /// Sadly, this is separate from just rvalue reference support because GCC
98 /// and MSVC implemented this later than everything else.
99 #if __has_feature(cxx_rvalue_references) || LLVM_GNUC_PREREQ(4, 8, 1)
100 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
102 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
105 /// Expands to '&' if ref-qualifiers for *this are supported.
107 /// This can be used to provide lvalue/rvalue overrides of member functions.
108 /// The rvalue override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
109 #if LLVM_HAS_RVALUE_REFERENCE_THIS
110 #define LLVM_LVALUE_FUNCTION &
112 #define LLVM_LVALUE_FUNCTION
115 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
116 /// into a shared library, then the class should be private to the library and
117 /// not accessible from outside it. Can also be used to mark variables and
118 /// functions, making them private to any shared library they are linked into.
119 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
120 #if (__has_attribute(visibility) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
121 !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_WIN32)
122 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
124 #define LLVM_LIBRARY_VISIBILITY
127 #if defined(__GNUC__)
128 #define LLVM_PREFETCH(addr, rw, locality) __builtin_prefetch(addr, rw, locality)
130 #define LLVM_PREFETCH(addr, rw, locality)
133 #if __has_attribute(used) || LLVM_GNUC_PREREQ(3, 1, 0)
134 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
136 #define LLVM_ATTRIBUTE_USED
139 /// LLVM_NODISCARD - Warn if a type or return value is discarded.
141 // Use the 'nodiscard' attribute in C++17 or newer mode.
142 #if __cplusplus > 201402L && LLVM_HAS_CPP_ATTRIBUTE(nodiscard)
143 #define LLVM_NODISCARD [[nodiscard]]
144 #elif LLVM_HAS_CPP_ATTRIBUTE(clang::warn_unused_result)
145 #define LLVM_NODISCARD [[clang::warn_unused_result]]
146 // Clang in C++14 mode claims that it has the 'nodiscard' attribute, but also
147 // warns in the pedantic mode that 'nodiscard' is a C++17 extension (PR33518).
148 // Use the 'nodiscard' attribute in C++14 mode only with GCC.
149 // TODO: remove this workaround when PR33518 is resolved.
150 #elif defined(__GNUC__) && LLVM_HAS_CPP_ATTRIBUTE(nodiscard)
151 #define LLVM_NODISCARD [[nodiscard]]
153 #define LLVM_NODISCARD
156 // Indicate that a non-static, non-const C++ member function reinitializes
157 // the entire object to a known state, independent of the previous state of
160 // The clang-tidy check bugprone-use-after-move recognizes this attribute as a
161 // marker that a moved-from object has left the indeterminate state and can be
163 #if LLVM_HAS_CPP_ATTRIBUTE(clang::reinitializes)
164 #define LLVM_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
166 #define LLVM_ATTRIBUTE_REINITIALIZES
169 // Some compilers warn about unused functions. When a function is sometimes
170 // used or not depending on build settings (e.g. a function only called from
171 // within "assert"), this attribute can be used to suppress such warnings.
173 // However, it shouldn't be used for unused *variables*, as those have a much
174 // more portable solution:
175 // (void)unused_var_name;
176 // Prefer cast-to-void wherever it is sufficient.
177 #if __has_attribute(unused) || LLVM_GNUC_PREREQ(3, 1, 0)
178 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
180 #define LLVM_ATTRIBUTE_UNUSED
183 // FIXME: Provide this for PE/COFF targets.
184 #if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
185 (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_WIN32))
186 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
188 #define LLVM_ATTRIBUTE_WEAK
191 // Prior to clang 3.2, clang did not accept any spelling of
192 // __has_attribute(const), so assume it is supported.
193 #if defined(__clang__) || defined(__GNUC__)
194 // aka 'CONST' but following LLVM Conventions.
195 #define LLVM_READNONE __attribute__((__const__))
197 #define LLVM_READNONE
200 #if __has_attribute(pure) || defined(__GNUC__)
201 // aka 'PURE' but following LLVM Conventions.
202 #define LLVM_READONLY __attribute__((__pure__))
204 #define LLVM_READONLY
207 #if __has_builtin(__builtin_expect) || LLVM_GNUC_PREREQ(4, 0, 0)
208 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
209 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
211 #define LLVM_LIKELY(EXPR) (EXPR)
212 #define LLVM_UNLIKELY(EXPR) (EXPR)
215 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
216 /// mark a method "not for inlining".
217 #if __has_attribute(noinline) || LLVM_GNUC_PREREQ(3, 4, 0)
218 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
219 #elif defined(_MSC_VER)
220 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
222 #define LLVM_ATTRIBUTE_NOINLINE
225 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
226 /// so, mark a method "always inline" because it is performance sensitive. GCC
227 /// 3.4 supported this but is buggy in various cases and produces unimplemented
228 /// errors, just use it in GCC 4.0 and later.
229 #if __has_attribute(always_inline) || LLVM_GNUC_PREREQ(4, 0, 0)
230 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
231 #elif defined(_MSC_VER)
232 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
234 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
238 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
239 #elif defined(_MSC_VER)
240 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
242 #define LLVM_ATTRIBUTE_NORETURN
245 #if __has_attribute(returns_nonnull) || LLVM_GNUC_PREREQ(4, 9, 0)
246 #define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
247 #elif defined(_MSC_VER)
248 #define LLVM_ATTRIBUTE_RETURNS_NONNULL _Ret_notnull_
250 #define LLVM_ATTRIBUTE_RETURNS_NONNULL
253 /// \macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a
254 /// pointer that does not alias any other valid pointer.
256 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
257 #elif defined(_MSC_VER)
258 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
260 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS
263 /// LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
264 #if __cplusplus > 201402L && LLVM_HAS_CPP_ATTRIBUTE(fallthrough)
265 #define LLVM_FALLTHROUGH [[fallthrough]]
266 #elif LLVM_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
267 #define LLVM_FALLTHROUGH [[gnu::fallthrough]]
268 #elif __has_attribute(fallthrough)
269 #define LLVM_FALLTHROUGH __attribute__((fallthrough))
270 #elif LLVM_HAS_CPP_ATTRIBUTE(clang::fallthrough)
271 #define LLVM_FALLTHROUGH [[clang::fallthrough]]
273 #define LLVM_FALLTHROUGH
276 /// LLVM_REQUIRE_CONSTANT_INITIALIZATION - Apply this to globals to ensure that
277 /// they are constant initialized.
278 #if LLVM_HAS_CPP_ATTRIBUTE(clang::require_constant_initialization)
279 #define LLVM_REQUIRE_CONSTANT_INITIALIZATION \
280 [[clang::require_constant_initialization]]
282 #define LLVM_REQUIRE_CONSTANT_INITIALIZATION
285 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
286 /// pedantic diagnostics.
288 #define LLVM_EXTENSION __extension__
290 #define LLVM_EXTENSION
293 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
294 #if __has_feature(attribute_deprecated_with_message)
295 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
296 decl __attribute__((deprecated(message)))
297 #elif defined(__GNUC__)
298 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
299 decl __attribute__((deprecated))
300 #elif defined(_MSC_VER)
301 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
302 __declspec(deprecated(message)) decl
304 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
308 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
309 /// to an expression which states that it is undefined behavior for the
310 /// compiler to reach this point. Otherwise is not defined.
311 #if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0)
312 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
313 #elif defined(_MSC_VER)
314 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
317 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
318 /// which causes the program to exit abnormally.
319 #if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0)
320 # define LLVM_BUILTIN_TRAP __builtin_trap()
321 #elif defined(_MSC_VER)
322 // The __debugbreak intrinsic is supported by MSVC, does not require forward
323 // declarations involving platform-specific typedefs (unlike RaiseException),
324 // results in a call to vectored exception handlers, and encodes to a short
325 // instruction that still causes the trapping behavior we want.
326 # define LLVM_BUILTIN_TRAP __debugbreak()
328 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
331 /// LLVM_BUILTIN_DEBUGTRAP - On compilers which support it, expands to
332 /// an expression which causes the program to break while running
333 /// under a debugger.
334 #if __has_builtin(__builtin_debugtrap)
335 # define LLVM_BUILTIN_DEBUGTRAP __builtin_debugtrap()
336 #elif defined(_MSC_VER)
337 // The __debugbreak intrinsic is supported by MSVC and breaks while
338 // running under the debugger, and also supports invoking a debugger
339 // when the OS is configured appropriately.
340 # define LLVM_BUILTIN_DEBUGTRAP __debugbreak()
342 // Just continue execution when built with compilers that have no
343 // support. This is a debugging aid and not intended to force the
344 // program to abort if encountered.
345 # define LLVM_BUILTIN_DEBUGTRAP
348 /// \macro LLVM_ASSUME_ALIGNED
349 /// Returns a pointer with an assumed alignment.
350 #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
351 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
352 #elif defined(LLVM_BUILTIN_UNREACHABLE)
353 // As of today, clang does not support __builtin_assume_aligned.
354 # define LLVM_ASSUME_ALIGNED(p, a) \
355 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
357 # define LLVM_ASSUME_ALIGNED(p, a) (p)
360 /// \macro LLVM_PACKED
361 /// Used to specify a packed structure.
370 /// LLVM_PACKED_START
379 # define LLVM_PACKED(d) __pragma(pack(push, 1)) d __pragma(pack(pop))
380 # define LLVM_PACKED_START __pragma(pack(push, 1))
381 # define LLVM_PACKED_END __pragma(pack(pop))
383 # define LLVM_PACKED(d) d __attribute__((packed))
384 # define LLVM_PACKED_START _Pragma("pack(push, 1)")
385 # define LLVM_PACKED_END _Pragma("pack(pop)")
388 /// \macro LLVM_PTR_SIZE
389 /// A constant integer equivalent to the value of sizeof(void*).
390 /// Generally used in combination with alignas or when doing computation in the
392 #ifdef __SIZEOF_POINTER__
393 # define LLVM_PTR_SIZE __SIZEOF_POINTER__
394 #elif defined(_WIN64)
395 # define LLVM_PTR_SIZE 8
396 #elif defined(_WIN32)
397 # define LLVM_PTR_SIZE 4
398 #elif defined(_MSC_VER)
399 # error "could not determine LLVM_PTR_SIZE as a constant int for MSVC"
401 # define LLVM_PTR_SIZE sizeof(void *)
404 /// \macro LLVM_MEMORY_SANITIZER_BUILD
405 /// Whether LLVM itself is built with MemorySanitizer instrumentation.
406 #if __has_feature(memory_sanitizer)
407 # define LLVM_MEMORY_SANITIZER_BUILD 1
408 # include <sanitizer/msan_interface.h>
410 # define LLVM_MEMORY_SANITIZER_BUILD 0
411 # define __msan_allocated_memory(p, size)
412 # define __msan_unpoison(p, size)
415 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
416 /// Whether LLVM itself is built with AddressSanitizer instrumentation.
417 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
418 # define LLVM_ADDRESS_SANITIZER_BUILD 1
419 # include <sanitizer/asan_interface.h>
421 # define LLVM_ADDRESS_SANITIZER_BUILD 0
422 # define __asan_poison_memory_region(p, size)
423 # define __asan_unpoison_memory_region(p, size)
426 /// \macro LLVM_THREAD_SANITIZER_BUILD
427 /// Whether LLVM itself is built with ThreadSanitizer instrumentation.
428 #if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
429 # define LLVM_THREAD_SANITIZER_BUILD 1
431 # define LLVM_THREAD_SANITIZER_BUILD 0
434 #if LLVM_THREAD_SANITIZER_BUILD
435 // Thread Sanitizer is a tool that finds races in code.
436 // See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
437 // tsan detects these exact functions by name.
441 void AnnotateHappensAfter(const char *file
, int line
, const volatile void *cv
);
442 void AnnotateHappensBefore(const char *file
, int line
, const volatile void *cv
);
443 void AnnotateIgnoreWritesBegin(const char *file
, int line
);
444 void AnnotateIgnoreWritesEnd(const char *file
, int line
);
449 // This marker is used to define a happens-before arc. The race detector will
450 // infer an arc from the begin to the end when they share the same pointer
452 # define TsanHappensBefore(cv) AnnotateHappensBefore(__FILE__, __LINE__, cv)
454 // This marker defines the destination of a happens-before arc.
455 # define TsanHappensAfter(cv) AnnotateHappensAfter(__FILE__, __LINE__, cv)
457 // Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
458 # define TsanIgnoreWritesBegin() AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
460 // Resume checking for racy writes.
461 # define TsanIgnoreWritesEnd() AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
463 # define TsanHappensBefore(cv)
464 # define TsanHappensAfter(cv)
465 # define TsanIgnoreWritesBegin()
466 # define TsanIgnoreWritesEnd()
469 /// \macro LLVM_NO_SANITIZE
470 /// Disable a particular sanitizer for a function.
471 #if __has_attribute(no_sanitize)
472 #define LLVM_NO_SANITIZE(KIND) __attribute__((no_sanitize(KIND)))
474 #define LLVM_NO_SANITIZE(KIND)
477 /// Mark debug helper function definitions like dump() that should not be
478 /// stripped from debug builds.
479 /// Note that you should also surround dump() functions with
480 /// `#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)` so they do always
481 /// get stripped in release builds.
482 // FIXME: Move this to a private config.h as it's not usable in public headers.
483 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
484 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
486 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
489 /// \macro LLVM_PRETTY_FUNCTION
490 /// Gets a user-friendly looking function signature for the current scope
491 /// using the best available method on each platform. The exact format of the
492 /// resulting string is implementation specific and non-portable, so this should
493 /// only be used, for example, for logging or diagnostics.
494 #if defined(_MSC_VER)
495 #define LLVM_PRETTY_FUNCTION __FUNCSIG__
496 #elif defined(__GNUC__) || defined(__clang__)
497 #define LLVM_PRETTY_FUNCTION __PRETTY_FUNCTION__
499 #define LLVM_PRETTY_FUNCTION __func__
502 /// \macro LLVM_THREAD_LOCAL
503 /// A thread-local storage specifier which can be used with globals,
504 /// extern globals, and static globals.
506 /// This is essentially an extremely restricted analog to C++11's thread_local
507 /// support, and uses that when available. However, it falls back on
508 /// platform-specific or vendor-provided extensions when necessary. These
509 /// extensions don't support many of the C++11 thread_local's features. You
510 /// should only use this for PODs that you can statically initialize to
511 /// some constant value. In almost all circumstances this is most appropriate
512 /// for use with a pointer, integer, or small aggregation of pointers and
514 #if LLVM_ENABLE_THREADS
515 #if __has_feature(cxx_thread_local)
516 #define LLVM_THREAD_LOCAL thread_local
517 #elif defined(_MSC_VER)
518 // MSVC supports this with a __declspec.
519 #define LLVM_THREAD_LOCAL __declspec(thread)
521 // Clang, GCC, and other compatible compilers used __thread prior to C++11 and
522 // we only need the restricted functionality that provides.
523 #define LLVM_THREAD_LOCAL __thread
525 #else // !LLVM_ENABLE_THREADS
526 // If threading is disabled entirely, this compiles to nothing and you get
527 // a normal global variable.
528 #define LLVM_THREAD_LOCAL
531 /// \macro LLVM_ENABLE_EXCEPTIONS
532 /// Whether LLVM is built with exception support.
533 #if __has_feature(cxx_exceptions)
534 #define LLVM_ENABLE_EXCEPTIONS 1
535 #elif defined(__GNUC__) && defined(__EXCEPTIONS)
536 #define LLVM_ENABLE_EXCEPTIONS 1
537 #elif defined(_MSC_VER) && defined(_CPPUNWIND)
538 #define LLVM_ENABLE_EXCEPTIONS 1
544 /// Allocate a buffer of memory with the given size and alignment.
546 /// When the compiler supports aligned operator new, this will use it to to
547 /// handle even over-aligned allocations.
549 /// However, this doesn't make any attempt to leverage the fancier techniques
550 /// like posix_memalign due to portability. It is mostly intended to allow
551 /// compatibility with platforms that, after aligned allocation was added, use
552 /// reduced default alignment.
553 inline void *allocate_buffer(size_t Size
, size_t Alignment
) {
554 return ::operator new(Size
555 #ifdef __cpp_aligned_new
557 std::align_val_t(Alignment
)
562 /// Deallocate a buffer of memory with the given size and alignment.
564 /// If supported, this will used the sized delete operator. Also if supported,
565 /// this will pass the alignment to the delete operator.
567 /// The pointer must have been allocated with the corresponding new operator,
568 /// most likely using the above helper.
569 inline void deallocate_buffer(void *Ptr
, size_t Size
, size_t Alignment
) {
570 ::operator delete(Ptr
571 #ifdef __cpp_sized_deallocation
575 #ifdef __cpp_aligned_new
577 std::align_val_t(Alignment
)
582 } // End namespace llvm
584 #endif // __cplusplus