blob: 7653daf7ee17f756701031b652b5748fecaf945c [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
Daniel Dunbarfb88ce02009-03-22 23:26:43 +000016ArgList::ArgList(const char **ArgBegin, const char **ArgEnd)
17 : NumInputArgStrings(ArgEnd - ArgBegin)
18{
Daniel Dunbard77aac92009-03-04 17:10:42 +000019 ArgStrings.append(ArgBegin, ArgEnd);
20}
21
22ArgList::~ArgList() {
Daniel Dunbar4b84bf92009-03-17 20:44:29 +000023 for (iterator it = begin(), ie = end(); it != ie; ++it)
Daniel Dunbard77aac92009-03-04 17:10:42 +000024 delete *it;
25}
Daniel Dunbar12d474a2009-03-04 22:40:08 +000026
27void ArgList::append(Arg *A) {
28 if (A->getOption().isUnsupported()) {
29 assert(0 && "FIXME: unsupported unsupported.");
30 }
31
32 Args.push_back(A);
33}
Daniel Dunbarf01869c2009-03-12 01:36:44 +000034
Daniel Dunbare9c70fa2009-03-15 00:48:16 +000035Arg *ArgList::getLastArg(options::ID Id, bool Claim) const {
Daniel Dunbarf01869c2009-03-12 01:36:44 +000036 // FIXME: Make search efficient?
37
38 // FIXME: This needs to not require loading of the option.
Daniel Dunbare9c70fa2009-03-15 00:48:16 +000039 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
40 if ((*it)->getOption().matches(Id)) {
41 if (Claim) (*it)->claim();
Daniel Dunbar08a0ae42009-03-12 16:03:38 +000042 return *it;
Daniel Dunbare9c70fa2009-03-15 00:48:16 +000043 }
44 }
Daniel Dunbarf01869c2009-03-12 01:36:44 +000045
Daniel Dunbar08a0ae42009-03-12 16:03:38 +000046 return 0;
Daniel Dunbarf01869c2009-03-12 01:36:44 +000047}
Daniel Dunbar72cac202009-03-12 18:20:18 +000048
Daniel Dunbar35f55a22009-03-17 18:51:42 +000049Arg *ArgList::getLastArg(options::ID Id0, options::ID Id1, bool Claim) const {
50 Arg *Res, *A0 = getLastArg(Id0, false), *A1 = getLastArg(Id1, false);
51
52 if (A0 && A1)
53 Res = A0->getIndex() > A1->getIndex() ? A0 : A1;
54 else
55 Res = A0 ? A0 : A1;
56
57 if (Claim && Res)
58 Res->claim();
59
60 return Res;
61}
62
Daniel Dunbar3a4dcdf2009-03-18 09:29:36 +000063bool ArgList::hasFlag(options::ID Pos, options::ID Neg, bool Default) const {
64 Arg *PosA = getLastArg(Pos);
65 Arg *NegA = getLastArg(Pos);
66 if (PosA && NegA)
67 return NegA->getIndex() < PosA->getIndex();
68 if (PosA) return true;
69 if (NegA) return false;
70 return Default;
71}
72
Daniel Dunbar019b2412009-03-17 17:51:18 +000073unsigned ArgList::MakeIndex(const char *String0) const {
Daniel Dunbar72cac202009-03-12 18:20:18 +000074 unsigned Index = ArgStrings.size();
75
76 // Tuck away so we have a reliable const char *.
77 SynthesizedStrings.push_back(String0);
78 ArgStrings.push_back(SynthesizedStrings.back().c_str());
79
80 return Index;
81}
82
Daniel Dunbar019b2412009-03-17 17:51:18 +000083unsigned ArgList::MakeIndex(const char *String0, const char *String1) const {
Daniel Dunbar72cac202009-03-12 18:20:18 +000084 unsigned Index0 = MakeIndex(String0);
85 unsigned Index1 = MakeIndex(String1);
Daniel Dunbar44925cc2009-03-13 17:25:24 +000086 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
Daniel Dunbar72cac202009-03-12 18:20:18 +000087 (void) Index1;
88 return Index0;
89}
90
Daniel Dunbar019b2412009-03-17 17:51:18 +000091const char *ArgList::MakeArgString(const char *Str) const {
92 return getArgString(MakeIndex(Str));
93}
94
95Arg *ArgList::MakeFlagArg(const Option *Opt) const {
Daniel Dunbar72cac202009-03-12 18:20:18 +000096 return new FlagArg(Opt, MakeIndex(Opt->getName()));
97}
98
Daniel Dunbar019b2412009-03-17 17:51:18 +000099Arg *ArgList::MakePositionalArg(const Option *Opt, const char *Value) const {
Daniel Dunbar72cac202009-03-12 18:20:18 +0000100 return new PositionalArg(Opt, MakeIndex(Value));
101}
102
Daniel Dunbar019b2412009-03-17 17:51:18 +0000103Arg *ArgList::MakeSeparateArg(const Option *Opt, const char *Value) const {
Daniel Dunbar72cac202009-03-12 18:20:18 +0000104 return new SeparateArg(Opt, MakeIndex(Opt->getName(), Value), 1);
105}
106
Daniel Dunbar019b2412009-03-17 17:51:18 +0000107Arg *ArgList::MakeJoinedArg(const Option *Opt, const char *Value) const {
Daniel Dunbar72cac202009-03-12 18:20:18 +0000108 std::string Joined(Opt->getName());
109 Joined += Value;
110 return new JoinedArg(Opt, MakeIndex(Joined.c_str()));
111}
Daniel Dunbar3a4dcdf2009-03-18 09:29:36 +0000112
113void ArgList::AddLastArg(ArgStringList &Output, options::ID Id) const {
114 if (Arg *A = getLastArg(Id)) {
115 A->claim();
116 A->render(*this, Output);
117 }
118}
119
120void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0) const {
121 // FIXME: Make fast.
122 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
123 const Arg *A = *it;
124 if (A->getOption().matches(Id0)) {
125 A->claim();
126 A->render(*this, Output);
127 }
128 }
129}
130
131void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
132 options::ID Id1) const {
133 // FIXME: Make fast.
134 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
135 const Arg *A = *it;
136 if (A->getOption().matches(Id0) || A->getOption().matches(Id1)) {
137 A->claim();
138 A->render(*this, Output);
139 }
140 }
141}
142
143void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
144 options::ID Id1, options::ID Id2) const {
145 // FIXME: Make fast.
146 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
147 const Arg *A = *it;
148 if (A->getOption().matches(Id0) || A->getOption().matches(Id1) ||
149 A->getOption().matches(Id2)) {
150 A->claim();
151 A->render(*this, Output);
152 }
153 }
154}
155
156void ArgList::AddAllArgValues(ArgStringList &Output, options::ID Id0) const {
157 // FIXME: Make fast.
158 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
159 const Arg *A = *it;
160 if (A->getOption().matches(Id0)) {
161 A->claim();
162 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
163 Output.push_back(A->getValue(*this, i));
164 }
165 }
166}
Daniel Dunbareefd9142009-03-20 15:59:01 +0000167
168void ArgList::AddAllArgValues(ArgStringList &Output, options::ID Id0,
169 options::ID Id1) const {
170 // FIXME: Make fast.
171 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
172 const Arg *A = *it;
173 if (A->getOption().matches(Id0) || A->getOption().matches(Id1)) {
174 A->claim();
175 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
176 Output.push_back(A->getValue(*this, i));
177 }
178 }
179}