Daniel Dunbar | 6ac1e22 | 2009-03-04 17:10:42 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "clang/Driver/ArgList.h" |
| 11 | #include "clang/Driver/Arg.h" |
Daniel Dunbar | 9358dc8 | 2009-03-04 22:40:08 +0000 | [diff] [blame] | 12 | #include "clang/Driver/Option.h" |
Daniel Dunbar | 6ac1e22 | 2009-03-04 17:10:42 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace clang::driver; |
| 15 | |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 16 | ArgList::ArgList(arglist_type &_Args) : Args(_Args) { |
Daniel Dunbar | 6ac1e22 | 2009-03-04 17:10:42 +0000 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | ArgList::~ArgList() { |
Daniel Dunbar | 6ac1e22 | 2009-03-04 17:10:42 +0000 | [diff] [blame] | 20 | } |
Daniel Dunbar | 9358dc8 | 2009-03-04 22:40:08 +0000 | [diff] [blame] | 21 | |
| 22 | void ArgList::append(Arg *A) { |
Daniel Dunbar | 9358dc8 | 2009-03-04 22:40:08 +0000 | [diff] [blame] | 23 | Args.push_back(A); |
| 24 | } |
Daniel Dunbar | d8cadd4 | 2009-03-12 01:36:44 +0000 | [diff] [blame] | 25 | |
Daniel Dunbar | 8022fd4 | 2009-03-15 00:48:16 +0000 | [diff] [blame] | 26 | Arg *ArgList::getLastArg(options::ID Id, bool Claim) const { |
Daniel Dunbar | d8cadd4 | 2009-03-12 01:36:44 +0000 | [diff] [blame] | 27 | // FIXME: Make search efficient? |
Daniel Dunbar | fe2e04a | 2009-03-24 17:31:30 +0000 | [diff] [blame] | 28 | for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it) { |
Daniel Dunbar | 8022fd4 | 2009-03-15 00:48:16 +0000 | [diff] [blame] | 29 | if ((*it)->getOption().matches(Id)) { |
| 30 | if (Claim) (*it)->claim(); |
Daniel Dunbar | 0c562a2 | 2009-03-12 16:03:38 +0000 | [diff] [blame] | 31 | return *it; |
Daniel Dunbar | 8022fd4 | 2009-03-15 00:48:16 +0000 | [diff] [blame] | 32 | } |
| 33 | } |
Daniel Dunbar | d8cadd4 | 2009-03-12 01:36:44 +0000 | [diff] [blame] | 34 | |
Daniel Dunbar | 0c562a2 | 2009-03-12 16:03:38 +0000 | [diff] [blame] | 35 | return 0; |
Daniel Dunbar | d8cadd4 | 2009-03-12 01:36:44 +0000 | [diff] [blame] | 36 | } |
Daniel Dunbar | bca58cb | 2009-03-12 18:20:18 +0000 | [diff] [blame] | 37 | |
Daniel Dunbar | cd4e186 | 2009-03-17 18:51:42 +0000 | [diff] [blame] | 38 | Arg *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 Dunbar | 18a7f33 | 2009-03-18 09:29:36 +0000 | [diff] [blame] | 52 | bool ArgList::hasFlag(options::ID Pos, options::ID Neg, bool Default) const { |
Daniel Dunbar | 9af6668 | 2009-04-07 21:08:57 +0000 | [diff] [blame] | 53 | if (Arg *A = getLastArg(Pos, Neg)) |
| 54 | return A->getOption().matches(Pos); |
Daniel Dunbar | 18a7f33 | 2009-03-18 09:29:36 +0000 | [diff] [blame] | 55 | return Default; |
| 56 | } |
| 57 | |
Daniel Dunbar | 18a7f33 | 2009-03-18 09:29:36 +0000 | [diff] [blame] | 58 | void 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 | |
| 65 | void 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 | |
| 76 | void 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 | |
| 88 | void 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 | |
| 101 | void 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 Dunbar | ee51031 | 2009-03-20 15:59:01 +0000 | [diff] [blame] | 112 | |
| 113 | void 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 Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 125 | |
Daniel Dunbar | 524b9fb | 2009-03-26 15:39:22 +0000 | [diff] [blame] | 126 | void 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 Dunbar | 68fb469 | 2009-04-03 20:51:31 +0000 | [diff] [blame] | 139 | void 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 Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 148 | // |
| 149 | |
| 150 | InputArgList::InputArgList(const char **ArgBegin, const char **ArgEnd) |
| 151 | : ArgList(ActualArgs), NumInputArgStrings(ArgEnd - ArgBegin) |
| 152 | { |
| 153 | ArgStrings.append(ArgBegin, ArgEnd); |
| 154 | } |
| 155 | |
| 156 | InputArgList::~InputArgList() { |
| 157 | // An InputArgList always owns its arguments. |
| 158 | for (iterator it = begin(), ie = end(); it != ie; ++it) |
| 159 | delete *it; |
| 160 | } |
| 161 | |
| 162 | unsigned 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 | |
| 172 | unsigned 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 | |
| 181 | const char *InputArgList::MakeArgString(const char *Str) const { |
| 182 | return getArgString(MakeIndex(Str)); |
| 183 | } |
| 184 | |
| 185 | // |
| 186 | |
| 187 | DerivedArgList::DerivedArgList(InputArgList &_BaseArgs, bool _OnlyProxy) |
| 188 | : ArgList(_OnlyProxy ? _BaseArgs.getArgs() : ActualArgs), |
| 189 | BaseArgs(_BaseArgs), OnlyProxy(_OnlyProxy) |
| 190 | { |
| 191 | } |
| 192 | |
| 193 | DerivedArgList::~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 | |
| 200 | const char *DerivedArgList::MakeArgString(const char *Str) const { |
| 201 | return BaseArgs.MakeArgString(Str); |
| 202 | } |
| 203 | |
Daniel Dunbar | 478edc2 | 2009-03-29 22:29:05 +0000 | [diff] [blame] | 204 | Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option *Opt) const { |
| 205 | return new FlagArg(Opt, BaseArgs.MakeIndex(Opt->getName()), BaseArg); |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Daniel Dunbar | 478edc2 | 2009-03-29 22:29:05 +0000 | [diff] [blame] | 208 | Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option *Opt, |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 209 | const char *Value) const { |
Daniel Dunbar | 478edc2 | 2009-03-29 22:29:05 +0000 | [diff] [blame] | 210 | return new PositionalArg(Opt, BaseArgs.MakeIndex(Value), BaseArg); |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Daniel Dunbar | 478edc2 | 2009-03-29 22:29:05 +0000 | [diff] [blame] | 213 | Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option *Opt, |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 214 | const char *Value) const { |
Daniel Dunbar | 478edc2 | 2009-03-29 22:29:05 +0000 | [diff] [blame] | 215 | return new SeparateArg(Opt, BaseArgs.MakeIndex(Opt->getName(), Value), 1, |
| 216 | BaseArg); |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Daniel Dunbar | 478edc2 | 2009-03-29 22:29:05 +0000 | [diff] [blame] | 219 | Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option *Opt, |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 220 | const char *Value) const { |
| 221 | std::string Joined(Opt->getName()); |
| 222 | Joined += Value; |
Daniel Dunbar | 478edc2 | 2009-03-29 22:29:05 +0000 | [diff] [blame] | 223 | return new JoinedArg(Opt, BaseArgs.MakeIndex(Joined.c_str()), BaseArg); |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 224 | } |