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/Config/llvm-config.h"
17 #include "llvm/Option/Arg.h"
18 #include "llvm/Option/ArgList.h"
19 #include "llvm/Option/Option.h"
20 #include "llvm/Option/OptSpecifier.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/raw_ostream.h"
32 using namespace llvm::opt;
34 void ArgList::append(Arg *A) {
37 // Update ranges for the option and all of its groups.
38 for (Option O = A->getOption().getUnaliasedOption(); O.isValid();
41 OptRanges.insert(std::make_pair(O.getID(), emptyRange())).first->second;
42 R.first = std::min<unsigned>(R.first, Args.size() - 1);
43 R.second = Args.size();
47 void ArgList::eraseArg(OptSpecifier Id) {
48 // Zero out the removed entries but keep them around so that we don't
49 // need to invalidate OptRanges.
50 for (Arg *const &A : filtered(Id)) {
51 // Avoid the need for a non-const filtered iterator variant.
52 Arg **ArgsBegin = Args.data();
53 ArgsBegin[&A - ArgsBegin] = nullptr;
55 OptRanges.erase(Id.getID());
59 ArgList::getRange(std::initializer_list<OptSpecifier> Ids) const {
60 OptRange R = emptyRange();
62 auto I = OptRanges.find(Id.getID());
63 if (I != OptRanges.end()) {
64 R.first = std::min(R.first, I->second.first);
65 R.second = std::max(R.second, I->second.second);
68 // Map an empty {-1, 0} range to {0, 0} so it can be used to form iterators.
74 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
75 if (Arg *A = getLastArg(Pos, Neg))
76 return A->getOption().matches(Pos);
80 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
82 if (Arg *A = getLastArg(Pos, PosAlias, Neg))
83 return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);
87 StringRef ArgList::getLastArgValue(OptSpecifier Id, StringRef Default) const {
88 if (Arg *A = getLastArg(Id))
93 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
94 SmallVector<const char *, 16> Values;
95 AddAllArgValues(Values, Id);
96 return std::vector<std::string>(Values.begin(), Values.end());
99 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
100 if (Arg *A = getLastArg(Id)) {
102 A->render(*this, Output);
106 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id0,
107 OptSpecifier Id1) const {
108 if (Arg *A = getLastArg(Id0, Id1)) {
110 A->render(*this, Output);
114 void ArgList::AddAllArgsExcept(ArgStringList &Output,
115 ArrayRef<OptSpecifier> Ids,
116 ArrayRef<OptSpecifier> ExcludeIds) const {
117 for (const Arg *Arg : *this) {
118 bool Excluded = false;
119 for (OptSpecifier Id : ExcludeIds) {
120 if (Arg->getOption().matches(Id)) {
126 for (OptSpecifier Id : Ids) {
127 if (Arg->getOption().matches(Id)) {
129 Arg->render(*this, Output);
137 /// This is a nicer interface when you don't have a list of Ids to exclude.
138 void ArgList::AddAllArgs(ArgStringList &Output,
139 ArrayRef<OptSpecifier> Ids) const {
140 ArrayRef<OptSpecifier> Exclude = None;
141 AddAllArgsExcept(Output, Ids, Exclude);
144 /// This 3-opt variant of AddAllArgs could be eliminated in favor of one
145 /// that accepts a single specifier, given the above which accepts any number.
146 void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
147 OptSpecifier Id1, OptSpecifier Id2) const {
148 for (auto Arg: filtered(Id0, Id1, Id2)) {
150 Arg->render(*this, Output);
154 void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
155 OptSpecifier Id1, OptSpecifier Id2) const {
156 for (auto Arg : filtered(Id0, Id1, Id2)) {
158 const auto &Values = Arg->getValues();
159 Output.append(Values.begin(), Values.end());
163 void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
164 const char *Translation,
166 for (auto Arg: filtered(Id0)) {
170 Output.push_back(MakeArgString(StringRef(Translation) +
173 Output.push_back(Translation);
174 Output.push_back(Arg->getValue(0));
179 void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
180 for (auto *Arg : filtered(Id0))
184 void ArgList::ClaimAllArgs() const {
185 for (auto *Arg : *this)
186 if (!Arg->isClaimed())
190 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
192 StringRef RHS) const {
193 StringRef Cur = getArgString(Index);
194 if (Cur.size() == LHS.size() + RHS.size() &&
195 Cur.startswith(LHS) && Cur.endswith(RHS))
198 return MakeArgString(LHS + RHS);
201 void ArgList::print(raw_ostream &O) const {
202 for (Arg *A : *this) {
208 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
209 LLVM_DUMP_METHOD void ArgList::dump() const { print(dbgs()); }
212 void InputArgList::releaseMemory() {
213 // An InputArgList always owns its arguments.
218 InputArgList::InputArgList(const char* const *ArgBegin,
219 const char* const *ArgEnd)
220 : NumInputArgStrings(ArgEnd - ArgBegin) {
221 ArgStrings.append(ArgBegin, ArgEnd);
224 unsigned InputArgList::MakeIndex(StringRef String0) const {
225 unsigned Index = ArgStrings.size();
227 // Tuck away so we have a reliable const char *.
228 SynthesizedStrings.push_back(String0);
229 ArgStrings.push_back(SynthesizedStrings.back().c_str());
234 unsigned InputArgList::MakeIndex(StringRef String0,
235 StringRef String1) const {
236 unsigned Index0 = MakeIndex(String0);
237 unsigned Index1 = MakeIndex(String1);
238 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
243 const char *InputArgList::MakeArgStringRef(StringRef Str) const {
244 return getArgString(MakeIndex(Str));
247 DerivedArgList::DerivedArgList(const InputArgList &BaseArgs)
248 : BaseArgs(BaseArgs) {}
250 const char *DerivedArgList::MakeArgStringRef(StringRef Str) const {
251 return BaseArgs.MakeArgString(Str);
254 void DerivedArgList::AddSynthesizedArg(Arg *A) {
255 SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
258 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
259 SynthesizedArgs.push_back(
260 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
261 BaseArgs.MakeIndex(Opt.getName()), BaseArg));
262 return SynthesizedArgs.back().get();
265 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
266 StringRef Value) const {
267 unsigned Index = BaseArgs.MakeIndex(Value);
268 SynthesizedArgs.push_back(
269 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
270 Index, BaseArgs.getArgString(Index), BaseArg));
271 return SynthesizedArgs.back().get();
274 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
275 StringRef Value) const {
276 unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
277 SynthesizedArgs.push_back(
278 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
279 Index, BaseArgs.getArgString(Index + 1), BaseArg));
280 return SynthesizedArgs.back().get();
283 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
284 StringRef Value) const {
285 unsigned Index = BaseArgs.MakeIndex((Opt.getName() + Value).str());
286 SynthesizedArgs.push_back(make_unique<Arg>(
287 Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
288 BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
289 return SynthesizedArgs.back().get();