-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
remove boost demangle #2405
Comments
Can it be |
In general, I do not share the "remove boost at all costs" goal, so to me the new implementation needs to be at least as good as the old one, or possibly better. |
|
@fwyzard If you want, you can test it here: https://godbolt.org/z/P6MsrKTKv Update: a more advanced example with namespace: https://godbolt.org/z/159Gs313h |
Yes. My minimal example produces only 15 lines assembler including printing: https://godbolt.org/z/YhxKx6n3W |
|
It was first constexpr but in some places, it produced some issues. |
I understand, the motivation was always that boost is not testing nvcc and HIP and this produced in the past many issues. |
Note that GCC and clang produce different output in some cases, for example for gcc
clang
But maybe the old one was also doing this ? |
Ah, looks like the two give a different output for inline namespaces. inline namespace nested {
struct Type {};
}
int main(){
std::cout << "the name is: " << demangled<Type> << '\n';
} Gives: gcc
clang
|
Coming back to this, I found a few more issues with this approach:
|
Here is an example, putting it all together: https://godbolt.org/z/1xT5Ejszx . /*
* Compile-time demangled type name, based on
* https://www.reddit.com/r/cpp/comments/lfi6jt/finally_a_possibly_portable_way_to_convert_types/
* and
* https://rodusek.com/posts/2021/03/09/getting-an-unmangled-type-name-at-compile-time/
*/
#include <array>
#include <iostream>
#include <source_location>
#include <string_view>
#include <utility>
namespace
{
// Use a fundamental type to detect the prefix and suffix, because MSVC includes the class, struct, or union
// keyword in the type signature.
using test_pattern_type = double;
constexpr std::string_view test_pattern_name("double");
// Generate a function name with full type signature that includes the user-provided type name.
// Use the compiler-specific extensions for known compilers because nvc++ does not include the full type signature
// in std::source_location::current().function_name() .
template<typename T>
consteval std::string_view get_function_name()
{
#if defined(__GNUC__) || defined(__clang__) || defined(__PGI) || defined(__NVCOMPILER)
// gcc, clang, PGI, nvc++
return __PRETTY_FUNCTION__;
#elif defined(_MSC_VER)
// MSVC
return __FUNCSIG__;
#else
// unknown compiler, use the c++20 standard approach
return std::source_location::current().function_name();
#endif
}
consteval size_t prefix_size()
{
return get_function_name<test_pattern_type>().find(test_pattern_name);
}
consteval size_t suffix_size()
{
return get_function_name<test_pattern_type>().size() - prefix_size() - test_pattern_name.size();
}
template<typename T>
consteval auto demangle_as_substr()
{
constexpr std::string_view text = get_function_name<T>();
constexpr size_t start = prefix_size();
constexpr size_t end = text.size() - suffix_size();
static_assert(start < end);
constexpr size_t length = end - start;
return text.substr(start, length);
}
template<std::size_t... Idxs>
consteval auto substring_as_array(std::string_view str, std::index_sequence<Idxs...>)
{
// Add null termination
return std::array{str[Idxs]..., '\0'};
}
template<typename T>
consteval auto demangle_as_array()
{
constexpr std::string_view name = demangle_as_substr<T>();
return substring_as_array(name, std::make_index_sequence<name.size()>{});
}
template<typename T>
inline constexpr auto storage = demangle_as_array<T>();
template<typename T>
consteval std::string_view demangle()
{
return std::string_view{storage<T>.data(), storage<T>.size()};
}
} // namespace
template<typename T>
inline constexpr std::string_view const demangled = demangle<T>();
/******************************************************************************/
using Data = std::array<int, 42>;
namespace test {
template<typename X>
struct Type{};
}
int main(void)
{
std::puts(demangled<int>.data());
std::puts(demangled<Data>.data());
std::puts(demangled<test::Type<void>>.data());
} GCC
Clang
MSVC
NVC++
|
I've put it up here: https://github.com/fwyzard/demangle :-) |
alpaka/include/alpaka/core/DemangleTypeNames.hpp
Lines 18 to 19 in e20236d
We aim to remove boost completely. With clang 15+ and gcc 11+ we can remove this code with the following code.
These versions support
std::source_location
.The text was updated successfully, but these errors were encountered: