roar
Loading...
Searching...
No Matches
demangle.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <boost/algorithm/string.hpp>
4
5#ifndef _MSC_VER
6# include <cxxabi.h>
7#endif
8#include <string>
9
10namespace Roar
11{
18 inline std::string demangleCppSymbol(
19 std::string const& name,
20 std::vector<std::pair<std::string, std::string>> const& replacers = {},
21 bool format = false)
22 {
23 // C++ Types can get very long, so using a very pessimistic buffer:
24 constexpr std::size_t bufferSize = 8 * 1024ull * 1024ull;
25
26 std::string demangled(bufferSize, '\0');
27 std::size_t mutableLength = bufferSize;
28
29#ifndef _MSC_VER
30 int status = 0;
31 abi::__cxa_demangle(name.c_str(), demangled.data(), &mutableLength, &status);
32#else
33 demangled = name;
34#endif
35 demangled.resize(mutableLength);
36
37 const auto pos = demangled.find('\0');
38 if (pos != std::string::npos)
39 demangled.resize(pos);
40
41 boost::replace_all(
42 demangled,
43 "std::__cxx11::basic_string<char, std::char_traits<char>, "
44 "std::allocator<char> >",
45 "std::string");
46
47 for (auto const& [needle, replacement] : replacers)
48 {
49 boost::replace_all(demangled, needle, replacement);
50 }
51
52 if (format)
53 {
54 int indent = 0;
55 std::string formatted;
56 auto addLineBreak = [&formatted, &indent](int indentChange) {
57 formatted.push_back('\n');
58 indent += indentChange;
59 for (int i = 0; i < indent; ++i)
60 {
61 formatted += " ";
62 }
63 };
64
65 for (auto character : demangled)
66 {
67 if (character == '<')
68 {
69 formatted.push_back(character);
70 addLineBreak(1);
71 }
72 else if (character == '>')
73 {
74 addLineBreak(-1);
75 formatted.push_back(character);
76 }
77 else if (character == ' ')
78 continue;
79 else if (character == ',')
80 {
81 formatted.push_back(character);
82 addLineBreak(0);
83 }
84 else
85 formatted.push_back(character);
86 }
87 demangled = formatted;
88 }
89
90 return demangled;
91 }
92} // namespace Roar
Definition authorization.hpp:10
std::string demangleCppSymbol(std::string const &name, std::vector< std::pair< std::string, std::string > > const &replacers={}, bool format=false)
Demangles a C++ symbol. Useful for tests or debugging purposes.
Definition demangle.hpp:18