Allow cpptrace::detail::demangle to demangle strings that have non-identifier characters at the end
This commit is contained in:
parent
dc0c683804
commit
247474389f
@ -7,26 +7,42 @@
|
||||
#include <cxxabi.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
namespace cpptrace {
|
||||
namespace detail {
|
||||
std::string demangle(const std::string& name) {
|
||||
int status;
|
||||
// https://itanium-cxx-abi.github.io/cxx-abi/abi.html#demangler
|
||||
// check both _Z and __Z, apple prefixes all symbols with an underscore
|
||||
if(!(starts_with(name, "_Z") || starts_with(name, "__Z"))) {
|
||||
return name;
|
||||
}
|
||||
// Mangled names don't have spaces, we might add a space and some extra info somewhere but we still want it to
|
||||
// be demanglable. Look for a space, if there is one swap it with a null terminator briefly.
|
||||
auto end = name.find(' ');
|
||||
std::string name_copy;
|
||||
std::reference_wrapper<const std::string> to_demangle = name;
|
||||
std::string rest;
|
||||
if(end != std::string::npos) {
|
||||
name_copy = name.substr(0, end);
|
||||
rest = name.substr(end);
|
||||
to_demangle = name_copy;
|
||||
}
|
||||
// presumably thread-safe
|
||||
// it appears safe to pass nullptr for status however the docs don't explicitly say it's safe so I don't
|
||||
// want to rely on it
|
||||
char* const demangled = abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status);
|
||||
int status;
|
||||
char* const demangled = abi::__cxa_demangle(to_demangle.get().c_str(), nullptr, nullptr, &status);
|
||||
// demangled will always be nullptr on non-zero status, and if __cxa_demangle ever fails for any reason
|
||||
// we'll just quietly return the mangled name
|
||||
if(demangled) {
|
||||
// TODO: raii_wrap the char*?
|
||||
std::string str = demangled;
|
||||
std::free(demangled);
|
||||
if(!rest.empty()) {
|
||||
str += rest;
|
||||
}
|
||||
return str;
|
||||
} else {
|
||||
return name;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user