[common] use abi::__cxa_demangle for demangling using the system's glibcxx/libc++

Signed-off-by: lizzie <lizzie@eden-emu.dev>
This commit is contained in:
lizzie 2026-04-26 10:16:50 +00:00
parent c95f8df8a5
commit 8840c58439
3 changed files with 37 additions and 17 deletions

View file

@ -252,7 +252,12 @@ if (lz4_ADDED)
endif()
target_link_libraries(common PUBLIC fmt::fmt stb::headers Threads::Threads unordered_dense::unordered_dense)
target_link_libraries(common PRIVATE lz4::lz4 LLVM::Demangle zstd::zstd)
target_link_libraries(common PRIVATE lz4::lz4 zstd::zstd)
# Please refer to src/common/demangle.cpp
if (MSVC)
target_link_libraries(common PRIVATE LLVM::Demangle)
endif()
if(ANDROID)
# For ASharedMemory_create

View file

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
@ -6,30 +6,42 @@
#include <string>
#include <string_view>
#include <cxxabi.h>
#include <llvm/Demangle/Demangle.h>
#include "common/demangle.h"
#include "common/scope_exit.h"
static bool IsItanium(std::string_view name) {
// A valid Itanium encoding requires 1-4 leading underscores, followed by 'Z'.
auto const pos = name.find_first_not_of('_');
return pos > 0 && pos <= 4 && pos < name.size() && name[pos] == 'Z';
}
namespace Common {
std::string DemangleSymbol(const std::string& mangled) {
std::string DemangleSymbol(const std::string&& mangled) {
if (mangled.size() > 0) {
auto const is_itanium = [](std::string_view name) -> bool {
// A valid Itanium encoding requires 1-4 leading underscores, followed by 'Z'.
auto const pos = name.find_first_not_of('_');
return pos > 0 && pos <= 4 && pos < name.size() && name[pos] == 'Z';
};
std::string ret = mangled;
if (is_itanium(mangled)) {
if (IsItanium(mangled)) {
#ifdef _MSC_VER
// requires the use of llvm
if (char* p = llvm::itaniumDemangle(mangled); p != nullptr) {
ret = std::string{p};
std::string ret = std::string{p};
std::free(p);
return ret;
}
#else
// can safely use libc++ and glibcxx provided demangling functions
// it's available since 2008(!) so no system should have issues with it
// see https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.3/a01696.html
int status;
if (char* p = abi::__cxa_demangle(mangled.c_str(), NULL, NULL, &status); p != nullptr) {
std::string ret = std::string{p};
std::free(p);
return ret;
}
#else
}
return ret;
return mangled;
}
return std::string{};
}
#endif
} // namespace Common

View file

@ -1,3 +1,6 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
@ -7,6 +10,6 @@
namespace Common {
std::string DemangleSymbol(const std::string& mangled);
std::string DemangleSymbol(const std::string&& mangled);
} // namespace Common