blob: 5cce962400907d987321cc91ddca190df9b88c2f [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"
Benjamin Kramer799003b2015-03-23 19:32:43 +000022#include "llvm/Support/raw_ostream.h"
Yuchen Wu342714c2013-12-13 01:15:07 +000023#include <algorithm>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000024#include <system_error>
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +000025
Devang Patel37140652011-09-28 18:50:00 +000026using namespace llvm;
27
28//===----------------------------------------------------------------------===//
29// GCOVFile implementation.
30
Yuchen Wubec4e902013-12-04 04:49:23 +000031/// readGCNO - Read GCNO buffer.
32bool GCOVFile::readGCNO(GCOVBuffer &Buffer) {
Justin Bogner011c7422015-01-23 22:38:01 +000033 if (!Buffer.readGCNOFormat())
34 return false;
35 if (!Buffer.readGCOVVersion(Version))
36 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000037
Justin Bogner011c7422015-01-23 22:38:01 +000038 if (!Buffer.readInt(Checksum))
39 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000040 while (true) {
Justin Bogner011c7422015-01-23 22:38:01 +000041 if (!Buffer.readFunctionTag())
42 break;
David Blaikie09757492014-04-21 21:40:16 +000043 auto GFun = make_unique<GCOVFunction>(*this);
Yuchen Wubec4e902013-12-04 04:49:23 +000044 if (!GFun->readGCNO(Buffer, Version))
45 return false;
David Blaikie09757492014-04-21 21:40:16 +000046 Functions.push_back(std::move(GFun));
Yuchen Wubec4e902013-12-04 04:49:23 +000047 }
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()");
Justin Bogner011c7422015-01-23 22:38:01 +000057 if (!Buffer.readGCDAFormat())
58 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000059 GCOV::GCOVVersion GCDAVersion;
Justin Bogner011c7422015-01-23 22:38:01 +000060 if (!Buffer.readGCOVVersion(GCDAVersion))
61 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000062 if (Version != GCDAVersion) {
63 errs() << "GCOV versions do not match.\n";
Devang Patel37140652011-09-28 18:50:00 +000064 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000065 }
Devang Patel37140652011-09-28 18:50:00 +000066
Yuchen Wubec4e902013-12-04 04:49:23 +000067 uint32_t GCDAChecksum;
Justin Bogner011c7422015-01-23 22:38:01 +000068 if (!Buffer.readInt(GCDAChecksum))
69 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000070 if (Checksum != GCDAChecksum) {
Justin Bogner011c7422015-01-23 22:38:01 +000071 errs() << "File checksums do not match: " << Checksum
72 << " != " << GCDAChecksum << ".\n";
Yuchen Wubec4e902013-12-04 04:49:23 +000073 return false;
74 }
75 for (size_t i = 0, e = Functions.size(); i < e; ++i) {
76 if (!Buffer.readFunctionTag()) {
77 errs() << "Unexpected number of functions.\n";
Yuchen Wubabe7492013-11-20 04:15:05 +000078 return false;
79 }
Yuchen Wubec4e902013-12-04 04:49:23 +000080 if (!Functions[i]->readGCDA(Buffer, Version))
81 return false;
82 }
83 if (Buffer.readObjectTag()) {
84 uint32_t Length;
85 uint32_t Dummy;
Justin Bogner011c7422015-01-23 22:38:01 +000086 if (!Buffer.readInt(Length))
87 return false;
88 if (!Buffer.readInt(Dummy))
89 return false; // checksum
90 if (!Buffer.readInt(Dummy))
91 return false; // num
92 if (!Buffer.readInt(RunCount))
93 return false;
94 Buffer.advanceCursor(Length - 3);
Yuchen Wubec4e902013-12-04 04:49:23 +000095 }
96 while (Buffer.readProgramTag()) {
97 uint32_t Length;
Justin Bogner011c7422015-01-23 22:38:01 +000098 if (!Buffer.readInt(Length))
99 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +0000100 Buffer.advanceCursor(Length);
101 ++ProgramCount;
Yuchen Wu14ae8e62013-10-25 02:22:21 +0000102 }
103
Devang Patel37140652011-09-28 18:50:00 +0000104 return true;
105}
106
Yuchen Wu03678152013-10-25 02:22:24 +0000107/// dump - Dump GCOVFile content to dbgs() for debugging purposes.
Yaron Kereneb2a2542016-01-29 20:50:44 +0000108LLVM_DUMP_METHOD void GCOVFile::dump() const {
David Blaikie09757492014-04-21 21:40:16 +0000109 for (const auto &FPtr : Functions)
110 FPtr->dump();
Devang Patel37140652011-09-28 18:50:00 +0000111}
112
113/// collectLineCounts - Collect line counts. This must be used after
114/// reading .gcno and .gcda files.
115void GCOVFile::collectLineCounts(FileInfo &FI) {
David Blaikie09757492014-04-21 21:40:16 +0000116 for (const auto &FPtr : Functions)
117 FPtr->collectLineCounts(FI);
Yuchen Wu30672d92013-11-05 01:11:58 +0000118 FI.setRunCount(RunCount);
Yuchen Wu14ae8e62013-10-25 02:22:21 +0000119 FI.setProgramCount(ProgramCount);
Devang Patel37140652011-09-28 18:50:00 +0000120}
121
122//===----------------------------------------------------------------------===//
123// GCOVFunction implementation.
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;
Justin Bogner011c7422015-01-23 22:38:01 +0000129 if (!Buff.readInt(Dummy))
130 return false; // Function header length
131 if (!Buff.readInt(Ident))
132 return false;
133 if (!Buff.readInt(Checksum))
134 return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000135 if (Version != GCOV::V402) {
136 uint32_t CfgChecksum;
Justin Bogner011c7422015-01-23 22:38:01 +0000137 if (!Buff.readInt(CfgChecksum))
138 return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000139 if (Parent.getChecksum() != CfgChecksum) {
140 errs() << "File checksums do not match: " << Parent.getChecksum()
141 << " != " << CfgChecksum << " in (" << Name << ").\n";
142 return false;
143 }
144 }
Justin Bogner011c7422015-01-23 22:38:01 +0000145 if (!Buff.readString(Name))
146 return false;
147 if (!Buff.readString(Filename))
148 return false;
149 if (!Buff.readInt(LineNumber))
150 return false;
Devang Patel37140652011-09-28 18:50:00 +0000151
152 // read blocks.
Yuchen Wue28da842013-11-14 00:07:15 +0000153 if (!Buff.readBlockTag()) {
154 errs() << "Block tag not found.\n";
155 return false;
156 }
157 uint32_t BlockCount;
Justin Bogner011c7422015-01-23 22:38:01 +0000158 if (!Buff.readInt(BlockCount))
159 return false;
Bob Wilson868e6e32013-10-22 20:02:36 +0000160 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
Justin Bogner011c7422015-01-23 22:38:01 +0000161 if (!Buff.readInt(Dummy))
162 return false; // Block flags;
David Blaikie09757492014-04-21 21:40:16 +0000163 Blocks.push_back(make_unique<GCOVBlock>(*this, i));
Devang Patel37140652011-09-28 18:50:00 +0000164 }
165
166 // read edges.
167 while (Buff.readEdgeTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +0000168 uint32_t EdgeCount;
Justin Bogner011c7422015-01-23 22:38:01 +0000169 if (!Buff.readInt(EdgeCount))
170 return false;
Yuchen Wue28da842013-11-14 00:07:15 +0000171 EdgeCount = (EdgeCount - 1) / 2;
172 uint32_t BlockNo;
Justin Bogner011c7422015-01-23 22:38:01 +0000173 if (!Buff.readInt(BlockNo))
174 return false;
Yuchen Wue28da842013-11-14 00:07:15 +0000175 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;
179 }
Bob Wilson868e6e32013-10-22 20:02:36 +0000180 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
Yuchen Wue28da842013-11-14 00:07:15 +0000181 uint32_t Dst;
Justin Bogner011c7422015-01-23 22:38:01 +0000182 if (!Buff.readInt(Dst))
183 return false;
David Blaikie09757492014-04-21 21:40:16 +0000184 Edges.push_back(make_unique<GCOVEdge>(*Blocks[BlockNo], *Blocks[Dst]));
185 GCOVEdge *Edge = Edges.back().get();
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000186 Blocks[BlockNo]->addDstEdge(Edge);
187 Blocks[Dst]->addSrcEdge(Edge);
Justin Bogner011c7422015-01-23 22:38:01 +0000188 if (!Buff.readInt(Dummy))
189 return false; // Edge flag
Devang Patel37140652011-09-28 18:50:00 +0000190 }
191 }
192
193 // read line table.
194 while (Buff.readLineTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +0000195 uint32_t LineTableLength;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000196 // Read the length of this line table.
Justin Bogner011c7422015-01-23 22:38:01 +0000197 if (!Buff.readInt(LineTableLength))
198 return false;
199 uint32_t EndPos = Buff.getCursor() + LineTableLength * 4;
Yuchen Wue28da842013-11-14 00:07:15 +0000200 uint32_t BlockNo;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000201 // Read the block number this table is associated with.
Justin Bogner011c7422015-01-23 22:38:01 +0000202 if (!Buff.readInt(BlockNo))
203 return false;
Yuchen Wue28da842013-11-14 00:07:15 +0000204 if (BlockNo >= BlockCount) {
Yuchen Wu4c9f19d2013-12-05 22:02:33 +0000205 errs() << "Unexpected block number: " << BlockNo << " (in " << Name
206 << ").\n";
Yuchen Wue28da842013-11-14 00:07:15 +0000207 return false;
Devang Patel37140652011-09-28 18:50:00 +0000208 }
David Blaikie09757492014-04-21 21:40:16 +0000209 GCOVBlock &Block = *Blocks[BlockNo];
Justin Bognerc475e1b2014-05-02 20:01:24 +0000210 // Read the word that pads the beginning of the line table. This may be a
211 // flag of some sort, but seems to always be zero.
Justin Bogner011c7422015-01-23 22:38:01 +0000212 if (!Buff.readInt(Dummy))
213 return false;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000214
215 // Line information starts here and continues up until the last word.
216 if (Buff.getCursor() != (EndPos - sizeof(uint32_t))) {
Yuchen Wud738bee2013-11-14 00:32:00 +0000217 StringRef F;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000218 // Read the source file name.
Justin Bogner011c7422015-01-23 22:38:01 +0000219 if (!Buff.readString(F))
220 return false;
Yuchen Wu4c9f19d2013-12-05 22:02:33 +0000221 if (Filename != F) {
222 errs() << "Multiple sources for a single basic block: " << Filename
223 << " != " << F << " (in " << Name << ").\n";
Yuchen Wud738bee2013-11-14 00:32:00 +0000224 return false;
225 }
Justin Bognerc475e1b2014-05-02 20:01:24 +0000226 // Read lines up to, but not including, the null terminator.
227 while (Buff.getCursor() < (EndPos - 2 * sizeof(uint32_t))) {
Yuchen Wue28da842013-11-14 00:07:15 +0000228 uint32_t Line;
Justin Bogner011c7422015-01-23 22:38:01 +0000229 if (!Buff.readInt(Line))
230 return false;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000231 // Line 0 means this instruction was injected by the compiler. Skip it.
Justin Bogner011c7422015-01-23 22:38:01 +0000232 if (!Line)
233 continue;
David Blaikie09757492014-04-21 21:40:16 +0000234 Block.addLine(Line);
Yuchen Wue28da842013-11-14 00:07:15 +0000235 }
Justin Bognerc475e1b2014-05-02 20:01:24 +0000236 // Read the null terminator.
Justin Bogner011c7422015-01-23 22:38:01 +0000237 if (!Buff.readInt(Dummy))
238 return false;
Yuchen Wue28da842013-11-14 00:07:15 +0000239 }
Justin Bognerc475e1b2014-05-02 20:01:24 +0000240 // The last word is either a flag or padding, it isn't clear which. Skip
241 // over it.
Justin Bogner011c7422015-01-23 22:38:01 +0000242 if (!Buff.readInt(Dummy))
243 return false;
Devang Patel37140652011-09-28 18:50:00 +0000244 }
245 return true;
246}
247
Yuchen Wuba718332013-12-03 00:15:49 +0000248/// readGCDA - Read a function from the GCDA buffer. Return false if an error
249/// occurs.
Yuchen Wubec4e902013-12-04 04:49:23 +0000250bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
Yuchen Wuba718332013-12-03 00:15:49 +0000251 uint32_t Dummy;
Justin Bogner011c7422015-01-23 22:38:01 +0000252 if (!Buff.readInt(Dummy))
253 return false; // Function header length
Daniel Jasper87a24d52013-12-04 08:57:17 +0000254
Yuchen Wu57529972013-12-04 05:42:28 +0000255 uint32_t GCDAIdent;
Justin Bogner011c7422015-01-23 22:38:01 +0000256 if (!Buff.readInt(GCDAIdent))
257 return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000258 if (Ident != GCDAIdent) {
Justin Bogner011c7422015-01-23 22:38:01 +0000259 errs() << "Function identifiers do not match: " << Ident
260 << " != " << GCDAIdent << " (in " << Name << ").\n";
Yuchen Wu57529972013-12-04 05:42:28 +0000261 return false;
262 }
Yuchen Wuba718332013-12-03 00:15:49 +0000263
Daniel Jasper87a24d52013-12-04 08:57:17 +0000264 uint32_t GCDAChecksum;
Justin Bogner011c7422015-01-23 22:38:01 +0000265 if (!Buff.readInt(GCDAChecksum))
266 return false;
Daniel Jasper87a24d52013-12-04 08:57:17 +0000267 if (Checksum != GCDAChecksum) {
Justin Bogner011c7422015-01-23 22:38:01 +0000268 errs() << "Function checksums do not match: " << Checksum
269 << " != " << GCDAChecksum << " (in " << Name << ").\n";
Daniel Jasper87a24d52013-12-04 08:57:17 +0000270 return false;
271 }
Yuchen Wu57529972013-12-04 05:42:28 +0000272
273 uint32_t CfgChecksum;
274 if (Version != GCOV::V402) {
Justin Bogner011c7422015-01-23 22:38:01 +0000275 if (!Buff.readInt(CfgChecksum))
276 return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000277 if (Parent.getChecksum() != CfgChecksum) {
278 errs() << "File checksums do not match: " << Parent.getChecksum()
279 << " != " << CfgChecksum << " (in " << Name << ").\n";
280 return false;
281 }
282 }
283
284 StringRef GCDAName;
Justin Bogner011c7422015-01-23 22:38:01 +0000285 if (!Buff.readString(GCDAName))
286 return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000287 if (Name != GCDAName) {
288 errs() << "Function names do not match: " << Name << " != " << GCDAName
289 << ".\n";
290 return false;
291 }
Yuchen Wuba718332013-12-03 00:15:49 +0000292
293 if (!Buff.readArcTag()) {
Yuchen Wu57529972013-12-04 05:42:28 +0000294 errs() << "Arc tag not found (in " << Name << ").\n";
Yuchen Wuba718332013-12-03 00:15:49 +0000295 return false;
296 }
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000297
Yuchen Wuba718332013-12-03 00:15:49 +0000298 uint32_t Count;
Justin Bogner011c7422015-01-23 22:38:01 +0000299 if (!Buff.readInt(Count))
300 return false;
Yuchen Wuba718332013-12-03 00:15:49 +0000301 Count /= 2;
302
303 // This for loop adds the counts for each block. A second nested loop is
304 // required to combine the edge counts that are contained in the GCDA file.
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000305 for (uint32_t BlockNo = 0; Count > 0; ++BlockNo) {
306 // The last block is always reserved for exit block
Justin Bognerd0a62432015-03-17 00:18:51 +0000307 if (BlockNo >= Blocks.size()) {
Yuchen Wu57529972013-12-04 05:42:28 +0000308 errs() << "Unexpected number of edges (in " << Name << ").\n";
Yuchen Wuba718332013-12-03 00:15:49 +0000309 return false;
310 }
Justin Bognerd0a62432015-03-17 00:18:51 +0000311 if (BlockNo == Blocks.size() - 1)
312 errs() << "(" << Name << ") has arcs from exit block.\n";
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000313 GCOVBlock &Block = *Blocks[BlockNo];
314 for (size_t EdgeNo = 0, End = Block.getNumDstEdges(); EdgeNo < End;
Justin Bogner011c7422015-01-23 22:38:01 +0000315 ++EdgeNo) {
Yuchen Wuba718332013-12-03 00:15:49 +0000316 if (Count == 0) {
Yuchen Wu57529972013-12-04 05:42:28 +0000317 errs() << "Unexpected number of edges (in " << Name << ").\n";
Yuchen Wuba718332013-12-03 00:15:49 +0000318 return false;
319 }
320 uint64_t ArcCount;
Justin Bogner011c7422015-01-23 22:38:01 +0000321 if (!Buff.readInt64(ArcCount))
322 return false;
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000323 Block.addCount(EdgeNo, ArcCount);
Yuchen Wuba718332013-12-03 00:15:49 +0000324 --Count;
325 }
Yuchen Wu342714c2013-12-13 01:15:07 +0000326 Block.sortDstEdges();
Yuchen Wuba718332013-12-03 00:15:49 +0000327 }
328 return true;
329}
330
Yuchen Wu342714c2013-12-13 01:15:07 +0000331/// getEntryCount - Get the number of times the function was called by
332/// retrieving the entry block's count.
333uint64_t GCOVFunction::getEntryCount() const {
334 return Blocks.front()->getCount();
335}
336
337/// getExitCount - Get the number of times the function returned by retrieving
338/// the exit block's count.
339uint64_t GCOVFunction::getExitCount() const {
340 return Blocks.back()->getCount();
341}
342
Yuchen Wu03678152013-10-25 02:22:24 +0000343/// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
Yaron Kereneb2a2542016-01-29 20:50:44 +0000344LLVM_DUMP_METHOD void GCOVFunction::dump() const {
Justin Bogner58e41342014-11-06 06:55:02 +0000345 dbgs() << "===== " << Name << " (" << Ident << ") @ " << Filename << ":"
346 << LineNumber << "\n";
David Blaikie09757492014-04-21 21:40:16 +0000347 for (const auto &Block : Blocks)
348 Block->dump();
Devang Patel37140652011-09-28 18:50:00 +0000349}
350
351/// collectLineCounts - Collect line counts. This must be used after
352/// reading .gcno and .gcda files.
353void GCOVFunction::collectLineCounts(FileInfo &FI) {
Justin Bogner95e0a702014-03-26 22:03:06 +0000354 // If the line number is zero, this is a function that doesn't actually appear
355 // in the source file, so there isn't anything we can do with it.
356 if (LineNumber == 0)
357 return;
358
David Blaikie09757492014-04-21 21:40:16 +0000359 for (const auto &Block : Blocks)
360 Block->collectLineCounts(FI);
Yuchen Wu342714c2013-12-13 01:15:07 +0000361 FI.addFunctionLine(Filename, LineNumber, this);
Devang Patel37140652011-09-28 18:50:00 +0000362}
363
364//===----------------------------------------------------------------------===//
365// GCOVBlock implementation.
366
367/// ~GCOVBlock - Delete GCOVBlock and its content.
368GCOVBlock::~GCOVBlock() {
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000369 SrcEdges.clear();
370 DstEdges.clear();
Yuchen Wud738bee2013-11-14 00:32:00 +0000371 Lines.clear();
Devang Patel37140652011-09-28 18:50:00 +0000372}
373
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000374/// addCount - Add to block counter while storing the edge count. If the
375/// destination has no outgoing edges, also update that block's count too.
376void GCOVBlock::addCount(size_t DstEdgeNo, uint64_t N) {
377 assert(DstEdgeNo < DstEdges.size()); // up to caller to ensure EdgeNo is valid
378 DstEdges[DstEdgeNo]->Count = N;
379 Counter += N;
David Blaikie09757492014-04-21 21:40:16 +0000380 if (!DstEdges[DstEdgeNo]->Dst.getNumDstEdges())
381 DstEdges[DstEdgeNo]->Dst.Counter += N;
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000382}
383
Yuchen Wu342714c2013-12-13 01:15:07 +0000384/// sortDstEdges - Sort destination edges by block number, nop if already
385/// sorted. This is required for printing branch info in the correct order.
386void GCOVBlock::sortDstEdges() {
387 if (!DstEdgesAreSorted) {
388 SortDstEdgesFunctor SortEdges;
389 std::stable_sort(DstEdges.begin(), DstEdges.end(), SortEdges);
390 }
391}
392
Devang Patel37140652011-09-28 18:50:00 +0000393/// collectLineCounts - Collect line counts. This must be used after
394/// reading .gcno and .gcda files.
395void GCOVBlock::collectLineCounts(FileInfo &FI) {
Justin Bogner000b5222015-01-23 22:57:02 +0000396 for (uint32_t N : Lines)
397 FI.addBlockLine(Parent.getFilename(), N, this);
Devang Patel37140652011-09-28 18:50:00 +0000398}
399
Yuchen Wu03678152013-10-25 02:22:24 +0000400/// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
Yaron Kereneb2a2542016-01-29 20:50:44 +0000401LLVM_DUMP_METHOD void GCOVBlock::dump() const {
Yuchen Wu03678152013-10-25 02:22:24 +0000402 dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000403 if (!SrcEdges.empty()) {
404 dbgs() << "\tSource Edges : ";
Justin Bogner000b5222015-01-23 22:57:02 +0000405 for (const GCOVEdge *Edge : SrcEdges)
David Blaikie09757492014-04-21 21:40:16 +0000406 dbgs() << Edge->Src.Number << " (" << Edge->Count << "), ";
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000407 dbgs() << "\n";
408 }
409 if (!DstEdges.empty()) {
410 dbgs() << "\tDestination Edges : ";
Justin Bogner000b5222015-01-23 22:57:02 +0000411 for (const GCOVEdge *Edge : DstEdges)
David Blaikie09757492014-04-21 21:40:16 +0000412 dbgs() << Edge->Dst.Number << " (" << Edge->Count << "), ";
Yuchen Wu03678152013-10-25 02:22:24 +0000413 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000414 }
415 if (!Lines.empty()) {
Yuchen Wu03678152013-10-25 02:22:24 +0000416 dbgs() << "\tLines : ";
Justin Bogner000b5222015-01-23 22:57:02 +0000417 for (uint32_t N : Lines)
418 dbgs() << (N) << ",";
Yuchen Wud738bee2013-11-14 00:32:00 +0000419 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000420 }
421}
422
423//===----------------------------------------------------------------------===//
Devang Patel37140652011-09-28 18:50:00 +0000424// FileInfo implementation.
425
Yuchen Wu342714c2013-12-13 01:15:07 +0000426// Safe integer division, returns 0 if numerator is 0.
427static uint32_t safeDiv(uint64_t Numerator, uint64_t Divisor) {
428 if (!Numerator)
429 return 0;
Justin Bogner011c7422015-01-23 22:38:01 +0000430 return Numerator / Divisor;
Yuchen Wu342714c2013-12-13 01:15:07 +0000431}
432
433// This custom division function mimics gcov's branch ouputs:
434// - Round to closest whole number
435// - Only output 0% or 100% if it's exactly that value
436static uint32_t branchDiv(uint64_t Numerator, uint64_t Divisor) {
437 if (!Numerator)
438 return 0;
439 if (Numerator == Divisor)
440 return 100;
441
Justin Bogner011c7422015-01-23 22:38:01 +0000442 uint8_t Res = (Numerator * 100 + Divisor / 2) / Divisor;
Yuchen Wu342714c2013-12-13 01:15:07 +0000443 if (Res == 0)
444 return 1;
445 if (Res == 100)
446 return 99;
447 return Res;
448}
449
Benjamin Kramerb1d8c462015-03-23 13:59:13 +0000450namespace {
Yuchen Wu73dc3812013-12-18 18:40:15 +0000451struct formatBranchInfo {
Richard Smithe7dc8bf2015-10-14 00:04:19 +0000452 formatBranchInfo(const GCOV::Options &Options, uint64_t Count, uint64_t Total)
Justin Bogner011c7422015-01-23 22:38:01 +0000453 : Options(Options), Count(Count), Total(Total) {}
Yuchen Wu73dc3812013-12-18 18:40:15 +0000454
455 void print(raw_ostream &OS) const {
456 if (!Total)
457 OS << "never executed";
458 else if (Options.BranchCount)
459 OS << "taken " << Count;
460 else
461 OS << "taken " << branchDiv(Count, Total) << "%";
462 }
463
Richard Smithe7dc8bf2015-10-14 00:04:19 +0000464 const GCOV::Options &Options;
Yuchen Wu73dc3812013-12-18 18:40:15 +0000465 uint64_t Count;
466 uint64_t Total;
467};
468
469static raw_ostream &operator<<(raw_ostream &OS, const formatBranchInfo &FBI) {
470 FBI.print(OS);
471 return OS;
472}
473
Justin Bognercf27e1b2014-05-07 02:11:23 +0000474class LineConsumer {
475 std::unique_ptr<MemoryBuffer> Buffer;
476 StringRef Remaining;
Justin Bogner011c7422015-01-23 22:38:01 +0000477
Justin Bognercf27e1b2014-05-07 02:11:23 +0000478public:
479 LineConsumer(StringRef Filename) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000480 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
481 MemoryBuffer::getFileOrSTDIN(Filename);
482 if (std::error_code EC = BufferOrErr.getError()) {
Justin Bognercf27e1b2014-05-07 02:11:23 +0000483 errs() << Filename << ": " << EC.message() << "\n";
484 Remaining = "";
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000485 } else {
486 Buffer = std::move(BufferOrErr.get());
Justin Bognercf27e1b2014-05-07 02:11:23 +0000487 Remaining = Buffer->getBuffer();
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000488 }
Justin Bognercf27e1b2014-05-07 02:11:23 +0000489 }
490 bool empty() { return Remaining.empty(); }
491 void printNext(raw_ostream &OS, uint32_t LineNum) {
492 StringRef Line;
493 if (empty())
494 Line = "/*EOF*/";
495 else
496 std::tie(Line, Remaining) = Remaining.split("\n");
497 OS << format("%5u:", LineNum) << Line << "\n";
498 }
499};
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +0000500} // end anonymous namespace
Justin Bognercf27e1b2014-05-07 02:11:23 +0000501
Justin Bognerc6af3502014-02-04 10:45:02 +0000502/// Convert a path to a gcov filename. If PreservePaths is true, this
503/// translates "/" to "#", ".." to "^", and drops ".", to match gcov.
504static std::string mangleCoveragePath(StringRef Filename, bool PreservePaths) {
505 if (!PreservePaths)
Justin Bognerc67f0252014-04-23 21:44:55 +0000506 return sys::path::filename(Filename).str();
Justin Bognerc6af3502014-02-04 10:45:02 +0000507
508 // This behaviour is defined by gcov in terms of text replacements, so it's
509 // not likely to do anything useful on filesystems with different textual
510 // conventions.
511 llvm::SmallString<256> Result("");
512 StringRef::iterator I, S, E;
513 for (I = S = Filename.begin(), E = Filename.end(); I != E; ++I) {
514 if (*I != '/')
515 continue;
516
517 if (I - S == 1 && *S == '.') {
518 // ".", the current directory, is skipped.
519 } else if (I - S == 2 && *S == '.' && *(S + 1) == '.') {
520 // "..", the parent directory, is replaced with "^".
521 Result.append("^#");
522 } else {
523 if (S < I)
524 // Leave other components intact,
525 Result.append(S, I);
526 // And separate with "#".
527 Result.push_back('#');
528 }
529 S = I + 1;
530 }
531
532 if (S < I)
533 Result.append(S, I);
Justin Bognerc6af3502014-02-04 10:45:02 +0000534 return Result.str();
535}
536
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000537std::string FileInfo::getCoveragePath(StringRef Filename,
538 StringRef MainFilename) {
539 if (Options.NoOutput)
540 // This is probably a bug in gcov, but when -n is specified, paths aren't
541 // mangled at all, and the -l and -p options are ignored. Here, we do the
542 // same.
543 return Filename;
544
545 std::string CoveragePath;
546 if (Options.LongFileNames && !Filename.equals(MainFilename))
547 CoveragePath =
548 mangleCoveragePath(MainFilename, Options.PreservePaths) + "##";
Justin Bogner011c7422015-01-23 22:38:01 +0000549 CoveragePath += mangleCoveragePath(Filename, Options.PreservePaths) + ".gcov";
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000550 return CoveragePath;
551}
552
553std::unique_ptr<raw_ostream>
554FileInfo::openCoveragePath(StringRef CoveragePath) {
555 if (Options.NoOutput)
Justin Bogner7c093732014-05-07 16:01:27 +0000556 return llvm::make_unique<raw_null_ostream>();
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000557
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000558 std::error_code EC;
Yaron Keren075759a2015-03-30 15:42:36 +0000559 auto OS = llvm::make_unique<raw_fd_ostream>(CoveragePath, EC,
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000560 sys::fs::F_Text);
561 if (EC) {
562 errs() << EC.message() << "\n";
Justin Bogner7c093732014-05-07 16:01:27 +0000563 return llvm::make_unique<raw_null_ostream>();
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000564 }
565 return std::move(OS);
566}
567
Devang Patel37140652011-09-28 18:50:00 +0000568/// print - Print source files with collected line count information.
Justin Bogner0b9858d2015-01-23 23:09:27 +0000569void FileInfo::print(raw_ostream &InfoOS, StringRef MainFilename,
570 StringRef GCNOFile, StringRef GCDAFile) {
Justin Bogner000b5222015-01-23 22:57:02 +0000571 for (const auto &LI : LineInfo) {
572 StringRef Filename = LI.first();
Justin Bognercf27e1b2014-05-07 02:11:23 +0000573 auto AllLines = LineConsumer(Filename);
Yuchen Wu8aac4f62013-11-19 20:57:20 +0000574
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000575 std::string CoveragePath = getCoveragePath(Filename, MainFilename);
Justin Bogner0b9858d2015-01-23 23:09:27 +0000576 std::unique_ptr<raw_ostream> CovStream = openCoveragePath(CoveragePath);
577 raw_ostream &CovOS = *CovStream;
Yuchen Wu26326ad2013-12-03 00:57:11 +0000578
Justin Bogner0b9858d2015-01-23 23:09:27 +0000579 CovOS << " -: 0:Source:" << Filename << "\n";
580 CovOS << " -: 0:Graph:" << GCNOFile << "\n";
581 CovOS << " -: 0:Data:" << GCDAFile << "\n";
582 CovOS << " -: 0:Runs:" << RunCount << "\n";
583 CovOS << " -: 0:Programs:" << ProgramCount << "\n";
Yuchen Wu8aac4f62013-11-19 20:57:20 +0000584
Justin Bogner000b5222015-01-23 22:57:02 +0000585 const LineData &Line = LI.second;
Yuchen Wubb6a4772013-12-19 00:29:25 +0000586 GCOVCoverage FileCoverage(Filename);
Justin Bogner011c7422015-01-23 22:38:01 +0000587 for (uint32_t LineIndex = 0; LineIndex < Line.LastLine || !AllLines.empty();
588 ++LineIndex) {
Yuchen Wu73dc3812013-12-18 18:40:15 +0000589 if (Options.BranchInfo) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000590 FunctionLines::const_iterator FuncsIt = Line.Functions.find(LineIndex);
591 if (FuncsIt != Line.Functions.end())
Justin Bogner0b9858d2015-01-23 23:09:27 +0000592 printFunctionSummary(CovOS, FuncsIt->second);
Yuchen Wu342714c2013-12-13 01:15:07 +0000593 }
Yuchen Wu1c068162013-12-03 01:35:31 +0000594
Yuchen Wu342714c2013-12-13 01:15:07 +0000595 BlockLines::const_iterator BlocksIt = Line.Blocks.find(LineIndex);
596 if (BlocksIt == Line.Blocks.end()) {
597 // No basic blocks are on this line. Not an executable line of code.
Justin Bogner0b9858d2015-01-23 23:09:27 +0000598 CovOS << " -:";
599 AllLines.printNext(CovOS, LineIndex + 1);
Yuchen Wu342714c2013-12-13 01:15:07 +0000600 } else {
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000601 const BlockVector &Blocks = BlocksIt->second;
Yuchen Wu342714c2013-12-13 01:15:07 +0000602
603 // Add up the block counts to form line counts.
Yuchen Wubb6a4772013-12-19 00:29:25 +0000604 DenseMap<const GCOVFunction *, bool> LineExecs;
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000605 uint64_t LineCount = 0;
Justin Bogner000b5222015-01-23 22:57:02 +0000606 for (const GCOVBlock *Block : Blocks) {
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000607 if (Options.AllBlocks) {
608 // Only take the highest block count for that line.
609 uint64_t BlockCount = Block->getCount();
610 LineCount = LineCount > BlockCount ? LineCount : BlockCount;
611 } else {
612 // Sum up all of the block counts.
613 LineCount += Block->getCount();
614 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000615
616 if (Options.FuncCoverage) {
617 // This is a slightly convoluted way to most accurately gather line
618 // statistics for functions. Basically what is happening is that we
619 // don't want to count a single line with multiple blocks more than
620 // once. However, we also don't simply want to give the total line
621 // count to every function that starts on the line. Thus, what is
622 // happening here are two things:
623 // 1) Ensure that the number of logical lines is only incremented
624 // once per function.
625 // 2) If there are multiple blocks on the same line, ensure that the
626 // number of lines executed is incremented as long as at least
627 // one of the blocks are executed.
628 const GCOVFunction *Function = &Block->getParent();
629 if (FuncCoverages.find(Function) == FuncCoverages.end()) {
Justin Bogner011c7422015-01-23 22:38:01 +0000630 std::pair<const GCOVFunction *, GCOVCoverage> KeyValue(
631 Function, GCOVCoverage(Function->getName()));
Yuchen Wubb6a4772013-12-19 00:29:25 +0000632 FuncCoverages.insert(KeyValue);
633 }
634 GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second;
635
636 if (LineExecs.find(Function) == LineExecs.end()) {
637 if (Block->getCount()) {
638 ++FuncCoverage.LinesExec;
639 LineExecs[Function] = true;
640 } else {
641 LineExecs[Function] = false;
642 }
643 ++FuncCoverage.LogicalLines;
644 } else if (!LineExecs[Function] && Block->getCount()) {
645 ++FuncCoverage.LinesExec;
646 LineExecs[Function] = true;
647 }
648 }
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000649 }
Yuchen Wu8256ee62013-12-18 21:12:51 +0000650
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000651 if (LineCount == 0)
Justin Bogner0b9858d2015-01-23 23:09:27 +0000652 CovOS << " #####:";
Yuchen Wu8256ee62013-12-18 21:12:51 +0000653 else {
Justin Bogner0b9858d2015-01-23 23:09:27 +0000654 CovOS << format("%9" PRIu64 ":", LineCount);
Yuchen Wubb6a4772013-12-19 00:29:25 +0000655 ++FileCoverage.LinesExec;
Yuchen Wu8256ee62013-12-18 21:12:51 +0000656 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000657 ++FileCoverage.LogicalLines;
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000658
Justin Bogner0b9858d2015-01-23 23:09:27 +0000659 AllLines.printNext(CovOS, LineIndex + 1);
Yuchen Wu342714c2013-12-13 01:15:07 +0000660
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000661 uint32_t BlockNo = 0;
Yuchen Wu342714c2013-12-13 01:15:07 +0000662 uint32_t EdgeNo = 0;
Justin Bogner000b5222015-01-23 22:57:02 +0000663 for (const GCOVBlock *Block : Blocks) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000664 // Only print block and branch information at the end of the block.
Justin Bogner011c7422015-01-23 22:38:01 +0000665 if (Block->getLastLine() != LineIndex + 1)
Yuchen Wu342714c2013-12-13 01:15:07 +0000666 continue;
667 if (Options.AllBlocks)
Justin Bogner0b9858d2015-01-23 23:09:27 +0000668 printBlockInfo(CovOS, *Block, LineIndex, BlockNo);
Yuchen Wu73dc3812013-12-18 18:40:15 +0000669 if (Options.BranchInfo) {
Yuchen Wu66d93b82013-12-16 22:14:02 +0000670 size_t NumEdges = Block->getNumDstEdges();
671 if (NumEdges > 1)
Justin Bogner0b9858d2015-01-23 23:09:27 +0000672 printBranchInfo(CovOS, *Block, FileCoverage, EdgeNo);
Yuchen Wu66d93b82013-12-16 22:14:02 +0000673 else if (Options.UncondBranch && NumEdges == 1)
Justin Bogner0b9858d2015-01-23 23:09:27 +0000674 printUncondBranchInfo(CovOS, EdgeNo,
675 (*Block->dst_begin())->Count);
Yuchen Wu66d93b82013-12-16 22:14:02 +0000676 }
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000677 }
678 }
Devang Patel37140652011-09-28 18:50:00 +0000679 }
Justin Bognerc6af3502014-02-04 10:45:02 +0000680 FileCoverages.push_back(std::make_pair(CoveragePath, FileCoverage));
Devang Patel37140652011-09-28 18:50:00 +0000681 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000682
683 // FIXME: There is no way to detect calls given current instrumentation.
684 if (Options.FuncCoverage)
Justin Bogner0b9858d2015-01-23 23:09:27 +0000685 printFuncCoverage(InfoOS);
686 printFileCoverage(InfoOS);
Devang Patel37140652011-09-28 18:50:00 +0000687}
Yuchen Wu342714c2013-12-13 01:15:07 +0000688
689/// printFunctionSummary - Print function and block summary.
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000690void FileInfo::printFunctionSummary(raw_ostream &OS,
Yuchen Wu342714c2013-12-13 01:15:07 +0000691 const FunctionVector &Funcs) const {
Justin Bogner000b5222015-01-23 22:57:02 +0000692 for (const GCOVFunction *Func : Funcs) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000693 uint64_t EntryCount = Func->getEntryCount();
Yuchen Wuc9b2dcd2013-12-18 18:46:25 +0000694 uint32_t BlocksExec = 0;
Justin Bogner000b5222015-01-23 22:57:02 +0000695 for (const GCOVBlock &Block : Func->blocks())
David Blaikie09757492014-04-21 21:40:16 +0000696 if (Block.getNumDstEdges() && Block.getCount())
Justin Bogner011c7422015-01-23 22:38:01 +0000697 ++BlocksExec;
Yuchen Wu342714c2013-12-13 01:15:07 +0000698
699 OS << "function " << Func->getName() << " called " << EntryCount
Justin Bogner011c7422015-01-23 22:38:01 +0000700 << " returned " << safeDiv(Func->getExitCount() * 100, EntryCount)
Yuchen Wu342714c2013-12-13 01:15:07 +0000701 << "% blocks executed "
Justin Bogner011c7422015-01-23 22:38:01 +0000702 << safeDiv(BlocksExec * 100, Func->getNumBlocks() - 1) << "%\n";
Yuchen Wu342714c2013-12-13 01:15:07 +0000703 }
704}
705
706/// printBlockInfo - Output counts for each block.
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000707void FileInfo::printBlockInfo(raw_ostream &OS, const GCOVBlock &Block,
Yuchen Wu342714c2013-12-13 01:15:07 +0000708 uint32_t LineIndex, uint32_t &BlockNo) const {
709 if (Block.getCount() == 0)
710 OS << " $$$$$:";
711 else
712 OS << format("%9" PRIu64 ":", Block.getCount());
Justin Bogner011c7422015-01-23 22:38:01 +0000713 OS << format("%5u-block %2u\n", LineIndex + 1, BlockNo++);
Yuchen Wu342714c2013-12-13 01:15:07 +0000714}
715
Yuchen Wu66d93b82013-12-16 22:14:02 +0000716/// printBranchInfo - Print conditional branch probabilities.
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000717void FileInfo::printBranchInfo(raw_ostream &OS, const GCOVBlock &Block,
Yuchen Wubb6a4772013-12-19 00:29:25 +0000718 GCOVCoverage &Coverage, uint32_t &EdgeNo) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000719 SmallVector<uint64_t, 16> BranchCounts;
720 uint64_t TotalCounts = 0;
Justin Bogner000b5222015-01-23 22:57:02 +0000721 for (const GCOVEdge *Edge : Block.dsts()) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000722 BranchCounts.push_back(Edge->Count);
723 TotalCounts += Edge->Count;
Justin Bogner011c7422015-01-23 22:38:01 +0000724 if (Block.getCount())
725 ++Coverage.BranchesExec;
726 if (Edge->Count)
727 ++Coverage.BranchesTaken;
Yuchen Wu8256ee62013-12-18 21:12:51 +0000728 ++Coverage.Branches;
Yuchen Wubb6a4772013-12-19 00:29:25 +0000729
730 if (Options.FuncCoverage) {
731 const GCOVFunction *Function = &Block.getParent();
732 GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second;
Justin Bogner011c7422015-01-23 22:38:01 +0000733 if (Block.getCount())
734 ++FuncCoverage.BranchesExec;
735 if (Edge->Count)
736 ++FuncCoverage.BranchesTaken;
Yuchen Wubb6a4772013-12-19 00:29:25 +0000737 ++FuncCoverage.Branches;
738 }
Yuchen Wu342714c2013-12-13 01:15:07 +0000739 }
740
Justin Bogner000b5222015-01-23 22:57:02 +0000741 for (uint64_t N : BranchCounts)
Yuchen Wu73dc3812013-12-18 18:40:15 +0000742 OS << format("branch %2u ", EdgeNo++)
Justin Bogner000b5222015-01-23 22:57:02 +0000743 << formatBranchInfo(Options, N, TotalCounts) << "\n";
Yuchen Wu342714c2013-12-13 01:15:07 +0000744}
Yuchen Wu66d93b82013-12-16 22:14:02 +0000745
746/// printUncondBranchInfo - Print unconditional branch probabilities.
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000747void FileInfo::printUncondBranchInfo(raw_ostream &OS, uint32_t &EdgeNo,
Yuchen Wu66d93b82013-12-16 22:14:02 +0000748 uint64_t Count) const {
Yuchen Wu73dc3812013-12-18 18:40:15 +0000749 OS << format("unconditional %2u ", EdgeNo++)
750 << formatBranchInfo(Options, Count, Count) << "\n";
Yuchen Wu66d93b82013-12-16 22:14:02 +0000751}
Yuchen Wu8256ee62013-12-18 21:12:51 +0000752
Yuchen Wubb6a4772013-12-19 00:29:25 +0000753// printCoverage - Print generic coverage info used by both printFuncCoverage
754// and printFileCoverage.
Justin Bogner0b9858d2015-01-23 23:09:27 +0000755void FileInfo::printCoverage(raw_ostream &OS,
756 const GCOVCoverage &Coverage) const {
757 OS << format("Lines executed:%.2f%% of %u\n",
758 double(Coverage.LinesExec) * 100 / Coverage.LogicalLines,
759 Coverage.LogicalLines);
Yuchen Wu8256ee62013-12-18 21:12:51 +0000760 if (Options.BranchInfo) {
761 if (Coverage.Branches) {
Justin Bogner0b9858d2015-01-23 23:09:27 +0000762 OS << format("Branches executed:%.2f%% of %u\n",
763 double(Coverage.BranchesExec) * 100 / Coverage.Branches,
764 Coverage.Branches);
765 OS << format("Taken at least once:%.2f%% of %u\n",
766 double(Coverage.BranchesTaken) * 100 / Coverage.Branches,
767 Coverage.Branches);
Yuchen Wu8256ee62013-12-18 21:12:51 +0000768 } else {
Justin Bogner0b9858d2015-01-23 23:09:27 +0000769 OS << "No branches\n";
Yuchen Wu8256ee62013-12-18 21:12:51 +0000770 }
Justin Bogner0b9858d2015-01-23 23:09:27 +0000771 OS << "No calls\n"; // to be consistent with gcov
Yuchen Wu8256ee62013-12-18 21:12:51 +0000772 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000773}
774
775// printFuncCoverage - Print per-function coverage info.
Justin Bogner0b9858d2015-01-23 23:09:27 +0000776void FileInfo::printFuncCoverage(raw_ostream &OS) const {
Justin Bogner000b5222015-01-23 22:57:02 +0000777 for (const auto &FC : FuncCoverages) {
778 const GCOVCoverage &Coverage = FC.second;
Justin Bogner0b9858d2015-01-23 23:09:27 +0000779 OS << "Function '" << Coverage.Name << "'\n";
780 printCoverage(OS, Coverage);
781 OS << "\n";
Yuchen Wubb6a4772013-12-19 00:29:25 +0000782 }
783}
784
785// printFileCoverage - Print per-file coverage info.
Justin Bogner0b9858d2015-01-23 23:09:27 +0000786void FileInfo::printFileCoverage(raw_ostream &OS) const {
Justin Bogner000b5222015-01-23 22:57:02 +0000787 for (const auto &FC : FileCoverages) {
788 const std::string &Filename = FC.first;
789 const GCOVCoverage &Coverage = FC.second;
Justin Bogner0b9858d2015-01-23 23:09:27 +0000790 OS << "File '" << Coverage.Name << "'\n";
791 printCoverage(OS, Coverage);
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000792 if (!Options.NoOutput)
Justin Bogner0b9858d2015-01-23 23:09:27 +0000793 OS << Coverage.Name << ":creating '" << Filename << "'\n";
794 OS << "\n";
Yuchen Wubb6a4772013-12-19 00:29:25 +0000795 }
Yuchen Wu8256ee62013-12-18 21:12:51 +0000796}