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