Skip to content

Commit 2f95542

Browse files
committed
[llvm] ConvertOption::accept(), acceptInternal() to std::unique_ptr<>
These functions transfer ownership to the caller. Make this clear in the type system. No behavior change.
1 parent 21429cf commit 2f95542

File tree

3 files changed

+42
-36
lines changed

3 files changed

+42
-36
lines changed

llvm/include/llvm/Option/Option.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ class Option {
205205
/// always be false.
206206
bool matches(OptSpecifier ID) const;
207207

208-
/// accept - Potentially accept the current argument, returning a
209-
/// new Arg instance, or 0 if the option does not accept this
210-
/// argument (or the argument is missing values).
208+
/// Potentially accept the current argument, returning a new Arg instance,
209+
/// or 0 if the option does not accept this argument (or the argument is
210+
/// missing values).
211211
///
212212
/// If the option accepts the current argument, accept() sets
213213
/// Index to the position where argument parsing should resume
@@ -217,12 +217,12 @@ class Option {
217217
/// underlying storage to represent a Joined argument.
218218
/// \p GroupedShortOption If true, we are handling the fallback case of
219219
/// parsing a prefix of the current argument as a short option.
220-
Arg *accept(const ArgList &Args, StringRef CurArg, bool GroupedShortOption,
221-
unsigned &Index) const;
220+
std::unique_ptr<Arg> accept(const ArgList &Args, StringRef CurArg,
221+
bool GroupedShortOption, unsigned &Index) const;
222222

223223
private:
224-
Arg *acceptInternal(const ArgList &Args, StringRef CurArg,
225-
unsigned &Index) const;
224+
std::unique_ptr<Arg> acceptInternal(const ArgList &Args, StringRef CurArg,
225+
unsigned &Index) const;
226226

227227
public:
228228
void print(raw_ostream &O) const;

llvm/lib/Option/OptTable.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,9 @@ Arg *OptTable::parseOneArgGrouped(InputArgList &Args, unsigned &Index) const {
359359
continue;
360360

361361
Option Opt(Start, this);
362-
if (Arg *A = Opt.accept(Args, StringRef(Args.getArgString(Index), ArgSize),
363-
false, Index))
364-
return A;
362+
if (std::unique_ptr<Arg> A = Opt.accept(
363+
Args, StringRef(Args.getArgString(Index), ArgSize), false, Index))
364+
return A.release();
365365

366366
// If Opt is a Flag of length 2 (e.g. "-a"), we know it is a prefix of
367367
// the current argument (e.g. "-abc"). Match it as a fallback if no longer
@@ -379,9 +379,10 @@ Arg *OptTable::parseOneArgGrouped(InputArgList &Args, unsigned &Index) const {
379379
if (Str[2] == '=')
380380
return new Arg(getOption(TheUnknownOptionID), Str, Index++, CStr);
381381

382-
if (Arg *A = Opt.accept(Args, Str.substr(0, 2), true, Index)) {
382+
if (std::unique_ptr<Arg> A =
383+
Opt.accept(Args, Str.substr(0, 2), true, Index)) {
383384
Args.replaceArgString(Index, Twine('-') + Str.substr(2));
384-
return A;
385+
return A.release();
385386
}
386387
}
387388

@@ -439,9 +440,9 @@ Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index,
439440
continue;
440441

441442
// See if this option matches.
442-
if (Arg *A = Opt.accept(Args, StringRef(Args.getArgString(Index), ArgSize),
443-
false, Index))
444-
return A;
443+
if (std::unique_ptr<Arg> A = Opt.accept(
444+
Args, StringRef(Args.getArgString(Index), ArgSize), false, Index))
445+
return A.release();
445446

446447
// Otherwise, see if this argument was missing values.
447448
if (Prev != Index)

llvm/lib/Option/Option.cpp

+26-21
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,24 @@ bool Option::matches(OptSpecifier Opt) const {
106106
return false;
107107
}
108108

109-
Arg *Option::acceptInternal(const ArgList &Args, StringRef Spelling,
110-
unsigned &Index) const {
109+
std::unique_ptr<Arg> Option::acceptInternal(const ArgList &Args,
110+
StringRef Spelling,
111+
unsigned &Index) const {
111112
size_t ArgSize = Spelling.size();
112113
switch (getKind()) {
113114
case FlagClass: {
114115
if (ArgSize != strlen(Args.getArgString(Index)))
115116
return nullptr;
116-
return new Arg(*this, Spelling, Index++);
117+
return std::make_unique<Arg>(*this, Spelling, Index++);
117118
}
118119
case JoinedClass: {
119120
const char *Value = Args.getArgString(Index) + ArgSize;
120-
return new Arg(*this, Spelling, Index++, Value);
121+
return std::make_unique<Arg>(*this, Spelling, Index++, Value);
121122
}
122123
case CommaJoinedClass: {
123124
// Always matches.
124125
const char *Str = Args.getArgString(Index) + ArgSize;
125-
Arg *A = new Arg(*this, Spelling, Index++);
126+
auto A = std::make_unique<Arg>(*this, Spelling, Index++);
126127

127128
// Parse out the comma separated values.
128129
const char *Prev = Str;
@@ -158,7 +159,8 @@ Arg *Option::acceptInternal(const ArgList &Args, StringRef Spelling,
158159
Args.getArgString(Index - 1) == nullptr)
159160
return nullptr;
160161

161-
return new Arg(*this, Spelling, Index - 2, Args.getArgString(Index - 1));
162+
return std::make_unique<Arg>(*this, Spelling, Index - 2,
163+
Args.getArgString(Index - 1));
162164
case MultiArgClass: {
163165
// Matches iff this is an exact match.
164166
// FIXME: Avoid strlen.
@@ -169,8 +171,8 @@ Arg *Option::acceptInternal(const ArgList &Args, StringRef Spelling,
169171
if (Index > Args.getNumInputArgStrings())
170172
return nullptr;
171173

172-
Arg *A = new Arg(*this, Spelling, Index - 1 - getNumArgs(),
173-
Args.getArgString(Index - getNumArgs()));
174+
auto A = std::make_unique<Arg>(*this, Spelling, Index - 1 - getNumArgs(),
175+
Args.getArgString(Index - getNumArgs()));
174176
for (unsigned i = 1; i != getNumArgs(); ++i)
175177
A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
176178
return A;
@@ -180,7 +182,7 @@ Arg *Option::acceptInternal(const ArgList &Args, StringRef Spelling,
180182
// FIXME: Avoid strlen.
181183
if (ArgSize != strlen(Args.getArgString(Index))) {
182184
const char *Value = Args.getArgString(Index) + ArgSize;
183-
return new Arg(*this, Spelling, Index++, Value);
185+
return std::make_unique<Arg>(*this, Spelling, Index++, Value);
184186
}
185187

186188
// Otherwise it must be separate.
@@ -189,7 +191,8 @@ Arg *Option::acceptInternal(const ArgList &Args, StringRef Spelling,
189191
Args.getArgString(Index - 1) == nullptr)
190192
return nullptr;
191193

192-
return new Arg(*this, Spelling, Index - 2, Args.getArgString(Index - 1));
194+
return std::make_unique<Arg>(*this, Spelling, Index - 2,
195+
Args.getArgString(Index - 1));
193196
}
194197
case JoinedAndSeparateClass:
195198
// Always matches.
@@ -198,22 +201,22 @@ Arg *Option::acceptInternal(const ArgList &Args, StringRef Spelling,
198201
Args.getArgString(Index - 1) == nullptr)
199202
return nullptr;
200203

201-
return new Arg(*this, Spelling, Index - 2,
202-
Args.getArgString(Index - 2) + ArgSize,
203-
Args.getArgString(Index - 1));
204+
return std::make_unique<Arg>(*this, Spelling, Index - 2,
205+
Args.getArgString(Index - 2) + ArgSize,
206+
Args.getArgString(Index - 1));
204207
case RemainingArgsClass: {
205208
// Matches iff this is an exact match.
206209
// FIXME: Avoid strlen.
207210
if (ArgSize != strlen(Args.getArgString(Index)))
208211
return nullptr;
209-
Arg *A = new Arg(*this, Spelling, Index++);
212+
auto A = std::make_unique<Arg>(*this, Spelling, Index++);
210213
while (Index < Args.getNumInputArgStrings() &&
211214
Args.getArgString(Index) != nullptr)
212215
A->getValues().push_back(Args.getArgString(Index++));
213216
return A;
214217
}
215218
case RemainingArgsJoinedClass: {
216-
Arg *A = new Arg(*this, Spelling, Index);
219+
auto A = std::make_unique<Arg>(*this, Spelling, Index);
217220
if (ArgSize != strlen(Args.getArgString(Index))) {
218221
// An inexact match means there is a joined arg.
219222
A->getValues().push_back(Args.getArgString(Index) + ArgSize);
@@ -230,17 +233,18 @@ Arg *Option::acceptInternal(const ArgList &Args, StringRef Spelling,
230233
}
231234
}
232235

233-
Arg *Option::accept(const ArgList &Args, StringRef CurArg,
234-
bool GroupedShortOption, unsigned &Index) const {
235-
std::unique_ptr<Arg> A(GroupedShortOption && getKind() == FlagClass
236-
? new Arg(*this, CurArg, Index)
236+
std::unique_ptr<Arg> Option::accept(const ArgList &Args, StringRef CurArg,
237+
bool GroupedShortOption,
238+
unsigned &Index) const {
239+
auto A(GroupedShortOption && getKind() == FlagClass
240+
? std::make_unique<Arg>(*this, CurArg, Index)
237241
: acceptInternal(Args, CurArg, Index));
238242
if (!A)
239243
return nullptr;
240244

241245
const Option &UnaliasedOption = getUnaliasedOption();
242246
if (getID() == UnaliasedOption.getID())
243-
return A.release();
247+
return A;
244248

245249
// "A" is an alias for a different flag. For most clients it's more convenient
246250
// if this function returns unaliased Args, so create an unaliased arg for
@@ -259,7 +263,8 @@ Arg *Option::accept(const ArgList &Args, StringRef CurArg,
259263
// Due to this, ArgList::getArgString(A->getIndex()) will return the spelling
260264
// of the aliased arg always, while A->getSpelling() returns either the
261265
// unaliased or the aliased arg, depending on which Arg object it's called on.
262-
Arg *UnaliasedA = new Arg(UnaliasedOption, UnaliasedSpelling, A->getIndex());
266+
auto UnaliasedA =
267+
std::make_unique<Arg>(UnaliasedOption, UnaliasedSpelling, A->getIndex());
263268
Arg *RawA = A.get();
264269
UnaliasedA->setAlias(std::move(A));
265270

0 commit comments

Comments
 (0)