llvm-project
92 строки · 2.7 Кб
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
17using namespace llvm;
18using namespace lld;
19
20// TODO(sbc): Remove this once CGOptLevel can be set completely based on bitcode
21// function metadata.
22int lld::args::getCGOptLevel(int optLevelLTO) {
23return std::clamp(optLevelLTO, 2, 3);
24}
25
26static int64_t getInteger(opt::InputArgList &args, unsigned key,
27int64_t Default, unsigned base) {
28auto *a = args.getLastArg(key);
29if (!a)
30return Default;
31
32int64_t v;
33StringRef s = a->getValue();
34if (base == 16)
35s.consume_front_insensitive("0x");
36if (to_integer(s, v, base))
37return v;
38
39StringRef spelling = args.getArgString(a->getIndex());
40error(spelling + ": number expected, but got '" + a->getValue() + "'");
41return 0;
42}
43
44int64_t lld::args::getInteger(opt::InputArgList &args, unsigned key,
45int64_t Default) {
46return ::getInteger(args, key, Default, 10);
47}
48
49int64_t lld::args::getHex(opt::InputArgList &args, unsigned key,
50int64_t Default) {
51return ::getInteger(args, key, Default, 16);
52}
53
54SmallVector<StringRef, 0> lld::args::getStrings(opt::InputArgList &args,
55int id) {
56SmallVector<StringRef, 0> v;
57for (auto *arg : args.filtered(id))
58v.push_back(arg->getValue());
59return v;
60}
61
62uint64_t lld::args::getZOptionValue(opt::InputArgList &args, int id,
63StringRef key, uint64_t defaultValue) {
64for (auto *arg : args.filtered(id)) {
65std::pair<StringRef, StringRef> kv = StringRef(arg->getValue()).split('=');
66if (kv.first == key) {
67if (!to_integer(kv.second, defaultValue))
68error("invalid " + key + ": " + kv.second);
69arg->claim();
70}
71}
72return defaultValue;
73}
74
75std::vector<StringRef> lld::args::getLines(MemoryBufferRef mb) {
76SmallVector<StringRef, 0> arr;
77mb.getBuffer().split(arr, '\n');
78
79std::vector<StringRef> ret;
80for (StringRef s : arr) {
81s = s.trim();
82if (!s.empty() && s[0] != '#')
83ret.push_back(s);
84}
85return ret;
86}
87
88StringRef lld::args::getFilenameWithoutExe(StringRef path) {
89if (path.ends_with_insensitive(".exe"))
90return sys::path::stem(path);
91return sys::path::filename(path);
92}
93