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