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