Skip to content
This repository has been archived by the owner on Apr 23, 2020. It is now read-only.

Commit

Permalink
Use unique_ptr for VPtrLocationsMap and VPtrInfoVector.
Browse files Browse the repository at this point in the history
Reviewers: timshen

Subscribers: cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@283770 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Justin Lebar committed Oct 10, 2016
1 parent 7b7c816 commit dd0969a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 63 deletions.
8 changes: 3 additions & 5 deletions include/clang/AST/VTableBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,12 @@ struct VPtrInfo {
}
};

typedef SmallVector<VPtrInfo *, 2> VPtrInfoVector;
typedef SmallVector<std::unique_ptr<VPtrInfo>, 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<const CXXRecordDecl *, unsigned> VBTableIndices;
Expand Down Expand Up @@ -477,8 +475,8 @@ class MicrosoftVTableContext : public VTableContextBase {
MethodVFTableLocationsTy;
MethodVFTableLocationsTy MethodVFTableLocations;

typedef llvm::DenseMap<const CXXRecordDecl *, VPtrInfoVector *>
VFPtrLocationsMapTy;
typedef llvm::DenseMap<const CXXRecordDecl *, VPtrInfoVector>
VFPtrLocationsMapTy;
VFPtrLocationsMapTy VFPtrLocations;

typedef std::pair<const CXXRecordDecl *, CharUnits> VFTableIdTy;
Expand Down
61 changes: 32 additions & 29 deletions lib/AST/VTableBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2536,12 +2536,12 @@ class VFTableBuilder {

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
Expand Down Expand Up @@ -3276,7 +3276,7 @@ void MicrosoftVTableContext::computeVTablePaths(bool ForVBTables,

// 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.
Expand All @@ -3292,14 +3292,14 @@ void MicrosoftVTableContext::computeVTablePaths(bool ForVBTables,
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.
Expand All @@ -3326,7 +3326,7 @@ void MicrosoftVTableContext::computeVTablePaths(bool ForVBTables,
if (const CXXRecordDecl *VB = P->getVBaseWithVPtr())
P->FullOffsetInMDC += Layout.getVBaseClassOffset(VB);

Paths.push_back(P);
Paths.push_back(std::move(P));
}

if (B.isVirtual())
Expand All @@ -3345,10 +3345,10 @@ void MicrosoftVTableContext::computeVTablePaths(bool ForVBTables,
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;
Expand All @@ -3361,19 +3361,22 @@ static bool rebucketPaths(VPtrInfoVector &Paths) {
// 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;) {
// Scan forward to find the end of the bucket.
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) {
Expand All @@ -3386,9 +3389,6 @@ static bool rebucketPaths(VPtrInfoVector &Paths) {
}

MicrosoftVTableContext::~MicrosoftVTableContext() {
for (auto &P : VFPtrLocations)
llvm::DeleteContainerPointers(*P.second);
llvm::DeleteContainerSeconds(VFPtrLocations);
llvm::DeleteContainerSeconds(VFTableLayouts);
llvm::DeleteContainerSeconds(VBaseInfo);
}
Expand Down Expand Up @@ -3475,7 +3475,8 @@ static CharUnits getOffsetOfFullPath(ASTContext &Context,
// 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())
Expand All @@ -3495,7 +3496,7 @@ static const FullPathTy *selectBestPath(ASTContext &Context,
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 =
Expand Down Expand Up @@ -3550,7 +3551,7 @@ static void computeFullPathsForVFTables(ASTContext &Context,
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,
Expand All @@ -3559,7 +3560,7 @@ static void computeFullPathsForVFTables(ASTContext &Context,
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();
Expand All @@ -3576,14 +3577,16 @@ void MicrosoftVTableContext::computeVTableRelatedInformation(

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);
Expand Down Expand Up @@ -3723,7 +3726,7 @@ MicrosoftVTableContext::getVFPtrOffsets(const CXXRecordDecl *RD) {
computeVTableRelatedInformation(RD);

assert(VFPtrLocations.count(RD) && "Couldn't find vfptr locations");
return *VFPtrLocations[RD];
return VFPtrLocations[RD];
}

const VTableLayout &
Expand Down
Loading

0 comments on commit dd0969a

Please sign in to comment.