1 //===- Args.cpp -----------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
20 // TODO(sbc): Remove this once CGOptLevel can be set completely based on bitcode
22 int lld::args::getCGOptLevel(int optLevelLTO
) {
23 return std::clamp(optLevelLTO
, 2, 3);
26 static int64_t getInteger(opt::InputArgList
&args
, unsigned key
,
27 int64_t Default
, unsigned base
) {
28 auto *a
= args
.getLastArg(key
);
33 StringRef s
= a
->getValue();
34 if (base
== 16 && (s
.startswith("0x") || s
.startswith("0X")))
36 if (to_integer(s
, v
, base
))
39 StringRef spelling
= args
.getArgString(a
->getIndex());
40 error(spelling
+ ": number expected, but got '" + a
->getValue() + "'");
44 int64_t lld::args::getInteger(opt::InputArgList
&args
, unsigned key
,
46 return ::getInteger(args
, key
, Default
, 10);
49 int64_t lld::args::getHex(opt::InputArgList
&args
, unsigned key
,
51 return ::getInteger(args
, key
, Default
, 16);
54 SmallVector
<StringRef
, 0> lld::args::getStrings(opt::InputArgList
&args
,
56 SmallVector
<StringRef
, 0> v
;
57 for (auto *arg
: args
.filtered(id
))
58 v
.push_back(arg
->getValue());
62 uint64_t lld::args::getZOptionValue(opt::InputArgList
&args
, int id
,
63 StringRef key
, uint64_t Default
) {
64 for (auto *arg
: args
.filtered_reverse(id
)) {
65 std::pair
<StringRef
, StringRef
> kv
= StringRef(arg
->getValue()).split('=');
66 if (kv
.first
== key
) {
67 uint64_t result
= Default
;
68 if (!to_integer(kv
.second
, result
))
69 error("invalid " + key
+ ": " + kv
.second
);
76 std::vector
<StringRef
> lld::args::getLines(MemoryBufferRef mb
) {
77 SmallVector
<StringRef
, 0> arr
;
78 mb
.getBuffer().split(arr
, '\n');
80 std::vector
<StringRef
> ret
;
81 for (StringRef s
: arr
) {
83 if (!s
.empty() && s
[0] != '#')
89 StringRef
lld::args::getFilenameWithoutExe(StringRef path
) {
90 if (path
.endswith_insensitive(".exe"))
91 return sys::path::stem(path
);
92 return sys::path::filename(path
);