Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 1 | //===-- llvm-c++filt.cpp --------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/Demangle/Demangle.h" |
Saleem Abdulrasool | a63fd04 | 2017-01-20 04:25:26 +0000 | [diff] [blame] | 10 | #include "llvm/Support/CommandLine.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 11 | #include "llvm/Support/InitLLVM.h" |
Davide Italiano | c37eb11 | 2016-09-27 18:50:30 +0000 | [diff] [blame] | 12 | #include "llvm/Support/raw_ostream.h" |
Saleem Abdulrasool | 7091820 | 2016-11-13 20:43:38 +0000 | [diff] [blame] | 13 | #include <cstdlib> |
| 14 | #include <iostream> |
Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace llvm; |
| 17 | |
Saleem Abdulrasool | 4c33f7f | 2017-01-21 02:36:26 +0000 | [diff] [blame] | 18 | enum Style { |
| 19 | Auto, ///< auto-detect mangling |
| 20 | GNU, ///< GNU |
| 21 | Lucid, ///< Lucid compiler (lcc) |
| 22 | ARM, |
| 23 | HP, ///< HP compiler (xCC) |
| 24 | EDG, ///< EDG compiler |
| 25 | GNUv3, ///< GNU C++ v3 ABI |
| 26 | Java, ///< Java (gcj) |
| 27 | GNAT ///< ADA copiler (gnat) |
| 28 | }; |
| 29 | static cl::opt<Style> |
| 30 | Format("format", cl::desc("decoration style"), |
| 31 | cl::values(clEnumValN(Auto, "auto", "auto-detect style"), |
| 32 | clEnumValN(GNU, "gnu", "GNU (itanium) style")), |
| 33 | cl::init(Auto)); |
| 34 | static cl::alias FormatShort("s", cl::desc("alias for --format"), |
| 35 | cl::aliasopt(Format)); |
| 36 | |
Saleem Abdulrasool | e5f6daa | 2017-01-22 17:41:10 +0000 | [diff] [blame] | 37 | static cl::opt<bool> StripUnderscore("strip-underscore", |
| 38 | cl::desc("strip the leading underscore"), |
| 39 | cl::init(false)); |
| 40 | static cl::alias StripUnderscoreShort("_", |
| 41 | cl::desc("alias for --strip-underscore"), |
| 42 | cl::aliasopt(StripUnderscore)); |
| 43 | |
Saleem Abdulrasool | a63fd04 | 2017-01-20 04:25:26 +0000 | [diff] [blame] | 44 | static cl::opt<bool> |
| 45 | Types("types", |
| 46 | cl::desc("attempt to demangle types as well as function names"), |
| 47 | cl::init(false)); |
| 48 | static cl::alias TypesShort("t", cl::desc("alias for --types"), |
| 49 | cl::aliasopt(Types)); |
| 50 | |
| 51 | static cl::list<std::string> |
| 52 | Decorated(cl::Positional, cl::desc("<mangled>"), cl::ZeroOrMore); |
| 53 | |
Saleem Abdulrasool | c8bcda2 | 2017-01-19 02:58:46 +0000 | [diff] [blame] | 54 | static void demangle(llvm::raw_ostream &OS, const std::string &Mangled) { |
Saleem Abdulrasool | 7091820 | 2016-11-13 20:43:38 +0000 | [diff] [blame] | 55 | int Status; |
Saleem Abdulrasool | e5f6daa | 2017-01-22 17:41:10 +0000 | [diff] [blame] | 56 | |
| 57 | const char *Decorated = Mangled.c_str(); |
| 58 | if (StripUnderscore) |
| 59 | if (Decorated[0] == '_') |
| 60 | ++Decorated; |
| 61 | size_t DecoratedLength = strlen(Decorated); |
| 62 | |
| 63 | char *Undecorated = nullptr; |
| 64 | |
| 65 | if (Types || ((DecoratedLength >= 2 && strncmp(Decorated, "_Z", 2) == 0) || |
| 66 | (DecoratedLength >= 4 && strncmp(Decorated, "___Z", 4) == 0))) |
| 67 | Undecorated = itaniumDemangle(Decorated, nullptr, nullptr, &Status); |
| 68 | |
Saleem Abdulrasool | 1723b8b | 2017-03-22 21:15:19 +0000 | [diff] [blame] | 69 | if (!Undecorated && |
| 70 | (DecoratedLength > 6 && strncmp(Decorated, "__imp_", 6) == 0)) { |
| 71 | OS << "import thunk for "; |
| 72 | Undecorated = itaniumDemangle(Decorated + 6, nullptr, nullptr, &Status); |
| 73 | } |
| 74 | |
Saleem Abdulrasool | e5f6daa | 2017-01-22 17:41:10 +0000 | [diff] [blame] | 75 | OS << (Undecorated ? Undecorated : Mangled) << '\n'; |
Adam Nemet | 95e0c5f | 2017-11-29 17:07:41 +0000 | [diff] [blame] | 76 | OS.flush(); |
Saleem Abdulrasool | e5f6daa | 2017-01-22 17:41:10 +0000 | [diff] [blame] | 77 | |
| 78 | free(Undecorated); |
Saleem Abdulrasool | 7091820 | 2016-11-13 20:43:38 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 81 | int main(int argc, char **argv) { |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 82 | InitLLVM X(argc, argv); |
Saleem Abdulrasool | f1790c2 | 2017-01-20 05:27:09 +0000 | [diff] [blame] | 83 | |
| 84 | cl::ParseCommandLineOptions(argc, argv, "llvm symbol undecoration tool\n"); |
Saleem Abdulrasool | a63fd04 | 2017-01-20 04:25:26 +0000 | [diff] [blame] | 85 | |
| 86 | if (Decorated.empty()) |
Saleem Abdulrasool | 7091820 | 2016-11-13 20:43:38 +0000 | [diff] [blame] | 87 | for (std::string Mangled; std::getline(std::cin, Mangled);) |
Saleem Abdulrasool | c8bcda2 | 2017-01-19 02:58:46 +0000 | [diff] [blame] | 88 | demangle(llvm::outs(), Mangled); |
Saleem Abdulrasool | 7091820 | 2016-11-13 20:43:38 +0000 | [diff] [blame] | 89 | else |
Saleem Abdulrasool | a63fd04 | 2017-01-20 04:25:26 +0000 | [diff] [blame] | 90 | for (const auto &Symbol : Decorated) |
| 91 | demangle(llvm::outs(), Symbol); |
Saleem Abdulrasool | 7091820 | 2016-11-13 20:43:38 +0000 | [diff] [blame] | 92 | |
| 93 | return EXIT_SUCCESS; |
Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 94 | } |