blob: 2da32bfacf306633799744cc2d1591ca74d52094 [file] [log] [blame]
Eugene Zelenkoaf615892017-06-16 00:43:26 +00001//===- Arg.cpp - Argument Implementations ---------------------------------===//
Michael J. Spencer41ee0412012-12-05 00:29:32 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Michael J. Spencer41ee0412012-12-05 00:29:32 +00006//
7//===----------------------------------------------------------------------===//
8
Michael J. Spencer41ee0412012-12-05 00:29:32 +00009#include "llvm/ADT/SmallString.h"
Nico Weber432a3882018-04-30 14:59:11 +000010#include "llvm/Config/llvm-config.h"
Eugene Zelenkoaf615892017-06-16 00:43:26 +000011#include "llvm/Option/Arg.h"
Michael J. Spencer41ee0412012-12-05 00:29:32 +000012#include "llvm/Option/ArgList.h"
13#include "llvm/Option/Option.h"
Eugene Zelenkoaf615892017-06-16 00:43:26 +000014#include "llvm/Support/Compiler.h"
Eric Christopher9a8b5e72015-12-18 18:55:26 +000015#include "llvm/Support/Debug.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/Support/raw_ostream.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
Aaron Ballman615eb472017-10-15 14:32:27 +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 {
Nico Webere3f06b42019-07-09 00:34:08 +000069 if (Alias)
70 return Alias->getAsString(Args);
71
Alp Tokere69170a2014-06-26 22:52:05 +000072 SmallString<256> Res;
Eugene Zelenkoaf615892017-06-16 00:43:26 +000073 raw_svector_ostream OS(Res);
Michael J. Spencer41ee0412012-12-05 00:29:32 +000074
75 ArgStringList ASL;
76 render(Args, ASL);
77 for (ArgStringList::iterator
78 it = ASL.begin(), ie = ASL.end(); it != ie; ++it) {
79 if (it != ASL.begin())
80 OS << ' ';
81 OS << *it;
82 }
83
Benjamin Krameradcd0262020-01-28 20:23:46 +010084 return std::string(OS.str());
Michael J. Spencer41ee0412012-12-05 00:29:32 +000085}
86
87void Arg::renderAsInput(const ArgList &Args, ArgStringList &Output) const {
88 if (!getOption().hasNoOptAsInput()) {
89 render(Args, Output);
90 return;
91 }
92
Benjamin Kramer6cd780f2015-02-17 15:29:18 +000093 Output.append(Values.begin(), Values.end());
Michael J. Spencer41ee0412012-12-05 00:29:32 +000094}
95
96void Arg::render(const ArgList &Args, ArgStringList &Output) const {
97 switch (getOption().getRenderStyle()) {
98 case Option::RenderValuesStyle:
Benjamin Kramer6cd780f2015-02-17 15:29:18 +000099 Output.append(Values.begin(), Values.end());
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000100 break;
101
102 case Option::RenderCommaJoinedStyle: {
Alp Tokere69170a2014-06-26 22:52:05 +0000103 SmallString<256> Res;
Eugene Zelenkoaf615892017-06-16 00:43:26 +0000104 raw_svector_ostream OS(Res);
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000105 OS << getSpelling();
106 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
107 if (i) OS << ',';
108 OS << getValue(i);
109 }
110 Output.push_back(Args.MakeArgString(OS.str()));
111 break;
112 }
113
114 case Option::RenderJoinedStyle:
115 Output.push_back(Args.GetOrMakeJoinedArgString(
116 getIndex(), getSpelling(), getValue(0)));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000117 Output.append(Values.begin() + 1, Values.end());
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000118 break;
119
120 case Option::RenderSeparateStyle:
121 Output.push_back(Args.MakeArgString(getSpelling()));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000122 Output.append(Values.begin(), Values.end());
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000123 break;
124 }
125}