1 //===- GCOV.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: " << BlockNo << " (in " << Name
166 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
168 if (!Buff.readInt(Dst)) return false;
169 GCOVEdge *Edge = new GCOVEdge(Blocks[BlockNo], Blocks[Dst]);
170 Edges.push_back(Edge);
171 Blocks[BlockNo]->addDstEdge(Edge);
172 Blocks[Dst]->addSrcEdge(Edge);
173 if (!Buff.readInt(Dummy)) return false; // Edge flag
178 while (Buff.readLineTag()) {
179 uint32_t LineTableLength;
180 if (!Buff.readInt(LineTableLength)) return false;
181 uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
183 if (!Buff.readInt(BlockNo)) return false;
184 if (BlockNo >= BlockCount) {
185 errs() << "Unexpected block number: " << BlockNo << " (in " << Name
189 GCOVBlock *Block = Blocks[BlockNo];
190 if (!Buff.readInt(Dummy)) return false; // flag
191 while (Buff.getCursor() != (EndPos - 4)) {
193 if (!Buff.readString(F)) return false;
195 errs() << "Multiple sources for a single basic block: " << Filename
196 << " != " << F << " (in " << Name << ").\n";
199 if (Buff.getCursor() == (EndPos - 4)) break;
202 if (!Buff.readInt(Line)) return false;
204 Block->addLine(Line);
207 if (!Buff.readInt(Dummy)) return false; // flag
212 /// readGCDA - Read a function from the GCDA buffer. Return false if an error
214 bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
216 if (!Buff.readInt(Dummy)) return false; // Function header length
219 if (!Buff.readInt(GCDAIdent)) return false;
220 if (Ident != GCDAIdent) {
221 errs() << "Function identifiers do not match: " << Ident << " != "
222 << GCDAIdent << " (in " << Name << ").\n";
226 uint32_t GCDAChecksum;
227 if (!Buff.readInt(GCDAChecksum)) return false;
228 if (Checksum != GCDAChecksum) {
229 errs() << "Function checksums do not match: " << Checksum << " != "
230 << GCDAChecksum << " (in " << Name << ").\n";
234 uint32_t CfgChecksum;
235 if (Version != GCOV::V402) {
236 if (!Buff.readInt(CfgChecksum)) return false;
237 if (Parent.getChecksum() != CfgChecksum) {
238 errs() << "File checksums do not match: " << Parent.getChecksum()
239 << " != " << CfgChecksum << " (in " << Name << ").\n";
245 if (!Buff.readString(GCDAName)) return false;
246 if (Name != GCDAName) {
247 errs() << "Function names do not match: " << Name << " != " << GCDAName
252 if (!Buff.readArcTag()) {
253 errs() << "Arc tag not found (in " << Name << ").\n";
258 if (!Buff.readInt(Count)) return false;
261 // This for loop adds the counts for each block. A second nested loop is
262 // required to combine the edge counts that are contained in the GCDA file.
263 for (uint32_t BlockNo = 0; Count > 0; ++BlockNo) {
264 // The last block is always reserved for exit block
265 if (BlockNo >= Blocks.size()-1) {
266 errs() << "Unexpected number of edges (in " << Name << ").\n";
269 GCOVBlock &Block = *Blocks[BlockNo];
270 for (size_t EdgeNo = 0, End = Block.getNumDstEdges(); EdgeNo < End;
273 errs() << "Unexpected number of edges (in " << Name << ").\n";
277 if (!Buff.readInt64(ArcCount)) return false;
278 Block.addCount(EdgeNo, ArcCount);
285 /// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
286 void GCOVFunction::dump() const {
287 dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
288 for (SmallVectorImpl<GCOVBlock *>::const_iterator I = Blocks.begin(),
289 E = Blocks.end(); I != E; ++I)
293 /// collectLineCounts - Collect line counts. This must be used after
294 /// reading .gcno and .gcda files.
295 void GCOVFunction::collectLineCounts(FileInfo &FI) {
296 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
297 E = Blocks.end(); I != E; ++I)
298 (*I)->collectLineCounts(FI);
301 //===----------------------------------------------------------------------===//
302 // GCOVBlock implementation.
304 /// ~GCOVBlock - Delete GCOVBlock and its content.
305 GCOVBlock::~GCOVBlock() {
311 /// addCount - Add to block counter while storing the edge count. If the
312 /// destination has no outgoing edges, also update that block's count too.
313 void GCOVBlock::addCount(size_t DstEdgeNo, uint64_t N) {
314 assert(DstEdgeNo < DstEdges.size()); // up to caller to ensure EdgeNo is valid
315 DstEdges[DstEdgeNo]->Count = N;
317 if (!DstEdges[DstEdgeNo]->Dst->getNumDstEdges())
318 DstEdges[DstEdgeNo]->Dst->Counter += N;
321 /// collectLineCounts - Collect line counts. This must be used after
322 /// reading .gcno and .gcda files.
323 void GCOVBlock::collectLineCounts(FileInfo &FI) {
324 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
325 E = Lines.end(); I != E; ++I)
326 FI.addBlockLine(Parent.getFilename(), *I, this);
329 /// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
330 void GCOVBlock::dump() const {
331 dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
332 if (!SrcEdges.empty()) {
333 dbgs() << "\tSource Edges : ";
334 for (EdgeIterator I = SrcEdges.begin(), E = SrcEdges.end(); I != E; ++I) {
335 const GCOVEdge *Edge = *I;
336 dbgs() << Edge->Src->Number << " (" << Edge->Count << "), ";
340 if (!DstEdges.empty()) {
341 dbgs() << "\tDestination Edges : ";
342 for (EdgeIterator I = DstEdges.begin(), E = DstEdges.end(); I != E; ++I) {
343 const GCOVEdge *Edge = *I;
344 dbgs() << Edge->Dst->Number << " (" << Edge->Count << "), ";
348 if (!Lines.empty()) {
349 dbgs() << "\tLines : ";
350 for (SmallVectorImpl<uint32_t>::const_iterator I = Lines.begin(),
351 E = Lines.end(); I != E; ++I)
352 dbgs() << (*I) << ",";
357 //===----------------------------------------------------------------------===//
358 // FileInfo implementation.
360 /// print - Print source files with collected line count information.
361 void FileInfo::print(StringRef GCNOFile, StringRef GCDAFile) const {
362 for (StringMap<LineData>::const_iterator I = LineInfo.begin(),
363 E = LineInfo.end(); I != E; ++I) {
364 StringRef Filename = I->first();
365 OwningPtr<MemoryBuffer> Buff;
366 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
367 errs() << Filename << ": " << ec.message() << "\n";
370 StringRef AllLines = Buff->getBuffer();
372 std::string CovFilename = Filename.str() + ".gcov";
373 std::string ErrorInfo;
374 raw_fd_ostream OS(CovFilename.c_str(), ErrorInfo);
375 if (!ErrorInfo.empty())
376 errs() << ErrorInfo << "\n";
378 OS << " -: 0:Source:" << Filename << "\n";
379 OS << " -: 0:Graph:" << GCNOFile << "\n";
380 OS << " -: 0:Data:" << GCDAFile << "\n";
381 OS << " -: 0:Runs:" << RunCount << "\n";
382 OS << " -: 0:Programs:" << ProgramCount << "\n";
384 const LineData &Line = I->second;
385 for (uint32_t i = 0; !AllLines.empty(); ++i) {
386 LineData::const_iterator BlocksIt = Line.find(i);
388 // Add up the block counts to form line counts.
389 if (BlocksIt != Line.end()) {
390 const BlockVector &Blocks = BlocksIt->second;
391 uint64_t LineCount = 0;
392 for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end();
394 LineCount += (*I)->getCount();
399 OS << format("%9" PRIu64 ":", LineCount);
403 std::pair<StringRef, StringRef> P = AllLines.split('\n');
404 OS << format("%5u:", i+1) << P.first << "\n";