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 if (!Buffer.readFunctionTag()) break;
51 GCOVFunction *GFun = new GCOVFunction();
52 if (!GFun->read(Buffer, Format))
54 Functions.push_back(GFun);
57 else if (isGCDAFile(Format)) {
58 for (size_t i = 0, e = Functions.size(); i < e; ++i) {
59 if (!Buffer.readFunctionTag()) {
60 errs() << "Unexpected number of functions.\n";
63 if (!Functions[i]->read(Buffer, Format))
66 if (Buffer.readObjectTag()) {
69 if (!Buffer.readInt(Length)) return false;
70 if (!Buffer.readInt(Dummy)) return false; // checksum
71 if (!Buffer.readInt(Dummy)) return false; // num
72 if (!Buffer.readInt(RunCount)) return false;;
73 Buffer.advanceCursor(Length-3);
75 while (Buffer.readProgramTag()) {
77 if (!Buffer.readInt(Length)) return false;
78 Buffer.advanceCursor(Length);
86 /// dump - Dump GCOVFile content to dbgs() for debugging purposes.
87 void GCOVFile::dump() {
88 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
89 E = Functions.end(); I != E; ++I)
93 /// collectLineCounts - Collect line counts. This must be used after
94 /// reading .gcno and .gcda files.
95 void GCOVFile::collectLineCounts(FileInfo &FI) {
96 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
97 E = Functions.end(); I != E; ++I)
98 (*I)->collectLineCounts(FI);
99 FI.setRunCount(RunCount);
100 FI.setProgramCount(ProgramCount);
103 //===----------------------------------------------------------------------===//
104 // GCOVFunction implementation.
106 /// ~GCOVFunction - Delete GCOVFunction and its content.
107 GCOVFunction::~GCOVFunction() {
108 DeleteContainerPointers(Blocks);
111 /// read - Read a function from the buffer. Return false if buffer cursor
112 /// does not point to a function tag.
113 bool GCOVFunction::read(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
115 if (!Buff.readInt(Dummy)) return false; // Function header length
116 if (!Buff.readInt(Ident)) return false;
117 if (!Buff.readInt(Dummy)) return false; // Checksum #1
118 if (Format != GCOV::GCNO_402 && Format != GCOV::GCDA_402)
119 if (!Buff.readInt(Dummy)) return false; // Checksum #2
121 if (!Buff.readString(Name)) return false;
123 if (Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404)
124 if (!Buff.readString(Filename)) return false;
126 if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) {
127 if (!Buff.readArcTag()) {
128 errs() << "Arc tag not found.\n";
132 if (!Buff.readInt(Count)) return false;
135 // This for loop adds the counts for each block. A second nested loop is
136 // required to combine the edge counts that are contained in the GCDA file.
137 for (uint32_t Line = 0; Count > 0; ++Line) {
138 GCOVBlock &Block = *Blocks[Line];
139 for (size_t Edge = 0, End = Block.getNumEdges(); Edge < End; ++Edge) {
141 errs() << "Unexpected number of edges.\n";
145 if (!Buff.readInt64(ArcCount)) return false;
146 Block.addCount(ArcCount);
153 if (!Buff.readInt(LineNumber)) return false;
156 if (!Buff.readBlockTag()) {
157 errs() << "Block tag not found.\n";
161 if (!Buff.readInt(BlockCount)) return false;
162 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
163 if (!Buff.readInt(Dummy)) return false; // Block flags;
164 Blocks.push_back(new GCOVBlock(i));
168 while (Buff.readEdgeTag()) {
170 if (!Buff.readInt(EdgeCount)) return false;
171 EdgeCount = (EdgeCount - 1) / 2;
173 if (!Buff.readInt(BlockNo)) return false;
174 if (BlockNo >= BlockCount) {
175 errs() << "Unexpected block number.\n";
178 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
180 if (!Buff.readInt(Dst)) return false;
181 Blocks[BlockNo]->addEdge(Dst);
182 if (!Buff.readInt(Dummy)) return false; // Edge flag
187 while (Buff.readLineTag()) {
188 uint32_t LineTableLength;
189 if (!Buff.readInt(LineTableLength)) return false;
190 uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
192 if (!Buff.readInt(BlockNo)) return false;
193 if (BlockNo >= BlockCount) {
194 errs() << "Unexpected block number.\n";
197 GCOVBlock *Block = Blocks[BlockNo];
198 if (!Buff.readInt(Dummy)) return false; // flag
199 while (Buff.getCursor() != (EndPos - 4)) {
201 if (!Buff.readString(Filename)) return false;
202 if (Buff.getCursor() == (EndPos - 4)) break;
205 if (!Buff.readInt(Line)) return false;
207 Block->addLine(Filename, Line);
210 if (!Buff.readInt(Dummy)) return false; // flag
215 /// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
216 void GCOVFunction::dump() {
217 dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
218 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
219 E = Blocks.end(); I != E; ++I)
223 /// collectLineCounts - Collect line counts. This must be used after
224 /// reading .gcno and .gcda files.
225 void GCOVFunction::collectLineCounts(FileInfo &FI) {
226 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
227 E = Blocks.end(); I != E; ++I)
228 (*I)->collectLineCounts(FI);
231 //===----------------------------------------------------------------------===//
232 // GCOVBlock implementation.
234 /// ~GCOVBlock - Delete GCOVBlock and its content.
235 GCOVBlock::~GCOVBlock() {
237 DeleteContainerSeconds(Lines);
240 void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) {
241 GCOVLines *&LinesForFile = Lines[Filename];
243 LinesForFile = new GCOVLines();
244 LinesForFile->add(LineNo);
247 /// collectLineCounts - Collect line counts. This must be used after
248 /// reading .gcno and .gcda files.
249 void GCOVBlock::collectLineCounts(FileInfo &FI) {
250 for (StringMap<GCOVLines *>::iterator I = Lines.begin(),
251 E = Lines.end(); I != E; ++I)
252 I->second->collectLineCounts(FI, I->first(), Counter);
255 /// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
256 void GCOVBlock::dump() {
257 dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
258 if (!Edges.empty()) {
259 dbgs() << "\tEdges : ";
260 for (SmallVectorImpl<uint32_t>::iterator I = Edges.begin(), E = Edges.end();
262 dbgs() << (*I) << ",";
265 if (!Lines.empty()) {
266 dbgs() << "\tLines : ";
267 for (StringMap<GCOVLines *>::iterator LI = Lines.begin(),
268 LE = Lines.end(); LI != LE; ++LI) {
269 dbgs() << LI->first() << " -> ";
276 //===----------------------------------------------------------------------===//
277 // GCOVLines implementation.
279 /// collectLineCounts - Collect line counts. This must be used after
280 /// reading .gcno and .gcda files.
281 void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename,
283 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
284 E = Lines.end(); I != E; ++I)
285 FI.addLineCount(Filename, *I, Count);
288 /// dump - Dump GCOVLines content to dbgs() for debugging purposes.
289 void GCOVLines::dump() {
290 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
291 E = Lines.end(); I != E; ++I)
292 dbgs() << (*I) << ",";
295 //===----------------------------------------------------------------------===//
296 // FileInfo implementation.
298 /// print - Print source files with collected line count information.
299 void FileInfo::print(raw_fd_ostream &OS, StringRef gcnoFile,
300 StringRef gcdaFile) {
301 for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end();
303 StringRef Filename = I->first();
304 OS << " -: 0:Source:" << Filename << "\n";
305 OS << " -: 0:Graph:" << gcnoFile << "\n";
306 OS << " -: 0:Data:" << gcdaFile << "\n";
307 OS << " -: 0:Runs:" << RunCount << "\n";
308 OS << " -: 0:Programs:" << ProgramCount << "\n";
309 LineCounts &L = LineInfo[Filename];
310 OwningPtr<MemoryBuffer> Buff;
311 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
312 errs() << Filename << ": " << ec.message() << "\n";
315 StringRef AllLines = Buff.take()->getBuffer();
317 while (!AllLines.empty()) {
318 if (L.find(i) != L.end()) {
322 OS << format("%9lu:", L[i]);
326 std::pair<StringRef, StringRef> P = AllLines.split('\n');
327 if (AllLines != P.first)
328 OS << format("%5u:", i+1) << P.first;