1 //===--- ArgList.cpp - Argument List Management ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Option/ArgList.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/Option/Arg.h"
15 #include "llvm/Option/Option.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm::opt;
22 void ArgList::append(Arg *A) {
25 // Update ranges for the option and all of its groups.
26 for (Option O = A->getOption().getUnaliasedOption(); O.isValid();
29 OptRanges.insert(std::make_pair(O.getID(), emptyRange())).first->second;
30 R.first = std::min<unsigned>(R.first, Args.size() - 1);
31 R.second = Args.size();
35 void ArgList::eraseArg(OptSpecifier Id) {
36 // Zero out the removed entries but keep them around so that we don't
37 // need to invalidate OptRanges.
38 for (Arg *const &A : filtered(Id)) {
39 // Avoid the need for a non-const filtered iterator variant.
40 Arg **ArgsBegin = Args.data();
41 ArgsBegin[&A - ArgsBegin] = nullptr;
43 OptRanges.erase(Id.getID());
47 ArgList::getRange(std::initializer_list<OptSpecifier> Ids) const {
48 OptRange R = emptyRange();
50 auto I = OptRanges.find(Id.getID());
51 if (I != OptRanges.end()) {
52 R.first = std::min(R.first, I->second.first);
53 R.second = std::max(R.second, I->second.second);
56 // Map an empty {-1, 0} range to {0, 0} so it can be used to form iterators.
62 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
63 if (Arg *A = getLastArg(Pos, Neg))
64 return A->getOption().matches(Pos);
68 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
70 if (Arg *A = getLastArg(Pos, PosAlias, Neg))
71 return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);
75 StringRef ArgList::getLastArgValue(OptSpecifier Id, StringRef Default) const {
76 if (Arg *A = getLastArg(Id))
81 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
82 SmallVector<const char *, 16> Values;
83 AddAllArgValues(Values, Id);
84 return std::vector<std::string>(Values.begin(), Values.end());
87 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
88 if (Arg *A = getLastArg(Id)) {
90 A->render(*this, Output);
94 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id0,
95 OptSpecifier Id1) const {
96 if (Arg *A = getLastArg(Id0, Id1)) {
98 A->render(*this, Output);
102 void ArgList::AddAllArgsExcept(ArgStringList &Output,
103 ArrayRef<OptSpecifier> Ids,
104 ArrayRef<OptSpecifier> ExcludeIds) const {
105 for (const Arg *Arg : *this) {
106 bool Excluded = false;
107 for (OptSpecifier Id : ExcludeIds) {
108 if (Arg->getOption().matches(Id)) {
114 for (OptSpecifier Id : Ids) {
115 if (Arg->getOption().matches(Id)) {
117 Arg->render(*this, Output);
125 /// This is a nicer interface when you don't have a list of Ids to exclude.
126 void ArgList::AddAllArgs(ArgStringList &Output,
127 ArrayRef<OptSpecifier> Ids) const {
128 ArrayRef<OptSpecifier> Exclude = None;
129 AddAllArgsExcept(Output, Ids, Exclude);
132 /// This 3-opt variant of AddAllArgs could be eliminated in favor of one
133 /// that accepts a single specifier, given the above which accepts any number.
134 void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
135 OptSpecifier Id1, OptSpecifier Id2) const {
136 for (auto Arg: filtered(Id0, Id1, Id2)) {
138 Arg->render(*this, Output);
142 void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
143 OptSpecifier Id1, OptSpecifier Id2) const {
144 for (auto Arg : filtered(Id0, Id1, Id2)) {
146 const auto &Values = Arg->getValues();
147 Output.append(Values.begin(), Values.end());
151 void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
152 const char *Translation,
154 for (auto Arg: filtered(Id0)) {
158 Output.push_back(MakeArgString(StringRef(Translation) +
161 Output.push_back(Translation);
162 Output.push_back(Arg->getValue(0));
167 void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
168 for (auto *Arg : filtered(Id0))
172 void ArgList::ClaimAllArgs() const {
173 for (auto *Arg : *this)
174 if (!Arg->isClaimed())
178 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
180 StringRef RHS) const {
181 StringRef Cur = getArgString(Index);
182 if (Cur.size() == LHS.size() + RHS.size() &&
183 Cur.startswith(LHS) && Cur.endswith(RHS))
186 return MakeArgString(LHS + RHS);
189 void ArgList::print(raw_ostream &O) const {
190 for (Arg *A : *this) {
196 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
197 LLVM_DUMP_METHOD void ArgList::dump() const { print(dbgs()); }
202 void InputArgList::releaseMemory() {
203 // An InputArgList always owns its arguments.
208 InputArgList::InputArgList(const char* const *ArgBegin,
209 const char* const *ArgEnd)
210 : NumInputArgStrings(ArgEnd - ArgBegin) {
211 ArgStrings.append(ArgBegin, ArgEnd);
214 unsigned InputArgList::MakeIndex(StringRef String0) const {
215 unsigned Index = ArgStrings.size();
217 // Tuck away so we have a reliable const char *.
218 SynthesizedStrings.push_back(String0);
219 ArgStrings.push_back(SynthesizedStrings.back().c_str());
224 unsigned InputArgList::MakeIndex(StringRef String0,
225 StringRef String1) const {
226 unsigned Index0 = MakeIndex(String0);
227 unsigned Index1 = MakeIndex(String1);
228 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
233 const char *InputArgList::MakeArgStringRef(StringRef Str) const {
234 return getArgString(MakeIndex(Str));
239 DerivedArgList::DerivedArgList(const InputArgList &BaseArgs)
240 : BaseArgs(BaseArgs) {}
242 const char *DerivedArgList::MakeArgStringRef(StringRef Str) const {
243 return BaseArgs.MakeArgString(Str);
246 void DerivedArgList::AddSynthesizedArg(Arg *A) {
247 SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
250 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
251 SynthesizedArgs.push_back(
252 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
253 BaseArgs.MakeIndex(Opt.getName()), BaseArg));
254 return SynthesizedArgs.back().get();
257 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
258 StringRef Value) const {
259 unsigned Index = BaseArgs.MakeIndex(Value);
260 SynthesizedArgs.push_back(
261 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
262 Index, BaseArgs.getArgString(Index), BaseArg));
263 return SynthesizedArgs.back().get();
266 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
267 StringRef Value) const {
268 unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
269 SynthesizedArgs.push_back(
270 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
271 Index, BaseArgs.getArgString(Index + 1), BaseArg));
272 return SynthesizedArgs.back().get();
275 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
276 StringRef Value) const {
277 unsigned Index = BaseArgs.MakeIndex((Opt.getName() + Value).str());
278 SynthesizedArgs.push_back(make_unique<Arg>(
279 Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
280 BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
281 return SynthesizedArgs.back().get();