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->readGCNO(Buffer, Format))
55 Functions.push_back(GFun);
57 } else if (isGCDAFile(Format)) {
59 if (!Buffer.readInt(Checksum2)) return false;
60 if (Checksum != Checksum2) {
61 errs() << "File checksum does not match.\n";
64 for (size_t i = 0, e = Functions.size(); i < e; ++i) {
65 if (!Buffer.readFunctionTag()) {
66 errs() << "Unexpected number of functions.\n";
69 if (!Functions[i]->readGCDA(Buffer, Format))
72 if (Buffer.readObjectTag()) {
75 if (!Buffer.readInt(Length)) return false;
76 if (!Buffer.readInt(Dummy)) return false; // checksum
77 if (!Buffer.readInt(Dummy)) return false; // num
78 if (!Buffer.readInt(RunCount)) return false;;
79 Buffer.advanceCursor(Length-3);
81 while (Buffer.readProgramTag()) {
83 if (!Buffer.readInt(Length)) return false;
84 Buffer.advanceCursor(Length);
92 /// dump - Dump GCOVFile content to dbgs() for debugging purposes.
93 void GCOVFile::dump() const {
94 for (SmallVectorImpl<GCOVFunction *>::const_iterator I = Functions.begin(),
95 E = Functions.end(); I != E; ++I)
99 /// collectLineCounts - Collect line counts. This must be used after
100 /// reading .gcno and .gcda files.
101 void GCOVFile::collectLineCounts(FileInfo &FI) {
102 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
103 E = Functions.end(); I != E; ++I)
104 (*I)->collectLineCounts(FI);
105 FI.setRunCount(RunCount);
106 FI.setProgramCount(ProgramCount);
109 //===----------------------------------------------------------------------===//
110 // GCOVFunction implementation.
112 /// ~GCOVFunction - Delete GCOVFunction and its content.
113 GCOVFunction::~GCOVFunction() {
114 DeleteContainerPointers(Blocks);
117 /// readGCNO - Read a function from the GCNO buffer. Return false if an error
119 bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
121 if (!Buff.readInt(Dummy)) return false; // Function header length
122 if (!Buff.readInt(Ident)) return false;
123 if (!Buff.readInt(Dummy)) return false; // Checksum #1
124 if (Format != GCOV::GCNO_402)
125 if (!Buff.readInt(Dummy)) return false; // Checksum #2
127 if (!Buff.readString(Name)) return false;
128 if (!Buff.readString(Filename)) return false;
129 if (!Buff.readInt(LineNumber)) return false;
132 if (!Buff.readBlockTag()) {
133 errs() << "Block tag not found.\n";
137 if (!Buff.readInt(BlockCount)) return false;
138 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
139 if (!Buff.readInt(Dummy)) return false; // Block flags;
140 Blocks.push_back(new GCOVBlock(*this, i));
144 while (Buff.readEdgeTag()) {
146 if (!Buff.readInt(EdgeCount)) return false;
147 EdgeCount = (EdgeCount - 1) / 2;
149 if (!Buff.readInt(BlockNo)) return false;
150 if (BlockNo >= BlockCount) {
151 errs() << "Unexpected block number.\n";
154 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
156 if (!Buff.readInt(Dst)) return false;
157 Blocks[BlockNo]->addEdge(Dst);
158 if (!Buff.readInt(Dummy)) return false; // Edge flag
163 while (Buff.readLineTag()) {
164 uint32_t LineTableLength;
165 if (!Buff.readInt(LineTableLength)) return false;
166 uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
168 if (!Buff.readInt(BlockNo)) return false;
169 if (BlockNo >= BlockCount) {
170 errs() << "Unexpected block number.\n";
173 GCOVBlock *Block = Blocks[BlockNo];
174 if (!Buff.readInt(Dummy)) return false; // flag
175 while (Buff.getCursor() != (EndPos - 4)) {
177 if (!Buff.readString(F)) return false;
179 errs() << "Multiple sources for a single basic block.\n";
182 if (Buff.getCursor() == (EndPos - 4)) break;
185 if (!Buff.readInt(Line)) return false;
187 Block->addLine(Line);
190 if (!Buff.readInt(Dummy)) return false; // flag
195 /// readGCDA - Read a function from the GCDA buffer. Return false if an error
197 bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
199 if (!Buff.readInt(Dummy)) return false; // Function header length
200 if (!Buff.readInt(Ident)) return false;
201 if (!Buff.readInt(Dummy)) return false; // Checksum #1
202 if (Format != GCOV::GCDA_402)
203 if (!Buff.readInt(Dummy)) return false; // Checksum #2
205 if (!Buff.readString(Name)) return false;
207 if (!Buff.readArcTag()) {
208 errs() << "Arc tag not found.\n";
212 if (!Buff.readInt(Count)) return false;
215 // This for loop adds the counts for each block. A second nested loop is
216 // required to combine the edge counts that are contained in the GCDA file.
217 for (uint32_t Line = 0; Count > 0; ++Line) {
218 if (Line >= Blocks.size()) {
219 errs() << "Unexpected number of edges.\n";
222 GCOVBlock &Block = *Blocks[Line];
223 for (size_t Edge = 0, End = Block.getNumEdges(); Edge < End; ++Edge) {
225 errs() << "Unexpected number of edges.\n";
229 if (!Buff.readInt64(ArcCount)) return false;
230 Block.addCount(ArcCount);
237 /// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
238 void GCOVFunction::dump() const {
239 dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
240 for (SmallVectorImpl<GCOVBlock *>::const_iterator I = Blocks.begin(),
241 E = Blocks.end(); I != E; ++I)
245 /// collectLineCounts - Collect line counts. This must be used after
246 /// reading .gcno and .gcda files.
247 void GCOVFunction::collectLineCounts(FileInfo &FI) {
248 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
249 E = Blocks.end(); I != E; ++I)
250 (*I)->collectLineCounts(FI);
253 //===----------------------------------------------------------------------===//
254 // GCOVBlock implementation.
256 /// ~GCOVBlock - Delete GCOVBlock and its content.
257 GCOVBlock::~GCOVBlock() {
262 /// collectLineCounts - Collect line counts. This must be used after
263 /// reading .gcno and .gcda files.
264 void GCOVBlock::collectLineCounts(FileInfo &FI) {
265 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
266 E = Lines.end(); I != E; ++I)
267 FI.addLineCount(Parent.getFilename(), *I, Counter);
270 /// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
271 void GCOVBlock::dump() const {
272 dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
273 if (!Edges.empty()) {
274 dbgs() << "\tEdges : ";
275 for (SmallVectorImpl<uint32_t>::const_iterator I = Edges.begin(), E = Edges.end();
277 dbgs() << (*I) << ",";
280 if (!Lines.empty()) {
281 dbgs() << "\tLines : ";
282 for (SmallVectorImpl<uint32_t>::const_iterator I = Lines.begin(),
283 E = Lines.end(); I != E; ++I)
284 dbgs() << (*I) << ",";
289 //===----------------------------------------------------------------------===//
290 // FileInfo implementation.
292 /// print - Print source files with collected line count information.
293 void FileInfo::print(raw_fd_ostream &OS, StringRef gcnoFile,
294 StringRef gcdaFile) const {
295 for (StringMap<LineCounts>::const_iterator I = LineInfo.begin(),
296 E = LineInfo.end(); I != E; ++I) {
297 StringRef Filename = I->first();
298 OwningPtr<MemoryBuffer> Buff;
299 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
300 errs() << Filename << ": " << ec.message() << "\n";
303 StringRef AllLines = Buff->getBuffer();
305 OS << " -: 0:Source:" << Filename << "\n";
306 OS << " -: 0:Graph:" << gcnoFile << "\n";
307 OS << " -: 0:Data:" << gcdaFile << "\n";
308 OS << " -: 0:Runs:" << RunCount << "\n";
309 OS << " -: 0:Programs:" << ProgramCount << "\n";
311 const LineCounts &L = I->second;
313 while (!AllLines.empty()) {
314 LineCounts::const_iterator CountIt = L.find(i);
315 if (CountIt != L.end()) {
316 if (CountIt->second == 0)
319 OS << format("%9" PRIu64 ":", CountIt->second);
323 std::pair<StringRef, StringRef> P = AllLines.split('\n');
324 if (AllLines != P.first)
325 OS << format("%5u:", i+1) << P.first;