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