]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r306956, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-cxxfilt / llvm-cxxfilt.cpp
1 //===-- llvm-c++filt.cpp --------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Demangle/Demangle.h"
11 #include "llvm/Support/CommandLine.h"
12 #include "llvm/Support/PrettyStackTrace.h"
13 #include "llvm/Support/Signals.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include <cstdlib>
16 #include <iostream>
17
18 using namespace llvm;
19
20 enum Style {
21   Auto,  ///< auto-detect mangling
22   GNU,   ///< GNU
23   Lucid, ///< Lucid compiler (lcc)
24   ARM,
25   HP,    ///< HP compiler (xCC)
26   EDG,   ///< EDG compiler
27   GNUv3, ///< GNU C++ v3 ABI
28   Java,  ///< Java (gcj)
29   GNAT   ///< ADA copiler (gnat)
30 };
31 static cl::opt<Style>
32     Format("format", cl::desc("decoration style"),
33            cl::values(clEnumValN(Auto, "auto", "auto-detect style"),
34                       clEnumValN(GNU, "gnu", "GNU (itanium) style")),
35            cl::init(Auto));
36 static cl::alias FormatShort("s", cl::desc("alias for --format"),
37                              cl::aliasopt(Format));
38
39 static cl::opt<bool> StripUnderscore("strip-underscore",
40                                      cl::desc("strip the leading underscore"),
41                                      cl::init(false));
42 static cl::alias StripUnderscoreShort("_",
43                                       cl::desc("alias for --strip-underscore"),
44                                       cl::aliasopt(StripUnderscore));
45
46 static cl::opt<bool>
47     Types("types",
48           cl::desc("attempt to demangle types as well as function names"),
49           cl::init(false));
50 static cl::alias TypesShort("t", cl::desc("alias for --types"),
51                             cl::aliasopt(Types));
52
53 static cl::list<std::string>
54 Decorated(cl::Positional, cl::desc("<mangled>"), cl::ZeroOrMore);
55
56 static void demangle(llvm::raw_ostream &OS, const std::string &Mangled) {
57   int Status;
58
59   const char *Decorated = Mangled.c_str();
60   if (StripUnderscore)
61     if (Decorated[0] == '_')
62       ++Decorated;
63   size_t DecoratedLength = strlen(Decorated);
64
65   char *Undecorated = nullptr;
66
67   if (Types || ((DecoratedLength >= 2 && strncmp(Decorated, "_Z", 2) == 0) ||
68                 (DecoratedLength >= 4 && strncmp(Decorated, "___Z", 4) == 0)))
69     Undecorated = itaniumDemangle(Decorated, nullptr, nullptr, &Status);
70
71   if (!Undecorated &&
72       (DecoratedLength > 6 && strncmp(Decorated, "__imp_", 6) == 0)) {
73     OS << "import thunk for ";
74     Undecorated = itaniumDemangle(Decorated + 6, nullptr, nullptr, &Status);
75   }
76
77   OS << (Undecorated ? Undecorated : Mangled) << '\n';
78
79   free(Undecorated);
80 }
81
82 int main(int argc, char **argv) {
83   sys::PrintStackTraceOnErrorSignal(argv[0]);
84   PrettyStackTraceProgram X(argc, argv);
85
86   cl::ParseCommandLineOptions(argc, argv, "llvm symbol undecoration tool\n");
87
88   if (Decorated.empty())
89     for (std::string Mangled; std::getline(std::cin, Mangled);)
90       demangle(llvm::outs(), Mangled);
91   else
92     for (const auto &Symbol : Decorated)
93       demangle(llvm::outs(), Symbol);
94
95   return EXIT_SUCCESS;
96 }