Skip to content

Commit

Permalink
Normalization of stdlib inline namespace names
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Shchapov <vladislav@shchapov.ru>
  • Loading branch information
phprus committed Sep 29, 2022
1 parent 0ccaed3 commit 6e33d88
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
32 changes: 28 additions & 4 deletions include/fmt/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,34 @@ struct formatter<
std::size_t size = 0;
std::unique_ptr<char, decltype(&std::free)> demangled_name_ptr(
abi::__cxa_demangle(ti.name(), nullptr, &size, &status), &std::free);
out = detail::write_bytes(
out,
string_view(demangled_name_ptr ? demangled_name_ptr.get() : ti.name()),
spec);
string_view demangled_name_view(
demangled_name_ptr ? demangled_name_ptr.get() : ti.name());

// Normalization of stdlib inline namespace names.
if (demangled_name_view.starts_with("std::")) {
// Separation of user's and system exception classes.
demangled_name_view.remove_prefix(5);
out = detail::write_bytes(out, string_view("std::"), spec);

// libc++ inline namespaces.
// std::__1::* -> std::*
// std::__1::__fs::* -> std::*
if (demangled_name_view.starts_with("__1::")) {
demangled_name_view.remove_prefix(5);
if (demangled_name_view.starts_with("__fs::"))
demangled_name_view.remove_prefix(6);
}
// libstdc++ inline namespaces.
// std::__cxx11::* -> std::*
// std::filesystem::__cxx11::* -> std::filesystem::*
else if (demangled_name_view.starts_with("__cxx11::")) {
demangled_name_view.remove_prefix(9);
} else if (demangled_name_view.starts_with("filesystem::__cxx11::")) {
demangled_name_view.remove_prefix(21);
out = detail::write_bytes(out, string_view("filesystem::"), spec);
}
}
out = detail::write_bytes(out, demangled_name_view, spec);
#elif FMT_MSC_VERSION
string_view demangled_name_view(ti.name());
if (demangled_name_view.starts_with("class "))
Expand Down
19 changes: 18 additions & 1 deletion test/std-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#include <vector>

#include "fmt/ranges.h"
#include "gtest/gtest.h"
#include "gtest-extra.h" // StartsWith

using testing::StartsWith;

TEST(std_test, path) {
#ifdef __cpp_lib_filesystem
Expand Down Expand Up @@ -116,4 +118,19 @@ TEST(std_test, exception) {
fmt::format("{:t}", ex));
EXPECT_EQ("My Exception", fmt::format("{:}", ex));
}

try {
throw std::system_error(std::error_code(), "message");
} catch (const std::system_error& ex) {
EXPECT_THAT(fmt::format("{:t}", ex), StartsWith("std::system_error: "));
}

#ifdef __cpp_lib_filesystem
try {
throw std::filesystem::filesystem_error("message", std::error_code());
} catch (const std::filesystem::filesystem_error& ex) {
EXPECT_THAT(fmt::format("{:t}", ex),
StartsWith("std::filesystem::filesystem_error: "));
}
#endif
}

0 comments on commit 6e33d88

Please sign in to comment.