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 reference *this?
61 /// Sadly, this is separate from just r-value reference support because GCC
62 /// implemented everything but this thus far. No release of GCC yet has support
63 /// for this feature so it is enabled with Clang only.
64 /// FIXME: This should change to a version check when GCC grows support for it.
65 #if __has_feature(cxx_rvalue_references)
66 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
68 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
71 /// \macro LLVM_HAS_VARIADIC_TEMPLATES
72 /// \brief Does this compiler support variadic templates.
74 /// Implies LLVM_HAS_RVALUE_REFERENCES and the existence of std::forward.
75 #if __has_feature(cxx_variadic_templates) || LLVM_MSC_PREREQ(1800)
76 # define LLVM_HAS_VARIADIC_TEMPLATES 1
78 # define LLVM_HAS_VARIADIC_TEMPLATES 0
81 /// Expands to '&' if r-value references are supported.
83 /// This can be used to provide l-value/r-value overrides of member functions.
84 /// The r-value override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
85 #if LLVM_HAS_RVALUE_REFERENCE_THIS
86 #define LLVM_LVALUE_FUNCTION &
88 #define LLVM_LVALUE_FUNCTION
91 /// LLVM_DELETED_FUNCTION - Expands to = delete if the compiler supports it.
92 /// Use to mark functions as uncallable. Member functions with this should
93 /// be declared private so that some behavior is kept in C++03 mode.
97 /// DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
98 /// DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
102 #if __has_feature(cxx_deleted_functions) || \
103 defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
104 #define LLVM_DELETED_FUNCTION = delete
106 #define LLVM_DELETED_FUNCTION
109 /// LLVM_FINAL - Expands to 'final' if the compiler supports it.
110 /// Use to mark classes or virtual methods as final.
111 #if __has_feature(cxx_override_control) || \
112 (defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC_PREREQ(4, 7)) || \
113 LLVM_MSC_PREREQ(1700)
114 #define LLVM_FINAL final
119 /// LLVM_OVERRIDE - Expands to 'override' if the compiler supports it.
120 /// Use to mark virtual methods as overriding a base class method.
121 #if __has_feature(cxx_override_control) || \
122 (defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC_PREREQ(4, 7)) || \
123 LLVM_MSC_PREREQ(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 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
140 #if (__has_attribute(visibility) || __GNUC_PREREQ(4, 0)) && \
141 !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
142 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
144 #define LLVM_LIBRARY_VISIBILITY
147 #if __has_attribute(used) || __GNUC_PREREQ(3, 1)
148 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
150 #define LLVM_ATTRIBUTE_USED
153 #if __has_attribute(warn_unused_result) || __GNUC_PREREQ(3, 4)
154 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
156 #define LLVM_ATTRIBUTE_UNUSED_RESULT
159 // Some compilers warn about unused functions. When a function is sometimes
160 // used or not depending on build settings (e.g. a function only called from
161 // within "assert"), this attribute can be used to suppress such warnings.
163 // However, it shouldn't be used for unused *variables*, as those have a much
164 // more portable solution:
165 // (void)unused_var_name;
166 // Prefer cast-to-void wherever it is sufficient.
167 #if __has_attribute(unused) || __GNUC_PREREQ(3, 1)
168 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
170 #define LLVM_ATTRIBUTE_UNUSED
173 // FIXME: Provide this for PE/COFF targets.
174 #if (__has_attribute(weak) || __GNUC_PREREQ(4, 0)) && \
175 (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
176 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
178 #define LLVM_ATTRIBUTE_WEAK
181 // Prior to clang 3.2, clang did not accept any spelling of
182 // __has_attribute(const), so assume it is supported.
183 #if defined(__clang__) || defined(__GNUC__)
184 // aka 'CONST' but following LLVM Conventions.
185 #define LLVM_READNONE __attribute__((__const__))
187 #define LLVM_READNONE
190 #if __has_attribute(pure) || defined(__GNUC__)
191 // aka 'PURE' but following LLVM Conventions.
192 #define LLVM_READONLY __attribute__((__pure__))
194 #define LLVM_READONLY
197 #if __has_builtin(__builtin_expect) || __GNUC_PREREQ(4, 0)
198 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
199 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
201 #define LLVM_LIKELY(EXPR) (EXPR)
202 #define LLVM_UNLIKELY(EXPR) (EXPR)
205 // C++ doesn't support 'extern template' of template specializations. GCC does,
206 // but requires __extension__ before it. In the header, use this:
207 // EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
208 // in the .cpp file, use this:
209 // TEMPLATE_INSTANTIATION(class foo<bar>);
211 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
212 #define TEMPLATE_INSTANTIATION(X) template X
214 #define EXTERN_TEMPLATE_INSTANTIATION(X)
215 #define TEMPLATE_INSTANTIATION(X)
218 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
219 /// mark a method "not for inlining".
220 #if __has_attribute(noinline) || __GNUC_PREREQ(3, 4)
221 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
222 #elif defined(_MSC_VER)
223 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
225 #define LLVM_ATTRIBUTE_NOINLINE
228 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
229 /// so, mark a method "always inline" because it is performance sensitive. GCC
230 /// 3.4 supported this but is buggy in various cases and produces unimplemented
231 /// errors, just use it in GCC 4.0 and later.
232 #if __has_attribute(always_inline) || __GNUC_PREREQ(4, 0)
233 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
234 #elif defined(_MSC_VER)
235 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
237 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
241 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
242 #elif defined(_MSC_VER)
243 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
245 #define LLVM_ATTRIBUTE_NORETURN
248 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
249 /// pedantic diagnostics.
251 #define LLVM_EXTENSION __extension__
253 #define LLVM_EXTENSION
256 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
257 #if __has_feature(attribute_deprecated_with_message)
258 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
259 decl __attribute__((deprecated(message)))
260 #elif defined(__GNUC__)
261 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
262 decl __attribute__((deprecated))
263 #elif defined(_MSC_VER)
264 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
265 __declspec(deprecated(message)) decl
267 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
271 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
272 /// to an expression which states that it is undefined behavior for the
273 /// compiler to reach this point. Otherwise is not defined.
274 #if __has_builtin(__builtin_unreachable) || __GNUC_PREREQ(4, 5)
275 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
276 #elif defined(_MSC_VER)
277 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
280 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
281 /// which causes the program to exit abnormally.
282 #if __has_builtin(__builtin_trap) || __GNUC_PREREQ(4, 3)
283 # define LLVM_BUILTIN_TRAP __builtin_trap()
285 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
288 /// \macro LLVM_ASSUME_ALIGNED
289 /// \brief Returns a pointer with an assumed alignment.
290 #if __has_builtin(__builtin_assume_aligned) && __GNUC_PREREQ(4, 7)
291 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
292 #elif defined(LLVM_BUILTIN_UNREACHABLE)
293 // As of today, clang does not support __builtin_assume_aligned.
294 # define LLVM_ASSUME_ALIGNED(p, a) \
295 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
297 # define LLVM_ASSUME_ALIGNED(p, a) (p)
300 /// \macro LLVM_FUNCTION_NAME
301 /// \brief Expands to __func__ on compilers which support it. Otherwise,
302 /// expands to a compiler-dependent replacement.
303 #if defined(_MSC_VER)
304 # define LLVM_FUNCTION_NAME __FUNCTION__
306 # define LLVM_FUNCTION_NAME __func__
309 /// \macro LLVM_MEMORY_SANITIZER_BUILD
310 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
311 #if __has_feature(memory_sanitizer)
312 # define LLVM_MEMORY_SANITIZER_BUILD 1
313 # include <sanitizer/msan_interface.h>
315 # define LLVM_MEMORY_SANITIZER_BUILD 0
316 # define __msan_allocated_memory(p, size)
317 # define __msan_unpoison(p, size)
320 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
321 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
322 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
323 # define LLVM_ADDRESS_SANITIZER_BUILD 1
325 # define LLVM_ADDRESS_SANITIZER_BUILD 0
328 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
329 /// \brief Is unaligned memory access fast on the host machine.
331 /// Don't specialize on alignment for platforms where unaligned memory accesses
332 /// generates the same code as aligned memory accesses for common types.
333 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
334 defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
335 defined(_X86_) || defined(__i386) || defined(__i386__)
336 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
338 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
341 /// \macro LLVM_EXPLICIT
342 /// \brief Expands to explicit on compilers which support explicit conversion
343 /// operators. Otherwise expands to nothing.
344 #if __has_feature(cxx_explicit_conversions) || \
345 defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
346 #define LLVM_EXPLICIT explicit
348 #define LLVM_EXPLICIT
351 /// \macro LLVM_STATIC_ASSERT
352 /// \brief Expands to C/C++'s static_assert on compilers which support it.
353 #if __has_feature(cxx_static_assert) || \
354 defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1600)
355 # define LLVM_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
356 #elif __has_feature(c_static_assert)
357 # define LLVM_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
358 #elif __has_extension(c_static_assert)
359 # define LLVM_STATIC_ASSERT(expr, msg) LLVM_EXTENSION _Static_assert(expr, msg)
361 # define LLVM_STATIC_ASSERT(expr, msg)
364 /// \brief Does the compiler support generalized initializers (using braced
365 /// lists and std::initializer_list). While clang may claim it supports general
366 /// initializers, if we're using MSVC's headers, we might not have a usable
367 /// std::initializer list type from the STL. Disable this for now.
368 #if __has_feature(cxx_generalized_initializers) && !defined(_MSC_VER)
369 #define LLVM_HAS_INITIALIZER_LISTS 1
371 #define LLVM_HAS_INITIALIZER_LISTS 0
374 /// \brief Mark debug helper function definitions like dump() that should not be
375 /// stripped from debug builds.
376 // FIXME: Move this to a private config.h as it's not usable in public headers.
377 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
378 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
380 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE