CoverageMappingReader.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- CoverageMappingReader.h - Code coverage mapping reader ---*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file contains support for reading coverage mapping data for
  15. // instrumentation based coverage.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPINGREADER_H
  19. #define LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPINGREADER_H
  20. #include "llvm/ADT/ArrayRef.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/ProfileData/Coverage/CoverageMapping.h"
  23. #include "llvm/ProfileData/InstrProf.h"
  24. #include "llvm/Support/Error.h"
  25. #include "llvm/Support/MemoryBuffer.h"
  26. #include <cstddef>
  27. #include <cstdint>
  28. #include <iterator>
  29. #include <memory>
  30. #include <vector>
  31. namespace llvm {
  32. namespace coverage {
  33. class CoverageMappingReader;
  34. /// Coverage mapping information for a single function.
  35. struct CoverageMappingRecord {
  36. StringRef FunctionName;
  37. uint64_t FunctionHash;
  38. ArrayRef<StringRef> Filenames;
  39. ArrayRef<CounterExpression> Expressions;
  40. ArrayRef<CounterMappingRegion> MappingRegions;
  41. };
  42. /// A file format agnostic iterator over coverage mapping data.
  43. class CoverageMappingIterator {
  44. CoverageMappingReader *Reader;
  45. CoverageMappingRecord Record;
  46. coveragemap_error ReadErr;
  47. void increment();
  48. public:
  49. using iterator_category = std::input_iterator_tag;
  50. using value_type = CoverageMappingRecord;
  51. using difference_type = std::ptrdiff_t;
  52. using pointer = value_type *;
  53. using reference = value_type &;
  54. CoverageMappingIterator()
  55. : Reader(nullptr), ReadErr(coveragemap_error::success) {}
  56. CoverageMappingIterator(CoverageMappingReader *Reader)
  57. : Reader(Reader), ReadErr(coveragemap_error::success) {
  58. increment();
  59. }
  60. ~CoverageMappingIterator() {
  61. if (ReadErr != coveragemap_error::success)
  62. llvm_unreachable("Unexpected error in coverage mapping iterator");
  63. }
  64. CoverageMappingIterator &operator++() {
  65. increment();
  66. return *this;
  67. }
  68. bool operator==(const CoverageMappingIterator &RHS) const {
  69. return Reader == RHS.Reader;
  70. }
  71. bool operator!=(const CoverageMappingIterator &RHS) const {
  72. return Reader != RHS.Reader;
  73. }
  74. Expected<CoverageMappingRecord &> operator*() {
  75. if (ReadErr != coveragemap_error::success) {
  76. auto E = make_error<CoverageMapError>(ReadErr);
  77. ReadErr = coveragemap_error::success;
  78. return std::move(E);
  79. }
  80. return Record;
  81. }
  82. Expected<CoverageMappingRecord *> operator->() {
  83. if (ReadErr != coveragemap_error::success) {
  84. auto E = make_error<CoverageMapError>(ReadErr);
  85. ReadErr = coveragemap_error::success;
  86. return std::move(E);
  87. }
  88. return &Record;
  89. }
  90. };
  91. class CoverageMappingReader {
  92. public:
  93. virtual ~CoverageMappingReader() = default;
  94. virtual Error readNextRecord(CoverageMappingRecord &Record) = 0;
  95. CoverageMappingIterator begin() { return CoverageMappingIterator(this); }
  96. CoverageMappingIterator end() { return CoverageMappingIterator(); }
  97. };
  98. /// Base class for the raw coverage mapping and filenames data readers.
  99. class RawCoverageReader {
  100. protected:
  101. StringRef Data;
  102. RawCoverageReader(StringRef Data) : Data(Data) {}
  103. Error readULEB128(uint64_t &Result);
  104. Error readIntMax(uint64_t &Result, uint64_t MaxPlus1);
  105. Error readSize(uint64_t &Result);
  106. Error readString(StringRef &Result);
  107. };
  108. /// Checks if the given coverage mapping data is exported for
  109. /// an unused function.
  110. class RawCoverageMappingDummyChecker : public RawCoverageReader {
  111. public:
  112. RawCoverageMappingDummyChecker(StringRef MappingData)
  113. : RawCoverageReader(MappingData) {}
  114. Expected<bool> isDummy();
  115. };
  116. /// Reader for the raw coverage mapping data.
  117. class RawCoverageMappingReader : public RawCoverageReader {
  118. ArrayRef<std::string> &TranslationUnitFilenames;
  119. std::vector<StringRef> &Filenames;
  120. std::vector<CounterExpression> &Expressions;
  121. std::vector<CounterMappingRegion> &MappingRegions;
  122. public:
  123. RawCoverageMappingReader(StringRef MappingData,
  124. ArrayRef<std::string> &TranslationUnitFilenames,
  125. std::vector<StringRef> &Filenames,
  126. std::vector<CounterExpression> &Expressions,
  127. std::vector<CounterMappingRegion> &MappingRegions)
  128. : RawCoverageReader(MappingData),
  129. TranslationUnitFilenames(TranslationUnitFilenames),
  130. Filenames(Filenames), Expressions(Expressions),
  131. MappingRegions(MappingRegions) {}
  132. RawCoverageMappingReader(const RawCoverageMappingReader &) = delete;
  133. RawCoverageMappingReader &
  134. operator=(const RawCoverageMappingReader &) = delete;
  135. Error read();
  136. private:
  137. Error decodeCounter(unsigned Value, Counter &C);
  138. Error readCounter(Counter &C);
  139. Error
  140. readMappingRegionsSubArray(std::vector<CounterMappingRegion> &MappingRegions,
  141. unsigned InferredFileID, size_t NumFileIDs);
  142. };
  143. /// Reader for the coverage mapping data that is emitted by the
  144. /// frontend and stored in an object file.
  145. class BinaryCoverageReader : public CoverageMappingReader {
  146. public:
  147. struct ProfileMappingRecord {
  148. CovMapVersion Version;
  149. StringRef FunctionName;
  150. uint64_t FunctionHash;
  151. StringRef CoverageMapping;
  152. size_t FilenamesBegin;
  153. size_t FilenamesSize;
  154. ProfileMappingRecord(CovMapVersion Version, StringRef FunctionName,
  155. uint64_t FunctionHash, StringRef CoverageMapping,
  156. size_t FilenamesBegin, size_t FilenamesSize)
  157. : Version(Version), FunctionName(FunctionName),
  158. FunctionHash(FunctionHash), CoverageMapping(CoverageMapping),
  159. FilenamesBegin(FilenamesBegin), FilenamesSize(FilenamesSize) {}
  160. };
  161. using FuncRecordsStorage = std::unique_ptr<MemoryBuffer>;
  162. private:
  163. std::vector<std::string> Filenames;
  164. std::vector<ProfileMappingRecord> MappingRecords;
  165. InstrProfSymtab ProfileNames;
  166. size_t CurrentRecord = 0;
  167. std::vector<StringRef> FunctionsFilenames;
  168. std::vector<CounterExpression> Expressions;
  169. std::vector<CounterMappingRegion> MappingRegions;
  170. // Used to tie the lifetimes of coverage function records to the lifetime of
  171. // this BinaryCoverageReader instance. Needed to support the format change in
  172. // D69471, which can split up function records into multiple sections on ELF.
  173. FuncRecordsStorage FuncRecords;
  174. BinaryCoverageReader(FuncRecordsStorage &&FuncRecords)
  175. : FuncRecords(std::move(FuncRecords)) {}
  176. public:
  177. BinaryCoverageReader(const BinaryCoverageReader &) = delete;
  178. BinaryCoverageReader &operator=(const BinaryCoverageReader &) = delete;
  179. static Expected<std::vector<std::unique_ptr<BinaryCoverageReader>>>
  180. create(MemoryBufferRef ObjectBuffer, StringRef Arch,
  181. SmallVectorImpl<std::unique_ptr<MemoryBuffer>> &ObjectFileBuffers,
  182. StringRef CompilationDir = "");
  183. static Expected<std::unique_ptr<BinaryCoverageReader>>
  184. createCoverageReaderFromBuffer(StringRef Coverage,
  185. FuncRecordsStorage &&FuncRecords,
  186. InstrProfSymtab &&ProfileNames,
  187. uint8_t BytesInAddress,
  188. support::endianness Endian,
  189. StringRef CompilationDir = "");
  190. Error readNextRecord(CoverageMappingRecord &Record) override;
  191. };
  192. /// Reader for the raw coverage filenames.
  193. class RawCoverageFilenamesReader : public RawCoverageReader {
  194. std::vector<std::string> &Filenames;
  195. StringRef CompilationDir;
  196. // Read an uncompressed sequence of filenames.
  197. Error readUncompressed(CovMapVersion Version, uint64_t NumFilenames);
  198. public:
  199. RawCoverageFilenamesReader(StringRef Data,
  200. std::vector<std::string> &Filenames,
  201. StringRef CompilationDir = "")
  202. : RawCoverageReader(Data), Filenames(Filenames),
  203. CompilationDir(CompilationDir) {}
  204. RawCoverageFilenamesReader(const RawCoverageFilenamesReader &) = delete;
  205. RawCoverageFilenamesReader &
  206. operator=(const RawCoverageFilenamesReader &) = delete;
  207. Error read(CovMapVersion Version);
  208. };
  209. } // end namespace coverage
  210. } // end namespace llvm
  211. #endif // LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPINGREADER_H
  212. #ifdef __GNUC__
  213. #pragma GCC diagnostic pop
  214. #endif