LLVM API Documentation

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