blob: 566c5b9d59dfcf2d7aeb24dd510d39a088bef790 [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
Justin Bognerdf82c622014-02-04 21:03:17 +000015#include "llvm/Support/GCOV.h"
Devang Patela9e8a252011-09-29 16:46:47 +000016#include "llvm/ADT/STLExtras.h"
Justin Bognerdf82c622014-02-04 21:03:17 +000017#include "llvm/Support/Debug.h"
Justin Bognerc6af3502014-02-04 10:45:02 +000018#include "llvm/Support/FileSystem.h"
Justin Bognerdf82c622014-02-04 21:03:17 +000019#include "llvm/Support/Format.h"
Devang Patel37140652011-09-28 18:50:00 +000020#include "llvm/Support/MemoryObject.h"
Justin Bognerc6af3502014-02-04 10:45:02 +000021#include "llvm/Support/Path.h"
Yuchen Wu342714c2013-12-13 01:15:07 +000022#include <algorithm>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000023#include <system_error>
Devang Patel37140652011-09-28 18:50:00 +000024using namespace llvm;
25
26//===----------------------------------------------------------------------===//
27// GCOVFile implementation.
28
Yuchen Wubec4e902013-12-04 04:49:23 +000029/// readGCNO - Read GCNO buffer.
30bool GCOVFile::readGCNO(GCOVBuffer &Buffer) {
31 if (!Buffer.readGCNOFormat()) return false;
32 if (!Buffer.readGCOVVersion(Version)) return false;
33
34 if (!Buffer.readInt(Checksum)) return false;
35 while (true) {
36 if (!Buffer.readFunctionTag()) break;
David Blaikie09757492014-04-21 21:40:16 +000037 auto GFun = make_unique<GCOVFunction>(*this);
Yuchen Wubec4e902013-12-04 04:49:23 +000038 if (!GFun->readGCNO(Buffer, Version))
39 return false;
David Blaikie09757492014-04-21 21:40:16 +000040 Functions.push_back(std::move(GFun));
Yuchen Wubec4e902013-12-04 04:49:23 +000041 }
42
Yuchen Wu21517e42013-12-04 05:07:36 +000043 GCNOInitialized = true;
Yuchen Wubec4e902013-12-04 04:49:23 +000044 return true;
Devang Patele5a8f2f92011-09-29 17:06:40 +000045}
46
Yuchen Wubec4e902013-12-04 04:49:23 +000047/// readGCDA - Read GCDA buffer. It is required that readGCDA() can only be
48/// called after readGCNO().
49bool GCOVFile::readGCDA(GCOVBuffer &Buffer) {
Yuchen Wu21517e42013-12-04 05:07:36 +000050 assert(GCNOInitialized && "readGCDA() can only be called after readGCNO()");
Yuchen Wubec4e902013-12-04 04:49:23 +000051 if (!Buffer.readGCDAFormat()) return false;
52 GCOV::GCOVVersion GCDAVersion;
53 if (!Buffer.readGCOVVersion(GCDAVersion)) return false;
54 if (Version != GCDAVersion) {
55 errs() << "GCOV versions do not match.\n";
Devang Patel37140652011-09-28 18:50:00 +000056 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000057 }
Devang Patel37140652011-09-28 18:50:00 +000058
Yuchen Wubec4e902013-12-04 04:49:23 +000059 uint32_t GCDAChecksum;
60 if (!Buffer.readInt(GCDAChecksum)) return false;
61 if (Checksum != GCDAChecksum) {
Yuchen Wu57529972013-12-04 05:42:28 +000062 errs() << "File checksums do not match: " << Checksum << " != "
63 << GCDAChecksum << ".\n";
Yuchen Wubec4e902013-12-04 04:49:23 +000064 return false;
65 }
66 for (size_t i = 0, e = Functions.size(); i < e; ++i) {
67 if (!Buffer.readFunctionTag()) {
68 errs() << "Unexpected number of functions.\n";
Yuchen Wubabe7492013-11-20 04:15:05 +000069 return false;
70 }
Yuchen Wubec4e902013-12-04 04:49:23 +000071 if (!Functions[i]->readGCDA(Buffer, Version))
72 return false;
73 }
74 if (Buffer.readObjectTag()) {
75 uint32_t Length;
76 uint32_t Dummy;
77 if (!Buffer.readInt(Length)) return false;
78 if (!Buffer.readInt(Dummy)) return false; // checksum
79 if (!Buffer.readInt(Dummy)) return false; // num
Yuchen Wu8742a282013-12-16 20:03:11 +000080 if (!Buffer.readInt(RunCount)) return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000081 Buffer.advanceCursor(Length-3);
82 }
83 while (Buffer.readProgramTag()) {
84 uint32_t Length;
85 if (!Buffer.readInt(Length)) return false;
86 Buffer.advanceCursor(Length);
87 ++ProgramCount;
Yuchen Wu14ae8e62013-10-25 02:22:21 +000088 }
89
Devang Patel37140652011-09-28 18:50:00 +000090 return true;
91}
92
Yuchen Wu03678152013-10-25 02:22:24 +000093/// dump - Dump GCOVFile content to dbgs() for debugging purposes.
Yuchen Wuef6909d2013-11-19 20:33:32 +000094void GCOVFile::dump() const {
David Blaikie09757492014-04-21 21:40:16 +000095 for (const auto &FPtr : Functions)
96 FPtr->dump();
Devang Patel37140652011-09-28 18:50:00 +000097}
98
99/// collectLineCounts - Collect line counts. This must be used after
100/// reading .gcno and .gcda files.
101void GCOVFile::collectLineCounts(FileInfo &FI) {
David Blaikie09757492014-04-21 21:40:16 +0000102 for (const auto &FPtr : Functions)
103 FPtr->collectLineCounts(FI);
Yuchen Wu30672d92013-11-05 01:11:58 +0000104 FI.setRunCount(RunCount);
Yuchen Wu14ae8e62013-10-25 02:22:21 +0000105 FI.setProgramCount(ProgramCount);
Devang Patel37140652011-09-28 18:50:00 +0000106}
107
108//===----------------------------------------------------------------------===//
109// GCOVFunction implementation.
110
Yuchen Wuba718332013-12-03 00:15:49 +0000111/// readGCNO - Read a function from the GCNO buffer. Return false if an error
112/// occurs.
Yuchen Wubec4e902013-12-04 04:49:23 +0000113bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
Yuchen Wue28da842013-11-14 00:07:15 +0000114 uint32_t Dummy;
115 if (!Buff.readInt(Dummy)) return false; // Function header length
116 if (!Buff.readInt(Ident)) return false;
Daniel Jasper87a24d52013-12-04 08:57:17 +0000117 if (!Buff.readInt(Checksum)) return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000118 if (Version != GCOV::V402) {
119 uint32_t CfgChecksum;
120 if (!Buff.readInt(CfgChecksum)) return false;
121 if (Parent.getChecksum() != CfgChecksum) {
122 errs() << "File checksums do not match: " << Parent.getChecksum()
123 << " != " << CfgChecksum << " in (" << Name << ").\n";
124 return false;
125 }
126 }
Yuchen Wue28da842013-11-14 00:07:15 +0000127 if (!Buff.readString(Name)) return false;
Yuchen Wuba718332013-12-03 00:15:49 +0000128 if (!Buff.readString(Filename)) return false;
Yuchen Wue28da842013-11-14 00:07:15 +0000129 if (!Buff.readInt(LineNumber)) return false;
Devang Patel37140652011-09-28 18:50:00 +0000130
131 // read blocks.
Yuchen Wue28da842013-11-14 00:07:15 +0000132 if (!Buff.readBlockTag()) {
133 errs() << "Block tag not found.\n";
134 return false;
135 }
136 uint32_t BlockCount;
137 if (!Buff.readInt(BlockCount)) return false;
Bob Wilson868e6e32013-10-22 20:02:36 +0000138 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
Yuchen Wue28da842013-11-14 00:07:15 +0000139 if (!Buff.readInt(Dummy)) return false; // Block flags;
David Blaikie09757492014-04-21 21:40:16 +0000140 Blocks.push_back(make_unique<GCOVBlock>(*this, i));
Devang Patel37140652011-09-28 18:50:00 +0000141 }
142
143 // read edges.
144 while (Buff.readEdgeTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +0000145 uint32_t EdgeCount;
146 if (!Buff.readInt(EdgeCount)) return false;
147 EdgeCount = (EdgeCount - 1) / 2;
148 uint32_t BlockNo;
149 if (!Buff.readInt(BlockNo)) return false;
150 if (BlockNo >= BlockCount) {
Yuchen Wu4c9f19d2013-12-05 22:02:33 +0000151 errs() << "Unexpected block number: " << BlockNo << " (in " << Name
152 << ").\n";
Yuchen Wue28da842013-11-14 00:07:15 +0000153 return false;
154 }
Bob Wilson868e6e32013-10-22 20:02:36 +0000155 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
Yuchen Wue28da842013-11-14 00:07:15 +0000156 uint32_t Dst;
157 if (!Buff.readInt(Dst)) return false;
David Blaikie09757492014-04-21 21:40:16 +0000158 Edges.push_back(make_unique<GCOVEdge>(*Blocks[BlockNo], *Blocks[Dst]));
159 GCOVEdge *Edge = Edges.back().get();
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000160 Blocks[BlockNo]->addDstEdge(Edge);
161 Blocks[Dst]->addSrcEdge(Edge);
Yuchen Wue28da842013-11-14 00:07:15 +0000162 if (!Buff.readInt(Dummy)) return false; // Edge flag
Devang Patel37140652011-09-28 18:50:00 +0000163 }
164 }
165
166 // read line table.
167 while (Buff.readLineTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +0000168 uint32_t LineTableLength;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000169 // Read the length of this line table.
Yuchen Wue28da842013-11-14 00:07:15 +0000170 if (!Buff.readInt(LineTableLength)) return false;
Bob Wilson00928bc2013-10-22 19:54:32 +0000171 uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
Yuchen Wue28da842013-11-14 00:07:15 +0000172 uint32_t BlockNo;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000173 // Read the block number this table is associated with.
Yuchen Wue28da842013-11-14 00:07:15 +0000174 if (!Buff.readInt(BlockNo)) return false;
175 if (BlockNo >= BlockCount) {
Yuchen Wu4c9f19d2013-12-05 22:02:33 +0000176 errs() << "Unexpected block number: " << BlockNo << " (in " << Name
177 << ").\n";
Yuchen Wue28da842013-11-14 00:07:15 +0000178 return false;
Devang Patel37140652011-09-28 18:50:00 +0000179 }
David Blaikie09757492014-04-21 21:40:16 +0000180 GCOVBlock &Block = *Blocks[BlockNo];
Justin Bognerc475e1b2014-05-02 20:01:24 +0000181 // Read the word that pads the beginning of the line table. This may be a
182 // flag of some sort, but seems to always be zero.
183 if (!Buff.readInt(Dummy)) return false;
184
185 // Line information starts here and continues up until the last word.
186 if (Buff.getCursor() != (EndPos - sizeof(uint32_t))) {
Yuchen Wud738bee2013-11-14 00:32:00 +0000187 StringRef F;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000188 // Read the source file name.
Yuchen Wud738bee2013-11-14 00:32:00 +0000189 if (!Buff.readString(F)) return false;
Yuchen Wu4c9f19d2013-12-05 22:02:33 +0000190 if (Filename != F) {
191 errs() << "Multiple sources for a single basic block: " << Filename
192 << " != " << F << " (in " << Name << ").\n";
Yuchen Wud738bee2013-11-14 00:32:00 +0000193 return false;
194 }
Justin Bognerc475e1b2014-05-02 20:01:24 +0000195 // Read lines up to, but not including, the null terminator.
196 while (Buff.getCursor() < (EndPos - 2 * sizeof(uint32_t))) {
Yuchen Wue28da842013-11-14 00:07:15 +0000197 uint32_t Line;
198 if (!Buff.readInt(Line)) return false;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000199 // Line 0 means this instruction was injected by the compiler. Skip it.
200 if (!Line) continue;
David Blaikie09757492014-04-21 21:40:16 +0000201 Block.addLine(Line);
Yuchen Wue28da842013-11-14 00:07:15 +0000202 }
Justin Bognerc475e1b2014-05-02 20:01:24 +0000203 // Read the null terminator.
204 if (!Buff.readInt(Dummy)) return false;
Yuchen Wue28da842013-11-14 00:07:15 +0000205 }
Justin Bognerc475e1b2014-05-02 20:01:24 +0000206 // The last word is either a flag or padding, it isn't clear which. Skip
207 // over it.
208 if (!Buff.readInt(Dummy)) return false;
Devang Patel37140652011-09-28 18:50:00 +0000209 }
210 return true;
211}
212
Yuchen Wuba718332013-12-03 00:15:49 +0000213/// readGCDA - Read a function from the GCDA buffer. Return false if an error
214/// occurs.
Yuchen Wubec4e902013-12-04 04:49:23 +0000215bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
Yuchen Wuba718332013-12-03 00:15:49 +0000216 uint32_t Dummy;
217 if (!Buff.readInt(Dummy)) return false; // Function header length
Daniel Jasper87a24d52013-12-04 08:57:17 +0000218
Yuchen Wu57529972013-12-04 05:42:28 +0000219 uint32_t GCDAIdent;
220 if (!Buff.readInt(GCDAIdent)) return false;
221 if (Ident != GCDAIdent) {
222 errs() << "Function identifiers do not match: " << Ident << " != "
223 << GCDAIdent << " (in " << Name << ").\n";
224 return false;
225 }
Yuchen Wuba718332013-12-03 00:15:49 +0000226
Daniel Jasper87a24d52013-12-04 08:57:17 +0000227 uint32_t GCDAChecksum;
228 if (!Buff.readInt(GCDAChecksum)) return false;
229 if (Checksum != GCDAChecksum) {
230 errs() << "Function checksums do not match: " << Checksum << " != "
231 << GCDAChecksum << " (in " << Name << ").\n";
232 return false;
233 }
Yuchen Wu57529972013-12-04 05:42:28 +0000234
235 uint32_t CfgChecksum;
236 if (Version != GCOV::V402) {
237 if (!Buff.readInt(CfgChecksum)) return false;
238 if (Parent.getChecksum() != CfgChecksum) {
239 errs() << "File checksums do not match: " << Parent.getChecksum()
240 << " != " << CfgChecksum << " (in " << Name << ").\n";
241 return false;
242 }
243 }
244
245 StringRef GCDAName;
246 if (!Buff.readString(GCDAName)) return false;
247 if (Name != GCDAName) {
248 errs() << "Function names do not match: " << Name << " != " << GCDAName
249 << ".\n";
250 return false;
251 }
Yuchen Wuba718332013-12-03 00:15:49 +0000252
253 if (!Buff.readArcTag()) {
Yuchen Wu57529972013-12-04 05:42:28 +0000254 errs() << "Arc tag not found (in " << Name << ").\n";
Yuchen Wuba718332013-12-03 00:15:49 +0000255 return false;
256 }
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000257
Yuchen Wuba718332013-12-03 00:15:49 +0000258 uint32_t Count;
259 if (!Buff.readInt(Count)) return false;
260 Count /= 2;
261
262 // This for loop adds the counts for each block. A second nested loop is
263 // required to combine the edge counts that are contained in the GCDA file.
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000264 for (uint32_t BlockNo = 0; Count > 0; ++BlockNo) {
265 // The last block is always reserved for exit block
266 if (BlockNo >= Blocks.size()-1) {
Yuchen Wu57529972013-12-04 05:42:28 +0000267 errs() << "Unexpected number of edges (in " << Name << ").\n";
Yuchen Wuba718332013-12-03 00:15:49 +0000268 return false;
269 }
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000270 GCOVBlock &Block = *Blocks[BlockNo];
271 for (size_t EdgeNo = 0, End = Block.getNumDstEdges(); EdgeNo < End;
272 ++EdgeNo) {
Yuchen Wuba718332013-12-03 00:15:49 +0000273 if (Count == 0) {
Yuchen Wu57529972013-12-04 05:42:28 +0000274 errs() << "Unexpected number of edges (in " << Name << ").\n";
Yuchen Wuba718332013-12-03 00:15:49 +0000275 return false;
276 }
277 uint64_t ArcCount;
278 if (!Buff.readInt64(ArcCount)) return false;
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000279 Block.addCount(EdgeNo, ArcCount);
Yuchen Wuba718332013-12-03 00:15:49 +0000280 --Count;
281 }
Yuchen Wu342714c2013-12-13 01:15:07 +0000282 Block.sortDstEdges();
Yuchen Wuba718332013-12-03 00:15:49 +0000283 }
284 return true;
285}
286
Yuchen Wu342714c2013-12-13 01:15:07 +0000287/// getEntryCount - Get the number of times the function was called by
288/// retrieving the entry block's count.
289uint64_t GCOVFunction::getEntryCount() const {
290 return Blocks.front()->getCount();
291}
292
293/// getExitCount - Get the number of times the function returned by retrieving
294/// the exit block's count.
295uint64_t GCOVFunction::getExitCount() const {
296 return Blocks.back()->getCount();
297}
298
Yuchen Wu03678152013-10-25 02:22:24 +0000299/// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
Yuchen Wuef6909d2013-11-19 20:33:32 +0000300void GCOVFunction::dump() const {
Yuchen Wu03678152013-10-25 02:22:24 +0000301 dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
David Blaikie09757492014-04-21 21:40:16 +0000302 for (const auto &Block : Blocks)
303 Block->dump();
Devang Patel37140652011-09-28 18:50:00 +0000304}
305
306/// collectLineCounts - Collect line counts. This must be used after
307/// reading .gcno and .gcda files.
308void GCOVFunction::collectLineCounts(FileInfo &FI) {
Justin Bogner95e0a702014-03-26 22:03:06 +0000309 // If the line number is zero, this is a function that doesn't actually appear
310 // in the source file, so there isn't anything we can do with it.
311 if (LineNumber == 0)
312 return;
313
David Blaikie09757492014-04-21 21:40:16 +0000314 for (const auto &Block : Blocks)
315 Block->collectLineCounts(FI);
Yuchen Wu342714c2013-12-13 01:15:07 +0000316 FI.addFunctionLine(Filename, LineNumber, this);
Devang Patel37140652011-09-28 18:50:00 +0000317}
318
319//===----------------------------------------------------------------------===//
320// GCOVBlock implementation.
321
322/// ~GCOVBlock - Delete GCOVBlock and its content.
323GCOVBlock::~GCOVBlock() {
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000324 SrcEdges.clear();
325 DstEdges.clear();
Yuchen Wud738bee2013-11-14 00:32:00 +0000326 Lines.clear();
Devang Patel37140652011-09-28 18:50:00 +0000327}
328
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000329/// addCount - Add to block counter while storing the edge count. If the
330/// destination has no outgoing edges, also update that block's count too.
331void GCOVBlock::addCount(size_t DstEdgeNo, uint64_t N) {
332 assert(DstEdgeNo < DstEdges.size()); // up to caller to ensure EdgeNo is valid
333 DstEdges[DstEdgeNo]->Count = N;
334 Counter += N;
David Blaikie09757492014-04-21 21:40:16 +0000335 if (!DstEdges[DstEdgeNo]->Dst.getNumDstEdges())
336 DstEdges[DstEdgeNo]->Dst.Counter += N;
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000337}
338
Yuchen Wu342714c2013-12-13 01:15:07 +0000339/// sortDstEdges - Sort destination edges by block number, nop if already
340/// sorted. This is required for printing branch info in the correct order.
341void GCOVBlock::sortDstEdges() {
342 if (!DstEdgesAreSorted) {
343 SortDstEdgesFunctor SortEdges;
344 std::stable_sort(DstEdges.begin(), DstEdges.end(), SortEdges);
345 }
346}
347
Devang Patel37140652011-09-28 18:50:00 +0000348/// collectLineCounts - Collect line counts. This must be used after
349/// reading .gcno and .gcda files.
350void GCOVBlock::collectLineCounts(FileInfo &FI) {
Yuchen Wud738bee2013-11-14 00:32:00 +0000351 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000352 E = Lines.end(); I != E; ++I)
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000353 FI.addBlockLine(Parent.getFilename(), *I, this);
Devang Patel37140652011-09-28 18:50:00 +0000354}
355
Yuchen Wu03678152013-10-25 02:22:24 +0000356/// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
Yuchen Wuef6909d2013-11-19 20:33:32 +0000357void GCOVBlock::dump() const {
Yuchen Wu03678152013-10-25 02:22:24 +0000358 dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000359 if (!SrcEdges.empty()) {
360 dbgs() << "\tSource Edges : ";
361 for (EdgeIterator I = SrcEdges.begin(), E = SrcEdges.end(); I != E; ++I) {
362 const GCOVEdge *Edge = *I;
David Blaikie09757492014-04-21 21:40:16 +0000363 dbgs() << Edge->Src.Number << " (" << Edge->Count << "), ";
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000364 }
365 dbgs() << "\n";
366 }
367 if (!DstEdges.empty()) {
368 dbgs() << "\tDestination Edges : ";
369 for (EdgeIterator I = DstEdges.begin(), E = DstEdges.end(); I != E; ++I) {
370 const GCOVEdge *Edge = *I;
David Blaikie09757492014-04-21 21:40:16 +0000371 dbgs() << Edge->Dst.Number << " (" << Edge->Count << "), ";
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000372 }
Yuchen Wu03678152013-10-25 02:22:24 +0000373 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000374 }
375 if (!Lines.empty()) {
Yuchen Wu03678152013-10-25 02:22:24 +0000376 dbgs() << "\tLines : ";
Yuchen Wuef6909d2013-11-19 20:33:32 +0000377 for (SmallVectorImpl<uint32_t>::const_iterator I = Lines.begin(),
Yuchen Wud738bee2013-11-14 00:32:00 +0000378 E = Lines.end(); I != E; ++I)
379 dbgs() << (*I) << ",";
380 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000381 }
382}
383
384//===----------------------------------------------------------------------===//
Devang Patel37140652011-09-28 18:50:00 +0000385// FileInfo implementation.
386
Yuchen Wu342714c2013-12-13 01:15:07 +0000387// Safe integer division, returns 0 if numerator is 0.
388static uint32_t safeDiv(uint64_t Numerator, uint64_t Divisor) {
389 if (!Numerator)
390 return 0;
391 return Numerator/Divisor;
392}
393
394// This custom division function mimics gcov's branch ouputs:
395// - Round to closest whole number
396// - Only output 0% or 100% if it's exactly that value
397static uint32_t branchDiv(uint64_t Numerator, uint64_t Divisor) {
398 if (!Numerator)
399 return 0;
400 if (Numerator == Divisor)
401 return 100;
402
403 uint8_t Res = (Numerator*100+Divisor/2) / Divisor;
404 if (Res == 0)
405 return 1;
406 if (Res == 100)
407 return 99;
408 return Res;
409}
410
Yuchen Wu73dc3812013-12-18 18:40:15 +0000411struct formatBranchInfo {
412 formatBranchInfo(const GCOVOptions &Options, uint64_t Count,
413 uint64_t Total) :
414 Options(Options), Count(Count), Total(Total) {}
415
416 void print(raw_ostream &OS) const {
417 if (!Total)
418 OS << "never executed";
419 else if (Options.BranchCount)
420 OS << "taken " << Count;
421 else
422 OS << "taken " << branchDiv(Count, Total) << "%";
423 }
424
425 const GCOVOptions &Options;
426 uint64_t Count;
427 uint64_t Total;
428};
429
430static raw_ostream &operator<<(raw_ostream &OS, const formatBranchInfo &FBI) {
431 FBI.print(OS);
432 return OS;
433}
434
Justin Bognercf27e1b2014-05-07 02:11:23 +0000435namespace {
436class LineConsumer {
437 std::unique_ptr<MemoryBuffer> Buffer;
438 StringRef Remaining;
439public:
440 LineConsumer(StringRef Filename) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000441 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
442 MemoryBuffer::getFileOrSTDIN(Filename);
443 if (std::error_code EC = BufferOrErr.getError()) {
Justin Bognercf27e1b2014-05-07 02:11:23 +0000444 errs() << Filename << ": " << EC.message() << "\n";
445 Remaining = "";
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000446 } else {
447 Buffer = std::move(BufferOrErr.get());
Justin Bognercf27e1b2014-05-07 02:11:23 +0000448 Remaining = Buffer->getBuffer();
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000449 }
Justin Bognercf27e1b2014-05-07 02:11:23 +0000450 }
451 bool empty() { return Remaining.empty(); }
452 void printNext(raw_ostream &OS, uint32_t LineNum) {
453 StringRef Line;
454 if (empty())
455 Line = "/*EOF*/";
456 else
457 std::tie(Line, Remaining) = Remaining.split("\n");
458 OS << format("%5u:", LineNum) << Line << "\n";
459 }
460};
461}
462
Justin Bognerc6af3502014-02-04 10:45:02 +0000463/// Convert a path to a gcov filename. If PreservePaths is true, this
464/// translates "/" to "#", ".." to "^", and drops ".", to match gcov.
465static std::string mangleCoveragePath(StringRef Filename, bool PreservePaths) {
466 if (!PreservePaths)
Justin Bognerc67f0252014-04-23 21:44:55 +0000467 return sys::path::filename(Filename).str();
Justin Bognerc6af3502014-02-04 10:45:02 +0000468
469 // This behaviour is defined by gcov in terms of text replacements, so it's
470 // not likely to do anything useful on filesystems with different textual
471 // conventions.
472 llvm::SmallString<256> Result("");
473 StringRef::iterator I, S, E;
474 for (I = S = Filename.begin(), E = Filename.end(); I != E; ++I) {
475 if (*I != '/')
476 continue;
477
478 if (I - S == 1 && *S == '.') {
479 // ".", the current directory, is skipped.
480 } else if (I - S == 2 && *S == '.' && *(S + 1) == '.') {
481 // "..", the parent directory, is replaced with "^".
482 Result.append("^#");
483 } else {
484 if (S < I)
485 // Leave other components intact,
486 Result.append(S, I);
487 // And separate with "#".
488 Result.push_back('#');
489 }
490 S = I + 1;
491 }
492
493 if (S < I)
494 Result.append(S, I);
Justin Bognerc6af3502014-02-04 10:45:02 +0000495 return Result.str();
496}
497
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000498std::string FileInfo::getCoveragePath(StringRef Filename,
499 StringRef MainFilename) {
500 if (Options.NoOutput)
501 // This is probably a bug in gcov, but when -n is specified, paths aren't
502 // mangled at all, and the -l and -p options are ignored. Here, we do the
503 // same.
504 return Filename;
505
506 std::string CoveragePath;
507 if (Options.LongFileNames && !Filename.equals(MainFilename))
508 CoveragePath =
509 mangleCoveragePath(MainFilename, Options.PreservePaths) + "##";
510 CoveragePath +=
511 mangleCoveragePath(Filename, Options.PreservePaths) + ".gcov";
512 return CoveragePath;
513}
514
515std::unique_ptr<raw_ostream>
516FileInfo::openCoveragePath(StringRef CoveragePath) {
517 if (Options.NoOutput)
Justin Bogner7c093732014-05-07 16:01:27 +0000518 return llvm::make_unique<raw_null_ostream>();
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000519
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000520 std::error_code EC;
521 auto OS = llvm::make_unique<raw_fd_ostream>(CoveragePath.str(), EC,
522 sys::fs::F_Text);
523 if (EC) {
524 errs() << EC.message() << "\n";
Justin Bogner7c093732014-05-07 16:01:27 +0000525 return llvm::make_unique<raw_null_ostream>();
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000526 }
527 return std::move(OS);
528}
529
Devang Patel37140652011-09-28 18:50:00 +0000530/// print - Print source files with collected line count information.
Justin Bognerc67f0252014-04-23 21:44:55 +0000531void FileInfo::print(StringRef MainFilename, StringRef GCNOFile,
532 StringRef GCDAFile) {
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000533 for (StringMap<LineData>::const_iterator I = LineInfo.begin(),
Yuchen Wuef6909d2013-11-19 20:33:32 +0000534 E = LineInfo.end(); I != E; ++I) {
Devang Patel37140652011-09-28 18:50:00 +0000535 StringRef Filename = I->first();
Justin Bognercf27e1b2014-05-07 02:11:23 +0000536 auto AllLines = LineConsumer(Filename);
Yuchen Wu8aac4f62013-11-19 20:57:20 +0000537
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000538 std::string CoveragePath = getCoveragePath(Filename, MainFilename);
539 std::unique_ptr<raw_ostream> S = openCoveragePath(CoveragePath);
540 raw_ostream &OS = *S;
Yuchen Wu26326ad2013-12-03 00:57:11 +0000541
Yuchen Wu8aac4f62013-11-19 20:57:20 +0000542 OS << " -: 0:Source:" << Filename << "\n";
Yuchen Wu21517e42013-12-04 05:07:36 +0000543 OS << " -: 0:Graph:" << GCNOFile << "\n";
544 OS << " -: 0:Data:" << GCDAFile << "\n";
Yuchen Wu8aac4f62013-11-19 20:57:20 +0000545 OS << " -: 0:Runs:" << RunCount << "\n";
546 OS << " -: 0:Programs:" << ProgramCount << "\n";
547
Yuchen Wu1c068162013-12-03 01:35:31 +0000548 const LineData &Line = I->second;
Yuchen Wubb6a4772013-12-19 00:29:25 +0000549 GCOVCoverage FileCoverage(Filename);
Justin Bognercf27e1b2014-05-07 02:11:23 +0000550 for (uint32_t LineIndex = 0;
551 LineIndex < Line.LastLine || !AllLines.empty(); ++LineIndex) {
Yuchen Wu73dc3812013-12-18 18:40:15 +0000552 if (Options.BranchInfo) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000553 FunctionLines::const_iterator FuncsIt = Line.Functions.find(LineIndex);
554 if (FuncsIt != Line.Functions.end())
555 printFunctionSummary(OS, FuncsIt->second);
556 }
Yuchen Wu1c068162013-12-03 01:35:31 +0000557
Yuchen Wu342714c2013-12-13 01:15:07 +0000558 BlockLines::const_iterator BlocksIt = Line.Blocks.find(LineIndex);
559 if (BlocksIt == Line.Blocks.end()) {
560 // No basic blocks are on this line. Not an executable line of code.
561 OS << " -:";
Justin Bognercf27e1b2014-05-07 02:11:23 +0000562 AllLines.printNext(OS, LineIndex + 1);
Yuchen Wu342714c2013-12-13 01:15:07 +0000563 } else {
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000564 const BlockVector &Blocks = BlocksIt->second;
Yuchen Wu342714c2013-12-13 01:15:07 +0000565
566 // Add up the block counts to form line counts.
Yuchen Wubb6a4772013-12-19 00:29:25 +0000567 DenseMap<const GCOVFunction *, bool> LineExecs;
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000568 uint64_t LineCount = 0;
569 for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end();
570 I != E; ++I) {
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000571 const GCOVBlock *Block = *I;
572 if (Options.AllBlocks) {
573 // Only take the highest block count for that line.
574 uint64_t BlockCount = Block->getCount();
575 LineCount = LineCount > BlockCount ? LineCount : BlockCount;
576 } else {
577 // Sum up all of the block counts.
578 LineCount += Block->getCount();
579 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000580
581 if (Options.FuncCoverage) {
582 // This is a slightly convoluted way to most accurately gather line
583 // statistics for functions. Basically what is happening is that we
584 // don't want to count a single line with multiple blocks more than
585 // once. However, we also don't simply want to give the total line
586 // count to every function that starts on the line. Thus, what is
587 // happening here are two things:
588 // 1) Ensure that the number of logical lines is only incremented
589 // once per function.
590 // 2) If there are multiple blocks on the same line, ensure that the
591 // number of lines executed is incremented as long as at least
592 // one of the blocks are executed.
593 const GCOVFunction *Function = &Block->getParent();
594 if (FuncCoverages.find(Function) == FuncCoverages.end()) {
595 std::pair<const GCOVFunction *, GCOVCoverage>
596 KeyValue(Function, GCOVCoverage(Function->getName()));
597 FuncCoverages.insert(KeyValue);
598 }
599 GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second;
600
601 if (LineExecs.find(Function) == LineExecs.end()) {
602 if (Block->getCount()) {
603 ++FuncCoverage.LinesExec;
604 LineExecs[Function] = true;
605 } else {
606 LineExecs[Function] = false;
607 }
608 ++FuncCoverage.LogicalLines;
609 } else if (!LineExecs[Function] && Block->getCount()) {
610 ++FuncCoverage.LinesExec;
611 LineExecs[Function] = true;
612 }
613 }
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000614 }
Yuchen Wu8256ee62013-12-18 21:12:51 +0000615
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000616 if (LineCount == 0)
Yuchen Wudbcf1972013-11-02 00:09:17 +0000617 OS << " #####:";
Yuchen Wu8256ee62013-12-18 21:12:51 +0000618 else {
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000619 OS << format("%9" PRIu64 ":", LineCount);
Yuchen Wubb6a4772013-12-19 00:29:25 +0000620 ++FileCoverage.LinesExec;
Yuchen Wu8256ee62013-12-18 21:12:51 +0000621 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000622 ++FileCoverage.LogicalLines;
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000623
Justin Bognercf27e1b2014-05-07 02:11:23 +0000624 AllLines.printNext(OS, LineIndex + 1);
Yuchen Wu342714c2013-12-13 01:15:07 +0000625
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000626 uint32_t BlockNo = 0;
Yuchen Wu342714c2013-12-13 01:15:07 +0000627 uint32_t EdgeNo = 0;
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000628 for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end();
629 I != E; ++I) {
630 const GCOVBlock *Block = *I;
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000631
Yuchen Wu342714c2013-12-13 01:15:07 +0000632 // Only print block and branch information at the end of the block.
633 if (Block->getLastLine() != LineIndex+1)
634 continue;
635 if (Options.AllBlocks)
636 printBlockInfo(OS, *Block, LineIndex, BlockNo);
Yuchen Wu73dc3812013-12-18 18:40:15 +0000637 if (Options.BranchInfo) {
Yuchen Wu66d93b82013-12-16 22:14:02 +0000638 size_t NumEdges = Block->getNumDstEdges();
639 if (NumEdges > 1)
Yuchen Wubb6a4772013-12-19 00:29:25 +0000640 printBranchInfo(OS, *Block, FileCoverage, EdgeNo);
Yuchen Wu66d93b82013-12-16 22:14:02 +0000641 else if (Options.UncondBranch && NumEdges == 1)
642 printUncondBranchInfo(OS, EdgeNo, (*Block->dst_begin())->Count);
643 }
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000644 }
645 }
Devang Patel37140652011-09-28 18:50:00 +0000646 }
Justin Bognerc6af3502014-02-04 10:45:02 +0000647 FileCoverages.push_back(std::make_pair(CoveragePath, FileCoverage));
Devang Patel37140652011-09-28 18:50:00 +0000648 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000649
650 // FIXME: There is no way to detect calls given current instrumentation.
651 if (Options.FuncCoverage)
652 printFuncCoverage();
653 printFileCoverage();
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000654 return;
Devang Patel37140652011-09-28 18:50:00 +0000655}
Yuchen Wu342714c2013-12-13 01:15:07 +0000656
657/// printFunctionSummary - Print function and block summary.
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000658void FileInfo::printFunctionSummary(raw_ostream &OS,
Yuchen Wu342714c2013-12-13 01:15:07 +0000659 const FunctionVector &Funcs) const {
660 for (FunctionVector::const_iterator I = Funcs.begin(), E = Funcs.end();
661 I != E; ++I) {
662 const GCOVFunction *Func = *I;
663 uint64_t EntryCount = Func->getEntryCount();
Yuchen Wuc9b2dcd2013-12-18 18:46:25 +0000664 uint32_t BlocksExec = 0;
Yuchen Wu342714c2013-12-13 01:15:07 +0000665 for (GCOVFunction::BlockIterator I = Func->block_begin(),
666 E = Func->block_end(); I != E; ++I) {
David Blaikie09757492014-04-21 21:40:16 +0000667 const GCOVBlock &Block = **I;
668 if (Block.getNumDstEdges() && Block.getCount())
Yuchen Wuc9b2dcd2013-12-18 18:46:25 +0000669 ++BlocksExec;
Yuchen Wu342714c2013-12-13 01:15:07 +0000670 }
671
672 OS << "function " << Func->getName() << " called " << EntryCount
673 << " returned " << safeDiv(Func->getExitCount()*100, EntryCount)
674 << "% blocks executed "
Yuchen Wuc9b2dcd2013-12-18 18:46:25 +0000675 << safeDiv(BlocksExec*100, Func->getNumBlocks()-1) << "%\n";
Yuchen Wu342714c2013-12-13 01:15:07 +0000676 }
677}
678
679/// printBlockInfo - Output counts for each block.
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000680void FileInfo::printBlockInfo(raw_ostream &OS, const GCOVBlock &Block,
Yuchen Wu342714c2013-12-13 01:15:07 +0000681 uint32_t LineIndex, uint32_t &BlockNo) const {
682 if (Block.getCount() == 0)
683 OS << " $$$$$:";
684 else
685 OS << format("%9" PRIu64 ":", Block.getCount());
686 OS << format("%5u-block %2u\n", LineIndex+1, BlockNo++);
687}
688
Yuchen Wu66d93b82013-12-16 22:14:02 +0000689/// printBranchInfo - Print conditional branch probabilities.
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000690void FileInfo::printBranchInfo(raw_ostream &OS, const GCOVBlock &Block,
Yuchen Wubb6a4772013-12-19 00:29:25 +0000691 GCOVCoverage &Coverage, uint32_t &EdgeNo) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000692 SmallVector<uint64_t, 16> BranchCounts;
693 uint64_t TotalCounts = 0;
694 for (GCOVBlock::EdgeIterator I = Block.dst_begin(), E = Block.dst_end();
695 I != E; ++I) {
696 const GCOVEdge *Edge = *I;
697 BranchCounts.push_back(Edge->Count);
698 TotalCounts += Edge->Count;
Yuchen Wu8256ee62013-12-18 21:12:51 +0000699 if (Block.getCount()) ++Coverage.BranchesExec;
700 if (Edge->Count) ++Coverage.BranchesTaken;
701 ++Coverage.Branches;
Yuchen Wubb6a4772013-12-19 00:29:25 +0000702
703 if (Options.FuncCoverage) {
704 const GCOVFunction *Function = &Block.getParent();
705 GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second;
706 if (Block.getCount()) ++FuncCoverage.BranchesExec;
707 if (Edge->Count) ++FuncCoverage.BranchesTaken;
708 ++FuncCoverage.Branches;
709 }
Yuchen Wu342714c2013-12-13 01:15:07 +0000710 }
711
712 for (SmallVectorImpl<uint64_t>::const_iterator I = BranchCounts.begin(),
713 E = BranchCounts.end(); I != E; ++I) {
Yuchen Wu73dc3812013-12-18 18:40:15 +0000714 OS << format("branch %2u ", EdgeNo++)
715 << formatBranchInfo(Options, *I, TotalCounts) << "\n";
Yuchen Wu342714c2013-12-13 01:15:07 +0000716 }
717}
Yuchen Wu66d93b82013-12-16 22:14:02 +0000718
719/// printUncondBranchInfo - Print unconditional branch probabilities.
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000720void FileInfo::printUncondBranchInfo(raw_ostream &OS, uint32_t &EdgeNo,
Yuchen Wu66d93b82013-12-16 22:14:02 +0000721 uint64_t Count) const {
Yuchen Wu73dc3812013-12-18 18:40:15 +0000722 OS << format("unconditional %2u ", EdgeNo++)
723 << formatBranchInfo(Options, Count, Count) << "\n";
Yuchen Wu66d93b82013-12-16 22:14:02 +0000724}
Yuchen Wu8256ee62013-12-18 21:12:51 +0000725
Yuchen Wubb6a4772013-12-19 00:29:25 +0000726// printCoverage - Print generic coverage info used by both printFuncCoverage
727// and printFileCoverage.
728void FileInfo::printCoverage(const GCOVCoverage &Coverage) const {
NAKAMURA Takumi6e3c4232013-12-19 08:46:28 +0000729 outs() << format("Lines executed:%.2f%% of %u\n",
Yuchen Wu8256ee62013-12-18 21:12:51 +0000730 double(Coverage.LinesExec)*100/Coverage.LogicalLines,
731 Coverage.LogicalLines);
732 if (Options.BranchInfo) {
733 if (Coverage.Branches) {
NAKAMURA Takumi6e3c4232013-12-19 08:46:28 +0000734 outs() << format("Branches executed:%.2f%% of %u\n",
Yuchen Wu8256ee62013-12-18 21:12:51 +0000735 double(Coverage.BranchesExec)*100/Coverage.Branches,
736 Coverage.Branches);
NAKAMURA Takumi6e3c4232013-12-19 08:46:28 +0000737 outs() << format("Taken at least once:%.2f%% of %u\n",
Yuchen Wu8256ee62013-12-18 21:12:51 +0000738 double(Coverage.BranchesTaken)*100/Coverage.Branches,
739 Coverage.Branches);
740 } else {
741 outs() << "No branches\n";
742 }
743 outs() << "No calls\n"; // to be consistent with gcov
744 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000745}
746
747// printFuncCoverage - Print per-function coverage info.
748void FileInfo::printFuncCoverage() const {
Justin Bognerc6af3502014-02-04 10:45:02 +0000749 for (FuncCoverageMap::const_iterator I = FuncCoverages.begin(),
750 E = FuncCoverages.end(); I != E; ++I) {
Yuchen Wubb6a4772013-12-19 00:29:25 +0000751 const GCOVCoverage &Coverage = I->second;
752 outs() << "Function '" << Coverage.Name << "'\n";
753 printCoverage(Coverage);
754 outs() << "\n";
755 }
756}
757
758// printFileCoverage - Print per-file coverage info.
759void FileInfo::printFileCoverage() const {
Justin Bognerc6af3502014-02-04 10:45:02 +0000760 for (FileCoverageList::const_iterator I = FileCoverages.begin(),
761 E = FileCoverages.end(); I != E; ++I) {
762 const std::string &Filename = I->first;
763 const GCOVCoverage &Coverage = I->second;
Yuchen Wubb6a4772013-12-19 00:29:25 +0000764 outs() << "File '" << Coverage.Name << "'\n";
765 printCoverage(Coverage);
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000766 if (!Options.NoOutput)
767 outs() << Coverage.Name << ":creating '" << Filename << "'\n";
768 outs() << "\n";
Yuchen Wubb6a4772013-12-19 00:29:25 +0000769 }
Yuchen Wu8256ee62013-12-18 21:12:51 +0000770}