Eric Fiselier | 6871bcb | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 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 | |
Eric Fiselier | 1f4231f | 2016-04-28 22:28:23 +0000 | [diff] [blame] | 14 | #include <ciso646> // Get STL specific macros like _LIBCPP_VERSION |
| 15 | |
Eric Fiselier | 1e33f12 | 2017-01-14 04:27:58 +0000 | [diff] [blame] | 16 | #if defined(__GNUC__) |
| 17 | #pragma GCC diagnostic push |
| 18 | #pragma GCC diagnostic ignored "-Wvariadic-macros" |
| 19 | #endif |
| 20 | |
Eric Fiselier | 6871bcb | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 21 | #define TEST_CONCAT1(X, Y) X##Y |
| 22 | #define TEST_CONCAT(X, Y) TEST_CONCAT1(X, Y) |
| 23 | |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 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 | |
Eric Fiselier | 015839a | 2016-10-04 21:25:51 +0000 | [diff] [blame] | 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 | |
Eric Fiselier | 6871bcb | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 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 | |
Eric Fiselier | fff5ec0 | 2015-12-09 22:03:06 +0000 | [diff] [blame] | 42 | #ifdef __has_builtin |
| 43 | #define TEST_HAS_BUILTIN(X) __has_builtin(X) |
| 44 | #else |
| 45 | #define TEST_HAS_BUILTIN(X) 0 |
| 46 | #endif |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 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 |
Eric Fiselier | fff5ec0 | 2015-12-09 22:03:06 +0000 | [diff] [blame] | 54 | |
Eric Fiselier | 7f1b098 | 2016-07-20 22:53:21 +0000 | [diff] [blame] | 55 | #if defined(__apple_build_version__) |
| 56 | #define TEST_APPLE_CLANG_VER (__clang_major__ * 100) + __clang_minor__ |
| 57 | #elif defined(__clang_major__) |
| 58 | #define TEST_CLANG_VER (__clang_major__ * 100) + __clang_minor__ |
| 59 | #elif defined(__GNUC__) |
| 60 | #define TEST_GCC_VER (__GNUC__ * 100 + __GNUC_MINOR__) |
| 61 | #endif |
| 62 | |
Eric Fiselier | 6871bcb | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 63 | /* Make a nice name for the standard version */ |
Eric Fiselier | f2f2a63 | 2016-06-14 21:31:42 +0000 | [diff] [blame] | 64 | #ifndef TEST_STD_VER |
Eric Fiselier | 6871bcb | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 65 | #if __cplusplus <= 199711L |
| 66 | # define TEST_STD_VER 3 |
| 67 | #elif __cplusplus <= 201103L |
| 68 | # define TEST_STD_VER 11 |
| 69 | #elif __cplusplus <= 201402L |
| 70 | # define TEST_STD_VER 14 |
| 71 | #else |
Eric Fiselier | 68436a9 | 2016-06-27 01:02:43 +0000 | [diff] [blame] | 72 | # define TEST_STD_VER 16 // current year; greater than current standard |
Eric Fiselier | 6871bcb | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 73 | #endif |
Eric Fiselier | f2f2a63 | 2016-06-14 21:31:42 +0000 | [diff] [blame] | 74 | #endif |
Eric Fiselier | 6871bcb | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 75 | |
Eric Fiselier | f49fe8f | 2016-09-04 00:48:54 +0000 | [diff] [blame] | 76 | // Attempt to deduce GCC version |
Eric Fiselier | 015839a | 2016-10-04 21:25:51 +0000 | [diff] [blame] | 77 | #if defined(_LIBCPP_VERSION) && TEST_HAS_INCLUDE(<features.h>) |
Eric Fiselier | f49fe8f | 2016-09-04 00:48:54 +0000 | [diff] [blame] | 78 | #include <features.h> |
| 79 | #define TEST_HAS_GLIBC |
| 80 | #define TEST_GLIBC_PREREQ(major, minor) __GLIBC_PREREQ(major, minor) |
| 81 | #endif |
| 82 | |
Eric Fiselier | 6871bcb | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 83 | /* Features that were introduced in C++14 */ |
| 84 | #if TEST_STD_VER >= 14 |
| 85 | #define TEST_HAS_EXTENDED_CONSTEXPR |
| 86 | #define TEST_HAS_VARIABLE_TEMPLATES |
| 87 | #endif |
| 88 | |
| 89 | /* Features that were introduced after C++14 */ |
| 90 | #if TEST_STD_VER > 14 |
| 91 | #endif |
| 92 | |
Eric Fiselier | 6871bcb | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 93 | #if TEST_STD_VER >= 11 |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 94 | #define TEST_ALIGNOF(...) alignof(__VA_ARGS__) |
| 95 | #define TEST_ALIGNAS(...) alignas(__VA_ARGS__) |
Eric Fiselier | 749adeb | 2015-08-19 17:21:46 +0000 | [diff] [blame] | 96 | #define TEST_CONSTEXPR constexpr |
Eric Fiselier | 6871bcb | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 97 | #define TEST_NOEXCEPT noexcept |
Eric Fiselier | 883cc25d | 2016-10-12 09:31:26 +0000 | [diff] [blame] | 98 | #define TEST_NOEXCEPT_COND(...) noexcept(__VA_ARGS__) |
Eric Fiselier | b530a25 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 99 | # if TEST_STD_VER >= 14 |
| 100 | # define TEST_CONSTEXPR_CXX14 constexpr |
| 101 | # else |
| 102 | # define TEST_CONSTEXPR_CXX14 |
| 103 | # endif |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 104 | # if TEST_STD_VER > 14 |
| 105 | # define TEST_THROW_SPEC(...) |
| 106 | # else |
| 107 | # define TEST_THROW_SPEC(...) throw(__VA_ARGS__) |
| 108 | # endif |
Eric Fiselier | 6871bcb | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 109 | #else |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 110 | #define TEST_ALIGNOF(...) __alignof(__VA_ARGS__) |
| 111 | #define TEST_ALIGNAS(...) __attribute__((__aligned__(__VA_ARGS__))) |
Eric Fiselier | 749adeb | 2015-08-19 17:21:46 +0000 | [diff] [blame] | 112 | #define TEST_CONSTEXPR |
Eric Fiselier | b530a25 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 113 | #define TEST_CONSTEXPR_CXX14 |
Eric Fiselier | d2c71a9 | 2016-10-12 11:20:27 +0000 | [diff] [blame] | 114 | #define TEST_NOEXCEPT throw() |
Eric Fiselier | 883cc25d | 2016-10-12 09:31:26 +0000 | [diff] [blame] | 115 | #define TEST_NOEXCEPT_COND(...) |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 116 | #define TEST_THROW_SPEC(...) throw(__VA_ARGS__) |
Eric Fiselier | 6871bcb | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 117 | #endif |
| 118 | |
Eric Fiselier | 6ce45e0 | 2016-10-12 10:19:48 +0000 | [diff] [blame] | 119 | #define TEST_ALIGNAS_TYPE(...) TEST_ALIGNAS(TEST_ALIGNOF(__VA_ARGS__)) |
| 120 | |
Eric Fiselier | 19d4483 | 2016-06-22 05:40:17 +0000 | [diff] [blame] | 121 | #if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cpp_rtti) \ |
Eric Fiselier | 94713d1 | 2016-06-22 05:33:52 +0000 | [diff] [blame] | 122 | && !defined(__GXX_RTTI) |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 123 | #define TEST_HAS_NO_RTTI |
| 124 | #endif |
| 125 | |
Eric Fiselier | 19d4483 | 2016-06-22 05:40:17 +0000 | [diff] [blame] | 126 | #if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \ |
Eric Fiselier | 94713d1 | 2016-06-22 05:33:52 +0000 | [diff] [blame] | 127 | && !defined(__EXCEPTIONS) |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 128 | #define TEST_HAS_NO_EXCEPTIONS |
| 129 | #endif |
| 130 | |
Eric Fiselier | 7362982 | 2015-10-01 08:34:37 +0000 | [diff] [blame] | 131 | #if TEST_HAS_FEATURE(address_sanitizer) || TEST_HAS_FEATURE(memory_sanitizer) || \ |
| 132 | TEST_HAS_FEATURE(thread_sanitizer) |
| 133 | #define TEST_HAS_SANITIZERS |
| 134 | #endif |
| 135 | |
Eric Fiselier | 3740071 | 2016-06-26 19:42:59 +0000 | [diff] [blame] | 136 | #if defined(_LIBCPP_NORETURN) |
| 137 | #define TEST_NORETURN _LIBCPP_NORETURN |
| 138 | #else |
| 139 | #define TEST_NORETURN [[noreturn]] |
| 140 | #endif |
| 141 | |
Eric Fiselier | c797958 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 142 | #define ASSERT_NOEXCEPT(...) \ |
| 143 | static_assert(noexcept(__VA_ARGS__), "Operation must be noexcept") |
| 144 | |
| 145 | #define ASSERT_NOT_NOEXCEPT(...) \ |
| 146 | static_assert(!noexcept(__VA_ARGS__), "Operation must NOT be noexcept") |
| 147 | |
Stephan T. Lavavej | 8a597d6 | 2016-12-09 19:53:08 +0000 | [diff] [blame] | 148 | /* Macros for testing libc++ specific behavior and extensions */ |
| 149 | #if defined(_LIBCPP_VERSION) |
| 150 | #define LIBCPP_ASSERT(...) assert(__VA_ARGS__) |
| 151 | #define LIBCPP_STATIC_ASSERT(...) static_assert(__VA_ARGS__) |
| 152 | #define LIBCPP_ASSERT_NOEXCEPT(...) ASSERT_NOEXCEPT(__VA_ARGS__) |
| 153 | #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ASSERT_NOT_NOEXCEPT(__VA_ARGS__) |
| 154 | #define LIBCPP_ONLY(...) __VA_ARGS__ |
| 155 | #else |
| 156 | #define LIBCPP_ASSERT(...) ((void)0) |
| 157 | #define LIBCPP_STATIC_ASSERT(...) ((void)0) |
| 158 | #define LIBCPP_ASSERT_NOEXCEPT(...) ((void)0) |
| 159 | #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ((void)0) |
| 160 | #define LIBCPP_ONLY(...) ((void)0) |
| 161 | #endif |
| 162 | |
Eric Fiselier | c797958 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 163 | namespace test_macros_detail { |
| 164 | template <class T, class U> |
| 165 | struct is_same { enum { value = 0};} ; |
| 166 | template <class T> |
| 167 | struct is_same<T, T> { enum {value = 1}; }; |
| 168 | } // namespace test_macros_detail |
| 169 | |
| 170 | #define ASSERT_SAME_TYPE(...) \ |
| 171 | static_assert(test_macros_detail::is_same<__VA_ARGS__>::value, \ |
| 172 | "Types differ uexpectedly") |
| 173 | |
Eric Fiselier | f188910 | 2016-10-01 10:34:13 +0000 | [diff] [blame] | 174 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 175 | #define TEST_THROW(...) throw __VA_ARGS__ |
| 176 | #else |
| 177 | #if defined(__GNUC__) |
| 178 | #define TEST_THROW(...) __builtin_abort() |
| 179 | #else |
| 180 | #include <stdlib.h> |
| 181 | #define TEST_THROW(...) ::abort() |
| 182 | #endif |
| 183 | #endif |
| 184 | |
Eric Fiselier | 4cdd915 | 2017-02-08 00:10:10 +0000 | [diff] [blame^] | 185 | #if defined(__GNUC__) || defined(__clang__) |
| 186 | template <class Tp> |
| 187 | inline void DoNotOptimize(Tp const& value) { |
| 188 | asm volatile("" : : "g"(value) : "memory"); |
| 189 | } |
| 190 | #else |
| 191 | template <class Tp> |
| 192 | inline void DoNotOptimize(Tp const&) { |
| 193 | // FIXME: Do something here... |
| 194 | } |
| 195 | #endif |
| 196 | |
Eric Fiselier | 1e33f12 | 2017-01-14 04:27:58 +0000 | [diff] [blame] | 197 | #if defined(__GNUC__) |
| 198 | #pragma GCC diagnostic pop |
| 199 | #endif |
| 200 | |
Eric Fiselier | 6871bcb | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 201 | #endif // SUPPORT_TEST_MACROS_HPP |