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/SmallVector.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #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') {
130 StringRef Str = Buffer->getBuffer().slice(Cursor, Cursor+4);
131 assert (Str.empty() == false && "Unexpected memory buffer end!");
133 Result = *(const uint32_t *)(Str.data());
137 uint64_t readInt64() {
138 uint64_t Lo = readInt();
139 uint64_t Hi = readInt();
140 uint64_t Result = Lo | (Hi << 32);
144 StringRef readString() {
145 uint32_t Len = readInt() * 4;
146 StringRef Str = Buffer->getBuffer().slice(Cursor, Cursor+Len);
151 uint64_t getCursor() const { return Cursor; }
153 MemoryBuffer *Buffer;
157 /// GCOVFile - Collects coverage information for one pair of coverage file
158 /// (.gcno and .gcda).
163 bool read(GCOVBuffer &Buffer);
165 void collectLineCounts(FileInfo &FI);
167 SmallVector<GCOVFunction *, 16> Functions;
170 /// GCOVFunction - Collects function information.
173 GCOVFunction() : Ident(0), LineNumber(0) {}
175 bool read(GCOVBuffer &Buffer, GCOV::GCOVFormat Format);
177 void collectLineCounts(FileInfo &FI);
183 SmallVector<GCOVBlock *, 16> Blocks;
186 /// GCOVBlock - Collects block information.
189 GCOVBlock(uint32_t N) : Number(N), Counter(0) {}
191 void addEdge(uint32_t N) { Edges.push_back(N); }
192 void addLine(StringRef Filename, uint32_t LineNo);
193 void addCount(uint64_t N) { Counter = N; }
195 void collectLineCounts(FileInfo &FI);
199 SmallVector<uint32_t, 16> Edges;
200 StringMap<GCOVLines *> Lines;
203 /// GCOVLines - A wrapper around a vector of int to keep track of line nos.
206 ~GCOVLines() { Lines.clear(); }
207 void add(uint32_t N) { Lines.push_back(N); }
208 void collectLineCounts(FileInfo &FI, StringRef Filename, uint32_t Count);
212 SmallVector<uint32_t, 4> Lines;
215 typedef SmallVector<uint32_t, 16> LineCounts;
218 void addLineCount(StringRef Filename, uint32_t Line, uint32_t Count);
221 StringMap<LineCounts> LineInfo;