Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit e1c0863

Browse files
committed
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
1 parent 2df01c6 commit e1c0863

File tree

2 files changed

+33
-49
lines changed

2 files changed

+33
-49
lines changed

include/llvm/Support/GCOV.h

+12-13
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ class GCOVFile {
232232
public:
233233
GCOVFile() : GCNOInitialized(false), Checksum(0), Functions(), RunCount(0),
234234
ProgramCount(0) {}
235-
~GCOVFile();
236235
bool readGCNO(GCOVBuffer &Buffer);
237236
bool readGCDA(GCOVBuffer &Buffer);
238237
uint32_t getChecksum() const { return Checksum; }
@@ -242,27 +241,27 @@ class GCOVFile {
242241
bool GCNOInitialized;
243242
GCOV::GCOVVersion Version;
244243
uint32_t Checksum;
245-
SmallVector<GCOVFunction *, 16> Functions;
244+
SmallVector<std::unique_ptr<GCOVFunction>, 16> Functions;
246245
uint32_t RunCount;
247246
uint32_t ProgramCount;
248247
};
249248

250249
/// GCOVEdge - Collects edge information.
251250
struct GCOVEdge {
252-
GCOVEdge(GCOVBlock *S, GCOVBlock *D): Src(S), Dst(D), Count(0) {}
251+
GCOVEdge(GCOVBlock &S, GCOVBlock &D) : Src(S), Dst(D), Count(0) {}
253252

254-
GCOVBlock *Src;
255-
GCOVBlock *Dst;
253+
GCOVBlock &Src;
254+
GCOVBlock &Dst;
256255
uint64_t Count;
257256
};
258257

259258
/// GCOVFunction - Collects function information.
260259
class GCOVFunction {
261260
public:
262-
typedef SmallVectorImpl<GCOVBlock *>::const_iterator BlockIterator;
261+
typedef SmallVectorImpl<std::unique_ptr<GCOVBlock>>::const_iterator
262+
BlockIterator;
263263

264264
GCOVFunction(GCOVFile &P) : Parent(P), Ident(0), LineNumber(0) {}
265-
~GCOVFunction();
266265
bool readGCNO(GCOVBuffer &Buffer, GCOV::GCOVVersion Version);
267266
bool readGCDA(GCOVBuffer &Buffer, GCOV::GCOVVersion Version);
268267
StringRef getName() const { return Name; }
@@ -283,8 +282,8 @@ class GCOVFunction {
283282
uint32_t LineNumber;
284283
StringRef Name;
285284
StringRef Filename;
286-
SmallVector<GCOVBlock *, 16> Blocks;
287-
SmallVector<GCOVEdge *, 16> Edges;
285+
SmallVector<std::unique_ptr<GCOVBlock>, 16> Blocks;
286+
SmallVector<std::unique_ptr<GCOVEdge>, 16> Edges;
288287
};
289288

290289
/// GCOVBlock - Collects block information.
@@ -298,7 +297,7 @@ class GCOVBlock {
298297

299298
struct SortDstEdgesFunctor {
300299
bool operator()(const GCOVEdge *E1, const GCOVEdge *E2) {
301-
return E1->Dst->Number < E2->Dst->Number;
300+
return E1->Dst.Number < E2->Dst.Number;
302301
}
303302
};
304303
public:
@@ -314,13 +313,13 @@ class GCOVBlock {
314313
uint64_t getCount() const { return Counter; }
315314

316315
void addSrcEdge(GCOVEdge *Edge) {
317-
assert(Edge->Dst == this); // up to caller to ensure edge is valid
316+
assert(&Edge->Dst == this); // up to caller to ensure edge is valid
318317
SrcEdges.push_back(Edge);
319318
}
320319
void addDstEdge(GCOVEdge *Edge) {
321-
assert(Edge->Src == this); // up to caller to ensure edge is valid
320+
assert(&Edge->Src == this); // up to caller to ensure edge is valid
322321
// Check if adding this edge causes list to become unsorted.
323-
if (DstEdges.size() && DstEdges.back()->Dst->Number > Edge->Dst->Number)
322+
if (DstEdges.size() && DstEdges.back()->Dst.Number > Edge->Dst.Number)
324323
DstEdgesAreSorted = false;
325324
DstEdges.push_back(Edge);
326325
}

lib/IR/GCOV.cpp

+21-36
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ using namespace llvm;
2626
//===----------------------------------------------------------------------===//
2727
// GCOVFile implementation.
2828

29-
/// ~GCOVFile - Delete GCOVFile and its content.
30-
GCOVFile::~GCOVFile() {
31-
DeleteContainerPointers(Functions);
32-
}
33-
3429
/// readGCNO - Read GCNO buffer.
3530
bool GCOVFile::readGCNO(GCOVBuffer &Buffer) {
3631
if (!Buffer.readGCNOFormat()) return false;
@@ -39,10 +34,10 @@ bool GCOVFile::readGCNO(GCOVBuffer &Buffer) {
3934
if (!Buffer.readInt(Checksum)) return false;
4035
while (true) {
4136
if (!Buffer.readFunctionTag()) break;
42-
GCOVFunction *GFun = new GCOVFunction(*this);
37+
auto GFun = make_unique<GCOVFunction>(*this);
4338
if (!GFun->readGCNO(Buffer, Version))
4439
return false;
45-
Functions.push_back(GFun);
40+
Functions.push_back(std::move(GFun));
4641
}
4742

4843
GCNOInitialized = true;
@@ -97,30 +92,22 @@ bool GCOVFile::readGCDA(GCOVBuffer &Buffer) {
9792

9893
/// dump - Dump GCOVFile content to dbgs() for debugging purposes.
9994
void GCOVFile::dump() const {
100-
for (SmallVectorImpl<GCOVFunction *>::const_iterator I = Functions.begin(),
101-
E = Functions.end(); I != E; ++I)
102-
(*I)->dump();
95+
for (const auto &FPtr : Functions)
96+
FPtr->dump();
10397
}
10498

10599
/// collectLineCounts - Collect line counts. This must be used after
106100
/// reading .gcno and .gcda files.
107101
void GCOVFile::collectLineCounts(FileInfo &FI) {
108-
for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
109-
E = Functions.end(); I != E; ++I)
110-
(*I)->collectLineCounts(FI);
102+
for (const auto &FPtr : Functions)
103+
FPtr->collectLineCounts(FI);
111104
FI.setRunCount(RunCount);
112105
FI.setProgramCount(ProgramCount);
113106
}
114107

115108
//===----------------------------------------------------------------------===//
116109
// GCOVFunction implementation.
117110

118-
/// ~GCOVFunction - Delete GCOVFunction and its content.
119-
GCOVFunction::~GCOVFunction() {
120-
DeleteContainerPointers(Blocks);
121-
DeleteContainerPointers(Edges);
122-
}
123-
124111
/// readGCNO - Read a function from the GCNO buffer. Return false if an error
125112
/// occurs.
126113
bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
@@ -150,7 +137,7 @@ bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
150137
if (!Buff.readInt(BlockCount)) return false;
151138
for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
152139
if (!Buff.readInt(Dummy)) return false; // Block flags;
153-
Blocks.push_back(new GCOVBlock(*this, i));
140+
Blocks.push_back(make_unique<GCOVBlock>(*this, i));
154141
}
155142

156143
// read edges.
@@ -168,8 +155,8 @@ bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
168155
for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
169156
uint32_t Dst;
170157
if (!Buff.readInt(Dst)) return false;
171-
GCOVEdge *Edge = new GCOVEdge(Blocks[BlockNo], Blocks[Dst]);
172-
Edges.push_back(Edge);
158+
Edges.push_back(make_unique<GCOVEdge>(*Blocks[BlockNo], *Blocks[Dst]));
159+
GCOVEdge *Edge = Edges.back().get();
173160
Blocks[BlockNo]->addDstEdge(Edge);
174161
Blocks[Dst]->addSrcEdge(Edge);
175162
if (!Buff.readInt(Dummy)) return false; // Edge flag
@@ -188,7 +175,7 @@ bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
188175
<< ").\n";
189176
return false;
190177
}
191-
GCOVBlock *Block = Blocks[BlockNo];
178+
GCOVBlock &Block = *Blocks[BlockNo];
192179
if (!Buff.readInt(Dummy)) return false; // flag
193180
while (Buff.getCursor() != (EndPos - 4)) {
194181
StringRef F;
@@ -203,7 +190,7 @@ bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
203190
uint32_t Line;
204191
if (!Buff.readInt(Line)) return false;
205192
if (!Line) break;
206-
Block->addLine(Line);
193+
Block.addLine(Line);
207194
}
208195
}
209196
if (!Buff.readInt(Dummy)) return false; // flag
@@ -300,9 +287,8 @@ uint64_t GCOVFunction::getExitCount() const {
300287
/// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
301288
void GCOVFunction::dump() const {
302289
dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
303-
for (SmallVectorImpl<GCOVBlock *>::const_iterator I = Blocks.begin(),
304-
E = Blocks.end(); I != E; ++I)
305-
(*I)->dump();
290+
for (const auto &Block : Blocks)
291+
Block->dump();
306292
}
307293

308294
/// collectLineCounts - Collect line counts. This must be used after
@@ -313,9 +299,8 @@ void GCOVFunction::collectLineCounts(FileInfo &FI) {
313299
if (LineNumber == 0)
314300
return;
315301

316-
for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
317-
E = Blocks.end(); I != E; ++I)
318-
(*I)->collectLineCounts(FI);
302+
for (const auto &Block : Blocks)
303+
Block->collectLineCounts(FI);
319304
FI.addFunctionLine(Filename, LineNumber, this);
320305
}
321306

@@ -335,8 +320,8 @@ void GCOVBlock::addCount(size_t DstEdgeNo, uint64_t N) {
335320
assert(DstEdgeNo < DstEdges.size()); // up to caller to ensure EdgeNo is valid
336321
DstEdges[DstEdgeNo]->Count = N;
337322
Counter += N;
338-
if (!DstEdges[DstEdgeNo]->Dst->getNumDstEdges())
339-
DstEdges[DstEdgeNo]->Dst->Counter += N;
323+
if (!DstEdges[DstEdgeNo]->Dst.getNumDstEdges())
324+
DstEdges[DstEdgeNo]->Dst.Counter += N;
340325
}
341326

342327
/// sortDstEdges - Sort destination edges by block number, nop if already
@@ -363,15 +348,15 @@ void GCOVBlock::dump() const {
363348
dbgs() << "\tSource Edges : ";
364349
for (EdgeIterator I = SrcEdges.begin(), E = SrcEdges.end(); I != E; ++I) {
365350
const GCOVEdge *Edge = *I;
366-
dbgs() << Edge->Src->Number << " (" << Edge->Count << "), ";
351+
dbgs() << Edge->Src.Number << " (" << Edge->Count << "), ";
367352
}
368353
dbgs() << "\n";
369354
}
370355
if (!DstEdges.empty()) {
371356
dbgs() << "\tDestination Edges : ";
372357
for (EdgeIterator I = DstEdges.begin(), E = DstEdges.end(); I != E; ++I) {
373358
const GCOVEdge *Edge = *I;
374-
dbgs() << Edge->Dst->Number << " (" << Edge->Count << "), ";
359+
dbgs() << Edge->Dst.Number << " (" << Edge->Count << "), ";
375360
}
376361
dbgs() << "\n";
377362
}
@@ -617,8 +602,8 @@ void FileInfo::printFunctionSummary(raw_fd_ostream &OS,
617602
uint32_t BlocksExec = 0;
618603
for (GCOVFunction::BlockIterator I = Func->block_begin(),
619604
E = Func->block_end(); I != E; ++I) {
620-
const GCOVBlock *Block = *I;
621-
if (Block->getNumDstEdges() && Block->getCount())
605+
const GCOVBlock &Block = **I;
606+
if (Block.getNumDstEdges() && Block.getCount())
622607
++BlocksExec;
623608
}
624609

0 commit comments

Comments
 (0)