Nick Lewycky | 3fdcc6f | 2010-12-31 17:31:54 +0000 | [diff] [blame] | 1 | //===--- Arg.cpp - Argument Implementations -------------------------------===// |
Daniel Dunbar | fb36cae | 2009-03-04 22:37:52 +0000 | [diff] [blame] | 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 "clang/Driver/Arg.h" |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 11 | #include "clang/Driver/ArgList.h" |
Daniel Dunbar | f781c41 | 2009-03-04 23:02:50 +0000 | [diff] [blame] | 12 | #include "clang/Driver/Option.h" |
Daniel Dunbar | e375c4a | 2010-06-09 22:31:04 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallString.h" |
Daniel Dunbar | bbd2a04 | 2010-03-20 01:12:00 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | f781c41 | 2009-03-04 23:02:50 +0000 | [diff] [blame] | 15 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | fb36cae | 2009-03-04 22:37:52 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace clang::driver; |
| 18 | |
Michael J. Spencer | e4151c5 | 2012-10-19 22:36:40 +0000 | [diff] [blame^] | 19 | Arg::Arg(const Option _Opt, unsigned _Index, const Arg *_BaseArg) |
Daniel Dunbar | 532c1ec | 2010-06-09 22:31:08 +0000 | [diff] [blame] | 20 | : Opt(_Opt), BaseArg(_BaseArg), Index(_Index), |
Daniel Dunbar | 4465a77 | 2010-06-09 22:31:00 +0000 | [diff] [blame] | 21 | Claimed(false), OwnsValues(false) { |
Daniel Dunbar | fb36cae | 2009-03-04 22:37:52 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Michael J. Spencer | e4151c5 | 2012-10-19 22:36:40 +0000 | [diff] [blame^] | 24 | Arg::Arg(const Option _Opt, unsigned _Index, |
Daniel Dunbar | 532c1ec | 2010-06-09 22:31:08 +0000 | [diff] [blame] | 25 | 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. Spencer | e4151c5 | 2012-10-19 22:36:40 +0000 | [diff] [blame^] | 31 | Arg::Arg(const Option _Opt, unsigned _Index, |
Daniel Dunbar | 532c1ec | 2010-06-09 22:31:08 +0000 | [diff] [blame] | 32 | 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 Dunbar | 4465a77 | 2010-06-09 22:31:00 +0000 | [diff] [blame] | 39 | Arg::~Arg() { |
| 40 | if (OwnsValues) { |
| 41 | for (unsigned i = 0, e = Values.size(); i != e; ++i) |
| 42 | delete[] Values[i]; |
| 43 | } |
| 44 | } |
Daniel Dunbar | fb36cae | 2009-03-04 22:37:52 +0000 | [diff] [blame] | 45 | |
Daniel Dunbar | f781c41 | 2009-03-04 23:02:50 +0000 | [diff] [blame] | 46 | void Arg::dump() const { |
| 47 | llvm::errs() << "<"; |
Daniel Dunbar | f781c41 | 2009-03-04 23:02:50 +0000 | [diff] [blame] | 48 | |
| 49 | llvm::errs() << " Opt:"; |
Michael J. Spencer | e4151c5 | 2012-10-19 22:36:40 +0000 | [diff] [blame^] | 50 | Opt.dump(); |
Daniel Dunbar | f781c41 | 2009-03-04 23:02:50 +0000 | [diff] [blame] | 51 | |
| 52 | llvm::errs() << " Index:" << Index; |
| 53 | |
Daniel Dunbar | 532c1ec | 2010-06-09 22:31:08 +0000 | [diff] [blame] | 54 | 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 Dunbar | f781c41 | 2009-03-04 23:02:50 +0000 | [diff] [blame] | 59 | |
Daniel Dunbar | 532c1ec | 2010-06-09 22:31:08 +0000 | [diff] [blame] | 60 | llvm::errs() << "]>\n"; |
Daniel Dunbar | f781c41 | 2009-03-04 23:02:50 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Daniel Dunbar | 38dd3d5 | 2009-03-20 06:14:23 +0000 | [diff] [blame] | 63 | std::string Arg::getAsString(const ArgList &Args) const { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 64 | SmallString<256> Res; |
Daniel Dunbar | e375c4a | 2010-06-09 22:31:04 +0000 | [diff] [blame] | 65 | llvm::raw_svector_ostream OS(Res); |
Daniel Dunbar | 38dd3d5 | 2009-03-20 06:14:23 +0000 | [diff] [blame] | 66 | |
| 67 | ArgStringList ASL; |
| 68 | render(Args, ASL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | for (ArgStringList::iterator |
Daniel Dunbar | 38dd3d5 | 2009-03-20 06:14:23 +0000 | [diff] [blame] | 70 | 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 Dunbar | 42f7fc2 | 2009-03-19 07:22:40 +0000 | [diff] [blame] | 79 | void 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 Dunbar | e375c4a | 2010-06-09 22:31:04 +0000 | [diff] [blame] | 89 | void 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 Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 97 | SmallString<256> Res; |
Daniel Dunbar | e375c4a | 2010-06-09 22:31:04 +0000 | [diff] [blame] | 98 | 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. Spencer | e4151c5 | 2012-10-19 22:36:40 +0000 | [diff] [blame^] | 107 | |
Daniel Dunbar | e375c4a | 2010-06-09 22:31:04 +0000 | [diff] [blame] | 108 | 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 Gregor | bcf6a80 | 2011-07-05 16:56:25 +0000 | [diff] [blame] | 116 | Output.push_back(getOption().getName().data()); |
Daniel Dunbar | e375c4a | 2010-06-09 22:31:04 +0000 | [diff] [blame] | 117 | for (unsigned i = 0, e = getNumValues(); i != e; ++i) |
| 118 | Output.push_back(getValue(Args, i)); |
| 119 | break; |
| 120 | } |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 121 | } |