diff --git a/lld/COFF/Symbols.cpp b/lld/COFF/Symbols.cpp --- a/lld/COFF/Symbols.cpp +++ b/lld/COFF/Symbols.cpp @@ -36,9 +36,9 @@ StringRef demangleInput = prefixless; if (config->machine == I386) demangleInput.consume_front("_"); - std::string demangled = demangle(demangleInput, true); + std::string demangled = demangle(std::string(demangleInput)); if (demangled != demangleInput) - return prefix + demangle(demangleInput, true); + return prefix + demangle(std::string(demangleInput)); return (prefix + prefixless).str(); } return std::string(symName); diff --git a/lld/ELF/SymbolTable.cpp b/lld/ELF/SymbolTable.cpp --- a/lld/ELF/SymbolTable.cpp +++ b/lld/ELF/SymbolTable.cpp @@ -21,6 +21,7 @@ #include "lld/Common/Memory.h" #include "lld/Common/Strings.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/Demangle/Demangle.h" using namespace llvm; using namespace llvm::object; @@ -145,13 +146,13 @@ StringRef name = sym->getName(); size_t pos = name.find('@'); if (pos == std::string::npos) - demangled = demangle(name, config->demangle); + demangled = demangle(std::string(name)); else if (pos + 1 == name.size() || name[pos + 1] == '@') - demangled = demangle(name.substr(0, pos), config->demangle); + demangled = demangle(std::string(name.substr(0, pos))); else - demangled = (demangle(name.substr(0, pos), config->demangle) + - name.substr(pos)) - .str(); + demangled = + (demangle(std::string(name.substr(0, pos))) + name.substr(pos)) + .str(); (*demangledSyms)[demangled].push_back(sym); } } diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp --- a/lld/ELF/Symbols.cpp +++ b/lld/ELF/Symbols.cpp @@ -15,7 +15,7 @@ #include "Target.h" #include "Writer.h" #include "lld/Common/ErrorHandler.h" -#include "lld/Common/Strings.h" +#include "llvm/Demangle/Demangle.h" #include "llvm/Support/Compiler.h" #include @@ -43,9 +43,16 @@ AssertSymbol(); } +// Returns a symbol for an error message. +static std::string maybeDemangleSymbol(StringRef symName) { + if (elf::config->demangle) + return demangle(std::string(symName)); + return std::string(symName); +} + std::string lld::toString(const elf::Symbol &sym) { StringRef name = sym.getName(); - std::string ret = demangle(name, config->demangle); + std::string ret = maybeDemangleSymbol(name); const char *suffix = sym.getVersionSuffix(); if (*suffix == '@') diff --git a/lld/MachO/SymbolTable.cpp b/lld/MachO/SymbolTable.cpp --- a/lld/MachO/SymbolTable.cpp +++ b/lld/MachO/SymbolTable.cpp @@ -15,6 +15,7 @@ #include "SyntheticSections.h" #include "lld/Common/ErrorHandler.h" #include "lld/Common/Memory.h" +#include "llvm/Demangle/Demangle.h" using namespace llvm; using namespace lld; diff --git a/lld/include/lld/Common/Strings.h b/lld/include/lld/Common/Strings.h --- a/lld/include/lld/Common/Strings.h +++ b/lld/include/lld/Common/Strings.h @@ -13,19 +13,11 @@ #include "llvm/ADT/Optional.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" -#include "llvm/Demangle/Demangle.h" #include "llvm/Support/GlobPattern.h" #include #include namespace lld { -// Returns a demangled symbol name. If Name is not a mangled name, it returns -// name. -inline std::string demangle(llvm::StringRef symName, bool shouldDemangle) { - if (shouldDemangle) - return llvm::demangle(symName.str().c_str()); - return std::string(symName); -} llvm::SmallVector parseHex(llvm::StringRef s); bool isValidCIdentifier(llvm::StringRef s); diff --git a/lld/wasm/Symbols.cpp b/lld/wasm/Symbols.cpp --- a/lld/wasm/Symbols.cpp +++ b/lld/wasm/Symbols.cpp @@ -15,7 +15,7 @@ #include "OutputSegment.h" #include "lld/Common/ErrorHandler.h" #include "lld/Common/Memory.h" -#include "lld/Common/Strings.h" +#include "llvm/Demangle/Demangle.h" #define DEBUG_TYPE "lld" @@ -34,8 +34,9 @@ // `main` in the case where we need to pass it arguments. if (name == "__main_argc_argv") return "main"; - - return demangle(name, config->demangle); + if (wasm::config->demangle) + return demangle(std::string(name)); + return std::string(name); } std::string toString(wasm::Symbol::Kind kind) {