Rafael Espindola | ec44cfd | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 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" |
Saleem Abdulrasool | 81cf2fa | 2017-01-20 04:25:26 +0000 | [diff] [blame] | 11 | #include "llvm/Support/CommandLine.h" |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 12 | #include "llvm/Support/InitLLVM.h" |
Davide Italiano | d370d4d | 2016-09-27 18:50:30 +0000 | [diff] [blame] | 13 | #include "llvm/Support/raw_ostream.h" |
Saleem Abdulrasool | a5627e5 | 2016-11-13 20:43:38 +0000 | [diff] [blame] | 14 | #include <cstdlib> |
| 15 | #include <iostream> |
Rafael Espindola | ec44cfd | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace llvm; |
| 18 | |
Saleem Abdulrasool | 5e8f180 | 2017-01-21 02:36:26 +0000 | [diff] [blame] | 19 | enum Style { |
| 20 | Auto, ///< auto-detect mangling |
| 21 | GNU, ///< GNU |
| 22 | Lucid, ///< Lucid compiler (lcc) |
| 23 | ARM, |
| 24 | HP, ///< HP compiler (xCC) |
| 25 | EDG, ///< EDG compiler |
| 26 | GNUv3, ///< GNU C++ v3 ABI |
| 27 | Java, ///< Java (gcj) |
| 28 | GNAT ///< ADA copiler (gnat) |
| 29 | }; |
| 30 | static cl::opt<Style> |
| 31 | Format("format", cl::desc("decoration style"), |
| 32 | cl::values(clEnumValN(Auto, "auto", "auto-detect style"), |
| 33 | clEnumValN(GNU, "gnu", "GNU (itanium) style")), |
| 34 | cl::init(Auto)); |
| 35 | static cl::alias FormatShort("s", cl::desc("alias for --format"), |
| 36 | cl::aliasopt(Format)); |
| 37 | |
Saleem Abdulrasool | 4133d85 | 2017-01-22 17:41:10 +0000 | [diff] [blame] | 38 | static cl::opt<bool> StripUnderscore("strip-underscore", |
| 39 | cl::desc("strip the leading underscore"), |
| 40 | cl::init(false)); |
| 41 | static cl::alias StripUnderscoreShort("_", |
| 42 | cl::desc("alias for --strip-underscore"), |
| 43 | cl::aliasopt(StripUnderscore)); |
| 44 | |
Saleem Abdulrasool | 81cf2fa | 2017-01-20 04:25:26 +0000 | [diff] [blame] | 45 | static cl::opt<bool> |
| 46 | Types("types", |
| 47 | cl::desc("attempt to demangle types as well as function names"), |
| 48 | cl::init(false)); |
| 49 | static cl::alias TypesShort("t", cl::desc("alias for --types"), |
| 50 | cl::aliasopt(Types)); |
| 51 | |
| 52 | static cl::list<std::string> |
| 53 | Decorated(cl::Positional, cl::desc("<mangled>"), cl::ZeroOrMore); |
| 54 | |
Saleem Abdulrasool | 08b4904 | 2017-01-19 02:58:46 +0000 | [diff] [blame] | 55 | static void demangle(llvm::raw_ostream &OS, const std::string &Mangled) { |
Saleem Abdulrasool | a5627e5 | 2016-11-13 20:43:38 +0000 | [diff] [blame] | 56 | int Status; |
Saleem Abdulrasool | 4133d85 | 2017-01-22 17:41:10 +0000 | [diff] [blame] | 57 | |
| 58 | const char *Decorated = Mangled.c_str(); |
| 59 | if (StripUnderscore) |
| 60 | if (Decorated[0] == '_') |
| 61 | ++Decorated; |
| 62 | size_t DecoratedLength = strlen(Decorated); |
| 63 | |
| 64 | char *Undecorated = nullptr; |
| 65 | |
| 66 | if (Types || ((DecoratedLength >= 2 && strncmp(Decorated, "_Z", 2) == 0) || |
| 67 | (DecoratedLength >= 4 && strncmp(Decorated, "___Z", 4) == 0))) |
| 68 | Undecorated = itaniumDemangle(Decorated, nullptr, nullptr, &Status); |
| 69 | |
Saleem Abdulrasool | 0010ede | 2017-03-22 21:15:19 +0000 | [diff] [blame] | 70 | if (!Undecorated && |
| 71 | (DecoratedLength > 6 && strncmp(Decorated, "__imp_", 6) == 0)) { |
| 72 | OS << "import thunk for "; |
| 73 | Undecorated = itaniumDemangle(Decorated + 6, nullptr, nullptr, &Status); |
| 74 | } |
| 75 | |
Saleem Abdulrasool | 4133d85 | 2017-01-22 17:41:10 +0000 | [diff] [blame] | 76 | OS << (Undecorated ? Undecorated : Mangled) << '\n'; |
Adam Nemet | d3ee065 | 2017-11-29 17:07:41 +0000 | [diff] [blame] | 77 | OS.flush(); |
Saleem Abdulrasool | 4133d85 | 2017-01-22 17:41:10 +0000 | [diff] [blame] | 78 | |
| 79 | free(Undecorated); |
Saleem Abdulrasool | a5627e5 | 2016-11-13 20:43:38 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Rafael Espindola | ec44cfd | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 82 | int main(int argc, char **argv) { |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 83 | InitLLVM X(argc, argv); |
Saleem Abdulrasool | 08015a5 | 2017-01-20 05:27:09 +0000 | [diff] [blame] | 84 | |
| 85 | cl::ParseCommandLineOptions(argc, argv, "llvm symbol undecoration tool\n"); |
Saleem Abdulrasool | 81cf2fa | 2017-01-20 04:25:26 +0000 | [diff] [blame] | 86 | |
| 87 | if (Decorated.empty()) |
Saleem Abdulrasool | a5627e5 | 2016-11-13 20:43:38 +0000 | [diff] [blame] | 88 | for (std::string Mangled; std::getline(std::cin, Mangled);) |
Saleem Abdulrasool | 08b4904 | 2017-01-19 02:58:46 +0000 | [diff] [blame] | 89 | demangle(llvm::outs(), Mangled); |
Saleem Abdulrasool | a5627e5 | 2016-11-13 20:43:38 +0000 | [diff] [blame] | 90 | else |
Saleem Abdulrasool | 81cf2fa | 2017-01-20 04:25:26 +0000 | [diff] [blame] | 91 | for (const auto &Symbol : Decorated) |
| 92 | demangle(llvm::outs(), Symbol); |
Saleem Abdulrasool | a5627e5 | 2016-11-13 20:43:38 +0000 | [diff] [blame] | 93 | |
| 94 | return EXIT_SUCCESS; |
Rafael Espindola | ec44cfd | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 95 | } |