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"
38 } // end GCOV namespace
40 /// GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific
44 GCOVBuffer(MemoryBuffer *B) : Buffer(B), Cursor(0) {}
46 /// readGCOVFormat - Read GCOV signature at the beginning of buffer.
47 GCOV::GCOVFormat readGCOVFormat() {
48 StringRef Magic = Buffer->getBuffer().slice(0, 12);
50 if (Magic == "oncg*404MVLL")
51 return GCOV::GCNO_404;
52 else if (Magic == "oncg*204MVLL")
53 return GCOV::GCNO_402;
54 else if (Magic == "adcg*404MVLL")
55 return GCOV::GCDA_404;
56 else if (Magic == "adcg*204MVLL")
57 return GCOV::GCDA_402;
60 return GCOV::InvalidGCOV;
63 /// readFunctionTag - If cursor points to a function tag then increment the
64 /// cursor and return true otherwise return false.
65 bool readFunctionTag() {
66 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
68 Tag[0] != '\0' || Tag[1] != '\0' ||
69 Tag[2] != '\0' || Tag[3] != '\1') {
76 /// readBlockTag - If cursor points to a block tag then increment the
77 /// cursor and return true otherwise return false.
79 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
81 Tag[0] != '\0' || Tag[1] != '\0' ||
82 Tag[2] != '\x41' || Tag[3] != '\x01') {
89 /// readEdgeTag - If cursor points to an edge tag then increment the
90 /// cursor and return true otherwise return false.
92 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
94 Tag[0] != '\0' || Tag[1] != '\0' ||
95 Tag[2] != '\x43' || Tag[3] != '\x01') {
102 /// readLineTag - If cursor points to a line tag then increment the
103 /// cursor and return true otherwise return false.
105 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
107 Tag[0] != '\0' || Tag[1] != '\0' ||
108 Tag[2] != '\x45' || Tag[3] != '\x01') {
115 /// readArcTag - If cursor points to an gcda arc tag then increment the
116 /// cursor and return true otherwise return false.
118 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
120 Tag[0] != '\0' || Tag[1] != '\0' ||
121 Tag[2] != '\xa1' || Tag[3] != '\1') {
128 /// readObjectTag - If cursor points to an object summary tag then increment
129 /// the cursor and return true otherwise return false.
130 bool readObjectTag() {
131 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
133 Tag[0] != '\0' || Tag[1] != '\0' ||
134 Tag[2] != '\0' || Tag[3] != '\xa1') {
141 /// readProgramTag - If cursor points to a program summary tag then increment
142 /// the cursor and return true otherwise return false.
143 bool readProgramTag() {
144 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
146 Tag[0] != '\0' || Tag[1] != '\0' ||
147 Tag[2] != '\0' || Tag[3] != '\xa3') {
154 bool readInt(uint32_t &Val) {
155 if (Buffer->getBuffer().size() < Cursor+4) {
156 errs() << "Unexpected end of memory buffer: " << Cursor+4 << ".\n";
159 StringRef Str = Buffer->getBuffer().slice(Cursor, Cursor+4);
161 Val = *(const uint32_t *)(Str.data());
165 bool readInt64(uint64_t &Val) {
167 if (!readInt(Lo) || !readInt(Hi)) return false;
168 Val = ((uint64_t)Hi << 32) | Lo;
172 bool readString(StringRef &Str) {
174 if (!readInt(Len)) return false;
176 if (Buffer->getBuffer().size() < Cursor+Len) {
177 errs() << "Unexpected end of memory buffer: " << Cursor+Len << ".\n";
180 Str = Buffer->getBuffer().slice(Cursor, Cursor+Len).split('\0').first;
185 uint64_t getCursor() const { return Cursor; }
186 void advanceCursor(uint32_t n) { Cursor += n*4; }
188 MemoryBuffer *Buffer;
192 /// GCOVFile - Collects coverage information for one pair of coverage file
193 /// (.gcno and .gcda).
196 GCOVFile() : Functions(), RunCount(0), ProgramCount(0) {}
198 bool read(GCOVBuffer &Buffer);
200 void collectLineCounts(FileInfo &FI);
202 SmallVector<GCOVFunction *, 16> Functions;
204 uint32_t ProgramCount;
207 /// GCOVFunction - Collects function information.
210 GCOVFunction() : Ident(0), LineNumber(0) {}
212 bool read(GCOVBuffer &Buffer, GCOV::GCOVFormat Format);
213 StringRef getFilename() const { return Filename; }
215 void collectLineCounts(FileInfo &FI);
221 SmallVector<GCOVBlock *, 16> Blocks;
224 /// GCOVBlock - Collects block information.
227 GCOVBlock(GCOVFunction &P, uint32_t N) :
228 Parent(P), Number(N), Counter(0), Edges(), Lines() {}
230 void addEdge(uint32_t N) { Edges.push_back(N); }
231 void addLine(uint32_t N) { Lines.push_back(N); }
232 void addCount(uint64_t N) { Counter += N; }
233 size_t getNumEdges() { return Edges.size(); }
235 void collectLineCounts(FileInfo &FI);
237 GCOVFunction &Parent;
240 SmallVector<uint32_t, 16> Edges;
241 SmallVector<uint32_t, 16> Lines;
244 typedef DenseMap<uint32_t, uint64_t> LineCounts;
247 void addLineCount(StringRef Filename, uint32_t Line, uint64_t Count) {
248 LineInfo[Filename][Line-1] += Count;
250 void setRunCount(uint32_t Runs) { RunCount = Runs; }
251 void setProgramCount(uint32_t Programs) { ProgramCount = Programs; }
252 void print(raw_fd_ostream &OS, StringRef gcnoFile, StringRef gcdaFile);
254 StringMap<LineCounts> LineInfo;
256 uint32_t ProgramCount;