blob: 88cc4d5cacc3ec9fed7f3bfc75bcd1d6a9121892 [file] [log] [blame]
Eric Fiselier6871bcb2015-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 Fiselier1f4231f2016-04-28 22:28:23 +000014#include <ciso646> // Get STL specific macros like _LIBCPP_VERSION
15
Eric Fiselier1e33f122017-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 Fiselier6871bcb2015-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 Fiselier3461dbc2015-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
Alexander Richardson2a6a3fc2018-07-24 12:40:56 +000030#ifndef __has_include
31#define __has_include(...) 0
Eric Fiselier015839a2016-10-04 21:25:51 +000032#endif
33
Eric Fiselier6871bcb2015-05-27 00:28:30 +000034#ifdef __has_extension
35#define TEST_HAS_EXTENSION(X) __has_extension(X)
36#else
37#define TEST_HAS_EXTENSION(X) 0
38#endif
39
Eric Fiselierfff5ec02015-12-09 22:03:06 +000040#ifdef __has_builtin
41#define TEST_HAS_BUILTIN(X) __has_builtin(X)
42#else
43#define TEST_HAS_BUILTIN(X) 0
44#endif
Eric Fiselier324506b2016-08-11 03:13:11 +000045#ifdef __is_identifier
46// '__is_identifier' returns '0' if '__x' is a reserved identifier provided by
47// the compiler and '1' otherwise.
48#define TEST_HAS_BUILTIN_IDENTIFIER(X) !__is_identifier(X)
49#else
50#define TEST_HAS_BUILTIN_IDENTIFIER(X) 0
51#endif
Eric Fiselierfff5ec02015-12-09 22:03:06 +000052
Casey Carter768a93f2017-05-10 19:10:49 +000053#if defined(__EDG__)
54# define TEST_COMPILER_EDG
55#elif defined(__clang__)
56# define TEST_COMPILER_CLANG
Eric Fiselier9506f142017-03-22 22:41:41 +000057# if defined(__apple_build_version__)
Casey Carter768a93f2017-05-10 19:10:49 +000058# define TEST_COMPILER_APPLE_CLANG
Eric Fiselier9506f142017-03-22 22:41:41 +000059# endif
60#elif defined(_MSC_VER)
61# define TEST_COMPILER_C1XX
62#elif defined(__GNUC__)
63# define TEST_COMPILER_GCC
64#endif
65
Eric Fiselier7f1b0982016-07-20 22:53:21 +000066#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#endif
73
Eric Fiselier6871bcb2015-05-27 00:28:30 +000074/* Make a nice name for the standard version */
Eric Fiselierf2f2a632016-06-14 21:31:42 +000075#ifndef TEST_STD_VER
Eric Fiselier6871bcb2015-05-27 00:28:30 +000076#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
Marshall Clow33116352017-07-17 03:02:27 +000082#elif __cplusplus <= 201703L
83# define TEST_STD_VER 17
Eric Fiselier6871bcb2015-05-27 00:28:30 +000084#else
Marshall Clow33116352017-07-17 03:02:27 +000085# define TEST_STD_VER 99 // greater than current standard
86// This is deliberately different than _LIBCPP_STD_VER to discourage matching them up.
Eric Fiselier6871bcb2015-05-27 00:28:30 +000087#endif
Eric Fiselierf2f2a632016-06-14 21:31:42 +000088#endif
Eric Fiselier6871bcb2015-05-27 00:28:30 +000089
Eric Fiselier5efe8ec2018-10-24 18:37:42 +000090// Attempt to deduce the GLIBC version
91#if (defined(__has_include) && __has_include(<features.h>)) || \
92 defined(__linux__)
Eric Fiselierf49fe8f2016-09-04 00:48:54 +000093#include <features.h>
Dan Albertd5d06a02018-11-01 22:35:51 +000094#if defined(__GLIBC_PREREQ)
Eric Fiselierf49fe8f2016-09-04 00:48:54 +000095#define TEST_HAS_GLIBC
96#define TEST_GLIBC_PREREQ(major, minor) __GLIBC_PREREQ(major, minor)
97#endif
Dan Albertd5d06a02018-11-01 22:35:51 +000098#endif
Eric Fiselierf49fe8f2016-09-04 00:48:54 +000099
Eric Fiselier6871bcb2015-05-27 00:28:30 +0000100#if TEST_STD_VER >= 11
Eric Fiselier3ca45662016-12-11 02:47:36 +0000101#define TEST_ALIGNOF(...) alignof(__VA_ARGS__)
102#define TEST_ALIGNAS(...) alignas(__VA_ARGS__)
Eric Fiselier749adeb2015-08-19 17:21:46 +0000103#define TEST_CONSTEXPR constexpr
Eric Fiselier6871bcb2015-05-27 00:28:30 +0000104#define TEST_NOEXCEPT noexcept
Eric Fiselier859f80b2017-04-13 10:17:23 +0000105#define TEST_NOEXCEPT_FALSE noexcept(false)
Eric Fiselier883cc25d2016-10-12 09:31:26 +0000106#define TEST_NOEXCEPT_COND(...) noexcept(__VA_ARGS__)
Eric Fiselierb530a252016-04-22 10:33:56 +0000107# if TEST_STD_VER >= 14
108# define TEST_CONSTEXPR_CXX14 constexpr
109# else
110# define TEST_CONSTEXPR_CXX14
111# endif
Eric Fiselier3ca45662016-12-11 02:47:36 +0000112# if TEST_STD_VER > 14
113# define TEST_THROW_SPEC(...)
114# else
115# define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
116# endif
Eric Fiselier6871bcb2015-05-27 00:28:30 +0000117#else
Eric Fiselier3ca45662016-12-11 02:47:36 +0000118#define TEST_ALIGNOF(...) __alignof(__VA_ARGS__)
119#define TEST_ALIGNAS(...) __attribute__((__aligned__(__VA_ARGS__)))
Eric Fiselier749adeb2015-08-19 17:21:46 +0000120#define TEST_CONSTEXPR
Eric Fiselierb530a252016-04-22 10:33:56 +0000121#define TEST_CONSTEXPR_CXX14
Eric Fiselierd2c71a92016-10-12 11:20:27 +0000122#define TEST_NOEXCEPT throw()
Eric Fiselier859f80b2017-04-13 10:17:23 +0000123#define TEST_NOEXCEPT_FALSE
Eric Fiselier883cc25d2016-10-12 09:31:26 +0000124#define TEST_NOEXCEPT_COND(...)
Eric Fiselier3ca45662016-12-11 02:47:36 +0000125#define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
Eric Fiselier6871bcb2015-05-27 00:28:30 +0000126#endif
127
Marshall Clow119f0a52018-07-31 18:23:57 +0000128// Sniff out to see if the underling C library has C11 features
129// Note that at this time (July 2018), MacOS X and iOS do NOT.
Marshall Clow171c77b2018-08-15 21:19:08 +0000130// This is cribbed from __config; but lives here as well because we can't assume libc++
Louis Dionne6513f372018-08-14 18:16:56 +0000131#if __ISO_C_VISIBLE >= 2011 || TEST_STD_VER >= 11
Marshall Clow119f0a52018-07-31 18:23:57 +0000132# if defined(__FreeBSD__)
Marshall Clow171c77b2018-08-15 21:19:08 +0000133// Specifically, FreeBSD does NOT have timespec_get, even though they have all
134// the rest of C11 - this is PR#38495
Marshall Clow119f0a52018-07-31 18:23:57 +0000135# define TEST_HAS_C11_FEATURES
136# elif defined(__Fuchsia__)
137# define TEST_HAS_C11_FEATURES
Marshall Clow171c77b2018-08-15 21:19:08 +0000138# define TEST_HAS_TIMESPEC_GET
Marshall Clow119f0a52018-07-31 18:23:57 +0000139# elif defined(__linux__)
Dan Albertd5d06a02018-11-01 22:35:51 +0000140// This block preserves the old behavior used by include/__config:
141// _LIBCPP_GLIBC_PREREQ would be defined to 0 if __GLIBC_PREREQ was not
142// available. The configuration here may be too vague though, as Bionic, uClibc,
143// newlib, etc may all support these features but need to be configured.
144# if defined(TEST_GLIBC_PREREQ)
145# if TEST_GLIBC_PREREQ(2, 17)
Marshall Clow171c77b2018-08-15 21:19:08 +0000146# define TEST_HAS_TIMESPEC_GET
Marshall Clow119f0a52018-07-31 18:23:57 +0000147# define TEST_HAS_C11_FEATURES
148# endif
Dan Albertd5d06a02018-11-01 22:35:51 +0000149# elif defined(_LIBCPP_HAS_MUSL_LIBC)
Marshall Clow119f0a52018-07-31 18:23:57 +0000150# define TEST_HAS_C11_FEATURES
Marshall Clow171c77b2018-08-15 21:19:08 +0000151# define TEST_HAS_TIMESPEC_GET
Marshall Clow119f0a52018-07-31 18:23:57 +0000152# endif
153# elif defined(_WIN32)
154# if defined(_MSC_VER) && !defined(__MINGW32__)
155# define TEST_HAS_C11_FEATURES // Using Microsoft's C Runtime library
Marshall Clow171c77b2018-08-15 21:19:08 +0000156# define TEST_HAS_TIMESPEC_GET
Marshall Clow119f0a52018-07-31 18:23:57 +0000157# endif
158# endif
159#endif
160
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
167/* Features that were introduced in C++17 */
168#if TEST_STD_VER >= 17
169#endif
170
171/* Features that were introduced after C++17 */
172#if TEST_STD_VER > 17
173#endif
174
175
Eric Fiselier6ce45e02016-10-12 10:19:48 +0000176#define TEST_ALIGNAS_TYPE(...) TEST_ALIGNAS(TEST_ALIGNOF(__VA_ARGS__))
177
Eric Fiselier19d44832016-06-22 05:40:17 +0000178#if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cpp_rtti) \
Eric Fiselier94713d12016-06-22 05:33:52 +0000179 && !defined(__GXX_RTTI)
Eric Fiselier3461dbc2015-07-31 02:24:58 +0000180#define TEST_HAS_NO_RTTI
181#endif
182
Eric Fiselier19d44832016-06-22 05:40:17 +0000183#if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \
Eric Fiselier94713d12016-06-22 05:33:52 +0000184 && !defined(__EXCEPTIONS)
Eric Fiselier3461dbc2015-07-31 02:24:58 +0000185#define TEST_HAS_NO_EXCEPTIONS
186#endif
187
Eric Fiselier73629822015-10-01 08:34:37 +0000188#if TEST_HAS_FEATURE(address_sanitizer) || TEST_HAS_FEATURE(memory_sanitizer) || \
189 TEST_HAS_FEATURE(thread_sanitizer)
190#define TEST_HAS_SANITIZERS
191#endif
192
Eric Fiselier37400712016-06-26 19:42:59 +0000193#if defined(_LIBCPP_NORETURN)
194#define TEST_NORETURN _LIBCPP_NORETURN
195#else
196#define TEST_NORETURN [[noreturn]]
197#endif
198
Eric Fiselier5d462492018-03-22 06:21:07 +0000199#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || \
200 (!(TEST_STD_VER > 14 || \
201 (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606L)))
Eric Fiselierf2918d12018-03-22 04:42:56 +0000202#define TEST_HAS_NO_ALIGNED_ALLOCATION
203#endif
204
Eric Fiselier3ca91852017-05-25 04:36:24 +0000205#if defined(_LIBCPP_SAFE_STATIC)
206#define TEST_SAFE_STATIC _LIBCPP_SAFE_STATIC
207#else
208#define TEST_SAFE_STATIC
209#endif
210
Eric Fiselier0913ca12018-04-06 21:37:23 +0000211// FIXME: Fix this feature check when either (A) a compiler provides a complete
212// implementation, or (b) a feature check macro is specified
213#define TEST_HAS_NO_SPACESHIP_OPERATOR
214
215
Marshall Clowd8323162017-03-23 06:20:18 +0000216#if TEST_STD_VER < 11
217#define ASSERT_NOEXCEPT(...)
218#define ASSERT_NOT_NOEXCEPT(...)
219#else
Eric Fiselierc7979582016-06-17 19:46:40 +0000220#define ASSERT_NOEXCEPT(...) \
221 static_assert(noexcept(__VA_ARGS__), "Operation must be noexcept")
222
223#define ASSERT_NOT_NOEXCEPT(...) \
224 static_assert(!noexcept(__VA_ARGS__), "Operation must NOT be noexcept")
Marshall Clowd8323162017-03-23 06:20:18 +0000225#endif
Eric Fiselierc7979582016-06-17 19:46:40 +0000226
Stephan T. Lavavej8a597d62016-12-09 19:53:08 +0000227/* Macros for testing libc++ specific behavior and extensions */
228#if defined(_LIBCPP_VERSION)
229#define LIBCPP_ASSERT(...) assert(__VA_ARGS__)
230#define LIBCPP_STATIC_ASSERT(...) static_assert(__VA_ARGS__)
231#define LIBCPP_ASSERT_NOEXCEPT(...) ASSERT_NOEXCEPT(__VA_ARGS__)
232#define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ASSERT_NOT_NOEXCEPT(__VA_ARGS__)
233#define LIBCPP_ONLY(...) __VA_ARGS__
234#else
235#define LIBCPP_ASSERT(...) ((void)0)
236#define LIBCPP_STATIC_ASSERT(...) ((void)0)
237#define LIBCPP_ASSERT_NOEXCEPT(...) ((void)0)
238#define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ((void)0)
239#define LIBCPP_ONLY(...) ((void)0)
240#endif
241
Billy Robert O'Neal IIIba40b052017-11-21 21:37:26 +0000242#define TEST_IGNORE_NODISCARD (void)
243
Eric Fiselierc7979582016-06-17 19:46:40 +0000244namespace test_macros_detail {
245template <class T, class U>
246struct is_same { enum { value = 0};} ;
247template <class T>
248struct is_same<T, T> { enum {value = 1}; };
249} // namespace test_macros_detail
250
251#define ASSERT_SAME_TYPE(...) \
Eric Fiselier34e38ca2017-04-12 22:43:49 +0000252 static_assert((test_macros_detail::is_same<__VA_ARGS__>::value), \
Casey Carter09272912017-05-30 20:12:55 +0000253 "Types differ unexpectedly")
Eric Fiselierc7979582016-06-17 19:46:40 +0000254
Eric Fiselierf1889102016-10-01 10:34:13 +0000255#ifndef TEST_HAS_NO_EXCEPTIONS
256#define TEST_THROW(...) throw __VA_ARGS__
257#else
258#if defined(__GNUC__)
259#define TEST_THROW(...) __builtin_abort()
260#else
261#include <stdlib.h>
262#define TEST_THROW(...) ::abort()
263#endif
264#endif
265
Eric Fiselier4cdd9152017-02-08 00:10:10 +0000266#if defined(__GNUC__) || defined(__clang__)
267template <class Tp>
Eric Fiselier107d6d62018-03-22 21:28:09 +0000268inline
269void DoNotOptimize(Tp const& value) {
270 asm volatile("" : : "r,m"(value) : "memory");
271}
272
273template <class Tp>
274inline void DoNotOptimize(Tp& value) {
275#if defined(__clang__)
276 asm volatile("" : "+r,m"(value) : : "memory");
277#else
278 asm volatile("" : "+m,r"(value) : : "memory");
279#endif
Eric Fiselier4cdd9152017-02-08 00:10:10 +0000280}
281#else
Eric Fiselierd2001da2017-03-11 00:07:08 +0000282#include <intrin.h>
Eric Fiselier4cdd9152017-02-08 00:10:10 +0000283template <class Tp>
Eric Fiselierd2001da2017-03-11 00:07:08 +0000284inline void DoNotOptimize(Tp const& value) {
Casey Carterd50571b2017-05-04 15:54:09 +0000285 const volatile void* volatile unused = __builtin_addressof(value);
286 static_cast<void>(unused);
Eric Fiselierd2001da2017-03-11 00:07:08 +0000287 _ReadWriteBarrier();
Eric Fiselier4cdd9152017-02-08 00:10:10 +0000288}
289#endif
290
Eric Fiselierfca28db2018-10-10 18:22:23 +0000291#if defined(__GNUC__)
292#define TEST_ALWAYS_INLINE __attribute__((always_inline))
293#define TEST_NOINLINE __attribute__((noinline))
294#elif defined(_MSC_VER)
295#define TEST_ALWAYS_INLINE __forceinline
296#define TEST_NOINLINE __declspec(noinline)
297#else
298#define TEST_ALWAYS_INLINE
299#define TEST_NOINLINE
300#endif
Eric Fiselierf2918d12018-03-22 04:42:56 +0000301
Eric Fiselier1e33f122017-01-14 04:27:58 +0000302#if defined(__GNUC__)
303#pragma GCC diagnostic pop
304#endif
305
Eric Fiselier6871bcb2015-05-27 00:28:30 +0000306#endif // SUPPORT_TEST_MACROS_HPP