2017-06-16 08:43:26 +08:00
|
|
|
//===- ArgList.cpp - Argument List Management -----------------------------===//
|
2012-12-05 08:29:32 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-12-05 08:29:32 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-16 08:43:26 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/None.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2014-04-21 06:37:46 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2017-06-16 08:43:26 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2012-12-05 08:29:32 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2018-04-30 22:59:11 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2012-12-05 08:29:32 +08:00
|
|
|
#include "llvm/Option/Arg.h"
|
2017-06-16 08:43:26 +08:00
|
|
|
#include "llvm/Option/ArgList.h"
|
2012-12-05 08:29:32 +08:00
|
|
|
#include "llvm/Option/Option.h"
|
2017-06-16 08:43:26 +08:00
|
|
|
#include "llvm/Option/OptSpecifier.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2015-12-19 02:55:26 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2012-12-05 08:29:32 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-06-16 08:43:26 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::opt;
|
|
|
|
|
|
|
|
void ArgList::append(Arg *A) {
|
|
|
|
Args.push_back(A);
|
|
|
|
|
2017-04-13 07:19:51 +08:00
|
|
|
// Update ranges for the option and all of its groups.
|
|
|
|
for (Option O = A->getOption().getUnaliasedOption(); O.isValid();
|
|
|
|
O = O.getGroup()) {
|
|
|
|
auto &R =
|
|
|
|
OptRanges.insert(std::make_pair(O.getID(), emptyRange())).first->second;
|
|
|
|
R.first = std::min<unsigned>(R.first, Args.size() - 1);
|
|
|
|
R.second = Args.size();
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 07:19:51 +08:00
|
|
|
void ArgList::eraseArg(OptSpecifier Id) {
|
|
|
|
// Zero out the removed entries but keep them around so that we don't
|
|
|
|
// need to invalidate OptRanges.
|
|
|
|
for (Arg *const &A : filtered(Id)) {
|
|
|
|
// Avoid the need for a non-const filtered iterator variant.
|
|
|
|
Arg **ArgsBegin = Args.data();
|
|
|
|
ArgsBegin[&A - ArgsBegin] = nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
2017-04-13 07:19:51 +08:00
|
|
|
OptRanges.erase(Id.getID());
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
2017-04-13 07:19:51 +08:00
|
|
|
ArgList::OptRange
|
|
|
|
ArgList::getRange(std::initializer_list<OptSpecifier> Ids) const {
|
|
|
|
OptRange R = emptyRange();
|
|
|
|
for (auto Id : Ids) {
|
|
|
|
auto I = OptRanges.find(Id.getID());
|
|
|
|
if (I != OptRanges.end()) {
|
|
|
|
R.first = std::min(R.first, I->second.first);
|
|
|
|
R.second = std::max(R.second, I->second.second);
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
}
|
2017-04-13 07:19:51 +08:00
|
|
|
// Map an empty {-1, 0} range to {0, 0} so it can be used to form iterators.
|
|
|
|
if (R.first == -1u)
|
|
|
|
R.first = 0;
|
|
|
|
return R;
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
|
|
|
|
if (Arg *A = getLastArg(Pos, Neg))
|
|
|
|
return A->getOption().matches(Pos);
|
|
|
|
return Default;
|
|
|
|
}
|
|
|
|
|
2013-06-14 02:12:12 +08:00
|
|
|
bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
|
|
|
|
bool Default) const {
|
|
|
|
if (Arg *A = getLastArg(Pos, PosAlias, Neg))
|
|
|
|
return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);
|
|
|
|
return Default;
|
|
|
|
}
|
|
|
|
|
2017-04-13 07:19:51 +08:00
|
|
|
StringRef ArgList::getLastArgValue(OptSpecifier Id, StringRef Default) const {
|
2012-12-05 08:29:32 +08:00
|
|
|
if (Arg *A = getLastArg(Id))
|
|
|
|
return A->getValue();
|
|
|
|
return Default;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
|
|
|
|
SmallVector<const char *, 16> Values;
|
|
|
|
AddAllArgValues(Values, Id);
|
|
|
|
return std::vector<std::string>(Values.begin(), Values.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
|
|
|
|
if (Arg *A = getLastArg(Id)) {
|
|
|
|
A->claim();
|
|
|
|
A->render(*this, Output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 02:12:12 +08:00
|
|
|
void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id0,
|
|
|
|
OptSpecifier Id1) const {
|
|
|
|
if (Arg *A = getLastArg(Id0, Id1)) {
|
|
|
|
A->claim();
|
|
|
|
A->render(*this, Output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 03:47:58 +08:00
|
|
|
void ArgList::AddAllArgsExcept(ArgStringList &Output,
|
|
|
|
ArrayRef<OptSpecifier> Ids,
|
|
|
|
ArrayRef<OptSpecifier> ExcludeIds) const {
|
2017-04-13 07:19:51 +08:00
|
|
|
for (const Arg *Arg : *this) {
|
2016-09-30 03:47:58 +08:00
|
|
|
bool Excluded = false;
|
|
|
|
for (OptSpecifier Id : ExcludeIds) {
|
2015-07-30 01:34:41 +08:00
|
|
|
if (Arg->getOption().matches(Id)) {
|
2016-09-30 03:47:58 +08:00
|
|
|
Excluded = true;
|
2015-07-30 01:34:41 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-09-30 03:47:58 +08:00
|
|
|
if (!Excluded) {
|
|
|
|
for (OptSpecifier Id : Ids) {
|
|
|
|
if (Arg->getOption().matches(Id)) {
|
|
|
|
Arg->claim();
|
|
|
|
Arg->render(*this, Output);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-30 01:34:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 03:47:58 +08:00
|
|
|
/// This is a nicer interface when you don't have a list of Ids to exclude.
|
|
|
|
void ArgList::AddAllArgs(ArgStringList &Output,
|
|
|
|
ArrayRef<OptSpecifier> Ids) const {
|
|
|
|
ArrayRef<OptSpecifier> Exclude = None;
|
|
|
|
AddAllArgsExcept(Output, Ids, Exclude);
|
|
|
|
}
|
|
|
|
|
2015-07-30 01:34:41 +08:00
|
|
|
/// This 3-opt variant of AddAllArgs could be eliminated in favor of one
|
|
|
|
/// that accepts a single specifier, given the above which accepts any number.
|
2012-12-05 08:29:32 +08:00
|
|
|
void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
|
|
|
|
OptSpecifier Id1, OptSpecifier Id2) const {
|
2014-07-09 21:03:37 +08:00
|
|
|
for (auto Arg: filtered(Id0, Id1, Id2)) {
|
|
|
|
Arg->claim();
|
|
|
|
Arg->render(*this, Output);
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
|
|
|
|
OptSpecifier Id1, OptSpecifier Id2) const {
|
2014-07-09 21:03:37 +08:00
|
|
|
for (auto Arg : filtered(Id0, Id1, Id2)) {
|
|
|
|
Arg->claim();
|
2015-02-17 23:29:18 +08:00
|
|
|
const auto &Values = Arg->getValues();
|
|
|
|
Output.append(Values.begin(), Values.end());
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
|
|
|
|
const char *Translation,
|
|
|
|
bool Joined) const {
|
2014-07-09 21:03:37 +08:00
|
|
|
for (auto Arg: filtered(Id0)) {
|
|
|
|
Arg->claim();
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
if (Joined) {
|
|
|
|
Output.push_back(MakeArgString(StringRef(Translation) +
|
2014-07-09 21:03:37 +08:00
|
|
|
Arg->getValue(0)));
|
2012-12-05 08:29:32 +08:00
|
|
|
} else {
|
|
|
|
Output.push_back(Translation);
|
2014-07-09 21:03:37 +08:00
|
|
|
Output.push_back(Arg->getValue(0));
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
|
2017-04-13 07:19:51 +08:00
|
|
|
for (auto *Arg : filtered(Id0))
|
2014-07-09 21:03:37 +08:00
|
|
|
Arg->claim();
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArgList::ClaimAllArgs() const {
|
2017-04-13 07:19:51 +08:00
|
|
|
for (auto *Arg : *this)
|
|
|
|
if (!Arg->isClaimed())
|
|
|
|
Arg->claim();
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
|
|
|
|
StringRef LHS,
|
|
|
|
StringRef RHS) const {
|
|
|
|
StringRef Cur = getArgString(Index);
|
|
|
|
if (Cur.size() == LHS.size() + RHS.size() &&
|
|
|
|
Cur.startswith(LHS) && Cur.endswith(RHS))
|
|
|
|
return Cur.data();
|
|
|
|
|
|
|
|
return MakeArgString(LHS + RHS);
|
|
|
|
}
|
|
|
|
|
2015-12-19 02:55:26 +08:00
|
|
|
void ArgList::print(raw_ostream &O) const {
|
2015-12-19 02:55:22 +08:00
|
|
|
for (Arg *A : *this) {
|
2015-12-19 02:55:26 +08:00
|
|
|
O << "* ";
|
|
|
|
A->print(O);
|
2015-12-19 02:55:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:32:27 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2015-12-19 02:55:26 +08:00
|
|
|
LLVM_DUMP_METHOD void ArgList::dump() const { print(dbgs()); }
|
2017-01-28 10:02:38 +08:00
|
|
|
#endif
|
2015-12-19 02:55:26 +08:00
|
|
|
|
2015-06-23 23:28:10 +08:00
|
|
|
void InputArgList::releaseMemory() {
|
|
|
|
// An InputArgList always owns its arguments.
|
|
|
|
for (Arg *A : *this)
|
|
|
|
delete A;
|
|
|
|
}
|
|
|
|
|
2012-12-05 08:29:32 +08:00
|
|
|
InputArgList::InputArgList(const char* const *ArgBegin,
|
|
|
|
const char* const *ArgEnd)
|
|
|
|
: NumInputArgStrings(ArgEnd - ArgBegin) {
|
|
|
|
ArgStrings.append(ArgBegin, ArgEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned InputArgList::MakeIndex(StringRef String0) const {
|
|
|
|
unsigned Index = ArgStrings.size();
|
|
|
|
|
|
|
|
// Tuck away so we have a reliable const char *.
|
2013-07-16 00:40:52 +08:00
|
|
|
SynthesizedStrings.push_back(String0);
|
|
|
|
ArgStrings.push_back(SynthesizedStrings.back().c_str());
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned InputArgList::MakeIndex(StringRef String0,
|
|
|
|
StringRef String1) const {
|
|
|
|
unsigned Index0 = MakeIndex(String0);
|
|
|
|
unsigned Index1 = MakeIndex(String1);
|
|
|
|
assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
|
|
|
|
(void) Index1;
|
|
|
|
return Index0;
|
|
|
|
}
|
|
|
|
|
2015-03-17 17:51:17 +08:00
|
|
|
const char *InputArgList::MakeArgStringRef(StringRef Str) const {
|
2012-12-05 08:29:32 +08:00
|
|
|
return getArgString(MakeIndex(Str));
|
|
|
|
}
|
|
|
|
|
2015-03-17 02:06:57 +08:00
|
|
|
DerivedArgList::DerivedArgList(const InputArgList &BaseArgs)
|
|
|
|
: BaseArgs(BaseArgs) {}
|
2012-12-05 08:29:32 +08:00
|
|
|
|
2015-03-17 17:51:17 +08:00
|
|
|
const char *DerivedArgList::MakeArgStringRef(StringRef Str) const {
|
2012-12-05 08:29:32 +08:00
|
|
|
return BaseArgs.MakeArgString(Str);
|
|
|
|
}
|
|
|
|
|
2014-04-21 06:37:46 +08:00
|
|
|
void DerivedArgList::AddSynthesizedArg(Arg *A) {
|
|
|
|
SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
|
|
|
|
}
|
|
|
|
|
2012-12-05 08:29:32 +08:00
|
|
|
Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
|
2014-06-20 12:36:29 +08:00
|
|
|
SynthesizedArgs.push_back(
|
|
|
|
make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
|
|
|
|
BaseArgs.MakeIndex(Opt.getName()), BaseArg));
|
2014-04-21 06:37:46 +08:00
|
|
|
return SynthesizedArgs.back().get();
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
|
|
|
|
StringRef Value) const {
|
|
|
|
unsigned Index = BaseArgs.MakeIndex(Value);
|
2014-06-20 12:36:29 +08:00
|
|
|
SynthesizedArgs.push_back(
|
|
|
|
make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
|
|
|
|
Index, BaseArgs.getArgString(Index), BaseArg));
|
2014-04-21 06:37:46 +08:00
|
|
|
return SynthesizedArgs.back().get();
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
|
|
|
|
StringRef Value) const {
|
|
|
|
unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
|
2014-06-20 12:36:29 +08:00
|
|
|
SynthesizedArgs.push_back(
|
|
|
|
make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
|
|
|
|
Index, BaseArgs.getArgString(Index + 1), BaseArg));
|
2014-04-21 06:37:46 +08:00
|
|
|
return SynthesizedArgs.back().get();
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
|
|
|
|
StringRef Value) const {
|
2015-03-30 23:42:36 +08:00
|
|
|
unsigned Index = BaseArgs.MakeIndex((Opt.getName() + Value).str());
|
2014-04-21 06:37:46 +08:00
|
|
|
SynthesizedArgs.push_back(make_unique<Arg>(
|
2014-06-20 12:36:29 +08:00
|
|
|
Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
|
|
|
|
BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
|
2014-04-21 06:37:46 +08:00
|
|
|
return SynthesizedArgs.back().get();
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|