blob: 45ef78e078ae31839218f91ef0d9c73020de8eb2 [file] [log] [blame]
Yuchen Wuc3e64242013-12-05 22:02:29 +00001//===- GCOV.cpp - LLVM coverage tool --------------------------------------===//
Devang Patel37140652011-09-28 18:50:00 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
NAKAMURA Takumi3b551962013-11-14 11:44:58 +000010// GCOV implements the interface to read and write coverage files that use
Devang Patel8dfb6552011-10-04 17:24:48 +000011// 'gcov' format.
Devang Patel37140652011-09-28 18:50:00 +000012//
Devang Patel37140652011-09-28 18:50:00 +000013//===----------------------------------------------------------------------===//
14
Yuchen Wu03678152013-10-25 02:22:24 +000015#include "llvm/Support/Debug.h"
Devang Patel37140652011-09-28 18:50:00 +000016#include "llvm/ADT/OwningPtr.h"
Devang Patela9e8a252011-09-29 16:46:47 +000017#include "llvm/ADT/STLExtras.h"
Bob Wilson3461bed2013-10-22 05:09:41 +000018#include "llvm/Support/Format.h"
Justin Bognerc6af3502014-02-04 10:45:02 +000019#include "llvm/Support/FileSystem.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000020#include "llvm/Support/GCOV.h"
Devang Patel37140652011-09-28 18:50:00 +000021#include "llvm/Support/MemoryObject.h"
Justin Bognerc6af3502014-02-04 10:45:02 +000022#include "llvm/Support/Path.h"
Devang Patel37140652011-09-28 18:50:00 +000023#include "llvm/Support/system_error.h"
Yuchen Wu342714c2013-12-13 01:15:07 +000024#include <algorithm>
Devang Patel37140652011-09-28 18:50:00 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28// GCOVFile implementation.
29
30/// ~GCOVFile - Delete GCOVFile and its content.
31GCOVFile::~GCOVFile() {
32 DeleteContainerPointers(Functions);
33}
34
Yuchen Wubec4e902013-12-04 04:49:23 +000035/// readGCNO - Read GCNO buffer.
36bool GCOVFile::readGCNO(GCOVBuffer &Buffer) {
37 if (!Buffer.readGCNOFormat()) return false;
38 if (!Buffer.readGCOVVersion(Version)) return false;
39
40 if (!Buffer.readInt(Checksum)) return false;
41 while (true) {
42 if (!Buffer.readFunctionTag()) break;
Yuchen Wu57529972013-12-04 05:42:28 +000043 GCOVFunction *GFun = new GCOVFunction(*this);
Yuchen Wubec4e902013-12-04 04:49:23 +000044 if (!GFun->readGCNO(Buffer, Version))
45 return false;
46 Functions.push_back(GFun);
47 }
48
Yuchen Wu21517e42013-12-04 05:07:36 +000049 GCNOInitialized = true;
Yuchen Wubec4e902013-12-04 04:49:23 +000050 return true;
Devang Patele5a8f2f92011-09-29 17:06:40 +000051}
52
Yuchen Wubec4e902013-12-04 04:49:23 +000053/// readGCDA - Read GCDA buffer. It is required that readGCDA() can only be
54/// called after readGCNO().
55bool GCOVFile::readGCDA(GCOVBuffer &Buffer) {
Yuchen Wu21517e42013-12-04 05:07:36 +000056 assert(GCNOInitialized && "readGCDA() can only be called after readGCNO()");
Yuchen Wubec4e902013-12-04 04:49:23 +000057 if (!Buffer.readGCDAFormat()) return false;
58 GCOV::GCOVVersion GCDAVersion;
59 if (!Buffer.readGCOVVersion(GCDAVersion)) return false;
60 if (Version != GCDAVersion) {
61 errs() << "GCOV versions do not match.\n";
Devang Patel37140652011-09-28 18:50:00 +000062 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000063 }
Devang Patel37140652011-09-28 18:50:00 +000064
Yuchen Wubec4e902013-12-04 04:49:23 +000065 uint32_t GCDAChecksum;
66 if (!Buffer.readInt(GCDAChecksum)) return false;
67 if (Checksum != GCDAChecksum) {
Yuchen Wu57529972013-12-04 05:42:28 +000068 errs() << "File checksums do not match: " << Checksum << " != "
69 << GCDAChecksum << ".\n";
Yuchen Wubec4e902013-12-04 04:49:23 +000070 return false;
71 }
72 for (size_t i = 0, e = Functions.size(); i < e; ++i) {
73 if (!Buffer.readFunctionTag()) {
74 errs() << "Unexpected number of functions.\n";
Yuchen Wubabe7492013-11-20 04:15:05 +000075 return false;
76 }
Yuchen Wubec4e902013-12-04 04:49:23 +000077 if (!Functions[i]->readGCDA(Buffer, Version))
78 return false;
79 }
80 if (Buffer.readObjectTag()) {
81 uint32_t Length;
82 uint32_t Dummy;
83 if (!Buffer.readInt(Length)) return false;
84 if (!Buffer.readInt(Dummy)) return false; // checksum
85 if (!Buffer.readInt(Dummy)) return false; // num
Yuchen Wu8742a282013-12-16 20:03:11 +000086 if (!Buffer.readInt(RunCount)) return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000087 Buffer.advanceCursor(Length-3);
88 }
89 while (Buffer.readProgramTag()) {
90 uint32_t Length;
91 if (!Buffer.readInt(Length)) return false;
92 Buffer.advanceCursor(Length);
93 ++ProgramCount;
Yuchen Wu14ae8e62013-10-25 02:22:21 +000094 }
95
Devang Patel37140652011-09-28 18:50:00 +000096 return true;
97}
98
Yuchen Wu03678152013-10-25 02:22:24 +000099/// dump - Dump GCOVFile content to dbgs() for debugging purposes.
Yuchen Wuef6909d2013-11-19 20:33:32 +0000100void GCOVFile::dump() const {
101 for (SmallVectorImpl<GCOVFunction *>::const_iterator I = Functions.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000102 E = Functions.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000103 (*I)->dump();
104}
105
106/// collectLineCounts - Collect line counts. This must be used after
107/// reading .gcno and .gcda files.
108void GCOVFile::collectLineCounts(FileInfo &FI) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000109 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
NAKAMURA Takumi3b551962013-11-14 11:44:58 +0000110 E = Functions.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000111 (*I)->collectLineCounts(FI);
Yuchen Wu30672d92013-11-05 01:11:58 +0000112 FI.setRunCount(RunCount);
Yuchen Wu14ae8e62013-10-25 02:22:21 +0000113 FI.setProgramCount(ProgramCount);
Devang Patel37140652011-09-28 18:50:00 +0000114}
115
116//===----------------------------------------------------------------------===//
117// GCOVFunction implementation.
118
119/// ~GCOVFunction - Delete GCOVFunction and its content.
120GCOVFunction::~GCOVFunction() {
121 DeleteContainerPointers(Blocks);
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000122 DeleteContainerPointers(Edges);
Devang Patel37140652011-09-28 18:50:00 +0000123}
124
Yuchen Wuba718332013-12-03 00:15:49 +0000125/// readGCNO - Read a function from the GCNO buffer. Return false if an error
126/// occurs.
Yuchen Wubec4e902013-12-04 04:49:23 +0000127bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
Yuchen Wue28da842013-11-14 00:07:15 +0000128 uint32_t Dummy;
129 if (!Buff.readInt(Dummy)) return false; // Function header length
130 if (!Buff.readInt(Ident)) return false;
Daniel Jasper87a24d52013-12-04 08:57:17 +0000131 if (!Buff.readInt(Checksum)) return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000132 if (Version != GCOV::V402) {
133 uint32_t CfgChecksum;
134 if (!Buff.readInt(CfgChecksum)) return false;
135 if (Parent.getChecksum() != CfgChecksum) {
136 errs() << "File checksums do not match: " << Parent.getChecksum()
137 << " != " << CfgChecksum << " in (" << Name << ").\n";
138 return false;
139 }
140 }
Yuchen Wue28da842013-11-14 00:07:15 +0000141 if (!Buff.readString(Name)) return false;
Yuchen Wuba718332013-12-03 00:15:49 +0000142 if (!Buff.readString(Filename)) return false;
Yuchen Wue28da842013-11-14 00:07:15 +0000143 if (!Buff.readInt(LineNumber)) return false;
Devang Patel37140652011-09-28 18:50:00 +0000144
145 // read blocks.
Yuchen Wue28da842013-11-14 00:07:15 +0000146 if (!Buff.readBlockTag()) {
147 errs() << "Block tag not found.\n";
148 return false;
149 }
150 uint32_t BlockCount;
151 if (!Buff.readInt(BlockCount)) return false;
Bob Wilson868e6e32013-10-22 20:02:36 +0000152 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
Yuchen Wue28da842013-11-14 00:07:15 +0000153 if (!Buff.readInt(Dummy)) return false; // Block flags;
Yuchen Wud738bee2013-11-14 00:32:00 +0000154 Blocks.push_back(new GCOVBlock(*this, i));
Devang Patel37140652011-09-28 18:50:00 +0000155 }
156
157 // read edges.
158 while (Buff.readEdgeTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +0000159 uint32_t EdgeCount;
160 if (!Buff.readInt(EdgeCount)) return false;
161 EdgeCount = (EdgeCount - 1) / 2;
162 uint32_t BlockNo;
163 if (!Buff.readInt(BlockNo)) return false;
164 if (BlockNo >= BlockCount) {
Yuchen Wu4c9f19d2013-12-05 22:02:33 +0000165 errs() << "Unexpected block number: " << BlockNo << " (in " << Name
166 << ").\n";
Yuchen Wue28da842013-11-14 00:07:15 +0000167 return false;
168 }
Bob Wilson868e6e32013-10-22 20:02:36 +0000169 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
Yuchen Wue28da842013-11-14 00:07:15 +0000170 uint32_t Dst;
171 if (!Buff.readInt(Dst)) return false;
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000172 GCOVEdge *Edge = new GCOVEdge(Blocks[BlockNo], Blocks[Dst]);
173 Edges.push_back(Edge);
174 Blocks[BlockNo]->addDstEdge(Edge);
175 Blocks[Dst]->addSrcEdge(Edge);
Yuchen Wue28da842013-11-14 00:07:15 +0000176 if (!Buff.readInt(Dummy)) return false; // Edge flag
Devang Patel37140652011-09-28 18:50:00 +0000177 }
178 }
179
180 // read line table.
181 while (Buff.readLineTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +0000182 uint32_t LineTableLength;
183 if (!Buff.readInt(LineTableLength)) return false;
Bob Wilson00928bc2013-10-22 19:54:32 +0000184 uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
Yuchen Wue28da842013-11-14 00:07:15 +0000185 uint32_t BlockNo;
186 if (!Buff.readInt(BlockNo)) return false;
187 if (BlockNo >= BlockCount) {
Yuchen Wu4c9f19d2013-12-05 22:02:33 +0000188 errs() << "Unexpected block number: " << BlockNo << " (in " << Name
189 << ").\n";
Yuchen Wue28da842013-11-14 00:07:15 +0000190 return false;
Devang Patel37140652011-09-28 18:50:00 +0000191 }
Yuchen Wue28da842013-11-14 00:07:15 +0000192 GCOVBlock *Block = Blocks[BlockNo];
193 if (!Buff.readInt(Dummy)) return false; // flag
194 while (Buff.getCursor() != (EndPos - 4)) {
Yuchen Wud738bee2013-11-14 00:32:00 +0000195 StringRef F;
196 if (!Buff.readString(F)) return false;
Yuchen Wu4c9f19d2013-12-05 22:02:33 +0000197 if (Filename != F) {
198 errs() << "Multiple sources for a single basic block: " << Filename
199 << " != " << F << " (in " << Name << ").\n";
Yuchen Wud738bee2013-11-14 00:32:00 +0000200 return false;
201 }
Yuchen Wue28da842013-11-14 00:07:15 +0000202 if (Buff.getCursor() == (EndPos - 4)) break;
203 while (true) {
204 uint32_t Line;
205 if (!Buff.readInt(Line)) return false;
206 if (!Line) break;
Yuchen Wud738bee2013-11-14 00:32:00 +0000207 Block->addLine(Line);
Yuchen Wue28da842013-11-14 00:07:15 +0000208 }
209 }
210 if (!Buff.readInt(Dummy)) return false; // flag
Devang Patel37140652011-09-28 18:50:00 +0000211 }
212 return true;
213}
214
Yuchen Wuba718332013-12-03 00:15:49 +0000215/// readGCDA - Read a function from the GCDA buffer. Return false if an error
216/// occurs.
Yuchen Wubec4e902013-12-04 04:49:23 +0000217bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
Yuchen Wuba718332013-12-03 00:15:49 +0000218 uint32_t Dummy;
219 if (!Buff.readInt(Dummy)) return false; // Function header length
Daniel Jasper87a24d52013-12-04 08:57:17 +0000220
Yuchen Wu57529972013-12-04 05:42:28 +0000221 uint32_t GCDAIdent;
222 if (!Buff.readInt(GCDAIdent)) return false;
223 if (Ident != GCDAIdent) {
224 errs() << "Function identifiers do not match: " << Ident << " != "
225 << GCDAIdent << " (in " << Name << ").\n";
226 return false;
227 }
Yuchen Wuba718332013-12-03 00:15:49 +0000228
Daniel Jasper87a24d52013-12-04 08:57:17 +0000229 uint32_t GCDAChecksum;
230 if (!Buff.readInt(GCDAChecksum)) return false;
231 if (Checksum != GCDAChecksum) {
232 errs() << "Function checksums do not match: " << Checksum << " != "
233 << GCDAChecksum << " (in " << Name << ").\n";
234 return false;
235 }
Yuchen Wu57529972013-12-04 05:42:28 +0000236
237 uint32_t CfgChecksum;
238 if (Version != GCOV::V402) {
239 if (!Buff.readInt(CfgChecksum)) return false;
240 if (Parent.getChecksum() != CfgChecksum) {
241 errs() << "File checksums do not match: " << Parent.getChecksum()
242 << " != " << CfgChecksum << " (in " << Name << ").\n";
243 return false;
244 }
245 }
246
247 StringRef GCDAName;
248 if (!Buff.readString(GCDAName)) return false;
249 if (Name != GCDAName) {
250 errs() << "Function names do not match: " << Name << " != " << GCDAName
251 << ".\n";
252 return false;
253 }
Yuchen Wuba718332013-12-03 00:15:49 +0000254
255 if (!Buff.readArcTag()) {
Yuchen Wu57529972013-12-04 05:42:28 +0000256 errs() << "Arc tag not found (in " << Name << ").\n";
Yuchen Wuba718332013-12-03 00:15:49 +0000257 return false;
258 }
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000259
Yuchen Wuba718332013-12-03 00:15:49 +0000260 uint32_t Count;
261 if (!Buff.readInt(Count)) return false;
262 Count /= 2;
263
264 // This for loop adds the counts for each block. A second nested loop is
265 // required to combine the edge counts that are contained in the GCDA file.
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000266 for (uint32_t BlockNo = 0; Count > 0; ++BlockNo) {
267 // The last block is always reserved for exit block
268 if (BlockNo >= Blocks.size()-1) {
Yuchen Wu57529972013-12-04 05:42:28 +0000269 errs() << "Unexpected number of edges (in " << Name << ").\n";
Yuchen Wuba718332013-12-03 00:15:49 +0000270 return false;
271 }
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000272 GCOVBlock &Block = *Blocks[BlockNo];
273 for (size_t EdgeNo = 0, End = Block.getNumDstEdges(); EdgeNo < End;
274 ++EdgeNo) {
Yuchen Wuba718332013-12-03 00:15:49 +0000275 if (Count == 0) {
Yuchen Wu57529972013-12-04 05:42:28 +0000276 errs() << "Unexpected number of edges (in " << Name << ").\n";
Yuchen Wuba718332013-12-03 00:15:49 +0000277 return false;
278 }
279 uint64_t ArcCount;
280 if (!Buff.readInt64(ArcCount)) return false;
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000281 Block.addCount(EdgeNo, ArcCount);
Yuchen Wuba718332013-12-03 00:15:49 +0000282 --Count;
283 }
Yuchen Wu342714c2013-12-13 01:15:07 +0000284 Block.sortDstEdges();
Yuchen Wuba718332013-12-03 00:15:49 +0000285 }
286 return true;
287}
288
Yuchen Wu342714c2013-12-13 01:15:07 +0000289/// getEntryCount - Get the number of times the function was called by
290/// retrieving the entry block's count.
291uint64_t GCOVFunction::getEntryCount() const {
292 return Blocks.front()->getCount();
293}
294
295/// getExitCount - Get the number of times the function returned by retrieving
296/// the exit block's count.
297uint64_t GCOVFunction::getExitCount() const {
298 return Blocks.back()->getCount();
299}
300
Yuchen Wu03678152013-10-25 02:22:24 +0000301/// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
Yuchen Wuef6909d2013-11-19 20:33:32 +0000302void GCOVFunction::dump() const {
Yuchen Wu03678152013-10-25 02:22:24 +0000303 dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
Yuchen Wuef6909d2013-11-19 20:33:32 +0000304 for (SmallVectorImpl<GCOVBlock *>::const_iterator I = Blocks.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000305 E = Blocks.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000306 (*I)->dump();
307}
308
309/// collectLineCounts - Collect line counts. This must be used after
310/// reading .gcno and .gcda files.
311void GCOVFunction::collectLineCounts(FileInfo &FI) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000312 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000313 E = Blocks.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000314 (*I)->collectLineCounts(FI);
Yuchen Wu342714c2013-12-13 01:15:07 +0000315 FI.addFunctionLine(Filename, LineNumber, this);
Devang Patel37140652011-09-28 18:50:00 +0000316}
317
318//===----------------------------------------------------------------------===//
319// GCOVBlock implementation.
320
321/// ~GCOVBlock - Delete GCOVBlock and its content.
322GCOVBlock::~GCOVBlock() {
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000323 SrcEdges.clear();
324 DstEdges.clear();
Yuchen Wud738bee2013-11-14 00:32:00 +0000325 Lines.clear();
Devang Patel37140652011-09-28 18:50:00 +0000326}
327
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000328/// addCount - Add to block counter while storing the edge count. If the
329/// destination has no outgoing edges, also update that block's count too.
330void GCOVBlock::addCount(size_t DstEdgeNo, uint64_t N) {
331 assert(DstEdgeNo < DstEdges.size()); // up to caller to ensure EdgeNo is valid
332 DstEdges[DstEdgeNo]->Count = N;
333 Counter += N;
334 if (!DstEdges[DstEdgeNo]->Dst->getNumDstEdges())
335 DstEdges[DstEdgeNo]->Dst->Counter += N;
336}
337
Yuchen Wu342714c2013-12-13 01:15:07 +0000338/// sortDstEdges - Sort destination edges by block number, nop if already
339/// sorted. This is required for printing branch info in the correct order.
340void GCOVBlock::sortDstEdges() {
341 if (!DstEdgesAreSorted) {
342 SortDstEdgesFunctor SortEdges;
343 std::stable_sort(DstEdges.begin(), DstEdges.end(), SortEdges);
344 }
345}
346
Devang Patel37140652011-09-28 18:50:00 +0000347/// collectLineCounts - Collect line counts. This must be used after
348/// reading .gcno and .gcda files.
349void GCOVBlock::collectLineCounts(FileInfo &FI) {
Yuchen Wud738bee2013-11-14 00:32:00 +0000350 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000351 E = Lines.end(); I != E; ++I)
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000352 FI.addBlockLine(Parent.getFilename(), *I, this);
Devang Patel37140652011-09-28 18:50:00 +0000353}
354
Yuchen Wu03678152013-10-25 02:22:24 +0000355/// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
Yuchen Wuef6909d2013-11-19 20:33:32 +0000356void GCOVBlock::dump() const {
Yuchen Wu03678152013-10-25 02:22:24 +0000357 dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000358 if (!SrcEdges.empty()) {
359 dbgs() << "\tSource Edges : ";
360 for (EdgeIterator I = SrcEdges.begin(), E = SrcEdges.end(); I != E; ++I) {
361 const GCOVEdge *Edge = *I;
362 dbgs() << Edge->Src->Number << " (" << Edge->Count << "), ";
363 }
364 dbgs() << "\n";
365 }
366 if (!DstEdges.empty()) {
367 dbgs() << "\tDestination Edges : ";
368 for (EdgeIterator I = DstEdges.begin(), E = DstEdges.end(); I != E; ++I) {
369 const GCOVEdge *Edge = *I;
370 dbgs() << Edge->Dst->Number << " (" << Edge->Count << "), ";
371 }
Yuchen Wu03678152013-10-25 02:22:24 +0000372 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000373 }
374 if (!Lines.empty()) {
Yuchen Wu03678152013-10-25 02:22:24 +0000375 dbgs() << "\tLines : ";
Yuchen Wuef6909d2013-11-19 20:33:32 +0000376 for (SmallVectorImpl<uint32_t>::const_iterator I = Lines.begin(),
Yuchen Wud738bee2013-11-14 00:32:00 +0000377 E = Lines.end(); I != E; ++I)
378 dbgs() << (*I) << ",";
379 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000380 }
381}
382
383//===----------------------------------------------------------------------===//
Devang Patel37140652011-09-28 18:50:00 +0000384// FileInfo implementation.
385
Yuchen Wu342714c2013-12-13 01:15:07 +0000386// Safe integer division, returns 0 if numerator is 0.
387static uint32_t safeDiv(uint64_t Numerator, uint64_t Divisor) {
388 if (!Numerator)
389 return 0;
390 return Numerator/Divisor;
391}
392
393// This custom division function mimics gcov's branch ouputs:
394// - Round to closest whole number
395// - Only output 0% or 100% if it's exactly that value
396static uint32_t branchDiv(uint64_t Numerator, uint64_t Divisor) {
397 if (!Numerator)
398 return 0;
399 if (Numerator == Divisor)
400 return 100;
401
402 uint8_t Res = (Numerator*100+Divisor/2) / Divisor;
403 if (Res == 0)
404 return 1;
405 if (Res == 100)
406 return 99;
407 return Res;
408}
409
Yuchen Wu73dc3812013-12-18 18:40:15 +0000410struct formatBranchInfo {
411 formatBranchInfo(const GCOVOptions &Options, uint64_t Count,
412 uint64_t Total) :
413 Options(Options), Count(Count), Total(Total) {}
414
415 void print(raw_ostream &OS) const {
416 if (!Total)
417 OS << "never executed";
418 else if (Options.BranchCount)
419 OS << "taken " << Count;
420 else
421 OS << "taken " << branchDiv(Count, Total) << "%";
422 }
423
424 const GCOVOptions &Options;
425 uint64_t Count;
426 uint64_t Total;
427};
428
429static raw_ostream &operator<<(raw_ostream &OS, const formatBranchInfo &FBI) {
430 FBI.print(OS);
431 return OS;
432}
433
Justin Bognerc6af3502014-02-04 10:45:02 +0000434/// Convert a path to a gcov filename. If PreservePaths is true, this
435/// translates "/" to "#", ".." to "^", and drops ".", to match gcov.
436static std::string mangleCoveragePath(StringRef Filename, bool PreservePaths) {
437 if (!PreservePaths)
438 return (sys::path::filename(Filename) + ".gcov").str();
439
440 // This behaviour is defined by gcov in terms of text replacements, so it's
441 // not likely to do anything useful on filesystems with different textual
442 // conventions.
443 llvm::SmallString<256> Result("");
444 StringRef::iterator I, S, E;
445 for (I = S = Filename.begin(), E = Filename.end(); I != E; ++I) {
446 if (*I != '/')
447 continue;
448
449 if (I - S == 1 && *S == '.') {
450 // ".", the current directory, is skipped.
451 } else if (I - S == 2 && *S == '.' && *(S + 1) == '.') {
452 // "..", the parent directory, is replaced with "^".
453 Result.append("^#");
454 } else {
455 if (S < I)
456 // Leave other components intact,
457 Result.append(S, I);
458 // And separate with "#".
459 Result.push_back('#');
460 }
461 S = I + 1;
462 }
463
464 if (S < I)
465 Result.append(S, I);
466 Result.append(".gcov");
467 return Result.str();
468}
469
Devang Patel37140652011-09-28 18:50:00 +0000470/// print - Print source files with collected line count information.
Yuchen Wubb6a4772013-12-19 00:29:25 +0000471void FileInfo::print(StringRef GCNOFile, StringRef GCDAFile) {
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000472 for (StringMap<LineData>::const_iterator I = LineInfo.begin(),
Yuchen Wuef6909d2013-11-19 20:33:32 +0000473 E = LineInfo.end(); I != E; ++I) {
Devang Patel37140652011-09-28 18:50:00 +0000474 StringRef Filename = I->first();
Devang Patel37140652011-09-28 18:50:00 +0000475 OwningPtr<MemoryBuffer> Buff;
476 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
477 errs() << Filename << ": " << ec.message() << "\n";
478 return;
479 }
Benjamin Kramer67421c12013-11-15 09:44:17 +0000480 StringRef AllLines = Buff->getBuffer();
Yuchen Wu8aac4f62013-11-19 20:57:20 +0000481
Justin Bognerc6af3502014-02-04 10:45:02 +0000482 std::string CoveragePath = mangleCoveragePath(Filename,
483 Options.PreservePaths);
Yuchen Wu26326ad2013-12-03 00:57:11 +0000484 std::string ErrorInfo;
Justin Bognerc6af3502014-02-04 10:45:02 +0000485 raw_fd_ostream OS(CoveragePath.c_str(), ErrorInfo);
Yuchen Wu26326ad2013-12-03 00:57:11 +0000486 if (!ErrorInfo.empty())
487 errs() << ErrorInfo << "\n";
488
Yuchen Wu8aac4f62013-11-19 20:57:20 +0000489 OS << " -: 0:Source:" << Filename << "\n";
Yuchen Wu21517e42013-12-04 05:07:36 +0000490 OS << " -: 0:Graph:" << GCNOFile << "\n";
491 OS << " -: 0:Data:" << GCDAFile << "\n";
Yuchen Wu8aac4f62013-11-19 20:57:20 +0000492 OS << " -: 0:Runs:" << RunCount << "\n";
493 OS << " -: 0:Programs:" << ProgramCount << "\n";
494
Yuchen Wu1c068162013-12-03 01:35:31 +0000495 const LineData &Line = I->second;
Yuchen Wubb6a4772013-12-19 00:29:25 +0000496 GCOVCoverage FileCoverage(Filename);
Yuchen Wu342714c2013-12-13 01:15:07 +0000497 for (uint32_t LineIndex = 0; !AllLines.empty(); ++LineIndex) {
Yuchen Wu73dc3812013-12-18 18:40:15 +0000498 if (Options.BranchInfo) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000499 FunctionLines::const_iterator FuncsIt = Line.Functions.find(LineIndex);
500 if (FuncsIt != Line.Functions.end())
501 printFunctionSummary(OS, FuncsIt->second);
502 }
Yuchen Wu1c068162013-12-03 01:35:31 +0000503
Yuchen Wu342714c2013-12-13 01:15:07 +0000504 BlockLines::const_iterator BlocksIt = Line.Blocks.find(LineIndex);
505 if (BlocksIt == Line.Blocks.end()) {
506 // No basic blocks are on this line. Not an executable line of code.
507 OS << " -:";
508 std::pair<StringRef, StringRef> P = AllLines.split('\n');
509 OS << format("%5u:", LineIndex+1) << P.first << "\n";
510 AllLines = P.second;
511 } else {
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000512 const BlockVector &Blocks = BlocksIt->second;
Yuchen Wu342714c2013-12-13 01:15:07 +0000513
514 // Add up the block counts to form line counts.
Yuchen Wubb6a4772013-12-19 00:29:25 +0000515 DenseMap<const GCOVFunction *, bool> LineExecs;
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000516 uint64_t LineCount = 0;
517 for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end();
518 I != E; ++I) {
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000519 const GCOVBlock *Block = *I;
520 if (Options.AllBlocks) {
521 // Only take the highest block count for that line.
522 uint64_t BlockCount = Block->getCount();
523 LineCount = LineCount > BlockCount ? LineCount : BlockCount;
524 } else {
525 // Sum up all of the block counts.
526 LineCount += Block->getCount();
527 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000528
529 if (Options.FuncCoverage) {
530 // This is a slightly convoluted way to most accurately gather line
531 // statistics for functions. Basically what is happening is that we
532 // don't want to count a single line with multiple blocks more than
533 // once. However, we also don't simply want to give the total line
534 // count to every function that starts on the line. Thus, what is
535 // happening here are two things:
536 // 1) Ensure that the number of logical lines is only incremented
537 // once per function.
538 // 2) If there are multiple blocks on the same line, ensure that the
539 // number of lines executed is incremented as long as at least
540 // one of the blocks are executed.
541 const GCOVFunction *Function = &Block->getParent();
542 if (FuncCoverages.find(Function) == FuncCoverages.end()) {
543 std::pair<const GCOVFunction *, GCOVCoverage>
544 KeyValue(Function, GCOVCoverage(Function->getName()));
545 FuncCoverages.insert(KeyValue);
546 }
547 GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second;
548
549 if (LineExecs.find(Function) == LineExecs.end()) {
550 if (Block->getCount()) {
551 ++FuncCoverage.LinesExec;
552 LineExecs[Function] = true;
553 } else {
554 LineExecs[Function] = false;
555 }
556 ++FuncCoverage.LogicalLines;
557 } else if (!LineExecs[Function] && Block->getCount()) {
558 ++FuncCoverage.LinesExec;
559 LineExecs[Function] = true;
560 }
561 }
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000562 }
Yuchen Wu8256ee62013-12-18 21:12:51 +0000563
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000564 if (LineCount == 0)
Yuchen Wudbcf1972013-11-02 00:09:17 +0000565 OS << " #####:";
Yuchen Wu8256ee62013-12-18 21:12:51 +0000566 else {
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000567 OS << format("%9" PRIu64 ":", LineCount);
Yuchen Wubb6a4772013-12-19 00:29:25 +0000568 ++FileCoverage.LinesExec;
Yuchen Wu8256ee62013-12-18 21:12:51 +0000569 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000570 ++FileCoverage.LogicalLines;
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000571
Yuchen Wu342714c2013-12-13 01:15:07 +0000572 std::pair<StringRef, StringRef> P = AllLines.split('\n');
573 OS << format("%5u:", LineIndex+1) << P.first << "\n";
574 AllLines = P.second;
575
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000576 uint32_t BlockNo = 0;
Yuchen Wu342714c2013-12-13 01:15:07 +0000577 uint32_t EdgeNo = 0;
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000578 for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end();
579 I != E; ++I) {
580 const GCOVBlock *Block = *I;
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000581
Yuchen Wu342714c2013-12-13 01:15:07 +0000582 // Only print block and branch information at the end of the block.
583 if (Block->getLastLine() != LineIndex+1)
584 continue;
585 if (Options.AllBlocks)
586 printBlockInfo(OS, *Block, LineIndex, BlockNo);
Yuchen Wu73dc3812013-12-18 18:40:15 +0000587 if (Options.BranchInfo) {
Yuchen Wu66d93b82013-12-16 22:14:02 +0000588 size_t NumEdges = Block->getNumDstEdges();
589 if (NumEdges > 1)
Yuchen Wubb6a4772013-12-19 00:29:25 +0000590 printBranchInfo(OS, *Block, FileCoverage, EdgeNo);
Yuchen Wu66d93b82013-12-16 22:14:02 +0000591 else if (Options.UncondBranch && NumEdges == 1)
592 printUncondBranchInfo(OS, EdgeNo, (*Block->dst_begin())->Count);
593 }
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000594 }
595 }
Devang Patel37140652011-09-28 18:50:00 +0000596 }
Justin Bognerc6af3502014-02-04 10:45:02 +0000597 FileCoverages.push_back(std::make_pair(CoveragePath, FileCoverage));
Devang Patel37140652011-09-28 18:50:00 +0000598 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000599
600 // FIXME: There is no way to detect calls given current instrumentation.
601 if (Options.FuncCoverage)
602 printFuncCoverage();
603 printFileCoverage();
Devang Patel37140652011-09-28 18:50:00 +0000604}
Yuchen Wu342714c2013-12-13 01:15:07 +0000605
606/// printFunctionSummary - Print function and block summary.
607void FileInfo::printFunctionSummary(raw_fd_ostream &OS,
608 const FunctionVector &Funcs) const {
609 for (FunctionVector::const_iterator I = Funcs.begin(), E = Funcs.end();
610 I != E; ++I) {
611 const GCOVFunction *Func = *I;
612 uint64_t EntryCount = Func->getEntryCount();
Yuchen Wuc9b2dcd2013-12-18 18:46:25 +0000613 uint32_t BlocksExec = 0;
Yuchen Wu342714c2013-12-13 01:15:07 +0000614 for (GCOVFunction::BlockIterator I = Func->block_begin(),
615 E = Func->block_end(); I != E; ++I) {
616 const GCOVBlock *Block = *I;
617 if (Block->getNumDstEdges() && Block->getCount())
Yuchen Wuc9b2dcd2013-12-18 18:46:25 +0000618 ++BlocksExec;
Yuchen Wu342714c2013-12-13 01:15:07 +0000619 }
620
621 OS << "function " << Func->getName() << " called " << EntryCount
622 << " returned " << safeDiv(Func->getExitCount()*100, EntryCount)
623 << "% blocks executed "
Yuchen Wuc9b2dcd2013-12-18 18:46:25 +0000624 << safeDiv(BlocksExec*100, Func->getNumBlocks()-1) << "%\n";
Yuchen Wu342714c2013-12-13 01:15:07 +0000625 }
626}
627
628/// printBlockInfo - Output counts for each block.
629void FileInfo::printBlockInfo(raw_fd_ostream &OS, const GCOVBlock &Block,
630 uint32_t LineIndex, uint32_t &BlockNo) const {
631 if (Block.getCount() == 0)
632 OS << " $$$$$:";
633 else
634 OS << format("%9" PRIu64 ":", Block.getCount());
635 OS << format("%5u-block %2u\n", LineIndex+1, BlockNo++);
636}
637
Yuchen Wu66d93b82013-12-16 22:14:02 +0000638/// printBranchInfo - Print conditional branch probabilities.
Yuchen Wu342714c2013-12-13 01:15:07 +0000639void FileInfo::printBranchInfo(raw_fd_ostream &OS, const GCOVBlock &Block,
Yuchen Wubb6a4772013-12-19 00:29:25 +0000640 GCOVCoverage &Coverage, uint32_t &EdgeNo) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000641 SmallVector<uint64_t, 16> BranchCounts;
642 uint64_t TotalCounts = 0;
643 for (GCOVBlock::EdgeIterator I = Block.dst_begin(), E = Block.dst_end();
644 I != E; ++I) {
645 const GCOVEdge *Edge = *I;
646 BranchCounts.push_back(Edge->Count);
647 TotalCounts += Edge->Count;
Yuchen Wu8256ee62013-12-18 21:12:51 +0000648 if (Block.getCount()) ++Coverage.BranchesExec;
649 if (Edge->Count) ++Coverage.BranchesTaken;
650 ++Coverage.Branches;
Yuchen Wubb6a4772013-12-19 00:29:25 +0000651
652 if (Options.FuncCoverage) {
653 const GCOVFunction *Function = &Block.getParent();
654 GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second;
655 if (Block.getCount()) ++FuncCoverage.BranchesExec;
656 if (Edge->Count) ++FuncCoverage.BranchesTaken;
657 ++FuncCoverage.Branches;
658 }
Yuchen Wu342714c2013-12-13 01:15:07 +0000659 }
660
661 for (SmallVectorImpl<uint64_t>::const_iterator I = BranchCounts.begin(),
662 E = BranchCounts.end(); I != E; ++I) {
Yuchen Wu73dc3812013-12-18 18:40:15 +0000663 OS << format("branch %2u ", EdgeNo++)
664 << formatBranchInfo(Options, *I, TotalCounts) << "\n";
Yuchen Wu342714c2013-12-13 01:15:07 +0000665 }
666}
Yuchen Wu66d93b82013-12-16 22:14:02 +0000667
668/// printUncondBranchInfo - Print unconditional branch probabilities.
669void FileInfo::printUncondBranchInfo(raw_fd_ostream &OS, uint32_t &EdgeNo,
670 uint64_t Count) const {
Yuchen Wu73dc3812013-12-18 18:40:15 +0000671 OS << format("unconditional %2u ", EdgeNo++)
672 << formatBranchInfo(Options, Count, Count) << "\n";
Yuchen Wu66d93b82013-12-16 22:14:02 +0000673}
Yuchen Wu8256ee62013-12-18 21:12:51 +0000674
Yuchen Wubb6a4772013-12-19 00:29:25 +0000675// printCoverage - Print generic coverage info used by both printFuncCoverage
676// and printFileCoverage.
677void FileInfo::printCoverage(const GCOVCoverage &Coverage) const {
NAKAMURA Takumi6e3c4232013-12-19 08:46:28 +0000678 outs() << format("Lines executed:%.2f%% of %u\n",
Yuchen Wu8256ee62013-12-18 21:12:51 +0000679 double(Coverage.LinesExec)*100/Coverage.LogicalLines,
680 Coverage.LogicalLines);
681 if (Options.BranchInfo) {
682 if (Coverage.Branches) {
NAKAMURA Takumi6e3c4232013-12-19 08:46:28 +0000683 outs() << format("Branches executed:%.2f%% of %u\n",
Yuchen Wu8256ee62013-12-18 21:12:51 +0000684 double(Coverage.BranchesExec)*100/Coverage.Branches,
685 Coverage.Branches);
NAKAMURA Takumi6e3c4232013-12-19 08:46:28 +0000686 outs() << format("Taken at least once:%.2f%% of %u\n",
Yuchen Wu8256ee62013-12-18 21:12:51 +0000687 double(Coverage.BranchesTaken)*100/Coverage.Branches,
688 Coverage.Branches);
689 } else {
690 outs() << "No branches\n";
691 }
692 outs() << "No calls\n"; // to be consistent with gcov
693 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000694}
695
696// printFuncCoverage - Print per-function coverage info.
697void FileInfo::printFuncCoverage() const {
Justin Bognerc6af3502014-02-04 10:45:02 +0000698 for (FuncCoverageMap::const_iterator I = FuncCoverages.begin(),
699 E = FuncCoverages.end(); I != E; ++I) {
Yuchen Wubb6a4772013-12-19 00:29:25 +0000700 const GCOVCoverage &Coverage = I->second;
701 outs() << "Function '" << Coverage.Name << "'\n";
702 printCoverage(Coverage);
703 outs() << "\n";
704 }
705}
706
707// printFileCoverage - Print per-file coverage info.
708void FileInfo::printFileCoverage() const {
Justin Bognerc6af3502014-02-04 10:45:02 +0000709 for (FileCoverageList::const_iterator I = FileCoverages.begin(),
710 E = FileCoverages.end(); I != E; ++I) {
711 const std::string &Filename = I->first;
712 const GCOVCoverage &Coverage = I->second;
Yuchen Wubb6a4772013-12-19 00:29:25 +0000713 outs() << "File '" << Coverage.Name << "'\n";
714 printCoverage(Coverage);
Justin Bognerc6af3502014-02-04 10:45:02 +0000715 outs() << Coverage.Name << ":creating '" << Filename << "'\n\n";
Yuchen Wubb6a4772013-12-19 00:29:25 +0000716 }
Yuchen Wu8256ee62013-12-18 21:12:51 +0000717}