GCOV.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. //===- GCOV.h - LLVM coverage tool ----------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This header provides the interface to read and write coverage files that
  11. // use 'gcov' format.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_SUPPORT_GCOV_H
  15. #define LLVM_SUPPORT_GCOV_H
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/MapVector.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/StringMap.h"
  20. #include "llvm/ADT/iterator.h"
  21. #include "llvm/Support/MemoryBuffer.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. namespace llvm {
  24. class GCOVFunction;
  25. class GCOVBlock;
  26. class FileInfo;
  27. namespace GCOV {
  28. enum GCOVVersion { V402, V404 };
  29. } // end GCOV namespace
  30. /// GCOVOptions - A struct for passing gcov options between functions.
  31. struct GCOVOptions {
  32. GCOVOptions(bool A, bool B, bool C, bool F, bool P, bool U, bool L, bool N)
  33. : AllBlocks(A), BranchInfo(B), BranchCount(C), FuncCoverage(F),
  34. PreservePaths(P), UncondBranch(U), LongFileNames(L), NoOutput(N) {}
  35. bool AllBlocks;
  36. bool BranchInfo;
  37. bool BranchCount;
  38. bool FuncCoverage;
  39. bool PreservePaths;
  40. bool UncondBranch;
  41. bool LongFileNames;
  42. bool NoOutput;
  43. };
  44. /// GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific
  45. /// read operations.
  46. class GCOVBuffer {
  47. public:
  48. GCOVBuffer(MemoryBuffer *B) : Buffer(B), Cursor(0) {}
  49. /// readGCNOFormat - Check GCNO signature is valid at the beginning of buffer.
  50. bool readGCNOFormat() {
  51. StringRef File = Buffer->getBuffer().slice(0, 4);
  52. if (File != "oncg") {
  53. errs() << "Unexpected file type: " << File << ".\n";
  54. return false;
  55. }
  56. Cursor = 4;
  57. return true;
  58. }
  59. /// readGCDAFormat - Check GCDA signature is valid at the beginning of buffer.
  60. bool readGCDAFormat() {
  61. StringRef File = Buffer->getBuffer().slice(0, 4);
  62. if (File != "adcg") {
  63. errs() << "Unexpected file type: " << File << ".\n";
  64. return false;
  65. }
  66. Cursor = 4;
  67. return true;
  68. }
  69. /// readGCOVVersion - Read GCOV version.
  70. bool readGCOVVersion(GCOV::GCOVVersion &Version) {
  71. StringRef VersionStr = Buffer->getBuffer().slice(Cursor, Cursor + 4);
  72. if (VersionStr == "*204") {
  73. Cursor += 4;
  74. Version = GCOV::V402;
  75. return true;
  76. }
  77. if (VersionStr == "*404") {
  78. Cursor += 4;
  79. Version = GCOV::V404;
  80. return true;
  81. }
  82. errs() << "Unexpected version: " << VersionStr << ".\n";
  83. return false;
  84. }
  85. /// readFunctionTag - If cursor points to a function tag then increment the
  86. /// cursor and return true otherwise return false.
  87. bool readFunctionTag() {
  88. StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor + 4);
  89. if (Tag.empty() || Tag[0] != '\0' || Tag[1] != '\0' || Tag[2] != '\0' ||
  90. Tag[3] != '\1') {
  91. return false;
  92. }
  93. Cursor += 4;
  94. return true;
  95. }
  96. /// readBlockTag - If cursor points to a block tag then increment the
  97. /// cursor and return true otherwise return false.
  98. bool readBlockTag() {
  99. StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor + 4);
  100. if (Tag.empty() || Tag[0] != '\0' || Tag[1] != '\0' || Tag[2] != '\x41' ||
  101. Tag[3] != '\x01') {
  102. return false;
  103. }
  104. Cursor += 4;
  105. return true;
  106. }
  107. /// readEdgeTag - If cursor points to an edge tag then increment the
  108. /// cursor and return true otherwise return false.
  109. bool readEdgeTag() {
  110. StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor + 4);
  111. if (Tag.empty() || Tag[0] != '\0' || Tag[1] != '\0' || Tag[2] != '\x43' ||
  112. Tag[3] != '\x01') {
  113. return false;
  114. }
  115. Cursor += 4;
  116. return true;
  117. }
  118. /// readLineTag - If cursor points to a line tag then increment the
  119. /// cursor and return true otherwise return false.
  120. bool readLineTag() {
  121. StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor + 4);
  122. if (Tag.empty() || Tag[0] != '\0' || Tag[1] != '\0' || Tag[2] != '\x45' ||
  123. Tag[3] != '\x01') {
  124. return false;
  125. }
  126. Cursor += 4;
  127. return true;
  128. }
  129. /// readArcTag - If cursor points to an gcda arc tag then increment the
  130. /// cursor and return true otherwise return false.
  131. bool readArcTag() {
  132. StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor + 4);
  133. if (Tag.empty() || Tag[0] != '\0' || Tag[1] != '\0' || Tag[2] != '\xa1' ||
  134. Tag[3] != '\1') {
  135. return false;
  136. }
  137. Cursor += 4;
  138. return true;
  139. }
  140. /// readObjectTag - If cursor points to an object summary tag then increment
  141. /// the cursor and return true otherwise return false.
  142. bool readObjectTag() {
  143. StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor + 4);
  144. if (Tag.empty() || Tag[0] != '\0' || Tag[1] != '\0' || Tag[2] != '\0' ||
  145. Tag[3] != '\xa1') {
  146. return false;
  147. }
  148. Cursor += 4;
  149. return true;
  150. }
  151. /// readProgramTag - If cursor points to a program summary tag then increment
  152. /// the cursor and return true otherwise return false.
  153. bool readProgramTag() {
  154. StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor + 4);
  155. if (Tag.empty() || Tag[0] != '\0' || Tag[1] != '\0' || Tag[2] != '\0' ||
  156. Tag[3] != '\xa3') {
  157. return false;
  158. }
  159. Cursor += 4;
  160. return true;
  161. }
  162. bool readInt(uint32_t &Val) {
  163. if (Buffer->getBuffer().size() < Cursor + 4) {
  164. errs() << "Unexpected end of memory buffer: " << Cursor + 4 << ".\n";
  165. return false;
  166. }
  167. StringRef Str = Buffer->getBuffer().slice(Cursor, Cursor + 4);
  168. Cursor += 4;
  169. Val = *(const uint32_t *)(Str.data());
  170. return true;
  171. }
  172. bool readInt64(uint64_t &Val) {
  173. uint32_t Lo, Hi;
  174. if (!readInt(Lo) || !readInt(Hi))
  175. return false;
  176. Val = ((uint64_t)Hi << 32) | Lo;
  177. return true;
  178. }
  179. bool readString(StringRef &Str) {
  180. uint32_t Len = 0;
  181. // Keep reading until we find a non-zero length. This emulates gcov's
  182. // behaviour, which appears to do the same.
  183. while (Len == 0)
  184. if (!readInt(Len))
  185. return false;
  186. Len *= 4;
  187. if (Buffer->getBuffer().size() < Cursor + Len) {
  188. errs() << "Unexpected end of memory buffer: " << Cursor + Len << ".\n";
  189. return false;
  190. }
  191. Str = Buffer->getBuffer().slice(Cursor, Cursor + Len).split('\0').first;
  192. Cursor += Len;
  193. return true;
  194. }
  195. uint64_t getCursor() const { return Cursor; }
  196. void advanceCursor(uint32_t n) { Cursor += n * 4; }
  197. private:
  198. MemoryBuffer *Buffer;
  199. uint64_t Cursor;
  200. };
  201. /// GCOVFile - Collects coverage information for one pair of coverage file
  202. /// (.gcno and .gcda).
  203. class GCOVFile {
  204. public:
  205. GCOVFile()
  206. : GCNOInitialized(false), Checksum(0), Functions(), RunCount(0),
  207. ProgramCount(0) {}
  208. bool readGCNO(GCOVBuffer &Buffer);
  209. bool readGCDA(GCOVBuffer &Buffer);
  210. uint32_t getChecksum() const { return Checksum; }
  211. void dump() const;
  212. void collectLineCounts(FileInfo &FI);
  213. private:
  214. bool GCNOInitialized;
  215. GCOV::GCOVVersion Version;
  216. uint32_t Checksum;
  217. SmallVector<std::unique_ptr<GCOVFunction>, 16> Functions;
  218. uint32_t RunCount;
  219. uint32_t ProgramCount;
  220. };
  221. /// GCOVEdge - Collects edge information.
  222. struct GCOVEdge {
  223. GCOVEdge(GCOVBlock &S, GCOVBlock &D) : Src(S), Dst(D), Count(0) {}
  224. GCOVBlock &Src;
  225. GCOVBlock &Dst;
  226. uint64_t Count;
  227. };
  228. /// GCOVFunction - Collects function information.
  229. class GCOVFunction {
  230. public:
  231. typedef pointee_iterator<SmallVectorImpl<
  232. std::unique_ptr<GCOVBlock>>::const_iterator> BlockIterator;
  233. GCOVFunction(GCOVFile &P) : Parent(P), Ident(0), LineNumber(0) {}
  234. bool readGCNO(GCOVBuffer &Buffer, GCOV::GCOVVersion Version);
  235. bool readGCDA(GCOVBuffer &Buffer, GCOV::GCOVVersion Version);
  236. StringRef getName() const { return Name; }
  237. StringRef getFilename() const { return Filename; }
  238. size_t getNumBlocks() const { return Blocks.size(); }
  239. uint64_t getEntryCount() const;
  240. uint64_t getExitCount() const;
  241. BlockIterator block_begin() const { return Blocks.begin(); }
  242. BlockIterator block_end() const { return Blocks.end(); }
  243. iterator_range<BlockIterator> blocks() const {
  244. return make_range(block_begin(), block_end());
  245. }
  246. void dump() const;
  247. void collectLineCounts(FileInfo &FI);
  248. private:
  249. GCOVFile &Parent;
  250. uint32_t Ident;
  251. uint32_t Checksum;
  252. uint32_t LineNumber;
  253. StringRef Name;
  254. StringRef Filename;
  255. SmallVector<std::unique_ptr<GCOVBlock>, 16> Blocks;
  256. SmallVector<std::unique_ptr<GCOVEdge>, 16> Edges;
  257. };
  258. /// GCOVBlock - Collects block information.
  259. class GCOVBlock {
  260. struct EdgeWeight {
  261. EdgeWeight(GCOVBlock *D) : Dst(D), Count(0) {}
  262. GCOVBlock *Dst;
  263. uint64_t Count;
  264. };
  265. struct SortDstEdgesFunctor {
  266. bool operator()(const GCOVEdge *E1, const GCOVEdge *E2) {
  267. return E1->Dst.Number < E2->Dst.Number;
  268. }
  269. };
  270. public:
  271. typedef SmallVectorImpl<GCOVEdge *>::const_iterator EdgeIterator;
  272. GCOVBlock(GCOVFunction &P, uint32_t N)
  273. : Parent(P), Number(N), Counter(0), DstEdgesAreSorted(true), SrcEdges(),
  274. DstEdges(), Lines() {}
  275. ~GCOVBlock();
  276. const GCOVFunction &getParent() const { return Parent; }
  277. void addLine(uint32_t N) { Lines.push_back(N); }
  278. uint32_t getLastLine() const { return Lines.back(); }
  279. void addCount(size_t DstEdgeNo, uint64_t N);
  280. uint64_t getCount() const { return Counter; }
  281. void addSrcEdge(GCOVEdge *Edge) {
  282. assert(&Edge->Dst == this); // up to caller to ensure edge is valid
  283. SrcEdges.push_back(Edge);
  284. }
  285. void addDstEdge(GCOVEdge *Edge) {
  286. assert(&Edge->Src == this); // up to caller to ensure edge is valid
  287. // Check if adding this edge causes list to become unsorted.
  288. if (DstEdges.size() && DstEdges.back()->Dst.Number > Edge->Dst.Number)
  289. DstEdgesAreSorted = false;
  290. DstEdges.push_back(Edge);
  291. }
  292. size_t getNumSrcEdges() const { return SrcEdges.size(); }
  293. size_t getNumDstEdges() const { return DstEdges.size(); }
  294. void sortDstEdges();
  295. EdgeIterator src_begin() const { return SrcEdges.begin(); }
  296. EdgeIterator src_end() const { return SrcEdges.end(); }
  297. iterator_range<EdgeIterator> srcs() const {
  298. return make_range(src_begin(), src_end());
  299. }
  300. EdgeIterator dst_begin() const { return DstEdges.begin(); }
  301. EdgeIterator dst_end() const { return DstEdges.end(); }
  302. iterator_range<EdgeIterator> dsts() const {
  303. return make_range(dst_begin(), dst_end());
  304. }
  305. void dump() const;
  306. void collectLineCounts(FileInfo &FI);
  307. private:
  308. GCOVFunction &Parent;
  309. uint32_t Number;
  310. uint64_t Counter;
  311. bool DstEdgesAreSorted;
  312. SmallVector<GCOVEdge *, 16> SrcEdges;
  313. SmallVector<GCOVEdge *, 16> DstEdges;
  314. SmallVector<uint32_t, 16> Lines;
  315. };
  316. class FileInfo {
  317. // It is unlikely--but possible--for multiple functions to be on the same
  318. // line.
  319. // Therefore this typedef allows LineData.Functions to store multiple
  320. // functions
  321. // per instance. This is rare, however, so optimize for the common case.
  322. typedef SmallVector<const GCOVFunction *, 1> FunctionVector;
  323. typedef DenseMap<uint32_t, FunctionVector> FunctionLines;
  324. typedef SmallVector<const GCOVBlock *, 4> BlockVector;
  325. typedef DenseMap<uint32_t, BlockVector> BlockLines;
  326. struct LineData {
  327. LineData() : LastLine(0) {}
  328. BlockLines Blocks;
  329. FunctionLines Functions;
  330. uint32_t LastLine;
  331. };
  332. struct GCOVCoverage {
  333. GCOVCoverage(StringRef Name)
  334. : Name(Name), LogicalLines(0), LinesExec(0), Branches(0),
  335. BranchesExec(0), BranchesTaken(0) {}
  336. StringRef Name;
  337. uint32_t LogicalLines;
  338. uint32_t LinesExec;
  339. uint32_t Branches;
  340. uint32_t BranchesExec;
  341. uint32_t BranchesTaken;
  342. };
  343. public:
  344. FileInfo(const GCOVOptions &Options)
  345. : Options(Options), LineInfo(), RunCount(0), ProgramCount(0) {}
  346. void addBlockLine(StringRef Filename, uint32_t Line, const GCOVBlock *Block) {
  347. if (Line > LineInfo[Filename].LastLine)
  348. LineInfo[Filename].LastLine = Line;
  349. LineInfo[Filename].Blocks[Line - 1].push_back(Block);
  350. }
  351. void addFunctionLine(StringRef Filename, uint32_t Line,
  352. const GCOVFunction *Function) {
  353. if (Line > LineInfo[Filename].LastLine)
  354. LineInfo[Filename].LastLine = Line;
  355. LineInfo[Filename].Functions[Line - 1].push_back(Function);
  356. }
  357. void setRunCount(uint32_t Runs) { RunCount = Runs; }
  358. void setProgramCount(uint32_t Programs) { ProgramCount = Programs; }
  359. void print(raw_ostream &OS, StringRef MainFilename, StringRef GCNOFile,
  360. StringRef GCDAFile);
  361. private:
  362. std::string getCoveragePath(StringRef Filename, StringRef MainFilename);
  363. std::unique_ptr<raw_ostream> openCoveragePath(StringRef CoveragePath);
  364. void printFunctionSummary(raw_ostream &OS, const FunctionVector &Funcs) const;
  365. void printBlockInfo(raw_ostream &OS, const GCOVBlock &Block,
  366. uint32_t LineIndex, uint32_t &BlockNo) const;
  367. void printBranchInfo(raw_ostream &OS, const GCOVBlock &Block,
  368. GCOVCoverage &Coverage, uint32_t &EdgeNo);
  369. void printUncondBranchInfo(raw_ostream &OS, uint32_t &EdgeNo,
  370. uint64_t Count) const;
  371. void printCoverage(raw_ostream &OS, const GCOVCoverage &Coverage) const;
  372. void printFuncCoverage(raw_ostream &OS) const;
  373. void printFileCoverage(raw_ostream &OS) const;
  374. const GCOVOptions &Options;
  375. StringMap<LineData> LineInfo;
  376. uint32_t RunCount;
  377. uint32_t ProgramCount;
  378. typedef SmallVector<std::pair<std::string, GCOVCoverage>, 4> FileCoverageList;
  379. typedef MapVector<const GCOVFunction *, GCOVCoverage> FuncCoverageMap;
  380. FileCoverageList FileCoverages;
  381. FuncCoverageMap FuncCoverages;
  382. };
  383. }
  384. #endif