blob: 68b91e6dc9a28f702ddf736aa33cec33d362f2da [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();
106 uint32_t Count = Buff.readInt() / 2;
107 for (unsigned i = 0, e = Count; i != e; ++i) {
108 Blocks[i]->addCount(Buff.readInt64());
109 }
Chad Rosier5dfe6da2012-02-22 17:25:00 +0000110 return true;
Devang Patel37140652011-09-28 18:50:00 +0000111 }
112
113 LineNumber = Buff.readInt();
114
115 // read blocks.
Nick Lewyckyc6b4f032012-09-23 03:58:21 +0000116 bool BlockTagFound = Buff.readBlockTag();
117 (void)BlockTagFound;
118 assert(BlockTagFound && "Block Tag not found!");
Devang Patel37140652011-09-28 18:50:00 +0000119 uint32_t BlockCount = Buff.readInt();
Bob Wilson868e6e32013-10-22 20:02:36 +0000120 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
Devang Patel37140652011-09-28 18:50:00 +0000121 Buff.readInt(); // Block flags;
122 Blocks.push_back(new GCOVBlock(i));
123 }
124
125 // read edges.
126 while (Buff.readEdgeTag()) {
127 uint32_t EdgeCount = (Buff.readInt() - 1) / 2;
128 uint32_t BlockNo = Buff.readInt();
Nick Lewyckyc6b4f032012-09-23 03:58:21 +0000129 assert(BlockNo < BlockCount && "Unexpected Block number!");
Bob Wilson868e6e32013-10-22 20:02:36 +0000130 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
Devang Patel37140652011-09-28 18:50:00 +0000131 Blocks[BlockNo]->addEdge(Buff.readInt());
132 Buff.readInt(); // Edge flag
133 }
134 }
135
136 // read line table.
137 while (Buff.readLineTag()) {
138 uint32_t LineTableLength = Buff.readInt();
Bob Wilson00928bc2013-10-22 19:54:32 +0000139 uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
Devang Patel37140652011-09-28 18:50:00 +0000140 uint32_t BlockNo = Buff.readInt();
Nick Lewyckyc6b4f032012-09-23 03:58:21 +0000141 assert(BlockNo < BlockCount && "Unexpected Block number!");
Devang Patel37140652011-09-28 18:50:00 +0000142 GCOVBlock *Block = Blocks[BlockNo];
143 Buff.readInt(); // flag
Bob Wilson00928bc2013-10-22 19:54:32 +0000144 while (Buff.getCursor() != (EndPos - 4)) {
Devang Patel37140652011-09-28 18:50:00 +0000145 StringRef Filename = Buff.readString();
Bob Wilson00928bc2013-10-22 19:54:32 +0000146 if (Buff.getCursor() == (EndPos - 4)) break;
Devang Patel37140652011-09-28 18:50:00 +0000147 while (uint32_t L = Buff.readInt())
Bill Wendlingea6397f2012-07-19 00:11:40 +0000148 Block->addLine(Filename, L);
Devang Patel37140652011-09-28 18:50:00 +0000149 }
150 Buff.readInt(); // flag
151 }
152 return true;
153}
154
155/// dump - Dump GCOVFunction content on standard out for debugging purposes.
156void GCOVFunction::dump() {
157 outs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
Craig Topperaf0dea12013-07-04 01:31:24 +0000158 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000159 E = Blocks.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000160 (*I)->dump();
161}
162
163/// collectLineCounts - Collect line counts. This must be used after
164/// reading .gcno and .gcda files.
165void GCOVFunction::collectLineCounts(FileInfo &FI) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000166 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000167 E = Blocks.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000168 (*I)->collectLineCounts(FI);
169}
170
171//===----------------------------------------------------------------------===//
172// GCOVBlock implementation.
173
174/// ~GCOVBlock - Delete GCOVBlock and its content.
175GCOVBlock::~GCOVBlock() {
176 Edges.clear();
177 DeleteContainerSeconds(Lines);
178}
179
180void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) {
181 GCOVLines *&LinesForFile = Lines[Filename];
182 if (!LinesForFile)
183 LinesForFile = new GCOVLines();
184 LinesForFile->add(LineNo);
185}
186
187/// collectLineCounts - Collect line counts. This must be used after
188/// reading .gcno and .gcda files.
189void GCOVBlock::collectLineCounts(FileInfo &FI) {
190 for (StringMap<GCOVLines *>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000191 E = Lines.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000192 I->second->collectLineCounts(FI, I->first(), Counter);
193}
194
195/// dump - Dump GCOVBlock content on standard out for debugging purposes.
196void GCOVBlock::dump() {
197 outs() << "Block : " << Number << " Counter : " << Counter << "\n";
198 if (!Edges.empty()) {
199 outs() << "\tEdges : ";
Craig Topperaf0dea12013-07-04 01:31:24 +0000200 for (SmallVectorImpl<uint32_t>::iterator I = Edges.begin(), E = Edges.end();
Bill Wendlingea6397f2012-07-19 00:11:40 +0000201 I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000202 outs() << (*I) << ",";
203 outs() << "\n";
204 }
205 if (!Lines.empty()) {
206 outs() << "\tLines : ";
207 for (StringMap<GCOVLines *>::iterator LI = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000208 LE = Lines.end(); LI != LE; ++LI) {
Devang Patel37140652011-09-28 18:50:00 +0000209 outs() << LI->first() << " -> ";
210 LI->second->dump();
211 outs() << "\n";
212 }
213 }
214}
215
216//===----------------------------------------------------------------------===//
217// GCOVLines implementation.
218
219/// collectLineCounts - Collect line counts. This must be used after
220/// reading .gcno and .gcda files.
221void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename,
Bob Wilson68bf30a2013-10-22 17:43:47 +0000222 uint64_t Count) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000223 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000224 E = Lines.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000225 FI.addLineCount(Filename, *I, Count);
226}
227
228/// dump - Dump GCOVLines content on standard out for debugging purposes.
229void GCOVLines::dump() {
Craig Topperaf0dea12013-07-04 01:31:24 +0000230 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000231 E = Lines.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000232 outs() << (*I) << ",";
233}
234
235//===----------------------------------------------------------------------===//
236// FileInfo implementation.
237
Devang Patel37140652011-09-28 18:50:00 +0000238/// print - Print source files with collected line count information.
Bob Wilson3461bed2013-10-22 05:09:41 +0000239void FileInfo::print(StringRef gcnoFile, StringRef gcdaFile) {
Devang Patel37140652011-09-28 18:50:00 +0000240 for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end();
241 I != E; ++I) {
242 StringRef Filename = I->first();
Bob Wilson3461bed2013-10-22 05:09:41 +0000243 outs() << " -: 0:Source:" << Filename << "\n";
244 outs() << " -: 0:Graph:" << gcnoFile << "\n";
245 outs() << " -: 0:Data:" << gcdaFile << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000246 LineCounts &L = LineInfo[Filename];
247 OwningPtr<MemoryBuffer> Buff;
248 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
249 errs() << Filename << ": " << ec.message() << "\n";
250 return;
251 }
252 StringRef AllLines = Buff.take()->getBuffer();
Yuchen Wu48342ee2013-10-23 19:45:03 +0000253 uint32_t i = 0;
254 while (!AllLines.empty()) {
255 if (L.find(i) != L.end()) {
256 if (L[i] == 0)
257 outs() << " #####:";
258 else
259 outs() << format("%9lu:", L[i]);
260 } else {
Bob Wilson3461bed2013-10-22 05:09:41 +0000261 outs() << " -:";
Yuchen Wu48342ee2013-10-23 19:45:03 +0000262 }
Devang Patel37140652011-09-28 18:50:00 +0000263 std::pair<StringRef, StringRef> P = AllLines.split('\n');
264 if (AllLines != P.first)
Bob Wilson3461bed2013-10-22 05:09:41 +0000265 outs() << format("%5u:", i+1) << P.first;
Devang Patel37140652011-09-28 18:50:00 +0000266 outs() << "\n";
267 AllLines = P.second;
Yuchen Wu48342ee2013-10-23 19:45:03 +0000268 ++i;
Devang Patel37140652011-09-28 18:50:00 +0000269 }
270 }
271}
272