annotate lld/Common/Args.cpp @ 240:ca573705d418

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