LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Arg.cpp
Go to the documentation of this file.
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/Option/Arg.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/Option/ArgList.h"
14 #include "llvm/Option/Option.h"
16 
17 using namespace llvm;
18 using namespace llvm::opt;
19 
20 Arg::Arg(const Option _Opt, StringRef S, unsigned _Index, const Arg *_BaseArg)
21  : Opt(_Opt), BaseArg(_BaseArg), Spelling(S), Index(_Index),
22  Claimed(false), OwnsValues(false) {
23 }
24 
25 Arg::Arg(const Option _Opt, StringRef S, unsigned _Index,
26  const char *Value0, const Arg *_BaseArg)
27  : Opt(_Opt), BaseArg(_BaseArg), Spelling(S), Index(_Index),
28  Claimed(false), OwnsValues(false) {
29  Values.push_back(Value0);
30 }
31 
32 Arg::Arg(const Option _Opt, StringRef S, unsigned _Index,
33  const char *Value0, const char *Value1, const Arg *_BaseArg)
34  : Opt(_Opt), BaseArg(_BaseArg), Spelling(S), Index(_Index),
35  Claimed(false), OwnsValues(false) {
36  Values.push_back(Value0);
37  Values.push_back(Value1);
38 }
39 
41  if (OwnsValues) {
42  for (unsigned i = 0, e = Values.size(); i != e; ++i)
43  delete[] Values[i];
44  }
45 }
46 
47 void Arg::dump() const {
48  llvm::errs() << "<";
49 
50  llvm::errs() << " Opt:";
51  Opt.dump();
52 
53  llvm::errs() << " Index:" << Index;
54 
55  llvm::errs() << " Values: [";
56  for (unsigned i = 0, e = Values.size(); i != e; ++i) {
57  if (i) llvm::errs() << ", ";
58  llvm::errs() << "'" << Values[i] << "'";
59  }
60 
61  llvm::errs() << "]>\n";
62 }
63 
64 std::string Arg::getAsString(const ArgList &Args) const {
65  SmallString<256> Res;
67 
68  ArgStringList ASL;
69  render(Args, ASL);
71  it = ASL.begin(), ie = ASL.end(); it != ie; ++it) {
72  if (it != ASL.begin())
73  OS << ' ';
74  OS << *it;
75  }
76 
77  return OS.str();
78 }
79 
80 void Arg::renderAsInput(const ArgList &Args, ArgStringList &Output) const {
81  if (!getOption().hasNoOptAsInput()) {
82  render(Args, Output);
83  return;
84  }
85 
86  for (unsigned i = 0, e = getNumValues(); i != e; ++i)
87  Output.push_back(getValue(i));
88 }
89 
90 void Arg::render(const ArgList &Args, ArgStringList &Output) const {
91  switch (getOption().getRenderStyle()) {
93  for (unsigned i = 0, e = getNumValues(); i != e; ++i)
94  Output.push_back(getValue(i));
95  break;
96 
98  SmallString<256> Res;
100  OS << getSpelling();
101  for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
102  if (i) OS << ',';
103  OS << getValue(i);
104  }
105  Output.push_back(Args.MakeArgString(OS.str()));
106  break;
107  }
108 
111  getIndex(), getSpelling(), getValue(0)));
112  for (unsigned i = 1, e = getNumValues(); i != e; ++i)
113  Output.push_back(getValue(i));
114  break;
115 
117  Output.push_back(Args.MakeArgString(getSpelling()));
118  for (unsigned i = 0, e = getNumValues(); i != e; ++i)
119  Output.push_back(getValue(i));
120  break;
121  }
122 }
void push_back(const T &Elt)
Definition: SmallVector.h:236
unsigned getIndex() const
Definition: Arg.h:75
void dump() const
Definition: Option.cpp:41
raw_ostream & errs()
virtual const char * MakeArgString(StringRef Str) const =0
const char * getValue(unsigned N=0) const
Definition: Arg.h:97
StringRef getSpelling() const
Definition: Arg.h:74
#define false
Definition: ConvertUTF.c:64
A concrete instance of a particular driver option.
Definition: Arg.h:34
const char * GetOrMakeJoinedArgString(unsigned Index, StringRef LHS, StringRef RHS) const
Create an arg string for (LHS + RHS), reusing the string at Index if possible.
Definition: ArgList.cpp:298
void renderAsInput(const ArgList &Args, ArgStringList &Output) const
Append the argument, render as an input, onto the given array as strings.
Definition: Arg.cpp:80
void render(const ArgList &Args, ArgStringList &Output) const
Append the argument onto the given array as strings.
Definition: Arg.cpp:90
void dump() const
Definition: Arg.cpp:47
const Option getOption() const
Definition: Arg.h:73
Defines the llvm::Arg class for parsed arguments.
unsigned getNumValues() const
Definition: Arg.h:96
std::string getAsString(const ArgList &Args) const
Return a formatted version of the argument and its values, for debugging and diagnostics.
Definition: Arg.cpp:64