LLVM  8.0.1
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/ADT/SmallString.h"
11 #include "llvm/Config/llvm-config.h"
12 #include "llvm/Option/Arg.h"
13 #include "llvm/Option/ArgList.h"
14 #include "llvm/Option/Option.h"
15 #include "llvm/Support/Compiler.h"
16 #include "llvm/Support/Debug.h"
18 
19 using namespace llvm;
20 using namespace llvm::opt;
21 
22 Arg::Arg(const Option Opt, StringRef S, unsigned Index, const Arg *BaseArg)
23  : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false),
24  OwnsValues(false) {}
25 
26 Arg::Arg(const Option Opt, StringRef S, unsigned Index, const char *Value0,
27  const Arg *BaseArg)
28  : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false),
29  OwnsValues(false) {
30  Values.push_back(Value0);
31 }
32 
33 Arg::Arg(const Option Opt, StringRef S, unsigned Index, const char *Value0,
34  const char *Value1, const Arg *BaseArg)
35  : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false),
36  OwnsValues(false) {
37  Values.push_back(Value0);
38  Values.push_back(Value1);
39 }
40 
42  if (OwnsValues) {
43  for (unsigned i = 0, e = Values.size(); i != e; ++i)
44  delete[] Values[i];
45  }
46 }
47 
48 void Arg::print(raw_ostream& O) const {
49  O << "<";
50 
51  O << " Opt:";
52  Opt.print(O);
53 
54  O << " Index:" << Index;
55 
56  O << " Values: [";
57  for (unsigned i = 0, e = Values.size(); i != e; ++i) {
58  if (i) O << ", ";
59  O << "'" << Values[i] << "'";
60  }
61 
62  O << "]>\n";
63 }
64 
65 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
66 LLVM_DUMP_METHOD void Arg::dump() const { print(dbgs()); }
67 #endif
68 
69 std::string Arg::getAsString(const ArgList &Args) const {
70  SmallString<256> Res;
71  raw_svector_ostream OS(Res);
72 
73  ArgStringList ASL;
74  render(Args, ASL);
76  it = ASL.begin(), ie = ASL.end(); it != ie; ++it) {
77  if (it != ASL.begin())
78  OS << ' ';
79  OS << *it;
80  }
81 
82  return OS.str();
83 }
84 
85 void Arg::renderAsInput(const ArgList &Args, ArgStringList &Output) const {
86  if (!getOption().hasNoOptAsInput()) {
87  render(Args, Output);
88  return;
89  }
90 
91  Output.append(Values.begin(), Values.end());
92 }
93 
94 void Arg::render(const ArgList &Args, ArgStringList &Output) const {
95  switch (getOption().getRenderStyle()) {
97  Output.append(Values.begin(), Values.end());
98  break;
99 
101  SmallString<256> Res;
102  raw_svector_ostream OS(Res);
103  OS << getSpelling();
104  for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
105  if (i) OS << ',';
106  OS << getValue(i);
107  }
108  Output.push_back(Args.MakeArgString(OS.str()));
109  break;
110  }
111 
114  getIndex(), getSpelling(), getValue(0)));
115  Output.append(Values.begin() + 1, Values.end());
116  break;
117 
119  Output.push_back(Args.MakeArgString(getSpelling()));
120  Output.append(Values.begin(), Values.end());
121  break;
122  }
123 }
unsigned getNumValues() const
Definition: Arg.h:94
This class represents lattice values for constants.
Definition: AllocatorList.h:24
void print(raw_ostream &O) const
Definition: Arg.cpp:48
void renderAsInput(const ArgList &Args, ArgStringList &Output) const
Append the argument, render as an input, onto the given array as strings.
Definition: Arg.cpp:85
void push_back(const T &Elt)
Definition: SmallVector.h:218
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:510
std::string getAsString(const ArgList &Args) const
Return a formatted version of the argument and its values, for debugging and diagnostics.
Definition: Arg.cpp:69
const Option & getOption() const
Definition: Arg.h:73
#define LLVM_DUMP_METHOD
Definition: Compiler.h:74
void print(raw_ostream &O) const
Definition: Option.cpp:42
Arg(const Option Opt, StringRef Spelling, unsigned Index, const Arg *BaseArg=nullptr)
Definition: Arg.cpp:22
Option - Abstract representation for a single form of driver argument.
Definition: Option.h:52
Definition: Arg.h:27
A concrete instance of a particular driver option.
Definition: Arg.h:35
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:129
const char * MakeArgString(const Twine &Str) const
Definition: ArgList.h:351
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:190
unsigned getIndex() const
Definition: Arg.h:75
size_t size() const
Definition: SmallVector.h:53
StringRef getSpelling() const
Definition: Arg.h:74
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:535
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
Defines the llvm::Arg class for parsed arguments.
typename SuperClass::iterator iterator
Definition: SmallVector.h:327
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:394
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:133
void dump() const
Definition: Arg.cpp:66
void render(const ArgList &Args, ArgStringList &Output) const
Append the argument onto the given array as strings.
Definition: Arg.cpp:94
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
ArgList - Ordered collection of driver arguments.
Definition: ArgList.h:117
const char * getValue(unsigned N=0) const
Definition: Arg.h:96
constexpr char Args[]
Key for Kernel::Metadata::mArgs.