clang API Documentation
00001 //===--- Arg.cpp - Argument Implementations -------------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "clang/Driver/Arg.h" 00011 #include "clang/Driver/ArgList.h" 00012 #include "clang/Driver/Option.h" 00013 #include "llvm/ADT/SmallString.h" 00014 #include "llvm/ADT/Twine.h" 00015 #include "llvm/Support/raw_ostream.h" 00016 00017 using namespace clang::driver; 00018 00019 Arg::Arg(const Option *_Opt, unsigned _Index, const Arg *_BaseArg) 00020 : Opt(_Opt), BaseArg(_BaseArg), Index(_Index), 00021 Claimed(false), OwnsValues(false) { 00022 } 00023 00024 Arg::Arg(const Option *_Opt, unsigned _Index, 00025 const char *Value0, const Arg *_BaseArg) 00026 : Opt(_Opt), BaseArg(_BaseArg), Index(_Index), 00027 Claimed(false), OwnsValues(false) { 00028 Values.push_back(Value0); 00029 } 00030 00031 Arg::Arg(const Option *_Opt, unsigned _Index, 00032 const char *Value0, const char *Value1, const Arg *_BaseArg) 00033 : Opt(_Opt), BaseArg(_BaseArg), Index(_Index), 00034 Claimed(false), OwnsValues(false) { 00035 Values.push_back(Value0); 00036 Values.push_back(Value1); 00037 } 00038 00039 Arg::~Arg() { 00040 if (OwnsValues) { 00041 for (unsigned i = 0, e = Values.size(); i != e; ++i) 00042 delete[] Values[i]; 00043 } 00044 } 00045 00046 void Arg::dump() const { 00047 llvm::errs() << "<"; 00048 00049 llvm::errs() << " Opt:"; 00050 Opt->dump(); 00051 00052 llvm::errs() << " Index:" << Index; 00053 00054 llvm::errs() << " Values: ["; 00055 for (unsigned i = 0, e = Values.size(); i != e; ++i) { 00056 if (i) llvm::errs() << ", "; 00057 llvm::errs() << "'" << Values[i] << "'"; 00058 } 00059 00060 llvm::errs() << "]>\n"; 00061 } 00062 00063 std::string Arg::getAsString(const ArgList &Args) const { 00064 SmallString<256> Res; 00065 llvm::raw_svector_ostream OS(Res); 00066 00067 ArgStringList ASL; 00068 render(Args, ASL); 00069 for (ArgStringList::iterator 00070 it = ASL.begin(), ie = ASL.end(); it != ie; ++it) { 00071 if (it != ASL.begin()) 00072 OS << ' '; 00073 OS << *it; 00074 } 00075 00076 return OS.str(); 00077 } 00078 00079 void Arg::renderAsInput(const ArgList &Args, ArgStringList &Output) const { 00080 if (!getOption().hasNoOptAsInput()) { 00081 render(Args, Output); 00082 return; 00083 } 00084 00085 for (unsigned i = 0, e = getNumValues(); i != e; ++i) 00086 Output.push_back(getValue(Args, i)); 00087 } 00088 00089 void Arg::render(const ArgList &Args, ArgStringList &Output) const { 00090 switch (getOption().getRenderStyle()) { 00091 case Option::RenderValuesStyle: 00092 for (unsigned i = 0, e = getNumValues(); i != e; ++i) 00093 Output.push_back(getValue(Args, i)); 00094 break; 00095 00096 case Option::RenderCommaJoinedStyle: { 00097 SmallString<256> Res; 00098 llvm::raw_svector_ostream OS(Res); 00099 OS << getOption().getName(); 00100 for (unsigned i = 0, e = getNumValues(); i != e; ++i) { 00101 if (i) OS << ','; 00102 OS << getValue(Args, i); 00103 } 00104 Output.push_back(Args.MakeArgString(OS.str())); 00105 break; 00106 } 00107 00108 case Option::RenderJoinedStyle: 00109 Output.push_back(Args.GetOrMakeJoinedArgString( 00110 getIndex(), getOption().getName(), getValue(Args, 0))); 00111 for (unsigned i = 1, e = getNumValues(); i != e; ++i) 00112 Output.push_back(getValue(Args, i)); 00113 break; 00114 00115 case Option::RenderSeparateStyle: 00116 Output.push_back(getOption().getName().data()); 00117 for (unsigned i = 0, e = getNumValues(); i != e; ++i) 00118 Output.push_back(getValue(Args, i)); 00119 break; 00120 } 00121 }