[C++11] Force the other C++11 standard library detection macros to be
[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 We require the host compiler to support r-value references.
60 #define LLVM_HAS_RVALUE_REFERENCES 1
61
62 /// \brief Does the compiler support r-value reference *this?
63 ///
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
70 #else
71 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
72 #endif
73
74 /// \macro LLVM_HAS_CXX11_TYPETRAITS
75 /// \brief Does the compiler have the C++11 type traits.
76 ///
77 /// #include <type_traits>
78 ///
79 /// * enable_if
80 /// * {true,false}_type
81 /// * is_constructible
82 /// * etc...
83 #define LLVM_HAS_CXX11_TYPETRAITS 1
84
85 /// \macro LLVM_HAS_CXX11_STDLIB
86 /// \brief Does the compiler have the C++11 standard library.
87 ///
88 /// Implies LLVM_HAS_RVALUE_REFERENCES, LLVM_HAS_CXX11_TYPETRAITS
89 #define LLVM_HAS_CXX11_STDLIB 1
90
91 /// \macro LLVM_HAS_VARIADIC_TEMPLATES
92 /// \brief Does this compiler support variadic templates.
93 ///
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
97 #else
98 # define LLVM_HAS_VARIADIC_TEMPLATES 0
99 #endif
100
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))
105 #else
106 #define llvm_move(value) (value)
107 #endif
108
109 /// Expands to '&' if r-value references are supported.
110 ///
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 &
115 #else
116 #define LLVM_LVALUE_FUNCTION
117 #endif
118
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.
122 ///
123 /// class DontCopy {
124 /// private:
125 ///   DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
126 ///   DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
127 /// public:
128 ///   ...
129 /// };
130 #if __has_feature(cxx_deleted_functions) || \
131     defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
132 #define LLVM_DELETED_FUNCTION = delete
133 #else
134 #define LLVM_DELETED_FUNCTION
135 #endif
136
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
143 #else
144 #define LLVM_FINAL
145 #endif
146
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
153 #else
154 #define LLVM_OVERRIDE
155 #endif
156
157 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
158 # define LLVM_CONSTEXPR constexpr
159 #else
160 # define LLVM_CONSTEXPR
161 #endif
162
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")))
171 #else
172 #define LLVM_LIBRARY_VISIBILITY
173 #endif
174
175 #if __has_attribute(used) || __GNUC_PREREQ(3, 1)
176 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
177 #else
178 #define LLVM_ATTRIBUTE_USED
179 #endif
180
181 #if __has_attribute(warn_unused_result) || __GNUC_PREREQ(3, 4)
182 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
183 #else
184 #define LLVM_ATTRIBUTE_UNUSED_RESULT
185 #endif
186
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.
190 //
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__))
197 #else
198 #define LLVM_ATTRIBUTE_UNUSED
199 #endif
200
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__))
205 #else
206 #define LLVM_ATTRIBUTE_WEAK
207 #endif
208
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__))
214 #else
215 #define LLVM_READNONE
216 #endif
217
218 #if __has_attribute(pure) || defined(__GNUC__)
219 // aka 'PURE' but following LLVM Conventions.
220 #define LLVM_READONLY __attribute__((__pure__))
221 #else
222 #define LLVM_READONLY
223 #endif
224
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)
228 #else
229 #define LLVM_LIKELY(EXPR) (EXPR)
230 #define LLVM_UNLIKELY(EXPR) (EXPR)
231 #endif
232
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>);
238 #ifdef __GNUC__
239 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
240 #define TEMPLATE_INSTANTIATION(X) template X
241 #else
242 #define EXTERN_TEMPLATE_INSTANTIATION(X)
243 #define TEMPLATE_INSTANTIATION(X)
244 #endif
245
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)
252 #else
253 #define LLVM_ATTRIBUTE_NOINLINE
254 #endif
255
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
264 #else
265 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
266 #endif
267
268 #ifdef __GNUC__
269 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
270 #elif defined(_MSC_VER)
271 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
272 #else
273 #define LLVM_ATTRIBUTE_NORETURN
274 #endif
275
276 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
277 /// pedantic diagnostics.
278 #ifdef __GNUC__
279 #define LLVM_EXTENSION __extension__
280 #else
281 #define LLVM_EXTENSION
282 #endif
283
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
294 #else
295 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
296   decl
297 #endif
298
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)
306 #endif
307
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()
312 #else
313 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
314 #endif
315
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)))
324 #else
325 # define LLVM_ASSUME_ALIGNED(p, a) (p)
326 #endif
327
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__
333 #else
334 # define LLVM_FUNCTION_NAME __func__
335 #endif
336
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>
342 #else
343 # define LLVM_MEMORY_SANITIZER_BUILD 0
344 # define __msan_allocated_memory(p, size)
345 # define __msan_unpoison(p, size)
346 #endif
347
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
352 #else
353 # define LLVM_ADDRESS_SANITIZER_BUILD 0
354 #endif
355
356 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
357 /// \brief Is unaligned memory access fast on the host machine.
358 ///
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
365 #else
366 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
367 #endif
368
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
375 #else
376 #define LLVM_EXPLICIT
377 #endif
378
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)
388 #else
389 # define LLVM_STATIC_ASSERT(expr, msg)
390 #endif
391
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
395 /// with MSVC.
396 #if __has_feature(cxx_strong_enums) || LLVM_MSC_PREREQ(1600)
397 # define LLVM_ENUM_INT_TYPE(intty) : intty
398 #else
399 # define LLVM_ENUM_INT_TYPE(intty)
400 #endif
401
402 /// \brief Does the compiler support C++11 semantics for strongly typed forward
403 /// declared enums?
404 #if __has_feature(cxx_strong_enums) || LLVM_MSC_PREREQ(1700)
405 #define LLVM_HAS_STRONG_ENUMS 1
406 #else
407 #define LLVM_HAS_STRONG_ENUMS 0
408 #endif
409
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
416 #else
417 #define LLVM_HAS_INITIALIZER_LISTS 0
418 #endif
419
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
425 #else
426 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
427 #endif
428
429 #endif