LLVM  9.0.0svn
Arg.cpp
Go to the documentation of this file.
1 //===- Arg.cpp - Argument Implementations ---------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ADT/SmallString.h"
10 #include "llvm/Config/llvm-config.h"
11 #include "llvm/Option/Arg.h"
12 #include "llvm/Option/ArgList.h"
13 #include "llvm/Option/Option.h"
14 #include "llvm/Support/Compiler.h"
15 #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 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
65 LLVM_DUMP_METHOD void Arg::dump() const { print(dbgs()); }
66 #endif
67 
68 std::string Arg::getAsString(const ArgList &Args) const {
69  SmallString<256> Res;
70  raw_svector_ostream OS(Res);
71 
72  ArgStringList ASL;
73  render(Args, ASL);
75  it = ASL.begin(), ie = ASL.end(); it != ie; ++it) {
76  if (it != ASL.begin())
77  OS << ' ';
78  OS << *it;
79  }
80 
81  return OS.str();
82 }
83 
84 void Arg::renderAsInput(const ArgList &Args, ArgStringList &Output) const {
85  if (!getOption().hasNoOptAsInput()) {
86  render(Args, Output);
87  return;
88  }
89 
90  Output.append(Values.begin(), Values.end());
91 }
92 
93 void Arg::render(const ArgList &Args, ArgStringList &Output) const {
94  switch (getOption().getRenderStyle()) {
96  Output.append(Values.begin(), Values.end());
97  break;
98 
100  SmallString<256> Res;
101  raw_svector_ostream OS(Res);
102  OS << getSpelling();
103  for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
104  if (i) OS << ',';
105  OS << getValue(i);
106  }
107  Output.push_back(Args.MakeArgString(OS.str()));
108  break;
109  }
110 
113  getIndex(), getSpelling(), getValue(0)));
114  Output.append(Values.begin() + 1, Values.end());
115  break;
116 
118  Output.push_back(Args.MakeArgString(getSpelling()));
119  Output.append(Values.begin(), Values.end());
120  break;
121  }
122 }
unsigned getNumValues() const
Definition: Arg.h:93
This class represents lattice values for constants.
Definition: AllocatorList.h:23
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:473
void print(raw_ostream &O) const
Definition: Arg.cpp:47
void renderAsInput(const ArgList &Args, ArgStringList &Output) const
Append the argument, render as an input, onto the given array as strings.
Definition: Arg.cpp:84
void push_back(const T &Elt)
Definition: SmallVector.h:211
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:509
std::string getAsString(const ArgList &Args) const
Return a formatted version of the argument and its values, for debugging and diagnostics.
Definition: Arg.cpp:68
const Option & getOption() const
Definition: Arg.h:72
void print(raw_ostream &O) const
Definition: Option.cpp:41
Arg(const Option Opt, StringRef Spelling, unsigned Index, const Arg *BaseArg=nullptr)
Definition: Arg.cpp:21
Option - Abstract representation for a single form of driver argument.
Definition: Option.h:51
Definition: Arg.h:26
A concrete instance of a particular driver option.
Definition: Arg.h:34
const char * MakeArgString(const Twine &Str) const
Definition: ArgList.h:350
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:189
unsigned getIndex() const
Definition: Arg.h:74
size_t size() const
Definition: SmallVector.h:52
StringRef getSpelling() const
Definition: Arg.h:73
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:534
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.
typename SuperClass::iterator iterator
Definition: SmallVector.h:319
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:387
void dump() const
Definition: Arg.cpp:65
void render(const ArgList &Args, ArgStringList &Output) const
Append the argument onto the given array as strings.
Definition: Arg.cpp:93
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
ArgList - Ordered collection of driver arguments.
Definition: ArgList.h:116
const char * getValue(unsigned N=0) const
Definition: Arg.h:95
constexpr char Args[]
Key for Kernel::Metadata::mArgs.