blob: 780ec6e3d52f9fe9594f158d9d1177ea805fdb67 [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
9#include "llvm/Demangle/Demangle.h"
Saleem Abdulrasoola63fd042017-01-20 04:25:26 +000010#include "llvm/Support/CommandLine.h"
Rui Ueyama197194b2018-04-13 18:26:06 +000011#include "llvm/Support/InitLLVM.h"
Davide Italianoc37eb112016-09-27 18:50:30 +000012#include "llvm/Support/raw_ostream.h"
Saleem Abdulrasool70918202016-11-13 20:43:38 +000013#include <cstdlib>
14#include <iostream>
Rafael Espindolab940b662016-09-06 19:16:48 +000015
16using namespace llvm;
17
Saleem Abdulrasool4c33f7f2017-01-21 02:36:26 +000018enum Style {
19 Auto, ///< auto-detect mangling
20 GNU, ///< GNU
21 Lucid, ///< Lucid compiler (lcc)
22 ARM,
23 HP, ///< HP compiler (xCC)
24 EDG, ///< EDG compiler
25 GNUv3, ///< GNU C++ v3 ABI
26 Java, ///< Java (gcj)
27 GNAT ///< ADA copiler (gnat)
28};
29static cl::opt<Style>
30 Format("format", cl::desc("decoration style"),
31 cl::values(clEnumValN(Auto, "auto", "auto-detect style"),
32 clEnumValN(GNU, "gnu", "GNU (itanium) style")),
33 cl::init(Auto));
34static cl::alias FormatShort("s", cl::desc("alias for --format"),
35 cl::aliasopt(Format));
36
Saleem Abdulrasoole5f6daa2017-01-22 17:41:10 +000037static cl::opt<bool> StripUnderscore("strip-underscore",
38 cl::desc("strip the leading underscore"),
39 cl::init(false));
40static cl::alias StripUnderscoreShort("_",
41 cl::desc("alias for --strip-underscore"),
42 cl::aliasopt(StripUnderscore));
43
Saleem Abdulrasoola63fd042017-01-20 04:25:26 +000044static cl::opt<bool>
45 Types("types",
46 cl::desc("attempt to demangle types as well as function names"),
47 cl::init(false));
48static cl::alias TypesShort("t", cl::desc("alias for --types"),
49 cl::aliasopt(Types));
50
51static cl::list<std::string>
52Decorated(cl::Positional, cl::desc("<mangled>"), cl::ZeroOrMore);
53
Saleem Abdulrasoolc8bcda22017-01-19 02:58:46 +000054static void demangle(llvm::raw_ostream &OS, const std::string &Mangled) {
Saleem Abdulrasool70918202016-11-13 20:43:38 +000055 int Status;
Saleem Abdulrasoole5f6daa2017-01-22 17:41:10 +000056
57 const char *Decorated = Mangled.c_str();
58 if (StripUnderscore)
59 if (Decorated[0] == '_')
60 ++Decorated;
61 size_t DecoratedLength = strlen(Decorated);
62
63 char *Undecorated = nullptr;
64
65 if (Types || ((DecoratedLength >= 2 && strncmp(Decorated, "_Z", 2) == 0) ||
66 (DecoratedLength >= 4 && strncmp(Decorated, "___Z", 4) == 0)))
67 Undecorated = itaniumDemangle(Decorated, nullptr, nullptr, &Status);
68
Saleem Abdulrasool1723b8b2017-03-22 21:15:19 +000069 if (!Undecorated &&
70 (DecoratedLength > 6 && strncmp(Decorated, "__imp_", 6) == 0)) {
71 OS << "import thunk for ";
72 Undecorated = itaniumDemangle(Decorated + 6, nullptr, nullptr, &Status);
73 }
74
Saleem Abdulrasoole5f6daa2017-01-22 17:41:10 +000075 OS << (Undecorated ? Undecorated : Mangled) << '\n';
Adam Nemet95e0c5f2017-11-29 17:07:41 +000076 OS.flush();
Saleem Abdulrasoole5f6daa2017-01-22 17:41:10 +000077
78 free(Undecorated);
Saleem Abdulrasool70918202016-11-13 20:43:38 +000079}
80
Rafael Espindolab940b662016-09-06 19:16:48 +000081int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +000082 InitLLVM X(argc, argv);
Saleem Abdulrasoolf1790c22017-01-20 05:27:09 +000083
84 cl::ParseCommandLineOptions(argc, argv, "llvm symbol undecoration tool\n");
Saleem Abdulrasoola63fd042017-01-20 04:25:26 +000085
86 if (Decorated.empty())
Saleem Abdulrasool70918202016-11-13 20:43:38 +000087 for (std::string Mangled; std::getline(std::cin, Mangled);)
Saleem Abdulrasoolc8bcda22017-01-19 02:58:46 +000088 demangle(llvm::outs(), Mangled);
Saleem Abdulrasool70918202016-11-13 20:43:38 +000089 else
Saleem Abdulrasoola63fd042017-01-20 04:25:26 +000090 for (const auto &Symbol : Decorated)
91 demangle(llvm::outs(), Symbol);
Saleem Abdulrasool70918202016-11-13 20:43:38 +000092
93 return EXIT_SUCCESS;
Rafael Espindolab940b662016-09-06 19:16:48 +000094}