1 //===-- llvm-c++filt.cpp --------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/StringExtras.h"
10 #include "llvm/Demangle/Demangle.h"
11 #include "llvm/Demangle/StringViewExtras.h"
12 #include "llvm/Option/Arg.h"
13 #include "llvm/Option/ArgList.h"
14 #include "llvm/Option/Option.h"
15 #include "llvm/Support/CommandLine.h"
16 #include "llvm/Support/LLVMDriver.h"
17 #include "llvm/Support/WithColor.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/TargetParser/Host.h"
20 #include "llvm/TargetParser/Triple.h"
28 OPT_INVALID
= 0, // This is not an option ID.
29 #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
34 #define PREFIX(NAME, VALUE) \
35 static constexpr llvm::StringLiteral NAME##_init[] = VALUE; \
36 static constexpr llvm::ArrayRef<llvm::StringLiteral> NAME( \
37 NAME##_init, std::size(NAME##_init) - 1);
41 using namespace llvm::opt
;
42 static constexpr opt::OptTable::Info InfoTable
[] = {
43 #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
48 class CxxfiltOptTable
: public opt::GenericOptTable
{
50 CxxfiltOptTable() : opt::GenericOptTable(InfoTable
) {
51 setGroupedShortOptions(true);
56 static bool ParseParams
;
57 static bool StripUnderscore
;
60 static StringRef ToolName
;
62 static void error(const Twine
&Message
) {
63 WithColor::error(errs(), ToolName
) << Message
<< '\n';
67 static std::string
demangle(const std::string
&Mangled
) {
68 using llvm::itanium_demangle::starts_with
;
69 std::string_view DecoratedStr
= Mangled
;
70 bool CanHaveLeadingDot
= true;
71 if (StripUnderscore
&& DecoratedStr
[0] == '_') {
72 DecoratedStr
.remove_prefix(1);
73 CanHaveLeadingDot
= false;
77 if (nonMicrosoftDemangle(DecoratedStr
, Result
, CanHaveLeadingDot
,
82 char *Undecorated
= nullptr;
85 Undecorated
= itaniumDemangle(DecoratedStr
, ParseParams
);
87 if (!Undecorated
&& starts_with(DecoratedStr
, "__imp_")) {
88 Prefix
= "import thunk for ";
89 Undecorated
= itaniumDemangle(DecoratedStr
.substr(6), ParseParams
);
92 Result
= Undecorated
? Prefix
+ Undecorated
: Mangled
;
97 // Split 'Source' on any character that fails to pass 'IsLegalChar'. The
98 // returned vector consists of pairs where 'first' is the delimited word, and
99 // 'second' are the delimiters following that word.
100 static void SplitStringDelims(
102 SmallVectorImpl
<std::pair
<StringRef
, StringRef
>> &OutFragments
,
103 function_ref
<bool(char)> IsLegalChar
) {
104 // The beginning of the input string.
105 const auto Head
= Source
.begin();
107 // Obtain any leading delimiters.
108 auto Start
= std::find_if(Head
, Source
.end(), IsLegalChar
);
110 OutFragments
.push_back({"", Source
.slice(0, Start
- Head
)});
112 // Capture each word and the delimiters following that word.
113 while (Start
!= Source
.end()) {
114 Start
= std::find_if(Start
, Source
.end(), IsLegalChar
);
115 auto End
= std::find_if_not(Start
, Source
.end(), IsLegalChar
);
116 auto DEnd
= std::find_if(End
, Source
.end(), IsLegalChar
);
117 OutFragments
.push_back({Source
.slice(Start
- Head
, End
- Head
),
118 Source
.slice(End
- Head
, DEnd
- Head
)});
123 // This returns true if 'C' is a character that can show up in an
124 // Itanium-mangled string.
125 static bool IsLegalItaniumChar(char C
) {
126 // Itanium CXX ABI [External Names]p5.1.1:
127 // '$' and '.' in mangled names are reserved for private implementations.
128 return isAlnum(C
) || C
== '.' || C
== '$' || C
== '_';
131 // If 'Split' is true, then 'Mangled' is broken into individual words and each
132 // word is demangled. Otherwise, the entire string is treated as a single
133 // mangled item. The result is output to 'OS'.
134 static void demangleLine(llvm::raw_ostream
&OS
, StringRef Mangled
, bool Split
) {
137 SmallVector
<std::pair
<StringRef
, StringRef
>, 16> Words
;
138 SplitStringDelims(Mangled
, Words
, IsLegalItaniumChar
);
139 for (const auto &Word
: Words
)
140 Result
+= ::demangle(std::string(Word
.first
)) + Word
.second
.str();
142 Result
= ::demangle(std::string(Mangled
));
143 OS
<< Result
<< '\n';
147 int llvm_cxxfilt_main(int argc
, char **argv
, const llvm::ToolContext
&) {
149 StringSaver
Saver(A
);
152 opt::InputArgList Args
= Tbl
.parseArgs(argc
, argv
, OPT_UNKNOWN
, Saver
,
153 [&](StringRef Msg
) { error(Msg
); });
154 if (Args
.hasArg(OPT_help
)) {
155 Tbl
.printHelp(outs(),
156 (Twine(ToolName
) + " [options] <mangled>").str().c_str(),
157 "LLVM symbol undecoration tool");
158 // TODO Replace this with OptTable API once it adds extrahelp support.
159 outs() << "\nPass @FILE as argument to read options from FILE.\n";
162 if (Args
.hasArg(OPT_version
)) {
163 outs() << ToolName
<< '\n';
164 cl::PrintVersionMessage();
168 // The default value depends on the default triple. Mach-O has symbols
169 // prefixed with "_", so strip by default.
171 Args
.getLastArg(OPT_strip_underscore
, OPT_no_strip_underscore
))
172 StripUnderscore
= A
->getOption().matches(OPT_strip_underscore
);
174 StripUnderscore
= Triple(sys::getProcessTriple()).isOSBinFormatMachO();
176 ParseParams
= !Args
.hasArg(OPT_no_params
);
178 Types
= Args
.hasArg(OPT_types
);
180 std::vector
<std::string
> Decorated
= Args
.getAllArgValues(OPT_INPUT
);
181 if (Decorated
.empty())
182 for (std::string Mangled
; std::getline(std::cin
, Mangled
);)
183 demangleLine(llvm::outs(), Mangled
, true);
185 for (const auto &Symbol
: Decorated
)
186 demangleLine(llvm::outs(), Symbol
, false);