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