Factor out common code to Common/Strings.cpp.

Differential Revision: https://reviews.llvm.org/D40530

llvm-svn: 319138
diff --git a/lld/ELF/Strings.cpp b/lld/ELF/Strings.cpp
index 909a306..0ef33a1 100644
--- a/lld/ELF/Strings.cpp
+++ b/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<std::string> 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;
-}
diff --git a/lld/ELF/Strings.h b/lld/ELF/Strings.h
index 3c7e744..5009df6 100644
--- a/lld/ELF/Strings.h
+++ b/lld/ELF/Strings.h
@@ -66,10 +66,6 @@
   std::vector<llvm::GlobPattern> Patterns;
 };
 
-// Returns a demangled C++ symbol name. If Name is not a mangled
-// name, it returns Optional::None.
-llvm::Optional<std::string> demangle(StringRef Name);
-
 inline ArrayRef<uint8_t> toArrayRef(StringRef S) {
   return {(const uint8_t *)S.data(), S.size()};
 }
diff --git a/lld/ELF/SymbolTable.cpp b/lld/ELF/SymbolTable.cpp
index 86c506a..08a6d2f 100644
--- a/lld/ELF/SymbolTable.cpp
+++ b/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;
@@ -631,7 +632,7 @@
     for (Symbol *Sym : SymVector) {
       if (!Sym->isDefined())
         continue;
-      if (Optional<std::string> S = demangle(Sym->getName()))
+      if (Optional<std::string> S = demangleItanium(Sym->getName()))
         (*DemangledSyms)[*S].push_back(Sym);
       else
         (*DemangledSyms)[Sym->getName()].push_back(Sym);
diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp
index 9822a2a..203551c 100644
--- a/lld/ELF/Symbols.cpp
+++ b/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 <cstring>
@@ -315,7 +315,7 @@
 // Returns a symbol for an error message.
 std::string lld::toString(const Symbol &B) {
   if (Config->Demangle)
-    if (Optional<std::string> S = demangle(B.getName()))
+    if (Optional<std::string> S = demangleItanium(B.getName()))
       return *S;
   return B.getName();
 }