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