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.
140 #if __cplusplus > 201402L && LLVM_HAS_CPP_ATTRIBUTE(nodiscard)
141 #define LLVM_NODISCARD [[nodiscard]]
142 #elif LLVM_HAS_CPP_ATTRIBUTE(clang::warn_unused_result)
143 #define LLVM_NODISCARD [[clang::warn_unused_result]]
145 #define LLVM_NODISCARD
148 // Indicate that a non-static, non-const C++ member function reinitializes
149 // the entire object to a known state, independent of the previous state of
152 // The clang-tidy check bugprone-use-after-move recognizes this attribute as a
153 // marker that a moved-from object has left the indeterminate state and can be
155 #if LLVM_HAS_CPP_ATTRIBUTE(clang::reinitializes)
156 #define LLVM_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
158 #define LLVM_ATTRIBUTE_REINITIALIZES
161 // Some compilers warn about unused functions. When a function is sometimes
162 // used or not depending on build settings (e.g. a function only called from
163 // within "assert"), this attribute can be used to suppress such warnings.
165 // However, it shouldn't be used for unused *variables*, as those have a much
166 // more portable solution:
167 // (void)unused_var_name;
168 // Prefer cast-to-void wherever it is sufficient.
169 #if __has_attribute(unused) || LLVM_GNUC_PREREQ(3, 1, 0)
170 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
172 #define LLVM_ATTRIBUTE_UNUSED
175 // FIXME: Provide this for PE/COFF targets.
176 #if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
177 (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_WIN32))
178 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
180 #define LLVM_ATTRIBUTE_WEAK
183 // Prior to clang 3.2, clang did not accept any spelling of
184 // __has_attribute(const), so assume it is supported.
185 #if defined(__clang__) || defined(__GNUC__)
186 // aka 'CONST' but following LLVM Conventions.
187 #define LLVM_READNONE __attribute__((__const__))
189 #define LLVM_READNONE
192 #if __has_attribute(pure) || defined(__GNUC__)
193 // aka 'PURE' but following LLVM Conventions.
194 #define LLVM_READONLY __attribute__((__pure__))
196 #define LLVM_READONLY
199 #if __has_builtin(__builtin_expect) || LLVM_GNUC_PREREQ(4, 0, 0)
200 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
201 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
203 #define LLVM_LIKELY(EXPR) (EXPR)
204 #define LLVM_UNLIKELY(EXPR) (EXPR)
207 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
208 /// mark a method "not for inlining".
209 #if __has_attribute(noinline) || LLVM_GNUC_PREREQ(3, 4, 0)
210 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
211 #elif defined(_MSC_VER)
212 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
214 #define LLVM_ATTRIBUTE_NOINLINE
217 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
218 /// so, mark a method "always inline" because it is performance sensitive. GCC
219 /// 3.4 supported this but is buggy in various cases and produces unimplemented
220 /// errors, just use it in GCC 4.0 and later.
221 #if __has_attribute(always_inline) || LLVM_GNUC_PREREQ(4, 0, 0)
222 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
223 #elif defined(_MSC_VER)
224 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
226 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
230 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
231 #elif defined(_MSC_VER)
232 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
234 #define LLVM_ATTRIBUTE_NORETURN
237 #if __has_attribute(returns_nonnull) || LLVM_GNUC_PREREQ(4, 9, 0)
238 #define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
239 #elif defined(_MSC_VER)
240 #define LLVM_ATTRIBUTE_RETURNS_NONNULL _Ret_notnull_
242 #define LLVM_ATTRIBUTE_RETURNS_NONNULL
245 /// \macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a
246 /// pointer that does not alias any other valid pointer.
248 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
249 #elif defined(_MSC_VER)
250 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
252 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS
255 /// LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
256 #if __cplusplus > 201402L && LLVM_HAS_CPP_ATTRIBUTE(fallthrough)
257 #define LLVM_FALLTHROUGH [[fallthrough]]
258 #elif LLVM_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
259 #define LLVM_FALLTHROUGH [[gnu::fallthrough]]
260 #elif __has_attribute(fallthrough)
261 #define LLVM_FALLTHROUGH __attribute__((fallthrough))
262 #elif LLVM_HAS_CPP_ATTRIBUTE(clang::fallthrough)
263 #define LLVM_FALLTHROUGH [[clang::fallthrough]]
265 #define LLVM_FALLTHROUGH
268 /// LLVM_REQUIRE_CONSTANT_INITIALIZATION - Apply this to globals to ensure that
269 /// they are constant initialized.
270 #if LLVM_HAS_CPP_ATTRIBUTE(clang::require_constant_initialization)
271 #define LLVM_REQUIRE_CONSTANT_INITIALIZATION \
272 [[clang::require_constant_initialization]]
274 #define LLVM_REQUIRE_CONSTANT_INITIALIZATION
277 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
278 /// pedantic diagnostics.
280 #define LLVM_EXTENSION __extension__
282 #define LLVM_EXTENSION
285 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
286 #if __has_feature(attribute_deprecated_with_message)
287 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
288 decl __attribute__((deprecated(message)))
289 #elif defined(__GNUC__)
290 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
291 decl __attribute__((deprecated))
292 #elif defined(_MSC_VER)
293 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
294 __declspec(deprecated(message)) decl
296 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
300 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
301 /// to an expression which states that it is undefined behavior for the
302 /// compiler to reach this point. Otherwise is not defined.
303 #if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0)
304 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
305 #elif defined(_MSC_VER)
306 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
309 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
310 /// which causes the program to exit abnormally.
311 #if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0)
312 # define LLVM_BUILTIN_TRAP __builtin_trap()
313 #elif defined(_MSC_VER)
314 // The __debugbreak intrinsic is supported by MSVC, does not require forward
315 // declarations involving platform-specific typedefs (unlike RaiseException),
316 // results in a call to vectored exception handlers, and encodes to a short
317 // instruction that still causes the trapping behavior we want.
318 # define LLVM_BUILTIN_TRAP __debugbreak()
320 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
323 /// LLVM_BUILTIN_DEBUGTRAP - On compilers which support it, expands to
324 /// an expression which causes the program to break while running
325 /// under a debugger.
326 #if __has_builtin(__builtin_debugtrap)
327 # define LLVM_BUILTIN_DEBUGTRAP __builtin_debugtrap()
328 #elif defined(_MSC_VER)
329 // The __debugbreak intrinsic is supported by MSVC and breaks while
330 // running under the debugger, and also supports invoking a debugger
331 // when the OS is configured appropriately.
332 # define LLVM_BUILTIN_DEBUGTRAP __debugbreak()
334 // Just continue execution when built with compilers that have no
335 // support. This is a debugging aid and not intended to force the
336 // program to abort if encountered.
337 # define LLVM_BUILTIN_DEBUGTRAP
340 /// \macro LLVM_ASSUME_ALIGNED
341 /// Returns a pointer with an assumed alignment.
342 #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
343 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
344 #elif defined(LLVM_BUILTIN_UNREACHABLE)
345 // As of today, clang does not support __builtin_assume_aligned.
346 # define LLVM_ASSUME_ALIGNED(p, a) \
347 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
349 # define LLVM_ASSUME_ALIGNED(p, a) (p)
352 /// \macro LLVM_PACKED
353 /// Used to specify a packed structure.
362 /// LLVM_PACKED_START
371 # define LLVM_PACKED(d) __pragma(pack(push, 1)) d __pragma(pack(pop))
372 # define LLVM_PACKED_START __pragma(pack(push, 1))
373 # define LLVM_PACKED_END __pragma(pack(pop))
375 # define LLVM_PACKED(d) d __attribute__((packed))
376 # define LLVM_PACKED_START _Pragma("pack(push, 1)")
377 # define LLVM_PACKED_END _Pragma("pack(pop)")
380 /// \macro LLVM_PTR_SIZE
381 /// A constant integer equivalent to the value of sizeof(void*).
382 /// Generally used in combination with alignas or when doing computation in the
384 #ifdef __SIZEOF_POINTER__
385 # define LLVM_PTR_SIZE __SIZEOF_POINTER__
386 #elif defined(_WIN64)
387 # define LLVM_PTR_SIZE 8
388 #elif defined(_WIN32)
389 # define LLVM_PTR_SIZE 4
390 #elif defined(_MSC_VER)
391 # error "could not determine LLVM_PTR_SIZE as a constant int for MSVC"
393 # define LLVM_PTR_SIZE sizeof(void *)
396 /// \macro LLVM_MEMORY_SANITIZER_BUILD
397 /// Whether LLVM itself is built with MemorySanitizer instrumentation.
398 #if __has_feature(memory_sanitizer)
399 # define LLVM_MEMORY_SANITIZER_BUILD 1
400 # include <sanitizer/msan_interface.h>
402 # define LLVM_MEMORY_SANITIZER_BUILD 0
403 # define __msan_allocated_memory(p, size)
404 # define __msan_unpoison(p, size)
407 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
408 /// Whether LLVM itself is built with AddressSanitizer instrumentation.
409 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
410 # define LLVM_ADDRESS_SANITIZER_BUILD 1
411 # include <sanitizer/asan_interface.h>
413 # define LLVM_ADDRESS_SANITIZER_BUILD 0
414 # define __asan_poison_memory_region(p, size)
415 # define __asan_unpoison_memory_region(p, size)
418 /// \macro LLVM_THREAD_SANITIZER_BUILD
419 /// Whether LLVM itself is built with ThreadSanitizer instrumentation.
420 #if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
421 # define LLVM_THREAD_SANITIZER_BUILD 1
423 # define LLVM_THREAD_SANITIZER_BUILD 0
426 #if LLVM_THREAD_SANITIZER_BUILD
427 // Thread Sanitizer is a tool that finds races in code.
428 // See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
429 // tsan detects these exact functions by name.
433 void AnnotateHappensAfter(const char *file
, int line
, const volatile void *cv
);
434 void AnnotateHappensBefore(const char *file
, int line
, const volatile void *cv
);
435 void AnnotateIgnoreWritesBegin(const char *file
, int line
);
436 void AnnotateIgnoreWritesEnd(const char *file
, int line
);
441 // This marker is used to define a happens-before arc. The race detector will
442 // infer an arc from the begin to the end when they share the same pointer
444 # define TsanHappensBefore(cv) AnnotateHappensBefore(__FILE__, __LINE__, cv)
446 // This marker defines the destination of a happens-before arc.
447 # define TsanHappensAfter(cv) AnnotateHappensAfter(__FILE__, __LINE__, cv)
449 // Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
450 # define TsanIgnoreWritesBegin() AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
452 // Resume checking for racy writes.
453 # define TsanIgnoreWritesEnd() AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
455 # define TsanHappensBefore(cv)
456 # define TsanHappensAfter(cv)
457 # define TsanIgnoreWritesBegin()
458 # define TsanIgnoreWritesEnd()
461 /// \macro LLVM_NO_SANITIZE
462 /// Disable a particular sanitizer for a function.
463 #if __has_attribute(no_sanitize)
464 #define LLVM_NO_SANITIZE(KIND) __attribute__((no_sanitize(KIND)))
466 #define LLVM_NO_SANITIZE(KIND)
469 /// Mark debug helper function definitions like dump() that should not be
470 /// stripped from debug builds.
471 /// Note that you should also surround dump() functions with
472 /// `#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)` so they do always
473 /// get stripped in release builds.
474 // FIXME: Move this to a private config.h as it's not usable in public headers.
475 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
476 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
478 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
481 /// \macro LLVM_PRETTY_FUNCTION
482 /// Gets a user-friendly looking function signature for the current scope
483 /// using the best available method on each platform. The exact format of the
484 /// resulting string is implementation specific and non-portable, so this should
485 /// only be used, for example, for logging or diagnostics.
486 #if defined(_MSC_VER)
487 #define LLVM_PRETTY_FUNCTION __FUNCSIG__
488 #elif defined(__GNUC__) || defined(__clang__)
489 #define LLVM_PRETTY_FUNCTION __PRETTY_FUNCTION__
491 #define LLVM_PRETTY_FUNCTION __func__
494 /// \macro LLVM_THREAD_LOCAL
495 /// A thread-local storage specifier which can be used with globals,
496 /// extern globals, and static globals.
498 /// This is essentially an extremely restricted analog to C++11's thread_local
499 /// support, and uses that when available. However, it falls back on
500 /// platform-specific or vendor-provided extensions when necessary. These
501 /// extensions don't support many of the C++11 thread_local's features. You
502 /// should only use this for PODs that you can statically initialize to
503 /// some constant value. In almost all circumstances this is most appropriate
504 /// for use with a pointer, integer, or small aggregation of pointers and
506 #if LLVM_ENABLE_THREADS
507 #if __has_feature(cxx_thread_local)
508 #define LLVM_THREAD_LOCAL thread_local
509 #elif defined(_MSC_VER)
510 // MSVC supports this with a __declspec.
511 #define LLVM_THREAD_LOCAL __declspec(thread)
513 // Clang, GCC, and other compatible compilers used __thread prior to C++11 and
514 // we only need the restricted functionality that provides.
515 #define LLVM_THREAD_LOCAL __thread
517 #else // !LLVM_ENABLE_THREADS
518 // If threading is disabled entirely, this compiles to nothing and you get
519 // a normal global variable.
520 #define LLVM_THREAD_LOCAL
523 /// \macro LLVM_ENABLE_EXCEPTIONS
524 /// Whether LLVM is built with exception support.
525 #if __has_feature(cxx_exceptions)
526 #define LLVM_ENABLE_EXCEPTIONS 1
527 #elif defined(__GNUC__) && defined(__EXCEPTIONS)
528 #define LLVM_ENABLE_EXCEPTIONS 1
529 #elif defined(_MSC_VER) && defined(_CPPUNWIND)
530 #define LLVM_ENABLE_EXCEPTIONS 1
536 /// Allocate a buffer of memory with the given size and alignment.
538 /// When the compiler supports aligned operator new, this will use it to to
539 /// handle even over-aligned allocations.
541 /// However, this doesn't make any attempt to leverage the fancier techniques
542 /// like posix_memalign due to portability. It is mostly intended to allow
543 /// compatibility with platforms that, after aligned allocation was added, use
544 /// reduced default alignment.
545 inline void *allocate_buffer(size_t Size
, size_t Alignment
) {
546 return ::operator new(Size
547 #ifdef __cpp_aligned_new
549 std::align_val_t(Alignment
)
554 /// Deallocate a buffer of memory with the given size and alignment.
556 /// If supported, this will used the sized delete operator. Also if supported,
557 /// this will pass the alignment to the delete operator.
559 /// The pointer must have been allocated with the corresponding new operator,
560 /// most likely using the above helper.
561 inline void deallocate_buffer(void *Ptr
, size_t Size
, size_t Alignment
) {
562 ::operator delete(Ptr
563 #ifdef __cpp_sized_deallocation
567 #ifdef __cpp_aligned_new
569 std::align_val_t(Alignment
)
574 } // End namespace llvm
576 #endif // __cplusplus