1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef SUPPORT_TEST_MACROS_HPP 11 #define SUPPORT_TEST_MACROS_HPP 12 13 // Attempt to get STL specific macros like _LIBCPP_VERSION using the most 14 // minimal header possible. If we're testing libc++, we should use `<__config>`. 15 // If <__config> isn't available, fall back to <ciso646>. 16 #ifdef __has_include 17 # if __has_include("<__config>") 18 # include <__config> 19 # define TEST_IMP_INCLUDED_HEADER 20 # endif 21 #endif 22 #ifndef TEST_IMP_INCLUDED_HEADER 23 #include <ciso646> 24 #endif 25 26 #if defined(__GNUC__) 27 #pragma GCC diagnostic push 28 #pragma GCC diagnostic ignored "-Wvariadic-macros" 29 #endif 30 31 #define TEST_STRINGIZE_IMPL(x) #x 32 #define TEST_STRINGIZE(x) TEST_STRINGIZE_IMPL(x) 33 34 #define TEST_CONCAT1(X, Y) X##Y 35 #define TEST_CONCAT(X, Y) TEST_CONCAT1(X, Y) 36 37 #ifdef __has_feature 38 #define TEST_HAS_FEATURE(X) __has_feature(X) 39 #else 40 #define TEST_HAS_FEATURE(X) 0 41 #endif 42 43 #ifndef __has_include 44 #define __has_include(...) 0 45 #endif 46 47 #ifdef __has_extension 48 #define TEST_HAS_EXTENSION(X) __has_extension(X) 49 #else 50 #define TEST_HAS_EXTENSION(X) 0 51 #endif 52 53 #ifdef __has_warning 54 #define TEST_HAS_WARNING(X) __has_warning(X) 55 #else 56 #define TEST_HAS_WARNING(X) 0 57 #endif 58 59 #ifdef __has_builtin 60 #define TEST_HAS_BUILTIN(X) __has_builtin(X) 61 #else 62 #define TEST_HAS_BUILTIN(X) 0 63 #endif 64 #ifdef __is_identifier 65 // '__is_identifier' returns '0' if '__x' is a reserved identifier provided by 66 // the compiler and '1' otherwise. 67 #define TEST_HAS_BUILTIN_IDENTIFIER(X) !__is_identifier(X) 68 #else 69 #define TEST_HAS_BUILTIN_IDENTIFIER(X) 0 70 #endif 71 72 #if defined(__EDG__) 73 # define TEST_COMPILER_EDG 74 #elif defined(__clang__) 75 # define TEST_COMPILER_CLANG 76 # if defined(__apple_build_version__) 77 # define TEST_COMPILER_APPLE_CLANG 78 # endif 79 #elif defined(_MSC_VER) 80 # define TEST_COMPILER_C1XX 81 #elif defined(__GNUC__) 82 # define TEST_COMPILER_GCC 83 #endif 84 85 #if defined(__apple_build_version__) 86 #define TEST_APPLE_CLANG_VER (__clang_major__ * 100) + __clang_minor__ 87 #elif defined(__clang_major__) 88 #define TEST_CLANG_VER (__clang_major__ * 100) + __clang_minor__ 89 #elif defined(__GNUC__) 90 // Given GCC XX.YY.ZZ, TEST_GCC_VER is XXYYZZ 91 #define TEST_GCC_VER ((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__) 92 #endif 93 94 /* Make a nice name for the standard version */ 95 #ifndef TEST_STD_VER 96 #if __cplusplus <= 199711L 97 # define TEST_STD_VER 3 98 #elif __cplusplus <= 201103L 99 # define TEST_STD_VER 11 100 #elif __cplusplus <= 201402L 101 # define TEST_STD_VER 14 102 #elif __cplusplus <= 201703L 103 # define TEST_STD_VER 17 104 #elif __cplusplus <= 202002L 105 # define TEST_STD_VER 20 106 #else 107 # define TEST_STD_VER 99 // greater than current standard 108 // This is deliberately different than _LIBCPP_STD_VER to discourage matching them up. 109 #endif 110 #endif 111 112 // Attempt to deduce the GLIBC version 113 #if (defined(__has_include) && __has_include(<features.h>)) || \ 114 defined(__linux__) 115 #include <features.h> 116 #if defined(__GLIBC_PREREQ) 117 #define TEST_HAS_GLIBC 118 #define TEST_GLIBC_PREREQ(major, minor) __GLIBC_PREREQ(major, minor) 119 #endif 120 #endif 121 122 #if TEST_STD_VER >= 11 123 # define TEST_ALIGNOF(...) alignof(__VA_ARGS__) 124 # define TEST_ALIGNAS(...) alignas(__VA_ARGS__) 125 # define TEST_CONSTEXPR constexpr 126 # define TEST_NOEXCEPT noexcept 127 # define TEST_NOEXCEPT_FALSE noexcept(false) 128 # define TEST_NOEXCEPT_COND(...) noexcept(__VA_ARGS__) 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_NOEXCEPT throw() 138 # define TEST_NOEXCEPT_FALSE 139 # define TEST_NOEXCEPT_COND(...) 140 #endif 141 142 #if TEST_STD_VER >= 11 143 # define TEST_THROW_SPEC(...) 144 #else 145 # define TEST_THROW_SPEC(...) throw(__VA_ARGS__) 146 #endif 147 148 #if TEST_STD_VER >= 14 149 # define TEST_CONSTEXPR_CXX14 constexpr 150 #else 151 # define TEST_CONSTEXPR_CXX14 152 #endif 153 154 #if TEST_STD_VER >= 17 155 # define TEST_CONSTEXPR_CXX17 constexpr 156 #else 157 # define TEST_CONSTEXPR_CXX17 158 #endif 159 160 #if TEST_STD_VER >= 20 161 # define TEST_CONSTEXPR_CXX20 constexpr 162 #else 163 # define TEST_CONSTEXPR_CXX20 164 #endif 165 166 #define TEST_ALIGNAS_TYPE(...) TEST_ALIGNAS(TEST_ALIGNOF(__VA_ARGS__)) 167 168 #if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cpp_rtti) \ 169 && !defined(__GXX_RTTI) 170 #define TEST_HAS_NO_RTTI 171 #endif 172 173 #if !defined(TEST_HAS_NO_RTTI) 174 # define RTTI_ASSERT(X) assert(X) 175 #else 176 # define RTTI_ASSERT(X) 177 #endif 178 179 #if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \ 180 && !defined(__EXCEPTIONS) 181 #define TEST_HAS_NO_EXCEPTIONS 182 #endif 183 184 #if TEST_HAS_FEATURE(address_sanitizer) || TEST_HAS_FEATURE(memory_sanitizer) || \ 185 TEST_HAS_FEATURE(thread_sanitizer) 186 #define TEST_HAS_SANITIZERS 187 #endif 188 189 #if defined(_LIBCPP_NORETURN) 190 #define TEST_NORETURN _LIBCPP_NORETURN 191 #else 192 #define TEST_NORETURN [[noreturn]] 193 #endif 194 195 #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || \ 196 (!(TEST_STD_VER > 14 || \ 197 (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606L))) 198 #define TEST_HAS_NO_ALIGNED_ALLOCATION 199 #endif 200 201 #if defined(_LIBCPP_SAFE_STATIC) 202 #define TEST_SAFE_STATIC _LIBCPP_SAFE_STATIC 203 #else 204 #define TEST_SAFE_STATIC 205 #endif 206 207 #if !defined(__cpp_impl_three_way_comparison) \ 208 && (!defined(_MSC_VER) || defined(__clang__) || _MSC_VER < 1920 || _MSVC_LANG <= 201703L) 209 #define TEST_HAS_NO_SPACESHIP_OPERATOR 210 #endif 211 212 #if TEST_STD_VER < 11 213 #define ASSERT_NOEXCEPT(...) 214 #define ASSERT_NOT_NOEXCEPT(...) 215 #else 216 #define ASSERT_NOEXCEPT(...) \ 217 static_assert(noexcept(__VA_ARGS__), "Operation must be noexcept") 218 219 #define ASSERT_NOT_NOEXCEPT(...) \ 220 static_assert(!noexcept(__VA_ARGS__), "Operation must NOT be noexcept") 221 #endif 222 223 /* Macros for testing libc++ specific behavior and extensions */ 224 #if defined(_LIBCPP_VERSION) 225 #define LIBCPP_ASSERT(...) assert(__VA_ARGS__) 226 #define LIBCPP_STATIC_ASSERT(...) static_assert(__VA_ARGS__) 227 #define LIBCPP_ASSERT_NOEXCEPT(...) ASSERT_NOEXCEPT(__VA_ARGS__) 228 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ASSERT_NOT_NOEXCEPT(__VA_ARGS__) 229 #define LIBCPP_ONLY(...) __VA_ARGS__ 230 #else 231 #define LIBCPP_ASSERT(...) ((void)0) 232 #define LIBCPP_STATIC_ASSERT(...) ((void)0) 233 #define LIBCPP_ASSERT_NOEXCEPT(...) ((void)0) 234 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ((void)0) 235 #define LIBCPP_ONLY(...) ((void)0) 236 #endif 237 238 #if !defined(_LIBCPP_HAS_NO_RANGES) 239 #define TEST_SUPPORTS_RANGES 240 #endif 241 242 #define TEST_IGNORE_NODISCARD (void) 243 244 namespace test_macros_detail { 245 template <class T, class U> 246 struct is_same { enum { value = 0};} ; 247 template <class T> 248 struct is_same<T, T> { enum {value = 1}; }; 249 } // namespace test_macros_detail 250 251 #define ASSERT_SAME_TYPE(...) \ 252 static_assert((test_macros_detail::is_same<__VA_ARGS__>::value), \ 253 "Types differ unexpectedly") 254 255 #ifndef TEST_HAS_NO_EXCEPTIONS 256 #define TEST_THROW(...) throw __VA_ARGS__ 257 #else 258 #if defined(__GNUC__) 259 #define TEST_THROW(...) __builtin_abort() 260 #else 261 #include <stdlib.h> 262 #define TEST_THROW(...) ::abort() 263 #endif 264 #endif 265 266 #if defined(__GNUC__) || defined(__clang__) 267 template <class Tp> 268 inline 269 void DoNotOptimize(Tp const& value) { 270 asm volatile("" : : "r,m"(value) : "memory"); 271 } 272 273 template <class Tp> 274 inline void DoNotOptimize(Tp& value) { 275 #if defined(__clang__) 276 asm volatile("" : "+r,m"(value) : : "memory"); 277 #else 278 asm volatile("" : "+m,r"(value) : : "memory"); 279 #endif 280 } 281 #else 282 #include <intrin.h> 283 template <class Tp> 284 inline void DoNotOptimize(Tp const& value) { 285 const volatile void* volatile unused = __builtin_addressof(value); 286 static_cast<void>(unused); 287 _ReadWriteBarrier(); 288 } 289 #endif 290 291 #if defined(__GNUC__) 292 #define TEST_ALWAYS_INLINE __attribute__((always_inline)) 293 #define TEST_NOINLINE __attribute__((noinline)) 294 #elif defined(_MSC_VER) 295 #define TEST_ALWAYS_INLINE __forceinline 296 #define TEST_NOINLINE __declspec(noinline) 297 #else 298 #define TEST_ALWAYS_INLINE 299 #define TEST_NOINLINE 300 #endif 301 302 #ifdef _WIN32 303 #define TEST_NOT_WIN32(...) ((void)0) 304 #else 305 #define TEST_NOT_WIN32(...) __VA_ARGS__ 306 #endif 307 308 #if (defined(_WIN32) && !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)) || \ 309 defined(__MVS__) 310 // Macros for waiving cases when we can't count allocations done within 311 // the library implementation. 312 // 313 // On Windows, when libc++ is built as a DLL, references to operator new/delete 314 // within the DLL are bound at link time to the operator new/delete within 315 // the library; replacing them in the user executable doesn't override the 316 // calls within the library. 317 // 318 // The same goes on IBM zOS. 319 #define ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(...) ((void)(__VA_ARGS__)) 320 #define TEST_SUPPORTS_LIBRARY_INTERNAL_ALLOCATIONS 0 321 #else 322 #define ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(...) assert(__VA_ARGS__) 323 #define TEST_SUPPORTS_LIBRARY_INTERNAL_ALLOCATIONS 1 324 #endif 325 326 #if (defined(_WIN32) && !defined(_MSC_VER) && \ 327 !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)) || \ 328 defined(__MVS__) 329 // Normally, a replaced e.g. 'operator new' ends up used if the user code 330 // does a call to e.g. 'operator new[]'; it's enough to replace the base 331 // versions and have it override all of them. 332 // 333 // When the fallback operators are located within the libc++ library and we 334 // can't override the calls within it (see above), this fallback mechanism 335 // doesn't work either. 336 // 337 // On Windows, when using the MSVC vcruntime, the operator new/delete fallbacks 338 // are linked separately from the libc++ library, linked statically into 339 // the end user executable, and these fallbacks work even in DLL configurations. 340 // In MinGW configurations when built as a DLL, and on zOS, these fallbacks 341 // don't work though. 342 #define ASSERT_WITH_OPERATOR_NEW_FALLBACKS(...) ((void)(__VA_ARGS__)) 343 #else 344 #define ASSERT_WITH_OPERATOR_NEW_FALLBACKS(...) assert(__VA_ARGS__) 345 #endif 346 347 #ifdef _WIN32 348 #define TEST_WIN_NO_FILESYSTEM_PERMS_NONE 349 #endif 350 351 // Support for carving out parts of the test suite, like removing wide characters, etc. 352 #if defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS) 353 # define TEST_HAS_NO_WIDE_CHARACTERS 354 #endif 355 356 #if defined(__GNUC__) 357 #pragma GCC diagnostic pop 358 #endif 359 360 #endif // SUPPORT_TEST_MACROS_HPP 361