llvm-cov: Store blocks rather than counts per line.
[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/Debug.h"
16 #include "llvm/Support/GCOV.h"
17 #include "llvm/ADT/OwningPtr.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/Support/Format.h"
20 #include "llvm/Support/MemoryObject.h"
21 #include "llvm/Support/system_error.h"
22 using namespace llvm;
23
24 //===----------------------------------------------------------------------===//
25 // GCOVFile implementation.
26
27 /// ~GCOVFile - Delete GCOVFile and its content.
28 GCOVFile::~GCOVFile() {
29   DeleteContainerPointers(Functions);
30 }
31
32 /// isGCDAFile - Return true if Format identifies a .gcda file.
33 static bool isGCDAFile(GCOV::GCOVFormat Format) {
34   return Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404;
35 }
36
37 /// isGCNOFile - Return true if Format identifies a .gcno file.
38 static bool isGCNOFile(GCOV::GCOVFormat Format) {
39   return Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404;
40 }
41
42 /// read - Read GCOV buffer.
43 bool GCOVFile::read(GCOVBuffer &Buffer) {
44   GCOV::GCOVFormat Format = Buffer.readGCOVFormat();
45   if (Format == GCOV::InvalidGCOV)
46     return false;
47
48   if (isGCNOFile(Format)) {
49     if (!Buffer.readInt(Checksum)) return false;
50     while (true) {
51       if (!Buffer.readFunctionTag()) break;
52       GCOVFunction *GFun = new GCOVFunction();
53       if (!GFun->readGCNO(Buffer, Format))
54         return false;
55       Functions.push_back(GFun);
56     }
57   } else if (isGCDAFile(Format)) {
58     uint32_t Checksum2;
59     if (!Buffer.readInt(Checksum2)) return false;
60     if (Checksum != Checksum2) {
61       errs() << "File checksum does not match.\n";
62       return false;
63     }
64     for (size_t i = 0, e = Functions.size(); i < e; ++i) {
65       if (!Buffer.readFunctionTag()) {
66         errs() << "Unexpected number of functions.\n";
67         return false;
68       }
69       if (!Functions[i]->readGCDA(Buffer, Format))
70         return false;
71     }
72     if (Buffer.readObjectTag()) {
73       uint32_t Length;
74       uint32_t Dummy;
75       if (!Buffer.readInt(Length)) return false;
76       if (!Buffer.readInt(Dummy)) return false; // checksum
77       if (!Buffer.readInt(Dummy)) return false; // num
78       if (!Buffer.readInt(RunCount)) return false;;
79       Buffer.advanceCursor(Length-3);
80     }
81     while (Buffer.readProgramTag()) {
82       uint32_t Length;
83       if (!Buffer.readInt(Length)) return false;
84       Buffer.advanceCursor(Length);
85       ++ProgramCount;
86     }
87   }
88
89   return true;
90 }
91
92 /// dump - Dump GCOVFile content to dbgs() for debugging purposes.
93 void GCOVFile::dump() const {
94   for (SmallVectorImpl<GCOVFunction *>::const_iterator I = Functions.begin(),
95          E = Functions.end(); I != E; ++I)
96     (*I)->dump();
97 }
98
99 /// collectLineCounts - Collect line counts. This must be used after
100 /// reading .gcno and .gcda files.
101 void GCOVFile::collectLineCounts(FileInfo &FI) {
102   for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
103          E = Functions.end(); I != E; ++I)
104     (*I)->collectLineCounts(FI);
105   FI.setRunCount(RunCount);
106   FI.setProgramCount(ProgramCount);
107 }
108
109 //===----------------------------------------------------------------------===//
110 // GCOVFunction implementation.
111
112 /// ~GCOVFunction - Delete GCOVFunction and its content.
113 GCOVFunction::~GCOVFunction() {
114   DeleteContainerPointers(Blocks);
115   DeleteContainerPointers(Edges);
116 }
117
118 /// readGCNO - Read a function from the GCNO buffer. Return false if an error
119 /// occurs.
120 bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
121   uint32_t Dummy;
122   if (!Buff.readInt(Dummy)) return false; // Function header length
123   if (!Buff.readInt(Ident)) return false;
124   if (!Buff.readInt(Dummy)) return false; // Checksum #1
125   if (Format != GCOV::GCNO_402)
126     if (!Buff.readInt(Dummy)) return false; // Checksum #2
127
128   if (!Buff.readString(Name)) return false;
129   if (!Buff.readString(Filename)) return false;
130   if (!Buff.readInt(LineNumber)) return false;
131
132   // read blocks.
133   if (!Buff.readBlockTag()) {
134     errs() << "Block tag not found.\n";
135     return false;
136   }
137   uint32_t BlockCount;
138   if (!Buff.readInt(BlockCount)) return false;
139   for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
140     if (!Buff.readInt(Dummy)) return false; // Block flags;
141     Blocks.push_back(new GCOVBlock(*this, i));
142   }
143
144   // read edges.
145   while (Buff.readEdgeTag()) {
146     uint32_t EdgeCount;
147     if (!Buff.readInt(EdgeCount)) return false;
148     EdgeCount = (EdgeCount - 1) / 2;
149     uint32_t BlockNo;
150     if (!Buff.readInt(BlockNo)) return false;
151     if (BlockNo >= BlockCount) {
152       errs() << "Unexpected block number.\n";
153       return false;
154     }
155     for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
156       uint32_t Dst;
157       if (!Buff.readInt(Dst)) return false;
158       GCOVEdge *Edge = new GCOVEdge(Blocks[BlockNo], Blocks[Dst]);
159       Edges.push_back(Edge);
160       Blocks[BlockNo]->addDstEdge(Edge);
161       Blocks[Dst]->addSrcEdge(Edge);
162       if (!Buff.readInt(Dummy)) return false; // Edge flag
163     }
164   }
165
166   // read line table.
167   while (Buff.readLineTag()) {
168     uint32_t LineTableLength;
169     if (!Buff.readInt(LineTableLength)) return false;
170     uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
171     uint32_t BlockNo;
172     if (!Buff.readInt(BlockNo)) return false;
173     if (BlockNo >= BlockCount) {
174       errs() << "Unexpected block number.\n";
175       return false;
176     }
177     GCOVBlock *Block = Blocks[BlockNo];
178     if (!Buff.readInt(Dummy)) return false; // flag
179     while (Buff.getCursor() != (EndPos - 4)) {
180       StringRef F;
181       if (!Buff.readString(F)) return false;
182       if (F != Filename) {
183         errs() << "Multiple sources for a single basic block.\n";
184         return false;
185       }
186       if (Buff.getCursor() == (EndPos - 4)) break;
187       while (true) {
188         uint32_t Line;
189         if (!Buff.readInt(Line)) return false;
190         if (!Line) break;
191         Block->addLine(Line);
192       }
193     }
194     if (!Buff.readInt(Dummy)) return false; // flag
195   }
196   return true;
197 }
198
199 /// readGCDA - Read a function from the GCDA buffer. Return false if an error
200 /// occurs.
201 bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
202   uint32_t Dummy;
203   if (!Buff.readInt(Dummy)) return false; // Function header length
204   if (!Buff.readInt(Ident)) return false;
205   if (!Buff.readInt(Dummy)) return false; // Checksum #1
206   if (Format != GCOV::GCDA_402)
207     if (!Buff.readInt(Dummy)) return false; // Checksum #2
208
209   if (!Buff.readString(Name)) return false;
210
211   if (!Buff.readArcTag()) {
212     errs() << "Arc tag not found.\n";
213     return false;
214   }
215
216   uint32_t Count;
217   if (!Buff.readInt(Count)) return false;
218   Count /= 2;
219
220   // This for loop adds the counts for each block. A second nested loop is
221   // required to combine the edge counts that are contained in the GCDA file.
222   for (uint32_t BlockNo = 0; Count > 0; ++BlockNo) {
223     // The last block is always reserved for exit block
224     if (BlockNo >= Blocks.size()-1) {
225       errs() << "Unexpected number of edges.\n";
226       return false;
227     }
228     GCOVBlock &Block = *Blocks[BlockNo];
229     for (size_t EdgeNo = 0, End = Block.getNumDstEdges(); EdgeNo < End;
230            ++EdgeNo) {
231       if (Count == 0) {
232         errs() << "Unexpected number of edges.\n";
233         return false;
234       }
235       uint64_t ArcCount;
236       if (!Buff.readInt64(ArcCount)) return false;
237       Block.addCount(EdgeNo, ArcCount);
238       --Count;
239     }
240   }
241   return true;
242 }
243
244 /// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
245 void GCOVFunction::dump() const {
246   dbgs() <<  "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
247   for (SmallVectorImpl<GCOVBlock *>::const_iterator I = Blocks.begin(),
248          E = Blocks.end(); I != E; ++I)
249     (*I)->dump();
250 }
251
252 /// collectLineCounts - Collect line counts. This must be used after
253 /// reading .gcno and .gcda files.
254 void GCOVFunction::collectLineCounts(FileInfo &FI) {
255   for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
256          E = Blocks.end(); I != E; ++I)
257     (*I)->collectLineCounts(FI);
258 }
259
260 //===----------------------------------------------------------------------===//
261 // GCOVBlock implementation.
262
263 /// ~GCOVBlock - Delete GCOVBlock and its content.
264 GCOVBlock::~GCOVBlock() {
265   SrcEdges.clear();
266   DstEdges.clear();
267   Lines.clear();
268 }
269
270 /// addCount - Add to block counter while storing the edge count. If the
271 /// destination has no outgoing edges, also update that block's count too.
272 void GCOVBlock::addCount(size_t DstEdgeNo, uint64_t N) {
273   assert(DstEdgeNo < DstEdges.size()); // up to caller to ensure EdgeNo is valid
274   DstEdges[DstEdgeNo]->Count = N;
275   Counter += N;
276   if (!DstEdges[DstEdgeNo]->Dst->getNumDstEdges())
277     DstEdges[DstEdgeNo]->Dst->Counter += N;
278 }
279
280 /// collectLineCounts - Collect line counts. This must be used after
281 /// reading .gcno and .gcda files.
282 void GCOVBlock::collectLineCounts(FileInfo &FI) {
283   for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
284          E = Lines.end(); I != E; ++I)
285     FI.addBlockLine(Parent.getFilename(), *I, this);
286 }
287
288 /// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
289 void GCOVBlock::dump() const {
290   dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
291   if (!SrcEdges.empty()) {
292     dbgs() << "\tSource Edges : ";
293     for (EdgeIterator I = SrcEdges.begin(), E = SrcEdges.end(); I != E; ++I) {
294       const GCOVEdge *Edge = *I;
295       dbgs() << Edge->Src->Number << " (" << Edge->Count << "), ";
296     }
297     dbgs() << "\n";
298   }
299   if (!DstEdges.empty()) {
300     dbgs() << "\tDestination Edges : ";
301     for (EdgeIterator I = DstEdges.begin(), E = DstEdges.end(); I != E; ++I) {
302       const GCOVEdge *Edge = *I;
303       dbgs() << Edge->Dst->Number << " (" << Edge->Count << "), ";
304     }
305     dbgs() << "\n";
306   }
307   if (!Lines.empty()) {
308     dbgs() << "\tLines : ";
309     for (SmallVectorImpl<uint32_t>::const_iterator I = Lines.begin(),
310            E = Lines.end(); I != E; ++I)
311       dbgs() << (*I) << ",";
312     dbgs() << "\n";
313   }
314 }
315
316 //===----------------------------------------------------------------------===//
317 // FileInfo implementation.
318
319 /// print -  Print source files with collected line count information.
320 void FileInfo::print(raw_fd_ostream &OS, StringRef gcnoFile,
321                      StringRef gcdaFile) const {
322   for (StringMap<LineData>::const_iterator I = LineInfo.begin(),
323          E = LineInfo.end(); I != E; ++I) {
324     StringRef Filename = I->first();
325     OwningPtr<MemoryBuffer> Buff;
326     if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
327       errs() << Filename << ": " << ec.message() << "\n";
328       return;
329     }
330     StringRef AllLines = Buff->getBuffer();
331
332     OS << "        -:    0:Source:" << Filename << "\n";
333     OS << "        -:    0:Graph:" << gcnoFile << "\n";
334     OS << "        -:    0:Data:" << gcdaFile << "\n";
335     OS << "        -:    0:Runs:" << RunCount << "\n";
336     OS << "        -:    0:Programs:" << ProgramCount << "\n";
337
338     const LineData &L = I->second;
339     uint32_t i = 0;
340     while (!AllLines.empty()) {
341       LineData::const_iterator BlocksIt = L.find(i);
342       if (BlocksIt != L.end()) {
343         const BlockVector &Blocks = BlocksIt->second;
344         uint64_t LineCount = 0;
345         for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end();
346                I != E; ++I) {
347           LineCount += (*I)->getCount();
348         }
349         if (LineCount == 0)
350           OS << "    #####:";
351         else
352           OS << format("%9" PRIu64 ":", LineCount);
353       } else {
354         OS << "        -:";
355       }
356       std::pair<StringRef, StringRef> P = AllLines.split('\n');
357       if (AllLines != P.first)
358         OS << format("%5u:", i+1) << P.first;
359       OS << "\n";
360       AllLines = P.second;
361       ++i;
362     }
363   }
364 }