1 //===- ArgList.cpp - Argument List Management -----------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/ADT/ArrayRef.h" 10 #include "llvm/ADT/SmallVector.h" 11 #include "llvm/ADT/STLExtras.h" 12 #include "llvm/ADT/StringRef.h" 13 #include "llvm/ADT/Twine.h" 14 #include "llvm/Config/llvm-config.h" 15 #include "llvm/Option/Arg.h" 16 #include "llvm/Option/ArgList.h" 17 #include "llvm/Option/Option.h" 18 #include "llvm/Option/OptSpecifier.h" 19 #include "llvm/Support/Compiler.h" 20 #include "llvm/Support/Debug.h" 21 #include "llvm/Support/raw_ostream.h" 22 #include <algorithm> 23 #include <cassert> 24 #include <memory> 25 #include <string> 26 #include <utility> 27 #include <vector> 28 29 using namespace llvm; 30 using namespace llvm::opt; 31 32 void ArgList::append(Arg *A) { 33 Args.push_back(A); 34 35 // Update ranges for the option and all of its groups. 36 for (Option O = A->getOption().getUnaliasedOption(); O.isValid(); 37 O = O.getGroup()) { 38 auto &R = 39 OptRanges.insert(std::make_pair(O.getID(), emptyRange())).first->second; 40 R.first = std::min<unsigned>(R.first, Args.size() - 1); 41 R.second = Args.size(); 42 } 43 } 44 45 void ArgList::eraseArg(OptSpecifier Id) { 46 // Zero out the removed entries but keep them around so that we don't 47 // need to invalidate OptRanges. 48 for (Arg *const &A : filtered(Id)) { 49 // Avoid the need for a non-const filtered iterator variant. 50 Arg **ArgsBegin = Args.data(); 51 ArgsBegin[&A - ArgsBegin] = nullptr; 52 } 53 OptRanges.erase(Id.getID()); 54 } 55 56 ArgList::OptRange 57 ArgList::getRange(std::initializer_list<OptSpecifier> Ids) const { 58 OptRange R = emptyRange(); 59 for (auto Id : Ids) { 60 auto I = OptRanges.find(Id.getID()); 61 if (I != OptRanges.end()) { 62 R.first = std::min(R.first, I->second.first); 63 R.second = std::max(R.second, I->second.second); 64 } 65 } 66 // Map an empty {-1, 0} range to {0, 0} so it can be used to form iterators. 67 if (R.first == -1u) 68 R.first = 0; 69 return R; 70 } 71 72 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const { 73 if (Arg *A = getLastArg(Pos, Neg)) 74 return A->getOption().matches(Pos); 75 return Default; 76 } 77 78 bool ArgList::hasFlagNoClaim(OptSpecifier Pos, OptSpecifier Neg, 79 bool Default) const { 80 if (Arg *A = getLastArgNoClaim(Pos, Neg)) 81 return A->getOption().matches(Pos); 82 return Default; 83 } 84 85 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg, 86 bool Default) const { 87 if (Arg *A = getLastArg(Pos, PosAlias, Neg)) 88 return A->getOption().matches(Pos) || A->getOption().matches(PosAlias); 89 return Default; 90 } 91 92 StringRef ArgList::getLastArgValue(OptSpecifier Id, StringRef Default) const { 93 if (Arg *A = getLastArg(Id)) 94 return A->getValue(); 95 return Default; 96 } 97 98 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const { 99 SmallVector<const char *, 16> Values; 100 AddAllArgValues(Values, Id); 101 return std::vector<std::string>(Values.begin(), Values.end()); 102 } 103 104 void ArgList::addOptInFlag(ArgStringList &Output, OptSpecifier Pos, 105 OptSpecifier Neg) const { 106 if (Arg *A = getLastArg(Pos, Neg)) 107 if (A->getOption().matches(Pos)) 108 A->render(*this, Output); 109 } 110 111 void ArgList::AddAllArgsExcept(ArgStringList &Output, 112 ArrayRef<OptSpecifier> Ids, 113 ArrayRef<OptSpecifier> ExcludeIds) const { 114 for (const Arg *Arg : *this) { 115 bool Excluded = false; 116 for (OptSpecifier Id : ExcludeIds) { 117 if (Arg->getOption().matches(Id)) { 118 Excluded = true; 119 break; 120 } 121 } 122 if (!Excluded) { 123 for (OptSpecifier Id : Ids) { 124 if (Arg->getOption().matches(Id)) { 125 Arg->claim(); 126 Arg->render(*this, Output); 127 break; 128 } 129 } 130 } 131 } 132 } 133 134 /// This is a nicer interface when you don't have a list of Ids to exclude. 135 void ArgList::AddAllArgs(ArgStringList &Output, 136 ArrayRef<OptSpecifier> Ids) const { 137 ArrayRef<OptSpecifier> Exclude = std::nullopt; 138 AddAllArgsExcept(Output, Ids, Exclude); 139 } 140 141 /// This 3-opt variant of AddAllArgs could be eliminated in favor of one 142 /// that accepts a single specifier, given the above which accepts any number. 143 void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0, 144 OptSpecifier Id1, OptSpecifier Id2) const { 145 for (auto *Arg : filtered(Id0, Id1, Id2)) { 146 Arg->claim(); 147 Arg->render(*this, Output); 148 } 149 } 150 151 void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0, 152 OptSpecifier Id1, OptSpecifier Id2) const { 153 for (auto *Arg : filtered(Id0, Id1, Id2)) { 154 Arg->claim(); 155 const auto &Values = Arg->getValues(); 156 Output.append(Values.begin(), Values.end()); 157 } 158 } 159 160 void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0, 161 const char *Translation, 162 bool Joined) const { 163 for (auto *Arg : filtered(Id0)) { 164 Arg->claim(); 165 166 if (Joined) { 167 Output.push_back(MakeArgString(StringRef(Translation) + 168 Arg->getValue(0))); 169 } else { 170 Output.push_back(Translation); 171 Output.push_back(Arg->getValue(0)); 172 } 173 } 174 } 175 176 void ArgList::ClaimAllArgs(OptSpecifier Id0) const { 177 for (auto *Arg : filtered(Id0)) 178 Arg->claim(); 179 } 180 181 void ArgList::ClaimAllArgs() const { 182 for (auto *Arg : *this) 183 if (!Arg->isClaimed()) 184 Arg->claim(); 185 } 186 187 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index, 188 StringRef LHS, 189 StringRef RHS) const { 190 StringRef Cur = getArgString(Index); 191 if (Cur.size() == LHS.size() + RHS.size() && 192 Cur.startswith(LHS) && Cur.endswith(RHS)) 193 return Cur.data(); 194 195 return MakeArgString(LHS + RHS); 196 } 197 198 void ArgList::print(raw_ostream &O) const { 199 for (Arg *A : *this) { 200 O << "* "; 201 A->print(O); 202 } 203 } 204 205 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 206 LLVM_DUMP_METHOD void ArgList::dump() const { print(dbgs()); } 207 #endif 208 209 void InputArgList::releaseMemory() { 210 // An InputArgList always owns its arguments. 211 for (Arg *A : *this) 212 delete A; 213 } 214 215 InputArgList::InputArgList(const char* const *ArgBegin, 216 const char* const *ArgEnd) 217 : NumInputArgStrings(ArgEnd - ArgBegin) { 218 ArgStrings.append(ArgBegin, ArgEnd); 219 } 220 221 unsigned InputArgList::MakeIndex(StringRef String0) const { 222 unsigned Index = ArgStrings.size(); 223 224 // Tuck away so we have a reliable const char *. 225 SynthesizedStrings.push_back(std::string(String0)); 226 ArgStrings.push_back(SynthesizedStrings.back().c_str()); 227 228 return Index; 229 } 230 231 unsigned InputArgList::MakeIndex(StringRef String0, 232 StringRef String1) const { 233 unsigned Index0 = MakeIndex(String0); 234 unsigned Index1 = MakeIndex(String1); 235 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!"); 236 (void) Index1; 237 return Index0; 238 } 239 240 const char *InputArgList::MakeArgStringRef(StringRef Str) const { 241 return getArgString(MakeIndex(Str)); 242 } 243 244 DerivedArgList::DerivedArgList(const InputArgList &BaseArgs) 245 : BaseArgs(BaseArgs) {} 246 247 const char *DerivedArgList::MakeArgStringRef(StringRef Str) const { 248 return BaseArgs.MakeArgString(Str); 249 } 250 251 void DerivedArgList::AddSynthesizedArg(Arg *A) { 252 SynthesizedArgs.push_back(std::unique_ptr<Arg>(A)); 253 } 254 255 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const { 256 SynthesizedArgs.push_back( 257 std::make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), 258 BaseArgs.MakeIndex(Opt.getName()), BaseArg)); 259 return SynthesizedArgs.back().get(); 260 } 261 262 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt, 263 StringRef Value) const { 264 unsigned Index = BaseArgs.MakeIndex(Value); 265 SynthesizedArgs.push_back( 266 std::make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), 267 Index, BaseArgs.getArgString(Index), BaseArg)); 268 return SynthesizedArgs.back().get(); 269 } 270 271 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt, 272 StringRef Value) const { 273 unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value); 274 SynthesizedArgs.push_back( 275 std::make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), 276 Index, BaseArgs.getArgString(Index + 1), BaseArg)); 277 return SynthesizedArgs.back().get(); 278 } 279 280 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt, 281 StringRef Value) const { 282 unsigned Index = BaseArgs.MakeIndex((Opt.getName() + Value).str()); 283 SynthesizedArgs.push_back(std::make_unique<Arg>( 284 Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index, 285 BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg)); 286 return SynthesizedArgs.back().get(); 287 } 288