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