Skip to content

Commit 4f92557

Browse files
author
Andrés Villegas
committed
[NFC][llvm-dwp] Switch from llvm::cl to OptTable
Switch the parse of command line options from llvm::cl to OptTable. The motivation for this change is to continue adding llvm based tools to the llvm driver multicall. For more information about the proposal and motivation, please see https://discourse.llvm.org/t/rfc-llvm-busybox-proposal/58494 Reviewed By: abrachet Differential Revision: https://reviews.llvm.org/D154642
1 parent b76c85b commit 4f92557

6 files changed

Lines changed: 110 additions & 23 deletions

File tree

llvm/test/tools/llvm-dwp/help.test

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@
22

33
# HELP: OVERVIEW
44
# HELP: USAGE
5-
# HELP: Color Options
6-
# HELP: Generic Options
7-
# HELP: Specific Options
5+
# HELP: OPTIONS

llvm/tools/llvm-dwp/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@ set(LLVM_LINK_COMPONENTS
66
DWP
77
MC
88
Object
9+
Option
910
Support
1011
TargetParser
1112
)
1213

14+
set(LLVM_TARGET_DEFINITIONS Opts.td)
15+
tablegen(LLVM Opts.inc -gen-opt-parser-defs)
16+
add_public_tablegen_target(DwpOptsTableGen)
17+
1318
add_llvm_tool(llvm-dwp
1419
llvm-dwp.cpp
1520

1621
DEPENDS
1722
intrinsics_gen
23+
DwpOptsTableGen
1824
)
1925

2026
if(LLVM_INSTALL_BINUTILS_SYMLINKS)

llvm/tools/llvm-dwp/Opts.td

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
include "llvm/Option/OptParser.td"
2+
3+
class F<string name, string help> : Flag<["-", "--"], name>, HelpText<help>;
4+
class S<string name, string help> : Separate<["-", "--"], name>, HelpText<help>;
5+
6+
def help : F<"help", "Display this help">;
7+
def : F<"h", "Alias for --help">, Alias<help>;
8+
def version : F<"version", "Display the version of this program">;
9+
10+
def execFileNames : S<"e", "Specify the executable/library files to get the list of *.dwo from.">, MetaVarName<"<filename>">;
11+
def outputFileName : S<"o", "Specify the output file.">, MetaVarName<"<filename>">;
12+
def continueOnCuIndexOverflow: F<"continue-on-cu-index-overflow", "This turns an error when offset for .debug_*.dwo sections "
13+
"overfolws into a warning.">, MetaVarName<"<filename>">;

llvm/tools/llvm-dwp/llvm-dwp.cpp

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "llvm/MC/MCSubtargetInfo.h"
2424
#include "llvm/MC/MCTargetOptionsCommandFlags.h"
2525
#include "llvm/MC/TargetRegistry.h"
26+
#include "llvm/Option/ArgList.h"
27+
#include "llvm/Option/Option.h"
2628
#include "llvm/Support/CommandLine.h"
2729
#include "llvm/Support/FileSystem.h"
2830
#include "llvm/Support/InitLLVM.h"
@@ -36,26 +38,46 @@ using namespace llvm::object;
3638

3739
static mc::RegisterMCTargetOptionsFlags MCTargetOptionsFlags;
3840

39-
cl::OptionCategory DwpCategory("Specific Options");
40-
static cl::list<std::string>
41-
InputFiles(cl::Positional, cl::desc("<input files>"), cl::cat(DwpCategory));
41+
// Command-line option boilerplate.
42+
namespace {
43+
enum ID {
44+
OPT_INVALID = 0, // This is not an option ID.
45+
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
46+
HELPTEXT, METAVAR, VALUES) \
47+
OPT_##ID,
48+
#include "Opts.inc"
49+
#undef OPTION
50+
};
4251

43-
static cl::list<std::string> ExecFilenames(
44-
"e",
45-
cl::desc(
46-
"Specify the executable/library files to get the list of *.dwo from"),
47-
cl::value_desc("filename"), cl::cat(DwpCategory));
52+
#define PREFIX(NAME, VALUE) \
53+
static constexpr StringLiteral NAME##_init[] = VALUE; \
54+
static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
55+
std::size(NAME##_init) - 1);
56+
#include "Opts.inc"
57+
#undef PREFIX
4858

49-
static cl::opt<std::string> OutputFilename(cl::Required, "o",
50-
cl::desc("Specify the output file."),
51-
cl::value_desc("filename"),
52-
cl::cat(DwpCategory));
59+
static constexpr opt::OptTable::Info InfoTable[] = {
60+
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
61+
HELPTEXT, METAVAR, VALUES) \
62+
{ \
63+
PREFIX, NAME, HELPTEXT, \
64+
METAVAR, OPT_##ID, opt::Option::KIND##Class, \
65+
PARAM, FLAGS, OPT_##GROUP, \
66+
OPT_##ALIAS, ALIASARGS, VALUES},
67+
#include "Opts.inc"
68+
#undef OPTION
69+
};
5370

54-
static cl::opt<bool> ContinueOnCuIndexOverflow(
55-
"continue-on-cu-index-overflow",
56-
cl::desc("This turns an error when offset for .debug_*.dwo sections "
57-
"overfolws into a warning."),
58-
cl::cat(DwpCategory));
71+
class DwpOptTable : public opt::GenericOptTable {
72+
public:
73+
DwpOptTable() : GenericOptTable(InfoTable) {}
74+
};
75+
} // end anonymous namespace
76+
77+
// Options
78+
static std::vector<std::string> ExecFilenames;
79+
static std::string OutputFilename;
80+
static bool ContinueOnCuIndexOverflow;
5981

6082
static Expected<SmallVector<std::string, 16>>
6183
getDWOFilenames(StringRef ExecFilename) {
@@ -106,15 +128,41 @@ static Expected<Triple> readTargetTriple(StringRef FileName) {
106128
int main(int argc, char **argv) {
107129
InitLLVM X(argc, argv);
108130

109-
cl::HideUnrelatedOptions({&DwpCategory, &getColorCategory()});
110-
cl::ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files\n");
131+
DwpOptTable Tbl;
132+
llvm::BumpPtrAllocator A;
133+
llvm::StringSaver Saver{A};
134+
opt::InputArgList Args =
135+
Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) {
136+
llvm::errs() << Msg << '\n';
137+
std::exit(1);
138+
});
139+
140+
if (Args.hasArg(OPT_help)) {
141+
Tbl.printHelp(llvm::outs(), "llvm-dwp [options] <input files>",
142+
"merge split dwarf (.dwo) files");
143+
std::exit(0);
144+
}
145+
146+
if (Args.hasArg(OPT_version)) {
147+
llvm::cl::PrintVersionMessage();
148+
std::exit(0);
149+
}
150+
151+
OutputFilename = Args.getLastArgValue(OPT_outputFileName, "");
152+
ContinueOnCuIndexOverflow = Args.hasArg(OPT_continueOnCuIndexOverflow);
153+
154+
for (const llvm::opt::Arg *A : Args.filtered(OPT_execFileNames))
155+
ExecFilenames.emplace_back(A->getValue());
156+
157+
std::vector<std::string> DWOFilenames;
158+
for (const llvm::opt::Arg *A : Args.filtered(OPT_INPUT))
159+
DWOFilenames.emplace_back(A->getValue());
111160

112161
llvm::InitializeAllTargetInfos();
113162
llvm::InitializeAllTargetMCs();
114163
llvm::InitializeAllTargets();
115164
llvm::InitializeAllAsmPrinters();
116165

117-
std::vector<std::string> DWOFilenames = InputFiles;
118166
for (const auto &ExecFilename : ExecFilenames) {
119167
auto DWOs = getDWOFilenames(ExecFilename);
120168
if (!DWOs) {

llvm/utils/gn/secondary/llvm/tools/llvm-dwp/BUILD.gn

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import("//llvm/tools/binutils_symlinks.gni")
22
import("//llvm/utils/gn/build/symlink_or_copy.gni")
3+
import("//llvm/utils/TableGen/tablegen.gni")
4+
5+
tablegen("Opts") {
6+
visibility = [ ":llvm-dwp" ]
7+
args = [ "-gen-opt-parser-defs" ]
8+
}
39

410
if (llvm_install_binutils_symlinks) {
511
symlink_or_copy("dwp") {
@@ -19,10 +25,12 @@ group("symlinks") {
1925

2026
executable("llvm-dwp") {
2127
deps = [
28+
":Opts",
2229
"//llvm/lib/DWP",
2330
"//llvm/lib/DebugInfo/DWARF",
2431
"//llvm/lib/MC",
2532
"//llvm/lib/Object",
33+
"//llvm/lib/Option",
2634
"//llvm/lib/Support",
2735
"//llvm/lib/Target:TargetsToBuild",
2836
"//llvm/lib/TargetParser",

utils/bazel/llvm-project-overlay/llvm/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3491,6 +3491,18 @@ cc_binary(
34913491
],
34923492
)
34933493

3494+
gentbl(
3495+
name = "DwpOptionsTableGen",
3496+
strip_include_prefix = "tools/llvm-dwp",
3497+
tbl_outs = [(
3498+
"-gen-opt-parser-defs",
3499+
"tools/llvm-dwp/Opts.inc",
3500+
)],
3501+
tblgen = ":llvm-tblgen",
3502+
td_file = "tools/llvm-dwp/Opts.td",
3503+
td_srcs = ["include/llvm/Option/OptParser.td"],
3504+
)
3505+
34943506
cc_binary(
34953507
name = "llvm-dwp",
34963508
srcs = glob([
@@ -3502,7 +3514,9 @@ cc_binary(
35023514
deps = [
35033515
":AllTargetsCodeGens",
35043516
":DWP",
3517+
":DwpOptionsTableGen",
35053518
":MC",
3519+
":Option",
35063520
":Support",
35073521
],
35083522
)

0 commit comments

Comments
 (0)