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