[PATCH] D57350: [llvm-cxxfilt] Split and demangle stdin input

Matt Davis via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 7 14:02:31 PST 2019


mattd updated this revision to Diff 185865.
mattd marked an inline comment as done and an inline comment as not done.
mattd retitled this revision from "[llvm-cxxfilt] Split input and demangle each word." to "[llvm-cxxfilt] Split and demangle stdin input".
mattd edited the summary of this revision.
mattd added a comment.
Herald added a project: LLVM.

I've updated the patch to replicate GNU's c++filt behavior.  This patch now only splits strings that are passed via stdin.  It does not split strings that are passed as arguments to llvm-cxxfilt via command line.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D57350/new/

https://reviews.llvm.org/D57350

Files:
  llvm/test/tools/llvm-cxxfilt/simple.test
  llvm/test/tools/llvm-cxxfilt/types.test
  llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp


Index: llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
===================================================================
--- llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
+++ llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/Demangle/Demangle.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/InitLLVM.h"
@@ -51,7 +52,7 @@
 static cl::list<std::string>
 Decorated(cl::Positional, cl::desc("<mangled>"), cl::ZeroOrMore);
 
-static void demangle(llvm::raw_ostream &OS, const std::string &Mangled) {
+static std::string demangle(llvm::raw_ostream &OS, const std::string &Mangled) {
   int Status;
 
   const char *Decorated = Mangled.c_str();
@@ -72,10 +73,28 @@
     Undecorated = itaniumDemangle(Decorated + 6, nullptr, nullptr, &Status);
   }
 
-  OS << (Undecorated ? Undecorated : Mangled) << '\n';
-  OS.flush();
-
+  std::string Result(Undecorated ? Undecorated : Mangled);
   free(Undecorated);
+  return Result;
+}
+
+// If 'Split' is true, then 'Mangled' is broken into individual words and each
+// word is demangled.  Otherwise, the entire string is treated as a single
+// mangled item.  The result is output to 'OS'.
+static void demangleLine(llvm::raw_ostream &OS, StringRef Mangled, bool Split) {
+  std::string Result;
+  if (Split) {
+    SmallVector<StringRef, 16> Words;
+    SplitString(Mangled, Words);
+    for (auto Word : Words)
+      Result += demangle(OS, Word) + ' ';
+    // Remove the trailing space character.
+    if (Result.back() == ' ')
+      Result.pop_back();
+  } else
+    Result = demangle(OS, Mangled);
+  OS << Result << '\n';
+  OS.flush();
 }
 
 int main(int argc, char **argv) {
@@ -85,10 +104,10 @@
 
   if (Decorated.empty())
     for (std::string Mangled; std::getline(std::cin, Mangled);)
-      demangle(llvm::outs(), Mangled);
+      demangleLine(llvm::outs(), Mangled, true);
   else
     for (const auto &Symbol : Decorated)
-      demangle(llvm::outs(), Symbol);
+      demangleLine(llvm::outs(), Symbol, false);
 
   return EXIT_SUCCESS;
 }
Index: llvm/test/tools/llvm-cxxfilt/types.test
===================================================================
--- llvm/test/tools/llvm-cxxfilt/types.test
+++ llvm/test/tools/llvm-cxxfilt/types.test
@@ -1,5 +1,6 @@
 RUN: llvm-cxxfilt -t f i | FileCheck %s
+RUN: echo "f i" | llvm-cxxfilt -t | FileCheck %s --check-prefix="CHECK-STRING"
 
 CHECK: float
 CHECK-NEXT: int
-
+CHECK-STRING: float int
Index: llvm/test/tools/llvm-cxxfilt/simple.test
===================================================================
--- llvm/test/tools/llvm-cxxfilt/simple.test
+++ llvm/test/tools/llvm-cxxfilt/simple.test
@@ -1,4 +1,7 @@
 RUN: llvm-cxxfilt _Z1fi abc | FileCheck %s
+RUN: echo "Mangled _Z1fi and _Z3foov in string." | llvm-cxxfilt \
+RUN:      | FileCheck %s --check-prefix=CHECK-STRING
 
 CHECK: f(int)
 CHECK-NEXT: abc
+CHECK-STRING: Mangled f(int) and foo() in string.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D57350.185865.patch
Type: text/x-patch
Size: 3020 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190207/36c30a01/attachment.bin>


More information about the llvm-commits mailing list