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