10b57cec5SDimitry Andric //===- Args.cpp -----------------------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "lld/Common/Args.h" 100b57cec5SDimitry Andric #include "lld/Common/ErrorHandler.h" 110b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 120b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h" 130b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 140b57cec5SDimitry Andric #include "llvm/Option/ArgList.h" 150b57cec5SDimitry Andric #include "llvm/Support/Path.h" 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric using namespace llvm; 180b57cec5SDimitry Andric using namespace lld; 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric // TODO(sbc): Remove this once CGOptLevel can be set completely based on bitcode 210b57cec5SDimitry Andric // function metadata. 220b57cec5SDimitry Andric CodeGenOpt::Level lld::args::getCGOptLevel(int optLevelLTO) { 230b57cec5SDimitry Andric if (optLevelLTO == 3) 240b57cec5SDimitry Andric return CodeGenOpt::Aggressive; 250b57cec5SDimitry Andric assert(optLevelLTO < 3); 260b57cec5SDimitry Andric return CodeGenOpt::Default; 270b57cec5SDimitry Andric } 280b57cec5SDimitry Andric 29e8d8bef9SDimitry Andric static int64_t getInteger(opt::InputArgList &args, unsigned key, 30e8d8bef9SDimitry Andric int64_t Default, unsigned base) { 310b57cec5SDimitry Andric auto *a = args.getLastArg(key); 320b57cec5SDimitry Andric if (!a) 330b57cec5SDimitry Andric return Default; 340b57cec5SDimitry Andric 350b57cec5SDimitry Andric int64_t v; 36e8d8bef9SDimitry Andric StringRef s = a->getValue(); 37e8d8bef9SDimitry Andric if (base == 16 && (s.startswith("0x") || s.startswith("0X"))) 38e8d8bef9SDimitry Andric s = s.drop_front(2); 39e8d8bef9SDimitry Andric if (to_integer(s, v, base)) 400b57cec5SDimitry Andric return v; 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric StringRef spelling = args.getArgString(a->getIndex()); 430b57cec5SDimitry Andric error(spelling + ": number expected, but got '" + a->getValue() + "'"); 440b57cec5SDimitry Andric return 0; 450b57cec5SDimitry Andric } 460b57cec5SDimitry Andric 47e8d8bef9SDimitry Andric int64_t lld::args::getInteger(opt::InputArgList &args, unsigned key, 48e8d8bef9SDimitry Andric int64_t Default) { 49e8d8bef9SDimitry Andric return ::getInteger(args, key, Default, 10); 50e8d8bef9SDimitry Andric } 51e8d8bef9SDimitry Andric 52e8d8bef9SDimitry Andric int64_t lld::args::getHex(opt::InputArgList &args, unsigned key, 53e8d8bef9SDimitry Andric int64_t Default) { 54e8d8bef9SDimitry Andric return ::getInteger(args, key, Default, 16); 55e8d8bef9SDimitry Andric } 56e8d8bef9SDimitry Andric 57*bdd1243dSDimitry Andric SmallVector<StringRef, 0> lld::args::getStrings(opt::InputArgList &args, 58*bdd1243dSDimitry Andric int id) { 59*bdd1243dSDimitry Andric SmallVector<StringRef, 0> v; 600b57cec5SDimitry Andric for (auto *arg : args.filtered(id)) 610b57cec5SDimitry Andric v.push_back(arg->getValue()); 620b57cec5SDimitry Andric return v; 630b57cec5SDimitry Andric } 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric uint64_t lld::args::getZOptionValue(opt::InputArgList &args, int id, 660b57cec5SDimitry Andric StringRef key, uint64_t Default) { 670b57cec5SDimitry Andric for (auto *arg : args.filtered_reverse(id)) { 680b57cec5SDimitry Andric std::pair<StringRef, StringRef> kv = StringRef(arg->getValue()).split('='); 690b57cec5SDimitry Andric if (kv.first == key) { 700b57cec5SDimitry Andric uint64_t result = Default; 710b57cec5SDimitry Andric if (!to_integer(kv.second, result)) 720b57cec5SDimitry Andric error("invalid " + key + ": " + kv.second); 730b57cec5SDimitry Andric return result; 740b57cec5SDimitry Andric } 750b57cec5SDimitry Andric } 760b57cec5SDimitry Andric return Default; 770b57cec5SDimitry Andric } 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric std::vector<StringRef> lld::args::getLines(MemoryBufferRef mb) { 800b57cec5SDimitry Andric SmallVector<StringRef, 0> arr; 810b57cec5SDimitry Andric mb.getBuffer().split(arr, '\n'); 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric std::vector<StringRef> ret; 840b57cec5SDimitry Andric for (StringRef s : arr) { 850b57cec5SDimitry Andric s = s.trim(); 860b57cec5SDimitry Andric if (!s.empty() && s[0] != '#') 870b57cec5SDimitry Andric ret.push_back(s); 880b57cec5SDimitry Andric } 890b57cec5SDimitry Andric return ret; 900b57cec5SDimitry Andric } 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric StringRef lld::args::getFilenameWithoutExe(StringRef path) { 93fe6060f1SDimitry Andric if (path.endswith_insensitive(".exe")) 940b57cec5SDimitry Andric return sys::path::stem(path); 950b57cec5SDimitry Andric return sys::path::filename(path); 960b57cec5SDimitry Andric } 97