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