2 //===---------------------------- test_macros.h ---------------------------===//
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
27 #pragma GCC diagnostic push
28 #pragma GCC diagnostic ignored "-Wvariadic-macros"
31 #define TEST_STRINGIZE_IMPL(x) #x
32 #define TEST_STRINGIZE(x) TEST_STRINGIZE_IMPL(x)
34 #define TEST_CONCAT1(X, Y) X##Y
35 #define TEST_CONCAT(X, Y) TEST_CONCAT1(X, Y)
38 #define TEST_HAS_FEATURE(X) __has_feature(X)
40 #define TEST_HAS_FEATURE(X) 0
44 #define __has_include(...) 0
47 #ifdef __has_extension
48 #define TEST_HAS_EXTENSION(X) __has_extension(X)
50 #define TEST_HAS_EXTENSION(X) 0
54 #define TEST_HAS_WARNING(X) __has_warning(X)
56 #define TEST_HAS_WARNING(X) 0
60 #define TEST_HAS_BUILTIN(X) __has_builtin(X)
62 #define TEST_HAS_BUILTIN(X) 0
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)
69 #define TEST_HAS_BUILTIN_IDENTIFIER(X) 0
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
79 #elif defined(_MSC_VER)
80 # define TEST_COMPILER_C1XX
81 #elif defined(__GNUC__)
82 # define TEST_COMPILER_GCC
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 #define TEST_GCC_VER (__GNUC__ * 100 + __GNUC_MINOR__)
91 #define TEST_GCC_VER_NEW (TEST_GCC_VER * 10 + __GNUC_PATCHLEVEL__)
94 /* Make a nice name for the standard version */
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
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 >= 17
141 # define TEST_THROW_SPEC(...)
143 # define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
146 #if TEST_STD_VER >= 14
147 # define TEST_CONSTEXPR_CXX14 constexpr
149 # define TEST_CONSTEXPR_CXX14
152 #if TEST_STD_VER >= 17
153 # define TEST_CONSTEXPR_CXX17 constexpr
155 # define TEST_CONSTEXPR_CXX17
158 #if TEST_STD_VER >= 20
159 # define TEST_CONSTEXPR_CXX20 constexpr
161 # define TEST_CONSTEXPR_CXX20
164 // Sniff out to see if the underlying C library has C11 features
165 // This is cribbed from __config; but lives here as well because we can't assume libc++
166 #if __ISO_C_VISIBLE >= 2011 || TEST_STD_VER >= 11
167 # if defined(__FreeBSD__)
168 // Specifically, FreeBSD does NOT have timespec_get, even though they have all
169 // the rest of C11 - this is PR#38495
170 # define TEST_HAS_ALIGNED_ALLOC
171 # define TEST_HAS_QUICK_EXIT
172 # elif defined(__BIONIC__)
173 # if __ANDROID_API__ >= 21
174 # define TEST_HAS_QUICK_EXIT
176 # if __ANDROID_API__ >= 28
177 # define TEST_HAS_ALIGNED_ALLOC
179 # if __ANDROID_API__ >= 29
180 # define TEST_HAS_TIMESPEC_GET
182 # elif defined(__Fuchsia__) || defined(__wasi__) || defined(__NetBSD__)
183 # define TEST_HAS_QUICK_EXIT
184 # define TEST_HAS_ALIGNED_ALLOC
185 # define TEST_HAS_TIMESPEC_GET
186 # elif defined(__linux__)
187 // This block preserves the old behavior used by include/__config:
188 // _LIBCPP_GLIBC_PREREQ would be defined to 0 if __GLIBC_PREREQ was not
189 // available. The configuration here may be too vague though, as Bionic, uClibc,
190 // newlib, etc may all support these features but need to be configured.
191 # if defined(TEST_GLIBC_PREREQ)
192 # if TEST_GLIBC_PREREQ(2, 15)
193 # define TEST_HAS_QUICK_EXIT
195 # if TEST_GLIBC_PREREQ(2, 17)
196 # define TEST_HAS_ALIGNED_ALLOC
197 # define TEST_HAS_TIMESPEC_GET
199 # elif defined(_LIBCPP_HAS_MUSL_LIBC)
200 # define TEST_HAS_QUICK_EXIT
201 # define TEST_HAS_ALIGNED_ALLOC
202 # define TEST_HAS_TIMESPEC_GET
204 # elif defined(_WIN32)
205 # if defined(_MSC_VER) && !defined(__MINGW32__)
206 # define TEST_HAS_QUICK_EXIT
207 # define TEST_HAS_ALIGNED_ALLOC
208 # define TEST_HAS_TIMESPEC_GET
210 # elif defined(__APPLE__)
211 // timespec_get and aligned_alloc were introduced in macOS 10.15 and
213 # if (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101500 || \
214 __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 130000 || \
215 __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ >= 130000 || \
216 __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ >= 60000)
217 # define TEST_HAS_ALIGNED_ALLOC
218 # define TEST_HAS_TIMESPEC_GET
223 /* Features that were introduced in C++14 */
224 #if TEST_STD_VER >= 14
225 #define TEST_HAS_EXTENDED_CONSTEXPR
226 #define TEST_HAS_VARIABLE_TEMPLATES
229 /* Features that were introduced in C++17 */
230 #if TEST_STD_VER >= 17
233 /* Features that were introduced after C++17 */
234 #if TEST_STD_VER > 17
238 #define TEST_ALIGNAS_TYPE(...) TEST_ALIGNAS(TEST_ALIGNOF(__VA_ARGS__))
240 #if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cpp_rtti) \
241 && !defined(__GXX_RTTI)
242 #define TEST_HAS_NO_RTTI
245 #if !defined(TEST_HAS_NO_RTTI)
246 # define RTTI_ASSERT(X) assert(X)
248 # define RTTI_ASSERT(X)
251 #if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \
252 && !defined(__EXCEPTIONS)
253 #define TEST_HAS_NO_EXCEPTIONS
256 #if TEST_HAS_FEATURE(address_sanitizer) || TEST_HAS_FEATURE(memory_sanitizer) || \
257 TEST_HAS_FEATURE(thread_sanitizer)
258 #define TEST_HAS_SANITIZERS
261 #if defined(_LIBCPP_NORETURN)
262 #define TEST_NORETURN _LIBCPP_NORETURN
264 #define TEST_NORETURN [[noreturn]]
267 #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || \
268 (!(TEST_STD_VER > 14 || \
269 (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606L)))
270 #define TEST_HAS_NO_ALIGNED_ALLOCATION
273 #if defined(_LIBCPP_SAFE_STATIC)
274 #define TEST_SAFE_STATIC _LIBCPP_SAFE_STATIC
276 #define TEST_SAFE_STATIC
279 #if !defined(__cpp_impl_three_way_comparison) \
280 && (!defined(_MSC_VER) || defined(__clang__) || _MSC_VER < 1920 || _MSVC_LANG <= 201703L)
281 #define TEST_HAS_NO_SPACESHIP_OPERATOR
284 #if TEST_STD_VER < 11
285 #define ASSERT_NOEXCEPT(...)
286 #define ASSERT_NOT_NOEXCEPT(...)
288 #define ASSERT_NOEXCEPT(...) \
289 static_assert(noexcept(__VA_ARGS__), "Operation must be noexcept")
291 #define ASSERT_NOT_NOEXCEPT(...) \
292 static_assert(!noexcept(__VA_ARGS__), "Operation must NOT be noexcept")
295 /* Macros for testing libc++ specific behavior and extensions */
296 #if defined(_LIBCPP_VERSION)
297 #define LIBCPP_ASSERT(...) assert(__VA_ARGS__)
298 #define LIBCPP_STATIC_ASSERT(...) static_assert(__VA_ARGS__)
299 #define LIBCPP_ASSERT_NOEXCEPT(...) ASSERT_NOEXCEPT(__VA_ARGS__)
300 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ASSERT_NOT_NOEXCEPT(__VA_ARGS__)
301 #define LIBCPP_ONLY(...) __VA_ARGS__
303 #define LIBCPP_ASSERT(...) ((void)0)
304 #define LIBCPP_STATIC_ASSERT(...) ((void)0)
305 #define LIBCPP_ASSERT_NOEXCEPT(...) ((void)0)
306 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ((void)0)
307 #define LIBCPP_ONLY(...) ((void)0)
310 #define TEST_IGNORE_NODISCARD (void)
312 namespace test_macros_detail
{
313 template <class T
, class U
>
314 struct is_same
{ enum { value
= 0};} ;
316 struct is_same
<T
, T
> { enum {value
= 1}; };
317 } // namespace test_macros_detail
319 #define ASSERT_SAME_TYPE(...) \
320 static_assert((test_macros_detail::is_same<__VA_ARGS__>::value), \
321 "Types differ unexpectedly")
323 #ifndef TEST_HAS_NO_EXCEPTIONS
324 #define TEST_THROW(...) throw __VA_ARGS__
326 #if defined(__GNUC__)
327 #define TEST_THROW(...) __builtin_abort()
330 #define TEST_THROW(...) ::abort()
334 #if defined(__GNUC__) || defined(__clang__)
337 void DoNotOptimize(Tp
const& value
) {
338 asm volatile("" : : "r,m"(value
) : "memory");
342 inline void DoNotOptimize(Tp
& value
) {
343 #if defined(__clang__)
344 asm volatile("" : "+r,m"(value
) : : "memory");
346 asm volatile("" : "+m,r"(value
) : : "memory");
352 inline void DoNotOptimize(Tp
const& value
) {
353 const volatile void* volatile unused
= __builtin_addressof(value
);
354 static_cast<void>(unused
);
359 #if defined(__GNUC__)
360 #define TEST_ALWAYS_INLINE __attribute__((always_inline))
361 #define TEST_NOINLINE __attribute__((noinline))
362 #elif defined(_MSC_VER)
363 #define TEST_ALWAYS_INLINE __forceinline
364 #define TEST_NOINLINE __declspec(noinline)
366 #define TEST_ALWAYS_INLINE
367 #define TEST_NOINLINE
370 #if defined(__GNUC__)
371 #pragma GCC diagnostic pop
374 #endif // SUPPORT_TEST_MACROS_HPP