blob: e4570b1229f9595138d44f558626c6cbfc77062b [file] [log] [blame]
Devang Pateld02c42b2011-09-28 18:50:00 +00001//===- tools/llvm-cov/GCOVReader.cpp - LLVM coverage tool -----------------===//
2//
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//
10// GCOVReader implements the interface to read coverage files that use 'gcov'
11// format.
12//
Devang Pateld02c42b2011-09-28 18:50:00 +000013//===----------------------------------------------------------------------===//
14
15#include "GCOVReader.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
30/// read - Read GCOV buffer.
31bool GCOVFile::read(GCOVBuffer &Buffer) {
Devang Patel8d6c0fb2011-09-29 16:48:44 +000032 GCOVFormat Format = Buffer.readGCOVFormat();
Devang Pateld02c42b2011-09-28 18:50:00 +000033 if (Format == InvalidGCOV)
34 return false;
35
36 unsigned i = 0;
Devang Patel7a502022011-09-29 16:46:47 +000037 while (1) {
Devang Pateld02c42b2011-09-28 18:50:00 +000038 GCOVFunction *GFun = NULL;
Devang Patel7a502022011-09-29 16:46:47 +000039 if (Format == GCDA_402 || Format == GCDA_404) {
Devang Pateld02c42b2011-09-28 18:50:00 +000040 if (i < Functions.size())
41 GFun = Functions[i];
42 } else
43 GFun = new GCOVFunction();
44
45 if (GFun && GFun->read(Buffer, Format)) {
Devang Patel7a502022011-09-29 16:46:47 +000046 if (Format == GCNO_402 || Format == GCNO_404)
Devang Pateld02c42b2011-09-28 18:50:00 +000047 Functions.push_back(GFun);
48 }
49 else {
50 delete GFun;
51 break;
52 }
53 ++i;
54 }
55 return true;
56}
57
58/// dump - Dump GCOVFile content on standard out for debugging purposes.
59void GCOVFile::dump() {
60 for (SmallVector<GCOVFunction *, 16>::iterator I = Functions.begin(),
61 E = Functions.end(); I != E; ++I)
62 (*I)->dump();
63}
64
65/// collectLineCounts - Collect line counts. This must be used after
66/// reading .gcno and .gcda files.
67void GCOVFile::collectLineCounts(FileInfo &FI) {
68 for (SmallVector<GCOVFunction *, 16>::iterator I = Functions.begin(),
69 E = Functions.end(); I != E; ++I)
70 (*I)->collectLineCounts(FI);
71 FI.print();
72}
73
74//===----------------------------------------------------------------------===//
75// GCOVFunction implementation.
76
77/// ~GCOVFunction - Delete GCOVFunction and its content.
78GCOVFunction::~GCOVFunction() {
79 DeleteContainerPointers(Blocks);
80}
81
82/// read - Read a aunction from the buffer. Return false if buffer cursor
83/// does not point to a function tag.
84bool GCOVFunction::read(GCOVBuffer &Buff, GCOVFormat Format) {
85 if (!Buff.readFunctionTag())
86 return false;
87
88 Buff.readInt(); // Function header length
89 Ident = Buff.readInt();
90 Buff.readInt(); // Checksum #1
91 if (Format != GCNO_402)
92 Buff.readInt(); // Checksum #2
93
94 Name = Buff.readString();
Devang Patel7a502022011-09-29 16:46:47 +000095 if (Format == GCNO_402 || Format == GCNO_404)
Devang Pateld02c42b2011-09-28 18:50:00 +000096 Filename = Buff.readString();
97
Devang Patel7a502022011-09-29 16:46:47 +000098 if (Format == GCDA_402 || Format == GCDA_404) {
Devang Pateld02c42b2011-09-28 18:50:00 +000099 Buff.readArcTag();
100 uint32_t Count = Buff.readInt() / 2;
101 for (unsigned i = 0, e = Count; i != e; ++i) {
102 Blocks[i]->addCount(Buff.readInt64());
103 }
104 return true;;
105 }
106
107 LineNumber = Buff.readInt();
108
109 // read blocks.
110 assert (Buff.readBlockTag() && "Block Tag not found!");
111 uint32_t BlockCount = Buff.readInt();
112 for (int i = 0, e = BlockCount; i != e; ++i) {
113 Buff.readInt(); // Block flags;
114 Blocks.push_back(new GCOVBlock(i));
115 }
116
117 // read edges.
118 while (Buff.readEdgeTag()) {
119 uint32_t EdgeCount = (Buff.readInt() - 1) / 2;
120 uint32_t BlockNo = Buff.readInt();
121 assert (BlockNo < BlockCount && "Unexpected Block number!");
122 for (int i = 0, e = EdgeCount; i != e; ++i) {
123 Blocks[BlockNo]->addEdge(Buff.readInt());
124 Buff.readInt(); // Edge flag
125 }
126 }
127
128 // read line table.
129 while (Buff.readLineTag()) {
130 uint32_t LineTableLength = Buff.readInt();
131 uint32_t Size = Buff.getCursor() + LineTableLength*4;
132 uint32_t BlockNo = Buff.readInt();
133 assert (BlockNo < BlockCount && "Unexpected Block number!");
134 GCOVBlock *Block = Blocks[BlockNo];
135 Buff.readInt(); // flag
136 while (Buff.getCursor() != (Size - 4)) {
137 StringRef Filename = Buff.readString();
138 if (Buff.getCursor() == (Size - 4)) break;
139 while (uint32_t L = Buff.readInt())
140 Block->addLine(Filename, L);
141 }
142 Buff.readInt(); // flag
143 }
144 return true;
145}
146
147/// dump - Dump GCOVFunction content on standard out for debugging purposes.
148void GCOVFunction::dump() {
149 outs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
150 for (SmallVector<GCOVBlock *, 16>::iterator I = Blocks.begin(),
151 E = Blocks.end(); I != E; ++I)
152 (*I)->dump();
153}
154
155/// collectLineCounts - Collect line counts. This must be used after
156/// reading .gcno and .gcda files.
157void GCOVFunction::collectLineCounts(FileInfo &FI) {
158 for (SmallVector<GCOVBlock *, 16>::iterator I = Blocks.begin(),
159 E = Blocks.end(); I != E; ++I)
160 (*I)->collectLineCounts(FI);
161}
162
163//===----------------------------------------------------------------------===//
164// GCOVBlock implementation.
165
166/// ~GCOVBlock - Delete GCOVBlock and its content.
167GCOVBlock::~GCOVBlock() {
168 Edges.clear();
169 DeleteContainerSeconds(Lines);
170}
171
172void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) {
173 GCOVLines *&LinesForFile = Lines[Filename];
174 if (!LinesForFile)
175 LinesForFile = new GCOVLines();
176 LinesForFile->add(LineNo);
177}
178
179/// collectLineCounts - Collect line counts. This must be used after
180/// reading .gcno and .gcda files.
181void GCOVBlock::collectLineCounts(FileInfo &FI) {
182 for (StringMap<GCOVLines *>::iterator I = Lines.begin(),
183 E = Lines.end(); I != E; ++I)
184 I->second->collectLineCounts(FI, I->first(), Counter);
185}
186
187/// dump - Dump GCOVBlock content on standard out for debugging purposes.
188void GCOVBlock::dump() {
189 outs() << "Block : " << Number << " Counter : " << Counter << "\n";
190 if (!Edges.empty()) {
191 outs() << "\tEdges : ";
192 for (SmallVector<uint32_t, 16>::iterator I = Edges.begin(), E = Edges.end();
193 I != E; ++I)
194 outs() << (*I) << ",";
195 outs() << "\n";
196 }
197 if (!Lines.empty()) {
198 outs() << "\tLines : ";
199 for (StringMap<GCOVLines *>::iterator LI = Lines.begin(),
200 LE = Lines.end(); LI != LE; ++LI) {
201 outs() << LI->first() << " -> ";
202 LI->second->dump();
203 outs() << "\n";
204 }
205 }
206}
207
208//===----------------------------------------------------------------------===//
209// GCOVLines implementation.
210
211/// collectLineCounts - Collect line counts. This must be used after
212/// reading .gcno and .gcda files.
213void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename,
214 uint32_t Count) {
215 for (SmallVector<uint32_t, 16>::iterator I = Lines.begin(),
216 E = Lines.end(); I != E; ++I)
217 FI.addLineCount(Filename, *I, Count);
218}
219
220/// dump - Dump GCOVLines content on standard out for debugging purposes.
221void GCOVLines::dump() {
222 for (SmallVector<uint32_t, 16>::iterator I = Lines.begin(),
223 E = Lines.end(); I != E; ++I)
224 outs() << (*I) << ",";
225}
226
227//===----------------------------------------------------------------------===//
228// FileInfo implementation.
229
230/// addLineCount - Add line count for the given line number in a file.
231void FileInfo::addLineCount(StringRef Filename, uint32_t Line, uint32_t Count) {
232 if (LineInfo.find(Filename) == LineInfo.end()) {
233 OwningPtr<MemoryBuffer> Buff;
234 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
235 errs() << Filename << ": " << ec.message() << "\n";
236 return;
237 }
238 StringRef AllLines = Buff.take()->getBuffer();
239 LineCounts L(AllLines.count('\n')+2);
240 L[Line-1] = Count;
241 LineInfo[Filename] = L;
242 return;
243 }
244 LineCounts &L = LineInfo[Filename];
245 L[Line-1] = Count;
246}
247
248/// print - Print source files with collected line count information.
249void FileInfo::print() {
250 for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end();
251 I != E; ++I) {
252 StringRef Filename = I->first();
253 outs() << Filename << "\n";
254 LineCounts &L = LineInfo[Filename];
255 OwningPtr<MemoryBuffer> Buff;
256 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
257 errs() << Filename << ": " << ec.message() << "\n";
258 return;
259 }
260 StringRef AllLines = Buff.take()->getBuffer();
261 for (unsigned i = 0, e = L.size(); i != e; ++i) {
262 if (L[i])
263 outs() << L[i] << ":\t";
264 else
265 outs() << " :\t";
266 std::pair<StringRef, StringRef> P = AllLines.split('\n');
267 if (AllLines != P.first)
268 outs() << P.first;
269 outs() << "\n";
270 AllLines = P.second;
271 }
272 }
273}
274
275