2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef SUPPORT_TEST_MACROS_HPP
11 #define SUPPORT_TEST_MACROS_HPP
14 # if __has_include("<version>")
23 #define TEST_STRINGIZE_IMPL(...) #__VA_ARGS__
24 #define TEST_STRINGIZE(...) TEST_STRINGIZE_IMPL(__VA_ARGS__)
26 #define TEST_CONCAT1(X, Y) X##Y
27 #define TEST_CONCAT(X, Y) TEST_CONCAT1(X, Y)
30 #define TEST_HAS_FEATURE(X) __has_feature(X)
32 #define TEST_HAS_FEATURE(X) 0
36 #define __has_include(...) 0
39 #ifdef __has_extension
40 #define TEST_HAS_EXTENSION(X) __has_extension(X)
42 #define TEST_HAS_EXTENSION(X) 0
46 #define TEST_HAS_WARNING(X) __has_warning(X)
48 #define TEST_HAS_WARNING(X) 0
52 #define TEST_HAS_BUILTIN(X) __has_builtin(X)
54 #define TEST_HAS_BUILTIN(X) 0
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)
61 #define TEST_HAS_BUILTIN_IDENTIFIER(X) 0
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
71 #elif defined(_MSC_VER)
72 # define TEST_COMPILER_MSVC
73 #elif defined(__GNUC__)
74 # define TEST_COMPILER_GCC
77 #if defined(__apple_build_version__)
78 // Given AppleClang XX.Y.Z, TEST_APPLE_CLANG_VER is XXYZ (e.g. AppleClang 14.0.3 => 1403)
79 #define TEST_APPLE_CLANG_VER (__apple_build_version__ / 10000)
80 #elif defined(__clang_major__)
81 #define TEST_CLANG_VER (__clang_major__ * 100) + __clang_minor__
82 #elif defined(__GNUC__)
83 // Given GCC XX.YY.ZZ, TEST_GCC_VER is XXYYZZ
84 #define TEST_GCC_VER ((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__)
87 /* Make a nice name for the standard version */
89 #if __cplusplus <= 199711L
90 # define TEST_STD_VER 3
91 #elif __cplusplus <= 201103L
92 # define TEST_STD_VER 11
93 #elif __cplusplus <= 201402L
94 # define TEST_STD_VER 14
95 #elif __cplusplus <= 201703L
96 # define TEST_STD_VER 17
97 #elif __cplusplus <= 202002L
98 # define TEST_STD_VER 20
99 #elif __cplusplus <= 202302L
100 # define TEST_STD_VER 23
102 # define TEST_STD_VER 99 // greater than current standard
103 // This is deliberately different than _LIBCPP_STD_VER to discourage matching them up.
107 // Attempt to deduce the GLIBC version
108 #if (defined(__has_include) && __has_include(<features.h>)) || \
110 #include <features.h>
111 #if defined(__GLIBC_PREREQ)
112 #define TEST_HAS_GLIBC
113 #define TEST_GLIBC_PREREQ(major, minor) __GLIBC_PREREQ(major, minor)
117 #if TEST_STD_VER >= 11
118 # define TEST_ALIGNOF(...) alignof(__VA_ARGS__)
119 # define TEST_ALIGNAS(...) alignas(__VA_ARGS__)
120 # define TEST_CONSTEXPR constexpr
121 # define TEST_NOEXCEPT noexcept
122 # define TEST_NOEXCEPT_FALSE noexcept(false)
123 # define TEST_NOEXCEPT_COND(...) noexcept(__VA_ARGS__)
125 # if defined(TEST_COMPILER_CLANG)
126 # define TEST_ALIGNOF(...) _Alignof(__VA_ARGS__)
128 # define TEST_ALIGNOF(...) __alignof(__VA_ARGS__)
130 # define TEST_ALIGNAS(...) __attribute__((__aligned__(__VA_ARGS__)))
131 # define TEST_CONSTEXPR
132 # define TEST_NOEXCEPT throw()
133 # define TEST_NOEXCEPT_FALSE
134 # define TEST_NOEXCEPT_COND(...)
137 #if TEST_STD_VER >= 11
138 # define TEST_THROW_SPEC(...)
140 # define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
143 #if defined(__cpp_lib_is_constant_evaluated) && __cpp_lib_is_constant_evaluated >= 201811L
144 # define TEST_IS_CONSTANT_EVALUATED std::is_constant_evaluated()
145 #elif TEST_HAS_BUILTIN(__builtin_is_constant_evaluated)
146 # define TEST_IS_CONSTANT_EVALUATED __builtin_is_constant_evaluated()
148 # define TEST_IS_CONSTANT_EVALUATED false
151 #if TEST_STD_VER >= 23
152 # define TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED true
154 # define TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED (!TEST_IS_CONSTANT_EVALUATED)
157 #if TEST_STD_VER >= 20
158 # define TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED true
160 # define TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED (!TEST_IS_CONSTANT_EVALUATED)
163 #if TEST_STD_VER >= 14
164 # define TEST_CONSTEXPR_CXX14 constexpr
166 # define TEST_CONSTEXPR_CXX14
169 #if TEST_STD_VER >= 17
170 # define TEST_CONSTEXPR_CXX17 constexpr
172 # define TEST_CONSTEXPR_CXX17
175 #if TEST_STD_VER >= 20
176 # define TEST_CONSTEXPR_CXX20 constexpr
178 # define TEST_CONSTEXPR_CXX20
181 #if TEST_STD_VER >= 23
182 # define TEST_CONSTEXPR_CXX23 constexpr
184 # define TEST_CONSTEXPR_CXX23
187 #define TEST_ALIGNAS_TYPE(...) TEST_ALIGNAS(TEST_ALIGNOF(__VA_ARGS__))
189 #if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cpp_rtti) \
190 && !defined(__GXX_RTTI)
191 #define TEST_HAS_NO_RTTI
194 #if !defined(TEST_HAS_NO_RTTI)
195 # define RTTI_ASSERT(X) assert(X)
197 # define RTTI_ASSERT(X)
200 #if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \
201 && !defined(__EXCEPTIONS)
202 #define TEST_HAS_NO_EXCEPTIONS
205 #if TEST_HAS_FEATURE(address_sanitizer) || TEST_HAS_FEATURE(hwaddress_sanitizer) || \
206 TEST_HAS_FEATURE(memory_sanitizer) || TEST_HAS_FEATURE(thread_sanitizer)
207 #define TEST_HAS_SANITIZERS
208 #define TEST_IS_EXECUTED_IN_A_SLOW_ENVIRONMENT
211 #if defined(_LIBCPP_NORETURN)
212 #define TEST_NORETURN _LIBCPP_NORETURN
214 #define TEST_NORETURN [[noreturn]]
217 #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || \
218 (!(TEST_STD_VER > 14 || \
219 (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606L)))
220 #define TEST_HAS_NO_ALIGNED_ALLOCATION
223 #if TEST_STD_VER > 17
224 #define TEST_CONSTINIT constinit
226 #define TEST_CONSTINIT
229 #if TEST_STD_VER < 11
230 #define ASSERT_NOEXCEPT(...)
231 #define ASSERT_NOT_NOEXCEPT(...)
233 #define ASSERT_NOEXCEPT(...) \
234 static_assert(noexcept(__VA_ARGS__), "Operation must be noexcept")
236 #define ASSERT_NOT_NOEXCEPT(...) \
237 static_assert(!noexcept(__VA_ARGS__), "Operation must NOT be noexcept")
240 /* Macros for testing libc++ specific behavior and extensions */
241 #if defined(_LIBCPP_VERSION)
242 #define LIBCPP_ASSERT(...) assert(__VA_ARGS__)
243 #define LIBCPP_STATIC_ASSERT(...) static_assert(__VA_ARGS__)
244 #define LIBCPP_ASSERT_NOEXCEPT(...) ASSERT_NOEXCEPT(__VA_ARGS__)
245 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ASSERT_NOT_NOEXCEPT(__VA_ARGS__)
246 #define LIBCPP_ONLY(...) __VA_ARGS__
248 #define LIBCPP_ASSERT(...) static_assert(true, "")
249 #define LIBCPP_STATIC_ASSERT(...) static_assert(true, "")
250 #define LIBCPP_ASSERT_NOEXCEPT(...) static_assert(true, "")
251 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) static_assert(true, "")
252 #define LIBCPP_ONLY(...) static_assert(true, "")
255 #if __has_cpp_attribute(nodiscard)
256 # define TEST_NODISCARD [[nodiscard]]
258 # define TEST_NODISCARD
261 #define TEST_IGNORE_NODISCARD (void)
263 namespace test_macros_detail
{
264 template <class T
, class U
>
265 struct is_same
{ enum { value
= 0};} ;
267 struct is_same
<T
, T
> { enum {value
= 1}; };
268 } // namespace test_macros_detail
270 #define ASSERT_SAME_TYPE(...) \
271 static_assert((test_macros_detail::is_same<__VA_ARGS__>::value), \
272 "Types differ unexpectedly")
274 #ifndef TEST_HAS_NO_EXCEPTIONS
275 #define TEST_THROW(...) throw __VA_ARGS__
277 #if defined(__GNUC__)
278 #define TEST_THROW(...) __builtin_abort()
281 #define TEST_THROW(...) ::abort()
285 #if defined(__GNUC__) || defined(__clang__)
288 void DoNotOptimize(Tp
const& value
) {
289 asm volatile("" : : "r,m"(value
) : "memory");
293 inline void DoNotOptimize(Tp
& value
) {
294 #if defined(__clang__)
295 asm volatile("" : "+r,m"(value
) : : "memory");
297 asm volatile("" : "+m,r"(value
) : : "memory");
303 inline void DoNotOptimize(Tp
const& value
) {
304 const volatile void* volatile unused
= __builtin_addressof(value
);
305 static_cast<void>(unused
);
310 #if defined(__GNUC__)
311 #define TEST_ALWAYS_INLINE __attribute__((always_inline))
312 #define TEST_NOINLINE __attribute__((noinline))
313 #elif defined(_MSC_VER)
314 #define TEST_ALWAYS_INLINE __forceinline
315 #define TEST_NOINLINE __declspec(noinline)
317 #define TEST_ALWAYS_INLINE
318 #define TEST_NOINLINE
322 #define TEST_NOT_WIN32(...) ((void)0)
324 #define TEST_NOT_WIN32(...) __VA_ARGS__
327 #if defined(TEST_WINDOWS_DLL) ||defined(__MVS__) || defined(_AIX)
328 // Macros for waiving cases when we can't count allocations done within
329 // the library implementation.
331 // On Windows, when libc++ is built as a DLL, references to operator new/delete
332 // within the DLL are bound at link time to the operator new/delete within
333 // the library; replacing them in the user executable doesn't override the
334 // calls within the library.
336 // The same goes on IBM zOS.
337 // The same goes on AIX.
338 #define ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(...) ((void)(__VA_ARGS__))
339 #define TEST_SUPPORTS_LIBRARY_INTERNAL_ALLOCATIONS 0
341 #define ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(...) assert(__VA_ARGS__)
342 #define TEST_SUPPORTS_LIBRARY_INTERNAL_ALLOCATIONS 1
345 #if (defined(TEST_WINDOWS_DLL) && !defined(_MSC_VER)) || \
347 // Normally, a replaced e.g. 'operator new' ends up used if the user code
348 // does a call to e.g. 'operator new[]'; it's enough to replace the base
349 // versions and have it override all of them.
351 // When the fallback operators are located within the libc++ library and we
352 // can't override the calls within it (see above), this fallback mechanism
353 // doesn't work either.
355 // On Windows, when using the MSVC vcruntime, the operator new/delete fallbacks
356 // are linked separately from the libc++ library, linked statically into
357 // the end user executable, and these fallbacks work even in DLL configurations.
358 // In MinGW configurations when built as a DLL, and on zOS, these fallbacks
359 // don't work though.
360 #define ASSERT_WITH_OPERATOR_NEW_FALLBACKS(...) ((void)(__VA_ARGS__))
362 #define ASSERT_WITH_OPERATOR_NEW_FALLBACKS(...) assert(__VA_ARGS__)
366 #define TEST_WIN_NO_FILESYSTEM_PERMS_NONE
369 // Support for carving out parts of the test suite, like removing wide characters, etc.
370 #if defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS)
371 # define TEST_HAS_NO_WIDE_CHARACTERS
374 #if defined(_LIBCPP_HAS_NO_UNICODE)
375 # define TEST_HAS_NO_UNICODE
376 #elif defined(_MSVC_EXECUTION_CHARACTER_SET) && _MSVC_EXECUTION_CHARACTER_SET != 65001
377 # define TEST_HAS_NO_UNICODE
380 #if defined(_LIBCPP_HAS_NO_INT128) || defined(_MSVC_STL_VERSION)
381 # define TEST_HAS_NO_INT128
384 #if defined(_LIBCPP_HAS_NO_LOCALIZATION)
385 # define TEST_HAS_NO_LOCALIZATION
388 #if TEST_STD_VER <= 17 || !defined(__cpp_char8_t)
389 # define TEST_HAS_NO_CHAR8_T
392 #if defined(_LIBCPP_HAS_NO_THREADS)
393 # define TEST_HAS_NO_THREADS
396 #if defined(_LIBCPP_HAS_NO_FILESYSTEM)
397 # define TEST_HAS_NO_FILESYSTEM
400 #if defined(_LIBCPP_HAS_NO_C8RTOMB_MBRTOC8)
401 # define TEST_HAS_NO_C8RTOMB_MBRTOC8
404 #if defined(_LIBCPP_HAS_NO_RANDOM_DEVICE)
405 # define TEST_HAS_NO_RANDOM_DEVICE
408 #if defined(TEST_COMPILER_CLANG)
409 # define TEST_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
410 # define TEST_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
411 # define TEST_CLANG_DIAGNOSTIC_IGNORED(str) _Pragma(TEST_STRINGIZE(clang diagnostic ignored str))
412 # define TEST_GCC_DIAGNOSTIC_IGNORED(str)
413 # define TEST_MSVC_DIAGNOSTIC_IGNORED(num)
414 #elif defined(TEST_COMPILER_GCC)
415 # define TEST_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
416 # define TEST_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
417 # define TEST_CLANG_DIAGNOSTIC_IGNORED(str)
418 # define TEST_GCC_DIAGNOSTIC_IGNORED(str) _Pragma(TEST_STRINGIZE(GCC diagnostic ignored str))
419 # define TEST_MSVC_DIAGNOSTIC_IGNORED(num)
420 #elif defined(TEST_COMPILER_MSVC)
421 # define TEST_DIAGNOSTIC_PUSH _Pragma("warning(push)")
422 # define TEST_DIAGNOSTIC_POP _Pragma("warning(pop)")
423 # define TEST_CLANG_DIAGNOSTIC_IGNORED(str)
424 # define TEST_GCC_DIAGNOSTIC_IGNORED(str)
425 # define TEST_MSVC_DIAGNOSTIC_IGNORED(num) _Pragma(TEST_STRINGIZE(warning(disable: num)))
427 # define TEST_DIAGNOSTIC_PUSH
428 # define TEST_DIAGNOSTIC_POP
429 # define TEST_CLANG_DIAGNOSTIC_IGNORED(str)
430 # define TEST_GCC_DIAGNOSTIC_IGNORED(str)
431 # define TEST_MSVC_DIAGNOSTIC_IGNORED(num)
434 #if __has_cpp_attribute(msvc::no_unique_address)
435 #define TEST_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
436 #elif __has_cpp_attribute(no_unique_address)
437 #define TEST_NO_UNIQUE_ADDRESS [[no_unique_address]]
439 #define TEST_NO_UNIQUE_ADDRESS
442 #ifdef _LIBCPP_SHORT_WCHAR
443 # define TEST_SHORT_WCHAR
446 // This is a temporary workaround for user-defined `operator new` definitions
447 // not being picked up on Apple platforms in some circumstances. This is under
448 // investigation and should be short-lived.
450 # define TEST_WORKAROUND_BUG_109234844_WEAK __attribute__((weak))
452 # define TEST_WORKAROUND_BUG_109234844_WEAK /* nothing */
456 # define TEST_IF_AIX(arg_true, arg_false) arg_true
458 # define TEST_IF_AIX(arg_true, arg_false) arg_false
461 #endif // SUPPORT_TEST_MACROS_HPP