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_attribute
25 # define __has_attribute(x) 0
29 # define __has_builtin(x) 0
32 /// \macro __GNUC_PREREQ
33 /// \brief Defines __GNUC_PREREQ if glibc's features.h isn't available.
35 # if defined(__GNUC__) && defined(__GNUC_MINOR__)
36 # define __GNUC_PREREQ(maj, min) \
37 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
39 # define __GNUC_PREREQ(maj, min) 0
43 /// \brief Does the compiler support r-value references?
44 /// This implies that <utility> provides the one-argument std::move; it
45 /// does not imply the existence of any other C++ library features.
46 #if (__has_feature(cxx_rvalue_references) \
47 || defined(__GXX_EXPERIMENTAL_CXX0X__) \
48 || (defined(_MSC_VER) && _MSC_VER >= 1600))
49 #define LLVM_HAS_RVALUE_REFERENCES 1
51 #define LLVM_HAS_RVALUE_REFERENCES 0
54 /// \brief Does the compiler support r-value reference *this?
56 /// Sadly, this is separate from just r-value reference support because GCC
57 /// implemented everything but this thus far. No release of GCC yet has support
58 /// for this feature so it is enabled with Clang only.
59 /// FIXME: This should change to a version check when GCC grows support for it.
60 #if __has_feature(cxx_rvalue_references)
61 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
63 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
66 /// \macro LLVM_HAS_CXX11_TYPETRAITS
67 /// \brief Does the compiler have the C++11 type traits.
69 /// #include <type_traits>
72 /// * {true,false}_type
73 /// * is_constructible
75 #if defined(__GXX_EXPERIMENTAL_CXX0X__) \
76 || (defined(_MSC_VER) && _MSC_VER >= 1700)
77 #define LLVM_HAS_CXX11_TYPETRAITS 1
79 #define LLVM_HAS_CXX11_TYPETRAITS 0
82 /// \macro LLVM_HAS_CXX11_STDLIB
83 /// \brief Does the compiler have the C++11 standard library.
85 /// Implies LLVM_HAS_RVALUE_REFERENCES, LLVM_HAS_CXX11_TYPETRAITS
86 #if defined(__GXX_EXPERIMENTAL_CXX0X__) \
87 || (defined(_MSC_VER) && _MSC_VER >= 1700)
88 #define LLVM_HAS_CXX11_STDLIB 1
90 #define LLVM_HAS_CXX11_STDLIB 0
93 /// \macro LLVM_HAS_VARIADIC_TEMPLATES
94 /// \brief Does this compiler support variadic templates.
96 /// Implies LLVM_HAS_RVALUE_REFERENCES and the existence of std::forward.
97 #if __has_feature(cxx_variadic_templates)
98 # define LLVM_HAS_VARIADIC_TEMPLATES 1
100 # define LLVM_HAS_VARIADIC_TEMPLATES 0
103 /// llvm_move - Expands to ::std::move if the compiler supports
104 /// r-value references; otherwise, expands to the argument.
105 #if LLVM_HAS_RVALUE_REFERENCES
106 #define llvm_move(value) (::std::move(value))
108 #define llvm_move(value) (value)
111 /// Expands to '&' if r-value references are supported.
113 /// This can be used to provide l-value/r-value overrides of member functions.
114 /// The r-value override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
115 #if LLVM_HAS_RVALUE_REFERENCE_THIS
116 #define LLVM_LVALUE_FUNCTION &
118 #define LLVM_LVALUE_FUNCTION
121 /// LLVM_DELETED_FUNCTION - Expands to = delete if the compiler supports it.
122 /// Use to mark functions as uncallable. Member functions with this should
123 /// be declared private so that some behavior is kept in C++03 mode.
127 /// DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
128 /// DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
132 #if (__has_feature(cxx_deleted_functions) \
133 || defined(__GXX_EXPERIMENTAL_CXX0X__))
134 // No version of MSVC currently supports this.
135 #define LLVM_DELETED_FUNCTION = delete
137 #define LLVM_DELETED_FUNCTION
140 /// LLVM_FINAL - Expands to 'final' if the compiler supports it.
141 /// Use to mark classes or virtual methods as final.
142 #if __has_feature(cxx_override_control) \
143 || (defined(_MSC_VER) && _MSC_VER >= 1700)
144 #define LLVM_FINAL final
149 /// LLVM_OVERRIDE - Expands to 'override' if the compiler supports it.
150 /// Use to mark virtual methods as overriding a base class method.
151 #if __has_feature(cxx_override_control) \
152 || (defined(_MSC_VER) && _MSC_VER >= 1700)
153 #define LLVM_OVERRIDE override
155 #define LLVM_OVERRIDE
158 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
159 # define LLVM_CONSTEXPR constexpr
161 # define LLVM_CONSTEXPR
164 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
165 /// into a shared library, then the class should be private to the library and
166 /// not accessible from outside it. Can also be used to mark variables and
167 /// functions, making them private to any shared library they are linked into.
168 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
169 #if (__has_attribute(visibility) || __GNUC_PREREQ(4, 0)) && \
170 !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
171 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
173 #define LLVM_LIBRARY_VISIBILITY
176 #if __has_attribute(used) || __GNUC_PREREQ(3, 1)
177 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
179 #define LLVM_ATTRIBUTE_USED
182 // Some compilers warn about unused functions. When a function is sometimes
183 // used or not depending on build settings (e.g. a function only called from
184 // within "assert"), this attribute can be used to suppress such warnings.
186 // However, it shouldn't be used for unused *variables*, as those have a much
187 // more portable solution:
188 // (void)unused_var_name;
189 // Prefer cast-to-void wherever it is sufficient.
190 #if __has_attribute(unused) || __GNUC_PREREQ(3, 1)
191 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
193 #define LLVM_ATTRIBUTE_UNUSED
196 // FIXME: Provide this for PE/COFF targets.
197 #if (__has_attribute(weak) || __GNUC_PREREQ(4, 0)) && \
198 (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
199 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
201 #define LLVM_ATTRIBUTE_WEAK
204 #if __has_attribute(__const__) || defined(__GNUC__)
205 // aka 'CONST' but following LLVM Conventions.
206 #define LLVM_READNONE __attribute__((__const__))
208 #define LLVM_READNONE
211 #if __has_attribute(pure) || defined(__GNUC__)
212 // aka 'PURE' but following LLVM Conventions.
213 #define LLVM_READONLY __attribute__((__pure__))
215 #define LLVM_READONLY
218 #if __has_builtin(__builtin_expect) || __GNUC_PREREQ(4, 0)
219 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
220 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
222 #define LLVM_LIKELY(EXPR) (EXPR)
223 #define LLVM_UNLIKELY(EXPR) (EXPR)
226 // C++ doesn't support 'extern template' of template specializations. GCC does,
227 // but requires __extension__ before it. In the header, use this:
228 // EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
229 // in the .cpp file, use this:
230 // TEMPLATE_INSTANTIATION(class foo<bar>);
232 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
233 #define TEMPLATE_INSTANTIATION(X) template X
235 #define EXTERN_TEMPLATE_INSTANTIATION(X)
236 #define TEMPLATE_INSTANTIATION(X)
239 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
240 /// mark a method "not for inlining".
241 #if __has_attribute(noinline) || __GNUC_PREREQ(3, 4)
242 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
243 #elif defined(_MSC_VER)
244 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
246 #define LLVM_ATTRIBUTE_NOINLINE
249 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
250 /// so, mark a method "always inline" because it is performance sensitive. GCC
251 /// 3.4 supported this but is buggy in various cases and produces unimplemented
252 /// errors, just use it in GCC 4.0 and later.
253 #if __has_attribute(always_inline) || __GNUC_PREREQ(4, 0)
254 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
255 #elif defined(_MSC_VER)
256 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
258 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
262 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
263 #elif defined(_MSC_VER)
264 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
266 #define LLVM_ATTRIBUTE_NORETURN
269 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
270 /// pedantic diagnostics.
272 #define LLVM_EXTENSION __extension__
274 #define LLVM_EXTENSION
277 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
278 #if __has_feature(attribute_deprecated_with_message)
279 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
280 decl __attribute__((deprecated(message)))
281 #elif defined(__GNUC__)
282 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
283 decl __attribute__((deprecated))
284 #elif defined(_MSC_VER)
285 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
286 __declspec(deprecated(message)) decl
288 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
292 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
293 /// to an expression which states that it is undefined behavior for the
294 /// compiler to reach this point. Otherwise is not defined.
295 #if __has_builtin(__builtin_unreachable) || __GNUC_PREREQ(4, 5)
296 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
297 #elif defined(_MSC_VER)
298 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
301 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
302 /// which causes the program to exit abnormally.
303 #if __has_builtin(__builtin_trap) || __GNUC_PREREQ(4, 3)
304 # define LLVM_BUILTIN_TRAP __builtin_trap()
306 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
309 /// \macro LLVM_ASSUME_ALIGNED
310 /// \brief Returns a pointer with an assumed alignment.
311 #if __has_builtin(__builtin_assume_aligned) && __GNUC_PREREQ(4, 7)
312 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
313 #elif defined(LLVM_BUILTIN_UNREACHABLE)
314 // As of today, clang does not support __builtin_assume_aligned.
315 # define LLVM_ASSUME_ALIGNED(p, a) \
316 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
318 # define LLVM_ASSUME_ALIGNED(p, a) (p)
321 /// \macro LLVM_FUNCTION_NAME
322 /// \brief Expands to __func__ on compilers which support it. Otherwise,
323 /// expands to a compiler-dependent replacement.
324 #if defined(_MSC_VER)
325 # define LLVM_FUNCTION_NAME __FUNCTION__
327 # define LLVM_FUNCTION_NAME __func__
330 #if defined(HAVE_SANITIZER_MSAN_INTERFACE_H)
331 # include <sanitizer/msan_interface.h>
333 # define __msan_allocated_memory(p, size)
334 # define __msan_unpoison(p, size)
337 /// \macro LLVM_MEMORY_SANITIZER_BUILD
338 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
339 #if __has_feature(memory_sanitizer)
340 # define LLVM_MEMORY_SANITIZER_BUILD 1
342 # define LLVM_MEMORY_SANITIZER_BUILD 0
345 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
346 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
347 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
348 # define LLVM_ADDRESS_SANITIZER_BUILD 1
350 # define LLVM_ADDRESS_SANITIZER_BUILD 0
353 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
354 /// \brief Is unaligned memory access fast on the host machine.
356 /// Don't specialize on alignment for platforms where unaligned memory accesses
357 /// generates the same code as aligned memory accesses for common types.
358 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
359 defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
360 defined(_X86_) || defined(__i386) || defined(__i386__)
361 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
363 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
366 /// \macro LLVM_EXPLICIT
367 /// \brief Expands to explicit on compilers which support explicit conversion
368 /// operators. Otherwise expands to nothing.
369 #if (__has_feature(cxx_explicit_conversions) \
370 || defined(__GXX_EXPERIMENTAL_CXX0X__))
371 #define LLVM_EXPLICIT explicit
373 #define LLVM_EXPLICIT
376 /// \macro LLVM_STATIC_ASSERT
377 /// \brief Expands to C/C++'s static_assert on compilers which support it.
378 #if __has_feature(cxx_static_assert)
379 # define LLVM_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
380 #elif __has_feature(c_static_assert)
381 # define LLVM_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
383 # define LLVM_STATIC_ASSERT(expr, msg)