annotate lld/Common/Args.cpp @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900 (2021-06-07)
parents 1d019706d866
children 5f17cb93ff66
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
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
29 static int64_t getInteger(opt::InputArgList &args, unsigned key,
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;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
36 StringRef s = a->getValue();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
37 if (base == 16 && (s.startswith("0x") || s.startswith("0X")))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
38 s = s.drop_front(2);
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
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
47 int64_t lld::args::getInteger(opt::InputArgList &args, unsigned key,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
48 int64_t Default) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
49 return ::getInteger(args, key, Default, 10);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
50 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
51
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
52 int64_t lld::args::getHex(opt::InputArgList &args, unsigned key,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
53 int64_t Default) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
54 return ::getInteger(args, key, Default, 16);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
55 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
56
150
anatofuz
parents:
diff changeset
57 std::vector<StringRef> lld::args::getStrings(opt::InputArgList &args, int id) {
anatofuz
parents:
diff changeset
58 std::vector<StringRef> v;
anatofuz
parents:
diff changeset
59 for (auto *arg : args.filtered(id))
anatofuz
parents:
diff changeset
60 v.push_back(arg->getValue());
anatofuz
parents:
diff changeset
61 return v;
anatofuz
parents:
diff changeset
62 }
anatofuz
parents:
diff changeset
63
anatofuz
parents:
diff changeset
64 uint64_t lld::args::getZOptionValue(opt::InputArgList &args, int id,
anatofuz
parents:
diff changeset
65 StringRef key, uint64_t Default) {
anatofuz
parents:
diff changeset
66 for (auto *arg : args.filtered_reverse(id)) {
anatofuz
parents:
diff changeset
67 std::pair<StringRef, StringRef> kv = StringRef(arg->getValue()).split('=');
anatofuz
parents:
diff changeset
68 if (kv.first == key) {
anatofuz
parents:
diff changeset
69 uint64_t result = Default;
anatofuz
parents:
diff changeset
70 if (!to_integer(kv.second, result))
anatofuz
parents:
diff changeset
71 error("invalid " + key + ": " + kv.second);
anatofuz
parents:
diff changeset
72 return result;
anatofuz
parents:
diff changeset
73 }
anatofuz
parents:
diff changeset
74 }
anatofuz
parents:
diff changeset
75 return Default;
anatofuz
parents:
diff changeset
76 }
anatofuz
parents:
diff changeset
77
anatofuz
parents:
diff changeset
78 std::vector<StringRef> lld::args::getLines(MemoryBufferRef mb) {
anatofuz
parents:
diff changeset
79 SmallVector<StringRef, 0> arr;
anatofuz
parents:
diff changeset
80 mb.getBuffer().split(arr, '\n');
anatofuz
parents:
diff changeset
81
anatofuz
parents:
diff changeset
82 std::vector<StringRef> ret;
anatofuz
parents:
diff changeset
83 for (StringRef s : arr) {
anatofuz
parents:
diff changeset
84 s = s.trim();
anatofuz
parents:
diff changeset
85 if (!s.empty() && s[0] != '#')
anatofuz
parents:
diff changeset
86 ret.push_back(s);
anatofuz
parents:
diff changeset
87 }
anatofuz
parents:
diff changeset
88 return ret;
anatofuz
parents:
diff changeset
89 }
anatofuz
parents:
diff changeset
90
anatofuz
parents:
diff changeset
91 StringRef lld::args::getFilenameWithoutExe(StringRef path) {
anatofuz
parents:
diff changeset
92 if (path.endswith_lower(".exe"))
anatofuz
parents:
diff changeset
93 return sys::path::stem(path);
anatofuz
parents:
diff changeset
94 return sys::path::filename(path);
anatofuz
parents:
diff changeset
95 }