• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(...) #__VA_ARGS__
27 #define TEST_STRINGIZE(...) TEST_STRINGIZE_IMPL(__VA_ARGS__)
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 // 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__)
88 #endif
89 
90 /* Make a nice name for the standard version */
91 #ifndef TEST_STD_VER
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
104 #else
105 # define TEST_STD_VER 99    // greater than current standard
106 // This is deliberately different than _LIBCPP_STD_VER to discourage matching them up.
107 #endif
108 #endif
109 
110 // Attempt to deduce the GLIBC version
111 #if (defined(__has_include) && __has_include(<features.h>)) || \
112     defined(__linux__)
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)
117 #endif
118 #endif
119 
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__)
127 #else
128 #   if defined(TEST_COMPILER_CLANG)
129 #    define TEST_ALIGNOF(...) _Alignof(__VA_ARGS__)
130 #   else
131 #    define TEST_ALIGNOF(...) __alignof(__VA_ARGS__)
132 #   endif
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(...)
138 #endif
139 
140 #if TEST_STD_VER >= 11
141 # define TEST_THROW_SPEC(...)
142 #else
143 # define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
144 #endif
145 
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()
150 #else
151 # define TEST_IS_CONSTANT_EVALUATED false
152 #endif
153 
154 #if TEST_STD_VER >= 14
155 # define TEST_CONSTEXPR_CXX14 constexpr
156 #else
157 # define TEST_CONSTEXPR_CXX14
158 #endif
159 
160 #if TEST_STD_VER >= 17
161 # define TEST_CONSTEXPR_CXX17 constexpr
162 #else
163 # define TEST_CONSTEXPR_CXX17
164 #endif
165 
166 #if TEST_STD_VER >= 20
167 # define TEST_CONSTEXPR_CXX20 constexpr
168 #else
169 # define TEST_CONSTEXPR_CXX20
170 #endif
171 
172 #if TEST_STD_VER >= 23
173 #  define TEST_CONSTEXPR_CXX23 constexpr
174 #else
175 #  define TEST_CONSTEXPR_CXX23
176 #endif
177 
178 #define TEST_ALIGNAS_TYPE(...) TEST_ALIGNAS(TEST_ALIGNOF(__VA_ARGS__))
179 
180 #if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cpp_rtti) \
181     && !defined(__GXX_RTTI)
182 #define TEST_HAS_NO_RTTI
183 #endif
184 
185 #if !defined(TEST_HAS_NO_RTTI)
186 # define RTTI_ASSERT(X) assert(X)
187 #else
188 # define RTTI_ASSERT(X)
189 #endif
190 
191 #if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \
192      && !defined(__EXCEPTIONS)
193 #define TEST_HAS_NO_EXCEPTIONS
194 #endif
195 
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
200 #endif
201 
202 #if defined(_LIBCPP_NORETURN)
203 #define TEST_NORETURN _LIBCPP_NORETURN
204 #else
205 #define TEST_NORETURN [[noreturn]]
206 #endif
207 
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
212 #endif
213 
214 #if TEST_STD_VER > 17
215 #define TEST_CONSTINIT constinit
216 #else
217 #define TEST_CONSTINIT
218 #endif
219 
220 #if TEST_STD_VER < 11
221 #define ASSERT_NOEXCEPT(...)
222 #define ASSERT_NOT_NOEXCEPT(...)
223 #else
224 #define ASSERT_NOEXCEPT(...) \
225     static_assert(noexcept(__VA_ARGS__), "Operation must be noexcept")
226 
227 #define ASSERT_NOT_NOEXCEPT(...) \
228     static_assert(!noexcept(__VA_ARGS__), "Operation must NOT be noexcept")
229 #endif
230 
231 /* Macros for testing libc++ specific behavior and extensions */
232 #if defined(_LIBCPP_VERSION)
233 #define LIBCPP_ASSERT(...) assert(__VA_ARGS__)
234 #define LIBCPP_STATIC_ASSERT(...) static_assert(__VA_ARGS__)
235 #define LIBCPP_ASSERT_NOEXCEPT(...) ASSERT_NOEXCEPT(__VA_ARGS__)
236 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ASSERT_NOT_NOEXCEPT(__VA_ARGS__)
237 #define LIBCPP_ONLY(...) __VA_ARGS__
238 #else
239 #define LIBCPP_ASSERT(...) static_assert(true, "")
240 #define LIBCPP_STATIC_ASSERT(...) static_assert(true, "")
241 #define LIBCPP_ASSERT_NOEXCEPT(...) static_assert(true, "")
242 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) static_assert(true, "")
243 #define LIBCPP_ONLY(...) static_assert(true, "")
244 #endif
245 
246 #if __has_cpp_attribute(nodiscard)
247 #  define TEST_NODISCARD [[nodiscard]]
248 #else
249 #  define TEST_NODISCARD
250 #endif
251 
252 #define TEST_IGNORE_NODISCARD (void)
253 
254 namespace test_macros_detail {
255 template <class T, class U>
256 struct is_same { enum { value = 0};} ;
257 template <class T>
258 struct is_same<T, T> { enum {value = 1}; };
259 } // namespace test_macros_detail
260 
261 #define ASSERT_SAME_TYPE(...) \
262     static_assert((test_macros_detail::is_same<__VA_ARGS__>::value), \
263                  "Types differ unexpectedly")
264 
265 #ifndef TEST_HAS_NO_EXCEPTIONS
266 #define TEST_THROW(...) throw __VA_ARGS__
267 #else
268 #if defined(__GNUC__)
269 #define TEST_THROW(...) __builtin_abort()
270 #else
271 #include <stdlib.h>
272 #define TEST_THROW(...) ::abort()
273 #endif
274 #endif
275 
276 #if defined(__GNUC__) || defined(__clang__)
277 template <class Tp>
278 inline
279 void DoNotOptimize(Tp const& value) {
280     asm volatile("" : : "r,m"(value) : "memory");
281 }
282 
283 template <class Tp>
284 inline void DoNotOptimize(Tp& value) {
285 #if defined(__clang__)
286   asm volatile("" : "+r,m"(value) : : "memory");
287 #else
288   asm volatile("" : "+m,r"(value) : : "memory");
289 #endif
290 }
291 #else
292 #include <intrin.h>
293 template <class Tp>
294 inline void DoNotOptimize(Tp const& value) {
295   const volatile void* volatile unused = __builtin_addressof(value);
296   static_cast<void>(unused);
297   _ReadWriteBarrier();
298 }
299 #endif
300 
301 #if defined(__GNUC__)
302 #define TEST_ALWAYS_INLINE __attribute__((always_inline))
303 #define TEST_NOINLINE __attribute__((noinline))
304 #elif defined(_MSC_VER)
305 #define TEST_ALWAYS_INLINE __forceinline
306 #define TEST_NOINLINE __declspec(noinline)
307 #else
308 #define TEST_ALWAYS_INLINE
309 #define TEST_NOINLINE
310 #endif
311 
312 #ifdef _WIN32
313 #define TEST_NOT_WIN32(...) ((void)0)
314 #else
315 #define TEST_NOT_WIN32(...) __VA_ARGS__
316 #endif
317 
318 #if defined(TEST_WINDOWS_DLL) ||defined(__MVS__) || defined(_AIX)
319 // Macros for waiving cases when we can't count allocations done within
320 // the library implementation.
321 //
322 // On Windows, when libc++ is built as a DLL, references to operator new/delete
323 // within the DLL are bound at link time to the operator new/delete within
324 // the library; replacing them in the user executable doesn't override the
325 // calls within the library.
326 //
327 // The same goes on IBM zOS.
328 // The same goes on AIX.
329 #define ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(...) ((void)(__VA_ARGS__))
330 #define TEST_SUPPORTS_LIBRARY_INTERNAL_ALLOCATIONS 0
331 #else
332 #define ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(...) assert(__VA_ARGS__)
333 #define TEST_SUPPORTS_LIBRARY_INTERNAL_ALLOCATIONS 1
334 #endif
335 
336 #if (defined(TEST_WINDOWS_DLL) && !defined(_MSC_VER)) ||                      \
337     defined(__MVS__)
338 // Normally, a replaced e.g. 'operator new' ends up used if the user code
339 // does a call to e.g. 'operator new[]'; it's enough to replace the base
340 // versions and have it override all of them.
341 //
342 // When the fallback operators are located within the libc++ library and we
343 // can't override the calls within it (see above), this fallback mechanism
344 // doesn't work either.
345 //
346 // On Windows, when using the MSVC vcruntime, the operator new/delete fallbacks
347 // are linked separately from the libc++ library, linked statically into
348 // the end user executable, and these fallbacks work even in DLL configurations.
349 // In MinGW configurations when built as a DLL, and on zOS, these fallbacks
350 // don't work though.
351 #define ASSERT_WITH_OPERATOR_NEW_FALLBACKS(...) ((void)(__VA_ARGS__))
352 #else
353 #define ASSERT_WITH_OPERATOR_NEW_FALLBACKS(...) assert(__VA_ARGS__)
354 #endif
355 
356 #ifdef _WIN32
357 #define TEST_WIN_NO_FILESYSTEM_PERMS_NONE
358 #endif
359 
360 // Support for carving out parts of the test suite, like removing wide characters, etc.
361 #if defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS)
362 #   define TEST_HAS_NO_WIDE_CHARACTERS
363 #endif
364 
365 #if defined(_LIBCPP_HAS_NO_UNICODE)
366 #   define TEST_HAS_NO_UNICODE
367 #elif defined(_MSVC_EXECUTION_CHARACTER_SET) && _MSVC_EXECUTION_CHARACTER_SET != 65001
368 #   define TEST_HAS_NO_UNICODE
369 #endif
370 
371 #if defined(_LIBCPP_HAS_NO_INT128) || defined(_MSVC_STL_VERSION)
372 #   define TEST_HAS_NO_INT128
373 #endif
374 
375 #if defined(_LIBCPP_HAS_NO_LOCALIZATION)
376 #  define TEST_HAS_NO_LOCALIZATION
377 #endif
378 
379 #if TEST_STD_VER <= 17 || !defined(__cpp_char8_t)
380 #  define TEST_HAS_NO_CHAR8_T
381 #endif
382 
383 #if defined(_LIBCPP_HAS_NO_THREADS)
384 #  define TEST_HAS_NO_THREADS
385 #endif
386 
387 #if defined(_LIBCPP_HAS_NO_FILESYSTEM)
388 #  define TEST_HAS_NO_FILESYSTEM
389 #endif
390 
391 #if defined(_LIBCPP_HAS_NO_C8RTOMB_MBRTOC8)
392 #  define TEST_HAS_NO_C8RTOMB_MBRTOC8
393 #endif
394 
395 #if defined(_LIBCPP_HAS_NO_RANDOM_DEVICE)
396 #  define TEST_HAS_NO_RANDOM_DEVICE
397 #endif
398 
399 #if defined(TEST_COMPILER_CLANG)
400 #  define TEST_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
401 #  define TEST_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
402 #  define TEST_CLANG_DIAGNOSTIC_IGNORED(str) _Pragma(TEST_STRINGIZE(clang diagnostic ignored str))
403 #  define TEST_GCC_DIAGNOSTIC_IGNORED(str)
404 #  define TEST_MSVC_DIAGNOSTIC_IGNORED(num)
405 #elif defined(TEST_COMPILER_GCC)
406 #  define TEST_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
407 #  define TEST_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
408 #  define TEST_CLANG_DIAGNOSTIC_IGNORED(str)
409 #  define TEST_GCC_DIAGNOSTIC_IGNORED(str) _Pragma(TEST_STRINGIZE(GCC diagnostic ignored str))
410 #  define TEST_MSVC_DIAGNOSTIC_IGNORED(num)
411 #elif defined(TEST_COMPILER_MSVC)
412 #  define TEST_DIAGNOSTIC_PUSH _Pragma("warning(push)")
413 #  define TEST_DIAGNOSTIC_POP _Pragma("warning(pop)")
414 #  define TEST_CLANG_DIAGNOSTIC_IGNORED(str)
415 #  define TEST_GCC_DIAGNOSTIC_IGNORED(str)
416 #  define TEST_MSVC_DIAGNOSTIC_IGNORED(num) _Pragma(TEST_STRINGIZE(warning(disable: num)))
417 #else
418 #  define TEST_DIAGNOSTIC_PUSH
419 #  define TEST_DIAGNOSTIC_POP
420 #  define TEST_CLANG_DIAGNOSTIC_IGNORED(str)
421 #  define TEST_GCC_DIAGNOSTIC_IGNORED(str)
422 #  define TEST_MSVC_DIAGNOSTIC_IGNORED(num)
423 #endif
424 
425 #if __has_cpp_attribute(msvc::no_unique_address)
426 #define TEST_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
427 #elif __has_cpp_attribute(no_unique_address)
428 #define TEST_NO_UNIQUE_ADDRESS [[no_unique_address]]
429 #else
430 #define TEST_NO_UNIQUE_ADDRESS
431 #endif
432 
433 #ifdef _LIBCPP_SHORT_WCHAR
434 #  define TEST_SHORT_WCHAR
435 #endif
436 
437 // This is a temporary workaround for user-defined `operator new` definitions
438 // not being picked up on Apple platforms in some circumstances. This is under
439 // investigation and should be short-lived.
440 #ifdef __APPLE__
441 #  define TEST_WORKAROUND_BUG_109234844_WEAK __attribute__((weak))
442 #else
443 #  define TEST_WORKAROUND_BUG_109234844_WEAK /* nothing */
444 #endif
445 
446 #ifdef _AIX
447 #  define TEST_IF_AIX(arg_true, arg_false) arg_true
448 #else
449 #  define TEST_IF_AIX(arg_true, arg_false) arg_false
450 #endif
451 
452 #endif // SUPPORT_TEST_MACROS_HPP
453