blob: ba45d91224fa4f13c6fab959f342f0033ac570e8 [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
Yuchen Wu03678152013-10-25 02:22:24 +000015#include "llvm/Support/Debug.h"
Devang Patel8dfb6552011-10-04 17:24:48 +000016#include "llvm/Support/GCOV.h"
Devang Patel37140652011-09-28 18:50:00 +000017#include "llvm/ADT/OwningPtr.h"
Devang Patela9e8a252011-09-29 16:46:47 +000018#include "llvm/ADT/STLExtras.h"
Bob Wilson3461bed2013-10-22 05:09:41 +000019#include "llvm/Support/Format.h"
Devang Patel37140652011-09-28 18:50:00 +000020#include "llvm/Support/MemoryObject.h"
21#include "llvm/Support/system_error.h"
22using namespace llvm;
23
24//===----------------------------------------------------------------------===//
25// GCOVFile implementation.
26
27/// ~GCOVFile - Delete GCOVFile and its content.
28GCOVFile::~GCOVFile() {
29 DeleteContainerPointers(Functions);
30}
31
Devang Patele5a8f2f92011-09-29 17:06:40 +000032/// isGCDAFile - Return true if Format identifies a .gcda file.
Bill Wendling6bbe4892012-08-31 17:31:28 +000033static bool isGCDAFile(GCOV::GCOVFormat Format) {
34 return Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404;
Devang Patele5a8f2f92011-09-29 17:06:40 +000035}
36
37/// isGCNOFile - Return true if Format identifies a .gcno file.
Bill Wendling6bbe4892012-08-31 17:31:28 +000038static bool isGCNOFile(GCOV::GCOVFormat Format) {
39 return Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404;
Devang Patele5a8f2f92011-09-29 17:06:40 +000040}
41
Devang Patel37140652011-09-28 18:50:00 +000042/// read - Read GCOV buffer.
43bool GCOVFile::read(GCOVBuffer &Buffer) {
Bill Wendling6bbe4892012-08-31 17:31:28 +000044 GCOV::GCOVFormat Format = Buffer.readGCOVFormat();
45 if (Format == GCOV::InvalidGCOV)
Devang Patel37140652011-09-28 18:50:00 +000046 return false;
47
Yuchen Wu14ae8e62013-10-25 02:22:21 +000048 if (isGCNOFile(Format)) {
49 while (true) {
Yuchen Wue28da842013-11-14 00:07:15 +000050 if (!Buffer.readFunctionTag()) break;
Yuchen Wu14ae8e62013-10-25 02:22:21 +000051 GCOVFunction *GFun = new GCOVFunction();
52 if (!GFun->read(Buffer, Format))
Yuchen Wue28da842013-11-14 00:07:15 +000053 return false;
Devang Patele5a8f2f92011-09-29 17:06:40 +000054 Functions.push_back(GFun);
Devang Patel37140652011-09-28 18:50:00 +000055 }
Devang Patel37140652011-09-28 18:50:00 +000056 }
Yuchen Wu14ae8e62013-10-25 02:22:21 +000057 else if (isGCDAFile(Format)) {
58 for (size_t i = 0, e = Functions.size(); i < e; ++i) {
Yuchen Wue28da842013-11-14 00:07:15 +000059 if (!Buffer.readFunctionTag()) {
60 errs() << "Unexpected number of functions.\n";
61 return false;
62 }
63 if (!Functions[i]->read(Buffer, Format))
64 return false;
Yuchen Wu14ae8e62013-10-25 02:22:21 +000065 }
Yuchen Wu30672d92013-11-05 01:11:58 +000066 if (Buffer.readObjectTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +000067 uint32_t Length;
68 uint32_t Dummy;
69 if (!Buffer.readInt(Length)) return false;
70 if (!Buffer.readInt(Dummy)) return false; // checksum
71 if (!Buffer.readInt(Dummy)) return false; // num
72 if (!Buffer.readInt(RunCount)) return false;;
Yuchen Wu30672d92013-11-05 01:11:58 +000073 Buffer.advanceCursor(Length-3);
74 }
75 while (Buffer.readProgramTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +000076 uint32_t Length;
77 if (!Buffer.readInt(Length)) return false;
Yuchen Wu30672d92013-11-05 01:11:58 +000078 Buffer.advanceCursor(Length);
Yuchen Wu14ae8e62013-10-25 02:22:21 +000079 ++ProgramCount;
Yuchen Wu30672d92013-11-05 01:11:58 +000080 }
Yuchen Wu14ae8e62013-10-25 02:22:21 +000081 }
82
Devang Patel37140652011-09-28 18:50:00 +000083 return true;
84}
85
Yuchen Wu03678152013-10-25 02:22:24 +000086/// dump - Dump GCOVFile content to dbgs() for debugging purposes.
Devang Patel37140652011-09-28 18:50:00 +000087void GCOVFile::dump() {
Craig Topperaf0dea12013-07-04 01:31:24 +000088 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +000089 E = Functions.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +000090 (*I)->dump();
91}
92
93/// collectLineCounts - Collect line counts. This must be used after
94/// reading .gcno and .gcda files.
95void GCOVFile::collectLineCounts(FileInfo &FI) {
Craig Topperaf0dea12013-07-04 01:31:24 +000096 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +000097 E = Functions.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +000098 (*I)->collectLineCounts(FI);
Yuchen Wu30672d92013-11-05 01:11:58 +000099 FI.setRunCount(RunCount);
Yuchen Wu14ae8e62013-10-25 02:22:21 +0000100 FI.setProgramCount(ProgramCount);
Devang Patel37140652011-09-28 18:50:00 +0000101}
102
103//===----------------------------------------------------------------------===//
104// GCOVFunction implementation.
105
106/// ~GCOVFunction - Delete GCOVFunction and its content.
107GCOVFunction::~GCOVFunction() {
108 DeleteContainerPointers(Blocks);
109}
110
Bob Wilson00928bc2013-10-22 19:54:32 +0000111/// read - Read a function from the buffer. Return false if buffer cursor
Devang Patel37140652011-09-28 18:50:00 +0000112/// does not point to a function tag.
Bill Wendling6bbe4892012-08-31 17:31:28 +0000113bool GCOVFunction::read(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
Yuchen Wue28da842013-11-14 00:07:15 +0000114 uint32_t Dummy;
115 if (!Buff.readInt(Dummy)) return false; // Function header length
116 if (!Buff.readInt(Ident)) return false;
117 if (!Buff.readInt(Dummy)) return false; // Checksum #1
Bill Wendlingc4325032013-06-25 18:13:52 +0000118 if (Format != GCOV::GCNO_402 && Format != GCOV::GCDA_402)
Yuchen Wue28da842013-11-14 00:07:15 +0000119 if (!Buff.readInt(Dummy)) return false; // Checksum #2
Devang Patel37140652011-09-28 18:50:00 +0000120
Yuchen Wue28da842013-11-14 00:07:15 +0000121 if (!Buff.readString(Name)) return false;
122
Bill Wendling6bbe4892012-08-31 17:31:28 +0000123 if (Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404)
Yuchen Wue28da842013-11-14 00:07:15 +0000124 if (!Buff.readString(Filename)) return false;
Devang Patel37140652011-09-28 18:50:00 +0000125
Bill Wendling6bbe4892012-08-31 17:31:28 +0000126 if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) {
Yuchen Wue28da842013-11-14 00:07:15 +0000127 if (!Buff.readArcTag()) {
128 errs() << "Arc tag not found.\n";
129 return false;
130 }
131 uint32_t Count;
132 if (!Buff.readInt(Count)) return false;
133 Count /= 2;
Yuchen Wu887c20f2013-10-24 01:51:04 +0000134
135 // This for loop adds the counts for each block. A second nested loop is
136 // required to combine the edge counts that are contained in the GCDA file.
Yuchen Wue28da842013-11-14 00:07:15 +0000137 for (uint32_t Line = 0; Count > 0; ++Line) {
Yuchen Wu887c20f2013-10-24 01:51:04 +0000138 GCOVBlock &Block = *Blocks[Line];
139 for (size_t Edge = 0, End = Block.getNumEdges(); Edge < End; ++Edge) {
Yuchen Wue28da842013-11-14 00:07:15 +0000140 if (Count == 0) {
141 errs() << "Unexpected number of edges.\n";
142 return false;
143 }
144 uint64_t ArcCount;
145 if (!Buff.readInt64(ArcCount)) return false;
146 Block.addCount(ArcCount);
147 --Count;
Yuchen Wu887c20f2013-10-24 01:51:04 +0000148 }
Devang Patel37140652011-09-28 18:50:00 +0000149 }
Chad Rosier5dfe6da2012-02-22 17:25:00 +0000150 return true;
Devang Patel37140652011-09-28 18:50:00 +0000151 }
152
Yuchen Wue28da842013-11-14 00:07:15 +0000153 if (!Buff.readInt(LineNumber)) return false;
Devang Patel37140652011-09-28 18:50:00 +0000154
155 // read blocks.
Yuchen Wue28da842013-11-14 00:07:15 +0000156 if (!Buff.readBlockTag()) {
157 errs() << "Block tag not found.\n";
158 return false;
159 }
160 uint32_t BlockCount;
161 if (!Buff.readInt(BlockCount)) return false;
Bob Wilson868e6e32013-10-22 20:02:36 +0000162 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
Yuchen Wue28da842013-11-14 00:07:15 +0000163 if (!Buff.readInt(Dummy)) return false; // Block flags;
Devang Patel37140652011-09-28 18:50:00 +0000164 Blocks.push_back(new GCOVBlock(i));
165 }
166
167 // read edges.
168 while (Buff.readEdgeTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +0000169 uint32_t EdgeCount;
170 if (!Buff.readInt(EdgeCount)) return false;
171 EdgeCount = (EdgeCount - 1) / 2;
172 uint32_t BlockNo;
173 if (!Buff.readInt(BlockNo)) return false;
174 if (BlockNo >= BlockCount) {
175 errs() << "Unexpected block number.\n";
176 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;
180 if (!Buff.readInt(Dst)) return false;
181 Blocks[BlockNo]->addEdge(Dst);
182 if (!Buff.readInt(Dummy)) return false; // Edge flag
Devang Patel37140652011-09-28 18:50:00 +0000183 }
184 }
185
186 // read line table.
187 while (Buff.readLineTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +0000188 uint32_t LineTableLength;
189 if (!Buff.readInt(LineTableLength)) return false;
Bob Wilson00928bc2013-10-22 19:54:32 +0000190 uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
Yuchen Wue28da842013-11-14 00:07:15 +0000191 uint32_t BlockNo;
192 if (!Buff.readInt(BlockNo)) return false;
193 if (BlockNo >= BlockCount) {
194 errs() << "Unexpected block number.\n";
195 return false;
Devang Patel37140652011-09-28 18:50:00 +0000196 }
Yuchen Wue28da842013-11-14 00:07:15 +0000197 GCOVBlock *Block = Blocks[BlockNo];
198 if (!Buff.readInt(Dummy)) return false; // flag
199 while (Buff.getCursor() != (EndPos - 4)) {
200 StringRef Filename;
201 if (!Buff.readString(Filename)) return false;
202 if (Buff.getCursor() == (EndPos - 4)) break;
203 while (true) {
204 uint32_t Line;
205 if (!Buff.readInt(Line)) return false;
206 if (!Line) break;
207 Block->addLine(Filename, Line);
208 }
209 }
210 if (!Buff.readInt(Dummy)) return false; // flag
Devang Patel37140652011-09-28 18:50:00 +0000211 }
212 return true;
213}
214
Yuchen Wu03678152013-10-25 02:22:24 +0000215/// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
Devang Patel37140652011-09-28 18:50:00 +0000216void GCOVFunction::dump() {
Yuchen Wu03678152013-10-25 02:22:24 +0000217 dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
Craig Topperaf0dea12013-07-04 01:31:24 +0000218 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000219 E = Blocks.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000220 (*I)->dump();
221}
222
223/// collectLineCounts - Collect line counts. This must be used after
224/// reading .gcno and .gcda files.
225void GCOVFunction::collectLineCounts(FileInfo &FI) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000226 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000227 E = Blocks.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000228 (*I)->collectLineCounts(FI);
229}
230
231//===----------------------------------------------------------------------===//
232// GCOVBlock implementation.
233
234/// ~GCOVBlock - Delete GCOVBlock and its content.
235GCOVBlock::~GCOVBlock() {
236 Edges.clear();
237 DeleteContainerSeconds(Lines);
238}
239
240void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) {
241 GCOVLines *&LinesForFile = Lines[Filename];
242 if (!LinesForFile)
243 LinesForFile = new GCOVLines();
244 LinesForFile->add(LineNo);
245}
246
247/// collectLineCounts - Collect line counts. This must be used after
248/// reading .gcno and .gcda files.
249void GCOVBlock::collectLineCounts(FileInfo &FI) {
250 for (StringMap<GCOVLines *>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000251 E = Lines.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000252 I->second->collectLineCounts(FI, I->first(), Counter);
253}
254
Yuchen Wu03678152013-10-25 02:22:24 +0000255/// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
Devang Patel37140652011-09-28 18:50:00 +0000256void GCOVBlock::dump() {
Yuchen Wu03678152013-10-25 02:22:24 +0000257 dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000258 if (!Edges.empty()) {
Yuchen Wu03678152013-10-25 02:22:24 +0000259 dbgs() << "\tEdges : ";
Craig Topperaf0dea12013-07-04 01:31:24 +0000260 for (SmallVectorImpl<uint32_t>::iterator I = Edges.begin(), E = Edges.end();
Bill Wendlingea6397f2012-07-19 00:11:40 +0000261 I != E; ++I)
Yuchen Wu03678152013-10-25 02:22:24 +0000262 dbgs() << (*I) << ",";
263 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000264 }
265 if (!Lines.empty()) {
Yuchen Wu03678152013-10-25 02:22:24 +0000266 dbgs() << "\tLines : ";
Devang Patel37140652011-09-28 18:50:00 +0000267 for (StringMap<GCOVLines *>::iterator LI = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000268 LE = Lines.end(); LI != LE; ++LI) {
Yuchen Wu03678152013-10-25 02:22:24 +0000269 dbgs() << LI->first() << " -> ";
Devang Patel37140652011-09-28 18:50:00 +0000270 LI->second->dump();
Yuchen Wu03678152013-10-25 02:22:24 +0000271 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000272 }
273 }
274}
275
276//===----------------------------------------------------------------------===//
277// GCOVLines implementation.
278
279/// collectLineCounts - Collect line counts. This must be used after
280/// reading .gcno and .gcda files.
281void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename,
Bob Wilson68bf30a2013-10-22 17:43:47 +0000282 uint64_t Count) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000283 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000284 E = Lines.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000285 FI.addLineCount(Filename, *I, Count);
286}
287
Yuchen Wu03678152013-10-25 02:22:24 +0000288/// dump - Dump GCOVLines content to dbgs() for debugging purposes.
Devang Patel37140652011-09-28 18:50:00 +0000289void GCOVLines::dump() {
Craig Topperaf0dea12013-07-04 01:31:24 +0000290 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000291 E = Lines.end(); I != E; ++I)
Yuchen Wu03678152013-10-25 02:22:24 +0000292 dbgs() << (*I) << ",";
Devang Patel37140652011-09-28 18:50:00 +0000293}
294
295//===----------------------------------------------------------------------===//
296// FileInfo implementation.
297
Devang Patel37140652011-09-28 18:50:00 +0000298/// print - Print source files with collected line count information.
Yuchen Wudbcf1972013-11-02 00:09:17 +0000299void FileInfo::print(raw_fd_ostream &OS, StringRef gcnoFile,
300 StringRef gcdaFile) {
Devang Patel37140652011-09-28 18:50:00 +0000301 for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end();
302 I != E; ++I) {
303 StringRef Filename = I->first();
Yuchen Wue68c5f82013-11-05 01:56:29 +0000304 OS << " -: 0:Source:" << Filename << "\n";
305 OS << " -: 0:Graph:" << gcnoFile << "\n";
306 OS << " -: 0:Data:" << gcdaFile << "\n";
307 OS << " -: 0:Runs:" << RunCount << "\n";
308 OS << " -: 0:Programs:" << ProgramCount << "\n";
309 LineCounts &L = LineInfo[Filename];
Devang Patel37140652011-09-28 18:50:00 +0000310 OwningPtr<MemoryBuffer> Buff;
311 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
312 errs() << Filename << ": " << ec.message() << "\n";
313 return;
314 }
315 StringRef AllLines = Buff.take()->getBuffer();
Yuchen Wu48342ee2013-10-23 19:45:03 +0000316 uint32_t i = 0;
317 while (!AllLines.empty()) {
318 if (L.find(i) != L.end()) {
319 if (L[i] == 0)
Yuchen Wudbcf1972013-11-02 00:09:17 +0000320 OS << " #####:";
Yuchen Wu48342ee2013-10-23 19:45:03 +0000321 else
Yuchen Wudbcf1972013-11-02 00:09:17 +0000322 OS << format("%9lu:", L[i]);
Yuchen Wu48342ee2013-10-23 19:45:03 +0000323 } else {
Yuchen Wudbcf1972013-11-02 00:09:17 +0000324 OS << " -:";
Yuchen Wu48342ee2013-10-23 19:45:03 +0000325 }
Devang Patel37140652011-09-28 18:50:00 +0000326 std::pair<StringRef, StringRef> P = AllLines.split('\n');
327 if (AllLines != P.first)
Yuchen Wudbcf1972013-11-02 00:09:17 +0000328 OS << format("%5u:", i+1) << P.first;
329 OS << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000330 AllLines = P.second;
Yuchen Wu48342ee2013-10-23 19:45:03 +0000331 ++i;
Devang Patel37140652011-09-28 18:50:00 +0000332 }
333 }
334}
335