Update the docs to require at least MSVC 2013.
[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 ///  * 1800: Microsoft Visual Studio 2013 / 12.0
56 ///  * 1900: Microsoft Visual Studio 2015 / 14.0
57 #ifdef _MSC_VER
58 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
59
60 // We require at least MSVC 2013.
61 #if !LLVM_MSC_PREREQ(1800)
62 #error LLVM requires at least MSVC 2013.
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(_MSC_VER)
291 // The __debugbreak intrinsic is supported by MSVC, does not require forward
292 // declarations involving platform-specific typedefs (unlike RaiseException),
293 // results in a call to vectored exception handlers, and encodes to a short
294 // instruction that still causes the trapping behavior we want.
295 # define LLVM_BUILTIN_TRAP __debugbreak()
296 #else
297 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
298 #endif
299
300 /// \macro LLVM_ASSUME_ALIGNED
301 /// \brief Returns a pointer with an assumed alignment.
302 #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
303 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
304 #elif defined(LLVM_BUILTIN_UNREACHABLE)
305 // As of today, clang does not support __builtin_assume_aligned.
306 # define LLVM_ASSUME_ALIGNED(p, a) \
307            (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
308 #else
309 # define LLVM_ASSUME_ALIGNED(p, a) (p)
310 #endif
311
312 /// \macro LLVM_FUNCTION_NAME
313 /// \brief Expands to __func__ on compilers which support it.  Otherwise,
314 /// expands to a compiler-dependent replacement.
315 #if defined(_MSC_VER)
316 # define LLVM_FUNCTION_NAME __FUNCTION__
317 #else
318 # define LLVM_FUNCTION_NAME __func__
319 #endif
320
321 /// \macro LLVM_MEMORY_SANITIZER_BUILD
322 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
323 #if __has_feature(memory_sanitizer)
324 # define LLVM_MEMORY_SANITIZER_BUILD 1
325 # include <sanitizer/msan_interface.h>
326 #else
327 # define LLVM_MEMORY_SANITIZER_BUILD 0
328 # define __msan_allocated_memory(p, size)
329 # define __msan_unpoison(p, size)
330 #endif
331
332 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
333 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
334 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
335 # define LLVM_ADDRESS_SANITIZER_BUILD 1
336 #else
337 # define LLVM_ADDRESS_SANITIZER_BUILD 0
338 #endif
339
340 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
341 /// \brief Is unaligned memory access fast on the host machine.
342 ///
343 /// Don't specialize on alignment for platforms where unaligned memory accesses
344 /// generates the same code as aligned memory accesses for common types.
345 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
346     defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
347     defined(_X86_) || defined(__i386) || defined(__i386__)
348 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
349 #else
350 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
351 #endif
352
353 /// \macro LLVM_EXPLICIT
354 /// \brief Expands to explicit on compilers which support explicit conversion
355 /// operators. Otherwise expands to nothing.
356 #if __has_feature(cxx_explicit_conversions) || \
357     defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
358 #define LLVM_EXPLICIT explicit
359 #else
360 #define LLVM_EXPLICIT
361 #endif
362
363 /// \brief Does the compiler support generalized initializers (using braced
364 /// lists and std::initializer_list).  While clang may claim it supports general
365 /// initializers, if we're using MSVC's headers, we might not have a usable
366 /// std::initializer list type from the STL.  Disable this for now.
367 #if __has_feature(cxx_generalized_initializers) && !defined(_MSC_VER)
368 #define LLVM_HAS_INITIALIZER_LISTS 1
369 #else
370 #define LLVM_HAS_INITIALIZER_LISTS 0
371 #endif
372
373 /// \brief Mark debug helper function definitions like dump() that should not be
374 /// stripped from debug builds.
375 // FIXME: Move this to a private config.h as it's not usable in public headers.
376 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
377 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
378 #else
379 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
380 #endif
381
382 /// \macro LLVM_THREAD_LOCAL
383 /// \brief A thread-local storage specifier which can be used with globals,
384 /// extern globals, and static globals.
385 ///
386 /// This is essentially an extremely restricted analog to C++11's thread_local
387 /// support, and uses that when available. However, it falls back on
388 /// platform-specific or vendor-provided extensions when necessary. These
389 /// extensions don't support many of the C++11 thread_local's features. You
390 /// should only use this for PODs that you can statically initialize to
391 /// some constant value. In almost all circumstances this is most appropriate
392 /// for use with a pointer, integer, or small aggregation of pointers and
393 /// integers.
394 #if LLVM_ENABLE_THREADS
395 #if __has_feature(cxx_thread_local)
396 #define LLVM_THREAD_LOCAL thread_local
397 #elif defined(_MSC_VER)
398 // MSVC supports this with a __declspec.
399 #define LLVM_THREAD_LOCAL __declspec(thread)
400 #else
401 // Clang, GCC, and other compatible compilers used __thread prior to C++11 and
402 // we only need the restricted functionality that provides.
403 #define LLVM_THREAD_LOCAL __thread
404 #endif
405 #else // !LLVM_ENABLE_THREADS
406 // If threading is disabled entirely, this compiles to nothing and you get
407 // a normal global variable.
408 #define LLVM_THREAD_LOCAL
409 #endif
410
411 #endif