LLVM  4.0.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 #include "llvm/Support/Debug.h"
17 
18 using namespace llvm;
19 using namespace llvm::opt;
20 
21 Arg::Arg(const Option Opt, StringRef S, unsigned Index, const Arg *BaseArg)
22  : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false),
23  OwnsValues(false) {}
24 
25 Arg::Arg(const Option Opt, StringRef S, unsigned Index, const char *Value0,
26  const Arg *BaseArg)
27  : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false),
28  OwnsValues(false) {
29  Values.push_back(Value0);
30 }
31 
32 Arg::Arg(const Option Opt, StringRef S, unsigned Index, const char *Value0,
33  const char *Value1, const Arg *BaseArg)
34  : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false),
35  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::print(raw_ostream& O) const {
48  O << "<";
49 
50  O << " Opt:";
51  Opt.print(O);
52 
53  O << " Index:" << Index;
54 
55  O << " Values: [";
56  for (unsigned i = 0, e = Values.size(); i != e; ++i) {
57  if (i) O << ", ";
58  O << "'" << Values[i] << "'";
59  }
60 
61  O << "]>\n";
62 }
63 
64 LLVM_DUMP_METHOD void Arg::dump() const { print(dbgs()); }
65 
66 std::string Arg::getAsString(const ArgList &Args) const {
67  SmallString<256> Res;
69 
70  ArgStringList ASL;
71  render(Args, ASL);
73  it = ASL.begin(), ie = ASL.end(); it != ie; ++it) {
74  if (it != ASL.begin())
75  OS << ' ';
76  OS << *it;
77  }
78 
79  return OS.str();
80 }
81 
82 void Arg::renderAsInput(const ArgList &Args, ArgStringList &Output) const {
83  if (!getOption().hasNoOptAsInput()) {
84  render(Args, Output);
85  return;
86  }
87 
88  Output.append(Values.begin(), Values.end());
89 }
90 
91 void Arg::render(const ArgList &Args, ArgStringList &Output) const {
92  switch (getOption().getRenderStyle()) {
94  Output.append(Values.begin(), Values.end());
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  Output.append(Values.begin() + 1, Values.end());
113  break;
114 
116  Output.push_back(Args.MakeArgString(getSpelling()));
117  Output.append(Values.begin(), Values.end());
118  break;
119  }
120 }
void push_back(const T &Elt)
Definition: SmallVector.h:211
unsigned getIndex() const
Definition: Arg.h:72
const Option & getOption() const
Definition: Arg.h:70
size_t i
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:450
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:490
const char * getValue(unsigned N=0) const
Definition: Arg.h:92
void print(raw_ostream &O) const
Definition: Option.cpp:39
StringRef getSpelling() const
Definition: Arg.h:71
void print(raw_ostream &O) const
Definition: Arg.cpp:47
Function Alias Analysis false
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
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
void dump() const
Definition: Arg.cpp:64
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:338
const char * MakeArgString(const Twine &Str) const
Definition: ArgList.h:303
void renderAsInput(const ArgList &Args, ArgStringList &Output) const
Append the argument, render as an input, onto the given array as strings.
Definition: Arg.cpp:82
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:392
void render(const ArgList &Args, ArgStringList &Output) const
Append the argument onto the given array as strings.
Definition: Arg.cpp:91
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:515
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Defines the llvm::Arg class for parsed arguments.
unsigned getNumValues() const
Definition: Arg.h:91
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
std::string getAsString(const ArgList &Args) const
Return a formatted version of the argument and its values, for debugging and diagnostics.
Definition: Arg.cpp:66
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
ArgList - Ordered collection of driver arguments.
Definition: ArgList.h:94