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