blob: 3e8a1d802314b01d2744fcfd375f4b06b322af21 [file] [log] [blame]
Michael J. Spencer41ee0412012-12-05 00:29:32 +00001//===--- 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"
Michael J. Spencer41ee0412012-12-05 00:29:32 +000011#include "llvm/ADT/SmallString.h"
12#include "llvm/ADT/Twine.h"
13#include "llvm/Option/ArgList.h"
14#include "llvm/Option/Option.h"
Vedant Kumar2892a4a2015-12-18 02:30:45 +000015#include "llvm/Support/raw_ostream.h"
Eric Christopher9a8b5e72015-12-18 18:55:26 +000016#include "llvm/Support/Debug.h"
Michael J. Spencer41ee0412012-12-05 00:29:32 +000017
18using namespace llvm;
19using namespace llvm::opt;
20
David Blaikie9f380a32015-03-16 18:06:57 +000021Arg::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) {}
Michael J. Spencer41ee0412012-12-05 00:29:32 +000024
David Blaikie9f380a32015-03-16 18:06:57 +000025Arg::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) {
Michael J. Spencer41ee0412012-12-05 00:29:32 +000029 Values.push_back(Value0);
30}
31
David Blaikie9f380a32015-03-16 18:06:57 +000032Arg::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) {
Michael J. Spencer41ee0412012-12-05 00:29:32 +000036 Values.push_back(Value0);
37 Values.push_back(Value1);
38}
39
40Arg::~Arg() {
41 if (OwnsValues) {
42 for (unsigned i = 0, e = Values.size(); i != e; ++i)
43 delete[] Values[i];
44 }
45}
46
Eric Christopher9a8b5e72015-12-18 18:55:26 +000047void Arg::print(raw_ostream& O) const {
48 O << "<";
Michael J. Spencer41ee0412012-12-05 00:29:32 +000049
Eric Christopher9a8b5e72015-12-18 18:55:26 +000050 O << " Opt:";
51 Opt.print(O);
Michael J. Spencer41ee0412012-12-05 00:29:32 +000052
Eric Christopher9a8b5e72015-12-18 18:55:26 +000053 O << " Index:" << Index;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000054
Eric Christopher9a8b5e72015-12-18 18:55:26 +000055 O << " Values: [";
Michael J. Spencer41ee0412012-12-05 00:29:32 +000056 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
Eric Christopher9a8b5e72015-12-18 18:55:26 +000057 if (i) O << ", ";
58 O << "'" << Values[i] << "'";
Michael J. Spencer41ee0412012-12-05 00:29:32 +000059 }
60
Eric Christopher9a8b5e72015-12-18 18:55:26 +000061 O << "]>\n";
Michael J. Spencer41ee0412012-12-05 00:29:32 +000062}
63
Matthias Braun8c209aa2017-01-28 02:02:38 +000064#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Eric Christopher9a8b5e72015-12-18 18:55:26 +000065LLVM_DUMP_METHOD void Arg::dump() const { print(dbgs()); }
Matthias Braun8c209aa2017-01-28 02:02:38 +000066#endif
Eric Christopher9a8b5e72015-12-18 18:55:26 +000067
Michael J. Spencer41ee0412012-12-05 00:29:32 +000068std::string Arg::getAsString(const ArgList &Args) const {
Alp Tokere69170a2014-06-26 22:52:05 +000069 SmallString<256> Res;
70 llvm::raw_svector_ostream OS(Res);
Michael J. Spencer41ee0412012-12-05 00:29:32 +000071
72 ArgStringList ASL;
73 render(Args, ASL);
74 for (ArgStringList::iterator
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
84void Arg::renderAsInput(const ArgList &Args, ArgStringList &Output) const {
85 if (!getOption().hasNoOptAsInput()) {
86 render(Args, Output);
87 return;
88 }
89
Benjamin Kramer6cd780f2015-02-17 15:29:18 +000090 Output.append(Values.begin(), Values.end());
Michael J. Spencer41ee0412012-12-05 00:29:32 +000091}
92
93void Arg::render(const ArgList &Args, ArgStringList &Output) const {
94 switch (getOption().getRenderStyle()) {
95 case Option::RenderValuesStyle:
Benjamin Kramer6cd780f2015-02-17 15:29:18 +000096 Output.append(Values.begin(), Values.end());
Michael J. Spencer41ee0412012-12-05 00:29:32 +000097 break;
98
99 case Option::RenderCommaJoinedStyle: {
Alp Tokere69170a2014-06-26 22:52:05 +0000100 SmallString<256> Res;
101 llvm::raw_svector_ostream OS(Res);
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000102 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
111 case Option::RenderJoinedStyle:
112 Output.push_back(Args.GetOrMakeJoinedArgString(
113 getIndex(), getSpelling(), getValue(0)));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000114 Output.append(Values.begin() + 1, Values.end());
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000115 break;
116
117 case Option::RenderSeparateStyle:
118 Output.push_back(Args.MakeArgString(getSpelling()));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000119 Output.append(Values.begin(), Values.end());
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000120 break;
121 }
122}