blob: be7109f3f656f65f7ab0721b14491659877eb66a [file] [log] [blame]
Daniel Dunbard77aac92009-03-04 17:10:42 +00001//===--- ArgList.cpp - Argument List Management -------------------------*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/Driver/ArgList.h"
11#include "clang/Driver/Arg.h"
Daniel Dunbar12d474a2009-03-04 22:40:08 +000012#include "clang/Driver/Option.h"
Daniel Dunbard77aac92009-03-04 17:10:42 +000013
14using namespace clang::driver;
15
Daniel Dunbara16e4fe2009-03-25 04:13:45 +000016ArgList::ArgList(arglist_type &_Args) : Args(_Args) {
Daniel Dunbard77aac92009-03-04 17:10:42 +000017}
18
19ArgList::~ArgList() {
Daniel Dunbard77aac92009-03-04 17:10:42 +000020}
Daniel Dunbar12d474a2009-03-04 22:40:08 +000021
22void ArgList::append(Arg *A) {
Daniel Dunbar12d474a2009-03-04 22:40:08 +000023 Args.push_back(A);
24}
Daniel Dunbarf01869c2009-03-12 01:36:44 +000025
Daniel Dunbare9c70fa2009-03-15 00:48:16 +000026Arg *ArgList::getLastArg(options::ID Id, bool Claim) const {
Daniel Dunbarf01869c2009-03-12 01:36:44 +000027 // FIXME: Make search efficient?
Daniel Dunbar7c952c92009-03-24 17:31:30 +000028 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it) {
Daniel Dunbare9c70fa2009-03-15 00:48:16 +000029 if ((*it)->getOption().matches(Id)) {
30 if (Claim) (*it)->claim();
Daniel Dunbar08a0ae42009-03-12 16:03:38 +000031 return *it;
Daniel Dunbare9c70fa2009-03-15 00:48:16 +000032 }
33 }
Mike Stump25cf7602009-09-09 15:08:12 +000034
Daniel Dunbar08a0ae42009-03-12 16:03:38 +000035 return 0;
Daniel Dunbarf01869c2009-03-12 01:36:44 +000036}
Daniel Dunbar72cac202009-03-12 18:20:18 +000037
Daniel Dunbar35f55a22009-03-17 18:51:42 +000038Arg *ArgList::getLastArg(options::ID Id0, options::ID Id1, bool Claim) const {
39 Arg *Res, *A0 = getLastArg(Id0, false), *A1 = getLastArg(Id1, false);
Mike Stump25cf7602009-09-09 15:08:12 +000040
Daniel Dunbar35f55a22009-03-17 18:51:42 +000041 if (A0 && A1)
42 Res = A0->getIndex() > A1->getIndex() ? A0 : A1;
43 else
44 Res = A0 ? A0 : A1;
45
46 if (Claim && Res)
47 Res->claim();
48
49 return Res;
50}
51
Bill Wendlinge1d4f5d2009-06-28 07:36:13 +000052Arg *ArgList::getLastArg(options::ID Id0, options::ID Id1, options::ID Id2,
53 bool Claim) const {
54 Arg *Res = 0;
55 Arg *A0 = getLastArg(Id0, false);
56 Arg *A1 = getLastArg(Id1, false);
57 Arg *A2 = getLastArg(Id2, false);
58
59 int A0Idx = A0 ? A0->getIndex() : -1;
60 int A1Idx = A1 ? A1->getIndex() : -1;
61 int A2Idx = A2 ? A2->getIndex() : -1;
62
63 if (A0Idx > A1Idx) {
64 if (A0Idx > A2Idx)
65 Res = A0;
66 else if (A2Idx != -1)
67 Res = A2;
68 } else {
69 if (A1Idx > A2Idx)
70 Res = A1;
71 else if (A2Idx != -1)
72 Res = A2;
73 }
74
75 if (Claim && Res)
76 Res->claim();
77
78 return Res;
79}
80
Daniel Dunbar3a4dcdf2009-03-18 09:29:36 +000081bool ArgList::hasFlag(options::ID Pos, options::ID Neg, bool Default) const {
Daniel Dunbar9e5c3952009-04-07 21:08:57 +000082 if (Arg *A = getLastArg(Pos, Neg))
83 return A->getOption().matches(Pos);
Daniel Dunbar3a4dcdf2009-03-18 09:29:36 +000084 return Default;
85}
86
Daniel Dunbar3a4dcdf2009-03-18 09:29:36 +000087void ArgList::AddLastArg(ArgStringList &Output, options::ID Id) const {
88 if (Arg *A = getLastArg(Id)) {
89 A->claim();
90 A->render(*this, Output);
91 }
92}
93
94void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0) const {
95 // FIXME: Make fast.
96 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
97 const Arg *A = *it;
98 if (A->getOption().matches(Id0)) {
99 A->claim();
100 A->render(*this, Output);
101 }
102 }
103}
104
Mike Stump25cf7602009-09-09 15:08:12 +0000105void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
Daniel Dunbar3a4dcdf2009-03-18 09:29:36 +0000106 options::ID Id1) const {
107 // FIXME: Make fast.
108 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
109 const Arg *A = *it;
110 if (A->getOption().matches(Id0) || A->getOption().matches(Id1)) {
111 A->claim();
112 A->render(*this, Output);
113 }
114 }
115}
116
Mike Stump25cf7602009-09-09 15:08:12 +0000117void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
Daniel Dunbar3a4dcdf2009-03-18 09:29:36 +0000118 options::ID Id1, options::ID Id2) const {
119 // FIXME: Make fast.
120 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
121 const Arg *A = *it;
122 if (A->getOption().matches(Id0) || A->getOption().matches(Id1) ||
123 A->getOption().matches(Id2)) {
124 A->claim();
125 A->render(*this, Output);
126 }
127 }
128}
129
130void ArgList::AddAllArgValues(ArgStringList &Output, options::ID Id0) const {
131 // FIXME: Make fast.
132 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
133 const Arg *A = *it;
134 if (A->getOption().matches(Id0)) {
135 A->claim();
136 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
137 Output.push_back(A->getValue(*this, i));
138 }
139 }
140}
Daniel Dunbareefd9142009-03-20 15:59:01 +0000141
Mike Stump25cf7602009-09-09 15:08:12 +0000142void ArgList::AddAllArgValues(ArgStringList &Output, options::ID Id0,
Daniel Dunbareefd9142009-03-20 15:59:01 +0000143 options::ID Id1) const {
144 // FIXME: Make fast.
145 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
146 const Arg *A = *it;
147 if (A->getOption().matches(Id0) || A->getOption().matches(Id1)) {
148 A->claim();
149 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
150 Output.push_back(A->getValue(*this, i));
151 }
152 }
153}
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000154
Daniel Dunbar7d3f8852009-03-26 15:39:22 +0000155void ArgList::AddAllArgsTranslated(ArgStringList &Output, options::ID Id0,
Daniel Dunbar2e6c74c2009-04-26 01:07:52 +0000156 const char *Translation,
157 bool Joined) const {
Daniel Dunbar7d3f8852009-03-26 15:39:22 +0000158 // FIXME: Make fast.
159 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
160 const Arg *A = *it;
161 if (A->getOption().matches(Id0)) {
162 A->claim();
Daniel Dunbar2e6c74c2009-04-26 01:07:52 +0000163
164 if (Joined) {
165 std::string Value = Translation;
166 Value += A->getValue(*this, 0);
167 Output.push_back(MakeArgString(Value.c_str()));
168 } else {
169 Output.push_back(Translation);
170 Output.push_back(A->getValue(*this, 0));
171 }
Daniel Dunbar7d3f8852009-03-26 15:39:22 +0000172 }
173 }
174}
175
Daniel Dunbareb4efed2009-04-03 20:51:31 +0000176void ArgList::ClaimAllArgs(options::ID Id0) const {
177 // FIXME: Make fast.
178 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
179 const Arg *A = *it;
180 if (A->getOption().matches(Id0))
181 A->claim();
182 }
183}
184
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000185//
186
Mike Stump25cf7602009-09-09 15:08:12 +0000187InputArgList::InputArgList(const char **ArgBegin, const char **ArgEnd)
188 : ArgList(ActualArgs), NumInputArgStrings(ArgEnd - ArgBegin) {
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000189 ArgStrings.append(ArgBegin, ArgEnd);
190}
191
192InputArgList::~InputArgList() {
193 // An InputArgList always owns its arguments.
194 for (iterator it = begin(), ie = end(); it != ie; ++it)
195 delete *it;
196}
197
198unsigned InputArgList::MakeIndex(const char *String0) const {
199 unsigned Index = ArgStrings.size();
200
201 // Tuck away so we have a reliable const char *.
202 SynthesizedStrings.push_back(String0);
203 ArgStrings.push_back(SynthesizedStrings.back().c_str());
204
205 return Index;
206}
207
Mike Stump25cf7602009-09-09 15:08:12 +0000208unsigned InputArgList::MakeIndex(const char *String0,
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000209 const char *String1) const {
210 unsigned Index0 = MakeIndex(String0);
211 unsigned Index1 = MakeIndex(String1);
212 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
213 (void) Index1;
214 return Index0;
215}
216
217const char *InputArgList::MakeArgString(const char *Str) const {
218 return getArgString(MakeIndex(Str));
219}
220
221//
222
223DerivedArgList::DerivedArgList(InputArgList &_BaseArgs, bool _OnlyProxy)
224 : ArgList(_OnlyProxy ? _BaseArgs.getArgs() : ActualArgs),
Mike Stump25cf7602009-09-09 15:08:12 +0000225 BaseArgs(_BaseArgs), OnlyProxy(_OnlyProxy) {
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000226}
227
228DerivedArgList::~DerivedArgList() {
229 // We only own the arguments we explicitly synthesized.
Mike Stump25cf7602009-09-09 15:08:12 +0000230 for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end();
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000231 it != ie; ++it)
232 delete *it;
233}
234
235const char *DerivedArgList::MakeArgString(const char *Str) const {
236 return BaseArgs.MakeArgString(Str);
237}
238
Daniel Dunbarae849522009-03-29 22:29:05 +0000239Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option *Opt) const {
240 return new FlagArg(Opt, BaseArgs.MakeIndex(Opt->getName()), BaseArg);
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000241}
242
Mike Stump25cf7602009-09-09 15:08:12 +0000243Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option *Opt,
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000244 const char *Value) const {
Daniel Dunbarae849522009-03-29 22:29:05 +0000245 return new PositionalArg(Opt, BaseArgs.MakeIndex(Value), BaseArg);
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000246}
247
Mike Stump25cf7602009-09-09 15:08:12 +0000248Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option *Opt,
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000249 const char *Value) const {
Mike Stump25cf7602009-09-09 15:08:12 +0000250 return new SeparateArg(Opt, BaseArgs.MakeIndex(Opt->getName(), Value), 1,
Daniel Dunbarae849522009-03-29 22:29:05 +0000251 BaseArg);
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000252}
253
Mike Stump25cf7602009-09-09 15:08:12 +0000254Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option *Opt,
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000255 const char *Value) const {
256 std::string Joined(Opt->getName());
257 Joined += Value;
Daniel Dunbarae849522009-03-29 22:29:05 +0000258 return new JoinedArg(Opt, BaseArgs.MakeIndex(Joined.c_str()), BaseArg);
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000259}