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 __GNUC_PREREQ
37 /// \brief Defines __GNUC_PREREQ if glibc's features.h isn't available.
39 # if defined(__GNUC__) && defined(__GNUC_MINOR__)
40 # define __GNUC_PREREQ(maj, min) \
41 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
43 # define __GNUC_PREREQ(maj, min) 0
47 /// \macro LLVM_MSC_PREREQ
48 /// \brief Is the compiler MSVC of at least the specified version?
49 /// The common \param version values to check for are:
50 /// * 1600: Microsoft Visual Studio 2010 / 10.0
51 /// * 1700: Microsoft Visual Studio 2012 / 11.0
52 /// * 1800: Microsoft Visual Studio 2013 / 12.0
54 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
56 #define LLVM_MSC_PREREQ(version) 0
59 /// \brief Does the compiler support r-value references?
60 /// This implies that <utility> provides the one-argument std::move; it
61 /// does not imply the existence of any other C++ library features.
62 #if __has_feature(cxx_rvalue_references) || \
63 defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1600)
64 #define LLVM_HAS_RVALUE_REFERENCES 1
66 #define LLVM_HAS_RVALUE_REFERENCES 0
69 /// \brief Does the compiler support r-value reference *this?
71 /// Sadly, this is separate from just r-value reference support because GCC
72 /// implemented everything but this thus far. No release of GCC yet has support
73 /// for this feature so it is enabled with Clang only.
74 /// FIXME: This should change to a version check when GCC grows support for it.
75 #if __has_feature(cxx_rvalue_references)
76 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
78 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
81 /// \macro LLVM_HAS_CXX11_TYPETRAITS
82 /// \brief Does the compiler have the C++11 type traits.
84 /// #include <type_traits>
87 /// * {true,false}_type
88 /// * is_constructible
90 #if defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1700)
91 #define LLVM_HAS_CXX11_TYPETRAITS 1
93 #define LLVM_HAS_CXX11_TYPETRAITS 0
96 /// \macro LLVM_HAS_CXX11_STDLIB
97 /// \brief Does the compiler have the C++11 standard library.
99 /// Implies LLVM_HAS_RVALUE_REFERENCES, LLVM_HAS_CXX11_TYPETRAITS
100 #if defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1700)
101 #define LLVM_HAS_CXX11_STDLIB 1
103 #define LLVM_HAS_CXX11_STDLIB 0
106 /// \macro LLVM_HAS_VARIADIC_TEMPLATES
107 /// \brief Does this compiler support variadic templates.
109 /// Implies LLVM_HAS_RVALUE_REFERENCES and the existence of std::forward.
110 #if __has_feature(cxx_variadic_templates) || LLVM_MSC_PREREQ(1800)
111 # define LLVM_HAS_VARIADIC_TEMPLATES 1
113 # define LLVM_HAS_VARIADIC_TEMPLATES 0
116 /// llvm_move - Expands to ::std::move if the compiler supports
117 /// r-value references; otherwise, expands to the argument.
118 #if LLVM_HAS_RVALUE_REFERENCES
119 #define llvm_move(value) (::std::move(value))
121 #define llvm_move(value) (value)
124 /// Expands to '&' if r-value references are supported.
126 /// This can be used to provide l-value/r-value overrides of member functions.
127 /// The r-value override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
128 #if LLVM_HAS_RVALUE_REFERENCE_THIS
129 #define LLVM_LVALUE_FUNCTION &
131 #define LLVM_LVALUE_FUNCTION
134 /// LLVM_DELETED_FUNCTION - Expands to = delete if the compiler supports it.
135 /// Use to mark functions as uncallable. Member functions with this should
136 /// be declared private so that some behavior is kept in C++03 mode.
140 /// DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
141 /// DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
145 #if __has_feature(cxx_deleted_functions) || \
146 defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
147 #define LLVM_DELETED_FUNCTION = delete
149 #define LLVM_DELETED_FUNCTION
152 /// LLVM_FINAL - Expands to 'final' if the compiler supports it.
153 /// Use to mark classes or virtual methods as final.
154 #if __has_feature(cxx_override_control) || \
155 (defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC_PREREQ(4, 7)) || \
156 LLVM_MSC_PREREQ(1700)
157 #define LLVM_FINAL final
162 /// LLVM_OVERRIDE - Expands to 'override' if the compiler supports it.
163 /// Use to mark virtual methods as overriding a base class method.
164 #if __has_feature(cxx_override_control) || \
165 (defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC_PREREQ(4, 7)) || \
166 LLVM_MSC_PREREQ(1700)
167 #define LLVM_OVERRIDE override
169 #define LLVM_OVERRIDE
172 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
173 # define LLVM_CONSTEXPR constexpr
175 # define LLVM_CONSTEXPR
178 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
179 /// into a shared library, then the class should be private to the library and
180 /// not accessible from outside it. Can also be used to mark variables and
181 /// functions, making them private to any shared library they are linked into.
182 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
183 #if (__has_attribute(visibility) || __GNUC_PREREQ(4, 0)) && \
184 !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
185 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
187 #define LLVM_LIBRARY_VISIBILITY
190 #if __has_attribute(used) || __GNUC_PREREQ(3, 1)
191 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
193 #define LLVM_ATTRIBUTE_USED
196 #if __has_attribute(warn_unused_result) || __GNUC_PREREQ(3, 4)
197 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
199 #define LLVM_ATTRIBUTE_UNUSED_RESULT
202 // Some compilers warn about unused functions. When a function is sometimes
203 // used or not depending on build settings (e.g. a function only called from
204 // within "assert"), this attribute can be used to suppress such warnings.
206 // However, it shouldn't be used for unused *variables*, as those have a much
207 // more portable solution:
208 // (void)unused_var_name;
209 // Prefer cast-to-void wherever it is sufficient.
210 #if __has_attribute(unused) || __GNUC_PREREQ(3, 1)
211 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
213 #define LLVM_ATTRIBUTE_UNUSED
216 // FIXME: Provide this for PE/COFF targets.
217 #if (__has_attribute(weak) || __GNUC_PREREQ(4, 0)) && \
218 (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
219 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
221 #define LLVM_ATTRIBUTE_WEAK
224 // Prior to clang 3.2, clang did not accept any spelling of
225 // __has_attribute(const), so assume it is supported.
226 #if defined(__clang__) || defined(__GNUC__)
227 // aka 'CONST' but following LLVM Conventions.
228 #define LLVM_READNONE __attribute__((__const__))
230 #define LLVM_READNONE
233 #if __has_attribute(pure) || defined(__GNUC__)
234 // aka 'PURE' but following LLVM Conventions.
235 #define LLVM_READONLY __attribute__((__pure__))
237 #define LLVM_READONLY
240 #if __has_builtin(__builtin_expect) || __GNUC_PREREQ(4, 0)
241 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
242 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
244 #define LLVM_LIKELY(EXPR) (EXPR)
245 #define LLVM_UNLIKELY(EXPR) (EXPR)
248 // C++ doesn't support 'extern template' of template specializations. GCC does,
249 // but requires __extension__ before it. In the header, use this:
250 // EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
251 // in the .cpp file, use this:
252 // TEMPLATE_INSTANTIATION(class foo<bar>);
254 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
255 #define TEMPLATE_INSTANTIATION(X) template X
257 #define EXTERN_TEMPLATE_INSTANTIATION(X)
258 #define TEMPLATE_INSTANTIATION(X)
261 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
262 /// mark a method "not for inlining".
263 #if __has_attribute(noinline) || __GNUC_PREREQ(3, 4)
264 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
265 #elif defined(_MSC_VER)
266 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
268 #define LLVM_ATTRIBUTE_NOINLINE
271 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
272 /// so, mark a method "always inline" because it is performance sensitive. GCC
273 /// 3.4 supported this but is buggy in various cases and produces unimplemented
274 /// errors, just use it in GCC 4.0 and later.
275 #if __has_attribute(always_inline) || __GNUC_PREREQ(4, 0)
276 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
277 #elif defined(_MSC_VER)
278 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
280 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
284 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
285 #elif defined(_MSC_VER)
286 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
288 #define LLVM_ATTRIBUTE_NORETURN
291 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
292 /// pedantic diagnostics.
294 #define LLVM_EXTENSION __extension__
296 #define LLVM_EXTENSION
299 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
300 #if __has_feature(attribute_deprecated_with_message)
301 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
302 decl __attribute__((deprecated(message)))
303 #elif defined(__GNUC__)
304 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
305 decl __attribute__((deprecated))
306 #elif defined(_MSC_VER)
307 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
308 __declspec(deprecated(message)) decl
310 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
314 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
315 /// to an expression which states that it is undefined behavior for the
316 /// compiler to reach this point. Otherwise is not defined.
317 #if __has_builtin(__builtin_unreachable) || __GNUC_PREREQ(4, 5)
318 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
319 #elif defined(_MSC_VER)
320 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
323 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
324 /// which causes the program to exit abnormally.
325 #if __has_builtin(__builtin_trap) || __GNUC_PREREQ(4, 3)
326 # define LLVM_BUILTIN_TRAP __builtin_trap()
328 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
331 /// \macro LLVM_ASSUME_ALIGNED
332 /// \brief Returns a pointer with an assumed alignment.
333 #if __has_builtin(__builtin_assume_aligned) && __GNUC_PREREQ(4, 7)
334 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
335 #elif defined(LLVM_BUILTIN_UNREACHABLE)
336 // As of today, clang does not support __builtin_assume_aligned.
337 # define LLVM_ASSUME_ALIGNED(p, a) \
338 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
340 # define LLVM_ASSUME_ALIGNED(p, a) (p)
343 /// \macro LLVM_FUNCTION_NAME
344 /// \brief Expands to __func__ on compilers which support it. Otherwise,
345 /// expands to a compiler-dependent replacement.
346 #if defined(_MSC_VER)
347 # define LLVM_FUNCTION_NAME __FUNCTION__
349 # define LLVM_FUNCTION_NAME __func__
352 /// \macro LLVM_MEMORY_SANITIZER_BUILD
353 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
354 #if __has_feature(memory_sanitizer)
355 # define LLVM_MEMORY_SANITIZER_BUILD 1
356 # include <sanitizer/msan_interface.h>
358 # define LLVM_MEMORY_SANITIZER_BUILD 0
359 # define __msan_allocated_memory(p, size)
360 # define __msan_unpoison(p, size)
363 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
364 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
365 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
366 # define LLVM_ADDRESS_SANITIZER_BUILD 1
368 # define LLVM_ADDRESS_SANITIZER_BUILD 0
371 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
372 /// \brief Is unaligned memory access fast on the host machine.
374 /// Don't specialize on alignment for platforms where unaligned memory accesses
375 /// generates the same code as aligned memory accesses for common types.
376 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
377 defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
378 defined(_X86_) || defined(__i386) || defined(__i386__)
379 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
381 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
384 /// \macro LLVM_EXPLICIT
385 /// \brief Expands to explicit on compilers which support explicit conversion
386 /// operators. Otherwise expands to nothing.
387 #if __has_feature(cxx_explicit_conversions) || \
388 defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
389 #define LLVM_EXPLICIT explicit
391 #define LLVM_EXPLICIT
394 /// \macro LLVM_STATIC_ASSERT
395 /// \brief Expands to C/C++'s static_assert on compilers which support it.
396 #if __has_feature(cxx_static_assert) || \
397 defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1600)
398 # define LLVM_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
399 #elif __has_feature(c_static_assert)
400 # define LLVM_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
401 #elif __has_extension(c_static_assert)
402 # define LLVM_STATIC_ASSERT(expr, msg) LLVM_EXTENSION _Static_assert(expr, msg)
404 # define LLVM_STATIC_ASSERT(expr, msg)
407 /// \macro LLVM_ENUM_INT_TYPE
408 /// \brief Expands to colon followed by the given integral type on compilers
409 /// which support C++11 strong enums. This can be used to make enums unsigned
411 #if __has_feature(cxx_strong_enums) || LLVM_MSC_PREREQ(1600)
412 # define LLVM_ENUM_INT_TYPE(intty) : intty
414 # define LLVM_ENUM_INT_TYPE(intty)
417 /// \brief Does the compiler support C++11 semantics for strongly typed forward
419 #if __has_feature(cxx_strong_enums) || LLVM_MSC_PREREQ(1700)
420 #define LLVM_HAS_STRONG_ENUMS 1
422 #define LLVM_HAS_STRONG_ENUMS 0
425 /// \brief Does the compiler support generalized initializers (using braced
426 /// lists and std::initializer_list). While clang may claim it supports general
427 /// initializers, if we're using MSVC's headers, we might not have a usable
428 /// std::initializer list type from the STL. Disable this for now.
429 #if __has_feature(cxx_generalized_initializers) && !defined(_MSC_VER)
430 #define LLVM_HAS_INITIALIZER_LISTS 1
432 #define LLVM_HAS_INITIALIZER_LISTS 0
435 /// \brief Mark debug helper function definitions like dump() that should not be
436 /// stripped from debug builds.
437 // FIXME: Move this to a private config.h as it's not usable in public headers.
438 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
439 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
441 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE