LLVM 22.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"
22#include "llvm/Support/Error.h"
24#include <cstddef>
25#include <cstdint>
26#include <iterator>
27#include <memory>
28#include <vector>
29
30namespace llvm {
31namespace coverage {
32
33class CoverageMappingReader;
34
35/// Coverage mapping information for a single function.
42};
43
44/// A file format agnostic iterator over coverage mapping data.
48 coveragemap_error ReadErr;
49
50 LLVM_ABI void increment();
51
52public:
53 using iterator_category = std::input_iterator_tag;
55 using difference_type = std::ptrdiff_t;
58
60 : Reader(nullptr), ReadErr(coveragemap_error::success) {}
61
63 : Reader(Reader), ReadErr(coveragemap_error::success) {
64 increment();
65 }
66
68 if (ReadErr != coveragemap_error::success)
69 llvm_unreachable("Unexpected error in coverage mapping iterator");
70 }
71
73 increment();
74 return *this;
75 }
77 return Reader == RHS.Reader;
78 }
80 return Reader != RHS.Reader;
81 }
83 if (ReadErr != coveragemap_error::success) {
84 auto E = make_error<CoverageMapError>(ReadErr);
86 return std::move(E);
87 }
88 return Record;
89 }
91 if (ReadErr != coveragemap_error::success) {
92 auto E = make_error<CoverageMapError>(ReadErr);
94 return std::move(E);
95 }
96 return &Record;
97 }
98};
99
101public:
102 virtual ~CoverageMappingReader() = default;
103
107};
108
109/// Base class for the raw coverage mapping and filenames data readers.
111protected:
113
115
117 LLVM_ABI Error readIntMax(uint64_t &Result, uint64_t MaxPlus1);
120};
121
122/// Checks if the given coverage mapping data is exported for
123/// an unused function.
125public:
127 : RawCoverageReader(MappingData) {}
128
130};
131
132/// Reader for the raw coverage mapping data.
134 ArrayRef<std::string> &TranslationUnitFilenames;
135 std::vector<StringRef> &Filenames;
136 std::vector<CounterExpression> &Expressions;
137 std::vector<CounterMappingRegion> &MappingRegions;
138
139public:
141 ArrayRef<std::string> &TranslationUnitFilenames,
142 std::vector<StringRef> &Filenames,
143 std::vector<CounterExpression> &Expressions,
144 std::vector<CounterMappingRegion> &MappingRegions)
145 : RawCoverageReader(MappingData),
146 TranslationUnitFilenames(TranslationUnitFilenames),
147 Filenames(Filenames), Expressions(Expressions),
148 MappingRegions(MappingRegions) {}
152
154
155private:
156 Error decodeCounter(unsigned Value, Counter &C);
157 Error readCounter(Counter &C);
158 Error
159 readMappingRegionsSubArray(std::vector<CounterMappingRegion> &MappingRegions,
160 unsigned InferredFileID, size_t NumFileIDs);
161};
162
163/// Reader for the coverage mapping data that is emitted by the
164/// frontend and stored in an object file.
166public:
174
176 uint64_t FunctionHash, StringRef CoverageMapping,
177 size_t FilenamesBegin, size_t FilenamesSize)
178 : Version(Version), FunctionName(FunctionName),
179 FunctionHash(FunctionHash), CoverageMapping(CoverageMapping),
180 FilenamesBegin(FilenamesBegin), FilenamesSize(FilenamesSize) {}
181 };
182
183 using FuncRecordsStorage = std::unique_ptr<MemoryBuffer>;
184 using CoverageMapCopyStorage = std::unique_ptr<MemoryBuffer>;
185
186private:
187 std::vector<std::string> Filenames;
188 std::vector<ProfileMappingRecord> MappingRecords;
189 std::unique_ptr<InstrProfSymtab> ProfileNames;
190 size_t CurrentRecord = 0;
191 std::vector<StringRef> FunctionsFilenames;
192 std::vector<CounterExpression> Expressions;
193 std::vector<CounterMappingRegion> MappingRegions;
194
195 // Used to tie the lifetimes of coverage function records to the lifetime of
196 // this BinaryCoverageReader instance. Needed to support the format change in
197 // D69471, which can split up function records into multiple sections on ELF.
198 FuncRecordsStorage FuncRecords;
199
200 // Used to tie the lifetimes of an optional copy of the coverage mapping data
201 // to the lifetime of this BinaryCoverageReader instance. Needed to support
202 // Wasm object format, which might require realignment of section contents.
203 CoverageMapCopyStorage CoverageMapCopy;
204
205 BinaryCoverageReader(std::unique_ptr<InstrProfSymtab> Symtab,
206 FuncRecordsStorage &&FuncRecords,
207 CoverageMapCopyStorage &&CoverageMapCopy)
208 : ProfileNames(std::move(Symtab)), FuncRecords(std::move(FuncRecords)),
209 CoverageMapCopy(std::move(CoverageMapCopy)) {}
210
211public:
214
216 create(MemoryBufferRef ObjectBuffer, StringRef Arch,
217 SmallVectorImpl<std::unique_ptr<MemoryBuffer>> &ObjectFileBuffers,
218 StringRef CompilationDir = "",
219 SmallVectorImpl<object::BuildIDRef> *BinaryIDs = nullptr);
220
222 createCoverageReaderFromBuffer(
223 StringRef Coverage, FuncRecordsStorage &&FuncRecords,
224 CoverageMapCopyStorage &&CoverageMap,
225 std::unique_ptr<InstrProfSymtab> ProfileNamesPtr, uint8_t BytesInAddress,
226 llvm::endianness Endian, StringRef CompilationDir = "");
227
228 Error readNextRecord(CoverageMappingRecord &Record) override;
229};
230
231/// Reader for the raw coverage filenames.
233 std::vector<std::string> &Filenames;
234 StringRef CompilationDir;
235
236 // Read an uncompressed sequence of filenames.
237 Error readUncompressed(CovMapVersion Version, uint64_t NumFilenames);
238
239public:
241 std::vector<std::string> &Filenames,
242 StringRef CompilationDir = "")
243 : RawCoverageReader(Data), Filenames(Filenames),
244 CompilationDir(CompilationDir) {}
248
250};
251
252} // end namespace coverage
253} // end namespace llvm
254
255#endif // LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPINGREADER_H
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition: Compiler.h:213
endianness Endian
Value * RHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
Tagged union holding either a T or a Error.
Definition: Error.h:485
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
LLVM Value Representation.
Definition: Value.h:75
Reader for the coverage mapping data that is emitted by the frontend and stored in an object file.
std::unique_ptr< MemoryBuffer > CoverageMapCopyStorage
BinaryCoverageReader & operator=(const BinaryCoverageReader &)=delete
std::unique_ptr< MemoryBuffer > FuncRecordsStorage
BinaryCoverageReader(const BinaryCoverageReader &)=delete
A file format agnostic iterator over coverage mapping data.
Expected< CoverageMappingRecord & > operator*()
bool operator==(const CoverageMappingIterator &RHS) const
Expected< CoverageMappingRecord * > operator->()
CoverageMappingIterator(CoverageMappingReader *Reader)
bool operator!=(const CoverageMappingIterator &RHS) const
virtual Error readNextRecord(CoverageMappingRecord &Record)=0
The mapping of profile information to coverage data.
Reader for the raw coverage filenames.
RawCoverageFilenamesReader(StringRef Data, std::vector< std::string > &Filenames, StringRef CompilationDir="")
LLVM_ABI Error read(CovMapVersion Version)
RawCoverageFilenamesReader(const RawCoverageFilenamesReader &)=delete
RawCoverageFilenamesReader & operator=(const RawCoverageFilenamesReader &)=delete
Checks if the given coverage mapping data is exported for an unused function.
Reader for the raw coverage mapping data.
RawCoverageMappingReader(const RawCoverageMappingReader &)=delete
RawCoverageMappingReader & operator=(const RawCoverageMappingReader &)=delete
RawCoverageMappingReader(StringRef MappingData, ArrayRef< std::string > &TranslationUnitFilenames, std::vector< StringRef > &Filenames, std::vector< CounterExpression > &Expressions, std::vector< CounterMappingRegion > &MappingRegions)
Base class for the raw coverage mapping and filenames data readers.
LLVM_ABI Error readSize(uint64_t &Result)
LLVM_ABI Error readIntMax(uint64_t &Result, uint64_t MaxPlus1)
LLVM_ABI Error readULEB128(uint64_t &Result)
LLVM_ABI Error readString(StringRef &Result)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1886
endianness
Definition: bit.h:71
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
ProfileMappingRecord(CovMapVersion Version, StringRef FunctionName, uint64_t FunctionHash, StringRef CoverageMapping, size_t FilenamesBegin, size_t FilenamesSize)
A Counter is an abstract value that describes how to compute the execution count for a region of code...
Coverage mapping information for a single function.
ArrayRef< CounterExpression > Expressions
ArrayRef< CounterMappingRegion > MappingRegions