2 //===---------------------------- test_macros.h ---------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef SUPPORT_TEST_MACROS_HPP
12 #define SUPPORT_TEST_MACROS_HPP
14 #include <ciso646> // Get STL specific macros like _LIBCPP_VERSION
17 #pragma GCC diagnostic push
18 #pragma GCC diagnostic ignored "-Wvariadic-macros"
21 #define TEST_CONCAT1(X, Y) X##Y
22 #define TEST_CONCAT(X, Y) TEST_CONCAT1(X, Y)
25 #define TEST_HAS_FEATURE(X) __has_feature(X)
27 #define TEST_HAS_FEATURE(X) 0
31 #define __has_include(...) 0
34 #ifdef __has_extension
35 #define TEST_HAS_EXTENSION(X) __has_extension(X)
37 #define TEST_HAS_EXTENSION(X) 0
41 #define TEST_HAS_BUILTIN(X) __has_builtin(X)
43 #define TEST_HAS_BUILTIN(X) 0
45 #ifdef __is_identifier
46 // '__is_identifier' returns '0' if '__x' is a reserved identifier provided by
47 // the compiler and '1' otherwise.
48 #define TEST_HAS_BUILTIN_IDENTIFIER(X) !__is_identifier(X)
50 #define TEST_HAS_BUILTIN_IDENTIFIER(X) 0
54 # define TEST_COMPILER_EDG
55 #elif defined(__clang__)
56 # define TEST_COMPILER_CLANG
57 # if defined(__apple_build_version__)
58 # define TEST_COMPILER_APPLE_CLANG
60 #elif defined(_MSC_VER)
61 # define TEST_COMPILER_C1XX
62 #elif defined(__GNUC__)
63 # define TEST_COMPILER_GCC
66 #if defined(__apple_build_version__)
67 #define TEST_APPLE_CLANG_VER (__clang_major__ * 100) + __clang_minor__
68 #elif defined(__clang_major__)
69 #define TEST_CLANG_VER (__clang_major__ * 100) + __clang_minor__
70 #elif defined(__GNUC__)
71 #define TEST_GCC_VER (__GNUC__ * 100 + __GNUC_MINOR__)
74 /* Make a nice name for the standard version */
76 #if __cplusplus <= 199711L
77 # define TEST_STD_VER 3
78 #elif __cplusplus <= 201103L
79 # define TEST_STD_VER 11
80 #elif __cplusplus <= 201402L
81 # define TEST_STD_VER 14
82 #elif __cplusplus <= 201703L
83 # define TEST_STD_VER 17
85 # define TEST_STD_VER 99 // greater than current standard
86 // This is deliberately different than _LIBCPP_STD_VER to discourage matching them up.
90 // Attempt to deduce GCC version
91 #if defined(_LIBCPP_VERSION) && __has_include(<features.h>)
93 #define TEST_HAS_GLIBC
94 #define TEST_GLIBC_PREREQ(major, minor) __GLIBC_PREREQ(major, minor)
97 #if TEST_STD_VER >= 11
98 #define TEST_ALIGNOF(...) alignof(__VA_ARGS__)
99 #define TEST_ALIGNAS(...) alignas(__VA_ARGS__)
100 #define TEST_CONSTEXPR constexpr
101 #define TEST_NOEXCEPT noexcept
102 #define TEST_NOEXCEPT_FALSE noexcept(false)
103 #define TEST_NOEXCEPT_COND(...) noexcept(__VA_ARGS__)
104 # if TEST_STD_VER >= 14
105 # define TEST_CONSTEXPR_CXX14 constexpr
107 # define TEST_CONSTEXPR_CXX14
109 # if TEST_STD_VER > 14
110 # define TEST_THROW_SPEC(...)
112 # define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
115 #define TEST_ALIGNOF(...) __alignof(__VA_ARGS__)
116 #define TEST_ALIGNAS(...) __attribute__((__aligned__(__VA_ARGS__)))
117 #define TEST_CONSTEXPR
118 #define TEST_CONSTEXPR_CXX14
119 #define TEST_NOEXCEPT throw()
120 #define TEST_NOEXCEPT_FALSE
121 #define TEST_NOEXCEPT_COND(...)
122 #define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
125 // Sniff out to see if the underling C library has C11 features
126 // Note that at this time (July 2018), MacOS X and iOS do NOT.
127 #if __ISO_C_VISIBLE >= 2011
128 # if defined(__FreeBSD__)
129 # define TEST_HAS_C11_FEATURES
130 # elif defined(__Fuchsia__)
131 # define TEST_HAS_C11_FEATURES
132 # elif defined(__linux__)
133 # if !defined(_LIBCPP_HAS_MUSL_LIBC)
134 # if _LIBCPP_GLIBC_PREREQ(2, 17)
135 # define TEST_HAS_C11_FEATURES
137 # else // defined(_LIBCPP_HAS_MUSL_LIBC)
138 # define TEST_HAS_C11_FEATURES
140 # elif defined(_WIN32)
141 # if defined(_MSC_VER) && !defined(__MINGW32__)
142 # define TEST_HAS_C11_FEATURES // Using Microsoft's C Runtime library
147 /* Features that were introduced in C++14 */
148 #if TEST_STD_VER >= 14
149 #define TEST_HAS_EXTENDED_CONSTEXPR
150 #define TEST_HAS_VARIABLE_TEMPLATES
153 /* Features that were introduced in C++17 */
154 #if TEST_STD_VER >= 17
157 /* Features that were introduced after C++17 */
158 #if TEST_STD_VER > 17
162 #define TEST_ALIGNAS_TYPE(...) TEST_ALIGNAS(TEST_ALIGNOF(__VA_ARGS__))
164 #if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cpp_rtti) \
165 && !defined(__GXX_RTTI)
166 #define TEST_HAS_NO_RTTI
169 #if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \
170 && !defined(__EXCEPTIONS)
171 #define TEST_HAS_NO_EXCEPTIONS
174 #if TEST_HAS_FEATURE(address_sanitizer) || TEST_HAS_FEATURE(memory_sanitizer) || \
175 TEST_HAS_FEATURE(thread_sanitizer)
176 #define TEST_HAS_SANITIZERS
179 #if defined(_LIBCPP_NORETURN)
180 #define TEST_NORETURN _LIBCPP_NORETURN
182 #define TEST_NORETURN [[noreturn]]
185 #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || \
186 (!(TEST_STD_VER > 14 || \
187 (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606L)))
188 #define TEST_HAS_NO_ALIGNED_ALLOCATION
191 #if defined(_LIBCPP_SAFE_STATIC)
192 #define TEST_SAFE_STATIC _LIBCPP_SAFE_STATIC
194 #define TEST_SAFE_STATIC
197 // FIXME: Fix this feature check when either (A) a compiler provides a complete
198 // implementation, or (b) a feature check macro is specified
199 #define TEST_HAS_NO_SPACESHIP_OPERATOR
202 #if TEST_STD_VER < 11
203 #define ASSERT_NOEXCEPT(...)
204 #define ASSERT_NOT_NOEXCEPT(...)
206 #define ASSERT_NOEXCEPT(...) \
207 static_assert(noexcept(__VA_ARGS__), "Operation must be noexcept")
209 #define ASSERT_NOT_NOEXCEPT(...) \
210 static_assert(!noexcept(__VA_ARGS__), "Operation must NOT be noexcept")
213 /* Macros for testing libc++ specific behavior and extensions */
214 #if defined(_LIBCPP_VERSION)
215 #define LIBCPP_ASSERT(...) assert(__VA_ARGS__)
216 #define LIBCPP_STATIC_ASSERT(...) static_assert(__VA_ARGS__)
217 #define LIBCPP_ASSERT_NOEXCEPT(...) ASSERT_NOEXCEPT(__VA_ARGS__)
218 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ASSERT_NOT_NOEXCEPT(__VA_ARGS__)
219 #define LIBCPP_ONLY(...) __VA_ARGS__
221 #define LIBCPP_ASSERT(...) ((void)0)
222 #define LIBCPP_STATIC_ASSERT(...) ((void)0)
223 #define LIBCPP_ASSERT_NOEXCEPT(...) ((void)0)
224 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ((void)0)
225 #define LIBCPP_ONLY(...) ((void)0)
228 #define TEST_IGNORE_NODISCARD (void)
230 namespace test_macros_detail {
231 template <class T, class U>
232 struct is_same { enum { value = 0};} ;
234 struct is_same<T, T> { enum {value = 1}; };
235 } // namespace test_macros_detail
237 #define ASSERT_SAME_TYPE(...) \
238 static_assert((test_macros_detail::is_same<__VA_ARGS__>::value), \
239 "Types differ unexpectedly")
241 #ifndef TEST_HAS_NO_EXCEPTIONS
242 #define TEST_THROW(...) throw __VA_ARGS__
244 #if defined(__GNUC__)
245 #define TEST_THROW(...) __builtin_abort()
248 #define TEST_THROW(...) ::abort()
252 #if defined(__GNUC__) || defined(__clang__)
255 void DoNotOptimize(Tp const& value) {
256 asm volatile("" : : "r,m"(value) : "memory");
260 inline void DoNotOptimize(Tp& value) {
261 #if defined(__clang__)
262 asm volatile("" : "+r,m"(value) : : "memory");
264 asm volatile("" : "+m,r"(value) : : "memory");
270 inline void DoNotOptimize(Tp const& value) {
271 const volatile void* volatile unused = __builtin_addressof(value);
272 static_cast<void>(unused);
278 #if defined(__GNUC__)
279 #pragma GCC diagnostic pop
282 #endif // SUPPORT_TEST_MACROS_HPP