Browse Source

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

most stdlibc++ already provide this functionality out of the box, very consistently and well implemented (usually)

my main irk is that the llvm itanium demangle is totally unescesary when there is a perfectly stable, tested and well documented equivalent functionality in the standard stdc++ provided in most UNIX oses

its mostly to reduce binary size by a very thin margin, but he stdc++ is more than capable of doing he same behaviour we use a dpeendency for

for mingw or such howeger,, demangling becomies trickier so we have to exclude windows entirely because well windows likes to do things differently dont they

Signed-off-by: lizzie <lizzie@eden-emu.dev>
Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/3894
Reviewed-by: crueter <crueter@eden-emu.dev>
Reviewed-by: MaranBr <maranbr@eden-emu.dev>
lizzie/openssl-fixup-cmake53453
lizzie 14 hours ago
committed by crueter
parent
commit
f729dbb3c3
No known key found for this signature in database GPG Key ID: 425ACD2D4830EBC6
  1. 6
      CMakeModules/FindLLVM.cmake
  2. 4
      externals/CMakeLists.txt
  3. 7
      src/common/CMakeLists.txt
  4. 42
      src/common/demangle.cpp
  5. 3
      src/common/demangle.h

6
CMakeModules/FindLLVM.cmake

@ -1,3 +1,6 @@
# SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: 2023 Alexandre Bouvier <contact@amb.tf>
#
# SPDX-License-Identifier: GPL-3.0-or-later
@ -13,7 +16,8 @@ endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(LLVM HANDLE_COMPONENTS CONFIG_MODE)
if (LLVM_FOUND AND LLVM_Demangle_FOUND AND NOT TARGET LLVM::Demangle)
# Demangle only for Windows targets
if (WIN32 AND LLVM_FOUND AND LLVM_Demangle_FOUND AND NOT TARGET LLVM::Demangle)
add_library(LLVM::Demangle INTERFACE IMPORTED)
target_compile_definitions(LLVM::Demangle INTERFACE ${LLVM_DEFINITIONS})
target_include_directories(LLVM::Demangle INTERFACE ${LLVM_INCLUDE_DIRS})

4
externals/CMakeLists.txt

@ -49,8 +49,8 @@ if (NOT TARGET stb::headers)
add_library(stb::headers ALIAS stb)
endif()
# ItaniumDemangle
if (NOT TARGET LLVM::Demangle)
# ItaniumDemangle (Windows only)
if (WIN32 AND NOT TARGET LLVM::Demangle)
add_library(demangle demangle/ItaniumDemangle.cpp)
target_include_directories(demangle PUBLIC ./demangle)
if (NOT MSVC)

7
src/common/CMakeLists.txt

@ -243,7 +243,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 (WIN32)
target_link_libraries(common PRIVATE LLVM::Demangle)
endif()
if(ANDROID)
# For ASharedMemory_create

42
src/common/demangle.cpp

@ -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,44 @@
#include <string>
#include <string_view>
#ifdef _WIN32
#include <llvm/Demangle/Demangle.h>
#else
#include <cxxabi.h>
#endif
#include "common/demangle.h"
#include "common/scope_exit.h"
namespace Common {
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) {
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 _WIN32
// 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;
}
#endif
}
return ret;
return mangled;
}
return std::string{};
}
} // namespace Common

3
src/common/demangle.h

@ -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

Loading…
Cancel
Save