Eric Fiselier | 8f1d85f | 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 | 375e2f6 | 2016-04-28 22:28:23 +0000 | [diff] [blame] | 14 | #include <ciso646> // Get STL specific macros like _LIBCPP_VERSION |
| 15 | |
Eric Fiselier | 8f1d85f | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 16 | #define TEST_CONCAT1(X, Y) X##Y |
| 17 | #define TEST_CONCAT(X, Y) TEST_CONCAT1(X, Y) |
| 18 | |
Eric Fiselier | 7175a07 | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 19 | #ifdef __has_feature |
| 20 | #define TEST_HAS_FEATURE(X) __has_feature(X) |
| 21 | #else |
| 22 | #define TEST_HAS_FEATURE(X) 0 |
| 23 | #endif |
| 24 | |
Eric Fiselier | 8f972f6 | 2016-10-04 21:25:51 +0000 | [diff] [blame^] | 25 | #ifdef __has_include |
| 26 | #define TEST_HAS_INCLUDE(X) __has_include(X) |
| 27 | #else |
| 28 | #define TEST_HAS_INCLUDE(X) 0 |
| 29 | #endif |
| 30 | |
Eric Fiselier | 8f1d85f | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 31 | #ifdef __has_extension |
| 32 | #define TEST_HAS_EXTENSION(X) __has_extension(X) |
| 33 | #else |
| 34 | #define TEST_HAS_EXTENSION(X) 0 |
| 35 | #endif |
| 36 | |
Eric Fiselier | 76d2446 | 2015-12-09 22:03:06 +0000 | [diff] [blame] | 37 | #ifdef __has_builtin |
| 38 | #define TEST_HAS_BUILTIN(X) __has_builtin(X) |
| 39 | #else |
| 40 | #define TEST_HAS_BUILTIN(X) 0 |
| 41 | #endif |
Eric Fiselier | e739d54 | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 42 | #ifdef __is_identifier |
| 43 | // '__is_identifier' returns '0' if '__x' is a reserved identifier provided by |
| 44 | // the compiler and '1' otherwise. |
| 45 | #define TEST_HAS_BUILTIN_IDENTIFIER(X) !__is_identifier(X) |
| 46 | #else |
| 47 | #define TEST_HAS_BUILTIN_IDENTIFIER(X) 0 |
| 48 | #endif |
Eric Fiselier | 76d2446 | 2015-12-09 22:03:06 +0000 | [diff] [blame] | 49 | |
Eric Fiselier | b5bdc07 | 2016-07-20 22:53:21 +0000 | [diff] [blame] | 50 | #if defined(__apple_build_version__) |
| 51 | #define TEST_APPLE_CLANG_VER (__clang_major__ * 100) + __clang_minor__ |
| 52 | #elif defined(__clang_major__) |
| 53 | #define TEST_CLANG_VER (__clang_major__ * 100) + __clang_minor__ |
| 54 | #elif defined(__GNUC__) |
| 55 | #define TEST_GCC_VER (__GNUC__ * 100 + __GNUC_MINOR__) |
| 56 | #endif |
| 57 | |
Eric Fiselier | 8f1d85f | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 58 | /* Make a nice name for the standard version */ |
Eric Fiselier | d24c465 | 2016-06-14 21:31:42 +0000 | [diff] [blame] | 59 | #ifndef TEST_STD_VER |
Eric Fiselier | 8f1d85f | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 60 | #if __cplusplus <= 199711L |
| 61 | # define TEST_STD_VER 3 |
| 62 | #elif __cplusplus <= 201103L |
| 63 | # define TEST_STD_VER 11 |
| 64 | #elif __cplusplus <= 201402L |
| 65 | # define TEST_STD_VER 14 |
| 66 | #else |
Eric Fiselier | 83d7ca9 | 2016-06-27 01:02:43 +0000 | [diff] [blame] | 67 | # define TEST_STD_VER 16 // current year; greater than current standard |
Eric Fiselier | 8f1d85f | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 68 | #endif |
Eric Fiselier | d24c465 | 2016-06-14 21:31:42 +0000 | [diff] [blame] | 69 | #endif |
Eric Fiselier | 8f1d85f | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 70 | |
Eric Fiselier | 4bc5e1f | 2016-09-04 00:48:54 +0000 | [diff] [blame] | 71 | // Attempt to deduce GCC version |
Eric Fiselier | 8f972f6 | 2016-10-04 21:25:51 +0000 | [diff] [blame^] | 72 | #if defined(_LIBCPP_VERSION) && TEST_HAS_INCLUDE(<features.h>) |
Eric Fiselier | 4bc5e1f | 2016-09-04 00:48:54 +0000 | [diff] [blame] | 73 | #include <features.h> |
| 74 | #define TEST_HAS_GLIBC |
| 75 | #define TEST_GLIBC_PREREQ(major, minor) __GLIBC_PREREQ(major, minor) |
| 76 | #endif |
| 77 | |
Eric Fiselier | 8f1d85f | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 78 | /* Features that were introduced in C++14 */ |
| 79 | #if TEST_STD_VER >= 14 |
| 80 | #define TEST_HAS_EXTENDED_CONSTEXPR |
| 81 | #define TEST_HAS_VARIABLE_TEMPLATES |
| 82 | #endif |
| 83 | |
| 84 | /* Features that were introduced after C++14 */ |
| 85 | #if TEST_STD_VER > 14 |
| 86 | #endif |
| 87 | |
Eric Fiselier | 8f1d85f | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 88 | #if TEST_STD_VER >= 11 |
Eric Fiselier | 00f4a49 | 2015-08-19 17:21:46 +0000 | [diff] [blame] | 89 | #define TEST_CONSTEXPR constexpr |
Eric Fiselier | 8f1d85f | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 90 | #define TEST_NOEXCEPT noexcept |
Eric Fiselier | 5ccbc48 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 91 | # if TEST_STD_VER >= 14 |
| 92 | # define TEST_CONSTEXPR_CXX14 constexpr |
| 93 | # else |
| 94 | # define TEST_CONSTEXPR_CXX14 |
| 95 | # endif |
Eric Fiselier | 8f1d85f | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 96 | #else |
Eric Fiselier | 00f4a49 | 2015-08-19 17:21:46 +0000 | [diff] [blame] | 97 | #define TEST_CONSTEXPR |
Eric Fiselier | 5ccbc48 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 98 | #define TEST_CONSTEXPR_CXX14 |
Eric Fiselier | 8f1d85f | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 99 | #define TEST_NOEXCEPT |
| 100 | #endif |
| 101 | |
Eric Fiselier | cef97f9 | 2016-06-22 05:40:17 +0000 | [diff] [blame] | 102 | #if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cpp_rtti) \ |
Eric Fiselier | 8905b11 | 2016-06-22 05:33:52 +0000 | [diff] [blame] | 103 | && !defined(__GXX_RTTI) |
Eric Fiselier | 7175a07 | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 104 | #define TEST_HAS_NO_RTTI |
| 105 | #endif |
| 106 | |
Eric Fiselier | cef97f9 | 2016-06-22 05:40:17 +0000 | [diff] [blame] | 107 | #if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \ |
Eric Fiselier | 8905b11 | 2016-06-22 05:33:52 +0000 | [diff] [blame] | 108 | && !defined(__EXCEPTIONS) |
Eric Fiselier | 7175a07 | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 109 | #define TEST_HAS_NO_EXCEPTIONS |
| 110 | #endif |
| 111 | |
Eric Fiselier | d95ca09 | 2015-10-01 08:34:37 +0000 | [diff] [blame] | 112 | #if TEST_HAS_FEATURE(address_sanitizer) || TEST_HAS_FEATURE(memory_sanitizer) || \ |
| 113 | TEST_HAS_FEATURE(thread_sanitizer) |
| 114 | #define TEST_HAS_SANITIZERS |
| 115 | #endif |
| 116 | |
Eric Fiselier | 8c4dc32 | 2016-06-26 19:42:59 +0000 | [diff] [blame] | 117 | #if defined(_LIBCPP_NORETURN) |
| 118 | #define TEST_NORETURN _LIBCPP_NORETURN |
| 119 | #else |
| 120 | #define TEST_NORETURN [[noreturn]] |
| 121 | #endif |
| 122 | |
Eric Fiselier | 375e2f6 | 2016-04-28 22:28:23 +0000 | [diff] [blame] | 123 | /* Macros for testing libc++ specific behavior and extensions */ |
| 124 | #if defined(_LIBCPP_VERSION) |
| 125 | #define LIBCPP_ASSERT(...) assert(__VA_ARGS__) |
| 126 | #define LIBCPP_STATIC_ASSERT(...) static_assert(__VA_ARGS__) |
| 127 | #else |
| 128 | #define LIBCPP_ASSERT(...) ((void)0) |
| 129 | #define LIBCPP_STATIC_ASSERT(...) ((void)0) |
| 130 | #endif |
| 131 | |
Eric Fiselier | 6e9a694 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 132 | #define ASSERT_NOEXCEPT(...) \ |
| 133 | static_assert(noexcept(__VA_ARGS__), "Operation must be noexcept") |
| 134 | |
| 135 | #define ASSERT_NOT_NOEXCEPT(...) \ |
| 136 | static_assert(!noexcept(__VA_ARGS__), "Operation must NOT be noexcept") |
| 137 | |
| 138 | namespace test_macros_detail { |
| 139 | template <class T, class U> |
| 140 | struct is_same { enum { value = 0};} ; |
| 141 | template <class T> |
| 142 | struct is_same<T, T> { enum {value = 1}; }; |
| 143 | } // namespace test_macros_detail |
| 144 | |
| 145 | #define ASSERT_SAME_TYPE(...) \ |
| 146 | static_assert(test_macros_detail::is_same<__VA_ARGS__>::value, \ |
| 147 | "Types differ uexpectedly") |
| 148 | |
Eric Fiselier | cdac787 | 2016-10-01 10:34:13 +0000 | [diff] [blame] | 149 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 150 | #define TEST_THROW(...) throw __VA_ARGS__ |
| 151 | #else |
| 152 | #if defined(__GNUC__) |
| 153 | #define TEST_THROW(...) __builtin_abort() |
| 154 | #else |
| 155 | #include <stdlib.h> |
| 156 | #define TEST_THROW(...) ::abort() |
| 157 | #endif |
| 158 | #endif |
| 159 | |
Eric Fiselier | 8f1d85f | 2015-05-27 00:28:30 +0000 | [diff] [blame] | 160 | #endif // SUPPORT_TEST_MACROS_HPP |