blob: 794ead9e71fbd686da118a4ac4142d56981843ca [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) {
50 GCOVFunction *GFun = new GCOVFunction();
51 if (!GFun->read(Buffer, Format))
52 break;
Devang Patele5a8f2f92011-09-29 17:06:40 +000053 Functions.push_back(GFun);
Devang Patel37140652011-09-28 18:50:00 +000054 }
Devang Patel37140652011-09-28 18:50:00 +000055 }
Yuchen Wu14ae8e62013-10-25 02:22:21 +000056 else if (isGCDAFile(Format)) {
57 for (size_t i = 0, e = Functions.size(); i < e; ++i) {
58 bool ReadGCDA = Functions[i]->read(Buffer, Format);
59 (void)ReadGCDA;
60 assert(ReadGCDA && ".gcda data does not match .gcno data");
61 }
62 while (Buffer.readProgramTag())
63 ++ProgramCount;
64 }
65
Devang Patel37140652011-09-28 18:50:00 +000066 return true;
67}
68
Yuchen Wu03678152013-10-25 02:22:24 +000069/// dump - Dump GCOVFile content to dbgs() for debugging purposes.
Devang Patel37140652011-09-28 18:50:00 +000070void GCOVFile::dump() {
Craig Topperaf0dea12013-07-04 01:31:24 +000071 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +000072 E = Functions.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +000073 (*I)->dump();
74}
75
76/// collectLineCounts - Collect line counts. This must be used after
77/// reading .gcno and .gcda files.
78void GCOVFile::collectLineCounts(FileInfo &FI) {
Craig Topperaf0dea12013-07-04 01:31:24 +000079 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +000080 E = Functions.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +000081 (*I)->collectLineCounts(FI);
Yuchen Wu14ae8e62013-10-25 02:22:21 +000082 FI.setProgramCount(ProgramCount);
Devang Patel37140652011-09-28 18:50:00 +000083}
84
85//===----------------------------------------------------------------------===//
86// GCOVFunction implementation.
87
88/// ~GCOVFunction - Delete GCOVFunction and its content.
89GCOVFunction::~GCOVFunction() {
90 DeleteContainerPointers(Blocks);
91}
92
Bob Wilson00928bc2013-10-22 19:54:32 +000093/// read - Read a function from the buffer. Return false if buffer cursor
Devang Patel37140652011-09-28 18:50:00 +000094/// does not point to a function tag.
Bill Wendling6bbe4892012-08-31 17:31:28 +000095bool GCOVFunction::read(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
Devang Patel37140652011-09-28 18:50:00 +000096 if (!Buff.readFunctionTag())
97 return false;
98
99 Buff.readInt(); // Function header length
100 Ident = Buff.readInt();
101 Buff.readInt(); // Checksum #1
Bill Wendlingc4325032013-06-25 18:13:52 +0000102 if (Format != GCOV::GCNO_402 && Format != GCOV::GCDA_402)
Devang Patel37140652011-09-28 18:50:00 +0000103 Buff.readInt(); // Checksum #2
104
105 Name = Buff.readString();
Bill Wendling6bbe4892012-08-31 17:31:28 +0000106 if (Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404)
Devang Patel37140652011-09-28 18:50:00 +0000107 Filename = Buff.readString();
108
Bill Wendling6bbe4892012-08-31 17:31:28 +0000109 if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) {
Devang Patel37140652011-09-28 18:50:00 +0000110 Buff.readArcTag();
Yuchen Wu887c20f2013-10-24 01:51:04 +0000111 uint32_t i = 0;
Devang Patel37140652011-09-28 18:50:00 +0000112 uint32_t Count = Buff.readInt() / 2;
Yuchen Wu887c20f2013-10-24 01:51:04 +0000113
114 // This for loop adds the counts for each block. A second nested loop is
115 // required to combine the edge counts that are contained in the GCDA file.
116 for (uint32_t Line = 0; i < Count; ++Line) {
117 GCOVBlock &Block = *Blocks[Line];
118 for (size_t Edge = 0, End = Block.getNumEdges(); Edge < End; ++Edge) {
119 assert(i < Count && "Unexpected number of Edges!");
120 Block.addCount(Buff.readInt64());
121 ++i;
122 }
Devang Patel37140652011-09-28 18:50:00 +0000123 }
Chad Rosier5dfe6da2012-02-22 17:25:00 +0000124 return true;
Devang Patel37140652011-09-28 18:50:00 +0000125 }
126
127 LineNumber = Buff.readInt();
128
129 // read blocks.
Nick Lewyckyc6b4f032012-09-23 03:58:21 +0000130 bool BlockTagFound = Buff.readBlockTag();
131 (void)BlockTagFound;
132 assert(BlockTagFound && "Block Tag not found!");
Devang Patel37140652011-09-28 18:50:00 +0000133 uint32_t BlockCount = Buff.readInt();
Bob Wilson868e6e32013-10-22 20:02:36 +0000134 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
Devang Patel37140652011-09-28 18:50:00 +0000135 Buff.readInt(); // Block flags;
136 Blocks.push_back(new GCOVBlock(i));
137 }
138
139 // read edges.
140 while (Buff.readEdgeTag()) {
141 uint32_t EdgeCount = (Buff.readInt() - 1) / 2;
142 uint32_t BlockNo = Buff.readInt();
Nick Lewyckyc6b4f032012-09-23 03:58:21 +0000143 assert(BlockNo < BlockCount && "Unexpected Block number!");
Bob Wilson868e6e32013-10-22 20:02:36 +0000144 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
Devang Patel37140652011-09-28 18:50:00 +0000145 Blocks[BlockNo]->addEdge(Buff.readInt());
146 Buff.readInt(); // Edge flag
147 }
148 }
149
150 // read line table.
151 while (Buff.readLineTag()) {
152 uint32_t LineTableLength = Buff.readInt();
Bob Wilson00928bc2013-10-22 19:54:32 +0000153 uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
Devang Patel37140652011-09-28 18:50:00 +0000154 uint32_t BlockNo = Buff.readInt();
Nick Lewyckyc6b4f032012-09-23 03:58:21 +0000155 assert(BlockNo < BlockCount && "Unexpected Block number!");
Devang Patel37140652011-09-28 18:50:00 +0000156 GCOVBlock *Block = Blocks[BlockNo];
157 Buff.readInt(); // flag
Bob Wilson00928bc2013-10-22 19:54:32 +0000158 while (Buff.getCursor() != (EndPos - 4)) {
Devang Patel37140652011-09-28 18:50:00 +0000159 StringRef Filename = Buff.readString();
Bob Wilson00928bc2013-10-22 19:54:32 +0000160 if (Buff.getCursor() == (EndPos - 4)) break;
Devang Patel37140652011-09-28 18:50:00 +0000161 while (uint32_t L = Buff.readInt())
Bill Wendlingea6397f2012-07-19 00:11:40 +0000162 Block->addLine(Filename, L);
Devang Patel37140652011-09-28 18:50:00 +0000163 }
164 Buff.readInt(); // flag
165 }
166 return true;
167}
168
Yuchen Wu03678152013-10-25 02:22:24 +0000169/// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
Devang Patel37140652011-09-28 18:50:00 +0000170void GCOVFunction::dump() {
Yuchen Wu03678152013-10-25 02:22:24 +0000171 dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
Craig Topperaf0dea12013-07-04 01:31:24 +0000172 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000173 E = Blocks.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000174 (*I)->dump();
175}
176
177/// collectLineCounts - Collect line counts. This must be used after
178/// reading .gcno and .gcda files.
179void GCOVFunction::collectLineCounts(FileInfo &FI) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000180 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000181 E = Blocks.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000182 (*I)->collectLineCounts(FI);
183}
184
185//===----------------------------------------------------------------------===//
186// GCOVBlock implementation.
187
188/// ~GCOVBlock - Delete GCOVBlock and its content.
189GCOVBlock::~GCOVBlock() {
190 Edges.clear();
191 DeleteContainerSeconds(Lines);
192}
193
194void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) {
195 GCOVLines *&LinesForFile = Lines[Filename];
196 if (!LinesForFile)
197 LinesForFile = new GCOVLines();
198 LinesForFile->add(LineNo);
199}
200
201/// collectLineCounts - Collect line counts. This must be used after
202/// reading .gcno and .gcda files.
203void GCOVBlock::collectLineCounts(FileInfo &FI) {
204 for (StringMap<GCOVLines *>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000205 E = Lines.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000206 I->second->collectLineCounts(FI, I->first(), Counter);
207}
208
Yuchen Wu03678152013-10-25 02:22:24 +0000209/// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
Devang Patel37140652011-09-28 18:50:00 +0000210void GCOVBlock::dump() {
Yuchen Wu03678152013-10-25 02:22:24 +0000211 dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000212 if (!Edges.empty()) {
Yuchen Wu03678152013-10-25 02:22:24 +0000213 dbgs() << "\tEdges : ";
Craig Topperaf0dea12013-07-04 01:31:24 +0000214 for (SmallVectorImpl<uint32_t>::iterator I = Edges.begin(), E = Edges.end();
Bill Wendlingea6397f2012-07-19 00:11:40 +0000215 I != E; ++I)
Yuchen Wu03678152013-10-25 02:22:24 +0000216 dbgs() << (*I) << ",";
217 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000218 }
219 if (!Lines.empty()) {
Yuchen Wu03678152013-10-25 02:22:24 +0000220 dbgs() << "\tLines : ";
Devang Patel37140652011-09-28 18:50:00 +0000221 for (StringMap<GCOVLines *>::iterator LI = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000222 LE = Lines.end(); LI != LE; ++LI) {
Yuchen Wu03678152013-10-25 02:22:24 +0000223 dbgs() << LI->first() << " -> ";
Devang Patel37140652011-09-28 18:50:00 +0000224 LI->second->dump();
Yuchen Wu03678152013-10-25 02:22:24 +0000225 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000226 }
227 }
228}
229
230//===----------------------------------------------------------------------===//
231// GCOVLines implementation.
232
233/// collectLineCounts - Collect line counts. This must be used after
234/// reading .gcno and .gcda files.
235void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename,
Bob Wilson68bf30a2013-10-22 17:43:47 +0000236 uint64_t Count) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000237 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000238 E = Lines.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000239 FI.addLineCount(Filename, *I, Count);
240}
241
Yuchen Wu03678152013-10-25 02:22:24 +0000242/// dump - Dump GCOVLines content to dbgs() for debugging purposes.
Devang Patel37140652011-09-28 18:50:00 +0000243void GCOVLines::dump() {
Craig Topperaf0dea12013-07-04 01:31:24 +0000244 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000245 E = Lines.end(); I != E; ++I)
Yuchen Wu03678152013-10-25 02:22:24 +0000246 dbgs() << (*I) << ",";
Devang Patel37140652011-09-28 18:50:00 +0000247}
248
249//===----------------------------------------------------------------------===//
250// FileInfo implementation.
251
Devang Patel37140652011-09-28 18:50:00 +0000252/// print - Print source files with collected line count information.
Yuchen Wudbcf1972013-11-02 00:09:17 +0000253void FileInfo::print(raw_fd_ostream &OS, StringRef gcnoFile,
254 StringRef gcdaFile) {
Devang Patel37140652011-09-28 18:50:00 +0000255 for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end();
256 I != E; ++I) {
257 StringRef Filename = I->first();
Yuchen Wudbcf1972013-11-02 00:09:17 +0000258 OS << " -: 0:Source:" << Filename << "\n";
259 OS << " -: 0:Graph:" << gcnoFile << "\n";
260 OS << " -: 0:Data:" << gcdaFile << "\n";
261 OS << " -: 0:Programs:" << ProgramCount << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000262 LineCounts &L = LineInfo[Filename];
263 OwningPtr<MemoryBuffer> Buff;
264 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
265 errs() << Filename << ": " << ec.message() << "\n";
266 return;
267 }
268 StringRef AllLines = Buff.take()->getBuffer();
Yuchen Wu48342ee2013-10-23 19:45:03 +0000269 uint32_t i = 0;
270 while (!AllLines.empty()) {
271 if (L.find(i) != L.end()) {
272 if (L[i] == 0)
Yuchen Wudbcf1972013-11-02 00:09:17 +0000273 OS << " #####:";
Yuchen Wu48342ee2013-10-23 19:45:03 +0000274 else
Yuchen Wudbcf1972013-11-02 00:09:17 +0000275 OS << format("%9lu:", L[i]);
Yuchen Wu48342ee2013-10-23 19:45:03 +0000276 } else {
Yuchen Wudbcf1972013-11-02 00:09:17 +0000277 OS << " -:";
Yuchen Wu48342ee2013-10-23 19:45:03 +0000278 }
Devang Patel37140652011-09-28 18:50:00 +0000279 std::pair<StringRef, StringRef> P = AllLines.split('\n');
280 if (AllLines != P.first)
Yuchen Wudbcf1972013-11-02 00:09:17 +0000281 OS << format("%5u:", i+1) << P.first;
282 OS << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000283 AllLines = P.second;
Yuchen Wu48342ee2013-10-23 19:45:03 +0000284 ++i;
Devang Patel37140652011-09-28 18:50:00 +0000285 }
286 }
287}
288