1 //===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_COMPILER_H
16 #define LLVM_SUPPORT_COMPILER_H
18 #include "llvm/Config/llvm-config.h"
21 # define __has_feature(x) 0
24 #ifndef __has_extension
25 # define __has_extension(x) 0
28 #ifndef __has_attribute
29 # define __has_attribute(x) 0
33 # define __has_builtin(x) 0
36 /// \macro LLVM_GNUC_PREREQ
37 /// \brief Extend the default __GNUC_PREREQ even if glibc's features.h isn't
39 #ifndef LLVM_GNUC_PREREQ
40 # if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
41 # define LLVM_GNUC_PREREQ(maj, min, patch) \
42 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
43 ((maj) << 20) + ((min) << 10) + (patch))
44 # elif defined(__GNUC__) && defined(__GNUC_MINOR__)
45 # define LLVM_GNUC_PREREQ(maj, min, patch) \
46 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
48 # define LLVM_GNUC_PREREQ(maj, min, patch) 0
52 /// \macro LLVM_MSC_PREREQ
53 /// \brief Is the compiler MSVC of at least the specified version?
54 /// The common \param version values to check for are:
55 /// * 1800: Microsoft Visual Studio 2013 / 12.0
56 /// * 1900: Microsoft Visual Studio 2015 / 14.0
58 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
60 // We require at least MSVC 2013.
61 #if !LLVM_MSC_PREREQ(1800)
62 #error LLVM requires at least MSVC 2013.
66 #define LLVM_MSC_PREREQ(version) 0
69 #if !defined(_MSC_VER) || defined(__clang__) || LLVM_MSC_PREREQ(1900)
70 #define LLVM_NOEXCEPT noexcept
75 /// \brief Does the compiler support r-value reference *this?
77 /// Sadly, this is separate from just r-value reference support because GCC
78 /// implemented this later than everything else.
79 #if __has_feature(cxx_rvalue_references) || LLVM_GNUC_PREREQ(4, 8, 1)
80 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
82 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
85 /// Expands to '&' if r-value references are supported.
87 /// This can be used to provide l-value/r-value overrides of member functions.
88 /// The r-value override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
89 #if LLVM_HAS_RVALUE_REFERENCE_THIS
90 #define LLVM_LVALUE_FUNCTION &
92 #define LLVM_LVALUE_FUNCTION
95 /// LLVM_DELETED_FUNCTION - Expands to = delete if the compiler supports it.
96 /// Use to mark functions as uncallable. Member functions with this should
97 /// be declared private so that some behavior is kept in C++03 mode.
101 /// DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
102 /// DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
106 #if __has_feature(cxx_deleted_functions) || \
107 defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
108 #define LLVM_DELETED_FUNCTION = delete
110 #define LLVM_DELETED_FUNCTION
113 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
114 # define LLVM_CONSTEXPR constexpr
116 # define LLVM_CONSTEXPR
119 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
120 /// into a shared library, then the class should be private to the library and
121 /// not accessible from outside it. Can also be used to mark variables and
122 /// functions, making them private to any shared library they are linked into.
123 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
124 #if (__has_attribute(visibility) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
125 !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
126 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
128 #define LLVM_LIBRARY_VISIBILITY
131 #if __has_attribute(sentinel) || LLVM_GNUC_PREREQ(3, 0, 0)
132 #define LLVM_END_WITH_NULL __attribute__((sentinel))
134 #define LLVM_END_WITH_NULL
137 #if __has_attribute(used) || LLVM_GNUC_PREREQ(3, 1, 0)
138 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
140 #define LLVM_ATTRIBUTE_USED
143 #if __has_attribute(warn_unused_result) || LLVM_GNUC_PREREQ(3, 4, 0)
144 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
146 #define LLVM_ATTRIBUTE_UNUSED_RESULT
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.
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__))
160 #define LLVM_ATTRIBUTE_UNUSED
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(LLVM_ON_WIN32))
166 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
168 #define LLVM_ATTRIBUTE_WEAK
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__))
177 #define LLVM_READNONE
180 #if __has_attribute(pure) || defined(__GNUC__)
181 // aka 'PURE' but following LLVM Conventions.
182 #define LLVM_READONLY __attribute__((__pure__))
184 #define LLVM_READONLY
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)
191 #define LLVM_LIKELY(EXPR) (EXPR)
192 #define LLVM_UNLIKELY(EXPR) (EXPR)
195 // C++ doesn't support 'extern template' of template specializations. GCC does,
196 // but requires __extension__ before it. In the header, use this:
197 // EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
198 // in the .cpp file, use this:
199 // TEMPLATE_INSTANTIATION(class foo<bar>);
201 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
202 #define TEMPLATE_INSTANTIATION(X) template X
204 #define EXTERN_TEMPLATE_INSTANTIATION(X)
205 #define TEMPLATE_INSTANTIATION(X)
208 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
209 /// mark a method "not for inlining".
210 #if __has_attribute(noinline) || LLVM_GNUC_PREREQ(3, 4, 0)
211 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
212 #elif defined(_MSC_VER)
213 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
215 #define LLVM_ATTRIBUTE_NOINLINE
218 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
219 /// so, mark a method "always inline" because it is performance sensitive. GCC
220 /// 3.4 supported this but is buggy in various cases and produces unimplemented
221 /// errors, just use it in GCC 4.0 and later.
222 #if __has_attribute(always_inline) || LLVM_GNUC_PREREQ(4, 0, 0)
223 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
224 #elif defined(_MSC_VER)
225 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
227 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
231 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
232 #elif defined(_MSC_VER)
233 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
235 #define LLVM_ATTRIBUTE_NORETURN
238 #if __has_attribute(returns_nonnull) || LLVM_GNUC_PREREQ(4, 9, 0)
239 #define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
241 #define LLVM_ATTRIBUTE_RETURNS_NONNULL
244 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
245 /// pedantic diagnostics.
247 #define LLVM_EXTENSION __extension__
249 #define LLVM_EXTENSION
252 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
253 #if __has_feature(attribute_deprecated_with_message)
254 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
255 decl __attribute__((deprecated(message)))
256 #elif defined(__GNUC__)
257 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
258 decl __attribute__((deprecated))
259 #elif defined(_MSC_VER)
260 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
261 __declspec(deprecated(message)) decl
263 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
267 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
268 /// to an expression which states that it is undefined behavior for the
269 /// compiler to reach this point. Otherwise is not defined.
270 #if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0)
271 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
272 #elif defined(_MSC_VER)
273 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
276 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
277 /// which causes the program to exit abnormally.
278 #if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0)
279 # define LLVM_BUILTIN_TRAP __builtin_trap()
280 #elif defined(_MSC_VER)
281 // The __debugbreak intrinsic is supported by MSVC, does not require forward
282 // declarations involving platform-specific typedefs (unlike RaiseException),
283 // results in a call to vectored exception handlers, and encodes to a short
284 // instruction that still causes the trapping behavior we want.
285 # define LLVM_BUILTIN_TRAP __debugbreak()
287 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
290 /// \macro LLVM_ASSUME_ALIGNED
291 /// \brief Returns a pointer with an assumed alignment.
292 #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
293 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
294 #elif defined(LLVM_BUILTIN_UNREACHABLE)
295 // As of today, clang does not support __builtin_assume_aligned.
296 # define LLVM_ASSUME_ALIGNED(p, a) \
297 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
299 # define LLVM_ASSUME_ALIGNED(p, a) (p)
302 /// \macro LLVM_FUNCTION_NAME
303 /// \brief Expands to __func__ on compilers which support it. Otherwise,
304 /// expands to a compiler-dependent replacement.
305 #if defined(_MSC_VER)
306 # define LLVM_FUNCTION_NAME __FUNCTION__
308 # define LLVM_FUNCTION_NAME __func__
311 /// \macro LLVM_MEMORY_SANITIZER_BUILD
312 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
313 #if __has_feature(memory_sanitizer)
314 # define LLVM_MEMORY_SANITIZER_BUILD 1
315 # include <sanitizer/msan_interface.h>
317 # define LLVM_MEMORY_SANITIZER_BUILD 0
318 # define __msan_allocated_memory(p, size)
319 # define __msan_unpoison(p, size)
322 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
323 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
324 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
325 # define LLVM_ADDRESS_SANITIZER_BUILD 1
327 # define LLVM_ADDRESS_SANITIZER_BUILD 0
330 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
331 /// \brief Is unaligned memory access fast on the host machine.
333 /// Don't specialize on alignment for platforms where unaligned memory accesses
334 /// generates the same code as aligned memory accesses for common types.
335 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
336 defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
337 defined(_X86_) || defined(__i386) || defined(__i386__)
338 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
340 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
343 /// \macro LLVM_EXPLICIT
344 /// \brief Expands to explicit on compilers which support explicit conversion
345 /// operators. Otherwise expands to nothing.
346 #if __has_feature(cxx_explicit_conversions) || \
347 defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
348 #define LLVM_EXPLICIT explicit
350 #define LLVM_EXPLICIT
353 /// \brief Does the compiler support generalized initializers (using braced
354 /// lists and std::initializer_list). While clang may claim it supports general
355 /// initializers, if we're using MSVC's headers, we might not have a usable
356 /// std::initializer list type from the STL. Disable this for now.
357 #if __has_feature(cxx_generalized_initializers) && !defined(_MSC_VER)
358 #define LLVM_HAS_INITIALIZER_LISTS 1
360 #define LLVM_HAS_INITIALIZER_LISTS 0
363 /// \brief Mark debug helper function definitions like dump() that should not be
364 /// stripped from debug builds.
365 // FIXME: Move this to a private config.h as it's not usable in public headers.
366 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
367 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
369 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
372 /// \macro LLVM_THREAD_LOCAL
373 /// \brief A thread-local storage specifier which can be used with globals,
374 /// extern globals, and static globals.
376 /// This is essentially an extremely restricted analog to C++11's thread_local
377 /// support, and uses that when available. However, it falls back on
378 /// platform-specific or vendor-provided extensions when necessary. These
379 /// extensions don't support many of the C++11 thread_local's features. You
380 /// should only use this for PODs that you can statically initialize to
381 /// some constant value. In almost all circumstances this is most appropriate
382 /// for use with a pointer, integer, or small aggregation of pointers and
384 #if LLVM_ENABLE_THREADS
385 #if __has_feature(cxx_thread_local)
386 #define LLVM_THREAD_LOCAL thread_local
387 #elif defined(_MSC_VER)
388 // MSVC supports this with a __declspec.
389 #define LLVM_THREAD_LOCAL __declspec(thread)
391 // Clang, GCC, and other compatible compilers used __thread prior to C++11 and
392 // we only need the restricted functionality that provides.
393 #define LLVM_THREAD_LOCAL __thread
395 #else // !LLVM_ENABLE_THREADS
396 // If threading is disabled entirely, this compiles to nothing and you get
397 // a normal global variable.
398 #define LLVM_THREAD_LOCAL