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