Switch all uses of LLVM_FINAL to just use 'final', and remove the macro.
[oota-llvm.git] / include / llvm / Support / Compiler.h
1 //===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_COMPILER_H
16 #define LLVM_SUPPORT_COMPILER_H
17
18 #include "llvm/Config/llvm-config.h"
19
20 #ifndef __has_feature
21 # define __has_feature(x) 0
22 #endif
23
24 #ifndef __has_extension
25 # define __has_extension(x) 0
26 #endif
27
28 #ifndef __has_attribute
29 # define __has_attribute(x) 0
30 #endif
31
32 #ifndef __has_builtin
33 # define __has_builtin(x) 0
34 #endif
35
36 /// \macro __GNUC_PREREQ
37 /// \brief Defines __GNUC_PREREQ if glibc's features.h isn't available.
38 #ifndef __GNUC_PREREQ
39 # if defined(__GNUC__) && defined(__GNUC_MINOR__)
40 #  define __GNUC_PREREQ(maj, min) \
41     ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
42 # else
43 #  define __GNUC_PREREQ(maj, min) 0
44 # endif
45 #endif
46
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
53 #ifdef _MSC_VER
54 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
55 #else
56 #define LLVM_MSC_PREREQ(version) 0
57 #endif
58
59 /// \brief Does the compiler support r-value reference *this?
60 ///
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
67 #else
68 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
69 #endif
70
71 /// \macro LLVM_HAS_VARIADIC_TEMPLATES
72 /// \brief Does this compiler support variadic templates.
73 ///
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
77 #else
78 # define LLVM_HAS_VARIADIC_TEMPLATES 0
79 #endif
80
81 /// Expands to '&' if r-value references are supported.
82 ///
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 &
87 #else
88 #define LLVM_LVALUE_FUNCTION
89 #endif
90
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.
94 ///
95 /// class DontCopy {
96 /// private:
97 ///   DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
98 ///   DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
99 /// public:
100 ///   ...
101 /// };
102 #if __has_feature(cxx_deleted_functions) || \
103     defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
104 #define LLVM_DELETED_FUNCTION = delete
105 #else
106 #define LLVM_DELETED_FUNCTION
107 #endif
108
109 /// LLVM_OVERRIDE - Expands to 'override' if the compiler supports it.
110 /// Use to mark virtual methods as overriding a base class method.
111 #if __has_feature(cxx_override_control) || \
112     (defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC_PREREQ(4, 7)) || \
113     LLVM_MSC_PREREQ(1700)
114 #define LLVM_OVERRIDE override
115 #else
116 #define LLVM_OVERRIDE
117 #endif
118
119 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
120 # define LLVM_CONSTEXPR constexpr
121 #else
122 # define LLVM_CONSTEXPR
123 #endif
124
125 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
126 /// into a shared library, then the class should be private to the library and
127 /// not accessible from outside it.  Can also be used to mark variables and
128 /// functions, making them private to any shared library they are linked into.
129 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
130 #if (__has_attribute(visibility) || __GNUC_PREREQ(4, 0)) &&                    \
131     !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
132 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
133 #else
134 #define LLVM_LIBRARY_VISIBILITY
135 #endif
136
137 #if __has_attribute(used) || __GNUC_PREREQ(3, 1)
138 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
139 #else
140 #define LLVM_ATTRIBUTE_USED
141 #endif
142
143 #if __has_attribute(warn_unused_result) || __GNUC_PREREQ(3, 4)
144 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
145 #else
146 #define LLVM_ATTRIBUTE_UNUSED_RESULT
147 #endif
148
149 // Some compilers warn about unused functions. When a function is sometimes
150 // used or not depending on build settings (e.g. a function only called from
151 // within "assert"), this attribute can be used to suppress such warnings.
152 //
153 // However, it shouldn't be used for unused *variables*, as those have a much
154 // more portable solution:
155 //   (void)unused_var_name;
156 // Prefer cast-to-void wherever it is sufficient.
157 #if __has_attribute(unused) || __GNUC_PREREQ(3, 1)
158 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
159 #else
160 #define LLVM_ATTRIBUTE_UNUSED
161 #endif
162
163 // FIXME: Provide this for PE/COFF targets.
164 #if (__has_attribute(weak) || __GNUC_PREREQ(4, 0)) &&                          \
165     (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
166 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
167 #else
168 #define LLVM_ATTRIBUTE_WEAK
169 #endif
170
171 // Prior to clang 3.2, clang did not accept any spelling of
172 // __has_attribute(const), so assume it is supported.
173 #if defined(__clang__) || defined(__GNUC__)
174 // aka 'CONST' but following LLVM Conventions.
175 #define LLVM_READNONE __attribute__((__const__))
176 #else
177 #define LLVM_READNONE
178 #endif
179
180 #if __has_attribute(pure) || defined(__GNUC__)
181 // aka 'PURE' but following LLVM Conventions.
182 #define LLVM_READONLY __attribute__((__pure__))
183 #else
184 #define LLVM_READONLY
185 #endif
186
187 #if __has_builtin(__builtin_expect) || __GNUC_PREREQ(4, 0)
188 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
189 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
190 #else
191 #define LLVM_LIKELY(EXPR) (EXPR)
192 #define LLVM_UNLIKELY(EXPR) (EXPR)
193 #endif
194
195 // C++ doesn't support 'extern template' of template specializations.  GCC does,
196 // but requires __extension__ before it.  In the header, use this:
197 //   EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
198 // in the .cpp file, use this:
199 //   TEMPLATE_INSTANTIATION(class foo<bar>);
200 #ifdef __GNUC__
201 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
202 #define TEMPLATE_INSTANTIATION(X) template X
203 #else
204 #define EXTERN_TEMPLATE_INSTANTIATION(X)
205 #define TEMPLATE_INSTANTIATION(X)
206 #endif
207
208 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
209 /// mark a method "not for inlining".
210 #if __has_attribute(noinline) || __GNUC_PREREQ(3, 4)
211 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
212 #elif defined(_MSC_VER)
213 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
214 #else
215 #define LLVM_ATTRIBUTE_NOINLINE
216 #endif
217
218 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
219 /// so, mark a method "always inline" because it is performance sensitive. GCC
220 /// 3.4 supported this but is buggy in various cases and produces unimplemented
221 /// errors, just use it in GCC 4.0 and later.
222 #if __has_attribute(always_inline) || __GNUC_PREREQ(4, 0)
223 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
224 #elif defined(_MSC_VER)
225 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
226 #else
227 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
228 #endif
229
230 #ifdef __GNUC__
231 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
232 #elif defined(_MSC_VER)
233 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
234 #else
235 #define LLVM_ATTRIBUTE_NORETURN
236 #endif
237
238 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
239 /// pedantic diagnostics.
240 #ifdef __GNUC__
241 #define LLVM_EXTENSION __extension__
242 #else
243 #define LLVM_EXTENSION
244 #endif
245
246 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
247 #if __has_feature(attribute_deprecated_with_message)
248 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
249   decl __attribute__((deprecated(message)))
250 #elif defined(__GNUC__)
251 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
252   decl __attribute__((deprecated))
253 #elif defined(_MSC_VER)
254 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
255   __declspec(deprecated(message)) decl
256 #else
257 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
258   decl
259 #endif
260
261 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
262 /// to an expression which states that it is undefined behavior for the
263 /// compiler to reach this point.  Otherwise is not defined.
264 #if __has_builtin(__builtin_unreachable) || __GNUC_PREREQ(4, 5)
265 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
266 #elif defined(_MSC_VER)
267 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
268 #endif
269
270 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
271 /// which causes the program to exit abnormally.
272 #if __has_builtin(__builtin_trap) || __GNUC_PREREQ(4, 3)
273 # define LLVM_BUILTIN_TRAP __builtin_trap()
274 #else
275 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
276 #endif
277
278 /// \macro LLVM_ASSUME_ALIGNED
279 /// \brief Returns a pointer with an assumed alignment.
280 #if __has_builtin(__builtin_assume_aligned) && __GNUC_PREREQ(4, 7)
281 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
282 #elif defined(LLVM_BUILTIN_UNREACHABLE)
283 // As of today, clang does not support __builtin_assume_aligned.
284 # define LLVM_ASSUME_ALIGNED(p, a) \
285            (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
286 #else
287 # define LLVM_ASSUME_ALIGNED(p, a) (p)
288 #endif
289
290 /// \macro LLVM_FUNCTION_NAME
291 /// \brief Expands to __func__ on compilers which support it.  Otherwise,
292 /// expands to a compiler-dependent replacement.
293 #if defined(_MSC_VER)
294 # define LLVM_FUNCTION_NAME __FUNCTION__
295 #else
296 # define LLVM_FUNCTION_NAME __func__
297 #endif
298
299 /// \macro LLVM_MEMORY_SANITIZER_BUILD
300 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
301 #if __has_feature(memory_sanitizer)
302 # define LLVM_MEMORY_SANITIZER_BUILD 1
303 # include <sanitizer/msan_interface.h>
304 #else
305 # define LLVM_MEMORY_SANITIZER_BUILD 0
306 # define __msan_allocated_memory(p, size)
307 # define __msan_unpoison(p, size)
308 #endif
309
310 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
311 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
312 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
313 # define LLVM_ADDRESS_SANITIZER_BUILD 1
314 #else
315 # define LLVM_ADDRESS_SANITIZER_BUILD 0
316 #endif
317
318 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
319 /// \brief Is unaligned memory access fast on the host machine.
320 ///
321 /// Don't specialize on alignment for platforms where unaligned memory accesses
322 /// generates the same code as aligned memory accesses for common types.
323 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
324     defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
325     defined(_X86_) || defined(__i386) || defined(__i386__)
326 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
327 #else
328 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
329 #endif
330
331 /// \macro LLVM_EXPLICIT
332 /// \brief Expands to explicit on compilers which support explicit conversion
333 /// operators. Otherwise expands to nothing.
334 #if __has_feature(cxx_explicit_conversions) || \
335     defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
336 #define LLVM_EXPLICIT explicit
337 #else
338 #define LLVM_EXPLICIT
339 #endif
340
341 /// \macro LLVM_STATIC_ASSERT
342 /// \brief Expands to C/C++'s static_assert on compilers which support it.
343 #if __has_feature(cxx_static_assert) || \
344     defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1600)
345 # define LLVM_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
346 #elif __has_feature(c_static_assert)
347 # define LLVM_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
348 #elif __has_extension(c_static_assert)
349 # define LLVM_STATIC_ASSERT(expr, msg) LLVM_EXTENSION _Static_assert(expr, msg)
350 #else
351 # define LLVM_STATIC_ASSERT(expr, msg)
352 #endif
353
354 /// \brief Does the compiler support generalized initializers (using braced
355 /// lists and std::initializer_list).  While clang may claim it supports general
356 /// initializers, if we're using MSVC's headers, we might not have a usable
357 /// std::initializer list type from the STL.  Disable this for now.
358 #if __has_feature(cxx_generalized_initializers) && !defined(_MSC_VER)
359 #define LLVM_HAS_INITIALIZER_LISTS 1
360 #else
361 #define LLVM_HAS_INITIALIZER_LISTS 0
362 #endif
363
364 /// \brief Mark debug helper function definitions like dump() that should not be
365 /// stripped from debug builds.
366 // FIXME: Move this to a private config.h as it's not usable in public headers.
367 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
368 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
369 #else
370 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
371 #endif
372
373 #endif