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