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