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/GCOV.h"
16 #include "llvm/ADT/OwningPtr.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/MemoryObject.h"
20 #include "llvm/Support/system_error.h"
23 //===----------------------------------------------------------------------===//
24 // GCOVFile implementation.
26 /// ~GCOVFile - Delete GCOVFile and its content.
27 GCOVFile::~GCOVFile() {
28 DeleteContainerPointers(Functions);
31 /// isGCDAFile - Return true if Format identifies a .gcda file.
32 static bool isGCDAFile(GCOV::GCOVFormat Format) {
33 return Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404;
36 /// isGCNOFile - Return true if Format identifies a .gcno file.
37 static bool isGCNOFile(GCOV::GCOVFormat Format) {
38 return Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404;
41 /// read - Read GCOV buffer.
42 bool GCOVFile::read(GCOVBuffer &Buffer) {
43 GCOV::GCOVFormat Format = Buffer.readGCOVFormat();
44 if (Format == GCOV::InvalidGCOV)
49 GCOVFunction *GFun = NULL;
50 if (isGCDAFile(Format)) {
51 // Use existing function while reading .gcda file.
52 assert(i < Functions.size() && ".gcda data does not match .gcno data");
54 } else if (isGCNOFile(Format)) {
55 GFun = new GCOVFunction();
56 Functions.push_back(GFun);
58 if (!GFun || !GFun->read(Buffer, Format))
65 /// dump - Dump GCOVFile content on standard out for debugging purposes.
66 void GCOVFile::dump() {
67 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
68 E = Functions.end(); I != E; ++I)
72 /// collectLineCounts - Collect line counts. This must be used after
73 /// reading .gcno and .gcda files.
74 void GCOVFile::collectLineCounts(FileInfo &FI) {
75 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
76 E = Functions.end(); I != E; ++I)
77 (*I)->collectLineCounts(FI);
80 //===----------------------------------------------------------------------===//
81 // GCOVFunction implementation.
83 /// ~GCOVFunction - Delete GCOVFunction and its content.
84 GCOVFunction::~GCOVFunction() {
85 DeleteContainerPointers(Blocks);
88 /// read - Read a function from the buffer. Return false if buffer cursor
89 /// does not point to a function tag.
90 bool GCOVFunction::read(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
91 if (!Buff.readFunctionTag())
94 Buff.readInt(); // Function header length
95 Ident = Buff.readInt();
96 Buff.readInt(); // Checksum #1
97 if (Format != GCOV::GCNO_402 && Format != GCOV::GCDA_402)
98 Buff.readInt(); // Checksum #2
100 Name = Buff.readString();
101 if (Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404)
102 Filename = Buff.readString();
104 if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) {
107 uint32_t Count = Buff.readInt() / 2;
109 // This for loop adds the counts for each block. A second nested loop is
110 // required to combine the edge counts that are contained in the GCDA file.
111 for (uint32_t Line = 0; i < Count; ++Line) {
112 GCOVBlock &Block = *Blocks[Line];
113 for (size_t Edge = 0, End = Block.getNumEdges(); Edge < End; ++Edge) {
114 assert(i < Count && "Unexpected number of Edges!");
115 Block.addCount(Buff.readInt64());
122 LineNumber = Buff.readInt();
125 bool BlockTagFound = Buff.readBlockTag();
127 assert(BlockTagFound && "Block Tag not found!");
128 uint32_t BlockCount = Buff.readInt();
129 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
130 Buff.readInt(); // Block flags;
131 Blocks.push_back(new GCOVBlock(i));
135 while (Buff.readEdgeTag()) {
136 uint32_t EdgeCount = (Buff.readInt() - 1) / 2;
137 uint32_t BlockNo = Buff.readInt();
138 assert(BlockNo < BlockCount && "Unexpected Block number!");
139 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
140 Blocks[BlockNo]->addEdge(Buff.readInt());
141 Buff.readInt(); // Edge flag
146 while (Buff.readLineTag()) {
147 uint32_t LineTableLength = Buff.readInt();
148 uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
149 uint32_t BlockNo = Buff.readInt();
150 assert(BlockNo < BlockCount && "Unexpected Block number!");
151 GCOVBlock *Block = Blocks[BlockNo];
152 Buff.readInt(); // flag
153 while (Buff.getCursor() != (EndPos - 4)) {
154 StringRef Filename = Buff.readString();
155 if (Buff.getCursor() == (EndPos - 4)) break;
156 while (uint32_t L = Buff.readInt())
157 Block->addLine(Filename, L);
159 Buff.readInt(); // flag
164 /// dump - Dump GCOVFunction content on standard out for debugging purposes.
165 void GCOVFunction::dump() {
166 outs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
167 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
168 E = Blocks.end(); I != E; ++I)
172 /// collectLineCounts - Collect line counts. This must be used after
173 /// reading .gcno and .gcda files.
174 void GCOVFunction::collectLineCounts(FileInfo &FI) {
175 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
176 E = Blocks.end(); I != E; ++I)
177 (*I)->collectLineCounts(FI);
180 //===----------------------------------------------------------------------===//
181 // GCOVBlock implementation.
183 /// ~GCOVBlock - Delete GCOVBlock and its content.
184 GCOVBlock::~GCOVBlock() {
186 DeleteContainerSeconds(Lines);
189 void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) {
190 GCOVLines *&LinesForFile = Lines[Filename];
192 LinesForFile = new GCOVLines();
193 LinesForFile->add(LineNo);
196 /// collectLineCounts - Collect line counts. This must be used after
197 /// reading .gcno and .gcda files.
198 void GCOVBlock::collectLineCounts(FileInfo &FI) {
199 for (StringMap<GCOVLines *>::iterator I = Lines.begin(),
200 E = Lines.end(); I != E; ++I)
201 I->second->collectLineCounts(FI, I->first(), Counter);
204 /// dump - Dump GCOVBlock content on standard out for debugging purposes.
205 void GCOVBlock::dump() {
206 outs() << "Block : " << Number << " Counter : " << Counter << "\n";
207 if (!Edges.empty()) {
208 outs() << "\tEdges : ";
209 for (SmallVectorImpl<uint32_t>::iterator I = Edges.begin(), E = Edges.end();
211 outs() << (*I) << ",";
214 if (!Lines.empty()) {
215 outs() << "\tLines : ";
216 for (StringMap<GCOVLines *>::iterator LI = Lines.begin(),
217 LE = Lines.end(); LI != LE; ++LI) {
218 outs() << LI->first() << " -> ";
225 //===----------------------------------------------------------------------===//
226 // GCOVLines implementation.
228 /// collectLineCounts - Collect line counts. This must be used after
229 /// reading .gcno and .gcda files.
230 void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename,
232 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
233 E = Lines.end(); I != E; ++I)
234 FI.addLineCount(Filename, *I, Count);
237 /// dump - Dump GCOVLines content on standard out for debugging purposes.
238 void GCOVLines::dump() {
239 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
240 E = Lines.end(); I != E; ++I)
241 outs() << (*I) << ",";
244 //===----------------------------------------------------------------------===//
245 // FileInfo implementation.
247 /// print - Print source files with collected line count information.
248 void FileInfo::print(StringRef gcnoFile, StringRef gcdaFile) {
249 for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end();
251 StringRef Filename = I->first();
252 outs() << " -: 0:Source:" << Filename << "\n";
253 outs() << " -: 0:Graph:" << gcnoFile << "\n";
254 outs() << " -: 0:Data:" << gcdaFile << "\n";
255 LineCounts &L = LineInfo[Filename];
256 OwningPtr<MemoryBuffer> Buff;
257 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
258 errs() << Filename << ": " << ec.message() << "\n";
261 StringRef AllLines = Buff.take()->getBuffer();
263 while (!AllLines.empty()) {
264 if (L.find(i) != L.end()) {
268 outs() << format("%9lu:", L[i]);
272 std::pair<StringRef, StringRef> P = AllLines.split('\n');
273 if (AllLines != P.first)
274 outs() << format("%5u:", i+1) << P.first;