[WebAssembly] Fix asan issue from https://reviews.llvm.org/D121349
[llvm-project.git] / libcxx / test / support / test_macros.h
blob130f3d4f0a948450fa668f5d008dd1b1392ceefa
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 #if defined(__GNUC__)
27 #pragma GCC diagnostic push
28 #pragma GCC diagnostic ignored "-Wvariadic-macros"
29 #endif
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)
37 #ifdef __has_feature
38 #define TEST_HAS_FEATURE(X) __has_feature(X)
39 #else
40 #define TEST_HAS_FEATURE(X) 0
41 #endif
43 #ifndef __has_include
44 #define __has_include(...) 0
45 #endif
47 #ifdef __has_extension
48 #define TEST_HAS_EXTENSION(X) __has_extension(X)
49 #else
50 #define TEST_HAS_EXTENSION(X) 0
51 #endif
53 #ifdef __has_warning
54 #define TEST_HAS_WARNING(X) __has_warning(X)
55 #else
56 #define TEST_HAS_WARNING(X) 0
57 #endif
59 #ifdef __has_builtin
60 #define TEST_HAS_BUILTIN(X) __has_builtin(X)
61 #else
62 #define TEST_HAS_BUILTIN(X) 0
63 #endif
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)
68 #else
69 #define TEST_HAS_BUILTIN_IDENTIFIER(X) 0
70 #endif
72 #if defined(__EDG__)
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
78 # endif
79 #elif defined(_MSC_VER)
80 # define TEST_COMPILER_MSVC
81 #elif defined(__GNUC__)
82 # define TEST_COMPILER_GCC
83 #endif
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 // Given GCC XX.YY.ZZ, TEST_GCC_VER is XXYYZZ
91 #define TEST_GCC_VER ((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__)
92 #endif
94 /* Make a nice name for the standard version */
95 #ifndef TEST_STD_VER
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
104 #elif __cplusplus <= 202002L
105 # define TEST_STD_VER 20
106 #else
107 # define TEST_STD_VER 99 // greater than current standard
108 // This is deliberately different than _LIBCPP_STD_VER to discourage matching them up.
109 #endif
110 #endif
112 // Attempt to deduce the GLIBC version
113 #if (defined(__has_include) && __has_include(<features.h>)) || \
114 defined(__linux__)
115 #include <features.h>
116 #if defined(__GLIBC_PREREQ)
117 #define TEST_HAS_GLIBC
118 #define TEST_GLIBC_PREREQ(major, minor) __GLIBC_PREREQ(major, minor)
119 #endif
120 #endif
122 #if TEST_STD_VER >= 11
123 # define TEST_ALIGNOF(...) alignof(__VA_ARGS__)
124 # define TEST_ALIGNAS(...) alignas(__VA_ARGS__)
125 # define TEST_CONSTEXPR constexpr
126 # define TEST_NOEXCEPT noexcept
127 # define TEST_NOEXCEPT_FALSE noexcept(false)
128 # define TEST_NOEXCEPT_COND(...) noexcept(__VA_ARGS__)
129 #else
130 # if defined(TEST_COMPILER_CLANG)
131 # define TEST_ALIGNOF(...) _Alignof(__VA_ARGS__)
132 # else
133 # define TEST_ALIGNOF(...) __alignof(__VA_ARGS__)
134 # endif
135 # define TEST_ALIGNAS(...) __attribute__((__aligned__(__VA_ARGS__)))
136 # define TEST_CONSTEXPR
137 # define TEST_NOEXCEPT throw()
138 # define TEST_NOEXCEPT_FALSE
139 # define TEST_NOEXCEPT_COND(...)
140 #endif
142 #if TEST_STD_VER >= 11
143 # define TEST_THROW_SPEC(...)
144 #else
145 # define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
146 #endif
148 #if defined(__cpp_lib_is_constant_evaluated) && __cpp_lib_is_constant_evaluated >= 201811L
149 # define TEST_IS_CONSTANT_EVALUATED std::is_constant_evaluated()
150 #elif TEST_HAS_BUILTIN(__builtin_is_constant_evaluated)
151 # define TEST_IS_CONSTANT_EVALUATED __builtin_is_constant_evaluated()
152 #else
153 # define TEST_IS_CONSTANT_EVALUATED false
154 #endif
156 #if TEST_STD_VER >= 14
157 # define TEST_CONSTEXPR_CXX14 constexpr
158 #else
159 # define TEST_CONSTEXPR_CXX14
160 #endif
162 #if TEST_STD_VER >= 17
163 # define TEST_CONSTEXPR_CXX17 constexpr
164 #else
165 # define TEST_CONSTEXPR_CXX17
166 #endif
168 #if TEST_STD_VER >= 20
169 # define TEST_CONSTEXPR_CXX20 constexpr
170 #else
171 # define TEST_CONSTEXPR_CXX20
172 #endif
174 #define TEST_ALIGNAS_TYPE(...) TEST_ALIGNAS(TEST_ALIGNOF(__VA_ARGS__))
176 #if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cpp_rtti) \
177 && !defined(__GXX_RTTI)
178 #define TEST_HAS_NO_RTTI
179 #endif
181 #if !defined(TEST_HAS_NO_RTTI)
182 # define RTTI_ASSERT(X) assert(X)
183 #else
184 # define RTTI_ASSERT(X)
185 #endif
187 #if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \
188 && !defined(__EXCEPTIONS)
189 #define TEST_HAS_NO_EXCEPTIONS
190 #endif
192 #if TEST_HAS_FEATURE(address_sanitizer) || TEST_HAS_FEATURE(memory_sanitizer) || \
193 TEST_HAS_FEATURE(thread_sanitizer)
194 #define TEST_HAS_SANITIZERS
195 #endif
197 #if defined(_LIBCPP_NORETURN)
198 #define TEST_NORETURN _LIBCPP_NORETURN
199 #else
200 #define TEST_NORETURN [[noreturn]]
201 #endif
203 #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || \
204 (!(TEST_STD_VER > 14 || \
205 (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606L)))
206 #define TEST_HAS_NO_ALIGNED_ALLOCATION
207 #endif
209 #if TEST_STD_VER > 17
210 #define TEST_CONSTINIT constinit
211 #elif defined(_LIBCPP_CONSTINIT)
212 #define TEST_CONSTINIT _LIBCPP_CONSTINIT
213 #else
214 #define TEST_CONSTINIT
215 #endif
217 #if !defined(__cpp_impl_three_way_comparison) \
218 && (!defined(_MSC_VER) || defined(__clang__) || _MSC_VER < 1920 || _MSVC_LANG <= 201703L)
219 #define TEST_HAS_NO_SPACESHIP_OPERATOR
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 #define TEST_IGNORE_NODISCARD (void)
250 namespace test_macros_detail {
251 template <class T, class U>
252 struct is_same { enum { value = 0};} ;
253 template <class T>
254 struct is_same<T, T> { enum {value = 1}; };
255 } // namespace test_macros_detail
257 #define ASSERT_SAME_TYPE(...) \
258 static_assert((test_macros_detail::is_same<__VA_ARGS__>::value), \
259 "Types differ unexpectedly")
261 #ifndef TEST_HAS_NO_EXCEPTIONS
262 #define TEST_THROW(...) throw __VA_ARGS__
263 #else
264 #if defined(__GNUC__)
265 #define TEST_THROW(...) __builtin_abort()
266 #else
267 #include <stdlib.h>
268 #define TEST_THROW(...) ::abort()
269 #endif
270 #endif
272 #if defined(__GNUC__) || defined(__clang__)
273 template <class Tp>
274 inline
275 void DoNotOptimize(Tp const& value) {
276 asm volatile("" : : "r,m"(value) : "memory");
279 template <class Tp>
280 inline void DoNotOptimize(Tp& value) {
281 #if defined(__clang__)
282 asm volatile("" : "+r,m"(value) : : "memory");
283 #else
284 asm volatile("" : "+m,r"(value) : : "memory");
285 #endif
287 #else
288 #include <intrin.h>
289 template <class Tp>
290 inline void DoNotOptimize(Tp const& value) {
291 const volatile void* volatile unused = __builtin_addressof(value);
292 static_cast<void>(unused);
293 _ReadWriteBarrier();
295 #endif
297 #if defined(__GNUC__)
298 #define TEST_ALWAYS_INLINE __attribute__((always_inline))
299 #define TEST_NOINLINE __attribute__((noinline))
300 #elif defined(_MSC_VER)
301 #define TEST_ALWAYS_INLINE __forceinline
302 #define TEST_NOINLINE __declspec(noinline)
303 #else
304 #define TEST_ALWAYS_INLINE
305 #define TEST_NOINLINE
306 #endif
308 #ifdef _WIN32
309 #define TEST_NOT_WIN32(...) ((void)0)
310 #else
311 #define TEST_NOT_WIN32(...) __VA_ARGS__
312 #endif
314 #if (defined(_WIN32) && !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)) || \
315 defined(__MVS__) || defined(_AIX)
316 // Macros for waiving cases when we can't count allocations done within
317 // the library implementation.
319 // On Windows, when libc++ is built as a DLL, references to operator new/delete
320 // within the DLL are bound at link time to the operator new/delete within
321 // the library; replacing them in the user executable doesn't override the
322 // calls within the library.
324 // The same goes on IBM zOS.
325 // The same goes on AIX.
326 #define ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(...) ((void)(__VA_ARGS__))
327 #define TEST_SUPPORTS_LIBRARY_INTERNAL_ALLOCATIONS 0
328 #else
329 #define ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(...) assert(__VA_ARGS__)
330 #define TEST_SUPPORTS_LIBRARY_INTERNAL_ALLOCATIONS 1
331 #endif
333 #if (defined(_WIN32) && !defined(_MSC_VER) && \
334 !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)) || \
335 defined(__MVS__)
336 // Normally, a replaced e.g. 'operator new' ends up used if the user code
337 // does a call to e.g. 'operator new[]'; it's enough to replace the base
338 // versions and have it override all of them.
340 // When the fallback operators are located within the libc++ library and we
341 // can't override the calls within it (see above), this fallback mechanism
342 // doesn't work either.
344 // On Windows, when using the MSVC vcruntime, the operator new/delete fallbacks
345 // are linked separately from the libc++ library, linked statically into
346 // the end user executable, and these fallbacks work even in DLL configurations.
347 // In MinGW configurations when built as a DLL, and on zOS, these fallbacks
348 // don't work though.
349 #define ASSERT_WITH_OPERATOR_NEW_FALLBACKS(...) ((void)(__VA_ARGS__))
350 #else
351 #define ASSERT_WITH_OPERATOR_NEW_FALLBACKS(...) assert(__VA_ARGS__)
352 #endif
354 #ifdef _WIN32
355 #define TEST_WIN_NO_FILESYSTEM_PERMS_NONE
356 #endif
358 // Support for carving out parts of the test suite, like removing wide characters, etc.
359 #if defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS)
360 # define TEST_HAS_NO_WIDE_CHARACTERS
361 #endif
363 #if defined(_LIBCPP_HAS_NO_UNICODE)
364 # define TEST_HAS_NO_UNICODE
365 #elif defined(_MSVC_EXECUTION_CHARACTER_SET) && _MSVC_EXECUTION_CHARACTER_SET != 65001
366 # define TEST_HAS_NO_UNICODE
367 #endif
369 #if defined(_LIBCPP_HAS_NO_INT128) || defined(_MSVC_STL_VERSION)
370 # define TEST_HAS_NO_INT128
371 #endif
373 #if defined(_LIBCPP_HAS_NO_UNICODE_CHARS)
374 # define TEST_HAS_NO_UNICODE_CHARS
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_LIBRARY)
390 # define TEST_HAS_NO_FILESYSTEM_LIBRARY
391 #endif
393 #if defined(_LIBCPP_HAS_NO_FGETPOS_FSETPOS)
394 # define TEST_HAS_NO_FGETPOS_FSETPOS
395 #endif
397 #if defined(__GNUC__)
398 #pragma GCC diagnostic pop
399 #endif
401 #endif // SUPPORT_TEST_MACROS_HPP