Use unique_ptr to handle ownership of synthesized args in DerivedArgList

This might be able to be simplified further by using Arg as a value type
in a linked list (to maintain pointer validity), but here's something
simple to start with.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206724 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
David Blaikie 2014-04-20 22:37:46 +00:00
parent f8107eb051
commit 7117d739d6
2 changed files with 29 additions and 31 deletions

View File

@ -15,6 +15,7 @@
#include "llvm/Option/OptSpecifier.h" #include "llvm/Option/OptSpecifier.h"
#include "llvm/Option/Option.h" #include "llvm/Option/Option.h"
#include <list> #include <list>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -334,7 +335,7 @@ class DerivedArgList : public ArgList {
const InputArgList &BaseArgs; const InputArgList &BaseArgs;
/// The list of arguments we synthesized. /// The list of arguments we synthesized.
mutable arglist_type SynthesizedArgs; mutable SmallVector<std::unique_ptr<Arg>, 16> SynthesizedArgs;
public: public:
/// Construct a new derived arg list from \p BaseArgs. /// Construct a new derived arg list from \p BaseArgs.
@ -358,9 +359,7 @@ public:
/// AddSynthesizedArg - Add a argument to the list of synthesized arguments /// AddSynthesizedArg - Add a argument to the list of synthesized arguments
/// (to be freed). /// (to be freed).
void AddSynthesizedArg(Arg *A) { void AddSynthesizedArg(Arg *A);
SynthesizedArgs.push_back(A);
}
const char *MakeArgString(StringRef Str) const override; const char *MakeArgString(StringRef Str) const override;

View File

@ -9,6 +9,7 @@
#include "llvm/Option/ArgList.h" #include "llvm/Option/ArgList.h"
#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallString.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Twine.h" #include "llvm/ADT/Twine.h"
#include "llvm/Option/Arg.h" #include "llvm/Option/Arg.h"
#include "llvm/Option/Option.h" #include "llvm/Option/Option.h"
@ -348,52 +349,50 @@ DerivedArgList::DerivedArgList(const InputArgList &_BaseArgs)
: BaseArgs(_BaseArgs) { : BaseArgs(_BaseArgs) {
} }
DerivedArgList::~DerivedArgList() { DerivedArgList::~DerivedArgList() {}
// We only own the arguments we explicitly synthesized.
for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end();
it != ie; ++it)
delete *it;
}
const char *DerivedArgList::MakeArgString(StringRef Str) const { const char *DerivedArgList::MakeArgString(StringRef Str) const {
return BaseArgs.MakeArgString(Str); return BaseArgs.MakeArgString(Str);
} }
void DerivedArgList::AddSynthesizedArg(Arg *A) {
SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
}
Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const { Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) + SynthesizedArgs.push_back(make_unique<Arg>(
Twine(Opt.getName())), Opt,
BaseArgs.MakeIndex(Opt.getName()), BaseArg); ArgList::MakeArgString(Twine(Opt.getPrefix()) + Twine(Opt.getName())),
SynthesizedArgs.push_back(A); BaseArgs.MakeIndex(Opt.getName()), BaseArg));
return A; return SynthesizedArgs.back().get();
} }
Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt, Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
StringRef Value) const { StringRef Value) const {
unsigned Index = BaseArgs.MakeIndex(Value); unsigned Index = BaseArgs.MakeIndex(Value);
Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) + SynthesizedArgs.push_back(make_unique<Arg>(
Twine(Opt.getName())), Opt,
Index, BaseArgs.getArgString(Index), BaseArg); ArgList::MakeArgString(Twine(Opt.getPrefix()) + Twine(Opt.getName())),
SynthesizedArgs.push_back(A); Index, BaseArgs.getArgString(Index), BaseArg));
return A; return SynthesizedArgs.back().get();
} }
Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt, Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
StringRef Value) const { StringRef Value) const {
unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value); unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) + SynthesizedArgs.push_back(make_unique<Arg>(
Twine(Opt.getName())), Opt,
Index, BaseArgs.getArgString(Index + 1), BaseArg); ArgList::MakeArgString(Twine(Opt.getPrefix()) + Twine(Opt.getName())),
SynthesizedArgs.push_back(A); Index, BaseArgs.getArgString(Index + 1), BaseArg));
return A; return SynthesizedArgs.back().get();
} }
Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt, Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
StringRef Value) const { StringRef Value) const {
unsigned Index = BaseArgs.MakeIndex(Opt.getName().str() + Value.str()); unsigned Index = BaseArgs.MakeIndex(Opt.getName().str() + Value.str());
Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) + SynthesizedArgs.push_back(make_unique<Arg>(
Twine(Opt.getName())), Index, Opt,
BaseArgs.getArgString(Index) + Opt.getName().size(), ArgList::MakeArgString(Twine(Opt.getPrefix()) + Twine(Opt.getName())),
BaseArg); Index, BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
SynthesizedArgs.push_back(A); return SynthesizedArgs.back().get();
return A;
} }