libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

test_macros.h (9334B)


      1 // -*- C++ -*-
      2 //===---------------------------- test_macros.h ---------------------------===//
      3 //
      4 //                     The LLVM Compiler Infrastructure
      5 //
      6 // This file is dual licensed under the MIT and the University of Illinois Open
      7 // Source Licenses. See LICENSE.TXT for details.
      8 //
      9 //===----------------------------------------------------------------------===//
     10 
     11 #ifndef SUPPORT_TEST_MACROS_HPP
     12 #define SUPPORT_TEST_MACROS_HPP
     13 
     14 // Attempt to get STL specific macros like _LIBCPP_VERSION using the most
     15 // minimal header possible. If we're testing libc++, we should use `<__config>`.
     16 // If <__config> isn't available, fall back to <ciso646>.
     17 #ifdef __has_include
     18 # if __has_include("<__config>")
     19 #   include <__config>
     20 #   define TEST_IMP_INCLUDED_HEADER
     21 # endif
     22 #endif
     23 #ifndef TEST_IMP_INCLUDED_HEADER
     24 #include <ciso646>
     25 #endif
     26 
     27 #if defined(__GNUC__)
     28 #pragma GCC diagnostic push
     29 #pragma GCC diagnostic ignored "-Wvariadic-macros"
     30 #endif
     31 
     32 #define TEST_CONCAT1(X, Y) X##Y
     33 #define TEST_CONCAT(X, Y) TEST_CONCAT1(X, Y)
     34 
     35 #ifdef __has_feature
     36 #define TEST_HAS_FEATURE(X) __has_feature(X)
     37 #else
     38 #define TEST_HAS_FEATURE(X) 0
     39 #endif
     40 
     41 #ifndef __has_include
     42 #define __has_include(...) 0
     43 #endif
     44 
     45 #ifdef __has_extension
     46 #define TEST_HAS_EXTENSION(X) __has_extension(X)
     47 #else
     48 #define TEST_HAS_EXTENSION(X) 0
     49 #endif
     50 
     51 #ifdef __has_builtin
     52 #define TEST_HAS_BUILTIN(X) __has_builtin(X)
     53 #else
     54 #define TEST_HAS_BUILTIN(X) 0
     55 #endif
     56 #ifdef __is_identifier
     57 // '__is_identifier' returns '0' if '__x' is a reserved identifier provided by
     58 // the compiler and '1' otherwise.
     59 #define TEST_HAS_BUILTIN_IDENTIFIER(X) !__is_identifier(X)
     60 #else
     61 #define TEST_HAS_BUILTIN_IDENTIFIER(X) 0
     62 #endif
     63 
     64 #if defined(__EDG__)
     65 # define TEST_COMPILER_EDG
     66 #elif defined(__clang__)
     67 # define TEST_COMPILER_CLANG
     68 # if defined(__apple_build_version__)
     69 #  define TEST_COMPILER_APPLE_CLANG
     70 # endif
     71 #elif defined(_MSC_VER)
     72 # define TEST_COMPILER_C1XX
     73 #elif defined(__GNUC__)
     74 # define TEST_COMPILER_GCC
     75 #endif
     76 
     77 #if defined(__apple_build_version__)
     78 #define TEST_APPLE_CLANG_VER (__clang_major__ * 100) + __clang_minor__
     79 #elif defined(__clang_major__)
     80 #define TEST_CLANG_VER (__clang_major__ * 100) + __clang_minor__
     81 #elif defined(__GNUC__)
     82 #define TEST_GCC_VER (__GNUC__ * 100 + __GNUC_MINOR__)
     83 #define TEST_GCC_VER_NEW (TEST_GCC_VER * 10 + __GNUC_PATCHLEVEL__)
     84 #endif
     85 
     86 /* Make a nice name for the standard version */
     87 #ifndef TEST_STD_VER
     88 #if  __cplusplus <= 199711L
     89 # define TEST_STD_VER 3
     90 #elif __cplusplus <= 201103L
     91 # define TEST_STD_VER 11
     92 #elif __cplusplus <= 201402L
     93 # define TEST_STD_VER 14
     94 #elif __cplusplus <= 201703L
     95 # define TEST_STD_VER 17
     96 #else
     97 # define TEST_STD_VER 99    // greater than current standard
     98 // This is deliberately different than _LIBCPP_STD_VER to discourage matching them up.
     99 #endif
    100 #endif
    101 
    102 // Attempt to deduce the GLIBC version
    103 #if (defined(__has_include) && __has_include(<features.h>)) || \
    104     defined(__linux__)
    105 #include <features.h>
    106 #if defined(__GLIBC_PREREQ)
    107 #define TEST_HAS_GLIBC
    108 #define TEST_GLIBC_PREREQ(major, minor) __GLIBC_PREREQ(major, minor)
    109 #endif
    110 #endif
    111 
    112 #if TEST_STD_VER >= 11
    113 #define TEST_ALIGNOF(...) alignof(__VA_ARGS__)
    114 #define TEST_ALIGNAS(...) alignas(__VA_ARGS__)
    115 #define TEST_CONSTEXPR constexpr
    116 #define TEST_NOEXCEPT noexcept
    117 #define TEST_NOEXCEPT_FALSE noexcept(false)
    118 #define TEST_NOEXCEPT_COND(...) noexcept(__VA_ARGS__)
    119 # if TEST_STD_VER >= 14
    120 #   define TEST_CONSTEXPR_CXX14 constexpr
    121 # else
    122 #   define TEST_CONSTEXPR_CXX14
    123 # endif
    124 # if TEST_STD_VER > 14
    125 #   define TEST_THROW_SPEC(...)
    126 # else
    127 #   define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
    128 # endif
    129 #else
    130 #if defined(TEST_COMPILER_CLANG)
    131 # define TEST_ALIGNOF(...) _Alignof(__VA_ARGS__)
    132 #else
    133 # define TEST_ALIGNOF(...) __alignof(__VA_ARGS__)
    134 #endif
    135 #define TEST_ALIGNAS(...) __attribute__((__aligned__(__VA_ARGS__)))
    136 #define TEST_CONSTEXPR
    137 #define TEST_CONSTEXPR_CXX14
    138 #define TEST_NOEXCEPT throw()
    139 #define TEST_NOEXCEPT_FALSE
    140 #define TEST_NOEXCEPT_COND(...)
    141 #define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
    142 #endif
    143 
    144 // Sniff out to see if the underling C library has C11 features
    145 // Note that at this time (July 2018), MacOS X and iOS do NOT.
    146 // This is cribbed from __config; but lives here as well because we can't assume libc++
    147 #if __ISO_C_VISIBLE >= 2011 || TEST_STD_VER >= 11
    148 #  if defined(__FreeBSD__)
    149 //  Specifically, FreeBSD does NOT have timespec_get, even though they have all
    150 //  the rest of C11 - this is PR#38495
    151 #    define TEST_HAS_C11_FEATURES
    152 #  elif defined(__Fuchsia__)
    153 #    define TEST_HAS_C11_FEATURES
    154 #    define TEST_HAS_TIMESPEC_GET
    155 #  elif defined(__linux__)
    156 // This block preserves the old behavior used by include/__config:
    157 // _LIBCPP_GLIBC_PREREQ would be defined to 0 if __GLIBC_PREREQ was not
    158 // available. The configuration here may be too vague though, as Bionic, uClibc,
    159 // newlib, etc may all support these features but need to be configured.
    160 #    if defined(TEST_GLIBC_PREREQ)
    161 #      if TEST_GLIBC_PREREQ(2, 17)
    162 #        define TEST_HAS_TIMESPEC_GET
    163 #        define TEST_HAS_C11_FEATURES
    164 #      endif
    165 #    elif defined(_LIBCPP_HAS_MUSL_LIBC)
    166 #      define TEST_HAS_C11_FEATURES
    167 #      define TEST_HAS_TIMESPEC_GET
    168 #    endif
    169 #  elif defined(_WIN32)
    170 #    if defined(_MSC_VER) && !defined(__MINGW32__)
    171 #      define TEST_HAS_C11_FEATURES // Using Microsoft's C Runtime library
    172 #      define TEST_HAS_TIMESPEC_GET
    173 #    endif
    174 #  endif
    175 #endif
    176 
    177 /* Features that were introduced in C++14 */
    178 #if TEST_STD_VER >= 14
    179 #define TEST_HAS_EXTENDED_CONSTEXPR
    180 #define TEST_HAS_VARIABLE_TEMPLATES
    181 #endif
    182 
    183 /* Features that were introduced in C++17 */
    184 #if TEST_STD_VER >= 17
    185 #endif
    186 
    187 /* Features that were introduced after C++17 */
    188 #if TEST_STD_VER > 17
    189 #endif
    190 
    191 
    192 #define TEST_ALIGNAS_TYPE(...) TEST_ALIGNAS(TEST_ALIGNOF(__VA_ARGS__))
    193 
    194 #if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cpp_rtti) \
    195     && !defined(__GXX_RTTI)
    196 #define TEST_HAS_NO_RTTI
    197 #endif
    198 
    199 #if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \
    200      && !defined(__EXCEPTIONS)
    201 #define TEST_HAS_NO_EXCEPTIONS
    202 #endif
    203 
    204 #if TEST_HAS_FEATURE(address_sanitizer) || TEST_HAS_FEATURE(memory_sanitizer) || \
    205     TEST_HAS_FEATURE(thread_sanitizer)
    206 #define TEST_HAS_SANITIZERS
    207 #endif
    208 
    209 #if defined(_LIBCPP_NORETURN)
    210 #define TEST_NORETURN _LIBCPP_NORETURN
    211 #else
    212 #define TEST_NORETURN [[noreturn]]
    213 #endif
    214 
    215 #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || \
    216   (!(TEST_STD_VER > 14 || \
    217     (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606L)))
    218 #define TEST_HAS_NO_ALIGNED_ALLOCATION
    219 #endif
    220 
    221 #if defined(_LIBCPP_SAFE_STATIC)
    222 #define TEST_SAFE_STATIC _LIBCPP_SAFE_STATIC
    223 #else
    224 #define TEST_SAFE_STATIC
    225 #endif
    226 
    227 // FIXME: Fix this feature check when either (A) a compiler provides a complete
    228 // implementation, or (b) a feature check macro is specified
    229 #if !defined(_MSC_VER) || defined(__clang__) || _MSC_VER < 1920 || _MSVC_LANG <= 201703L
    230 #define TEST_HAS_NO_SPACESHIP_OPERATOR
    231 #endif
    232 
    233 #if TEST_STD_VER < 11
    234 #define ASSERT_NOEXCEPT(...)
    235 #define ASSERT_NOT_NOEXCEPT(...)
    236 #else
    237 #define ASSERT_NOEXCEPT(...) \
    238     static_assert(noexcept(__VA_ARGS__), "Operation must be noexcept")
    239 
    240 #define ASSERT_NOT_NOEXCEPT(...) \
    241     static_assert(!noexcept(__VA_ARGS__), "Operation must NOT be noexcept")
    242 #endif
    243 
    244 /* Macros for testing libc++ specific behavior and extensions */
    245 #if defined(_LIBCPP_VERSION)
    246 #define LIBCPP_ASSERT(...) assert(__VA_ARGS__)
    247 #define LIBCPP_STATIC_ASSERT(...) static_assert(__VA_ARGS__)
    248 #define LIBCPP_ASSERT_NOEXCEPT(...) ASSERT_NOEXCEPT(__VA_ARGS__)
    249 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ASSERT_NOT_NOEXCEPT(__VA_ARGS__)
    250 #define LIBCPP_ONLY(...) __VA_ARGS__
    251 #else
    252 #define LIBCPP_ASSERT(...) ((void)0)
    253 #define LIBCPP_STATIC_ASSERT(...) ((void)0)
    254 #define LIBCPP_ASSERT_NOEXCEPT(...) ((void)0)
    255 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ((void)0)
    256 #define LIBCPP_ONLY(...) ((void)0)
    257 #endif
    258 
    259 #define TEST_IGNORE_NODISCARD (void)
    260 
    261 namespace test_macros_detail {
    262 template <class T, class U>
    263 struct is_same { enum { value = 0};} ;
    264 template <class T>
    265 struct is_same<T, T> { enum {value = 1}; };
    266 } // namespace test_macros_detail
    267 
    268 #define ASSERT_SAME_TYPE(...) \
    269     static_assert((test_macros_detail::is_same<__VA_ARGS__>::value), \
    270                  "Types differ unexpectedly")
    271 
    272 #ifndef TEST_HAS_NO_EXCEPTIONS
    273 #define TEST_THROW(...) throw __VA_ARGS__
    274 #else
    275 #if defined(__GNUC__)
    276 #define TEST_THROW(...) __builtin_abort()
    277 #else
    278 #include <stdlib.h>
    279 #define TEST_THROW(...) ::abort()
    280 #endif
    281 #endif
    282 
    283 #if defined(__GNUC__) || defined(__clang__)
    284 template <class Tp>
    285 inline
    286 void DoNotOptimize(Tp const& value) {
    287     asm volatile("" : : "r,m"(value) : "memory");
    288 }
    289 
    290 template <class Tp>
    291 inline void DoNotOptimize(Tp& value) {
    292 #if defined(__clang__)
    293   asm volatile("" : "+r,m"(value) : : "memory");
    294 #else
    295   asm volatile("" : "+m,r"(value) : : "memory");
    296 #endif
    297 }
    298 #else
    299 #include <intrin.h>
    300 template <class Tp>
    301 inline void DoNotOptimize(Tp const& value) {
    302   const volatile void* volatile unused = __builtin_addressof(value);
    303   static_cast<void>(unused);
    304   _ReadWriteBarrier();
    305 }
    306 #endif
    307 
    308 #if defined(__GNUC__)
    309 #define TEST_ALWAYS_INLINE __attribute__((always_inline))
    310 #define TEST_NOINLINE __attribute__((noinline))
    311 #elif defined(_MSC_VER)
    312 #define TEST_ALWAYS_INLINE __forceinline
    313 #define TEST_NOINLINE __declspec(noinline)
    314 #else
    315 #define TEST_ALWAYS_INLINE
    316 #define TEST_NOINLINE
    317 #endif
    318 
    319 #if defined(__GNUC__)
    320 #pragma GCC diagnostic pop
    321 #endif
    322 
    323 #endif // SUPPORT_TEST_MACROS_HPP