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
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>.
17 # if __has_include("<__config>")
19 # define TEST_IMP_INCLUDED_HEADER
22 #ifndef TEST_IMP_INCLUDED_HEADER
26 #define TEST_STRINGIZE_IMPL(...) #__VA_ARGS__
27 #define TEST_STRINGIZE(...) TEST_STRINGIZE_IMPL(__VA_ARGS__)
29 #define TEST_CONCAT1(X, Y) X##Y
30 #define TEST_CONCAT(X, Y) TEST_CONCAT1(X, Y)
33 #define TEST_HAS_FEATURE(X) __has_feature(X)
35 #define TEST_HAS_FEATURE(X) 0
39 #define __has_include(...) 0
42 #ifdef __has_extension
43 #define TEST_HAS_EXTENSION(X) __has_extension(X)
45 #define TEST_HAS_EXTENSION(X) 0
49 #define TEST_HAS_WARNING(X) __has_warning(X)
51 #define TEST_HAS_WARNING(X) 0
55 #define TEST_HAS_BUILTIN(X) __has_builtin(X)
57 #define TEST_HAS_BUILTIN(X) 0
59 #ifdef __is_identifier
60 // '__is_identifier' returns '0' if '__x' is a reserved identifier provided by
61 // the compiler and '1' otherwise.
62 #define TEST_HAS_BUILTIN_IDENTIFIER(X) !__is_identifier(X)
64 #define TEST_HAS_BUILTIN_IDENTIFIER(X) 0
68 # define TEST_COMPILER_EDG
69 #elif defined(__clang__)
70 # define TEST_COMPILER_CLANG
71 # if defined(__apple_build_version__)
72 # define TEST_COMPILER_APPLE_CLANG
74 #elif defined(_MSC_VER)
75 # define TEST_COMPILER_MSVC
76 #elif defined(__GNUC__)
77 # define TEST_COMPILER_GCC
80 #if defined(__apple_build_version__)
81 // Given AppleClang XX.Y.Z, TEST_APPLE_CLANG_VER is XXYZ (e.g. AppleClang 14.0.3 => 1403)
82 #define TEST_APPLE_CLANG_VER (__apple_build_version__ / 10000)
83 #elif defined(__clang_major__)
84 #define TEST_CLANG_VER (__clang_major__ * 100) + __clang_minor__
85 #elif defined(__GNUC__)
86 // Given GCC XX.YY.ZZ, TEST_GCC_VER is XXYYZZ
87 #define TEST_GCC_VER ((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__)
90 /* Make a nice name for the standard version */
92 #if __cplusplus <= 199711L
93 # define TEST_STD_VER 3
94 #elif __cplusplus <= 201103L
95 # define TEST_STD_VER 11
96 #elif __cplusplus <= 201402L
97 # define TEST_STD_VER 14
98 #elif __cplusplus <= 201703L
99 # define TEST_STD_VER 17
100 #elif __cplusplus <= 202002L
101 # define TEST_STD_VER 20
102 #elif __cplusplus <= 202302L
103 # define TEST_STD_VER 23
105 # define TEST_STD_VER 99 // greater than current standard
106 // This is deliberately different than _LIBCPP_STD_VER to discourage matching them up.
110 // Attempt to deduce the GLIBC version
111 #if (defined(__has_include) && __has_include(<features.h>)) || \
113 #include <features.h>
114 #if defined(__GLIBC_PREREQ)
115 #define TEST_HAS_GLIBC
116 #define TEST_GLIBC_PREREQ(major, minor) __GLIBC_PREREQ(major, minor)
120 #if TEST_STD_VER >= 11
121 # define TEST_ALIGNOF(...) alignof(__VA_ARGS__)
122 # define TEST_ALIGNAS(...) alignas(__VA_ARGS__)
123 # define TEST_CONSTEXPR constexpr
124 # define TEST_NOEXCEPT noexcept
125 # define TEST_NOEXCEPT_FALSE noexcept(false)
126 # define TEST_NOEXCEPT_COND(...) noexcept(__VA_ARGS__)
128 # if defined(TEST_COMPILER_CLANG)
129 # define TEST_ALIGNOF(...) _Alignof(__VA_ARGS__)
131 # define TEST_ALIGNOF(...) __alignof(__VA_ARGS__)
133 # define TEST_ALIGNAS(...) __attribute__((__aligned__(__VA_ARGS__)))
134 # define TEST_CONSTEXPR
135 # define TEST_NOEXCEPT throw()
136 # define TEST_NOEXCEPT_FALSE
137 # define TEST_NOEXCEPT_COND(...)
140 #if TEST_STD_VER >= 11
141 # define TEST_THROW_SPEC(...)
143 # define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
146 #if defined(__cpp_lib_is_constant_evaluated) && __cpp_lib_is_constant_evaluated >= 201811L
147 # define TEST_IS_CONSTANT_EVALUATED std::is_constant_evaluated()
148 #elif TEST_HAS_BUILTIN(__builtin_is_constant_evaluated)
149 # define TEST_IS_CONSTANT_EVALUATED __builtin_is_constant_evaluated()
151 # define TEST_IS_CONSTANT_EVALUATED false
154 #if TEST_STD_VER >= 14
155 # define TEST_CONSTEXPR_CXX14 constexpr
157 # define TEST_CONSTEXPR_CXX14
160 #if TEST_STD_VER >= 17
161 # define TEST_CONSTEXPR_CXX17 constexpr
163 # define TEST_CONSTEXPR_CXX17
166 #if TEST_STD_VER >= 20
167 # define TEST_CONSTEXPR_CXX20 constexpr
169 # define TEST_CONSTEXPR_CXX20
172 #if TEST_STD_VER >= 23
173 # define TEST_CONSTEXPR_CXX23 constexpr
175 # define TEST_CONSTEXPR_CXX23
178 #define TEST_ALIGNAS_TYPE(...) TEST_ALIGNAS(TEST_ALIGNOF(__VA_ARGS__))
180 #if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cpp_rtti) \
181 && !defined(__GXX_RTTI)
182 #define TEST_HAS_NO_RTTI
185 #if !defined(TEST_HAS_NO_RTTI)
186 # define RTTI_ASSERT(X) assert(X)
188 # define RTTI_ASSERT(X)
191 #if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \
192 && !defined(__EXCEPTIONS)
193 #define TEST_HAS_NO_EXCEPTIONS
196 #if TEST_HAS_FEATURE(address_sanitizer) || TEST_HAS_FEATURE(hwaddress_sanitizer) || \
197 TEST_HAS_FEATURE(memory_sanitizer) || TEST_HAS_FEATURE(thread_sanitizer)
198 #define TEST_HAS_SANITIZERS
199 #define TEST_IS_EXECUTED_IN_A_SLOW_ENVIRONMENT
202 #if defined(_LIBCPP_NORETURN)
203 #define TEST_NORETURN _LIBCPP_NORETURN
205 #define TEST_NORETURN [[noreturn]]
208 #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || \
209 (!(TEST_STD_VER > 14 || \
210 (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606L)))
211 #define TEST_HAS_NO_ALIGNED_ALLOCATION
214 #if TEST_STD_VER > 17
215 #define TEST_CONSTINIT constinit
216 #elif defined(_LIBCPP_CONSTINIT)
217 #define TEST_CONSTINIT _LIBCPP_CONSTINIT
219 #define TEST_CONSTINIT
222 #if TEST_STD_VER < 11
223 #define ASSERT_NOEXCEPT(...)
224 #define ASSERT_NOT_NOEXCEPT(...)
226 #define ASSERT_NOEXCEPT(...) \
227 static_assert(noexcept(__VA_ARGS__), "Operation must be noexcept")
229 #define ASSERT_NOT_NOEXCEPT(...) \
230 static_assert(!noexcept(__VA_ARGS__), "Operation must NOT be noexcept")
233 /* Macros for testing libc++ specific behavior and extensions */
234 #if defined(_LIBCPP_VERSION)
235 #define LIBCPP_ASSERT(...) assert(__VA_ARGS__)
236 #define LIBCPP_STATIC_ASSERT(...) static_assert(__VA_ARGS__)
237 #define LIBCPP_ASSERT_NOEXCEPT(...) ASSERT_NOEXCEPT(__VA_ARGS__)
238 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ASSERT_NOT_NOEXCEPT(__VA_ARGS__)
239 #define LIBCPP_ONLY(...) __VA_ARGS__
241 #define LIBCPP_ASSERT(...) static_assert(true, "")
242 #define LIBCPP_STATIC_ASSERT(...) static_assert(true, "")
243 #define LIBCPP_ASSERT_NOEXCEPT(...) static_assert(true, "")
244 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) static_assert(true, "")
245 #define LIBCPP_ONLY(...) static_assert(true, "")
248 #if __has_cpp_attribute(nodiscard)
249 # define TEST_NODISCARD [[nodiscard]]
251 # define TEST_NODISCARD
254 #define TEST_IGNORE_NODISCARD (void)
256 namespace test_macros_detail
{
257 template <class T
, class U
>
258 struct is_same
{ enum { value
= 0};} ;
260 struct is_same
<T
, T
> { enum {value
= 1}; };
261 } // namespace test_macros_detail
263 #define ASSERT_SAME_TYPE(...) \
264 static_assert((test_macros_detail::is_same<__VA_ARGS__>::value), \
265 "Types differ unexpectedly")
267 #ifndef TEST_HAS_NO_EXCEPTIONS
268 #define TEST_THROW(...) throw __VA_ARGS__
270 #if defined(__GNUC__)
271 #define TEST_THROW(...) __builtin_abort()
274 #define TEST_THROW(...) ::abort()
278 #if defined(__GNUC__) || defined(__clang__)
281 void DoNotOptimize(Tp
const& value
) {
282 asm volatile("" : : "r,m"(value
) : "memory");
286 inline void DoNotOptimize(Tp
& value
) {
287 #if defined(__clang__)
288 asm volatile("" : "+r,m"(value
) : : "memory");
290 asm volatile("" : "+m,r"(value
) : : "memory");
296 inline void DoNotOptimize(Tp
const& value
) {
297 const volatile void* volatile unused
= __builtin_addressof(value
);
298 static_cast<void>(unused
);
303 #if defined(__GNUC__)
304 #define TEST_ALWAYS_INLINE __attribute__((always_inline))
305 #define TEST_NOINLINE __attribute__((noinline))
306 #elif defined(_MSC_VER)
307 #define TEST_ALWAYS_INLINE __forceinline
308 #define TEST_NOINLINE __declspec(noinline)
310 #define TEST_ALWAYS_INLINE
311 #define TEST_NOINLINE
315 #define TEST_NOT_WIN32(...) ((void)0)
317 #define TEST_NOT_WIN32(...) __VA_ARGS__
320 #if defined(TEST_WINDOWS_DLL) ||defined(__MVS__) || defined(_AIX)
321 // Macros for waiving cases when we can't count allocations done within
322 // the library implementation.
324 // On Windows, when libc++ is built as a DLL, references to operator new/delete
325 // within the DLL are bound at link time to the operator new/delete within
326 // the library; replacing them in the user executable doesn't override the
327 // calls within the library.
329 // The same goes on IBM zOS.
330 // The same goes on AIX.
331 #define ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(...) ((void)(__VA_ARGS__))
332 #define TEST_SUPPORTS_LIBRARY_INTERNAL_ALLOCATIONS 0
334 #define ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(...) assert(__VA_ARGS__)
335 #define TEST_SUPPORTS_LIBRARY_INTERNAL_ALLOCATIONS 1
338 #if (defined(TEST_WINDOWS_DLL) && !defined(_MSC_VER)) || \
340 // Normally, a replaced e.g. 'operator new' ends up used if the user code
341 // does a call to e.g. 'operator new[]'; it's enough to replace the base
342 // versions and have it override all of them.
344 // When the fallback operators are located within the libc++ library and we
345 // can't override the calls within it (see above), this fallback mechanism
346 // doesn't work either.
348 // On Windows, when using the MSVC vcruntime, the operator new/delete fallbacks
349 // are linked separately from the libc++ library, linked statically into
350 // the end user executable, and these fallbacks work even in DLL configurations.
351 // In MinGW configurations when built as a DLL, and on zOS, these fallbacks
352 // don't work though.
353 #define ASSERT_WITH_OPERATOR_NEW_FALLBACKS(...) ((void)(__VA_ARGS__))
355 #define ASSERT_WITH_OPERATOR_NEW_FALLBACKS(...) assert(__VA_ARGS__)
359 #define TEST_WIN_NO_FILESYSTEM_PERMS_NONE
362 // Support for carving out parts of the test suite, like removing wide characters, etc.
363 #if defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS)
364 # define TEST_HAS_NO_WIDE_CHARACTERS
367 #if defined(_LIBCPP_HAS_NO_UNICODE)
368 # define TEST_HAS_NO_UNICODE
369 #elif defined(_MSVC_EXECUTION_CHARACTER_SET) && _MSVC_EXECUTION_CHARACTER_SET != 65001
370 # define TEST_HAS_NO_UNICODE
373 #if defined(_LIBCPP_HAS_NO_INT128) || defined(_MSVC_STL_VERSION)
374 # define TEST_HAS_NO_INT128
377 #if defined(_LIBCPP_HAS_NO_LOCALIZATION)
378 # define TEST_HAS_NO_LOCALIZATION
381 #if TEST_STD_VER <= 17 || !defined(__cpp_char8_t)
382 # define TEST_HAS_NO_CHAR8_T
385 #if defined(_LIBCPP_HAS_NO_THREADS)
386 # define TEST_HAS_NO_THREADS
389 #if defined(_LIBCPP_HAS_NO_FILESYSTEM)
390 # define TEST_HAS_NO_FILESYSTEM
393 #if defined(_LIBCPP_HAS_NO_FGETPOS_FSETPOS)
394 # define TEST_HAS_NO_FGETPOS_FSETPOS
397 #if defined(_LIBCPP_HAS_NO_C8RTOMB_MBRTOC8)
398 # define TEST_HAS_NO_C8RTOMB_MBRTOC8
401 #if defined(_LIBCPP_HAS_NO_RANDOM_DEVICE)
402 # define TEST_HAS_NO_RANDOM_DEVICE
405 #if defined(TEST_COMPILER_CLANG)
406 # define TEST_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
407 # define TEST_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
408 # define TEST_CLANG_DIAGNOSTIC_IGNORED(str) _Pragma(TEST_STRINGIZE(clang diagnostic ignored str))
409 # define TEST_GCC_DIAGNOSTIC_IGNORED(str)
410 # define TEST_MSVC_DIAGNOSTIC_IGNORED(num)
411 #elif defined(TEST_COMPILER_GCC)
412 # define TEST_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
413 # define TEST_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
414 # define TEST_CLANG_DIAGNOSTIC_IGNORED(str)
415 # define TEST_GCC_DIAGNOSTIC_IGNORED(str) _Pragma(TEST_STRINGIZE(GCC diagnostic ignored str))
416 # define TEST_MSVC_DIAGNOSTIC_IGNORED(num)
417 #elif defined(TEST_COMPILER_MSVC)
418 # define TEST_DIAGNOSTIC_PUSH _Pragma("warning(push)")
419 # define TEST_DIAGNOSTIC_POP _Pragma("warning(pop)")
420 # define TEST_CLANG_DIAGNOSTIC_IGNORED(str)
421 # define TEST_GCC_DIAGNOSTIC_IGNORED(str)
422 # define TEST_MSVC_DIAGNOSTIC_IGNORED(num) _Pragma(TEST_STRINGIZE(warning(disable: num)))
424 # define TEST_DIAGNOSTIC_PUSH
425 # define TEST_DIAGNOSTIC_POP
426 # define TEST_CLANG_DIAGNOSTIC_IGNORED(str)
427 # define TEST_GCC_DIAGNOSTIC_IGNORED(str)
428 # define TEST_MSVC_DIAGNOSTIC_IGNORED(num)
431 #if __has_cpp_attribute(msvc::no_unique_address)
432 #define TEST_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
433 #elif __has_cpp_attribute(no_unique_address)
434 #define TEST_NO_UNIQUE_ADDRESS [[no_unique_address]]
436 #define TEST_NO_UNIQUE_ADDRESS
439 #ifdef _LIBCPP_SHORT_WCHAR
440 # define TEST_SHORT_WCHAR
443 // This is a temporary workaround for user-defined `operator new` definitions
444 // not being picked up on Apple platforms in some circumstances. This is under
445 // investigation and should be short-lived.
447 # define TEST_WORKAROUND_BUG_109234844_WEAK __attribute__((weak))
449 # define TEST_WORKAROUND_BUG_109234844_WEAK /* nothing */
452 #endif // SUPPORT_TEST_MACROS_HPP