test_macros.h 9.1 KB

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