551 lines · c
1// -*- C++ -*-2//===----------------------------------------------------------------------===//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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef SUPPORT_TEST_MACROS_HPP11#define SUPPORT_TEST_MACROS_HPP12 13#ifdef __has_include14# if __has_include(<version>)15# include <version>16# else17# include <ciso646>18# endif19#else20# include <ciso646>21#endif22 23#define TEST_STRINGIZE_IMPL(...) #__VA_ARGS__24#define TEST_STRINGIZE(...) TEST_STRINGIZE_IMPL(__VA_ARGS__)25 26#define TEST_CONCAT1(X, Y) X##Y27#define TEST_CONCAT(X, Y) TEST_CONCAT1(X, Y)28 29#ifdef __has_feature30#define TEST_HAS_FEATURE(X) __has_feature(X)31#else32#define TEST_HAS_FEATURE(X) 033#endif34 35#ifndef __has_include36#define __has_include(...) 037#endif38 39#ifdef __has_extension40#define TEST_HAS_EXTENSION(X) __has_extension(X)41#else42#define TEST_HAS_EXTENSION(X) 043#endif44 45#ifdef __has_warning46#define TEST_HAS_WARNING(X) __has_warning(X)47#else48#define TEST_HAS_WARNING(X) 049#endif50 51#ifdef __has_builtin52#define TEST_HAS_BUILTIN(X) __has_builtin(X)53#else54#define TEST_HAS_BUILTIN(X) 055#endif56#ifdef __is_identifier57// '__is_identifier' returns '0' if '__x' is a reserved identifier provided by58// the compiler and '1' otherwise.59#define TEST_HAS_BUILTIN_IDENTIFIER(X) !__is_identifier(X)60#else61#define TEST_HAS_BUILTIN_IDENTIFIER(X) 062#endif63 64#if defined(__EDG__)65# define TEST_COMPILER_EDG66#elif defined(__clang__)67# define TEST_COMPILER_CLANG68# if defined(__apple_build_version__)69# define TEST_COMPILER_APPLE_CLANG70# endif71#elif defined(_MSC_VER)72# define TEST_COMPILER_MSVC73#elif defined(__GNUC__)74# define TEST_COMPILER_GCC75#endif76 77#if defined(__apple_build_version__)78// Given AppleClang XX.Y.Z, TEST_APPLE_CLANG_VER is XXYZ (e.g. AppleClang 14.0.3 => 1403)79#define TEST_APPLE_CLANG_VER (__apple_build_version__ / 10000)80#elif defined(__clang_major__)81#define TEST_CLANG_VER (__clang_major__ * 100) + __clang_minor__82#elif defined(__GNUC__)83// Given GCC XX.YY.ZZ, TEST_GCC_VER is XXYYZZ84#define TEST_GCC_VER ((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__)85#endif86 87/* Make a nice name for the standard version */88#ifndef TEST_STD_VER89#if __cplusplus <= 199711L90# define TEST_STD_VER 391#elif __cplusplus <= 201103L92# define TEST_STD_VER 1193#elif __cplusplus <= 201402L94# define TEST_STD_VER 1495#elif __cplusplus <= 201703L96# define TEST_STD_VER 1797#elif __cplusplus <= 202002L98# define TEST_STD_VER 2099#elif __cplusplus <= 202302L100# define TEST_STD_VER 23101#else102# define TEST_STD_VER 99 // greater than current standard103// This is deliberately different than _LIBCPP_STD_VER to discourage matching them up.104#endif105#endif106 107// Attempt to deduce the GLIBC version108#if (defined(__has_include) && __has_include(<features.h>)) || \109 defined(__linux__)110#include <features.h>111#if defined(__GLIBC_PREREQ)112#define TEST_HAS_GLIBC113#define TEST_GLIBC_PREREQ(major, minor) __GLIBC_PREREQ(major, minor)114#endif115#endif116 117#if TEST_STD_VER >= 11118# define TEST_ALIGNOF(...) alignof(__VA_ARGS__)119# define TEST_ALIGNAS(...) alignas(__VA_ARGS__)120# define TEST_CONSTEXPR constexpr121# define TEST_NOEXCEPT noexcept122# define TEST_NOEXCEPT_FALSE noexcept(false)123# define TEST_NOEXCEPT_COND(...) noexcept(__VA_ARGS__)124#else125# if defined(TEST_COMPILER_CLANG)126# define TEST_ALIGNOF(...) _Alignof(__VA_ARGS__)127# else128# define TEST_ALIGNOF(...) __alignof(__VA_ARGS__)129# endif130# define TEST_ALIGNAS(...) __attribute__((__aligned__(__VA_ARGS__)))131# define TEST_CONSTEXPR132# define TEST_NOEXCEPT throw()133# define TEST_NOEXCEPT_FALSE134# define TEST_NOEXCEPT_COND(...)135#endif136 137#if TEST_STD_VER >= 11138# define TEST_THROW_SPEC(...)139#else140# define TEST_THROW_SPEC(...) throw(__VA_ARGS__)141#endif142 143#if defined(__cpp_lib_is_constant_evaluated) && __cpp_lib_is_constant_evaluated >= 201811L144# define TEST_IS_CONSTANT_EVALUATED std::is_constant_evaluated()145#elif TEST_HAS_BUILTIN(__builtin_is_constant_evaluated)146# define TEST_IS_CONSTANT_EVALUATED __builtin_is_constant_evaluated()147#else148# define TEST_IS_CONSTANT_EVALUATED false149#endif150 151#if TEST_STD_VER >= 20152# define TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED true153#else154# define TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED (!TEST_IS_CONSTANT_EVALUATED)155#endif156 157#if TEST_STD_VER >= 23158# define TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED true159#else160# define TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED (!TEST_IS_CONSTANT_EVALUATED)161#endif162 163#if TEST_STD_VER >= 26164# define TEST_STD_AT_LEAST_26_OR_RUNTIME_EVALUATED true165#else166# define TEST_STD_AT_LEAST_26_OR_RUNTIME_EVALUATED (!TEST_IS_CONSTANT_EVALUATED)167#endif168 169#if TEST_STD_VER >= 14170# define TEST_CONSTEXPR_CXX14 constexpr171#else172# define TEST_CONSTEXPR_CXX14173#endif174 175#if TEST_STD_VER >= 17176# define TEST_CONSTEXPR_CXX17 constexpr177#else178# define TEST_CONSTEXPR_CXX17179#endif180 181#if TEST_STD_VER >= 20182# define TEST_CONSTEXPR_CXX20 constexpr183#else184# define TEST_CONSTEXPR_CXX20185#endif186 187#if TEST_STD_VER >= 23188# define TEST_CONSTEXPR_CXX23 constexpr189#else190# define TEST_CONSTEXPR_CXX23191#endif192 193#if TEST_STD_VER >= 26194# define TEST_CONSTEXPR_CXX26 constexpr195#else196# define TEST_CONSTEXPR_CXX26197#endif198 199#define TEST_ALIGNAS_TYPE(...) TEST_ALIGNAS(TEST_ALIGNOF(__VA_ARGS__))200 201#if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cpp_rtti) \202 && !defined(__GXX_RTTI)203#define TEST_HAS_NO_RTTI204#endif205 206#if !defined(TEST_HAS_NO_RTTI)207# define RTTI_ASSERT(X) assert(X)208#else209# define RTTI_ASSERT(X)210#endif211 212#if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cpp_exceptions) \213 && !defined(__EXCEPTIONS)214#define TEST_HAS_NO_EXCEPTIONS215#endif216 217#if TEST_HAS_FEATURE(address_sanitizer) || TEST_HAS_FEATURE(hwaddress_sanitizer) || \218 TEST_HAS_FEATURE(memory_sanitizer) || TEST_HAS_FEATURE(thread_sanitizer)219#define TEST_HAS_SANITIZERS220#define TEST_IS_EXECUTED_IN_A_SLOW_ENVIRONMENT221#endif222 223#ifdef _LIBCPP_USE_FROZEN_CXX03_HEADERS224# ifdef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION225# define TEST_HAS_NO_ALIGNED_ALLOCATION226# endif227#elif defined(_LIBCPP_VERSION) && !_LIBCPP_HAS_ALIGNED_ALLOCATION228# define TEST_HAS_NO_ALIGNED_ALLOCATION229#elif TEST_STD_VER < 17 && (!defined(__cpp_aligned_new) || __cpp_aligned_new < 201606L)230# define TEST_HAS_NO_ALIGNED_ALLOCATION231#endif232 233#if TEST_STD_VER > 17234# define TEST_CONSTINIT constinit235#elif __has_cpp_attribute(clang::require_constant_initialization)236# define TEST_CONSTINIT [[clang::require_constant_initialization]]237#else238# define TEST_CONSTINIT239#endif240 241#if TEST_STD_VER < 11242#define ASSERT_NOEXCEPT(...)243#define ASSERT_NOT_NOEXCEPT(...)244#else245#define ASSERT_NOEXCEPT(...) \246 static_assert(noexcept(__VA_ARGS__), "Operation must be noexcept")247 248#define ASSERT_NOT_NOEXCEPT(...) \249 static_assert(!noexcept(__VA_ARGS__), "Operation must NOT be noexcept")250#endif251 252/* Macros for testing libc++ specific behavior and extensions */253#if defined(_LIBCPP_VERSION)254#define LIBCPP_ASSERT(...) assert(__VA_ARGS__)255#define LIBCPP_STATIC_ASSERT(...) static_assert(__VA_ARGS__)256#define LIBCPP_ASSERT_NOEXCEPT(...) ASSERT_NOEXCEPT(__VA_ARGS__)257#define LIBCPP_ASSERT_NOT_NOEXCEPT(...) ASSERT_NOT_NOEXCEPT(__VA_ARGS__)258#define LIBCPP_ONLY(...) __VA_ARGS__259#else260#define LIBCPP_ASSERT(...) static_assert(true, "")261#define LIBCPP_STATIC_ASSERT(...) static_assert(true, "")262#define LIBCPP_ASSERT_NOEXCEPT(...) static_assert(true, "")263#define LIBCPP_ASSERT_NOT_NOEXCEPT(...) static_assert(true, "")264#define LIBCPP_ONLY(...) static_assert(true, "")265#endif266 267#ifdef _LIBCPP_USE_FROZEN_CXX03_HEADERS268# define LIBCPP_NON_FROZEN_ASSERT(...) static_assert(true, "")269#else270# define LIBCPP_NON_FROZEN_ASSERT(...) LIBCPP_ASSERT(__VA_ARGS__)271#endif272 273#if __has_cpp_attribute(nodiscard)274# define TEST_NODISCARD [[nodiscard]]275#else276# define TEST_NODISCARD277#endif278 279#define TEST_IGNORE_NODISCARD (void)280 281#ifdef _LIBCPP_USE_FROZEN_CXX03_HEADERS282// from-chars is a C++17 feature, so it's never available anyways283#elif !defined(_LIBCPP_VERSION) || _LIBCPP_AVAILABILITY_HAS_FROM_CHARS_FLOATING_POINT284# define TEST_HAS_FROM_CHARS_FLOATING_POINT285#endif286 287namespace test_macros_detail {288template <class T, class U>289struct is_same { enum { value = 0};} ;290template <class T>291struct is_same<T, T> { enum {value = 1}; };292} // namespace test_macros_detail293 294#define ASSERT_SAME_TYPE(...) \295 static_assert((test_macros_detail::is_same<__VA_ARGS__>::value), \296 "Types differ unexpectedly")297 298#ifndef TEST_HAS_NO_EXCEPTIONS299#define TEST_THROW(...) throw __VA_ARGS__300#else301#if defined(__GNUC__)302#define TEST_THROW(...) __builtin_abort()303#else304#include <stdlib.h>305#define TEST_THROW(...) ::abort()306#endif307#endif308 309#if defined(__GNUC__) || defined(__clang__)310// This function can be used to hide some objects from compiler optimizations.311//312// For example, this is useful to hide the result of a call to `new` and ensure313// that the compiler doesn't elide the call to new/delete. Otherwise, elliding314// calls to new/delete is allowed by the Standard and compilers actually do it315// when optimizations are enabled.316template <class Tp>317inline Tp const& DoNotOptimize(Tp const& value) {318 // The `m` constraint is invalid in the AMDGPU backend.319# if defined(__AMDGPU__) || defined(__NVPTX__)320 asm volatile("" : : "r"(value) : "memory");321# else322 asm volatile("" : : "r,m"(value) : "memory");323# endif324 return value;325}326 327template <class Tp>328inline Tp& DoNotOptimize(Tp& value) {329 // The `m` and `r` output constraint is invalid in the AMDGPU backend as well330 // as i8 / i1 arguments, so we just capture the pointer instead.331# if defined(__AMDGPU__)332 Tp* tmp = &value;333 asm volatile("" : "+v"(tmp) : : "memory");334# elif defined(__clang__)335 asm volatile("" : "+r,m"(value) : : "memory");336# else337 asm volatile("" : "+m,r"(value) : : "memory");338# endif339 return value;340}341#else342#include <intrin.h>343template <class Tp>344inline Tp const& DoNotOptimize(Tp const& value) {345 const volatile void* volatile unused = __builtin_addressof(value);346 static_cast<void>(unused);347 _ReadWriteBarrier();348 return value;349}350#endif351 352#if defined(__GNUC__)353#define TEST_ALWAYS_INLINE __attribute__((always_inline))354#define TEST_NOINLINE __attribute__((noinline))355#elif defined(_MSC_VER)356#define TEST_ALWAYS_INLINE __forceinline357#define TEST_NOINLINE __declspec(noinline)358#else359#define TEST_ALWAYS_INLINE360#define TEST_NOINLINE361#endif362 363#ifdef _WIN32364#define TEST_NOT_WIN32(...) ((void)0)365#else366#define TEST_NOT_WIN32(...) __VA_ARGS__367#endif368 369#if defined(TEST_WINDOWS_DLL) ||defined(__MVS__) || defined(_AIX)370// Macros for waiving cases when we can't count allocations done within371// the library implementation.372//373// On Windows, when libc++ is built as a DLL, references to operator new/delete374// within the DLL are bound at link time to the operator new/delete within375// the library; replacing them in the user executable doesn't override the376// calls within the library.377//378// The same goes on IBM zOS.379// The same goes on AIX.380#define ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(...) ((void)(__VA_ARGS__))381#define TEST_SUPPORTS_LIBRARY_INTERNAL_ALLOCATIONS 0382#else383#define ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(...) assert(__VA_ARGS__)384#define TEST_SUPPORTS_LIBRARY_INTERNAL_ALLOCATIONS 1385#endif386 387#if (defined(TEST_WINDOWS_DLL) && !defined(_MSC_VER)) || \388 defined(__MVS__)389// Normally, a replaced e.g. 'operator new' ends up used if the user code390// does a call to e.g. 'operator new[]'; it's enough to replace the base391// versions and have it override all of them.392//393// When the fallback operators are located within the libc++ library and we394// can't override the calls within it (see above), this fallback mechanism395// doesn't work either.396//397// On Windows, when using the MSVC vcruntime, the operator new/delete fallbacks398// are linked separately from the libc++ library, linked statically into399// the end user executable, and these fallbacks work even in DLL configurations.400// In MinGW configurations when built as a DLL, and on zOS, these fallbacks401// don't work though.402#define ASSERT_WITH_OPERATOR_NEW_FALLBACKS(...) ((void)(__VA_ARGS__))403#else404#define ASSERT_WITH_OPERATOR_NEW_FALLBACKS(...) assert(__VA_ARGS__)405#endif406 407#ifdef _WIN32408#define TEST_WIN_NO_FILESYSTEM_PERMS_NONE409#endif410 411// Support for carving out parts of the test suite, like removing wide characters, etc.412#if defined(_LIBCPP_VERSION) && !_LIBCPP_HAS_WIDE_CHARACTERS413# define TEST_HAS_NO_WIDE_CHARACTERS414#endif415 416#if defined(_LIBCPP_VERSION) && !_LIBCPP_HAS_UNICODE417# define TEST_HAS_NO_UNICODE418#elif defined(_MSVC_EXECUTION_CHARACTER_SET) && _MSVC_EXECUTION_CHARACTER_SET != 65001419# define TEST_HAS_NO_UNICODE420#endif421 422#ifdef _LIBCPP_USE_FROZEN_CXX03_HEADERS423# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR424# define TEST_HAS_OPEN_WITH_WCHAR425# endif426#elif defined(_LIBCPP_VERSION) && _LIBCPP_HAS_OPEN_WITH_WCHAR427# define TEST_HAS_OPEN_WITH_WCHAR428#endif429 430#ifdef _LIBCPP_USE_FROZEN_CXX03_HEADERS431# ifdef _LIBCPP_HAS_NO_INT128432# define TEST_HAS_NO_INT128433# endif434#elif (defined(_LIBCPP_VERSION) && !_LIBCPP_HAS_INT128) || defined(_MSVC_STL_VERSION)435# define TEST_HAS_NO_INT128436#endif437 438#if defined(_LIBCPP_VERSION) && !_LIBCPP_HAS_LOCALIZATION439# define TEST_HAS_NO_LOCALIZATION440#endif441 442#if TEST_STD_VER <= 17 || !defined(__cpp_char8_t)443# define TEST_HAS_NO_CHAR8_T444#endif445 446#if defined(_LIBCPP_VERSION) && !_LIBCPP_HAS_THREADS447# define TEST_HAS_NO_THREADS448#endif449 450#if defined(_LIBCPP_VERSION) && !_LIBCPP_HAS_FILESYSTEM451# define TEST_HAS_NO_FILESYSTEM452#endif453 454#ifdef _LIBCPP_USE_FROZEN_CXX03_HEADERS455# ifdef _LIBCPP_HAS_NO_C8RTOMB_MBRTOC8456# define TEST_HAS_NO_C8RTOMB_MBRTOC8457# endif458#elif defined(_LIBCPP_VERSION) && !_LIBCPP_HAS_C8RTOMB_MBRTOC8459# define TEST_HAS_NO_C8RTOMB_MBRTOC8460#endif461 462#if defined(_LIBCPP_VERSION) && !_LIBCPP_HAS_RANDOM_DEVICE463# define TEST_HAS_NO_RANDOM_DEVICE464#endif465 466#ifdef _LIBCPP_USE_FROZEN_CXX03_HEADERS467// This is a C++20 feature, so it's never available anyways468# define TEST_HAS_NO_EXPERIMENTAL_TZDB469#elif defined(_LIBCPP_VERSION) && !_LIBCPP_HAS_EXPERIMENTAL_TZDB470# define TEST_HAS_NO_EXPERIMENTAL_TZDB471#endif472 473#if defined(_LIBCPP_VERSION) && !_LIBCPP_HAS_TIME_ZONE_DATABASE474# define TEST_HAS_NO_TIME_ZONE_DATABASE475#endif476 477#if defined(TEST_COMPILER_CLANG)478# define TEST_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")479# define TEST_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")480# define TEST_CLANG_DIAGNOSTIC_IGNORED(str) _Pragma(TEST_STRINGIZE(clang diagnostic ignored str))481# define TEST_GCC_DIAGNOSTIC_IGNORED(str)482# define TEST_MSVC_DIAGNOSTIC_IGNORED(num)483#elif defined(TEST_COMPILER_GCC)484# define TEST_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")485# define TEST_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")486# define TEST_CLANG_DIAGNOSTIC_IGNORED(str)487# define TEST_GCC_DIAGNOSTIC_IGNORED(str) _Pragma(TEST_STRINGIZE(GCC diagnostic ignored str))488# define TEST_MSVC_DIAGNOSTIC_IGNORED(num)489#elif defined(TEST_COMPILER_MSVC)490# define TEST_DIAGNOSTIC_PUSH _Pragma("warning(push)")491# define TEST_DIAGNOSTIC_POP _Pragma("warning(pop)")492# define TEST_CLANG_DIAGNOSTIC_IGNORED(str)493# define TEST_GCC_DIAGNOSTIC_IGNORED(str)494# define TEST_MSVC_DIAGNOSTIC_IGNORED(num) _Pragma(TEST_STRINGIZE(warning(disable: num)))495#else496# define TEST_DIAGNOSTIC_PUSH497# define TEST_DIAGNOSTIC_POP498# define TEST_CLANG_DIAGNOSTIC_IGNORED(str)499# define TEST_GCC_DIAGNOSTIC_IGNORED(str)500# define TEST_MSVC_DIAGNOSTIC_IGNORED(num)501#endif502 503#if __has_cpp_attribute(msvc::no_unique_address)504#define TEST_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]505#elif __has_cpp_attribute(no_unique_address)506#define TEST_NO_UNIQUE_ADDRESS [[no_unique_address]]507#else508#define TEST_NO_UNIQUE_ADDRESS509#endif510 511#ifdef _LIBCPP_SHORT_WCHAR512# define TEST_SHORT_WCHAR513#endif514 515#ifdef _LIBCPP_ABI_MICROSOFT516# define TEST_ABI_MICROSOFT517#endif518 519// This is a temporary workaround for user-defined `operator new` definitions520// not being picked up on Apple platforms in some circumstances. This is under521// investigation and should be short-lived.522#ifdef __APPLE__523# define TEST_WORKAROUND_BUG_109234844_WEAK __attribute__((weak))524#else525# define TEST_WORKAROUND_BUG_109234844_WEAK /* nothing */526#endif527 528#ifdef _AIX529# define TEST_IF_AIX(arg_true, arg_false) arg_true530#else531# define TEST_IF_AIX(arg_true, arg_false) arg_false532#endif533 534// Placement `operator new`/`operator new[]` are not yet constexpr in C++26535// when using MS ABI, because they are from <vcruntime_new.h>.536#if defined(__cpp_lib_constexpr_new) && __cpp_lib_constexpr_new >= 202406L537# define TEST_CONSTEXPR_OPERATOR_NEW constexpr538#else539# define TEST_CONSTEXPR_OPERATOR_NEW540#endif541 542#if defined(_MSC_VER) || __SIZEOF_LONG_DOUBLE__ == __SIZEOF_DOUBLE__543# define TEST_LONG_DOUBLE_IS_DOUBLE544#endif545 546#if defined(__LDBL_MANT_DIG__) && __LDBL_MANT_DIG__ == 64547# define TEST_LONG_DOUBLE_IS_80_BIT548#endif549 550#endif // SUPPORT_TEST_MACROS_HPP551