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