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