[lld] r274804 - Move demangle() from Symbols.cpp to Strings.cpp.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 7 16:04:16 PDT 2016


Author: ruiu
Date: Thu Jul  7 18:04:15 2016
New Revision: 274804

URL: http://llvm.org/viewvc/llvm-project?rev=274804&view=rev
Log:
Move demangle() from Symbols.cpp to Strings.cpp.

Symbols.cpp contains functions to handle ELF symbols.
demangle() function is essentially a function to work on a
string rather than on an ELF symbol. So Strings.cpp is a
better place to put that function.

This change also make demangle to demangle symbols unconditionally.
Previously, it demangled symbols only when Config->Demangle is true.

Modified:
    lld/trunk/ELF/Strings.cpp
    lld/trunk/ELF/Strings.h
    lld/trunk/ELF/SymbolTable.cpp
    lld/trunk/ELF/Symbols.cpp
    lld/trunk/ELF/Symbols.h

Modified: lld/trunk/ELF/Strings.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Strings.cpp?rev=274804&r1=274803&r2=274804&view=diff
==============================================================================
--- lld/trunk/ELF/Strings.cpp (original)
+++ lld/trunk/ELF/Strings.cpp Thu Jul  7 18:04:15 2016
@@ -11,8 +11,13 @@
 #include "Error.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/Twine.h"
+#include "llvm/Config/config.h"
 #include <algorithm>
 
+#ifdef HAVE_CXXABI_H
+#include <cxxabi.h>
+#endif
+
 using namespace llvm;
 using namespace lld;
 using namespace lld::elf;
@@ -68,3 +73,26 @@ bool elf::isValidCIdentifier(StringRef S
   return !S.empty() && isAlpha(S[0]) &&
          std::all_of(S.begin() + 1, S.end(), isAlnum);
 }
+
+// Returns the demangled C++ symbol name for Name.
+std::string elf::demangle(StringRef Name) {
+#if !defined(HAVE_CXXABI_H)
+  return Name;
+#else
+  // __cxa_demangle 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 __cxa_demangle 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 Name;
+
+  char *Buf =
+      abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
+  if (!Buf)
+    return Name;
+  std::string S(Buf);
+  free(Buf);
+  return S;
+#endif
+}

Modified: lld/trunk/ELF/Strings.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Strings.h?rev=274804&r1=274803&r2=274804&view=diff
==============================================================================
--- lld/trunk/ELF/Strings.h (original)
+++ lld/trunk/ELF/Strings.h Thu Jul  7 18:04:15 2016
@@ -18,6 +18,11 @@ namespace elf {
 bool globMatch(StringRef S, StringRef T);
 std::vector<uint8_t> parseHex(StringRef S);
 bool isValidCIdentifier(StringRef S);
+
+// Returns a demangled C++ symbol name. If Name is not a mangled
+// name or the system does not provide __cxa_demangle function,
+// it returns an unmodified string.
+std::string demangle(StringRef Name);
 }
 }
 

Modified: lld/trunk/ELF/SymbolTable.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SymbolTable.cpp?rev=274804&r1=274803&r2=274804&view=diff
==============================================================================
--- lld/trunk/ELF/SymbolTable.cpp (original)
+++ lld/trunk/ELF/SymbolTable.cpp Thu Jul  7 18:04:15 2016
@@ -242,9 +242,11 @@ SymbolTable<ELFT>::insert(StringRef Name
 template <typename ELFT>
 std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Existing,
                                            InputFile *NewFile) {
-  StringRef Sym = Existing->getName();
-  return demangle(Sym) + " in " + getFilename(Existing->getSourceFile<ELFT>()) +
-         " and " + getFilename(NewFile);
+  std::string Sym = Existing->getName();
+  if (Config->Demangle)
+    Sym = demangle(Sym);
+  return Sym + " in " + getFilename(Existing->getSourceFile<ELFT>()) + " and " +
+         getFilename(NewFile);
 }
 
 template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {

Modified: lld/trunk/ELF/Symbols.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Symbols.cpp?rev=274804&r1=274803&r2=274804&view=diff
==============================================================================
--- lld/trunk/ELF/Symbols.cpp (original)
+++ lld/trunk/ELF/Symbols.cpp Thu Jul  7 18:04:15 2016
@@ -15,11 +15,6 @@
 #include "Target.h"
 
 #include "llvm/ADT/STLExtras.h"
-#include "llvm/Config/config.h"
-
-#ifdef HAVE_CXXABI_H
-#include <cxxabi.h>
-#endif
 
 using namespace llvm;
 using namespace llvm::object;
@@ -249,32 +244,6 @@ std::unique_ptr<InputFile> LazyObject::g
   return createObjectFile(MBRef);
 }
 
-// Returns the demangled C++ symbol name for Name.
-std::string elf::demangle(StringRef Name) {
-#if !defined(HAVE_CXXABI_H)
-  return Name;
-#else
-  if (!Config->Demangle)
-    return Name;
-
-  // __cxa_demangle 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 __cxa_demangle 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 Name;
-
-  char *Buf =
-      abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
-  if (!Buf)
-    return Name;
-  std::string S(Buf);
-  free(Buf);
-  return S;
-#endif
-}
-
 bool Symbol::includeInDynsym() const {
   if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
     return false;

Modified: lld/trunk/ELF/Symbols.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Symbols.h?rev=274804&r1=274803&r2=274804&view=diff
==============================================================================
--- lld/trunk/ELF/Symbols.h (original)
+++ lld/trunk/ELF/Symbols.h Thu Jul  7 18:04:15 2016
@@ -36,11 +36,6 @@ template <class ELFT> class OutputSectio
 template <class ELFT> class OutputSectionBase;
 template <class ELFT> class SharedFile;
 
-// Returns a demangled C++ symbol name. If Name is not a mangled
-// name or the system does not provide __cxa_demangle function,
-// it returns the unmodified string.
-std::string demangle(StringRef Name);
-
 struct Symbol;
 
 // The base class for real symbol classes.




More information about the llvm-commits mailing list