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 "clang/Driver/ArgList.h"
11 #include "clang/Driver/Arg.h"
12 #include "clang/Driver/DriverDiagnostic.h"
13 #include "clang/Driver/Option.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Support/raw_ostream.h"
19 using namespace clang;
20 using namespace clang::driver;
22 void arg_iterator::SkipToNextArg() {
23 for (; Current != Args.end(); ++Current) {
24 // Done if there are no filters.
28 // Otherwise require a match.
29 const Option &O = (*Current)->getOption();
31 (Id1.isValid() && O.matches(Id1)) ||
32 (Id2.isValid() && O.matches(Id2)))
45 void ArgList::append(Arg *A) {
49 void ArgList::eraseArg(OptSpecifier Id) {
50 for (iterator it = begin(), ie = end(); it != ie; ) {
51 if ((*it)->getOption().matches(Id)) {
60 Arg *ArgList::getLastArgNoClaim(OptSpecifier Id) const {
61 // FIXME: Make search efficient?
62 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
63 if ((*it)->getOption().matches(Id))
68 Arg *ArgList::getLastArg(OptSpecifier Id) const {
70 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
71 if ((*it)->getOption().matches(Id)) {
80 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const {
82 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
83 if ((*it)->getOption().matches(Id0) ||
84 (*it)->getOption().matches(Id1)) {
94 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
95 OptSpecifier Id2) const {
97 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
98 if ((*it)->getOption().matches(Id0) ||
99 (*it)->getOption().matches(Id1) ||
100 (*it)->getOption().matches(Id2)) {
109 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
110 OptSpecifier Id2, OptSpecifier Id3) const {
112 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
113 if ((*it)->getOption().matches(Id0) ||
114 (*it)->getOption().matches(Id1) ||
115 (*it)->getOption().matches(Id2) ||
116 (*it)->getOption().matches(Id3)) {
125 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
126 if (Arg *A = getLastArg(Pos, Neg))
127 return A->getOption().matches(Pos);
131 StringRef ArgList::getLastArgValue(OptSpecifier Id,
132 StringRef Default) const {
133 if (Arg *A = getLastArg(Id))
134 return A->getValue(*this);
138 int ArgList::getLastArgIntValue(OptSpecifier Id, int Default,
139 clang::DiagnosticsEngine &Diags) const {
142 if (Arg *A = getLastArg(Id)) {
143 if (StringRef(A->getValue(*this)).getAsInteger(10, Res))
144 Diags.Report(diag::err_drv_invalid_int_value)
145 << A->getAsString(*this) << A->getValue(*this);
151 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
152 SmallVector<const char *, 16> Values;
153 AddAllArgValues(Values, Id);
154 return std::vector<std::string>(Values.begin(), Values.end());
157 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
158 if (Arg *A = getLastArg(Id)) {
160 A->render(*this, Output);
164 void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
165 OptSpecifier Id1, OptSpecifier Id2) const {
166 for (arg_iterator it = filtered_begin(Id0, Id1, Id2),
167 ie = filtered_end(); it != ie; ++it) {
169 (*it)->render(*this, Output);
173 void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
174 OptSpecifier Id1, OptSpecifier Id2) const {
175 for (arg_iterator it = filtered_begin(Id0, Id1, Id2),
176 ie = filtered_end(); it != ie; ++it) {
178 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
179 Output.push_back((*it)->getValue(*this, i));
183 void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
184 const char *Translation,
186 for (arg_iterator it = filtered_begin(Id0),
187 ie = filtered_end(); it != ie; ++it) {
191 Output.push_back(MakeArgString(StringRef(Translation) +
192 (*it)->getValue(*this, 0)));
194 Output.push_back(Translation);
195 Output.push_back((*it)->getValue(*this, 0));
200 void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
201 for (arg_iterator it = filtered_begin(Id0),
202 ie = filtered_end(); it != ie; ++it)
206 void ArgList::ClaimAllArgs() const {
207 for (const_iterator it = begin(), ie = end(); it != ie; ++it)
208 if (!(*it)->isClaimed())
212 const char *ArgList::MakeArgString(const Twine &T) const {
213 llvm::SmallString<256> Str;
215 return MakeArgString(Str.str());
218 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
220 StringRef RHS) const {
221 StringRef Cur = getArgString(Index);
222 if (Cur.size() == LHS.size() + RHS.size() &&
223 Cur.startswith(LHS) && Cur.endswith(RHS))
226 return MakeArgString(LHS + RHS);
231 InputArgList::InputArgList(const char* const *ArgBegin,
232 const char* const *ArgEnd)
233 : NumInputArgStrings(ArgEnd - ArgBegin) {
234 ArgStrings.append(ArgBegin, ArgEnd);
237 InputArgList::~InputArgList() {
238 // An InputArgList always owns its arguments.
239 for (iterator it = begin(), ie = end(); it != ie; ++it)
243 unsigned InputArgList::MakeIndex(StringRef String0) const {
244 unsigned Index = ArgStrings.size();
246 // Tuck away so we have a reliable const char *.
247 SynthesizedStrings.push_back(String0);
248 ArgStrings.push_back(SynthesizedStrings.back().c_str());
253 unsigned InputArgList::MakeIndex(StringRef String0,
254 StringRef String1) const {
255 unsigned Index0 = MakeIndex(String0);
256 unsigned Index1 = MakeIndex(String1);
257 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
262 const char *InputArgList::MakeArgString(StringRef Str) const {
263 return getArgString(MakeIndex(Str));
268 DerivedArgList::DerivedArgList(const InputArgList &_BaseArgs)
269 : BaseArgs(_BaseArgs) {
272 DerivedArgList::~DerivedArgList() {
273 // We only own the arguments we explicitly synthesized.
274 for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end();
279 const char *DerivedArgList::MakeArgString(StringRef Str) const {
280 return BaseArgs.MakeArgString(Str);
283 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option *Opt) const {
284 Arg *A = new Arg(Opt, BaseArgs.MakeIndex(Opt->getName()), BaseArg);
285 SynthesizedArgs.push_back(A);
289 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option *Opt,
290 StringRef Value) const {
291 unsigned Index = BaseArgs.MakeIndex(Value);
292 Arg *A = new Arg(Opt, Index, BaseArgs.getArgString(Index), BaseArg);
293 SynthesizedArgs.push_back(A);
297 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option *Opt,
298 StringRef Value) const {
299 unsigned Index = BaseArgs.MakeIndex(Opt->getName(), Value);
300 Arg *A = new Arg(Opt, Index, BaseArgs.getArgString(Index + 1), BaseArg);
301 SynthesizedArgs.push_back(A);
305 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option *Opt,
306 StringRef Value) const {
307 unsigned Index = BaseArgs.MakeIndex(Opt->getName().str() + Value.str());
308 Arg *A = new Arg(Opt, Index,
309 BaseArgs.getArgString(Index) + Opt->getName().size(),
311 SynthesizedArgs.push_back(A);