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