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'
13 //===----------------------------------------------------------------------===//
15 #include "GCOVReader.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"
22 //===----------------------------------------------------------------------===//
23 // GCOVFile implementation.
25 /// ~GCOVFile - Delete GCOVFile and its content.
26 GCOVFile::~GCOVFile() {
27 DeleteContainerPointers(Functions);
30 /// isGCDAFile - Return true if Format identifies a .gcda file.
31 static bool isGCDAFile(GCOVFormat Format) {
32 return Format == GCDA_402 || Format == GCDA_404;
35 /// isGCNOFile - Return true if Format identifies a .gcno file.
36 static bool isGCNOFile(GCOVFormat Format) {
37 return Format == GCNO_402 || Format == GCNO_404;
40 /// read - Read GCOV buffer.
41 bool GCOVFile::read(GCOVBuffer &Buffer) {
42 GCOVFormat Format = Buffer.readGCOVFormat();
43 if (Format == InvalidGCOV)
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");
53 } else if (isGCNOFile(Format)){
54 GFun = new GCOVFunction();
55 Functions.push_back(GFun);
57 if (!GFun || !GFun->read(Buffer, Format))
64 /// dump - Dump GCOVFile content on standard out for debugging purposes.
65 void GCOVFile::dump() {
66 for (SmallVector<GCOVFunction *, 16>::iterator I = Functions.begin(),
67 E = Functions.end(); I != E; ++I)
71 /// collectLineCounts - Collect line counts. This must be used after
72 /// reading .gcno and .gcda files.
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);
80 //===----------------------------------------------------------------------===//
81 // GCOVFunction implementation.
83 /// ~GCOVFunction - Delete GCOVFunction and its content.
84 GCOVFunction::~GCOVFunction() {
85 DeleteContainerPointers(Blocks);
88 /// read - Read a aunction from the buffer. Return false if buffer cursor
89 /// does not point to a function tag.
90 bool GCOVFunction::read(GCOVBuffer &Buff, GCOVFormat Format) {
91 if (!Buff.readFunctionTag())
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
100 Name = Buff.readString();
101 if (Format == GCNO_402 || Format == GCNO_404)
102 Filename = Buff.readString();
104 if (Format == GCDA_402 || Format == GCDA_404) {
106 uint32_t Count = Buff.readInt() / 2;
107 for (unsigned i = 0, e = Count; i != e; ++i) {
108 Blocks[i]->addCount(Buff.readInt64());
113 LineNumber = Buff.readInt();
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));
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
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);
148 Buff.readInt(); // flag
153 /// dump - Dump GCOVFunction content on standard out for debugging purposes.
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)
161 /// collectLineCounts - Collect line counts. This must be used after
162 /// reading .gcno and .gcda files.
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);
169 //===----------------------------------------------------------------------===//
170 // GCOVBlock implementation.
172 /// ~GCOVBlock - Delete GCOVBlock and its content.
173 GCOVBlock::~GCOVBlock() {
175 DeleteContainerSeconds(Lines);
178 void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) {
179 GCOVLines *&LinesForFile = Lines[Filename];
181 LinesForFile = new GCOVLines();
182 LinesForFile->add(LineNo);
185 /// collectLineCounts - Collect line counts. This must be used after
186 /// reading .gcno and .gcda files.
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);
193 /// dump - Dump GCOVBlock content on standard out for debugging purposes.
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();
200 outs() << (*I) << ",";
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() << " -> ";
214 //===----------------------------------------------------------------------===//
215 // GCOVLines implementation.
217 /// collectLineCounts - Collect line counts. This must be used after
218 /// reading .gcno and .gcda files.
219 void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename,
221 for (SmallVector<uint32_t, 16>::iterator I = Lines.begin(),
222 E = Lines.end(); I != E; ++I)
223 FI.addLineCount(Filename, *I, Count);
226 /// dump - Dump GCOVLines content on standard out for debugging purposes.
227 void GCOVLines::dump() {
228 for (SmallVector<uint32_t, 16>::iterator I = Lines.begin(),
229 E = Lines.end(); I != E; ++I)
230 outs() << (*I) << ",";
233 //===----------------------------------------------------------------------===//
234 // FileInfo implementation.
236 /// addLineCount - Add line count for the given line number in a file.
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";
244 StringRef AllLines = Buff.take()->getBuffer();
245 LineCounts L(AllLines.count('\n')+2);
247 LineInfo[Filename] = L;
250 LineCounts &L = LineInfo[Filename];
254 /// print - Print source files with collected line count information.
255 void FileInfo::print() {
256 for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end();
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";
266 StringRef AllLines = Buff.take()->getBuffer();
267 for (unsigned i = 0, e = L.size(); i != e; ++i) {
269 outs() << L[i] << ":\t";
272 std::pair<StringRef, StringRef> P = AllLines.split('\n');
273 if (AllLines != P.first)