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 "clang/Driver/ArgList.h" 11 #include "clang/Driver/Arg.h" 12 #include "clang/Driver/DriverDiagnostic.h" 13 #include "clang/Driver/Option.h" 14 15 #include "llvm/ADT/SmallString.h" 16 #include "llvm/ADT/Twine.h" 17 #include "llvm/Support/raw_ostream.h" 18 19 using namespace clang; 20 using namespace clang::driver; 21 22 void arg_iterator::SkipToNextArg() { 23 for (; Current != Args.end(); ++Current) { 24 // Done if there are no filters. 25 if (!Id0.isValid()) 26 break; 27 28 // Otherwise require a match. 29 const Option &O = (*Current)->getOption(); 30 if (O.matches(Id0) || 31 (Id1.isValid() && O.matches(Id1)) || 32 (Id2.isValid() && O.matches(Id2))) 33 break; 34 } 35 } 36 37 // 38 39 ArgList::ArgList() { 40 } 41 42 ArgList::~ArgList() { 43 } 44 45 void ArgList::append(Arg *A) { 46 Args.push_back(A); 47 } 48 49 void ArgList::eraseArg(OptSpecifier Id) { 50 for (iterator it = begin(), ie = end(); it != ie; ) { 51 if ((*it)->getOption().matches(Id)) { 52 it = Args.erase(it); 53 ie = end(); 54 } else { 55 ++it; 56 } 57 } 58 } 59 60 Arg *ArgList::getLastArgNoClaim(OptSpecifier Id) const { 61 // FIXME: Make search efficient? 62 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it) 63 if ((*it)->getOption().matches(Id)) 64 return *it; 65 return 0; 66 } 67 68 Arg *ArgList::getLastArg(OptSpecifier Id) const { 69 Arg *Res = 0; 70 for (const_iterator it = begin(), ie = end(); it != ie; ++it) { 71 if ((*it)->getOption().matches(Id)) { 72 Res = *it; 73 Res->claim(); 74 } 75 } 76 77 return Res; 78 } 79 80 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const { 81 Arg *Res = 0; 82 for (const_iterator it = begin(), ie = end(); it != ie; ++it) { 83 if ((*it)->getOption().matches(Id0) || 84 (*it)->getOption().matches(Id1)) { 85 Res = *it; 86 Res->claim(); 87 88 } 89 } 90 91 return Res; 92 } 93 94 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, 95 OptSpecifier Id2) const { 96 Arg *Res = 0; 97 for (const_iterator it = begin(), ie = end(); it != ie; ++it) { 98 if ((*it)->getOption().matches(Id0) || 99 (*it)->getOption().matches(Id1) || 100 (*it)->getOption().matches(Id2)) { 101 Res = *it; 102 Res->claim(); 103 } 104 } 105 106 return Res; 107 } 108 109 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, 110 OptSpecifier Id2, OptSpecifier Id3) const { 111 Arg *Res = 0; 112 for (const_iterator it = begin(), ie = end(); it != ie; ++it) { 113 if ((*it)->getOption().matches(Id0) || 114 (*it)->getOption().matches(Id1) || 115 (*it)->getOption().matches(Id2) || 116 (*it)->getOption().matches(Id3)) { 117 Res = *it; 118 Res->claim(); 119 } 120 } 121 122 return Res; 123 } 124 125 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const { 126 if (Arg *A = getLastArg(Pos, Neg)) 127 return A->getOption().matches(Pos); 128 return Default; 129 } 130 131 StringRef ArgList::getLastArgValue(OptSpecifier Id, 132 StringRef Default) const { 133 if (Arg *A = getLastArg(Id)) 134 return A->getValue(*this); 135 return Default; 136 } 137 138 int ArgList::getLastArgIntValue(OptSpecifier Id, int Default, 139 clang::DiagnosticsEngine &Diags) const { 140 int Res = Default; 141 142 if (Arg *A = getLastArg(Id)) { 143 if (StringRef(A->getValue(*this)).getAsInteger(10, Res)) 144 Diags.Report(diag::err_drv_invalid_int_value) 145 << A->getAsString(*this) << A->getValue(*this); 146 } 147 148 return Res; 149 } 150 151 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const { 152 SmallVector<const char *, 16> Values; 153 AddAllArgValues(Values, Id); 154 return std::vector<std::string>(Values.begin(), Values.end()); 155 } 156 157 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const { 158 if (Arg *A = getLastArg(Id)) { 159 A->claim(); 160 A->render(*this, Output); 161 } 162 } 163 164 void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0, 165 OptSpecifier Id1, OptSpecifier Id2) const { 166 for (arg_iterator it = filtered_begin(Id0, Id1, Id2), 167 ie = filtered_end(); it != ie; ++it) { 168 (*it)->claim(); 169 (*it)->render(*this, Output); 170 } 171 } 172 173 void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0, 174 OptSpecifier Id1, OptSpecifier Id2) const { 175 for (arg_iterator it = filtered_begin(Id0, Id1, Id2), 176 ie = filtered_end(); it != ie; ++it) { 177 (*it)->claim(); 178 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i) 179 Output.push_back((*it)->getValue(*this, i)); 180 } 181 } 182 183 void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0, 184 const char *Translation, 185 bool Joined) const { 186 for (arg_iterator it = filtered_begin(Id0), 187 ie = filtered_end(); it != ie; ++it) { 188 (*it)->claim(); 189 190 if (Joined) { 191 Output.push_back(MakeArgString(StringRef(Translation) + 192 (*it)->getValue(*this, 0))); 193 } else { 194 Output.push_back(Translation); 195 Output.push_back((*it)->getValue(*this, 0)); 196 } 197 } 198 } 199 200 void ArgList::ClaimAllArgs(OptSpecifier Id0) const { 201 for (arg_iterator it = filtered_begin(Id0), 202 ie = filtered_end(); it != ie; ++it) 203 (*it)->claim(); 204 } 205 206 void ArgList::ClaimAllArgs() const { 207 for (const_iterator it = begin(), ie = end(); it != ie; ++it) 208 if (!(*it)->isClaimed()) 209 (*it)->claim(); 210 } 211 212 const char *ArgList::MakeArgString(const Twine &T) const { 213 llvm::SmallString<256> Str; 214 T.toVector(Str); 215 return MakeArgString(Str.str()); 216 } 217 218 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index, 219 StringRef LHS, 220 StringRef RHS) const { 221 StringRef Cur = getArgString(Index); 222 if (Cur.size() == LHS.size() + RHS.size() && 223 Cur.startswith(LHS) && Cur.endswith(RHS)) 224 return Cur.data(); 225 226 return MakeArgString(LHS + RHS); 227 } 228 229 // 230 231 InputArgList::InputArgList(const char* const *ArgBegin, 232 const char* const *ArgEnd) 233 : NumInputArgStrings(ArgEnd - ArgBegin) { 234 ArgStrings.append(ArgBegin, ArgEnd); 235 } 236 237 InputArgList::~InputArgList() { 238 // An InputArgList always owns its arguments. 239 for (iterator it = begin(), ie = end(); it != ie; ++it) 240 delete *it; 241 } 242 243 unsigned InputArgList::MakeIndex(StringRef String0) const { 244 unsigned Index = ArgStrings.size(); 245 246 // Tuck away so we have a reliable const char *. 247 SynthesizedStrings.push_back(String0); 248 ArgStrings.push_back(SynthesizedStrings.back().c_str()); 249 250 return Index; 251 } 252 253 unsigned InputArgList::MakeIndex(StringRef String0, 254 StringRef String1) const { 255 unsigned Index0 = MakeIndex(String0); 256 unsigned Index1 = MakeIndex(String1); 257 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!"); 258 (void) Index1; 259 return Index0; 260 } 261 262 const char *InputArgList::MakeArgString(StringRef Str) const { 263 return getArgString(MakeIndex(Str)); 264 } 265 266 // 267 268 DerivedArgList::DerivedArgList(const InputArgList &_BaseArgs) 269 : BaseArgs(_BaseArgs) { 270 } 271 272 DerivedArgList::~DerivedArgList() { 273 // We only own the arguments we explicitly synthesized. 274 for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end(); 275 it != ie; ++it) 276 delete *it; 277 } 278 279 const char *DerivedArgList::MakeArgString(StringRef Str) const { 280 return BaseArgs.MakeArgString(Str); 281 } 282 283 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option *Opt) const { 284 Arg *A = new Arg(Opt, BaseArgs.MakeIndex(Opt->getName()), BaseArg); 285 SynthesizedArgs.push_back(A); 286 return A; 287 } 288 289 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option *Opt, 290 StringRef Value) const { 291 unsigned Index = BaseArgs.MakeIndex(Value); 292 Arg *A = new Arg(Opt, Index, BaseArgs.getArgString(Index), BaseArg); 293 SynthesizedArgs.push_back(A); 294 return A; 295 } 296 297 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option *Opt, 298 StringRef Value) const { 299 unsigned Index = BaseArgs.MakeIndex(Opt->getName(), Value); 300 Arg *A = new Arg(Opt, Index, BaseArgs.getArgString(Index + 1), BaseArg); 301 SynthesizedArgs.push_back(A); 302 return A; 303 } 304 305 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option *Opt, 306 StringRef Value) const { 307 unsigned Index = BaseArgs.MakeIndex(Opt->getName().str() + Value.str()); 308 Arg *A = new Arg(Opt, Index, 309 BaseArgs.getArgString(Index) + Opt->getName().size(), 310 BaseArg); 311 SynthesizedArgs.push_back(A); 312 return A; 313 } 314