CoverageMappingReader.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //=-- CoverageMappingReader.h - Code coverage mapping reader ------*- C++ -*-=//
  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 file contains support for reading coverage mapping data for
  11. // instrumentation based coverage.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_PROFILEDATA_COVERAGEMAPPINGREADER_H
  15. #define LLVM_PROFILEDATA_COVERAGEMAPPINGREADER_H
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/ADT/Triple.h"
  19. #include "llvm/Object/ObjectFile.h"
  20. #include "llvm/ProfileData/CoverageMapping.h"
  21. #include "llvm/ProfileData/InstrProf.h"
  22. #include "llvm/Support/FileSystem.h"
  23. #include "llvm/Support/MemoryBuffer.h"
  24. #include <iterator>
  25. namespace llvm {
  26. namespace coverage {
  27. class CoverageMappingReader;
  28. /// \brief Coverage mapping information for a single function.
  29. struct CoverageMappingRecord {
  30. StringRef FunctionName;
  31. uint64_t FunctionHash;
  32. ArrayRef<StringRef> Filenames;
  33. ArrayRef<CounterExpression> Expressions;
  34. ArrayRef<CounterMappingRegion> MappingRegions;
  35. };
  36. /// \brief A file format agnostic iterator over coverage mapping data.
  37. class CoverageMappingIterator
  38. : public std::iterator<std::input_iterator_tag, CoverageMappingRecord> {
  39. CoverageMappingReader *Reader;
  40. CoverageMappingRecord Record;
  41. void increment();
  42. public:
  43. CoverageMappingIterator() : Reader(nullptr) {}
  44. CoverageMappingIterator(CoverageMappingReader *Reader) : Reader(Reader) {
  45. increment();
  46. }
  47. CoverageMappingIterator &operator++() {
  48. increment();
  49. return *this;
  50. }
  51. bool operator==(const CoverageMappingIterator &RHS) {
  52. return Reader == RHS.Reader;
  53. }
  54. bool operator!=(const CoverageMappingIterator &RHS) {
  55. return Reader != RHS.Reader;
  56. }
  57. CoverageMappingRecord &operator*() { return Record; }
  58. CoverageMappingRecord *operator->() { return &Record; }
  59. };
  60. class CoverageMappingReader {
  61. public:
  62. virtual std::error_code readNextRecord(CoverageMappingRecord &Record) = 0;
  63. CoverageMappingIterator begin() { return CoverageMappingIterator(this); }
  64. CoverageMappingIterator end() { return CoverageMappingIterator(); }
  65. virtual ~CoverageMappingReader() {}
  66. };
  67. /// \brief Base class for the raw coverage mapping and filenames data readers.
  68. class RawCoverageReader {
  69. protected:
  70. StringRef Data;
  71. RawCoverageReader(StringRef Data) : Data(Data) {}
  72. std::error_code readULEB128(uint64_t &Result);
  73. std::error_code readIntMax(uint64_t &Result, uint64_t MaxPlus1);
  74. std::error_code readSize(uint64_t &Result);
  75. std::error_code readString(StringRef &Result);
  76. };
  77. /// \brief Reader for the raw coverage filenames.
  78. class RawCoverageFilenamesReader : public RawCoverageReader {
  79. std::vector<StringRef> &Filenames;
  80. RawCoverageFilenamesReader(const RawCoverageFilenamesReader &) = delete;
  81. RawCoverageFilenamesReader &
  82. operator=(const RawCoverageFilenamesReader &) = delete;
  83. public:
  84. RawCoverageFilenamesReader(StringRef Data, std::vector<StringRef> &Filenames)
  85. : RawCoverageReader(Data), Filenames(Filenames) {}
  86. std::error_code read();
  87. };
  88. /// \brief Reader for the raw coverage mapping data.
  89. class RawCoverageMappingReader : public RawCoverageReader {
  90. ArrayRef<StringRef> TranslationUnitFilenames;
  91. std::vector<StringRef> &Filenames;
  92. std::vector<CounterExpression> &Expressions;
  93. std::vector<CounterMappingRegion> &MappingRegions;
  94. RawCoverageMappingReader(const RawCoverageMappingReader &) = delete;
  95. RawCoverageMappingReader &
  96. operator=(const RawCoverageMappingReader &) = delete;
  97. public:
  98. RawCoverageMappingReader(StringRef MappingData,
  99. ArrayRef<StringRef> TranslationUnitFilenames,
  100. std::vector<StringRef> &Filenames,
  101. std::vector<CounterExpression> &Expressions,
  102. std::vector<CounterMappingRegion> &MappingRegions)
  103. : RawCoverageReader(MappingData),
  104. TranslationUnitFilenames(TranslationUnitFilenames),
  105. Filenames(Filenames), Expressions(Expressions),
  106. MappingRegions(MappingRegions) {}
  107. std::error_code read();
  108. private:
  109. std::error_code decodeCounter(unsigned Value, Counter &C);
  110. std::error_code readCounter(Counter &C);
  111. std::error_code
  112. readMappingRegionsSubArray(std::vector<CounterMappingRegion> &MappingRegions,
  113. unsigned InferredFileID, size_t NumFileIDs);
  114. };
  115. /// \brief Reader for the coverage mapping data that is emitted by the
  116. /// frontend and stored in an object file.
  117. class BinaryCoverageReader : public CoverageMappingReader {
  118. public:
  119. struct ProfileMappingRecord {
  120. CoverageMappingVersion Version;
  121. StringRef FunctionName;
  122. uint64_t FunctionHash;
  123. StringRef CoverageMapping;
  124. size_t FilenamesBegin;
  125. size_t FilenamesSize;
  126. ProfileMappingRecord(CoverageMappingVersion Version, StringRef FunctionName,
  127. uint64_t FunctionHash, StringRef CoverageMapping,
  128. size_t FilenamesBegin, size_t FilenamesSize)
  129. : Version(Version), FunctionName(FunctionName),
  130. FunctionHash(FunctionHash), CoverageMapping(CoverageMapping),
  131. FilenamesBegin(FilenamesBegin), FilenamesSize(FilenamesSize) {}
  132. };
  133. private:
  134. std::vector<StringRef> Filenames;
  135. std::vector<ProfileMappingRecord> MappingRecords;
  136. size_t CurrentRecord;
  137. std::vector<StringRef> FunctionsFilenames;
  138. std::vector<CounterExpression> Expressions;
  139. std::vector<CounterMappingRegion> MappingRegions;
  140. BinaryCoverageReader(const BinaryCoverageReader &) = delete;
  141. BinaryCoverageReader &operator=(const BinaryCoverageReader &) = delete;
  142. BinaryCoverageReader() : CurrentRecord(0) {}
  143. public:
  144. static ErrorOr<std::unique_ptr<BinaryCoverageReader>>
  145. create(std::unique_ptr<MemoryBuffer> &ObjectBuffer,
  146. StringRef Arch);
  147. std::error_code readNextRecord(CoverageMappingRecord &Record) override;
  148. };
  149. } // end namespace coverage
  150. } // end namespace llvm
  151. #endif