Last active
February 25, 2025 10:50
-
-
Save dk949/f30acf59c57719587ee37b280f902d52 to your computer and use it in GitHub Desktop.
Function to print c++ type names as plain text in gcc and clang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef UT_DEMANGLE_HPP | |
#define UT_DEMANGLE_HPP | |
/// Get type name as a string | |
/* Usage: | |
* #include "demangle.hpp" | |
* #include <iostream> | |
* | |
* namespace A::B { | |
* inline namespace C { | |
* struct S { }; | |
* | |
* template<typename> | |
* struct Wrap { }; | |
* | |
* template<template<class> class> | |
* struct WrapWrap { }; | |
* | |
* class Base { public: virtual ~Base() = default; }; | |
* class Derived : public Base { }; | |
* | |
* } // namespace C | |
* } // namespace A::B | |
* | |
* int main() { | |
* using namespace A::B; | |
* WrapWrap<Wrap> const a; | |
* Base b {}; | |
* Derived d {}; | |
* Base &b_ref = b; | |
* Base const&d_ref = d; | |
* | |
* std::cout | |
* << "dynamic mangled: " << typeid(a).name() << '\n' | |
* << "dynamic demangled: " << ut::typeNameDynamic(a) << '\n' | |
* << "static demangled: " << ut::typeName<decltype(a)>() << '\n' | |
* << "____________________________________________________________\n" | |
* << "dynamic mangled: " << typeid(b_ref).name() << '\n' | |
* << "dynamic demangled: " << ut::typeNameDynamic(b_ref) << '\n' | |
* << "static demangled: " << ut::typeName<decltype(b_ref)>() << '\n' | |
* << "____________________________________________________________\n" | |
* << "dynamic mangled: " << typeid(d_ref).name() << '\n' | |
* << "dynamic demangled: " << ut::typeNameDynamic(d_ref) << '\n' | |
* << "static demangled: " << ut::typeName<decltype(d_ref)>() << '\n' | |
* ; | |
* } | |
* | |
* // Possible output: | |
* // dynamic mangled: N1A1B1C8WrapWrapINS1_4WrapEEE | |
* // dynamic demangled: A::B::C::WrapWrap<A::B::C::Wrap> | |
* // static demangled: const A::B::C::WrapWrap<A::B::C::Wrap> | |
* // ____________________________________________________________ | |
* // dynamic mangled: N1A1B1C4BaseE | |
* // dynamic demangled: A::B::C::Base | |
* // static demangled: A::B::C::Base& | |
* // ____________________________________________________________ | |
* // dynamic mangled: N1A1B1C7DerivedE | |
* // dynamic demangled: A::B::C::Derived | |
* // static demangled: const A::B::C::Base& | |
* | |
*/ | |
#ifdef __GNUC__ // GCC and Clang | |
# include <cxxabi.h> | |
#endif | |
#include <memory> | |
#include <source_location> | |
#include <stdexcept> | |
#include <string> | |
#include <string_view> | |
namespace ut { | |
namespace detail { | |
template<typename T> | |
consteval char const *getTypeName() { | |
return std::source_location::current().function_name(); | |
} | |
} | |
/** Get the static type as a compile time string_view | |
* | |
* Preserves `const`, `volatile` and reference qualifiers | |
*/ | |
template<typename T> | |
consteval std::string_view typeName() { | |
std::string_view const type_name = detail::getTypeName<T>(); | |
auto const open_pos = [&] { | |
if (auto clang_pos = type_name.find("[T = "); clang_pos != type_name.npos) | |
return clang_pos + 5; | |
else if (auto gcc_pos = type_name.find("[with T = "); gcc_pos != type_name.npos) | |
return gcc_pos + 10; | |
else | |
return type_name.npos; | |
}(); | |
if (open_pos != type_name.npos) { // gcc and clang | |
auto const close_pos = type_name.find(']'); | |
if (close_pos == type_name.npos) throw std::logic_error("Could not parse end of type (GCC,Clang)"); | |
return type_name.substr(open_pos, close_pos - open_pos); | |
} else { // msvc | |
auto const type_name_open = type_name.find('<') + 1; | |
auto const type_name_close = type_name.find(">(void)") + 1; | |
if (type_name_open == type_name.npos) throw std::logic_error("Could not parse start of type (MSVC)"); | |
if (type_name_close == type_name.npos) throw std::logic_error("Could not parse end of type (MSVC)"); | |
return type_name.substr(type_name_open, type_name_close - type_name_open); | |
} | |
} | |
/* Return the dynamic type of `val` as a string | |
* | |
* NOTE: `const`, `volatile` and reference qualifiers will be discarded | |
* | |
* @throws: `std::bad_alloc` | |
* @throws: `std::runtime_error` | |
*/ | |
template<typename T> | |
[[nodiscard]] | |
std::string typeNameDynamic(T const &val); | |
#ifdef __GNUC__ // GCC and Clang | |
template<typename T> | |
[[nodiscard]] | |
std::string typeNameDynamic(T const &val) { | |
auto const *name = typeid(val).name(); | |
int status = 1; | |
std::unique_ptr<char, decltype(&std::free)> realname { | |
abi::__cxa_demangle(name, nullptr, nullptr, &status), | |
std::free, | |
}; | |
switch (status) { | |
case 0: break; | |
case -1: throw std::bad_alloc(); | |
case -2: return name; | |
case -3: throw std::runtime_error("Demangling failed due to an internal error"); | |
default: throw std::runtime_error("Unexpected exit code from __cxa_demangle: " + std::to_string(status)); | |
} | |
if (realname) | |
return realname.get(); | |
else | |
throw std::runtime_error("Demangling failed. Status: " + std::to_string(status)); | |
} | |
#else // other compilers. This will return a platform specific string (on MSVC this is the demangled name) | |
template<typename T> | |
[[nodiscard]] | |
std::string typeNameDynamic(T const &val) { | |
return typeid(val).name(); | |
} | |
#endif | |
} // namespace ut | |
/* | |
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <https://unlicense.org> | |
*/ | |
#endif // UT_DEMANGLE_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
convert types into human readable strings |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment