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 #ifdef __CHERI_PURE_CAPABILITY__
32 #define TEST_CHERI_NO_SUBOBJECT_WARNING _Pragma("clang diagnostic ignored \"-Wcheri-subobject-bounds-suspicious\"")
33 #else
34 #define TEST_CHERI_NO_SUBOBJECT_WARNING
35 #endif
36 
37 #define TEST_STRINGIZE_IMPL(x) #x
38 #define TEST_STRINGIZE(x) TEST_STRINGIZE_IMPL(x)
39 
40 #define TEST_CONCAT1(X, Y) X##Y
41 #define TEST_CONCAT(X, Y) TEST_CONCAT1(X, Y)
42 
43 #ifdef __has_feature
44 #define TEST_HAS_FEATURE(X) __has_feature(X)
45 #else
46 #define TEST_HAS_FEATURE(X) 0
47 #endif
48 
49 #ifndef __has_include
50 #define __has_include(...) 0
51 #endif
52 
53 #ifdef __has_extension
54 #define TEST_HAS_EXTENSION(X) __has_extension(X)
55 #else
56 #define TEST_HAS_EXTENSION(X) 0
57 #endif
58 
59 #ifdef __has_warning
60 #define TEST_HAS_WARNING(X) __has_warning(X)
61 #else
62 #define TEST_HAS_WARNING(X) 0
63 #endif
64 
65 #ifdef __has_builtin
66 #define TEST_HAS_BUILTIN(X) __has_builtin(X)
67 #else
68 #define TEST_HAS_BUILTIN(X) 0
69 #endif
70 #ifdef __is_identifier
71 // '__is_identifier' returns '0' if '__x' is a reserved identifier provided by
72 // the compiler and '1' otherwise.
73 #define TEST_HAS_BUILTIN_IDENTIFIER(X) !__is_identifier(X)
74 #else
75 #define TEST_HAS_BUILTIN_IDENTIFIER(X) 0
76 #endif
77 
78 #if defined(__EDG__)
79 # define TEST_COMPILER_EDG
80 #elif defined(__clang__)
81 # define TEST_COMPILER_CLANG
82 # if defined(__apple_build_version__)
83 #  define TEST_COMPILER_APPLE_CLANG
84 # endif
85 #elif defined(_MSC_VER)
86 # define TEST_COMPILER_C1XX
87 #elif defined(__GNUC__)
88 # define TEST_COMPILER_GCC
89 #endif
90 
91 #if defined(__apple_build_version__)
92 #define TEST_APPLE_CLANG_VER (__clang_major__ * 100) + __clang_minor__
93 #elif defined(__clang_major__)
94 #define TEST_CLANG_VER (__clang_major__ * 100) + __clang_minor__
95 #elif defined(__GNUC__)
96 #define TEST_GCC_VER (__GNUC__ * 100 + __GNUC_MINOR__)
97 #define TEST_GCC_VER_NEW (TEST_GCC_VER * 10 + __GNUC_PATCHLEVEL__)
98 #endif
99 
100 /* Make a nice name for the standard version */
101 #ifndef TEST_STD_VER
102 #if  __cplusplus <= 199711L
103 # define TEST_STD_VER 3
104 #elif __cplusplus <= 201103L
105 # define TEST_STD_VER 11
106 #elif __cplusplus <= 201402L
107 # define TEST_STD_VER 14
108 #elif __cplusplus <= 201703L
109 # define TEST_STD_VER 17
110 #else
111 # define TEST_STD_VER 99    // greater than current standard
112 // This is deliberately different than _LIBCPP_STD_VER to discourage matching them up.
113 #endif
114 #endif
115 
116 // Attempt to deduce the GLIBC version
117 #if (defined(__has_include) && __has_include(<features.h>)) || \
118     defined(__linux__)
119 #include <features.h>
120 #if defined(__GLIBC_PREREQ)
121 #define TEST_HAS_GLIBC
122 #define TEST_GLIBC_PREREQ(major, minor) __GLIBC_PREREQ(major, minor)
123 #endif
124 #endif
125 
126 #if TEST_STD_VER >= 11
127 # define TEST_ALIGNOF(...) alignof(__VA_ARGS__)
128 # define TEST_ALIGNAS(...) alignas(__VA_ARGS__)
129 # define TEST_CONSTEXPR constexpr
130 # define TEST_NOEXCEPT noexcept
131 # define TEST_NOEXCEPT_FALSE noexcept(false)
132 # define TEST_NOEXCEPT_COND(...) noexcept(__VA_ARGS__)
133 #else
134 #   if defined(TEST_COMPILER_CLANG)
135 #    define TEST_ALIGNOF(...) _Alignof(__VA_ARGS__)
136 #   else
137 #    define TEST_ALIGNOF(...) __alignof(__VA_ARGS__)
138 #   endif
139 # define TEST_ALIGNAS(...) __attribute__((__aligned__(__VA_ARGS__)))
140 # define TEST_CONSTEXPR
141 # define TEST_NOEXCEPT throw()
142 # define TEST_NOEXCEPT_FALSE
143 # define TEST_NOEXCEPT_COND(...)
144 #endif
145 
146 #if TEST_STD_VER >= 17
147 # define TEST_THROW_SPEC(...)
148 #else
149 # define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
150 #endif
151 
152 #if TEST_STD_VER >= 14
153 # define TEST_CONSTEXPR_CXX14 constexpr
154 #else
155 # define TEST_CONSTEXPR_CXX14
156 #endif
157 
158 #if TEST_STD_VER >= 17
159 # define TEST_CONSTEXPR_CXX17 constexpr
160 #else
161 # define TEST_CONSTEXPR_CXX17
162 #endif
163 
164 #if TEST_STD_VER >= 20
165 # define TEST_CONSTEXPR_CXX20 constexpr
166 #else
167 # define TEST_CONSTEXPR_CXX20
168 #endif
169 
170 // Sniff out to see if the underlying C library has C11 features
171 // Note that at this time (July 2018), MacOS X and iOS do NOT.
172 // This is cribbed from __config; but lives here as well because we can't assume libc++
173 #if __ISO_C_VISIBLE >= 2011 || TEST_STD_VER >= 11
174 #  if defined(__FreeBSD__)
175 //  Specifically, FreeBSD does NOT have timespec_get, even though they have all
176 //  the rest of C11 - this is PR#38495
177 #    define TEST_HAS_ALIGNED_ALLOC
178 #    define TEST_HAS_C11_FEATURES
179 #  elif defined(__BIONIC__)
180 #    define TEST_HAS_C11_FEATURES
181 #    if __ANDROID_API__ >= 28
182 #      define TEST_HAS_ALIGNED_ALLOC
183 #    endif
184 #    if __ANDROID_API__ >= 29
185 #      define TEST_HAS_TIMESPEC_GET
186 #    endif
187 #  elif defined(__Fuchsia__) || defined(__wasi__) || defined(__NetBSD__)
188 #    define TEST_HAS_ALIGNED_ALLOC
189 #    define TEST_HAS_C11_FEATURES
190 #    define TEST_HAS_TIMESPEC_GET
191 #  elif defined(__linux__)
192 // This block preserves the old behavior used by include/__config:
193 // _LIBCPP_GLIBC_PREREQ would be defined to 0 if __GLIBC_PREREQ was not
194 // available. The configuration here may be too vague though, as Bionic, uClibc,
195 // newlib, etc may all support these features but need to be configured.
196 #    if defined(TEST_GLIBC_PREREQ)
197 #      if TEST_GLIBC_PREREQ(2, 17)
198 #        define TEST_HAS_ALIGNED_ALLOC
199 #        define TEST_HAS_TIMESPEC_GET
200 #        define TEST_HAS_C11_FEATURES
201 #      endif
202 #    elif defined(_LIBCPP_HAS_MUSL_LIBC)
203 #      define TEST_HAS_ALIGNED_ALLOC
204 #      define TEST_HAS_C11_FEATURES
205 #      define TEST_HAS_TIMESPEC_GET
206 #    endif
207 #  elif defined(_WIN32)
208 #    if defined(_MSC_VER) && !defined(__MINGW32__)
209 #      define TEST_HAS_ALIGNED_ALLOC
210 #      define TEST_HAS_C11_FEATURES // Using Microsoft's C Runtime library
211 #      define TEST_HAS_TIMESPEC_GET
212 #    endif
213 #  endif
214 #endif
215 
216 /* Features that were introduced in C++14 */
217 #if TEST_STD_VER >= 14
218 #define TEST_HAS_EXTENDED_CONSTEXPR
219 #define TEST_HAS_VARIABLE_TEMPLATES
220 #endif
221 
222 /* Features that were introduced in C++17 */
223 #if TEST_STD_VER >= 17
224 #endif
225 
226 /* Features that were introduced after C++17 */
227 #if TEST_STD_VER > 17
228 #endif
229 
230 
231 #define TEST_ALIGNAS_TYPE(...) TEST_ALIGNAS(TEST_ALIGNOF(__VA_ARGS__))
232 
233 #if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cpp_rtti) \
234     && !defined(__GXX_RTTI)
235 #define TEST_HAS_NO_RTTI
236 #endif
237 
238 #if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \
239      && !defined(__EXCEPTIONS)
240 #define TEST_HAS_NO_EXCEPTIONS
241 #endif
242 
243 #if TEST_HAS_FEATURE(address_sanitizer) || TEST_HAS_FEATURE(memory_sanitizer) || \
244     TEST_HAS_FEATURE(thread_sanitizer)
245 #define TEST_HAS_SANITIZERS
246 #endif
247 
248 // Use a function macro to generate an error if test_macros.h was not included
249 #if defined(_LIBCPP_SLOW_TEST_HOST)
250 #define TEST_SLOW_HOST() 1
251 #else
252 #define TEST_SLOW_HOST() 0
253 #endif
254 
255 #if defined(_LIBCPP_NORETURN)
256 #define TEST_NORETURN _LIBCPP_NORETURN
257 #else
258 #define TEST_NORETURN [[noreturn]]
259 #endif
260 
261 #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || \
262   (!(TEST_STD_VER > 14 || \
263     (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606L)))
264 #define TEST_HAS_NO_ALIGNED_ALLOCATION
265 #endif
266 
267 #if defined(_LIBCPP_SAFE_STATIC)
268 #define TEST_SAFE_STATIC _LIBCPP_SAFE_STATIC
269 #else
270 #define TEST_SAFE_STATIC
271 #endif
272 
273 #if !defined(__cpp_impl_three_way_comparison) \
274     && (!defined(_MSC_VER) || defined(__clang__) || _MSC_VER < 1920 || _MSVC_LANG <= 201703L)
275 #define TEST_HAS_NO_SPACESHIP_OPERATOR
276 #endif
277 
278 #if TEST_STD_VER < 11
279 #define ASSERT_NOEXCEPT(...)
280 #define ASSERT_NOT_NOEXCEPT(...)
281 #else
282 #define ASSERT_NOEXCEPT(...) \
283     static_assert(noexcept(__VA_ARGS__), "Operation must be noexcept")
284 
285 #define ASSERT_NOT_NOEXCEPT(...) \
286     static_assert(!noexcept(__VA_ARGS__), "Operation must NOT be noexcept")
287 #endif
288 
289 /* Macros for testing libc++ specific behavior and extensions */
290 #if defined(_LIBCPP_VERSION)
291 #define LIBCPP_ASSERT(...) assert(__VA_ARGS__)
292 #define LIBCPP_STATIC_ASSERT(...) static_assert(__VA_ARGS__)
293 #define LIBCPP_ASSERT_NOEXCEPT(...) ASSERT_NOEXCEPT(__VA_ARGS__)
294 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ASSERT_NOT_NOEXCEPT(__VA_ARGS__)
295 #define LIBCPP_ONLY(...) __VA_ARGS__
296 #else
297 #define LIBCPP_ASSERT(...) ((void)0)
298 #define LIBCPP_STATIC_ASSERT(...) ((void)0)
299 #define LIBCPP_ASSERT_NOEXCEPT(...) ((void)0)
300 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ((void)0)
301 #define LIBCPP_ONLY(...) ((void)0)
302 #endif
303 
304 #define TEST_IGNORE_NODISCARD (void)
305 
306 namespace test_macros_detail {
307 template <class T, class U>
308 struct is_same { enum { value = 0};} ;
309 template <class T>
310 struct is_same<T, T> { enum {value = 1}; };
311 } // namespace test_macros_detail
312 
313 #define ASSERT_SAME_TYPE(...) \
314     static_assert((test_macros_detail::is_same<__VA_ARGS__>::value), \
315                  "Types differ unexpectedly")
316 
317 #ifndef TEST_HAS_NO_EXCEPTIONS
318 #define TEST_THROW(...) throw __VA_ARGS__
319 #else
320 #if defined(__GNUC__)
321 #define TEST_THROW(...) __builtin_abort()
322 #else
323 #include <stdlib.h>
324 #define TEST_THROW(...) ::abort()
325 #endif
326 #endif
327 
328 #if defined(__GNUC__) || defined(__clang__)
329 template <class Tp>
330 inline
331 void DoNotOptimize(Tp const& value) {
332     asm volatile("" : : "r,m"(value) : "memory");
333 }
334 
335 template <class Tp>
336 inline void DoNotOptimize(Tp& value) {
337 #if defined(__clang__)
338   asm volatile("" : "+r,m"(value) : : "memory");
339 #else
340   asm volatile("" : "+m,r"(value) : : "memory");
341 #endif
342 }
343 #else
344 #include <intrin.h>
345 template <class Tp>
346 inline void DoNotOptimize(Tp const& value) {
347   const volatile void* volatile unused = __builtin_addressof(value);
348   static_cast<void>(unused);
349   _ReadWriteBarrier();
350 }
351 #endif
352 
353 #if defined(__GNUC__)
354 #define TEST_ALWAYS_INLINE __attribute__((always_inline))
355 #define TEST_NOINLINE __attribute__((noinline))
356 #elif defined(_MSC_VER)
357 #define TEST_ALWAYS_INLINE __forceinline
358 #define TEST_NOINLINE __declspec(noinline)
359 #else
360 #define TEST_ALWAYS_INLINE
361 #define TEST_NOINLINE
362 #endif
363 
364 #if defined(__GNUC__)
365 #pragma GCC diagnostic pop
366 #endif
367 
368 #endif // SUPPORT_TEST_MACROS_HPP
369