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