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/MemoryObject.h"
19 #include "llvm/Support/system_error.h"
22 //===----------------------------------------------------------------------===//
23 // GCOVFile implementation.
25 /// ~GCOVFile - Delete GCOVFile and its content.
26 GCOVFile::~GCOVFile() {
27 DeleteContainerPointers(Functions);
30 /// isGCDAFile - Return true if Format identifies a .gcda file.
31 static bool isGCDAFile(GCOV::GCOVFormat Format) {
32 return Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404;
35 /// isGCNOFile - Return true if Format identifies a .gcno file.
36 static bool isGCNOFile(GCOV::GCOVFormat Format) {
37 return Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404;
40 /// read - Read GCOV buffer.
41 bool GCOVFile::read(GCOVBuffer &Buffer) {
42 GCOV::GCOVFormat Format = Buffer.readGCOVFormat();
43 if (Format == GCOV::InvalidGCOV)
48 GCOVFunction *GFun = NULL;
49 if (isGCDAFile(Format)) {
50 // Use existing function while reading .gcda file.
51 assert(i < Functions.size() && ".gcda data does not match .gcno data");
53 } else if (isGCNOFile(Format)){
54 GFun = new GCOVFunction();
55 Functions.push_back(GFun);
57 if (!GFun || !GFun->read(Buffer, Format))
64 /// dump - Dump GCOVFile content on standard out for debugging purposes.
65 void GCOVFile::dump() {
66 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
67 E = Functions.end(); I != E; ++I)
71 /// collectLineCounts - Collect line counts. This must be used after
72 /// reading .gcno and .gcda files.
73 void GCOVFile::collectLineCounts(FileInfo &FI) {
74 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
75 E = Functions.end(); I != E; ++I)
76 (*I)->collectLineCounts(FI);
79 //===----------------------------------------------------------------------===//
80 // GCOVFunction implementation.
82 /// ~GCOVFunction - Delete GCOVFunction and its content.
83 GCOVFunction::~GCOVFunction() {
84 DeleteContainerPointers(Blocks);
87 /// read - Read a aunction from the buffer. Return false if buffer cursor
88 /// does not point to a function tag.
89 bool GCOVFunction::read(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
90 if (!Buff.readFunctionTag())
93 Buff.readInt(); // Function header length
94 Ident = Buff.readInt();
95 Buff.readInt(); // Checksum #1
96 if (Format != GCOV::GCNO_402 && Format != GCOV::GCDA_402)
97 Buff.readInt(); // Checksum #2
99 Name = Buff.readString();
100 if (Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404)
101 Filename = Buff.readString();
103 if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) {
105 uint32_t Count = Buff.readInt() / 2;
106 for (unsigned i = 0, e = Count; i != e; ++i) {
107 Blocks[i]->addCount(Buff.readInt64());
112 LineNumber = Buff.readInt();
115 bool BlockTagFound = Buff.readBlockTag();
117 assert(BlockTagFound && "Block Tag not found!");
118 uint32_t BlockCount = Buff.readInt();
119 for (int i = 0, e = BlockCount; i != e; ++i) {
120 Buff.readInt(); // Block flags;
121 Blocks.push_back(new GCOVBlock(i));
125 while (Buff.readEdgeTag()) {
126 uint32_t EdgeCount = (Buff.readInt() - 1) / 2;
127 uint32_t BlockNo = Buff.readInt();
128 assert(BlockNo < BlockCount && "Unexpected Block number!");
129 for (int i = 0, e = EdgeCount; i != e; ++i) {
130 Blocks[BlockNo]->addEdge(Buff.readInt());
131 Buff.readInt(); // Edge flag
136 while (Buff.readLineTag()) {
137 uint32_t LineTableLength = Buff.readInt();
138 uint32_t Size = Buff.getCursor() + LineTableLength*4;
139 uint32_t BlockNo = Buff.readInt();
140 assert(BlockNo < BlockCount && "Unexpected Block number!");
141 GCOVBlock *Block = Blocks[BlockNo];
142 Buff.readInt(); // flag
143 while (Buff.getCursor() != (Size - 4)) {
144 StringRef Filename = Buff.readString();
145 if (Buff.getCursor() == (Size - 4)) break;
146 while (uint32_t L = Buff.readInt())
147 Block->addLine(Filename, L);
149 Buff.readInt(); // flag
154 /// dump - Dump GCOVFunction content on standard out for debugging purposes.
155 void GCOVFunction::dump() {
156 outs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
157 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
158 E = Blocks.end(); I != E; ++I)
162 /// collectLineCounts - Collect line counts. This must be used after
163 /// reading .gcno and .gcda files.
164 void GCOVFunction::collectLineCounts(FileInfo &FI) {
165 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
166 E = Blocks.end(); I != E; ++I)
167 (*I)->collectLineCounts(FI);
170 //===----------------------------------------------------------------------===//
171 // GCOVBlock implementation.
173 /// ~GCOVBlock - Delete GCOVBlock and its content.
174 GCOVBlock::~GCOVBlock() {
176 DeleteContainerSeconds(Lines);
179 void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) {
180 GCOVLines *&LinesForFile = Lines[Filename];
182 LinesForFile = new GCOVLines();
183 LinesForFile->add(LineNo);
186 /// collectLineCounts - Collect line counts. This must be used after
187 /// reading .gcno and .gcda files.
188 void GCOVBlock::collectLineCounts(FileInfo &FI) {
189 for (StringMap<GCOVLines *>::iterator I = Lines.begin(),
190 E = Lines.end(); I != E; ++I)
191 I->second->collectLineCounts(FI, I->first(), Counter);
194 /// dump - Dump GCOVBlock content on standard out for debugging purposes.
195 void GCOVBlock::dump() {
196 outs() << "Block : " << Number << " Counter : " << Counter << "\n";
197 if (!Edges.empty()) {
198 outs() << "\tEdges : ";
199 for (SmallVectorImpl<uint32_t>::iterator I = Edges.begin(), E = Edges.end();
201 outs() << (*I) << ",";
204 if (!Lines.empty()) {
205 outs() << "\tLines : ";
206 for (StringMap<GCOVLines *>::iterator LI = Lines.begin(),
207 LE = Lines.end(); LI != LE; ++LI) {
208 outs() << LI->first() << " -> ";
215 //===----------------------------------------------------------------------===//
216 // GCOVLines implementation.
218 /// collectLineCounts - Collect line counts. This must be used after
219 /// reading .gcno and .gcda files.
220 void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename,
222 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
223 E = Lines.end(); I != E; ++I)
224 FI.addLineCount(Filename, *I, Count);
227 /// dump - Dump GCOVLines content on standard out for debugging purposes.
228 void GCOVLines::dump() {
229 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
230 E = Lines.end(); I != E; ++I)
231 outs() << (*I) << ",";
234 //===----------------------------------------------------------------------===//
235 // FileInfo implementation.
237 /// addLineCount - Add line count for the given line number in a file.
238 void FileInfo::addLineCount(StringRef Filename, uint32_t Line, uint32_t Count) {
239 if (LineInfo.find(Filename) == LineInfo.end()) {
240 OwningPtr<MemoryBuffer> Buff;
241 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
242 errs() << Filename << ": " << ec.message() << "\n";
245 StringRef AllLines = Buff.take()->getBuffer();
246 LineCounts L(AllLines.count('\n')+2);
248 LineInfo[Filename] = L;
251 LineCounts &L = LineInfo[Filename];
255 /// print - Print source files with collected line count information.
256 void FileInfo::print() {
257 for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end();
259 StringRef Filename = I->first();
260 outs() << Filename << "\n";
261 LineCounts &L = LineInfo[Filename];
262 OwningPtr<MemoryBuffer> Buff;
263 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
264 errs() << Filename << ": " << ec.message() << "\n";
267 StringRef AllLines = Buff.take()->getBuffer();
268 for (unsigned i = 0, e = L.size(); i != e; ++i) {
270 outs() << L[i] << ":\t";
273 std::pair<StringRef, StringRef> P = AllLines.split('\n');
274 if (AllLines != P.first)