Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / support / test_macros.h
blob80cf600c858e122f410e132977cd40bd644451e3
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 //===----------------------------------------------------------------------===//
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>.
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
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)
32 #ifdef __has_feature
33 #define TEST_HAS_FEATURE(X) __has_feature(X)
34 #else
35 #define TEST_HAS_FEATURE(X) 0
36 #endif
38 #ifndef __has_include
39 #define __has_include(...) 0
40 #endif
42 #ifdef __has_extension
43 #define TEST_HAS_EXTENSION(X) __has_extension(X)
44 #else
45 #define TEST_HAS_EXTENSION(X) 0
46 #endif
48 #ifdef __has_warning
49 #define TEST_HAS_WARNING(X) __has_warning(X)
50 #else
51 #define TEST_HAS_WARNING(X) 0
52 #endif
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
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
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
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
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
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
140 #if TEST_STD_VER >= 11
141 # define TEST_THROW_SPEC(...)
142 #else
143 # define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
144 #endif
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
154 #if TEST_STD_VER >= 14
155 # define TEST_CONSTEXPR_CXX14 constexpr
156 #else
157 # define TEST_CONSTEXPR_CXX14
158 #endif
160 #if TEST_STD_VER >= 17
161 # define TEST_CONSTEXPR_CXX17 constexpr
162 #else
163 # define TEST_CONSTEXPR_CXX17
164 #endif
166 #if TEST_STD_VER >= 20
167 # define TEST_CONSTEXPR_CXX20 constexpr
168 #else
169 # define TEST_CONSTEXPR_CXX20
170 #endif
172 #if TEST_STD_VER >= 23
173 # define TEST_CONSTEXPR_CXX23 constexpr
174 #else
175 # define TEST_CONSTEXPR_CXX23
176 #endif
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
183 #endif
185 #if !defined(TEST_HAS_NO_RTTI)
186 # define RTTI_ASSERT(X) assert(X)
187 #else
188 # define RTTI_ASSERT(X)
189 #endif
191 #if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \
192 && !defined(__EXCEPTIONS)
193 #define TEST_HAS_NO_EXCEPTIONS
194 #endif
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
202 #if defined(_LIBCPP_NORETURN)
203 #define TEST_NORETURN _LIBCPP_NORETURN
204 #else
205 #define TEST_NORETURN [[noreturn]]
206 #endif
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
214 #if TEST_STD_VER > 17
215 #define TEST_CONSTINIT constinit
216 #elif defined(_LIBCPP_CONSTINIT)
217 #define TEST_CONSTINIT _LIBCPP_CONSTINIT
218 #else
219 #define TEST_CONSTINIT
220 #endif
222 #if TEST_STD_VER < 11
223 #define ASSERT_NOEXCEPT(...)
224 #define ASSERT_NOT_NOEXCEPT(...)
225 #else
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")
231 #endif
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__
240 #else
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, "")
246 #endif
248 #if __has_cpp_attribute(nodiscard)
249 # define TEST_NODISCARD [[nodiscard]]
250 #else
251 # define TEST_NODISCARD
252 #endif
254 #define TEST_IGNORE_NODISCARD (void)
256 namespace test_macros_detail {
257 template <class T, class U>
258 struct is_same { enum { value = 0};} ;
259 template <class T>
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__
269 #else
270 #if defined(__GNUC__)
271 #define TEST_THROW(...) __builtin_abort()
272 #else
273 #include <stdlib.h>
274 #define TEST_THROW(...) ::abort()
275 #endif
276 #endif
278 #if defined(__GNUC__) || defined(__clang__)
279 template <class Tp>
280 inline
281 void DoNotOptimize(Tp const& value) {
282 asm volatile("" : : "r,m"(value) : "memory");
285 template <class Tp>
286 inline void DoNotOptimize(Tp& value) {
287 #if defined(__clang__)
288 asm volatile("" : "+r,m"(value) : : "memory");
289 #else
290 asm volatile("" : "+m,r"(value) : : "memory");
291 #endif
293 #else
294 #include <intrin.h>
295 template <class Tp>
296 inline void DoNotOptimize(Tp const& value) {
297 const volatile void* volatile unused = __builtin_addressof(value);
298 static_cast<void>(unused);
299 _ReadWriteBarrier();
301 #endif
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)
309 #else
310 #define TEST_ALWAYS_INLINE
311 #define TEST_NOINLINE
312 #endif
314 #ifdef _WIN32
315 #define TEST_NOT_WIN32(...) ((void)0)
316 #else
317 #define TEST_NOT_WIN32(...) __VA_ARGS__
318 #endif
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
333 #else
334 #define ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(...) assert(__VA_ARGS__)
335 #define TEST_SUPPORTS_LIBRARY_INTERNAL_ALLOCATIONS 1
336 #endif
338 #if (defined(TEST_WINDOWS_DLL) && !defined(_MSC_VER)) || \
339 defined(__MVS__)
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__))
354 #else
355 #define ASSERT_WITH_OPERATOR_NEW_FALLBACKS(...) assert(__VA_ARGS__)
356 #endif
358 #ifdef _WIN32
359 #define TEST_WIN_NO_FILESYSTEM_PERMS_NONE
360 #endif
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
365 #endif
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
371 #endif
373 #if defined(_LIBCPP_HAS_NO_INT128) || defined(_MSVC_STL_VERSION)
374 # define TEST_HAS_NO_INT128
375 #endif
377 #if defined(_LIBCPP_HAS_NO_LOCALIZATION)
378 # define TEST_HAS_NO_LOCALIZATION
379 #endif
381 #if TEST_STD_VER <= 17 || !defined(__cpp_char8_t)
382 # define TEST_HAS_NO_CHAR8_T
383 #endif
385 #if defined(_LIBCPP_HAS_NO_THREADS)
386 # define TEST_HAS_NO_THREADS
387 #endif
389 #if defined(_LIBCPP_HAS_NO_FILESYSTEM)
390 # define TEST_HAS_NO_FILESYSTEM
391 #endif
393 #if defined(_LIBCPP_HAS_NO_FGETPOS_FSETPOS)
394 # define TEST_HAS_NO_FGETPOS_FSETPOS
395 #endif
397 #if defined(_LIBCPP_HAS_NO_C8RTOMB_MBRTOC8)
398 # define TEST_HAS_NO_C8RTOMB_MBRTOC8
399 #endif
401 #if defined(_LIBCPP_HAS_NO_RANDOM_DEVICE)
402 # define TEST_HAS_NO_RANDOM_DEVICE
403 #endif
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)))
423 #else
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)
429 #endif
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]]
435 #else
436 #define TEST_NO_UNIQUE_ADDRESS
437 #endif
439 #ifdef _LIBCPP_SHORT_WCHAR
440 # define TEST_SHORT_WCHAR
441 #endif
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.
446 #ifdef __APPLE__
447 # define TEST_WORKAROUND_BUG_109234844_WEAK __attribute__((weak))
448 #else
449 # define TEST_WORKAROUND_BUG_109234844_WEAK /* nothing */
450 #endif
452 #endif // SUPPORT_TEST_MACROS_HPP