2016-09-07 03:16:48 +08:00
|
|
|
//===-- llvm-c++filt.cpp --------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-09-07 03:16:48 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Demangle/Demangle.h"
|
2017-01-20 12:25:26 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2018-04-14 02:26:06 +08:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2016-09-28 02:50:30 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-11-14 04:43:38 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
2016-09-07 03:16:48 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2017-01-21 10:36:26 +08:00
|
|
|
enum Style {
|
|
|
|
Auto, ///< auto-detect mangling
|
|
|
|
GNU, ///< GNU
|
|
|
|
Lucid, ///< Lucid compiler (lcc)
|
|
|
|
ARM,
|
|
|
|
HP, ///< HP compiler (xCC)
|
|
|
|
EDG, ///< EDG compiler
|
|
|
|
GNUv3, ///< GNU C++ v3 ABI
|
|
|
|
Java, ///< Java (gcj)
|
|
|
|
GNAT ///< ADA copiler (gnat)
|
|
|
|
};
|
|
|
|
static cl::opt<Style>
|
|
|
|
Format("format", cl::desc("decoration style"),
|
|
|
|
cl::values(clEnumValN(Auto, "auto", "auto-detect style"),
|
|
|
|
clEnumValN(GNU, "gnu", "GNU (itanium) style")),
|
|
|
|
cl::init(Auto));
|
|
|
|
static cl::alias FormatShort("s", cl::desc("alias for --format"),
|
|
|
|
cl::aliasopt(Format));
|
|
|
|
|
2017-01-23 01:41:10 +08:00
|
|
|
static cl::opt<bool> StripUnderscore("strip-underscore",
|
|
|
|
cl::desc("strip the leading underscore"),
|
|
|
|
cl::init(false));
|
|
|
|
static cl::alias StripUnderscoreShort("_",
|
|
|
|
cl::desc("alias for --strip-underscore"),
|
|
|
|
cl::aliasopt(StripUnderscore));
|
|
|
|
|
2017-01-20 12:25:26 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
Types("types",
|
|
|
|
cl::desc("attempt to demangle types as well as function names"),
|
|
|
|
cl::init(false));
|
|
|
|
static cl::alias TypesShort("t", cl::desc("alias for --types"),
|
|
|
|
cl::aliasopt(Types));
|
|
|
|
|
|
|
|
static cl::list<std::string>
|
|
|
|
Decorated(cl::Positional, cl::desc("<mangled>"), cl::ZeroOrMore);
|
|
|
|
|
2017-01-19 10:58:46 +08:00
|
|
|
static void demangle(llvm::raw_ostream &OS, const std::string &Mangled) {
|
2016-11-14 04:43:38 +08:00
|
|
|
int Status;
|
2017-01-23 01:41:10 +08:00
|
|
|
|
|
|
|
const char *Decorated = Mangled.c_str();
|
|
|
|
if (StripUnderscore)
|
|
|
|
if (Decorated[0] == '_')
|
|
|
|
++Decorated;
|
|
|
|
size_t DecoratedLength = strlen(Decorated);
|
|
|
|
|
|
|
|
char *Undecorated = nullptr;
|
|
|
|
|
|
|
|
if (Types || ((DecoratedLength >= 2 && strncmp(Decorated, "_Z", 2) == 0) ||
|
|
|
|
(DecoratedLength >= 4 && strncmp(Decorated, "___Z", 4) == 0)))
|
|
|
|
Undecorated = itaniumDemangle(Decorated, nullptr, nullptr, &Status);
|
|
|
|
|
2017-03-23 05:15:19 +08:00
|
|
|
if (!Undecorated &&
|
|
|
|
(DecoratedLength > 6 && strncmp(Decorated, "__imp_", 6) == 0)) {
|
|
|
|
OS << "import thunk for ";
|
|
|
|
Undecorated = itaniumDemangle(Decorated + 6, nullptr, nullptr, &Status);
|
|
|
|
}
|
|
|
|
|
2017-01-23 01:41:10 +08:00
|
|
|
OS << (Undecorated ? Undecorated : Mangled) << '\n';
|
2017-11-30 01:07:41 +08:00
|
|
|
OS.flush();
|
2017-01-23 01:41:10 +08:00
|
|
|
|
|
|
|
free(Undecorated);
|
2016-11-14 04:43:38 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 03:16:48 +08:00
|
|
|
int main(int argc, char **argv) {
|
2018-04-14 02:26:06 +08:00
|
|
|
InitLLVM X(argc, argv);
|
2017-01-20 13:27:09 +08:00
|
|
|
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm symbol undecoration tool\n");
|
2017-01-20 12:25:26 +08:00
|
|
|
|
|
|
|
if (Decorated.empty())
|
2016-11-14 04:43:38 +08:00
|
|
|
for (std::string Mangled; std::getline(std::cin, Mangled);)
|
2017-01-19 10:58:46 +08:00
|
|
|
demangle(llvm::outs(), Mangled);
|
2016-11-14 04:43:38 +08:00
|
|
|
else
|
2017-01-20 12:25:26 +08:00
|
|
|
for (const auto &Symbol : Decorated)
|
|
|
|
demangle(llvm::outs(), Symbol);
|
2016-11-14 04:43:38 +08:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2016-09-07 03:16:48 +08:00
|
|
|
}
|