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 #define TEST_STRINGIZE_IMPL(x) #x
27 #define TEST_STRINGIZE(x) TEST_STRINGIZE_IMPL(x)
28 
29 #define TEST_CONCAT1(X, Y) X##Y
30 #define TEST_CONCAT(X, Y) TEST_CONCAT1(X, Y)
31 
32 #ifdef __has_feature
33 #define TEST_HAS_FEATURE(X) __has_feature(X)
34 #else
35 #define TEST_HAS_FEATURE(X) 0
36 #endif
37 
38 #ifndef __has_include
39 #define __has_include(...) 0
40 #endif
41 
42 #ifdef __has_extension
43 #define TEST_HAS_EXTENSION(X) __has_extension(X)
44 #else
45 #define TEST_HAS_EXTENSION(X) 0
46 #endif
47 
48 #ifdef __has_warning
49 #define TEST_HAS_WARNING(X) __has_warning(X)
50 #else
51 #define TEST_HAS_WARNING(X) 0
52 #endif
53 
54 #ifdef __has_builtin
55 #define TEST_HAS_BUILTIN(X) __has_builtin(X)
56 #else
57 #define TEST_HAS_BUILTIN(X) 0
58 #endif
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)
63 #else
64 #define TEST_HAS_BUILTIN_IDENTIFIER(X) 0
65 #endif
66 
67 #if defined(__EDG__)
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
73 # endif
74 #elif defined(_MSC_VER)
75 # define TEST_COMPILER_MSVC
76 #elif defined(__GNUC__)
77 # define TEST_COMPILER_GCC
78 #endif
79 
80 #if defined(__apple_build_version__)
81 #define TEST_APPLE_CLANG_VER (__clang_major__ * 100) + __clang_minor__
82 #elif defined(__clang_major__)
83 #define TEST_CLANG_VER (__clang_major__ * 100) + __clang_minor__
84 #elif defined(__GNUC__)
85 // Given GCC XX.YY.ZZ, TEST_GCC_VER is XXYYZZ
86 #define TEST_GCC_VER ((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__)
87 #endif
88 
89 /* Make a nice name for the standard version */
90 #ifndef TEST_STD_VER
91 #if  __cplusplus <= 199711L
92 # define TEST_STD_VER 3
93 #elif __cplusplus <= 201103L
94 # define TEST_STD_VER 11
95 #elif __cplusplus <= 201402L
96 # define TEST_STD_VER 14
97 #elif __cplusplus <= 201703L
98 # define TEST_STD_VER 17
99 #elif __cplusplus <= 202002L
100 # define TEST_STD_VER 20
101 #else
102 # define TEST_STD_VER 99    // greater than current standard
103 // This is deliberately different than _LIBCPP_STD_VER to discourage matching them up.
104 #endif
105 #endif
106 
107 // Attempt to deduce the GLIBC version
108 #if (defined(__has_include) && __has_include(<features.h>)) || \
109     defined(__linux__)
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)
114 #endif
115 #endif
116 
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__)
124 #else
125 #   if defined(TEST_COMPILER_CLANG)
126 #    define TEST_ALIGNOF(...) _Alignof(__VA_ARGS__)
127 #   else
128 #    define TEST_ALIGNOF(...) __alignof(__VA_ARGS__)
129 #   endif
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(...)
135 #endif
136 
137 #if TEST_STD_VER >= 11
138 # define TEST_THROW_SPEC(...)
139 #else
140 # define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
141 #endif
142 
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()
147 #else
148 # define TEST_IS_CONSTANT_EVALUATED false
149 #endif
150 
151 #if TEST_STD_VER >= 14
152 # define TEST_CONSTEXPR_CXX14 constexpr
153 #else
154 # define TEST_CONSTEXPR_CXX14
155 #endif
156 
157 #if TEST_STD_VER >= 17
158 # define TEST_CONSTEXPR_CXX17 constexpr
159 #else
160 # define TEST_CONSTEXPR_CXX17
161 #endif
162 
163 #if TEST_STD_VER >= 20
164 # define TEST_CONSTEXPR_CXX20 constexpr
165 #else
166 # define TEST_CONSTEXPR_CXX20
167 #endif
168 
169 #define TEST_ALIGNAS_TYPE(...) TEST_ALIGNAS(TEST_ALIGNOF(__VA_ARGS__))
170 
171 #if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cpp_rtti) \
172     && !defined(__GXX_RTTI)
173 #define TEST_HAS_NO_RTTI
174 #endif
175 
176 #if !defined(TEST_HAS_NO_RTTI)
177 # define RTTI_ASSERT(X) assert(X)
178 #else
179 # define RTTI_ASSERT(X)
180 #endif
181 
182 #if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \
183      && !defined(__EXCEPTIONS)
184 #define TEST_HAS_NO_EXCEPTIONS
185 #endif
186 
187 #if TEST_HAS_FEATURE(address_sanitizer) || TEST_HAS_FEATURE(memory_sanitizer) || \
188     TEST_HAS_FEATURE(thread_sanitizer)
189 #define TEST_HAS_SANITIZERS
190 #endif
191 
192 #if defined(_LIBCPP_NORETURN)
193 #define TEST_NORETURN _LIBCPP_NORETURN
194 #else
195 #define TEST_NORETURN [[noreturn]]
196 #endif
197 
198 #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || \
199   (!(TEST_STD_VER > 14 || \
200     (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606L)))
201 #define TEST_HAS_NO_ALIGNED_ALLOCATION
202 #endif
203 
204 #if TEST_STD_VER > 17
205 #define TEST_CONSTINIT constinit
206 #elif defined(_LIBCPP_CONSTINIT)
207 #define TEST_CONSTINIT _LIBCPP_CONSTINIT
208 #else
209 #define TEST_CONSTINIT
210 #endif
211 
212 #if !defined(__cpp_impl_three_way_comparison) \
213     && (!defined(_MSC_VER) || defined(__clang__) || _MSC_VER < 1920 || _MSVC_LANG <= 201703L)
214 #define TEST_HAS_NO_SPACESHIP_OPERATOR
215 #endif
216 
217 #if TEST_STD_VER < 11
218 #define ASSERT_NOEXCEPT(...)
219 #define ASSERT_NOT_NOEXCEPT(...)
220 #else
221 #define ASSERT_NOEXCEPT(...) \
222     static_assert(noexcept(__VA_ARGS__), "Operation must be noexcept")
223 
224 #define ASSERT_NOT_NOEXCEPT(...) \
225     static_assert(!noexcept(__VA_ARGS__), "Operation must NOT be noexcept")
226 #endif
227 
228 /* Macros for testing libc++ specific behavior and extensions */
229 #if defined(_LIBCPP_VERSION)
230 #define LIBCPP_ASSERT(...) assert(__VA_ARGS__)
231 #define LIBCPP_STATIC_ASSERT(...) static_assert(__VA_ARGS__)
232 #define LIBCPP_ASSERT_NOEXCEPT(...) ASSERT_NOEXCEPT(__VA_ARGS__)
233 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ASSERT_NOT_NOEXCEPT(__VA_ARGS__)
234 #define LIBCPP_ONLY(...) __VA_ARGS__
235 #else
236 #define LIBCPP_ASSERT(...) static_assert(true, "")
237 #define LIBCPP_STATIC_ASSERT(...) static_assert(true, "")
238 #define LIBCPP_ASSERT_NOEXCEPT(...) static_assert(true, "")
239 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) static_assert(true, "")
240 #define LIBCPP_ONLY(...) static_assert(true, "")
241 #endif
242 
243 #define TEST_IGNORE_NODISCARD (void)
244 
245 namespace test_macros_detail {
246 template <class T, class U>
247 struct is_same { enum { value = 0};} ;
248 template <class T>
249 struct is_same<T, T> { enum {value = 1}; };
250 } // namespace test_macros_detail
251 
252 #define ASSERT_SAME_TYPE(...) \
253     static_assert((test_macros_detail::is_same<__VA_ARGS__>::value), \
254                  "Types differ unexpectedly")
255 
256 #ifndef TEST_HAS_NO_EXCEPTIONS
257 #define TEST_THROW(...) throw __VA_ARGS__
258 #else
259 #if defined(__GNUC__)
260 #define TEST_THROW(...) __builtin_abort()
261 #else
262 #include <stdlib.h>
263 #define TEST_THROW(...) ::abort()
264 #endif
265 #endif
266 
267 #if defined(__GNUC__) || defined(__clang__)
268 template <class Tp>
269 inline
270 void DoNotOptimize(Tp const& value) {
271     asm volatile("" : : "r,m"(value) : "memory");
272 }
273 
274 template <class Tp>
275 inline void DoNotOptimize(Tp& value) {
276 #if defined(__clang__)
277   asm volatile("" : "+r,m"(value) : : "memory");
278 #else
279   asm volatile("" : "+m,r"(value) : : "memory");
280 #endif
281 }
282 #else
283 #include <intrin.h>
284 template <class Tp>
285 inline void DoNotOptimize(Tp const& value) {
286   const volatile void* volatile unused = __builtin_addressof(value);
287   static_cast<void>(unused);
288   _ReadWriteBarrier();
289 }
290 #endif
291 
292 #if defined(__GNUC__)
293 #define TEST_ALWAYS_INLINE __attribute__((always_inline))
294 #define TEST_NOINLINE __attribute__((noinline))
295 #elif defined(_MSC_VER)
296 #define TEST_ALWAYS_INLINE __forceinline
297 #define TEST_NOINLINE __declspec(noinline)
298 #else
299 #define TEST_ALWAYS_INLINE
300 #define TEST_NOINLINE
301 #endif
302 
303 #ifdef _WIN32
304 #define TEST_NOT_WIN32(...) ((void)0)
305 #else
306 #define TEST_NOT_WIN32(...) __VA_ARGS__
307 #endif
308 
309 #if defined(TEST_WINDOWS_DLL) ||defined(__MVS__) || defined(_AIX)
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 // The same goes on AIX.
320 #define ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(...) ((void)(__VA_ARGS__))
321 #define TEST_SUPPORTS_LIBRARY_INTERNAL_ALLOCATIONS 0
322 #else
323 #define ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(...) assert(__VA_ARGS__)
324 #define TEST_SUPPORTS_LIBRARY_INTERNAL_ALLOCATIONS 1
325 #endif
326 
327 #if (defined(TEST_WINDOWS_DLL) && !defined(_MSC_VER)) ||                      \
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(_LIBCPP_HAS_NO_UNICODE)
357 #   define TEST_HAS_NO_UNICODE
358 #elif defined(_MSVC_EXECUTION_CHARACTER_SET) && _MSVC_EXECUTION_CHARACTER_SET != 65001
359 #   define TEST_HAS_NO_UNICODE
360 #endif
361 
362 #if defined(_LIBCPP_HAS_NO_INT128) || defined(_MSVC_STL_VERSION)
363 #   define TEST_HAS_NO_INT128
364 #endif
365 
366 #if defined(_LIBCPP_HAS_NO_LOCALIZATION)
367 #  define TEST_HAS_NO_LOCALIZATION
368 #endif
369 
370 #if TEST_STD_VER <= 17 || !defined(__cpp_char8_t)
371 #  define TEST_HAS_NO_CHAR8_T
372 #endif
373 
374 #if defined(_LIBCPP_HAS_NO_THREADS)
375 #  define TEST_HAS_NO_THREADS
376 #endif
377 
378 #if defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
379 #  define TEST_HAS_NO_FILESYSTEM_LIBRARY
380 #endif
381 
382 #if defined(_LIBCPP_HAS_NO_FGETPOS_FSETPOS)
383 #  define TEST_HAS_NO_FGETPOS_FSETPOS
384 #endif
385 
386 #if defined(TEST_COMPILER_CLANG)
387 #  define TEST_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
388 #  define TEST_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
389 #  define TEST_CLANG_DIAGNOSTIC_IGNORED(str) _Pragma(TEST_STRINGIZE(clang diagnostic ignored str))
390 #  define TEST_GCC_DIAGNOSTIC_IGNORED(str)
391 #  define TEST_MSVC_DIAGNOSTIC_IGNORED(num)
392 #elif defined(TEST_COMPILER_GCC)
393 #  define TEST_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
394 #  define TEST_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
395 #  define TEST_CLANG_DIAGNOSTIC_IGNORED(str)
396 #  define TEST_GCC_DIAGNOSTIC_IGNORED(str) _Pragma(TEST_STRINGIZE(GCC diagnostic ignored str))
397 #  define TEST_MSVC_DIAGNOSTIC_IGNORED(num)
398 #elif defined(TEST_COMPILER_MSVC)
399 #  define TEST_DIAGNOSTIC_PUSH _Pragma("warning(push)")
400 #  define TEST_DIAGNOSTIC_POP _Pragma("warning(pop)")
401 #  define TEST_CLANG_DIAGNOSTIC_IGNORED(str)
402 #  define TEST_GCC_DIAGNOSTIC_IGNORED(str)
403 #  define TEST_MSVC_DIAGNOSTIC_IGNORED(num) _Pragma(TEST_STRINGIZE(warning(disable: num)))
404 #else
405 #  define TEST_DIAGNOSTIC_PUSH
406 #  define TEST_DIAGNOSTIC_POP
407 #  define TEST_CLANG_DIAGNOSTIC_IGNORED(str)
408 #  define TEST_GCC_DIAGNOSTIC_IGNORED(str)
409 #  define TEST_MSVC_DIAGNOSTIC_IGNORED(num)
410 #endif
411 
412 #if __has_cpp_attribute(msvc::no_unique_address)
413 #define TEST_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
414 #elif __has_cpp_attribute(no_unique_address)
415 #define TEST_NO_UNIQUE_ADDRESS [[no_unique_address]]
416 #else
417 #define TEST_NO_UNIQUE_ADDRESS
418 #endif
419 
420 #endif // SUPPORT_TEST_MACROS_HPP
421