blob: 704d7c9ec1242a7399197f631311f047045d3c3c [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- 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"
Rafael Espindola192e1fa2015-08-06 15:08:23 +000017#include "Error.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000018#include "llvm/ADT/STLExtras.h"
Rui Ueyamaeeb22f82015-09-25 15:37:33 +000019#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/StringSaver.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000021
22using namespace llvm;
23
24using namespace lld;
25using 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
35static 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
46class ELFOptTable : public opt::OptTable {
47public:
48 ELFOptTable() : OptTable(infoTable, array_lengthof(infoTable)) {}
49};
50
51// Parses a given list of options.
52opt::InputArgList ArgParser::parse(ArrayRef<const char *> Argv) {
53 // Make InputArgList from string vectors.
54 ELFOptTable Table;
55 unsigned MissingIndex;
56 unsigned MissingCount;
57
Rui Ueyamaeeb22f82015-09-25 15:37:33 +000058 // 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);
Michael J. Spencer84487f12015-07-24 21:03:07 +000065 if (MissingCount)
66 error(Twine("missing arg value for \"") + Args.getArgString(MissingIndex) +
67 "\", expected " + Twine(MissingCount) +
68 (MissingCount == 1 ? " argument.\n" : " arguments"));
Rui Ueyamad5b5ab72015-09-24 18:55:33 +000069
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
Michael J. Spencer84487f12015-07-24 21:03:07 +000076 return Args;
77}