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 /// \macro LLVM_MSC_PREREQ
44 /// \brief Is the compiler MSVC of at least the specified version?
45 /// The common \param version values to check for are:
46 /// * 1600: Microsoft Visual Studio 2010 / 10.0
47 /// * 1700: Microsoft Visual Studio 2012 / 11.0
48 /// * 1800: Microsoft Visual Studio 2013 / 12.0
50 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
52 #define LLVM_MSC_PREREQ(version) 0
55 /// \brief Does the compiler support r-value references?
56 /// This implies that <utility> provides the one-argument std::move; it
57 /// does not imply the existence of any other C++ library features.
58 #if __has_feature(cxx_rvalue_references) || \
59 defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1600)
60 #define LLVM_HAS_RVALUE_REFERENCES 1
62 #define LLVM_HAS_RVALUE_REFERENCES 0
65 /// \brief Does the compiler support r-value reference *this?
67 /// Sadly, this is separate from just r-value reference support because GCC
68 /// implemented everything but this thus far. No release of GCC yet has support
69 /// for this feature so it is enabled with Clang only.
70 /// FIXME: This should change to a version check when GCC grows support for it.
71 #if __has_feature(cxx_rvalue_references)
72 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
74 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
77 /// \macro LLVM_HAS_CXX11_TYPETRAITS
78 /// \brief Does the compiler have the C++11 type traits.
80 /// #include <type_traits>
83 /// * {true,false}_type
84 /// * is_constructible
86 #if defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1700)
87 #define LLVM_HAS_CXX11_TYPETRAITS 1
89 #define LLVM_HAS_CXX11_TYPETRAITS 0
92 /// \macro LLVM_HAS_CXX11_STDLIB
93 /// \brief Does the compiler have the C++11 standard library.
95 /// Implies LLVM_HAS_RVALUE_REFERENCES, LLVM_HAS_CXX11_TYPETRAITS
96 #if defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1700)
97 #define LLVM_HAS_CXX11_STDLIB 1
99 #define LLVM_HAS_CXX11_STDLIB 0
102 /// \macro LLVM_HAS_VARIADIC_TEMPLATES
103 /// \brief Does this compiler support variadic templates.
105 /// Implies LLVM_HAS_RVALUE_REFERENCES and the existence of std::forward.
106 #if __has_feature(cxx_variadic_templates)
107 # define LLVM_HAS_VARIADIC_TEMPLATES 1
109 # define LLVM_HAS_VARIADIC_TEMPLATES 0
112 /// llvm_move - Expands to ::std::move if the compiler supports
113 /// r-value references; otherwise, expands to the argument.
114 #if LLVM_HAS_RVALUE_REFERENCES
115 #define llvm_move(value) (::std::move(value))
117 #define llvm_move(value) (value)
120 /// Expands to '&' if r-value references are supported.
122 /// This can be used to provide l-value/r-value overrides of member functions.
123 /// The r-value override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
124 #if LLVM_HAS_RVALUE_REFERENCE_THIS
125 #define LLVM_LVALUE_FUNCTION &
127 #define LLVM_LVALUE_FUNCTION
130 /// LLVM_DELETED_FUNCTION - Expands to = delete if the compiler supports it.
131 /// Use to mark functions as uncallable. Member functions with this should
132 /// be declared private so that some behavior is kept in C++03 mode.
136 /// DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
137 /// DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
141 #if __has_feature(cxx_deleted_functions) || \
142 defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
143 #define LLVM_DELETED_FUNCTION = delete
145 #define LLVM_DELETED_FUNCTION
148 /// LLVM_FINAL - Expands to 'final' if the compiler supports it.
149 /// Use to mark classes or virtual methods as final.
150 #if __has_feature(cxx_override_control) || LLVM_MSC_PREREQ(1700)
151 #define LLVM_FINAL final
156 /// LLVM_OVERRIDE - Expands to 'override' if the compiler supports it.
157 /// Use to mark virtual methods as overriding a base class method.
158 #if __has_feature(cxx_override_control) || LLVM_MSC_PREREQ(1700)
159 #define LLVM_OVERRIDE override
161 #define LLVM_OVERRIDE
164 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
165 # define LLVM_CONSTEXPR constexpr
167 # define LLVM_CONSTEXPR
170 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
171 /// into a shared library, then the class should be private to the library and
172 /// not accessible from outside it. Can also be used to mark variables and
173 /// functions, making them private to any shared library they are linked into.
174 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
175 #if (__has_attribute(visibility) || __GNUC_PREREQ(4, 0)) && \
176 !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
177 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
179 #define LLVM_LIBRARY_VISIBILITY
182 #if __has_attribute(used) || __GNUC_PREREQ(3, 1)
183 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
185 #define LLVM_ATTRIBUTE_USED
188 #if __has_attribute(warn_unused_result) || __GNUC_PREREQ(3, 4)
189 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
191 #define LLVM_ATTRIBUTE_UNUSED_RESULT
194 // Some compilers warn about unused functions. When a function is sometimes
195 // used or not depending on build settings (e.g. a function only called from
196 // within "assert"), this attribute can be used to suppress such warnings.
198 // However, it shouldn't be used for unused *variables*, as those have a much
199 // more portable solution:
200 // (void)unused_var_name;
201 // Prefer cast-to-void wherever it is sufficient.
202 #if __has_attribute(unused) || __GNUC_PREREQ(3, 1)
203 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
205 #define LLVM_ATTRIBUTE_UNUSED
208 // FIXME: Provide this for PE/COFF targets.
209 #if (__has_attribute(weak) || __GNUC_PREREQ(4, 0)) && \
210 (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
211 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
213 #define LLVM_ATTRIBUTE_WEAK
216 // Prior to clang 3.2, clang did not accept any spelling of
217 // __has_attribute(const), so assume it is supported.
218 #if defined(__clang__) || defined(__GNUC__)
219 // aka 'CONST' but following LLVM Conventions.
220 #define LLVM_READNONE __attribute__((__const__))
222 #define LLVM_READNONE
225 #if __has_attribute(pure) || defined(__GNUC__)
226 // aka 'PURE' but following LLVM Conventions.
227 #define LLVM_READONLY __attribute__((__pure__))
229 #define LLVM_READONLY
232 #if __has_builtin(__builtin_expect) || __GNUC_PREREQ(4, 0)
233 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
234 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
236 #define LLVM_LIKELY(EXPR) (EXPR)
237 #define LLVM_UNLIKELY(EXPR) (EXPR)
240 // C++ doesn't support 'extern template' of template specializations. GCC does,
241 // but requires __extension__ before it. In the header, use this:
242 // EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
243 // in the .cpp file, use this:
244 // TEMPLATE_INSTANTIATION(class foo<bar>);
246 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
247 #define TEMPLATE_INSTANTIATION(X) template X
249 #define EXTERN_TEMPLATE_INSTANTIATION(X)
250 #define TEMPLATE_INSTANTIATION(X)
253 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
254 /// mark a method "not for inlining".
255 #if __has_attribute(noinline) || __GNUC_PREREQ(3, 4)
256 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
257 #elif defined(_MSC_VER)
258 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
260 #define LLVM_ATTRIBUTE_NOINLINE
263 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
264 /// so, mark a method "always inline" because it is performance sensitive. GCC
265 /// 3.4 supported this but is buggy in various cases and produces unimplemented
266 /// errors, just use it in GCC 4.0 and later.
267 #if __has_attribute(always_inline) || __GNUC_PREREQ(4, 0)
268 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
269 #elif defined(_MSC_VER)
270 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
272 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
276 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
277 #elif defined(_MSC_VER)
278 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
280 #define LLVM_ATTRIBUTE_NORETURN
283 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
284 /// pedantic diagnostics.
286 #define LLVM_EXTENSION __extension__
288 #define LLVM_EXTENSION
291 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
292 #if __has_feature(attribute_deprecated_with_message)
293 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
294 decl __attribute__((deprecated(message)))
295 #elif defined(__GNUC__)
296 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
297 decl __attribute__((deprecated))
298 #elif defined(_MSC_VER)
299 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
300 __declspec(deprecated(message)) decl
302 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
306 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
307 /// to an expression which states that it is undefined behavior for the
308 /// compiler to reach this point. Otherwise is not defined.
309 #if __has_builtin(__builtin_unreachable) || __GNUC_PREREQ(4, 5)
310 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
311 #elif defined(_MSC_VER)
312 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
315 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
316 /// which causes the program to exit abnormally.
317 #if __has_builtin(__builtin_trap) || __GNUC_PREREQ(4, 3)
318 # define LLVM_BUILTIN_TRAP __builtin_trap()
320 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
323 /// \macro LLVM_ASSUME_ALIGNED
324 /// \brief Returns a pointer with an assumed alignment.
325 #if __has_builtin(__builtin_assume_aligned) && __GNUC_PREREQ(4, 7)
326 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
327 #elif defined(LLVM_BUILTIN_UNREACHABLE)
328 // As of today, clang does not support __builtin_assume_aligned.
329 # define LLVM_ASSUME_ALIGNED(p, a) \
330 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
332 # define LLVM_ASSUME_ALIGNED(p, a) (p)
335 /// \macro LLVM_FUNCTION_NAME
336 /// \brief Expands to __func__ on compilers which support it. Otherwise,
337 /// expands to a compiler-dependent replacement.
338 #if defined(_MSC_VER)
339 # define LLVM_FUNCTION_NAME __FUNCTION__
341 # define LLVM_FUNCTION_NAME __func__
344 #if defined(HAVE_SANITIZER_MSAN_INTERFACE_H)
345 # include <sanitizer/msan_interface.h>
347 # define __msan_allocated_memory(p, size)
348 # define __msan_unpoison(p, size)
351 /// \macro LLVM_MEMORY_SANITIZER_BUILD
352 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
353 #if __has_feature(memory_sanitizer)
354 # define LLVM_MEMORY_SANITIZER_BUILD 1
356 # define LLVM_MEMORY_SANITIZER_BUILD 0
359 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
360 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
361 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
362 # define LLVM_ADDRESS_SANITIZER_BUILD 1
364 # define LLVM_ADDRESS_SANITIZER_BUILD 0
367 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
368 /// \brief Is unaligned memory access fast on the host machine.
370 /// Don't specialize on alignment for platforms where unaligned memory accesses
371 /// generates the same code as aligned memory accesses for common types.
372 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
373 defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
374 defined(_X86_) || defined(__i386) || defined(__i386__)
375 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
377 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
380 /// \macro LLVM_EXPLICIT
381 /// \brief Expands to explicit on compilers which support explicit conversion
382 /// operators. Otherwise expands to nothing.
383 #if __has_feature(cxx_explicit_conversions) || \
384 defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
385 #define LLVM_EXPLICIT explicit
387 #define LLVM_EXPLICIT
390 /// \macro LLVM_STATIC_ASSERT
391 /// \brief Expands to C/C++'s static_assert on compilers which support it.
392 #if __has_feature(cxx_static_assert) || \
393 defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1600)
394 # define LLVM_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
395 #elif __has_feature(c_static_assert)
396 # define LLVM_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
398 # define LLVM_STATIC_ASSERT(expr, msg)
401 /// \macro LLVM_ENUM_INT_TYPE
402 /// \brief Expands to colon followed by the given integral type on compilers
403 /// which support C++11 strong enums. This can be used to make enums unsigned
405 #if __has_feature(cxx_strong_enums) || LLVM_MSC_PREREQ(1600)
406 # define LLVM_ENUM_INT_TYPE(intty) : intty
408 # define LLVM_ENUM_INT_TYPE(intty)
411 /// \brief Does the compiler support C++11 semantics for strongly typed forward
413 #if __has_feature(cxx_strong_enums) || LLVM_MSC_PREREQ(1700)
414 #define LLVM_HAS_STRONG_ENUMS 1
416 #define LLVM_HAS_STRONG_ENUMS 0
419 /// \brief Does the compiler support generalized initializers (using braced
420 /// lists and std::initializer_list). While clang may claim it supports general
421 /// initializers, if we're using MSVC's headers, we might not have a usable
422 /// std::initializer list type from the STL. Disable this for now.
423 #if __has_feature(cxx_generalized_initializers) && !defined(_MSC_VER)
424 #define LLVM_HAS_INITIALIZER_LISTS 1
426 #define LLVM_HAS_INITIALIZER_LISTS 0