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