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
16 #define TEST_CONCAT1(X, Y) X##Y
17 #define TEST_CONCAT(X, Y) TEST_CONCAT1(X, Y)
20 #define TEST_HAS_FEATURE(X) __has_feature(X)
22 #define TEST_HAS_FEATURE(X) 0
26 #define TEST_HAS_INCLUDE(X) __has_include(X)
28 #define TEST_HAS_INCLUDE(X) 0
31 #ifdef __has_extension
32 #define TEST_HAS_EXTENSION(X) __has_extension(X)
34 #define TEST_HAS_EXTENSION(X) 0
38 #define TEST_HAS_BUILTIN(X) __has_builtin(X)
40 #define TEST_HAS_BUILTIN(X) 0
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)
47 #define TEST_HAS_BUILTIN_IDENTIFIER(X) 0
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__)
58 /* Make a nice name for the standard version */
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
67 # define TEST_STD_VER 16 // current year; greater than current standard
71 // Attempt to deduce GCC version
72 #if defined(_LIBCPP_VERSION) && TEST_HAS_INCLUDE(<features.h>)
74 #define TEST_HAS_GLIBC
75 #define TEST_GLIBC_PREREQ(major, minor) __GLIBC_PREREQ(major, minor)
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
84 /* Features that were introduced after C++14 */
88 #if TEST_STD_VER >= 11
89 #define TEST_ALIGNOF(...) alignof(__VA_ARGS__)
90 #define TEST_ALIGNAS(...) alignas(__VA_ARGS__)
91 #define TEST_CONSTEXPR constexpr
92 #define TEST_NOEXCEPT noexcept
93 #define TEST_NOEXCEPT_COND(...) noexcept(__VA_ARGS__)
94 # if TEST_STD_VER >= 14
95 # define TEST_CONSTEXPR_CXX14 constexpr
97 # define TEST_CONSTEXPR_CXX14
99 # if TEST_STD_VER > 14
100 # define TEST_THROW_SPEC(...)
102 # define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
105 #define TEST_ALIGNOF(...) __alignof(__VA_ARGS__)
106 #define TEST_ALIGNAS(...) __attribute__((__aligned__(__VA_ARGS__)))
107 #define TEST_CONSTEXPR
108 #define TEST_CONSTEXPR_CXX14
109 #define TEST_NOEXCEPT throw()
110 #define TEST_NOEXCEPT_COND(...)
111 #define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
114 #define TEST_ALIGNAS_TYPE(...) TEST_ALIGNAS(TEST_ALIGNOF(__VA_ARGS__))
116 #if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cpp_rtti) \
117 && !defined(__GXX_RTTI)
118 #define TEST_HAS_NO_RTTI
121 #if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \
122 && !defined(__EXCEPTIONS)
123 #define TEST_HAS_NO_EXCEPTIONS
126 #if TEST_HAS_FEATURE(address_sanitizer) || TEST_HAS_FEATURE(memory_sanitizer) || \
127 TEST_HAS_FEATURE(thread_sanitizer)
128 #define TEST_HAS_SANITIZERS
131 #if defined(_LIBCPP_NORETURN)
132 #define TEST_NORETURN _LIBCPP_NORETURN
134 #define TEST_NORETURN [[noreturn]]
137 #define ASSERT_NOEXCEPT(...) \
138 static_assert(noexcept(__VA_ARGS__), "Operation must be noexcept")
140 #define ASSERT_NOT_NOEXCEPT(...) \
141 static_assert(!noexcept(__VA_ARGS__), "Operation must NOT be noexcept")
143 /* Macros for testing libc++ specific behavior and extensions */
144 #if defined(_LIBCPP_VERSION)
145 #define LIBCPP_ASSERT(...) assert(__VA_ARGS__)
146 #define LIBCPP_STATIC_ASSERT(...) static_assert(__VA_ARGS__)
147 #define LIBCPP_ASSERT_NOEXCEPT(...) ASSERT_NOEXCEPT(__VA_ARGS__)
148 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ASSERT_NOT_NOEXCEPT(__VA_ARGS__)
149 #define LIBCPP_ONLY(...) __VA_ARGS__
151 #define LIBCPP_ASSERT(...) ((void)0)
152 #define LIBCPP_STATIC_ASSERT(...) ((void)0)
153 #define LIBCPP_ASSERT_NOEXCEPT(...) ((void)0)
154 #define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ((void)0)
155 #define LIBCPP_ONLY(...) ((void)0)
158 namespace test_macros_detail {
159 template <class T, class U>
160 struct is_same { enum { value = 0};} ;
162 struct is_same<T, T> { enum {value = 1}; };
163 } // namespace test_macros_detail
165 #define ASSERT_SAME_TYPE(...) \
166 static_assert(test_macros_detail::is_same<__VA_ARGS__>::value, \
167 "Types differ uexpectedly")
169 #ifndef TEST_HAS_NO_EXCEPTIONS
170 #define TEST_THROW(...) throw __VA_ARGS__
172 #if defined(__GNUC__)
173 #define TEST_THROW(...) __builtin_abort()
176 #define TEST_THROW(...) ::abort()
180 #endif // SUPPORT_TEST_MACROS_HPP