[llvm] e84468c - [llvm-cxxfilt] Improve strip-underscore behavior

Steven Wu via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 18 15:06:04 PST 2019


Author: Steven Wu
Date: 2019-11-18T15:05:41-08:00
New Revision: e84468c1f145e9a5e54640ebdc78bc40c7da3fce

URL: https://github.com/llvm/llvm-project/commit/e84468c1f145e9a5e54640ebdc78bc40c7da3fce
DIFF: https://github.com/llvm/llvm-project/commit/e84468c1f145e9a5e54640ebdc78bc40c7da3fce.diff

LOG: [llvm-cxxfilt] Improve strip-underscore behavior

Summary:
For platform that uses macho format, c++filt should be stripping the
leading underscore by default. Introduce the binutil compatible "-n"
option to control strip-undercore behaivor together with the existing
"-_" option and fallback to system default if none of them are set.

rdar://problem/57173514

Reviewers: compnerd, erik.pilkington, dexonsmith, mattd

Reviewed By: compnerd, erik.pilkington

Subscribers: jkorous, ributzka, llvm-commits

Tags: #llvm

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

Added: 
    llvm/test/tools/llvm-cxxfilt/darwin.test

Modified: 
    llvm/test/tools/llvm-cxxfilt/abitag.test
    llvm/test/tools/llvm-cxxfilt/coff-import.test
    llvm/test/tools/llvm-cxxfilt/delimiters.test
    llvm/test/tools/llvm-cxxfilt/empty-line.test
    llvm/test/tools/llvm-cxxfilt/invalid.test
    llvm/test/tools/llvm-cxxfilt/noargs.test
    llvm/test/tools/llvm-cxxfilt/simple.test
    llvm/test/tools/llvm-cxxfilt/types.test
    llvm/test/tools/llvm-cxxfilt/underscore.test
    llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-cxxfilt/abitag.test b/llvm/test/tools/llvm-cxxfilt/abitag.test
index f4fabc87bae4..172062b8d658 100644
--- a/llvm/test/tools/llvm-cxxfilt/abitag.test
+++ b/llvm/test/tools/llvm-cxxfilt/abitag.test
@@ -1,4 +1,4 @@
-RUN: llvm-cxxfilt _Z14returns_stringB5cxx11v _Z6globalB5cxx11 _Z6globalB12a_longer_tag | FileCheck %s
+RUN: llvm-cxxfilt -n _Z14returns_stringB5cxx11v _Z6globalB5cxx11 _Z6globalB12a_longer_tag | FileCheck %s
 
 CHECK: returns_string[abi:cxx11]()
 CHECK-NEXT: global[abi:cxx11]

diff  --git a/llvm/test/tools/llvm-cxxfilt/coff-import.test b/llvm/test/tools/llvm-cxxfilt/coff-import.test
index 35494d7a8326..409fd88e595d 100644
--- a/llvm/test/tools/llvm-cxxfilt/coff-import.test
+++ b/llvm/test/tools/llvm-cxxfilt/coff-import.test
@@ -1,5 +1,5 @@
 RUN: llvm-cxxfilt -_ ___imp__ZSt6futureIvE | FileCheck %s
-RUN: llvm-cxxfilt __imp__ZSt6futureIvE | FileCheck %s
+RUN: llvm-cxxfilt -n __imp__ZSt6futureIvE | FileCheck %s
 
 CHECK: import thunk for std::future<void>
 

diff  --git a/llvm/test/tools/llvm-cxxfilt/darwin.test b/llvm/test/tools/llvm-cxxfilt/darwin.test
new file mode 100644
index 000000000000..761a23d54c3f
--- /dev/null
+++ b/llvm/test/tools/llvm-cxxfilt/darwin.test
@@ -0,0 +1,4 @@
+REQUIRES: system-darwin
+
+RUN: llvm-cxxfilt __Z1fv | FileCheck %s
+CHECK: f()

diff  --git a/llvm/test/tools/llvm-cxxfilt/delimiters.test b/llvm/test/tools/llvm-cxxfilt/delimiters.test
index e70cc309cfb4..1c5b0e352a74 100644
--- a/llvm/test/tools/llvm-cxxfilt/delimiters.test
+++ b/llvm/test/tools/llvm-cxxfilt/delimiters.test
@@ -28,7 +28,7 @@ RUN:      '_Z3Foo|'   \
 RUN:      '_Z3Foo}'   \
 RUN:      '_Z3Foo~,,' \
 RUN:      '_Z3Foo,,_Z3Bar::_Z3Baz  _Z3Foo,_Z3Bar:_Z3Baz' \
-RUN:      '_Z3Foo$ ._Z3Foo' | llvm-cxxfilt | FileCheck %s
+RUN:      '_Z3Foo$ ._Z3Foo' | llvm-cxxfilt -n | FileCheck %s
 
 CHECK: ,,Foo!
 CHECK: Foo"

diff  --git a/llvm/test/tools/llvm-cxxfilt/empty-line.test b/llvm/test/tools/llvm-cxxfilt/empty-line.test
index 1b62ee0812f0..80c72208aa96 100644
--- a/llvm/test/tools/llvm-cxxfilt/empty-line.test
+++ b/llvm/test/tools/llvm-cxxfilt/empty-line.test
@@ -2,7 +2,7 @@
 RUN: echo "" > %t
 RUN: echo "_Z3foov" >> %t
 RUN: echo "" >> %t
-RUN: llvm-cxxfilt < %t | FileCheck %s
+RUN: llvm-cxxfilt -n < %t | FileCheck %s
 
 CHECK:       {{^$}}
 CHECK-NEXT:  foo()

diff  --git a/llvm/test/tools/llvm-cxxfilt/invalid.test b/llvm/test/tools/llvm-cxxfilt/invalid.test
index 10f3b2e81323..13866f3fb90e 100644
--- a/llvm/test/tools/llvm-cxxfilt/invalid.test
+++ b/llvm/test/tools/llvm-cxxfilt/invalid.test
@@ -1,4 +1,4 @@
-RUN: llvm-cxxfilt _Z1fi __Z1fi f ___ZSt1ff_block_invoke | FileCheck %s
+RUN: llvm-cxxfilt -n _Z1fi __Z1fi f ___ZSt1ff_block_invoke | FileCheck %s
 
 CHECK: f(int)
 CHECK-NEXT: __Z1fi

diff  --git a/llvm/test/tools/llvm-cxxfilt/noargs.test b/llvm/test/tools/llvm-cxxfilt/noargs.test
index 71262d20059c..47eac84cbe8e 100644
--- a/llvm/test/tools/llvm-cxxfilt/noargs.test
+++ b/llvm/test/tools/llvm-cxxfilt/noargs.test
@@ -1,4 +1,4 @@
-RUN: sed -n 's/^STDIN: //p' %s | llvm-cxxfilt | FileCheck %s
+RUN: sed -n 's/^STDIN: //p' %s | llvm-cxxfilt -n | FileCheck %s
 
 STDIN: _Znw
 STDIN: _Znwj

diff  --git a/llvm/test/tools/llvm-cxxfilt/simple.test b/llvm/test/tools/llvm-cxxfilt/simple.test
index 90dd18d58dcb..674f4f599b7e 100644
--- a/llvm/test/tools/llvm-cxxfilt/simple.test
+++ b/llvm/test/tools/llvm-cxxfilt/simple.test
@@ -1,7 +1,7 @@
-RUN: llvm-cxxfilt _Z1fi abc | FileCheck %s
-RUN: echo "Mangled _Z1fi and _Z3foov in string." | llvm-cxxfilt \
+RUN: llvm-cxxfilt -n _Z1fi abc | FileCheck %s
+RUN: echo "Mangled _Z1fi and _Z3foov in string." | llvm-cxxfilt -n \
 RUN:   | FileCheck %s --check-prefix=CHECK-STRING
-RUN: llvm-cxxfilt "CLI remains mangled _Z1fi" \
+RUN: llvm-cxxfilt -n "CLI remains mangled _Z1fi" \
 RUN:   | FileCheck %s --check-prefix=CHECK-MANGLED
 
 CHECK: f(int)

diff  --git a/llvm/test/tools/llvm-cxxfilt/types.test b/llvm/test/tools/llvm-cxxfilt/types.test
index 2213c386f82e..d99b6b156e89 100644
--- a/llvm/test/tools/llvm-cxxfilt/types.test
+++ b/llvm/test/tools/llvm-cxxfilt/types.test
@@ -1,4 +1,4 @@
-RUN: llvm-cxxfilt -t f i | FileCheck %s
+RUN: llvm-cxxfilt -n -t f i | FileCheck %s
 RUN: echo "f i" | llvm-cxxfilt -t | FileCheck %s --check-prefix="CHECK-STRING"
 
 CHECK: float

diff  --git a/llvm/test/tools/llvm-cxxfilt/underscore.test b/llvm/test/tools/llvm-cxxfilt/underscore.test
index 4a4ce898a9c3..f5b85110c62e 100644
--- a/llvm/test/tools/llvm-cxxfilt/underscore.test
+++ b/llvm/test/tools/llvm-cxxfilt/underscore.test
@@ -1,5 +1,5 @@
 RUN: llvm-cxxfilt -_ __ZN2ns1fE _ZSt1f _f | FileCheck %s -check-prefix CHECK-STRIPPED
-RUN: llvm-cxxfilt __ZN2ns1fE _ZSt1f _f | FileCheck %s -check-prefix CHECK-UNSTRIPPED
+RUN: llvm-cxxfilt -n __ZN2ns1fE _ZSt1f _f | FileCheck %s -check-prefix CHECK-UNSTRIPPED
 
 CHECK-STRIPPED: ns::f
 CHECK-STRIPPED: _ZSt1f

diff  --git a/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp b/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
index 9ac8bcf0ff01..75d6985133a1 100644
--- a/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
+++ b/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
@@ -7,8 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/Triple.h"
 #include "llvm/Demangle/Demangle.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Host.h"
 #include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/raw_ostream.h"
 #include <cstdlib>
@@ -41,6 +43,13 @@ static cl::opt<bool> StripUnderscore("strip-underscore",
 static cl::alias StripUnderscoreShort("_",
                                       cl::desc("alias for --strip-underscore"),
                                       cl::aliasopt(StripUnderscore));
+static cl::opt<bool>
+    NoStripUnderscore("no-strip-underscore",
+                      cl::desc("do not strip the leading underscore"),
+                      cl::init(false));
+static cl::alias
+    NoStripUnderscoreShort("n", cl::desc("alias for --no-strip-underscore"),
+                           cl::aliasopt(NoStripUnderscore));
 
 static cl::opt<bool>
     Types("types",
@@ -55,11 +64,21 @@ Decorated(cl::Positional, cl::desc("<mangled>"), cl::ZeroOrMore);
 static cl::extrahelp
     HelpResponse("\nPass @FILE as argument to read options from FILE.\n");
 
+static bool shouldStripUnderscore() {
+  if (StripUnderscore)
+    return true;
+  if (NoStripUnderscore)
+    return false;
+  // If none of them are set, use the default value for platform.
+  // macho has symbols prefix with "_" so strip by default.
+  return Triple(sys::getProcessTriple()).isOSBinFormatMachO();
+}
+
 static std::string demangle(llvm::raw_ostream &OS, const std::string &Mangled) {
   int Status;
 
   const char *DecoratedStr = Mangled.c_str();
-  if (StripUnderscore)
+  if (shouldStripUnderscore())
     if (DecoratedStr[0] == '_')
       ++DecoratedStr;
   size_t DecoratedLength = strlen(DecoratedStr);


        


More information about the llvm-commits mailing list