Skip to content
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

Open
psychocoderHPC opened this issue Oct 1, 2024 · 10 comments
Open

remove boost demangle #2405

psychocoderHPC opened this issue Oct 1, 2024 · 10 comments

Comments

@psychocoderHPC
Copy link
Member

psychocoderHPC commented Oct 1, 2024

template<typename T>
inline const std::string demangled = boost::core::demangle(typeid(T).name());

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.

#include <source_location>
#include <string>
#include <string_view>
    /// \file
    /// use source_location to derive the demangled type name
    /// based on:
    /// https://www.reddit.com/r/cpp/comments/lfi6jt/finally_a_possibly_portable_way_to_convert_types/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button

    struct DummyType
    {
    };

    template<typename T>
    inline auto EmbedTypeIntoSignature()
    {
        return std::string_view{std::source_location::current().function_name()};
    }

    template<typename T>
    struct Demangled
    {
        static auto name()
        {
            constexpr size_t testSignatureLength = sizeof("DummyType") - 1;
            auto const DummySignature = EmbedTypeIntoSignature<DummyType>();
            // count char's until the type name starts
            auto const startPosition = DummySignature.find("DummyType");
            // count char's after the type information by removing type name information and pre information
            auto const tailLength = DummySignature.size() - startPosition - testSignatureLength;
            auto const EmbeddingSignature = EmbedTypeIntoSignature<T>();
            auto const typeLength = EmbeddingSignature.size() - startPosition - tailLength;
            return EmbeddingSignature.substr(startPosition, typeLength);
        }
    };

    template<typename T>
    auto demangled = Demangled<T>::name();
@psychocoderHPC psychocoderHPC added this to the 2.0.0 milestone Oct 1, 2024
@fwyzard
Copy link
Contributor

fwyzard commented Oct 1, 2024

Can it be constexpr ?

@fwyzard
Copy link
Contributor

fwyzard commented Oct 1, 2024

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.

@SimeonEhrig
Copy link
Member

std::source_location::current() is actual pretty well. I think it is more a question if we want remove the compilers, which does not support std::source_location::current().

@SimeonEhrig
Copy link
Member

SimeonEhrig commented Oct 1, 2024

@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

@SimeonEhrig
Copy link
Member

Can it be constexpr ?

Yes. My minimal example produces only 15 lines assembler including printing: https://godbolt.org/z/YhxKx6n3W
(looks like there is a small bug with the string termination symbol \0).

@fwyzard
Copy link
Contributor

fwyzard commented Oct 1, 2024

Can it be constexpr ?

Yes: https://godbolt.org/z/cbKdEPz4q .

@psychocoderHPC
Copy link
Member Author

Can it be constexpr ?

It was first constexpr but in some places, it produced some issues.

@psychocoderHPC
Copy link
Member Author

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.

I understand, the motivation was always that boost is not testing nvcc and HIP and this produced in the past many issues.
Nevertheless this implementation is doing what it should.
Removing boost will reduce the test complexity a lot.

@fwyzard
Copy link
Contributor

fwyzard commented Oct 2, 2024

Note that GCC and clang produce different output in some cases, for example for std::string:

gcc

the name is: std::__cxx11::basic_string<char>

clang

the name is: std::basic_string<char>

But maybe the old one was also doing this ?

@fwyzard
Copy link
Contributor

fwyzard commented Oct 2, 2024

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

the name is: nested::Type

clang

the name is: Type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants