blob: b8907d01b43deb7961ae979122f15cbb37d0cfcf [file] [log] [blame]
Devang Patel58c62002011-10-04 17:24:48 +00001//===- GCOVr.cpp - LLVM coverage tool -------------------------------------===//
Devang Pateld02c42b2011-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 Patel58c62002011-10-04 17:24:48 +000010// GCOV implements the interface to read and write coverage files that use
11// 'gcov' format.
Devang Pateld02c42b2011-09-28 18:50:00 +000012//
Devang Pateld02c42b2011-09-28 18:50:00 +000013//===----------------------------------------------------------------------===//
14
Devang Patel58c62002011-10-04 17:24:48 +000015#include "llvm/Support/GCOV.h"
Devang Pateld02c42b2011-09-28 18:50:00 +000016#include "llvm/ADT/OwningPtr.h"
Devang Patel7a502022011-09-29 16:46:47 +000017#include "llvm/ADT/STLExtras.h"
Devang Pateld02c42b2011-09-28 18:50:00 +000018#include "llvm/Support/MemoryObject.h"
19#include "llvm/Support/system_error.h"
20using namespace llvm;
21
22//===----------------------------------------------------------------------===//
23// GCOVFile implementation.
24
25/// ~GCOVFile - Delete GCOVFile and its content.
26GCOVFile::~GCOVFile() {
27 DeleteContainerPointers(Functions);
28}
29
Devang Patel0066f922011-09-29 17:06:40 +000030/// isGCDAFile - Return true if Format identifies a .gcda file.
Bill Wendlinge4fb6ea2012-08-31 17:31:28 +000031static bool isGCDAFile(GCOV::GCOVFormat Format) {
32 return Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404;
Devang Patel0066f922011-09-29 17:06:40 +000033}
34
35/// isGCNOFile - Return true if Format identifies a .gcno file.
Bill Wendlinge4fb6ea2012-08-31 17:31:28 +000036static bool isGCNOFile(GCOV::GCOVFormat Format) {
37 return Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404;
Devang Patel0066f922011-09-29 17:06:40 +000038}
39
Devang Pateld02c42b2011-09-28 18:50:00 +000040/// read - Read GCOV buffer.
41bool GCOVFile::read(GCOVBuffer &Buffer) {
Bill Wendlinge4fb6ea2012-08-31 17:31:28 +000042 GCOV::GCOVFormat Format = Buffer.readGCOVFormat();
43 if (Format == GCOV::InvalidGCOV)
Devang Pateld02c42b2011-09-28 18:50:00 +000044 return false;
45
46 unsigned i = 0;
Devang Patel7a502022011-09-29 16:46:47 +000047 while (1) {
Devang Pateld02c42b2011-09-28 18:50:00 +000048 GCOVFunction *GFun = NULL;
Devang Patel0066f922011-09-29 17:06:40 +000049 if (isGCDAFile(Format)) {
50 // Use existing function while reading .gcda file.
Nick Lewycky85aa4f62012-09-23 03:58:21 +000051 assert(i < Functions.size() && ".gcda data does not match .gcno data");
Devang Patel0066f922011-09-29 17:06:40 +000052 GFun = Functions[i];
53 } else if (isGCNOFile(Format)){
Devang Pateld02c42b2011-09-28 18:50:00 +000054 GFun = new GCOVFunction();
Devang Patel0066f922011-09-29 17:06:40 +000055 Functions.push_back(GFun);
Devang Pateld02c42b2011-09-28 18:50:00 +000056 }
Devang Patel0066f922011-09-29 17:06:40 +000057 if (!GFun || !GFun->read(Buffer, Format))
Devang Pateld02c42b2011-09-28 18:50:00 +000058 break;
Devang Pateld02c42b2011-09-28 18:50:00 +000059 ++i;
60 }
61 return true;
62}
63
64/// dump - Dump GCOVFile content on standard out for debugging purposes.
65void GCOVFile::dump() {
Craig Topper6227d5c2013-07-04 01:31:24 +000066 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
Bill Wendling56cb2292012-07-19 00:11:40 +000067 E = Functions.end(); I != E; ++I)
Devang Pateld02c42b2011-09-28 18:50:00 +000068 (*I)->dump();
69}
70
71/// collectLineCounts - Collect line counts. This must be used after
72/// reading .gcno and .gcda files.
73void GCOVFile::collectLineCounts(FileInfo &FI) {
Craig Topper6227d5c2013-07-04 01:31:24 +000074 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
Bill Wendling56cb2292012-07-19 00:11:40 +000075 E = Functions.end(); I != E; ++I)
Devang Pateld02c42b2011-09-28 18:50:00 +000076 (*I)->collectLineCounts(FI);
Devang Pateld02c42b2011-09-28 18:50:00 +000077}
78
79//===----------------------------------------------------------------------===//
80// GCOVFunction implementation.
81
82/// ~GCOVFunction - Delete GCOVFunction and its content.
83GCOVFunction::~GCOVFunction() {
84 DeleteContainerPointers(Blocks);
85}
86
87/// read - Read a aunction from the buffer. Return false if buffer cursor
88/// does not point to a function tag.
Bill Wendlinge4fb6ea2012-08-31 17:31:28 +000089bool GCOVFunction::read(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
Devang Pateld02c42b2011-09-28 18:50:00 +000090 if (!Buff.readFunctionTag())
91 return false;
92
93 Buff.readInt(); // Function header length
94 Ident = Buff.readInt();
95 Buff.readInt(); // Checksum #1
Bill Wendlinga37d96a2013-06-25 18:13:52 +000096 if (Format != GCOV::GCNO_402 && Format != GCOV::GCDA_402)
Devang Pateld02c42b2011-09-28 18:50:00 +000097 Buff.readInt(); // Checksum #2
98
99 Name = Buff.readString();
Bill Wendlinge4fb6ea2012-08-31 17:31:28 +0000100 if (Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404)
Devang Pateld02c42b2011-09-28 18:50:00 +0000101 Filename = Buff.readString();
102
Bill Wendlinge4fb6ea2012-08-31 17:31:28 +0000103 if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) {
Devang Pateld02c42b2011-09-28 18:50:00 +0000104 Buff.readArcTag();
105 uint32_t Count = Buff.readInt() / 2;
106 for (unsigned i = 0, e = Count; i != e; ++i) {
107 Blocks[i]->addCount(Buff.readInt64());
108 }
Chad Rosier90f20042012-02-22 17:25:00 +0000109 return true;
Devang Pateld02c42b2011-09-28 18:50:00 +0000110 }
111
112 LineNumber = Buff.readInt();
113
114 // read blocks.
Nick Lewycky85aa4f62012-09-23 03:58:21 +0000115 bool BlockTagFound = Buff.readBlockTag();
116 (void)BlockTagFound;
117 assert(BlockTagFound && "Block Tag not found!");
Devang Pateld02c42b2011-09-28 18:50:00 +0000118 uint32_t BlockCount = Buff.readInt();
119 for (int i = 0, e = BlockCount; i != e; ++i) {
120 Buff.readInt(); // Block flags;
121 Blocks.push_back(new GCOVBlock(i));
122 }
123
124 // read edges.
125 while (Buff.readEdgeTag()) {
126 uint32_t EdgeCount = (Buff.readInt() - 1) / 2;
127 uint32_t BlockNo = Buff.readInt();
Nick Lewycky85aa4f62012-09-23 03:58:21 +0000128 assert(BlockNo < BlockCount && "Unexpected Block number!");
Devang Pateld02c42b2011-09-28 18:50:00 +0000129 for (int i = 0, e = EdgeCount; i != e; ++i) {
130 Blocks[BlockNo]->addEdge(Buff.readInt());
131 Buff.readInt(); // Edge flag
132 }
133 }
134
135 // read line table.
136 while (Buff.readLineTag()) {
137 uint32_t LineTableLength = Buff.readInt();
138 uint32_t Size = Buff.getCursor() + LineTableLength*4;
139 uint32_t BlockNo = Buff.readInt();
Nick Lewycky85aa4f62012-09-23 03:58:21 +0000140 assert(BlockNo < BlockCount && "Unexpected Block number!");
Devang Pateld02c42b2011-09-28 18:50:00 +0000141 GCOVBlock *Block = Blocks[BlockNo];
142 Buff.readInt(); // flag
143 while (Buff.getCursor() != (Size - 4)) {
144 StringRef Filename = Buff.readString();
145 if (Buff.getCursor() == (Size - 4)) break;
146 while (uint32_t L = Buff.readInt())
Bill Wendling56cb2292012-07-19 00:11:40 +0000147 Block->addLine(Filename, L);
Devang Pateld02c42b2011-09-28 18:50:00 +0000148 }
149 Buff.readInt(); // flag
150 }
151 return true;
152}
153
154/// dump - Dump GCOVFunction content on standard out for debugging purposes.
155void GCOVFunction::dump() {
156 outs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
Craig Topper6227d5c2013-07-04 01:31:24 +0000157 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000158 E = Blocks.end(); I != E; ++I)
Devang Pateld02c42b2011-09-28 18:50:00 +0000159 (*I)->dump();
160}
161
162/// collectLineCounts - Collect line counts. This must be used after
163/// reading .gcno and .gcda files.
164void GCOVFunction::collectLineCounts(FileInfo &FI) {
Craig Topper6227d5c2013-07-04 01:31:24 +0000165 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000166 E = Blocks.end(); I != E; ++I)
Devang Pateld02c42b2011-09-28 18:50:00 +0000167 (*I)->collectLineCounts(FI);
168}
169
170//===----------------------------------------------------------------------===//
171// GCOVBlock implementation.
172
173/// ~GCOVBlock - Delete GCOVBlock and its content.
174GCOVBlock::~GCOVBlock() {
175 Edges.clear();
176 DeleteContainerSeconds(Lines);
177}
178
179void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) {
180 GCOVLines *&LinesForFile = Lines[Filename];
181 if (!LinesForFile)
182 LinesForFile = new GCOVLines();
183 LinesForFile->add(LineNo);
184}
185
186/// collectLineCounts - Collect line counts. This must be used after
187/// reading .gcno and .gcda files.
188void GCOVBlock::collectLineCounts(FileInfo &FI) {
189 for (StringMap<GCOVLines *>::iterator I = Lines.begin(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000190 E = Lines.end(); I != E; ++I)
Devang Pateld02c42b2011-09-28 18:50:00 +0000191 I->second->collectLineCounts(FI, I->first(), Counter);
192}
193
194/// dump - Dump GCOVBlock content on standard out for debugging purposes.
195void GCOVBlock::dump() {
196 outs() << "Block : " << Number << " Counter : " << Counter << "\n";
197 if (!Edges.empty()) {
198 outs() << "\tEdges : ";
Craig Topper6227d5c2013-07-04 01:31:24 +0000199 for (SmallVectorImpl<uint32_t>::iterator I = Edges.begin(), E = Edges.end();
Bill Wendling56cb2292012-07-19 00:11:40 +0000200 I != E; ++I)
Devang Pateld02c42b2011-09-28 18:50:00 +0000201 outs() << (*I) << ",";
202 outs() << "\n";
203 }
204 if (!Lines.empty()) {
205 outs() << "\tLines : ";
206 for (StringMap<GCOVLines *>::iterator LI = Lines.begin(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000207 LE = Lines.end(); LI != LE; ++LI) {
Devang Pateld02c42b2011-09-28 18:50:00 +0000208 outs() << LI->first() << " -> ";
209 LI->second->dump();
210 outs() << "\n";
211 }
212 }
213}
214
215//===----------------------------------------------------------------------===//
216// GCOVLines implementation.
217
218/// collectLineCounts - Collect line counts. This must be used after
219/// reading .gcno and .gcda files.
220void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename,
Bill Wendling56cb2292012-07-19 00:11:40 +0000221 uint32_t Count) {
Craig Topper6227d5c2013-07-04 01:31:24 +0000222 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000223 E = Lines.end(); I != E; ++I)
Devang Pateld02c42b2011-09-28 18:50:00 +0000224 FI.addLineCount(Filename, *I, Count);
225}
226
227/// dump - Dump GCOVLines content on standard out for debugging purposes.
228void GCOVLines::dump() {
Craig Topper6227d5c2013-07-04 01:31:24 +0000229 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000230 E = Lines.end(); I != E; ++I)
Devang Pateld02c42b2011-09-28 18:50:00 +0000231 outs() << (*I) << ",";
232}
233
234//===----------------------------------------------------------------------===//
235// FileInfo implementation.
236
237/// addLineCount - Add line count for the given line number in a file.
238void FileInfo::addLineCount(StringRef Filename, uint32_t Line, uint32_t Count) {
239 if (LineInfo.find(Filename) == LineInfo.end()) {
240 OwningPtr<MemoryBuffer> Buff;
241 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
242 errs() << Filename << ": " << ec.message() << "\n";
243 return;
244 }
245 StringRef AllLines = Buff.take()->getBuffer();
246 LineCounts L(AllLines.count('\n')+2);
247 L[Line-1] = Count;
248 LineInfo[Filename] = L;
249 return;
250 }
251 LineCounts &L = LineInfo[Filename];
252 L[Line-1] = Count;
253}
254
255/// print - Print source files with collected line count information.
256void FileInfo::print() {
257 for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end();
258 I != E; ++I) {
259 StringRef Filename = I->first();
260 outs() << Filename << "\n";
261 LineCounts &L = LineInfo[Filename];
262 OwningPtr<MemoryBuffer> Buff;
263 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
264 errs() << Filename << ": " << ec.message() << "\n";
265 return;
266 }
267 StringRef AllLines = Buff.take()->getBuffer();
268 for (unsigned i = 0, e = L.size(); i != e; ++i) {
269 if (L[i])
Bill Wendling56cb2292012-07-19 00:11:40 +0000270 outs() << L[i] << ":\t";
Devang Pateld02c42b2011-09-28 18:50:00 +0000271 else
Bill Wendling56cb2292012-07-19 00:11:40 +0000272 outs() << " :\t";
Devang Pateld02c42b2011-09-28 18:50:00 +0000273 std::pair<StringRef, StringRef> P = AllLines.split('\n');
274 if (AllLines != P.first)
Bill Wendling56cb2292012-07-19 00:11:40 +0000275 outs() << P.first;
Devang Pateld02c42b2011-09-28 18:50:00 +0000276 outs() << "\n";
277 AllLines = P.second;
278 }
279 }
280}
281
282