blob: c3de2d1a49657743c45082f4b7774ed49a496b3f [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
Eric Christopher9a8b5e72015-12-18 18:55:26 +000064LLVM_DUMP_METHOD void Arg::dump() const { print(dbgs()); }
65
Michael J. Spencer41ee0412012-12-05 00:29:32 +000066std::string Arg::getAsString(const ArgList &Args) const {
Alp Tokere69170a2014-06-26 22:52:05 +000067 SmallString<256> Res;
68 llvm::raw_svector_ostream OS(Res);
Michael J. Spencer41ee0412012-12-05 00:29:32 +000069
70 ArgStringList ASL;
71 render(Args, ASL);
72 for (ArgStringList::iterator
73 it = ASL.begin(), ie = ASL.end(); it != ie; ++it) {
74 if (it != ASL.begin())
75 OS << ' ';
76 OS << *it;
77 }
78
79 return OS.str();
80}
81
82void Arg::renderAsInput(const ArgList &Args, ArgStringList &Output) const {
83 if (!getOption().hasNoOptAsInput()) {
84 render(Args, Output);
85 return;
86 }
87
Benjamin Kramer6cd780f2015-02-17 15:29:18 +000088 Output.append(Values.begin(), Values.end());
Michael J. Spencer41ee0412012-12-05 00:29:32 +000089}
90
91void Arg::render(const ArgList &Args, ArgStringList &Output) const {
92 switch (getOption().getRenderStyle()) {
93 case Option::RenderValuesStyle:
Benjamin Kramer6cd780f2015-02-17 15:29:18 +000094 Output.append(Values.begin(), Values.end());
Michael J. Spencer41ee0412012-12-05 00:29:32 +000095 break;
96
97 case Option::RenderCommaJoinedStyle: {
Alp Tokere69170a2014-06-26 22:52:05 +000098 SmallString<256> Res;
99 llvm::raw_svector_ostream OS(Res);
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000100 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)));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000112 Output.append(Values.begin() + 1, Values.end());
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000113 break;
114
115 case Option::RenderSeparateStyle:
116 Output.push_back(Args.MakeArgString(getSpelling()));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000117 Output.append(Values.begin(), Values.end());
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000118 break;
119 }
120}