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