/// always be false.
bool matches(OptSpecifier ID) const;
- /// accept - Potentially accept the current argument, returning a
- /// new Arg instance, or 0 if the option does not accept this
- /// argument (or the argument is missing values).
+ /// Potentially accept the current argument, returning a new Arg instance,
+ /// or 0 if the option does not accept this argument (or the argument is
+ /// missing values).
///
/// If the option accepts the current argument, accept() sets
/// Index to the position where argument parsing should resume
/// underlying storage to represent a Joined argument.
/// \p GroupedShortOption If true, we are handling the fallback case of
/// parsing a prefix of the current argument as a short option.
- Arg *accept(const ArgList &Args, StringRef CurArg, bool GroupedShortOption,
- unsigned &Index) const;
+ std::unique_ptr<Arg> accept(const ArgList &Args, StringRef CurArg,
+ bool GroupedShortOption, unsigned &Index) const;
private:
- Arg *acceptInternal(const ArgList &Args, StringRef CurArg,
- unsigned &Index) const;
+ std::unique_ptr<Arg> acceptInternal(const ArgList &Args, StringRef CurArg,
+ unsigned &Index) const;
public:
void print(raw_ostream &O) const;
continue;
Option Opt(Start, this);
- if (Arg *A = Opt.accept(Args, StringRef(Args.getArgString(Index), ArgSize),
- false, Index))
- return A;
+ if (std::unique_ptr<Arg> A = Opt.accept(
+ Args, StringRef(Args.getArgString(Index), ArgSize), false, Index))
+ return A.release();
// If Opt is a Flag of length 2 (e.g. "-a"), we know it is a prefix of
// the current argument (e.g. "-abc"). Match it as a fallback if no longer
if (Str[2] == '=')
return new Arg(getOption(TheUnknownOptionID), Str, Index++, CStr);
- if (Arg *A = Opt.accept(Args, Str.substr(0, 2), true, Index)) {
+ if (std::unique_ptr<Arg> A =
+ Opt.accept(Args, Str.substr(0, 2), true, Index)) {
Args.replaceArgString(Index, Twine('-') + Str.substr(2));
- return A;
+ return A.release();
}
}
continue;
// See if this option matches.
- if (Arg *A = Opt.accept(Args, StringRef(Args.getArgString(Index), ArgSize),
- false, Index))
- return A;
+ if (std::unique_ptr<Arg> A = Opt.accept(
+ Args, StringRef(Args.getArgString(Index), ArgSize), false, Index))
+ return A.release();
// Otherwise, see if this argument was missing values.
if (Prev != Index)
return false;
}
-Arg *Option::acceptInternal(const ArgList &Args, StringRef Spelling,
- unsigned &Index) const {
+std::unique_ptr<Arg> Option::acceptInternal(const ArgList &Args,
+ StringRef Spelling,
+ unsigned &Index) const {
size_t ArgSize = Spelling.size();
switch (getKind()) {
case FlagClass: {
if (ArgSize != strlen(Args.getArgString(Index)))
return nullptr;
- return new Arg(*this, Spelling, Index++);
+ return std::make_unique<Arg>(*this, Spelling, Index++);
}
case JoinedClass: {
const char *Value = Args.getArgString(Index) + ArgSize;
- return new Arg(*this, Spelling, Index++, Value);
+ return std::make_unique<Arg>(*this, Spelling, Index++, Value);
}
case CommaJoinedClass: {
// Always matches.
const char *Str = Args.getArgString(Index) + ArgSize;
- Arg *A = new Arg(*this, Spelling, Index++);
+ auto A = std::make_unique<Arg>(*this, Spelling, Index++);
// Parse out the comma separated values.
const char *Prev = Str;
Args.getArgString(Index - 1) == nullptr)
return nullptr;
- return new Arg(*this, Spelling, Index - 2, Args.getArgString(Index - 1));
+ return std::make_unique<Arg>(*this, Spelling, Index - 2,
+ Args.getArgString(Index - 1));
case MultiArgClass: {
// Matches iff this is an exact match.
// FIXME: Avoid strlen.
if (Index > Args.getNumInputArgStrings())
return nullptr;
- Arg *A = new Arg(*this, Spelling, Index - 1 - getNumArgs(),
- Args.getArgString(Index - getNumArgs()));
+ auto A = std::make_unique<Arg>(*this, Spelling, Index - 1 - getNumArgs(),
+ Args.getArgString(Index - getNumArgs()));
for (unsigned i = 1; i != getNumArgs(); ++i)
A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
return A;
// FIXME: Avoid strlen.
if (ArgSize != strlen(Args.getArgString(Index))) {
const char *Value = Args.getArgString(Index) + ArgSize;
- return new Arg(*this, Spelling, Index++, Value);
+ return std::make_unique<Arg>(*this, Spelling, Index++, Value);
}
// Otherwise it must be separate.
Args.getArgString(Index - 1) == nullptr)
return nullptr;
- return new Arg(*this, Spelling, Index - 2, Args.getArgString(Index - 1));
+ return std::make_unique<Arg>(*this, Spelling, Index - 2,
+ Args.getArgString(Index - 1));
}
case JoinedAndSeparateClass:
// Always matches.
Args.getArgString(Index - 1) == nullptr)
return nullptr;
- return new Arg(*this, Spelling, Index - 2,
- Args.getArgString(Index - 2) + ArgSize,
- Args.getArgString(Index - 1));
+ return std::make_unique<Arg>(*this, Spelling, Index - 2,
+ Args.getArgString(Index - 2) + ArgSize,
+ Args.getArgString(Index - 1));
case RemainingArgsClass: {
// Matches iff this is an exact match.
// FIXME: Avoid strlen.
if (ArgSize != strlen(Args.getArgString(Index)))
return nullptr;
- Arg *A = new Arg(*this, Spelling, Index++);
+ auto A = std::make_unique<Arg>(*this, Spelling, Index++);
while (Index < Args.getNumInputArgStrings() &&
Args.getArgString(Index) != nullptr)
A->getValues().push_back(Args.getArgString(Index++));
return A;
}
case RemainingArgsJoinedClass: {
- Arg *A = new Arg(*this, Spelling, Index);
+ auto A = std::make_unique<Arg>(*this, Spelling, Index);
if (ArgSize != strlen(Args.getArgString(Index))) {
// An inexact match means there is a joined arg.
A->getValues().push_back(Args.getArgString(Index) + ArgSize);
}
}
-Arg *Option::accept(const ArgList &Args, StringRef CurArg,
- bool GroupedShortOption, unsigned &Index) const {
- std::unique_ptr<Arg> A(GroupedShortOption && getKind() == FlagClass
- ? new Arg(*this, CurArg, Index)
+std::unique_ptr<Arg> Option::accept(const ArgList &Args, StringRef CurArg,
+ bool GroupedShortOption,
+ unsigned &Index) const {
+ auto A(GroupedShortOption && getKind() == FlagClass
+ ? std::make_unique<Arg>(*this, CurArg, Index)
: acceptInternal(Args, CurArg, Index));
if (!A)
return nullptr;
const Option &UnaliasedOption = getUnaliasedOption();
if (getID() == UnaliasedOption.getID())
- return A.release();
+ return A;
// "A" is an alias for a different flag. For most clients it's more convenient
// if this function returns unaliased Args, so create an unaliased arg for
// Due to this, ArgList::getArgString(A->getIndex()) will return the spelling
// of the aliased arg always, while A->getSpelling() returns either the
// unaliased or the aliased arg, depending on which Arg object it's called on.
- Arg *UnaliasedA = new Arg(UnaliasedOption, UnaliasedSpelling, A->getIndex());
+ auto UnaliasedA =
+ std::make_unique<Arg>(UnaliasedOption, UnaliasedSpelling, A->getIndex());
Arg *RawA = A.get();
UnaliasedA->setAlias(std::move(A));