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