• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- Args.cpp -----------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lld/Common/Args.h"
10 #include "lld/Common/ErrorHandler.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Option/ArgList.h"
15 #include "llvm/Support/Path.h"
16 
17 using namespace llvm;
18 using namespace lld;
19 
20 // TODO(sbc): Remove this once CGOptLevel can be set completely based on bitcode
21 // function metadata.
getCGOptLevel(int optLevelLTO)22 CodeGenOpt::Level lld::args::getCGOptLevel(int optLevelLTO) {
23   if (optLevelLTO == 3)
24     return CodeGenOpt::Aggressive;
25   assert(optLevelLTO < 3);
26   return CodeGenOpt::Default;
27 }
28 
getInteger(opt::InputArgList & args,unsigned key,int64_t Default,unsigned base)29 static int64_t getInteger(opt::InputArgList &args, unsigned key,
30                           int64_t Default, unsigned base) {
31   auto *a = args.getLastArg(key);
32   if (!a)
33     return Default;
34 
35   int64_t v;
36   StringRef s = a->getValue();
37   if (base == 16 && (s.startswith("0x") || s.startswith("0X")))
38     s = s.drop_front(2);
39   if (to_integer(s, v, base))
40     return v;
41 
42   StringRef spelling = args.getArgString(a->getIndex());
43   error(spelling + ": number expected, but got '" + a->getValue() + "'");
44   return 0;
45 }
46 
getInteger(opt::InputArgList & args,unsigned key,int64_t Default)47 int64_t lld::args::getInteger(opt::InputArgList &args, unsigned key,
48                               int64_t Default) {
49   return ::getInteger(args, key, Default, 10);
50 }
51 
getHex(opt::InputArgList & args,unsigned key,int64_t Default)52 int64_t lld::args::getHex(opt::InputArgList &args, unsigned key,
53                           int64_t Default) {
54   return ::getInteger(args, key, Default, 16);
55 }
56 
getStrings(opt::InputArgList & args,int id)57 std::vector<StringRef> lld::args::getStrings(opt::InputArgList &args, int id) {
58   std::vector<StringRef> v;
59   for (auto *arg : args.filtered(id))
60     v.push_back(arg->getValue());
61   return v;
62 }
63 
getZOptionValue(opt::InputArgList & args,int id,StringRef key,uint64_t Default)64 uint64_t lld::args::getZOptionValue(opt::InputArgList &args, int id,
65                                     StringRef key, uint64_t Default) {
66   for (auto *arg : args.filtered_reverse(id)) {
67     std::pair<StringRef, StringRef> kv = StringRef(arg->getValue()).split('=');
68     if (kv.first == key) {
69       uint64_t result = Default;
70       if (!to_integer(kv.second, result))
71         error("invalid " + key + ": " + kv.second);
72       return result;
73     }
74   }
75   return Default;
76 }
77 
getLines(MemoryBufferRef mb)78 std::vector<StringRef> lld::args::getLines(MemoryBufferRef mb) {
79   SmallVector<StringRef, 0> arr;
80   mb.getBuffer().split(arr, '\n');
81 
82   std::vector<StringRef> ret;
83   for (StringRef s : arr) {
84     s = s.trim();
85     if (!s.empty() && s[0] != '#')
86       ret.push_back(s);
87   }
88   return ret;
89 }
90 
getFilenameWithoutExe(StringRef path)91 StringRef lld::args::getFilenameWithoutExe(StringRef path) {
92   if (path.endswith_lower(".exe"))
93     return sys::path::stem(path);
94   return sys::path::filename(path);
95 }
96