1 //===- Arg.cpp - Argument Implementations ---------------------------------===// 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/ADT/SmallString.h" 11 #include "llvm/Option/Arg.h" 12 #include "llvm/Option/ArgList.h" 13 #include "llvm/Option/Option.h" 14 #include "llvm/Support/Compiler.h" 15 #include "llvm/Support/Debug.h" 16 #include "llvm/Support/raw_ostream.h" 17 18 using namespace llvm; 19 using namespace llvm::opt; 20 21 Arg::Arg(const Option Opt, StringRef S, unsigned Index, const Arg *BaseArg) 22 : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false), 23 OwnsValues(false) {} 24 25 Arg::Arg(const Option Opt, StringRef S, unsigned Index, const char *Value0, 26 const Arg *BaseArg) 27 : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false), 28 OwnsValues(false) { 29 Values.push_back(Value0); 30 } 31 32 Arg::Arg(const Option Opt, StringRef S, unsigned Index, const char *Value0, 33 const char *Value1, const Arg *BaseArg) 34 : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false), 35 OwnsValues(false) { 36 Values.push_back(Value0); 37 Values.push_back(Value1); 38 } 39 40 Arg::~Arg() { 41 if (OwnsValues) { 42 for (unsigned i = 0, e = Values.size(); i != e; ++i) 43 delete[] Values[i]; 44 } 45 } 46 47 void Arg::print(raw_ostream& O) const { 48 O << "<"; 49 50 O << " Opt:"; 51 Opt.print(O); 52 53 O << " Index:" << Index; 54 55 O << " Values: ["; 56 for (unsigned i = 0, e = Values.size(); i != e; ++i) { 57 if (i) O << ", "; 58 O << "'" << Values[i] << "'"; 59 } 60 61 O << "]>\n"; 62 } 63 64 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 65 LLVM_DUMP_METHOD void Arg::dump() const { print(dbgs()); } 66 #endif 67 68 std::string Arg::getAsString(const ArgList &Args) const { 69 SmallString<256> Res; 70 raw_svector_ostream OS(Res); 71 72 ArgStringList ASL; 73 render(Args, ASL); 74 for (ArgStringList::iterator 75 it = ASL.begin(), ie = ASL.end(); it != ie; ++it) { 76 if (it != ASL.begin()) 77 OS << ' '; 78 OS << *it; 79 } 80 81 return OS.str(); 82 } 83 84 void Arg::renderAsInput(const ArgList &Args, ArgStringList &Output) const { 85 if (!getOption().hasNoOptAsInput()) { 86 render(Args, Output); 87 return; 88 } 89 90 Output.append(Values.begin(), Values.end()); 91 } 92 93 void Arg::render(const ArgList &Args, ArgStringList &Output) const { 94 switch (getOption().getRenderStyle()) { 95 case Option::RenderValuesStyle: 96 Output.append(Values.begin(), Values.end()); 97 break; 98 99 case Option::RenderCommaJoinedStyle: { 100 SmallString<256> Res; 101 raw_svector_ostream OS(Res); 102 OS << getSpelling(); 103 for (unsigned i = 0, e = getNumValues(); i != e; ++i) { 104 if (i) OS << ','; 105 OS << getValue(i); 106 } 107 Output.push_back(Args.MakeArgString(OS.str())); 108 break; 109 } 110 111 case Option::RenderJoinedStyle: 112 Output.push_back(Args.GetOrMakeJoinedArgString( 113 getIndex(), getSpelling(), getValue(0))); 114 Output.append(Values.begin() + 1, Values.end()); 115 break; 116 117 case Option::RenderSeparateStyle: 118 Output.push_back(Args.MakeArgString(getSpelling())); 119 Output.append(Values.begin(), Values.end()); 120 break; 121 } 122 } 123