1 | //===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file defines several macros, based on the current compiler. This allows |
10 | // use of compiler-specific features in a way that remains portable. This header |
11 | // can be included from either C or C++. |
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 | #include <stddef.h> |
21 | |
22 | #if defined(_MSC_VER) |
23 | #include <sal.h> |
24 | #endif |
25 | |
26 | #ifndef __has_feature |
27 | # define __has_feature(x) 0 |
28 | #endif |
29 | |
30 | #ifndef __has_extension |
31 | # define __has_extension(x) 0 |
32 | #endif |
33 | |
34 | #ifndef __has_attribute |
35 | # define __has_attribute(x) 0 |
36 | #endif |
37 | |
38 | #ifndef __has_builtin |
39 | # define __has_builtin(x) 0 |
40 | #endif |
41 | |
42 | #ifndef __has_include |
43 | # define __has_include(x) 0 |
44 | #endif |
45 | |
46 | // Only use __has_cpp_attribute in C++ mode. GCC defines __has_cpp_attribute in |
47 | // C mode, but the :: in __has_cpp_attribute(scoped::attribute) is invalid. |
48 | #ifndef LLVM_HAS_CPP_ATTRIBUTE |
49 | #if defined(__cplusplus) && defined(__has_cpp_attribute) |
50 | # define LLVM_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) |
51 | #else |
52 | # define LLVM_HAS_CPP_ATTRIBUTE(x) 0 |
53 | #endif |
54 | #endif |
55 | |
56 | /// \macro LLVM_GNUC_PREREQ |
57 | /// Extend the default __GNUC_PREREQ even if glibc's features.h isn't |
58 | /// available. |
59 | #ifndef LLVM_GNUC_PREREQ |
60 | # if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) |
61 | # define LLVM_GNUC_PREREQ(maj, min, patch) \ |
62 | ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \ |
63 | ((maj) << 20) + ((min) << 10) + (patch)) |
64 | # elif defined(__GNUC__) && defined(__GNUC_MINOR__) |
65 | # define LLVM_GNUC_PREREQ(maj, min, patch) \ |
66 | ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10)) |
67 | # else |
68 | # define LLVM_GNUC_PREREQ(maj, min, patch) 0 |
69 | # endif |
70 | #endif |
71 | |
72 | /// \macro LLVM_MSC_PREREQ |
73 | /// Is the compiler MSVC of at least the specified version? |
74 | /// The common \param version values to check for are: |
75 | /// * 1910: VS2017, version 15.1 & 15.2 |
76 | /// * 1911: VS2017, version 15.3 & 15.4 |
77 | /// * 1912: VS2017, version 15.5 |
78 | /// * 1913: VS2017, version 15.6 |
79 | /// * 1914: VS2017, version 15.7 |
80 | /// * 1915: VS2017, version 15.8 |
81 | /// * 1916: VS2017, version 15.9 |
82 | /// * 1920: VS2019, version 16.0 |
83 | /// * 1921: VS2019, version 16.1 |
84 | /// * 1922: VS2019, version 16.2 |
85 | /// * 1923: VS2019, version 16.3 |
86 | /// * 1924: VS2019, version 16.4 |
87 | /// * 1925: VS2019, version 16.5 |
88 | /// * 1926: VS2019, version 16.6 |
89 | /// * 1927: VS2019, version 16.7 |
90 | /// * 1928: VS2019, version 16.8 + 16.9 |
91 | /// * 1929: VS2019, version 16.10 + 16.11 |
92 | /// * 1930: VS2022, version 17.0 |
93 | #ifdef _MSC_VER |
94 | #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version)) |
95 | |
96 | // We require at least VS 2019. |
97 | #if !defined(LLVM_FORCE_USE_OLD_TOOLCHAIN) |
98 | #if !LLVM_MSC_PREREQ(1920) |
99 | #error LLVM requires at least VS 2019. |
100 | #endif |
101 | #endif |
102 | |
103 | #else |
104 | #define LLVM_MSC_PREREQ(version) 0 |
105 | #endif |
106 | |
107 | /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked |
108 | /// into a shared library, then the class should be private to the library and |
109 | /// not accessible from outside it. Can also be used to mark variables and |
110 | /// functions, making them private to any shared library they are linked into. |
111 | /// On PE/COFF targets, library visibility is the default, so this isn't needed. |
112 | /// |
113 | /// LLVM_EXTERNAL_VISIBILITY - classes, functions, and variables marked with |
114 | /// this attribute will be made public and visible outside of any shared library |
115 | /// they are linked in to. |
116 | |
117 | #if LLVM_HAS_CPP_ATTRIBUTE(gnu::visibility) |
118 | #define LLVM_ATTRIBUTE_VISIBILITY_HIDDEN [[gnu::visibility("hidden")]] |
119 | #define LLVM_ATTRIBUTE_VISIBILITY_DEFAULT [[gnu::visibility("default")]] |
120 | #elif __has_attribute(visibility) |
121 | #define LLVM_ATTRIBUTE_VISIBILITY_HIDDEN __attribute__((visibility("hidden"))) |
122 | #define LLVM_ATTRIBUTE_VISIBILITY_DEFAULT __attribute__((visibility("default"))) |
123 | #else |
124 | #define LLVM_ATTRIBUTE_VISIBILITY_HIDDEN |
125 | #define LLVM_ATTRIBUTE_VISIBILITY_DEFAULT |
126 | #endif |
127 | |
128 | |
129 | #if (!(defined(_WIN32) || defined(__CYGWIN__)) || \ |
130 | (defined(__MINGW32__) && defined(__clang__))) |
131 | #define LLVM_LIBRARY_VISIBILITY LLVM_ATTRIBUTE_VISIBILITY_HIDDEN |
132 | #if defined(LLVM_BUILD_LLVM_DYLIB) || defined(LLVM_BUILD_SHARED_LIBS) |
133 | #define LLVM_EXTERNAL_VISIBILITY LLVM_ATTRIBUTE_VISIBILITY_DEFAULT |
134 | #else |
135 | #define LLVM_EXTERNAL_VISIBILITY |
136 | #endif |
137 | #else |
138 | #define LLVM_LIBRARY_VISIBILITY |
139 | #define LLVM_EXTERNAL_VISIBILITY |
140 | #endif |
141 | |
142 | #if defined(__GNUC__) |
143 | #define LLVM_PREFETCH(addr, rw, locality) __builtin_prefetch(addr, rw, locality) |
144 | #else |
145 | #define LLVM_PREFETCH(addr, rw, locality) |
146 | #endif |
147 | |
148 | #if __has_attribute(used) |
149 | #define LLVM_ATTRIBUTE_USED __attribute__((__used__)) |
150 | #else |
151 | #define LLVM_ATTRIBUTE_USED |
152 | #endif |
153 | |
154 | #if defined(__clang__) |
155 | #define LLVM_DEPRECATED(MSG, FIX) __attribute__((deprecated(MSG, FIX))) |
156 | #else |
157 | #define LLVM_DEPRECATED(MSG, FIX) [[deprecated(MSG)]] |
158 | #endif |
159 | |
160 | // clang-format off |
161 | #if defined(__clang__) || defined(__GNUC__) |
162 | #define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH \ |
163 | _Pragma("GCC diagnostic push") \ |
164 | _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") |
165 | #define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP \ |
166 | _Pragma("GCC diagnostic pop") |
167 | #elif defined(_MSC_VER) |
168 | #define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH \ |
169 | _Pragma("warning(push)") \ |
170 | _Pragma("warning(disable : 4996)") |
171 | #define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP \ |
172 | _Pragma("warning(pop)") |
173 | #else |
174 | #define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH |
175 | #define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP |
176 | #endif |
177 | // clang-format on |
178 | |
179 | // Indicate that a non-static, non-const C++ member function reinitializes |
180 | // the entire object to a known state, independent of the previous state of |
181 | // the object. |
182 | // |
183 | // The clang-tidy check bugprone-use-after-move recognizes this attribute as a |
184 | // marker that a moved-from object has left the indeterminate state and can be |
185 | // reused. |
186 | #if LLVM_HAS_CPP_ATTRIBUTE(clang::reinitializes) |
187 | #define LLVM_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]] |
188 | #else |
189 | #define LLVM_ATTRIBUTE_REINITIALIZES |
190 | #endif |
191 | |
192 | // Some compilers warn about unused functions. When a function is sometimes |
193 | // used or not depending on build settings (e.g. a function only called from |
194 | // within "assert"), this attribute can be used to suppress such warnings. |
195 | // |
196 | // However, it shouldn't be used for unused *variables*, as those have a much |
197 | // more portable solution: |
198 | // (void)unused_var_name; |
199 | // Prefer cast-to-void wherever it is sufficient. |
200 | #if __has_attribute(unused) |
201 | #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__)) |
202 | #else |
203 | #define LLVM_ATTRIBUTE_UNUSED |
204 | #endif |
205 | |
206 | // FIXME: Provide this for PE/COFF targets. |
207 | #if __has_attribute(weak) && !defined(__MINGW32__) && !defined(__CYGWIN__) && \ |
208 | !defined(_WIN32) |
209 | #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__)) |
210 | #else |
211 | #define LLVM_ATTRIBUTE_WEAK |
212 | #endif |
213 | |
214 | // Prior to clang 3.2, clang did not accept any spelling of |
215 | // __has_attribute(const), so assume it is supported. |
216 | #if defined(__clang__) || defined(__GNUC__) |
217 | // aka 'CONST' but following LLVM Conventions. |
218 | #define LLVM_READNONE __attribute__((__const__)) |
219 | #else |
220 | #define LLVM_READNONE |
221 | #endif |
222 | |
223 | #if __has_attribute(pure) || defined(__GNUC__) |
224 | // aka 'PURE' but following LLVM Conventions. |
225 | #define LLVM_READONLY __attribute__((__pure__)) |
226 | #else |
227 | #define LLVM_READONLY |
228 | #endif |
229 | |
230 | #if __has_attribute(minsize) |
231 | #define LLVM_ATTRIBUTE_MINSIZE __attribute__((minsize)) |
232 | #else |
233 | #define LLVM_ATTRIBUTE_MINSIZE |
234 | #endif |
235 | |
236 | #if __has_builtin(__builtin_expect) || defined(__GNUC__) |
237 | #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true) |
238 | #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false) |
239 | #else |
240 | #define LLVM_LIKELY(EXPR) (EXPR) |
241 | #define LLVM_UNLIKELY(EXPR) (EXPR) |
242 | #endif |
243 | |
244 | /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so, |
245 | /// mark a method "not for inlining". |
246 | #if __has_attribute(noinline) |
247 | #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline)) |
248 | #elif defined(_MSC_VER) |
249 | #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline) |
250 | #else |
251 | #define LLVM_ATTRIBUTE_NOINLINE |
252 | #endif |
253 | |
254 | /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do |
255 | /// so, mark a method "always inline" because it is performance sensitive. |
256 | #if __has_attribute(always_inline) |
257 | #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline)) |
258 | #elif defined(_MSC_VER) |
259 | #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline |
260 | #else |
261 | #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline |
262 | #endif |
263 | |
264 | /// LLVM_ATTRIBUTE_NO_DEBUG - On compilers where we have a directive to do |
265 | /// so, mark a method "no debug" because debug info makes the debugger |
266 | /// experience worse. |
267 | #if __has_attribute(nodebug) |
268 | #define LLVM_ATTRIBUTE_NODEBUG __attribute__((nodebug)) |
269 | #else |
270 | #define LLVM_ATTRIBUTE_NODEBUG |
271 | #endif |
272 | |
273 | #if __has_attribute(returns_nonnull) |
274 | #define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull)) |
275 | #elif defined(_MSC_VER) |
276 | #define LLVM_ATTRIBUTE_RETURNS_NONNULL _Ret_notnull_ |
277 | #else |
278 | #define LLVM_ATTRIBUTE_RETURNS_NONNULL |
279 | #endif |
280 | |
281 | /// \macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a |
282 | /// pointer that does not alias any other valid pointer. |
283 | #ifdef __GNUC__ |
284 | #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__)) |
285 | #elif defined(_MSC_VER) |
286 | #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict) |
287 | #else |
288 | #define LLVM_ATTRIBUTE_RETURNS_NOALIAS |
289 | #endif |
290 | |
291 | /// LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements. |
292 | #if defined(__cplusplus) && __cplusplus > 201402L && LLVM_HAS_CPP_ATTRIBUTE(fallthrough) |
293 | #define LLVM_FALLTHROUGH [[fallthrough]] |
294 | #elif LLVM_HAS_CPP_ATTRIBUTE(gnu::fallthrough) |
295 | #define LLVM_FALLTHROUGH [[gnu::fallthrough]] |
296 | #elif __has_attribute(fallthrough) |
297 | #define LLVM_FALLTHROUGH __attribute__((fallthrough)) |
298 | #elif LLVM_HAS_CPP_ATTRIBUTE(clang::fallthrough) |
299 | #define LLVM_FALLTHROUGH [[clang::fallthrough]] |
300 | #else |
301 | #define LLVM_FALLTHROUGH |
302 | #endif |
303 | |
304 | /// LLVM_REQUIRE_CONSTANT_INITIALIZATION - Apply this to globals to ensure that |
305 | /// they are constant initialized. |
306 | #if LLVM_HAS_CPP_ATTRIBUTE(clang::require_constant_initialization) |
307 | #define LLVM_REQUIRE_CONSTANT_INITIALIZATION \ |
308 | [[clang::require_constant_initialization]] |
309 | #else |
310 | #define LLVM_REQUIRE_CONSTANT_INITIALIZATION |
311 | #endif |
312 | |
313 | /// LLVM_GSL_OWNER - Apply this to owning classes like SmallVector to enable |
314 | /// lifetime warnings. |
315 | #if LLVM_HAS_CPP_ATTRIBUTE(gsl::Owner) |
316 | #define LLVM_GSL_OWNER [[gsl::Owner]] |
317 | #else |
318 | #define LLVM_GSL_OWNER |
319 | #endif |
320 | |
321 | /// LLVM_GSL_POINTER - Apply this to non-owning classes like |
322 | /// StringRef to enable lifetime warnings. |
323 | #if LLVM_HAS_CPP_ATTRIBUTE(gsl::Pointer) |
324 | #define LLVM_GSL_POINTER [[gsl::Pointer]] |
325 | #else |
326 | #define LLVM_GSL_POINTER |
327 | #endif |
328 | |
329 | #if LLVM_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L |
330 | #define LLVM_CTOR_NODISCARD [[nodiscard]] |
331 | #else |
332 | #define LLVM_CTOR_NODISCARD |
333 | #endif |
334 | |
335 | /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress |
336 | /// pedantic diagnostics. |
337 | #ifdef __GNUC__ |
338 | #define LLVM_EXTENSION __extension__ |
339 | #else |
340 | #define LLVM_EXTENSION |
341 | #endif |
342 | |
343 | /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands |
344 | /// to an expression which states that it is undefined behavior for the |
345 | /// compiler to reach this point. Otherwise is not defined. |
346 | /// |
347 | /// '#else' is intentionally left out so that other macro logic (e.g., |
348 | /// LLVM_ASSUME_ALIGNED and llvm_unreachable()) can detect whether |
349 | /// LLVM_BUILTIN_UNREACHABLE has a definition. |
350 | #if __has_builtin(__builtin_unreachable) || defined(__GNUC__) |
351 | # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable() |
352 | #elif defined(_MSC_VER) |
353 | # define LLVM_BUILTIN_UNREACHABLE __assume(false) |
354 | #endif |
355 | |
356 | /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression |
357 | /// which causes the program to exit abnormally. |
358 | #if __has_builtin(__builtin_trap) || defined(__GNUC__) |
359 | # define LLVM_BUILTIN_TRAP __builtin_trap() |
360 | #elif defined(_MSC_VER) |
361 | // The __debugbreak intrinsic is supported by MSVC, does not require forward |
362 | // declarations involving platform-specific typedefs (unlike RaiseException), |
363 | // results in a call to vectored exception handlers, and encodes to a short |
364 | // instruction that still causes the trapping behavior we want. |
365 | # define LLVM_BUILTIN_TRAP __debugbreak() |
366 | #else |
367 | # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0 |
368 | #endif |
369 | |
370 | /// LLVM_BUILTIN_DEBUGTRAP - On compilers which support it, expands to |
371 | /// an expression which causes the program to break while running |
372 | /// under a debugger. |
373 | #if __has_builtin(__builtin_debugtrap) |
374 | # define LLVM_BUILTIN_DEBUGTRAP __builtin_debugtrap() |
375 | #elif defined(_MSC_VER) |
376 | // The __debugbreak intrinsic is supported by MSVC and breaks while |
377 | // running under the debugger, and also supports invoking a debugger |
378 | // when the OS is configured appropriately. |
379 | # define LLVM_BUILTIN_DEBUGTRAP __debugbreak() |
380 | #else |
381 | // Just continue execution when built with compilers that have no |
382 | // support. This is a debugging aid and not intended to force the |
383 | // program to abort if encountered. |
384 | # define LLVM_BUILTIN_DEBUGTRAP |
385 | #endif |
386 | |
387 | /// \macro LLVM_ASSUME_ALIGNED |
388 | /// Returns a pointer with an assumed alignment. |
389 | #if __has_builtin(__builtin_assume_aligned) || defined(__GNUC__) |
390 | # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a) |
391 | #elif defined(LLVM_BUILTIN_UNREACHABLE) |
392 | # define LLVM_ASSUME_ALIGNED(p, a) \ |
393 | (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p))) |
394 | #else |
395 | # define LLVM_ASSUME_ALIGNED(p, a) (p) |
396 | #endif |
397 | |
398 | /// \macro LLVM_PACKED |
399 | /// Used to specify a packed structure. |
400 | /// LLVM_PACKED( |
401 | /// struct A { |
402 | /// int i; |
403 | /// int j; |
404 | /// int k; |
405 | /// long long l; |
406 | /// }); |
407 | /// |
408 | /// LLVM_PACKED_START |
409 | /// struct B { |
410 | /// int i; |
411 | /// int j; |
412 | /// int k; |
413 | /// long long l; |
414 | /// }; |
415 | /// LLVM_PACKED_END |
416 | #ifdef _MSC_VER |
417 | # define LLVM_PACKED(d) __pragma(pack(push, 1)) d __pragma(pack(pop)) |
418 | # define LLVM_PACKED_START __pragma(pack(push, 1)) |
419 | # define LLVM_PACKED_END __pragma(pack(pop)) |
420 | #else |
421 | # define LLVM_PACKED(d) d __attribute__((packed)) |
422 | # define LLVM_PACKED_START _Pragma("pack(push, 1)") |
423 | # define LLVM_PACKED_END _Pragma("pack(pop)") |
424 | #endif |
425 | |
426 | /// \macro LLVM_MEMORY_SANITIZER_BUILD |
427 | /// Whether LLVM itself is built with MemorySanitizer instrumentation. |
428 | #if __has_feature(memory_sanitizer) |
429 | # define LLVM_MEMORY_SANITIZER_BUILD 1 |
430 | # include <sanitizer/msan_interface.h> |
431 | # define LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE __attribute__((no_sanitize_memory)) |
432 | #else |
433 | # define LLVM_MEMORY_SANITIZER_BUILD 0 |
434 | # define __msan_allocated_memory(p, size) |
435 | # define __msan_unpoison(p, size) |
436 | # define LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE |
437 | #endif |
438 | |
439 | /// \macro LLVM_ADDRESS_SANITIZER_BUILD |
440 | /// Whether LLVM itself is built with AddressSanitizer instrumentation. |
441 | #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) |
442 | # define LLVM_ADDRESS_SANITIZER_BUILD 1 |
443 | #if __has_include(<sanitizer/asan_interface.h>) |
444 | # include <sanitizer/asan_interface.h> |
445 | #else |
446 | // These declarations exist to support ASan with MSVC. If MSVC eventually ships |
447 | // asan_interface.h in their headers, then we can remove this. |
448 | #ifdef __cplusplus |
449 | extern "C" { |
450 | #endif |
451 | void __asan_poison_memory_region(void const volatile *addr, size_t size); |
452 | void __asan_unpoison_memory_region(void const volatile *addr, size_t size); |
453 | #ifdef __cplusplus |
454 | } // extern "C" |
455 | #endif |
456 | #endif |
457 | #else |
458 | # define LLVM_ADDRESS_SANITIZER_BUILD 0 |
459 | # define __asan_poison_memory_region(p, size) |
460 | # define __asan_unpoison_memory_region(p, size) |
461 | #endif |
462 | |
463 | /// \macro LLVM_HWADDRESS_SANITIZER_BUILD |
464 | /// Whether LLVM itself is built with HWAddressSanitizer instrumentation. |
465 | #if __has_feature(hwaddress_sanitizer) |
466 | #define LLVM_HWADDRESS_SANITIZER_BUILD 1 |
467 | #else |
468 | #define LLVM_HWADDRESS_SANITIZER_BUILD 0 |
469 | #endif |
470 | |
471 | /// \macro LLVM_THREAD_SANITIZER_BUILD |
472 | /// Whether LLVM itself is built with ThreadSanitizer instrumentation. |
473 | #if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__) |
474 | # define LLVM_THREAD_SANITIZER_BUILD 1 |
475 | #else |
476 | # define LLVM_THREAD_SANITIZER_BUILD 0 |
477 | #endif |
478 | |
479 | #if LLVM_THREAD_SANITIZER_BUILD |
480 | // Thread Sanitizer is a tool that finds races in code. |
481 | // See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations . |
482 | // tsan detects these exact functions by name. |
483 | #ifdef __cplusplus |
484 | extern "C" { |
485 | #endif |
486 | void AnnotateHappensAfter(const char *file, int line, const volatile void *cv); |
487 | void AnnotateHappensBefore(const char *file, int line, const volatile void *cv); |
488 | void AnnotateIgnoreWritesBegin(const char *file, int line); |
489 | void AnnotateIgnoreWritesEnd(const char *file, int line); |
490 | #ifdef __cplusplus |
491 | } |
492 | #endif |
493 | |
494 | // This marker is used to define a happens-before arc. The race detector will |
495 | // infer an arc from the begin to the end when they share the same pointer |
496 | // argument. |
497 | # define TsanHappensBefore(cv) AnnotateHappensBefore(__FILE__, __LINE__, cv) |
498 | |
499 | // This marker defines the destination of a happens-before arc. |
500 | # define TsanHappensAfter(cv) AnnotateHappensAfter(__FILE__, __LINE__, cv) |
501 | |
502 | // Ignore any races on writes between here and the next TsanIgnoreWritesEnd. |
503 | # define TsanIgnoreWritesBegin() AnnotateIgnoreWritesBegin(__FILE__, __LINE__) |
504 | |
505 | // Resume checking for racy writes. |
506 | # define TsanIgnoreWritesEnd() AnnotateIgnoreWritesEnd(__FILE__, __LINE__) |
507 | #else |
508 | # define TsanHappensBefore(cv) |
509 | # define TsanHappensAfter(cv) |
510 | # define TsanIgnoreWritesBegin() |
511 | # define TsanIgnoreWritesEnd() |
512 | #endif |
513 | |
514 | /// \macro LLVM_NO_SANITIZE |
515 | /// Disable a particular sanitizer for a function. |
516 | #if __has_attribute(no_sanitize) |
517 | #define LLVM_NO_SANITIZE(KIND) __attribute__((no_sanitize(KIND))) |
518 | #else |
519 | #define LLVM_NO_SANITIZE(KIND) |
520 | #endif |
521 | |
522 | /// Mark debug helper function definitions like dump() that should not be |
523 | /// stripped from debug builds. |
524 | /// Note that you should also surround dump() functions with |
525 | /// `#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)` so they do always |
526 | /// get stripped in release builds. |
527 | // FIXME: Move this to a private config.h as it's not usable in public headers. |
528 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
529 | #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED |
530 | #else |
531 | #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE |
532 | #endif |
533 | |
534 | /// \macro LLVM_PRETTY_FUNCTION |
535 | /// Gets a user-friendly looking function signature for the current scope |
536 | /// using the best available method on each platform. The exact format of the |
537 | /// resulting string is implementation specific and non-portable, so this should |
538 | /// only be used, for example, for logging or diagnostics. |
539 | #if defined(_MSC_VER) |
540 | #define LLVM_PRETTY_FUNCTION __FUNCSIG__ |
541 | #elif defined(__GNUC__) || defined(__clang__) |
542 | #define LLVM_PRETTY_FUNCTION __PRETTY_FUNCTION__ |
543 | #else |
544 | #define LLVM_PRETTY_FUNCTION __func__ |
545 | #endif |
546 | |
547 | /// \macro LLVM_THREAD_LOCAL |
548 | /// A thread-local storage specifier which can be used with globals, |
549 | /// extern globals, and static globals. |
550 | /// |
551 | /// This is essentially an extremely restricted analog to C++11's thread_local |
552 | /// support. It uses thread_local if available, falling back on gcc __thread |
553 | /// if not. __thread doesn't support many of the C++11 thread_local's |
554 | /// features. You should only use this for PODs that you can statically |
555 | /// initialize to some constant value. In almost all circumstances this is most |
556 | /// appropriate for use with a pointer, integer, or small aggregation of |
557 | /// pointers and integers. |
558 | #if LLVM_ENABLE_THREADS |
559 | #if __has_feature(cxx_thread_local) || defined(_MSC_VER) |
560 | #define LLVM_THREAD_LOCAL thread_local |
561 | #else |
562 | // Clang, GCC, and other compatible compilers used __thread prior to C++11 and |
563 | // we only need the restricted functionality that provides. |
564 | #define LLVM_THREAD_LOCAL __thread |
565 | #endif |
566 | #else // !LLVM_ENABLE_THREADS |
567 | // If threading is disabled entirely, this compiles to nothing and you get |
568 | // a normal global variable. |
569 | #define LLVM_THREAD_LOCAL |
570 | #endif |
571 | |
572 | /// \macro LLVM_ENABLE_EXCEPTIONS |
573 | /// Whether LLVM is built with exception support. |
574 | #if __has_feature(cxx_exceptions) |
575 | #define LLVM_ENABLE_EXCEPTIONS 1 |
576 | #elif defined(__GNUC__) && defined(__EXCEPTIONS) |
577 | #define LLVM_ENABLE_EXCEPTIONS 1 |
578 | #elif defined(_MSC_VER) && defined(_CPPUNWIND) |
579 | #define LLVM_ENABLE_EXCEPTIONS 1 |
580 | #endif |
581 | |
582 | /// \macro LLVM_NO_PROFILE_INSTRUMENT_FUNCTION |
583 | /// Disable the profile instrument for a function. |
584 | #if __has_attribute(no_profile_instrument_function) |
585 | #define LLVM_NO_PROFILE_INSTRUMENT_FUNCTION \ |
586 | __attribute__((no_profile_instrument_function)) |
587 | #else |
588 | #define LLVM_NO_PROFILE_INSTRUMENT_FUNCTION |
589 | #endif |
590 | |
591 | /// \macro LLVM_PREFERRED_TYPE |
592 | /// Adjust type of bit-field in debug info. |
593 | #if __has_attribute(preferred_type) |
594 | #define LLVM_PREFERRED_TYPE(T) __attribute__((preferred_type(T))) |
595 | #else |
596 | #define LLVM_PREFERRED_TYPE(T) |
597 | #endif |
598 | |
599 | #endif |
600 | |