Index: lld/COFF/CMakeLists.txt =================================================================== --- lld/COFF/CMakeLists.txt +++ lld/COFF/CMakeLists.txt @@ -18,7 +18,6 @@ MarkLive.cpp MinGW.cpp PDB.cpp - Strings.cpp SymbolTable.cpp Symbols.cpp Writer.cpp Index: lld/COFF/Symbols.cpp =================================================================== --- lld/COFF/Symbols.cpp +++ lld/COFF/Symbols.cpp @@ -10,8 +10,8 @@ #include "Symbols.h" #include "InputFiles.h" #include "Memory.h" -#include "Strings.h" #include "lld/Common/ErrorHandler.h" +#include "lld/Common/Strings.h" #include "llvm/ADT/STLExtras.h" #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" @@ -21,7 +21,7 @@ // Returns a symbol name for an error message. std::string lld::toString(coff::Symbol &B) { - if (Optional S = coff::demangle(B.getName())) + if (Optional S = demangleMSVC(B.getName())) return ("\"" + *S + "\" (" + B.getName() + ")").str(); return B.getName(); } Index: lld/Common/CMakeLists.txt =================================================================== --- lld/Common/CMakeLists.txt +++ lld/Common/CMakeLists.txt @@ -5,6 +5,7 @@ add_lld_library(lldCommon ErrorHandler.cpp Reproduce.cpp + Strings.cpp TargetOptionsCommandFlags.cpp Threads.cpp Version.cpp Index: lld/Common/Strings.cpp =================================================================== --- lld/Common/Strings.cpp +++ lld/Common/Strings.cpp @@ -7,20 +7,38 @@ // //===----------------------------------------------------------------------===// -#include "Strings.h" -#include +#include "lld/Common/Strings.h" +#include "llvm/Demangle/Demangle.h" #if defined(_MSC_VER) -#include #include +#include +#include #pragma comment(lib, "dbghelp.lib") #endif -using namespace lld; -using namespace lld::coff; using namespace llvm; +using namespace lld; + +// Returns the demangled C++ symbol name for Name. +Optional lld::demangleItanium(StringRef Name) { + // itaniumDemangle can be used to demangle strings other than symbol + // names which do not necessarily start with "_Z". Name can be + // either a C or C++ symbol. Don't call itaniumDemangle if the name + // does not look like a C++ symbol name to avoid getting unexpected + // result for a C symbol that happens to match a mangled type name. + if (!Name.startswith("_Z")) + return None; + + char *Buf = itaniumDemangle(Name.str().c_str(), nullptr, nullptr, nullptr); + if (!Buf) + return None; + std::string S(Buf); + free(Buf); + return S; +} -Optional coff::demangle(StringRef S) { +Optional lld::demangleMSVC(StringRef S) { #if defined(_MSC_VER) // UnDecorateSymbolName is not thread-safe, so we need a mutex. static std::mutex Mu; Index: lld/ELF/Strings.h =================================================================== --- lld/ELF/Strings.h +++ lld/ELF/Strings.h @@ -66,10 +66,6 @@ std::vector Patterns; }; -// Returns a demangled C++ symbol name. If Name is not a mangled -// name, it returns Optional::None. -llvm::Optional demangle(StringRef Name); - inline ArrayRef toArrayRef(StringRef S) { return {(const uint8_t *)S.data(), S.size()}; } Index: lld/ELF/Strings.cpp =================================================================== --- lld/ELF/Strings.cpp +++ lld/ELF/Strings.cpp @@ -60,21 +60,3 @@ std::all_of(S.begin() + 1, S.end(), [](char C) { return C == '_' || isAlnum(C); }); } - -// Returns the demangled C++ symbol name for Name. -Optional elf::demangle(StringRef Name) { - // itaniumDemangle can be used to demangle strings other than symbol - // names which do not necessarily start with "_Z". Name can be - // either a C or C++ symbol. Don't call itaniumDemangle if the name - // does not look like a C++ symbol name to avoid getting unexpected - // result for a C symbol that happens to match a mangled type name. - if (!Name.startswith("_Z")) - return None; - - char *Buf = itaniumDemangle(Name.str().c_str(), nullptr, nullptr, nullptr); - if (!Buf) - return None; - std::string S(Buf); - free(Buf); - return S; -} Index: lld/ELF/SymbolTable.cpp =================================================================== --- lld/ELF/SymbolTable.cpp +++ lld/ELF/SymbolTable.cpp @@ -21,6 +21,7 @@ #include "Symbols.h" #include "SyntheticSections.h" #include "lld/Common/ErrorHandler.h" +#include "lld/Common/Strings.h" #include "llvm/ADT/STLExtras.h" using namespace llvm; @@ -632,7 +633,7 @@ for (Symbol *Sym : SymVector) { if (!Sym->isDefined()) continue; - if (Optional S = demangle(Sym->getName())) + if (Optional S = demangleItanium(Sym->getName())) (*DemangledSyms)[*S].push_back(Sym); else (*DemangledSyms)[Sym->getName()].push_back(Sym); Index: lld/ELF/Symbols.cpp =================================================================== --- lld/ELF/Symbols.cpp +++ lld/ELF/Symbols.cpp @@ -11,12 +11,12 @@ #include "InputFiles.h" #include "InputSection.h" #include "OutputSections.h" -#include "Strings.h" #include "SyntheticSections.h" #include "Target.h" #include "Writer.h" #include "lld/Common/ErrorHandler.h" +#include "lld/Common/Strings.h" #include "llvm/ADT/STLExtras.h" #include "llvm/Support/Path.h" #include @@ -315,7 +315,7 @@ // Returns a symbol for an error message. std::string lld::toString(const Symbol &B) { if (Config->Demangle) - if (Optional S = demangle(B.getName())) + if (Optional S = demangleItanium(B.getName())) return *S; return B.getName(); } Index: lld/include/lld/Common/Strings.h =================================================================== --- lld/include/lld/Common/Strings.h +++ lld/include/lld/Common/Strings.h @@ -7,17 +7,18 @@ // //===----------------------------------------------------------------------===// -#ifndef LLD_COFF_STRINGS_H -#define LLD_COFF_STRINGS_H +#ifndef LLD_STRINGS_H +#define LLD_STRINGS_H #include "llvm/ADT/Optional.h" #include "llvm/ADT/StringRef.h" #include namespace lld { -namespace coff { -llvm::Optional demangle(llvm::StringRef S); -} +// Returns a demangled C++ symbol name. If Name is not a mangled +// name, it returns Optional::None. +llvm::Optional demangleItanium(llvm::StringRef Name); +llvm::Optional demangleMSVC(llvm::StringRef Name); } #endif Index: lld/wasm/Strings.h =================================================================== --- lld/wasm/Strings.h +++ lld/wasm/Strings.h @@ -17,10 +17,6 @@ namespace lld { namespace wasm { -// Returns a demangled C++ symbol name. If Name is not a mangled -// name, it returns Optional::None. -llvm::Optional demangle(llvm::StringRef Name); - std::string displayName(llvm::StringRef Name); } // namespace wasm Index: lld/wasm/Strings.cpp =================================================================== --- lld/wasm/Strings.cpp +++ lld/wasm/Strings.cpp @@ -9,32 +9,14 @@ #include "Strings.h" #include "Config.h" +#include "lld/Common/Strings.h" #include "llvm/ADT/StringRef.h" -#include "llvm/Demangle/Demangle.h" using namespace llvm; -// Returns the demangled C++ symbol name for Name. -Optional lld::wasm::demangle(StringRef Name) { - // itaniumDemangle can be used to demangle strings other than symbol - // names which do not necessarily start with "_Z". Name can be - // either a C or C++ symbol. Don't call itaniumDemangle if the name - // does not look like a C++ symbol name to avoid getting unexpected - // result for a C symbol that happens to match a mangled type name. - if (!Name.startswith("_Z")) - return None; - - char *Buf = itaniumDemangle(Name.str().c_str(), nullptr, nullptr, nullptr); - if (!Buf) - return None; - std::string S(Buf); - free(Buf); - return S; -} - std::string lld::wasm::displayName(StringRef Name) { if (Config->Demangle) - if (Optional S = demangle(Name)) + if (Optional S = demangleItanium(Name)) return "`" + *S + "'"; return Name; }