From: David Blaikie Date: Mon, 21 Apr 2014 21:40:16 +0000 (+0000) Subject: Use unique_ptr to manage ownership of GCOVFunctions, Blocks, and Edges. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=e1c0863d74f7995dd0890e35b585526457d118d5;p=oota-llvm.git Use unique_ptr to manage ownership of GCOVFunctions, Blocks, and Edges. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206796 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Support/GCOV.h b/include/llvm/Support/GCOV.h index 902f2dbc83b..ae7e4a969a3 100644 --- a/include/llvm/Support/GCOV.h +++ b/include/llvm/Support/GCOV.h @@ -232,7 +232,6 @@ class GCOVFile { public: GCOVFile() : GCNOInitialized(false), Checksum(0), Functions(), RunCount(0), ProgramCount(0) {} - ~GCOVFile(); bool readGCNO(GCOVBuffer &Buffer); bool readGCDA(GCOVBuffer &Buffer); uint32_t getChecksum() const { return Checksum; } @@ -242,27 +241,27 @@ private: bool GCNOInitialized; GCOV::GCOVVersion Version; uint32_t Checksum; - SmallVector Functions; + SmallVector, 16> Functions; uint32_t RunCount; uint32_t ProgramCount; }; /// GCOVEdge - Collects edge information. struct GCOVEdge { - GCOVEdge(GCOVBlock *S, GCOVBlock *D): Src(S), Dst(D), Count(0) {} + GCOVEdge(GCOVBlock &S, GCOVBlock &D) : Src(S), Dst(D), Count(0) {} - GCOVBlock *Src; - GCOVBlock *Dst; + GCOVBlock &Src; + GCOVBlock &Dst; uint64_t Count; }; /// GCOVFunction - Collects function information. class GCOVFunction { public: - typedef SmallVectorImpl::const_iterator BlockIterator; + typedef SmallVectorImpl>::const_iterator + BlockIterator; GCOVFunction(GCOVFile &P) : Parent(P), Ident(0), LineNumber(0) {} - ~GCOVFunction(); bool readGCNO(GCOVBuffer &Buffer, GCOV::GCOVVersion Version); bool readGCDA(GCOVBuffer &Buffer, GCOV::GCOVVersion Version); StringRef getName() const { return Name; } @@ -283,8 +282,8 @@ private: uint32_t LineNumber; StringRef Name; StringRef Filename; - SmallVector Blocks; - SmallVector Edges; + SmallVector, 16> Blocks; + SmallVector, 16> Edges; }; /// GCOVBlock - Collects block information. @@ -298,7 +297,7 @@ class GCOVBlock { struct SortDstEdgesFunctor { bool operator()(const GCOVEdge *E1, const GCOVEdge *E2) { - return E1->Dst->Number < E2->Dst->Number; + return E1->Dst.Number < E2->Dst.Number; } }; public: @@ -314,13 +313,13 @@ public: uint64_t getCount() const { return Counter; } void addSrcEdge(GCOVEdge *Edge) { - assert(Edge->Dst == this); // up to caller to ensure edge is valid + assert(&Edge->Dst == this); // up to caller to ensure edge is valid SrcEdges.push_back(Edge); } void addDstEdge(GCOVEdge *Edge) { - assert(Edge->Src == this); // up to caller to ensure edge is valid + assert(&Edge->Src == this); // up to caller to ensure edge is valid // Check if adding this edge causes list to become unsorted. - if (DstEdges.size() && DstEdges.back()->Dst->Number > Edge->Dst->Number) + if (DstEdges.size() && DstEdges.back()->Dst.Number > Edge->Dst.Number) DstEdgesAreSorted = false; DstEdges.push_back(Edge); } diff --git a/lib/IR/GCOV.cpp b/lib/IR/GCOV.cpp index f69bdc4f8cc..0e344ddfead 100644 --- a/lib/IR/GCOV.cpp +++ b/lib/IR/GCOV.cpp @@ -26,11 +26,6 @@ using namespace llvm; //===----------------------------------------------------------------------===// // GCOVFile implementation. -/// ~GCOVFile - Delete GCOVFile and its content. -GCOVFile::~GCOVFile() { - DeleteContainerPointers(Functions); -} - /// readGCNO - Read GCNO buffer. bool GCOVFile::readGCNO(GCOVBuffer &Buffer) { if (!Buffer.readGCNOFormat()) return false; @@ -39,10 +34,10 @@ bool GCOVFile::readGCNO(GCOVBuffer &Buffer) { if (!Buffer.readInt(Checksum)) return false; while (true) { if (!Buffer.readFunctionTag()) break; - GCOVFunction *GFun = new GCOVFunction(*this); + auto GFun = make_unique(*this); if (!GFun->readGCNO(Buffer, Version)) return false; - Functions.push_back(GFun); + Functions.push_back(std::move(GFun)); } GCNOInitialized = true; @@ -97,17 +92,15 @@ bool GCOVFile::readGCDA(GCOVBuffer &Buffer) { /// dump - Dump GCOVFile content to dbgs() for debugging purposes. void GCOVFile::dump() const { - for (SmallVectorImpl::const_iterator I = Functions.begin(), - E = Functions.end(); I != E; ++I) - (*I)->dump(); + for (const auto &FPtr : Functions) + FPtr->dump(); } /// collectLineCounts - Collect line counts. This must be used after /// reading .gcno and .gcda files. void GCOVFile::collectLineCounts(FileInfo &FI) { - for (SmallVectorImpl::iterator I = Functions.begin(), - E = Functions.end(); I != E; ++I) - (*I)->collectLineCounts(FI); + for (const auto &FPtr : Functions) + FPtr->collectLineCounts(FI); FI.setRunCount(RunCount); FI.setProgramCount(ProgramCount); } @@ -115,12 +108,6 @@ void GCOVFile::collectLineCounts(FileInfo &FI) { //===----------------------------------------------------------------------===// // GCOVFunction implementation. -/// ~GCOVFunction - Delete GCOVFunction and its content. -GCOVFunction::~GCOVFunction() { - DeleteContainerPointers(Blocks); - DeleteContainerPointers(Edges); -} - /// readGCNO - Read a function from the GCNO buffer. Return false if an error /// occurs. bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { @@ -150,7 +137,7 @@ bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { if (!Buff.readInt(BlockCount)) return false; for (uint32_t i = 0, e = BlockCount; i != e; ++i) { if (!Buff.readInt(Dummy)) return false; // Block flags; - Blocks.push_back(new GCOVBlock(*this, i)); + Blocks.push_back(make_unique(*this, i)); } // read edges. @@ -168,8 +155,8 @@ bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { for (uint32_t i = 0, e = EdgeCount; i != e; ++i) { uint32_t Dst; if (!Buff.readInt(Dst)) return false; - GCOVEdge *Edge = new GCOVEdge(Blocks[BlockNo], Blocks[Dst]); - Edges.push_back(Edge); + Edges.push_back(make_unique(*Blocks[BlockNo], *Blocks[Dst])); + GCOVEdge *Edge = Edges.back().get(); Blocks[BlockNo]->addDstEdge(Edge); Blocks[Dst]->addSrcEdge(Edge); if (!Buff.readInt(Dummy)) return false; // Edge flag @@ -188,7 +175,7 @@ bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { << ").\n"; return false; } - GCOVBlock *Block = Blocks[BlockNo]; + GCOVBlock &Block = *Blocks[BlockNo]; if (!Buff.readInt(Dummy)) return false; // flag while (Buff.getCursor() != (EndPos - 4)) { StringRef F; @@ -203,7 +190,7 @@ bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { uint32_t Line; if (!Buff.readInt(Line)) return false; if (!Line) break; - Block->addLine(Line); + Block.addLine(Line); } } if (!Buff.readInt(Dummy)) return false; // flag @@ -300,9 +287,8 @@ uint64_t GCOVFunction::getExitCount() const { /// dump - Dump GCOVFunction content to dbgs() for debugging purposes. void GCOVFunction::dump() const { dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n"; - for (SmallVectorImpl::const_iterator I = Blocks.begin(), - E = Blocks.end(); I != E; ++I) - (*I)->dump(); + for (const auto &Block : Blocks) + Block->dump(); } /// collectLineCounts - Collect line counts. This must be used after @@ -313,9 +299,8 @@ void GCOVFunction::collectLineCounts(FileInfo &FI) { if (LineNumber == 0) return; - for (SmallVectorImpl::iterator I = Blocks.begin(), - E = Blocks.end(); I != E; ++I) - (*I)->collectLineCounts(FI); + for (const auto &Block : Blocks) + Block->collectLineCounts(FI); FI.addFunctionLine(Filename, LineNumber, this); } @@ -335,8 +320,8 @@ void GCOVBlock::addCount(size_t DstEdgeNo, uint64_t N) { assert(DstEdgeNo < DstEdges.size()); // up to caller to ensure EdgeNo is valid DstEdges[DstEdgeNo]->Count = N; Counter += N; - if (!DstEdges[DstEdgeNo]->Dst->getNumDstEdges()) - DstEdges[DstEdgeNo]->Dst->Counter += N; + if (!DstEdges[DstEdgeNo]->Dst.getNumDstEdges()) + DstEdges[DstEdgeNo]->Dst.Counter += N; } /// sortDstEdges - Sort destination edges by block number, nop if already @@ -363,7 +348,7 @@ void GCOVBlock::dump() const { dbgs() << "\tSource Edges : "; for (EdgeIterator I = SrcEdges.begin(), E = SrcEdges.end(); I != E; ++I) { const GCOVEdge *Edge = *I; - dbgs() << Edge->Src->Number << " (" << Edge->Count << "), "; + dbgs() << Edge->Src.Number << " (" << Edge->Count << "), "; } dbgs() << "\n"; } @@ -371,7 +356,7 @@ void GCOVBlock::dump() const { dbgs() << "\tDestination Edges : "; for (EdgeIterator I = DstEdges.begin(), E = DstEdges.end(); I != E; ++I) { const GCOVEdge *Edge = *I; - dbgs() << Edge->Dst->Number << " (" << Edge->Count << "), "; + dbgs() << Edge->Dst.Number << " (" << Edge->Count << "), "; } dbgs() << "\n"; } @@ -617,8 +602,8 @@ void FileInfo::printFunctionSummary(raw_fd_ostream &OS, uint32_t BlocksExec = 0; for (GCOVFunction::BlockIterator I = Func->block_begin(), E = Func->block_end(); I != E; ++I) { - const GCOVBlock *Block = *I; - if (Block->getNumDstEdges() && Block->getCount()) + const GCOVBlock &Block = **I; + if (Block.getNumDstEdges() && Block.getCount()) ++BlocksExec; }