1 //===- DriverUtils.cpp ----------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains utility functions for the driver. Because there
11 // are so many small functions, we created this separate file to make
12 // Driver.cpp less cluttered.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "Driver.h"
17 #include "Error.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/StringSaver.h"
21 
22 using namespace llvm;
23 
24 using namespace lld;
25 using namespace lld::elf2;
26 
27 // Create OptTable
28 
29 // Create prefix string literals used in Options.td
30 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
31 #include "Options.inc"
32 #undef PREFIX
33 
34 // Create table mapping all options defined in Options.td
35 static const opt::OptTable::Info infoTable[] = {
36 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10)            \
37   {                                                                            \
38     X1, X2, X9, X10, OPT_##ID, opt::Option::KIND##Class, X8, X7, OPT_##GROUP,  \
39         OPT_##ALIAS, X6                                                        \
40   }                                                                            \
41   ,
42 #include "Options.inc"
43 #undef OPTION
44 };
45 
46 class ELFOptTable : public opt::OptTable {
47 public:
48   ELFOptTable() : OptTable(infoTable, array_lengthof(infoTable)) {}
49 };
50 
51 // Parses a given list of options.
52 opt::InputArgList ArgParser::parse(ArrayRef<const char *> Argv) {
53   // Make InputArgList from string vectors.
54   ELFOptTable Table;
55   unsigned MissingIndex;
56   unsigned MissingCount;
57 
58   // Expand response files. '@<filename>' is replaced by the file's contents.
59   SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size());
60   StringSaver Saver(Alloc);
61   llvm::cl::ExpandResponseFiles(Saver, llvm::cl::TokenizeGNUCommandLine, Vec);
62 
63   // Parse options and then do error checking.
64   opt::InputArgList Args = Table.ParseArgs(Vec, MissingIndex, MissingCount);
65   if (MissingCount)
66     error(Twine("missing arg value for \"") + Args.getArgString(MissingIndex) +
67           "\", expected " + Twine(MissingCount) +
68           (MissingCount == 1 ? " argument.\n" : " arguments"));
69 
70   iterator_range<opt::arg_iterator> Unknowns = Args.filtered(OPT_UNKNOWN);
71   for (auto *Arg : Unknowns)
72     warning("warning: unknown argument: " + Arg->getSpelling());
73   if (Unknowns.begin() != Unknowns.end())
74     error("unknown argument(s) found");
75 
76   return Args;
77 }
78