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 "llvm/Option/ArgList.h" 11 #include "llvm/ADT/SmallString.h" 12 #include "llvm/ADT/STLExtras.h" 13 #include "llvm/ADT/Twine.h" 14 #include "llvm/Option/Arg.h" 15 #include "llvm/Option/Option.h" 16 #include "llvm/Support/raw_ostream.h" 17 18 using namespace llvm; 19 using namespace llvm::opt; 20 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 ArgList::~ArgList() { 37 } 38 39 void ArgList::append(Arg *A) { 40 Args.push_back(A); 41 } 42 43 void ArgList::eraseArg(OptSpecifier Id) { 44 Args.erase(std::remove_if(begin(), end(), 45 [=](Arg *A) { return A->getOption().matches(Id); }), 46 end()); 47 } 48 49 Arg *ArgList::getLastArgNoClaim(OptSpecifier Id) const { 50 // FIXME: Make search efficient? 51 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it) 52 if ((*it)->getOption().matches(Id)) 53 return *it; 54 return nullptr; 55 } 56 57 Arg *ArgList::getLastArg(OptSpecifier Id) const { 58 Arg *Res = nullptr; 59 for (const_iterator it = begin(), ie = end(); it != ie; ++it) { 60 if ((*it)->getOption().matches(Id)) { 61 Res = *it; 62 Res->claim(); 63 } 64 } 65 66 return Res; 67 } 68 69 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const { 70 Arg *Res = nullptr; 71 for (const_iterator it = begin(), ie = end(); it != ie; ++it) { 72 if ((*it)->getOption().matches(Id0) || 73 (*it)->getOption().matches(Id1)) { 74 Res = *it; 75 Res->claim(); 76 77 } 78 } 79 80 return Res; 81 } 82 83 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, 84 OptSpecifier Id2) const { 85 Arg *Res = nullptr; 86 for (const_iterator it = begin(), ie = end(); it != ie; ++it) { 87 if ((*it)->getOption().matches(Id0) || 88 (*it)->getOption().matches(Id1) || 89 (*it)->getOption().matches(Id2)) { 90 Res = *it; 91 Res->claim(); 92 } 93 } 94 95 return Res; 96 } 97 98 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, 99 OptSpecifier Id2, OptSpecifier Id3) const { 100 Arg *Res = nullptr; 101 for (const_iterator it = begin(), ie = end(); it != ie; ++it) { 102 if ((*it)->getOption().matches(Id0) || 103 (*it)->getOption().matches(Id1) || 104 (*it)->getOption().matches(Id2) || 105 (*it)->getOption().matches(Id3)) { 106 Res = *it; 107 Res->claim(); 108 } 109 } 110 111 return Res; 112 } 113 114 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, 115 OptSpecifier Id2, OptSpecifier Id3, 116 OptSpecifier Id4) const { 117 Arg *Res = nullptr; 118 for (const_iterator it = begin(), ie = end(); it != ie; ++it) { 119 if ((*it)->getOption().matches(Id0) || 120 (*it)->getOption().matches(Id1) || 121 (*it)->getOption().matches(Id2) || 122 (*it)->getOption().matches(Id3) || 123 (*it)->getOption().matches(Id4)) { 124 Res = *it; 125 Res->claim(); 126 } 127 } 128 129 return Res; 130 } 131 132 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, 133 OptSpecifier Id2, OptSpecifier Id3, 134 OptSpecifier Id4, OptSpecifier Id5) const { 135 Arg *Res = nullptr; 136 for (const_iterator it = begin(), ie = end(); it != ie; ++it) { 137 if ((*it)->getOption().matches(Id0) || 138 (*it)->getOption().matches(Id1) || 139 (*it)->getOption().matches(Id2) || 140 (*it)->getOption().matches(Id3) || 141 (*it)->getOption().matches(Id4) || 142 (*it)->getOption().matches(Id5)) { 143 Res = *it; 144 Res->claim(); 145 } 146 } 147 148 return Res; 149 } 150 151 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, 152 OptSpecifier Id2, OptSpecifier Id3, 153 OptSpecifier Id4, OptSpecifier Id5, 154 OptSpecifier Id6) const { 155 Arg *Res = nullptr; 156 for (const_iterator it = begin(), ie = end(); it != ie; ++it) { 157 if ((*it)->getOption().matches(Id0) || 158 (*it)->getOption().matches(Id1) || 159 (*it)->getOption().matches(Id2) || 160 (*it)->getOption().matches(Id3) || 161 (*it)->getOption().matches(Id4) || 162 (*it)->getOption().matches(Id5) || 163 (*it)->getOption().matches(Id6)) { 164 Res = *it; 165 Res->claim(); 166 } 167 } 168 169 return Res; 170 } 171 172 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, 173 OptSpecifier Id2, OptSpecifier Id3, 174 OptSpecifier Id4, OptSpecifier Id5, 175 OptSpecifier Id6, OptSpecifier Id7) const { 176 Arg *Res = nullptr; 177 for (const_iterator it = begin(), ie = end(); it != ie; ++it) { 178 if ((*it)->getOption().matches(Id0) || 179 (*it)->getOption().matches(Id1) || 180 (*it)->getOption().matches(Id2) || 181 (*it)->getOption().matches(Id3) || 182 (*it)->getOption().matches(Id4) || 183 (*it)->getOption().matches(Id5) || 184 (*it)->getOption().matches(Id6) || 185 (*it)->getOption().matches(Id7)) { 186 Res = *it; 187 Res->claim(); 188 } 189 } 190 191 return Res; 192 } 193 194 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const { 195 if (Arg *A = getLastArg(Pos, Neg)) 196 return A->getOption().matches(Pos); 197 return Default; 198 } 199 200 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg, 201 bool Default) const { 202 if (Arg *A = getLastArg(Pos, PosAlias, Neg)) 203 return A->getOption().matches(Pos) || A->getOption().matches(PosAlias); 204 return Default; 205 } 206 207 StringRef ArgList::getLastArgValue(OptSpecifier Id, 208 StringRef Default) const { 209 if (Arg *A = getLastArg(Id)) 210 return A->getValue(); 211 return Default; 212 } 213 214 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const { 215 SmallVector<const char *, 16> Values; 216 AddAllArgValues(Values, Id); 217 return std::vector<std::string>(Values.begin(), Values.end()); 218 } 219 220 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const { 221 if (Arg *A = getLastArg(Id)) { 222 A->claim(); 223 A->render(*this, Output); 224 } 225 } 226 227 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id0, 228 OptSpecifier Id1) const { 229 if (Arg *A = getLastArg(Id0, Id1)) { 230 A->claim(); 231 A->render(*this, Output); 232 } 233 } 234 235 void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0, 236 OptSpecifier Id1, OptSpecifier Id2) const { 237 for (arg_iterator it = filtered_begin(Id0, Id1, Id2), 238 ie = filtered_end(); it != ie; ++it) { 239 (*it)->claim(); 240 (*it)->render(*this, Output); 241 } 242 } 243 244 void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0, 245 OptSpecifier Id1, OptSpecifier Id2) const { 246 for (arg_iterator it = filtered_begin(Id0, Id1, Id2), 247 ie = filtered_end(); it != ie; ++it) { 248 (*it)->claim(); 249 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i) 250 Output.push_back((*it)->getValue(i)); 251 } 252 } 253 254 void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0, 255 const char *Translation, 256 bool Joined) const { 257 for (arg_iterator it = filtered_begin(Id0), 258 ie = filtered_end(); it != ie; ++it) { 259 (*it)->claim(); 260 261 if (Joined) { 262 Output.push_back(MakeArgString(StringRef(Translation) + 263 (*it)->getValue(0))); 264 } else { 265 Output.push_back(Translation); 266 Output.push_back((*it)->getValue(0)); 267 } 268 } 269 } 270 271 void ArgList::ClaimAllArgs(OptSpecifier Id0) const { 272 for (arg_iterator it = filtered_begin(Id0), 273 ie = filtered_end(); it != ie; ++it) 274 (*it)->claim(); 275 } 276 277 void ArgList::ClaimAllArgs() const { 278 for (const_iterator it = begin(), ie = end(); it != ie; ++it) 279 if (!(*it)->isClaimed()) 280 (*it)->claim(); 281 } 282 283 const char *ArgList::MakeArgString(const Twine &T) const { 284 SmallString<256> Str; 285 return MakeArgString(T.toStringRef(Str)); 286 } 287 288 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index, 289 StringRef LHS, 290 StringRef RHS) const { 291 StringRef Cur = getArgString(Index); 292 if (Cur.size() == LHS.size() + RHS.size() && 293 Cur.startswith(LHS) && Cur.endswith(RHS)) 294 return Cur.data(); 295 296 return MakeArgString(LHS + RHS); 297 } 298 299 // 300 301 InputArgList::InputArgList(const char* const *ArgBegin, 302 const char* const *ArgEnd) 303 : NumInputArgStrings(ArgEnd - ArgBegin) { 304 ArgStrings.append(ArgBegin, ArgEnd); 305 } 306 307 InputArgList::~InputArgList() { 308 // An InputArgList always owns its arguments. 309 for (iterator it = begin(), ie = end(); it != ie; ++it) 310 delete *it; 311 } 312 313 unsigned InputArgList::MakeIndex(StringRef String0) const { 314 unsigned Index = ArgStrings.size(); 315 316 // Tuck away so we have a reliable const char *. 317 SynthesizedStrings.push_back(String0); 318 ArgStrings.push_back(SynthesizedStrings.back().c_str()); 319 320 return Index; 321 } 322 323 unsigned InputArgList::MakeIndex(StringRef String0, 324 StringRef String1) const { 325 unsigned Index0 = MakeIndex(String0); 326 unsigned Index1 = MakeIndex(String1); 327 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!"); 328 (void) Index1; 329 return Index0; 330 } 331 332 const char *InputArgList::MakeArgString(StringRef Str) const { 333 return getArgString(MakeIndex(Str)); 334 } 335 336 // 337 338 DerivedArgList::DerivedArgList(const InputArgList &_BaseArgs) 339 : BaseArgs(_BaseArgs) { 340 } 341 342 DerivedArgList::~DerivedArgList() {} 343 344 const char *DerivedArgList::MakeArgString(StringRef Str) const { 345 return BaseArgs.MakeArgString(Str); 346 } 347 348 void DerivedArgList::AddSynthesizedArg(Arg *A) { 349 SynthesizedArgs.push_back(std::unique_ptr<Arg>(A)); 350 } 351 352 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const { 353 SynthesizedArgs.push_back(make_unique<Arg>( 354 Opt, 355 ArgList::MakeArgString(Twine(Opt.getPrefix()) + Twine(Opt.getName())), 356 BaseArgs.MakeIndex(Opt.getName()), BaseArg)); 357 return SynthesizedArgs.back().get(); 358 } 359 360 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt, 361 StringRef Value) const { 362 unsigned Index = BaseArgs.MakeIndex(Value); 363 SynthesizedArgs.push_back(make_unique<Arg>( 364 Opt, 365 ArgList::MakeArgString(Twine(Opt.getPrefix()) + Twine(Opt.getName())), 366 Index, BaseArgs.getArgString(Index), BaseArg)); 367 return SynthesizedArgs.back().get(); 368 } 369 370 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt, 371 StringRef Value) const { 372 unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value); 373 SynthesizedArgs.push_back(make_unique<Arg>( 374 Opt, 375 ArgList::MakeArgString(Twine(Opt.getPrefix()) + Twine(Opt.getName())), 376 Index, BaseArgs.getArgString(Index + 1), BaseArg)); 377 return SynthesizedArgs.back().get(); 378 } 379 380 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt, 381 StringRef Value) const { 382 unsigned Index = BaseArgs.MakeIndex(Opt.getName().str() + Value.str()); 383 SynthesizedArgs.push_back(make_unique<Arg>( 384 Opt, 385 ArgList::MakeArgString(Twine(Opt.getPrefix()) + Twine(Opt.getName())), 386 Index, BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg)); 387 return SynthesizedArgs.back().get(); 388 } 389