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 /// * 1700: Microsoft Visual Studio 2012 / 11.0
56 /// * 1800: Microsoft Visual Studio 2013 / 12.0
58 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
60 // We require at least MSVC 2012.
61 #if !LLVM_MSC_PREREQ(1700)
62 #error LLVM requires at least MSVC 2012.
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 everything but this thus far. No release of GCC yet has support
79 /// for this feature so it is enabled with Clang only.
80 #if __has_feature(cxx_rvalue_references) || LLVM_GNUC_PREREQ(4, 8, 1)
81 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
83 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
86 /// \macro LLVM_HAS_VARIADIC_TEMPLATES
87 /// \brief Does this compiler support variadic templates.
89 /// Implies LLVM_HAS_RVALUE_REFERENCES and the existence of std::forward.
90 #if __has_feature(cxx_variadic_templates) || LLVM_MSC_PREREQ(1800)
91 # define LLVM_HAS_VARIADIC_TEMPLATES 1
93 # define LLVM_HAS_VARIADIC_TEMPLATES 0
96 /// Expands to '&' if r-value references are supported.
98 /// This can be used to provide l-value/r-value overrides of member functions.
99 /// The r-value override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
100 #if LLVM_HAS_RVALUE_REFERENCE_THIS
101 #define LLVM_LVALUE_FUNCTION &
103 #define LLVM_LVALUE_FUNCTION
106 /// LLVM_DELETED_FUNCTION - Expands to = delete if the compiler supports it.
107 /// Use to mark functions as uncallable. Member functions with this should
108 /// be declared private so that some behavior is kept in C++03 mode.
112 /// DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
113 /// DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
117 #if __has_feature(cxx_deleted_functions) || \
118 defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
119 #define LLVM_DELETED_FUNCTION = delete
121 #define LLVM_DELETED_FUNCTION
124 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
125 # define LLVM_CONSTEXPR constexpr
127 # define LLVM_CONSTEXPR
130 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
131 /// into a shared library, then the class should be private to the library and
132 /// not accessible from outside it. Can also be used to mark variables and
133 /// functions, making them private to any shared library they are linked into.
134 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
135 #if (__has_attribute(visibility) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
136 !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
137 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
139 #define LLVM_LIBRARY_VISIBILITY
142 #if __has_attribute(sentinel) || LLVM_GNUC_PREREQ(3, 0, 0)
143 #define LLVM_END_WITH_NULL __attribute__((sentinel))
145 #define LLVM_END_WITH_NULL
148 #if __has_attribute(used) || LLVM_GNUC_PREREQ(3, 1, 0)
149 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
151 #define LLVM_ATTRIBUTE_USED
154 #if __has_attribute(warn_unused_result) || LLVM_GNUC_PREREQ(3, 4, 0)
155 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
157 #define LLVM_ATTRIBUTE_UNUSED_RESULT
160 // Some compilers warn about unused functions. When a function is sometimes
161 // used or not depending on build settings (e.g. a function only called from
162 // within "assert"), this attribute can be used to suppress such warnings.
164 // However, it shouldn't be used for unused *variables*, as those have a much
165 // more portable solution:
166 // (void)unused_var_name;
167 // Prefer cast-to-void wherever it is sufficient.
168 #if __has_attribute(unused) || LLVM_GNUC_PREREQ(3, 1, 0)
169 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
171 #define LLVM_ATTRIBUTE_UNUSED
174 // FIXME: Provide this for PE/COFF targets.
175 #if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
176 (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
177 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
179 #define LLVM_ATTRIBUTE_WEAK
182 // Prior to clang 3.2, clang did not accept any spelling of
183 // __has_attribute(const), so assume it is supported.
184 #if defined(__clang__) || defined(__GNUC__)
185 // aka 'CONST' but following LLVM Conventions.
186 #define LLVM_READNONE __attribute__((__const__))
188 #define LLVM_READNONE
191 #if __has_attribute(pure) || defined(__GNUC__)
192 // aka 'PURE' but following LLVM Conventions.
193 #define LLVM_READONLY __attribute__((__pure__))
195 #define LLVM_READONLY
198 #if __has_builtin(__builtin_expect) || LLVM_GNUC_PREREQ(4, 0, 0)
199 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
200 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
202 #define LLVM_LIKELY(EXPR) (EXPR)
203 #define LLVM_UNLIKELY(EXPR) (EXPR)
206 // C++ doesn't support 'extern template' of template specializations. GCC does,
207 // but requires __extension__ before it. In the header, use this:
208 // EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
209 // in the .cpp file, use this:
210 // TEMPLATE_INSTANTIATION(class foo<bar>);
212 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
213 #define TEMPLATE_INSTANTIATION(X) template X
215 #define EXTERN_TEMPLATE_INSTANTIATION(X)
216 #define TEMPLATE_INSTANTIATION(X)
219 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
220 /// mark a method "not for inlining".
221 #if __has_attribute(noinline) || LLVM_GNUC_PREREQ(3, 4, 0)
222 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
223 #elif defined(_MSC_VER)
224 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
226 #define LLVM_ATTRIBUTE_NOINLINE
229 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
230 /// so, mark a method "always inline" because it is performance sensitive. GCC
231 /// 3.4 supported this but is buggy in various cases and produces unimplemented
232 /// errors, just use it in GCC 4.0 and later.
233 #if __has_attribute(always_inline) || LLVM_GNUC_PREREQ(4, 0, 0)
234 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
235 #elif defined(_MSC_VER)
236 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
238 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
242 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
243 #elif defined(_MSC_VER)
244 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
246 #define LLVM_ATTRIBUTE_NORETURN
249 #if __has_attribute(returns_nonnull) || LLVM_GNUC_PREREQ(4, 9, 0)
250 #define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
252 #define LLVM_ATTRIBUTE_RETURNS_NONNULL
255 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
256 /// pedantic diagnostics.
258 #define LLVM_EXTENSION __extension__
260 #define LLVM_EXTENSION
263 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
264 #if __has_feature(attribute_deprecated_with_message)
265 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
266 decl __attribute__((deprecated(message)))
267 #elif defined(__GNUC__)
268 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
269 decl __attribute__((deprecated))
270 #elif defined(_MSC_VER)
271 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
272 __declspec(deprecated(message)) decl
274 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
278 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
279 /// to an expression which states that it is undefined behavior for the
280 /// compiler to reach this point. Otherwise is not defined.
281 #if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0)
282 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
283 #elif defined(_MSC_VER)
284 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
287 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
288 /// which causes the program to exit abnormally.
289 #if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0)
290 # define LLVM_BUILTIN_TRAP __builtin_trap()
292 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
295 /// \macro LLVM_ASSUME_ALIGNED
296 /// \brief Returns a pointer with an assumed alignment.
297 #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
298 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
299 #elif defined(LLVM_BUILTIN_UNREACHABLE)
300 // As of today, clang does not support __builtin_assume_aligned.
301 # define LLVM_ASSUME_ALIGNED(p, a) \
302 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
304 # define LLVM_ASSUME_ALIGNED(p, a) (p)
307 /// \macro LLVM_FUNCTION_NAME
308 /// \brief Expands to __func__ on compilers which support it. Otherwise,
309 /// expands to a compiler-dependent replacement.
310 #if defined(_MSC_VER)
311 # define LLVM_FUNCTION_NAME __FUNCTION__
313 # define LLVM_FUNCTION_NAME __func__
316 /// \macro LLVM_MEMORY_SANITIZER_BUILD
317 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
318 #if __has_feature(memory_sanitizer)
319 # define LLVM_MEMORY_SANITIZER_BUILD 1
320 # include <sanitizer/msan_interface.h>
322 # define LLVM_MEMORY_SANITIZER_BUILD 0
323 # define __msan_allocated_memory(p, size)
324 # define __msan_unpoison(p, size)
327 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
328 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
329 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
330 # define LLVM_ADDRESS_SANITIZER_BUILD 1
332 # define LLVM_ADDRESS_SANITIZER_BUILD 0
335 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
336 /// \brief Is unaligned memory access fast on the host machine.
338 /// Don't specialize on alignment for platforms where unaligned memory accesses
339 /// generates the same code as aligned memory accesses for common types.
340 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
341 defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
342 defined(_X86_) || defined(__i386) || defined(__i386__)
343 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
345 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
348 /// \macro LLVM_EXPLICIT
349 /// \brief Expands to explicit on compilers which support explicit conversion
350 /// operators. Otherwise expands to nothing.
351 #if __has_feature(cxx_explicit_conversions) || \
352 defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
353 #define LLVM_EXPLICIT explicit
355 #define LLVM_EXPLICIT
358 /// \brief Does the compiler support generalized initializers (using braced
359 /// lists and std::initializer_list). While clang may claim it supports general
360 /// initializers, if we're using MSVC's headers, we might not have a usable
361 /// std::initializer list type from the STL. Disable this for now.
362 #if __has_feature(cxx_generalized_initializers) && !defined(_MSC_VER)
363 #define LLVM_HAS_INITIALIZER_LISTS 1
365 #define LLVM_HAS_INITIALIZER_LISTS 0
368 /// \brief Mark debug helper function definitions like dump() that should not be
369 /// stripped from debug builds.
370 // FIXME: Move this to a private config.h as it's not usable in public headers.
371 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
372 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
374 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE