OSDN Git Service

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