1 // -*- C++ -*-
2 //===---------------------------- test_macros.h ---------------------------===//
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 #if defined(__GNUC__)
27 #pragma GCC diagnostic push
28 #pragma GCC diagnostic ignored "-Wvariadic-macros"
29 #endif
30 
31 #define TEST_STRINGIZE_IMPL(x) #x
32 #define TEST_STRINGIZE(x) TEST_STRINGIZE_IMPL(x)
33 
34 #define TEST_CONCAT1(X, Y) X##Y
35 #define TEST_CONCAT(X, Y) TEST_CONCAT1(X, Y)
36 
37 #ifdef __has_feature
38 #define TEST_HAS_FEATURE(X) __has_feature(X)
39 #else
40 #define TEST_HAS_FEATURE(X) 0
41 #endif
42 
43 #ifndef __has_include
44 #define __has_include(...) 0
45 #endif
46 
47 #ifdef __has_extension
48 #define TEST_HAS_EXTENSION(X) __has_extension(X)
49 #else
50 #define TEST_HAS_EXTENSION(X) 0
51 #endif
52 
53 #ifdef __has_warning
54 #define TEST_HAS_WARNING(X) __has_warning(X)
55 #else
56 #define TEST_HAS_WARNING(X) 0
57 #endif
58 
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
71 
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_C1XX
81 #elif defined(__GNUC__)
82 # define TEST_COMPILER_GCC
83 #endif
84 
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__)
92 #endif
93 
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
111 
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
121 
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
141 
142 #if TEST_STD_VER >= 17
143 # define TEST_THROW_SPEC(...)
144 #else
145 # define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
146 #endif
147 
148 #if TEST_STD_VER >= 14
149 # define TEST_CONSTEXPR_CXX14 constexpr
150 #else
151 # define TEST_CONSTEXPR_CXX14
152 #endif
153 
154 #if TEST_STD_VER >= 17
155 # define TEST_CONSTEXPR_CXX17 constexpr
156 #else
157 # define TEST_CONSTEXPR_CXX17
158 #endif
159 
160 #if TEST_STD_VER >= 20
161 # define TEST_CONSTEXPR_CXX20 constexpr
162 #else
163 # define TEST_CONSTEXPR_CXX20
164 #endif
165 
166 // Sniff out to see if the underlying C library has C11 features
167 // This is cribbed from __config; but lives here as well because we can't assume libc++
168 #if __ISO_C_VISIBLE >= 2011 || TEST_STD_VER >= 11
169 #  if defined(__FreeBSD__)
170 #    if __FreeBSD_version >= 1300064 || \
171        (__FreeBSD_version >= 1201504 && __FreeBSD_version < 1300000)
172 #      define TEST_HAS_TIMESPEC_GET
173 #    endif
174 #    define TEST_HAS_ALIGNED_ALLOC
175 #    define TEST_HAS_QUICK_EXIT
176 #  elif defined(__BIONIC__)
177 #    if __ANDROID_API__ >= 21
178 #      define TEST_HAS_QUICK_EXIT
179 #    endif
180 #    if __ANDROID_API__ >= 28
181 #      define TEST_HAS_ALIGNED_ALLOC
182 #    endif
183 #    if __ANDROID_API__ >= 29
184 #      define TEST_HAS_TIMESPEC_GET
185 #    endif
186 #  elif defined(__Fuchsia__) || defined(__wasi__) || defined(__NetBSD__)
187 #    define TEST_HAS_QUICK_EXIT
188 #    define TEST_HAS_ALIGNED_ALLOC
189 #    define TEST_HAS_TIMESPEC_GET
190 #  elif defined(__linux__)
191 // This block preserves the old behavior used by include/__config:
192 // _LIBCPP_GLIBC_PREREQ would be defined to 0 if __GLIBC_PREREQ was not
193 // available. The configuration here may be too vague though, as Bionic, uClibc,
194 // newlib, etc may all support these features but need to be configured.
195 #    if defined(TEST_GLIBC_PREREQ)
196 #      if TEST_GLIBC_PREREQ(2, 15)
197 #        define TEST_HAS_QUICK_EXIT
198 #      endif
199 #      if TEST_GLIBC_PREREQ(2, 17)
200 #        define TEST_HAS_ALIGNED_ALLOC
201 #        define TEST_HAS_TIMESPEC_GET
202 #      endif
203 #    elif defined(_LIBCPP_HAS_MUSL_LIBC)
204 #      define TEST_HAS_QUICK_EXIT
205 #      define TEST_HAS_ALIGNED_ALLOC
206 #      define TEST_HAS_TIMESPEC_GET
207 #    endif
208 #  elif defined(_WIN32)
209 #    if defined(_MSC_VER) && !defined(__MINGW32__)
210 #      define TEST_HAS_QUICK_EXIT
211 #      define TEST_HAS_ALIGNED_ALLOC
212 #      define TEST_HAS_TIMESPEC_GET
213 #    endif
214 #  elif defined(__APPLE__)
215      // timespec_get and aligned_alloc were introduced in macOS 10.15 and
216      // aligned releases
217 #    if (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101500 || \
218          __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 130000 || \
219          __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ >= 130000 || \
220          __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ >= 60000)
221 #      define TEST_HAS_ALIGNED_ALLOC
222 #      define TEST_HAS_TIMESPEC_GET
223 #    endif
224 #  endif // __APPLE__
225 #endif
226 
227 /* Features that were introduced in C++14 */
228 #if TEST_STD_VER >= 14
229 #define TEST_HAS_EXTENDED_CONSTEXPR
230 #define TEST_HAS_VARIABLE_TEMPLATES
231 #endif
232 
233 /* Features that were introduced in C++17 */
234 #if TEST_STD_VER >= 17
235 #endif
236 
237 /* Features that were introduced after C++17 */
238 #if TEST_STD_VER > 17
239 #endif
240 
241 
242 #define TEST_ALIGNAS_TYPE(...) TEST_ALIGNAS(TEST_ALIGNOF(__VA_ARGS__))
243 
244 #if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cpp_rtti) \
245     && !defined(__GXX_RTTI)
246 #define TEST_HAS_NO_RTTI
247 #endif
248 
249 #if !defined(TEST_HAS_NO_RTTI)
250 # define RTTI_ASSERT(X) assert(X)
251 #else
252 # define RTTI_ASSERT(X)
253 #endif
254 
255 #if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \
256      && !defined(__EXCEPTIONS)
257 #define TEST_HAS_NO_EXCEPTIONS
258 #endif
259 
260 #if TEST_HAS_FEATURE(address_sanitizer) || TEST_HAS_FEATURE(memory_sanitizer) || \
261     TEST_HAS_FEATURE(thread_sanitizer)
262 #define TEST_HAS_SANITIZERS
263 #endif
264 
265 #if defined(_LIBCPP_NORETURN)
266 #define TEST_NORETURN _LIBCPP_NORETURN
267 #else
268 #define TEST_NORETURN [[noreturn]]
269 #endif
270 
271 #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || \
272   (!(TEST_STD_VER > 14 || \
273     (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606L)))
274 #define TEST_HAS_NO_ALIGNED_ALLOCATION
275 #endif
276 
277 #if defined(_LIBCPP_SAFE_STATIC)
278 #define TEST_SAFE_STATIC _LIBCPP_SAFE_STATIC
279 #else
280 #define TEST_SAFE_STATIC
281 #endif
282 
283 #if !defined(__cpp_impl_three_way_comparison) \
284     && (!defined(_MSC_VER) || defined(__clang__) || _MSC_VER < 1920 || _MSVC_LANG <= 201703L)
285 #define TEST_HAS_NO_SPACESHIP_OPERATOR
286 #endif
287 
288 #if TEST_STD_VER < 11
289 #define ASSERT_NOEXCEPT(...)
290 #define ASSERT_NOT_NOEXCEPT(...)
291 #else
292 #define ASSERT_NOEXCEPT(...) \
293     static_assert(noexcept(__VA_ARGS__), "Operation must be noexcept")
294 
295 #define ASSERT_NOT_NOEXCEPT(...) \
296     static_assert(!noexcept(__VA_ARGS__), "Operation must NOT be noexcept")
297 #endif
298 
299 /* Macros for testing libc++ specific behavior and extensions */
300 #if defined(_LIBCPP_VERSION)
301 #define LIBCPP_ASSERT(...) assert(__VA_ARGS__)
302 #define LIBCPP_STATIC_ASSERT(...) static_assert(__VA_ARGS__)
303 #define LIBCPP_ASSERT_NOEXCEPT(...) ASSERT_NOEXCEPT(__VA_ARGS__)
304 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ASSERT_NOT_NOEXCEPT(__VA_ARGS__)
305 #define LIBCPP_ONLY(...) __VA_ARGS__
306 #else
307 #define LIBCPP_ASSERT(...) ((void)0)
308 #define LIBCPP_STATIC_ASSERT(...) ((void)0)
309 #define LIBCPP_ASSERT_NOEXCEPT(...) ((void)0)
310 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ((void)0)
311 #define LIBCPP_ONLY(...) ((void)0)
312 #endif
313 
314 #define TEST_IGNORE_NODISCARD (void)
315 
316 namespace test_macros_detail {
317 template <class T, class U>
318 struct is_same { enum { value = 0};} ;
319 template <class T>
320 struct is_same<T, T> { enum {value = 1}; };
321 } // namespace test_macros_detail
322 
323 #define ASSERT_SAME_TYPE(...) \
324     static_assert((test_macros_detail::is_same<__VA_ARGS__>::value), \
325                  "Types differ unexpectedly")
326 
327 #ifndef TEST_HAS_NO_EXCEPTIONS
328 #define TEST_THROW(...) throw __VA_ARGS__
329 #else
330 #if defined(__GNUC__)
331 #define TEST_THROW(...) __builtin_abort()
332 #else
333 #include <stdlib.h>
334 #define TEST_THROW(...) ::abort()
335 #endif
336 #endif
337 
338 #if defined(__GNUC__) || defined(__clang__)
339 template <class Tp>
340 inline
341 void DoNotOptimize(Tp const& value) {
342     asm volatile("" : : "r,m"(value) : "memory");
343 }
344 
345 template <class Tp>
346 inline void DoNotOptimize(Tp& value) {
347 #if defined(__clang__)
348   asm volatile("" : "+r,m"(value) : : "memory");
349 #else
350   asm volatile("" : "+m,r"(value) : : "memory");
351 #endif
352 }
353 #else
354 #include <intrin.h>
355 template <class Tp>
356 inline void DoNotOptimize(Tp const& value) {
357   const volatile void* volatile unused = __builtin_addressof(value);
358   static_cast<void>(unused);
359   _ReadWriteBarrier();
360 }
361 #endif
362 
363 #if defined(__GNUC__)
364 #define TEST_ALWAYS_INLINE __attribute__((always_inline))
365 #define TEST_NOINLINE __attribute__((noinline))
366 #elif defined(_MSC_VER)
367 #define TEST_ALWAYS_INLINE __forceinline
368 #define TEST_NOINLINE __declspec(noinline)
369 #else
370 #define TEST_ALWAYS_INLINE
371 #define TEST_NOINLINE
372 #endif
373 
374 #if defined(__GNUC__)
375 #pragma GCC diagnostic pop
376 #endif
377 
378 #endif // SUPPORT_TEST_MACROS_HPP
379