blob: b687346a2c054a964aec6cebda24f988841fa6a2 [file] [log] [blame]
Yuchen Wu298c7652013-12-05 22:02:29 +00001//===- GCOV.cpp - LLVM coverage tool --------------------------------------===//
Devang Pateld02c42b2011-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 Takumi12e90cb2013-11-14 11:44:58 +000010// GCOV implements the interface to read and write coverage files that use
Devang Patel58c62002011-10-04 17:24:48 +000011// 'gcov' format.
Devang Pateld02c42b2011-09-28 18:50:00 +000012//
Devang Pateld02c42b2011-09-28 18:50:00 +000013//===----------------------------------------------------------------------===//
14
David Blaikie7711c312017-11-03 20:57:10 +000015#include "llvm/ProfileData/GCOV.h"
Devang Patel7a502022011-09-29 16:46:47 +000016#include "llvm/ADT/STLExtras.h"
Nico Weber0f38c602018-04-30 14:59:11 +000017#include "llvm/Config/llvm-config.h"
Justin Bognerd1cf8042014-02-04 21:03:17 +000018#include "llvm/Support/Debug.h"
Justin Bogner01c05502014-02-04 10:45:02 +000019#include "llvm/Support/FileSystem.h"
Justin Bognerd1cf8042014-02-04 21:03:17 +000020#include "llvm/Support/Format.h"
Justin Bogner01c05502014-02-04 10:45:02 +000021#include "llvm/Support/Path.h"
Benjamin Kramer1bfcd1f2015-03-23 19:32:43 +000022#include "llvm/Support/raw_ostream.h"
Yuchen Wua2639792013-12-13 01:15:07 +000023#include <algorithm>
Rafael Espindolad5132f92014-06-12 17:38:55 +000024#include <system_error>
Eugene Zelenko380d47d2016-02-02 18:20:45 +000025
Devang Pateld02c42b2011-09-28 18:50:00 +000026using namespace llvm;
27
28//===----------------------------------------------------------------------===//
29// GCOVFile implementation.
30
Yuchen Wu812ad832013-12-04 04:49:23 +000031/// readGCNO - Read GCNO buffer.
32bool GCOVFile::readGCNO(GCOVBuffer &Buffer) {
Justin Bogner4c61bbe2015-01-23 22:38:01 +000033 if (!Buffer.readGCNOFormat())
34 return false;
35 if (!Buffer.readGCOVVersion(Version))
36 return false;
Yuchen Wu812ad832013-12-04 04:49:23 +000037
Justin Bogner4c61bbe2015-01-23 22:38:01 +000038 if (!Buffer.readInt(Checksum))
39 return false;
Yuchen Wu812ad832013-12-04 04:49:23 +000040 while (true) {
Justin Bogner4c61bbe2015-01-23 22:38:01 +000041 if (!Buffer.readFunctionTag())
42 break;
David Blaikiee1c08632014-04-21 21:40:16 +000043 auto GFun = make_unique<GCOVFunction>(*this);
Yuchen Wu812ad832013-12-04 04:49:23 +000044 if (!GFun->readGCNO(Buffer, Version))
45 return false;
David Blaikiee1c08632014-04-21 21:40:16 +000046 Functions.push_back(std::move(GFun));
Yuchen Wu812ad832013-12-04 04:49:23 +000047 }
48
Yuchen Wu9bd29912013-12-04 05:07:36 +000049 GCNOInitialized = true;
Yuchen Wu812ad832013-12-04 04:49:23 +000050 return true;
Devang Patel0066f922011-09-29 17:06:40 +000051}
52
Yuchen Wu812ad832013-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 Wu9bd29912013-12-04 05:07:36 +000056 assert(GCNOInitialized && "readGCDA() can only be called after readGCNO()");
Justin Bogner4c61bbe2015-01-23 22:38:01 +000057 if (!Buffer.readGCDAFormat())
58 return false;
Yuchen Wu812ad832013-12-04 04:49:23 +000059 GCOV::GCOVVersion GCDAVersion;
Justin Bogner4c61bbe2015-01-23 22:38:01 +000060 if (!Buffer.readGCOVVersion(GCDAVersion))
61 return false;
Yuchen Wu812ad832013-12-04 04:49:23 +000062 if (Version != GCDAVersion) {
63 errs() << "GCOV versions do not match.\n";
Devang Pateld02c42b2011-09-28 18:50:00 +000064 return false;
Yuchen Wu812ad832013-12-04 04:49:23 +000065 }
Devang Pateld02c42b2011-09-28 18:50:00 +000066
Yuchen Wu812ad832013-12-04 04:49:23 +000067 uint32_t GCDAChecksum;
Justin Bogner4c61bbe2015-01-23 22:38:01 +000068 if (!Buffer.readInt(GCDAChecksum))
69 return false;
Yuchen Wu812ad832013-12-04 04:49:23 +000070 if (Checksum != GCDAChecksum) {
Justin Bogner4c61bbe2015-01-23 22:38:01 +000071 errs() << "File checksums do not match: " << Checksum
72 << " != " << GCDAChecksum << ".\n";
Yuchen Wu812ad832013-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 Wud23c7592013-11-20 04:15:05 +000078 return false;
79 }
Yuchen Wu812ad832013-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 Bogner4c61bbe2015-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 Wu812ad832013-12-04 04:49:23 +000095 }
96 while (Buffer.readProgramTag()) {
97 uint32_t Length;
Justin Bogner4c61bbe2015-01-23 22:38:01 +000098 if (!Buffer.readInt(Length))
99 return false;
Yuchen Wu812ad832013-12-04 04:49:23 +0000100 Buffer.advanceCursor(Length);
101 ++ProgramCount;
Yuchen Wu76fa4d62013-10-25 02:22:21 +0000102 }
103
Devang Pateld02c42b2011-09-28 18:50:00 +0000104 return true;
105}
106
Matthias Braun88d20752017-01-28 02:02:38 +0000107void GCOVFile::print(raw_ostream &OS) const {
108 for (const auto &FPtr : Functions)
109 FPtr->print(OS);
110}
111
Aaron Ballman1d03d382017-10-15 14:32:27 +0000112#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yuchen Wu81e38282013-10-25 02:22:24 +0000113/// dump - Dump GCOVFile content to dbgs() for debugging purposes.
Calixte Denizet24b5a532018-09-20 16:09:30 +0000114LLVM_DUMP_METHOD void GCOVFile::dump() const { print(dbgs()); }
Matthias Braun88d20752017-01-28 02:02:38 +0000115#endif
Devang Pateld02c42b2011-09-28 18:50:00 +0000116
117/// collectLineCounts - Collect line counts. This must be used after
118/// reading .gcno and .gcda files.
119void GCOVFile::collectLineCounts(FileInfo &FI) {
David Blaikiee1c08632014-04-21 21:40:16 +0000120 for (const auto &FPtr : Functions)
121 FPtr->collectLineCounts(FI);
Yuchen Wue85959c2013-11-05 01:11:58 +0000122 FI.setRunCount(RunCount);
Yuchen Wu76fa4d62013-10-25 02:22:21 +0000123 FI.setProgramCount(ProgramCount);
Devang Pateld02c42b2011-09-28 18:50:00 +0000124}
125
126//===----------------------------------------------------------------------===//
127// GCOVFunction implementation.
128
Yuchen Wuc4b184e2013-12-03 00:15:49 +0000129/// readGCNO - Read a function from the GCNO buffer. Return false if an error
130/// occurs.
Yuchen Wu812ad832013-12-04 04:49:23 +0000131bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
Yuchen Wudbb51ff2013-11-14 00:07:15 +0000132 uint32_t Dummy;
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000133 if (!Buff.readInt(Dummy))
134 return false; // Function header length
135 if (!Buff.readInt(Ident))
136 return false;
137 if (!Buff.readInt(Checksum))
138 return false;
Yuchen Wu45a5b272013-12-04 05:42:28 +0000139 if (Version != GCOV::V402) {
140 uint32_t CfgChecksum;
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000141 if (!Buff.readInt(CfgChecksum))
142 return false;
Yuchen Wu45a5b272013-12-04 05:42:28 +0000143 if (Parent.getChecksum() != CfgChecksum) {
144 errs() << "File checksums do not match: " << Parent.getChecksum()
145 << " != " << CfgChecksum << " in (" << Name << ").\n";
146 return false;
147 }
148 }
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000149 if (!Buff.readString(Name))
150 return false;
151 if (!Buff.readString(Filename))
152 return false;
153 if (!Buff.readInt(LineNumber))
154 return false;
Devang Pateld02c42b2011-09-28 18:50:00 +0000155
156 // read blocks.
Yuchen Wudbb51ff2013-11-14 00:07:15 +0000157 if (!Buff.readBlockTag()) {
158 errs() << "Block tag not found.\n";
159 return false;
160 }
161 uint32_t BlockCount;
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000162 if (!Buff.readInt(BlockCount))
163 return false;
Bob Wilson67fa5392013-10-22 20:02:36 +0000164 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000165 if (!Buff.readInt(Dummy))
166 return false; // Block flags;
David Blaikiee1c08632014-04-21 21:40:16 +0000167 Blocks.push_back(make_unique<GCOVBlock>(*this, i));
Devang Pateld02c42b2011-09-28 18:50:00 +0000168 }
169
170 // read edges.
171 while (Buff.readEdgeTag()) {
Yuchen Wudbb51ff2013-11-14 00:07:15 +0000172 uint32_t EdgeCount;
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000173 if (!Buff.readInt(EdgeCount))
174 return false;
Yuchen Wudbb51ff2013-11-14 00:07:15 +0000175 EdgeCount = (EdgeCount - 1) / 2;
176 uint32_t BlockNo;
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000177 if (!Buff.readInt(BlockNo))
178 return false;
Yuchen Wudbb51ff2013-11-14 00:07:15 +0000179 if (BlockNo >= BlockCount) {
Yuchen Wu7d3a11f2013-12-05 22:02:33 +0000180 errs() << "Unexpected block number: " << BlockNo << " (in " << Name
181 << ").\n";
Yuchen Wudbb51ff2013-11-14 00:07:15 +0000182 return false;
183 }
Bob Wilson67fa5392013-10-22 20:02:36 +0000184 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
Yuchen Wudbb51ff2013-11-14 00:07:15 +0000185 uint32_t Dst;
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000186 if (!Buff.readInt(Dst))
187 return false;
David Blaikiee1c08632014-04-21 21:40:16 +0000188 Edges.push_back(make_unique<GCOVEdge>(*Blocks[BlockNo], *Blocks[Dst]));
189 GCOVEdge *Edge = Edges.back().get();
Yuchen Wu2331c9f2013-12-03 00:24:44 +0000190 Blocks[BlockNo]->addDstEdge(Edge);
191 Blocks[Dst]->addSrcEdge(Edge);
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000192 if (!Buff.readInt(Dummy))
193 return false; // Edge flag
Devang Pateld02c42b2011-09-28 18:50:00 +0000194 }
195 }
196
197 // read line table.
198 while (Buff.readLineTag()) {
Yuchen Wudbb51ff2013-11-14 00:07:15 +0000199 uint32_t LineTableLength;
Justin Bogner688567e2014-05-02 20:01:24 +0000200 // Read the length of this line table.
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000201 if (!Buff.readInt(LineTableLength))
202 return false;
203 uint32_t EndPos = Buff.getCursor() + LineTableLength * 4;
Yuchen Wudbb51ff2013-11-14 00:07:15 +0000204 uint32_t BlockNo;
Justin Bogner688567e2014-05-02 20:01:24 +0000205 // Read the block number this table is associated with.
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000206 if (!Buff.readInt(BlockNo))
207 return false;
Yuchen Wudbb51ff2013-11-14 00:07:15 +0000208 if (BlockNo >= BlockCount) {
Yuchen Wu7d3a11f2013-12-05 22:02:33 +0000209 errs() << "Unexpected block number: " << BlockNo << " (in " << Name
210 << ").\n";
Yuchen Wudbb51ff2013-11-14 00:07:15 +0000211 return false;
Devang Pateld02c42b2011-09-28 18:50:00 +0000212 }
David Blaikiee1c08632014-04-21 21:40:16 +0000213 GCOVBlock &Block = *Blocks[BlockNo];
Justin Bogner688567e2014-05-02 20:01:24 +0000214 // Read the word that pads the beginning of the line table. This may be a
215 // flag of some sort, but seems to always be zero.
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000216 if (!Buff.readInt(Dummy))
217 return false;
Justin Bogner688567e2014-05-02 20:01:24 +0000218
219 // Line information starts here and continues up until the last word.
220 if (Buff.getCursor() != (EndPos - sizeof(uint32_t))) {
Yuchen Wu131a7642013-11-14 00:32:00 +0000221 StringRef F;
Justin Bogner688567e2014-05-02 20:01:24 +0000222 // Read the source file name.
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000223 if (!Buff.readString(F))
224 return false;
Yuchen Wu7d3a11f2013-12-05 22:02:33 +0000225 if (Filename != F) {
226 errs() << "Multiple sources for a single basic block: " << Filename
227 << " != " << F << " (in " << Name << ").\n";
Yuchen Wu131a7642013-11-14 00:32:00 +0000228 return false;
229 }
Justin Bogner688567e2014-05-02 20:01:24 +0000230 // Read lines up to, but not including, the null terminator.
231 while (Buff.getCursor() < (EndPos - 2 * sizeof(uint32_t))) {
Yuchen Wudbb51ff2013-11-14 00:07:15 +0000232 uint32_t Line;
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000233 if (!Buff.readInt(Line))
234 return false;
Justin Bogner688567e2014-05-02 20:01:24 +0000235 // Line 0 means this instruction was injected by the compiler. Skip it.
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000236 if (!Line)
237 continue;
David Blaikiee1c08632014-04-21 21:40:16 +0000238 Block.addLine(Line);
Yuchen Wudbb51ff2013-11-14 00:07:15 +0000239 }
Justin Bogner688567e2014-05-02 20:01:24 +0000240 // Read the null terminator.
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000241 if (!Buff.readInt(Dummy))
242 return false;
Yuchen Wudbb51ff2013-11-14 00:07:15 +0000243 }
Justin Bogner688567e2014-05-02 20:01:24 +0000244 // The last word is either a flag or padding, it isn't clear which. Skip
245 // over it.
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000246 if (!Buff.readInt(Dummy))
247 return false;
Devang Pateld02c42b2011-09-28 18:50:00 +0000248 }
249 return true;
250}
251
Yuchen Wuc4b184e2013-12-03 00:15:49 +0000252/// readGCDA - Read a function from the GCDA buffer. Return false if an error
253/// occurs.
Yuchen Wu812ad832013-12-04 04:49:23 +0000254bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
Justin Bognereb833ba2016-02-08 22:49:40 +0000255 uint32_t HeaderLength;
256 if (!Buff.readInt(HeaderLength))
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000257 return false; // Function header length
Daniel Jasperbfbf8d32013-12-04 08:57:17 +0000258
Justin Bognereb833ba2016-02-08 22:49:40 +0000259 uint64_t EndPos = Buff.getCursor() + HeaderLength * sizeof(uint32_t);
260
Yuchen Wu45a5b272013-12-04 05:42:28 +0000261 uint32_t GCDAIdent;
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000262 if (!Buff.readInt(GCDAIdent))
263 return false;
Yuchen Wu45a5b272013-12-04 05:42:28 +0000264 if (Ident != GCDAIdent) {
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000265 errs() << "Function identifiers do not match: " << Ident
266 << " != " << GCDAIdent << " (in " << Name << ").\n";
Yuchen Wu45a5b272013-12-04 05:42:28 +0000267 return false;
268 }
Yuchen Wuc4b184e2013-12-03 00:15:49 +0000269
Daniel Jasperbfbf8d32013-12-04 08:57:17 +0000270 uint32_t GCDAChecksum;
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000271 if (!Buff.readInt(GCDAChecksum))
272 return false;
Daniel Jasperbfbf8d32013-12-04 08:57:17 +0000273 if (Checksum != GCDAChecksum) {
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000274 errs() << "Function checksums do not match: " << Checksum
275 << " != " << GCDAChecksum << " (in " << Name << ").\n";
Daniel Jasperbfbf8d32013-12-04 08:57:17 +0000276 return false;
277 }
Yuchen Wu45a5b272013-12-04 05:42:28 +0000278
279 uint32_t CfgChecksum;
280 if (Version != GCOV::V402) {
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000281 if (!Buff.readInt(CfgChecksum))
282 return false;
Yuchen Wu45a5b272013-12-04 05:42:28 +0000283 if (Parent.getChecksum() != CfgChecksum) {
284 errs() << "File checksums do not match: " << Parent.getChecksum()
285 << " != " << CfgChecksum << " (in " << Name << ").\n";
286 return false;
287 }
288 }
289
Justin Bognereb833ba2016-02-08 22:49:40 +0000290 if (Buff.getCursor() < EndPos) {
291 StringRef GCDAName;
292 if (!Buff.readString(GCDAName))
293 return false;
294 if (Name != GCDAName) {
295 errs() << "Function names do not match: " << Name << " != " << GCDAName
296 << ".\n";
297 return false;
298 }
Yuchen Wu45a5b272013-12-04 05:42:28 +0000299 }
Yuchen Wuc4b184e2013-12-03 00:15:49 +0000300
301 if (!Buff.readArcTag()) {
Yuchen Wu45a5b272013-12-04 05:42:28 +0000302 errs() << "Arc tag not found (in " << Name << ").\n";
Yuchen Wuc4b184e2013-12-03 00:15:49 +0000303 return false;
304 }
Yuchen Wu2331c9f2013-12-03 00:24:44 +0000305
Yuchen Wuc4b184e2013-12-03 00:15:49 +0000306 uint32_t Count;
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000307 if (!Buff.readInt(Count))
308 return false;
Yuchen Wuc4b184e2013-12-03 00:15:49 +0000309 Count /= 2;
310
311 // This for loop adds the counts for each block. A second nested loop is
312 // required to combine the edge counts that are contained in the GCDA file.
Yuchen Wu2331c9f2013-12-03 00:24:44 +0000313 for (uint32_t BlockNo = 0; Count > 0; ++BlockNo) {
314 // The last block is always reserved for exit block
Justin Bognerbdd22122015-03-17 00:18:51 +0000315 if (BlockNo >= Blocks.size()) {
Yuchen Wu45a5b272013-12-04 05:42:28 +0000316 errs() << "Unexpected number of edges (in " << Name << ").\n";
Yuchen Wuc4b184e2013-12-03 00:15:49 +0000317 return false;
318 }
Justin Bognerbdd22122015-03-17 00:18:51 +0000319 if (BlockNo == Blocks.size() - 1)
320 errs() << "(" << Name << ") has arcs from exit block.\n";
Yuchen Wu2331c9f2013-12-03 00:24:44 +0000321 GCOVBlock &Block = *Blocks[BlockNo];
322 for (size_t EdgeNo = 0, End = Block.getNumDstEdges(); EdgeNo < End;
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000323 ++EdgeNo) {
Yuchen Wuc4b184e2013-12-03 00:15:49 +0000324 if (Count == 0) {
Yuchen Wu45a5b272013-12-04 05:42:28 +0000325 errs() << "Unexpected number of edges (in " << Name << ").\n";
Yuchen Wuc4b184e2013-12-03 00:15:49 +0000326 return false;
327 }
328 uint64_t ArcCount;
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000329 if (!Buff.readInt64(ArcCount))
330 return false;
Yuchen Wu2331c9f2013-12-03 00:24:44 +0000331 Block.addCount(EdgeNo, ArcCount);
Yuchen Wuc4b184e2013-12-03 00:15:49 +0000332 --Count;
333 }
Yuchen Wua2639792013-12-13 01:15:07 +0000334 Block.sortDstEdges();
Yuchen Wuc4b184e2013-12-03 00:15:49 +0000335 }
336 return true;
337}
338
Yuchen Wua2639792013-12-13 01:15:07 +0000339/// getEntryCount - Get the number of times the function was called by
340/// retrieving the entry block's count.
341uint64_t GCOVFunction::getEntryCount() const {
342 return Blocks.front()->getCount();
343}
344
345/// getExitCount - Get the number of times the function returned by retrieving
346/// the exit block's count.
347uint64_t GCOVFunction::getExitCount() const {
348 return Blocks.back()->getCount();
349}
350
Matthias Braun88d20752017-01-28 02:02:38 +0000351void GCOVFunction::print(raw_ostream &OS) const {
352 OS << "===== " << Name << " (" << Ident << ") @ " << Filename << ":"
353 << LineNumber << "\n";
354 for (const auto &Block : Blocks)
355 Block->print(OS);
356}
357
Aaron Ballman1d03d382017-10-15 14:32:27 +0000358#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yuchen Wu81e38282013-10-25 02:22:24 +0000359/// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
Calixte Denizet24b5a532018-09-20 16:09:30 +0000360LLVM_DUMP_METHOD void GCOVFunction::dump() const { print(dbgs()); }
Matthias Braun88d20752017-01-28 02:02:38 +0000361#endif
Devang Pateld02c42b2011-09-28 18:50:00 +0000362
363/// collectLineCounts - Collect line counts. This must be used after
364/// reading .gcno and .gcda files.
365void GCOVFunction::collectLineCounts(FileInfo &FI) {
Justin Bogner2a6873f2014-03-26 22:03:06 +0000366 // If the line number is zero, this is a function that doesn't actually appear
367 // in the source file, so there isn't anything we can do with it.
368 if (LineNumber == 0)
369 return;
370
David Blaikiee1c08632014-04-21 21:40:16 +0000371 for (const auto &Block : Blocks)
372 Block->collectLineCounts(FI);
Yuchen Wua2639792013-12-13 01:15:07 +0000373 FI.addFunctionLine(Filename, LineNumber, this);
Devang Pateld02c42b2011-09-28 18:50:00 +0000374}
375
376//===----------------------------------------------------------------------===//
377// GCOVBlock implementation.
378
379/// ~GCOVBlock - Delete GCOVBlock and its content.
380GCOVBlock::~GCOVBlock() {
Yuchen Wu2331c9f2013-12-03 00:24:44 +0000381 SrcEdges.clear();
382 DstEdges.clear();
Yuchen Wu131a7642013-11-14 00:32:00 +0000383 Lines.clear();
Devang Pateld02c42b2011-09-28 18:50:00 +0000384}
385
Yuchen Wu2331c9f2013-12-03 00:24:44 +0000386/// addCount - Add to block counter while storing the edge count. If the
387/// destination has no outgoing edges, also update that block's count too.
388void GCOVBlock::addCount(size_t DstEdgeNo, uint64_t N) {
389 assert(DstEdgeNo < DstEdges.size()); // up to caller to ensure EdgeNo is valid
390 DstEdges[DstEdgeNo]->Count = N;
391 Counter += N;
David Blaikiee1c08632014-04-21 21:40:16 +0000392 if (!DstEdges[DstEdgeNo]->Dst.getNumDstEdges())
393 DstEdges[DstEdgeNo]->Dst.Counter += N;
Yuchen Wu2331c9f2013-12-03 00:24:44 +0000394}
395
Yuchen Wua2639792013-12-13 01:15:07 +0000396/// sortDstEdges - Sort destination edges by block number, nop if already
397/// sorted. This is required for printing branch info in the correct order.
398void GCOVBlock::sortDstEdges() {
399 if (!DstEdgesAreSorted) {
400 SortDstEdgesFunctor SortEdges;
401 std::stable_sort(DstEdges.begin(), DstEdges.end(), SortEdges);
402 }
403}
404
Devang Pateld02c42b2011-09-28 18:50:00 +0000405/// collectLineCounts - Collect line counts. This must be used after
406/// reading .gcno and .gcda files.
407void GCOVBlock::collectLineCounts(FileInfo &FI) {
Justin Bogner798383b2015-01-23 22:57:02 +0000408 for (uint32_t N : Lines)
409 FI.addBlockLine(Parent.getFilename(), N, this);
Devang Pateld02c42b2011-09-28 18:50:00 +0000410}
411
Matthias Braun88d20752017-01-28 02:02:38 +0000412void GCOVBlock::print(raw_ostream &OS) const {
413 OS << "Block : " << Number << " Counter : " << Counter << "\n";
Yuchen Wu2331c9f2013-12-03 00:24:44 +0000414 if (!SrcEdges.empty()) {
Matthias Braun88d20752017-01-28 02:02:38 +0000415 OS << "\tSource Edges : ";
Justin Bogner798383b2015-01-23 22:57:02 +0000416 for (const GCOVEdge *Edge : SrcEdges)
Matthias Braun88d20752017-01-28 02:02:38 +0000417 OS << Edge->Src.Number << " (" << Edge->Count << "), ";
418 OS << "\n";
Yuchen Wu2331c9f2013-12-03 00:24:44 +0000419 }
420 if (!DstEdges.empty()) {
Matthias Braun88d20752017-01-28 02:02:38 +0000421 OS << "\tDestination Edges : ";
Justin Bogner798383b2015-01-23 22:57:02 +0000422 for (const GCOVEdge *Edge : DstEdges)
Matthias Braun88d20752017-01-28 02:02:38 +0000423 OS << Edge->Dst.Number << " (" << Edge->Count << "), ";
424 OS << "\n";
Devang Pateld02c42b2011-09-28 18:50:00 +0000425 }
426 if (!Lines.empty()) {
Matthias Braun88d20752017-01-28 02:02:38 +0000427 OS << "\tLines : ";
Justin Bogner798383b2015-01-23 22:57:02 +0000428 for (uint32_t N : Lines)
Matthias Braun88d20752017-01-28 02:02:38 +0000429 OS << (N) << ",";
430 OS << "\n";
Devang Pateld02c42b2011-09-28 18:50:00 +0000431 }
432}
433
Aaron Ballman1d03d382017-10-15 14:32:27 +0000434#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun88d20752017-01-28 02:02:38 +0000435/// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
Calixte Denizet24b5a532018-09-20 16:09:30 +0000436LLVM_DUMP_METHOD void GCOVBlock::dump() const { print(dbgs()); }
Matthias Braun88d20752017-01-28 02:02:38 +0000437#endif
438
Devang Pateld02c42b2011-09-28 18:50:00 +0000439//===----------------------------------------------------------------------===//
Calixte Denizet24b5a532018-09-20 16:09:30 +0000440// Cycles detection
441//
442// The algorithm in GCC is based on the algorihtm by Hawick & James:
443// "Enumerating Circuits and Loops in Graphs with Self-Arcs and Multiple-Arcs"
444// http://complexity.massey.ac.nz/cstn/013/cstn-013.pdf.
445
446/// Get the count for the detected cycle.
447uint64_t GCOVBlock::getCycleCount(const Edges &Path) {
448 uint64_t CycleCount = std::numeric_limits<uint64_t>::max();
449 for (auto E : Path) {
450 CycleCount = std::min(E->CyclesCount, CycleCount);
451 }
452 for (auto E : Path) {
453 E->CyclesCount -= CycleCount;
454 }
455 return CycleCount;
456}
457
458/// Unblock a vertex previously marked as blocked.
459void GCOVBlock::unblock(const GCOVBlock *U, BlockVector &Blocked,
460 BlockVectorLists &BlockLists) {
461 auto it = find(Blocked, U);
462 if (it == Blocked.end()) {
463 return;
464 }
465
466 const size_t index = it - Blocked.begin();
467 Blocked.erase(it);
468
469 const BlockVector ToUnblock(BlockLists[index]);
470 BlockLists.erase(BlockLists.begin() + index);
471 for (auto GB : ToUnblock) {
472 GCOVBlock::unblock(GB, Blocked, BlockLists);
473 }
474}
475
476bool GCOVBlock::lookForCircuit(const GCOVBlock *V, const GCOVBlock *Start,
477 Edges &Path, BlockVector &Blocked,
478 BlockVectorLists &BlockLists,
479 const BlockVector &Blocks, uint64_t &Count) {
480 Blocked.push_back(V);
481 BlockLists.emplace_back(BlockVector());
482 bool FoundCircuit = false;
483
484 for (auto E : V->dsts()) {
485 const GCOVBlock *W = &E->Dst;
486 if (W < Start || find(Blocks, W) == Blocks.end()) {
487 continue;
488 }
489
490 Path.push_back(E);
491
492 if (W == Start) {
493 // We've a cycle.
494 Count += GCOVBlock::getCycleCount(Path);
495 FoundCircuit = true;
496 } else if (find(Blocked, W) == Blocked.end() && // W is not blocked.
497 GCOVBlock::lookForCircuit(W, Start, Path, Blocked, BlockLists,
498 Blocks, Count)) {
499 FoundCircuit = true;
500 }
501
502 Path.pop_back();
503 }
504
505 if (FoundCircuit) {
506 GCOVBlock::unblock(V, Blocked, BlockLists);
507 } else {
508 for (auto E : V->dsts()) {
509 const GCOVBlock *W = &E->Dst;
510 if (W < Start || find(Blocks, W) == Blocks.end()) {
511 continue;
512 }
513 const size_t index = find(Blocked, W) - Blocked.begin();
514 BlockVector &List = BlockLists[index];
515 if (find(List, V) == List.end()) {
516 List.push_back(V);
517 }
518 }
519 }
520
521 return FoundCircuit;
522}
523
524/// Get the count for the list of blocks which lie on the same line.
525void GCOVBlock::getCyclesCount(const BlockVector &Blocks, uint64_t &Count) {
526 for (auto Block : Blocks) {
527 Edges Path;
528 BlockVector Blocked;
529 BlockVectorLists BlockLists;
530
531 GCOVBlock::lookForCircuit(Block, Block, Path, Blocked, BlockLists, Blocks,
532 Count);
533 }
534}
535
536/// Get the count for the list of blocks which lie on the same line.
537uint64_t GCOVBlock::getLineCount(const BlockVector &Blocks) {
538 uint64_t Count = 0;
539
540 for (auto Block : Blocks) {
541 if (Block->getNumSrcEdges() == 0) {
542 // The block has no predecessors and a non-null counter
543 // (can be the case with entry block in functions).
544 Count += Block->getCount();
545 } else {
546 // Add counts from predecessors that are not on the same line.
547 for (auto E : Block->srcs()) {
548 const GCOVBlock *W = &E->Src;
549 if (find(Blocks, W) == Blocks.end()) {
550 Count += E->Count;
551 }
552 }
553 }
554 for (auto E : Block->dsts()) {
555 E->CyclesCount = E->Count;
556 }
557 }
558
559 GCOVBlock::getCyclesCount(Blocks, Count);
560
561 return Count;
562}
563
564//===----------------------------------------------------------------------===//
Devang Pateld02c42b2011-09-28 18:50:00 +0000565// FileInfo implementation.
566
Yuchen Wua2639792013-12-13 01:15:07 +0000567// Safe integer division, returns 0 if numerator is 0.
568static uint32_t safeDiv(uint64_t Numerator, uint64_t Divisor) {
569 if (!Numerator)
570 return 0;
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000571 return Numerator / Divisor;
Yuchen Wua2639792013-12-13 01:15:07 +0000572}
573
574// This custom division function mimics gcov's branch ouputs:
575// - Round to closest whole number
576// - Only output 0% or 100% if it's exactly that value
577static uint32_t branchDiv(uint64_t Numerator, uint64_t Divisor) {
578 if (!Numerator)
579 return 0;
580 if (Numerator == Divisor)
581 return 100;
582
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000583 uint8_t Res = (Numerator * 100 + Divisor / 2) / Divisor;
Yuchen Wua2639792013-12-13 01:15:07 +0000584 if (Res == 0)
585 return 1;
586 if (Res == 100)
587 return 99;
588 return Res;
589}
590
Benjamin Kramercab2a092015-03-23 13:59:13 +0000591namespace {
Yuchen Wuf6358f32013-12-18 18:40:15 +0000592struct formatBranchInfo {
Richard Smith11840052015-10-14 00:04:19 +0000593 formatBranchInfo(const GCOV::Options &Options, uint64_t Count, uint64_t Total)
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000594 : Options(Options), Count(Count), Total(Total) {}
Yuchen Wuf6358f32013-12-18 18:40:15 +0000595
596 void print(raw_ostream &OS) const {
597 if (!Total)
598 OS << "never executed";
599 else if (Options.BranchCount)
600 OS << "taken " << Count;
601 else
602 OS << "taken " << branchDiv(Count, Total) << "%";
603 }
604
Richard Smith11840052015-10-14 00:04:19 +0000605 const GCOV::Options &Options;
Yuchen Wuf6358f32013-12-18 18:40:15 +0000606 uint64_t Count;
607 uint64_t Total;
608};
609
610static raw_ostream &operator<<(raw_ostream &OS, const formatBranchInfo &FBI) {
611 FBI.print(OS);
612 return OS;
613}
614
Justin Bognerc92330c2014-05-07 02:11:23 +0000615class LineConsumer {
616 std::unique_ptr<MemoryBuffer> Buffer;
617 StringRef Remaining;
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000618
Justin Bognerc92330c2014-05-07 02:11:23 +0000619public:
620 LineConsumer(StringRef Filename) {
Rafael Espindola7cba2a92014-07-06 17:43:13 +0000621 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
622 MemoryBuffer::getFileOrSTDIN(Filename);
623 if (std::error_code EC = BufferOrErr.getError()) {
Justin Bognerc92330c2014-05-07 02:11:23 +0000624 errs() << Filename << ": " << EC.message() << "\n";
625 Remaining = "";
Rafael Espindola7cba2a92014-07-06 17:43:13 +0000626 } else {
627 Buffer = std::move(BufferOrErr.get());
Justin Bognerc92330c2014-05-07 02:11:23 +0000628 Remaining = Buffer->getBuffer();
Rafael Espindola7cba2a92014-07-06 17:43:13 +0000629 }
Justin Bognerc92330c2014-05-07 02:11:23 +0000630 }
631 bool empty() { return Remaining.empty(); }
632 void printNext(raw_ostream &OS, uint32_t LineNum) {
633 StringRef Line;
634 if (empty())
635 Line = "/*EOF*/";
636 else
637 std::tie(Line, Remaining) = Remaining.split("\n");
638 OS << format("%5u:", LineNum) << Line << "\n";
639 }
640};
Eugene Zelenko380d47d2016-02-02 18:20:45 +0000641} // end anonymous namespace
Justin Bognerc92330c2014-05-07 02:11:23 +0000642
Justin Bogner01c05502014-02-04 10:45:02 +0000643/// Convert a path to a gcov filename. If PreservePaths is true, this
644/// translates "/" to "#", ".." to "^", and drops ".", to match gcov.
645static std::string mangleCoveragePath(StringRef Filename, bool PreservePaths) {
646 if (!PreservePaths)
Justin Bognerde2703d2014-04-23 21:44:55 +0000647 return sys::path::filename(Filename).str();
Justin Bogner01c05502014-02-04 10:45:02 +0000648
649 // This behaviour is defined by gcov in terms of text replacements, so it's
650 // not likely to do anything useful on filesystems with different textual
651 // conventions.
652 llvm::SmallString<256> Result("");
653 StringRef::iterator I, S, E;
654 for (I = S = Filename.begin(), E = Filename.end(); I != E; ++I) {
655 if (*I != '/')
656 continue;
657
658 if (I - S == 1 && *S == '.') {
659 // ".", the current directory, is skipped.
660 } else if (I - S == 2 && *S == '.' && *(S + 1) == '.') {
661 // "..", the parent directory, is replaced with "^".
662 Result.append("^#");
663 } else {
664 if (S < I)
665 // Leave other components intact,
666 Result.append(S, I);
667 // And separate with "#".
668 Result.push_back('#');
669 }
670 S = I + 1;
671 }
672
673 if (S < I)
674 Result.append(S, I);
Justin Bogner01c05502014-02-04 10:45:02 +0000675 return Result.str();
676}
677
Justin Bogner56d05e42014-05-07 02:11:18 +0000678std::string FileInfo::getCoveragePath(StringRef Filename,
679 StringRef MainFilename) {
680 if (Options.NoOutput)
681 // This is probably a bug in gcov, but when -n is specified, paths aren't
682 // mangled at all, and the -l and -p options are ignored. Here, we do the
683 // same.
684 return Filename;
685
686 std::string CoveragePath;
687 if (Options.LongFileNames && !Filename.equals(MainFilename))
688 CoveragePath =
689 mangleCoveragePath(MainFilename, Options.PreservePaths) + "##";
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000690 CoveragePath += mangleCoveragePath(Filename, Options.PreservePaths) + ".gcov";
Justin Bogner56d05e42014-05-07 02:11:18 +0000691 return CoveragePath;
692}
693
694std::unique_ptr<raw_ostream>
695FileInfo::openCoveragePath(StringRef CoveragePath) {
696 if (Options.NoOutput)
Justin Bogner459a8aa2014-05-07 16:01:27 +0000697 return llvm::make_unique<raw_null_ostream>();
Justin Bogner56d05e42014-05-07 02:11:18 +0000698
Rafael Espindola8c968622014-08-25 18:16:47 +0000699 std::error_code EC;
Calixte Denizet24b5a532018-09-20 16:09:30 +0000700 auto OS =
701 llvm::make_unique<raw_fd_ostream>(CoveragePath, EC, sys::fs::F_Text);
Rafael Espindola8c968622014-08-25 18:16:47 +0000702 if (EC) {
703 errs() << EC.message() << "\n";
Justin Bogner459a8aa2014-05-07 16:01:27 +0000704 return llvm::make_unique<raw_null_ostream>();
Justin Bogner56d05e42014-05-07 02:11:18 +0000705 }
706 return std::move(OS);
707}
708
Devang Pateld02c42b2011-09-28 18:50:00 +0000709/// print - Print source files with collected line count information.
Justin Bognerc6945d92015-01-23 23:09:27 +0000710void FileInfo::print(raw_ostream &InfoOS, StringRef MainFilename,
711 StringRef GCNOFile, StringRef GCDAFile) {
Vedant Kumar4bfa64a2017-04-26 00:16:10 +0000712 SmallVector<StringRef, 4> Filenames;
713 for (const auto &LI : LineInfo)
714 Filenames.push_back(LI.first());
Fangrui Song3b35e172018-09-27 02:13:45 +0000715 llvm::sort(Filenames);
Vedant Kumar4bfa64a2017-04-26 00:16:10 +0000716
717 for (StringRef Filename : Filenames) {
Justin Bognerc92330c2014-05-07 02:11:23 +0000718 auto AllLines = LineConsumer(Filename);
Yuchen Wua49e1f52013-11-19 20:57:20 +0000719
Justin Bogner56d05e42014-05-07 02:11:18 +0000720 std::string CoveragePath = getCoveragePath(Filename, MainFilename);
Justin Bognerc6945d92015-01-23 23:09:27 +0000721 std::unique_ptr<raw_ostream> CovStream = openCoveragePath(CoveragePath);
722 raw_ostream &CovOS = *CovStream;
Yuchen Wu6cd7a3f2013-12-03 00:57:11 +0000723
Justin Bognerc6945d92015-01-23 23:09:27 +0000724 CovOS << " -: 0:Source:" << Filename << "\n";
725 CovOS << " -: 0:Graph:" << GCNOFile << "\n";
726 CovOS << " -: 0:Data:" << GCDAFile << "\n";
727 CovOS << " -: 0:Runs:" << RunCount << "\n";
728 CovOS << " -: 0:Programs:" << ProgramCount << "\n";
Yuchen Wua49e1f52013-11-19 20:57:20 +0000729
Vedant Kumar4bfa64a2017-04-26 00:16:10 +0000730 const LineData &Line = LineInfo[Filename];
Yuchen Wud2189592013-12-19 00:29:25 +0000731 GCOVCoverage FileCoverage(Filename);
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000732 for (uint32_t LineIndex = 0; LineIndex < Line.LastLine || !AllLines.empty();
733 ++LineIndex) {
Yuchen Wuf6358f32013-12-18 18:40:15 +0000734 if (Options.BranchInfo) {
Yuchen Wua2639792013-12-13 01:15:07 +0000735 FunctionLines::const_iterator FuncsIt = Line.Functions.find(LineIndex);
736 if (FuncsIt != Line.Functions.end())
Justin Bognerc6945d92015-01-23 23:09:27 +0000737 printFunctionSummary(CovOS, FuncsIt->second);
Yuchen Wua2639792013-12-13 01:15:07 +0000738 }
Yuchen Wud25d7a52013-12-03 01:35:31 +0000739
Yuchen Wua2639792013-12-13 01:15:07 +0000740 BlockLines::const_iterator BlocksIt = Line.Blocks.find(LineIndex);
741 if (BlocksIt == Line.Blocks.end()) {
742 // No basic blocks are on this line. Not an executable line of code.
Justin Bognerc6945d92015-01-23 23:09:27 +0000743 CovOS << " -:";
744 AllLines.printNext(CovOS, LineIndex + 1);
Yuchen Wua2639792013-12-13 01:15:07 +0000745 } else {
Yuchen Wu357fcf92013-12-03 00:38:21 +0000746 const BlockVector &Blocks = BlocksIt->second;
Yuchen Wua2639792013-12-13 01:15:07 +0000747
748 // Add up the block counts to form line counts.
Yuchen Wud2189592013-12-19 00:29:25 +0000749 DenseMap<const GCOVFunction *, bool> LineExecs;
Justin Bogner798383b2015-01-23 22:57:02 +0000750 for (const GCOVBlock *Block : Blocks) {
Yuchen Wud2189592013-12-19 00:29:25 +0000751 if (Options.FuncCoverage) {
752 // This is a slightly convoluted way to most accurately gather line
753 // statistics for functions. Basically what is happening is that we
754 // don't want to count a single line with multiple blocks more than
755 // once. However, we also don't simply want to give the total line
756 // count to every function that starts on the line. Thus, what is
757 // happening here are two things:
758 // 1) Ensure that the number of logical lines is only incremented
759 // once per function.
760 // 2) If there are multiple blocks on the same line, ensure that the
761 // number of lines executed is incremented as long as at least
762 // one of the blocks are executed.
763 const GCOVFunction *Function = &Block->getParent();
764 if (FuncCoverages.find(Function) == FuncCoverages.end()) {
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000765 std::pair<const GCOVFunction *, GCOVCoverage> KeyValue(
766 Function, GCOVCoverage(Function->getName()));
Yuchen Wud2189592013-12-19 00:29:25 +0000767 FuncCoverages.insert(KeyValue);
768 }
769 GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second;
770
771 if (LineExecs.find(Function) == LineExecs.end()) {
772 if (Block->getCount()) {
773 ++FuncCoverage.LinesExec;
774 LineExecs[Function] = true;
775 } else {
776 LineExecs[Function] = false;
777 }
778 ++FuncCoverage.LogicalLines;
779 } else if (!LineExecs[Function] && Block->getCount()) {
780 ++FuncCoverage.LinesExec;
781 LineExecs[Function] = true;
782 }
783 }
Yuchen Wu357fcf92013-12-03 00:38:21 +0000784 }
Yuchen Wu8cb0f6e2013-12-18 21:12:51 +0000785
Calixte Denizet24b5a532018-09-20 16:09:30 +0000786 const uint64_t LineCount = GCOVBlock::getLineCount(Blocks);
Yuchen Wu357fcf92013-12-03 00:38:21 +0000787 if (LineCount == 0)
Justin Bognerc6945d92015-01-23 23:09:27 +0000788 CovOS << " #####:";
Yuchen Wu8cb0f6e2013-12-18 21:12:51 +0000789 else {
Justin Bognerc6945d92015-01-23 23:09:27 +0000790 CovOS << format("%9" PRIu64 ":", LineCount);
Yuchen Wud2189592013-12-19 00:29:25 +0000791 ++FileCoverage.LinesExec;
Yuchen Wu8cb0f6e2013-12-18 21:12:51 +0000792 }
Yuchen Wud2189592013-12-19 00:29:25 +0000793 ++FileCoverage.LogicalLines;
Yuchen Wu30d5ef52013-12-10 01:02:07 +0000794
Justin Bognerc6945d92015-01-23 23:09:27 +0000795 AllLines.printNext(CovOS, LineIndex + 1);
Yuchen Wua2639792013-12-13 01:15:07 +0000796
Yuchen Wu30d5ef52013-12-10 01:02:07 +0000797 uint32_t BlockNo = 0;
Yuchen Wua2639792013-12-13 01:15:07 +0000798 uint32_t EdgeNo = 0;
Justin Bogner798383b2015-01-23 22:57:02 +0000799 for (const GCOVBlock *Block : Blocks) {
Yuchen Wua2639792013-12-13 01:15:07 +0000800 // Only print block and branch information at the end of the block.
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000801 if (Block->getLastLine() != LineIndex + 1)
Yuchen Wua2639792013-12-13 01:15:07 +0000802 continue;
803 if (Options.AllBlocks)
Justin Bognerc6945d92015-01-23 23:09:27 +0000804 printBlockInfo(CovOS, *Block, LineIndex, BlockNo);
Yuchen Wuf6358f32013-12-18 18:40:15 +0000805 if (Options.BranchInfo) {
Yuchen Wu89452cf2013-12-16 22:14:02 +0000806 size_t NumEdges = Block->getNumDstEdges();
807 if (NumEdges > 1)
Justin Bognerc6945d92015-01-23 23:09:27 +0000808 printBranchInfo(CovOS, *Block, FileCoverage, EdgeNo);
Yuchen Wu89452cf2013-12-16 22:14:02 +0000809 else if (Options.UncondBranch && NumEdges == 1)
Justin Bognerc6945d92015-01-23 23:09:27 +0000810 printUncondBranchInfo(CovOS, EdgeNo,
811 (*Block->dst_begin())->Count);
Yuchen Wu89452cf2013-12-16 22:14:02 +0000812 }
Yuchen Wu30d5ef52013-12-10 01:02:07 +0000813 }
814 }
Devang Pateld02c42b2011-09-28 18:50:00 +0000815 }
Justin Bogner01c05502014-02-04 10:45:02 +0000816 FileCoverages.push_back(std::make_pair(CoveragePath, FileCoverage));
Devang Pateld02c42b2011-09-28 18:50:00 +0000817 }
Yuchen Wud2189592013-12-19 00:29:25 +0000818
819 // FIXME: There is no way to detect calls given current instrumentation.
820 if (Options.FuncCoverage)
Justin Bognerc6945d92015-01-23 23:09:27 +0000821 printFuncCoverage(InfoOS);
822 printFileCoverage(InfoOS);
Devang Pateld02c42b2011-09-28 18:50:00 +0000823}
Yuchen Wua2639792013-12-13 01:15:07 +0000824
825/// printFunctionSummary - Print function and block summary.
Justin Bogner56d05e42014-05-07 02:11:18 +0000826void FileInfo::printFunctionSummary(raw_ostream &OS,
Yuchen Wua2639792013-12-13 01:15:07 +0000827 const FunctionVector &Funcs) const {
Justin Bogner798383b2015-01-23 22:57:02 +0000828 for (const GCOVFunction *Func : Funcs) {
Yuchen Wua2639792013-12-13 01:15:07 +0000829 uint64_t EntryCount = Func->getEntryCount();
Yuchen Wu2ceb6ae2013-12-18 18:46:25 +0000830 uint32_t BlocksExec = 0;
Justin Bogner798383b2015-01-23 22:57:02 +0000831 for (const GCOVBlock &Block : Func->blocks())
David Blaikiee1c08632014-04-21 21:40:16 +0000832 if (Block.getNumDstEdges() && Block.getCount())
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000833 ++BlocksExec;
Yuchen Wua2639792013-12-13 01:15:07 +0000834
835 OS << "function " << Func->getName() << " called " << EntryCount
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000836 << " returned " << safeDiv(Func->getExitCount() * 100, EntryCount)
Yuchen Wua2639792013-12-13 01:15:07 +0000837 << "% blocks executed "
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000838 << safeDiv(BlocksExec * 100, Func->getNumBlocks() - 1) << "%\n";
Yuchen Wua2639792013-12-13 01:15:07 +0000839 }
840}
841
842/// printBlockInfo - Output counts for each block.
Justin Bogner56d05e42014-05-07 02:11:18 +0000843void FileInfo::printBlockInfo(raw_ostream &OS, const GCOVBlock &Block,
Yuchen Wua2639792013-12-13 01:15:07 +0000844 uint32_t LineIndex, uint32_t &BlockNo) const {
845 if (Block.getCount() == 0)
846 OS << " $$$$$:";
847 else
848 OS << format("%9" PRIu64 ":", Block.getCount());
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000849 OS << format("%5u-block %2u\n", LineIndex + 1, BlockNo++);
Yuchen Wua2639792013-12-13 01:15:07 +0000850}
851
Yuchen Wu89452cf2013-12-16 22:14:02 +0000852/// printBranchInfo - Print conditional branch probabilities.
Justin Bogner56d05e42014-05-07 02:11:18 +0000853void FileInfo::printBranchInfo(raw_ostream &OS, const GCOVBlock &Block,
Yuchen Wud2189592013-12-19 00:29:25 +0000854 GCOVCoverage &Coverage, uint32_t &EdgeNo) {
Yuchen Wua2639792013-12-13 01:15:07 +0000855 SmallVector<uint64_t, 16> BranchCounts;
856 uint64_t TotalCounts = 0;
Justin Bogner798383b2015-01-23 22:57:02 +0000857 for (const GCOVEdge *Edge : Block.dsts()) {
Yuchen Wua2639792013-12-13 01:15:07 +0000858 BranchCounts.push_back(Edge->Count);
859 TotalCounts += Edge->Count;
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000860 if (Block.getCount())
861 ++Coverage.BranchesExec;
862 if (Edge->Count)
863 ++Coverage.BranchesTaken;
Yuchen Wu8cb0f6e2013-12-18 21:12:51 +0000864 ++Coverage.Branches;
Yuchen Wud2189592013-12-19 00:29:25 +0000865
866 if (Options.FuncCoverage) {
867 const GCOVFunction *Function = &Block.getParent();
868 GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second;
Justin Bogner4c61bbe2015-01-23 22:38:01 +0000869 if (Block.getCount())
870 ++FuncCoverage.BranchesExec;
871 if (Edge->Count)
872 ++FuncCoverage.BranchesTaken;
Yuchen Wud2189592013-12-19 00:29:25 +0000873 ++FuncCoverage.Branches;
874 }
Yuchen Wua2639792013-12-13 01:15:07 +0000875 }
876
Justin Bogner798383b2015-01-23 22:57:02 +0000877 for (uint64_t N : BranchCounts)
Yuchen Wuf6358f32013-12-18 18:40:15 +0000878 OS << format("branch %2u ", EdgeNo++)
Justin Bogner798383b2015-01-23 22:57:02 +0000879 << formatBranchInfo(Options, N, TotalCounts) << "\n";
Yuchen Wua2639792013-12-13 01:15:07 +0000880}
Yuchen Wu89452cf2013-12-16 22:14:02 +0000881
882/// printUncondBranchInfo - Print unconditional branch probabilities.
Justin Bogner56d05e42014-05-07 02:11:18 +0000883void FileInfo::printUncondBranchInfo(raw_ostream &OS, uint32_t &EdgeNo,
Yuchen Wu89452cf2013-12-16 22:14:02 +0000884 uint64_t Count) const {
Yuchen Wuf6358f32013-12-18 18:40:15 +0000885 OS << format("unconditional %2u ", EdgeNo++)
886 << formatBranchInfo(Options, Count, Count) << "\n";
Yuchen Wu89452cf2013-12-16 22:14:02 +0000887}
Yuchen Wu8cb0f6e2013-12-18 21:12:51 +0000888
Yuchen Wud2189592013-12-19 00:29:25 +0000889// printCoverage - Print generic coverage info used by both printFuncCoverage
890// and printFileCoverage.
Justin Bognerc6945d92015-01-23 23:09:27 +0000891void FileInfo::printCoverage(raw_ostream &OS,
892 const GCOVCoverage &Coverage) const {
893 OS << format("Lines executed:%.2f%% of %u\n",
894 double(Coverage.LinesExec) * 100 / Coverage.LogicalLines,
895 Coverage.LogicalLines);
Yuchen Wu8cb0f6e2013-12-18 21:12:51 +0000896 if (Options.BranchInfo) {
897 if (Coverage.Branches) {
Justin Bognerc6945d92015-01-23 23:09:27 +0000898 OS << format("Branches executed:%.2f%% of %u\n",
899 double(Coverage.BranchesExec) * 100 / Coverage.Branches,
900 Coverage.Branches);
901 OS << format("Taken at least once:%.2f%% of %u\n",
902 double(Coverage.BranchesTaken) * 100 / Coverage.Branches,
903 Coverage.Branches);
Yuchen Wu8cb0f6e2013-12-18 21:12:51 +0000904 } else {
Justin Bognerc6945d92015-01-23 23:09:27 +0000905 OS << "No branches\n";
Yuchen Wu8cb0f6e2013-12-18 21:12:51 +0000906 }
Justin Bognerc6945d92015-01-23 23:09:27 +0000907 OS << "No calls\n"; // to be consistent with gcov
Yuchen Wu8cb0f6e2013-12-18 21:12:51 +0000908 }
Yuchen Wud2189592013-12-19 00:29:25 +0000909}
910
911// printFuncCoverage - Print per-function coverage info.
Justin Bognerc6945d92015-01-23 23:09:27 +0000912void FileInfo::printFuncCoverage(raw_ostream &OS) const {
Justin Bogner798383b2015-01-23 22:57:02 +0000913 for (const auto &FC : FuncCoverages) {
914 const GCOVCoverage &Coverage = FC.second;
Justin Bognerc6945d92015-01-23 23:09:27 +0000915 OS << "Function '" << Coverage.Name << "'\n";
916 printCoverage(OS, Coverage);
917 OS << "\n";
Yuchen Wud2189592013-12-19 00:29:25 +0000918 }
919}
920
921// printFileCoverage - Print per-file coverage info.
Justin Bognerc6945d92015-01-23 23:09:27 +0000922void FileInfo::printFileCoverage(raw_ostream &OS) const {
Justin Bogner798383b2015-01-23 22:57:02 +0000923 for (const auto &FC : FileCoverages) {
924 const std::string &Filename = FC.first;
925 const GCOVCoverage &Coverage = FC.second;
Justin Bognerc6945d92015-01-23 23:09:27 +0000926 OS << "File '" << Coverage.Name << "'\n";
927 printCoverage(OS, Coverage);
Justin Bogner56d05e42014-05-07 02:11:18 +0000928 if (!Options.NoOutput)
Justin Bognerc6945d92015-01-23 23:09:27 +0000929 OS << Coverage.Name << ":creating '" << Filename << "'\n";
930 OS << "\n";
Yuchen Wud2189592013-12-19 00:29:25 +0000931 }
Yuchen Wu8cb0f6e2013-12-18 21:12:51 +0000932}