Index: ELF/Driver.h =================================================================== --- ELF/Driver.h +++ ELF/Driver.h @@ -38,10 +38,6 @@ std::vector> OwningMBs; }; -// Parses command line options. -llvm::opt::InputArgList parseArgs(llvm::BumpPtrAllocator *A, - ArrayRef Args); - // Create enum with OPT_xxx values for each option in Options.td enum { OPT_INVALID = 0, @@ -50,7 +46,6 @@ #undef OPTION }; -void printHelp(const char *Argv0); void printVersion(); std::string findFromSearchPaths(StringRef Path); Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -18,6 +18,7 @@ #include "Writer.h" #include "lld/Driver/Driver.h" #include "llvm/ADT/StringExtras.h" +#include "llvm/Support/CommandLine.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/raw_ostream.h" #include @@ -32,6 +33,62 @@ Configuration *elf::Config; LinkerDriver *elf::Driver; +// Create OptTable +// Create prefix string literals used in Options.td +#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#include "Options.inc" +#undef PREFIX + +// Create table mapping all options defined in Options.td +static const opt::OptTable::Info infoTable[] = { +#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \ + { \ + X1, X2, X9, X10, OPT_##ID, opt::Option::KIND##Class, X8, X7, OPT_##GROUP, \ + OPT_##ALIAS, X6 \ + } \ + , +#include "Options.inc" +#undef OPTION +}; + +class ELFOptTable : public opt::OptTable { +public: + ELFOptTable() : OptTable(infoTable) {} +}; + +// Parses a given list of options. +static opt::InputArgList parseArgs(llvm::BumpPtrAllocator *A, + ArrayRef Argv, + ELFOptTable &Table) { + // Make InputArgList from string vectors. + unsigned MissingIndex; + unsigned MissingCount; + + // Expand response files. '@' is replaced by the file's contents. + SmallVector Vec(Argv.data(), Argv.data() + Argv.size()); + StringSaver Saver(*A); + llvm::cl::ExpandResponseFiles(Saver, llvm::cl::TokenizeGNUCommandLine, Vec); + + // Parse options and then do error checking. + opt::InputArgList Args = Table.ParseArgs(Vec, MissingIndex, MissingCount); + if (MissingCount) + error(Twine("missing arg value for \"") + Args.getArgString(MissingIndex) + + "\", expected " + Twine(MissingCount) + + (MissingCount == 1 ? " argument.\n" : " arguments")); + + iterator_range Unknowns = Args.filtered(OPT_UNKNOWN); + for (auto *Arg : Unknowns) + warning("warning: unknown argument: " + Arg->getSpelling()); + if (Unknowns.begin() != Unknowns.end()) + error("unknown argument(s) found"); + return Args; +} + +static void printHelp(const char *Argv0) { + ELFOptTable Table; + Table.PrintHelp(outs(), Argv0, "lld", false); +} + bool elf::link(ArrayRef Args, raw_ostream &Error) { HasError = false; ErrorOS = &Error; @@ -170,7 +227,8 @@ } void LinkerDriver::main(ArrayRef ArgsArr) { - opt::InputArgList Args = parseArgs(&Alloc, ArgsArr.slice(1)); + ELFOptTable Table; + opt::InputArgList Args = parseArgs(&Alloc, ArgsArr.slice(1), Table); if (Args.hasArg(OPT_help)) { printHelp(ArgsArr[0]); return; Index: ELF/DriverUtils.cpp =================================================================== --- ELF/DriverUtils.cpp +++ ELF/DriverUtils.cpp @@ -17,7 +17,6 @@ #include "Error.h" #include "lld/Config/Version.h" #include "llvm/ADT/STLExtras.h" -#include "llvm/Support/CommandLine.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" #include "llvm/Support/StringSaver.h" @@ -27,63 +26,6 @@ using namespace lld; using namespace lld::elf; -// Create OptTable - -// Create prefix string literals used in Options.td -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; -#include "Options.inc" -#undef PREFIX - -// Create table mapping all options defined in Options.td -static const opt::OptTable::Info infoTable[] = { -#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \ - { \ - X1, X2, X9, X10, OPT_##ID, opt::Option::KIND##Class, X8, X7, OPT_##GROUP, \ - OPT_##ALIAS, X6 \ - } \ - , -#include "Options.inc" -#undef OPTION -}; - -class ELFOptTable : public opt::OptTable { -public: - ELFOptTable() : OptTable(infoTable) {} -}; - -// Parses a given list of options. -opt::InputArgList elf::parseArgs(llvm::BumpPtrAllocator *A, - ArrayRef Argv) { - // Make InputArgList from string vectors. - ELFOptTable Table; - unsigned MissingIndex; - unsigned MissingCount; - - // Expand response files. '@' is replaced by the file's contents. - SmallVector Vec(Argv.data(), Argv.data() + Argv.size()); - StringSaver Saver(*A); - llvm::cl::ExpandResponseFiles(Saver, llvm::cl::TokenizeGNUCommandLine, Vec); - - // Parse options and then do error checking. - opt::InputArgList Args = Table.ParseArgs(Vec, MissingIndex, MissingCount); - if (MissingCount) - error(Twine("missing arg value for \"") + Args.getArgString(MissingIndex) + - "\", expected " + Twine(MissingCount) + - (MissingCount == 1 ? " argument.\n" : " arguments")); - - iterator_range Unknowns = Args.filtered(OPT_UNKNOWN); - for (auto *Arg : Unknowns) - warning("warning: unknown argument: " + Arg->getSpelling()); - if (Unknowns.begin() != Unknowns.end()) - error("unknown argument(s) found"); - return Args; -} - -void elf::printHelp(const char *Argv0) { - ELFOptTable Table; - Table.PrintHelp(outs(), Argv0, "lld", false); -} - void elf::printVersion() { outs() << "LLD " << getLLDVersion(); std::string S = getLLDRepositoryVersion();