Add LLVM runtime checks to build path
[clang/acc.git] / lib / Driver / ArgList.cpp
blob54dd4bb77538218421a17b1811c93196fd27d398
1 //===--- 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 //===----------------------------------------------------------------------===//
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) {
19 ArgList::~ArgList() {
22 void ArgList::append(Arg *A) {
23 Args.push_back(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();
31 return *it;
35 return 0;
38 Arg *ArgList::getLastArg(options::ID Id0, options::ID Id1, bool Claim) const {
39 Arg *Res, *A0 = getLastArg(Id0, false), *A1 = getLastArg(Id1, false);
41 if (A0 && A1)
42 Res = A0->getIndex() > A1->getIndex() ? A0 : A1;
43 else
44 Res = A0 ? A0 : A1;
46 if (Claim && Res)
47 Res->claim();
49 return Res;
52 Arg *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);
59 int A0Idx = A0 ? A0->getIndex() : -1;
60 int A1Idx = A1 ? A1->getIndex() : -1;
61 int A2Idx = A2 ? A2->getIndex() : -1;
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;
75 if (Claim && Res)
76 Res->claim();
78 return Res;
81 bool ArgList::hasFlag(options::ID Pos, options::ID Neg, bool Default) const {
82 if (Arg *A = getLastArg(Pos, Neg))
83 return A->getOption().matches(Pos);
84 return Default;
87 void ArgList::AddLastArg(ArgStringList &Output, options::ID Id) const {
88 if (Arg *A = getLastArg(Id)) {
89 A->claim();
90 A->render(*this, Output);
94 void 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);
105 void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
106 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);
117 void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
118 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);
130 void 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));
142 void ArgList::AddAllArgValues(ArgStringList &Output, options::ID Id0,
143 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));
155 void ArgList::AddAllArgsTranslated(ArgStringList &Output, options::ID Id0,
156 const char *Translation,
157 bool Joined) const {
158 // 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();
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));
176 void 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();
187 InputArgList::InputArgList(const char **ArgBegin, const char **ArgEnd)
188 : ArgList(ActualArgs), NumInputArgStrings(ArgEnd - ArgBegin)
190 ArgStrings.append(ArgBegin, ArgEnd);
193 InputArgList::~InputArgList() {
194 // An InputArgList always owns its arguments.
195 for (iterator it = begin(), ie = end(); it != ie; ++it)
196 delete *it;
199 unsigned InputArgList::MakeIndex(const char *String0) const {
200 unsigned Index = ArgStrings.size();
202 // Tuck away so we have a reliable const char *.
203 SynthesizedStrings.push_back(String0);
204 ArgStrings.push_back(SynthesizedStrings.back().c_str());
206 return Index;
209 unsigned InputArgList::MakeIndex(const char *String0,
210 const char *String1) const {
211 unsigned Index0 = MakeIndex(String0);
212 unsigned Index1 = MakeIndex(String1);
213 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
214 (void) Index1;
215 return Index0;
218 const char *InputArgList::MakeArgString(const char *Str) const {
219 return getArgString(MakeIndex(Str));
224 DerivedArgList::DerivedArgList(InputArgList &_BaseArgs, bool _OnlyProxy)
225 : ArgList(_OnlyProxy ? _BaseArgs.getArgs() : ActualArgs),
226 BaseArgs(_BaseArgs), OnlyProxy(_OnlyProxy)
230 DerivedArgList::~DerivedArgList() {
231 // We only own the arguments we explicitly synthesized.
232 for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end();
233 it != ie; ++it)
234 delete *it;
237 const char *DerivedArgList::MakeArgString(const char *Str) const {
238 return BaseArgs.MakeArgString(Str);
241 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option *Opt) const {
242 return new FlagArg(Opt, BaseArgs.MakeIndex(Opt->getName()), BaseArg);
245 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option *Opt,
246 const char *Value) const {
247 return new PositionalArg(Opt, BaseArgs.MakeIndex(Value), BaseArg);
250 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option *Opt,
251 const char *Value) const {
252 return new SeparateArg(Opt, BaseArgs.MakeIndex(Opt->getName(), Value), 1,
253 BaseArg);
256 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option *Opt,
257 const char *Value) const {
258 std::string Joined(Opt->getName());
259 Joined += Value;
260 return new JoinedArg(Opt, BaseArgs.MakeIndex(Joined.c_str()), BaseArg);