blob: b156d7cf714614ce28e34a6f39eddd4bb2514a86 [file] [log] [blame]
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001//===--- Arg.cpp - Argument Implementations -------------------------------===//
Daniel Dunbarfb36cae2009-03-04 22:37:52 +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
10#include "clang/Driver/Arg.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000011#include "clang/Driver/ArgList.h"
Daniel Dunbarf781c412009-03-04 23:02:50 +000012#include "clang/Driver/Option.h"
Daniel Dunbare375c4a2010-06-09 22:31:04 +000013#include "llvm/ADT/SmallString.h"
Daniel Dunbarbbd2a042010-03-20 01:12:00 +000014#include "llvm/ADT/Twine.h"
Daniel Dunbarf781c412009-03-04 23:02:50 +000015#include "llvm/Support/raw_ostream.h"
Daniel Dunbarfb36cae2009-03-04 22:37:52 +000016
17using namespace clang::driver;
18
Michael J. Spencere4151c52012-10-19 22:36:40 +000019Arg::Arg(const Option _Opt, unsigned _Index, const Arg *_BaseArg)
Daniel Dunbar532c1ec2010-06-09 22:31:08 +000020 : Opt(_Opt), BaseArg(_BaseArg), Index(_Index),
Daniel Dunbar4465a772010-06-09 22:31:00 +000021 Claimed(false), OwnsValues(false) {
Daniel Dunbarfb36cae2009-03-04 22:37:52 +000022}
23
Michael J. Spencere4151c52012-10-19 22:36:40 +000024Arg::Arg(const Option _Opt, unsigned _Index,
Daniel Dunbar532c1ec2010-06-09 22:31:08 +000025 const char *Value0, const Arg *_BaseArg)
26 : Opt(_Opt), BaseArg(_BaseArg), Index(_Index),
27 Claimed(false), OwnsValues(false) {
28 Values.push_back(Value0);
29}
30
Michael J. Spencere4151c52012-10-19 22:36:40 +000031Arg::Arg(const Option _Opt, unsigned _Index,
Daniel Dunbar532c1ec2010-06-09 22:31:08 +000032 const char *Value0, const char *Value1, const Arg *_BaseArg)
33 : Opt(_Opt), BaseArg(_BaseArg), Index(_Index),
34 Claimed(false), OwnsValues(false) {
35 Values.push_back(Value0);
36 Values.push_back(Value1);
37}
38
Daniel Dunbar4465a772010-06-09 22:31:00 +000039Arg::~Arg() {
40 if (OwnsValues) {
41 for (unsigned i = 0, e = Values.size(); i != e; ++i)
42 delete[] Values[i];
43 }
44}
Daniel Dunbarfb36cae2009-03-04 22:37:52 +000045
Daniel Dunbarf781c412009-03-04 23:02:50 +000046void Arg::dump() const {
47 llvm::errs() << "<";
Daniel Dunbarf781c412009-03-04 23:02:50 +000048
49 llvm::errs() << " Opt:";
Michael J. Spencere4151c52012-10-19 22:36:40 +000050 Opt.dump();
Daniel Dunbarf781c412009-03-04 23:02:50 +000051
52 llvm::errs() << " Index:" << Index;
53
Daniel Dunbar532c1ec2010-06-09 22:31:08 +000054 llvm::errs() << " Values: [";
55 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
56 if (i) llvm::errs() << ", ";
57 llvm::errs() << "'" << Values[i] << "'";
58 }
Daniel Dunbarf781c412009-03-04 23:02:50 +000059
Daniel Dunbar532c1ec2010-06-09 22:31:08 +000060 llvm::errs() << "]>\n";
Daniel Dunbarf781c412009-03-04 23:02:50 +000061}
62
Daniel Dunbar38dd3d52009-03-20 06:14:23 +000063std::string Arg::getAsString(const ArgList &Args) const {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000064 SmallString<256> Res;
Daniel Dunbare375c4a2010-06-09 22:31:04 +000065 llvm::raw_svector_ostream OS(Res);
Daniel Dunbar38dd3d52009-03-20 06:14:23 +000066
67 ArgStringList ASL;
68 render(Args, ASL);
Mike Stump1eb44332009-09-09 15:08:12 +000069 for (ArgStringList::iterator
Daniel Dunbar38dd3d52009-03-20 06:14:23 +000070 it = ASL.begin(), ie = ASL.end(); it != ie; ++it) {
71 if (it != ASL.begin())
72 OS << ' ';
73 OS << *it;
74 }
75
76 return OS.str();
77}
78
Daniel Dunbar42f7fc22009-03-19 07:22:40 +000079void Arg::renderAsInput(const ArgList &Args, ArgStringList &Output) const {
80 if (!getOption().hasNoOptAsInput()) {
81 render(Args, Output);
82 return;
83 }
84
85 for (unsigned i = 0, e = getNumValues(); i != e; ++i)
86 Output.push_back(getValue(Args, i));
87}
88
Daniel Dunbare375c4a2010-06-09 22:31:04 +000089void Arg::render(const ArgList &Args, ArgStringList &Output) const {
90 switch (getOption().getRenderStyle()) {
91 case Option::RenderValuesStyle:
92 for (unsigned i = 0, e = getNumValues(); i != e; ++i)
93 Output.push_back(getValue(Args, i));
94 break;
95
96 case Option::RenderCommaJoinedStyle: {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000097 SmallString<256> Res;
Daniel Dunbare375c4a2010-06-09 22:31:04 +000098 llvm::raw_svector_ostream OS(Res);
99 OS << getOption().getName();
100 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
101 if (i) OS << ',';
102 OS << getValue(Args, i);
103 }
104 Output.push_back(Args.MakeArgString(OS.str()));
105 break;
106 }
Michael J. Spencere4151c52012-10-19 22:36:40 +0000107
Daniel Dunbare375c4a2010-06-09 22:31:04 +0000108 case Option::RenderJoinedStyle:
109 Output.push_back(Args.GetOrMakeJoinedArgString(
110 getIndex(), getOption().getName(), getValue(Args, 0)));
111 for (unsigned i = 1, e = getNumValues(); i != e; ++i)
112 Output.push_back(getValue(Args, i));
113 break;
114
115 case Option::RenderSeparateStyle:
Douglas Gregorbcf6a802011-07-05 16:56:25 +0000116 Output.push_back(getOption().getName().data());
Daniel Dunbare375c4a2010-06-09 22:31:04 +0000117 for (unsigned i = 0, e = getNumValues(); i != e; ++i)
118 Output.push_back(getValue(Args, i));
119 break;
120 }
Daniel Dunbar06482622009-03-05 06:38:47 +0000121}