10b57cec5SDimitry Andric //===-- llvm-c++filt.cpp --------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
100b57cec5SDimitry Andric #include "llvm/Demangle/Demangle.h"
1106c3fb27SDimitry Andric #include "llvm/Demangle/StringViewExtras.h"
12fe6060f1SDimitry Andric #include "llvm/Option/Arg.h"
13fe6060f1SDimitry Andric #include "llvm/Option/ArgList.h"
14fe6060f1SDimitry Andric #include "llvm/Option/Option.h"
150b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
1606c3fb27SDimitry Andric #include "llvm/Support/LLVMDriver.h"
17fe6060f1SDimitry Andric #include "llvm/Support/WithColor.h"
180b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
1906c3fb27SDimitry Andric #include "llvm/TargetParser/Host.h"
2006c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h"
210b57cec5SDimitry Andric #include <cstdlib>
220b57cec5SDimitry Andric #include <iostream>
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric using namespace llvm;
250b57cec5SDimitry Andric 
26fe6060f1SDimitry Andric namespace {
27fe6060f1SDimitry Andric enum ID {
28fe6060f1SDimitry Andric   OPT_INVALID = 0, // This is not an option ID.
295f757f3fSDimitry Andric #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
30fe6060f1SDimitry Andric #include "Opts.inc"
31fe6060f1SDimitry Andric #undef OPTION
320b57cec5SDimitry Andric };
330b57cec5SDimitry Andric 
34bdd1243dSDimitry Andric #define PREFIX(NAME, VALUE)                                                    \
35bdd1243dSDimitry Andric   static constexpr llvm::StringLiteral NAME##_init[] = VALUE;                  \
36bdd1243dSDimitry Andric   static constexpr llvm::ArrayRef<llvm::StringLiteral> NAME(                   \
37bdd1243dSDimitry Andric       NAME##_init, std::size(NAME##_init) - 1);
38fe6060f1SDimitry Andric #include "Opts.inc"
39fe6060f1SDimitry Andric #undef PREFIX
400b57cec5SDimitry Andric 
415f757f3fSDimitry Andric using namespace llvm::opt;
42bdd1243dSDimitry Andric static constexpr opt::OptTable::Info InfoTable[] = {
435f757f3fSDimitry Andric #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
44fe6060f1SDimitry Andric #include "Opts.inc"
45fe6060f1SDimitry Andric #undef OPTION
46fe6060f1SDimitry Andric };
470b57cec5SDimitry Andric 
48bdd1243dSDimitry Andric class CxxfiltOptTable : public opt::GenericOptTable {
49fe6060f1SDimitry Andric public:
CxxfiltOptTable()50bdd1243dSDimitry Andric   CxxfiltOptTable() : opt::GenericOptTable(InfoTable) {
51bdd1243dSDimitry Andric     setGroupedShortOptions(true);
52bdd1243dSDimitry Andric   }
53fe6060f1SDimitry Andric };
54fe6060f1SDimitry Andric } // namespace
550b57cec5SDimitry Andric 
561db9f3b2SDimitry Andric static bool ParseParams;
57fe6060f1SDimitry Andric static bool StripUnderscore;
58fe6060f1SDimitry Andric static bool Types;
590b57cec5SDimitry Andric 
60fe6060f1SDimitry Andric static StringRef ToolName;
61fe6060f1SDimitry Andric 
error(const Twine & Message)62fe6060f1SDimitry Andric static void error(const Twine &Message) {
63fe6060f1SDimitry Andric   WithColor::error(errs(), ToolName) << Message << '\n';
64fe6060f1SDimitry Andric   exit(1);
65480093f4SDimitry Andric }
66480093f4SDimitry Andric 
demangle(const std::string & Mangled)67480093f4SDimitry Andric static std::string demangle(const std::string &Mangled) {
6806c3fb27SDimitry Andric   using llvm::itanium_demangle::starts_with;
6906c3fb27SDimitry Andric   std::string_view DecoratedStr = Mangled;
705f757f3fSDimitry Andric   bool CanHaveLeadingDot = true;
715f757f3fSDimitry Andric   if (StripUnderscore && DecoratedStr[0] == '_') {
7206c3fb27SDimitry Andric     DecoratedStr.remove_prefix(1);
735f757f3fSDimitry Andric     CanHaveLeadingDot = false;
745f757f3fSDimitry Andric   }
750b57cec5SDimitry Andric 
76349cc55cSDimitry Andric   std::string Result;
771db9f3b2SDimitry Andric   if (nonMicrosoftDemangle(DecoratedStr, Result, CanHaveLeadingDot,
781db9f3b2SDimitry Andric                            ParseParams))
79349cc55cSDimitry Andric     return Result;
80349cc55cSDimitry Andric 
81349cc55cSDimitry Andric   std::string Prefix;
820b57cec5SDimitry Andric   char *Undecorated = nullptr;
830b57cec5SDimitry Andric 
84349cc55cSDimitry Andric   if (Types)
851db9f3b2SDimitry Andric     Undecorated = itaniumDemangle(DecoratedStr, ParseParams);
860b57cec5SDimitry Andric 
8706c3fb27SDimitry Andric   if (!Undecorated && starts_with(DecoratedStr, "__imp_")) {
88480093f4SDimitry Andric     Prefix = "import thunk for ";
891db9f3b2SDimitry Andric     Undecorated = itaniumDemangle(DecoratedStr.substr(6), ParseParams);
900b57cec5SDimitry Andric   }
910b57cec5SDimitry Andric 
92349cc55cSDimitry Andric   Result = Undecorated ? Prefix + Undecorated : Mangled;
930b57cec5SDimitry Andric   free(Undecorated);
940b57cec5SDimitry Andric   return Result;
950b57cec5SDimitry Andric }
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric // Split 'Source' on any character that fails to pass 'IsLegalChar'.  The
980b57cec5SDimitry Andric // returned vector consists of pairs where 'first' is the delimited word, and
990b57cec5SDimitry Andric // 'second' are the delimiters following that word.
SplitStringDelims(StringRef Source,SmallVectorImpl<std::pair<StringRef,StringRef>> & OutFragments,function_ref<bool (char)> IsLegalChar)1000b57cec5SDimitry Andric static void SplitStringDelims(
1010b57cec5SDimitry Andric     StringRef Source,
1020b57cec5SDimitry Andric     SmallVectorImpl<std::pair<StringRef, StringRef>> &OutFragments,
1030b57cec5SDimitry Andric     function_ref<bool(char)> IsLegalChar) {
1040b57cec5SDimitry Andric   // The beginning of the input string.
1050b57cec5SDimitry Andric   const auto Head = Source.begin();
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric   // Obtain any leading delimiters.
1080b57cec5SDimitry Andric   auto Start = std::find_if(Head, Source.end(), IsLegalChar);
1090b57cec5SDimitry Andric   if (Start != Head)
1100b57cec5SDimitry Andric     OutFragments.push_back({"", Source.slice(0, Start - Head)});
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   // Capture each word and the delimiters following that word.
1130b57cec5SDimitry Andric   while (Start != Source.end()) {
1140b57cec5SDimitry Andric     Start = std::find_if(Start, Source.end(), IsLegalChar);
1150b57cec5SDimitry Andric     auto End = std::find_if_not(Start, Source.end(), IsLegalChar);
1160b57cec5SDimitry Andric     auto DEnd = std::find_if(End, Source.end(), IsLegalChar);
1170b57cec5SDimitry Andric     OutFragments.push_back({Source.slice(Start - Head, End - Head),
1180b57cec5SDimitry Andric                             Source.slice(End - Head, DEnd - Head)});
1190b57cec5SDimitry Andric     Start = DEnd;
1200b57cec5SDimitry Andric   }
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric // This returns true if 'C' is a character that can show up in an
1240b57cec5SDimitry Andric // Itanium-mangled string.
IsLegalItaniumChar(char C)1250b57cec5SDimitry Andric static bool IsLegalItaniumChar(char C) {
1260b57cec5SDimitry Andric   // Itanium CXX ABI [External Names]p5.1.1:
1270b57cec5SDimitry Andric   // '$' and '.' in mangled names are reserved for private implementations.
128349cc55cSDimitry Andric   return isAlnum(C) || C == '.' || C == '$' || C == '_';
1290b57cec5SDimitry Andric }
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric // If 'Split' is true, then 'Mangled' is broken into individual words and each
1320b57cec5SDimitry Andric // word is demangled.  Otherwise, the entire string is treated as a single
1330b57cec5SDimitry Andric // mangled item.  The result is output to 'OS'.
demangleLine(llvm::raw_ostream & OS,StringRef Mangled,bool Split)1340b57cec5SDimitry Andric static void demangleLine(llvm::raw_ostream &OS, StringRef Mangled, bool Split) {
1350b57cec5SDimitry Andric   std::string Result;
1360b57cec5SDimitry Andric   if (Split) {
1370b57cec5SDimitry Andric     SmallVector<std::pair<StringRef, StringRef>, 16> Words;
1380b57cec5SDimitry Andric     SplitStringDelims(Mangled, Words, IsLegalItaniumChar);
1390b57cec5SDimitry Andric     for (const auto &Word : Words)
1405ffd83dbSDimitry Andric       Result += ::demangle(std::string(Word.first)) + Word.second.str();
1410b57cec5SDimitry Andric   } else
1425ffd83dbSDimitry Andric     Result = ::demangle(std::string(Mangled));
1430b57cec5SDimitry Andric   OS << Result << '\n';
1440b57cec5SDimitry Andric   OS.flush();
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric 
llvm_cxxfilt_main(int argc,char ** argv,const llvm::ToolContext &)14706c3fb27SDimitry Andric int llvm_cxxfilt_main(int argc, char **argv, const llvm::ToolContext &) {
148fe6060f1SDimitry Andric   BumpPtrAllocator A;
149fe6060f1SDimitry Andric   StringSaver Saver(A);
150fe6060f1SDimitry Andric   CxxfiltOptTable Tbl;
151fe6060f1SDimitry Andric   ToolName = argv[0];
152fe6060f1SDimitry Andric   opt::InputArgList Args = Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver,
153fe6060f1SDimitry Andric                                          [&](StringRef Msg) { error(Msg); });
154fe6060f1SDimitry Andric   if (Args.hasArg(OPT_help)) {
155fe6060f1SDimitry Andric     Tbl.printHelp(outs(),
156fe6060f1SDimitry Andric                   (Twine(ToolName) + " [options] <mangled>").str().c_str(),
157fe6060f1SDimitry Andric                   "LLVM symbol undecoration tool");
158fe6060f1SDimitry Andric     // TODO Replace this with OptTable API once it adds extrahelp support.
159fe6060f1SDimitry Andric     outs() << "\nPass @FILE as argument to read options from FILE.\n";
160fe6060f1SDimitry Andric     return 0;
161fe6060f1SDimitry Andric   }
162fe6060f1SDimitry Andric   if (Args.hasArg(OPT_version)) {
163fe6060f1SDimitry Andric     outs() << ToolName << '\n';
164fe6060f1SDimitry Andric     cl::PrintVersionMessage();
165fe6060f1SDimitry Andric     return 0;
166fe6060f1SDimitry Andric   }
1670b57cec5SDimitry Andric 
168fe6060f1SDimitry Andric   // The default value depends on the default triple. Mach-O has symbols
169fe6060f1SDimitry Andric   // prefixed with "_", so strip by default.
170fe6060f1SDimitry Andric   if (opt::Arg *A =
171fe6060f1SDimitry Andric           Args.getLastArg(OPT_strip_underscore, OPT_no_strip_underscore))
172fe6060f1SDimitry Andric     StripUnderscore = A->getOption().matches(OPT_strip_underscore);
173fe6060f1SDimitry Andric   else
174fe6060f1SDimitry Andric     StripUnderscore = Triple(sys::getProcessTriple()).isOSBinFormatMachO();
1750b57cec5SDimitry Andric 
1761db9f3b2SDimitry Andric   ParseParams = !Args.hasArg(OPT_no_params);
1771db9f3b2SDimitry Andric 
178fe6060f1SDimitry Andric   Types = Args.hasArg(OPT_types);
179fe6060f1SDimitry Andric 
180fe6060f1SDimitry Andric   std::vector<std::string> Decorated = Args.getAllArgValues(OPT_INPUT);
1810b57cec5SDimitry Andric   if (Decorated.empty())
1820b57cec5SDimitry Andric     for (std::string Mangled; std::getline(std::cin, Mangled);)
1830b57cec5SDimitry Andric       demangleLine(llvm::outs(), Mangled, true);
1840b57cec5SDimitry Andric   else
1850b57cec5SDimitry Andric     for (const auto &Symbol : Decorated)
1860b57cec5SDimitry Andric       demangleLine(llvm::outs(), Symbol, false);
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric   return EXIT_SUCCESS;
1890b57cec5SDimitry Andric }
190