1 //===- GCOVr.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 // GCOV implements the interface to read and write coverage files that use
13 //===----------------------------------------------------------------------===//
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"
24 //===----------------------------------------------------------------------===//
25 // GCOVFile implementation.
27 /// ~GCOVFile - Delete GCOVFile and its content.
28 GCOVFile::~GCOVFile() {
29 DeleteContainerPointers(Functions);
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;
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;
42 /// read - Read GCOV buffer.
43 bool GCOVFile::read(GCOVBuffer &Buffer) {
44 GCOV::GCOVFormat Format = Buffer.readGCOVFormat();
45 if (Format == GCOV::InvalidGCOV)
48 if (isGCNOFile(Format)) {
50 GCOVFunction *GFun = new GCOVFunction();
51 if (!GFun->read(Buffer, Format))
53 Functions.push_back(GFun);
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);
60 assert(ReadGCDA && ".gcda data does not match .gcno data");
62 while (Buffer.readProgramTag())
69 /// dump - Dump GCOVFile content to dbgs() for debugging purposes.
70 void GCOVFile::dump() {
71 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
72 E = Functions.end(); I != E; ++I)
76 /// collectLineCounts - Collect line counts. This must be used after
77 /// reading .gcno and .gcda files.
78 void GCOVFile::collectLineCounts(FileInfo &FI) {
79 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
80 E = Functions.end(); I != E; ++I)
81 (*I)->collectLineCounts(FI);
82 FI.setProgramCount(ProgramCount);
85 //===----------------------------------------------------------------------===//
86 // GCOVFunction implementation.
88 /// ~GCOVFunction - Delete GCOVFunction and its content.
89 GCOVFunction::~GCOVFunction() {
90 DeleteContainerPointers(Blocks);
93 /// read - Read a function from the buffer. Return false if buffer cursor
94 /// does not point to a function tag.
95 bool GCOVFunction::read(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
96 if (!Buff.readFunctionTag())
99 Buff.readInt(); // Function header length
100 Ident = Buff.readInt();
101 Buff.readInt(); // Checksum #1
102 if (Format != GCOV::GCNO_402 && Format != GCOV::GCDA_402)
103 Buff.readInt(); // Checksum #2
105 Name = Buff.readString();
106 if (Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404)
107 Filename = Buff.readString();
109 if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) {
112 uint32_t Count = Buff.readInt() / 2;
114 // This for loop adds the counts for each block. A second nested loop is
115 // required to combine the edge counts that are contained in the GCDA file.
116 for (uint32_t Line = 0; i < Count; ++Line) {
117 GCOVBlock &Block = *Blocks[Line];
118 for (size_t Edge = 0, End = Block.getNumEdges(); Edge < End; ++Edge) {
119 assert(i < Count && "Unexpected number of Edges!");
120 Block.addCount(Buff.readInt64());
127 LineNumber = Buff.readInt();
130 bool BlockTagFound = Buff.readBlockTag();
132 assert(BlockTagFound && "Block Tag not found!");
133 uint32_t BlockCount = Buff.readInt();
134 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
135 Buff.readInt(); // Block flags;
136 Blocks.push_back(new GCOVBlock(i));
140 while (Buff.readEdgeTag()) {
141 uint32_t EdgeCount = (Buff.readInt() - 1) / 2;
142 uint32_t BlockNo = Buff.readInt();
143 assert(BlockNo < BlockCount && "Unexpected Block number!");
144 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
145 Blocks[BlockNo]->addEdge(Buff.readInt());
146 Buff.readInt(); // Edge flag
151 while (Buff.readLineTag()) {
152 uint32_t LineTableLength = Buff.readInt();
153 uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
154 uint32_t BlockNo = Buff.readInt();
155 assert(BlockNo < BlockCount && "Unexpected Block number!");
156 GCOVBlock *Block = Blocks[BlockNo];
157 Buff.readInt(); // flag
158 while (Buff.getCursor() != (EndPos - 4)) {
159 StringRef Filename = Buff.readString();
160 if (Buff.getCursor() == (EndPos - 4)) break;
161 while (uint32_t L = Buff.readInt())
162 Block->addLine(Filename, L);
164 Buff.readInt(); // flag
169 /// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
170 void GCOVFunction::dump() {
171 dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
172 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
173 E = Blocks.end(); I != E; ++I)
177 /// collectLineCounts - Collect line counts. This must be used after
178 /// reading .gcno and .gcda files.
179 void GCOVFunction::collectLineCounts(FileInfo &FI) {
180 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
181 E = Blocks.end(); I != E; ++I)
182 (*I)->collectLineCounts(FI);
185 //===----------------------------------------------------------------------===//
186 // GCOVBlock implementation.
188 /// ~GCOVBlock - Delete GCOVBlock and its content.
189 GCOVBlock::~GCOVBlock() {
191 DeleteContainerSeconds(Lines);
194 void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) {
195 GCOVLines *&LinesForFile = Lines[Filename];
197 LinesForFile = new GCOVLines();
198 LinesForFile->add(LineNo);
201 /// collectLineCounts - Collect line counts. This must be used after
202 /// reading .gcno and .gcda files.
203 void GCOVBlock::collectLineCounts(FileInfo &FI) {
204 for (StringMap<GCOVLines *>::iterator I = Lines.begin(),
205 E = Lines.end(); I != E; ++I)
206 I->second->collectLineCounts(FI, I->first(), Counter);
209 /// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
210 void GCOVBlock::dump() {
211 dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
212 if (!Edges.empty()) {
213 dbgs() << "\tEdges : ";
214 for (SmallVectorImpl<uint32_t>::iterator I = Edges.begin(), E = Edges.end();
216 dbgs() << (*I) << ",";
219 if (!Lines.empty()) {
220 dbgs() << "\tLines : ";
221 for (StringMap<GCOVLines *>::iterator LI = Lines.begin(),
222 LE = Lines.end(); LI != LE; ++LI) {
223 dbgs() << LI->first() << " -> ";
230 //===----------------------------------------------------------------------===//
231 // GCOVLines implementation.
233 /// collectLineCounts - Collect line counts. This must be used after
234 /// reading .gcno and .gcda files.
235 void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename,
237 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
238 E = Lines.end(); I != E; ++I)
239 FI.addLineCount(Filename, *I, Count);
242 /// dump - Dump GCOVLines content to dbgs() for debugging purposes.
243 void GCOVLines::dump() {
244 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
245 E = Lines.end(); I != E; ++I)
246 dbgs() << (*I) << ",";
249 //===----------------------------------------------------------------------===//
250 // FileInfo implementation.
252 /// print - Print source files with collected line count information.
253 void FileInfo::print(StringRef gcnoFile, StringRef gcdaFile) {
254 for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end();
256 StringRef Filename = I->first();
257 outs() << " -: 0:Source:" << Filename << "\n";
258 outs() << " -: 0:Graph:" << gcnoFile << "\n";
259 outs() << " -: 0:Data:" << gcdaFile << "\n";
260 outs() << " -: 0:Programs:" << ProgramCount << "\n";
261 LineCounts &L = LineInfo[Filename];
262 OwningPtr<MemoryBuffer> Buff;
263 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
264 errs() << Filename << ": " << ec.message() << "\n";
267 StringRef AllLines = Buff.take()->getBuffer();
269 while (!AllLines.empty()) {
270 if (L.find(i) != L.end()) {
274 outs() << format("%9lu:", L[i]);
278 std::pair<StringRef, StringRef> P = AllLines.split('\n');
279 if (AllLines != P.first)
280 outs() << format("%5u:", i+1) << P.first;