[clang] 4bd5fbe - PragmaNamespace::Handlers: Use unique_ptr to simplify memory management

David Blaikie via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 28 22:39:06 PDT 2020


Author: David Blaikie
Date: 2020-04-28T22:31:15-07:00
New Revision: 4bd5fbec4bef71d79cbcd916237c8c7b467fb782

URL: https://github.com/llvm/llvm-project/commit/4bd5fbec4bef71d79cbcd916237c8c7b467fb782
DIFF: https://github.com/llvm/llvm-project/commit/4bd5fbec4bef71d79cbcd916237c8c7b467fb782.diff

LOG: 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.

Added: 
    

Modified: 
    clang/include/clang/Lex/Pragma.h
    clang/lib/Lex/Pragma.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Lex/Pragma.h b/clang/include/clang/Lex/Pragma.h
index e9434269c19c..cf8cca5414ea 100644
--- a/clang/include/clang/Lex/Pragma.h
+++ b/clang/include/clang/Lex/Pragma.h
@@ -96,11 +96,10 @@ class EmptyPragmaHandler : public PragmaHandler {
 class PragmaNamespace : public PragmaHandler {
   /// Handlers - This is a map of the handlers in this namespace with their name
   /// as key.
-  llvm::StringMap<PragmaHandler *> Handlers;
+  llvm::StringMap<std::unique_ptr<PragmaHandler>> Handlers;
 
 public:
   explicit PragmaNamespace(StringRef Name) : PragmaHandler(Name) {}
-  ~PragmaNamespace() override;
 
   /// 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

diff  --git a/clang/lib/Lex/Pragma.cpp b/clang/lib/Lex/Pragma.cpp
index 57a95815488e..8d93eb3d6cdc 100644
--- a/clang/lib/Lex/Pragma.cpp
+++ b/clang/lib/Lex/Pragma.cpp
@@ -71,31 +71,36 @@ void EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
 // PragmaNamespace Implementation.
 //===----------------------------------------------------------------------===//
 
-PragmaNamespace::~PragmaNamespace() {
-  llvm::DeleteContainerSeconds(Handlers);
-}
-
 /// 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
 /// exists, otherwise return null.  If IgnoreNull is true (the default) then
 /// the null handler isn't returned on failure to match.
 PragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
                                             bool IgnoreNull) const {
-  if (PragmaHandler *Handler = Handlers.lookup(Name))
-    return Handler;
-  return IgnoreNull ? nullptr : Handlers.lookup(StringRef());
+  auto I = Handlers.find(Name);
+  if (I != Handlers.end())
+    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) {
-  assert(!Handlers.lookup(Handler->getName()) &&
+  assert(!Handlers.count(Handler->getName()) &&
          "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) {
-  assert(Handlers.lookup(Handler->getName()) &&
+  auto I = Handlers.find(Handler->getName());
+  assert(I != Handlers.end() &&
          "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,


        


More information about the cfe-commits mailing list