1 parent
4fc129fb7e
commit
e7186f17f0
42 files changed
+1816
-413
No files matched your search
@@ -0,0 +1,63 @@
|
||||
#include <Void/Testing/Detail/Symbol.hpp>
|
||||
#include <filesystem>
|
||||
#include <algorithm>
|
||||
|
||||
#ifdef _WIN32
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <dbghelp.h>
|
||||
#pragma comment(lib, "dbghelp.lib")
|
||||
#else
|
||||
#include <dlfcn.h>
|
||||
#ifdef __GNUC__
|
||||
#include <cxxabi.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace Void::Testing::Detail {
|
||||
|
||||
std::string resolve_symbol_name(const void* address) {
|
||||
#ifdef _WIN32
|
||||
static bool sym_initialized = false;
|
||||
if (!sym_initialized) { SymInitialize(GetCurrentProcess(), nullptr, TRUE); sym_initialized = true; }
|
||||
char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
|
||||
auto* symbol = reinterpret_cast<SYMBOL_INFO*>(buffer);
|
||||
symbol->SizeOfStruct = sizeof(SYMBOL_INFO); symbol->MaxNameLen = MAX_SYM_NAME;
|
||||
DWORD64 displacement = 0;
|
||||
if (SymFromAddr(GetCurrentProcess(), reinterpret_cast<DWORD64>(address), &displacement, symbol)) return symbol->Name;
|
||||
return {};
|
||||
#else
|
||||
Dl_info info{}; if (dladdr(address, &info) && info.dli_sname) return info.dli_sname; return {};
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string demangle(std::string_view mangled) {
|
||||
if (mangled.empty()) return {};
|
||||
#ifdef _WIN32
|
||||
return std::string{mangled};
|
||||
#else
|
||||
#ifdef __GNUC__
|
||||
int status = 0; char* demangled = abi::__cxa_demangle(mangled.data(), nullptr, nullptr, &status);
|
||||
if (status == 0 && demangled) { std::string result{demangled}; std::free(demangled); return result; }
|
||||
return std::string{mangled};
|
||||
#else
|
||||
return std::string{mangled};
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string pretty_name(const void* address) { auto m = resolve_symbol_name(address); return m.empty() ? "" : demangle(m); }
|
||||
|
||||
std::string extract_test_name(std::string_view pretty_symbol) {
|
||||
if (pretty_symbol.empty()) return "<unnamed>";
|
||||
auto paren = pretty_symbol.find('(');
|
||||
std::string_view name_part = (paren != std::string_view::npos) ? pretty_symbol.substr(0, paren) : pretty_symbol;
|
||||
auto last_colon = name_part.rfind("::");
|
||||
if (last_colon != std::string_view::npos) name_part = name_part.substr(last_colon + 2);
|
||||
while (!name_part.empty() && name_part.front() == ' ') name_part.remove_prefix(1);
|
||||
while (!name_part.empty() && name_part.back() == ' ') name_part.remove_suffix(1);
|
||||
return std::string{name_part};
|
||||
}
|
||||
|
||||
std::string suite_from_file(std::string_view file_path) { return std::filesystem::path{file_path}.stem().string(); }
|
||||
} // namespace Void::Testing::Detail
|
||||
Reference in new issue
Block a user