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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-02-12 04:30:53 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
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)
|
2019-02-15 10:43:14 +08:00
|
|
|
GNAT ///< ADA compiler (gnat)
|
2017-01-21 10:36:26 +08:00
|
|
|
};
|
|
|
|
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);
|
|
|
|
|
2019-02-12 04:30:53 +08:00
|
|
|
static std::string 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);
|
|
|
|
}
|
|
|
|
|
2019-02-12 04:30:53 +08:00
|
|
|
std::string Result(Undecorated ? Undecorated : Mangled);
|
2017-01-23 01:41:10 +08:00
|
|
|
free(Undecorated);
|
2019-02-12 04:30:53 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If 'Split' is true, then 'Mangled' is broken into individual words and each
|
|
|
|
// word is demangled. Otherwise, the entire string is treated as a single
|
|
|
|
// mangled item. The result is output to 'OS'.
|
|
|
|
static void demangleLine(llvm::raw_ostream &OS, StringRef Mangled, bool Split) {
|
|
|
|
std::string Result;
|
|
|
|
if (Split) {
|
2019-02-28 03:52:02 +08:00
|
|
|
SmallVector<StringRef, 16> Words;
|
|
|
|
SplitString(Mangled, Words);
|
|
|
|
for (auto Word : Words)
|
|
|
|
Result += demangle(OS, Word) + ' ';
|
|
|
|
// Remove the trailing space character.
|
|
|
|
if (Result.back() == ' ')
|
|
|
|
Result.pop_back();
|
2019-02-12 04:30:53 +08:00
|
|
|
} else
|
|
|
|
Result = demangle(OS, Mangled);
|
|
|
|
OS << Result << '\n';
|
|
|
|
OS.flush();
|
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);)
|
2019-02-12 04:30:53 +08:00
|
|
|
demangleLine(llvm::outs(), Mangled, true);
|
2016-11-14 04:43:38 +08:00
|
|
|
else
|
2017-01-20 12:25:26 +08:00
|
|
|
for (const auto &Symbol : Decorated)
|
2019-02-12 04:30:53 +08:00
|
|
|
demangleLine(llvm::outs(), Symbol, false);
|
2016-11-14 04:43:38 +08:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2016-09-07 03:16:48 +08:00
|
|
|
}
|