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" 12 #include "clang/Driver/DriverDiagnostic.h" 13 #include "clang/Driver/Option.h" 14 15 #include "llvm/ADT/SmallString.h" 16 #include "llvm/ADT/Twine.h" 17 #include "llvm/Support/raw_ostream.h" 18 19 using namespace clang; 20 using namespace clang::driver; 21 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 39 ArgList::ArgList() { 40 } 41 42 ArgList::~ArgList() { 43 } 44 45 void ArgList::append(Arg *A) { 46 Args.push_back(A); 47 } 48 49 void ArgList::eraseArg(OptSpecifier Id) { 50 for (iterator it = begin(), ie = end(); it != ie; ) { 51 if ((*it)->getOption().matches(Id)) { 52 it = Args.erase(it); 53 ie = end(); 54 } else { 55 ++it; 56 } 57 } 58 } 59 60 Arg *ArgList::getLastArgNoClaim(OptSpecifier Id) const { 61 // FIXME: Make search efficient? 62 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it) 63 if ((*it)->getOption().matches(Id)) 64 return *it; 65 return 0; 66 } 67 68 Arg *ArgList::getLastArg(OptSpecifier Id) const { 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; 78 } 79 80 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const { 81 Arg *Res = 0; 82 for (const_iterator it = begin(), ie = end(); it != ie; ++it) { 83 if ((*it)->getOption().matches(Id0) || 84 (*it)->getOption().matches(Id1)) { 85 Res = *it; 86 Res->claim(); 87 88 } 89 } 90 91 return Res; 92 } 93 94 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, 95 OptSpecifier Id2) const { 96 Arg *Res = 0; 97 for (const_iterator it = begin(), ie = end(); it != ie; ++it) { 98 if ((*it)->getOption().matches(Id0) || 99 (*it)->getOption().matches(Id1) || 100 (*it)->getOption().matches(Id2)) { 101 Res = *it; 102 Res->claim(); 103 } 104 } 105 106 return Res; 107 } 108 109 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, 110 OptSpecifier Id2, OptSpecifier Id3) const { 111 Arg *Res = 0; 112 for (const_iterator it = begin(), ie = end(); it != ie; ++it) { 113 if ((*it)->getOption().matches(Id0) || 114 (*it)->getOption().matches(Id1) || 115 (*it)->getOption().matches(Id2) || 116 (*it)->getOption().matches(Id3)) { 117 Res = *it; 118 Res->claim(); 119 } 120 } 121 122 return Res; 123 } 124 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 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 205 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const { 206 if (Arg *A = getLastArg(Pos, Neg)) 207 return A->getOption().matches(Pos); 208 return Default; 209 } 210 211 StringRef ArgList::getLastArgValue(OptSpecifier Id, 212 StringRef Default) const { 213 if (Arg *A = getLastArg(Id)) 214 return A->getValue(*this); 215 return Default; 216 } 217 218 int ArgList::getLastArgIntValue(OptSpecifier Id, int Default, 219 clang::DiagnosticsEngine *Diags) const { 220 int Res = Default; 221 222 if (Arg *A = getLastArg(Id)) { 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 } 228 } 229 230 return Res; 231 } 232 233 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const { 234 SmallVector<const char *, 16> Values; 235 AddAllArgValues(Values, Id); 236 return std::vector<std::string>(Values.begin(), Values.end()); 237 } 238 239 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const { 240 if (Arg *A = getLastArg(Id)) { 241 A->claim(); 242 A->render(*this, Output); 243 } 244 } 245 246 void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0, 247 OptSpecifier Id1, OptSpecifier Id2) const { 248 for (arg_iterator it = filtered_begin(Id0, Id1, Id2), 249 ie = filtered_end(); it != ie; ++it) { 250 (*it)->claim(); 251 (*it)->render(*this, Output); 252 } 253 } 254 255 void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0, 256 OptSpecifier Id1, OptSpecifier Id2) const { 257 for (arg_iterator it = filtered_begin(Id0, Id1, Id2), 258 ie = filtered_end(); it != ie; ++it) { 259 (*it)->claim(); 260 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i) 261 Output.push_back((*it)->getValue(*this, i)); 262 } 263 } 264 265 void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0, 266 const char *Translation, 267 bool Joined) const { 268 for (arg_iterator it = filtered_begin(Id0), 269 ie = filtered_end(); it != ie; ++it) { 270 (*it)->claim(); 271 272 if (Joined) { 273 Output.push_back(MakeArgString(StringRef(Translation) + 274 (*it)->getValue(*this, 0))); 275 } else { 276 Output.push_back(Translation); 277 Output.push_back((*it)->getValue(*this, 0)); 278 } 279 } 280 } 281 282 void ArgList::ClaimAllArgs(OptSpecifier Id0) const { 283 for (arg_iterator it = filtered_begin(Id0), 284 ie = filtered_end(); it != ie; ++it) 285 (*it)->claim(); 286 } 287 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 294 const char *ArgList::MakeArgString(const Twine &T) const { 295 SmallString<256> Str; 296 T.toVector(Str); 297 return MakeArgString(Str.str()); 298 } 299 300 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index, 301 StringRef LHS, 302 StringRef RHS) const { 303 StringRef Cur = getArgString(Index); 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 311 // 312 313 InputArgList::InputArgList(const char* const *ArgBegin, 314 const char* const *ArgEnd) 315 : NumInputArgStrings(ArgEnd - ArgBegin) { 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 325 unsigned InputArgList::MakeIndex(StringRef String0) const { 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 335 unsigned InputArgList::MakeIndex(StringRef String0, 336 StringRef String1) const { 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 344 const char *InputArgList::MakeArgString(StringRef Str) const { 345 return getArgString(MakeIndex(Str)); 346 } 347 348 // 349 350 DerivedArgList::DerivedArgList(const InputArgList &_BaseArgs) 351 : BaseArgs(_BaseArgs) { 352 } 353 354 DerivedArgList::~DerivedArgList() { 355 // We only own the arguments we explicitly synthesized. 356 for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end(); 357 it != ie; ++it) 358 delete *it; 359 } 360 361 const char *DerivedArgList::MakeArgString(StringRef Str) const { 362 return BaseArgs.MakeArgString(Str); 363 } 364 365 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option *Opt) const { 366 Arg *A = new Arg(Opt, BaseArgs.MakeIndex(Opt->getName()), BaseArg); 367 SynthesizedArgs.push_back(A); 368 return A; 369 } 370 371 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option *Opt, 372 StringRef Value) const { 373 unsigned Index = BaseArgs.MakeIndex(Value); 374 Arg *A = new Arg(Opt, Index, BaseArgs.getArgString(Index), BaseArg); 375 SynthesizedArgs.push_back(A); 376 return A; 377 } 378 379 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option *Opt, 380 StringRef Value) const { 381 unsigned Index = BaseArgs.MakeIndex(Opt->getName(), Value); 382 Arg *A = new Arg(Opt, Index, BaseArgs.getArgString(Index + 1), BaseArg); 383 SynthesizedArgs.push_back(A); 384 return A; 385 } 386 387 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option *Opt, 388 StringRef Value) const { 389 unsigned Index = BaseArgs.MakeIndex(Opt->getName().str() + Value.str()); 390 Arg *A = new Arg(Opt, Index, 391 BaseArgs.getArgString(Index) + Opt->getName().size(), 392 BaseArg); 393 SynthesizedArgs.push_back(A); 394 return A; 395 } 396