comparison include/llvm/Support/Compiler.h @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
comparison
equal deleted inserted replaced
146:3fc4d5c3e21e 148:63bd29f05246
1 //===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===// 1 //===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // 4 // See https://llvm.org/LICENSE.txt for license information.
5 // This file is distributed under the University of Illinois Open Source 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 // License. See LICENSE.TXT for details.
7 // 6 //
8 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
9 // 8 //
10 // This file defines several macros, based on the current compiler. This allows 9 // This file defines several macros, based on the current compiler. This allows
11 // use of compiler-specific features in a way that remains portable. 10 // use of compiler-specific features in a way that remains portable.
15 #ifndef LLVM_SUPPORT_COMPILER_H 14 #ifndef LLVM_SUPPORT_COMPILER_H
16 #define LLVM_SUPPORT_COMPILER_H 15 #define LLVM_SUPPORT_COMPILER_H
17 16
18 #include "llvm/Config/llvm-config.h" 17 #include "llvm/Config/llvm-config.h"
19 18
19 #include <new>
20 #include <stddef.h>
21
20 #if defined(_MSC_VER) 22 #if defined(_MSC_VER)
21 #include <sal.h> 23 #include <sal.h>
22 #endif 24 #endif
23 25
24 #ifndef __has_feature 26 #ifndef __has_feature
40 #ifndef __has_builtin 42 #ifndef __has_builtin
41 # define __has_builtin(x) 0 43 # define __has_builtin(x) 0
42 #endif 44 #endif
43 45
44 /// \macro LLVM_GNUC_PREREQ 46 /// \macro LLVM_GNUC_PREREQ
45 /// \brief Extend the default __GNUC_PREREQ even if glibc's features.h isn't 47 /// Extend the default __GNUC_PREREQ even if glibc's features.h isn't
46 /// available. 48 /// available.
47 #ifndef LLVM_GNUC_PREREQ 49 #ifndef LLVM_GNUC_PREREQ
48 # if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) 50 # if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
49 # define LLVM_GNUC_PREREQ(maj, min, patch) \ 51 # define LLVM_GNUC_PREREQ(maj, min, patch) \
50 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \ 52 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
56 # define LLVM_GNUC_PREREQ(maj, min, patch) 0 58 # define LLVM_GNUC_PREREQ(maj, min, patch) 0
57 # endif 59 # endif
58 #endif 60 #endif
59 61
60 /// \macro LLVM_MSC_PREREQ 62 /// \macro LLVM_MSC_PREREQ
61 /// \brief Is the compiler MSVC of at least the specified version? 63 /// Is the compiler MSVC of at least the specified version?
62 /// The common \param version values to check for are: 64 /// The common \param version values to check for are:
63 /// * 1900: Microsoft Visual Studio 2015 / 14.0 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
64 #ifdef _MSC_VER 74 #ifdef _MSC_VER
65 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version)) 75 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
66 76
67 // We require at least MSVC 2015. 77 // We require at least MSVC 2017.
68 #if !LLVM_MSC_PREREQ(1900) 78 #if !LLVM_MSC_PREREQ(1910)
69 #error LLVM requires at least MSVC 2015. 79 #error LLVM requires at least MSVC 2017.
70 #endif 80 #endif
71 81
72 #else 82 #else
73 #define LLVM_MSC_PREREQ(version) 0 83 #define LLVM_MSC_PREREQ(version) 0
74 #endif 84 #endif
75 85
76 /// \brief Does the compiler support ref-qualifiers for *this? 86 /// Does the compiler support ref-qualifiers for *this?
77 /// 87 ///
78 /// Sadly, this is separate from just rvalue reference support because GCC 88 /// Sadly, this is separate from just rvalue reference support because GCC
79 /// and MSVC implemented this later than everything else. 89 /// and MSVC implemented this later than everything else.
80 #if __has_feature(cxx_rvalue_references) || LLVM_GNUC_PREREQ(4, 8, 1) 90 #if __has_feature(cxx_rvalue_references) || LLVM_GNUC_PREREQ(4, 8, 1)
81 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1 91 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
97 /// into a shared library, then the class should be private to the library and 107 /// 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 108 /// 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. 109 /// 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. 110 /// 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)) && \ 111 #if (__has_attribute(visibility) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
102 !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32) 112 !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_WIN32)
103 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden"))) 113 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
104 #else 114 #else
105 #define LLVM_LIBRARY_VISIBILITY 115 #define LLVM_LIBRARY_VISIBILITY
106 #endif 116 #endif
107 117
126 #define LLVM_NODISCARD 136 #define LLVM_NODISCARD
127 #elif __has_cpp_attribute(clang::warn_unused_result) 137 #elif __has_cpp_attribute(clang::warn_unused_result)
128 #define LLVM_NODISCARD [[clang::warn_unused_result]] 138 #define LLVM_NODISCARD [[clang::warn_unused_result]]
129 #else 139 #else
130 #define LLVM_NODISCARD 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
131 #endif 154 #endif
132 155
133 // Some compilers warn about unused functions. When a function is sometimes 156 // 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 157 // 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. 158 // within "assert"), this attribute can be used to suppress such warnings.
144 #define LLVM_ATTRIBUTE_UNUSED 167 #define LLVM_ATTRIBUTE_UNUSED
145 #endif 168 #endif
146 169
147 // FIXME: Provide this for PE/COFF targets. 170 // FIXME: Provide this for PE/COFF targets.
148 #if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) && \ 171 #if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
149 (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)) 172 (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_WIN32))
150 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__)) 173 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
151 #else 174 #else
152 #define LLVM_ATTRIBUTE_WEAK 175 #define LLVM_ATTRIBUTE_WEAK
153 #endif 176 #endif
154 177
237 #define LLVM_FALLTHROUGH [[clang::fallthrough]] 260 #define LLVM_FALLTHROUGH [[clang::fallthrough]]
238 #else 261 #else
239 #define LLVM_FALLTHROUGH 262 #define LLVM_FALLTHROUGH
240 #endif 263 #endif
241 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
242 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress 274 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
243 /// pedantic diagnostics. 275 /// pedantic diagnostics.
244 #ifdef __GNUC__ 276 #ifdef __GNUC__
245 #define LLVM_EXTENSION __extension__ 277 #define LLVM_EXTENSION __extension__
246 #else 278 #else
301 // program to abort if encountered. 333 // program to abort if encountered.
302 # define LLVM_BUILTIN_DEBUGTRAP 334 # define LLVM_BUILTIN_DEBUGTRAP
303 #endif 335 #endif
304 336
305 /// \macro LLVM_ASSUME_ALIGNED 337 /// \macro LLVM_ASSUME_ALIGNED
306 /// \brief Returns a pointer with an assumed alignment. 338 /// Returns a pointer with an assumed alignment.
307 #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0) 339 #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
308 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a) 340 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
309 #elif defined(LLVM_BUILTIN_UNREACHABLE) 341 #elif defined(LLVM_BUILTIN_UNREACHABLE)
310 // As of today, clang does not support __builtin_assume_aligned. 342 // As of today, clang does not support __builtin_assume_aligned.
311 # define LLVM_ASSUME_ALIGNED(p, a) \ 343 # define LLVM_ASSUME_ALIGNED(p, a) \
312 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p))) 344 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
313 #else 345 #else
314 # define LLVM_ASSUME_ALIGNED(p, a) (p) 346 # define LLVM_ASSUME_ALIGNED(p, a) (p)
315 #endif 347 #endif
316 348
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 349 /// \macro LLVM_PACKED
326 /// \brief Used to specify a packed structure. 350 /// Used to specify a packed structure.
327 /// LLVM_PACKED( 351 /// LLVM_PACKED(
328 /// struct A { 352 /// struct A {
329 /// int i; 353 /// int i;
330 /// int j; 354 /// int j;
331 /// int k; 355 /// int k;
349 # define LLVM_PACKED_START _Pragma("pack(push, 1)") 373 # define LLVM_PACKED_START _Pragma("pack(push, 1)")
350 # define LLVM_PACKED_END _Pragma("pack(pop)") 374 # define LLVM_PACKED_END _Pragma("pack(pop)")
351 #endif 375 #endif
352 376
353 /// \macro LLVM_PTR_SIZE 377 /// \macro LLVM_PTR_SIZE
354 /// \brief A constant integer equivalent to the value of sizeof(void*). 378 /// A constant integer equivalent to the value of sizeof(void*).
355 /// Generally used in combination with LLVM_ALIGNAS or when doing computation in 379 /// Generally used in combination with alignas or when doing computation in the
356 /// the preprocessor. 380 /// preprocessor.
357 #ifdef __SIZEOF_POINTER__ 381 #ifdef __SIZEOF_POINTER__
358 # define LLVM_PTR_SIZE __SIZEOF_POINTER__ 382 # define LLVM_PTR_SIZE __SIZEOF_POINTER__
359 #elif defined(_WIN64) 383 #elif defined(_WIN64)
360 # define LLVM_PTR_SIZE 8 384 # define LLVM_PTR_SIZE 8
361 #elif defined(_WIN32) 385 #elif defined(_WIN32)
365 #else 389 #else
366 # define LLVM_PTR_SIZE sizeof(void *) 390 # define LLVM_PTR_SIZE sizeof(void *)
367 #endif 391 #endif
368 392
369 /// \macro LLVM_MEMORY_SANITIZER_BUILD 393 /// \macro LLVM_MEMORY_SANITIZER_BUILD
370 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation. 394 /// Whether LLVM itself is built with MemorySanitizer instrumentation.
371 #if __has_feature(memory_sanitizer) 395 #if __has_feature(memory_sanitizer)
372 # define LLVM_MEMORY_SANITIZER_BUILD 1 396 # define LLVM_MEMORY_SANITIZER_BUILD 1
373 # include <sanitizer/msan_interface.h> 397 # include <sanitizer/msan_interface.h>
374 #else 398 #else
375 # define LLVM_MEMORY_SANITIZER_BUILD 0 399 # define LLVM_MEMORY_SANITIZER_BUILD 0
376 # define __msan_allocated_memory(p, size) 400 # define __msan_allocated_memory(p, size)
377 # define __msan_unpoison(p, size) 401 # define __msan_unpoison(p, size)
378 #endif 402 #endif
379 403
380 /// \macro LLVM_ADDRESS_SANITIZER_BUILD 404 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
381 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation. 405 /// Whether LLVM itself is built with AddressSanitizer instrumentation.
382 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) 406 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
383 # define LLVM_ADDRESS_SANITIZER_BUILD 1 407 # define LLVM_ADDRESS_SANITIZER_BUILD 1
384 # include <sanitizer/asan_interface.h> 408 # include <sanitizer/asan_interface.h>
385 #else 409 #else
386 # define LLVM_ADDRESS_SANITIZER_BUILD 0 410 # define LLVM_ADDRESS_SANITIZER_BUILD 0
387 # define __asan_poison_memory_region(p, size) 411 # define __asan_poison_memory_region(p, size)
388 # define __asan_unpoison_memory_region(p, size) 412 # define __asan_unpoison_memory_region(p, size)
389 #endif 413 #endif
390 414
391 /// \macro LLVM_THREAD_SANITIZER_BUILD 415 /// \macro LLVM_THREAD_SANITIZER_BUILD
392 /// \brief Whether LLVM itself is built with ThreadSanitizer instrumentation. 416 /// Whether LLVM itself is built with ThreadSanitizer instrumentation.
393 #if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__) 417 #if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
394 # define LLVM_THREAD_SANITIZER_BUILD 1 418 # define LLVM_THREAD_SANITIZER_BUILD 1
395 #else 419 #else
396 # define LLVM_THREAD_SANITIZER_BUILD 0 420 # define LLVM_THREAD_SANITIZER_BUILD 0
397 #endif 421 #endif
430 # define TsanIgnoreWritesBegin() 454 # define TsanIgnoreWritesBegin()
431 # define TsanIgnoreWritesEnd() 455 # define TsanIgnoreWritesEnd()
432 #endif 456 #endif
433 457
434 /// \macro LLVM_NO_SANITIZE 458 /// \macro LLVM_NO_SANITIZE
435 /// \brief Disable a particular sanitizer for a function. 459 /// Disable a particular sanitizer for a function.
436 #if __has_attribute(no_sanitize) 460 #if __has_attribute(no_sanitize)
437 #define LLVM_NO_SANITIZE(KIND) __attribute__((no_sanitize(KIND))) 461 #define LLVM_NO_SANITIZE(KIND) __attribute__((no_sanitize(KIND)))
438 #else 462 #else
439 #define LLVM_NO_SANITIZE(KIND) 463 #define LLVM_NO_SANITIZE(KIND)
440 #endif 464 #endif
441 465
442 /// \brief Mark debug helper function definitions like dump() that should not be 466 /// Mark debug helper function definitions like dump() that should not be
443 /// stripped from debug builds. 467 /// stripped from debug builds.
444 /// Note that you should also surround dump() functions with 468 /// Note that you should also surround dump() functions with
445 /// `#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)` so they do always 469 /// `#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)` so they do always
446 /// get stripped in release builds. 470 /// get stripped in release builds.
447 // FIXME: Move this to a private config.h as it's not usable in public headers. 471 // FIXME: Move this to a private config.h as it's not usable in public headers.
450 #else 474 #else
451 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE 475 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
452 #endif 476 #endif
453 477
454 /// \macro LLVM_PRETTY_FUNCTION 478 /// \macro LLVM_PRETTY_FUNCTION
455 /// \brief Gets a user-friendly looking function signature for the current scope 479 /// 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 480 /// 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 481 /// resulting string is implementation specific and non-portable, so this should
458 /// only be used, for example, for logging or diagnostics. 482 /// only be used, for example, for logging or diagnostics.
459 #if defined(_MSC_VER) 483 #if defined(_MSC_VER)
460 #define LLVM_PRETTY_FUNCTION __FUNCSIG__ 484 #define LLVM_PRETTY_FUNCTION __FUNCSIG__
463 #else 487 #else
464 #define LLVM_PRETTY_FUNCTION __func__ 488 #define LLVM_PRETTY_FUNCTION __func__
465 #endif 489 #endif
466 490
467 /// \macro LLVM_THREAD_LOCAL 491 /// \macro LLVM_THREAD_LOCAL
468 /// \brief A thread-local storage specifier which can be used with globals, 492 /// A thread-local storage specifier which can be used with globals,
469 /// extern globals, and static globals. 493 /// extern globals, and static globals.
470 /// 494 ///
471 /// This is essentially an extremely restricted analog to C++11's thread_local 495 /// 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 496 /// support, and uses that when available. However, it falls back on
473 /// platform-specific or vendor-provided extensions when necessary. These 497 /// platform-specific or vendor-provided extensions when necessary. These
492 // a normal global variable. 516 // a normal global variable.
493 #define LLVM_THREAD_LOCAL 517 #define LLVM_THREAD_LOCAL
494 #endif 518 #endif
495 519
496 /// \macro LLVM_ENABLE_EXCEPTIONS 520 /// \macro LLVM_ENABLE_EXCEPTIONS
497 /// \brief Whether LLVM is built with exception support. 521 /// Whether LLVM is built with exception support.
498 #if __has_feature(cxx_exceptions) 522 #if __has_feature(cxx_exceptions)
499 #define LLVM_ENABLE_EXCEPTIONS 1 523 #define LLVM_ENABLE_EXCEPTIONS 1
500 #elif defined(__GNUC__) && defined(__EXCEPTIONS) 524 #elif defined(__GNUC__) && defined(__EXCEPTIONS)
501 #define LLVM_ENABLE_EXCEPTIONS 1 525 #define LLVM_ENABLE_EXCEPTIONS 1
502 #elif defined(_MSC_VER) && defined(_CPPUNWIND) 526 #elif defined(_MSC_VER) && defined(_CPPUNWIND)
503 #define LLVM_ENABLE_EXCEPTIONS 1 527 #define LLVM_ENABLE_EXCEPTIONS 1
504 #endif 528 #endif
505 529
506 #endif 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