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"
17 #include "llvm/Support/raw_ostream.h"
18
19 using namespace llvm;
20 using namespace llvm::opt;
21
Arg(const Option Opt,StringRef S,unsigned Index,const Arg * BaseArg)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
Arg(const Option Opt,StringRef S,unsigned Index,const char * Value0,const Arg * BaseArg)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
Arg(const Option Opt,StringRef S,unsigned Index,const char * Value0,const char * Value1,const Arg * BaseArg)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
~Arg()41 Arg::~Arg() {
42 if (OwnsValues) {
43 for (unsigned i = 0, e = Values.size(); i != e; ++i)
44 delete[] Values[i];
45 }
46 }
47
print(raw_ostream & O) const48 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)
dump() const66 LLVM_DUMP_METHOD void Arg::dump() const { print(dbgs()); }
67 #endif
68
getAsString(const ArgList & Args) const69 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);
75 for (ArgStringList::iterator
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
renderAsInput(const ArgList & Args,ArgStringList & Output) const85 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
render(const ArgList & Args,ArgStringList & Output) const94 void Arg::render(const ArgList &Args, ArgStringList &Output) const {
95 switch (getOption().getRenderStyle()) {
96 case Option::RenderValuesStyle:
97 Output.append(Values.begin(), Values.end());
98 break;
99
100 case Option::RenderCommaJoinedStyle: {
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
112 case Option::RenderJoinedStyle:
113 Output.push_back(Args.GetOrMakeJoinedArgString(
114 getIndex(), getSpelling(), getValue(0)));
115 Output.append(Values.begin() + 1, Values.end());
116 break;
117
118 case Option::RenderSeparateStyle:
119 Output.push_back(Args.MakeArgString(getSpelling()));
120 Output.append(Values.begin(), Values.end());
121 break;
122 }
123 }
124