• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
~GCOVFile()26 GCOVFile::~GCOVFile() {
27   DeleteContainerPointers(Functions);
28 }
29 
30 /// isGCDAFile - Return true if Format identifies a .gcda file.
isGCDAFile(GCOVFormat Format)31 static bool isGCDAFile(GCOVFormat Format) {
32   return Format == GCDA_402 || Format == GCDA_404;
33 }
34 
35 /// isGCNOFile - Return true if Format identifies a .gcno file.
isGCNOFile(GCOVFormat Format)36 static bool isGCNOFile(GCOVFormat Format) {
37   return Format == GCNO_402 || Format == GCNO_404;
38 }
39 
40 /// read - Read GCOV buffer.
read(GCOVBuffer & Buffer)41 bool GCOVFile::read(GCOVBuffer &Buffer) {
42   GCOVFormat Format = Buffer.readGCOVFormat();
43   if (Format == 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.
dump()65 void GCOVFile::dump() {
66   for (SmallVector<GCOVFunction *, 16>::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.
collectLineCounts(FileInfo & FI)73 void GCOVFile::collectLineCounts(FileInfo &FI) {
74   for (SmallVector<GCOVFunction *, 16>::iterator I = Functions.begin(),
75 	 E = Functions.end(); I != E; ++I)
76     (*I)->collectLineCounts(FI);
77   FI.print();
78 }
79 
80 //===----------------------------------------------------------------------===//
81 // GCOVFunction implementation.
82 
83 /// ~GCOVFunction - Delete GCOVFunction and its content.
~GCOVFunction()84 GCOVFunction::~GCOVFunction() {
85   DeleteContainerPointers(Blocks);
86 }
87 
88 /// read - Read a aunction from the buffer. Return false if buffer cursor
89 /// does not point to a function tag.
read(GCOVBuffer & Buff,GCOVFormat Format)90 bool GCOVFunction::read(GCOVBuffer &Buff, GCOVFormat Format) {
91   if (!Buff.readFunctionTag())
92     return false;
93 
94   Buff.readInt(); // Function header length
95   Ident = Buff.readInt();
96   Buff.readInt(); // Checksum #1
97   if (Format != GCNO_402)
98     Buff.readInt(); // Checksum #2
99 
100   Name = Buff.readString();
101   if (Format == GCNO_402 || Format == GCNO_404)
102     Filename = Buff.readString();
103 
104   if (Format == GCDA_402 || Format == GCDA_404) {
105     Buff.readArcTag();
106     uint32_t Count = Buff.readInt() / 2;
107     for (unsigned i = 0, e = Count; i != e; ++i) {
108       Blocks[i]->addCount(Buff.readInt64());
109     }
110     return true;;
111   }
112 
113   LineNumber = Buff.readInt();
114 
115   // read blocks.
116   assert (Buff.readBlockTag() && "Block Tag not found!");
117   uint32_t BlockCount = Buff.readInt();
118   for (int i = 0, e = BlockCount; i != e; ++i) {
119     Buff.readInt(); // Block flags;
120     Blocks.push_back(new GCOVBlock(i));
121   }
122 
123   // read edges.
124   while (Buff.readEdgeTag()) {
125     uint32_t EdgeCount = (Buff.readInt() - 1) / 2;
126     uint32_t BlockNo = Buff.readInt();
127     assert (BlockNo < BlockCount && "Unexpected Block number!");
128     for (int i = 0, e = EdgeCount; i != e; ++i) {
129       Blocks[BlockNo]->addEdge(Buff.readInt());
130       Buff.readInt(); // Edge flag
131     }
132   }
133 
134   // read line table.
135   while (Buff.readLineTag()) {
136     uint32_t LineTableLength = Buff.readInt();
137     uint32_t Size = Buff.getCursor() + LineTableLength*4;
138     uint32_t BlockNo = Buff.readInt();
139     assert (BlockNo < BlockCount && "Unexpected Block number!");
140     GCOVBlock *Block = Blocks[BlockNo];
141     Buff.readInt(); // flag
142     while (Buff.getCursor() != (Size - 4)) {
143       StringRef Filename = Buff.readString();
144       if (Buff.getCursor() == (Size - 4)) break;
145       while (uint32_t L = Buff.readInt())
146 	Block->addLine(Filename, L);
147     }
148     Buff.readInt(); // flag
149   }
150   return true;
151 }
152 
153 /// dump - Dump GCOVFunction content on standard out for debugging purposes.
dump()154 void GCOVFunction::dump() {
155   outs() <<  "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
156   for (SmallVector<GCOVBlock *, 16>::iterator I = Blocks.begin(),
157 	 E = Blocks.end(); I != E; ++I)
158     (*I)->dump();
159 }
160 
161 /// collectLineCounts - Collect line counts. This must be used after
162 /// reading .gcno and .gcda files.
collectLineCounts(FileInfo & FI)163 void GCOVFunction::collectLineCounts(FileInfo &FI) {
164   for (SmallVector<GCOVBlock *, 16>::iterator I = Blocks.begin(),
165 	 E = Blocks.end(); I != E; ++I)
166     (*I)->collectLineCounts(FI);
167 }
168 
169 //===----------------------------------------------------------------------===//
170 // GCOVBlock implementation.
171 
172 /// ~GCOVBlock - Delete GCOVBlock and its content.
~GCOVBlock()173 GCOVBlock::~GCOVBlock() {
174   Edges.clear();
175   DeleteContainerSeconds(Lines);
176 }
177 
addLine(StringRef Filename,uint32_t LineNo)178 void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) {
179   GCOVLines *&LinesForFile = Lines[Filename];
180   if (!LinesForFile)
181     LinesForFile = new GCOVLines();
182   LinesForFile->add(LineNo);
183 }
184 
185 /// collectLineCounts - Collect line counts. This must be used after
186 /// reading .gcno and .gcda files.
collectLineCounts(FileInfo & FI)187 void GCOVBlock::collectLineCounts(FileInfo &FI) {
188   for (StringMap<GCOVLines *>::iterator I = Lines.begin(),
189 	 E = Lines.end(); I != E; ++I)
190     I->second->collectLineCounts(FI, I->first(), Counter);
191 }
192 
193 /// dump - Dump GCOVBlock content on standard out for debugging purposes.
dump()194 void GCOVBlock::dump() {
195   outs() << "Block : " << Number << " Counter : " << Counter << "\n";
196   if (!Edges.empty()) {
197     outs() << "\tEdges : ";
198     for (SmallVector<uint32_t, 16>::iterator I = Edges.begin(), E = Edges.end();
199 	 I != E; ++I)
200       outs() << (*I) << ",";
201     outs() << "\n";
202   }
203   if (!Lines.empty()) {
204     outs() << "\tLines : ";
205     for (StringMap<GCOVLines *>::iterator LI = Lines.begin(),
206 	   LE = Lines.end(); LI != LE; ++LI) {
207       outs() << LI->first() << " -> ";
208       LI->second->dump();
209       outs() << "\n";
210     }
211   }
212 }
213 
214 //===----------------------------------------------------------------------===//
215 // GCOVLines implementation.
216 
217 /// collectLineCounts - Collect line counts. This must be used after
218 /// reading .gcno and .gcda files.
collectLineCounts(FileInfo & FI,StringRef Filename,uint32_t Count)219 void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename,
220 				  uint32_t Count) {
221   for (SmallVector<uint32_t, 16>::iterator I = Lines.begin(),
222 	 E = Lines.end(); I != E; ++I)
223     FI.addLineCount(Filename, *I, Count);
224 }
225 
226 /// dump - Dump GCOVLines content on standard out for debugging purposes.
dump()227 void GCOVLines::dump() {
228   for (SmallVector<uint32_t, 16>::iterator I = Lines.begin(),
229 	 E = Lines.end(); I != E; ++I)
230     outs() << (*I) << ",";
231 }
232 
233 //===----------------------------------------------------------------------===//
234 // FileInfo implementation.
235 
236 /// addLineCount - Add line count for the given line number in a file.
addLineCount(StringRef Filename,uint32_t Line,uint32_t Count)237 void FileInfo::addLineCount(StringRef Filename, uint32_t Line, uint32_t Count) {
238   if (LineInfo.find(Filename) == LineInfo.end()) {
239     OwningPtr<MemoryBuffer> Buff;
240     if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
241       errs() << Filename << ": " << ec.message() << "\n";
242       return;
243     }
244     StringRef AllLines = Buff.take()->getBuffer();
245     LineCounts L(AllLines.count('\n')+2);
246     L[Line-1] = Count;
247     LineInfo[Filename] = L;
248     return;
249   }
250   LineCounts &L = LineInfo[Filename];
251   L[Line-1] = Count;
252 }
253 
254 /// print -  Print source files with collected line count information.
print()255 void FileInfo::print() {
256   for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end();
257        I != E; ++I) {
258     StringRef Filename = I->first();
259     outs() << Filename << "\n";
260     LineCounts &L = LineInfo[Filename];
261     OwningPtr<MemoryBuffer> Buff;
262     if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
263       errs() << Filename << ": " << ec.message() << "\n";
264       return;
265     }
266     StringRef AllLines = Buff.take()->getBuffer();
267     for (unsigned i = 0, e = L.size(); i != e; ++i) {
268       if (L[i])
269 	outs() << L[i] << ":\t";
270       else
271 	outs() << " :\t";
272       std::pair<StringRef, StringRef> P = AllLines.split('\n');
273       if (AllLines != P.first)
274 	outs() << P.first;
275       outs() << "\n";
276       AllLines = P.second;
277     }
278   }
279 }
280 
281 
282