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