f87d797ad23e26f71523974f416e95a375ea0599
[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_CXX11_TYPETRAITS
72 /// \brief Does the compiler have the C++11 type traits.
73 ///
74 /// #include <type_traits>
75 ///
76 /// * enable_if
77 /// * {true,false}_type
78 /// * is_constructible
79 /// * etc...
80 #define LLVM_HAS_CXX11_TYPETRAITS 1
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 #define LLVM_HAS_CXX11_STDLIB 1
87
88 /// \macro LLVM_HAS_VARIADIC_TEMPLATES
89 /// \brief Does this compiler support variadic templates.
90 ///
91 /// Implies LLVM_HAS_RVALUE_REFERENCES and the existence of std::forward.
92 #if __has_feature(cxx_variadic_templates) || LLVM_MSC_PREREQ(1800)
93 # define LLVM_HAS_VARIADIC_TEMPLATES 1
94 #else
95 # define LLVM_HAS_VARIADIC_TEMPLATES 0
96 #endif
97
98 /// llvm_move - Expands to ::std::move. This is a hold-over from when we did
99 /// not support R-value references.
100 #define llvm_move(value) (::std::move(value))
101
102 /// Expands to '&' if r-value references are supported.
103 ///
104 /// This can be used to provide l-value/r-value overrides of member functions.
105 /// The r-value override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
106 #if LLVM_HAS_RVALUE_REFERENCE_THIS
107 #define LLVM_LVALUE_FUNCTION &
108 #else
109 #define LLVM_LVALUE_FUNCTION
110 #endif
111
112 /// LLVM_DELETED_FUNCTION - Expands to = delete if the compiler supports it.
113 /// Use to mark functions as uncallable. Member functions with this should
114 /// be declared private so that some behavior is kept in C++03 mode.
115 ///
116 /// class DontCopy {
117 /// private:
118 ///   DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
119 ///   DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
120 /// public:
121 ///   ...
122 /// };
123 #if __has_feature(cxx_deleted_functions) || \
124     defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
125 #define LLVM_DELETED_FUNCTION = delete
126 #else
127 #define LLVM_DELETED_FUNCTION
128 #endif
129
130 /// LLVM_FINAL - Expands to 'final' if the compiler supports it.
131 /// Use to mark classes or virtual methods as final.
132 #if __has_feature(cxx_override_control) || \
133     (defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC_PREREQ(4, 7)) || \
134     LLVM_MSC_PREREQ(1700)
135 #define LLVM_FINAL final
136 #else
137 #define LLVM_FINAL
138 #endif
139
140 /// LLVM_OVERRIDE - Expands to 'override' if the compiler supports it.
141 /// Use to mark virtual methods as overriding a base class method.
142 #if __has_feature(cxx_override_control) || \
143     (defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC_PREREQ(4, 7)) || \
144     LLVM_MSC_PREREQ(1700)
145 #define LLVM_OVERRIDE override
146 #else
147 #define LLVM_OVERRIDE
148 #endif
149
150 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
151 # define LLVM_CONSTEXPR constexpr
152 #else
153 # define LLVM_CONSTEXPR
154 #endif
155
156 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
157 /// into a shared library, then the class should be private to the library and
158 /// not accessible from outside it.  Can also be used to mark variables and
159 /// functions, making them private to any shared library they are linked into.
160 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
161 #if (__has_attribute(visibility) || __GNUC_PREREQ(4, 0)) &&                    \
162     !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
163 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
164 #else
165 #define LLVM_LIBRARY_VISIBILITY
166 #endif
167
168 #if __has_attribute(used) || __GNUC_PREREQ(3, 1)
169 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
170 #else
171 #define LLVM_ATTRIBUTE_USED
172 #endif
173
174 #if __has_attribute(warn_unused_result) || __GNUC_PREREQ(3, 4)
175 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
176 #else
177 #define LLVM_ATTRIBUTE_UNUSED_RESULT
178 #endif
179
180 // Some compilers warn about unused functions. When a function is sometimes
181 // used or not depending on build settings (e.g. a function only called from
182 // within "assert"), this attribute can be used to suppress such warnings.
183 //
184 // However, it shouldn't be used for unused *variables*, as those have a much
185 // more portable solution:
186 //   (void)unused_var_name;
187 // Prefer cast-to-void wherever it is sufficient.
188 #if __has_attribute(unused) || __GNUC_PREREQ(3, 1)
189 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
190 #else
191 #define LLVM_ATTRIBUTE_UNUSED
192 #endif
193
194 // FIXME: Provide this for PE/COFF targets.
195 #if (__has_attribute(weak) || __GNUC_PREREQ(4, 0)) &&                          \
196     (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
197 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
198 #else
199 #define LLVM_ATTRIBUTE_WEAK
200 #endif
201
202 // Prior to clang 3.2, clang did not accept any spelling of
203 // __has_attribute(const), so assume it is supported.
204 #if defined(__clang__) || 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 /// \macro LLVM_MEMORY_SANITIZER_BUILD
331 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
332 #if __has_feature(memory_sanitizer)
333 # define LLVM_MEMORY_SANITIZER_BUILD 1
334 # include <sanitizer/msan_interface.h>
335 #else
336 # define LLVM_MEMORY_SANITIZER_BUILD 0
337 # define __msan_allocated_memory(p, size)
338 # define __msan_unpoison(p, size)
339 #endif
340
341 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
342 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
343 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
344 # define LLVM_ADDRESS_SANITIZER_BUILD 1
345 #else
346 # define LLVM_ADDRESS_SANITIZER_BUILD 0
347 #endif
348
349 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
350 /// \brief Is unaligned memory access fast on the host machine.
351 ///
352 /// Don't specialize on alignment for platforms where unaligned memory accesses
353 /// generates the same code as aligned memory accesses for common types.
354 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
355     defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
356     defined(_X86_) || defined(__i386) || defined(__i386__)
357 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
358 #else
359 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
360 #endif
361
362 /// \macro LLVM_EXPLICIT
363 /// \brief Expands to explicit on compilers which support explicit conversion
364 /// operators. Otherwise expands to nothing.
365 #if __has_feature(cxx_explicit_conversions) || \
366     defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
367 #define LLVM_EXPLICIT explicit
368 #else
369 #define LLVM_EXPLICIT
370 #endif
371
372 /// \macro LLVM_STATIC_ASSERT
373 /// \brief Expands to C/C++'s static_assert on compilers which support it.
374 #if __has_feature(cxx_static_assert) || \
375     defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1600)
376 # define LLVM_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
377 #elif __has_feature(c_static_assert)
378 # define LLVM_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
379 #elif __has_extension(c_static_assert)
380 # define LLVM_STATIC_ASSERT(expr, msg) LLVM_EXTENSION _Static_assert(expr, msg)
381 #else
382 # define LLVM_STATIC_ASSERT(expr, msg)
383 #endif
384
385 /// \macro LLVM_ENUM_INT_TYPE
386 /// \brief Expands to colon followed by the given integral type on compilers
387 /// which support C++11 strong enums.  This can be used to make enums unsigned
388 /// with MSVC.
389 #if __has_feature(cxx_strong_enums) || LLVM_MSC_PREREQ(1600)
390 # define LLVM_ENUM_INT_TYPE(intty) : intty
391 #else
392 # define LLVM_ENUM_INT_TYPE(intty)
393 #endif
394
395 /// \brief Does the compiler support C++11 semantics for strongly typed forward
396 /// declared enums?
397 #if __has_feature(cxx_strong_enums) || LLVM_MSC_PREREQ(1700)
398 #define LLVM_HAS_STRONG_ENUMS 1
399 #else
400 #define LLVM_HAS_STRONG_ENUMS 0
401 #endif
402
403 /// \brief Does the compiler support generalized initializers (using braced
404 /// lists and std::initializer_list).  While clang may claim it supports general
405 /// initializers, if we're using MSVC's headers, we might not have a usable
406 /// std::initializer list type from the STL.  Disable this for now.
407 #if __has_feature(cxx_generalized_initializers) && !defined(_MSC_VER)
408 #define LLVM_HAS_INITIALIZER_LISTS 1
409 #else
410 #define LLVM_HAS_INITIALIZER_LISTS 0
411 #endif
412
413 /// \brief Mark debug helper function definitions like dump() that should not be
414 /// stripped from debug builds.
415 // FIXME: Move this to a private config.h as it's not usable in public headers.
416 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
417 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
418 #else
419 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
420 #endif
421
422 #endif