[MIParser] Set RegClassOrRegBank during instruction parsing
[llvm-complete.git] / tools / llvm-undname / llvm-undname.cpp
blobaee9946b290474313037c2c62b3f151215222f8e
1 //===-- llvm-undname.cpp - Microsoft ABI name undecorator
2 //------------------===//
3 //
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
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.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Demangle/Demangle.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/ErrorOr.h"
19 #include "llvm/Support/InitLLVM.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/Process.h"
22 #include "llvm/Support/WithColor.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <cstdio>
25 #include <cstring>
26 #include <iostream>
27 #include <string>
29 using namespace llvm;
31 cl::opt<bool> DumpBackReferences("backrefs", cl::Optional,
32 cl::desc("dump backreferences"), cl::Hidden,
33 cl::init(false));
34 cl::opt<bool> NoAccessSpecifier("no-access-specifier", cl::Optional,
35 cl::desc("skip access specifiers"), cl::Hidden,
36 cl::init(false));
37 cl::opt<bool> NoCallingConvention("no-calling-convention", cl::Optional,
38 cl::desc("skip calling convention"),
39 cl::Hidden, cl::init(false));
40 cl::opt<bool> NoReturnType("no-return-type", cl::Optional,
41 cl::desc("skip return types"), cl::Hidden,
42 cl::init(false));
43 cl::opt<bool> NoMemberType("no-member-type", cl::Optional,
44 cl::desc("skip member types"), cl::Hidden,
45 cl::init(false));
46 cl::opt<std::string> RawFile("raw-file", cl::Optional,
47 cl::desc("for fuzzer data"), cl::Hidden);
48 cl::list<std::string> Symbols(cl::Positional, cl::desc("<input symbols>"),
49 cl::ZeroOrMore);
51 static bool msDemangle(const std::string &S) {
52 int Status;
53 MSDemangleFlags Flags = MSDF_None;
54 if (DumpBackReferences)
55 Flags = MSDemangleFlags(Flags | MSDF_DumpBackrefs);
56 if (NoAccessSpecifier)
57 Flags = MSDemangleFlags(Flags | MSDF_NoAccessSpecifier);
58 if (NoCallingConvention)
59 Flags = MSDemangleFlags(Flags | MSDF_NoCallingConvention);
60 if (NoReturnType)
61 Flags = MSDemangleFlags(Flags | MSDF_NoReturnType);
62 if (NoMemberType)
63 Flags = MSDemangleFlags(Flags | MSDF_NoMemberType);
65 char *ResultBuf =
66 microsoftDemangle(S.c_str(), nullptr, nullptr, &Status, Flags);
67 if (Status == llvm::demangle_success) {
68 outs() << ResultBuf << "\n";
69 outs().flush();
70 } else {
71 WithColor::error() << "Invalid mangled name\n";
73 std::free(ResultBuf);
74 return Status == llvm::demangle_success;
77 int main(int argc, char **argv) {
78 InitLLVM X(argc, argv);
80 cl::ParseCommandLineOptions(argc, argv, "llvm-undname\n");
82 if (!RawFile.empty()) {
83 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
84 MemoryBuffer::getFileOrSTDIN(RawFile);
85 if (std::error_code EC = FileOrErr.getError()) {
86 WithColor::error() << "Could not open input file \'" << RawFile
87 << "\': " << EC.message() << '\n';
88 return 1;
90 return msDemangle(FileOrErr->get()->getBuffer()) ? 0 : 1;
93 bool Success = true;
94 if (Symbols.empty()) {
95 while (true) {
96 std::string LineStr;
97 std::getline(std::cin, LineStr);
98 if (std::cin.eof())
99 break;
101 StringRef Line(LineStr);
102 Line = Line.trim();
103 if (Line.empty() || Line.startswith("#") || Line.startswith(";"))
104 continue;
106 // If the user is manually typing in these decorated names, don't echo
107 // them to the terminal a second time. If they're coming from redirected
108 // input, however, then we should display the input line so that the
109 // mangled and demangled name can be easily correlated in the output.
110 if (!sys::Process::StandardInIsUserInput()) {
111 outs() << Line << "\n";
112 outs().flush();
114 if (!msDemangle(Line))
115 Success = false;
116 outs() << "\n";
118 } else {
119 for (StringRef S : Symbols) {
120 outs() << S << "\n";
121 outs().flush();
122 if (!msDemangle(S))
123 Success = false;
124 outs() << "\n";
128 return Success ? 0 : 1;