1 // -*- C++ -*- 2 //===---------------------------- test_macros.h ---------------------------===// 3 // 4 // The LLVM Compiler Infrastructure 5 // 6 // This file is dual licensed under the MIT and the University of Illinois Open 7 // Source Licenses. See LICENSE.TXT for details. 8 // 9 //===----------------------------------------------------------------------===// 10 11 #ifndef SUPPORT_TEST_MACROS_HPP 12 #define SUPPORT_TEST_MACROS_HPP 13 14 #include <ciso646> // Get STL specific macros like _LIBCPP_VERSION 15 16 #if defined(__GNUC__) 17 #pragma GCC diagnostic push 18 #pragma GCC diagnostic ignored "-Wvariadic-macros" 19 #endif 20 21 #define TEST_CONCAT1(X, Y) X##Y 22 #define TEST_CONCAT(X, Y) TEST_CONCAT1(X, Y) 23 24 #ifdef __has_feature 25 #define TEST_HAS_FEATURE(X) __has_feature(X) 26 #else 27 #define TEST_HAS_FEATURE(X) 0 28 #endif 29 30 #ifdef __has_include 31 #define TEST_HAS_INCLUDE(X) __has_include(X) 32 #else 33 #define TEST_HAS_INCLUDE(X) 0 34 #endif 35 36 #ifdef __has_extension 37 #define TEST_HAS_EXTENSION(X) __has_extension(X) 38 #else 39 #define TEST_HAS_EXTENSION(X) 0 40 #endif 41 42 #ifdef __has_builtin 43 #define TEST_HAS_BUILTIN(X) __has_builtin(X) 44 #else 45 #define TEST_HAS_BUILTIN(X) 0 46 #endif 47 #ifdef __is_identifier 48 // '__is_identifier' returns '0' if '__x' is a reserved identifier provided by 49 // the compiler and '1' otherwise. 50 #define TEST_HAS_BUILTIN_IDENTIFIER(X) !__is_identifier(X) 51 #else 52 #define TEST_HAS_BUILTIN_IDENTIFIER(X) 0 53 #endif 54 55 #if defined(__EDG__) 56 # define TEST_COMPILER_EDG 57 #elif defined(__clang__) 58 # define TEST_COMPILER_CLANG 59 # if defined(__apple_build_version__) 60 # define TEST_COMPILER_APPLE_CLANG 61 # endif 62 #elif defined(_MSC_VER) 63 # define TEST_COMPILER_C1XX 64 #elif defined(__GNUC__) 65 # define TEST_COMPILER_GCC 66 #endif 67 68 #if defined(__apple_build_version__) 69 #define TEST_APPLE_CLANG_VER (__clang_major__ * 100) + __clang_minor__ 70 #elif defined(__clang_major__) 71 #define TEST_CLANG_VER (__clang_major__ * 100) + __clang_minor__ 72 #elif defined(__GNUC__) 73 #define TEST_GCC_VER (__GNUC__ * 100 + __GNUC_MINOR__) 74 #endif 75 76 /* Make a nice name for the standard version */ 77 #ifndef TEST_STD_VER 78 #if __cplusplus <= 199711L 79 # define TEST_STD_VER 3 80 #elif __cplusplus <= 201103L 81 # define TEST_STD_VER 11 82 #elif __cplusplus <= 201402L 83 # define TEST_STD_VER 14 84 #elif __cplusplus <= 201703L 85 # define TEST_STD_VER 17 86 #else 87 # define TEST_STD_VER 99 // greater than current standard 88 // This is deliberately different than _LIBCPP_STD_VER to discourage matching them up. 89 #endif 90 #endif 91 92 // Attempt to deduce GCC version 93 #if defined(_LIBCPP_VERSION) && TEST_HAS_INCLUDE(<features.h>) 94 #include <features.h> 95 #define TEST_HAS_GLIBC 96 #define TEST_GLIBC_PREREQ(major, minor) __GLIBC_PREREQ(major, minor) 97 #endif 98 99 /* Features that were introduced in C++14 */ 100 #if TEST_STD_VER >= 14 101 #define TEST_HAS_EXTENDED_CONSTEXPR 102 #define TEST_HAS_VARIABLE_TEMPLATES 103 #endif 104 105 /* Features that were introduced after C++14 */ 106 #if TEST_STD_VER > 14 107 #endif 108 109 #if TEST_STD_VER >= 11 110 #define TEST_ALIGNOF(...) alignof(__VA_ARGS__) 111 #define TEST_ALIGNAS(...) alignas(__VA_ARGS__) 112 #define TEST_CONSTEXPR constexpr 113 #define TEST_NOEXCEPT noexcept 114 #define TEST_NOEXCEPT_FALSE noexcept(false) 115 #define TEST_NOEXCEPT_COND(...) noexcept(__VA_ARGS__) 116 # if TEST_STD_VER >= 14 117 # define TEST_CONSTEXPR_CXX14 constexpr 118 # else 119 # define TEST_CONSTEXPR_CXX14 120 # endif 121 # if TEST_STD_VER > 14 122 # define TEST_THROW_SPEC(...) 123 # else 124 # define TEST_THROW_SPEC(...) throw(__VA_ARGS__) 125 # endif 126 #else 127 #define TEST_ALIGNOF(...) __alignof(__VA_ARGS__) 128 #define TEST_ALIGNAS(...) __attribute__((__aligned__(__VA_ARGS__))) 129 #define TEST_CONSTEXPR 130 #define TEST_CONSTEXPR_CXX14 131 #define TEST_NOEXCEPT throw() 132 #define TEST_NOEXCEPT_FALSE 133 #define TEST_NOEXCEPT_COND(...) 134 #define TEST_THROW_SPEC(...) throw(__VA_ARGS__) 135 #endif 136 137 #define TEST_ALIGNAS_TYPE(...) TEST_ALIGNAS(TEST_ALIGNOF(__VA_ARGS__)) 138 139 #if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cpp_rtti) \ 140 && !defined(__GXX_RTTI) 141 #define TEST_HAS_NO_RTTI 142 #endif 143 144 #if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \ 145 && !defined(__EXCEPTIONS) 146 #define TEST_HAS_NO_EXCEPTIONS 147 #endif 148 149 #if TEST_HAS_FEATURE(address_sanitizer) || TEST_HAS_FEATURE(memory_sanitizer) || \ 150 TEST_HAS_FEATURE(thread_sanitizer) 151 #define TEST_HAS_SANITIZERS 152 #endif 153 154 #if defined(_LIBCPP_NORETURN) 155 #define TEST_NORETURN _LIBCPP_NORETURN 156 #else 157 #define TEST_NORETURN [[noreturn]] 158 #endif 159 160 #if defined(_LIBCPP_SAFE_STATIC) 161 #define TEST_SAFE_STATIC _LIBCPP_SAFE_STATIC 162 #else 163 #define TEST_SAFE_STATIC 164 #endif 165 166 #if TEST_STD_VER < 11 167 #define ASSERT_NOEXCEPT(...) 168 #define ASSERT_NOT_NOEXCEPT(...) 169 #else 170 #define ASSERT_NOEXCEPT(...) \ 171 static_assert(noexcept(__VA_ARGS__), "Operation must be noexcept") 172 173 #define ASSERT_NOT_NOEXCEPT(...) \ 174 static_assert(!noexcept(__VA_ARGS__), "Operation must NOT be noexcept") 175 #endif 176 177 /* Macros for testing libc++ specific behavior and extensions */ 178 #if defined(_LIBCPP_VERSION) 179 #define LIBCPP_ASSERT(...) assert(__VA_ARGS__) 180 #define LIBCPP_STATIC_ASSERT(...) static_assert(__VA_ARGS__) 181 #define LIBCPP_ASSERT_NOEXCEPT(...) ASSERT_NOEXCEPT(__VA_ARGS__) 182 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ASSERT_NOT_NOEXCEPT(__VA_ARGS__) 183 #define LIBCPP_ONLY(...) __VA_ARGS__ 184 #else 185 #define LIBCPP_ASSERT(...) ((void)0) 186 #define LIBCPP_STATIC_ASSERT(...) ((void)0) 187 #define LIBCPP_ASSERT_NOEXCEPT(...) ((void)0) 188 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ((void)0) 189 #define LIBCPP_ONLY(...) ((void)0) 190 #endif 191 192 #define TEST_IGNORE_NODISCARD (void) 193 194 namespace test_macros_detail { 195 template <class T, class U> 196 struct is_same { enum { value = 0};} ; 197 template <class T> 198 struct is_same<T, T> { enum {value = 1}; }; 199 } // namespace test_macros_detail 200 201 #define ASSERT_SAME_TYPE(...) \ 202 static_assert((test_macros_detail::is_same<__VA_ARGS__>::value), \ 203 "Types differ unexpectedly") 204 205 #ifndef TEST_HAS_NO_EXCEPTIONS 206 #define TEST_THROW(...) throw __VA_ARGS__ 207 #else 208 #if defined(__GNUC__) 209 #define TEST_THROW(...) __builtin_abort() 210 #else 211 #include <stdlib.h> 212 #define TEST_THROW(...) ::abort() 213 #endif 214 #endif 215 216 #if defined(__GNUC__) || defined(__clang__) 217 template <class Tp> 218 inline void DoNotOptimize(Tp const& value) { 219 asm volatile("" : : "g"(value) : "memory"); 220 } 221 #else 222 #include <intrin.h> 223 template <class Tp> 224 inline void DoNotOptimize(Tp const& value) { 225 const volatile void* volatile unused = __builtin_addressof(value); 226 static_cast<void>(unused); 227 _ReadWriteBarrier(); 228 } 229 #endif 230 231 #if defined(__GNUC__) 232 #pragma GCC diagnostic pop 233 #endif 234 235 #endif // SUPPORT_TEST_MACROS_HPP 236