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 "llvm/ADT/STLExtras.h"
18 
19 using namespace llvm;
20 
21 using namespace lld;
22 using namespace lld::elf2;
23 
24 // Create OptTable
25 
26 // Create prefix string literals used in Options.td
27 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
28 #include "Options.inc"
29 #undef PREFIX
30 
31 // Create table mapping all options defined in Options.td
32 static const opt::OptTable::Info infoTable[] = {
33 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10)            \
34   {                                                                            \
35     X1, X2, X9, X10, OPT_##ID, opt::Option::KIND##Class, X8, X7, OPT_##GROUP,  \
36         OPT_##ALIAS, X6                                                        \
37   }                                                                            \
38   ,
39 #include "Options.inc"
40 #undef OPTION
41 };
42 
43 class ELFOptTable : public opt::OptTable {
44 public:
45   ELFOptTable() : OptTable(infoTable, array_lengthof(infoTable)) {}
46 };
47 
48 // Parses a given list of options.
49 opt::InputArgList ArgParser::parse(ArrayRef<const char *> Argv) {
50   // Make InputArgList from string vectors.
51   ELFOptTable Table;
52   unsigned MissingIndex;
53   unsigned MissingCount;
54 
55   opt::InputArgList Args = Table.ParseArgs(Argv, MissingIndex, MissingCount);
56   if (MissingCount)
57     error(Twine("missing arg value for \"") + Args.getArgString(MissingIndex) +
58           "\", expected " + Twine(MissingCount) +
59           (MissingCount == 1 ? " argument.\n" : " arguments"));
60   for (auto *Arg : Args.filtered(OPT_UNKNOWN))
61     error(Twine("unknown argument: ") + Arg->getSpelling());
62   return Args;
63 }
64