blob: 4ce40e3ab26c6128f1dd3b5733cc9f826c58d9a2 [file] [log] [blame]
Eugene Zelenko1d475d82017-06-16 00:43:26 +00001//===- Arg.cpp - Argument Implementations ---------------------------------===//
Michael J. Spencer96a564f2012-12-05 00:29:32 +00002//
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
Michael J. Spencer96a564f2012-12-05 00:29:32 +000010#include "llvm/ADT/SmallString.h"
Nico Weber0f38c602018-04-30 14:59:11 +000011#include "llvm/Config/llvm-config.h"
Eugene Zelenko1d475d82017-06-16 00:43:26 +000012#include "llvm/Option/Arg.h"
Michael J. Spencer96a564f2012-12-05 00:29:32 +000013#include "llvm/Option/ArgList.h"
14#include "llvm/Option/Option.h"
Eugene Zelenko1d475d82017-06-16 00:43:26 +000015#include "llvm/Support/Compiler.h"
Eric Christopher96e80fc2015-12-18 18:55:26 +000016#include "llvm/Support/Debug.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000017#include "llvm/Support/raw_ostream.h"
Michael J. Spencer96a564f2012-12-05 00:29:32 +000018
19using namespace llvm;
20using namespace llvm::opt;
21
David Blaikie7610ba72015-03-16 18:06:57 +000022Arg::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) {}
Michael J. Spencer96a564f2012-12-05 00:29:32 +000025
David Blaikie7610ba72015-03-16 18:06:57 +000026Arg::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) {
Michael J. Spencer96a564f2012-12-05 00:29:32 +000030 Values.push_back(Value0);
31}
32
David Blaikie7610ba72015-03-16 18:06:57 +000033Arg::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) {
Michael J. Spencer96a564f2012-12-05 00:29:32 +000037 Values.push_back(Value0);
38 Values.push_back(Value1);
39}
40
41Arg::~Arg() {
42 if (OwnsValues) {
43 for (unsigned i = 0, e = Values.size(); i != e; ++i)
44 delete[] Values[i];
45 }
46}
47
Eric Christopher96e80fc2015-12-18 18:55:26 +000048void Arg::print(raw_ostream& O) const {
49 O << "<";
Michael J. Spencer96a564f2012-12-05 00:29:32 +000050
Eric Christopher96e80fc2015-12-18 18:55:26 +000051 O << " Opt:";
52 Opt.print(O);
Michael J. Spencer96a564f2012-12-05 00:29:32 +000053
Eric Christopher96e80fc2015-12-18 18:55:26 +000054 O << " Index:" << Index;
Michael J. Spencer96a564f2012-12-05 00:29:32 +000055
Eric Christopher96e80fc2015-12-18 18:55:26 +000056 O << " Values: [";
Michael J. Spencer96a564f2012-12-05 00:29:32 +000057 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
Eric Christopher96e80fc2015-12-18 18:55:26 +000058 if (i) O << ", ";
59 O << "'" << Values[i] << "'";
Michael J. Spencer96a564f2012-12-05 00:29:32 +000060 }
61
Eric Christopher96e80fc2015-12-18 18:55:26 +000062 O << "]>\n";
Michael J. Spencer96a564f2012-12-05 00:29:32 +000063}
64
Aaron Ballman1d03d382017-10-15 14:32:27 +000065#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Eric Christopher96e80fc2015-12-18 18:55:26 +000066LLVM_DUMP_METHOD void Arg::dump() const { print(dbgs()); }
Matthias Braun88d20752017-01-28 02:02:38 +000067#endif
Eric Christopher96e80fc2015-12-18 18:55:26 +000068
Michael J. Spencer96a564f2012-12-05 00:29:32 +000069std::string Arg::getAsString(const ArgList &Args) const {
Alp Toker8dd8d5c2014-06-26 22:52:05 +000070 SmallString<256> Res;
Eugene Zelenko1d475d82017-06-16 00:43:26 +000071 raw_svector_ostream OS(Res);
Michael J. Spencer96a564f2012-12-05 00:29:32 +000072
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
85void Arg::renderAsInput(const ArgList &Args, ArgStringList &Output) const {
86 if (!getOption().hasNoOptAsInput()) {
87 render(Args, Output);
88 return;
89 }
90
Benjamin Kramer1a50a122015-02-17 15:29:18 +000091 Output.append(Values.begin(), Values.end());
Michael J. Spencer96a564f2012-12-05 00:29:32 +000092}
93
94void Arg::render(const ArgList &Args, ArgStringList &Output) const {
95 switch (getOption().getRenderStyle()) {
96 case Option::RenderValuesStyle:
Benjamin Kramer1a50a122015-02-17 15:29:18 +000097 Output.append(Values.begin(), Values.end());
Michael J. Spencer96a564f2012-12-05 00:29:32 +000098 break;
99
100 case Option::RenderCommaJoinedStyle: {
Alp Toker8dd8d5c2014-06-26 22:52:05 +0000101 SmallString<256> Res;
Eugene Zelenko1d475d82017-06-16 00:43:26 +0000102 raw_svector_ostream OS(Res);
Michael J. Spencer96a564f2012-12-05 00:29:32 +0000103 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)));
Benjamin Kramer1a50a122015-02-17 15:29:18 +0000115 Output.append(Values.begin() + 1, Values.end());
Michael J. Spencer96a564f2012-12-05 00:29:32 +0000116 break;
117
118 case Option::RenderSeparateStyle:
119 Output.push_back(Args.MakeArgString(getSpelling()));
Benjamin Kramer1a50a122015-02-17 15:29:18 +0000120 Output.append(Values.begin(), Values.end());
Michael J. Spencer96a564f2012-12-05 00:29:32 +0000121 break;
122 }
123}