blob: a9f7f45ee30595443dffc73709721a7503d6a616 [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) {
Justin Bogner34a34aa2016-02-08 22:49:40 +0000251 uint32_t HeaderLength;
252 if (!Buff.readInt(HeaderLength))
Justin Bogner011c7422015-01-23 22:38:01 +0000253 return false; // Function header length
Daniel Jasper87a24d52013-12-04 08:57:17 +0000254
Justin Bogner34a34aa2016-02-08 22:49:40 +0000255 uint64_t EndPos = Buff.getCursor() + HeaderLength * sizeof(uint32_t);
256
Yuchen Wu57529972013-12-04 05:42:28 +0000257 uint32_t GCDAIdent;
Justin Bogner011c7422015-01-23 22:38:01 +0000258 if (!Buff.readInt(GCDAIdent))
259 return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000260 if (Ident != GCDAIdent) {
Justin Bogner011c7422015-01-23 22:38:01 +0000261 errs() << "Function identifiers do not match: " << Ident
262 << " != " << GCDAIdent << " (in " << Name << ").\n";
Yuchen Wu57529972013-12-04 05:42:28 +0000263 return false;
264 }
Yuchen Wuba718332013-12-03 00:15:49 +0000265
Daniel Jasper87a24d52013-12-04 08:57:17 +0000266 uint32_t GCDAChecksum;
Justin Bogner011c7422015-01-23 22:38:01 +0000267 if (!Buff.readInt(GCDAChecksum))
268 return false;
Daniel Jasper87a24d52013-12-04 08:57:17 +0000269 if (Checksum != GCDAChecksum) {
Justin Bogner011c7422015-01-23 22:38:01 +0000270 errs() << "Function checksums do not match: " << Checksum
271 << " != " << GCDAChecksum << " (in " << Name << ").\n";
Daniel Jasper87a24d52013-12-04 08:57:17 +0000272 return false;
273 }
Yuchen Wu57529972013-12-04 05:42:28 +0000274
275 uint32_t CfgChecksum;
276 if (Version != GCOV::V402) {
Justin Bogner011c7422015-01-23 22:38:01 +0000277 if (!Buff.readInt(CfgChecksum))
278 return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000279 if (Parent.getChecksum() != CfgChecksum) {
280 errs() << "File checksums do not match: " << Parent.getChecksum()
281 << " != " << CfgChecksum << " (in " << Name << ").\n";
282 return false;
283 }
284 }
285
Justin Bogner34a34aa2016-02-08 22:49:40 +0000286 if (Buff.getCursor() < EndPos) {
287 StringRef GCDAName;
288 if (!Buff.readString(GCDAName))
289 return false;
290 if (Name != GCDAName) {
291 errs() << "Function names do not match: " << Name << " != " << GCDAName
292 << ".\n";
293 return false;
294 }
Yuchen Wu57529972013-12-04 05:42:28 +0000295 }
Yuchen Wuba718332013-12-03 00:15:49 +0000296
297 if (!Buff.readArcTag()) {
Yuchen Wu57529972013-12-04 05:42:28 +0000298 errs() << "Arc tag not found (in " << Name << ").\n";
Yuchen Wuba718332013-12-03 00:15:49 +0000299 return false;
300 }
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000301
Yuchen Wuba718332013-12-03 00:15:49 +0000302 uint32_t Count;
Justin Bogner011c7422015-01-23 22:38:01 +0000303 if (!Buff.readInt(Count))
304 return false;
Yuchen Wuba718332013-12-03 00:15:49 +0000305 Count /= 2;
306
307 // This for loop adds the counts for each block. A second nested loop is
308 // required to combine the edge counts that are contained in the GCDA file.
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000309 for (uint32_t BlockNo = 0; Count > 0; ++BlockNo) {
310 // The last block is always reserved for exit block
Justin Bognerd0a62432015-03-17 00:18:51 +0000311 if (BlockNo >= Blocks.size()) {
Yuchen Wu57529972013-12-04 05:42:28 +0000312 errs() << "Unexpected number of edges (in " << Name << ").\n";
Yuchen Wuba718332013-12-03 00:15:49 +0000313 return false;
314 }
Justin Bognerd0a62432015-03-17 00:18:51 +0000315 if (BlockNo == Blocks.size() - 1)
316 errs() << "(" << Name << ") has arcs from exit block.\n";
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000317 GCOVBlock &Block = *Blocks[BlockNo];
318 for (size_t EdgeNo = 0, End = Block.getNumDstEdges(); EdgeNo < End;
Justin Bogner011c7422015-01-23 22:38:01 +0000319 ++EdgeNo) {
Yuchen Wuba718332013-12-03 00:15:49 +0000320 if (Count == 0) {
Yuchen Wu57529972013-12-04 05:42:28 +0000321 errs() << "Unexpected number of edges (in " << Name << ").\n";
Yuchen Wuba718332013-12-03 00:15:49 +0000322 return false;
323 }
324 uint64_t ArcCount;
Justin Bogner011c7422015-01-23 22:38:01 +0000325 if (!Buff.readInt64(ArcCount))
326 return false;
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000327 Block.addCount(EdgeNo, ArcCount);
Yuchen Wuba718332013-12-03 00:15:49 +0000328 --Count;
329 }
Yuchen Wu342714c2013-12-13 01:15:07 +0000330 Block.sortDstEdges();
Yuchen Wuba718332013-12-03 00:15:49 +0000331 }
332 return true;
333}
334
Yuchen Wu342714c2013-12-13 01:15:07 +0000335/// getEntryCount - Get the number of times the function was called by
336/// retrieving the entry block's count.
337uint64_t GCOVFunction::getEntryCount() const {
338 return Blocks.front()->getCount();
339}
340
341/// getExitCount - Get the number of times the function returned by retrieving
342/// the exit block's count.
343uint64_t GCOVFunction::getExitCount() const {
344 return Blocks.back()->getCount();
345}
346
Yuchen Wu03678152013-10-25 02:22:24 +0000347/// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
Yaron Kereneb2a2542016-01-29 20:50:44 +0000348LLVM_DUMP_METHOD void GCOVFunction::dump() const {
Justin Bogner58e41342014-11-06 06:55:02 +0000349 dbgs() << "===== " << Name << " (" << Ident << ") @ " << Filename << ":"
350 << LineNumber << "\n";
David Blaikie09757492014-04-21 21:40:16 +0000351 for (const auto &Block : Blocks)
352 Block->dump();
Devang Patel37140652011-09-28 18:50:00 +0000353}
354
355/// collectLineCounts - Collect line counts. This must be used after
356/// reading .gcno and .gcda files.
357void GCOVFunction::collectLineCounts(FileInfo &FI) {
Justin Bogner95e0a702014-03-26 22:03:06 +0000358 // If the line number is zero, this is a function that doesn't actually appear
359 // in the source file, so there isn't anything we can do with it.
360 if (LineNumber == 0)
361 return;
362
David Blaikie09757492014-04-21 21:40:16 +0000363 for (const auto &Block : Blocks)
364 Block->collectLineCounts(FI);
Yuchen Wu342714c2013-12-13 01:15:07 +0000365 FI.addFunctionLine(Filename, LineNumber, this);
Devang Patel37140652011-09-28 18:50:00 +0000366}
367
368//===----------------------------------------------------------------------===//
369// GCOVBlock implementation.
370
371/// ~GCOVBlock - Delete GCOVBlock and its content.
372GCOVBlock::~GCOVBlock() {
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000373 SrcEdges.clear();
374 DstEdges.clear();
Yuchen Wud738bee2013-11-14 00:32:00 +0000375 Lines.clear();
Devang Patel37140652011-09-28 18:50:00 +0000376}
377
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000378/// addCount - Add to block counter while storing the edge count. If the
379/// destination has no outgoing edges, also update that block's count too.
380void GCOVBlock::addCount(size_t DstEdgeNo, uint64_t N) {
381 assert(DstEdgeNo < DstEdges.size()); // up to caller to ensure EdgeNo is valid
382 DstEdges[DstEdgeNo]->Count = N;
383 Counter += N;
David Blaikie09757492014-04-21 21:40:16 +0000384 if (!DstEdges[DstEdgeNo]->Dst.getNumDstEdges())
385 DstEdges[DstEdgeNo]->Dst.Counter += N;
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000386}
387
Yuchen Wu342714c2013-12-13 01:15:07 +0000388/// sortDstEdges - Sort destination edges by block number, nop if already
389/// sorted. This is required for printing branch info in the correct order.
390void GCOVBlock::sortDstEdges() {
391 if (!DstEdgesAreSorted) {
392 SortDstEdgesFunctor SortEdges;
393 std::stable_sort(DstEdges.begin(), DstEdges.end(), SortEdges);
394 }
395}
396
Devang Patel37140652011-09-28 18:50:00 +0000397/// collectLineCounts - Collect line counts. This must be used after
398/// reading .gcno and .gcda files.
399void GCOVBlock::collectLineCounts(FileInfo &FI) {
Justin Bogner000b5222015-01-23 22:57:02 +0000400 for (uint32_t N : Lines)
401 FI.addBlockLine(Parent.getFilename(), N, this);
Devang Patel37140652011-09-28 18:50:00 +0000402}
403
Yuchen Wu03678152013-10-25 02:22:24 +0000404/// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
Yaron Kereneb2a2542016-01-29 20:50:44 +0000405LLVM_DUMP_METHOD void GCOVBlock::dump() const {
Yuchen Wu03678152013-10-25 02:22:24 +0000406 dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000407 if (!SrcEdges.empty()) {
408 dbgs() << "\tSource Edges : ";
Justin Bogner000b5222015-01-23 22:57:02 +0000409 for (const GCOVEdge *Edge : SrcEdges)
David Blaikie09757492014-04-21 21:40:16 +0000410 dbgs() << Edge->Src.Number << " (" << Edge->Count << "), ";
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000411 dbgs() << "\n";
412 }
413 if (!DstEdges.empty()) {
414 dbgs() << "\tDestination Edges : ";
Justin Bogner000b5222015-01-23 22:57:02 +0000415 for (const GCOVEdge *Edge : DstEdges)
David Blaikie09757492014-04-21 21:40:16 +0000416 dbgs() << Edge->Dst.Number << " (" << Edge->Count << "), ";
Yuchen Wu03678152013-10-25 02:22:24 +0000417 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000418 }
419 if (!Lines.empty()) {
Yuchen Wu03678152013-10-25 02:22:24 +0000420 dbgs() << "\tLines : ";
Justin Bogner000b5222015-01-23 22:57:02 +0000421 for (uint32_t N : Lines)
422 dbgs() << (N) << ",";
Yuchen Wud738bee2013-11-14 00:32:00 +0000423 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000424 }
425}
426
427//===----------------------------------------------------------------------===//
Devang Patel37140652011-09-28 18:50:00 +0000428// FileInfo implementation.
429
Yuchen Wu342714c2013-12-13 01:15:07 +0000430// Safe integer division, returns 0 if numerator is 0.
431static uint32_t safeDiv(uint64_t Numerator, uint64_t Divisor) {
432 if (!Numerator)
433 return 0;
Justin Bogner011c7422015-01-23 22:38:01 +0000434 return Numerator / Divisor;
Yuchen Wu342714c2013-12-13 01:15:07 +0000435}
436
437// This custom division function mimics gcov's branch ouputs:
438// - Round to closest whole number
439// - Only output 0% or 100% if it's exactly that value
440static uint32_t branchDiv(uint64_t Numerator, uint64_t Divisor) {
441 if (!Numerator)
442 return 0;
443 if (Numerator == Divisor)
444 return 100;
445
Justin Bogner011c7422015-01-23 22:38:01 +0000446 uint8_t Res = (Numerator * 100 + Divisor / 2) / Divisor;
Yuchen Wu342714c2013-12-13 01:15:07 +0000447 if (Res == 0)
448 return 1;
449 if (Res == 100)
450 return 99;
451 return Res;
452}
453
Benjamin Kramerb1d8c462015-03-23 13:59:13 +0000454namespace {
Yuchen Wu73dc3812013-12-18 18:40:15 +0000455struct formatBranchInfo {
Richard Smithe7dc8bf2015-10-14 00:04:19 +0000456 formatBranchInfo(const GCOV::Options &Options, uint64_t Count, uint64_t Total)
Justin Bogner011c7422015-01-23 22:38:01 +0000457 : Options(Options), Count(Count), Total(Total) {}
Yuchen Wu73dc3812013-12-18 18:40:15 +0000458
459 void print(raw_ostream &OS) const {
460 if (!Total)
461 OS << "never executed";
462 else if (Options.BranchCount)
463 OS << "taken " << Count;
464 else
465 OS << "taken " << branchDiv(Count, Total) << "%";
466 }
467
Richard Smithe7dc8bf2015-10-14 00:04:19 +0000468 const GCOV::Options &Options;
Yuchen Wu73dc3812013-12-18 18:40:15 +0000469 uint64_t Count;
470 uint64_t Total;
471};
472
473static raw_ostream &operator<<(raw_ostream &OS, const formatBranchInfo &FBI) {
474 FBI.print(OS);
475 return OS;
476}
477
Justin Bognercf27e1b2014-05-07 02:11:23 +0000478class LineConsumer {
479 std::unique_ptr<MemoryBuffer> Buffer;
480 StringRef Remaining;
Justin Bogner011c7422015-01-23 22:38:01 +0000481
Justin Bognercf27e1b2014-05-07 02:11:23 +0000482public:
483 LineConsumer(StringRef Filename) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000484 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
485 MemoryBuffer::getFileOrSTDIN(Filename);
486 if (std::error_code EC = BufferOrErr.getError()) {
Justin Bognercf27e1b2014-05-07 02:11:23 +0000487 errs() << Filename << ": " << EC.message() << "\n";
488 Remaining = "";
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000489 } else {
490 Buffer = std::move(BufferOrErr.get());
Justin Bognercf27e1b2014-05-07 02:11:23 +0000491 Remaining = Buffer->getBuffer();
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000492 }
Justin Bognercf27e1b2014-05-07 02:11:23 +0000493 }
494 bool empty() { return Remaining.empty(); }
495 void printNext(raw_ostream &OS, uint32_t LineNum) {
496 StringRef Line;
497 if (empty())
498 Line = "/*EOF*/";
499 else
500 std::tie(Line, Remaining) = Remaining.split("\n");
501 OS << format("%5u:", LineNum) << Line << "\n";
502 }
503};
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +0000504} // end anonymous namespace
Justin Bognercf27e1b2014-05-07 02:11:23 +0000505
Justin Bognerc6af3502014-02-04 10:45:02 +0000506/// Convert a path to a gcov filename. If PreservePaths is true, this
507/// translates "/" to "#", ".." to "^", and drops ".", to match gcov.
508static std::string mangleCoveragePath(StringRef Filename, bool PreservePaths) {
509 if (!PreservePaths)
Justin Bognerc67f0252014-04-23 21:44:55 +0000510 return sys::path::filename(Filename).str();
Justin Bognerc6af3502014-02-04 10:45:02 +0000511
512 // This behaviour is defined by gcov in terms of text replacements, so it's
513 // not likely to do anything useful on filesystems with different textual
514 // conventions.
515 llvm::SmallString<256> Result("");
516 StringRef::iterator I, S, E;
517 for (I = S = Filename.begin(), E = Filename.end(); I != E; ++I) {
518 if (*I != '/')
519 continue;
520
521 if (I - S == 1 && *S == '.') {
522 // ".", the current directory, is skipped.
523 } else if (I - S == 2 && *S == '.' && *(S + 1) == '.') {
524 // "..", the parent directory, is replaced with "^".
525 Result.append("^#");
526 } else {
527 if (S < I)
528 // Leave other components intact,
529 Result.append(S, I);
530 // And separate with "#".
531 Result.push_back('#');
532 }
533 S = I + 1;
534 }
535
536 if (S < I)
537 Result.append(S, I);
Justin Bognerc6af3502014-02-04 10:45:02 +0000538 return Result.str();
539}
540
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000541std::string FileInfo::getCoveragePath(StringRef Filename,
542 StringRef MainFilename) {
543 if (Options.NoOutput)
544 // This is probably a bug in gcov, but when -n is specified, paths aren't
545 // mangled at all, and the -l and -p options are ignored. Here, we do the
546 // same.
547 return Filename;
548
549 std::string CoveragePath;
550 if (Options.LongFileNames && !Filename.equals(MainFilename))
551 CoveragePath =
552 mangleCoveragePath(MainFilename, Options.PreservePaths) + "##";
Justin Bogner011c7422015-01-23 22:38:01 +0000553 CoveragePath += mangleCoveragePath(Filename, Options.PreservePaths) + ".gcov";
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000554 return CoveragePath;
555}
556
557std::unique_ptr<raw_ostream>
558FileInfo::openCoveragePath(StringRef CoveragePath) {
559 if (Options.NoOutput)
Justin Bogner7c093732014-05-07 16:01:27 +0000560 return llvm::make_unique<raw_null_ostream>();
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000561
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000562 std::error_code EC;
Yaron Keren075759a2015-03-30 15:42:36 +0000563 auto OS = llvm::make_unique<raw_fd_ostream>(CoveragePath, EC,
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000564 sys::fs::F_Text);
565 if (EC) {
566 errs() << EC.message() << "\n";
Justin Bogner7c093732014-05-07 16:01:27 +0000567 return llvm::make_unique<raw_null_ostream>();
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000568 }
569 return std::move(OS);
570}
571
Devang Patel37140652011-09-28 18:50:00 +0000572/// print - Print source files with collected line count information.
Justin Bogner0b9858d2015-01-23 23:09:27 +0000573void FileInfo::print(raw_ostream &InfoOS, StringRef MainFilename,
574 StringRef GCNOFile, StringRef GCDAFile) {
Justin Bogner000b5222015-01-23 22:57:02 +0000575 for (const auto &LI : LineInfo) {
576 StringRef Filename = LI.first();
Justin Bognercf27e1b2014-05-07 02:11:23 +0000577 auto AllLines = LineConsumer(Filename);
Yuchen Wu8aac4f62013-11-19 20:57:20 +0000578
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000579 std::string CoveragePath = getCoveragePath(Filename, MainFilename);
Justin Bogner0b9858d2015-01-23 23:09:27 +0000580 std::unique_ptr<raw_ostream> CovStream = openCoveragePath(CoveragePath);
581 raw_ostream &CovOS = *CovStream;
Yuchen Wu26326ad2013-12-03 00:57:11 +0000582
Justin Bogner0b9858d2015-01-23 23:09:27 +0000583 CovOS << " -: 0:Source:" << Filename << "\n";
584 CovOS << " -: 0:Graph:" << GCNOFile << "\n";
585 CovOS << " -: 0:Data:" << GCDAFile << "\n";
586 CovOS << " -: 0:Runs:" << RunCount << "\n";
587 CovOS << " -: 0:Programs:" << ProgramCount << "\n";
Yuchen Wu8aac4f62013-11-19 20:57:20 +0000588
Justin Bogner000b5222015-01-23 22:57:02 +0000589 const LineData &Line = LI.second;
Yuchen Wubb6a4772013-12-19 00:29:25 +0000590 GCOVCoverage FileCoverage(Filename);
Justin Bogner011c7422015-01-23 22:38:01 +0000591 for (uint32_t LineIndex = 0; LineIndex < Line.LastLine || !AllLines.empty();
592 ++LineIndex) {
Yuchen Wu73dc3812013-12-18 18:40:15 +0000593 if (Options.BranchInfo) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000594 FunctionLines::const_iterator FuncsIt = Line.Functions.find(LineIndex);
595 if (FuncsIt != Line.Functions.end())
Justin Bogner0b9858d2015-01-23 23:09:27 +0000596 printFunctionSummary(CovOS, FuncsIt->second);
Yuchen Wu342714c2013-12-13 01:15:07 +0000597 }
Yuchen Wu1c068162013-12-03 01:35:31 +0000598
Yuchen Wu342714c2013-12-13 01:15:07 +0000599 BlockLines::const_iterator BlocksIt = Line.Blocks.find(LineIndex);
600 if (BlocksIt == Line.Blocks.end()) {
601 // No basic blocks are on this line. Not an executable line of code.
Justin Bogner0b9858d2015-01-23 23:09:27 +0000602 CovOS << " -:";
603 AllLines.printNext(CovOS, LineIndex + 1);
Yuchen Wu342714c2013-12-13 01:15:07 +0000604 } else {
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000605 const BlockVector &Blocks = BlocksIt->second;
Yuchen Wu342714c2013-12-13 01:15:07 +0000606
607 // Add up the block counts to form line counts.
Yuchen Wubb6a4772013-12-19 00:29:25 +0000608 DenseMap<const GCOVFunction *, bool> LineExecs;
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000609 uint64_t LineCount = 0;
Justin Bogner000b5222015-01-23 22:57:02 +0000610 for (const GCOVBlock *Block : Blocks) {
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000611 if (Options.AllBlocks) {
612 // Only take the highest block count for that line.
613 uint64_t BlockCount = Block->getCount();
614 LineCount = LineCount > BlockCount ? LineCount : BlockCount;
615 } else {
616 // Sum up all of the block counts.
617 LineCount += Block->getCount();
618 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000619
620 if (Options.FuncCoverage) {
621 // This is a slightly convoluted way to most accurately gather line
622 // statistics for functions. Basically what is happening is that we
623 // don't want to count a single line with multiple blocks more than
624 // once. However, we also don't simply want to give the total line
625 // count to every function that starts on the line. Thus, what is
626 // happening here are two things:
627 // 1) Ensure that the number of logical lines is only incremented
628 // once per function.
629 // 2) If there are multiple blocks on the same line, ensure that the
630 // number of lines executed is incremented as long as at least
631 // one of the blocks are executed.
632 const GCOVFunction *Function = &Block->getParent();
633 if (FuncCoverages.find(Function) == FuncCoverages.end()) {
Justin Bogner011c7422015-01-23 22:38:01 +0000634 std::pair<const GCOVFunction *, GCOVCoverage> KeyValue(
635 Function, GCOVCoverage(Function->getName()));
Yuchen Wubb6a4772013-12-19 00:29:25 +0000636 FuncCoverages.insert(KeyValue);
637 }
638 GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second;
639
640 if (LineExecs.find(Function) == LineExecs.end()) {
641 if (Block->getCount()) {
642 ++FuncCoverage.LinesExec;
643 LineExecs[Function] = true;
644 } else {
645 LineExecs[Function] = false;
646 }
647 ++FuncCoverage.LogicalLines;
648 } else if (!LineExecs[Function] && Block->getCount()) {
649 ++FuncCoverage.LinesExec;
650 LineExecs[Function] = true;
651 }
652 }
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000653 }
Yuchen Wu8256ee62013-12-18 21:12:51 +0000654
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000655 if (LineCount == 0)
Justin Bogner0b9858d2015-01-23 23:09:27 +0000656 CovOS << " #####:";
Yuchen Wu8256ee62013-12-18 21:12:51 +0000657 else {
Justin Bogner0b9858d2015-01-23 23:09:27 +0000658 CovOS << format("%9" PRIu64 ":", LineCount);
Yuchen Wubb6a4772013-12-19 00:29:25 +0000659 ++FileCoverage.LinesExec;
Yuchen Wu8256ee62013-12-18 21:12:51 +0000660 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000661 ++FileCoverage.LogicalLines;
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000662
Justin Bogner0b9858d2015-01-23 23:09:27 +0000663 AllLines.printNext(CovOS, LineIndex + 1);
Yuchen Wu342714c2013-12-13 01:15:07 +0000664
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000665 uint32_t BlockNo = 0;
Yuchen Wu342714c2013-12-13 01:15:07 +0000666 uint32_t EdgeNo = 0;
Justin Bogner000b5222015-01-23 22:57:02 +0000667 for (const GCOVBlock *Block : Blocks) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000668 // Only print block and branch information at the end of the block.
Justin Bogner011c7422015-01-23 22:38:01 +0000669 if (Block->getLastLine() != LineIndex + 1)
Yuchen Wu342714c2013-12-13 01:15:07 +0000670 continue;
671 if (Options.AllBlocks)
Justin Bogner0b9858d2015-01-23 23:09:27 +0000672 printBlockInfo(CovOS, *Block, LineIndex, BlockNo);
Yuchen Wu73dc3812013-12-18 18:40:15 +0000673 if (Options.BranchInfo) {
Yuchen Wu66d93b82013-12-16 22:14:02 +0000674 size_t NumEdges = Block->getNumDstEdges();
675 if (NumEdges > 1)
Justin Bogner0b9858d2015-01-23 23:09:27 +0000676 printBranchInfo(CovOS, *Block, FileCoverage, EdgeNo);
Yuchen Wu66d93b82013-12-16 22:14:02 +0000677 else if (Options.UncondBranch && NumEdges == 1)
Justin Bogner0b9858d2015-01-23 23:09:27 +0000678 printUncondBranchInfo(CovOS, EdgeNo,
679 (*Block->dst_begin())->Count);
Yuchen Wu66d93b82013-12-16 22:14:02 +0000680 }
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000681 }
682 }
Devang Patel37140652011-09-28 18:50:00 +0000683 }
Justin Bognerc6af3502014-02-04 10:45:02 +0000684 FileCoverages.push_back(std::make_pair(CoveragePath, FileCoverage));
Devang Patel37140652011-09-28 18:50:00 +0000685 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000686
687 // FIXME: There is no way to detect calls given current instrumentation.
688 if (Options.FuncCoverage)
Justin Bogner0b9858d2015-01-23 23:09:27 +0000689 printFuncCoverage(InfoOS);
690 printFileCoverage(InfoOS);
Devang Patel37140652011-09-28 18:50:00 +0000691}
Yuchen Wu342714c2013-12-13 01:15:07 +0000692
693/// printFunctionSummary - Print function and block summary.
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000694void FileInfo::printFunctionSummary(raw_ostream &OS,
Yuchen Wu342714c2013-12-13 01:15:07 +0000695 const FunctionVector &Funcs) const {
Justin Bogner000b5222015-01-23 22:57:02 +0000696 for (const GCOVFunction *Func : Funcs) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000697 uint64_t EntryCount = Func->getEntryCount();
Yuchen Wuc9b2dcd2013-12-18 18:46:25 +0000698 uint32_t BlocksExec = 0;
Justin Bogner000b5222015-01-23 22:57:02 +0000699 for (const GCOVBlock &Block : Func->blocks())
David Blaikie09757492014-04-21 21:40:16 +0000700 if (Block.getNumDstEdges() && Block.getCount())
Justin Bogner011c7422015-01-23 22:38:01 +0000701 ++BlocksExec;
Yuchen Wu342714c2013-12-13 01:15:07 +0000702
703 OS << "function " << Func->getName() << " called " << EntryCount
Justin Bogner011c7422015-01-23 22:38:01 +0000704 << " returned " << safeDiv(Func->getExitCount() * 100, EntryCount)
Yuchen Wu342714c2013-12-13 01:15:07 +0000705 << "% blocks executed "
Justin Bogner011c7422015-01-23 22:38:01 +0000706 << safeDiv(BlocksExec * 100, Func->getNumBlocks() - 1) << "%\n";
Yuchen Wu342714c2013-12-13 01:15:07 +0000707 }
708}
709
710/// printBlockInfo - Output counts for each block.
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000711void FileInfo::printBlockInfo(raw_ostream &OS, const GCOVBlock &Block,
Yuchen Wu342714c2013-12-13 01:15:07 +0000712 uint32_t LineIndex, uint32_t &BlockNo) const {
713 if (Block.getCount() == 0)
714 OS << " $$$$$:";
715 else
716 OS << format("%9" PRIu64 ":", Block.getCount());
Justin Bogner011c7422015-01-23 22:38:01 +0000717 OS << format("%5u-block %2u\n", LineIndex + 1, BlockNo++);
Yuchen Wu342714c2013-12-13 01:15:07 +0000718}
719
Yuchen Wu66d93b82013-12-16 22:14:02 +0000720/// printBranchInfo - Print conditional branch probabilities.
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000721void FileInfo::printBranchInfo(raw_ostream &OS, const GCOVBlock &Block,
Yuchen Wubb6a4772013-12-19 00:29:25 +0000722 GCOVCoverage &Coverage, uint32_t &EdgeNo) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000723 SmallVector<uint64_t, 16> BranchCounts;
724 uint64_t TotalCounts = 0;
Justin Bogner000b5222015-01-23 22:57:02 +0000725 for (const GCOVEdge *Edge : Block.dsts()) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000726 BranchCounts.push_back(Edge->Count);
727 TotalCounts += Edge->Count;
Justin Bogner011c7422015-01-23 22:38:01 +0000728 if (Block.getCount())
729 ++Coverage.BranchesExec;
730 if (Edge->Count)
731 ++Coverage.BranchesTaken;
Yuchen Wu8256ee62013-12-18 21:12:51 +0000732 ++Coverage.Branches;
Yuchen Wubb6a4772013-12-19 00:29:25 +0000733
734 if (Options.FuncCoverage) {
735 const GCOVFunction *Function = &Block.getParent();
736 GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second;
Justin Bogner011c7422015-01-23 22:38:01 +0000737 if (Block.getCount())
738 ++FuncCoverage.BranchesExec;
739 if (Edge->Count)
740 ++FuncCoverage.BranchesTaken;
Yuchen Wubb6a4772013-12-19 00:29:25 +0000741 ++FuncCoverage.Branches;
742 }
Yuchen Wu342714c2013-12-13 01:15:07 +0000743 }
744
Justin Bogner000b5222015-01-23 22:57:02 +0000745 for (uint64_t N : BranchCounts)
Yuchen Wu73dc3812013-12-18 18:40:15 +0000746 OS << format("branch %2u ", EdgeNo++)
Justin Bogner000b5222015-01-23 22:57:02 +0000747 << formatBranchInfo(Options, N, TotalCounts) << "\n";
Yuchen Wu342714c2013-12-13 01:15:07 +0000748}
Yuchen Wu66d93b82013-12-16 22:14:02 +0000749
750/// printUncondBranchInfo - Print unconditional branch probabilities.
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000751void FileInfo::printUncondBranchInfo(raw_ostream &OS, uint32_t &EdgeNo,
Yuchen Wu66d93b82013-12-16 22:14:02 +0000752 uint64_t Count) const {
Yuchen Wu73dc3812013-12-18 18:40:15 +0000753 OS << format("unconditional %2u ", EdgeNo++)
754 << formatBranchInfo(Options, Count, Count) << "\n";
Yuchen Wu66d93b82013-12-16 22:14:02 +0000755}
Yuchen Wu8256ee62013-12-18 21:12:51 +0000756
Yuchen Wubb6a4772013-12-19 00:29:25 +0000757// printCoverage - Print generic coverage info used by both printFuncCoverage
758// and printFileCoverage.
Justin Bogner0b9858d2015-01-23 23:09:27 +0000759void FileInfo::printCoverage(raw_ostream &OS,
760 const GCOVCoverage &Coverage) const {
761 OS << format("Lines executed:%.2f%% of %u\n",
762 double(Coverage.LinesExec) * 100 / Coverage.LogicalLines,
763 Coverage.LogicalLines);
Yuchen Wu8256ee62013-12-18 21:12:51 +0000764 if (Options.BranchInfo) {
765 if (Coverage.Branches) {
Justin Bogner0b9858d2015-01-23 23:09:27 +0000766 OS << format("Branches executed:%.2f%% of %u\n",
767 double(Coverage.BranchesExec) * 100 / Coverage.Branches,
768 Coverage.Branches);
769 OS << format("Taken at least once:%.2f%% of %u\n",
770 double(Coverage.BranchesTaken) * 100 / Coverage.Branches,
771 Coverage.Branches);
Yuchen Wu8256ee62013-12-18 21:12:51 +0000772 } else {
Justin Bogner0b9858d2015-01-23 23:09:27 +0000773 OS << "No branches\n";
Yuchen Wu8256ee62013-12-18 21:12:51 +0000774 }
Justin Bogner0b9858d2015-01-23 23:09:27 +0000775 OS << "No calls\n"; // to be consistent with gcov
Yuchen Wu8256ee62013-12-18 21:12:51 +0000776 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000777}
778
779// printFuncCoverage - Print per-function coverage info.
Justin Bogner0b9858d2015-01-23 23:09:27 +0000780void FileInfo::printFuncCoverage(raw_ostream &OS) const {
Justin Bogner000b5222015-01-23 22:57:02 +0000781 for (const auto &FC : FuncCoverages) {
782 const GCOVCoverage &Coverage = FC.second;
Justin Bogner0b9858d2015-01-23 23:09:27 +0000783 OS << "Function '" << Coverage.Name << "'\n";
784 printCoverage(OS, Coverage);
785 OS << "\n";
Yuchen Wubb6a4772013-12-19 00:29:25 +0000786 }
787}
788
789// printFileCoverage - Print per-file coverage info.
Justin Bogner0b9858d2015-01-23 23:09:27 +0000790void FileInfo::printFileCoverage(raw_ostream &OS) const {
Justin Bogner000b5222015-01-23 22:57:02 +0000791 for (const auto &FC : FileCoverages) {
792 const std::string &Filename = FC.first;
793 const GCOVCoverage &Coverage = FC.second;
Justin Bogner0b9858d2015-01-23 23:09:27 +0000794 OS << "File '" << Coverage.Name << "'\n";
795 printCoverage(OS, Coverage);
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000796 if (!Options.NoOutput)
Justin Bogner0b9858d2015-01-23 23:09:27 +0000797 OS << Coverage.Name << ":creating '" << Filename << "'\n";
798 OS << "\n";
Yuchen Wubb6a4772013-12-19 00:29:25 +0000799 }
Yuchen Wu8256ee62013-12-18 21:12:51 +0000800}