1 //===-- llvm-undname.cpp - Microsoft ABI name undecorator
2 //------------------===//
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
8 //===----------------------------------------------------------------------===//
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"
31 cl::OptionCategory
UndNameCategory("UndName Options");
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
));
61 static bool msDemangle(const std::string
&S
) {
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
);
71 Flags
= MSDemangleFlags(Flags
| MSDF_NoReturnType
);
73 Flags
= MSDemangleFlags(Flags
| MSDF_NoMemberType
);
75 Flags
= MSDemangleFlags(Flags
| MSDF_NoVariableType
);
78 char *ResultBuf
= microsoftDemangle(S
, &NRead
, &Status
, Flags
);
79 if (Status
== llvm::demangle_success
) {
80 outs() << ResultBuf
<< "\n";
82 if (WarnTrailing
&& NRead
< S
.size())
83 WithColor::warning() << "trailing characters: " << S
.c_str() + NRead
86 WithColor::error() << "Invalid mangled name\n";
89 return Status
== llvm::demangle_success
;
92 int main(int argc
, char **argv
) {
93 InitLLVM
X(argc
, argv
);
95 cl::HideUnrelatedOptions({&UndNameCategory
, &getColorCategory()});
96 cl::ParseCommandLineOptions(argc
, argv
, "llvm-undname\n");
98 if (!RawFile
.empty()) {
99 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> FileOrErr
=
100 MemoryBuffer::getFileOrSTDIN(RawFile
);
101 if (std::error_code EC
= FileOrErr
.getError()) {
102 WithColor::error() << "Could not open input file \'" << RawFile
103 << "\': " << EC
.message() << '\n';
106 return msDemangle(std::string(FileOrErr
->get()->getBuffer())) ? 0 : 1;
110 if (Symbols
.empty()) {
113 std::getline(std::cin
, LineStr
);
117 StringRef
Line(LineStr
);
119 if (Line
.empty() || Line
.startswith("#") || Line
.startswith(";"))
122 // If the user is manually typing in these decorated names, don't echo
123 // them to the terminal a second time. If they're coming from redirected
124 // input, however, then we should display the input line so that the
125 // mangled and demangled name can be easily correlated in the output.
126 if (!sys::Process::StandardInIsUserInput()) {
127 outs() << Line
<< "\n";
130 if (!msDemangle(std::string(Line
)))
135 for (StringRef S
: Symbols
) {
138 if (!msDemangle(std::string(S
)))
144 return Success
? 0 : 1;