1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-25 04:02:41 +01:00

[llvm-cxxfilt] Switch command line parsing from llvm::cl to OptTable

Similar to D104889. The tool is very simple and its long options are uncommon,
so just drop the one-dash form in this patch.

Reviewed By: jhenderson

Differential Revision: https://reviews.llvm.org/D105605
This commit is contained in:
Fangrui Song 2021-07-09 10:10:45 -07:00
parent 74d215416a
commit 6c0a5fae66
6 changed files with 114 additions and 56 deletions

View File

@ -48,10 +48,6 @@ OPTIONS
Print a summary of command line options. Print a summary of command line options.
.. option:: --help-list
Print an uncategorized summary of command line options.
.. option:: --no-strip-underscore, -n .. option:: --no-strip-underscore, -n
Do not strip a leading underscore. This is the default for all platforms Do not strip a leading underscore. This is the default for all platforms

View File

@ -1,7 +1,7 @@
RUN: llvm-cxxfilt -h | FileCheck %s RUN: llvm-cxxfilt -h | FileCheck %s
RUN: llvm-cxxfilt --help | FileCheck %s RUN: llvm-cxxfilt --help | FileCheck %s
CHECK: OVERVIEW: llvm symbol undecoration tool CHECK: OVERVIEW: LLVM symbol undecoration tool
CHECK: USAGE: llvm-cxxfilt{{(.exe)?}} [options] <mangled>{{$}} CHECK: USAGE: {{.*}}llvm-cxxfilt{{(.exe)?}} [options] <mangled>{{$}}
CHECK: OPTIONS: CHECK: OPTIONS:
CHECK: @FILE CHECK: @FILE

View File

@ -1,10 +1,18 @@
set(LLVM_LINK_COMPONENTS set(LLVM_LINK_COMPONENTS
Demangle Demangle
Option
Support Support
) )
set(LLVM_TARGET_DEFINITIONS Opts.td)
tablegen(LLVM Opts.inc -gen-opt-parser-defs)
add_public_tablegen_target(CxxfiltOptsTableGen)
add_llvm_tool(llvm-cxxfilt add_llvm_tool(llvm-cxxfilt
llvm-cxxfilt.cpp llvm-cxxfilt.cpp
DEPENDS
CxxfiltOptsTableGen
) )
if(LLVM_INSTALL_BINUTILS_SYMLINKS) if(LLVM_INSTALL_BINUTILS_SYMLINKS)

View File

@ -0,0 +1,28 @@
include "llvm/Option/OptParser.td"
class F<string letter, string help> : Flag<["-"], letter>, HelpText<help>;
class FF<string name, string help> : Flag<["--"], name>, HelpText<help>;
multiclass BB<string name, string help1, string help2> {
def NAME: Flag<["--"], name>, HelpText<help1>;
def no_ # NAME: Flag<["--"], "no-" # name>, HelpText<help2>;
}
multiclass Eq<string name, string help> {
def NAME #_EQ : Joined<["--"], name #"=">,
HelpText<help>;
def : Separate<["--"], name>, Alias<!cast<Joined>(NAME #_EQ)>;
}
def help : FF<"help", "Display this help">;
defm strip_underscore : BB<"strip-underscore", "Strip the leading underscore", "Don't strip the leading underscore">;
def types : FF<"types", "">;
def version : FF<"version", "Display the version">;
defm : Eq<"format", "Specify mangling format. Currently ignored because only 'gnu' is supported">;
def : F<"s", "Alias for --format">;
def : F<"_", "Alias for --strip-underscore">, Alias<strip_underscore>;
def : F<"h", "Alias for --help">, Alias<help>;
def : F<"n", "Alias for --no-strip-underscore">, Alias<no_strip_underscore>;
def : F<"t", "Alias for --types">, Alias<types>;

View File

@ -9,69 +9,59 @@
#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/Triple.h" #include "llvm/ADT/Triple.h"
#include "llvm/Demangle/Demangle.h" #include "llvm/Demangle/Demangle.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Option/Option.h"
#include "llvm/Support/CommandLine.h" #include "llvm/Support/CommandLine.h"
#include "llvm/Support/Host.h" #include "llvm/Support/Host.h"
#include "llvm/Support/InitLLVM.h" #include "llvm/Support/InitLLVM.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
using namespace llvm; using namespace llvm;
enum Style { namespace {
Auto, ///< auto-detect mangling enum ID {
GNU, ///< GNU OPT_INVALID = 0, // This is not an option ID.
Lucid, ///< Lucid compiler (lcc) #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
ARM, HELPTEXT, METAVAR, VALUES) \
HP, ///< HP compiler (xCC) OPT_##ID,
EDG, ///< EDG compiler #include "Opts.inc"
GNUv3, ///< GNU C++ v3 ABI #undef OPTION
Java, ///< Java (gcj)
GNAT ///< ADA compiler (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));
static cl::opt<bool> StripUnderscore("strip-underscore", #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
cl::desc("strip the leading underscore"), #include "Opts.inc"
cl::init(false)); #undef PREFIX
static cl::alias StripUnderscoreShort("_",
cl::desc("alias for --strip-underscore"),
cl::aliasopt(StripUnderscore));
static cl::opt<bool>
NoStripUnderscore("no-strip-underscore",
cl::desc("do not strip the leading underscore"),
cl::init(false));
static cl::alias
NoStripUnderscoreShort("n", cl::desc("alias for --no-strip-underscore"),
cl::aliasopt(NoStripUnderscore));
static cl::opt<bool> const opt::OptTable::Info InfoTable[] = {
Types("types", #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
cl::desc("attempt to demangle types as well as function names"), HELPTEXT, METAVAR, VALUES) \
cl::init(false)); { \
static cl::alias TypesShort("t", cl::desc("alias for --types"), PREFIX, NAME, HELPTEXT, \
cl::aliasopt(Types)); METAVAR, OPT_##ID, opt::Option::KIND##Class, \
PARAM, FLAGS, OPT_##GROUP, \
OPT_##ALIAS, ALIASARGS, VALUES},
#include "Opts.inc"
#undef OPTION
};
static cl::list<std::string> class CxxfiltOptTable : public opt::OptTable {
Decorated(cl::Positional, cl::desc("<mangled>"), cl::ZeroOrMore); public:
CxxfiltOptTable() : OptTable(InfoTable) { setGroupedShortOptions(true); }
};
} // namespace
static cl::extrahelp static bool StripUnderscore;
HelpResponse("\nPass @FILE as argument to read options from FILE.\n"); static bool Types;
static bool shouldStripUnderscore() { static StringRef ToolName;
if (StripUnderscore)
return true; static void error(const Twine &Message) {
if (NoStripUnderscore) WithColor::error(errs(), ToolName) << Message << '\n';
return false; exit(1);
// If none of them are set, use the default value for platform.
// macho has symbols prefix with "_" so strip by default.
return Triple(sys::getProcessTriple()).isOSBinFormatMachO();
} }
static std::string demangle(const std::string &Mangled) { static std::string demangle(const std::string &Mangled) {
@ -79,7 +69,7 @@ static std::string demangle(const std::string &Mangled) {
std::string Prefix; std::string Prefix;
const char *DecoratedStr = Mangled.c_str(); const char *DecoratedStr = Mangled.c_str();
if (shouldStripUnderscore()) if (StripUnderscore)
if (DecoratedStr[0] == '_') if (DecoratedStr[0] == '_')
++DecoratedStr; ++DecoratedStr;
size_t DecoratedLength = strlen(DecoratedStr); size_t DecoratedLength = strlen(DecoratedStr);
@ -159,9 +149,37 @@ static void demangleLine(llvm::raw_ostream &OS, StringRef Mangled, bool Split) {
int main(int argc, char **argv) { int main(int argc, char **argv) {
InitLLVM X(argc, argv); InitLLVM X(argc, argv);
BumpPtrAllocator A;
StringSaver Saver(A);
CxxfiltOptTable Tbl;
ToolName = argv[0];
opt::InputArgList Args = Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver,
[&](StringRef Msg) { error(Msg); });
if (Args.hasArg(OPT_help)) {
Tbl.printHelp(outs(),
(Twine(ToolName) + " [options] <mangled>").str().c_str(),
"LLVM symbol undecoration tool");
// TODO Replace this with OptTable API once it adds extrahelp support.
outs() << "\nPass @FILE as argument to read options from FILE.\n";
return 0;
}
if (Args.hasArg(OPT_version)) {
outs() << ToolName << '\n';
cl::PrintVersionMessage();
return 0;
}
cl::ParseCommandLineOptions(argc, argv, "llvm symbol undecoration tool\n"); // The default value depends on the default triple. Mach-O has symbols
// prefixed with "_", so strip by default.
if (opt::Arg *A =
Args.getLastArg(OPT_strip_underscore, OPT_no_strip_underscore))
StripUnderscore = A->getOption().matches(OPT_strip_underscore);
else
StripUnderscore = Triple(sys::getProcessTriple()).isOSBinFormatMachO();
Types = Args.hasArg(OPT_types);
std::vector<std::string> Decorated = Args.getAllArgValues(OPT_INPUT);
if (Decorated.empty()) if (Decorated.empty())
for (std::string Mangled; std::getline(std::cin, Mangled);) for (std::string Mangled; std::getline(std::cin, Mangled);)
demangleLine(llvm::outs(), Mangled, true); demangleLine(llvm::outs(), Mangled, true);

View File

@ -1,6 +1,12 @@
import("//llvm/tools/binutils_symlinks.gni") import("//llvm/tools/binutils_symlinks.gni")
import("//llvm/utils/TableGen/tablegen.gni")
import("//llvm/utils/gn/build/symlink_or_copy.gni") import("//llvm/utils/gn/build/symlink_or_copy.gni")
tablegen("Opts") {
visibility = [ ":llvm-cxxfilt" ]
args = [ "-gen-opt-parser-defs" ]
}
if (llvm_install_binutils_symlinks) { if (llvm_install_binutils_symlinks) {
symlink_or_copy("cxxfilt") { # Can't have '+' in target name. symlink_or_copy("cxxfilt") { # Can't have '+' in target name.
deps = [ ":llvm-cxxfilt" ] deps = [ ":llvm-cxxfilt" ]
@ -19,7 +25,9 @@ group("symlinks") {
executable("llvm-cxxfilt") { executable("llvm-cxxfilt") {
deps = [ deps = [
":Opts",
"//llvm/lib/Demangle", "//llvm/lib/Demangle",
"//llvm/lib/Option",
"//llvm/lib/Support", "//llvm/lib/Support",
] ]
sources = [ "llvm-cxxfilt.cpp" ] sources = [ "llvm-cxxfilt.cpp" ]