Args.cpp revision 344779
1//===- Args.cpp -----------------------------------------------------------===// 2// 3// The LLVM Linker 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#include "lld/Common/Args.h" 11#include "lld/Common/ErrorHandler.h" 12#include "llvm/ADT/SmallVector.h" 13#include "llvm/ADT/StringExtras.h" 14#include "llvm/ADT/StringRef.h" 15#include "llvm/Option/ArgList.h" 16#include "llvm/Support/Path.h" 17 18using namespace llvm; 19using namespace lld; 20 21int lld::args::getInteger(opt::InputArgList &Args, unsigned Key, int Default) { 22 auto *A = Args.getLastArg(Key); 23 if (!A) 24 return Default; 25 26 int V; 27 if (to_integer(A->getValue(), V, 10)) 28 return V; 29 30 StringRef Spelling = Args.getArgString(A->getIndex()); 31 error(Spelling + ": number expected, but got '" + A->getValue() + "'"); 32 return 0; 33} 34 35std::vector<StringRef> lld::args::getStrings(opt::InputArgList &Args, int Id) { 36 std::vector<StringRef> V; 37 for (auto *Arg : Args.filtered(Id)) 38 V.push_back(Arg->getValue()); 39 return V; 40} 41 42uint64_t lld::args::getZOptionValue(opt::InputArgList &Args, int Id, 43 StringRef Key, uint64_t Default) { 44 for (auto *Arg : Args.filtered_reverse(Id)) { 45 std::pair<StringRef, StringRef> KV = StringRef(Arg->getValue()).split('='); 46 if (KV.first == Key) { 47 uint64_t Result = Default; 48 if (!to_integer(KV.second, Result)) 49 error("invalid " + Key + ": " + KV.second); 50 return Result; 51 } 52 } 53 return Default; 54} 55 56std::vector<StringRef> lld::args::getLines(MemoryBufferRef MB) { 57 SmallVector<StringRef, 0> Arr; 58 MB.getBuffer().split(Arr, '\n'); 59 60 std::vector<StringRef> Ret; 61 for (StringRef S : Arr) { 62 S = S.trim(); 63 if (!S.empty() && S[0] != '#') 64 Ret.push_back(S); 65 } 66 return Ret; 67} 68 69StringRef lld::args::getFilenameWithoutExe(StringRef Path) { 70 if (Path.endswith_lower(".exe")) 71 return sys::path::stem(Path); 72 return sys::path::filename(Path); 73} 74