blob: 6ed589131725e82700fbe97f01a60ed96ed33ab6 [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>
Devang Patel37140652011-09-28 18:50:00 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28// GCOVFile implementation.
29
Yuchen Wubec4e902013-12-04 04:49:23 +000030/// readGCNO - Read GCNO buffer.
31bool GCOVFile::readGCNO(GCOVBuffer &Buffer) {
Justin Bogner011c7422015-01-23 22:38:01 +000032 if (!Buffer.readGCNOFormat())
33 return false;
34 if (!Buffer.readGCOVVersion(Version))
35 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000036
Justin Bogner011c7422015-01-23 22:38:01 +000037 if (!Buffer.readInt(Checksum))
38 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000039 while (true) {
Justin Bogner011c7422015-01-23 22:38:01 +000040 if (!Buffer.readFunctionTag())
41 break;
David Blaikie09757492014-04-21 21:40:16 +000042 auto GFun = make_unique<GCOVFunction>(*this);
Yuchen Wubec4e902013-12-04 04:49:23 +000043 if (!GFun->readGCNO(Buffer, Version))
44 return false;
David Blaikie09757492014-04-21 21:40:16 +000045 Functions.push_back(std::move(GFun));
Yuchen Wubec4e902013-12-04 04:49:23 +000046 }
47
Yuchen Wu21517e42013-12-04 05:07:36 +000048 GCNOInitialized = true;
Yuchen Wubec4e902013-12-04 04:49:23 +000049 return true;
Devang Patele5a8f2f92011-09-29 17:06:40 +000050}
51
Yuchen Wubec4e902013-12-04 04:49:23 +000052/// readGCDA - Read GCDA buffer. It is required that readGCDA() can only be
53/// called after readGCNO().
54bool GCOVFile::readGCDA(GCOVBuffer &Buffer) {
Yuchen Wu21517e42013-12-04 05:07:36 +000055 assert(GCNOInitialized && "readGCDA() can only be called after readGCNO()");
Justin Bogner011c7422015-01-23 22:38:01 +000056 if (!Buffer.readGCDAFormat())
57 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000058 GCOV::GCOVVersion GCDAVersion;
Justin Bogner011c7422015-01-23 22:38:01 +000059 if (!Buffer.readGCOVVersion(GCDAVersion))
60 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000061 if (Version != GCDAVersion) {
62 errs() << "GCOV versions do not match.\n";
Devang Patel37140652011-09-28 18:50:00 +000063 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000064 }
Devang Patel37140652011-09-28 18:50:00 +000065
Yuchen Wubec4e902013-12-04 04:49:23 +000066 uint32_t GCDAChecksum;
Justin Bogner011c7422015-01-23 22:38:01 +000067 if (!Buffer.readInt(GCDAChecksum))
68 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000069 if (Checksum != GCDAChecksum) {
Justin Bogner011c7422015-01-23 22:38:01 +000070 errs() << "File checksums do not match: " << Checksum
71 << " != " << GCDAChecksum << ".\n";
Yuchen Wubec4e902013-12-04 04:49:23 +000072 return false;
73 }
74 for (size_t i = 0, e = Functions.size(); i < e; ++i) {
75 if (!Buffer.readFunctionTag()) {
76 errs() << "Unexpected number of functions.\n";
Yuchen Wubabe7492013-11-20 04:15:05 +000077 return false;
78 }
Yuchen Wubec4e902013-12-04 04:49:23 +000079 if (!Functions[i]->readGCDA(Buffer, Version))
80 return false;
81 }
82 if (Buffer.readObjectTag()) {
83 uint32_t Length;
84 uint32_t Dummy;
Justin Bogner011c7422015-01-23 22:38:01 +000085 if (!Buffer.readInt(Length))
86 return false;
87 if (!Buffer.readInt(Dummy))
88 return false; // checksum
89 if (!Buffer.readInt(Dummy))
90 return false; // num
91 if (!Buffer.readInt(RunCount))
92 return false;
93 Buffer.advanceCursor(Length - 3);
Yuchen Wubec4e902013-12-04 04:49:23 +000094 }
95 while (Buffer.readProgramTag()) {
96 uint32_t Length;
Justin Bogner011c7422015-01-23 22:38:01 +000097 if (!Buffer.readInt(Length))
98 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000099 Buffer.advanceCursor(Length);
100 ++ProgramCount;
Yuchen Wu14ae8e62013-10-25 02:22:21 +0000101 }
102
Devang Patel37140652011-09-28 18:50:00 +0000103 return true;
104}
105
Yuchen Wu03678152013-10-25 02:22:24 +0000106/// dump - Dump GCOVFile content to dbgs() for debugging purposes.
Yuchen Wuef6909d2013-11-19 20:33:32 +0000107void GCOVFile::dump() const {
David Blaikie09757492014-04-21 21:40:16 +0000108 for (const auto &FPtr : Functions)
109 FPtr->dump();
Devang Patel37140652011-09-28 18:50:00 +0000110}
111
112/// collectLineCounts - Collect line counts. This must be used after
113/// reading .gcno and .gcda files.
114void GCOVFile::collectLineCounts(FileInfo &FI) {
David Blaikie09757492014-04-21 21:40:16 +0000115 for (const auto &FPtr : Functions)
116 FPtr->collectLineCounts(FI);
Yuchen Wu30672d92013-11-05 01:11:58 +0000117 FI.setRunCount(RunCount);
Yuchen Wu14ae8e62013-10-25 02:22:21 +0000118 FI.setProgramCount(ProgramCount);
Devang Patel37140652011-09-28 18:50:00 +0000119}
120
121//===----------------------------------------------------------------------===//
122// GCOVFunction implementation.
123
Yuchen Wuba718332013-12-03 00:15:49 +0000124/// readGCNO - Read a function from the GCNO buffer. Return false if an error
125/// occurs.
Yuchen Wubec4e902013-12-04 04:49:23 +0000126bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
Yuchen Wue28da842013-11-14 00:07:15 +0000127 uint32_t Dummy;
Justin Bogner011c7422015-01-23 22:38:01 +0000128 if (!Buff.readInt(Dummy))
129 return false; // Function header length
130 if (!Buff.readInt(Ident))
131 return false;
132 if (!Buff.readInt(Checksum))
133 return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000134 if (Version != GCOV::V402) {
135 uint32_t CfgChecksum;
Justin Bogner011c7422015-01-23 22:38:01 +0000136 if (!Buff.readInt(CfgChecksum))
137 return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000138 if (Parent.getChecksum() != CfgChecksum) {
139 errs() << "File checksums do not match: " << Parent.getChecksum()
140 << " != " << CfgChecksum << " in (" << Name << ").\n";
141 return false;
142 }
143 }
Justin Bogner011c7422015-01-23 22:38:01 +0000144 if (!Buff.readString(Name))
145 return false;
146 if (!Buff.readString(Filename))
147 return false;
148 if (!Buff.readInt(LineNumber))
149 return false;
Devang Patel37140652011-09-28 18:50:00 +0000150
151 // read blocks.
Yuchen Wue28da842013-11-14 00:07:15 +0000152 if (!Buff.readBlockTag()) {
153 errs() << "Block tag not found.\n";
154 return false;
155 }
156 uint32_t BlockCount;
Justin Bogner011c7422015-01-23 22:38:01 +0000157 if (!Buff.readInt(BlockCount))
158 return false;
Bob Wilson868e6e32013-10-22 20:02:36 +0000159 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
Justin Bogner011c7422015-01-23 22:38:01 +0000160 if (!Buff.readInt(Dummy))
161 return false; // Block flags;
David Blaikie09757492014-04-21 21:40:16 +0000162 Blocks.push_back(make_unique<GCOVBlock>(*this, i));
Devang Patel37140652011-09-28 18:50:00 +0000163 }
164
165 // read edges.
166 while (Buff.readEdgeTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +0000167 uint32_t EdgeCount;
Justin Bogner011c7422015-01-23 22:38:01 +0000168 if (!Buff.readInt(EdgeCount))
169 return false;
Yuchen Wue28da842013-11-14 00:07:15 +0000170 EdgeCount = (EdgeCount - 1) / 2;
171 uint32_t BlockNo;
Justin Bogner011c7422015-01-23 22:38:01 +0000172 if (!Buff.readInt(BlockNo))
173 return false;
Yuchen Wue28da842013-11-14 00:07:15 +0000174 if (BlockNo >= BlockCount) {
Yuchen Wu4c9f19d2013-12-05 22:02:33 +0000175 errs() << "Unexpected block number: " << BlockNo << " (in " << Name
176 << ").\n";
Yuchen Wue28da842013-11-14 00:07:15 +0000177 return false;
178 }
Bob Wilson868e6e32013-10-22 20:02:36 +0000179 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
Yuchen Wue28da842013-11-14 00:07:15 +0000180 uint32_t Dst;
Justin Bogner011c7422015-01-23 22:38:01 +0000181 if (!Buff.readInt(Dst))
182 return false;
David Blaikie09757492014-04-21 21:40:16 +0000183 Edges.push_back(make_unique<GCOVEdge>(*Blocks[BlockNo], *Blocks[Dst]));
184 GCOVEdge *Edge = Edges.back().get();
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000185 Blocks[BlockNo]->addDstEdge(Edge);
186 Blocks[Dst]->addSrcEdge(Edge);
Justin Bogner011c7422015-01-23 22:38:01 +0000187 if (!Buff.readInt(Dummy))
188 return false; // Edge flag
Devang Patel37140652011-09-28 18:50:00 +0000189 }
190 }
191
192 // read line table.
193 while (Buff.readLineTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +0000194 uint32_t LineTableLength;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000195 // Read the length of this line table.
Justin Bogner011c7422015-01-23 22:38:01 +0000196 if (!Buff.readInt(LineTableLength))
197 return false;
198 uint32_t EndPos = Buff.getCursor() + LineTableLength * 4;
Yuchen Wue28da842013-11-14 00:07:15 +0000199 uint32_t BlockNo;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000200 // Read the block number this table is associated with.
Justin Bogner011c7422015-01-23 22:38:01 +0000201 if (!Buff.readInt(BlockNo))
202 return false;
Yuchen Wue28da842013-11-14 00:07:15 +0000203 if (BlockNo >= BlockCount) {
Yuchen Wu4c9f19d2013-12-05 22:02:33 +0000204 errs() << "Unexpected block number: " << BlockNo << " (in " << Name
205 << ").\n";
Yuchen Wue28da842013-11-14 00:07:15 +0000206 return false;
Devang Patel37140652011-09-28 18:50:00 +0000207 }
David Blaikie09757492014-04-21 21:40:16 +0000208 GCOVBlock &Block = *Blocks[BlockNo];
Justin Bognerc475e1b2014-05-02 20:01:24 +0000209 // Read the word that pads the beginning of the line table. This may be a
210 // flag of some sort, but seems to always be zero.
Justin Bogner011c7422015-01-23 22:38:01 +0000211 if (!Buff.readInt(Dummy))
212 return false;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000213
214 // Line information starts here and continues up until the last word.
215 if (Buff.getCursor() != (EndPos - sizeof(uint32_t))) {
Yuchen Wud738bee2013-11-14 00:32:00 +0000216 StringRef F;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000217 // Read the source file name.
Justin Bogner011c7422015-01-23 22:38:01 +0000218 if (!Buff.readString(F))
219 return false;
Yuchen Wu4c9f19d2013-12-05 22:02:33 +0000220 if (Filename != F) {
221 errs() << "Multiple sources for a single basic block: " << Filename
222 << " != " << F << " (in " << Name << ").\n";
Yuchen Wud738bee2013-11-14 00:32:00 +0000223 return false;
224 }
Justin Bognerc475e1b2014-05-02 20:01:24 +0000225 // Read lines up to, but not including, the null terminator.
226 while (Buff.getCursor() < (EndPos - 2 * sizeof(uint32_t))) {
Yuchen Wue28da842013-11-14 00:07:15 +0000227 uint32_t Line;
Justin Bogner011c7422015-01-23 22:38:01 +0000228 if (!Buff.readInt(Line))
229 return false;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000230 // Line 0 means this instruction was injected by the compiler. Skip it.
Justin Bogner011c7422015-01-23 22:38:01 +0000231 if (!Line)
232 continue;
David Blaikie09757492014-04-21 21:40:16 +0000233 Block.addLine(Line);
Yuchen Wue28da842013-11-14 00:07:15 +0000234 }
Justin Bognerc475e1b2014-05-02 20:01:24 +0000235 // Read the null terminator.
Justin Bogner011c7422015-01-23 22:38:01 +0000236 if (!Buff.readInt(Dummy))
237 return false;
Yuchen Wue28da842013-11-14 00:07:15 +0000238 }
Justin Bognerc475e1b2014-05-02 20:01:24 +0000239 // The last word is either a flag or padding, it isn't clear which. Skip
240 // over it.
Justin Bogner011c7422015-01-23 22:38:01 +0000241 if (!Buff.readInt(Dummy))
242 return false;
Devang Patel37140652011-09-28 18:50:00 +0000243 }
244 return true;
245}
246
Yuchen Wuba718332013-12-03 00:15:49 +0000247/// readGCDA - Read a function from the GCDA buffer. Return false if an error
248/// occurs.
Yuchen Wubec4e902013-12-04 04:49:23 +0000249bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
Yuchen Wuba718332013-12-03 00:15:49 +0000250 uint32_t Dummy;
Justin Bogner011c7422015-01-23 22:38:01 +0000251 if (!Buff.readInt(Dummy))
252 return false; // Function header length
Daniel Jasper87a24d52013-12-04 08:57:17 +0000253
Yuchen Wu57529972013-12-04 05:42:28 +0000254 uint32_t GCDAIdent;
Justin Bogner011c7422015-01-23 22:38:01 +0000255 if (!Buff.readInt(GCDAIdent))
256 return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000257 if (Ident != GCDAIdent) {
Justin Bogner011c7422015-01-23 22:38:01 +0000258 errs() << "Function identifiers do not match: " << Ident
259 << " != " << GCDAIdent << " (in " << Name << ").\n";
Yuchen Wu57529972013-12-04 05:42:28 +0000260 return false;
261 }
Yuchen Wuba718332013-12-03 00:15:49 +0000262
Daniel Jasper87a24d52013-12-04 08:57:17 +0000263 uint32_t GCDAChecksum;
Justin Bogner011c7422015-01-23 22:38:01 +0000264 if (!Buff.readInt(GCDAChecksum))
265 return false;
Daniel Jasper87a24d52013-12-04 08:57:17 +0000266 if (Checksum != GCDAChecksum) {
Justin Bogner011c7422015-01-23 22:38:01 +0000267 errs() << "Function checksums do not match: " << Checksum
268 << " != " << GCDAChecksum << " (in " << Name << ").\n";
Daniel Jasper87a24d52013-12-04 08:57:17 +0000269 return false;
270 }
Yuchen Wu57529972013-12-04 05:42:28 +0000271
272 uint32_t CfgChecksum;
273 if (Version != GCOV::V402) {
Justin Bogner011c7422015-01-23 22:38:01 +0000274 if (!Buff.readInt(CfgChecksum))
275 return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000276 if (Parent.getChecksum() != CfgChecksum) {
277 errs() << "File checksums do not match: " << Parent.getChecksum()
278 << " != " << CfgChecksum << " (in " << Name << ").\n";
279 return false;
280 }
281 }
282
283 StringRef GCDAName;
Justin Bogner011c7422015-01-23 22:38:01 +0000284 if (!Buff.readString(GCDAName))
285 return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000286 if (Name != GCDAName) {
287 errs() << "Function names do not match: " << Name << " != " << GCDAName
288 << ".\n";
289 return false;
290 }
Yuchen Wuba718332013-12-03 00:15:49 +0000291
292 if (!Buff.readArcTag()) {
Yuchen Wu57529972013-12-04 05:42:28 +0000293 errs() << "Arc tag not found (in " << Name << ").\n";
Yuchen Wuba718332013-12-03 00:15:49 +0000294 return false;
295 }
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000296
Yuchen Wuba718332013-12-03 00:15:49 +0000297 uint32_t Count;
Justin Bogner011c7422015-01-23 22:38:01 +0000298 if (!Buff.readInt(Count))
299 return false;
Yuchen Wuba718332013-12-03 00:15:49 +0000300 Count /= 2;
301
302 // This for loop adds the counts for each block. A second nested loop is
303 // required to combine the edge counts that are contained in the GCDA file.
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000304 for (uint32_t BlockNo = 0; Count > 0; ++BlockNo) {
305 // The last block is always reserved for exit block
Justin Bognerd0a62432015-03-17 00:18:51 +0000306 if (BlockNo >= Blocks.size()) {
Yuchen Wu57529972013-12-04 05:42:28 +0000307 errs() << "Unexpected number of edges (in " << Name << ").\n";
Yuchen Wuba718332013-12-03 00:15:49 +0000308 return false;
309 }
Justin Bognerd0a62432015-03-17 00:18:51 +0000310 if (BlockNo == Blocks.size() - 1)
311 errs() << "(" << Name << ") has arcs from exit block.\n";
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000312 GCOVBlock &Block = *Blocks[BlockNo];
313 for (size_t EdgeNo = 0, End = Block.getNumDstEdges(); EdgeNo < End;
Justin Bogner011c7422015-01-23 22:38:01 +0000314 ++EdgeNo) {
Yuchen Wuba718332013-12-03 00:15:49 +0000315 if (Count == 0) {
Yuchen Wu57529972013-12-04 05:42:28 +0000316 errs() << "Unexpected number of edges (in " << Name << ").\n";
Yuchen Wuba718332013-12-03 00:15:49 +0000317 return false;
318 }
319 uint64_t ArcCount;
Justin Bogner011c7422015-01-23 22:38:01 +0000320 if (!Buff.readInt64(ArcCount))
321 return false;
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000322 Block.addCount(EdgeNo, ArcCount);
Yuchen Wuba718332013-12-03 00:15:49 +0000323 --Count;
324 }
Yuchen Wu342714c2013-12-13 01:15:07 +0000325 Block.sortDstEdges();
Yuchen Wuba718332013-12-03 00:15:49 +0000326 }
327 return true;
328}
329
Yuchen Wu342714c2013-12-13 01:15:07 +0000330/// getEntryCount - Get the number of times the function was called by
331/// retrieving the entry block's count.
332uint64_t GCOVFunction::getEntryCount() const {
333 return Blocks.front()->getCount();
334}
335
336/// getExitCount - Get the number of times the function returned by retrieving
337/// the exit block's count.
338uint64_t GCOVFunction::getExitCount() const {
339 return Blocks.back()->getCount();
340}
341
Yuchen Wu03678152013-10-25 02:22:24 +0000342/// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
Yuchen Wuef6909d2013-11-19 20:33:32 +0000343void GCOVFunction::dump() const {
Justin Bogner58e41342014-11-06 06:55:02 +0000344 dbgs() << "===== " << Name << " (" << Ident << ") @ " << Filename << ":"
345 << LineNumber << "\n";
David Blaikie09757492014-04-21 21:40:16 +0000346 for (const auto &Block : Blocks)
347 Block->dump();
Devang Patel37140652011-09-28 18:50:00 +0000348}
349
350/// collectLineCounts - Collect line counts. This must be used after
351/// reading .gcno and .gcda files.
352void GCOVFunction::collectLineCounts(FileInfo &FI) {
Justin Bogner95e0a702014-03-26 22:03:06 +0000353 // If the line number is zero, this is a function that doesn't actually appear
354 // in the source file, so there isn't anything we can do with it.
355 if (LineNumber == 0)
356 return;
357
David Blaikie09757492014-04-21 21:40:16 +0000358 for (const auto &Block : Blocks)
359 Block->collectLineCounts(FI);
Yuchen Wu342714c2013-12-13 01:15:07 +0000360 FI.addFunctionLine(Filename, LineNumber, this);
Devang Patel37140652011-09-28 18:50:00 +0000361}
362
363//===----------------------------------------------------------------------===//
364// GCOVBlock implementation.
365
366/// ~GCOVBlock - Delete GCOVBlock and its content.
367GCOVBlock::~GCOVBlock() {
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000368 SrcEdges.clear();
369 DstEdges.clear();
Yuchen Wud738bee2013-11-14 00:32:00 +0000370 Lines.clear();
Devang Patel37140652011-09-28 18:50:00 +0000371}
372
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000373/// addCount - Add to block counter while storing the edge count. If the
374/// destination has no outgoing edges, also update that block's count too.
375void GCOVBlock::addCount(size_t DstEdgeNo, uint64_t N) {
376 assert(DstEdgeNo < DstEdges.size()); // up to caller to ensure EdgeNo is valid
377 DstEdges[DstEdgeNo]->Count = N;
378 Counter += N;
David Blaikie09757492014-04-21 21:40:16 +0000379 if (!DstEdges[DstEdgeNo]->Dst.getNumDstEdges())
380 DstEdges[DstEdgeNo]->Dst.Counter += N;
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000381}
382
Yuchen Wu342714c2013-12-13 01:15:07 +0000383/// sortDstEdges - Sort destination edges by block number, nop if already
384/// sorted. This is required for printing branch info in the correct order.
385void GCOVBlock::sortDstEdges() {
386 if (!DstEdgesAreSorted) {
387 SortDstEdgesFunctor SortEdges;
388 std::stable_sort(DstEdges.begin(), DstEdges.end(), SortEdges);
389 }
390}
391
Devang Patel37140652011-09-28 18:50:00 +0000392/// collectLineCounts - Collect line counts. This must be used after
393/// reading .gcno and .gcda files.
394void GCOVBlock::collectLineCounts(FileInfo &FI) {
Justin Bogner000b5222015-01-23 22:57:02 +0000395 for (uint32_t N : Lines)
396 FI.addBlockLine(Parent.getFilename(), N, this);
Devang Patel37140652011-09-28 18:50:00 +0000397}
398
Yuchen Wu03678152013-10-25 02:22:24 +0000399/// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
Yuchen Wuef6909d2013-11-19 20:33:32 +0000400void GCOVBlock::dump() const {
Yuchen Wu03678152013-10-25 02:22:24 +0000401 dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000402 if (!SrcEdges.empty()) {
403 dbgs() << "\tSource Edges : ";
Justin Bogner000b5222015-01-23 22:57:02 +0000404 for (const GCOVEdge *Edge : SrcEdges)
David Blaikie09757492014-04-21 21:40:16 +0000405 dbgs() << Edge->Src.Number << " (" << Edge->Count << "), ";
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000406 dbgs() << "\n";
407 }
408 if (!DstEdges.empty()) {
409 dbgs() << "\tDestination Edges : ";
Justin Bogner000b5222015-01-23 22:57:02 +0000410 for (const GCOVEdge *Edge : DstEdges)
David Blaikie09757492014-04-21 21:40:16 +0000411 dbgs() << Edge->Dst.Number << " (" << Edge->Count << "), ";
Yuchen Wu03678152013-10-25 02:22:24 +0000412 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000413 }
414 if (!Lines.empty()) {
Yuchen Wu03678152013-10-25 02:22:24 +0000415 dbgs() << "\tLines : ";
Justin Bogner000b5222015-01-23 22:57:02 +0000416 for (uint32_t N : Lines)
417 dbgs() << (N) << ",";
Yuchen Wud738bee2013-11-14 00:32:00 +0000418 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000419 }
420}
421
422//===----------------------------------------------------------------------===//
Devang Patel37140652011-09-28 18:50:00 +0000423// FileInfo implementation.
424
Yuchen Wu342714c2013-12-13 01:15:07 +0000425// Safe integer division, returns 0 if numerator is 0.
426static uint32_t safeDiv(uint64_t Numerator, uint64_t Divisor) {
427 if (!Numerator)
428 return 0;
Justin Bogner011c7422015-01-23 22:38:01 +0000429 return Numerator / Divisor;
Yuchen Wu342714c2013-12-13 01:15:07 +0000430}
431
432// This custom division function mimics gcov's branch ouputs:
433// - Round to closest whole number
434// - Only output 0% or 100% if it's exactly that value
435static uint32_t branchDiv(uint64_t Numerator, uint64_t Divisor) {
436 if (!Numerator)
437 return 0;
438 if (Numerator == Divisor)
439 return 100;
440
Justin Bogner011c7422015-01-23 22:38:01 +0000441 uint8_t Res = (Numerator * 100 + Divisor / 2) / Divisor;
Yuchen Wu342714c2013-12-13 01:15:07 +0000442 if (Res == 0)
443 return 1;
444 if (Res == 100)
445 return 99;
446 return Res;
447}
448
Benjamin Kramerb1d8c462015-03-23 13:59:13 +0000449namespace {
Yuchen Wu73dc3812013-12-18 18:40:15 +0000450struct formatBranchInfo {
Justin Bogner011c7422015-01-23 22:38:01 +0000451 formatBranchInfo(const GCOVOptions &Options, uint64_t Count, uint64_t Total)
452 : Options(Options), Count(Count), Total(Total) {}
Yuchen Wu73dc3812013-12-18 18:40:15 +0000453
454 void print(raw_ostream &OS) const {
455 if (!Total)
456 OS << "never executed";
457 else if (Options.BranchCount)
458 OS << "taken " << Count;
459 else
460 OS << "taken " << branchDiv(Count, Total) << "%";
461 }
462
463 const GCOVOptions &Options;
464 uint64_t Count;
465 uint64_t Total;
466};
467
468static raw_ostream &operator<<(raw_ostream &OS, const formatBranchInfo &FBI) {
469 FBI.print(OS);
470 return OS;
471}
472
Justin Bognercf27e1b2014-05-07 02:11:23 +0000473class LineConsumer {
474 std::unique_ptr<MemoryBuffer> Buffer;
475 StringRef Remaining;
Justin Bogner011c7422015-01-23 22:38:01 +0000476
Justin Bognercf27e1b2014-05-07 02:11:23 +0000477public:
478 LineConsumer(StringRef Filename) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000479 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
480 MemoryBuffer::getFileOrSTDIN(Filename);
481 if (std::error_code EC = BufferOrErr.getError()) {
Justin Bognercf27e1b2014-05-07 02:11:23 +0000482 errs() << Filename << ": " << EC.message() << "\n";
483 Remaining = "";
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000484 } else {
485 Buffer = std::move(BufferOrErr.get());
Justin Bognercf27e1b2014-05-07 02:11:23 +0000486 Remaining = Buffer->getBuffer();
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000487 }
Justin Bognercf27e1b2014-05-07 02:11:23 +0000488 }
489 bool empty() { return Remaining.empty(); }
490 void printNext(raw_ostream &OS, uint32_t LineNum) {
491 StringRef Line;
492 if (empty())
493 Line = "/*EOF*/";
494 else
495 std::tie(Line, Remaining) = Remaining.split("\n");
496 OS << format("%5u:", LineNum) << Line << "\n";
497 }
498};
499}
500
Justin Bognerc6af3502014-02-04 10:45:02 +0000501/// Convert a path to a gcov filename. If PreservePaths is true, this
502/// translates "/" to "#", ".." to "^", and drops ".", to match gcov.
503static std::string mangleCoveragePath(StringRef Filename, bool PreservePaths) {
504 if (!PreservePaths)
Justin Bognerc67f0252014-04-23 21:44:55 +0000505 return sys::path::filename(Filename).str();
Justin Bognerc6af3502014-02-04 10:45:02 +0000506
507 // This behaviour is defined by gcov in terms of text replacements, so it's
508 // not likely to do anything useful on filesystems with different textual
509 // conventions.
510 llvm::SmallString<256> Result("");
511 StringRef::iterator I, S, E;
512 for (I = S = Filename.begin(), E = Filename.end(); I != E; ++I) {
513 if (*I != '/')
514 continue;
515
516 if (I - S == 1 && *S == '.') {
517 // ".", the current directory, is skipped.
518 } else if (I - S == 2 && *S == '.' && *(S + 1) == '.') {
519 // "..", the parent directory, is replaced with "^".
520 Result.append("^#");
521 } else {
522 if (S < I)
523 // Leave other components intact,
524 Result.append(S, I);
525 // And separate with "#".
526 Result.push_back('#');
527 }
528 S = I + 1;
529 }
530
531 if (S < I)
532 Result.append(S, I);
Justin Bognerc6af3502014-02-04 10:45:02 +0000533 return Result.str();
534}
535
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000536std::string FileInfo::getCoveragePath(StringRef Filename,
537 StringRef MainFilename) {
538 if (Options.NoOutput)
539 // This is probably a bug in gcov, but when -n is specified, paths aren't
540 // mangled at all, and the -l and -p options are ignored. Here, we do the
541 // same.
542 return Filename;
543
544 std::string CoveragePath;
545 if (Options.LongFileNames && !Filename.equals(MainFilename))
546 CoveragePath =
547 mangleCoveragePath(MainFilename, Options.PreservePaths) + "##";
Justin Bogner011c7422015-01-23 22:38:01 +0000548 CoveragePath += mangleCoveragePath(Filename, Options.PreservePaths) + ".gcov";
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000549 return CoveragePath;
550}
551
552std::unique_ptr<raw_ostream>
553FileInfo::openCoveragePath(StringRef CoveragePath) {
554 if (Options.NoOutput)
Justin Bogner7c093732014-05-07 16:01:27 +0000555 return llvm::make_unique<raw_null_ostream>();
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000556
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000557 std::error_code EC;
Yaron Keren075759a2015-03-30 15:42:36 +0000558 auto OS = llvm::make_unique<raw_fd_ostream>(CoveragePath, EC,
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000559 sys::fs::F_Text);
560 if (EC) {
561 errs() << EC.message() << "\n";
Justin Bogner7c093732014-05-07 16:01:27 +0000562 return llvm::make_unique<raw_null_ostream>();
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000563 }
564 return std::move(OS);
565}
566
Devang Patel37140652011-09-28 18:50:00 +0000567/// print - Print source files with collected line count information.
Justin Bogner0b9858d2015-01-23 23:09:27 +0000568void FileInfo::print(raw_ostream &InfoOS, StringRef MainFilename,
569 StringRef GCNOFile, StringRef GCDAFile) {
Justin Bogner000b5222015-01-23 22:57:02 +0000570 for (const auto &LI : LineInfo) {
571 StringRef Filename = LI.first();
Justin Bognercf27e1b2014-05-07 02:11:23 +0000572 auto AllLines = LineConsumer(Filename);
Yuchen Wu8aac4f62013-11-19 20:57:20 +0000573
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000574 std::string CoveragePath = getCoveragePath(Filename, MainFilename);
Justin Bogner0b9858d2015-01-23 23:09:27 +0000575 std::unique_ptr<raw_ostream> CovStream = openCoveragePath(CoveragePath);
576 raw_ostream &CovOS = *CovStream;
Yuchen Wu26326ad2013-12-03 00:57:11 +0000577
Justin Bogner0b9858d2015-01-23 23:09:27 +0000578 CovOS << " -: 0:Source:" << Filename << "\n";
579 CovOS << " -: 0:Graph:" << GCNOFile << "\n";
580 CovOS << " -: 0:Data:" << GCDAFile << "\n";
581 CovOS << " -: 0:Runs:" << RunCount << "\n";
582 CovOS << " -: 0:Programs:" << ProgramCount << "\n";
Yuchen Wu8aac4f62013-11-19 20:57:20 +0000583
Justin Bogner000b5222015-01-23 22:57:02 +0000584 const LineData &Line = LI.second;
Yuchen Wubb6a4772013-12-19 00:29:25 +0000585 GCOVCoverage FileCoverage(Filename);
Justin Bogner011c7422015-01-23 22:38:01 +0000586 for (uint32_t LineIndex = 0; LineIndex < Line.LastLine || !AllLines.empty();
587 ++LineIndex) {
Yuchen Wu73dc3812013-12-18 18:40:15 +0000588 if (Options.BranchInfo) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000589 FunctionLines::const_iterator FuncsIt = Line.Functions.find(LineIndex);
590 if (FuncsIt != Line.Functions.end())
Justin Bogner0b9858d2015-01-23 23:09:27 +0000591 printFunctionSummary(CovOS, FuncsIt->second);
Yuchen Wu342714c2013-12-13 01:15:07 +0000592 }
Yuchen Wu1c068162013-12-03 01:35:31 +0000593
Yuchen Wu342714c2013-12-13 01:15:07 +0000594 BlockLines::const_iterator BlocksIt = Line.Blocks.find(LineIndex);
595 if (BlocksIt == Line.Blocks.end()) {
596 // No basic blocks are on this line. Not an executable line of code.
Justin Bogner0b9858d2015-01-23 23:09:27 +0000597 CovOS << " -:";
598 AllLines.printNext(CovOS, LineIndex + 1);
Yuchen Wu342714c2013-12-13 01:15:07 +0000599 } else {
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000600 const BlockVector &Blocks = BlocksIt->second;
Yuchen Wu342714c2013-12-13 01:15:07 +0000601
602 // Add up the block counts to form line counts.
Yuchen Wubb6a4772013-12-19 00:29:25 +0000603 DenseMap<const GCOVFunction *, bool> LineExecs;
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000604 uint64_t LineCount = 0;
Justin Bogner000b5222015-01-23 22:57:02 +0000605 for (const GCOVBlock *Block : Blocks) {
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000606 if (Options.AllBlocks) {
607 // Only take the highest block count for that line.
608 uint64_t BlockCount = Block->getCount();
609 LineCount = LineCount > BlockCount ? LineCount : BlockCount;
610 } else {
611 // Sum up all of the block counts.
612 LineCount += Block->getCount();
613 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000614
615 if (Options.FuncCoverage) {
616 // This is a slightly convoluted way to most accurately gather line
617 // statistics for functions. Basically what is happening is that we
618 // don't want to count a single line with multiple blocks more than
619 // once. However, we also don't simply want to give the total line
620 // count to every function that starts on the line. Thus, what is
621 // happening here are two things:
622 // 1) Ensure that the number of logical lines is only incremented
623 // once per function.
624 // 2) If there are multiple blocks on the same line, ensure that the
625 // number of lines executed is incremented as long as at least
626 // one of the blocks are executed.
627 const GCOVFunction *Function = &Block->getParent();
628 if (FuncCoverages.find(Function) == FuncCoverages.end()) {
Justin Bogner011c7422015-01-23 22:38:01 +0000629 std::pair<const GCOVFunction *, GCOVCoverage> KeyValue(
630 Function, GCOVCoverage(Function->getName()));
Yuchen Wubb6a4772013-12-19 00:29:25 +0000631 FuncCoverages.insert(KeyValue);
632 }
633 GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second;
634
635 if (LineExecs.find(Function) == LineExecs.end()) {
636 if (Block->getCount()) {
637 ++FuncCoverage.LinesExec;
638 LineExecs[Function] = true;
639 } else {
640 LineExecs[Function] = false;
641 }
642 ++FuncCoverage.LogicalLines;
643 } else if (!LineExecs[Function] && Block->getCount()) {
644 ++FuncCoverage.LinesExec;
645 LineExecs[Function] = true;
646 }
647 }
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000648 }
Yuchen Wu8256ee62013-12-18 21:12:51 +0000649
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000650 if (LineCount == 0)
Justin Bogner0b9858d2015-01-23 23:09:27 +0000651 CovOS << " #####:";
Yuchen Wu8256ee62013-12-18 21:12:51 +0000652 else {
Justin Bogner0b9858d2015-01-23 23:09:27 +0000653 CovOS << format("%9" PRIu64 ":", LineCount);
Yuchen Wubb6a4772013-12-19 00:29:25 +0000654 ++FileCoverage.LinesExec;
Yuchen Wu8256ee62013-12-18 21:12:51 +0000655 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000656 ++FileCoverage.LogicalLines;
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000657
Justin Bogner0b9858d2015-01-23 23:09:27 +0000658 AllLines.printNext(CovOS, LineIndex + 1);
Yuchen Wu342714c2013-12-13 01:15:07 +0000659
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000660 uint32_t BlockNo = 0;
Yuchen Wu342714c2013-12-13 01:15:07 +0000661 uint32_t EdgeNo = 0;
Justin Bogner000b5222015-01-23 22:57:02 +0000662 for (const GCOVBlock *Block : Blocks) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000663 // Only print block and branch information at the end of the block.
Justin Bogner011c7422015-01-23 22:38:01 +0000664 if (Block->getLastLine() != LineIndex + 1)
Yuchen Wu342714c2013-12-13 01:15:07 +0000665 continue;
666 if (Options.AllBlocks)
Justin Bogner0b9858d2015-01-23 23:09:27 +0000667 printBlockInfo(CovOS, *Block, LineIndex, BlockNo);
Yuchen Wu73dc3812013-12-18 18:40:15 +0000668 if (Options.BranchInfo) {
Yuchen Wu66d93b82013-12-16 22:14:02 +0000669 size_t NumEdges = Block->getNumDstEdges();
670 if (NumEdges > 1)
Justin Bogner0b9858d2015-01-23 23:09:27 +0000671 printBranchInfo(CovOS, *Block, FileCoverage, EdgeNo);
Yuchen Wu66d93b82013-12-16 22:14:02 +0000672 else if (Options.UncondBranch && NumEdges == 1)
Justin Bogner0b9858d2015-01-23 23:09:27 +0000673 printUncondBranchInfo(CovOS, EdgeNo,
674 (*Block->dst_begin())->Count);
Yuchen Wu66d93b82013-12-16 22:14:02 +0000675 }
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000676 }
677 }
Devang Patel37140652011-09-28 18:50:00 +0000678 }
Justin Bognerc6af3502014-02-04 10:45:02 +0000679 FileCoverages.push_back(std::make_pair(CoveragePath, FileCoverage));
Devang Patel37140652011-09-28 18:50:00 +0000680 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000681
682 // FIXME: There is no way to detect calls given current instrumentation.
683 if (Options.FuncCoverage)
Justin Bogner0b9858d2015-01-23 23:09:27 +0000684 printFuncCoverage(InfoOS);
685 printFileCoverage(InfoOS);
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000686 return;
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}