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
3739static 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
6082static Expected<SmallVector<std::string, 16 >>
6183getDWOFilenames (StringRef ExecFilename) {
@@ -106,15 +128,41 @@ static Expected<Triple> readTargetTriple(StringRef FileName) {
106128int 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) {
0 commit comments