LLVM  3.7.0
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), Claimed(false),
22  OwnsValues(false) {}
23 
24 Arg::Arg(const Option Opt, StringRef S, unsigned Index, const char *Value0,
25  const Arg *BaseArg)
26  : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false),
27  OwnsValues(false) {
28  Values.push_back(Value0);
29 }
30 
31 Arg::Arg(const Option Opt, StringRef S, unsigned Index, const char *Value0,
32  const char *Value1, const Arg *BaseArg)
33  : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false),
34  OwnsValues(false) {
35  Values.push_back(Value0);
36  Values.push_back(Value1);
37 }
38 
40  if (OwnsValues) {
41  for (unsigned i = 0, e = Values.size(); i != e; ++i)
42  delete[] Values[i];
43  }
44 }
45 
46 void Arg::dump() const {
47  llvm::errs() << "<";
48 
49  llvm::errs() << " Opt:";
50  Opt.dump();
51 
52  llvm::errs() << " Index:" << Index;
53 
54  llvm::errs() << " Values: [";
55  for (unsigned i = 0, e = Values.size(); i != e; ++i) {
56  if (i) llvm::errs() << ", ";
57  llvm::errs() << "'" << Values[i] << "'";
58  }
59 
60  llvm::errs() << "]>\n";
61 }
62 
63 std::string Arg::getAsString(const ArgList &Args) const {
64  SmallString<256> Res;
66 
67  ArgStringList ASL;
68  render(Args, ASL);
70  it = ASL.begin(), ie = ASL.end(); it != ie; ++it) {
71  if (it != ASL.begin())
72  OS << ' ';
73  OS << *it;
74  }
75 
76  return OS.str();
77 }
78 
79 void Arg::renderAsInput(const ArgList &Args, ArgStringList &Output) const {
80  if (!getOption().hasNoOptAsInput()) {
81  render(Args, Output);
82  return;
83  }
84 
85  Output.append(Values.begin(), Values.end());
86 }
87 
88 void Arg::render(const ArgList &Args, ArgStringList &Output) const {
89  switch (getOption().getRenderStyle()) {
91  Output.append(Values.begin(), Values.end());
92  break;
93 
95  SmallString<256> Res;
97  OS << getSpelling();
98  for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
99  if (i) OS << ',';
100  OS << getValue(i);
101  }
102  Output.push_back(Args.MakeArgString(OS.str()));
103  break;
104  }
105 
108  getIndex(), getSpelling(), getValue(0)));
109  Output.append(Values.begin() + 1, Values.end());
110  break;
111 
113  Output.push_back(Args.MakeArgString(getSpelling()));
114  Output.append(Values.begin(), Values.end());
115  break;
116  }
117 }
void push_back(const T &Elt)
Definition: SmallVector.h:222
unsigned getIndex() const
Definition: Arg.h:72
void dump() const
Definition: Option.cpp:38
const Option & getOption() const
Definition: Arg.h:70
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:488
const char * getValue(unsigned N=0) const
Definition: Arg.h:92
StringRef getSpelling() const
Definition: Arg.h:71
#define false
Definition: ConvertUTF.c:65
Option - Abstract representation for a single form of driver argument.
Definition: Option.h:44
A concrete instance of a particular driver option.
Definition: Arg.h:31
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:305
const char * MakeArgString(const Twine &Str) const
Definition: ArgList.h:296
void renderAsInput(const ArgList &Args, ArgStringList &Output) const
Append the argument, render as an input, onto the given array as strings.
Definition: Arg.cpp:79
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:416
void render(const ArgList &Args, ArgStringList &Output) const
Append the argument onto the given array as strings.
Definition: Arg.cpp:88
void dump() const
Definition: Arg.cpp:46
Defines the llvm::Arg class for parsed arguments.
StringRef str()
Flushes the stream contents to the target vector and return a StringRef for the vector contents...
unsigned getNumValues() const
Definition: Arg.h:91
std::string getAsString(const ArgList &Args) const
Return a formatted version of the argument and its values, for debugging and diagnostics.
Definition: Arg.cpp:63
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
ArgList - Ordered collection of driver arguments.
Definition: ArgList.h:94