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