blob: 9ac8bcf0ff015da6d8c0a54b2c626c5b33d131bb [file] [log] [blame]
Rafael Espindolab940b662016-09-06 19:16:48 +00001//===-- llvm-c++filt.cpp --------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Rafael Espindolab940b662016-09-06 19:16:48 +00006//
7//===----------------------------------------------------------------------===//
8
Matt Davis22c21932019-02-11 20:30:53 +00009#include "llvm/ADT/StringExtras.h"
Rafael Espindolab940b662016-09-06 19:16:48 +000010#include "llvm/Demangle/Demangle.h"
Saleem Abdulrasoola63fd042017-01-20 04:25:26 +000011#include "llvm/Support/CommandLine.h"
Rui Ueyama197194b2018-04-13 18:26:06 +000012#include "llvm/Support/InitLLVM.h"
Davide Italianoc37eb112016-09-27 18:50:30 +000013#include "llvm/Support/raw_ostream.h"
Saleem Abdulrasool70918202016-11-13 20:43:38 +000014#include <cstdlib>
15#include <iostream>
Rafael Espindolab940b662016-09-06 19:16:48 +000016
17using namespace llvm;
18
Saleem Abdulrasool4c33f7f2017-01-21 02:36:26 +000019enum Style {
20 Auto, ///< auto-detect mangling
21 GNU, ///< GNU
22 Lucid, ///< Lucid compiler (lcc)
23 ARM,
24 HP, ///< HP compiler (xCC)
25 EDG, ///< EDG compiler
26 GNUv3, ///< GNU C++ v3 ABI
27 Java, ///< Java (gcj)
Matt Davis58227432019-02-15 02:43:14 +000028 GNAT ///< ADA compiler (gnat)
Saleem Abdulrasool4c33f7f2017-01-21 02:36:26 +000029};
30static cl::opt<Style>
31 Format("format", cl::desc("decoration style"),
32 cl::values(clEnumValN(Auto, "auto", "auto-detect style"),
33 clEnumValN(GNU, "gnu", "GNU (itanium) style")),
34 cl::init(Auto));
35static cl::alias FormatShort("s", cl::desc("alias for --format"),
36 cl::aliasopt(Format));
37
Saleem Abdulrasoole5f6daa2017-01-22 17:41:10 +000038static cl::opt<bool> StripUnderscore("strip-underscore",
39 cl::desc("strip the leading underscore"),
40 cl::init(false));
41static cl::alias StripUnderscoreShort("_",
42 cl::desc("alias for --strip-underscore"),
43 cl::aliasopt(StripUnderscore));
44
Saleem Abdulrasoola63fd042017-01-20 04:25:26 +000045static cl::opt<bool>
46 Types("types",
47 cl::desc("attempt to demangle types as well as function names"),
48 cl::init(false));
49static cl::alias TypesShort("t", cl::desc("alias for --types"),
50 cl::aliasopt(Types));
51
52static cl::list<std::string>
53Decorated(cl::Positional, cl::desc("<mangled>"), cl::ZeroOrMore);
54
James Henderson9485b262019-06-21 11:49:20 +000055static cl::extrahelp
56 HelpResponse("\nPass @FILE as argument to read options from FILE.\n");
57
Matt Davis22c21932019-02-11 20:30:53 +000058static std::string demangle(llvm::raw_ostream &OS, const std::string &Mangled) {
Saleem Abdulrasool70918202016-11-13 20:43:38 +000059 int Status;
Saleem Abdulrasoole5f6daa2017-01-22 17:41:10 +000060
Simon Pilgrim952391d2019-05-09 15:58:16 +000061 const char *DecoratedStr = Mangled.c_str();
Saleem Abdulrasoole5f6daa2017-01-22 17:41:10 +000062 if (StripUnderscore)
Simon Pilgrim952391d2019-05-09 15:58:16 +000063 if (DecoratedStr[0] == '_')
64 ++DecoratedStr;
65 size_t DecoratedLength = strlen(DecoratedStr);
Saleem Abdulrasoole5f6daa2017-01-22 17:41:10 +000066
67 char *Undecorated = nullptr;
68
Simon Pilgrim952391d2019-05-09 15:58:16 +000069 if (Types ||
70 ((DecoratedLength >= 2 && strncmp(DecoratedStr, "_Z", 2) == 0) ||
71 (DecoratedLength >= 4 && strncmp(DecoratedStr, "___Z", 4) == 0)))
72 Undecorated = itaniumDemangle(DecoratedStr, nullptr, nullptr, &Status);
Saleem Abdulrasoole5f6daa2017-01-22 17:41:10 +000073
Saleem Abdulrasool1723b8b2017-03-22 21:15:19 +000074 if (!Undecorated &&
Simon Pilgrim952391d2019-05-09 15:58:16 +000075 (DecoratedLength > 6 && strncmp(DecoratedStr, "__imp_", 6) == 0)) {
Saleem Abdulrasool1723b8b2017-03-22 21:15:19 +000076 OS << "import thunk for ";
Simon Pilgrim952391d2019-05-09 15:58:16 +000077 Undecorated = itaniumDemangle(DecoratedStr + 6, nullptr, nullptr, &Status);
Saleem Abdulrasool1723b8b2017-03-22 21:15:19 +000078 }
79
Matt Davis22c21932019-02-11 20:30:53 +000080 std::string Result(Undecorated ? Undecorated : Mangled);
Saleem Abdulrasoole5f6daa2017-01-22 17:41:10 +000081 free(Undecorated);
Matt Davis22c21932019-02-11 20:30:53 +000082 return Result;
83}
84
Matt Davis1d5c2352019-02-27 21:39:11 +000085// Split 'Source' on any character that fails to pass 'IsLegalChar'. The
86// returned vector consists of pairs where 'first' is the delimited word, and
87// 'second' are the delimiters following that word.
88static void SplitStringDelims(
89 StringRef Source,
90 SmallVectorImpl<std::pair<StringRef, StringRef>> &OutFragments,
91 function_ref<bool(char)> IsLegalChar) {
92 // The beginning of the input string.
93 const auto Head = Source.begin();
94
95 // Obtain any leading delimiters.
96 auto Start = std::find_if(Head, Source.end(), IsLegalChar);
97 if (Start != Head)
98 OutFragments.push_back({"", Source.slice(0, Start - Head)});
99
100 // Capture each word and the delimiters following that word.
101 while (Start != Source.end()) {
102 Start = std::find_if(Start, Source.end(), IsLegalChar);
103 auto End = std::find_if_not(Start, Source.end(), IsLegalChar);
104 auto DEnd = std::find_if(End, Source.end(), IsLegalChar);
105 OutFragments.push_back({Source.slice(Start - Head, End - Head),
106 Source.slice(End - Head, DEnd - Head)});
107 Start = DEnd;
108 }
109}
110
111// This returns true if 'C' is a character that can show up in an
112// Itanium-mangled string.
113static bool IsLegalItaniumChar(char C) {
114 // Itanium CXX ABI [External Names]p5.1.1:
115 // '$' and '.' in mangled names are reserved for private implementations.
116 return isalnum(C) || C == '.' || C == '$' || C == '_';
117}
118
Matt Davis22c21932019-02-11 20:30:53 +0000119// If 'Split' is true, then 'Mangled' is broken into individual words and each
120// word is demangled. Otherwise, the entire string is treated as a single
121// mangled item. The result is output to 'OS'.
122static void demangleLine(llvm::raw_ostream &OS, StringRef Mangled, bool Split) {
123 std::string Result;
124 if (Split) {
Matt Davis1d5c2352019-02-27 21:39:11 +0000125 SmallVector<std::pair<StringRef, StringRef>, 16> Words;
126 SplitStringDelims(Mangled, Words, IsLegalItaniumChar);
127 for (const auto &Word : Words)
128 Result += demangle(OS, Word.first) + Word.second.str();
Matt Davis22c21932019-02-11 20:30:53 +0000129 } else
130 Result = demangle(OS, Mangled);
131 OS << Result << '\n';
132 OS.flush();
Saleem Abdulrasool70918202016-11-13 20:43:38 +0000133}
134
Rafael Espindolab940b662016-09-06 19:16:48 +0000135int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000136 InitLLVM X(argc, argv);
Saleem Abdulrasoolf1790c22017-01-20 05:27:09 +0000137
138 cl::ParseCommandLineOptions(argc, argv, "llvm symbol undecoration tool\n");
Saleem Abdulrasoola63fd042017-01-20 04:25:26 +0000139
140 if (Decorated.empty())
Saleem Abdulrasool70918202016-11-13 20:43:38 +0000141 for (std::string Mangled; std::getline(std::cin, Mangled);)
Matt Davis22c21932019-02-11 20:30:53 +0000142 demangleLine(llvm::outs(), Mangled, true);
Saleem Abdulrasool70918202016-11-13 20:43:38 +0000143 else
Saleem Abdulrasoola63fd042017-01-20 04:25:26 +0000144 for (const auto &Symbol : Decorated)
Matt Davis22c21932019-02-11 20:30:53 +0000145 demangleLine(llvm::outs(), Symbol, false);
Saleem Abdulrasool70918202016-11-13 20:43:38 +0000146
147 return EXIT_SUCCESS;
Rafael Espindolab940b662016-09-06 19:16:48 +0000148}