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