LLVM 20.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
29namespace llvm {
30namespace coverage {
31
32class CoverageMappingReader;
33
34/// Coverage mapping information for a single function.
41};
42
43/// A file format agnostic iterator over coverage mapping data.
47 coveragemap_error ReadErr;
48
49 void increment();
50
51public:
52 using iterator_category = std::input_iterator_tag;
54 using difference_type = std::ptrdiff_t;
57
59 : Reader(nullptr), ReadErr(coveragemap_error::success) {}
60
62 : Reader(Reader), 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 }
76 return Reader == RHS.Reader;
77 }
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
100public:
101 virtual ~CoverageMappingReader() = default;
102
106};
107
108/// Base class for the raw coverage mapping and filenames data readers.
110protected:
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.
124public:
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
138public:
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) {}
151
152 Error read();
153
154private:
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.
165public:
173
176 size_t FilenamesBegin, size_t FilenamesSize)
180 };
181
182 using FuncRecordsStorage = std::unique_ptr<MemoryBuffer>;
183
184private:
185 std::vector<std::string> Filenames;
186 std::vector<ProfileMappingRecord> MappingRecords;
187 std::unique_ptr<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
198 BinaryCoverageReader(std::unique_ptr<InstrProfSymtab> Symtab,
199 FuncRecordsStorage &&FuncRecords)
200 : ProfileNames(std::move(Symtab)), FuncRecords(std::move(FuncRecords)) {}
201
202public:
205
207 create(MemoryBufferRef ObjectBuffer, StringRef Arch,
208 SmallVectorImpl<std::unique_ptr<MemoryBuffer>> &ObjectFileBuffers,
209 StringRef CompilationDir = "",
210 SmallVectorImpl<object::BuildIDRef> *BinaryIDs = nullptr);
211
214 StringRef Coverage, FuncRecordsStorage &&FuncRecords,
215 std::unique_ptr<InstrProfSymtab> ProfileNamesPtr, uint8_t BytesInAddress,
216 llvm::endianness Endian, 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
229public:
231 std::vector<std::string> &Filenames,
232 StringRef CompilationDir = "")
233 : RawCoverageReader(Data), Filenames(Filenames),
234 CompilationDir(CompilationDir) {}
238
240};
241
242} // end namespace coverage
243} // end namespace llvm
244
245#endif // LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPINGREADER_H
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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:160
Tagged union holding either a T or a Error.
Definition: Error.h:481
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
LLVM Value Representation.
Definition: Value.h:74
Reader for the coverage mapping data that is emitted by the frontend and stored in an object file.
static Expected< std::unique_ptr< BinaryCoverageReader > > createCoverageReaderFromBuffer(StringRef Coverage, FuncRecordsStorage &&FuncRecords, std::unique_ptr< InstrProfSymtab > ProfileNamesPtr, uint8_t BytesInAddress, llvm::endianness Endian, StringRef CompilationDir="")
static Expected< std::vector< std::unique_ptr< BinaryCoverageReader > > > create(MemoryBufferRef ObjectBuffer, StringRef Arch, SmallVectorImpl< std::unique_ptr< MemoryBuffer > > &ObjectFileBuffers, StringRef CompilationDir="", SmallVectorImpl< object::BuildIDRef > *BinaryIDs=nullptr)
BinaryCoverageReader & operator=(const BinaryCoverageReader &)=delete
std::unique_ptr< MemoryBuffer > FuncRecordsStorage
Error readNextRecord(CoverageMappingRecord &Record) override
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="")
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.
Error readIntMax(uint64_t &Result, uint64_t MaxPlus1)
#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:1856
endianness
Definition: bit.h:70
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
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