xref: /openbsd-src/gnu/llvm/llvm/tools/llvm-undname/llvm-undname.cpp (revision d89ec533011f513df1010f142a111086a0785f09)
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.
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/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>
28 
29 using namespace llvm;
30 
31 cl::OptionCategory UndNameCategory("UndName Options");
32 
33 cl::opt<bool> DumpBackReferences("backrefs", cl::Optional,
34                                  cl::desc("dump backreferences"), cl::Hidden,
35                                  cl::init(false), cl::cat(UndNameCategory));
36 cl::opt<bool> NoAccessSpecifier("no-access-specifier", cl::Optional,
37                                 cl::desc("skip access specifiers"), cl::Hidden,
38                                 cl::init(false), cl::cat(UndNameCategory));
39 cl::opt<bool> NoCallingConvention("no-calling-convention", cl::Optional,
40                                   cl::desc("skip calling convention"),
41                                   cl::Hidden, cl::init(false),
42                                   cl::cat(UndNameCategory));
43 cl::opt<bool> NoReturnType("no-return-type", cl::Optional,
44                            cl::desc("skip return types"), cl::Hidden,
45                            cl::init(false), cl::cat(UndNameCategory));
46 cl::opt<bool> NoMemberType("no-member-type", cl::Optional,
47                            cl::desc("skip member types"), cl::Hidden,
48                            cl::init(false), cl::cat(UndNameCategory));
49 cl::opt<std::string> RawFile("raw-file", cl::Optional,
50                              cl::desc("for fuzzer data"), cl::Hidden,
51                              cl::cat(UndNameCategory));
52 cl::opt<bool> WarnTrailing("warn-trailing", cl::Optional,
53                            cl::desc("warn on trailing characters"), cl::Hidden,
54                            cl::init(false), cl::cat(UndNameCategory));
55 cl::list<std::string> Symbols(cl::Positional, cl::desc("<input symbols>"),
56                               cl::ZeroOrMore, cl::cat(UndNameCategory));
57 
58 static bool msDemangle(const std::string &S) {
59   int Status;
60   MSDemangleFlags Flags = MSDF_None;
61   if (DumpBackReferences)
62     Flags = MSDemangleFlags(Flags | MSDF_DumpBackrefs);
63   if (NoAccessSpecifier)
64     Flags = MSDemangleFlags(Flags | MSDF_NoAccessSpecifier);
65   if (NoCallingConvention)
66     Flags = MSDemangleFlags(Flags | MSDF_NoCallingConvention);
67   if (NoReturnType)
68     Flags = MSDemangleFlags(Flags | MSDF_NoReturnType);
69   if (NoMemberType)
70     Flags = MSDemangleFlags(Flags | MSDF_NoMemberType);
71 
72   size_t NRead;
73   char *ResultBuf =
74       microsoftDemangle(S.c_str(), &NRead, nullptr, nullptr, &Status, Flags);
75   if (Status == llvm::demangle_success) {
76     outs() << ResultBuf << "\n";
77     outs().flush();
78     if (WarnTrailing && NRead < S.size())
79       WithColor::warning() << "trailing characters: " << S.c_str() + NRead
80                            << "\n";
81   } else {
82     WithColor::error() << "Invalid mangled name\n";
83   }
84   std::free(ResultBuf);
85   return Status == llvm::demangle_success;
86 }
87 
88 int main(int argc, char **argv) {
89   InitLLVM X(argc, argv);
90 
91   cl::HideUnrelatedOptions({&UndNameCategory, &getColorCategory()});
92   cl::ParseCommandLineOptions(argc, argv, "llvm-undname\n");
93 
94   if (!RawFile.empty()) {
95     ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
96         MemoryBuffer::getFileOrSTDIN(RawFile);
97     if (std::error_code EC = FileOrErr.getError()) {
98       WithColor::error() << "Could not open input file \'" << RawFile
99                          << "\': " << EC.message() << '\n';
100       return 1;
101     }
102     return msDemangle(std::string(FileOrErr->get()->getBuffer())) ? 0 : 1;
103   }
104 
105   bool Success = true;
106   if (Symbols.empty()) {
107     while (true) {
108       std::string LineStr;
109       std::getline(std::cin, LineStr);
110       if (std::cin.eof())
111         break;
112 
113       StringRef Line(LineStr);
114       Line = Line.trim();
115       if (Line.empty() || Line.startswith("#") || Line.startswith(";"))
116         continue;
117 
118       // If the user is manually typing in these decorated names, don't echo
119       // them to the terminal a second time.  If they're coming from redirected
120       // input, however, then we should display the input line so that the
121       // mangled and demangled name can be easily correlated in the output.
122       if (!sys::Process::StandardInIsUserInput()) {
123         outs() << Line << "\n";
124         outs().flush();
125       }
126       if (!msDemangle(std::string(Line)))
127         Success = false;
128       outs() << "\n";
129     }
130   } else {
131     for (StringRef S : Symbols) {
132       outs() << S << "\n";
133       outs().flush();
134       if (!msDemangle(std::string(S)))
135         Success = false;
136       outs() << "\n";
137     }
138   }
139 
140   return Success ? 0 : 1;
141 }
142