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/config.h"
21 # define __has_feature(x) 0
24 /// \brief Does the compiler support r-value references?
25 /// This implies that <utility> provides the one-argument std::move; it
26 /// does not imply the existence of any other C++ library features.
27 #if (__has_feature(cxx_rvalue_references) \
28 || defined(__GXX_EXPERIMENTAL_CXX0X__) \
29 || (defined(_MSC_VER) && _MSC_VER >= 1600))
30 #define LLVM_HAS_RVALUE_REFERENCES 1
32 #define LLVM_HAS_RVALUE_REFERENCES 0
35 /// \brief Does the compiler support r-value reference *this?
37 /// Sadly, this is separate from just r-value reference support because GCC
38 /// implemented everything but this thus far. No release of GCC yet has support
39 /// for this feature so it is enabled with Clang only.
40 /// FIXME: This should change to a version check when GCC grows support for it.
41 #if __has_feature(cxx_rvalue_references)
42 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
44 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
47 /// \macro LLVM_HAS_CXX11_TYPETRAITS
48 /// \brief Does the compiler have the C++11 type traits.
50 /// #include <type_traits>
53 /// * {true,false}_type
54 /// * is_constructible
56 #if defined(__GXX_EXPERIMENTAL_CXX0X__) \
57 || (defined(_MSC_VER) && _MSC_VER >= 1700)
58 #define LLVM_HAS_CXX11_TYPETRAITS 1
60 #define LLVM_HAS_CXX11_TYPETRAITS 0
63 /// \macro LLVM_HAS_CXX11_STDLIB
64 /// \brief Does the compiler have the C++11 standard library.
66 /// Implies LLVM_HAS_RVALUE_REFERENCES, LLVM_HAS_CXX11_TYPETRAITS
67 #if defined(__GXX_EXPERIMENTAL_CXX0X__) \
68 || (defined(_MSC_VER) && _MSC_VER >= 1700)
69 #define LLVM_HAS_CXX11_STDLIB 1
71 #define LLVM_HAS_CXX11_STDLIB 0
74 /// llvm_move - Expands to ::std::move if the compiler supports
75 /// r-value references; otherwise, expands to the argument.
76 #if LLVM_HAS_RVALUE_REFERENCES
77 #define llvm_move(value) (::std::move(value))
79 #define llvm_move(value) (value)
82 /// Expands to '&' if r-value references are supported.
84 /// This can be used to provide l-value/r-value overrides of member functions.
85 /// The r-value override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
86 #if LLVM_HAS_RVALUE_REFERENCE_THIS
87 #define LLVM_LVALUE_FUNCTION &
89 #define LLVM_LVALUE_FUNCTION
92 /// LLVM_DELETED_FUNCTION - Expands to = delete if the compiler supports it.
93 /// Use to mark functions as uncallable. Member functions with this should
94 /// be declared private so that some behavior is kept in C++03 mode.
98 /// DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
99 /// DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
103 #if (__has_feature(cxx_deleted_functions) \
104 || defined(__GXX_EXPERIMENTAL_CXX0X__))
105 // No version of MSVC currently supports this.
106 #define LLVM_DELETED_FUNCTION = delete
108 #define LLVM_DELETED_FUNCTION
111 /// LLVM_FINAL - Expands to 'final' if the compiler supports it.
112 /// Use to mark classes or virtual methods as final.
113 #if __has_feature(cxx_override_control) \
114 || (defined(_MSC_VER) && _MSC_VER >= 1700)
115 #define LLVM_FINAL final
120 /// LLVM_OVERRIDE - Expands to 'override' if the compiler supports it.
121 /// Use to mark virtual methods as overriding a base class method.
122 #if __has_feature(cxx_override_control) \
123 || (defined(_MSC_VER) && _MSC_VER >= 1700)
124 #define LLVM_OVERRIDE override
126 #define LLVM_OVERRIDE
129 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
130 # define LLVM_CONSTEXPR constexpr
132 # define LLVM_CONSTEXPR
135 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
136 /// into a shared library, then the class should be private to the library and
137 /// not accessible from outside it. Can also be used to mark variables and
138 /// functions, making them private to any shared library they are linked into.
139 #if (__GNUC__ >= 4) && !defined(__MINGW32__) && !defined(__CYGWIN__)
140 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
142 #define LLVM_LIBRARY_VISIBILITY
145 #if (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
146 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
148 #define LLVM_ATTRIBUTE_USED
151 // Some compilers warn about unused functions. When a function is sometimes
152 // used or not depending on build settings (e.g. a function only called from
153 // within "assert"), this attribute can be used to suppress such warnings.
155 // However, it shouldn't be used for unused *variables*, as those have a much
156 // more portable solution:
157 // (void)unused_var_name;
158 // Prefer cast-to-void wherever it is sufficient.
159 #if (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
160 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
162 #define LLVM_ATTRIBUTE_UNUSED
165 #if (__GNUC__ >= 4) && !defined(__MINGW32__) && !defined(__CYGWIN__)
166 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
168 #define LLVM_ATTRIBUTE_WEAK
171 #ifdef __GNUC__ // aka 'CONST' but following LLVM Conventions.
172 #define LLVM_READNONE __attribute__((__const__))
174 #define LLVM_READNONE
177 #ifdef __GNUC__ // aka 'PURE' but following LLVM Conventions.
178 #define LLVM_READONLY __attribute__((__pure__))
180 #define LLVM_READONLY
184 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
185 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
187 #define LLVM_LIKELY(EXPR) (EXPR)
188 #define LLVM_UNLIKELY(EXPR) (EXPR)
191 // C++ doesn't support 'extern template' of template specializations. GCC does,
192 // but requires __extension__ before it. In the header, use this:
193 // EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
194 // in the .cpp file, use this:
195 // TEMPLATE_INSTANTIATION(class foo<bar>);
197 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
198 #define TEMPLATE_INSTANTIATION(X) template X
200 #define EXTERN_TEMPLATE_INSTANTIATION(X)
201 #define TEMPLATE_INSTANTIATION(X)
204 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
205 /// mark a method "not for inlining".
206 #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
207 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
208 #elif defined(_MSC_VER)
209 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
211 #define LLVM_ATTRIBUTE_NOINLINE
214 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
215 /// so, mark a method "always inline" because it is performance sensitive. GCC
216 /// 3.4 supported this but is buggy in various cases and produces unimplemented
217 /// errors, just use it in GCC 4.0 and later.
219 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
220 #elif defined(_MSC_VER)
221 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
223 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
227 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
228 #elif defined(_MSC_VER)
229 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
231 #define LLVM_ATTRIBUTE_NORETURN
234 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
235 /// pedantic diagnostics.
237 #define LLVM_EXTENSION __extension__
239 #define LLVM_EXTENSION
242 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
243 #if __has_feature(attribute_deprecated_with_message)
244 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
245 decl __attribute__((deprecated(message)))
246 #elif defined(__GNUC__)
247 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
248 decl __attribute__((deprecated))
249 #elif defined(_MSC_VER)
250 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
251 __declspec(deprecated(message)) decl
253 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
257 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
258 /// to an expression which states that it is undefined behavior for the
259 /// compiler to reach this point. Otherwise is not defined.
260 #if defined(__clang__) || (__GNUC__ > 4) \
261 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
262 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
263 #elif defined(_MSC_VER)
264 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
267 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
268 /// which causes the program to exit abnormally.
269 #if defined(__clang__) || (__GNUC__ > 4) \
270 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
271 # define LLVM_BUILTIN_TRAP __builtin_trap()
273 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
276 /// \macro LLVM_ASSUME_ALIGNED
277 /// \brief Returns a pointer with an assumed alignment.
278 #if !defined(__clang__) && ((__GNUC__ > 4) \
279 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
280 // FIXME: Enable on clang when it supports it.
281 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
282 #elif defined(LLVM_BUILTIN_UNREACHABLE)
283 # define LLVM_ASSUME_ALIGNED(p, a) \
284 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
286 # define LLVM_ASSUME_ALIGNED(p, a) (p)
289 /// \macro LLVM_FUNCTION_NAME
290 /// \brief Expands to __func__ on compilers which support it. Otherwise,
291 /// expands to a compiler-dependent replacement.
292 #if defined(_MSC_VER)
293 # define LLVM_FUNCTION_NAME __FUNCTION__
295 # define LLVM_FUNCTION_NAME __func__
298 /// \macro LLVM_ENABLE_MSAN_ANNOTATIONS
299 /// \brief Are MemorySanitizer annotations available.
300 #if defined(HAVE_SANITIZER_MSAN_INTERFACE_H)
301 # include <sanitizer/msan_interface.h>
303 # define __msan_allocated_memory(p, size)
306 /// \macro LLVM_MEMORY_SANITIZER_BUILD
307 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
308 #if __has_feature(memory_sanitizer)
309 # define LLVM_MEMORY_SANITIZER_BUILD 1
311 # define LLVM_MEMORY_SANITIZER_BUILD 0