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