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/ADT/ArrayRef.h"
11 #include "llvm/ADT/None.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Option/Arg.h"
17 #include "llvm/Option/ArgList.h"
18 #include "llvm/Option/Option.h"
19 #include "llvm/Option/OptSpecifier.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
31 using namespace llvm::opt;
33 void ArgList::append(Arg *A) {
36 // Update ranges for the option and all of its groups.
37 for (Option O = A->getOption().getUnaliasedOption(); O.isValid();
40 OptRanges.insert(std::make_pair(O.getID(), emptyRange())).first->second;
41 R.first = std::min<unsigned>(R.first, Args.size() - 1);
42 R.second = Args.size();
46 void ArgList::eraseArg(OptSpecifier Id) {
47 // Zero out the removed entries but keep them around so that we don't
48 // need to invalidate OptRanges.
49 for (Arg *const &A : filtered(Id)) {
50 // Avoid the need for a non-const filtered iterator variant.
51 Arg **ArgsBegin = Args.data();
52 ArgsBegin[&A - ArgsBegin] = nullptr;
54 OptRanges.erase(Id.getID());
58 ArgList::getRange(std::initializer_list<OptSpecifier> Ids) const {
59 OptRange R = emptyRange();
61 auto I = OptRanges.find(Id.getID());
62 if (I != OptRanges.end()) {
63 R.first = std::min(R.first, I->second.first);
64 R.second = std::max(R.second, I->second.second);
67 // Map an empty {-1, 0} range to {0, 0} so it can be used to form iterators.
73 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
74 if (Arg *A = getLastArg(Pos, Neg))
75 return A->getOption().matches(Pos);
79 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
81 if (Arg *A = getLastArg(Pos, PosAlias, Neg))
82 return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);
86 StringRef ArgList::getLastArgValue(OptSpecifier Id, StringRef Default) const {
87 if (Arg *A = getLastArg(Id))
92 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
93 SmallVector<const char *, 16> Values;
94 AddAllArgValues(Values, Id);
95 return std::vector<std::string>(Values.begin(), Values.end());
98 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
99 if (Arg *A = getLastArg(Id)) {
101 A->render(*this, Output);
105 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id0,
106 OptSpecifier Id1) const {
107 if (Arg *A = getLastArg(Id0, Id1)) {
109 A->render(*this, Output);
113 void ArgList::AddAllArgsExcept(ArgStringList &Output,
114 ArrayRef<OptSpecifier> Ids,
115 ArrayRef<OptSpecifier> ExcludeIds) const {
116 for (const Arg *Arg : *this) {
117 bool Excluded = false;
118 for (OptSpecifier Id : ExcludeIds) {
119 if (Arg->getOption().matches(Id)) {
125 for (OptSpecifier Id : Ids) {
126 if (Arg->getOption().matches(Id)) {
128 Arg->render(*this, Output);
136 /// This is a nicer interface when you don't have a list of Ids to exclude.
137 void ArgList::AddAllArgs(ArgStringList &Output,
138 ArrayRef<OptSpecifier> Ids) const {
139 ArrayRef<OptSpecifier> Exclude = None;
140 AddAllArgsExcept(Output, Ids, Exclude);
143 /// This 3-opt variant of AddAllArgs could be eliminated in favor of one
144 /// that accepts a single specifier, given the above which accepts any number.
145 void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
146 OptSpecifier Id1, OptSpecifier Id2) const {
147 for (auto Arg: filtered(Id0, Id1, Id2)) {
149 Arg->render(*this, Output);
153 void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
154 OptSpecifier Id1, OptSpecifier Id2) const {
155 for (auto Arg : filtered(Id0, Id1, Id2)) {
157 const auto &Values = Arg->getValues();
158 Output.append(Values.begin(), Values.end());
162 void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
163 const char *Translation,
165 for (auto Arg: filtered(Id0)) {
169 Output.push_back(MakeArgString(StringRef(Translation) +
172 Output.push_back(Translation);
173 Output.push_back(Arg->getValue(0));
178 void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
179 for (auto *Arg : filtered(Id0))
183 void ArgList::ClaimAllArgs() const {
184 for (auto *Arg : *this)
185 if (!Arg->isClaimed())
189 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
191 StringRef RHS) const {
192 StringRef Cur = getArgString(Index);
193 if (Cur.size() == LHS.size() + RHS.size() &&
194 Cur.startswith(LHS) && Cur.endswith(RHS))
197 return MakeArgString(LHS + RHS);
200 void ArgList::print(raw_ostream &O) const {
201 for (Arg *A : *this) {
207 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
208 LLVM_DUMP_METHOD void ArgList::dump() const { print(dbgs()); }
211 void InputArgList::releaseMemory() {
212 // An InputArgList always owns its arguments.
217 InputArgList::InputArgList(const char* const *ArgBegin,
218 const char* const *ArgEnd)
219 : NumInputArgStrings(ArgEnd - ArgBegin) {
220 ArgStrings.append(ArgBegin, ArgEnd);
223 unsigned InputArgList::MakeIndex(StringRef String0) const {
224 unsigned Index = ArgStrings.size();
226 // Tuck away so we have a reliable const char *.
227 SynthesizedStrings.push_back(String0);
228 ArgStrings.push_back(SynthesizedStrings.back().c_str());
233 unsigned InputArgList::MakeIndex(StringRef String0,
234 StringRef String1) const {
235 unsigned Index0 = MakeIndex(String0);
236 unsigned Index1 = MakeIndex(String1);
237 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
242 const char *InputArgList::MakeArgStringRef(StringRef Str) const {
243 return getArgString(MakeIndex(Str));
246 DerivedArgList::DerivedArgList(const InputArgList &BaseArgs)
247 : BaseArgs(BaseArgs) {}
249 const char *DerivedArgList::MakeArgStringRef(StringRef Str) const {
250 return BaseArgs.MakeArgString(Str);
253 void DerivedArgList::AddSynthesizedArg(Arg *A) {
254 SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
257 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
258 SynthesizedArgs.push_back(
259 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
260 BaseArgs.MakeIndex(Opt.getName()), BaseArg));
261 return SynthesizedArgs.back().get();
264 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
265 StringRef Value) const {
266 unsigned Index = BaseArgs.MakeIndex(Value);
267 SynthesizedArgs.push_back(
268 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
269 Index, BaseArgs.getArgString(Index), BaseArg));
270 return SynthesizedArgs.back().get();
273 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
274 StringRef Value) const {
275 unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
276 SynthesizedArgs.push_back(
277 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
278 Index, BaseArgs.getArgString(Index + 1), BaseArg));
279 return SynthesizedArgs.back().get();
282 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
283 StringRef Value) const {
284 unsigned Index = BaseArgs.MakeIndex((Opt.getName() + Value).str());
285 SynthesizedArgs.push_back(make_unique<Arg>(
286 Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
287 BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
288 return SynthesizedArgs.back().get();