[clang] 409df39 - ASTWriter::FileDeclIDs: Use unique_ptr to simplify memory management

David Blaikie via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 28 18:11:18 PDT 2020


Author: David Blaikie
Date: 2020-04-28T18:05:28-07:00
New Revision: 409df3987cb8d6c4a9005b2e633d0116c315375d

URL: https://github.com/llvm/llvm-project/commit/409df3987cb8d6c4a9005b2e633d0116c315375d
DIFF: https://github.com/llvm/llvm-project/commit/409df3987cb8d6c4a9005b2e633d0116c315375d.diff

LOG: ASTWriter::FileDeclIDs: Use unique_ptr to simplify memory management

Added: 
    

Modified: 
    clang/include/clang/Serialization/ASTWriter.h
    clang/lib/Serialization/ASTWriter.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Serialization/ASTWriter.h b/clang/include/clang/Serialization/ASTWriter.h
index 8dc4889e3ae8..bc5a782f20cc 100644
--- a/clang/include/clang/Serialization/ASTWriter.h
+++ b/clang/include/clang/Serialization/ASTWriter.h
@@ -216,7 +216,8 @@ class ASTWriter : public ASTDeserializationListener,
     /// indicates the index that this particular vector has in the global one.
     unsigned FirstDeclIndex;
   };
-  using FileDeclIDsTy = llvm::DenseMap<FileID, DeclIDInFileInfo *>;
+  using FileDeclIDsTy =
+      llvm::DenseMap<FileID, std::unique_ptr<DeclIDInFileInfo>>;
 
   /// Map from file SLocEntries to info about the file-level declarations
   /// that it contains.

diff  --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp
index 31d004f6c946..03121515a5cc 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -2898,8 +2898,10 @@ void ASTWriter::WriteTypeDeclOffsets() {
 void ASTWriter::WriteFileDeclIDsMap() {
   using namespace llvm;
 
-  SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
-      FileDeclIDs.begin(), FileDeclIDs.end());
+  SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs;
+  SortedFileDeclIDs.reserve(FileDeclIDs.size());
+  for (const auto &P : FileDeclIDs)
+    SortedFileDeclIDs.push_back(std::make_pair(P.first, P.second.get()));
   llvm::sort(SortedFileDeclIDs, llvm::less_first());
 
   // Join the vectors of DeclIDs from all files.
@@ -4297,9 +4299,7 @@ ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
   }
 }
 
-ASTWriter::~ASTWriter() {
-  llvm::DeleteContainerSeconds(FileDeclIDs);
-}
+ASTWriter::~ASTWriter() = default;
 
 const LangOptions &ASTWriter::getLangOpts() const {
   assert(WritingAST && "can't determine lang opts when not writing AST");
@@ -5366,9 +5366,9 @@ void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
     return;
   assert(SM.getSLocEntry(FID).isFile());
 
-  DeclIDInFileInfo *&Info = FileDeclIDs[FID];
+  std::unique_ptr<DeclIDInFileInfo> &Info = FileDeclIDs[FID];
   if (!Info)
-    Info = new DeclIDInFileInfo();
+    Info = std::make_unique<DeclIDInFileInfo>();
 
   std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
   LocDeclIDsTy &Decls = Info->DeclIDs;


        


More information about the cfe-commits mailing list