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