Index: include/llvm/Demangle/Demangle.h =================================================================== --- include/llvm/Demangle/Demangle.h +++ include/llvm/Demangle/Demangle.h @@ -11,6 +11,7 @@ #define LLVM_DEMANGLE_DEMANGLE_H #include +#include namespace llvm { /// This is a llvm local version of __cxa_demangle. Other than the name and @@ -36,6 +37,13 @@ char *microsoftDemangle(const char *mangled_name, char *buf, size_t *n, int *status, MSDemangleFlags Flags = MSDF_None); +/// Attempt to demangle a string using different demangling schemes. +/// The function uses heuristics to determine which demangling scheme to use. +/// \param MangledName - reference to string to demangle. +/// \returns - the demangled string, or a copy of the input string if no +/// demangling occurred. +std::string demangle(const std::string &MangledName); + /// "Partial" demangler. This supports demangling a string into an AST /// (typically an intermediate stage in itaniumDemangle) and querying certain /// properties or partially printing the demangled name. Index: lib/Demangle/CMakeLists.txt =================================================================== --- lib/Demangle/CMakeLists.txt +++ lib/Demangle/CMakeLists.txt @@ -1,4 +1,5 @@ add_llvm_library(LLVMDemangle + Demangle.cpp ItaniumDemangle.cpp MicrosoftDemangle.cpp MicrosoftDemangleNodes.cpp Index: lib/Demangle/Demangle.cpp =================================================================== --- lib/Demangle/Demangle.cpp +++ lib/Demangle/Demangle.cpp @@ -0,0 +1,30 @@ +//===-- Demangle.cpp - Common demangling functions ------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file This file contains definitions of common demangling functions. +/// +//===----------------------------------------------------------------------===// + +#include "llvm/Demangle/Demangle.h" + +std::string llvm::demangle(const std::string &MangledName) { + char *Demangled; + if (MangledName.compare(0, 2, "_Z") == 0) + Demangled = itaniumDemangle(MangledName.c_str(), nullptr, nullptr, nullptr); + else + Demangled = + microsoftDemangle(MangledName.c_str(), nullptr, nullptr, nullptr); + + if (!Demangled) + return MangledName; + + std::string Ret = Demangled; + free(Demangled); + return Ret; +} Index: tools/llvm-objdump/llvm-objdump.cpp =================================================================== --- tools/llvm-objdump/llvm-objdump.cpp +++ tools/llvm-objdump/llvm-objdump.cpp @@ -463,21 +463,6 @@ return A.getOffset() < B.getOffset(); } -static std::string demangle(StringRef Name) { - char *Demangled = nullptr; - if (Name.startswith("_Z")) - Demangled = itaniumDemangle(Name.data(), Demangled, nullptr, nullptr); - else if (Name.startswith("?")) - Demangled = microsoftDemangle(Name.data(), Demangled, nullptr, nullptr); - - if (!Demangled) - return Name; - - std::string Ret = Demangled; - free(Demangled); - return Ret; -} - template static std::error_code getRelocationValueString(const ELFObjectFile *Obj, const RelocationRef &RelRef, Index: unittests/Demangle/CMakeLists.txt =================================================================== --- unittests/Demangle/CMakeLists.txt +++ unittests/Demangle/CMakeLists.txt @@ -4,6 +4,7 @@ ) add_llvm_unittest(DemangleTests + DemangleTest.cpp ItaniumDemangleTest.cpp PartialDemangleTest.cpp ) Index: unittests/Demangle/DemangleTest.cpp =================================================================== --- unittests/Demangle/DemangleTest.cpp +++ unittests/Demangle/DemangleTest.cpp @@ -0,0 +1,19 @@ +//===-- DemangleTest.cpp --------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Demangle/Demangle.h" +#include "gmock/gmock.h" + +using namespace llvm; + +TEST(Demangle, demangleTest) { + EXPECT_EQ(demangle("_Z3fooi"), "foo(int)"); + EXPECT_EQ(demangle("?foo@@YAXH@Z"), "void __cdecl foo(int)"); + EXPECT_EQ(demangle("foo"), "foo"); +}