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.

llvm-svn: 292759
diff --git a/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp b/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
index 8f90bcf..8be2a6d 100644
--- a/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
+++ b/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
@@ -36,6 +36,13 @@
 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 @@
 
 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) {