Nick Lewycky | 3fdcc6f | 2010-12-31 17:31:54 +0000 | [diff] [blame] | 1 | //===--- ArgList.cpp - Argument List Management ---------------------------===// |
Daniel Dunbar | 6ac1e22 | 2009-03-04 17:10:42 +0000 | [diff] [blame] | 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 | 03e8ab2 | 2010-05-20 16:54:55 +0000 | [diff] [blame] | 12 | #include "clang/Driver/DriverDiagnostic.h" |
Daniel Dunbar | 9358dc8 | 2009-03-04 22:40:08 +0000 | [diff] [blame] | 13 | #include "clang/Driver/Option.h" |
Daniel Dunbar | 6ac1e22 | 2009-03-04 17:10:42 +0000 | [diff] [blame] | 14 | |
Daniel Dunbar | 16484af | 2009-09-09 22:32:26 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
| 16 | #include "llvm/ADT/Twine.h" |
| 17 | #include "llvm/Support/raw_ostream.h" |
| 18 | |
Benjamin Kramer | a5ddbca | 2010-05-21 19:58:44 +0000 | [diff] [blame] | 19 | using namespace clang; |
Daniel Dunbar | 6ac1e22 | 2009-03-04 17:10:42 +0000 | [diff] [blame] | 20 | using namespace clang::driver; |
| 21 | |
Daniel Dunbar | 3b84f5b | 2009-11-25 11:33:30 +0000 | [diff] [blame] | 22 | void arg_iterator::SkipToNextArg() { |
| 23 | for (; Current != Args.end(); ++Current) { |
| 24 | // Done if there are no filters. |
| 25 | if (!Id0.isValid()) |
| 26 | break; |
| 27 | |
| 28 | // Otherwise require a match. |
| 29 | const Option &O = (*Current)->getOption(); |
| 30 | if (O.matches(Id0) || |
| 31 | (Id1.isValid() && O.matches(Id1)) || |
| 32 | (Id2.isValid() && O.matches(Id2))) |
| 33 | break; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // |
| 38 | |
Daniel Dunbar | 3612bc8 | 2010-06-11 22:00:22 +0000 | [diff] [blame] | 39 | ArgList::ArgList() { |
Daniel Dunbar | 6ac1e22 | 2009-03-04 17:10:42 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | ArgList::~ArgList() { |
Daniel Dunbar | 6ac1e22 | 2009-03-04 17:10:42 +0000 | [diff] [blame] | 43 | } |
Daniel Dunbar | 9358dc8 | 2009-03-04 22:40:08 +0000 | [diff] [blame] | 44 | |
| 45 | void ArgList::append(Arg *A) { |
Daniel Dunbar | 9358dc8 | 2009-03-04 22:40:08 +0000 | [diff] [blame] | 46 | Args.push_back(A); |
| 47 | } |
Daniel Dunbar | d8cadd4 | 2009-03-12 01:36:44 +0000 | [diff] [blame] | 48 | |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 49 | void ArgList::eraseArg(OptSpecifier Id) { |
Chad Rosier | aec8f45 | 2011-08-08 17:17:15 +0000 | [diff] [blame] | 50 | for (iterator it = begin(), ie = end(); it != ie; ) { |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 51 | if ((*it)->getOption().matches(Id)) { |
Chad Rosier | aec8f45 | 2011-08-08 17:17:15 +0000 | [diff] [blame] | 52 | it = Args.erase(it); |
Chad Rosier | b18d503 | 2011-08-12 23:38:19 +0000 | [diff] [blame] | 53 | ie = end(); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 54 | } else { |
Chad Rosier | aec8f45 | 2011-08-08 17:17:15 +0000 | [diff] [blame] | 55 | ++it; |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 56 | } |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 60 | Arg *ArgList::getLastArgNoClaim(OptSpecifier Id) const { |
Daniel Dunbar | d8cadd4 | 2009-03-12 01:36:44 +0000 | [diff] [blame] | 61 | // FIXME: Make search efficient? |
Daniel Dunbar | e4bdae7 | 2009-11-19 04:00:53 +0000 | [diff] [blame] | 62 | for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it) |
| 63 | if ((*it)->getOption().matches(Id)) |
Daniel Dunbar | 0c562a2 | 2009-03-12 16:03:38 +0000 | [diff] [blame] | 64 | return *it; |
Daniel Dunbar | 0c562a2 | 2009-03-12 16:03:38 +0000 | [diff] [blame] | 65 | return 0; |
Daniel Dunbar | d8cadd4 | 2009-03-12 01:36:44 +0000 | [diff] [blame] | 66 | } |
Daniel Dunbar | bca58cb | 2009-03-12 18:20:18 +0000 | [diff] [blame] | 67 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 68 | Arg *ArgList::getLastArg(OptSpecifier Id) const { |
Rafael Espindola | 592f241 | 2010-12-20 22:45:09 +0000 | [diff] [blame] | 69 | Arg *Res = 0; |
| 70 | for (const_iterator it = begin(), ie = end(); it != ie; ++it) { |
| 71 | if ((*it)->getOption().matches(Id)) { |
| 72 | Res = *it; |
| 73 | Res->claim(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return Res; |
Daniel Dunbar | e4bdae7 | 2009-11-19 04:00:53 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 80 | Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const { |
Daniel Dunbar | fdbe65e | 2010-06-14 20:20:44 +0000 | [diff] [blame] | 81 | Arg *Res = 0; |
Rafael Espindola | 592f241 | 2010-12-20 22:45:09 +0000 | [diff] [blame] | 82 | for (const_iterator it = begin(), ie = end(); it != ie; ++it) { |
Daniel Dunbar | fdbe65e | 2010-06-14 20:20:44 +0000 | [diff] [blame] | 83 | if ((*it)->getOption().matches(Id0) || |
| 84 | (*it)->getOption().matches(Id1)) { |
| 85 | Res = *it; |
Rafael Espindola | 592f241 | 2010-12-20 22:45:09 +0000 | [diff] [blame] | 86 | Res->claim(); |
| 87 | |
Daniel Dunbar | fdbe65e | 2010-06-14 20:20:44 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
Daniel Dunbar | cd4e186 | 2009-03-17 18:51:42 +0000 | [diff] [blame] | 90 | |
Daniel Dunbar | cd4e186 | 2009-03-17 18:51:42 +0000 | [diff] [blame] | 91 | return Res; |
| 92 | } |
| 93 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 94 | Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, |
| 95 | OptSpecifier Id2) const { |
Bill Wendling | 45483f7 | 2009-06-28 07:36:13 +0000 | [diff] [blame] | 96 | Arg *Res = 0; |
Rafael Espindola | 592f241 | 2010-12-20 22:45:09 +0000 | [diff] [blame] | 97 | for (const_iterator it = begin(), ie = end(); it != ie; ++it) { |
Daniel Dunbar | fdbe65e | 2010-06-14 20:20:44 +0000 | [diff] [blame] | 98 | if ((*it)->getOption().matches(Id0) || |
| 99 | (*it)->getOption().matches(Id1) || |
| 100 | (*it)->getOption().matches(Id2)) { |
| 101 | Res = *it; |
Rafael Espindola | 592f241 | 2010-12-20 22:45:09 +0000 | [diff] [blame] | 102 | Res->claim(); |
Daniel Dunbar | fdbe65e | 2010-06-14 20:20:44 +0000 | [diff] [blame] | 103 | } |
Bill Wendling | 45483f7 | 2009-06-28 07:36:13 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Bill Wendling | 45483f7 | 2009-06-28 07:36:13 +0000 | [diff] [blame] | 106 | return Res; |
| 107 | } |
| 108 | |
Daniel Dunbar | 47e879d | 2010-07-13 23:31:40 +0000 | [diff] [blame] | 109 | Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, |
| 110 | OptSpecifier Id2, OptSpecifier Id3) const { |
| 111 | Arg *Res = 0; |
Rafael Espindola | 592f241 | 2010-12-20 22:45:09 +0000 | [diff] [blame] | 112 | for (const_iterator it = begin(), ie = end(); it != ie; ++it) { |
Daniel Dunbar | 47e879d | 2010-07-13 23:31:40 +0000 | [diff] [blame] | 113 | if ((*it)->getOption().matches(Id0) || |
| 114 | (*it)->getOption().matches(Id1) || |
| 115 | (*it)->getOption().matches(Id2) || |
| 116 | (*it)->getOption().matches(Id3)) { |
| 117 | Res = *it; |
Rafael Espindola | 592f241 | 2010-12-20 22:45:09 +0000 | [diff] [blame] | 118 | Res->claim(); |
Daniel Dunbar | 47e879d | 2010-07-13 23:31:40 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Daniel Dunbar | 47e879d | 2010-07-13 23:31:40 +0000 | [diff] [blame] | 122 | return Res; |
| 123 | } |
| 124 | |
Chandler Carruth | abf07a7 | 2012-01-02 14:19:45 +0000 | [diff] [blame] | 125 | Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, |
| 126 | OptSpecifier Id2, OptSpecifier Id3, |
| 127 | OptSpecifier Id4) const { |
| 128 | Arg *Res = 0; |
| 129 | for (const_iterator it = begin(), ie = end(); it != ie; ++it) { |
| 130 | if ((*it)->getOption().matches(Id0) || |
| 131 | (*it)->getOption().matches(Id1) || |
| 132 | (*it)->getOption().matches(Id2) || |
| 133 | (*it)->getOption().matches(Id3) || |
| 134 | (*it)->getOption().matches(Id4)) { |
| 135 | Res = *it; |
| 136 | Res->claim(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return Res; |
| 141 | } |
| 142 | |
Simon Atanasyan | 003ab66 | 2012-05-29 18:50:33 +0000 | [diff] [blame^] | 143 | Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, |
| 144 | OptSpecifier Id2, OptSpecifier Id3, |
| 145 | OptSpecifier Id4, OptSpecifier Id5) const { |
| 146 | Arg *Res = 0; |
| 147 | for (const_iterator it = begin(), ie = end(); it != ie; ++it) { |
| 148 | if ((*it)->getOption().matches(Id0) || |
| 149 | (*it)->getOption().matches(Id1) || |
| 150 | (*it)->getOption().matches(Id2) || |
| 151 | (*it)->getOption().matches(Id3) || |
| 152 | (*it)->getOption().matches(Id4) || |
| 153 | (*it)->getOption().matches(Id5)) { |
| 154 | Res = *it; |
| 155 | Res->claim(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return Res; |
| 160 | } |
| 161 | |
| 162 | Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, |
| 163 | OptSpecifier Id2, OptSpecifier Id3, |
| 164 | OptSpecifier Id4, OptSpecifier Id5, |
| 165 | OptSpecifier Id6) const { |
| 166 | Arg *Res = 0; |
| 167 | for (const_iterator it = begin(), ie = end(); it != ie; ++it) { |
| 168 | if ((*it)->getOption().matches(Id0) || |
| 169 | (*it)->getOption().matches(Id1) || |
| 170 | (*it)->getOption().matches(Id2) || |
| 171 | (*it)->getOption().matches(Id3) || |
| 172 | (*it)->getOption().matches(Id4) || |
| 173 | (*it)->getOption().matches(Id5) || |
| 174 | (*it)->getOption().matches(Id6)) { |
| 175 | Res = *it; |
| 176 | Res->claim(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | return Res; |
| 181 | } |
| 182 | |
| 183 | Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, |
| 184 | OptSpecifier Id2, OptSpecifier Id3, |
| 185 | OptSpecifier Id4, OptSpecifier Id5, |
| 186 | OptSpecifier Id6, OptSpecifier Id7) const { |
| 187 | Arg *Res = 0; |
| 188 | for (const_iterator it = begin(), ie = end(); it != ie; ++it) { |
| 189 | if ((*it)->getOption().matches(Id0) || |
| 190 | (*it)->getOption().matches(Id1) || |
| 191 | (*it)->getOption().matches(Id2) || |
| 192 | (*it)->getOption().matches(Id3) || |
| 193 | (*it)->getOption().matches(Id4) || |
| 194 | (*it)->getOption().matches(Id5) || |
| 195 | (*it)->getOption().matches(Id6) || |
| 196 | (*it)->getOption().matches(Id7)) { |
| 197 | Res = *it; |
| 198 | Res->claim(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return Res; |
| 203 | } |
| 204 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 205 | bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const { |
Daniel Dunbar | 9af6668 | 2009-04-07 21:08:57 +0000 | [diff] [blame] | 206 | if (Arg *A = getLastArg(Pos, Neg)) |
| 207 | return A->getOption().matches(Pos); |
Daniel Dunbar | 18a7f33 | 2009-03-18 09:29:36 +0000 | [diff] [blame] | 208 | return Default; |
| 209 | } |
| 210 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 211 | StringRef ArgList::getLastArgValue(OptSpecifier Id, |
| 212 | StringRef Default) const { |
Daniel Dunbar | 03e8ab2 | 2010-05-20 16:54:55 +0000 | [diff] [blame] | 213 | if (Arg *A = getLastArg(Id)) |
| 214 | return A->getValue(*this); |
| 215 | return Default; |
| 216 | } |
| 217 | |
| 218 | int ArgList::getLastArgIntValue(OptSpecifier Id, int Default, |
Chad Rosier | 2dec85b | 2012-03-13 20:09:56 +0000 | [diff] [blame] | 219 | clang::DiagnosticsEngine *Diags) const { |
Daniel Dunbar | 03e8ab2 | 2010-05-20 16:54:55 +0000 | [diff] [blame] | 220 | int Res = Default; |
| 221 | |
| 222 | if (Arg *A = getLastArg(Id)) { |
Chad Rosier | 2dec85b | 2012-03-13 20:09:56 +0000 | [diff] [blame] | 223 | if (StringRef(A->getValue(*this)).getAsInteger(10, Res)) { |
| 224 | if (Diags) |
| 225 | Diags->Report(diag::err_drv_invalid_int_value) |
| 226 | << A->getAsString(*this) << A->getValue(*this); |
| 227 | } |
Daniel Dunbar | 03e8ab2 | 2010-05-20 16:54:55 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | return Res; |
| 231 | } |
| 232 | |
| 233 | std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 234 | SmallVector<const char *, 16> Values; |
Daniel Dunbar | 03e8ab2 | 2010-05-20 16:54:55 +0000 | [diff] [blame] | 235 | AddAllArgValues(Values, Id); |
| 236 | return std::vector<std::string>(Values.begin(), Values.end()); |
| 237 | } |
| 238 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 239 | void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const { |
Daniel Dunbar | 18a7f33 | 2009-03-18 09:29:36 +0000 | [diff] [blame] | 240 | if (Arg *A = getLastArg(Id)) { |
| 241 | A->claim(); |
| 242 | A->render(*this, Output); |
| 243 | } |
| 244 | } |
| 245 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 246 | void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0, |
| 247 | OptSpecifier Id1, OptSpecifier Id2) const { |
Daniel Dunbar | 3b84f5b | 2009-11-25 11:33:30 +0000 | [diff] [blame] | 248 | for (arg_iterator it = filtered_begin(Id0, Id1, Id2), |
| 249 | ie = filtered_end(); it != ie; ++it) { |
Daniel Dunbar | 7e4953e | 2010-06-11 22:00:13 +0000 | [diff] [blame] | 250 | (*it)->claim(); |
| 251 | (*it)->render(*this, Output); |
Daniel Dunbar | 18a7f33 | 2009-03-18 09:29:36 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
Daniel Dunbar | ee51031 | 2009-03-20 15:59:01 +0000 | [diff] [blame] | 254 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 255 | void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0, |
Daniel Dunbar | 3b84f5b | 2009-11-25 11:33:30 +0000 | [diff] [blame] | 256 | OptSpecifier Id1, OptSpecifier Id2) const { |
| 257 | for (arg_iterator it = filtered_begin(Id0, Id1, Id2), |
| 258 | ie = filtered_end(); it != ie; ++it) { |
Daniel Dunbar | 7e4953e | 2010-06-11 22:00:13 +0000 | [diff] [blame] | 259 | (*it)->claim(); |
| 260 | for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i) |
| 261 | Output.push_back((*it)->getValue(*this, i)); |
Daniel Dunbar | ee51031 | 2009-03-20 15:59:01 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 264 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 265 | void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0, |
Daniel Dunbar | 4df9a66 | 2009-04-26 01:07:52 +0000 | [diff] [blame] | 266 | const char *Translation, |
| 267 | bool Joined) const { |
Daniel Dunbar | 3b84f5b | 2009-11-25 11:33:30 +0000 | [diff] [blame] | 268 | for (arg_iterator it = filtered_begin(Id0), |
| 269 | ie = filtered_end(); it != ie; ++it) { |
Daniel Dunbar | 7e4953e | 2010-06-11 22:00:13 +0000 | [diff] [blame] | 270 | (*it)->claim(); |
Daniel Dunbar | 4df9a66 | 2009-04-26 01:07:52 +0000 | [diff] [blame] | 271 | |
Daniel Dunbar | 3b84f5b | 2009-11-25 11:33:30 +0000 | [diff] [blame] | 272 | if (Joined) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 273 | Output.push_back(MakeArgString(StringRef(Translation) + |
Daniel Dunbar | 7e4953e | 2010-06-11 22:00:13 +0000 | [diff] [blame] | 274 | (*it)->getValue(*this, 0))); |
Daniel Dunbar | 3b84f5b | 2009-11-25 11:33:30 +0000 | [diff] [blame] | 275 | } else { |
| 276 | Output.push_back(Translation); |
Daniel Dunbar | 7e4953e | 2010-06-11 22:00:13 +0000 | [diff] [blame] | 277 | Output.push_back((*it)->getValue(*this, 0)); |
Daniel Dunbar | 524b9fb | 2009-03-26 15:39:22 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 282 | void ArgList::ClaimAllArgs(OptSpecifier Id0) const { |
Daniel Dunbar | 3b84f5b | 2009-11-25 11:33:30 +0000 | [diff] [blame] | 283 | for (arg_iterator it = filtered_begin(Id0), |
| 284 | ie = filtered_end(); it != ie; ++it) |
Daniel Dunbar | 7e4953e | 2010-06-11 22:00:13 +0000 | [diff] [blame] | 285 | (*it)->claim(); |
Daniel Dunbar | 68fb469 | 2009-04-03 20:51:31 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 288 | void ArgList::ClaimAllArgs() const { |
| 289 | for (const_iterator it = begin(), ie = end(); it != ie; ++it) |
| 290 | if (!(*it)->isClaimed()) |
| 291 | (*it)->claim(); |
| 292 | } |
| 293 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 294 | const char *ArgList::MakeArgString(const Twine &T) const { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 295 | SmallString<256> Str; |
Daniel Dunbar | 16484af | 2009-09-09 22:32:26 +0000 | [diff] [blame] | 296 | T.toVector(Str); |
| 297 | return MakeArgString(Str.str()); |
| 298 | } |
| 299 | |
Daniel Dunbar | 312a8b7 | 2010-06-09 18:49:38 +0000 | [diff] [blame] | 300 | const char *ArgList::GetOrMakeJoinedArgString(unsigned Index, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 301 | StringRef LHS, |
| 302 | StringRef RHS) const { |
| 303 | StringRef Cur = getArgString(Index); |
Daniel Dunbar | 312a8b7 | 2010-06-09 18:49:38 +0000 | [diff] [blame] | 304 | if (Cur.size() == LHS.size() + RHS.size() && |
| 305 | Cur.startswith(LHS) && Cur.endswith(RHS)) |
| 306 | return Cur.data(); |
| 307 | |
| 308 | return MakeArgString(LHS + RHS); |
| 309 | } |
| 310 | |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 311 | // |
| 312 | |
Axel Naumann | 9d520c5 | 2010-10-11 09:18:43 +0000 | [diff] [blame] | 313 | InputArgList::InputArgList(const char* const *ArgBegin, |
| 314 | const char* const *ArgEnd) |
Daniel Dunbar | 3612bc8 | 2010-06-11 22:00:22 +0000 | [diff] [blame] | 315 | : NumInputArgStrings(ArgEnd - ArgBegin) { |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 316 | ArgStrings.append(ArgBegin, ArgEnd); |
| 317 | } |
| 318 | |
| 319 | InputArgList::~InputArgList() { |
| 320 | // An InputArgList always owns its arguments. |
| 321 | for (iterator it = begin(), ie = end(); it != ie; ++it) |
| 322 | delete *it; |
| 323 | } |
| 324 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 325 | unsigned InputArgList::MakeIndex(StringRef String0) const { |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 326 | unsigned Index = ArgStrings.size(); |
| 327 | |
| 328 | // Tuck away so we have a reliable const char *. |
| 329 | SynthesizedStrings.push_back(String0); |
| 330 | ArgStrings.push_back(SynthesizedStrings.back().c_str()); |
| 331 | |
| 332 | return Index; |
| 333 | } |
| 334 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 335 | unsigned InputArgList::MakeIndex(StringRef String0, |
| 336 | StringRef String1) const { |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 337 | unsigned Index0 = MakeIndex(String0); |
| 338 | unsigned Index1 = MakeIndex(String1); |
| 339 | assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!"); |
| 340 | (void) Index1; |
| 341 | return Index0; |
| 342 | } |
| 343 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 344 | const char *InputArgList::MakeArgString(StringRef Str) const { |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 345 | return getArgString(MakeIndex(Str)); |
| 346 | } |
| 347 | |
| 348 | // |
| 349 | |
Daniel Dunbar | 279c1db | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 350 | DerivedArgList::DerivedArgList(const InputArgList &_BaseArgs) |
Daniel Dunbar | 3612bc8 | 2010-06-11 22:00:22 +0000 | [diff] [blame] | 351 | : BaseArgs(_BaseArgs) { |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | DerivedArgList::~DerivedArgList() { |
| 355 | // We only own the arguments we explicitly synthesized. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end(); |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 357 | it != ie; ++it) |
| 358 | delete *it; |
| 359 | } |
| 360 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 361 | const char *DerivedArgList::MakeArgString(StringRef Str) const { |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 362 | return BaseArgs.MakeArgString(Str); |
| 363 | } |
| 364 | |
Daniel Dunbar | 478edc2 | 2009-03-29 22:29:05 +0000 | [diff] [blame] | 365 | Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option *Opt) const { |
Daniel Dunbar | 532c1ec | 2010-06-09 22:31:08 +0000 | [diff] [blame] | 366 | Arg *A = new Arg(Opt, BaseArgs.MakeIndex(Opt->getName()), BaseArg); |
Daniel Dunbar | fd48cb3 | 2010-03-11 18:04:53 +0000 | [diff] [blame] | 367 | SynthesizedArgs.push_back(A); |
| 368 | return A; |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 371 | Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option *Opt, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 372 | StringRef Value) const { |
Daniel Dunbar | 4465a77 | 2010-06-09 22:31:00 +0000 | [diff] [blame] | 373 | unsigned Index = BaseArgs.MakeIndex(Value); |
Daniel Dunbar | 532c1ec | 2010-06-09 22:31:08 +0000 | [diff] [blame] | 374 | Arg *A = new Arg(Opt, Index, BaseArgs.getArgString(Index), BaseArg); |
Daniel Dunbar | fd48cb3 | 2010-03-11 18:04:53 +0000 | [diff] [blame] | 375 | SynthesizedArgs.push_back(A); |
| 376 | return A; |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option *Opt, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 380 | StringRef Value) const { |
Daniel Dunbar | 4465a77 | 2010-06-09 22:31:00 +0000 | [diff] [blame] | 381 | unsigned Index = BaseArgs.MakeIndex(Opt->getName(), Value); |
Daniel Dunbar | fdbe65e | 2010-06-14 20:20:44 +0000 | [diff] [blame] | 382 | Arg *A = new Arg(Opt, Index, BaseArgs.getArgString(Index + 1), BaseArg); |
Daniel Dunbar | fd48cb3 | 2010-03-11 18:04:53 +0000 | [diff] [blame] | 383 | SynthesizedArgs.push_back(A); |
| 384 | return A; |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option *Opt, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 388 | StringRef Value) const { |
Douglas Gregor | bcf6a80 | 2011-07-05 16:56:25 +0000 | [diff] [blame] | 389 | unsigned Index = BaseArgs.MakeIndex(Opt->getName().str() + Value.str()); |
Daniel Dunbar | 532c1ec | 2010-06-09 22:31:08 +0000 | [diff] [blame] | 390 | Arg *A = new Arg(Opt, Index, |
Douglas Gregor | bcf6a80 | 2011-07-05 16:56:25 +0000 | [diff] [blame] | 391 | BaseArgs.getArgString(Index) + Opt->getName().size(), |
Daniel Dunbar | 532c1ec | 2010-06-09 22:31:08 +0000 | [diff] [blame] | 392 | BaseArg); |
Daniel Dunbar | fd48cb3 | 2010-03-11 18:04:53 +0000 | [diff] [blame] | 393 | SynthesizedArgs.push_back(A); |
| 394 | return A; |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 395 | } |