LLVM  14.0.0git
CoverageMappingReader.h
Go to the documentation of this file.
1 //===- CoverageMappingReader.h - Code coverage mapping reader ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for reading coverage mapping data for
10 // instrumentation based coverage.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPINGREADER_H
15 #define LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPINGREADER_H
16 
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/Error.h"
23 #include <cstddef>
24 #include <cstdint>
25 #include <iterator>
26 #include <memory>
27 #include <vector>
28 
29 namespace llvm {
30 namespace coverage {
31 
32 class CoverageMappingReader;
33 
34 /// Coverage mapping information for a single function.
41 };
42 
43 /// A file format agnostic iterator over coverage mapping data.
45  CoverageMappingReader *Reader;
47  coveragemap_error ReadErr;
48 
49  void increment();
50 
51 public:
52  using iterator_category = std::input_iterator_tag;
54  using difference_type = std::ptrdiff_t;
55  using pointer = value_type *;
56  using reference = value_type &;
57 
59  : Reader(nullptr), Record(), ReadErr(coveragemap_error::success) {}
60 
62  : Reader(Reader), Record(), ReadErr(coveragemap_error::success) {
63  increment();
64  }
65 
67  if (ReadErr != coveragemap_error::success)
68  llvm_unreachable("Unexpected error in coverage mapping iterator");
69  }
70 
72  increment();
73  return *this;
74  }
75  bool operator==(const CoverageMappingIterator &RHS) const {
76  return Reader == RHS.Reader;
77  }
78  bool operator!=(const CoverageMappingIterator &RHS) const {
79  return Reader != RHS.Reader;
80  }
82  if (ReadErr != coveragemap_error::success) {
83  auto E = make_error<CoverageMapError>(ReadErr);
85  return std::move(E);
86  }
87  return Record;
88  }
90  if (ReadErr != coveragemap_error::success) {
91  auto E = make_error<CoverageMapError>(ReadErr);
93  return std::move(E);
94  }
95  return &Record;
96  }
97 };
98 
100 public:
101  virtual ~CoverageMappingReader() = default;
102 
106 };
107 
108 /// Base class for the raw coverage mapping and filenames data readers.
110 protected:
112 
114 
115  Error readULEB128(uint64_t &Result);
116  Error readIntMax(uint64_t &Result, uint64_t MaxPlus1);
117  Error readSize(uint64_t &Result);
118  Error readString(StringRef &Result);
119 };
120 
121 /// Checks if the given coverage mapping data is exported for
122 /// an unused function.
124 public:
126  : RawCoverageReader(MappingData) {}
127 
129 };
130 
131 /// Reader for the raw coverage mapping data.
133  ArrayRef<std::string> &TranslationUnitFilenames;
134  std::vector<StringRef> &Filenames;
135  std::vector<CounterExpression> &Expressions;
136  std::vector<CounterMappingRegion> &MappingRegions;
137 
138 public:
140  ArrayRef<std::string> &TranslationUnitFilenames,
141  std::vector<StringRef> &Filenames,
142  std::vector<CounterExpression> &Expressions,
143  std::vector<CounterMappingRegion> &MappingRegions)
144  : RawCoverageReader(MappingData),
145  TranslationUnitFilenames(TranslationUnitFilenames),
146  Filenames(Filenames), Expressions(Expressions),
147  MappingRegions(MappingRegions) {}
150  operator=(const RawCoverageMappingReader &) = delete;
151 
152  Error read();
153 
154 private:
155  Error decodeCounter(unsigned Value, Counter &C);
156  Error readCounter(Counter &C);
157  Error
158  readMappingRegionsSubArray(std::vector<CounterMappingRegion> &MappingRegions,
159  unsigned InferredFileID, size_t NumFileIDs);
160 };
161 
162 /// Reader for the coverage mapping data that is emitted by the
163 /// frontend and stored in an object file.
165 public:
173 
176  size_t FilenamesBegin, size_t FilenamesSize)
180  };
181 
182  using FuncRecordsStorage = std::unique_ptr<MemoryBuffer>;
183 
184 private:
185  std::vector<std::string> Filenames;
186  std::vector<ProfileMappingRecord> MappingRecords;
187  InstrProfSymtab ProfileNames;
188  size_t CurrentRecord = 0;
189  std::vector<StringRef> FunctionsFilenames;
190  std::vector<CounterExpression> Expressions;
191  std::vector<CounterMappingRegion> MappingRegions;
192 
193  // Used to tie the lifetimes of coverage function records to the lifetime of
194  // this BinaryCoverageReader instance. Needed to support the format change in
195  // D69471, which can split up function records into multiple sections on ELF.
196  FuncRecordsStorage FuncRecords;
197 
199  : FuncRecords(std::move(FuncRecords)) {}
200 
201 public:
202  BinaryCoverageReader(const BinaryCoverageReader &) = delete;
204 
206  create(MemoryBufferRef ObjectBuffer, StringRef Arch,
207  SmallVectorImpl<std::unique_ptr<MemoryBuffer>> &ObjectFileBuffers,
208  StringRef CompilationDir = "");
209 
212  FuncRecordsStorage &&FuncRecords,
213  InstrProfSymtab &&ProfileNames,
214  uint8_t BytesInAddress,
215  support::endianness Endian,
216  StringRef CompilationDir = "");
217 
219 };
220 
221 /// Reader for the raw coverage filenames.
223  std::vector<std::string> &Filenames;
224  StringRef CompilationDir;
225 
226  // Read an uncompressed sequence of filenames.
227  Error readUncompressed(CovMapVersion Version, uint64_t NumFilenames);
228 
229 public:
231  std::vector<std::string> &Filenames,
232  StringRef CompilationDir = "")
233  : RawCoverageReader(Data), Filenames(Filenames),
234  CompilationDir(CompilationDir) {}
237  operator=(const RawCoverageFilenamesReader &) = delete;
238 
240 };
241 
242 } // end namespace coverage
243 } // end namespace llvm
244 
245 #endif // LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPINGREADER_H
llvm::coverage::coveragemap_error::success
@ success
MemoryBuffer.h
llvm::coverage::BinaryCoverageReader::ProfileMappingRecord::FilenamesBegin
size_t FilenamesBegin
Definition: CoverageMappingReader.h:171
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::coverage::CoverageMappingReader
Definition: CoverageMappingReader.h:99
llvm::coverage::CovMapVersion
CovMapVersion
Definition: CoverageMapping.h:993
llvm::coverage::CoverageMappingIterator::operator==
bool operator==(const CoverageMappingIterator &RHS) const
Definition: CoverageMappingReader.h:75
StringRef.h
llvm::coverage::BinaryCoverageReader::readNextRecord
Error readNextRecord(CoverageMappingRecord &Record) override
Definition: CoverageMappingReader.cpp:1154
Error.h
llvm::coverage::CoverageMappingIterator::difference_type
std::ptrdiff_t difference_type
Definition: CoverageMappingReader.h:54
llvm::coverage::RawCoverageMappingReader::RawCoverageMappingReader
RawCoverageMappingReader(StringRef MappingData, ArrayRef< std::string > &TranslationUnitFilenames, std::vector< StringRef > &Filenames, std::vector< CounterExpression > &Expressions, std::vector< CounterMappingRegion > &MappingRegions)
Definition: CoverageMappingReader.h:139
llvm::coverage::coveragemap_error
coveragemap_error
Definition: CoverageMapping.h:51
llvm::coverage::BinaryCoverageReader
Reader for the coverage mapping data that is emitted by the frontend and stored in an object file.
Definition: CoverageMappingReader.h:164
llvm::coverage::BinaryCoverageReader::ProfileMappingRecord::FunctionName
StringRef FunctionName
Definition: CoverageMappingReader.h:168
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::coverage::BinaryCoverageReader::operator=
BinaryCoverageReader & operator=(const BinaryCoverageReader &)=delete
llvm::coverage::BinaryCoverageReader::ProfileMappingRecord::CoverageMapping
StringRef CoverageMapping
Definition: CoverageMappingReader.h:170
llvm::coverage::CoverageMappingIterator::CoverageMappingIterator
CoverageMappingIterator()
Definition: CoverageMappingReader.h:58
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::coverage::BinaryCoverageReader::createCoverageReaderFromBuffer
static Expected< std::unique_ptr< BinaryCoverageReader > > createCoverageReaderFromBuffer(StringRef Coverage, FuncRecordsStorage &&FuncRecords, InstrProfSymtab &&ProfileNames, uint8_t BytesInAddress, support::endianness Endian, StringRef CompilationDir="")
Definition: CoverageMappingReader.cpp:826
llvm::coverage::CoverageMappingIterator::operator!=
bool operator!=(const CoverageMappingIterator &RHS) const
Definition: CoverageMappingReader.h:78
llvm::coverage::CoverageMappingRecord::FunctionName
StringRef FunctionName
Definition: CoverageMappingReader.h:36
llvm::coverage::BinaryCoverageReader::ProfileMappingRecord
Definition: CoverageMappingReader.h:166
llvm::coverage::RawCoverageMappingDummyChecker::isDummy
Expected< bool > isDummy()
Definition: CoverageMappingReader.cpp:417
llvm::coverage::CoverageMappingRecord::Expressions
ArrayRef< CounterExpression > Expressions
Definition: CoverageMappingReader.h:39
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::coverage::RawCoverageMappingDummyChecker::RawCoverageMappingDummyChecker
RawCoverageMappingDummyChecker(StringRef MappingData)
Definition: CoverageMappingReader.h:125
llvm::coverage::CoverageMappingIterator
A file format agnostic iterator over coverage mapping data.
Definition: CoverageMappingReader.h:44
llvm::coverage::CoverageMappingReader::begin
CoverageMappingIterator begin()
Definition: CoverageMappingReader.h:104
InstrProf.h
llvm::IndexedInstrProf::Version
const uint64_t Version
Definition: InstrProf.h:991
llvm::coverage::RawCoverageReader::readSize
Error readSize(uint64_t &Result)
Definition: CoverageMappingReader.cpp:83
llvm::coverage::BinaryCoverageReader::ProfileMappingRecord::Version
CovMapVersion Version
Definition: CoverageMappingReader.h:167
llvm::coverage::BinaryCoverageReader::ProfileMappingRecord::FunctionHash
uint64_t FunctionHash
Definition: CoverageMappingReader.h:169
llvm::coverage::BinaryCoverageReader::create
static Expected< std::vector< std::unique_ptr< BinaryCoverageReader > > > create(MemoryBufferRef ObjectBuffer, StringRef Arch, SmallVectorImpl< std::unique_ptr< MemoryBuffer >> &ObjectFileBuffers, StringRef CompilationDir="")
Definition: CoverageMappingReader.cpp:1072
llvm::coverage::RawCoverageMappingReader
Reader for the raw coverage mapping data.
Definition: CoverageMappingReader.h:132
llvm::coverage::BinaryCoverageReader::ProfileMappingRecord::FilenamesSize
size_t FilenamesSize
Definition: CoverageMappingReader.h:172
uint64_t
llvm::coverage::RawCoverageFilenamesReader::RawCoverageFilenamesReader
RawCoverageFilenamesReader(StringRef Data, std::vector< std::string > &Filenames, StringRef CompilationDir="")
Definition: CoverageMappingReader.h:230
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::coverage::Counter
A Counter is an abstract value that describes how to compute the execution count for a region of code...
Definition: CoverageMapping.h:92
llvm::coverage::RawCoverageReader::Data
StringRef Data
Definition: CoverageMappingReader.h:111
ArrayRef.h
llvm::coverage::RawCoverageReader
Base class for the raw coverage mapping and filenames data readers.
Definition: CoverageMappingReader.h:109
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1605
llvm::coverage::CoverageMappingReader::~CoverageMappingReader
virtual ~CoverageMappingReader()=default
llvm::coverage::RawCoverageMappingReader::operator=
RawCoverageMappingReader & operator=(const RawCoverageMappingReader &)=delete
llvm::Record
Definition: Record.h:1472
llvm::coverage::RawCoverageMappingDummyChecker
Checks if the given coverage mapping data is exported for an unused function.
Definition: CoverageMappingReader.h:123
llvm::coverage::CoverageMappingIterator::operator++
CoverageMappingIterator & operator++()
Definition: CoverageMappingReader.h:71
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::coverage::RawCoverageMappingReader::read
Error read()
Definition: CoverageMappingReader.cpp:349
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::coverage::CoverageMapping
The mapping of profile information to coverage data.
Definition: CoverageMapping.h:570
llvm::coverage::RawCoverageReader::readULEB128
Error readULEB128(uint64_t &Result)
Definition: CoverageMappingReader.cpp:64
CoverageMapping.h
llvm::coverage::RawCoverageFilenamesReader::operator=
RawCoverageFilenamesReader & operator=(const RawCoverageFilenamesReader &)=delete
llvm::coverage::CoverageMappingIterator::operator*
Expected< CoverageMappingRecord & > operator*()
Definition: CoverageMappingReader.h:81
llvm::coverage::RawCoverageFilenamesReader
Reader for the raw coverage filenames.
Definition: CoverageMappingReader.h:222
llvm::coverage::RawCoverageReader::readIntMax
Error readIntMax(uint64_t &Result, uint64_t MaxPlus1)
Definition: CoverageMappingReader.cpp:75
llvm::coverage::CoverageMappingIterator::~CoverageMappingIterator
~CoverageMappingIterator()
Definition: CoverageMappingReader.h:66
std
Definition: BitVector.h:838
llvm::InstrProfSymtab
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Definition: InstrProf.h:414
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::coverage::RawCoverageReader::RawCoverageReader
RawCoverageReader(StringRef Data)
Definition: CoverageMappingReader.h:113
llvm::coverage::CoverageMappingRecord::FunctionHash
uint64_t FunctionHash
Definition: CoverageMappingReader.h:37
llvm::coverage::CoverageMappingRecord::MappingRegions
ArrayRef< CounterMappingRegion > MappingRegions
Definition: CoverageMappingReader.h:40
llvm::coverage::CoverageMappingRecord::Filenames
ArrayRef< StringRef > Filenames
Definition: CoverageMappingReader.h:38
llvm::coverage::CoverageMappingReader::readNextRecord
virtual Error readNextRecord(CoverageMappingRecord &Record)=0
llvm::coverage::CoverageMappingIterator::operator->
Expected< CoverageMappingRecord * > operator->()
Definition: CoverageMappingReader.h:89
Expressions
gvn Early GVN Hoisting of Expressions
Definition: GVNHoist.cpp:1261
llvm::support::endianness
endianness
Definition: Endian.h:27
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:43
llvm::coverage::CoverageMappingIterator::iterator_category
std::input_iterator_tag iterator_category
Definition: CoverageMappingReader.h:52
llvm::coverage::BinaryCoverageReader::ProfileMappingRecord::ProfileMappingRecord
ProfileMappingRecord(CovMapVersion Version, StringRef FunctionName, uint64_t FunctionHash, StringRef CoverageMapping, size_t FilenamesBegin, size_t FilenamesSize)
Definition: CoverageMappingReader.h:174
llvm::coverage::CoverageMappingReader::end
CoverageMappingIterator end()
Definition: CoverageMappingReader.h:105
llvm::coverage::RawCoverageFilenamesReader::read
Error read(CovMapVersion Version)
Definition: CoverageMappingReader.cpp:101
llvm::coverage::CoverageMappingIterator::CoverageMappingIterator
CoverageMappingIterator(CoverageMappingReader *Reader)
Definition: CoverageMappingReader.h:61
llvm::coverage::BinaryCoverageReader::FuncRecordsStorage
std::unique_ptr< MemoryBuffer > FuncRecordsStorage
Definition: CoverageMappingReader.h:182
llvm::coverage::CoverageMappingRecord
Coverage mapping information for a single function.
Definition: CoverageMappingReader.h:35
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::coverage::RawCoverageReader::readString
Error readString(StringRef &Result)
Definition: CoverageMappingReader.cpp:92