2011-01-01 01:31:54 +08:00
|
|
|
//===--- ArgList.cpp - Argument List Management ---------------------------===//
|
2009-03-05 01:10:42 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Driver/ArgList.h"
|
|
|
|
#include "clang/Driver/Arg.h"
|
2010-05-21 00:54:55 +08:00
|
|
|
#include "clang/Driver/DriverDiagnostic.h"
|
2009-03-05 06:40:08 +08:00
|
|
|
#include "clang/Driver/Option.h"
|
2009-03-05 01:10:42 +08:00
|
|
|
|
2009-09-10 06:32:26 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
2010-05-22 03:58:44 +08:00
|
|
|
using namespace clang;
|
2009-03-05 01:10:42 +08:00
|
|
|
using namespace clang::driver;
|
|
|
|
|
2009-11-25 19:33:30 +08:00
|
|
|
void arg_iterator::SkipToNextArg() {
|
|
|
|
for (; Current != Args.end(); ++Current) {
|
|
|
|
// Done if there are no filters.
|
|
|
|
if (!Id0.isValid())
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Otherwise require a match.
|
|
|
|
const Option &O = (*Current)->getOption();
|
|
|
|
if (O.matches(Id0) ||
|
|
|
|
(Id1.isValid() && O.matches(Id1)) ||
|
|
|
|
(Id2.isValid() && O.matches(Id2)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2010-06-12 06:00:22 +08:00
|
|
|
ArgList::ArgList() {
|
2009-03-05 01:10:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ArgList::~ArgList() {
|
|
|
|
}
|
2009-03-05 06:40:08 +08:00
|
|
|
|
|
|
|
void ArgList::append(Arg *A) {
|
|
|
|
Args.push_back(A);
|
|
|
|
}
|
2009-03-12 09:36:44 +08:00
|
|
|
|
2011-08-03 01:58:04 +08:00
|
|
|
void ArgList::eraseArg(OptSpecifier Id) {
|
2011-08-09 01:17:15 +08:00
|
|
|
for (iterator it = begin(), ie = end(); it != ie; ) {
|
2011-08-03 01:58:04 +08:00
|
|
|
if ((*it)->getOption().matches(Id)) {
|
2011-08-09 01:17:15 +08:00
|
|
|
it = Args.erase(it);
|
2011-08-13 07:38:19 +08:00
|
|
|
ie = end();
|
2011-08-18 07:08:45 +08:00
|
|
|
} else {
|
2011-08-09 01:17:15 +08:00
|
|
|
++it;
|
2011-08-18 07:08:45 +08:00
|
|
|
}
|
2011-08-03 01:58:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-19 12:14:53 +08:00
|
|
|
Arg *ArgList::getLastArgNoClaim(OptSpecifier Id) const {
|
2009-03-12 09:36:44 +08:00
|
|
|
// FIXME: Make search efficient?
|
2009-11-19 12:00:53 +08:00
|
|
|
for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
|
|
|
|
if ((*it)->getOption().matches(Id))
|
2009-03-13 00:03:38 +08:00
|
|
|
return *it;
|
|
|
|
return 0;
|
2009-03-12 09:36:44 +08:00
|
|
|
}
|
2009-03-13 02:20:18 +08:00
|
|
|
|
2009-11-19 12:14:53 +08:00
|
|
|
Arg *ArgList::getLastArg(OptSpecifier Id) const {
|
2010-12-21 06:45:09 +08:00
|
|
|
Arg *Res = 0;
|
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
|
|
|
if ((*it)->getOption().matches(Id)) {
|
|
|
|
Res = *it;
|
|
|
|
Res->claim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
2009-11-19 12:00:53 +08:00
|
|
|
}
|
|
|
|
|
2009-11-19 12:14:53 +08:00
|
|
|
Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const {
|
2010-06-15 04:20:44 +08:00
|
|
|
Arg *Res = 0;
|
2010-12-21 06:45:09 +08:00
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
2010-06-15 04:20:44 +08:00
|
|
|
if ((*it)->getOption().matches(Id0) ||
|
|
|
|
(*it)->getOption().matches(Id1)) {
|
|
|
|
Res = *it;
|
2010-12-21 06:45:09 +08:00
|
|
|
Res->claim();
|
|
|
|
|
2010-06-15 04:20:44 +08:00
|
|
|
}
|
|
|
|
}
|
2009-03-18 02:51:42 +08:00
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2009-11-19 12:14:53 +08:00
|
|
|
Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
|
|
|
|
OptSpecifier Id2) const {
|
2009-06-28 15:36:13 +08:00
|
|
|
Arg *Res = 0;
|
2010-12-21 06:45:09 +08:00
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
2010-06-15 04:20:44 +08:00
|
|
|
if ((*it)->getOption().matches(Id0) ||
|
|
|
|
(*it)->getOption().matches(Id1) ||
|
|
|
|
(*it)->getOption().matches(Id2)) {
|
|
|
|
Res = *it;
|
2010-12-21 06:45:09 +08:00
|
|
|
Res->claim();
|
2010-06-15 04:20:44 +08:00
|
|
|
}
|
2009-06-28 15:36:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2010-07-14 07:31:40 +08:00
|
|
|
Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
|
|
|
|
OptSpecifier Id2, OptSpecifier Id3) const {
|
|
|
|
Arg *Res = 0;
|
2010-12-21 06:45:09 +08:00
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
2010-07-14 07:31:40 +08:00
|
|
|
if ((*it)->getOption().matches(Id0) ||
|
|
|
|
(*it)->getOption().matches(Id1) ||
|
|
|
|
(*it)->getOption().matches(Id2) ||
|
|
|
|
(*it)->getOption().matches(Id3)) {
|
|
|
|
Res = *it;
|
2010-12-21 06:45:09 +08:00
|
|
|
Res->claim();
|
2010-07-14 07:31:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2009-11-19 12:14:53 +08:00
|
|
|
bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
|
2009-04-08 05:08:57 +08:00
|
|
|
if (Arg *A = getLastArg(Pos, Neg))
|
|
|
|
return A->getOption().matches(Pos);
|
2009-03-18 17:29:36 +08:00
|
|
|
return Default;
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef ArgList::getLastArgValue(OptSpecifier Id,
|
|
|
|
StringRef Default) const {
|
2010-05-21 00:54:55 +08:00
|
|
|
if (Arg *A = getLastArg(Id))
|
|
|
|
return A->getValue(*this);
|
|
|
|
return Default;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ArgList::getLastArgIntValue(OptSpecifier Id, int Default,
|
2011-09-26 07:23:43 +08:00
|
|
|
clang::DiagnosticsEngine &Diags) const {
|
2010-05-21 00:54:55 +08:00
|
|
|
int Res = Default;
|
|
|
|
|
|
|
|
if (Arg *A = getLastArg(Id)) {
|
2011-07-23 18:55:15 +08:00
|
|
|
if (StringRef(A->getValue(*this)).getAsInteger(10, Res))
|
2010-05-21 00:54:55 +08:00
|
|
|
Diags.Report(diag::err_drv_invalid_int_value)
|
|
|
|
<< A->getAsString(*this) << A->getValue(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<const char *, 16> Values;
|
2010-05-21 00:54:55 +08:00
|
|
|
AddAllArgValues(Values, Id);
|
|
|
|
return std::vector<std::string>(Values.begin(), Values.end());
|
|
|
|
}
|
|
|
|
|
2009-11-19 12:14:53 +08:00
|
|
|
void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
|
2009-03-18 17:29:36 +08:00
|
|
|
if (Arg *A = getLastArg(Id)) {
|
|
|
|
A->claim();
|
|
|
|
A->render(*this, Output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-19 12:14:53 +08:00
|
|
|
void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
|
|
|
|
OptSpecifier Id1, OptSpecifier Id2) const {
|
2009-11-25 19:33:30 +08:00
|
|
|
for (arg_iterator it = filtered_begin(Id0, Id1, Id2),
|
|
|
|
ie = filtered_end(); it != ie; ++it) {
|
2010-06-12 06:00:13 +08:00
|
|
|
(*it)->claim();
|
|
|
|
(*it)->render(*this, Output);
|
2009-03-18 17:29:36 +08:00
|
|
|
}
|
|
|
|
}
|
2009-03-20 23:59:01 +08:00
|
|
|
|
2009-11-19 12:14:53 +08:00
|
|
|
void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
|
2009-11-25 19:33:30 +08:00
|
|
|
OptSpecifier Id1, OptSpecifier Id2) const {
|
|
|
|
for (arg_iterator it = filtered_begin(Id0, Id1, Id2),
|
|
|
|
ie = filtered_end(); it != ie; ++it) {
|
2010-06-12 06:00:13 +08:00
|
|
|
(*it)->claim();
|
|
|
|
for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
|
|
|
|
Output.push_back((*it)->getValue(*this, i));
|
2009-03-20 23:59:01 +08:00
|
|
|
}
|
|
|
|
}
|
2009-03-25 12:13:45 +08:00
|
|
|
|
2009-11-19 12:14:53 +08:00
|
|
|
void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
|
2009-04-26 09:07:52 +08:00
|
|
|
const char *Translation,
|
|
|
|
bool Joined) const {
|
2009-11-25 19:33:30 +08:00
|
|
|
for (arg_iterator it = filtered_begin(Id0),
|
|
|
|
ie = filtered_end(); it != ie; ++it) {
|
2010-06-12 06:00:13 +08:00
|
|
|
(*it)->claim();
|
2009-11-25 19:33:30 +08:00
|
|
|
|
|
|
|
if (Joined) {
|
2011-07-23 18:55:15 +08:00
|
|
|
Output.push_back(MakeArgString(StringRef(Translation) +
|
2010-06-12 06:00:13 +08:00
|
|
|
(*it)->getValue(*this, 0)));
|
2009-11-25 19:33:30 +08:00
|
|
|
} else {
|
|
|
|
Output.push_back(Translation);
|
2010-06-12 06:00:13 +08:00
|
|
|
Output.push_back((*it)->getValue(*this, 0));
|
2009-03-26 23:39:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-19 12:14:53 +08:00
|
|
|
void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
|
2009-11-25 19:33:30 +08:00
|
|
|
for (arg_iterator it = filtered_begin(Id0),
|
|
|
|
ie = filtered_end(); it != ie; ++it)
|
2010-06-12 06:00:13 +08:00
|
|
|
(*it)->claim();
|
2009-04-04 04:51:31 +08:00
|
|
|
}
|
|
|
|
|
2011-08-03 01:58:04 +08:00
|
|
|
void ArgList::ClaimAllArgs() const {
|
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it)
|
|
|
|
if (!(*it)->isClaimed())
|
|
|
|
(*it)->claim();
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
const char *ArgList::MakeArgString(const Twine &T) const {
|
2009-09-10 06:32:26 +08:00
|
|
|
llvm::SmallString<256> Str;
|
|
|
|
T.toVector(Str);
|
|
|
|
return MakeArgString(Str.str());
|
|
|
|
}
|
|
|
|
|
2010-06-10 02:49:38 +08:00
|
|
|
const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef LHS,
|
|
|
|
StringRef RHS) const {
|
|
|
|
StringRef Cur = getArgString(Index);
|
2010-06-10 02:49:38 +08:00
|
|
|
if (Cur.size() == LHS.size() + RHS.size() &&
|
|
|
|
Cur.startswith(LHS) && Cur.endswith(RHS))
|
|
|
|
return Cur.data();
|
|
|
|
|
|
|
|
return MakeArgString(LHS + RHS);
|
|
|
|
}
|
|
|
|
|
2009-03-25 12:13:45 +08:00
|
|
|
//
|
|
|
|
|
2010-10-11 17:18:43 +08:00
|
|
|
InputArgList::InputArgList(const char* const *ArgBegin,
|
|
|
|
const char* const *ArgEnd)
|
2010-06-12 06:00:22 +08:00
|
|
|
: NumInputArgStrings(ArgEnd - ArgBegin) {
|
2009-03-25 12:13:45 +08:00
|
|
|
ArgStrings.append(ArgBegin, ArgEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
InputArgList::~InputArgList() {
|
|
|
|
// An InputArgList always owns its arguments.
|
|
|
|
for (iterator it = begin(), ie = end(); it != ie; ++it)
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
unsigned InputArgList::MakeIndex(StringRef String0) const {
|
2009-03-25 12:13:45 +08:00
|
|
|
unsigned Index = ArgStrings.size();
|
|
|
|
|
|
|
|
// Tuck away so we have a reliable const char *.
|
|
|
|
SynthesizedStrings.push_back(String0);
|
|
|
|
ArgStrings.push_back(SynthesizedStrings.back().c_str());
|
|
|
|
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
unsigned InputArgList::MakeIndex(StringRef String0,
|
|
|
|
StringRef String1) const {
|
2009-03-25 12:13:45 +08:00
|
|
|
unsigned Index0 = MakeIndex(String0);
|
|
|
|
unsigned Index1 = MakeIndex(String1);
|
|
|
|
assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
|
|
|
|
(void) Index1;
|
|
|
|
return Index0;
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
const char *InputArgList::MakeArgString(StringRef Str) const {
|
2009-03-25 12:13:45 +08:00
|
|
|
return getArgString(MakeIndex(Str));
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2010-06-12 06:00:26 +08:00
|
|
|
DerivedArgList::DerivedArgList(const InputArgList &_BaseArgs)
|
2010-06-12 06:00:22 +08:00
|
|
|
: BaseArgs(_BaseArgs) {
|
2009-03-25 12:13:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DerivedArgList::~DerivedArgList() {
|
|
|
|
// We only own the arguments we explicitly synthesized.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end();
|
2009-03-25 12:13:45 +08:00
|
|
|
it != ie; ++it)
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
const char *DerivedArgList::MakeArgString(StringRef Str) const {
|
2009-03-25 12:13:45 +08:00
|
|
|
return BaseArgs.MakeArgString(Str);
|
|
|
|
}
|
|
|
|
|
2009-03-30 06:29:05 +08:00
|
|
|
Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option *Opt) const {
|
2010-06-10 06:31:08 +08:00
|
|
|
Arg *A = new Arg(Opt, BaseArgs.MakeIndex(Opt->getName()), BaseArg);
|
2010-03-12 02:04:53 +08:00
|
|
|
SynthesizedArgs.push_back(A);
|
|
|
|
return A;
|
2009-03-25 12:13:45 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option *Opt,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Value) const {
|
2010-06-10 06:31:00 +08:00
|
|
|
unsigned Index = BaseArgs.MakeIndex(Value);
|
2010-06-10 06:31:08 +08:00
|
|
|
Arg *A = new Arg(Opt, Index, BaseArgs.getArgString(Index), BaseArg);
|
2010-03-12 02:04:53 +08:00
|
|
|
SynthesizedArgs.push_back(A);
|
|
|
|
return A;
|
2009-03-25 12:13:45 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option *Opt,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Value) const {
|
2010-06-10 06:31:00 +08:00
|
|
|
unsigned Index = BaseArgs.MakeIndex(Opt->getName(), Value);
|
2010-06-15 04:20:44 +08:00
|
|
|
Arg *A = new Arg(Opt, Index, BaseArgs.getArgString(Index + 1), BaseArg);
|
2010-03-12 02:04:53 +08:00
|
|
|
SynthesizedArgs.push_back(A);
|
|
|
|
return A;
|
2009-03-25 12:13:45 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option *Opt,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Value) const {
|
2011-07-06 00:56:25 +08:00
|
|
|
unsigned Index = BaseArgs.MakeIndex(Opt->getName().str() + Value.str());
|
2010-06-10 06:31:08 +08:00
|
|
|
Arg *A = new Arg(Opt, Index,
|
2011-07-06 00:56:25 +08:00
|
|
|
BaseArgs.getArgString(Index) + Opt->getName().size(),
|
2010-06-10 06:31:08 +08:00
|
|
|
BaseArg);
|
2010-03-12 02:04:53 +08:00
|
|
|
SynthesizedArgs.push_back(A);
|
|
|
|
return A;
|
2009-03-25 12:13:45 +08:00
|
|
|
}
|