1//===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines several macros, based on the current compiler. This allows
11// use of compiler-specific features in a way that remains portable.
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 <new>
21#include <stddef.h>
22
23#if defined(_MSC_VER)
24#include <sal.h>
25#endif
26
27#ifndef __has_feature
28# define __has_feature(x) 0
29#endif
30
31#ifndef __has_extension
32# define __has_extension(x) 0
33#endif
34
35#ifndef __has_attribute
36# define __has_attribute(x) 0
37#endif
38
39#ifndef __has_cpp_attribute
40# define __has_cpp_attribute(x) 0
41#endif
42
43#ifndef __has_builtin
44# define __has_builtin(x) 0
45#endif
46
47/// \macro LLVM_GNUC_PREREQ
48/// Extend the default __GNUC_PREREQ even if glibc's features.h isn't
49/// available.
50#ifndef LLVM_GNUC_PREREQ
51# if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
52# define LLVM_GNUC_PREREQ(maj, min, patch) \
53 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
54 ((maj) << 20) + ((min) << 10) + (patch))
55# elif defined(__GNUC__) && defined(__GNUC_MINOR__)
56# define LLVM_GNUC_PREREQ(maj, min, patch) \
57 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
58# else
59# define LLVM_GNUC_PREREQ(maj, min, patch) 0
60# endif
61#endif
62
63/// \macro LLVM_MSC_PREREQ
64/// Is the compiler MSVC of at least the specified version?
65/// The common \param version values to check for are:
66/// * 1900: Microsoft Visual Studio 2015 / 14.0
67#ifdef _MSC_VER
68#define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
69
70// We require at least MSVC 2015.
71#if !LLVM_MSC_PREREQ(1900)
72#error LLVM requires at least MSVC 2015.
73#endif
74
75#else
76#define LLVM_MSC_PREREQ(version) 0
77#endif
78
79/// Does the compiler support ref-qualifiers for *this?
80///
81/// Sadly, this is separate from just rvalue reference support because GCC
82/// and MSVC implemented this later than everything else.
83#if __has_feature(cxx_rvalue_references) || LLVM_GNUC_PREREQ(4, 8, 1)
84#define LLVM_HAS_RVALUE_REFERENCE_THIS 1
85#else
86#define LLVM_HAS_RVALUE_REFERENCE_THIS 0
87#endif
88
89/// Expands to '&' if ref-qualifiers for *this are supported.
90///
91/// This can be used to provide lvalue/rvalue overrides of member functions.
92/// The rvalue override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
93#if LLVM_HAS_RVALUE_REFERENCE_THIS
94#define LLVM_LVALUE_FUNCTION &
95#else
96#define LLVM_LVALUE_FUNCTION
97#endif
98
99/// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
100/// into a shared library, then the class should be private to the library and
101/// not accessible from outside it. Can also be used to mark variables and
102/// functions, making them private to any shared library they are linked into.
103/// On PE/COFF targets, library visibility is the default, so this isn't needed.
104#if (__has_attribute(visibility) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
105 !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_WIN32)
106#define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
107#else
108#define LLVM_LIBRARY_VISIBILITY
109#endif
110
111#if defined(__GNUC__)
112#define LLVM_PREFETCH(addr, rw, locality) __builtin_prefetch(addr, rw, locality)
113#else
114#define LLVM_PREFETCH(addr, rw, locality)
115#endif
116
117#if __has_attribute(used) || LLVM_GNUC_PREREQ(3, 1, 0)
118#define LLVM_ATTRIBUTE_USED __attribute__((__used__))
119#else
120#define LLVM_ATTRIBUTE_USED
121#endif
122
123/// LLVM_NODISCARD - Warn if a type or return value is discarded.
124#if __cplusplus > 201402L && __has_cpp_attribute(nodiscard)
125#define LLVM_NODISCARD [[nodiscard]]
126#elif !__cplusplus
127// Workaround for llvm.org/PR23435, since clang 3.6 and below emit a spurious
128// error when __has_cpp_attribute is given a scoped attribute in C mode.
129#define LLVM_NODISCARD
130#elif __has_cpp_attribute(clang::warn_unused_result)
131#define LLVM_NODISCARD [[clang::warn_unused_result]]
132#else
133#define LLVM_NODISCARD
134#endif
135
136// Indicate that a non-static, non-const C++ member function reinitializes
137// the entire object to a known state, independent of the previous state of
138// the object.
139//
140// The clang-tidy check bugprone-use-after-move recognizes this attribute as a
141// marker that a moved-from object has left the indeterminate state and can be
142// reused.
143#if __has_cpp_attribute(clang::reinitializes)
144#define LLVM_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
145#else
146#define LLVM_ATTRIBUTE_REINITIALIZES
147#endif
148
149// Some compilers warn about unused functions. When a function is sometimes
150// used or not depending on build settings (e.g. a function only called from
151// within "assert"), this attribute can be used to suppress such warnings.
152//
153// However, it shouldn't be used for unused *variables*, as those have a much
154// more portable solution:
155// (void)unused_var_name;
156// Prefer cast-to-void wherever it is sufficient.
157#if __has_attribute(unused) || LLVM_GNUC_PREREQ(3, 1, 0)
158#define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
159#else
160#define LLVM_ATTRIBUTE_UNUSED
161#endif
162
163// FIXME: Provide this for PE/COFF targets.
164#if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
165 (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_WIN32))
166#define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
167#else
168#define LLVM_ATTRIBUTE_WEAK
169#endif
170
171// Prior to clang 3.2, clang did not accept any spelling of
172// __has_attribute(const), so assume it is supported.
173#if defined(__clang__) || defined(__GNUC__)
174// aka 'CONST' but following LLVM Conventions.
175#define LLVM_READNONE __attribute__((__const__))
176#else
177#define LLVM_READNONE
178#endif
179
180#if __has_attribute(pure) || defined(__GNUC__)
181// aka 'PURE' but following LLVM Conventions.
182#define LLVM_READONLY __attribute__((__pure__))
183#else
184#define LLVM_READONLY
185#endif
186
187#if __has_builtin(__builtin_expect) || LLVM_GNUC_PREREQ(4, 0, 0)
188#define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
189#define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
190#else
191#define LLVM_LIKELY(EXPR) (EXPR)
192#define LLVM_UNLIKELY(EXPR) (EXPR)
193#endif
194
195/// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
196/// mark a method "not for inlining".
197#if __has_attribute(noinline) || LLVM_GNUC_PREREQ(3, 4, 0)
198#define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
199#elif defined(_MSC_VER)
200#define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
201#else
202#define LLVM_ATTRIBUTE_NOINLINE
203#endif
204
205/// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
206/// so, mark a method "always inline" because it is performance sensitive. GCC
207/// 3.4 supported this but is buggy in various cases and produces unimplemented
208/// errors, just use it in GCC 4.0 and later.
209#if __has_attribute(always_inline) || LLVM_GNUC_PREREQ(4, 0, 0)
210#define LLVM_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
211#elif defined(_MSC_VER)
212#define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
213#else
214#define LLVM_ATTRIBUTE_ALWAYS_INLINE
215#endif
216
217#ifdef __GNUC__
218#define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
219#elif defined(_MSC_VER)
220#define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
221#else
222#define LLVM_ATTRIBUTE_NORETURN
223#endif
224
225#if __has_attribute(returns_nonnull) || LLVM_GNUC_PREREQ(4, 9, 0)
226#define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
227#elif defined(_MSC_VER)
228#define LLVM_ATTRIBUTE_RETURNS_NONNULL _Ret_notnull_
229#else
230#define LLVM_ATTRIBUTE_RETURNS_NONNULL
231#endif
232
233/// \macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a
234/// pointer that does not alias any other valid pointer.
235#ifdef __GNUC__
236#define LLVM_ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
237#elif defined(_MSC_VER)
238#define LLVM_ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
239#else
240#define LLVM_ATTRIBUTE_RETURNS_NOALIAS
241#endif
242
243/// LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
244#if __cplusplus > 201402L && __has_cpp_attribute(fallthrough)
245#define LLVM_FALLTHROUGH [[fallthrough]]
246#elif __has_cpp_attribute(gnu::fallthrough)
247#define LLVM_FALLTHROUGH [[gnu::fallthrough]]
248#elif !__cplusplus
249// Workaround for llvm.org/PR23435, since clang 3.6 and below emit a spurious
250// error when __has_cpp_attribute is given a scoped attribute in C mode.
251#define LLVM_FALLTHROUGH
252#elif __has_cpp_attribute(clang::fallthrough)
253#define LLVM_FALLTHROUGH [[clang::fallthrough]]
254#else
255#define LLVM_FALLTHROUGH
256#endif
257
258/// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
259/// pedantic diagnostics.
260#ifdef __GNUC__
261#define LLVM_EXTENSION __extension__
262#else
263#define LLVM_EXTENSION
264#endif
265
266// LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
267#if __has_feature(attribute_deprecated_with_message)
268# define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
269 decl __attribute__((deprecated(message)))
270#elif defined(__GNUC__)
271# define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
272 decl __attribute__((deprecated))
273#elif defined(_MSC_VER)
274# define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
275 __declspec(deprecated(message)) decl
276#else
277# define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
278 decl
279#endif
280
281/// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
282/// to an expression which states that it is undefined behavior for the
283/// compiler to reach this point. Otherwise is not defined.
284#if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0)
285# define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
286#elif defined(_MSC_VER)
287# define LLVM_BUILTIN_UNREACHABLE __assume(false)
288#endif
289
290/// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
291/// which causes the program to exit abnormally.
292#if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0)
293# define LLVM_BUILTIN_TRAP __builtin_trap()
294#elif defined(_MSC_VER)
295// The __debugbreak intrinsic is supported by MSVC, does not require forward
296// declarations involving platform-specific typedefs (unlike RaiseException),
297// results in a call to vectored exception handlers, and encodes to a short
298// instruction that still causes the trapping behavior we want.
299# define LLVM_BUILTIN_TRAP __debugbreak()
300#else
301# define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
302#endif
303
304/// LLVM_BUILTIN_DEBUGTRAP - On compilers which support it, expands to
305/// an expression which causes the program to break while running
306/// under a debugger.
307#if __has_builtin(__builtin_debugtrap)
308# define LLVM_BUILTIN_DEBUGTRAP __builtin_debugtrap()
309#elif defined(_MSC_VER)
310// The __debugbreak intrinsic is supported by MSVC and breaks while
311// running under the debugger, and also supports invoking a debugger
312// when the OS is configured appropriately.
313# define LLVM_BUILTIN_DEBUGTRAP __debugbreak()
314#else
315// Just continue execution when built with compilers that have no
316// support. This is a debugging aid and not intended to force the
317// program to abort if encountered.
318# define LLVM_BUILTIN_DEBUGTRAP
319#endif
320
321/// \macro LLVM_ASSUME_ALIGNED
322/// Returns a pointer with an assumed alignment.
323#if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
324# define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
325#elif defined(LLVM_BUILTIN_UNREACHABLE)
326// As of today, clang does not support __builtin_assume_aligned.
327# define LLVM_ASSUME_ALIGNED(p, a) \
328 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
329#else
330# define LLVM_ASSUME_ALIGNED(p, a) (p)
331#endif
332
333/// \macro LLVM_ALIGNAS
334/// Used to specify a minimum alignment for a structure or variable.
335#if __GNUC__ && !__has_feature(cxx_alignas) && !LLVM_GNUC_PREREQ(4, 8, 1)
336# define LLVM_ALIGNAS(x) __attribute__((aligned(x)))
337#else
338# define LLVM_ALIGNAS(x) alignas(x)
339#endif
340
341/// \macro LLVM_PACKED
342/// Used to specify a packed structure.
343/// LLVM_PACKED(
344/// struct A {
345/// int i;
346/// int j;
347/// int k;
348/// long long l;
349/// });
350///
351/// LLVM_PACKED_START
352/// struct B {
353/// int i;
354/// int j;
355/// int k;
356/// long long l;
357/// };
358/// LLVM_PACKED_END
359#ifdef _MSC_VER
360# define LLVM_PACKED(d) __pragma(pack(push, 1)) d __pragma(pack(pop))
361# define LLVM_PACKED_START __pragma(pack(push, 1))
362# define LLVM_PACKED_END __pragma(pack(pop))
363#else
364# define LLVM_PACKED(d) d __attribute__((packed))
365# define LLVM_PACKED_START _Pragma("pack(push, 1)")
366# define LLVM_PACKED_END _Pragma("pack(pop)")
367#endif
368
369/// \macro LLVM_PTR_SIZE
370/// A constant integer equivalent to the value of sizeof(void*).
371/// Generally used in combination with LLVM_ALIGNAS or when doing computation in
372/// the preprocessor.
373#ifdef __SIZEOF_POINTER__
374# define LLVM_PTR_SIZE __SIZEOF_POINTER__
375#elif defined(_WIN64)
376# define LLVM_PTR_SIZE 8
377#elif defined(_WIN32)
378# define LLVM_PTR_SIZE 4
379#elif defined(_MSC_VER)
380# error "could not determine LLVM_PTR_SIZE as a constant int for MSVC"
381#else
382# define LLVM_PTR_SIZE sizeof(void *)
383#endif
384
385/// \macro LLVM_MEMORY_SANITIZER_BUILD
386/// Whether LLVM itself is built with MemorySanitizer instrumentation.
387#if __has_feature(memory_sanitizer)
388# define LLVM_MEMORY_SANITIZER_BUILD 1
389# include <sanitizer/msan_interface.h>
390#else
391# define LLVM_MEMORY_SANITIZER_BUILD 0
392# define __msan_allocated_memory(p, size)
393# define __msan_unpoison(p, size)
394#endif
395
396/// \macro LLVM_ADDRESS_SANITIZER_BUILD
397/// Whether LLVM itself is built with AddressSanitizer instrumentation.
398#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
399# define LLVM_ADDRESS_SANITIZER_BUILD 1
400# include <sanitizer/asan_interface.h>
401#else
402# define LLVM_ADDRESS_SANITIZER_BUILD 0
403# define __asan_poison_memory_region(p, size)
404# define __asan_unpoison_memory_region(p, size)
405#endif
406
407/// \macro LLVM_THREAD_SANITIZER_BUILD
408/// Whether LLVM itself is built with ThreadSanitizer instrumentation.
409#if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
410# define LLVM_THREAD_SANITIZER_BUILD 1
411#else
412# define LLVM_THREAD_SANITIZER_BUILD 0
413#endif
414
415#if LLVM_THREAD_SANITIZER_BUILD
416// Thread Sanitizer is a tool that finds races in code.
417// See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
418// tsan detects these exact functions by name.
419#ifdef __cplusplus
420extern "C" {
421#endif
422void AnnotateHappensAfter(const char *file, int line, const volatile void *cv);
423void AnnotateHappensBefore(const char *file, int line, const volatile void *cv);
424void AnnotateIgnoreWritesBegin(const char *file, int line);
425void AnnotateIgnoreWritesEnd(const char *file, int line);
426#ifdef __cplusplus
427}
428#endif
429
430// This marker is used to define a happens-before arc. The race detector will
431// infer an arc from the begin to the end when they share the same pointer
432// argument.
433# define TsanHappensBefore(cv) AnnotateHappensBefore(__FILE__, __LINE__, cv)
434
435// This marker defines the destination of a happens-before arc.
436# define TsanHappensAfter(cv) AnnotateHappensAfter(__FILE__, __LINE__, cv)
437
438// Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
439# define TsanIgnoreWritesBegin() AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
440
441// Resume checking for racy writes.
442# define TsanIgnoreWritesEnd() AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
443#else
444# define TsanHappensBefore(cv)
445# define TsanHappensAfter(cv)
446# define TsanIgnoreWritesBegin()
447# define TsanIgnoreWritesEnd()
448#endif
449
450/// \macro LLVM_NO_SANITIZE
451/// Disable a particular sanitizer for a function.
452#if __has_attribute(no_sanitize)
453#define LLVM_NO_SANITIZE(KIND) __attribute__((no_sanitize(KIND)))
454#else
455#define LLVM_NO_SANITIZE(KIND)
456#endif
457
458/// Mark debug helper function definitions like dump() that should not be
459/// stripped from debug builds.
460/// Note that you should also surround dump() functions with
461/// `#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)` so they do always
462/// get stripped in release builds.
463// FIXME: Move this to a private config.h as it's not usable in public headers.
464#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
465#define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
466#else
467#define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
468#endif
469
470/// \macro LLVM_PRETTY_FUNCTION
471/// Gets a user-friendly looking function signature for the current scope
472/// using the best available method on each platform. The exact format of the
473/// resulting string is implementation specific and non-portable, so this should
474/// only be used, for example, for logging or diagnostics.
475#if defined(_MSC_VER)
476#define LLVM_PRETTY_FUNCTION __FUNCSIG__
477#elif defined(__GNUC__) || defined(__clang__)
478#define LLVM_PRETTY_FUNCTION __PRETTY_FUNCTION__
479#else
480#define LLVM_PRETTY_FUNCTION __func__
481#endif
482
483/// \macro LLVM_THREAD_LOCAL
484/// A thread-local storage specifier which can be used with globals,
485/// extern globals, and static globals.
486///
487/// This is essentially an extremely restricted analog to C++11's thread_local
488/// support, and uses that when available. However, it falls back on
489/// platform-specific or vendor-provided extensions when necessary. These
490/// extensions don't support many of the C++11 thread_local's features. You
491/// should only use this for PODs that you can statically initialize to
492/// some constant value. In almost all circumstances this is most appropriate
493/// for use with a pointer, integer, or small aggregation of pointers and
494/// integers.
495#if LLVM_ENABLE_THREADS
496#if __has_feature(cxx_thread_local)
497#define LLVM_THREAD_LOCAL thread_local
498#elif defined(_MSC_VER)
499// MSVC supports this with a __declspec.
500#define LLVM_THREAD_LOCAL __declspec(thread)
501#else
502// Clang, GCC, and other compatible compilers used __thread prior to C++11 and
503// we only need the restricted functionality that provides.
504#define LLVM_THREAD_LOCAL __thread
505#endif
506#else // !LLVM_ENABLE_THREADS
507// If threading is disabled entirely, this compiles to nothing and you get
508// a normal global variable.
509#define LLVM_THREAD_LOCAL
510#endif
511
512/// \macro LLVM_ENABLE_EXCEPTIONS
513/// Whether LLVM is built with exception support.
514#if __has_feature(cxx_exceptions)
515#define LLVM_ENABLE_EXCEPTIONS 1
516#elif defined(__GNUC__) && defined(__EXCEPTIONS)
517#define LLVM_ENABLE_EXCEPTIONS 1
518#elif defined(_MSC_VER) && defined(_CPPUNWIND)
519#define LLVM_ENABLE_EXCEPTIONS 1
520#endif
521
522namespace llvm {
523
524/// Allocate a buffer of memory with the given size and alignment.
525///
526/// When the compiler supports aligned operator new, this will use it to to
527/// handle even over-aligned allocations.
528///
529/// However, this doesn't make any attempt to leverage the fancier techniques
530/// like posix_memalign due to portability. It is mostly intended to allow
531/// compatibility with platforms that, after aligned allocation was added, use
532/// reduced default alignment.
533inline void *allocate_buffer(size_t Size, size_t Alignment) {
534 return ::operator new(Size
535#ifdef __cpp_aligned_new
536 ,
537 std::align_val_t(Alignment)
538#endif
539 );
540}
541
542/// Deallocate a buffer of memory with the given size and alignment.
543///
544/// If supported, this will used the sized delete operator. Also if supported,
545/// this will pass the alignment to the delete operator.
546///
547/// The pointer must have been allocated with the corresponding new operator,
548/// most likely using the above helper.
549inline void deallocate_buffer(void *Ptr, size_t Size, size_t Alignment) {
550 ::operator delete(Ptr
551#ifdef __cpp_sized_deallocation
552 ,
553 Size
554#endif
555#ifdef __cpp_aligned_new
556 ,
557 std::align_val_t(Alignment)
558#endif
559 );
560}
561
562} // End namespace llvm
563
564#endif
565