1 //===-- llvm/Support/GCOV.h - LLVM coverage tool ----------------*- C++ -*-===//
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 // This header provides the interface to read and write coverage files that
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_GCOV_H
16 #define LLVM_SUPPORT_GCOV_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/raw_ostream.h"
35 } // end GCOV namespace
37 /// GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific
41 GCOVBuffer(MemoryBuffer *B) : Buffer(B), Cursor(0) {}
43 /// readGCNOFormat - Check GCNO signature is valid at the beginning of buffer.
44 bool readGCNOFormat() {
45 StringRef File = Buffer->getBuffer().slice(0, 4);
47 errs() << "Unexpected file type: " << File << ".\n";
54 /// readGCDAFormat - Check GCDA signature is valid at the beginning of buffer.
55 bool readGCDAFormat() {
56 StringRef File = Buffer->getBuffer().slice(0, 4);
58 errs() << "Unexpected file type: " << File << ".\n";
65 /// readGCOVVersion - Read GCOV version.
66 bool readGCOVVersion(GCOV::GCOVVersion &Version) {
67 StringRef VersionStr = Buffer->getBuffer().slice(Cursor, Cursor+4);
68 if (VersionStr == "*204") {
73 if (VersionStr == "*404") {
78 errs() << "Unexpected version: " << VersionStr << ".\n";
82 /// readFunctionTag - If cursor points to a function tag then increment the
83 /// cursor and return true otherwise return false.
84 bool readFunctionTag() {
85 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
87 Tag[0] != '\0' || Tag[1] != '\0' ||
88 Tag[2] != '\0' || Tag[3] != '\1') {
95 /// readBlockTag - If cursor points to a block tag then increment the
96 /// cursor and return true otherwise return false.
98 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
100 Tag[0] != '\0' || Tag[1] != '\0' ||
101 Tag[2] != '\x41' || Tag[3] != '\x01') {
108 /// readEdgeTag - If cursor points to an edge tag then increment the
109 /// cursor and return true otherwise return false.
111 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
113 Tag[0] != '\0' || Tag[1] != '\0' ||
114 Tag[2] != '\x43' || Tag[3] != '\x01') {
121 /// readLineTag - If cursor points to a line tag then increment the
122 /// cursor and return true otherwise return false.
124 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
126 Tag[0] != '\0' || Tag[1] != '\0' ||
127 Tag[2] != '\x45' || Tag[3] != '\x01') {
134 /// readArcTag - If cursor points to an gcda arc tag then increment the
135 /// cursor and return true otherwise return false.
137 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
139 Tag[0] != '\0' || Tag[1] != '\0' ||
140 Tag[2] != '\xa1' || Tag[3] != '\1') {
147 /// readObjectTag - If cursor points to an object summary tag then increment
148 /// the cursor and return true otherwise return false.
149 bool readObjectTag() {
150 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
152 Tag[0] != '\0' || Tag[1] != '\0' ||
153 Tag[2] != '\0' || Tag[3] != '\xa1') {
160 /// readProgramTag - If cursor points to a program summary tag then increment
161 /// the cursor and return true otherwise return false.
162 bool readProgramTag() {
163 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
165 Tag[0] != '\0' || Tag[1] != '\0' ||
166 Tag[2] != '\0' || Tag[3] != '\xa3') {
173 bool readInt(uint32_t &Val) {
174 if (Buffer->getBuffer().size() < Cursor+4) {
175 errs() << "Unexpected end of memory buffer: " << Cursor+4 << ".\n";
178 StringRef Str = Buffer->getBuffer().slice(Cursor, Cursor+4);
180 Val = *(const uint32_t *)(Str.data());
184 bool readInt64(uint64_t &Val) {
186 if (!readInt(Lo) || !readInt(Hi)) return false;
187 Val = ((uint64_t)Hi << 32) | Lo;
191 bool readString(StringRef &Str) {
193 if (!readInt(Len)) return false;
195 if (Buffer->getBuffer().size() < Cursor+Len) {
196 errs() << "Unexpected end of memory buffer: " << Cursor+Len << ".\n";
199 Str = Buffer->getBuffer().slice(Cursor, Cursor+Len).split('\0').first;
204 uint64_t getCursor() const { return Cursor; }
205 void advanceCursor(uint32_t n) { Cursor += n*4; }
207 MemoryBuffer *Buffer;
211 /// GCOVFile - Collects coverage information for one pair of coverage file
212 /// (.gcno and .gcda).
215 GCOVFile() : GCNOInitialized(false), Checksum(0), Functions(), RunCount(0),
218 bool readGCNO(GCOVBuffer &Buffer);
219 bool readGCDA(GCOVBuffer &Buffer);
220 uint32_t getChecksum() const { return Checksum; }
222 void collectLineCounts(FileInfo &FI);
224 bool GCNOInitialized;
225 GCOV::GCOVVersion Version;
227 SmallVector<GCOVFunction *, 16> Functions;
229 uint32_t ProgramCount;
233 GCOVEdge(GCOVBlock *S, GCOVBlock *D): Src(S), Dst(D), Count(0) {}
240 /// GCOVFunction - Collects function information.
243 GCOVFunction(GCOVFile &P) : Parent(P), Ident(0), LineNumber(0) {}
245 bool readGCNO(GCOVBuffer &Buffer, GCOV::GCOVVersion Version);
246 bool readGCDA(GCOVBuffer &Buffer, GCOV::GCOVVersion Version);
247 StringRef getFilename() const { return Filename; }
249 void collectLineCounts(FileInfo &FI);
257 SmallVector<GCOVBlock *, 16> Blocks;
258 SmallVector<GCOVEdge *, 16> Edges;
261 /// GCOVBlock - Collects block information.
264 typedef SmallVectorImpl<GCOVEdge *>::const_iterator EdgeIterator;
266 GCOVBlock(GCOVFunction &P, uint32_t N) :
267 Parent(P), Number(N), Counter(0), SrcEdges(), DstEdges(), Lines() {}
269 void addSrcEdge(GCOVEdge *Edge) {
270 assert(Edge->Dst == this); // up to caller to ensure edge is valid
271 SrcEdges.push_back(Edge);
273 void addDstEdge(GCOVEdge *Edge) {
274 assert(Edge->Src == this); // up to caller to ensure edge is valid
275 DstEdges.push_back(Edge);
277 void addLine(uint32_t N) { Lines.push_back(N); }
278 void addCount(size_t DstEdgeNo, uint64_t N);
279 uint64_t getCount() const { return Counter; }
280 size_t getNumSrcEdges() const { return SrcEdges.size(); }
281 size_t getNumDstEdges() const { return DstEdges.size(); }
283 EdgeIterator src_begin() const { return SrcEdges.begin(); }
284 EdgeIterator src_end() const { return SrcEdges.end(); }
285 EdgeIterator dst_begin() const { return DstEdges.begin(); }
286 EdgeIterator dst_end() const { return DstEdges.end(); }
289 void collectLineCounts(FileInfo &FI);
291 GCOVFunction &Parent;
294 SmallVector<GCOVEdge *, 16> SrcEdges;
295 SmallVector<GCOVEdge *, 16> DstEdges;
296 SmallVector<uint32_t, 16> Lines;
299 typedef SmallVector<const GCOVBlock *, 4> BlockVector;
300 typedef DenseMap<uint32_t, BlockVector> LineData;
303 void addBlockLine(StringRef Filename, uint32_t Line, const GCOVBlock *Block) {
304 LineInfo[Filename][Line-1].push_back(Block);
306 void setRunCount(uint32_t Runs) { RunCount = Runs; }
307 void setProgramCount(uint32_t Programs) { ProgramCount = Programs; }
308 void print(StringRef GCNOFile, StringRef GCDAFile) const;
310 StringMap<LineData> LineInfo;
312 uint32_t ProgramCount;