blob: bd3aab351d622ae316f02b1189e7a0c12f549101 [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 {
53 Arg *PosA = getLastArg(Pos);
54 Arg *NegA = getLastArg(Pos);
55 if (PosA && NegA)
56 return NegA->getIndex() < PosA->getIndex();
57 if (PosA) return true;
58 if (NegA) return false;
59 return Default;
60}
61
Daniel Dunbar3a4dcdf2009-03-18 09:29:36 +000062void ArgList::AddLastArg(ArgStringList &Output, options::ID Id) const {
63 if (Arg *A = getLastArg(Id)) {
64 A->claim();
65 A->render(*this, Output);
66 }
67}
68
69void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0) const {
70 // FIXME: Make fast.
71 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
72 const Arg *A = *it;
73 if (A->getOption().matches(Id0)) {
74 A->claim();
75 A->render(*this, Output);
76 }
77 }
78}
79
80void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
81 options::ID Id1) const {
82 // FIXME: Make fast.
83 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
84 const Arg *A = *it;
85 if (A->getOption().matches(Id0) || A->getOption().matches(Id1)) {
86 A->claim();
87 A->render(*this, Output);
88 }
89 }
90}
91
92void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
93 options::ID Id1, options::ID Id2) const {
94 // FIXME: Make fast.
95 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
96 const Arg *A = *it;
97 if (A->getOption().matches(Id0) || A->getOption().matches(Id1) ||
98 A->getOption().matches(Id2)) {
99 A->claim();
100 A->render(*this, Output);
101 }
102 }
103}
104
105void ArgList::AddAllArgValues(ArgStringList &Output, options::ID Id0) const {
106 // FIXME: Make fast.
107 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
108 const Arg *A = *it;
109 if (A->getOption().matches(Id0)) {
110 A->claim();
111 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
112 Output.push_back(A->getValue(*this, i));
113 }
114 }
115}
Daniel Dunbareefd9142009-03-20 15:59:01 +0000116
117void ArgList::AddAllArgValues(ArgStringList &Output, options::ID Id0,
118 options::ID Id1) 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->claim();
124 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
125 Output.push_back(A->getValue(*this, i));
126 }
127 }
128}
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000129
130//
131
132InputArgList::InputArgList(const char **ArgBegin, const char **ArgEnd)
133 : ArgList(ActualArgs), NumInputArgStrings(ArgEnd - ArgBegin)
134{
135 ArgStrings.append(ArgBegin, ArgEnd);
136}
137
138InputArgList::~InputArgList() {
139 // An InputArgList always owns its arguments.
140 for (iterator it = begin(), ie = end(); it != ie; ++it)
141 delete *it;
142}
143
144unsigned InputArgList::MakeIndex(const char *String0) const {
145 unsigned Index = ArgStrings.size();
146
147 // Tuck away so we have a reliable const char *.
148 SynthesizedStrings.push_back(String0);
149 ArgStrings.push_back(SynthesizedStrings.back().c_str());
150
151 return Index;
152}
153
154unsigned InputArgList::MakeIndex(const char *String0,
155 const char *String1) const {
156 unsigned Index0 = MakeIndex(String0);
157 unsigned Index1 = MakeIndex(String1);
158 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
159 (void) Index1;
160 return Index0;
161}
162
163const char *InputArgList::MakeArgString(const char *Str) const {
164 return getArgString(MakeIndex(Str));
165}
166
167//
168
169DerivedArgList::DerivedArgList(InputArgList &_BaseArgs, bool _OnlyProxy)
170 : ArgList(_OnlyProxy ? _BaseArgs.getArgs() : ActualArgs),
171 BaseArgs(_BaseArgs), OnlyProxy(_OnlyProxy)
172{
173}
174
175DerivedArgList::~DerivedArgList() {
176 // We only own the arguments we explicitly synthesized.
177 for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end();
178 it != ie; ++it)
179 delete *it;
180}
181
182const char *DerivedArgList::MakeArgString(const char *Str) const {
183 return BaseArgs.MakeArgString(Str);
184}
185
186Arg *DerivedArgList::MakeFlagArg(const Option *Opt) const {
187 return new FlagArg(Opt, BaseArgs.MakeIndex(Opt->getName()));
188}
189
190Arg *DerivedArgList::MakePositionalArg(const Option *Opt,
191 const char *Value) const {
192 return new PositionalArg(Opt, BaseArgs.MakeIndex(Value));
193}
194
195Arg *DerivedArgList::MakeSeparateArg(const Option *Opt,
196 const char *Value) const {
197 return new SeparateArg(Opt, BaseArgs.MakeIndex(Opt->getName(), Value), 1);
198}
199
200Arg *DerivedArgList::MakeJoinedArg(const Option *Opt,
201 const char *Value) const {
202 std::string Joined(Opt->getName());
203 Joined += Value;
204 return new JoinedArg(Opt, BaseArgs.MakeIndex(Joined.c_str()));
205}