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"
39 } // end GCOV namespace
41 /// GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific
45 GCOVBuffer(MemoryBuffer *B) : Buffer(B), Cursor(0) {}
47 /// readGCOVFormat - Read GCOV signature at the beginning of buffer.
48 GCOV::GCOVFormat readGCOVFormat() {
49 StringRef Magic = Buffer->getBuffer().slice(0, 12);
51 if (Magic == "oncg*404MVLL")
52 return GCOV::GCNO_404;
53 else if (Magic == "oncg*204MVLL")
54 return GCOV::GCNO_402;
55 else if (Magic == "adcg*404MVLL")
56 return GCOV::GCDA_404;
57 else if (Magic == "adcg*204MVLL")
58 return GCOV::GCDA_402;
61 return GCOV::InvalidGCOV;
64 /// readFunctionTag - If cursor points to a function tag then increment the
65 /// cursor and return true otherwise return false.
66 bool readFunctionTag() {
67 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
69 Tag[0] != '\0' || Tag[1] != '\0' ||
70 Tag[2] != '\0' || Tag[3] != '\1') {
77 /// readBlockTag - If cursor points to a block tag then increment the
78 /// cursor and return true otherwise return false.
80 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
82 Tag[0] != '\0' || Tag[1] != '\0' ||
83 Tag[2] != '\x41' || Tag[3] != '\x01') {
90 /// readEdgeTag - If cursor points to an edge tag then increment the
91 /// cursor and return true otherwise return false.
93 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
95 Tag[0] != '\0' || Tag[1] != '\0' ||
96 Tag[2] != '\x43' || Tag[3] != '\x01') {
103 /// readLineTag - If cursor points to a line tag then increment the
104 /// cursor and return true otherwise return false.
106 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
108 Tag[0] != '\0' || Tag[1] != '\0' ||
109 Tag[2] != '\x45' || Tag[3] != '\x01') {
116 /// readArcTag - If cursor points to an gcda arc tag then increment the
117 /// cursor and return true otherwise return false.
119 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
121 Tag[0] != '\0' || Tag[1] != '\0' ||
122 Tag[2] != '\xa1' || Tag[3] != '\1') {
129 /// readProgramTag - If cursor points to a program summary tag then increment
130 /// the cursor and return true otherwise return false.
131 bool readProgramTag() {
132 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
134 Tag[0] != '\0' || Tag[1] != '\0' ||
135 Tag[2] != '\0' || Tag[3] != '\xa3') {
144 StringRef Str = Buffer->getBuffer().slice(Cursor, Cursor+4);
145 assert (Str.empty() == false && "Unexpected memory buffer end!");
147 Result = *(const uint32_t *)(Str.data());
151 uint64_t readInt64() {
152 uint64_t Lo = readInt();
153 uint64_t Hi = readInt();
154 uint64_t Result = Lo | (Hi << 32);
158 StringRef readString() {
159 uint32_t Len = readInt() * 4;
160 StringRef Str = Buffer->getBuffer().slice(Cursor, Cursor+Len);
162 return Str.split('\0').first;
165 uint64_t getCursor() const { return Cursor; }
167 MemoryBuffer *Buffer;
171 /// GCOVFile - Collects coverage information for one pair of coverage file
172 /// (.gcno and .gcda).
175 GCOVFile() : Functions(), ProgramCount(0) {}
177 bool read(GCOVBuffer &Buffer);
179 void collectLineCounts(FileInfo &FI);
181 SmallVector<GCOVFunction *, 16> Functions;
182 uint32_t ProgramCount;
185 /// GCOVFunction - Collects function information.
188 GCOVFunction() : Ident(0), LineNumber(0) {}
190 bool read(GCOVBuffer &Buffer, GCOV::GCOVFormat Format);
192 void collectLineCounts(FileInfo &FI);
198 SmallVector<GCOVBlock *, 16> Blocks;
201 /// GCOVBlock - Collects block information.
204 GCOVBlock(uint32_t N) : Number(N), Counter(0) {}
206 void addEdge(uint32_t N) { Edges.push_back(N); }
207 void addLine(StringRef Filename, uint32_t LineNo);
208 void addCount(uint64_t N) { Counter += N; }
209 size_t getNumEdges() { return Edges.size(); }
211 void collectLineCounts(FileInfo &FI);
215 SmallVector<uint32_t, 16> Edges;
216 StringMap<GCOVLines *> Lines;
219 /// GCOVLines - A wrapper around a vector of int to keep track of line nos.
222 ~GCOVLines() { Lines.clear(); }
223 void add(uint32_t N) { Lines.push_back(N); }
224 void collectLineCounts(FileInfo &FI, StringRef Filename, uint64_t Count);
228 SmallVector<uint32_t, 4> Lines;
231 typedef DenseMap<uint32_t, uint64_t> LineCounts;
234 void addLineCount(StringRef Filename, uint32_t Line, uint64_t Count) {
235 LineInfo[Filename][Line-1] += Count;
237 void setProgramCount(uint32_t PC) { ProgramCount = PC; }
238 void print(StringRef gcnoFile, StringRef gcdaFile);
240 StringMap<LineCounts> LineInfo;
241 uint32_t ProgramCount;