blob: fe8ebb2e9a2c15fc4d3be3ece2e846c9730fa06e [file] [log] [blame]
Devang Patel8dfb6552011-10-04 17:24:48 +00001//===- GCOVr.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//
Devang Patel8dfb6552011-10-04 17:24:48 +000010// GCOV implements the interface to read and write coverage files that use
11// 'gcov' format.
Devang Patel37140652011-09-28 18:50:00 +000012//
Devang Patel37140652011-09-28 18:50:00 +000013//===----------------------------------------------------------------------===//
14
Devang Patel8dfb6552011-10-04 17:24:48 +000015#include "llvm/Support/GCOV.h"
Devang Patel37140652011-09-28 18:50:00 +000016#include "llvm/ADT/OwningPtr.h"
Devang Patela9e8a252011-09-29 16:46:47 +000017#include "llvm/ADT/STLExtras.h"
Bob Wilson3461bed2013-10-22 05:09:41 +000018#include "llvm/Support/Format.h"
Devang Patel37140652011-09-28 18:50:00 +000019#include "llvm/Support/MemoryObject.h"
20#include "llvm/Support/system_error.h"
21using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// GCOVFile implementation.
25
26/// ~GCOVFile - Delete GCOVFile and its content.
27GCOVFile::~GCOVFile() {
28 DeleteContainerPointers(Functions);
29}
30
Devang Patele5a8f2f92011-09-29 17:06:40 +000031/// isGCDAFile - Return true if Format identifies a .gcda file.
Bill Wendling6bbe4892012-08-31 17:31:28 +000032static bool isGCDAFile(GCOV::GCOVFormat Format) {
33 return Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404;
Devang Patele5a8f2f92011-09-29 17:06:40 +000034}
35
36/// isGCNOFile - Return true if Format identifies a .gcno file.
Bill Wendling6bbe4892012-08-31 17:31:28 +000037static bool isGCNOFile(GCOV::GCOVFormat Format) {
38 return Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404;
Devang Patele5a8f2f92011-09-29 17:06:40 +000039}
40
Devang Patel37140652011-09-28 18:50:00 +000041/// read - Read GCOV buffer.
42bool GCOVFile::read(GCOVBuffer &Buffer) {
Bill Wendling6bbe4892012-08-31 17:31:28 +000043 GCOV::GCOVFormat Format = Buffer.readGCOVFormat();
44 if (Format == GCOV::InvalidGCOV)
Devang Patel37140652011-09-28 18:50:00 +000045 return false;
46
Yuchen Wu14ae8e62013-10-25 02:22:21 +000047 if (isGCNOFile(Format)) {
48 while (true) {
49 GCOVFunction *GFun = new GCOVFunction();
50 if (!GFun->read(Buffer, Format))
51 break;
Devang Patele5a8f2f92011-09-29 17:06:40 +000052 Functions.push_back(GFun);
Devang Patel37140652011-09-28 18:50:00 +000053 }
Devang Patel37140652011-09-28 18:50:00 +000054 }
Yuchen Wu14ae8e62013-10-25 02:22:21 +000055 else if (isGCDAFile(Format)) {
56 for (size_t i = 0, e = Functions.size(); i < e; ++i) {
57 bool ReadGCDA = Functions[i]->read(Buffer, Format);
58 (void)ReadGCDA;
59 assert(ReadGCDA && ".gcda data does not match .gcno data");
60 }
61 while (Buffer.readProgramTag())
62 ++ProgramCount;
63 }
64
Devang Patel37140652011-09-28 18:50:00 +000065 return true;
66}
67
68/// dump - Dump GCOVFile content on standard out for debugging purposes.
69void GCOVFile::dump() {
Craig Topperaf0dea12013-07-04 01:31:24 +000070 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +000071 E = Functions.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +000072 (*I)->dump();
73}
74
75/// collectLineCounts - Collect line counts. This must be used after
76/// reading .gcno and .gcda files.
77void GCOVFile::collectLineCounts(FileInfo &FI) {
Craig Topperaf0dea12013-07-04 01:31:24 +000078 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +000079 E = Functions.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +000080 (*I)->collectLineCounts(FI);
Yuchen Wu14ae8e62013-10-25 02:22:21 +000081 FI.setProgramCount(ProgramCount);
Devang Patel37140652011-09-28 18:50:00 +000082}
83
84//===----------------------------------------------------------------------===//
85// GCOVFunction implementation.
86
87/// ~GCOVFunction - Delete GCOVFunction and its content.
88GCOVFunction::~GCOVFunction() {
89 DeleteContainerPointers(Blocks);
90}
91
Bob Wilson00928bc2013-10-22 19:54:32 +000092/// read - Read a function from the buffer. Return false if buffer cursor
Devang Patel37140652011-09-28 18:50:00 +000093/// does not point to a function tag.
Bill Wendling6bbe4892012-08-31 17:31:28 +000094bool GCOVFunction::read(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
Devang Patel37140652011-09-28 18:50:00 +000095 if (!Buff.readFunctionTag())
96 return false;
97
98 Buff.readInt(); // Function header length
99 Ident = Buff.readInt();
100 Buff.readInt(); // Checksum #1
Bill Wendlingc4325032013-06-25 18:13:52 +0000101 if (Format != GCOV::GCNO_402 && Format != GCOV::GCDA_402)
Devang Patel37140652011-09-28 18:50:00 +0000102 Buff.readInt(); // Checksum #2
103
104 Name = Buff.readString();
Bill Wendling6bbe4892012-08-31 17:31:28 +0000105 if (Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404)
Devang Patel37140652011-09-28 18:50:00 +0000106 Filename = Buff.readString();
107
Bill Wendling6bbe4892012-08-31 17:31:28 +0000108 if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) {
Devang Patel37140652011-09-28 18:50:00 +0000109 Buff.readArcTag();
Yuchen Wu887c20f2013-10-24 01:51:04 +0000110 uint32_t i = 0;
Devang Patel37140652011-09-28 18:50:00 +0000111 uint32_t Count = Buff.readInt() / 2;
Yuchen Wu887c20f2013-10-24 01:51:04 +0000112
113 // This for loop adds the counts for each block. A second nested loop is
114 // required to combine the edge counts that are contained in the GCDA file.
115 for (uint32_t Line = 0; i < Count; ++Line) {
116 GCOVBlock &Block = *Blocks[Line];
117 for (size_t Edge = 0, End = Block.getNumEdges(); Edge < End; ++Edge) {
118 assert(i < Count && "Unexpected number of Edges!");
119 Block.addCount(Buff.readInt64());
120 ++i;
121 }
Devang Patel37140652011-09-28 18:50:00 +0000122 }
Chad Rosier5dfe6da2012-02-22 17:25:00 +0000123 return true;
Devang Patel37140652011-09-28 18:50:00 +0000124 }
125
126 LineNumber = Buff.readInt();
127
128 // read blocks.
Nick Lewyckyc6b4f032012-09-23 03:58:21 +0000129 bool BlockTagFound = Buff.readBlockTag();
130 (void)BlockTagFound;
131 assert(BlockTagFound && "Block Tag not found!");
Devang Patel37140652011-09-28 18:50:00 +0000132 uint32_t BlockCount = Buff.readInt();
Bob Wilson868e6e32013-10-22 20:02:36 +0000133 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
Devang Patel37140652011-09-28 18:50:00 +0000134 Buff.readInt(); // Block flags;
135 Blocks.push_back(new GCOVBlock(i));
136 }
137
138 // read edges.
139 while (Buff.readEdgeTag()) {
140 uint32_t EdgeCount = (Buff.readInt() - 1) / 2;
141 uint32_t BlockNo = Buff.readInt();
Nick Lewyckyc6b4f032012-09-23 03:58:21 +0000142 assert(BlockNo < BlockCount && "Unexpected Block number!");
Bob Wilson868e6e32013-10-22 20:02:36 +0000143 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
Devang Patel37140652011-09-28 18:50:00 +0000144 Blocks[BlockNo]->addEdge(Buff.readInt());
145 Buff.readInt(); // Edge flag
146 }
147 }
148
149 // read line table.
150 while (Buff.readLineTag()) {
151 uint32_t LineTableLength = Buff.readInt();
Bob Wilson00928bc2013-10-22 19:54:32 +0000152 uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
Devang Patel37140652011-09-28 18:50:00 +0000153 uint32_t BlockNo = Buff.readInt();
Nick Lewyckyc6b4f032012-09-23 03:58:21 +0000154 assert(BlockNo < BlockCount && "Unexpected Block number!");
Devang Patel37140652011-09-28 18:50:00 +0000155 GCOVBlock *Block = Blocks[BlockNo];
156 Buff.readInt(); // flag
Bob Wilson00928bc2013-10-22 19:54:32 +0000157 while (Buff.getCursor() != (EndPos - 4)) {
Devang Patel37140652011-09-28 18:50:00 +0000158 StringRef Filename = Buff.readString();
Bob Wilson00928bc2013-10-22 19:54:32 +0000159 if (Buff.getCursor() == (EndPos - 4)) break;
Devang Patel37140652011-09-28 18:50:00 +0000160 while (uint32_t L = Buff.readInt())
Bill Wendlingea6397f2012-07-19 00:11:40 +0000161 Block->addLine(Filename, L);
Devang Patel37140652011-09-28 18:50:00 +0000162 }
163 Buff.readInt(); // flag
164 }
165 return true;
166}
167
168/// dump - Dump GCOVFunction content on standard out for debugging purposes.
169void GCOVFunction::dump() {
170 outs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
Craig Topperaf0dea12013-07-04 01:31:24 +0000171 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000172 E = Blocks.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000173 (*I)->dump();
174}
175
176/// collectLineCounts - Collect line counts. This must be used after
177/// reading .gcno and .gcda files.
178void GCOVFunction::collectLineCounts(FileInfo &FI) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000179 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000180 E = Blocks.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000181 (*I)->collectLineCounts(FI);
182}
183
184//===----------------------------------------------------------------------===//
185// GCOVBlock implementation.
186
187/// ~GCOVBlock - Delete GCOVBlock and its content.
188GCOVBlock::~GCOVBlock() {
189 Edges.clear();
190 DeleteContainerSeconds(Lines);
191}
192
193void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) {
194 GCOVLines *&LinesForFile = Lines[Filename];
195 if (!LinesForFile)
196 LinesForFile = new GCOVLines();
197 LinesForFile->add(LineNo);
198}
199
200/// collectLineCounts - Collect line counts. This must be used after
201/// reading .gcno and .gcda files.
202void GCOVBlock::collectLineCounts(FileInfo &FI) {
203 for (StringMap<GCOVLines *>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000204 E = Lines.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000205 I->second->collectLineCounts(FI, I->first(), Counter);
206}
207
208/// dump - Dump GCOVBlock content on standard out for debugging purposes.
209void GCOVBlock::dump() {
210 outs() << "Block : " << Number << " Counter : " << Counter << "\n";
211 if (!Edges.empty()) {
212 outs() << "\tEdges : ";
Craig Topperaf0dea12013-07-04 01:31:24 +0000213 for (SmallVectorImpl<uint32_t>::iterator I = Edges.begin(), E = Edges.end();
Bill Wendlingea6397f2012-07-19 00:11:40 +0000214 I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000215 outs() << (*I) << ",";
216 outs() << "\n";
217 }
218 if (!Lines.empty()) {
219 outs() << "\tLines : ";
220 for (StringMap<GCOVLines *>::iterator LI = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000221 LE = Lines.end(); LI != LE; ++LI) {
Devang Patel37140652011-09-28 18:50:00 +0000222 outs() << LI->first() << " -> ";
223 LI->second->dump();
224 outs() << "\n";
225 }
226 }
227}
228
229//===----------------------------------------------------------------------===//
230// GCOVLines implementation.
231
232/// collectLineCounts - Collect line counts. This must be used after
233/// reading .gcno and .gcda files.
234void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename,
Bob Wilson68bf30a2013-10-22 17:43:47 +0000235 uint64_t Count) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000236 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000237 E = Lines.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000238 FI.addLineCount(Filename, *I, Count);
239}
240
241/// dump - Dump GCOVLines content on standard out for debugging purposes.
242void GCOVLines::dump() {
Craig Topperaf0dea12013-07-04 01:31:24 +0000243 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000244 E = Lines.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000245 outs() << (*I) << ",";
246}
247
248//===----------------------------------------------------------------------===//
249// FileInfo implementation.
250
Devang Patel37140652011-09-28 18:50:00 +0000251/// print - Print source files with collected line count information.
Bob Wilson3461bed2013-10-22 05:09:41 +0000252void FileInfo::print(StringRef gcnoFile, StringRef gcdaFile) {
Devang Patel37140652011-09-28 18:50:00 +0000253 for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end();
254 I != E; ++I) {
255 StringRef Filename = I->first();
Bob Wilson3461bed2013-10-22 05:09:41 +0000256 outs() << " -: 0:Source:" << Filename << "\n";
257 outs() << " -: 0:Graph:" << gcnoFile << "\n";
258 outs() << " -: 0:Data:" << gcdaFile << "\n";
Yuchen Wu14ae8e62013-10-25 02:22:21 +0000259 outs() << " -: 0:Programs:" << ProgramCount << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000260 LineCounts &L = LineInfo[Filename];
261 OwningPtr<MemoryBuffer> Buff;
262 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
263 errs() << Filename << ": " << ec.message() << "\n";
264 return;
265 }
266 StringRef AllLines = Buff.take()->getBuffer();
Yuchen Wu48342ee2013-10-23 19:45:03 +0000267 uint32_t i = 0;
268 while (!AllLines.empty()) {
269 if (L.find(i) != L.end()) {
270 if (L[i] == 0)
271 outs() << " #####:";
272 else
273 outs() << format("%9lu:", L[i]);
274 } else {
Bob Wilson3461bed2013-10-22 05:09:41 +0000275 outs() << " -:";
Yuchen Wu48342ee2013-10-23 19:45:03 +0000276 }
Devang Patel37140652011-09-28 18:50:00 +0000277 std::pair<StringRef, StringRef> P = AllLines.split('\n');
278 if (AllLines != P.first)
Bob Wilson3461bed2013-10-22 05:09:41 +0000279 outs() << format("%5u:", i+1) << P.first;
Devang Patel37140652011-09-28 18:50:00 +0000280 outs() << "\n";
281 AllLines = P.second;
Yuchen Wu48342ee2013-10-23 19:45:03 +0000282 ++i;
Devang Patel37140652011-09-28 18:50:00 +0000283 }
284 }
285}
286