blob: 06800bdd34154ca7a599b75093f7dce4ae461672 [file] [log] [blame]
Eric Fiselier8f1d85f2015-05-27 00:28:30 +00001// -*- 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 Fiselier375e2f62016-04-28 22:28:23 +000014#include <ciso646> // Get STL specific macros like _LIBCPP_VERSION
15
Eric Fiselier50ca3242017-01-14 04:27:58 +000016#if defined(__GNUC__)
17#pragma GCC diagnostic push
18#pragma GCC diagnostic ignored "-Wvariadic-macros"
19#endif
20
Eric Fiselier8f1d85f2015-05-27 00:28:30 +000021#define TEST_CONCAT1(X, Y) X##Y
22#define TEST_CONCAT(X, Y) TEST_CONCAT1(X, Y)
23
Eric Fiselier7175a072015-07-31 02:24:58 +000024#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 Fiselier8f972f62016-10-04 21:25:51 +000030#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 Fiselier8f1d85f2015-05-27 00:28:30 +000036#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 Fiselier76d24462015-12-09 22:03:06 +000042#ifdef __has_builtin
43#define TEST_HAS_BUILTIN(X) __has_builtin(X)
44#else
45#define TEST_HAS_BUILTIN(X) 0
46#endif
Eric Fiseliere739d542016-08-11 03:13:11 +000047#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 Fiselier76d24462015-12-09 22:03:06 +000054
Casey Carterf0346a52017-05-10 19:10:49 +000055#if defined(__EDG__)
56# define TEST_COMPILER_EDG
57#elif defined(__clang__)
58# define TEST_COMPILER_CLANG
Eric Fiselier0da4cb82017-03-22 22:41:41 +000059# if defined(__apple_build_version__)
Casey Carterf0346a52017-05-10 19:10:49 +000060# define TEST_COMPILER_APPLE_CLANG
Eric Fiselier0da4cb82017-03-22 22:41:41 +000061# endif
62#elif defined(_MSC_VER)
63# define TEST_COMPILER_C1XX
64#elif defined(__GNUC__)
65# define TEST_COMPILER_GCC
66#endif
67
Eric Fiselierb5bdc072016-07-20 22:53:21 +000068#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
Eric Fiselier8f1d85f2015-05-27 00:28:30 +000076/* Make a nice name for the standard version */
Eric Fiselierd24c4652016-06-14 21:31:42 +000077#ifndef TEST_STD_VER
Eric Fiselier8f1d85f2015-05-27 00:28:30 +000078#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
Marshall Clow7afe61a2017-07-17 03:02:27 +000084#elif __cplusplus <= 201703L
85# define TEST_STD_VER 17
Eric Fiselier8f1d85f2015-05-27 00:28:30 +000086#else
Marshall Clow7afe61a2017-07-17 03:02:27 +000087# define TEST_STD_VER 99 // greater than current standard
88// This is deliberately different than _LIBCPP_STD_VER to discourage matching them up.
Eric Fiselier8f1d85f2015-05-27 00:28:30 +000089#endif
Eric Fiselierd24c4652016-06-14 21:31:42 +000090#endif
Eric Fiselier8f1d85f2015-05-27 00:28:30 +000091
Eric Fiselier4bc5e1f2016-09-04 00:48:54 +000092// Attempt to deduce GCC version
Eric Fiselier8f972f62016-10-04 21:25:51 +000093#if defined(_LIBCPP_VERSION) && TEST_HAS_INCLUDE(<features.h>)
Eric Fiselier4bc5e1f2016-09-04 00:48:54 +000094#include <features.h>
95#define TEST_HAS_GLIBC
96#define TEST_GLIBC_PREREQ(major, minor) __GLIBC_PREREQ(major, minor)
97#endif
98
Eric Fiselier8f1d85f2015-05-27 00:28:30 +000099/* 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
Eric Fiselier8f1d85f2015-05-27 00:28:30 +0000109#if TEST_STD_VER >= 11
Eric Fiseliere5bca2b2016-12-11 02:47:36 +0000110#define TEST_ALIGNOF(...) alignof(__VA_ARGS__)
111#define TEST_ALIGNAS(...) alignas(__VA_ARGS__)
Eric Fiselier00f4a492015-08-19 17:21:46 +0000112#define TEST_CONSTEXPR constexpr
Eric Fiselier8f1d85f2015-05-27 00:28:30 +0000113#define TEST_NOEXCEPT noexcept
Eric Fiselierd3b3b1f2017-04-13 10:17:23 +0000114#define TEST_NOEXCEPT_FALSE noexcept(false)
Eric Fiselier159b45f2016-10-12 09:31:26 +0000115#define TEST_NOEXCEPT_COND(...) noexcept(__VA_ARGS__)
Eric Fiselier5ccbc482016-04-22 10:33:56 +0000116# if TEST_STD_VER >= 14
117# define TEST_CONSTEXPR_CXX14 constexpr
118# else
119# define TEST_CONSTEXPR_CXX14
120# endif
Eric Fiseliere5bca2b2016-12-11 02:47:36 +0000121# if TEST_STD_VER > 14
122# define TEST_THROW_SPEC(...)
123# else
124# define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
125# endif
Eric Fiselier8f1d85f2015-05-27 00:28:30 +0000126#else
Eric Fiseliere5bca2b2016-12-11 02:47:36 +0000127#define TEST_ALIGNOF(...) __alignof(__VA_ARGS__)
128#define TEST_ALIGNAS(...) __attribute__((__aligned__(__VA_ARGS__)))
Eric Fiselier00f4a492015-08-19 17:21:46 +0000129#define TEST_CONSTEXPR
Eric Fiselier5ccbc482016-04-22 10:33:56 +0000130#define TEST_CONSTEXPR_CXX14
Eric Fiselierde8e3d32016-10-12 11:20:27 +0000131#define TEST_NOEXCEPT throw()
Eric Fiselierd3b3b1f2017-04-13 10:17:23 +0000132#define TEST_NOEXCEPT_FALSE
Eric Fiselier159b45f2016-10-12 09:31:26 +0000133#define TEST_NOEXCEPT_COND(...)
Eric Fiseliere5bca2b2016-12-11 02:47:36 +0000134#define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
Eric Fiselier8f1d85f2015-05-27 00:28:30 +0000135#endif
136
Eric Fiselier47755782016-10-12 10:19:48 +0000137#define TEST_ALIGNAS_TYPE(...) TEST_ALIGNAS(TEST_ALIGNOF(__VA_ARGS__))
138
Eric Fiseliercef97f92016-06-22 05:40:17 +0000139#if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cpp_rtti) \
Eric Fiselier8905b112016-06-22 05:33:52 +0000140 && !defined(__GXX_RTTI)
Eric Fiselier7175a072015-07-31 02:24:58 +0000141#define TEST_HAS_NO_RTTI
142#endif
143
Eric Fiseliercef97f92016-06-22 05:40:17 +0000144#if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \
Eric Fiselier8905b112016-06-22 05:33:52 +0000145 && !defined(__EXCEPTIONS)
Eric Fiselier7175a072015-07-31 02:24:58 +0000146#define TEST_HAS_NO_EXCEPTIONS
147#endif
148
Eric Fiselierd95ca092015-10-01 08:34:37 +0000149#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
Eric Fiselier8c4dc322016-06-26 19:42:59 +0000154#if defined(_LIBCPP_NORETURN)
155#define TEST_NORETURN _LIBCPP_NORETURN
156#else
157#define TEST_NORETURN [[noreturn]]
158#endif
159
Eric Fiseliera8312872018-03-22 04:42:56 +0000160#if !defined(__cpp_aligned_new) || __cpp_aligned_new < 201606L || \
161 defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
162#define TEST_HAS_NO_ALIGNED_ALLOCATION
163#endif
164
Eric Fiselier737c3bf2017-05-25 04:36:24 +0000165#if defined(_LIBCPP_SAFE_STATIC)
166#define TEST_SAFE_STATIC _LIBCPP_SAFE_STATIC
167#else
168#define TEST_SAFE_STATIC
169#endif
170
Marshall Clow570f32c2017-03-23 06:20:18 +0000171#if TEST_STD_VER < 11
172#define ASSERT_NOEXCEPT(...)
173#define ASSERT_NOT_NOEXCEPT(...)
174#else
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000175#define ASSERT_NOEXCEPT(...) \
176 static_assert(noexcept(__VA_ARGS__), "Operation must be noexcept")
177
178#define ASSERT_NOT_NOEXCEPT(...) \
179 static_assert(!noexcept(__VA_ARGS__), "Operation must NOT be noexcept")
Marshall Clow570f32c2017-03-23 06:20:18 +0000180#endif
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000181
Stephan T. Lavavejaa1d62b2016-12-09 19:53:08 +0000182/* Macros for testing libc++ specific behavior and extensions */
183#if defined(_LIBCPP_VERSION)
184#define LIBCPP_ASSERT(...) assert(__VA_ARGS__)
185#define LIBCPP_STATIC_ASSERT(...) static_assert(__VA_ARGS__)
186#define LIBCPP_ASSERT_NOEXCEPT(...) ASSERT_NOEXCEPT(__VA_ARGS__)
187#define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ASSERT_NOT_NOEXCEPT(__VA_ARGS__)
188#define LIBCPP_ONLY(...) __VA_ARGS__
189#else
190#define LIBCPP_ASSERT(...) ((void)0)
191#define LIBCPP_STATIC_ASSERT(...) ((void)0)
192#define LIBCPP_ASSERT_NOEXCEPT(...) ((void)0)
193#define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ((void)0)
194#define LIBCPP_ONLY(...) ((void)0)
195#endif
196
Billy Robert O'Neal III7e250fc2017-11-21 21:37:26 +0000197#define TEST_IGNORE_NODISCARD (void)
198
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000199namespace test_macros_detail {
200template <class T, class U>
201struct is_same { enum { value = 0};} ;
202template <class T>
203struct is_same<T, T> { enum {value = 1}; };
204} // namespace test_macros_detail
205
206#define ASSERT_SAME_TYPE(...) \
Eric Fiselier39683f12017-04-12 22:43:49 +0000207 static_assert((test_macros_detail::is_same<__VA_ARGS__>::value), \
Casey Carterbed65742017-05-30 20:12:55 +0000208 "Types differ unexpectedly")
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000209
Eric Fiseliercdac7872016-10-01 10:34:13 +0000210#ifndef TEST_HAS_NO_EXCEPTIONS
211#define TEST_THROW(...) throw __VA_ARGS__
212#else
213#if defined(__GNUC__)
214#define TEST_THROW(...) __builtin_abort()
215#else
216#include <stdlib.h>
217#define TEST_THROW(...) ::abort()
218#endif
219#endif
220
Eric Fiselier25f28d02017-02-08 00:10:10 +0000221#if defined(__GNUC__) || defined(__clang__)
222template <class Tp>
223inline void DoNotOptimize(Tp const& value) {
224 asm volatile("" : : "g"(value) : "memory");
225}
226#else
Eric Fiselier76880f52017-03-11 00:07:08 +0000227#include <intrin.h>
Eric Fiselier25f28d02017-02-08 00:10:10 +0000228template <class Tp>
Eric Fiselier76880f52017-03-11 00:07:08 +0000229inline void DoNotOptimize(Tp const& value) {
Casey Carter49abbf52017-05-04 15:54:09 +0000230 const volatile void* volatile unused = __builtin_addressof(value);
231 static_cast<void>(unused);
Eric Fiselier76880f52017-03-11 00:07:08 +0000232 _ReadWriteBarrier();
Eric Fiselier25f28d02017-02-08 00:10:10 +0000233}
234#endif
235
Eric Fiseliera8312872018-03-22 04:42:56 +0000236
Eric Fiselier50ca3242017-01-14 04:27:58 +0000237#if defined(__GNUC__)
238#pragma GCC diagnostic pop
239#endif
240
Eric Fiselier8f1d85f2015-05-27 00:28:30 +0000241#endif // SUPPORT_TEST_MACROS_HPP