Zachary Turner | 42e40ea | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 1 | //===-- llvm-undname.cpp - Microsoft ABI name undecorator |
| 2 | //------------------===// |
| 3 | // |
| 4 | // The LLVM Compiler Infrastructure |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | // |
| 11 | // This utility works like the windows undname utility. It converts mangled |
| 12 | // Microsoft symbol names into pretty C/C++ human-readable names. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/ADT/StringRef.h" |
| 17 | #include "llvm/Demangle/Demangle.h" |
| 18 | #include "llvm/Support/CommandLine.h" |
| 19 | #include "llvm/Support/InitLLVM.h" |
| 20 | #include "llvm/Support/Process.h" |
Jonas Devlieghere | d96e13f | 2018-11-11 22:11:47 +0000 | [diff] [blame] | 21 | #include "llvm/Support/WithColor.h" |
Zachary Turner | 42e40ea | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | #include <cstdio> |
| 24 | #include <cstring> |
| 25 | #include <iostream> |
| 26 | #include <string> |
| 27 | |
| 28 | using namespace llvm; |
| 29 | |
Zachary Turner | 39fff69 | 2018-08-01 18:33:04 +0000 | [diff] [blame] | 30 | cl::opt<bool> DumpBackReferences("backrefs", cl::Optional, |
| 31 | cl::desc("dump backreferences"), cl::Hidden, |
| 32 | cl::init(false)); |
Zachary Turner | 42e40ea | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 33 | cl::list<std::string> Symbols(cl::Positional, cl::desc("<input symbols>"), |
| 34 | cl::ZeroOrMore); |
| 35 | |
| 36 | static void demangle(const std::string &S) { |
| 37 | int Status; |
Zachary Turner | 39fff69 | 2018-08-01 18:33:04 +0000 | [diff] [blame] | 38 | MSDemangleFlags Flags = MSDF_None; |
| 39 | if (DumpBackReferences) |
| 40 | Flags = MSDemangleFlags(Flags | MSDF_DumpBackrefs); |
| 41 | |
| 42 | char *ResultBuf = |
| 43 | microsoftDemangle(S.c_str(), nullptr, nullptr, &Status, Flags); |
Zachary Turner | 42e40ea | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 44 | if (Status == llvm::demangle_success) { |
| 45 | outs() << ResultBuf << "\n"; |
| 46 | outs().flush(); |
| 47 | } else { |
Jonas Devlieghere | d96e13f | 2018-11-11 22:11:47 +0000 | [diff] [blame] | 48 | WithColor::error() << "Invalid mangled name\n"; |
Zachary Turner | 42e40ea | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 49 | } |
| 50 | std::free(ResultBuf); |
Martin Storsjo | 9f7b748 | 2018-07-20 20:48:36 +0000 | [diff] [blame] | 51 | } |
Zachary Turner | 42e40ea | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 52 | |
| 53 | int main(int argc, char **argv) { |
| 54 | InitLLVM X(argc, argv); |
| 55 | |
| 56 | cl::ParseCommandLineOptions(argc, argv, "llvm-undname\n"); |
| 57 | |
| 58 | if (Symbols.empty()) { |
| 59 | while (true) { |
| 60 | std::string LineStr; |
| 61 | std::getline(std::cin, LineStr); |
| 62 | if (std::cin.eof()) |
| 63 | break; |
| 64 | |
| 65 | StringRef Line(LineStr); |
| 66 | Line = Line.trim(); |
| 67 | if (Line.empty() || Line.startswith("#") || Line.startswith(";")) |
| 68 | continue; |
| 69 | |
| 70 | // If the user is manually typing in these decorated names, don't echo |
| 71 | // them to the terminal a second time. If they're coming from redirected |
| 72 | // input, however, then we should display the input line so that the |
| 73 | // mangled and demangled name can be easily correlated in the output. |
Zachary Turner | 322f6d0 | 2018-07-21 15:39:05 +0000 | [diff] [blame] | 74 | if (!sys::Process::StandardInIsUserInput()) { |
Zachary Turner | 42e40ea | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 75 | outs() << Line << "\n"; |
Zachary Turner | 322f6d0 | 2018-07-21 15:39:05 +0000 | [diff] [blame] | 76 | outs().flush(); |
| 77 | } |
Zachary Turner | 42e40ea | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 78 | demangle(Line); |
| 79 | outs() << "\n"; |
| 80 | } |
| 81 | } else { |
| 82 | for (StringRef S : Symbols) { |
| 83 | outs() << S << "\n"; |
Zachary Turner | 322f6d0 | 2018-07-21 15:39:05 +0000 | [diff] [blame] | 84 | outs().flush(); |
Zachary Turner | 42e40ea | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 85 | demangle(S); |
| 86 | outs() << "\n"; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return 0; |
Martin Storsjo | 9f7b748 | 2018-07-20 20:48:36 +0000 | [diff] [blame] | 91 | } |