llvm.org GIT mirror llvm / ab9186d
Move Compiler.h from Support to Demangler to fix layering. Support depends on Demangle (Support/Unix/Signals.inc), so Demangle including Support/Compiler.h created a circular dependency. Leave a forwarding shim of Compiler.h because it makes more sense for users (a deeper fix might involve splitting Support into lower and upper Support - but that also sounds a bit weird/awkward) than thinking about the dependency on the Demangler. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@328072 91177308-0d34-0410-b5e6-96231b3b80d8 David Blaikie 2 years ago
3 changed file(s) with 519 addition(s) and 498 deletion(s). Raw diff Collapse all Expand all
0 //===-- llvm/Demangle/Compiler.h - Compiler abstraction support -*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
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.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_COMPILER_H
15 #define LLVM_SUPPORT_COMPILER_H
16
17 #include "llvm/Config/llvm-config.h"
18
19 #if defined(_MSC_VER)
20 #include
21 #endif
22
23 #ifndef __has_feature
24 # define __has_feature(x) 0
25 #endif
26
27 #ifndef __has_extension
28 # define __has_extension(x) 0
29 #endif
30
31 #ifndef __has_attribute
32 # define __has_attribute(x) 0
33 #endif
34
35 #ifndef __has_cpp_attribute
36 # define __has_cpp_attribute(x) 0
37 #endif
38
39 #ifndef __has_builtin
40 # define __has_builtin(x) 0
41 #endif
42
43 /// \macro LLVM_GNUC_PREREQ
44 /// \brief Extend the default __GNUC_PREREQ even if glibc's features.h isn't
45 /// available.
46 #ifndef LLVM_GNUC_PREREQ
47 # if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
48 # define LLVM_GNUC_PREREQ(maj, min, patch) \
49 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
50 ((maj) << 20) + ((min) << 10) + (patch))
51 # elif defined(__GNUC__) && defined(__GNUC_MINOR__)
52 # define LLVM_GNUC_PREREQ(maj, min, patch) \
53 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
54 # else
55 # define LLVM_GNUC_PREREQ(maj, min, patch) 0
56 # endif
57 #endif
58
59 /// \macro LLVM_MSC_PREREQ
60 /// \brief Is the compiler MSVC of at least the specified version?
61 /// The common \param version values to check for are:
62 /// * 1900: Microsoft Visual Studio 2015 / 14.0
63 #ifdef _MSC_VER
64 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
65
66 // We require at least MSVC 2015.
67 #if !LLVM_MSC_PREREQ(1900)
68 #error LLVM requires at least MSVC 2015.
69 #endif
70
71 #else
72 #define LLVM_MSC_PREREQ(version) 0
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 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
96 /// into a shared library, then the class should be private to the library and
97 /// not accessible from outside it. Can also be used to mark variables and
98 /// functions, making them private to any shared library they are linked into.
99 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
100 #if (__has_attribute(visibility) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
101 !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
102 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
103 #else
104 #define LLVM_LIBRARY_VISIBILITY
105 #endif
106
107 #if defined(__GNUC__)
108 #define LLVM_PREFETCH(addr, rw, locality) __builtin_prefetch(addr, rw, locality)
109 #else
110 #define LLVM_PREFETCH(addr, rw, locality)
111 #endif
112
113 #if __has_attribute(used) || LLVM_GNUC_PREREQ(3, 1, 0)
114 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
115 #else
116 #define LLVM_ATTRIBUTE_USED
117 #endif
118
119 /// LLVM_NODISCARD - Warn if a type or return value is discarded.
120 #if __cplusplus > 201402L && __has_cpp_attribute(nodiscard)
121 #define LLVM_NODISCARD [[nodiscard]]
122 #elif !__cplusplus
123 // Workaround for llvm.org/PR23435, since clang 3.6 and below emit a spurious
124 // error when __has_cpp_attribute is given a scoped attribute in C mode.
125 #define LLVM_NODISCARD
126 #elif __has_cpp_attribute(clang::warn_unused_result)
127 #define LLVM_NODISCARD [[clang::warn_unused_result]]
128 #else
129 #define LLVM_NODISCARD
130 #endif
131
132 // Some compilers warn about unused functions. When a function is sometimes
133 // used or not depending on build settings (e.g. a function only called from
134 // within "assert"), this attribute can be used to suppress such warnings.
135 //
136 // However, it shouldn't be used for unused *variables*, as those have a much
137 // more portable solution:
138 // (void)unused_var_name;
139 // Prefer cast-to-void wherever it is sufficient.
140 #if __has_attribute(unused) || LLVM_GNUC_PREREQ(3, 1, 0)
141 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
142 #else
143 #define LLVM_ATTRIBUTE_UNUSED
144 #endif
145
146 // FIXME: Provide this for PE/COFF targets.
147 #if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
148 (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
149 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
150 #else
151 #define LLVM_ATTRIBUTE_WEAK
152 #endif
153
154 // Prior to clang 3.2, clang did not accept any spelling of
155 // __has_attribute(const), so assume it is supported.
156 #if defined(__clang__) || defined(__GNUC__)
157 // aka 'CONST' but following LLVM Conventions.
158 #define LLVM_READNONE __attribute__((__const__))
159 #else
160 #define LLVM_READNONE
161 #endif
162
163 #if __has_attribute(pure) || defined(__GNUC__)
164 // aka 'PURE' but following LLVM Conventions.
165 #define LLVM_READONLY __attribute__((__pure__))
166 #else
167 #define LLVM_READONLY
168 #endif
169
170 #if __has_builtin(__builtin_expect) || LLVM_GNUC_PREREQ(4, 0, 0)
171 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
172 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
173 #else
174 #define LLVM_LIKELY(EXPR) (EXPR)
175 #define LLVM_UNLIKELY(EXPR) (EXPR)
176 #endif
177
178 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
179 /// mark a method "not for inlining".
180 #if __has_attribute(noinline) || LLVM_GNUC_PREREQ(3, 4, 0)
181 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
182 #elif defined(_MSC_VER)
183 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
184 #else
185 #define LLVM_ATTRIBUTE_NOINLINE
186 #endif
187
188 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
189 /// so, mark a method "always inline" because it is performance sensitive. GCC
190 /// 3.4 supported this but is buggy in various cases and produces unimplemented
191 /// errors, just use it in GCC 4.0 and later.
192 #if __has_attribute(always_inline) || LLVM_GNUC_PREREQ(4, 0, 0)
193 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
194 #elif defined(_MSC_VER)
195 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
196 #else
197 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
198 #endif
199
200 #ifdef __GNUC__
201 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
202 #elif defined(_MSC_VER)
203 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
204 #else
205 #define LLVM_ATTRIBUTE_NORETURN
206 #endif
207
208 #if __has_attribute(returns_nonnull) || LLVM_GNUC_PREREQ(4, 9, 0)
209 #define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
210 #elif defined(_MSC_VER)
211 #define LLVM_ATTRIBUTE_RETURNS_NONNULL _Ret_notnull_
212 #else
213 #define LLVM_ATTRIBUTE_RETURNS_NONNULL
214 #endif
215
216 /// \macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a
217 /// pointer that does not alias any other valid pointer.
218 #ifdef __GNUC__
219 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
220 #elif defined(_MSC_VER)
221 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
222 #else
223 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS
224 #endif
225
226 /// LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
227 #if __cplusplus > 201402L && __has_cpp_attribute(fallthrough)
228 #define LLVM_FALLTHROUGH [[fallthrough]]
229 #elif __has_cpp_attribute(gnu::fallthrough)
230 #define LLVM_FALLTHROUGH [[gnu::fallthrough]]
231 #elif !__cplusplus
232 // Workaround for llvm.org/PR23435, since clang 3.6 and below emit a spurious
233 // error when __has_cpp_attribute is given a scoped attribute in C mode.
234 #define LLVM_FALLTHROUGH
235 #elif __has_cpp_attribute(clang::fallthrough)
236 #define LLVM_FALLTHROUGH [[clang::fallthrough]]
237 #else
238 #define LLVM_FALLTHROUGH
239 #endif
240
241 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
242 /// pedantic diagnostics.
243 #ifdef __GNUC__
244 #define LLVM_EXTENSION __extension__
245 #else
246 #define LLVM_EXTENSION
247 #endif
248
249 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
250 #if __has_feature(attribute_deprecated_with_message)
251 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
252 decl __attribute__((deprecated(message)))
253 #elif defined(__GNUC__)
254 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
255 decl __attribute__((deprecated))
256 #elif defined(_MSC_VER)
257 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
258 __declspec(deprecated(message)) decl
259 #else
260 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
261 decl
262 #endif
263
264 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
265 /// to an expression which states that it is undefined behavior for the
266 /// compiler to reach this point. Otherwise is not defined.
267 #if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0)
268 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
269 #elif defined(_MSC_VER)
270 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
271 #endif
272
273 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
274 /// which causes the program to exit abnormally.
275 #if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0)
276 # define LLVM_BUILTIN_TRAP __builtin_trap()
277 #elif defined(_MSC_VER)
278 // The __debugbreak intrinsic is supported by MSVC, does not require forward
279 // declarations involving platform-specific typedefs (unlike RaiseException),
280 // results in a call to vectored exception handlers, and encodes to a short
281 // instruction that still causes the trapping behavior we want.
282 # define LLVM_BUILTIN_TRAP __debugbreak()
283 #else
284 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
285 #endif
286
287 /// LLVM_BUILTIN_DEBUGTRAP - On compilers which support it, expands to
288 /// an expression which causes the program to break while running
289 /// under a debugger.
290 #if __has_builtin(__builtin_debugtrap)
291 # define LLVM_BUILTIN_DEBUGTRAP __builtin_debugtrap()
292 #elif defined(_MSC_VER)
293 // The __debugbreak intrinsic is supported by MSVC and breaks while
294 // running under the debugger, and also supports invoking a debugger
295 // when the OS is configured appropriately.
296 # define LLVM_BUILTIN_DEBUGTRAP __debugbreak()
297 #else
298 // Just continue execution when built with compilers that have no
299 // support. This is a debugging aid and not intended to force the
300 // program to abort if encountered.
301 # define LLVM_BUILTIN_DEBUGTRAP
302 #endif
303
304 /// \macro LLVM_ASSUME_ALIGNED
305 /// \brief Returns a pointer with an assumed alignment.
306 #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
307 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
308 #elif defined(LLVM_BUILTIN_UNREACHABLE)
309 // As of today, clang does not support __builtin_assume_aligned.
310 # define LLVM_ASSUME_ALIGNED(p, a) \
311 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
312 #else
313 # define LLVM_ASSUME_ALIGNED(p, a) (p)
314 #endif
315
316 /// \macro LLVM_ALIGNAS
317 /// \brief Used to specify a minimum alignment for a structure or variable.
318 #if __GNUC__ && !__has_feature(cxx_alignas) && !LLVM_GNUC_PREREQ(4, 8, 1)
319 # define LLVM_ALIGNAS(x) __attribute__((aligned(x)))
320 #else
321 # define LLVM_ALIGNAS(x) alignas(x)
322 #endif
323
324 /// \macro LLVM_PACKED
325 /// \brief Used to specify a packed structure.
326 /// LLVM_PACKED(
327 /// struct A {
328 /// int i;
329 /// int j;
330 /// int k;
331 /// long long l;
332 /// });
333 ///
334 /// LLVM_PACKED_START
335 /// struct B {
336 /// int i;
337 /// int j;
338 /// int k;
339 /// long long l;
340 /// };
341 /// LLVM_PACKED_END
342 #ifdef _MSC_VER
343 # define LLVM_PACKED(d) __pragma(pack(push, 1)) d __pragma(pack(pop))
344 # define LLVM_PACKED_START __pragma(pack(push, 1))
345 # define LLVM_PACKED_END __pragma(pack(pop))
346 #else
347 # define LLVM_PACKED(d) d __attribute__((packed))
348 # define LLVM_PACKED_START _Pragma("pack(push, 1)")
349 # define LLVM_PACKED_END _Pragma("pack(pop)")
350 #endif
351
352 /// \macro LLVM_PTR_SIZE
353 /// \brief A constant integer equivalent to the value of sizeof(void*).
354 /// Generally used in combination with LLVM_ALIGNAS or when doing computation in
355 /// the preprocessor.
356 #ifdef __SIZEOF_POINTER__
357 # define LLVM_PTR_SIZE __SIZEOF_POINTER__
358 #elif defined(_WIN64)
359 # define LLVM_PTR_SIZE 8
360 #elif defined(_WIN32)
361 # define LLVM_PTR_SIZE 4
362 #elif defined(_MSC_VER)
363 # error "could not determine LLVM_PTR_SIZE as a constant int for MSVC"
364 #else
365 # define LLVM_PTR_SIZE sizeof(void *)
366 #endif
367
368 /// \macro LLVM_MEMORY_SANITIZER_BUILD
369 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
370 #if __has_feature(memory_sanitizer)
371 # define LLVM_MEMORY_SANITIZER_BUILD 1
372 # include
373 #else
374 # define LLVM_MEMORY_SANITIZER_BUILD 0
375 # define __msan_allocated_memory(p, size)
376 # define __msan_unpoison(p, size)
377 #endif
378
379 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
380 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
381 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
382 # define LLVM_ADDRESS_SANITIZER_BUILD 1
383 # include
384 #else
385 # define LLVM_ADDRESS_SANITIZER_BUILD 0
386 # define __asan_poison_memory_region(p, size)
387 # define __asan_unpoison_memory_region(p, size)
388 #endif
389
390 /// \macro LLVM_THREAD_SANITIZER_BUILD
391 /// \brief Whether LLVM itself is built with ThreadSanitizer instrumentation.
392 #if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
393 # define LLVM_THREAD_SANITIZER_BUILD 1
394 #else
395 # define LLVM_THREAD_SANITIZER_BUILD 0
396 #endif
397
398 #if LLVM_THREAD_SANITIZER_BUILD
399 // Thread Sanitizer is a tool that finds races in code.
400 // See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
401 // tsan detects these exact functions by name.
402 #ifdef __cplusplus
403 extern "C" {
404 #endif
405 void AnnotateHappensAfter(const char *file, int line, const volatile void *cv);
406 void AnnotateHappensBefore(const char *file, int line, const volatile void *cv);
407 void AnnotateIgnoreWritesBegin(const char *file, int line);
408 void AnnotateIgnoreWritesEnd(const char *file, int line);
409 #ifdef __cplusplus
410 }
411 #endif
412
413 // This marker is used to define a happens-before arc. The race detector will
414 // infer an arc from the begin to the end when they share the same pointer
415 // argument.
416 # define TsanHappensBefore(cv) AnnotateHappensBefore(__FILE__, __LINE__, cv)
417
418 // This marker defines the destination of a happens-before arc.
419 # define TsanHappensAfter(cv) AnnotateHappensAfter(__FILE__, __LINE__, cv)
420
421 // Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
422 # define TsanIgnoreWritesBegin() AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
423
424 // Resume checking for racy writes.
425 # define TsanIgnoreWritesEnd() AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
426 #else
427 # define TsanHappensBefore(cv)
428 # define TsanHappensAfter(cv)
429 # define TsanIgnoreWritesBegin()
430 # define TsanIgnoreWritesEnd()
431 #endif
432
433 /// \macro LLVM_NO_SANITIZE
434 /// \brief Disable a particular sanitizer for a function.
435 #if __has_attribute(no_sanitize)
436 #define LLVM_NO_SANITIZE(KIND) __attribute__((no_sanitize(KIND)))
437 #else
438 #define LLVM_NO_SANITIZE(KIND)
439 #endif
440
441 /// \brief Mark debug helper function definitions like dump() that should not be
442 /// stripped from debug builds.
443 /// Note that you should also surround dump() functions with
444 /// `#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)` so they do always
445 /// get stripped in release builds.
446 // FIXME: Move this to a private config.h as it's not usable in public headers.
447 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
448 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
449 #else
450 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
451 #endif
452
453 /// \macro LLVM_PRETTY_FUNCTION
454 /// \brief Gets a user-friendly looking function signature for the current scope
455 /// using the best available method on each platform. The exact format of the
456 /// resulting string is implementation specific and non-portable, so this should
457 /// only be used, for example, for logging or diagnostics.
458 #if defined(_MSC_VER)
459 #define LLVM_PRETTY_FUNCTION __FUNCSIG__
460 #elif defined(__GNUC__) || defined(__clang__)
461 #define LLVM_PRETTY_FUNCTION __PRETTY_FUNCTION__
462 #else
463 #define LLVM_PRETTY_FUNCTION __func__
464 #endif
465
466 /// \macro LLVM_THREAD_LOCAL
467 /// \brief A thread-local storage specifier which can be used with globals,
468 /// extern globals, and static globals.
469 ///
470 /// This is essentially an extremely restricted analog to C++11's thread_local
471 /// support, and uses that when available. However, it falls back on
472 /// platform-specific or vendor-provided extensions when necessary. These
473 /// extensions don't support many of the C++11 thread_local's features. You
474 /// should only use this for PODs that you can statically initialize to
475 /// some constant value. In almost all circumstances this is most appropriate
476 /// for use with a pointer, integer, or small aggregation of pointers and
477 /// integers.
478 #if LLVM_ENABLE_THREADS
479 #if __has_feature(cxx_thread_local)
480 #define LLVM_THREAD_LOCAL thread_local
481 #elif defined(_MSC_VER)
482 // MSVC supports this with a __declspec.
483 #define LLVM_THREAD_LOCAL __declspec(thread)
484 #else
485 // Clang, GCC, and other compatible compilers used __thread prior to C++11 and
486 // we only need the restricted functionality that provides.
487 #define LLVM_THREAD_LOCAL __thread
488 #endif
489 #else // !LLVM_ENABLE_THREADS
490 // If threading is disabled entirely, this compiles to nothing and you get
491 // a normal global variable.
492 #define LLVM_THREAD_LOCAL
493 #endif
494
495 /// \macro LLVM_ENABLE_EXCEPTIONS
496 /// \brief Whether LLVM is built with exception support.
497 #if __has_feature(cxx_exceptions)
498 #define LLVM_ENABLE_EXCEPTIONS 1
499 #elif defined(__GNUC__) && defined(__EXCEPTIONS)
500 #define LLVM_ENABLE_EXCEPTIONS 1
501 #elif defined(_MSC_VER) && defined(_CPPUNWIND)
502 #define LLVM_ENABLE_EXCEPTIONS 1
503 #endif
504
505 #endif
66 //
77 //===----------------------------------------------------------------------===//
88 //
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.
9 // Due to layering constraints (Support depends on Demangler) this is a thin
10 // wrapper around the implementation that lives in llvm-c, though most clients
11 // can/should think of this as being provided by Support for simplicity (not
12 // many clients are aware of their dependency on Demangler/it's a weird place to
13 // own this - but didn't seem to justify splitting Support into "lower support"
14 // and "upper support").
1115 //
1216 //===----------------------------------------------------------------------===//
1317
14 #ifndef LLVM_SUPPORT_COMPILER_H
15 #define LLVM_SUPPORT_COMPILER_H
16
17 #include "llvm/Config/llvm-config.h"
18
19 #if defined(_MSC_VER)
20 #include
21 #endif
22
23 #ifndef __has_feature
24 # define __has_feature(x) 0
25 #endif
26
27 #ifndef __has_extension
28 # define __has_extension(x) 0
29 #endif
30
31 #ifndef __has_attribute
32 # define __has_attribute(x) 0
33 #endif
34
35 #ifndef __has_cpp_attribute
36 # define __has_cpp_attribute(x) 0
37 #endif
38
39 #ifndef __has_builtin
40 # define __has_builtin(x) 0
41 #endif
42
43 /// \macro LLVM_GNUC_PREREQ
44 /// \brief Extend the default __GNUC_PREREQ even if glibc's features.h isn't
45 /// available.
46 #ifndef LLVM_GNUC_PREREQ
47 # if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
48 # define LLVM_GNUC_PREREQ(maj, min, patch) \
49 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
50 ((maj) << 20) + ((min) << 10) + (patch))
51 # elif defined(__GNUC__) && defined(__GNUC_MINOR__)
52 # define LLVM_GNUC_PREREQ(maj, min, patch) \
53 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
54 # else
55 # define LLVM_GNUC_PREREQ(maj, min, patch) 0
56 # endif
57 #endif
58
59 /// \macro LLVM_MSC_PREREQ
60 /// \brief Is the compiler MSVC of at least the specified version?
61 /// The common \param version values to check for are:
62 /// * 1900: Microsoft Visual Studio 2015 / 14.0
63 #ifdef _MSC_VER
64 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
65
66 // We require at least MSVC 2015.
67 #if !LLVM_MSC_PREREQ(1900)
68 #error LLVM requires at least MSVC 2015.
69 #endif
70
71 #else
72 #define LLVM_MSC_PREREQ(version) 0
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 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
96 /// into a shared library, then the class should be private to the library and
97 /// not accessible from outside it. Can also be used to mark variables and
98 /// functions, making them private to any shared library they are linked into.
99 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
100 #if (__has_attribute(visibility) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
101 !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
102 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
103 #else
104 #define LLVM_LIBRARY_VISIBILITY
105 #endif
106
107 #if defined(__GNUC__)
108 #define LLVM_PREFETCH(addr, rw, locality) __builtin_prefetch(addr, rw, locality)
109 #else
110 #define LLVM_PREFETCH(addr, rw, locality)
111 #endif
112
113 #if __has_attribute(used) || LLVM_GNUC_PREREQ(3, 1, 0)
114 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
115 #else
116 #define LLVM_ATTRIBUTE_USED
117 #endif
118
119 /// LLVM_NODISCARD - Warn if a type or return value is discarded.
120 #if __cplusplus > 201402L && __has_cpp_attribute(nodiscard)
121 #define LLVM_NODISCARD [[nodiscard]]
122 #elif !__cplusplus
123 // Workaround for llvm.org/PR23435, since clang 3.6 and below emit a spurious
124 // error when __has_cpp_attribute is given a scoped attribute in C mode.
125 #define LLVM_NODISCARD
126 #elif __has_cpp_attribute(clang::warn_unused_result)
127 #define LLVM_NODISCARD [[clang::warn_unused_result]]
128 #else
129 #define LLVM_NODISCARD
130 #endif
131
132 // Some compilers warn about unused functions. When a function is sometimes
133 // used or not depending on build settings (e.g. a function only called from
134 // within "assert"), this attribute can be used to suppress such warnings.
135 //
136 // However, it shouldn't be used for unused *variables*, as those have a much
137 // more portable solution:
138 // (void)unused_var_name;
139 // Prefer cast-to-void wherever it is sufficient.
140 #if __has_attribute(unused) || LLVM_GNUC_PREREQ(3, 1, 0)
141 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
142 #else
143 #define LLVM_ATTRIBUTE_UNUSED
144 #endif
145
146 // FIXME: Provide this for PE/COFF targets.
147 #if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
148 (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
149 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
150 #else
151 #define LLVM_ATTRIBUTE_WEAK
152 #endif
153
154 // Prior to clang 3.2, clang did not accept any spelling of
155 // __has_attribute(const), so assume it is supported.
156 #if defined(__clang__) || defined(__GNUC__)
157 // aka 'CONST' but following LLVM Conventions.
158 #define LLVM_READNONE __attribute__((__const__))
159 #else
160 #define LLVM_READNONE
161 #endif
162
163 #if __has_attribute(pure) || defined(__GNUC__)
164 // aka 'PURE' but following LLVM Conventions.
165 #define LLVM_READONLY __attribute__((__pure__))
166 #else
167 #define LLVM_READONLY
168 #endif
169
170 #if __has_builtin(__builtin_expect) || LLVM_GNUC_PREREQ(4, 0, 0)
171 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
172 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
173 #else
174 #define LLVM_LIKELY(EXPR) (EXPR)
175 #define LLVM_UNLIKELY(EXPR) (EXPR)
176 #endif
177
178 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
179 /// mark a method "not for inlining".
180 #if __has_attribute(noinline) || LLVM_GNUC_PREREQ(3, 4, 0)
181 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
182 #elif defined(_MSC_VER)
183 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
184 #else
185 #define LLVM_ATTRIBUTE_NOINLINE
186 #endif
187
188 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
189 /// so, mark a method "always inline" because it is performance sensitive. GCC
190 /// 3.4 supported this but is buggy in various cases and produces unimplemented
191 /// errors, just use it in GCC 4.0 and later.
192 #if __has_attribute(always_inline) || LLVM_GNUC_PREREQ(4, 0, 0)
193 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
194 #elif defined(_MSC_VER)
195 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
196 #else
197 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
198 #endif
199
200 #ifdef __GNUC__
201 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
202 #elif defined(_MSC_VER)
203 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
204 #else
205 #define LLVM_ATTRIBUTE_NORETURN
206 #endif
207
208 #if __has_attribute(returns_nonnull) || LLVM_GNUC_PREREQ(4, 9, 0)
209 #define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
210 #elif defined(_MSC_VER)
211 #define LLVM_ATTRIBUTE_RETURNS_NONNULL _Ret_notnull_
212 #else
213 #define LLVM_ATTRIBUTE_RETURNS_NONNULL
214 #endif
215
216 /// \macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a
217 /// pointer that does not alias any other valid pointer.
218 #ifdef __GNUC__
219 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
220 #elif defined(_MSC_VER)
221 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
222 #else
223 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS
224 #endif
225
226 /// LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
227 #if __cplusplus > 201402L && __has_cpp_attribute(fallthrough)
228 #define LLVM_FALLTHROUGH [[fallthrough]]
229 #elif __has_cpp_attribute(gnu::fallthrough)
230 #define LLVM_FALLTHROUGH [[gnu::fallthrough]]
231 #elif !__cplusplus
232 // Workaround for llvm.org/PR23435, since clang 3.6 and below emit a spurious
233 // error when __has_cpp_attribute is given a scoped attribute in C mode.
234 #define LLVM_FALLTHROUGH
235 #elif __has_cpp_attribute(clang::fallthrough)
236 #define LLVM_FALLTHROUGH [[clang::fallthrough]]
237 #else
238 #define LLVM_FALLTHROUGH
239 #endif
240
241 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
242 /// pedantic diagnostics.
243 #ifdef __GNUC__
244 #define LLVM_EXTENSION __extension__
245 #else
246 #define LLVM_EXTENSION
247 #endif
248
249 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
250 #if __has_feature(attribute_deprecated_with_message)
251 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
252 decl __attribute__((deprecated(message)))
253 #elif defined(__GNUC__)
254 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
255 decl __attribute__((deprecated))
256 #elif defined(_MSC_VER)
257 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
258 __declspec(deprecated(message)) decl
259 #else
260 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
261 decl
262 #endif
263
264 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
265 /// to an expression which states that it is undefined behavior for the
266 /// compiler to reach this point. Otherwise is not defined.
267 #if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0)
268 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
269 #elif defined(_MSC_VER)
270 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
271 #endif
272
273 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
274 /// which causes the program to exit abnormally.
275 #if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0)
276 # define LLVM_BUILTIN_TRAP __builtin_trap()
277 #elif defined(_MSC_VER)
278 // The __debugbreak intrinsic is supported by MSVC, does not require forward
279 // declarations involving platform-specific typedefs (unlike RaiseException),
280 // results in a call to vectored exception handlers, and encodes to a short
281 // instruction that still causes the trapping behavior we want.
282 # define LLVM_BUILTIN_TRAP __debugbreak()
283 #else
284 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
285 #endif
286
287 /// LLVM_BUILTIN_DEBUGTRAP - On compilers which support it, expands to
288 /// an expression which causes the program to break while running
289 /// under a debugger.
290 #if __has_builtin(__builtin_debugtrap)
291 # define LLVM_BUILTIN_DEBUGTRAP __builtin_debugtrap()
292 #elif defined(_MSC_VER)
293 // The __debugbreak intrinsic is supported by MSVC and breaks while
294 // running under the debugger, and also supports invoking a debugger
295 // when the OS is configured appropriately.
296 # define LLVM_BUILTIN_DEBUGTRAP __debugbreak()
297 #else
298 // Just continue execution when built with compilers that have no
299 // support. This is a debugging aid and not intended to force the
300 // program to abort if encountered.
301 # define LLVM_BUILTIN_DEBUGTRAP
302 #endif
303
304 /// \macro LLVM_ASSUME_ALIGNED
305 /// \brief Returns a pointer with an assumed alignment.
306 #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
307 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
308 #elif defined(LLVM_BUILTIN_UNREACHABLE)
309 // As of today, clang does not support __builtin_assume_aligned.
310 # define LLVM_ASSUME_ALIGNED(p, a) \
311 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
312 #else
313 # define LLVM_ASSUME_ALIGNED(p, a) (p)
314 #endif
315
316 /// \macro LLVM_ALIGNAS
317 /// \brief Used to specify a minimum alignment for a structure or variable.
318 #if __GNUC__ && !__has_feature(cxx_alignas) && !LLVM_GNUC_PREREQ(4, 8, 1)
319 # define LLVM_ALIGNAS(x) __attribute__((aligned(x)))
320 #else
321 # define LLVM_ALIGNAS(x) alignas(x)
322 #endif
323
324 /// \macro LLVM_PACKED
325 /// \brief Used to specify a packed structure.
326 /// LLVM_PACKED(
327 /// struct A {
328 /// int i;
329 /// int j;
330 /// int k;
331 /// long long l;
332 /// });
333 ///
334 /// LLVM_PACKED_START
335 /// struct B {
336 /// int i;
337 /// int j;
338 /// int k;
339 /// long long l;
340 /// };
341 /// LLVM_PACKED_END
342 #ifdef _MSC_VER
343 # define LLVM_PACKED(d) __pragma(pack(push, 1)) d __pragma(pack(pop))
344 # define LLVM_PACKED_START __pragma(pack(push, 1))
345 # define LLVM_PACKED_END __pragma(pack(pop))
346 #else
347 # define LLVM_PACKED(d) d __attribute__((packed))
348 # define LLVM_PACKED_START _Pragma("pack(push, 1)")
349 # define LLVM_PACKED_END _Pragma("pack(pop)")
350 #endif
351
352 /// \macro LLVM_PTR_SIZE
353 /// \brief A constant integer equivalent to the value of sizeof(void*).
354 /// Generally used in combination with LLVM_ALIGNAS or when doing computation in
355 /// the preprocessor.
356 #ifdef __SIZEOF_POINTER__
357 # define LLVM_PTR_SIZE __SIZEOF_POINTER__
358 #elif defined(_WIN64)
359 # define LLVM_PTR_SIZE 8
360 #elif defined(_WIN32)
361 # define LLVM_PTR_SIZE 4
362 #elif defined(_MSC_VER)
363 # error "could not determine LLVM_PTR_SIZE as a constant int for MSVC"
364 #else
365 # define LLVM_PTR_SIZE sizeof(void *)
366 #endif
367
368 /// \macro LLVM_MEMORY_SANITIZER_BUILD
369 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
370 #if __has_feature(memory_sanitizer)
371 # define LLVM_MEMORY_SANITIZER_BUILD 1
372 # include
373 #else
374 # define LLVM_MEMORY_SANITIZER_BUILD 0
375 # define __msan_allocated_memory(p, size)
376 # define __msan_unpoison(p, size)
377 #endif
378
379 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
380 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
381 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
382 # define LLVM_ADDRESS_SANITIZER_BUILD 1
383 # include
384 #else
385 # define LLVM_ADDRESS_SANITIZER_BUILD 0
386 # define __asan_poison_memory_region(p, size)
387 # define __asan_unpoison_memory_region(p, size)
388 #endif
389
390 /// \macro LLVM_THREAD_SANITIZER_BUILD
391 /// \brief Whether LLVM itself is built with ThreadSanitizer instrumentation.
392 #if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
393 # define LLVM_THREAD_SANITIZER_BUILD 1
394 #else
395 # define LLVM_THREAD_SANITIZER_BUILD 0
396 #endif
397
398 #if LLVM_THREAD_SANITIZER_BUILD
399 // Thread Sanitizer is a tool that finds races in code.
400 // See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
401 // tsan detects these exact functions by name.
402 #ifdef __cplusplus
403 extern "C" {
404 #endif
405 void AnnotateHappensAfter(const char *file, int line, const volatile void *cv);
406 void AnnotateHappensBefore(const char *file, int line, const volatile void *cv);
407 void AnnotateIgnoreWritesBegin(const char *file, int line);
408 void AnnotateIgnoreWritesEnd(const char *file, int line);
409 #ifdef __cplusplus
410 }
411 #endif
412
413 // This marker is used to define a happens-before arc. The race detector will
414 // infer an arc from the begin to the end when they share the same pointer
415 // argument.
416 # define TsanHappensBefore(cv) AnnotateHappensBefore(__FILE__, __LINE__, cv)
417
418 // This marker defines the destination of a happens-before arc.
419 # define TsanHappensAfter(cv) AnnotateHappensAfter(__FILE__, __LINE__, cv)
420
421 // Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
422 # define TsanIgnoreWritesBegin() AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
423
424 // Resume checking for racy writes.
425 # define TsanIgnoreWritesEnd() AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
426 #else
427 # define TsanHappensBefore(cv)
428 # define TsanHappensAfter(cv)
429 # define TsanIgnoreWritesBegin()
430 # define TsanIgnoreWritesEnd()
431 #endif
432
433 /// \macro LLVM_NO_SANITIZE
434 /// \brief Disable a particular sanitizer for a function.
435 #if __has_attribute(no_sanitize)
436 #define LLVM_NO_SANITIZE(KIND) __attribute__((no_sanitize(KIND)))
437 #else
438 #define LLVM_NO_SANITIZE(KIND)
439 #endif
440
441 /// \brief Mark debug helper function definitions like dump() that should not be
442 /// stripped from debug builds.
443 /// Note that you should also surround dump() functions with
444 /// `#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)` so they do always
445 /// get stripped in release builds.
446 // FIXME: Move this to a private config.h as it's not usable in public headers.
447 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
448 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
449 #else
450 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
451 #endif
452
453 /// \macro LLVM_PRETTY_FUNCTION
454 /// \brief Gets a user-friendly looking function signature for the current scope
455 /// using the best available method on each platform. The exact format of the
456 /// resulting string is implementation specific and non-portable, so this should
457 /// only be used, for example, for logging or diagnostics.
458 #if defined(_MSC_VER)
459 #define LLVM_PRETTY_FUNCTION __FUNCSIG__
460 #elif defined(__GNUC__) || defined(__clang__)
461 #define LLVM_PRETTY_FUNCTION __PRETTY_FUNCTION__
462 #else
463 #define LLVM_PRETTY_FUNCTION __func__
464 #endif
465
466 /// \macro LLVM_THREAD_LOCAL
467 /// \brief A thread-local storage specifier which can be used with globals,
468 /// extern globals, and static globals.
469 ///
470 /// This is essentially an extremely restricted analog to C++11's thread_local
471 /// support, and uses that when available. However, it falls back on
472 /// platform-specific or vendor-provided extensions when necessary. These
473 /// extensions don't support many of the C++11 thread_local's features. You
474 /// should only use this for PODs that you can statically initialize to
475 /// some constant value. In almost all circumstances this is most appropriate
476 /// for use with a pointer, integer, or small aggregation of pointers and
477 /// integers.
478 #if LLVM_ENABLE_THREADS
479 #if __has_feature(cxx_thread_local)
480 #define LLVM_THREAD_LOCAL thread_local
481 #elif defined(_MSC_VER)
482 // MSVC supports this with a __declspec.
483 #define LLVM_THREAD_LOCAL __declspec(thread)
484 #else
485 // Clang, GCC, and other compatible compilers used __thread prior to C++11 and
486 // we only need the restricted functionality that provides.
487 #define LLVM_THREAD_LOCAL __thread
488 #endif
489 #else // !LLVM_ENABLE_THREADS
490 // If threading is disabled entirely, this compiles to nothing and you get
491 // a normal global variable.
492 #define LLVM_THREAD_LOCAL
493 #endif
494
495 /// \macro LLVM_ENABLE_EXCEPTIONS
496 /// \brief Whether LLVM is built with exception support.
497 #if __has_feature(cxx_exceptions)
498 #define LLVM_ENABLE_EXCEPTIONS 1
499 #elif defined(__GNUC__) && defined(__EXCEPTIONS)
500 #define LLVM_ENABLE_EXCEPTIONS 1
501 #elif defined(_MSC_VER) && defined(_CPPUNWIND)
502 #define LLVM_ENABLE_EXCEPTIONS 1
503 #endif
504
505 #endif
18 #include "llvm/Demangle/Compiler.h"
1313 // - All C++14 and C++17 features
1414
1515 #include "llvm/Demangle/Demangle.h"
16 #include "llvm/Support/Compiler.h"
17 #include
16
17 #include "llvm/Demangle/Compiler.h"
18
1819 #include
19 #include
2020 #include
21 #include
2122 #include
2223 #include
2324 #include
24 #include <cctype>
25 #include <numeric>
26 #include
2527
2628 #ifdef _MSC_VER
2729 // snprintf is implemented in VS 2015