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)) {
49 if (!Buffer.readInt(Checksum)) return false;
51 if (!Buffer.readFunctionTag()) break;
52 GCOVFunction *GFun = new GCOVFunction();
53 if (!GFun->read(Buffer, Format))
55 Functions.push_back(GFun);
58 else if (isGCDAFile(Format)) {
60 if (!Buffer.readInt(Checksum2)) return false;
61 if (Checksum != Checksum2) {
62 errs() << "File checksum does not match.\n";
65 for (size_t i = 0, e = Functions.size(); i < e; ++i) {
66 if (!Buffer.readFunctionTag()) {
67 errs() << "Unexpected number of functions.\n";
70 if (!Functions[i]->read(Buffer, Format))
73 if (Buffer.readObjectTag()) {
76 if (!Buffer.readInt(Length)) return false;
77 if (!Buffer.readInt(Dummy)) return false; // checksum
78 if (!Buffer.readInt(Dummy)) return false; // num
79 if (!Buffer.readInt(RunCount)) return false;;
80 Buffer.advanceCursor(Length-3);
82 while (Buffer.readProgramTag()) {
84 if (!Buffer.readInt(Length)) return false;
85 Buffer.advanceCursor(Length);
93 /// dump - Dump GCOVFile content to dbgs() for debugging purposes.
94 void GCOVFile::dump() const {
95 for (SmallVectorImpl<GCOVFunction *>::const_iterator I = Functions.begin(),
96 E = Functions.end(); I != E; ++I)
100 /// collectLineCounts - Collect line counts. This must be used after
101 /// reading .gcno and .gcda files.
102 void GCOVFile::collectLineCounts(FileInfo &FI) {
103 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
104 E = Functions.end(); I != E; ++I)
105 (*I)->collectLineCounts(FI);
106 FI.setRunCount(RunCount);
107 FI.setProgramCount(ProgramCount);
110 //===----------------------------------------------------------------------===//
111 // GCOVFunction implementation.
113 /// ~GCOVFunction - Delete GCOVFunction and its content.
114 GCOVFunction::~GCOVFunction() {
115 DeleteContainerPointers(Blocks);
118 /// read - Read a function from the buffer. Return false if buffer cursor
119 /// does not point to a function tag.
120 bool GCOVFunction::read(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
122 if (!Buff.readInt(Dummy)) return false; // Function header length
123 if (!Buff.readInt(Ident)) return false;
124 if (!Buff.readInt(Dummy)) return false; // Checksum #1
125 if (Format != GCOV::GCNO_402 && Format != GCOV::GCDA_402)
126 if (!Buff.readInt(Dummy)) return false; // Checksum #2
128 if (!Buff.readString(Name)) return false;
130 if (Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404)
131 if (!Buff.readString(Filename)) return false;
133 if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) {
134 if (!Buff.readArcTag()) {
135 errs() << "Arc tag not found.\n";
139 if (!Buff.readInt(Count)) return false;
142 // This for loop adds the counts for each block. A second nested loop is
143 // required to combine the edge counts that are contained in the GCDA file.
144 for (uint32_t Line = 0; Count > 0; ++Line) {
145 if (Line >= Blocks.size()) {
146 errs() << "Unexpected number of edges.\n";
149 GCOVBlock &Block = *Blocks[Line];
150 for (size_t Edge = 0, End = Block.getNumEdges(); Edge < End; ++Edge) {
152 errs() << "Unexpected number of edges.\n";
156 if (!Buff.readInt64(ArcCount)) return false;
157 Block.addCount(ArcCount);
164 if (!Buff.readInt(LineNumber)) return false;
167 if (!Buff.readBlockTag()) {
168 errs() << "Block tag not found.\n";
172 if (!Buff.readInt(BlockCount)) return false;
173 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
174 if (!Buff.readInt(Dummy)) return false; // Block flags;
175 Blocks.push_back(new GCOVBlock(*this, i));
179 while (Buff.readEdgeTag()) {
181 if (!Buff.readInt(EdgeCount)) return false;
182 EdgeCount = (EdgeCount - 1) / 2;
184 if (!Buff.readInt(BlockNo)) return false;
185 if (BlockNo >= BlockCount) {
186 errs() << "Unexpected block number.\n";
189 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
191 if (!Buff.readInt(Dst)) return false;
192 Blocks[BlockNo]->addEdge(Dst);
193 if (!Buff.readInt(Dummy)) return false; // Edge flag
198 while (Buff.readLineTag()) {
199 uint32_t LineTableLength;
200 if (!Buff.readInt(LineTableLength)) return false;
201 uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
203 if (!Buff.readInt(BlockNo)) return false;
204 if (BlockNo >= BlockCount) {
205 errs() << "Unexpected block number.\n";
208 GCOVBlock *Block = Blocks[BlockNo];
209 if (!Buff.readInt(Dummy)) return false; // flag
210 while (Buff.getCursor() != (EndPos - 4)) {
212 if (!Buff.readString(F)) return false;
214 errs() << "Multiple sources for a single basic block.\n";
217 if (Buff.getCursor() == (EndPos - 4)) break;
220 if (!Buff.readInt(Line)) return false;
222 Block->addLine(Line);
225 if (!Buff.readInt(Dummy)) return false; // flag
230 /// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
231 void GCOVFunction::dump() const {
232 dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
233 for (SmallVectorImpl<GCOVBlock *>::const_iterator I = Blocks.begin(),
234 E = Blocks.end(); I != E; ++I)
238 /// collectLineCounts - Collect line counts. This must be used after
239 /// reading .gcno and .gcda files.
240 void GCOVFunction::collectLineCounts(FileInfo &FI) {
241 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
242 E = Blocks.end(); I != E; ++I)
243 (*I)->collectLineCounts(FI);
246 //===----------------------------------------------------------------------===//
247 // GCOVBlock implementation.
249 /// ~GCOVBlock - Delete GCOVBlock and its content.
250 GCOVBlock::~GCOVBlock() {
255 /// collectLineCounts - Collect line counts. This must be used after
256 /// reading .gcno and .gcda files.
257 void GCOVBlock::collectLineCounts(FileInfo &FI) {
258 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
259 E = Lines.end(); I != E; ++I)
260 FI.addLineCount(Parent.getFilename(), *I, Counter);
263 /// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
264 void GCOVBlock::dump() const {
265 dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
266 if (!Edges.empty()) {
267 dbgs() << "\tEdges : ";
268 for (SmallVectorImpl<uint32_t>::const_iterator I = Edges.begin(), E = Edges.end();
270 dbgs() << (*I) << ",";
273 if (!Lines.empty()) {
274 dbgs() << "\tLines : ";
275 for (SmallVectorImpl<uint32_t>::const_iterator I = Lines.begin(),
276 E = Lines.end(); I != E; ++I)
277 dbgs() << (*I) << ",";
282 //===----------------------------------------------------------------------===//
283 // FileInfo implementation.
285 /// print - Print source files with collected line count information.
286 void FileInfo::print(raw_fd_ostream &OS, StringRef gcnoFile,
287 StringRef gcdaFile) const {
288 for (StringMap<LineCounts>::const_iterator I = LineInfo.begin(),
289 E = LineInfo.end(); I != E; ++I) {
290 StringRef Filename = I->first();
291 OwningPtr<MemoryBuffer> Buff;
292 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
293 errs() << Filename << ": " << ec.message() << "\n";
296 StringRef AllLines = Buff->getBuffer();
298 OS << " -: 0:Source:" << Filename << "\n";
299 OS << " -: 0:Graph:" << gcnoFile << "\n";
300 OS << " -: 0:Data:" << gcdaFile << "\n";
301 OS << " -: 0:Runs:" << RunCount << "\n";
302 OS << " -: 0:Programs:" << ProgramCount << "\n";
304 const LineCounts &L = I->second;
306 while (!AllLines.empty()) {
307 LineCounts::const_iterator CountIt = L.find(i);
308 if (CountIt != L.end()) {
309 if (CountIt->second == 0)
312 OS << format("%9" PRIu64 ":", CountIt->second);
316 std::pair<StringRef, StringRef> P = AllLines.split('\n');
317 if (AllLines != P.first)
318 OS << format("%5u:", i+1) << P.first;