1 //===- GCOVr.cpp - LLVM coverage tool -------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // GCOV implements the interface to read and write coverage files that use
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/GCOV.h"
17 #include "llvm/ADT/OwningPtr.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/Support/Format.h"
20 #include "llvm/Support/MemoryObject.h"
21 #include "llvm/Support/system_error.h"
24 //===----------------------------------------------------------------------===//
25 // GCOVFile implementation.
27 /// ~GCOVFile - Delete GCOVFile and its content.
28 GCOVFile::~GCOVFile() {
29 DeleteContainerPointers(Functions);
32 /// readGCNO - Read GCNO buffer.
33 bool GCOVFile::readGCNO(GCOVBuffer &Buffer) {
34 if (!Buffer.readGCNOFormat()) return false;
35 if (!Buffer.readGCOVVersion(Version)) return false;
37 if (!Buffer.readInt(Checksum)) return false;
39 if (!Buffer.readFunctionTag()) break;
40 GCOVFunction *GFun = new GCOVFunction(*this);
41 if (!GFun->readGCNO(Buffer, Version))
43 Functions.push_back(GFun);
46 GCNOInitialized = true;
50 /// readGCDA - Read GCDA buffer. It is required that readGCDA() can only be
51 /// called after readGCNO().
52 bool GCOVFile::readGCDA(GCOVBuffer &Buffer) {
53 assert(GCNOInitialized && "readGCDA() can only be called after readGCNO()");
54 if (!Buffer.readGCDAFormat()) return false;
55 GCOV::GCOVVersion GCDAVersion;
56 if (!Buffer.readGCOVVersion(GCDAVersion)) return false;
57 if (Version != GCDAVersion) {
58 errs() << "GCOV versions do not match.\n";
62 uint32_t GCDAChecksum;
63 if (!Buffer.readInt(GCDAChecksum)) return false;
64 if (Checksum != GCDAChecksum) {
65 errs() << "File checksums do not match: " << Checksum << " != "
66 << GCDAChecksum << ".\n";
69 for (size_t i = 0, e = Functions.size(); i < e; ++i) {
70 if (!Buffer.readFunctionTag()) {
71 errs() << "Unexpected number of functions.\n";
74 if (!Functions[i]->readGCDA(Buffer, Version))
77 if (Buffer.readObjectTag()) {
80 if (!Buffer.readInt(Length)) return false;
81 if (!Buffer.readInt(Dummy)) return false; // checksum
82 if (!Buffer.readInt(Dummy)) return false; // num
83 if (!Buffer.readInt(RunCount)) return false;;
84 Buffer.advanceCursor(Length-3);
86 while (Buffer.readProgramTag()) {
88 if (!Buffer.readInt(Length)) return false;
89 Buffer.advanceCursor(Length);
96 /// dump - Dump GCOVFile content to dbgs() for debugging purposes.
97 void GCOVFile::dump() const {
98 for (SmallVectorImpl<GCOVFunction *>::const_iterator I = Functions.begin(),
99 E = Functions.end(); I != E; ++I)
103 /// collectLineCounts - Collect line counts. This must be used after
104 /// reading .gcno and .gcda files.
105 void GCOVFile::collectLineCounts(FileInfo &FI) {
106 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
107 E = Functions.end(); I != E; ++I)
108 (*I)->collectLineCounts(FI);
109 FI.setRunCount(RunCount);
110 FI.setProgramCount(ProgramCount);
113 //===----------------------------------------------------------------------===//
114 // GCOVFunction implementation.
116 /// ~GCOVFunction - Delete GCOVFunction and its content.
117 GCOVFunction::~GCOVFunction() {
118 DeleteContainerPointers(Blocks);
119 DeleteContainerPointers(Edges);
122 /// readGCNO - Read a function from the GCNO buffer. Return false if an error
124 bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
126 if (!Buff.readInt(Dummy)) return false; // Function header length
127 if (!Buff.readInt(Ident)) return false;
128 if (!Buff.readInt(Checksum)) return false;
129 if (Version != GCOV::V402) {
130 uint32_t CfgChecksum;
131 if (!Buff.readInt(CfgChecksum)) return false;
132 if (Parent.getChecksum() != CfgChecksum) {
133 errs() << "File checksums do not match: " << Parent.getChecksum()
134 << " != " << CfgChecksum << " in (" << Name << ").\n";
138 if (!Buff.readString(Name)) return false;
139 if (!Buff.readString(Filename)) return false;
140 if (!Buff.readInt(LineNumber)) return false;
143 if (!Buff.readBlockTag()) {
144 errs() << "Block tag not found.\n";
148 if (!Buff.readInt(BlockCount)) return false;
149 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
150 if (!Buff.readInt(Dummy)) return false; // Block flags;
151 Blocks.push_back(new GCOVBlock(*this, i));
155 while (Buff.readEdgeTag()) {
157 if (!Buff.readInt(EdgeCount)) return false;
158 EdgeCount = (EdgeCount - 1) / 2;
160 if (!Buff.readInt(BlockNo)) return false;
161 if (BlockNo >= BlockCount) {
162 errs() << "Unexpected block number (in " << Name << ").\n";
165 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
167 if (!Buff.readInt(Dst)) return false;
168 GCOVEdge *Edge = new GCOVEdge(Blocks[BlockNo], Blocks[Dst]);
169 Edges.push_back(Edge);
170 Blocks[BlockNo]->addDstEdge(Edge);
171 Blocks[Dst]->addSrcEdge(Edge);
172 if (!Buff.readInt(Dummy)) return false; // Edge flag
177 while (Buff.readLineTag()) {
178 uint32_t LineTableLength;
179 if (!Buff.readInt(LineTableLength)) return false;
180 uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
182 if (!Buff.readInt(BlockNo)) return false;
183 if (BlockNo >= BlockCount) {
184 errs() << "Unexpected block number (in " << Name << ").\n";
187 GCOVBlock *Block = Blocks[BlockNo];
188 if (!Buff.readInt(Dummy)) return false; // flag
189 while (Buff.getCursor() != (EndPos - 4)) {
191 if (!Buff.readString(F)) return false;
193 errs() << "Multiple sources for a single basic block (in "
197 if (Buff.getCursor() == (EndPos - 4)) break;
200 if (!Buff.readInt(Line)) return false;
202 Block->addLine(Line);
205 if (!Buff.readInt(Dummy)) return false; // flag
210 /// readGCDA - Read a function from the GCDA buffer. Return false if an error
212 bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
214 if (!Buff.readInt(Dummy)) return false; // Function header length
217 if (!Buff.readInt(GCDAIdent)) return false;
218 if (Ident != GCDAIdent) {
219 errs() << "Function identifiers do not match: " << Ident << " != "
220 << GCDAIdent << " (in " << Name << ").\n";
224 uint32_t GCDAChecksum;
225 if (!Buff.readInt(GCDAChecksum)) return false;
226 if (Checksum != GCDAChecksum) {
227 errs() << "Function checksums do not match: " << Checksum << " != "
228 << GCDAChecksum << " (in " << Name << ").\n";
232 uint32_t CfgChecksum;
233 if (Version != GCOV::V402) {
234 if (!Buff.readInt(CfgChecksum)) return false;
235 if (Parent.getChecksum() != CfgChecksum) {
236 errs() << "File checksums do not match: " << Parent.getChecksum()
237 << " != " << CfgChecksum << " (in " << Name << ").\n";
243 if (!Buff.readString(GCDAName)) return false;
244 if (Name != GCDAName) {
245 errs() << "Function names do not match: " << Name << " != " << GCDAName
250 if (!Buff.readArcTag()) {
251 errs() << "Arc tag not found (in " << Name << ").\n";
256 if (!Buff.readInt(Count)) return false;
259 // This for loop adds the counts for each block. A second nested loop is
260 // required to combine the edge counts that are contained in the GCDA file.
261 for (uint32_t BlockNo = 0; Count > 0; ++BlockNo) {
262 // The last block is always reserved for exit block
263 if (BlockNo >= Blocks.size()-1) {
264 errs() << "Unexpected number of edges (in " << Name << ").\n";
267 GCOVBlock &Block = *Blocks[BlockNo];
268 for (size_t EdgeNo = 0, End = Block.getNumDstEdges(); EdgeNo < End;
271 errs() << "Unexpected number of edges (in " << Name << ").\n";
275 if (!Buff.readInt64(ArcCount)) return false;
276 Block.addCount(EdgeNo, ArcCount);
283 /// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
284 void GCOVFunction::dump() const {
285 dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
286 for (SmallVectorImpl<GCOVBlock *>::const_iterator I = Blocks.begin(),
287 E = Blocks.end(); I != E; ++I)
291 /// collectLineCounts - Collect line counts. This must be used after
292 /// reading .gcno and .gcda files.
293 void GCOVFunction::collectLineCounts(FileInfo &FI) {
294 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
295 E = Blocks.end(); I != E; ++I)
296 (*I)->collectLineCounts(FI);
299 //===----------------------------------------------------------------------===//
300 // GCOVBlock implementation.
302 /// ~GCOVBlock - Delete GCOVBlock and its content.
303 GCOVBlock::~GCOVBlock() {
309 /// addCount - Add to block counter while storing the edge count. If the
310 /// destination has no outgoing edges, also update that block's count too.
311 void GCOVBlock::addCount(size_t DstEdgeNo, uint64_t N) {
312 assert(DstEdgeNo < DstEdges.size()); // up to caller to ensure EdgeNo is valid
313 DstEdges[DstEdgeNo]->Count = N;
315 if (!DstEdges[DstEdgeNo]->Dst->getNumDstEdges())
316 DstEdges[DstEdgeNo]->Dst->Counter += N;
319 /// collectLineCounts - Collect line counts. This must be used after
320 /// reading .gcno and .gcda files.
321 void GCOVBlock::collectLineCounts(FileInfo &FI) {
322 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
323 E = Lines.end(); I != E; ++I)
324 FI.addBlockLine(Parent.getFilename(), *I, this);
327 /// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
328 void GCOVBlock::dump() const {
329 dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
330 if (!SrcEdges.empty()) {
331 dbgs() << "\tSource Edges : ";
332 for (EdgeIterator I = SrcEdges.begin(), E = SrcEdges.end(); I != E; ++I) {
333 const GCOVEdge *Edge = *I;
334 dbgs() << Edge->Src->Number << " (" << Edge->Count << "), ";
338 if (!DstEdges.empty()) {
339 dbgs() << "\tDestination Edges : ";
340 for (EdgeIterator I = DstEdges.begin(), E = DstEdges.end(); I != E; ++I) {
341 const GCOVEdge *Edge = *I;
342 dbgs() << Edge->Dst->Number << " (" << Edge->Count << "), ";
346 if (!Lines.empty()) {
347 dbgs() << "\tLines : ";
348 for (SmallVectorImpl<uint32_t>::const_iterator I = Lines.begin(),
349 E = Lines.end(); I != E; ++I)
350 dbgs() << (*I) << ",";
355 //===----------------------------------------------------------------------===//
356 // FileInfo implementation.
358 /// print - Print source files with collected line count information.
359 void FileInfo::print(StringRef GCNOFile, StringRef GCDAFile) const {
360 for (StringMap<LineData>::const_iterator I = LineInfo.begin(),
361 E = LineInfo.end(); I != E; ++I) {
362 StringRef Filename = I->first();
363 OwningPtr<MemoryBuffer> Buff;
364 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
365 errs() << Filename << ": " << ec.message() << "\n";
368 StringRef AllLines = Buff->getBuffer();
370 std::string CovFilename = Filename.str() + ".gcov";
371 std::string ErrorInfo;
372 raw_fd_ostream OS(CovFilename.c_str(), ErrorInfo);
373 if (!ErrorInfo.empty())
374 errs() << ErrorInfo << "\n";
376 OS << " -: 0:Source:" << Filename << "\n";
377 OS << " -: 0:Graph:" << GCNOFile << "\n";
378 OS << " -: 0:Data:" << GCDAFile << "\n";
379 OS << " -: 0:Runs:" << RunCount << "\n";
380 OS << " -: 0:Programs:" << ProgramCount << "\n";
382 const LineData &Line = I->second;
383 for (uint32_t i = 0; !AllLines.empty(); ++i) {
384 LineData::const_iterator BlocksIt = Line.find(i);
386 // Add up the block counts to form line counts.
387 if (BlocksIt != Line.end()) {
388 const BlockVector &Blocks = BlocksIt->second;
389 uint64_t LineCount = 0;
390 for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end();
392 LineCount += (*I)->getCount();
397 OS << format("%9" PRIu64 ":", LineCount);
401 std::pair<StringRef, StringRef> P = AllLines.split('\n');
402 OS << format("%5u:", i+1) << P.first << "\n";