blob: a082dc7cfe85122f143e8b9d363327a93703d1fb [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
Matt Davis22c21932019-02-11 20:30:53 +000055static std::string demangle(llvm::raw_ostream &OS, const std::string &Mangled) {
Saleem Abdulrasool70918202016-11-13 20:43:38 +000056 int Status;
Saleem Abdulrasoole5f6daa2017-01-22 17:41:10 +000057
58 const char *Decorated = Mangled.c_str();
59 if (StripUnderscore)
60 if (Decorated[0] == '_')
61 ++Decorated;
62 size_t DecoratedLength = strlen(Decorated);
63
64 char *Undecorated = nullptr;
65
66 if (Types || ((DecoratedLength >= 2 && strncmp(Decorated, "_Z", 2) == 0) ||
67 (DecoratedLength >= 4 && strncmp(Decorated, "___Z", 4) == 0)))
68 Undecorated = itaniumDemangle(Decorated, nullptr, nullptr, &Status);
69
Saleem Abdulrasool1723b8b2017-03-22 21:15:19 +000070 if (!Undecorated &&
71 (DecoratedLength > 6 && strncmp(Decorated, "__imp_", 6) == 0)) {
72 OS << "import thunk for ";
73 Undecorated = itaniumDemangle(Decorated + 6, nullptr, nullptr, &Status);
74 }
75
Matt Davis22c21932019-02-11 20:30:53 +000076 std::string Result(Undecorated ? Undecorated : Mangled);
Saleem Abdulrasoole5f6daa2017-01-22 17:41:10 +000077 free(Undecorated);
Matt Davis22c21932019-02-11 20:30:53 +000078 return Result;
79}
80
Matt Davis1d5c2352019-02-27 21:39:11 +000081// Split 'Source' on any character that fails to pass 'IsLegalChar'. The
82// returned vector consists of pairs where 'first' is the delimited word, and
83// 'second' are the delimiters following that word.
84static void SplitStringDelims(
85 StringRef Source,
86 SmallVectorImpl<std::pair<StringRef, StringRef>> &OutFragments,
87 function_ref<bool(char)> IsLegalChar) {
88 // The beginning of the input string.
89 const auto Head = Source.begin();
90
91 // Obtain any leading delimiters.
92 auto Start = std::find_if(Head, Source.end(), IsLegalChar);
93 if (Start != Head)
94 OutFragments.push_back({"", Source.slice(0, Start - Head)});
95
96 // Capture each word and the delimiters following that word.
97 while (Start != Source.end()) {
98 Start = std::find_if(Start, Source.end(), IsLegalChar);
99 auto End = std::find_if_not(Start, Source.end(), IsLegalChar);
100 auto DEnd = std::find_if(End, Source.end(), IsLegalChar);
101 OutFragments.push_back({Source.slice(Start - Head, End - Head),
102 Source.slice(End - Head, DEnd - Head)});
103 Start = DEnd;
104 }
105}
106
107// This returns true if 'C' is a character that can show up in an
108// Itanium-mangled string.
109static bool IsLegalItaniumChar(char C) {
110 // Itanium CXX ABI [External Names]p5.1.1:
111 // '$' and '.' in mangled names are reserved for private implementations.
112 return isalnum(C) || C == '.' || C == '$' || C == '_';
113}
114
Matt Davis22c21932019-02-11 20:30:53 +0000115// If 'Split' is true, then 'Mangled' is broken into individual words and each
116// word is demangled. Otherwise, the entire string is treated as a single
117// mangled item. The result is output to 'OS'.
118static void demangleLine(llvm::raw_ostream &OS, StringRef Mangled, bool Split) {
119 std::string Result;
120 if (Split) {
Matt Davis1d5c2352019-02-27 21:39:11 +0000121 SmallVector<std::pair<StringRef, StringRef>, 16> Words;
122 SplitStringDelims(Mangled, Words, IsLegalItaniumChar);
123 for (const auto &Word : Words)
124 Result += demangle(OS, Word.first) + Word.second.str();
Matt Davis22c21932019-02-11 20:30:53 +0000125 } else
126 Result = demangle(OS, Mangled);
127 OS << Result << '\n';
128 OS.flush();
Saleem Abdulrasool70918202016-11-13 20:43:38 +0000129}
130
Rafael Espindolab940b662016-09-06 19:16:48 +0000131int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000132 InitLLVM X(argc, argv);
Saleem Abdulrasoolf1790c22017-01-20 05:27:09 +0000133
134 cl::ParseCommandLineOptions(argc, argv, "llvm symbol undecoration tool\n");
Saleem Abdulrasoola63fd042017-01-20 04:25:26 +0000135
136 if (Decorated.empty())
Saleem Abdulrasool70918202016-11-13 20:43:38 +0000137 for (std::string Mangled; std::getline(std::cin, Mangled);)
Matt Davis22c21932019-02-11 20:30:53 +0000138 demangleLine(llvm::outs(), Mangled, true);
Saleem Abdulrasool70918202016-11-13 20:43:38 +0000139 else
Saleem Abdulrasoola63fd042017-01-20 04:25:26 +0000140 for (const auto &Symbol : Decorated)
Matt Davis22c21932019-02-11 20:30:53 +0000141 demangleLine(llvm::outs(), Symbol, false);
Saleem Abdulrasool70918202016-11-13 20:43:38 +0000142
143 return EXIT_SUCCESS;
Rafael Espindolab940b662016-09-06 19:16:48 +0000144}