Index: clang/include/clang/AST/VTableBuilder.h =================================================================== --- clang/include/clang/AST/VTableBuilder.h +++ clang/include/clang/AST/VTableBuilder.h @@ -417,14 +417,12 @@ } }; -typedef SmallVector VPtrInfoVector; +typedef SmallVector, 2> VPtrInfoVector; /// All virtual base related information about a given record decl. Includes /// information on all virtual base tables and the path components that are used /// to mangle them. struct VirtualBaseInfo { - ~VirtualBaseInfo() { llvm::DeleteContainerPointers(VBPtrPaths); } - /// A map from virtual base to vbtable index for doing a conversion from the /// the derived class to the a base. llvm::DenseMap VBTableIndices; @@ -477,8 +475,8 @@ MethodVFTableLocationsTy; MethodVFTableLocationsTy MethodVFTableLocations; - typedef llvm::DenseMap - VFPtrLocationsMapTy; + typedef llvm::DenseMap + VFPtrLocationsMapTy; VFPtrLocationsMapTy VFPtrLocations; typedef std::pair VFTableIdTy; Index: clang/lib/AST/VTableBuilder.cpp =================================================================== --- clang/lib/AST/VTableBuilder.cpp +++ 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(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 &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(*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, 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 &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 FullPaths; - for (VPtrInfo *Info : Paths) { + for (const std::unique_ptr& 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 &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 & Index: clang/lib/CodeGen/MicrosoftCXXABI.cpp =================================================================== --- clang/lib/CodeGen/MicrosoftCXXABI.cpp +++ clang/lib/CodeGen/MicrosoftCXXABI.cpp @@ -96,7 +96,7 @@ const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); const VBTableGlobals &VBGlobals = enumerateVBTables(RD); - for (const VPtrInfo *VBT : *VBGlobals.VBTables) { + for (const std::unique_ptr &VBT : *VBGlobals.VBTables) { const ASTRecordLayout &SubobjectLayout = Context.getASTRecordLayout(VBT->IntroducingObject); CharUnits Offs = VBT->NonVirtualOffset; @@ -122,7 +122,7 @@ void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) override; llvm::GlobalVariable *getMSCompleteObjectLocator(const CXXRecordDecl *RD, - const VPtrInfo *Info); + const VPtrInfo &Info); llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override; CatchTypeInfo @@ -254,7 +254,7 @@ CXXDtorType Type, bool ForVirtualBase, bool Delegating, Address This) override; - void emitVTableTypeMetadata(VPtrInfo *Info, const CXXRecordDecl *RD, + void emitVTableTypeMetadata(const VPtrInfo &Info, const CXXRecordDecl *RD, llvm::GlobalVariable *VTable); void emitVTableDefinitions(CodeGenVTables &CGVT, @@ -1214,7 +1214,7 @@ const VBTableGlobals &VBGlobals = enumerateVBTables(RD); for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) { - const VPtrInfo *VBT = (*VBGlobals.VBTables)[I]; + const std::unique_ptr &VBT = (*VBGlobals.VBTables)[I]; llvm::GlobalVariable *GV = VBGlobals.Globals[I]; const ASTRecordLayout &SubobjectLayout = Context.getASTRecordLayout(VBT->IntroducingObject); @@ -1509,7 +1509,7 @@ getFromDtorType(Type)); } -void MicrosoftCXXABI::emitVTableTypeMetadata(VPtrInfo *Info, +void MicrosoftCXXABI::emitVTableTypeMetadata(const VPtrInfo &Info, const CXXRecordDecl *RD, llvm::GlobalVariable *VTable) { if (!CGM.getCodeGenOpts().PrepareForLTO) @@ -1524,20 +1524,20 @@ getContext().getTargetInfo().getPointerWidth(0)) : CharUnits::Zero(); - if (Info->PathToIntroducingObject.empty()) { + if (Info.PathToIntroducingObject.empty()) { CGM.AddVTableTypeMetadata(VTable, AddressPoint, RD); return; } // Add a bitset entry for the least derived base belonging to this vftable. CGM.AddVTableTypeMetadata(VTable, AddressPoint, - Info->PathToIntroducingObject.back()); + Info.PathToIntroducingObject.back()); // Add a bitset entry for each derived class that is laid out at the same // offset as the least derived base. - for (unsigned I = Info->PathToIntroducingObject.size() - 1; I != 0; --I) { - const CXXRecordDecl *DerivedRD = Info->PathToIntroducingObject[I - 1]; - const CXXRecordDecl *BaseRD = Info->PathToIntroducingObject[I]; + for (unsigned I = Info.PathToIntroducingObject.size() - 1; I != 0; --I) { + const CXXRecordDecl *DerivedRD = Info.PathToIntroducingObject[I - 1]; + const CXXRecordDecl *BaseRD = Info.PathToIntroducingObject[I]; const ASTRecordLayout &Layout = getContext().getASTRecordLayout(DerivedRD); @@ -1553,7 +1553,7 @@ } // Finally do the same for the most derived class. - if (Info->FullOffsetInMDC.isZero()) + if (Info.FullOffsetInMDC.isZero()) CGM.AddVTableTypeMetadata(VTable, AddressPoint, RD); } @@ -1562,7 +1562,7 @@ MicrosoftVTableContext &VFTContext = CGM.getMicrosoftVTableContext(); const VPtrInfoVector &VFPtrs = VFTContext.getVFPtrOffsets(RD); - for (VPtrInfo *Info : VFPtrs) { + for (const std::unique_ptr& Info : VFPtrs) { llvm::GlobalVariable *VTable = getAddrOfVTable(RD, Info->FullOffsetInMDC); if (VTable->hasInitializer()) continue; @@ -1573,13 +1573,13 @@ llvm::Constant *RTTI = nullptr; if (any_of(VTLayout.vtable_components(), [](const VTableComponent &VTC) { return VTC.isRTTIKind(); })) - RTTI = getMSCompleteObjectLocator(RD, Info); + RTTI = getMSCompleteObjectLocator(RD, *Info); llvm::Constant *Init = CGVT.CreateVTableInitializer(VTLayout, RTTI); VTable->setInitializer(Init); - emitVTableTypeMetadata(Info, RD, VTable); + emitVTableTypeMetadata(*Info, RD, VTable); } } @@ -1600,10 +1600,10 @@ } static void mangleVFTableName(MicrosoftMangleContext &MangleContext, - const CXXRecordDecl *RD, const VPtrInfo *VFPtr, + const CXXRecordDecl *RD, const VPtrInfo &VFPtr, SmallString<256> &Name) { llvm::raw_svector_ostream Out(Name); - MangleContext.mangleCXXVFTable(RD, VFPtr->MangledPath, Out); + MangleContext.mangleCXXVFTable(RD, VFPtr.MangledPath, Out); } llvm::Constant * @@ -1650,25 +1650,25 @@ llvm::StringSet<> ObservedMangledNames; for (size_t J = 0, F = VFPtrs.size(); J != F; ++J) { SmallString<256> Name; - mangleVFTableName(getMangleContext(), RD, VFPtrs[J], Name); + mangleVFTableName(getMangleContext(), RD, *VFPtrs[J], Name); if (!ObservedMangledNames.insert(Name.str()).second) llvm_unreachable("Already saw this mangling before?"); } #endif } - VPtrInfo *const *VFPtrI = - std::find_if(VFPtrs.begin(), VFPtrs.end(), [&](VPtrInfo *VPI) { + const std::unique_ptr *VFPtrI = std::find_if( + VFPtrs.begin(), VFPtrs.end(), [&](const std::unique_ptr& VPI) { return VPI->FullOffsetInMDC == VPtrOffset; }); if (VFPtrI == VFPtrs.end()) { VFTablesMap[ID] = nullptr; return nullptr; } - VPtrInfo *VFPtr = *VFPtrI; + const std::unique_ptr &VFPtr = *VFPtrI; SmallString<256> VFTableName; - mangleVFTableName(getMangleContext(), RD, VFPtr, VFTableName); + mangleVFTableName(getMangleContext(), RD, *VFPtr, VFTableName); // Classes marked __declspec(dllimport) need vftables generated on the // import-side in order to support features like constexpr. No other @@ -1964,7 +1964,7 @@ void MicrosoftCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) { const VBTableGlobals &VBGlobals = enumerateVBTables(RD); for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) { - const VPtrInfo *VBT = (*VBGlobals.VBTables)[I]; + const std::unique_ptr& VBT = (*VBGlobals.VBTables)[I]; llvm::GlobalVariable *GV = VBGlobals.Globals[I]; if (GV->isDeclaration()) emitVBTableDefinition(*VBT, RD, GV); @@ -3426,7 +3426,7 @@ llvm::GlobalVariable * getBaseClassArray(SmallVectorImpl &Classes); llvm::GlobalVariable *getClassHierarchyDescriptor(); - llvm::GlobalVariable *getCompleteObjectLocator(const VPtrInfo *Info); + llvm::GlobalVariable *getCompleteObjectLocator(const VPtrInfo &Info); CodeGenModule &CGM; ASTContext &Context; @@ -3608,11 +3608,11 @@ } llvm::GlobalVariable * -MSRTTIBuilder::getCompleteObjectLocator(const VPtrInfo *Info) { +MSRTTIBuilder::getCompleteObjectLocator(const VPtrInfo &Info) { SmallString<256> MangledName; { llvm::raw_svector_ostream Out(MangledName); - ABI.getMangleContext().mangleCXXRTTICompleteObjectLocator(RD, Info->MangledPath, Out); + ABI.getMangleContext().mangleCXXRTTICompleteObjectLocator(RD, Info.MangledPath, Out); } // Check to see if we've already computed this complete object locator. @@ -3620,15 +3620,15 @@ return COL; // Compute the fields of the complete object locator. - int OffsetToTop = Info->FullOffsetInMDC.getQuantity(); + int OffsetToTop = Info.FullOffsetInMDC.getQuantity(); int VFPtrOffset = 0; // The offset includes the vtordisp if one exists. - if (const CXXRecordDecl *VBase = Info->getVBaseWithVPtr()) + if (const CXXRecordDecl *VBase = Info.getVBaseWithVPtr()) if (Context.getASTRecordLayout(RD) .getVBaseOffsetsMap() .find(VBase) ->second.hasVtorDisp()) - VFPtrOffset = Info->NonVirtualOffset.getQuantity() + 4; + VFPtrOffset = Info.NonVirtualOffset.getQuantity() + 4; // Forward-declare the complete object locator. llvm::StructType *Type = ABI.getCompleteObjectLocatorType(); @@ -3756,7 +3756,7 @@ /// \brief Gets or a creates a Microsoft CompleteObjectLocator. llvm::GlobalVariable * MicrosoftCXXABI::getMSCompleteObjectLocator(const CXXRecordDecl *RD, - const VPtrInfo *Info) { + const VPtrInfo &Info) { return MSRTTIBuilder(*this, RD).getCompleteObjectLocator(Info); }