blob: ee201f28bd145a821f2c3b2756a42ab6b8563fcc [file] [log] [blame]
Daniel Dunbard77aac92009-03-04 17:10:42 +00001//===--- ArgList.cpp - Argument List Management -------------------------*-===//
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/ArgList.h"
11#include "clang/Driver/Arg.h"
Daniel Dunbar12d474a2009-03-04 22:40:08 +000012#include "clang/Driver/Option.h"
Daniel Dunbard77aac92009-03-04 17:10:42 +000013
14using namespace clang::driver;
15
16ArgList::ArgList(const char **ArgBegin, const char **ArgEnd) {
17 ArgStrings.append(ArgBegin, ArgEnd);
18}
19
20ArgList::~ArgList() {
Daniel Dunbar4b84bf92009-03-17 20:44:29 +000021 for (iterator it = begin(), ie = end(); it != ie; ++it)
Daniel Dunbard77aac92009-03-04 17:10:42 +000022 delete *it;
23}
Daniel Dunbar12d474a2009-03-04 22:40:08 +000024
25void ArgList::append(Arg *A) {
26 if (A->getOption().isUnsupported()) {
27 assert(0 && "FIXME: unsupported unsupported.");
28 }
29
30 Args.push_back(A);
31}
Daniel Dunbarf01869c2009-03-12 01:36:44 +000032
Daniel Dunbare9c70fa2009-03-15 00:48:16 +000033Arg *ArgList::getLastArg(options::ID Id, bool Claim) const {
Daniel Dunbarf01869c2009-03-12 01:36:44 +000034 // FIXME: Make search efficient?
35
36 // FIXME: This needs to not require loading of the option.
Daniel Dunbare9c70fa2009-03-15 00:48:16 +000037 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
38 if ((*it)->getOption().matches(Id)) {
39 if (Claim) (*it)->claim();
Daniel Dunbar08a0ae42009-03-12 16:03:38 +000040 return *it;
Daniel Dunbare9c70fa2009-03-15 00:48:16 +000041 }
42 }
Daniel Dunbarf01869c2009-03-12 01:36:44 +000043
Daniel Dunbar08a0ae42009-03-12 16:03:38 +000044 return 0;
Daniel Dunbarf01869c2009-03-12 01:36:44 +000045}
Daniel Dunbar72cac202009-03-12 18:20:18 +000046
Daniel Dunbar35f55a22009-03-17 18:51:42 +000047Arg *ArgList::getLastArg(options::ID Id0, options::ID Id1, bool Claim) const {
48 Arg *Res, *A0 = getLastArg(Id0, false), *A1 = getLastArg(Id1, false);
49
50 if (A0 && A1)
51 Res = A0->getIndex() > A1->getIndex() ? A0 : A1;
52 else
53 Res = A0 ? A0 : A1;
54
55 if (Claim && Res)
56 Res->claim();
57
58 return Res;
59}
60
Daniel Dunbar3a4dcdf2009-03-18 09:29:36 +000061bool ArgList::hasFlag(options::ID Pos, options::ID Neg, bool Default) const {
62 Arg *PosA = getLastArg(Pos);
63 Arg *NegA = getLastArg(Pos);
64 if (PosA && NegA)
65 return NegA->getIndex() < PosA->getIndex();
66 if (PosA) return true;
67 if (NegA) return false;
68 return Default;
69}
70
Daniel Dunbar019b2412009-03-17 17:51:18 +000071unsigned ArgList::MakeIndex(const char *String0) const {
Daniel Dunbar72cac202009-03-12 18:20:18 +000072 unsigned Index = ArgStrings.size();
73
74 // Tuck away so we have a reliable const char *.
75 SynthesizedStrings.push_back(String0);
76 ArgStrings.push_back(SynthesizedStrings.back().c_str());
77
78 return Index;
79}
80
Daniel Dunbar019b2412009-03-17 17:51:18 +000081unsigned ArgList::MakeIndex(const char *String0, const char *String1) const {
Daniel Dunbar72cac202009-03-12 18:20:18 +000082 unsigned Index0 = MakeIndex(String0);
83 unsigned Index1 = MakeIndex(String1);
Daniel Dunbar44925cc2009-03-13 17:25:24 +000084 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
Daniel Dunbar72cac202009-03-12 18:20:18 +000085 (void) Index1;
86 return Index0;
87}
88
Daniel Dunbar019b2412009-03-17 17:51:18 +000089const char *ArgList::MakeArgString(const char *Str) const {
90 return getArgString(MakeIndex(Str));
91}
92
93Arg *ArgList::MakeFlagArg(const Option *Opt) const {
Daniel Dunbar72cac202009-03-12 18:20:18 +000094 return new FlagArg(Opt, MakeIndex(Opt->getName()));
95}
96
Daniel Dunbar019b2412009-03-17 17:51:18 +000097Arg *ArgList::MakePositionalArg(const Option *Opt, const char *Value) const {
Daniel Dunbar72cac202009-03-12 18:20:18 +000098 return new PositionalArg(Opt, MakeIndex(Value));
99}
100
Daniel Dunbar019b2412009-03-17 17:51:18 +0000101Arg *ArgList::MakeSeparateArg(const Option *Opt, const char *Value) const {
Daniel Dunbar72cac202009-03-12 18:20:18 +0000102 return new SeparateArg(Opt, MakeIndex(Opt->getName(), Value), 1);
103}
104
Daniel Dunbar019b2412009-03-17 17:51:18 +0000105Arg *ArgList::MakeJoinedArg(const Option *Opt, const char *Value) const {
Daniel Dunbar72cac202009-03-12 18:20:18 +0000106 std::string Joined(Opt->getName());
107 Joined += Value;
108 return new JoinedArg(Opt, MakeIndex(Joined.c_str()));
109}
Daniel Dunbar3a4dcdf2009-03-18 09:29:36 +0000110
111void ArgList::AddLastArg(ArgStringList &Output, options::ID Id) const {
112 if (Arg *A = getLastArg(Id)) {
113 A->claim();
114 A->render(*this, Output);
115 }
116}
117
118void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0) const {
119 // FIXME: Make fast.
120 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
121 const Arg *A = *it;
122 if (A->getOption().matches(Id0)) {
123 A->claim();
124 A->render(*this, Output);
125 }
126 }
127}
128
129void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
130 options::ID Id1) const {
131 // FIXME: Make fast.
132 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
133 const Arg *A = *it;
134 if (A->getOption().matches(Id0) || A->getOption().matches(Id1)) {
135 A->claim();
136 A->render(*this, Output);
137 }
138 }
139}
140
141void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
142 options::ID Id1, options::ID Id2) const {
143 // FIXME: Make fast.
144 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
145 const Arg *A = *it;
146 if (A->getOption().matches(Id0) || A->getOption().matches(Id1) ||
147 A->getOption().matches(Id2)) {
148 A->claim();
149 A->render(*this, Output);
150 }
151 }
152}
153
154void ArgList::AddAllArgValues(ArgStringList &Output, options::ID Id0) const {
155 // FIXME: Make fast.
156 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
157 const Arg *A = *it;
158 if (A->getOption().matches(Id0)) {
159 A->claim();
160 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
161 Output.push_back(A->getValue(*this, i));
162 }
163 }
164}