1 //===-- tools/llvm-cov/GCOVReader.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 coverage files that use 'gcov'
13 //===----------------------------------------------------------------------===//
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 /// GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific
42 GCOVBuffer(MemoryBuffer *B) : Buffer(B), Cursor(0) {}
44 /// readGCOVFormat - Read GCOV signature at the beginning of buffer.
45 enum GCOVFormat readGCOVFormat() {
46 StringRef Magic = Buffer->getBuffer().slice(0, 12);
48 if (Magic == "oncg*404MVLL")
50 else if (Magic == "oncg*204MVLL")
52 else if (Magic == "adcg*404MVLL")
54 else if (Magic == "adcg*204MVLL")
61 /// readFunctionTag - If cursor points to a function tag then increment the
62 /// cursor and return true otherwise return false.
63 bool readFunctionTag() {
64 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
66 Tag[0] != '\0' || Tag[1] != '\0' ||
67 Tag[2] != '\0' || Tag[3] != '\1') {
74 /// readBlockTag - If cursor points to a block tag then increment the
75 /// cursor and return true otherwise return false.
77 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
79 Tag[0] != '\0' || Tag[1] != '\0' ||
80 Tag[2] != '\x41' || Tag[3] != '\x01') {
87 /// readEdgeTag - If cursor points to an edge tag then increment the
88 /// cursor and return true otherwise return false.
90 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
92 Tag[0] != '\0' || Tag[1] != '\0' ||
93 Tag[2] != '\x43' || Tag[3] != '\x01') {
100 /// readLineTag - If cursor points to a line tag then increment the
101 /// cursor and return true otherwise return false.
103 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
105 Tag[0] != '\0' || Tag[1] != '\0' ||
106 Tag[2] != '\x45' || Tag[3] != '\x01') {
113 /// readArcTag - If cursor points to an gcda arc tag then increment the
114 /// cursor and return true otherwise return false.
116 StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
118 Tag[0] != '\0' || Tag[1] != '\0' ||
119 Tag[2] != '\xa1' || Tag[3] != '\1') {
128 StringRef Str = Buffer->getBuffer().slice(Cursor, Cursor+4);
129 assert (Str.empty() == false && "Unexpected memory buffer end!");
131 Result = *(uint32_t *)(Str.data());
135 uint64_t readInt64() {
136 uint64_t Lo = readInt();
137 uint64_t Hi = readInt();
138 uint64_t Result = Lo | (Hi << 32);
142 StringRef readString() {
143 uint32_t Len = readInt() * 4;
144 StringRef Str = Buffer->getBuffer().slice(Cursor, Cursor+Len);
149 uint64_t getCursor() const { return Cursor; }
151 MemoryBuffer *Buffer;
155 /// GCOVFile - Collects coverage information for one pair of coverage file
156 /// (.gcno and .gcda).
161 bool read(GCOVBuffer &Buffer);
163 void collectLineCounts(FileInfo &FI);
165 SmallVector<GCOVFunction *, 16> Functions;
168 /// GCOVFunction - Collects function information.
171 GCOVFunction() : Ident(0), LineNumber(0) {}
173 bool read(GCOVBuffer &Buffer, GCOVFormat Format);
175 void collectLineCounts(FileInfo &FI);
181 SmallVector<GCOVBlock *, 16> Blocks;
184 /// GCOVBlock - Collects block information.
187 GCOVBlock(uint32_t N) : Number(N), Counter(0) {}
189 void addEdge(uint32_t N) { Edges.push_back(N); }
190 void addLine(StringRef Filename, uint32_t LineNo);
191 void addCount(uint64_t N) { Counter = N; }
193 void collectLineCounts(FileInfo &FI);
197 SmallVector<uint32_t, 16> Edges;
198 StringMap<GCOVLines *> Lines;
201 /// GCOVLines - A wrapper around a vector of int to keep track of line nos.
204 ~GCOVLines() { Lines.clear(); }
205 void add(uint32_t N) { Lines.push_back(N); }
206 void collectLineCounts(FileInfo &FI, StringRef Filename, uint32_t Count);
210 SmallVector<uint32_t, 4> Lines;
213 typedef SmallVector<uint32_t, 16> LineCounts;
216 void addLineCount(StringRef Filename, uint32_t Line, uint32_t Count);
219 StringMap<LineCounts> LineInfo;