PragmaNamespace::Handlers: Use unique_ptr to simplify memory management

The API actually passes and returns ownership too, but the usage uis
complicated enough that I'm not going to unique_ptr-ify those add/remove
calls.
This commit is contained in:
David Blaikie 2020-04-28 18:59:40 -07:00
parent 957c5dd78b
commit 4bd5fbec4b
2 changed files with 17 additions and 13 deletions

View File

@ -96,11 +96,10 @@ public:
class PragmaNamespace : public PragmaHandler { class PragmaNamespace : public PragmaHandler {
/// Handlers - This is a map of the handlers in this namespace with their name /// Handlers - This is a map of the handlers in this namespace with their name
/// as key. /// as key.
llvm::StringMap<PragmaHandler *> Handlers; llvm::StringMap<std::unique_ptr<PragmaHandler>> Handlers;
public: public:
explicit PragmaNamespace(StringRef Name) : PragmaHandler(Name) {} explicit PragmaNamespace(StringRef Name) : PragmaHandler(Name) {}
~PragmaNamespace() override;
/// FindHandler - Check to see if there is already a handler for the /// FindHandler - Check to see if there is already a handler for the
/// specified name. If not, return the handler for the null name if it /// specified name. If not, return the handler for the null name if it

View File

@ -71,31 +71,36 @@ void EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
// PragmaNamespace Implementation. // PragmaNamespace Implementation.
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
PragmaNamespace::~PragmaNamespace() {
llvm::DeleteContainerSeconds(Handlers);
}
/// FindHandler - Check to see if there is already a handler for the /// FindHandler - Check to see if there is already a handler for the
/// specified name. If not, return the handler for the null identifier if it /// specified name. If not, return the handler for the null identifier if it
/// exists, otherwise return null. If IgnoreNull is true (the default) then /// exists, otherwise return null. If IgnoreNull is true (the default) then
/// the null handler isn't returned on failure to match. /// the null handler isn't returned on failure to match.
PragmaHandler *PragmaNamespace::FindHandler(StringRef Name, PragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
bool IgnoreNull) const { bool IgnoreNull) const {
if (PragmaHandler *Handler = Handlers.lookup(Name)) auto I = Handlers.find(Name);
return Handler; if (I != Handlers.end())
return IgnoreNull ? nullptr : Handlers.lookup(StringRef()); return I->getValue().get();
if (IgnoreNull)
return nullptr;
I = Handlers.find(StringRef());
if (I != Handlers.end())
return I->getValue().get();
return nullptr;
} }
void PragmaNamespace::AddPragma(PragmaHandler *Handler) { void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
assert(!Handlers.lookup(Handler->getName()) && assert(!Handlers.count(Handler->getName()) &&
"A handler with this name is already registered in this namespace"); "A handler with this name is already registered in this namespace");
Handlers[Handler->getName()] = Handler; Handlers[Handler->getName()].reset(Handler);
} }
void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) { void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
assert(Handlers.lookup(Handler->getName()) && auto I = Handlers.find(Handler->getName());
assert(I != Handlers.end() &&
"Handler not registered in this namespace"); "Handler not registered in this namespace");
Handlers.erase(Handler->getName()); // Release ownership back to the caller.
I->getValue().release();
Handlers.erase(I);
} }
void PragmaNamespace::HandlePragma(Preprocessor &PP, void PragmaNamespace::HandlePragma(Preprocessor &PP,