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 We require the host compiler to support r-value references.
60 #define LLVM_HAS_RVALUE_REFERENCES 1
62 /// \brief Does the compiler support r-value reference *this?
64 /// Sadly, this is separate from just r-value reference support because GCC
65 /// implemented everything but this thus far. No release of GCC yet has support
66 /// for this feature so it is enabled with Clang only.
67 /// FIXME: This should change to a version check when GCC grows support for it.
68 #if __has_feature(cxx_rvalue_references)
69 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
71 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
74 /// \macro LLVM_HAS_CXX11_TYPETRAITS
75 /// \brief Does the compiler have the C++11 type traits.
77 /// #include <type_traits>
80 /// * {true,false}_type
81 /// * is_constructible
83 #define LLVM_HAS_CXX11_TYPETRAITS 1
85 /// \macro LLVM_HAS_CXX11_STDLIB
86 /// \brief Does the compiler have the C++11 standard library.
88 /// Implies LLVM_HAS_RVALUE_REFERENCES, LLVM_HAS_CXX11_TYPETRAITS
89 #define LLVM_HAS_CXX11_STDLIB 1
91 /// \macro LLVM_HAS_VARIADIC_TEMPLATES
92 /// \brief Does this compiler support variadic templates.
94 /// Implies LLVM_HAS_RVALUE_REFERENCES and the existence of std::forward.
95 #if __has_feature(cxx_variadic_templates) || LLVM_MSC_PREREQ(1800)
96 # define LLVM_HAS_VARIADIC_TEMPLATES 1
98 # define LLVM_HAS_VARIADIC_TEMPLATES 0
101 /// llvm_move - Expands to ::std::move if the compiler supports
102 /// r-value references; otherwise, expands to the argument.
103 #if LLVM_HAS_RVALUE_REFERENCES
104 #define llvm_move(value) (::std::move(value))
106 #define llvm_move(value) (value)
109 /// Expands to '&' if r-value references are supported.
111 /// This can be used to provide l-value/r-value overrides of member functions.
112 /// The r-value override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
113 #if LLVM_HAS_RVALUE_REFERENCE_THIS
114 #define LLVM_LVALUE_FUNCTION &
116 #define LLVM_LVALUE_FUNCTION
119 /// LLVM_DELETED_FUNCTION - Expands to = delete if the compiler supports it.
120 /// Use to mark functions as uncallable. Member functions with this should
121 /// be declared private so that some behavior is kept in C++03 mode.
125 /// DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
126 /// DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
130 #if __has_feature(cxx_deleted_functions) || \
131 defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
132 #define LLVM_DELETED_FUNCTION = delete
134 #define LLVM_DELETED_FUNCTION
137 /// LLVM_FINAL - Expands to 'final' if the compiler supports it.
138 /// Use to mark classes or virtual methods as final.
139 #if __has_feature(cxx_override_control) || \
140 (defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC_PREREQ(4, 7)) || \
141 LLVM_MSC_PREREQ(1700)
142 #define LLVM_FINAL final
147 /// LLVM_OVERRIDE - Expands to 'override' if the compiler supports it.
148 /// Use to mark virtual methods as overriding a base class method.
149 #if __has_feature(cxx_override_control) || \
150 (defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC_PREREQ(4, 7)) || \
151 LLVM_MSC_PREREQ(1700)
152 #define LLVM_OVERRIDE override
154 #define LLVM_OVERRIDE
157 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
158 # define LLVM_CONSTEXPR constexpr
160 # define LLVM_CONSTEXPR
163 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
164 /// into a shared library, then the class should be private to the library and
165 /// not accessible from outside it. Can also be used to mark variables and
166 /// functions, making them private to any shared library they are linked into.
167 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
168 #if (__has_attribute(visibility) || __GNUC_PREREQ(4, 0)) && \
169 !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
170 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
172 #define LLVM_LIBRARY_VISIBILITY
175 #if __has_attribute(used) || __GNUC_PREREQ(3, 1)
176 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
178 #define LLVM_ATTRIBUTE_USED
181 #if __has_attribute(warn_unused_result) || __GNUC_PREREQ(3, 4)
182 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
184 #define LLVM_ATTRIBUTE_UNUSED_RESULT
187 // Some compilers warn about unused functions. When a function is sometimes
188 // used or not depending on build settings (e.g. a function only called from
189 // within "assert"), this attribute can be used to suppress such warnings.
191 // However, it shouldn't be used for unused *variables*, as those have a much
192 // more portable solution:
193 // (void)unused_var_name;
194 // Prefer cast-to-void wherever it is sufficient.
195 #if __has_attribute(unused) || __GNUC_PREREQ(3, 1)
196 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
198 #define LLVM_ATTRIBUTE_UNUSED
201 // FIXME: Provide this for PE/COFF targets.
202 #if (__has_attribute(weak) || __GNUC_PREREQ(4, 0)) && \
203 (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
204 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
206 #define LLVM_ATTRIBUTE_WEAK
209 // Prior to clang 3.2, clang did not accept any spelling of
210 // __has_attribute(const), so assume it is supported.
211 #if defined(__clang__) || defined(__GNUC__)
212 // aka 'CONST' but following LLVM Conventions.
213 #define LLVM_READNONE __attribute__((__const__))
215 #define LLVM_READNONE
218 #if __has_attribute(pure) || defined(__GNUC__)
219 // aka 'PURE' but following LLVM Conventions.
220 #define LLVM_READONLY __attribute__((__pure__))
222 #define LLVM_READONLY
225 #if __has_builtin(__builtin_expect) || __GNUC_PREREQ(4, 0)
226 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
227 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
229 #define LLVM_LIKELY(EXPR) (EXPR)
230 #define LLVM_UNLIKELY(EXPR) (EXPR)
233 // C++ doesn't support 'extern template' of template specializations. GCC does,
234 // but requires __extension__ before it. In the header, use this:
235 // EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
236 // in the .cpp file, use this:
237 // TEMPLATE_INSTANTIATION(class foo<bar>);
239 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
240 #define TEMPLATE_INSTANTIATION(X) template X
242 #define EXTERN_TEMPLATE_INSTANTIATION(X)
243 #define TEMPLATE_INSTANTIATION(X)
246 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
247 /// mark a method "not for inlining".
248 #if __has_attribute(noinline) || __GNUC_PREREQ(3, 4)
249 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
250 #elif defined(_MSC_VER)
251 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
253 #define LLVM_ATTRIBUTE_NOINLINE
256 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
257 /// so, mark a method "always inline" because it is performance sensitive. GCC
258 /// 3.4 supported this but is buggy in various cases and produces unimplemented
259 /// errors, just use it in GCC 4.0 and later.
260 #if __has_attribute(always_inline) || __GNUC_PREREQ(4, 0)
261 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
262 #elif defined(_MSC_VER)
263 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
265 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
269 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
270 #elif defined(_MSC_VER)
271 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
273 #define LLVM_ATTRIBUTE_NORETURN
276 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
277 /// pedantic diagnostics.
279 #define LLVM_EXTENSION __extension__
281 #define LLVM_EXTENSION
284 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
285 #if __has_feature(attribute_deprecated_with_message)
286 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
287 decl __attribute__((deprecated(message)))
288 #elif defined(__GNUC__)
289 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
290 decl __attribute__((deprecated))
291 #elif defined(_MSC_VER)
292 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
293 __declspec(deprecated(message)) decl
295 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
299 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
300 /// to an expression which states that it is undefined behavior for the
301 /// compiler to reach this point. Otherwise is not defined.
302 #if __has_builtin(__builtin_unreachable) || __GNUC_PREREQ(4, 5)
303 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
304 #elif defined(_MSC_VER)
305 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
308 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
309 /// which causes the program to exit abnormally.
310 #if __has_builtin(__builtin_trap) || __GNUC_PREREQ(4, 3)
311 # define LLVM_BUILTIN_TRAP __builtin_trap()
313 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
316 /// \macro LLVM_ASSUME_ALIGNED
317 /// \brief Returns a pointer with an assumed alignment.
318 #if __has_builtin(__builtin_assume_aligned) && __GNUC_PREREQ(4, 7)
319 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
320 #elif defined(LLVM_BUILTIN_UNREACHABLE)
321 // As of today, clang does not support __builtin_assume_aligned.
322 # define LLVM_ASSUME_ALIGNED(p, a) \
323 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
325 # define LLVM_ASSUME_ALIGNED(p, a) (p)
328 /// \macro LLVM_FUNCTION_NAME
329 /// \brief Expands to __func__ on compilers which support it. Otherwise,
330 /// expands to a compiler-dependent replacement.
331 #if defined(_MSC_VER)
332 # define LLVM_FUNCTION_NAME __FUNCTION__
334 # define LLVM_FUNCTION_NAME __func__
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
341 # include <sanitizer/msan_interface.h>
343 # define LLVM_MEMORY_SANITIZER_BUILD 0
344 # define __msan_allocated_memory(p, size)
345 # define __msan_unpoison(p, size)
348 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
349 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
350 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
351 # define LLVM_ADDRESS_SANITIZER_BUILD 1
353 # define LLVM_ADDRESS_SANITIZER_BUILD 0
356 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
357 /// \brief Is unaligned memory access fast on the host machine.
359 /// Don't specialize on alignment for platforms where unaligned memory accesses
360 /// generates the same code as aligned memory accesses for common types.
361 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
362 defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
363 defined(_X86_) || defined(__i386) || defined(__i386__)
364 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
366 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
369 /// \macro LLVM_EXPLICIT
370 /// \brief Expands to explicit on compilers which support explicit conversion
371 /// operators. Otherwise expands to nothing.
372 #if __has_feature(cxx_explicit_conversions) || \
373 defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
374 #define LLVM_EXPLICIT explicit
376 #define LLVM_EXPLICIT
379 /// \macro LLVM_STATIC_ASSERT
380 /// \brief Expands to C/C++'s static_assert on compilers which support it.
381 #if __has_feature(cxx_static_assert) || \
382 defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1600)
383 # define LLVM_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
384 #elif __has_feature(c_static_assert)
385 # define LLVM_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
386 #elif __has_extension(c_static_assert)
387 # define LLVM_STATIC_ASSERT(expr, msg) LLVM_EXTENSION _Static_assert(expr, msg)
389 # define LLVM_STATIC_ASSERT(expr, msg)
392 /// \macro LLVM_ENUM_INT_TYPE
393 /// \brief Expands to colon followed by the given integral type on compilers
394 /// which support C++11 strong enums. This can be used to make enums unsigned
396 #if __has_feature(cxx_strong_enums) || LLVM_MSC_PREREQ(1600)
397 # define LLVM_ENUM_INT_TYPE(intty) : intty
399 # define LLVM_ENUM_INT_TYPE(intty)
402 /// \brief Does the compiler support C++11 semantics for strongly typed forward
404 #if __has_feature(cxx_strong_enums) || LLVM_MSC_PREREQ(1700)
405 #define LLVM_HAS_STRONG_ENUMS 1
407 #define LLVM_HAS_STRONG_ENUMS 0
410 /// \brief Does the compiler support generalized initializers (using braced
411 /// lists and std::initializer_list). While clang may claim it supports general
412 /// initializers, if we're using MSVC's headers, we might not have a usable
413 /// std::initializer list type from the STL. Disable this for now.
414 #if __has_feature(cxx_generalized_initializers) && !defined(_MSC_VER)
415 #define LLVM_HAS_INITIALIZER_LISTS 1
417 #define LLVM_HAS_INITIALIZER_LISTS 0
420 /// \brief Mark debug helper function definitions like dump() that should not be
421 /// stripped from debug builds.
422 // FIXME: Move this to a private config.h as it's not usable in public headers.
423 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
424 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
426 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE