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