llvm-cov dump to dbgs() instead of outs().
[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     while (true) {
50       GCOVFunction *GFun = new GCOVFunction();
51       if (!GFun->read(Buffer, Format))
52         break;
53       Functions.push_back(GFun);
54     }
55   }
56   else if (isGCDAFile(Format)) {
57     for (size_t i = 0, e = Functions.size(); i < e; ++i) {
58       bool ReadGCDA = Functions[i]->read(Buffer, Format);
59       (void)ReadGCDA;
60       assert(ReadGCDA && ".gcda data does not match .gcno data");
61     }
62     while (Buffer.readProgramTag())
63       ++ProgramCount;
64   }
65
66   return true;
67 }
68
69 /// dump - Dump GCOVFile content to dbgs() for debugging purposes.
70 void GCOVFile::dump() {
71   for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
72          E = Functions.end(); I != E; ++I)
73     (*I)->dump();
74 }
75
76 /// collectLineCounts - Collect line counts. This must be used after
77 /// reading .gcno and .gcda files.
78 void GCOVFile::collectLineCounts(FileInfo &FI) {
79   for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
80          E = Functions.end(); I != E; ++I) 
81     (*I)->collectLineCounts(FI);
82   FI.setProgramCount(ProgramCount);
83 }
84
85 //===----------------------------------------------------------------------===//
86 // GCOVFunction implementation.
87
88 /// ~GCOVFunction - Delete GCOVFunction and its content.
89 GCOVFunction::~GCOVFunction() {
90   DeleteContainerPointers(Blocks);
91 }
92
93 /// read - Read a function from the buffer. Return false if buffer cursor
94 /// does not point to a function tag.
95 bool GCOVFunction::read(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
96   if (!Buff.readFunctionTag())
97     return false;
98
99   Buff.readInt(); // Function header length
100   Ident = Buff.readInt(); 
101   Buff.readInt(); // Checksum #1
102   if (Format != GCOV::GCNO_402 && Format != GCOV::GCDA_402)
103     Buff.readInt(); // Checksum #2
104
105   Name = Buff.readString();
106   if (Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404)
107     Filename = Buff.readString();
108
109   if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) {
110     Buff.readArcTag();
111     uint32_t i = 0;
112     uint32_t Count = Buff.readInt() / 2;
113
114     // This for loop adds the counts for each block. A second nested loop is
115     // required to combine the edge counts that are contained in the GCDA file.
116     for (uint32_t Line = 0; i < Count; ++Line) {
117       GCOVBlock &Block = *Blocks[Line];
118       for (size_t Edge = 0, End = Block.getNumEdges(); Edge < End; ++Edge) {
119         assert(i < Count && "Unexpected number of Edges!");
120         Block.addCount(Buff.readInt64());
121         ++i;
122       }
123     }
124     return true;
125   }
126
127   LineNumber = Buff.readInt();
128
129   // read blocks.
130   bool BlockTagFound = Buff.readBlockTag();
131   (void)BlockTagFound;
132   assert(BlockTagFound && "Block Tag not found!");
133   uint32_t BlockCount = Buff.readInt();
134   for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
135     Buff.readInt(); // Block flags;
136     Blocks.push_back(new GCOVBlock(i));
137   }
138
139   // read edges.
140   while (Buff.readEdgeTag()) {
141     uint32_t EdgeCount = (Buff.readInt() - 1) / 2;
142     uint32_t BlockNo = Buff.readInt();
143     assert(BlockNo < BlockCount && "Unexpected Block number!");
144     for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
145       Blocks[BlockNo]->addEdge(Buff.readInt());
146       Buff.readInt(); // Edge flag
147     }
148   }
149
150   // read line table.
151   while (Buff.readLineTag()) {
152     uint32_t LineTableLength = Buff.readInt();
153     uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
154     uint32_t BlockNo = Buff.readInt();
155     assert(BlockNo < BlockCount && "Unexpected Block number!");
156     GCOVBlock *Block = Blocks[BlockNo];
157     Buff.readInt(); // flag
158     while (Buff.getCursor() != (EndPos - 4)) {
159       StringRef Filename = Buff.readString();
160       if (Buff.getCursor() == (EndPos - 4)) break;
161       while (uint32_t L = Buff.readInt())
162         Block->addLine(Filename, L);
163     }
164     Buff.readInt(); // flag
165   }
166   return true;
167 }
168
169 /// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
170 void GCOVFunction::dump() {
171   dbgs() <<  "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
172   for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
173          E = Blocks.end(); I != E; ++I)
174     (*I)->dump();
175 }
176
177 /// collectLineCounts - Collect line counts. This must be used after
178 /// reading .gcno and .gcda files.
179 void GCOVFunction::collectLineCounts(FileInfo &FI) {
180   for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
181          E = Blocks.end(); I != E; ++I)
182     (*I)->collectLineCounts(FI);
183 }
184
185 //===----------------------------------------------------------------------===//
186 // GCOVBlock implementation.
187
188 /// ~GCOVBlock - Delete GCOVBlock and its content.
189 GCOVBlock::~GCOVBlock() {
190   Edges.clear();
191   DeleteContainerSeconds(Lines);
192 }
193
194 void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) {
195   GCOVLines *&LinesForFile = Lines[Filename];
196   if (!LinesForFile)
197     LinesForFile = new GCOVLines();
198   LinesForFile->add(LineNo);
199 }
200
201 /// collectLineCounts - Collect line counts. This must be used after
202 /// reading .gcno and .gcda files.
203 void GCOVBlock::collectLineCounts(FileInfo &FI) {
204   for (StringMap<GCOVLines *>::iterator I = Lines.begin(),
205          E = Lines.end(); I != E; ++I)
206     I->second->collectLineCounts(FI, I->first(), Counter);
207 }
208
209 /// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
210 void GCOVBlock::dump() {
211   dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
212   if (!Edges.empty()) {
213     dbgs() << "\tEdges : ";
214     for (SmallVectorImpl<uint32_t>::iterator I = Edges.begin(), E = Edges.end();
215          I != E; ++I)
216       dbgs() << (*I) << ",";
217     dbgs() << "\n";
218   }
219   if (!Lines.empty()) {
220     dbgs() << "\tLines : ";
221     for (StringMap<GCOVLines *>::iterator LI = Lines.begin(),
222            LE = Lines.end(); LI != LE; ++LI) {
223       dbgs() << LI->first() << " -> ";
224       LI->second->dump();
225       dbgs() << "\n";
226     }
227   }
228 }
229
230 //===----------------------------------------------------------------------===//
231 // GCOVLines implementation.
232
233 /// collectLineCounts - Collect line counts. This must be used after
234 /// reading .gcno and .gcda files.
235 void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename, 
236                                   uint64_t Count) {
237   for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
238          E = Lines.end(); I != E; ++I)
239     FI.addLineCount(Filename, *I, Count);
240 }
241
242 /// dump - Dump GCOVLines content to dbgs() for debugging purposes.
243 void GCOVLines::dump() {
244   for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
245          E = Lines.end(); I != E; ++I)
246     dbgs() << (*I) << ",";
247 }
248
249 //===----------------------------------------------------------------------===//
250 // FileInfo implementation.
251
252 /// print -  Print source files with collected line count information.
253 void FileInfo::print(StringRef gcnoFile, StringRef gcdaFile) {
254   for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end();
255        I != E; ++I) {
256     StringRef Filename = I->first();
257     outs() << "        -:    0:Source:" << Filename << "\n";
258     outs() << "        -:    0:Graph:" << gcnoFile << "\n";
259     outs() << "        -:    0:Data:" << gcdaFile << "\n";
260     outs() << "        -:    0:Programs:" << ProgramCount << "\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     uint32_t i = 0;
269     while (!AllLines.empty()) {
270       if (L.find(i) != L.end()) {
271         if (L[i] == 0)
272           outs() << "    #####:";
273         else
274           outs() << format("%9lu:", L[i]);
275       } else {
276         outs() << "        -:";
277       }
278       std::pair<StringRef, StringRef> P = AllLines.split('\n');
279       if (AllLines != P.first)
280         outs() << format("%5u:", i+1) << P.first;
281       outs() << "\n";
282       AllLines = P.second;
283       ++i;
284     }
285   }
286 }
287