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/Option.h"
14 using namespace clang::driver;
16 ArgList::ArgList(arglist_type &_Args) : Args(_Args) {
22 void ArgList::append(Arg *A) {
26 Arg *ArgList::getLastArg(options::ID Id, bool Claim) const {
27 // FIXME: Make search efficient?
28 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it) {
29 if ((*it)->getOption().matches(Id)) {
30 if (Claim) (*it)->claim();
38 Arg *ArgList::getLastArg(options::ID Id0, options::ID Id1, bool Claim) const {
39 Arg *Res, *A0 = getLastArg(Id0, false), *A1 = getLastArg(Id1, false);
42 Res = A0->getIndex() > A1->getIndex() ? A0 : A1;
52 bool ArgList::hasFlag(options::ID Pos, options::ID Neg, bool Default) const {
53 if (Arg *A = getLastArg(Pos, Neg))
54 return A->getOption().matches(Pos);
58 void ArgList::AddLastArg(ArgStringList &Output, options::ID Id) const {
59 if (Arg *A = getLastArg(Id)) {
61 A->render(*this, Output);
65 void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0) const {
67 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
69 if (A->getOption().matches(Id0)) {
71 A->render(*this, Output);
76 void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
77 options::ID Id1) const {
79 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
81 if (A->getOption().matches(Id0) || A->getOption().matches(Id1)) {
83 A->render(*this, Output);
88 void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
89 options::ID Id1, options::ID Id2) const {
91 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
93 if (A->getOption().matches(Id0) || A->getOption().matches(Id1) ||
94 A->getOption().matches(Id2)) {
96 A->render(*this, Output);
101 void ArgList::AddAllArgValues(ArgStringList &Output, options::ID Id0) const {
103 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
105 if (A->getOption().matches(Id0)) {
107 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
108 Output.push_back(A->getValue(*this, i));
113 void ArgList::AddAllArgValues(ArgStringList &Output, options::ID Id0,
114 options::ID Id1) const {
116 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
118 if (A->getOption().matches(Id0) || A->getOption().matches(Id1)) {
120 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
121 Output.push_back(A->getValue(*this, i));
126 void ArgList::AddAllArgsTranslated(ArgStringList &Output, options::ID Id0,
127 const char *Translation,
130 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
132 if (A->getOption().matches(Id0)) {
136 std::string Value = Translation;
137 Value += A->getValue(*this, 0);
138 Output.push_back(MakeArgString(Value.c_str()));
140 Output.push_back(Translation);
141 Output.push_back(A->getValue(*this, 0));
147 void ArgList::ClaimAllArgs(options::ID Id0) const {
149 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
151 if (A->getOption().matches(Id0))
158 InputArgList::InputArgList(const char **ArgBegin, const char **ArgEnd)
159 : ArgList(ActualArgs), NumInputArgStrings(ArgEnd - ArgBegin)
161 ArgStrings.append(ArgBegin, ArgEnd);
164 InputArgList::~InputArgList() {
165 // An InputArgList always owns its arguments.
166 for (iterator it = begin(), ie = end(); it != ie; ++it)
170 unsigned InputArgList::MakeIndex(const char *String0) const {
171 unsigned Index = ArgStrings.size();
173 // Tuck away so we have a reliable const char *.
174 SynthesizedStrings.push_back(String0);
175 ArgStrings.push_back(SynthesizedStrings.back().c_str());
180 unsigned InputArgList::MakeIndex(const char *String0,
181 const char *String1) const {
182 unsigned Index0 = MakeIndex(String0);
183 unsigned Index1 = MakeIndex(String1);
184 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
189 const char *InputArgList::MakeArgString(const char *Str) const {
190 return getArgString(MakeIndex(Str));
195 DerivedArgList::DerivedArgList(InputArgList &_BaseArgs, bool _OnlyProxy)
196 : ArgList(_OnlyProxy ? _BaseArgs.getArgs() : ActualArgs),
197 BaseArgs(_BaseArgs), OnlyProxy(_OnlyProxy)
201 DerivedArgList::~DerivedArgList() {
202 // We only own the arguments we explicitly synthesized.
203 for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end();
208 const char *DerivedArgList::MakeArgString(const char *Str) const {
209 return BaseArgs.MakeArgString(Str);
212 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option *Opt) const {
213 return new FlagArg(Opt, BaseArgs.MakeIndex(Opt->getName()), BaseArg);
216 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option *Opt,
217 const char *Value) const {
218 return new PositionalArg(Opt, BaseArgs.MakeIndex(Value), BaseArg);
221 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option *Opt,
222 const char *Value) const {
223 return new SeparateArg(Opt, BaseArgs.MakeIndex(Opt->getName(), Value), 1,
227 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option *Opt,
228 const char *Value) const {
229 std::string Joined(Opt->getName());
231 return new JoinedArg(Opt, BaseArgs.MakeIndex(Joined.c_str()), BaseArg);