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