[llvm] r292759 - llvm-cxxfilt: support `-_`

Saleem Abdulrasool via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 22 09:41:11 PST 2017


Author: compnerd
Date: Sun Jan 22 11:41:10 2017
New Revision: 292759

URL: http://llvm.org/viewvc/llvm-project?rev=292759&view=rev
Log:
llvm-cxxfilt: support `-_`

Add the `--strip-underscore` option to llvm-cxxfilt to strip the leading
underscore.  This is useful for when dealing with targets which add a
leading underscore.

Added:
    llvm/trunk/test/tools/llvm-cxxfilt/underscore.test
Modified:
    llvm/trunk/tools/llvm-cxxfilt/llvm-cxxfilt.cpp

Added: llvm/trunk/test/tools/llvm-cxxfilt/underscore.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-cxxfilt/underscore.test?rev=292759&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-cxxfilt/underscore.test (added)
+++ llvm/trunk/test/tools/llvm-cxxfilt/underscore.test Sun Jan 22 11:41:10 2017
@@ -0,0 +1,11 @@
+RUN: llvm-cxxfilt -_ __ZN2ns1fE _ZSt1f _f | FileCheck %s -check-prefix CHECK-STRIPPED
+RUN: llvm-cxxfilt __ZN2ns1fE _ZSt1f _f | FileCheck %s -check-prefix CHECK-UNSTRIPPED
+
+CHECK-STRIPPED: ns::f
+CHECK-STRIPPED: _ZSt1f
+CHECK-STRIPPED: _f
+
+CHECK-UNSTRIPPED: __ZN2ns1fE
+CHECK-UNSTRIPPED: std::f
+CHECK-UNSTRIPPED: _f
+

Modified: llvm/trunk/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-cxxfilt/llvm-cxxfilt.cpp?rev=292759&r1=292758&r2=292759&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-cxxfilt/llvm-cxxfilt.cpp (original)
+++ llvm/trunk/tools/llvm-cxxfilt/llvm-cxxfilt.cpp Sun Jan 22 11:41:10 2017
@@ -36,6 +36,13 @@ static cl::opt<Style>
 static cl::alias FormatShort("s", cl::desc("alias for --format"),
                              cl::aliasopt(Format));
 
+static cl::opt<bool> StripUnderscore("strip-underscore",
+                                     cl::desc("strip the leading underscore"),
+                                     cl::init(false));
+static cl::alias StripUnderscoreShort("_",
+                                      cl::desc("alias for --strip-underscore"),
+                                      cl::aliasopt(StripUnderscore));
+
 static cl::opt<bool>
     Types("types",
           cl::desc("attempt to demangle types as well as function names"),
@@ -48,12 +55,22 @@ Decorated(cl::Positional, cl::desc("<man
 
 static void demangle(llvm::raw_ostream &OS, const std::string &Mangled) {
   int Status;
-  char *Demangled = nullptr;
-  if (Types || ((Mangled.size() >= 2 && Mangled.compare(0, 2, "_Z")) ||
-                (Mangled.size() >= 4 && Mangled.compare(0, 4, "___Z"))))
-    Demangled = itaniumDemangle(Mangled.c_str(), nullptr, nullptr, &Status);
-  OS << (Demangled ? Demangled : Mangled) << '\n';
-  free(Demangled);
+
+  const char *Decorated = Mangled.c_str();
+  if (StripUnderscore)
+    if (Decorated[0] == '_')
+      ++Decorated;
+  size_t DecoratedLength = strlen(Decorated);
+
+  char *Undecorated = nullptr;
+
+  if (Types || ((DecoratedLength >= 2 && strncmp(Decorated, "_Z", 2) == 0) ||
+                (DecoratedLength >= 4 && strncmp(Decorated, "___Z", 4) == 0)))
+    Undecorated = itaniumDemangle(Decorated, nullptr, nullptr, &Status);
+
+  OS << (Undecorated ? Undecorated : Mangled) << '\n';
+
+  free(Undecorated);
 }
 
 int main(int argc, char **argv) {




More information about the llvm-commits mailing list