blob: 3c04716333ef5dbaa1b334b39c21e4aedbfec3b6 [file] [log] [blame]
Yuchen Wuc3e64242013-12-05 22:02:29 +00001//===- GCOV.cpp - LLVM coverage tool --------------------------------------===//
Devang Patel37140652011-09-28 18:50:00 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
NAKAMURA Takumi3b551962013-11-14 11:44:58 +000010// GCOV implements the interface to read and write coverage files that use
Devang Patel8dfb6552011-10-04 17:24:48 +000011// 'gcov' format.
Devang Patel37140652011-09-28 18:50:00 +000012//
Devang Patel37140652011-09-28 18:50:00 +000013//===----------------------------------------------------------------------===//
14
Justin Bognerdf82c622014-02-04 21:03:17 +000015#include "llvm/Support/GCOV.h"
Devang Patela9e8a252011-09-29 16:46:47 +000016#include "llvm/ADT/STLExtras.h"
Justin Bognerdf82c622014-02-04 21:03:17 +000017#include "llvm/Support/Debug.h"
Justin Bognerc6af3502014-02-04 10:45:02 +000018#include "llvm/Support/FileSystem.h"
Justin Bognerdf82c622014-02-04 21:03:17 +000019#include "llvm/Support/Format.h"
Devang Patel37140652011-09-28 18:50:00 +000020#include "llvm/Support/MemoryObject.h"
Justin Bognerc6af3502014-02-04 10:45:02 +000021#include "llvm/Support/Path.h"
Yuchen Wu342714c2013-12-13 01:15:07 +000022#include <algorithm>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000023#include <system_error>
Devang Patel37140652011-09-28 18:50:00 +000024using namespace llvm;
25
26//===----------------------------------------------------------------------===//
27// GCOVFile implementation.
28
Yuchen Wubec4e902013-12-04 04:49:23 +000029/// readGCNO - Read GCNO buffer.
30bool GCOVFile::readGCNO(GCOVBuffer &Buffer) {
Justin Bogner011c7422015-01-23 22:38:01 +000031 if (!Buffer.readGCNOFormat())
32 return false;
33 if (!Buffer.readGCOVVersion(Version))
34 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000035
Justin Bogner011c7422015-01-23 22:38:01 +000036 if (!Buffer.readInt(Checksum))
37 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000038 while (true) {
Justin Bogner011c7422015-01-23 22:38:01 +000039 if (!Buffer.readFunctionTag())
40 break;
David Blaikie09757492014-04-21 21:40:16 +000041 auto GFun = make_unique<GCOVFunction>(*this);
Yuchen Wubec4e902013-12-04 04:49:23 +000042 if (!GFun->readGCNO(Buffer, Version))
43 return false;
David Blaikie09757492014-04-21 21:40:16 +000044 Functions.push_back(std::move(GFun));
Yuchen Wubec4e902013-12-04 04:49:23 +000045 }
46
Yuchen Wu21517e42013-12-04 05:07:36 +000047 GCNOInitialized = true;
Yuchen Wubec4e902013-12-04 04:49:23 +000048 return true;
Devang Patele5a8f2f92011-09-29 17:06:40 +000049}
50
Yuchen Wubec4e902013-12-04 04:49:23 +000051/// readGCDA - Read GCDA buffer. It is required that readGCDA() can only be
52/// called after readGCNO().
53bool GCOVFile::readGCDA(GCOVBuffer &Buffer) {
Yuchen Wu21517e42013-12-04 05:07:36 +000054 assert(GCNOInitialized && "readGCDA() can only be called after readGCNO()");
Justin Bogner011c7422015-01-23 22:38:01 +000055 if (!Buffer.readGCDAFormat())
56 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000057 GCOV::GCOVVersion GCDAVersion;
Justin Bogner011c7422015-01-23 22:38:01 +000058 if (!Buffer.readGCOVVersion(GCDAVersion))
59 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000060 if (Version != GCDAVersion) {
61 errs() << "GCOV versions do not match.\n";
Devang Patel37140652011-09-28 18:50:00 +000062 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000063 }
Devang Patel37140652011-09-28 18:50:00 +000064
Yuchen Wubec4e902013-12-04 04:49:23 +000065 uint32_t GCDAChecksum;
Justin Bogner011c7422015-01-23 22:38:01 +000066 if (!Buffer.readInt(GCDAChecksum))
67 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000068 if (Checksum != GCDAChecksum) {
Justin Bogner011c7422015-01-23 22:38:01 +000069 errs() << "File checksums do not match: " << Checksum
70 << " != " << GCDAChecksum << ".\n";
Yuchen Wubec4e902013-12-04 04:49:23 +000071 return false;
72 }
73 for (size_t i = 0, e = Functions.size(); i < e; ++i) {
74 if (!Buffer.readFunctionTag()) {
75 errs() << "Unexpected number of functions.\n";
Yuchen Wubabe7492013-11-20 04:15:05 +000076 return false;
77 }
Yuchen Wubec4e902013-12-04 04:49:23 +000078 if (!Functions[i]->readGCDA(Buffer, Version))
79 return false;
80 }
81 if (Buffer.readObjectTag()) {
82 uint32_t Length;
83 uint32_t Dummy;
Justin Bogner011c7422015-01-23 22:38:01 +000084 if (!Buffer.readInt(Length))
85 return false;
86 if (!Buffer.readInt(Dummy))
87 return false; // checksum
88 if (!Buffer.readInt(Dummy))
89 return false; // num
90 if (!Buffer.readInt(RunCount))
91 return false;
92 Buffer.advanceCursor(Length - 3);
Yuchen Wubec4e902013-12-04 04:49:23 +000093 }
94 while (Buffer.readProgramTag()) {
95 uint32_t Length;
Justin Bogner011c7422015-01-23 22:38:01 +000096 if (!Buffer.readInt(Length))
97 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000098 Buffer.advanceCursor(Length);
99 ++ProgramCount;
Yuchen Wu14ae8e62013-10-25 02:22:21 +0000100 }
101
Devang Patel37140652011-09-28 18:50:00 +0000102 return true;
103}
104
Yuchen Wu03678152013-10-25 02:22:24 +0000105/// dump - Dump GCOVFile content to dbgs() for debugging purposes.
Yuchen Wuef6909d2013-11-19 20:33:32 +0000106void GCOVFile::dump() const {
David Blaikie09757492014-04-21 21:40:16 +0000107 for (const auto &FPtr : Functions)
108 FPtr->dump();
Devang Patel37140652011-09-28 18:50:00 +0000109}
110
111/// collectLineCounts - Collect line counts. This must be used after
112/// reading .gcno and .gcda files.
113void GCOVFile::collectLineCounts(FileInfo &FI) {
David Blaikie09757492014-04-21 21:40:16 +0000114 for (const auto &FPtr : Functions)
115 FPtr->collectLineCounts(FI);
Yuchen Wu30672d92013-11-05 01:11:58 +0000116 FI.setRunCount(RunCount);
Yuchen Wu14ae8e62013-10-25 02:22:21 +0000117 FI.setProgramCount(ProgramCount);
Devang Patel37140652011-09-28 18:50:00 +0000118}
119
120//===----------------------------------------------------------------------===//
121// GCOVFunction implementation.
122
Yuchen Wuba718332013-12-03 00:15:49 +0000123/// readGCNO - Read a function from the GCNO buffer. Return false if an error
124/// occurs.
Yuchen Wubec4e902013-12-04 04:49:23 +0000125bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
Yuchen Wue28da842013-11-14 00:07:15 +0000126 uint32_t Dummy;
Justin Bogner011c7422015-01-23 22:38:01 +0000127 if (!Buff.readInt(Dummy))
128 return false; // Function header length
129 if (!Buff.readInt(Ident))
130 return false;
131 if (!Buff.readInt(Checksum))
132 return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000133 if (Version != GCOV::V402) {
134 uint32_t CfgChecksum;
Justin Bogner011c7422015-01-23 22:38:01 +0000135 if (!Buff.readInt(CfgChecksum))
136 return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000137 if (Parent.getChecksum() != CfgChecksum) {
138 errs() << "File checksums do not match: " << Parent.getChecksum()
139 << " != " << CfgChecksum << " in (" << Name << ").\n";
140 return false;
141 }
142 }
Justin Bogner011c7422015-01-23 22:38:01 +0000143 if (!Buff.readString(Name))
144 return false;
145 if (!Buff.readString(Filename))
146 return false;
147 if (!Buff.readInt(LineNumber))
148 return false;
Devang Patel37140652011-09-28 18:50:00 +0000149
150 // read blocks.
Yuchen Wue28da842013-11-14 00:07:15 +0000151 if (!Buff.readBlockTag()) {
152 errs() << "Block tag not found.\n";
153 return false;
154 }
155 uint32_t BlockCount;
Justin Bogner011c7422015-01-23 22:38:01 +0000156 if (!Buff.readInt(BlockCount))
157 return false;
Bob Wilson868e6e32013-10-22 20:02:36 +0000158 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
Justin Bogner011c7422015-01-23 22:38:01 +0000159 if (!Buff.readInt(Dummy))
160 return false; // Block flags;
David Blaikie09757492014-04-21 21:40:16 +0000161 Blocks.push_back(make_unique<GCOVBlock>(*this, i));
Devang Patel37140652011-09-28 18:50:00 +0000162 }
163
164 // read edges.
165 while (Buff.readEdgeTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +0000166 uint32_t EdgeCount;
Justin Bogner011c7422015-01-23 22:38:01 +0000167 if (!Buff.readInt(EdgeCount))
168 return false;
Yuchen Wue28da842013-11-14 00:07:15 +0000169 EdgeCount = (EdgeCount - 1) / 2;
170 uint32_t BlockNo;
Justin Bogner011c7422015-01-23 22:38:01 +0000171 if (!Buff.readInt(BlockNo))
172 return false;
Yuchen Wue28da842013-11-14 00:07:15 +0000173 if (BlockNo >= BlockCount) {
Yuchen Wu4c9f19d2013-12-05 22:02:33 +0000174 errs() << "Unexpected block number: " << BlockNo << " (in " << Name
175 << ").\n";
Yuchen Wue28da842013-11-14 00:07:15 +0000176 return false;
177 }
Bob Wilson868e6e32013-10-22 20:02:36 +0000178 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
Yuchen Wue28da842013-11-14 00:07:15 +0000179 uint32_t Dst;
Justin Bogner011c7422015-01-23 22:38:01 +0000180 if (!Buff.readInt(Dst))
181 return false;
David Blaikie09757492014-04-21 21:40:16 +0000182 Edges.push_back(make_unique<GCOVEdge>(*Blocks[BlockNo], *Blocks[Dst]));
183 GCOVEdge *Edge = Edges.back().get();
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000184 Blocks[BlockNo]->addDstEdge(Edge);
185 Blocks[Dst]->addSrcEdge(Edge);
Justin Bogner011c7422015-01-23 22:38:01 +0000186 if (!Buff.readInt(Dummy))
187 return false; // Edge flag
Devang Patel37140652011-09-28 18:50:00 +0000188 }
189 }
190
191 // read line table.
192 while (Buff.readLineTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +0000193 uint32_t LineTableLength;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000194 // Read the length of this line table.
Justin Bogner011c7422015-01-23 22:38:01 +0000195 if (!Buff.readInt(LineTableLength))
196 return false;
197 uint32_t EndPos = Buff.getCursor() + LineTableLength * 4;
Yuchen Wue28da842013-11-14 00:07:15 +0000198 uint32_t BlockNo;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000199 // Read the block number this table is associated with.
Justin Bogner011c7422015-01-23 22:38:01 +0000200 if (!Buff.readInt(BlockNo))
201 return false;
Yuchen Wue28da842013-11-14 00:07:15 +0000202 if (BlockNo >= BlockCount) {
Yuchen Wu4c9f19d2013-12-05 22:02:33 +0000203 errs() << "Unexpected block number: " << BlockNo << " (in " << Name
204 << ").\n";
Yuchen Wue28da842013-11-14 00:07:15 +0000205 return false;
Devang Patel37140652011-09-28 18:50:00 +0000206 }
David Blaikie09757492014-04-21 21:40:16 +0000207 GCOVBlock &Block = *Blocks[BlockNo];
Justin Bognerc475e1b2014-05-02 20:01:24 +0000208 // Read the word that pads the beginning of the line table. This may be a
209 // flag of some sort, but seems to always be zero.
Justin Bogner011c7422015-01-23 22:38:01 +0000210 if (!Buff.readInt(Dummy))
211 return false;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000212
213 // Line information starts here and continues up until the last word.
214 if (Buff.getCursor() != (EndPos - sizeof(uint32_t))) {
Yuchen Wud738bee2013-11-14 00:32:00 +0000215 StringRef F;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000216 // Read the source file name.
Justin Bogner011c7422015-01-23 22:38:01 +0000217 if (!Buff.readString(F))
218 return false;
Yuchen Wu4c9f19d2013-12-05 22:02:33 +0000219 if (Filename != F) {
220 errs() << "Multiple sources for a single basic block: " << Filename
221 << " != " << F << " (in " << Name << ").\n";
Yuchen Wud738bee2013-11-14 00:32:00 +0000222 return false;
223 }
Justin Bognerc475e1b2014-05-02 20:01:24 +0000224 // Read lines up to, but not including, the null terminator.
225 while (Buff.getCursor() < (EndPos - 2 * sizeof(uint32_t))) {
Yuchen Wue28da842013-11-14 00:07:15 +0000226 uint32_t Line;
Justin Bogner011c7422015-01-23 22:38:01 +0000227 if (!Buff.readInt(Line))
228 return false;
Justin Bognerc475e1b2014-05-02 20:01:24 +0000229 // Line 0 means this instruction was injected by the compiler. Skip it.
Justin Bogner011c7422015-01-23 22:38:01 +0000230 if (!Line)
231 continue;
David Blaikie09757492014-04-21 21:40:16 +0000232 Block.addLine(Line);
Yuchen Wue28da842013-11-14 00:07:15 +0000233 }
Justin Bognerc475e1b2014-05-02 20:01:24 +0000234 // Read the null terminator.
Justin Bogner011c7422015-01-23 22:38:01 +0000235 if (!Buff.readInt(Dummy))
236 return false;
Yuchen Wue28da842013-11-14 00:07:15 +0000237 }
Justin Bognerc475e1b2014-05-02 20:01:24 +0000238 // The last word is either a flag or padding, it isn't clear which. Skip
239 // over it.
Justin Bogner011c7422015-01-23 22:38:01 +0000240 if (!Buff.readInt(Dummy))
241 return false;
Devang Patel37140652011-09-28 18:50:00 +0000242 }
243 return true;
244}
245
Yuchen Wuba718332013-12-03 00:15:49 +0000246/// readGCDA - Read a function from the GCDA buffer. Return false if an error
247/// occurs.
Yuchen Wubec4e902013-12-04 04:49:23 +0000248bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
Yuchen Wuba718332013-12-03 00:15:49 +0000249 uint32_t Dummy;
Justin Bogner011c7422015-01-23 22:38:01 +0000250 if (!Buff.readInt(Dummy))
251 return false; // Function header length
Daniel Jasper87a24d52013-12-04 08:57:17 +0000252
Yuchen Wu57529972013-12-04 05:42:28 +0000253 uint32_t GCDAIdent;
Justin Bogner011c7422015-01-23 22:38:01 +0000254 if (!Buff.readInt(GCDAIdent))
255 return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000256 if (Ident != GCDAIdent) {
Justin Bogner011c7422015-01-23 22:38:01 +0000257 errs() << "Function identifiers do not match: " << Ident
258 << " != " << GCDAIdent << " (in " << Name << ").\n";
Yuchen Wu57529972013-12-04 05:42:28 +0000259 return false;
260 }
Yuchen Wuba718332013-12-03 00:15:49 +0000261
Daniel Jasper87a24d52013-12-04 08:57:17 +0000262 uint32_t GCDAChecksum;
Justin Bogner011c7422015-01-23 22:38:01 +0000263 if (!Buff.readInt(GCDAChecksum))
264 return false;
Daniel Jasper87a24d52013-12-04 08:57:17 +0000265 if (Checksum != GCDAChecksum) {
Justin Bogner011c7422015-01-23 22:38:01 +0000266 errs() << "Function checksums do not match: " << Checksum
267 << " != " << GCDAChecksum << " (in " << Name << ").\n";
Daniel Jasper87a24d52013-12-04 08:57:17 +0000268 return false;
269 }
Yuchen Wu57529972013-12-04 05:42:28 +0000270
271 uint32_t CfgChecksum;
272 if (Version != GCOV::V402) {
Justin Bogner011c7422015-01-23 22:38:01 +0000273 if (!Buff.readInt(CfgChecksum))
274 return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000275 if (Parent.getChecksum() != CfgChecksum) {
276 errs() << "File checksums do not match: " << Parent.getChecksum()
277 << " != " << CfgChecksum << " (in " << Name << ").\n";
278 return false;
279 }
280 }
281
282 StringRef GCDAName;
Justin Bogner011c7422015-01-23 22:38:01 +0000283 if (!Buff.readString(GCDAName))
284 return false;
Yuchen Wu57529972013-12-04 05:42:28 +0000285 if (Name != GCDAName) {
286 errs() << "Function names do not match: " << Name << " != " << GCDAName
287 << ".\n";
288 return false;
289 }
Yuchen Wuba718332013-12-03 00:15:49 +0000290
291 if (!Buff.readArcTag()) {
Yuchen Wu57529972013-12-04 05:42:28 +0000292 errs() << "Arc tag not found (in " << Name << ").\n";
Yuchen Wuba718332013-12-03 00:15:49 +0000293 return false;
294 }
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000295
Yuchen Wuba718332013-12-03 00:15:49 +0000296 uint32_t Count;
Justin Bogner011c7422015-01-23 22:38:01 +0000297 if (!Buff.readInt(Count))
298 return false;
Yuchen Wuba718332013-12-03 00:15:49 +0000299 Count /= 2;
300
301 // This for loop adds the counts for each block. A second nested loop is
302 // required to combine the edge counts that are contained in the GCDA file.
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000303 for (uint32_t BlockNo = 0; Count > 0; ++BlockNo) {
304 // The last block is always reserved for exit block
Justin Bogner011c7422015-01-23 22:38:01 +0000305 if (BlockNo >= Blocks.size() - 1) {
Yuchen Wu57529972013-12-04 05:42:28 +0000306 errs() << "Unexpected number of edges (in " << Name << ").\n";
Yuchen Wuba718332013-12-03 00:15:49 +0000307 return false;
308 }
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000309 GCOVBlock &Block = *Blocks[BlockNo];
310 for (size_t EdgeNo = 0, End = Block.getNumDstEdges(); EdgeNo < End;
Justin Bogner011c7422015-01-23 22:38:01 +0000311 ++EdgeNo) {
Yuchen Wuba718332013-12-03 00:15:49 +0000312 if (Count == 0) {
Yuchen Wu57529972013-12-04 05:42:28 +0000313 errs() << "Unexpected number of edges (in " << Name << ").\n";
Yuchen Wuba718332013-12-03 00:15:49 +0000314 return false;
315 }
316 uint64_t ArcCount;
Justin Bogner011c7422015-01-23 22:38:01 +0000317 if (!Buff.readInt64(ArcCount))
318 return false;
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000319 Block.addCount(EdgeNo, ArcCount);
Yuchen Wuba718332013-12-03 00:15:49 +0000320 --Count;
321 }
Yuchen Wu342714c2013-12-13 01:15:07 +0000322 Block.sortDstEdges();
Yuchen Wuba718332013-12-03 00:15:49 +0000323 }
324 return true;
325}
326
Yuchen Wu342714c2013-12-13 01:15:07 +0000327/// getEntryCount - Get the number of times the function was called by
328/// retrieving the entry block's count.
329uint64_t GCOVFunction::getEntryCount() const {
330 return Blocks.front()->getCount();
331}
332
333/// getExitCount - Get the number of times the function returned by retrieving
334/// the exit block's count.
335uint64_t GCOVFunction::getExitCount() const {
336 return Blocks.back()->getCount();
337}
338
Yuchen Wu03678152013-10-25 02:22:24 +0000339/// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
Yuchen Wuef6909d2013-11-19 20:33:32 +0000340void GCOVFunction::dump() const {
Justin Bogner58e41342014-11-06 06:55:02 +0000341 dbgs() << "===== " << Name << " (" << Ident << ") @ " << Filename << ":"
342 << LineNumber << "\n";
David Blaikie09757492014-04-21 21:40:16 +0000343 for (const auto &Block : Blocks)
344 Block->dump();
Devang Patel37140652011-09-28 18:50:00 +0000345}
346
347/// collectLineCounts - Collect line counts. This must be used after
348/// reading .gcno and .gcda files.
349void GCOVFunction::collectLineCounts(FileInfo &FI) {
Justin Bogner95e0a702014-03-26 22:03:06 +0000350 // If the line number is zero, this is a function that doesn't actually appear
351 // in the source file, so there isn't anything we can do with it.
352 if (LineNumber == 0)
353 return;
354
David Blaikie09757492014-04-21 21:40:16 +0000355 for (const auto &Block : Blocks)
356 Block->collectLineCounts(FI);
Yuchen Wu342714c2013-12-13 01:15:07 +0000357 FI.addFunctionLine(Filename, LineNumber, this);
Devang Patel37140652011-09-28 18:50:00 +0000358}
359
360//===----------------------------------------------------------------------===//
361// GCOVBlock implementation.
362
363/// ~GCOVBlock - Delete GCOVBlock and its content.
364GCOVBlock::~GCOVBlock() {
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000365 SrcEdges.clear();
366 DstEdges.clear();
Yuchen Wud738bee2013-11-14 00:32:00 +0000367 Lines.clear();
Devang Patel37140652011-09-28 18:50:00 +0000368}
369
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000370/// addCount - Add to block counter while storing the edge count. If the
371/// destination has no outgoing edges, also update that block's count too.
372void GCOVBlock::addCount(size_t DstEdgeNo, uint64_t N) {
373 assert(DstEdgeNo < DstEdges.size()); // up to caller to ensure EdgeNo is valid
374 DstEdges[DstEdgeNo]->Count = N;
375 Counter += N;
David Blaikie09757492014-04-21 21:40:16 +0000376 if (!DstEdges[DstEdgeNo]->Dst.getNumDstEdges())
377 DstEdges[DstEdgeNo]->Dst.Counter += N;
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000378}
379
Yuchen Wu342714c2013-12-13 01:15:07 +0000380/// sortDstEdges - Sort destination edges by block number, nop if already
381/// sorted. This is required for printing branch info in the correct order.
382void GCOVBlock::sortDstEdges() {
383 if (!DstEdgesAreSorted) {
384 SortDstEdgesFunctor SortEdges;
385 std::stable_sort(DstEdges.begin(), DstEdges.end(), SortEdges);
386 }
387}
388
Devang Patel37140652011-09-28 18:50:00 +0000389/// collectLineCounts - Collect line counts. This must be used after
390/// reading .gcno and .gcda files.
391void GCOVBlock::collectLineCounts(FileInfo &FI) {
Justin Bogner011c7422015-01-23 22:38:01 +0000392 for (auto I = Lines.begin(), E = Lines.end(); I != E; ++I)
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000393 FI.addBlockLine(Parent.getFilename(), *I, this);
Devang Patel37140652011-09-28 18:50:00 +0000394}
395
Yuchen Wu03678152013-10-25 02:22:24 +0000396/// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
Yuchen Wuef6909d2013-11-19 20:33:32 +0000397void GCOVBlock::dump() const {
Yuchen Wu03678152013-10-25 02:22:24 +0000398 dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000399 if (!SrcEdges.empty()) {
400 dbgs() << "\tSource Edges : ";
401 for (EdgeIterator I = SrcEdges.begin(), E = SrcEdges.end(); I != E; ++I) {
402 const GCOVEdge *Edge = *I;
David Blaikie09757492014-04-21 21:40:16 +0000403 dbgs() << Edge->Src.Number << " (" << Edge->Count << "), ";
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000404 }
405 dbgs() << "\n";
406 }
407 if (!DstEdges.empty()) {
408 dbgs() << "\tDestination Edges : ";
409 for (EdgeIterator I = DstEdges.begin(), E = DstEdges.end(); I != E; ++I) {
410 const GCOVEdge *Edge = *I;
David Blaikie09757492014-04-21 21:40:16 +0000411 dbgs() << Edge->Dst.Number << " (" << Edge->Count << "), ";
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000412 }
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 : ";
Yuchen Wuef6909d2013-11-19 20:33:32 +0000417 for (SmallVectorImpl<uint32_t>::const_iterator I = Lines.begin(),
Justin Bogner011c7422015-01-23 22:38:01 +0000418 E = Lines.end();
419 I != E; ++I)
Yuchen Wud738bee2013-11-14 00:32:00 +0000420 dbgs() << (*I) << ",";
421 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000422 }
423}
424
425//===----------------------------------------------------------------------===//
Devang Patel37140652011-09-28 18:50:00 +0000426// FileInfo implementation.
427
Yuchen Wu342714c2013-12-13 01:15:07 +0000428// Safe integer division, returns 0 if numerator is 0.
429static uint32_t safeDiv(uint64_t Numerator, uint64_t Divisor) {
430 if (!Numerator)
431 return 0;
Justin Bogner011c7422015-01-23 22:38:01 +0000432 return Numerator / Divisor;
Yuchen Wu342714c2013-12-13 01:15:07 +0000433}
434
435// This custom division function mimics gcov's branch ouputs:
436// - Round to closest whole number
437// - Only output 0% or 100% if it's exactly that value
438static uint32_t branchDiv(uint64_t Numerator, uint64_t Divisor) {
439 if (!Numerator)
440 return 0;
441 if (Numerator == Divisor)
442 return 100;
443
Justin Bogner011c7422015-01-23 22:38:01 +0000444 uint8_t Res = (Numerator * 100 + Divisor / 2) / Divisor;
Yuchen Wu342714c2013-12-13 01:15:07 +0000445 if (Res == 0)
446 return 1;
447 if (Res == 100)
448 return 99;
449 return Res;
450}
451
Yuchen Wu73dc3812013-12-18 18:40:15 +0000452struct formatBranchInfo {
Justin Bogner011c7422015-01-23 22:38:01 +0000453 formatBranchInfo(const GCOVOptions &Options, uint64_t Count, uint64_t Total)
454 : Options(Options), Count(Count), Total(Total) {}
Yuchen Wu73dc3812013-12-18 18:40:15 +0000455
456 void print(raw_ostream &OS) const {
457 if (!Total)
458 OS << "never executed";
459 else if (Options.BranchCount)
460 OS << "taken " << Count;
461 else
462 OS << "taken " << branchDiv(Count, Total) << "%";
463 }
464
465 const GCOVOptions &Options;
466 uint64_t Count;
467 uint64_t Total;
468};
469
470static raw_ostream &operator<<(raw_ostream &OS, const formatBranchInfo &FBI) {
471 FBI.print(OS);
472 return OS;
473}
474
Justin Bognercf27e1b2014-05-07 02:11:23 +0000475namespace {
476class LineConsumer {
477 std::unique_ptr<MemoryBuffer> Buffer;
478 StringRef Remaining;
Justin Bogner011c7422015-01-23 22:38:01 +0000479
Justin Bognercf27e1b2014-05-07 02:11:23 +0000480public:
481 LineConsumer(StringRef Filename) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000482 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
483 MemoryBuffer::getFileOrSTDIN(Filename);
484 if (std::error_code EC = BufferOrErr.getError()) {
Justin Bognercf27e1b2014-05-07 02:11:23 +0000485 errs() << Filename << ": " << EC.message() << "\n";
486 Remaining = "";
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000487 } else {
488 Buffer = std::move(BufferOrErr.get());
Justin Bognercf27e1b2014-05-07 02:11:23 +0000489 Remaining = Buffer->getBuffer();
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000490 }
Justin Bognercf27e1b2014-05-07 02:11:23 +0000491 }
492 bool empty() { return Remaining.empty(); }
493 void printNext(raw_ostream &OS, uint32_t LineNum) {
494 StringRef Line;
495 if (empty())
496 Line = "/*EOF*/";
497 else
498 std::tie(Line, Remaining) = Remaining.split("\n");
499 OS << format("%5u:", LineNum) << Line << "\n";
500 }
501};
502}
503
Justin Bognerc6af3502014-02-04 10:45:02 +0000504/// Convert a path to a gcov filename. If PreservePaths is true, this
505/// translates "/" to "#", ".." to "^", and drops ".", to match gcov.
506static std::string mangleCoveragePath(StringRef Filename, bool PreservePaths) {
507 if (!PreservePaths)
Justin Bognerc67f0252014-04-23 21:44:55 +0000508 return sys::path::filename(Filename).str();
Justin Bognerc6af3502014-02-04 10:45:02 +0000509
510 // This behaviour is defined by gcov in terms of text replacements, so it's
511 // not likely to do anything useful on filesystems with different textual
512 // conventions.
513 llvm::SmallString<256> Result("");
514 StringRef::iterator I, S, E;
515 for (I = S = Filename.begin(), E = Filename.end(); I != E; ++I) {
516 if (*I != '/')
517 continue;
518
519 if (I - S == 1 && *S == '.') {
520 // ".", the current directory, is skipped.
521 } else if (I - S == 2 && *S == '.' && *(S + 1) == '.') {
522 // "..", the parent directory, is replaced with "^".
523 Result.append("^#");
524 } else {
525 if (S < I)
526 // Leave other components intact,
527 Result.append(S, I);
528 // And separate with "#".
529 Result.push_back('#');
530 }
531 S = I + 1;
532 }
533
534 if (S < I)
535 Result.append(S, I);
Justin Bognerc6af3502014-02-04 10:45:02 +0000536 return Result.str();
537}
538
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000539std::string FileInfo::getCoveragePath(StringRef Filename,
540 StringRef MainFilename) {
541 if (Options.NoOutput)
542 // This is probably a bug in gcov, but when -n is specified, paths aren't
543 // mangled at all, and the -l and -p options are ignored. Here, we do the
544 // same.
545 return Filename;
546
547 std::string CoveragePath;
548 if (Options.LongFileNames && !Filename.equals(MainFilename))
549 CoveragePath =
550 mangleCoveragePath(MainFilename, Options.PreservePaths) + "##";
Justin Bogner011c7422015-01-23 22:38:01 +0000551 CoveragePath += mangleCoveragePath(Filename, Options.PreservePaths) + ".gcov";
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000552 return CoveragePath;
553}
554
555std::unique_ptr<raw_ostream>
556FileInfo::openCoveragePath(StringRef CoveragePath) {
557 if (Options.NoOutput)
Justin Bogner7c093732014-05-07 16:01:27 +0000558 return llvm::make_unique<raw_null_ostream>();
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000559
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000560 std::error_code EC;
561 auto OS = llvm::make_unique<raw_fd_ostream>(CoveragePath.str(), EC,
562 sys::fs::F_Text);
563 if (EC) {
564 errs() << EC.message() << "\n";
Justin Bogner7c093732014-05-07 16:01:27 +0000565 return llvm::make_unique<raw_null_ostream>();
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000566 }
567 return std::move(OS);
568}
569
Devang Patel37140652011-09-28 18:50:00 +0000570/// print - Print source files with collected line count information.
Justin Bognerc67f0252014-04-23 21:44:55 +0000571void FileInfo::print(StringRef MainFilename, StringRef GCNOFile,
572 StringRef GCDAFile) {
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000573 for (StringMap<LineData>::const_iterator I = LineInfo.begin(),
Justin Bogner011c7422015-01-23 22:38:01 +0000574 E = LineInfo.end();
575 I != E; ++I) {
Devang Patel37140652011-09-28 18:50:00 +0000576 StringRef Filename = I->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);
580 std::unique_ptr<raw_ostream> S = openCoveragePath(CoveragePath);
581 raw_ostream &OS = *S;
Yuchen Wu26326ad2013-12-03 00:57:11 +0000582
Yuchen Wu8aac4f62013-11-19 20:57:20 +0000583 OS << " -: 0:Source:" << Filename << "\n";
Yuchen Wu21517e42013-12-04 05:07:36 +0000584 OS << " -: 0:Graph:" << GCNOFile << "\n";
585 OS << " -: 0:Data:" << GCDAFile << "\n";
Yuchen Wu8aac4f62013-11-19 20:57:20 +0000586 OS << " -: 0:Runs:" << RunCount << "\n";
587 OS << " -: 0:Programs:" << ProgramCount << "\n";
588
Yuchen Wu1c068162013-12-03 01:35:31 +0000589 const LineData &Line = I->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())
596 printFunctionSummary(OS, FuncsIt->second);
597 }
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.
602 OS << " -:";
Justin Bognercf27e1b2014-05-07 02:11:23 +0000603 AllLines.printNext(OS, 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;
610 for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end();
Justin Bogner011c7422015-01-23 22:38:01 +0000611 I != E; ++I) {
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000612 const GCOVBlock *Block = *I;
613 if (Options.AllBlocks) {
614 // Only take the highest block count for that line.
615 uint64_t BlockCount = Block->getCount();
616 LineCount = LineCount > BlockCount ? LineCount : BlockCount;
617 } else {
618 // Sum up all of the block counts.
619 LineCount += Block->getCount();
620 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000621
622 if (Options.FuncCoverage) {
623 // This is a slightly convoluted way to most accurately gather line
624 // statistics for functions. Basically what is happening is that we
625 // don't want to count a single line with multiple blocks more than
626 // once. However, we also don't simply want to give the total line
627 // count to every function that starts on the line. Thus, what is
628 // happening here are two things:
629 // 1) Ensure that the number of logical lines is only incremented
630 // once per function.
631 // 2) If there are multiple blocks on the same line, ensure that the
632 // number of lines executed is incremented as long as at least
633 // one of the blocks are executed.
634 const GCOVFunction *Function = &Block->getParent();
635 if (FuncCoverages.find(Function) == FuncCoverages.end()) {
Justin Bogner011c7422015-01-23 22:38:01 +0000636 std::pair<const GCOVFunction *, GCOVCoverage> KeyValue(
637 Function, GCOVCoverage(Function->getName()));
Yuchen Wubb6a4772013-12-19 00:29:25 +0000638 FuncCoverages.insert(KeyValue);
639 }
640 GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second;
641
642 if (LineExecs.find(Function) == LineExecs.end()) {
643 if (Block->getCount()) {
644 ++FuncCoverage.LinesExec;
645 LineExecs[Function] = true;
646 } else {
647 LineExecs[Function] = false;
648 }
649 ++FuncCoverage.LogicalLines;
650 } else if (!LineExecs[Function] && Block->getCount()) {
651 ++FuncCoverage.LinesExec;
652 LineExecs[Function] = true;
653 }
654 }
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000655 }
Yuchen Wu8256ee62013-12-18 21:12:51 +0000656
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000657 if (LineCount == 0)
Yuchen Wudbcf1972013-11-02 00:09:17 +0000658 OS << " #####:";
Yuchen Wu8256ee62013-12-18 21:12:51 +0000659 else {
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000660 OS << format("%9" PRIu64 ":", LineCount);
Yuchen Wubb6a4772013-12-19 00:29:25 +0000661 ++FileCoverage.LinesExec;
Yuchen Wu8256ee62013-12-18 21:12:51 +0000662 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000663 ++FileCoverage.LogicalLines;
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000664
Justin Bognercf27e1b2014-05-07 02:11:23 +0000665 AllLines.printNext(OS, LineIndex + 1);
Yuchen Wu342714c2013-12-13 01:15:07 +0000666
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000667 uint32_t BlockNo = 0;
Yuchen Wu342714c2013-12-13 01:15:07 +0000668 uint32_t EdgeNo = 0;
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000669 for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end();
Justin Bogner011c7422015-01-23 22:38:01 +0000670 I != E; ++I) {
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000671 const GCOVBlock *Block = *I;
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000672
Yuchen Wu342714c2013-12-13 01:15:07 +0000673 // Only print block and branch information at the end of the block.
Justin Bogner011c7422015-01-23 22:38:01 +0000674 if (Block->getLastLine() != LineIndex + 1)
Yuchen Wu342714c2013-12-13 01:15:07 +0000675 continue;
676 if (Options.AllBlocks)
677 printBlockInfo(OS, *Block, LineIndex, BlockNo);
Yuchen Wu73dc3812013-12-18 18:40:15 +0000678 if (Options.BranchInfo) {
Yuchen Wu66d93b82013-12-16 22:14:02 +0000679 size_t NumEdges = Block->getNumDstEdges();
680 if (NumEdges > 1)
Yuchen Wubb6a4772013-12-19 00:29:25 +0000681 printBranchInfo(OS, *Block, FileCoverage, EdgeNo);
Yuchen Wu66d93b82013-12-16 22:14:02 +0000682 else if (Options.UncondBranch && NumEdges == 1)
683 printUncondBranchInfo(OS, EdgeNo, (*Block->dst_begin())->Count);
684 }
Yuchen Wu8c6bb5f2013-12-10 01:02:07 +0000685 }
686 }
Devang Patel37140652011-09-28 18:50:00 +0000687 }
Justin Bognerc6af3502014-02-04 10:45:02 +0000688 FileCoverages.push_back(std::make_pair(CoveragePath, FileCoverage));
Devang Patel37140652011-09-28 18:50:00 +0000689 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000690
691 // FIXME: There is no way to detect calls given current instrumentation.
692 if (Options.FuncCoverage)
693 printFuncCoverage();
694 printFileCoverage();
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000695 return;
Devang Patel37140652011-09-28 18:50:00 +0000696}
Yuchen Wu342714c2013-12-13 01:15:07 +0000697
698/// printFunctionSummary - Print function and block summary.
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000699void FileInfo::printFunctionSummary(raw_ostream &OS,
Yuchen Wu342714c2013-12-13 01:15:07 +0000700 const FunctionVector &Funcs) const {
701 for (FunctionVector::const_iterator I = Funcs.begin(), E = Funcs.end();
Justin Bogner011c7422015-01-23 22:38:01 +0000702 I != E; ++I) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000703 const GCOVFunction *Func = *I;
704 uint64_t EntryCount = Func->getEntryCount();
Yuchen Wuc9b2dcd2013-12-18 18:46:25 +0000705 uint32_t BlocksExec = 0;
Yuchen Wu342714c2013-12-13 01:15:07 +0000706 for (GCOVFunction::BlockIterator I = Func->block_begin(),
Justin Bogner011c7422015-01-23 22:38:01 +0000707 E = Func->block_end();
708 I != E; ++I) {
David Blaikie09757492014-04-21 21:40:16 +0000709 const GCOVBlock &Block = **I;
710 if (Block.getNumDstEdges() && Block.getCount())
Justin Bogner011c7422015-01-23 22:38:01 +0000711 ++BlocksExec;
Yuchen Wu342714c2013-12-13 01:15:07 +0000712 }
713
714 OS << "function " << Func->getName() << " called " << EntryCount
Justin Bogner011c7422015-01-23 22:38:01 +0000715 << " returned " << safeDiv(Func->getExitCount() * 100, EntryCount)
Yuchen Wu342714c2013-12-13 01:15:07 +0000716 << "% blocks executed "
Justin Bogner011c7422015-01-23 22:38:01 +0000717 << safeDiv(BlocksExec * 100, Func->getNumBlocks() - 1) << "%\n";
Yuchen Wu342714c2013-12-13 01:15:07 +0000718 }
719}
720
721/// printBlockInfo - Output counts for each block.
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000722void FileInfo::printBlockInfo(raw_ostream &OS, const GCOVBlock &Block,
Yuchen Wu342714c2013-12-13 01:15:07 +0000723 uint32_t LineIndex, uint32_t &BlockNo) const {
724 if (Block.getCount() == 0)
725 OS << " $$$$$:";
726 else
727 OS << format("%9" PRIu64 ":", Block.getCount());
Justin Bogner011c7422015-01-23 22:38:01 +0000728 OS << format("%5u-block %2u\n", LineIndex + 1, BlockNo++);
Yuchen Wu342714c2013-12-13 01:15:07 +0000729}
730
Yuchen Wu66d93b82013-12-16 22:14:02 +0000731/// printBranchInfo - Print conditional branch probabilities.
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000732void FileInfo::printBranchInfo(raw_ostream &OS, const GCOVBlock &Block,
Yuchen Wubb6a4772013-12-19 00:29:25 +0000733 GCOVCoverage &Coverage, uint32_t &EdgeNo) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000734 SmallVector<uint64_t, 16> BranchCounts;
735 uint64_t TotalCounts = 0;
736 for (GCOVBlock::EdgeIterator I = Block.dst_begin(), E = Block.dst_end();
Justin Bogner011c7422015-01-23 22:38:01 +0000737 I != E; ++I) {
Yuchen Wu342714c2013-12-13 01:15:07 +0000738 const GCOVEdge *Edge = *I;
739 BranchCounts.push_back(Edge->Count);
740 TotalCounts += Edge->Count;
Justin Bogner011c7422015-01-23 22:38:01 +0000741 if (Block.getCount())
742 ++Coverage.BranchesExec;
743 if (Edge->Count)
744 ++Coverage.BranchesTaken;
Yuchen Wu8256ee62013-12-18 21:12:51 +0000745 ++Coverage.Branches;
Yuchen Wubb6a4772013-12-19 00:29:25 +0000746
747 if (Options.FuncCoverage) {
748 const GCOVFunction *Function = &Block.getParent();
749 GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second;
Justin Bogner011c7422015-01-23 22:38:01 +0000750 if (Block.getCount())
751 ++FuncCoverage.BranchesExec;
752 if (Edge->Count)
753 ++FuncCoverage.BranchesTaken;
Yuchen Wubb6a4772013-12-19 00:29:25 +0000754 ++FuncCoverage.Branches;
755 }
Yuchen Wu342714c2013-12-13 01:15:07 +0000756 }
757
758 for (SmallVectorImpl<uint64_t>::const_iterator I = BranchCounts.begin(),
Justin Bogner011c7422015-01-23 22:38:01 +0000759 E = BranchCounts.end();
760 I != E; ++I) {
Yuchen Wu73dc3812013-12-18 18:40:15 +0000761 OS << format("branch %2u ", EdgeNo++)
762 << formatBranchInfo(Options, *I, TotalCounts) << "\n";
Yuchen Wu342714c2013-12-13 01:15:07 +0000763 }
764}
Yuchen Wu66d93b82013-12-16 22:14:02 +0000765
766/// printUncondBranchInfo - Print unconditional branch probabilities.
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000767void FileInfo::printUncondBranchInfo(raw_ostream &OS, uint32_t &EdgeNo,
Yuchen Wu66d93b82013-12-16 22:14:02 +0000768 uint64_t Count) const {
Yuchen Wu73dc3812013-12-18 18:40:15 +0000769 OS << format("unconditional %2u ", EdgeNo++)
770 << formatBranchInfo(Options, Count, Count) << "\n";
Yuchen Wu66d93b82013-12-16 22:14:02 +0000771}
Yuchen Wu8256ee62013-12-18 21:12:51 +0000772
Yuchen Wubb6a4772013-12-19 00:29:25 +0000773// printCoverage - Print generic coverage info used by both printFuncCoverage
774// and printFileCoverage.
775void FileInfo::printCoverage(const GCOVCoverage &Coverage) const {
NAKAMURA Takumi6e3c4232013-12-19 08:46:28 +0000776 outs() << format("Lines executed:%.2f%% of %u\n",
Justin Bogner011c7422015-01-23 22:38:01 +0000777 double(Coverage.LinesExec) * 100 / Coverage.LogicalLines,
Yuchen Wu8256ee62013-12-18 21:12:51 +0000778 Coverage.LogicalLines);
779 if (Options.BranchInfo) {
780 if (Coverage.Branches) {
NAKAMURA Takumi6e3c4232013-12-19 08:46:28 +0000781 outs() << format("Branches executed:%.2f%% of %u\n",
Justin Bogner011c7422015-01-23 22:38:01 +0000782 double(Coverage.BranchesExec) * 100 / Coverage.Branches,
Yuchen Wu8256ee62013-12-18 21:12:51 +0000783 Coverage.Branches);
NAKAMURA Takumi6e3c4232013-12-19 08:46:28 +0000784 outs() << format("Taken at least once:%.2f%% of %u\n",
Justin Bogner011c7422015-01-23 22:38:01 +0000785 double(Coverage.BranchesTaken) * 100 / Coverage.Branches,
Yuchen Wu8256ee62013-12-18 21:12:51 +0000786 Coverage.Branches);
787 } else {
788 outs() << "No branches\n";
789 }
790 outs() << "No calls\n"; // to be consistent with gcov
791 }
Yuchen Wubb6a4772013-12-19 00:29:25 +0000792}
793
794// printFuncCoverage - Print per-function coverage info.
795void FileInfo::printFuncCoverage() const {
Justin Bognerc6af3502014-02-04 10:45:02 +0000796 for (FuncCoverageMap::const_iterator I = FuncCoverages.begin(),
Justin Bogner011c7422015-01-23 22:38:01 +0000797 E = FuncCoverages.end();
798 I != E; ++I) {
Yuchen Wubb6a4772013-12-19 00:29:25 +0000799 const GCOVCoverage &Coverage = I->second;
800 outs() << "Function '" << Coverage.Name << "'\n";
801 printCoverage(Coverage);
802 outs() << "\n";
803 }
804}
805
806// printFileCoverage - Print per-file coverage info.
807void FileInfo::printFileCoverage() const {
Justin Bognerc6af3502014-02-04 10:45:02 +0000808 for (FileCoverageList::const_iterator I = FileCoverages.begin(),
Justin Bogner011c7422015-01-23 22:38:01 +0000809 E = FileCoverages.end();
810 I != E; ++I) {
Justin Bognerc6af3502014-02-04 10:45:02 +0000811 const std::string &Filename = I->first;
812 const GCOVCoverage &Coverage = I->second;
Yuchen Wubb6a4772013-12-19 00:29:25 +0000813 outs() << "File '" << Coverage.Name << "'\n";
814 printCoverage(Coverage);
Justin Bogner1a18d7c2014-05-07 02:11:18 +0000815 if (!Options.NoOutput)
816 outs() << Coverage.Name << ":creating '" << Filename << "'\n";
817 outs() << "\n";
Yuchen Wubb6a4772013-12-19 00:29:25 +0000818 }
Yuchen Wu8256ee62013-12-18 21:12:51 +0000819}