Args.cpp revision 341825
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  auto *A = Args.getLastArg(Key);
22  if (!A)
23    return Default;
24
25  int V;
26  if (to_integer(A->getValue(), V, 10))
27    return V;
28
29  StringRef Spelling = Args.getArgString(A->getIndex());
30  error(Spelling + ": number expected, but got '" + A->getValue() + "'");
31  return 0;
32}
33
34std::vector<StringRef> lld::args::getStrings(opt::InputArgList &Args, int Id) {
35  std::vector<StringRef> V;
36  for (auto *Arg : Args.filtered(Id))
37    V.push_back(Arg->getValue());
38  return V;
39}
40
41uint64_t lld::args::getZOptionValue(opt::InputArgList &Args, int Id,
42                                    StringRef Key, uint64_t Default) {
43  for (auto *Arg : Args.filtered(Id)) {
44    std::pair<StringRef, StringRef> KV = StringRef(Arg->getValue()).split('=');
45    if (KV.first == Key) {
46      uint64_t Result = Default;
47      if (!to_integer(KV.second, Result))
48        error("invalid " + Key + ": " + KV.second);
49      return Result;
50    }
51  }
52  return Default;
53}
54
55std::vector<StringRef> lld::args::getLines(MemoryBufferRef MB) {
56  SmallVector<StringRef, 0> Arr;
57  MB.getBuffer().split(Arr, '\n');
58
59  std::vector<StringRef> Ret;
60  for (StringRef S : Arr) {
61    S = S.trim();
62    if (!S.empty() && S[0] != '#')
63      Ret.push_back(S);
64  }
65  return Ret;
66}
67