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 | |
Matt Davis | 22c2193 | 2019-02-11 20:30:53 +0000 | [diff] [blame] | 9 | #include "llvm/ADT/StringExtras.h" |
Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 10 | #include "llvm/Demangle/Demangle.h" |
Saleem Abdulrasool | a63fd04 | 2017-01-20 04:25:26 +0000 | [diff] [blame] | 11 | #include "llvm/Support/CommandLine.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 12 | #include "llvm/Support/InitLLVM.h" |
Davide Italiano | c37eb11 | 2016-09-27 18:50:30 +0000 | [diff] [blame] | 13 | #include "llvm/Support/raw_ostream.h" |
Saleem Abdulrasool | 7091820 | 2016-11-13 20:43:38 +0000 | [diff] [blame] | 14 | #include <cstdlib> |
| 15 | #include <iostream> |
Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace llvm; |
| 18 | |
Saleem Abdulrasool | 4c33f7f | 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) |
Matt Davis | 5822743 | 2019-02-15 02:43:14 +0000 | [diff] [blame^] | 28 | GNAT ///< ADA compiler (gnat) |
Saleem Abdulrasool | 4c33f7f | 2017-01-21 02:36:26 +0000 | [diff] [blame] | 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 | e5f6daa | 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 | a63fd04 | 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 | |
Matt Davis | 22c2193 | 2019-02-11 20:30:53 +0000 | [diff] [blame] | 55 | static std::string demangle(llvm::raw_ostream &OS, const std::string &Mangled) { |
Saleem Abdulrasool | 7091820 | 2016-11-13 20:43:38 +0000 | [diff] [blame] | 56 | int Status; |
Saleem Abdulrasool | e5f6daa | 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 | 1723b8b | 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 | |
Matt Davis | 22c2193 | 2019-02-11 20:30:53 +0000 | [diff] [blame] | 76 | std::string Result(Undecorated ? Undecorated : Mangled); |
Saleem Abdulrasool | e5f6daa | 2017-01-22 17:41:10 +0000 | [diff] [blame] | 77 | free(Undecorated); |
Matt Davis | 22c2193 | 2019-02-11 20:30:53 +0000 | [diff] [blame] | 78 | return Result; |
| 79 | } |
| 80 | |
| 81 | // If 'Split' is true, then 'Mangled' is broken into individual words and each |
| 82 | // word is demangled. Otherwise, the entire string is treated as a single |
| 83 | // mangled item. The result is output to 'OS'. |
| 84 | static void demangleLine(llvm::raw_ostream &OS, StringRef Mangled, bool Split) { |
| 85 | std::string Result; |
| 86 | if (Split) { |
| 87 | SmallVector<StringRef, 16> Words; |
| 88 | SplitString(Mangled, Words); |
| 89 | for (auto Word : Words) |
| 90 | Result += demangle(OS, Word) + ' '; |
| 91 | // Remove the trailing space character. |
| 92 | if (Result.back() == ' ') |
| 93 | Result.pop_back(); |
| 94 | } else |
| 95 | Result = demangle(OS, Mangled); |
| 96 | OS << Result << '\n'; |
| 97 | OS.flush(); |
Saleem Abdulrasool | 7091820 | 2016-11-13 20:43:38 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 100 | int main(int argc, char **argv) { |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 101 | InitLLVM X(argc, argv); |
Saleem Abdulrasool | f1790c2 | 2017-01-20 05:27:09 +0000 | [diff] [blame] | 102 | |
| 103 | cl::ParseCommandLineOptions(argc, argv, "llvm symbol undecoration tool\n"); |
Saleem Abdulrasool | a63fd04 | 2017-01-20 04:25:26 +0000 | [diff] [blame] | 104 | |
| 105 | if (Decorated.empty()) |
Saleem Abdulrasool | 7091820 | 2016-11-13 20:43:38 +0000 | [diff] [blame] | 106 | for (std::string Mangled; std::getline(std::cin, Mangled);) |
Matt Davis | 22c2193 | 2019-02-11 20:30:53 +0000 | [diff] [blame] | 107 | demangleLine(llvm::outs(), Mangled, true); |
Saleem Abdulrasool | 7091820 | 2016-11-13 20:43:38 +0000 | [diff] [blame] | 108 | else |
Saleem Abdulrasool | a63fd04 | 2017-01-20 04:25:26 +0000 | [diff] [blame] | 109 | for (const auto &Symbol : Decorated) |
Matt Davis | 22c2193 | 2019-02-11 20:30:53 +0000 | [diff] [blame] | 110 | demangleLine(llvm::outs(), Symbol, false); |
Saleem Abdulrasool | 7091820 | 2016-11-13 20:43:38 +0000 | [diff] [blame] | 111 | |
| 112 | return EXIT_SUCCESS; |
Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 113 | } |