Move the printing of llvm-cov information out from collectLineCounts().
[oota-llvm.git] / lib / IR / GCOV.cpp
1 //===- GCOVr.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 // GCOV implements the interface to read and write coverage files that use 
11 // 'gcov' format.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Support/GCOV.h"
16 #include "llvm/ADT/OwningPtr.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/Support/MemoryObject.h"
19 #include "llvm/Support/system_error.h"
20 using namespace llvm;
21
22 //===----------------------------------------------------------------------===//
23 // GCOVFile implementation.
24
25 /// ~GCOVFile - Delete GCOVFile and its content.
26 GCOVFile::~GCOVFile() {
27   DeleteContainerPointers(Functions);
28 }
29
30 /// isGCDAFile - Return true if Format identifies a .gcda file.
31 static bool isGCDAFile(GCOV::GCOVFormat Format) {
32   return Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404;
33 }
34
35 /// isGCNOFile - Return true if Format identifies a .gcno file.
36 static bool isGCNOFile(GCOV::GCOVFormat Format) {
37   return Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404;
38 }
39
40 /// read - Read GCOV buffer.
41 bool GCOVFile::read(GCOVBuffer &Buffer) {
42   GCOV::GCOVFormat Format = Buffer.readGCOVFormat();
43   if (Format == GCOV::InvalidGCOV)
44     return false;
45
46   unsigned i = 0;
47   while (1) {
48     GCOVFunction *GFun = NULL;
49     if (isGCDAFile(Format)) {
50       // Use existing function while reading .gcda file.
51       assert(i < Functions.size() && ".gcda data does not match .gcno data");
52       GFun = Functions[i];
53     } else if (isGCNOFile(Format)){
54       GFun = new GCOVFunction();
55       Functions.push_back(GFun);
56     }
57     if (!GFun || !GFun->read(Buffer, Format))
58       break;
59     ++i;
60   }
61   return true;
62 }
63
64 /// dump - Dump GCOVFile content on standard out for debugging purposes.
65 void GCOVFile::dump() {
66   for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
67          E = Functions.end(); I != E; ++I)
68     (*I)->dump();
69 }
70
71 /// collectLineCounts - Collect line counts. This must be used after
72 /// reading .gcno and .gcda files.
73 void GCOVFile::collectLineCounts(FileInfo &FI) {
74   for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
75          E = Functions.end(); I != E; ++I) 
76     (*I)->collectLineCounts(FI);
77 }
78
79 //===----------------------------------------------------------------------===//
80 // GCOVFunction implementation.
81
82 /// ~GCOVFunction - Delete GCOVFunction and its content.
83 GCOVFunction::~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.
89 bool GCOVFunction::read(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
90   if (!Buff.readFunctionTag())
91     return false;
92
93   Buff.readInt(); // Function header length
94   Ident = Buff.readInt(); 
95   Buff.readInt(); // Checksum #1
96   if (Format != GCOV::GCNO_402 && Format != GCOV::GCDA_402)
97     Buff.readInt(); // Checksum #2
98
99   Name = Buff.readString();
100   if (Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404)
101     Filename = Buff.readString();
102
103   if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) {
104     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     }
109     return true;
110   }
111
112   LineNumber = Buff.readInt();
113
114   // read blocks.
115   bool BlockTagFound = Buff.readBlockTag();
116   (void)BlockTagFound;
117   assert(BlockTagFound && "Block Tag not found!");
118   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();
128     assert(BlockNo < BlockCount && "Unexpected Block number!");
129     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();
140     assert(BlockNo < BlockCount && "Unexpected Block number!");
141     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())
147         Block->addLine(Filename, L);
148     }
149     Buff.readInt(); // flag
150   }
151   return true;
152 }
153
154 /// dump - Dump GCOVFunction content on standard out for debugging purposes.
155 void GCOVFunction::dump() {
156   outs() <<  "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
157   for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
158          E = Blocks.end(); I != E; ++I)
159     (*I)->dump();
160 }
161
162 /// collectLineCounts - Collect line counts. This must be used after
163 /// reading .gcno and .gcda files.
164 void GCOVFunction::collectLineCounts(FileInfo &FI) {
165   for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
166          E = Blocks.end(); I != E; ++I)
167     (*I)->collectLineCounts(FI);
168 }
169
170 //===----------------------------------------------------------------------===//
171 // GCOVBlock implementation.
172
173 /// ~GCOVBlock - Delete GCOVBlock and its content.
174 GCOVBlock::~GCOVBlock() {
175   Edges.clear();
176   DeleteContainerSeconds(Lines);
177 }
178
179 void 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.
188 void GCOVBlock::collectLineCounts(FileInfo &FI) {
189   for (StringMap<GCOVLines *>::iterator I = Lines.begin(),
190          E = Lines.end(); I != E; ++I)
191     I->second->collectLineCounts(FI, I->first(), Counter);
192 }
193
194 /// dump - Dump GCOVBlock content on standard out for debugging purposes.
195 void GCOVBlock::dump() {
196   outs() << "Block : " << Number << " Counter : " << Counter << "\n";
197   if (!Edges.empty()) {
198     outs() << "\tEdges : ";
199     for (SmallVectorImpl<uint32_t>::iterator I = Edges.begin(), E = Edges.end();
200          I != E; ++I)
201       outs() << (*I) << ",";
202     outs() << "\n";
203   }
204   if (!Lines.empty()) {
205     outs() << "\tLines : ";
206     for (StringMap<GCOVLines *>::iterator LI = Lines.begin(),
207            LE = Lines.end(); LI != LE; ++LI) {
208       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.
220 void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename, 
221                                   uint32_t Count) {
222   for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
223          E = Lines.end(); I != E; ++I)
224     FI.addLineCount(Filename, *I, Count);
225 }
226
227 /// dump - Dump GCOVLines content on standard out for debugging purposes.
228 void GCOVLines::dump() {
229   for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
230          E = Lines.end(); I != E; ++I)
231     outs() << (*I) << ",";
232 }
233
234 //===----------------------------------------------------------------------===//
235 // FileInfo implementation.
236
237 /// addLineCount - Add line count for the given line number in a file.
238 void 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.
256 void 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])
270         outs() << L[i] << ":\t";
271       else
272         outs() << " :\t";
273       std::pair<StringRef, StringRef> P = AllLines.split('\n');
274       if (AllLines != P.first)
275         outs() << P.first;
276       outs() << "\n";
277       AllLines = P.second;
278     }
279   }
280 }
281
282