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