Skip to content
Snippets Groups Projects
Commit 60d3b5e3 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add a demangle facility

Code was previously inlined into BacktraceManager.
parent def923f4
No related branches found
No related tags found
1 merge request!37Feature/language
......@@ -4,10 +4,11 @@
#include <rang.hpp>
#include <cmath>
#include <cxxabi.h>
#include <execinfo.h>
#include <regex>
#include <Demangle.hpp>
BacktraceManager::BacktraceManager()
{
const int size = 100;
......@@ -35,7 +36,6 @@ operator<<(std::ostream& os, const BacktraceManager& btm)
const auto& line = lines[i_line];
os << rang::fg::green << "[" << std::setw(width) << i_line + 1 << '/' << lines.size() << "] " << rang::fg::reset;
std::smatch matchex;
int status = -1;
if (std::regex_search(line, matchex, mangled_function)) {
std::string prefix = matchex.prefix().str();
std::string function = line.substr(matchex.position() + 1, matchex.length() - 2);
......@@ -43,13 +43,7 @@ operator<<(std::ostream& os, const BacktraceManager& btm)
os << prefix << '(';
if (function.size() > 0) {
char* demangled = abi::__cxa_demangle(function.c_str(), NULL, NULL, &status);
if (status == 0) {
os << rang::style::bold << demangled << rang::style::reset;
free(demangled);
} else {
os << rang::style::bold << function << rang::style::reset;
}
os << rang::style::bold << demangle(function) << rang::style::reset;
}
os << '+' << suffix << '\n';
} else {
......
......@@ -8,6 +8,7 @@ add_library(
BuildInfo.cpp
BacktraceManager.cpp
ConsoleManager.cpp
Demangle.cpp
FPEManager.cpp
Messenger.cpp
Partitioner.cpp
......
#include <Demangle.hpp>
#include <cxxabi.h>
#include <memory>
std::string
demangle(const std::string_view mangled)
{
int status = -1;
char* cxa_demangled = abi::__cxa_demangle(mangled.data(), NULL, NULL, &status);
if (status == 0) {
std::string demangled{cxa_demangled};
free(cxa_demangled);
return demangled;
} else {
return std::string{mangled};
}
}
#ifndef DEMANGLE_HPP
#define DEMANGLE_HPP
#include <string>
#include <typeinfo>
std::string demangle(const std::string_view mangled);
template <typename T>
inline std::string
demangle()
{
return demangle(typeid(T).name());
}
#endif // DEMANGLE_HPP
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment