Revert "Introduce a string_ostream string builder facilty"
[oota-llvm.git] / lib / Option / Arg.cpp
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"
15 #include "llvm/Support/raw_ostream.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),
22     Claimed(false), OwnsValues(false) {
23 }
24
25 Arg::Arg(const Option _Opt, StringRef S, unsigned _Index,
26          const char *Value0, const Arg *_BaseArg)
27   : Opt(_Opt), BaseArg(_BaseArg), Spelling(S), Index(_Index),
28     Claimed(false), OwnsValues(false) {
29   Values.push_back(Value0);
30 }
31
32 Arg::Arg(const Option _Opt, StringRef S, unsigned _Index,
33          const char *Value0, const char *Value1, const Arg *_BaseArg)
34   : Opt(_Opt), BaseArg(_BaseArg), Spelling(S), Index(_Index),
35     Claimed(false), OwnsValues(false) {
36   Values.push_back(Value0);
37   Values.push_back(Value1);
38 }
39
40 Arg::~Arg() {
41   if (OwnsValues) {
42     for (unsigned i = 0, e = Values.size(); i != e; ++i)
43       delete[] Values[i];
44   }
45 }
46
47 void Arg::dump() const {
48   llvm::errs() << "<";
49
50   llvm::errs() << " Opt:";
51   Opt.dump();
52
53   llvm::errs() << " Index:" << Index;
54
55   llvm::errs() << " Values: [";
56   for (unsigned i = 0, e = Values.size(); i != e; ++i) {
57     if (i) llvm::errs() << ", ";
58     llvm::errs() << "'" << Values[i] << "'";
59   }
60
61   llvm::errs() << "]>\n";
62 }
63
64 std::string Arg::getAsString(const ArgList &Args) const {
65   SmallString<256> Res;
66   llvm::raw_svector_ostream OS(Res);
67
68   ArgStringList ASL;
69   render(Args, ASL);
70   for (ArgStringList::iterator
71          it = ASL.begin(), ie = ASL.end(); it != ie; ++it) {
72     if (it != ASL.begin())
73       OS << ' ';
74     OS << *it;
75   }
76
77   return OS.str();
78 }
79
80 void Arg::renderAsInput(const ArgList &Args, ArgStringList &Output) const {
81   if (!getOption().hasNoOptAsInput()) {
82     render(Args, Output);
83     return;
84   }
85
86   for (unsigned i = 0, e = getNumValues(); i != e; ++i)
87     Output.push_back(getValue(i));
88 }
89
90 void Arg::render(const ArgList &Args, ArgStringList &Output) const {
91   switch (getOption().getRenderStyle()) {
92   case Option::RenderValuesStyle:
93     for (unsigned i = 0, e = getNumValues(); i != e; ++i)
94       Output.push_back(getValue(i));
95     break;
96
97   case Option::RenderCommaJoinedStyle: {
98     SmallString<256> Res;
99     llvm::raw_svector_ostream OS(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
109  case Option::RenderJoinedStyle:
110     Output.push_back(Args.GetOrMakeJoinedArgString(
111                        getIndex(), getSpelling(), getValue(0)));
112     for (unsigned i = 1, e = getNumValues(); i != e; ++i)
113       Output.push_back(getValue(i));
114     break;
115
116   case Option::RenderSeparateStyle:
117     Output.push_back(Args.MakeArgString(getSpelling()));
118     for (unsigned i = 0, e = getNumValues(); i != e; ++i)
119       Output.push_back(getValue(i));
120     break;
121   }
122 }