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 if (Buffer.readObjectTag()) {
63 uint32_t Length = Buffer.readInt();
64 Buffer.readInt(); // checksum
65 Buffer.readInt(); // num
66 RunCount = Buffer.readInt();
67 Buffer.advanceCursor(Length-3);
69 while (Buffer.readProgramTag()) {
70 uint32_t Length = Buffer.readInt();
71 Buffer.advanceCursor(Length);
79 /// dump - Dump GCOVFile content to dbgs() for debugging purposes.
80 void GCOVFile::dump() {
81 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
82 E = Functions.end(); I != E; ++I)
86 /// collectLineCounts - Collect line counts. This must be used after
87 /// reading .gcno and .gcda files.
88 void GCOVFile::collectLineCounts(FileInfo &FI) {
89 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
90 E = Functions.end(); I != E; ++I)
91 (*I)->collectLineCounts(FI);
92 FI.setRunCount(RunCount);
93 FI.setProgramCount(ProgramCount);
96 //===----------------------------------------------------------------------===//
97 // GCOVFunction implementation.
99 /// ~GCOVFunction - Delete GCOVFunction and its content.
100 GCOVFunction::~GCOVFunction() {
101 DeleteContainerPointers(Blocks);
104 /// read - Read a function from the buffer. Return false if buffer cursor
105 /// does not point to a function tag.
106 bool GCOVFunction::read(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
107 if (!Buff.readFunctionTag())
110 Buff.readInt(); // Function header length
111 Ident = Buff.readInt();
112 Buff.readInt(); // Checksum #1
113 if (Format != GCOV::GCNO_402 && Format != GCOV::GCDA_402)
114 Buff.readInt(); // Checksum #2
116 Name = Buff.readString();
117 if (Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404)
118 Filename = Buff.readString();
120 if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) {
123 uint32_t Count = Buff.readInt() / 2;
125 // This for loop adds the counts for each block. A second nested loop is
126 // required to combine the edge counts that are contained in the GCDA file.
127 for (uint32_t Line = 0; i < Count; ++Line) {
128 GCOVBlock &Block = *Blocks[Line];
129 for (size_t Edge = 0, End = Block.getNumEdges(); Edge < End; ++Edge) {
130 assert(i < Count && "Unexpected number of Edges!");
131 Block.addCount(Buff.readInt64());
138 LineNumber = Buff.readInt();
141 bool BlockTagFound = Buff.readBlockTag();
143 assert(BlockTagFound && "Block Tag not found!");
144 uint32_t BlockCount = Buff.readInt();
145 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
146 Buff.readInt(); // Block flags;
147 Blocks.push_back(new GCOVBlock(i));
151 while (Buff.readEdgeTag()) {
152 uint32_t EdgeCount = (Buff.readInt() - 1) / 2;
153 uint32_t BlockNo = Buff.readInt();
154 assert(BlockNo < BlockCount && "Unexpected Block number!");
155 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
156 Blocks[BlockNo]->addEdge(Buff.readInt());
157 Buff.readInt(); // Edge flag
162 while (Buff.readLineTag()) {
163 uint32_t LineTableLength = Buff.readInt();
164 uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
165 uint32_t BlockNo = Buff.readInt();
166 assert(BlockNo < BlockCount && "Unexpected Block number!");
167 GCOVBlock *Block = Blocks[BlockNo];
168 Buff.readInt(); // flag
169 while (Buff.getCursor() != (EndPos - 4)) {
170 StringRef Filename = Buff.readString();
171 if (Buff.getCursor() == (EndPos - 4)) break;
172 while (uint32_t L = Buff.readInt())
173 Block->addLine(Filename, L);
175 Buff.readInt(); // flag
180 /// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
181 void GCOVFunction::dump() {
182 dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
183 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
184 E = Blocks.end(); I != E; ++I)
188 /// collectLineCounts - Collect line counts. This must be used after
189 /// reading .gcno and .gcda files.
190 void GCOVFunction::collectLineCounts(FileInfo &FI) {
191 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
192 E = Blocks.end(); I != E; ++I)
193 (*I)->collectLineCounts(FI);
196 //===----------------------------------------------------------------------===//
197 // GCOVBlock implementation.
199 /// ~GCOVBlock - Delete GCOVBlock and its content.
200 GCOVBlock::~GCOVBlock() {
202 DeleteContainerSeconds(Lines);
205 void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) {
206 GCOVLines *&LinesForFile = Lines[Filename];
208 LinesForFile = new GCOVLines();
209 LinesForFile->add(LineNo);
212 /// collectLineCounts - Collect line counts. This must be used after
213 /// reading .gcno and .gcda files.
214 void GCOVBlock::collectLineCounts(FileInfo &FI) {
215 for (StringMap<GCOVLines *>::iterator I = Lines.begin(),
216 E = Lines.end(); I != E; ++I)
217 I->second->collectLineCounts(FI, I->first(), Counter);
220 /// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
221 void GCOVBlock::dump() {
222 dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
223 if (!Edges.empty()) {
224 dbgs() << "\tEdges : ";
225 for (SmallVectorImpl<uint32_t>::iterator I = Edges.begin(), E = Edges.end();
227 dbgs() << (*I) << ",";
230 if (!Lines.empty()) {
231 dbgs() << "\tLines : ";
232 for (StringMap<GCOVLines *>::iterator LI = Lines.begin(),
233 LE = Lines.end(); LI != LE; ++LI) {
234 dbgs() << LI->first() << " -> ";
241 //===----------------------------------------------------------------------===//
242 // GCOVLines implementation.
244 /// collectLineCounts - Collect line counts. This must be used after
245 /// reading .gcno and .gcda files.
246 void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename,
248 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
249 E = Lines.end(); I != E; ++I)
250 FI.addLineCount(Filename, *I, Count);
253 /// dump - Dump GCOVLines content to dbgs() for debugging purposes.
254 void GCOVLines::dump() {
255 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
256 E = Lines.end(); I != E; ++I)
257 dbgs() << (*I) << ",";
260 //===----------------------------------------------------------------------===//
261 // FileInfo implementation.
263 /// print - Print source files with collected line count information.
264 void FileInfo::print(raw_fd_ostream &OS, StringRef gcnoFile,
265 StringRef gcdaFile) {
266 for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end();
268 StringRef Filename = I->first();
269 OwningPtr<MemoryBuffer> Buff;
270 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
271 errs() << Filename << ": " << ec.message() << "\n";
274 StringRef AllLines = Buff.take()->getBuffer();
276 OS << " -: 0:Source:" << Filename << "\n";
277 OS << " -: 0:Graph:" << gcnoFile << "\n";
278 OS << " -: 0:Data:" << gcdaFile << "\n";
279 OS << " -: 0:Runs:" << RunCount << "\n";
280 OS << " -: 0:Programs:" << ProgramCount << "\n";
282 LineCounts &L = LineInfo[Filename];
284 while (!AllLines.empty()) {
285 if (L.find(i) != L.end()) {
289 OS << format("%9lu:", L[i]);
293 std::pair<StringRef, StringRef> P = AllLines.split('\n');
294 if (AllLines != P.first)
295 OS << format("%5u:", i+1) << P.first;