blob: 908523a3a66515ad3609816904465245c57d2530 [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
47 unsigned i = 0;
Devang Patela9e8a252011-09-29 16:46:47 +000048 while (1) {
Devang Patel37140652011-09-28 18:50:00 +000049 GCOVFunction *GFun = NULL;
Devang Patele5a8f2f92011-09-29 17:06:40 +000050 if (isGCDAFile(Format)) {
51 // Use existing function while reading .gcda file.
Nick Lewyckyc6b4f032012-09-23 03:58:21 +000052 assert(i < Functions.size() && ".gcda data does not match .gcno data");
Devang Patele5a8f2f92011-09-29 17:06:40 +000053 GFun = Functions[i];
Yuchen Wufce33962013-10-23 03:41:03 +000054 } else if (isGCNOFile(Format)) {
Devang Patel37140652011-09-28 18:50:00 +000055 GFun = new GCOVFunction();
Devang Patele5a8f2f92011-09-29 17:06:40 +000056 Functions.push_back(GFun);
Devang Patel37140652011-09-28 18:50:00 +000057 }
Devang Patele5a8f2f92011-09-29 17:06:40 +000058 if (!GFun || !GFun->read(Buffer, Format))
Devang Patel37140652011-09-28 18:50:00 +000059 break;
Devang Patel37140652011-09-28 18:50:00 +000060 ++i;
61 }
62 return true;
63}
64
65/// dump - Dump GCOVFile content on standard out for debugging purposes.
66void GCOVFile::dump() {
Craig Topperaf0dea12013-07-04 01:31:24 +000067 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +000068 E = Functions.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +000069 (*I)->dump();
70}
71
72/// collectLineCounts - Collect line counts. This must be used after
73/// reading .gcno and .gcda files.
74void GCOVFile::collectLineCounts(FileInfo &FI) {
Craig Topperaf0dea12013-07-04 01:31:24 +000075 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +000076 E = Functions.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +000077 (*I)->collectLineCounts(FI);
Devang Patel37140652011-09-28 18:50:00 +000078}
79
80//===----------------------------------------------------------------------===//
81// GCOVFunction implementation.
82
83/// ~GCOVFunction - Delete GCOVFunction and its content.
84GCOVFunction::~GCOVFunction() {
85 DeleteContainerPointers(Blocks);
86}
87
Bob Wilson00928bc2013-10-22 19:54:32 +000088/// read - Read a function from the buffer. Return false if buffer cursor
Devang Patel37140652011-09-28 18:50:00 +000089/// does not point to a function tag.
Bill Wendling6bbe4892012-08-31 17:31:28 +000090bool GCOVFunction::read(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
Devang Patel37140652011-09-28 18:50:00 +000091 if (!Buff.readFunctionTag())
92 return false;
93
94 Buff.readInt(); // Function header length
95 Ident = Buff.readInt();
96 Buff.readInt(); // Checksum #1
Bill Wendlingc4325032013-06-25 18:13:52 +000097 if (Format != GCOV::GCNO_402 && Format != GCOV::GCDA_402)
Devang Patel37140652011-09-28 18:50:00 +000098 Buff.readInt(); // Checksum #2
99
100 Name = Buff.readString();
Bill Wendling6bbe4892012-08-31 17:31:28 +0000101 if (Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404)
Devang Patel37140652011-09-28 18:50:00 +0000102 Filename = Buff.readString();
103
Bill Wendling6bbe4892012-08-31 17:31:28 +0000104 if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) {
Devang Patel37140652011-09-28 18:50:00 +0000105 Buff.readArcTag();
Yuchen Wu887c20f2013-10-24 01:51:04 +0000106 uint32_t i = 0;
Devang Patel37140652011-09-28 18:50:00 +0000107 uint32_t Count = Buff.readInt() / 2;
Yuchen Wu887c20f2013-10-24 01:51:04 +0000108
109 // This for loop adds the counts for each block. A second nested loop is
110 // required to combine the edge counts that are contained in the GCDA file.
111 for (uint32_t Line = 0; i < Count; ++Line) {
112 GCOVBlock &Block = *Blocks[Line];
113 for (size_t Edge = 0, End = Block.getNumEdges(); Edge < End; ++Edge) {
114 assert(i < Count && "Unexpected number of Edges!");
115 Block.addCount(Buff.readInt64());
116 ++i;
117 }
Devang Patel37140652011-09-28 18:50:00 +0000118 }
Chad Rosier5dfe6da2012-02-22 17:25:00 +0000119 return true;
Devang Patel37140652011-09-28 18:50:00 +0000120 }
121
122 LineNumber = Buff.readInt();
123
124 // read blocks.
Nick Lewyckyc6b4f032012-09-23 03:58:21 +0000125 bool BlockTagFound = Buff.readBlockTag();
126 (void)BlockTagFound;
127 assert(BlockTagFound && "Block Tag not found!");
Devang Patel37140652011-09-28 18:50:00 +0000128 uint32_t BlockCount = Buff.readInt();
Bob Wilson868e6e32013-10-22 20:02:36 +0000129 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
Devang Patel37140652011-09-28 18:50:00 +0000130 Buff.readInt(); // Block flags;
131 Blocks.push_back(new GCOVBlock(i));
132 }
133
134 // read edges.
135 while (Buff.readEdgeTag()) {
136 uint32_t EdgeCount = (Buff.readInt() - 1) / 2;
137 uint32_t BlockNo = Buff.readInt();
Nick Lewyckyc6b4f032012-09-23 03:58:21 +0000138 assert(BlockNo < BlockCount && "Unexpected Block number!");
Bob Wilson868e6e32013-10-22 20:02:36 +0000139 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
Devang Patel37140652011-09-28 18:50:00 +0000140 Blocks[BlockNo]->addEdge(Buff.readInt());
141 Buff.readInt(); // Edge flag
142 }
143 }
144
145 // read line table.
146 while (Buff.readLineTag()) {
147 uint32_t LineTableLength = Buff.readInt();
Bob Wilson00928bc2013-10-22 19:54:32 +0000148 uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
Devang Patel37140652011-09-28 18:50:00 +0000149 uint32_t BlockNo = Buff.readInt();
Nick Lewyckyc6b4f032012-09-23 03:58:21 +0000150 assert(BlockNo < BlockCount && "Unexpected Block number!");
Devang Patel37140652011-09-28 18:50:00 +0000151 GCOVBlock *Block = Blocks[BlockNo];
152 Buff.readInt(); // flag
Bob Wilson00928bc2013-10-22 19:54:32 +0000153 while (Buff.getCursor() != (EndPos - 4)) {
Devang Patel37140652011-09-28 18:50:00 +0000154 StringRef Filename = Buff.readString();
Bob Wilson00928bc2013-10-22 19:54:32 +0000155 if (Buff.getCursor() == (EndPos - 4)) break;
Devang Patel37140652011-09-28 18:50:00 +0000156 while (uint32_t L = Buff.readInt())
Bill Wendlingea6397f2012-07-19 00:11:40 +0000157 Block->addLine(Filename, L);
Devang Patel37140652011-09-28 18:50:00 +0000158 }
159 Buff.readInt(); // flag
160 }
161 return true;
162}
163
164/// dump - Dump GCOVFunction content on standard out for debugging purposes.
165void GCOVFunction::dump() {
166 outs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
Craig Topperaf0dea12013-07-04 01:31:24 +0000167 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000168 E = Blocks.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000169 (*I)->dump();
170}
171
172/// collectLineCounts - Collect line counts. This must be used after
173/// reading .gcno and .gcda files.
174void GCOVFunction::collectLineCounts(FileInfo &FI) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000175 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000176 E = Blocks.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000177 (*I)->collectLineCounts(FI);
178}
179
180//===----------------------------------------------------------------------===//
181// GCOVBlock implementation.
182
183/// ~GCOVBlock - Delete GCOVBlock and its content.
184GCOVBlock::~GCOVBlock() {
185 Edges.clear();
186 DeleteContainerSeconds(Lines);
187}
188
189void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) {
190 GCOVLines *&LinesForFile = Lines[Filename];
191 if (!LinesForFile)
192 LinesForFile = new GCOVLines();
193 LinesForFile->add(LineNo);
194}
195
196/// collectLineCounts - Collect line counts. This must be used after
197/// reading .gcno and .gcda files.
198void GCOVBlock::collectLineCounts(FileInfo &FI) {
199 for (StringMap<GCOVLines *>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000200 E = Lines.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000201 I->second->collectLineCounts(FI, I->first(), Counter);
202}
203
204/// dump - Dump GCOVBlock content on standard out for debugging purposes.
205void GCOVBlock::dump() {
206 outs() << "Block : " << Number << " Counter : " << Counter << "\n";
207 if (!Edges.empty()) {
208 outs() << "\tEdges : ";
Craig Topperaf0dea12013-07-04 01:31:24 +0000209 for (SmallVectorImpl<uint32_t>::iterator I = Edges.begin(), E = Edges.end();
Bill Wendlingea6397f2012-07-19 00:11:40 +0000210 I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000211 outs() << (*I) << ",";
212 outs() << "\n";
213 }
214 if (!Lines.empty()) {
215 outs() << "\tLines : ";
216 for (StringMap<GCOVLines *>::iterator LI = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000217 LE = Lines.end(); LI != LE; ++LI) {
Devang Patel37140652011-09-28 18:50:00 +0000218 outs() << LI->first() << " -> ";
219 LI->second->dump();
220 outs() << "\n";
221 }
222 }
223}
224
225//===----------------------------------------------------------------------===//
226// GCOVLines implementation.
227
228/// collectLineCounts - Collect line counts. This must be used after
229/// reading .gcno and .gcda files.
230void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename,
Bob Wilson68bf30a2013-10-22 17:43:47 +0000231 uint64_t Count) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000232 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000233 E = Lines.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000234 FI.addLineCount(Filename, *I, Count);
235}
236
237/// dump - Dump GCOVLines content on standard out for debugging purposes.
238void GCOVLines::dump() {
Craig Topperaf0dea12013-07-04 01:31:24 +0000239 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000240 E = Lines.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000241 outs() << (*I) << ",";
242}
243
244//===----------------------------------------------------------------------===//
245// FileInfo implementation.
246
Devang Patel37140652011-09-28 18:50:00 +0000247/// print - Print source files with collected line count information.
Bob Wilson3461bed2013-10-22 05:09:41 +0000248void FileInfo::print(StringRef gcnoFile, StringRef gcdaFile) {
Devang Patel37140652011-09-28 18:50:00 +0000249 for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end();
250 I != E; ++I) {
251 StringRef Filename = I->first();
Bob Wilson3461bed2013-10-22 05:09:41 +0000252 outs() << " -: 0:Source:" << Filename << "\n";
253 outs() << " -: 0:Graph:" << gcnoFile << "\n";
254 outs() << " -: 0:Data:" << gcdaFile << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000255 LineCounts &L = LineInfo[Filename];
256 OwningPtr<MemoryBuffer> Buff;
257 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
258 errs() << Filename << ": " << ec.message() << "\n";
259 return;
260 }
261 StringRef AllLines = Buff.take()->getBuffer();
Yuchen Wu48342ee2013-10-23 19:45:03 +0000262 uint32_t i = 0;
263 while (!AllLines.empty()) {
264 if (L.find(i) != L.end()) {
265 if (L[i] == 0)
266 outs() << " #####:";
267 else
268 outs() << format("%9lu:", L[i]);
269 } else {
Bob Wilson3461bed2013-10-22 05:09:41 +0000270 outs() << " -:";
Yuchen Wu48342ee2013-10-23 19:45:03 +0000271 }
Devang Patel37140652011-09-28 18:50:00 +0000272 std::pair<StringRef, StringRef> P = AllLines.split('\n');
273 if (AllLines != P.first)
Bob Wilson3461bed2013-10-22 05:09:41 +0000274 outs() << format("%5u:", i+1) << P.first;
Devang Patel37140652011-09-28 18:50:00 +0000275 outs() << "\n";
276 AllLines = P.second;
Yuchen Wu48342ee2013-10-23 19:45:03 +0000277 ++i;
Devang Patel37140652011-09-28 18:50:00 +0000278 }
279 }
280}
281