From e4d4fad597cf5b068f1cc0d1e257274aaafab21e Mon Sep 17 00:00:00 2001 From: Corentin Schreiber Date: Sat, 24 Sep 2022 12:34:14 +0100 Subject: [PATCH] Added type_to_string --- src/catch2/catch_all.hpp | 1 + .../internal/catch_compiler_capabilities.hpp | 2 ++ src/catch2/internal/catch_type_to_string.hpp | 33 +++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 src/catch2/internal/catch_type_to_string.hpp diff --git a/src/catch2/catch_all.hpp b/src/catch2/catch_all.hpp index 94f55223..2a24de69 100644 --- a/src/catch2/catch_all.hpp +++ b/src/catch2/catch_all.hpp @@ -109,6 +109,7 @@ #include #include #include +#include #include #include #include diff --git a/src/catch2/internal/catch_compiler_capabilities.hpp b/src/catch2/internal/catch_compiler_capabilities.hpp index 9ffae596..5a00d496 100644 --- a/src/catch2/internal/catch_compiler_capabilities.hpp +++ b/src/catch2/internal/catch_compiler_capabilities.hpp @@ -58,6 +58,7 @@ # define CATCH_INTERNAL_IGNORE_BUT_WARN(...) (void)__builtin_constant_p(__VA_ARGS__) +# define CATCH_INTERNAL_TYPEID_DEMANGLE #endif #if defined(__CUDACC__) && !defined(__clang__) @@ -73,6 +74,7 @@ # define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION _Pragma( "clang diagnostic push" ) # define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION _Pragma( "clang diagnostic pop" ) +# define CATCH_INTERNAL_TYPEID_DEMANGLE #endif // __clang__ && !_MSC_VER #if defined(__clang__) diff --git a/src/catch2/internal/catch_type_to_string.hpp b/src/catch2/internal/catch_type_to_string.hpp new file mode 100644 index 00000000..c5f99009 --- /dev/null +++ b/src/catch2/internal/catch_type_to_string.hpp @@ -0,0 +1,33 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 +#ifndef CATCH_TYPE_TO_STRING_HPP_INCLUDED +#define CATCH_TYPE_TO_STRING_HPP_INCLUDED + +#include + +#if defined( CATCH_INTERNAL_TYPEID_DEMANGLE ) +# include +# include +#endif + +namespace Catch { + template std::string type_to_string() { +#if defined( CATCH_INTERNAL_TYPEID_DEMANGLE ) + const char* name = typeid( T ).name(); + int status = 0; + std::unique_ptr res{ + abi::__cxa_demangle( name, NULL, NULL, &status ), std::free }; + + return status == 0 ? res.get() : name; +#else + return typeid( T ).name(); +#endif + } +} // end namespace Catch + +#endif // CATCH_TYPE_TO_STRING_HPP_INCLUDED