blob: 78236730248b0db1a43de4430552918786f57958 [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 }
Daniel Dunbarf01869c2009-03-12 01:36:44 +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);
40
41 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
Daniel Dunbar3a4dcdf2009-03-18 09:29:36 +000052bool ArgList::hasFlag(options::ID Pos, options::ID Neg, bool Default) const {
Daniel Dunbar9e5c3952009-04-07 21:08:57 +000053 if (Arg *A = getLastArg(Pos, Neg))
54 return A->getOption().matches(Pos);
Daniel Dunbar3a4dcdf2009-03-18 09:29:36 +000055 return Default;
56}
57
Daniel Dunbar3a4dcdf2009-03-18 09:29:36 +000058void ArgList::AddLastArg(ArgStringList &Output, options::ID Id) const {
59 if (Arg *A = getLastArg(Id)) {
60 A->claim();
61 A->render(*this, Output);
62 }
63}
64
65void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0) const {
66 // FIXME: Make fast.
67 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
68 const Arg *A = *it;
69 if (A->getOption().matches(Id0)) {
70 A->claim();
71 A->render(*this, Output);
72 }
73 }
74}
75
76void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
77 options::ID Id1) const {
78 // FIXME: Make fast.
79 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
80 const Arg *A = *it;
81 if (A->getOption().matches(Id0) || A->getOption().matches(Id1)) {
82 A->claim();
83 A->render(*this, Output);
84 }
85 }
86}
87
88void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
89 options::ID Id1, options::ID Id2) const {
90 // FIXME: Make fast.
91 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
92 const Arg *A = *it;
93 if (A->getOption().matches(Id0) || A->getOption().matches(Id1) ||
94 A->getOption().matches(Id2)) {
95 A->claim();
96 A->render(*this, Output);
97 }
98 }
99}
100
101void ArgList::AddAllArgValues(ArgStringList &Output, options::ID Id0) const {
102 // FIXME: Make fast.
103 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
104 const Arg *A = *it;
105 if (A->getOption().matches(Id0)) {
106 A->claim();
107 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
108 Output.push_back(A->getValue(*this, i));
109 }
110 }
111}
Daniel Dunbareefd9142009-03-20 15:59:01 +0000112
113void ArgList::AddAllArgValues(ArgStringList &Output, options::ID Id0,
114 options::ID Id1) const {
115 // FIXME: Make fast.
116 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
117 const Arg *A = *it;
118 if (A->getOption().matches(Id0) || A->getOption().matches(Id1)) {
119 A->claim();
120 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
121 Output.push_back(A->getValue(*this, i));
122 }
123 }
124}
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000125
Daniel Dunbar7d3f8852009-03-26 15:39:22 +0000126void ArgList::AddAllArgsTranslated(ArgStringList &Output, options::ID Id0,
127 const char *Translation) const {
128 // FIXME: Make fast.
129 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
130 const Arg *A = *it;
131 if (A->getOption().matches(Id0)) {
132 A->claim();
133 Output.push_back(Translation);
134 Output.push_back(A->getValue(*this, 0));
135 }
136 }
137}
138
Daniel Dunbareb4efed2009-04-03 20:51:31 +0000139void ArgList::ClaimAllArgs(options::ID Id0) const {
140 // FIXME: Make fast.
141 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
142 const Arg *A = *it;
143 if (A->getOption().matches(Id0))
144 A->claim();
145 }
146}
147
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000148//
149
150InputArgList::InputArgList(const char **ArgBegin, const char **ArgEnd)
151 : ArgList(ActualArgs), NumInputArgStrings(ArgEnd - ArgBegin)
152{
153 ArgStrings.append(ArgBegin, ArgEnd);
154}
155
156InputArgList::~InputArgList() {
157 // An InputArgList always owns its arguments.
158 for (iterator it = begin(), ie = end(); it != ie; ++it)
159 delete *it;
160}
161
162unsigned InputArgList::MakeIndex(const char *String0) const {
163 unsigned Index = ArgStrings.size();
164
165 // Tuck away so we have a reliable const char *.
166 SynthesizedStrings.push_back(String0);
167 ArgStrings.push_back(SynthesizedStrings.back().c_str());
168
169 return Index;
170}
171
172unsigned InputArgList::MakeIndex(const char *String0,
173 const char *String1) const {
174 unsigned Index0 = MakeIndex(String0);
175 unsigned Index1 = MakeIndex(String1);
176 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
177 (void) Index1;
178 return Index0;
179}
180
181const char *InputArgList::MakeArgString(const char *Str) const {
182 return getArgString(MakeIndex(Str));
183}
184
185//
186
187DerivedArgList::DerivedArgList(InputArgList &_BaseArgs, bool _OnlyProxy)
188 : ArgList(_OnlyProxy ? _BaseArgs.getArgs() : ActualArgs),
189 BaseArgs(_BaseArgs), OnlyProxy(_OnlyProxy)
190{
191}
192
193DerivedArgList::~DerivedArgList() {
194 // We only own the arguments we explicitly synthesized.
195 for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end();
196 it != ie; ++it)
197 delete *it;
198}
199
200const char *DerivedArgList::MakeArgString(const char *Str) const {
201 return BaseArgs.MakeArgString(Str);
202}
203
Daniel Dunbarae849522009-03-29 22:29:05 +0000204Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option *Opt) const {
205 return new FlagArg(Opt, BaseArgs.MakeIndex(Opt->getName()), BaseArg);
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000206}
207
Daniel Dunbarae849522009-03-29 22:29:05 +0000208Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option *Opt,
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000209 const char *Value) const {
Daniel Dunbarae849522009-03-29 22:29:05 +0000210 return new PositionalArg(Opt, BaseArgs.MakeIndex(Value), BaseArg);
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000211}
212
Daniel Dunbarae849522009-03-29 22:29:05 +0000213Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option *Opt,
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000214 const char *Value) const {
Daniel Dunbarae849522009-03-29 22:29:05 +0000215 return new SeparateArg(Opt, BaseArgs.MakeIndex(Opt->getName(), Value), 1,
216 BaseArg);
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000217}
218
Daniel Dunbarae849522009-03-29 22:29:05 +0000219Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option *Opt,
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000220 const char *Value) const {
221 std::string Joined(Opt->getName());
222 Joined += Value;
Daniel Dunbarae849522009-03-29 22:29:05 +0000223 return new JoinedArg(Opt, BaseArgs.MakeIndex(Joined.c_str()), BaseArg);
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000224}