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