Use unique_ptr for VPtrLocationsMap and VPtrInfoVector.

Reviewers: timshen

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D25422

llvm-svn: 283770
diff --git a/clang/lib/AST/VTableBuilder.cpp b/clang/lib/AST/VTableBuilder.cpp
index 12a96ad..81207f5 100644
--- a/clang/lib/AST/VTableBuilder.cpp
+++ b/clang/lib/AST/VTableBuilder.cpp
@@ -2536,12 +2536,12 @@
 
 public:
   VFTableBuilder(MicrosoftVTableContext &VTables,
-                 const CXXRecordDecl *MostDerivedClass, const VPtrInfo *Which)
+                 const CXXRecordDecl *MostDerivedClass, const VPtrInfo &Which)
       : VTables(VTables),
         Context(MostDerivedClass->getASTContext()),
         MostDerivedClass(MostDerivedClass),
         MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)),
-        WhichVFPtr(*Which),
+        WhichVFPtr(Which),
         Overriders(MostDerivedClass, CharUnits(), MostDerivedClass) {
     // Provide the RTTI component if RTTIData is enabled. If the vftable would
     // be available externally, we should not provide the RTTI componenent. It
@@ -3276,7 +3276,7 @@
 
   // Base case: this subobject has its own vptr.
   if (ForVBTables ? Layout.hasOwnVBPtr() : Layout.hasOwnVFPtr())
-    Paths.push_back(new VPtrInfo(RD));
+    Paths.push_back(llvm::make_unique<VPtrInfo>(RD));
 
   // Recursive case: get all the vbtables from our bases and remove anything
   // that shares a virtual base.
@@ -3292,14 +3292,14 @@
     const VPtrInfoVector &BasePaths =
         ForVBTables ? enumerateVBTables(Base) : getVFPtrOffsets(Base);
 
-    for (VPtrInfo *BaseInfo : BasePaths) {
+    for (const std::unique_ptr<VPtrInfo> &BaseInfo : BasePaths) {
       // Don't include the path if it goes through a virtual base that we've
       // already included.
       if (setsIntersect(VBasesSeen, BaseInfo->ContainingVBases))
         continue;
 
       // Copy the path and adjust it as necessary.
-      VPtrInfo *P = new VPtrInfo(*BaseInfo);
+      auto P = llvm::make_unique<VPtrInfo>(*BaseInfo);
 
       // We mangle Base into the path if the path would've been ambiguous and it
       // wasn't already extended with Base.
@@ -3326,7 +3326,7 @@
       if (const CXXRecordDecl *VB = P->getVBaseWithVPtr())
         P->FullOffsetInMDC += Layout.getVBaseClassOffset(VB);
 
-      Paths.push_back(P);
+      Paths.push_back(std::move(P));
     }
 
     if (B.isVirtual())
@@ -3345,10 +3345,10 @@
     Changed = rebucketPaths(Paths);
 }
 
-static bool extendPath(VPtrInfo *P) {
-  if (P->NextBaseToMangle) {
-    P->MangledPath.push_back(P->NextBaseToMangle);
-    P->NextBaseToMangle = nullptr;// Prevent the path from being extended twice.
+static bool extendPath(VPtrInfo &P) {
+  if (P.NextBaseToMangle) {
+    P.MangledPath.push_back(P.NextBaseToMangle);
+    P.NextBaseToMangle = nullptr;// Prevent the path from being extended twice.
     return true;
   }
   return false;
@@ -3361,10 +3361,12 @@
   // sorted vector to implement a multiset to form the buckets.  Note that the
   // ordering is based on pointers, but it doesn't change our output order.  The
   // current algorithm is designed to match MSVC 2012's names.
-  VPtrInfoVector PathsSorted(Paths);
+  llvm::SmallVector<std::reference_wrapper<VPtrInfo>, 2> PathsSorted(
+      llvm::make_pointee_iterator(Paths.begin()),
+      llvm::make_pointee_iterator(Paths.end()));
   std::sort(PathsSorted.begin(), PathsSorted.end(),
-            [](const VPtrInfo *LHS, const VPtrInfo *RHS) {
-    return LHS->MangledPath < RHS->MangledPath;
+            [](const VPtrInfo &LHS, const VPtrInfo &RHS) {
+    return LHS.MangledPath < RHS.MangledPath;
   });
   bool Changed = false;
   for (size_t I = 0, E = PathsSorted.size(); I != E;) {
@@ -3372,8 +3374,9 @@
     size_t BucketStart = I;
     do {
       ++I;
-    } while (I != E && PathsSorted[BucketStart]->MangledPath ==
-                           PathsSorted[I]->MangledPath);
+    } while (I != E &&
+             PathsSorted[BucketStart].get().MangledPath ==
+                 PathsSorted[I].get().MangledPath);
 
     // If this bucket has multiple paths, extend them all.
     if (I - BucketStart > 1) {
@@ -3386,9 +3389,6 @@
 }
 
 MicrosoftVTableContext::~MicrosoftVTableContext() {
-  for (auto &P : VFPtrLocations) 
-    llvm::DeleteContainerPointers(*P.second);
-  llvm::DeleteContainerSeconds(VFPtrLocations);
   llvm::DeleteContainerSeconds(VFTableLayouts);
   llvm::DeleteContainerSeconds(VBaseInfo);
 }
@@ -3475,7 +3475,8 @@
 // two paths introduce overrides which the other path doesn't contain, issue a
 // diagnostic.
 static const FullPathTy *selectBestPath(ASTContext &Context,
-                                        const CXXRecordDecl *RD, VPtrInfo *Info,
+                                        const CXXRecordDecl *RD,
+                                        const VPtrInfo &Info,
                                         std::list<FullPathTy> &FullPaths) {
   // Handle some easy cases first.
   if (FullPaths.empty())
@@ -3495,7 +3496,7 @@
     CharUnits BaseOffset =
         getOffsetOfFullPath(Context, TopLevelRD, SpecificPath);
     FinalOverriders Overriders(TopLevelRD, CharUnits::Zero(), TopLevelRD);
-    for (const CXXMethodDecl *MD : Info->IntroducingObject->methods()) {
+    for (const CXXMethodDecl *MD : Info.IntroducingObject->methods()) {
       if (!MD->isVirtual())
         continue;
       FinalOverriders::OverriderInfo OI =
@@ -3550,7 +3551,7 @@
   const ASTRecordLayout &MostDerivedLayout = Context.getASTRecordLayout(RD);
   FullPathTy FullPath;
   std::list<FullPathTy> FullPaths;
-  for (VPtrInfo *Info : Paths) {
+  for (const std::unique_ptr<VPtrInfo>& Info : Paths) {
     findPathsToSubobject(
         Context, MostDerivedLayout, RD, CharUnits::Zero(),
         BaseSubobject(Info->IntroducingObject, Info->FullOffsetInMDC), FullPath,
@@ -3559,7 +3560,7 @@
     removeRedundantPaths(FullPaths);
     Info->PathToIntroducingObject.clear();
     if (const FullPathTy *BestPath =
-            selectBestPath(Context, RD, Info, FullPaths))
+            selectBestPath(Context, RD, *Info, FullPaths))
       for (const BaseSubobject &BSO : *BestPath)
         Info->PathToIntroducingObject.push_back(BSO.getBase());
     FullPaths.clear();
@@ -3576,14 +3577,16 @@
 
   const VTableLayout::AddressPointsMapTy EmptyAddressPointsMap;
 
-  VPtrInfoVector *VFPtrs = new VPtrInfoVector();
-  computeVTablePaths(/*ForVBTables=*/false, RD, *VFPtrs);
-  computeFullPathsForVFTables(Context, RD, *VFPtrs);
-  VFPtrLocations[RD] = VFPtrs;
+  {
+    VPtrInfoVector VFPtrs;
+    computeVTablePaths(/*ForVBTables=*/false, RD, VFPtrs);
+    computeFullPathsForVFTables(Context, RD, VFPtrs);
+    VFPtrLocations[RD] = std::move(VFPtrs);
+  }
 
   MethodVFTableLocationsTy NewMethodLocations;
-  for (const VPtrInfo *VFPtr : *VFPtrs) {
-    VFTableBuilder Builder(*this, RD, VFPtr);
+  for (const std::unique_ptr<VPtrInfo> &VFPtr : VFPtrLocations[RD]) {
+    VFTableBuilder Builder(*this, RD, *VFPtr);
 
     VFTableIdTy id(RD, VFPtr->FullOffsetInMDC);
     assert(VFTableLayouts.count(id) == 0);
@@ -3723,7 +3726,7 @@
   computeVTableRelatedInformation(RD);
 
   assert(VFPtrLocations.count(RD) && "Couldn't find vfptr locations");
-  return *VFPtrLocations[RD];
+  return VFPtrLocations[RD];
 }
 
 const VTableLayout &