Args.cpp revision 326947
1//===- Args.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#include "lld/Common/Args.h"
11#include "lld/Common/ErrorHandler.h"
12#include "llvm/ADT/SmallVector.h"
13#include "llvm/ADT/StringExtras.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/Option/ArgList.h"
16
17using namespace llvm;
18using namespace lld;
19
20int lld::args::getInteger(opt::InputArgList &Args, unsigned Key, int Default) {
21  int V = Default;
22  if (auto *Arg = Args.getLastArg(Key)) {
23    StringRef S = Arg->getValue();
24    if (!to_integer(S, V, 10))
25      error(Arg->getSpelling() + ": number expected, but got '" + S + "'");
26  }
27  return V;
28}
29
30std::vector<StringRef> lld::args::getStrings(opt::InputArgList &Args, int Id) {
31  std::vector<StringRef> V;
32  for (auto *Arg : Args.filtered(Id))
33    V.push_back(Arg->getValue());
34  return V;
35}
36
37uint64_t lld::args::getZOptionValue(opt::InputArgList &Args, int Id,
38                                    StringRef Key, uint64_t Default) {
39  for (auto *Arg : Args.filtered(Id)) {
40    std::pair<StringRef, StringRef> KV = StringRef(Arg->getValue()).split('=');
41    if (KV.first == Key) {
42      uint64_t Result = Default;
43      if (!to_integer(KV.second, Result))
44        error("invalid " + Key + ": " + KV.second);
45      return Result;
46    }
47  }
48  return Default;
49}
50
51std::vector<StringRef> lld::args::getLines(MemoryBufferRef MB) {
52  SmallVector<StringRef, 0> Arr;
53  MB.getBuffer().split(Arr, '\n');
54
55  std::vector<StringRef> Ret;
56  for (StringRef S : Arr) {
57    S = S.trim();
58    if (!S.empty() && S[0] != '#')
59      Ret.push_back(S);
60  }
61  return Ret;
62}
63