1 //===- tools/llvm-cov/GCOVReader.cpp - LLVM coverage tool -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // GCOVReader implements the interface to read coverage files that use 'gcov'
14 //===----------------------------------------------------------------------===//
16 #include "GCOVReader.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/OwningPtr.h"
19 #include "llvm/Support/MemoryObject.h"
20 #include "llvm/Support/system_error.h"
23 //===----------------------------------------------------------------------===//
24 // GCOVFile implementation.
26 /// ~GCOVFile - Delete GCOVFile and its content.
27 GCOVFile::~GCOVFile() {
28 DeleteContainerPointers(Functions);
31 /// read - Read GCOV buffer.
32 bool GCOVFile::read(GCOVBuffer &Buffer) {
33 Format = Buffer.readGCOVFormat();
34 if (Format == InvalidGCOV)
39 GCOVFunction *GFun = NULL;
40 if(Format == GCDA_402 || Format == GCDA_404) {
41 if (i < Functions.size())
44 GFun = new GCOVFunction();
46 if (GFun && GFun->read(Buffer, Format)) {
47 if(Format == GCNO_402 || Format == GCNO_404)
48 Functions.push_back(GFun);
59 /// dump - Dump GCOVFile content on standard out for debugging purposes.
60 void GCOVFile::dump() {
61 for (SmallVector<GCOVFunction *, 16>::iterator I = Functions.begin(),
62 E = Functions.end(); I != E; ++I)
66 /// collectLineCounts - Collect line counts. This must be used after
67 /// reading .gcno and .gcda files.
68 void GCOVFile::collectLineCounts(FileInfo &FI) {
69 for (SmallVector<GCOVFunction *, 16>::iterator I = Functions.begin(),
70 E = Functions.end(); I != E; ++I)
71 (*I)->collectLineCounts(FI);
75 //===----------------------------------------------------------------------===//
76 // GCOVFunction implementation.
78 /// ~GCOVFunction - Delete GCOVFunction and its content.
79 GCOVFunction::~GCOVFunction() {
80 DeleteContainerPointers(Blocks);
83 /// read - Read a aunction from the buffer. Return false if buffer cursor
84 /// does not point to a function tag.
85 bool GCOVFunction::read(GCOVBuffer &Buff, GCOVFormat Format) {
86 if (!Buff.readFunctionTag())
89 Buff.readInt(); // Function header length
90 Ident = Buff.readInt();
91 Buff.readInt(); // Checksum #1
92 if (Format != GCNO_402)
93 Buff.readInt(); // Checksum #2
95 Name = Buff.readString();
96 if(Format == GCNO_402 || Format == GCNO_404)
97 Filename = Buff.readString();
99 if(Format == GCDA_402 || Format == GCDA_404) {
101 uint32_t Count = Buff.readInt() / 2;
102 for (unsigned i = 0, e = Count; i != e; ++i) {
103 Blocks[i]->addCount(Buff.readInt64());
108 LineNumber = Buff.readInt();
111 assert (Buff.readBlockTag() && "Block Tag not found!");
112 uint32_t BlockCount = Buff.readInt();
113 for (int i = 0, e = BlockCount; i != e; ++i) {
114 Buff.readInt(); // Block flags;
115 Blocks.push_back(new GCOVBlock(i));
119 while (Buff.readEdgeTag()) {
120 uint32_t EdgeCount = (Buff.readInt() - 1) / 2;
121 uint32_t BlockNo = Buff.readInt();
122 assert (BlockNo < BlockCount && "Unexpected Block number!");
123 for (int i = 0, e = EdgeCount; i != e; ++i) {
124 Blocks[BlockNo]->addEdge(Buff.readInt());
125 Buff.readInt(); // Edge flag
130 while (Buff.readLineTag()) {
131 uint32_t LineTableLength = Buff.readInt();
132 uint32_t Size = Buff.getCursor() + LineTableLength*4;
133 uint32_t BlockNo = Buff.readInt();
134 assert (BlockNo < BlockCount && "Unexpected Block number!");
135 GCOVBlock *Block = Blocks[BlockNo];
136 Buff.readInt(); // flag
137 while (Buff.getCursor() != (Size - 4)) {
138 StringRef Filename = Buff.readString();
139 if (Buff.getCursor() == (Size - 4)) break;
140 while (uint32_t L = Buff.readInt())
141 Block->addLine(Filename, L);
143 Buff.readInt(); // flag
148 /// dump - Dump GCOVFunction content on standard out for debugging purposes.
149 void GCOVFunction::dump() {
150 outs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
151 for (SmallVector<GCOVBlock *, 16>::iterator I = Blocks.begin(),
152 E = Blocks.end(); I != E; ++I)
156 /// collectLineCounts - Collect line counts. This must be used after
157 /// reading .gcno and .gcda files.
158 void GCOVFunction::collectLineCounts(FileInfo &FI) {
159 for (SmallVector<GCOVBlock *, 16>::iterator I = Blocks.begin(),
160 E = Blocks.end(); I != E; ++I)
161 (*I)->collectLineCounts(FI);
164 //===----------------------------------------------------------------------===//
165 // GCOVBlock implementation.
167 /// ~GCOVBlock - Delete GCOVBlock and its content.
168 GCOVBlock::~GCOVBlock() {
170 DeleteContainerSeconds(Lines);
173 void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) {
174 GCOVLines *&LinesForFile = Lines[Filename];
176 LinesForFile = new GCOVLines();
177 LinesForFile->add(LineNo);
180 /// collectLineCounts - Collect line counts. This must be used after
181 /// reading .gcno and .gcda files.
182 void GCOVBlock::collectLineCounts(FileInfo &FI) {
183 for (StringMap<GCOVLines *>::iterator I = Lines.begin(),
184 E = Lines.end(); I != E; ++I)
185 I->second->collectLineCounts(FI, I->first(), Counter);
188 /// dump - Dump GCOVBlock content on standard out for debugging purposes.
189 void GCOVBlock::dump() {
190 outs() << "Block : " << Number << " Counter : " << Counter << "\n";
191 if (!Edges.empty()) {
192 outs() << "\tEdges : ";
193 for (SmallVector<uint32_t, 16>::iterator I = Edges.begin(), E = Edges.end();
195 outs() << (*I) << ",";
198 if (!Lines.empty()) {
199 outs() << "\tLines : ";
200 for (StringMap<GCOVLines *>::iterator LI = Lines.begin(),
201 LE = Lines.end(); LI != LE; ++LI) {
202 outs() << LI->first() << " -> ";
209 //===----------------------------------------------------------------------===//
210 // GCOVLines implementation.
212 /// collectLineCounts - Collect line counts. This must be used after
213 /// reading .gcno and .gcda files.
214 void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename,
216 for (SmallVector<uint32_t, 16>::iterator I = Lines.begin(),
217 E = Lines.end(); I != E; ++I)
218 FI.addLineCount(Filename, *I, Count);
221 /// dump - Dump GCOVLines content on standard out for debugging purposes.
222 void GCOVLines::dump() {
223 for (SmallVector<uint32_t, 16>::iterator I = Lines.begin(),
224 E = Lines.end(); I != E; ++I)
225 outs() << (*I) << ",";
228 //===----------------------------------------------------------------------===//
229 // FileInfo implementation.
231 /// addLineCount - Add line count for the given line number in a file.
232 void FileInfo::addLineCount(StringRef Filename, uint32_t Line, uint32_t Count) {
233 if (LineInfo.find(Filename) == LineInfo.end()) {
234 OwningPtr<MemoryBuffer> Buff;
235 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
236 errs() << Filename << ": " << ec.message() << "\n";
239 StringRef AllLines = Buff.take()->getBuffer();
240 LineCounts L(AllLines.count('\n')+2);
242 LineInfo[Filename] = L;
245 LineCounts &L = LineInfo[Filename];
249 /// print - Print source files with collected line count information.
250 void FileInfo::print() {
251 for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end();
253 StringRef Filename = I->first();
254 outs() << Filename << "\n";
255 LineCounts &L = LineInfo[Filename];
256 OwningPtr<MemoryBuffer> Buff;
257 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
258 errs() << Filename << ": " << ec.message() << "\n";
261 StringRef AllLines = Buff.take()->getBuffer();
262 for (unsigned i = 0, e = L.size(); i != e; ++i) {
264 outs() << L[i] << ":\t";
267 std::pair<StringRef, StringRef> P = AllLines.split('\n');
268 if (AllLines != P.first)