#ifndef LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPINGREADER_H
#define LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPINGREADER_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ProfileData/Coverage/CoverageMapping.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/MemoryBuffer.h"
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <memory>
#include <vector>
namespace llvm {
namespace coverage {
class CoverageMappingReader;
struct CoverageMappingRecord {
StringRef FunctionName;
uint64_t FunctionHash;
ArrayRef<StringRef> Filenames;
ArrayRef<CounterExpression> Expressions;
ArrayRef<CounterMappingRegion> MappingRegions;
};
class CoverageMappingIterator {
CoverageMappingReader *Reader;
CoverageMappingRecord Record;
coveragemap_error ReadErr;
void increment();
public:
using iterator_category = std::input_iterator_tag;
using value_type = CoverageMappingRecord;
using difference_type = std::ptrdiff_t;
using pointer = value_type *;
using reference = value_type &;
CoverageMappingIterator()
: Reader(nullptr), ReadErr(coveragemap_error::success) {}
CoverageMappingIterator(CoverageMappingReader *Reader)
: Reader(Reader), ReadErr(coveragemap_error::success) {
increment();
}
~CoverageMappingIterator() {
if (ReadErr != coveragemap_error::success)
llvm_unreachable("Unexpected error in coverage mapping iterator");
}
CoverageMappingIterator &operator++() {
increment();
return *this;
}
bool operator==(const CoverageMappingIterator &RHS) const {
return Reader == RHS.Reader;
}
bool operator!=(const CoverageMappingIterator &RHS) const {
return Reader != RHS.Reader;
}
Expected<CoverageMappingRecord &> operator*() {
if (ReadErr != coveragemap_error::success) {
auto E = make_error<CoverageMapError>(ReadErr);
ReadErr = coveragemap_error::success;
return std::move(E);
}
return Record;
}
Expected<CoverageMappingRecord *> operator->() {
if (ReadErr != coveragemap_error::success) {
auto E = make_error<CoverageMapError>(ReadErr);
ReadErr = coveragemap_error::success;
return std::move(E);
}
return &Record;
}
};
class CoverageMappingReader {
public:
virtual ~CoverageMappingReader() = default;
virtual Error readNextRecord(CoverageMappingRecord &Record) = 0;
CoverageMappingIterator begin() { return CoverageMappingIterator(this); }
CoverageMappingIterator end() { return CoverageMappingIterator(); }
};
class RawCoverageReader {
protected:
StringRef Data;
RawCoverageReader(StringRef Data) : Data(Data) {}
Error readULEB128(uint64_t &Result);
Error readIntMax(uint64_t &Result, uint64_t MaxPlus1);
Error readSize(uint64_t &Result);
Error readString(StringRef &Result);
};
class RawCoverageMappingDummyChecker : public RawCoverageReader {
public:
RawCoverageMappingDummyChecker(StringRef MappingData)
: RawCoverageReader(MappingData) {}
Expected<bool> isDummy();
};
class RawCoverageMappingReader : public RawCoverageReader {
ArrayRef<std::string> &TranslationUnitFilenames;
std::vector<StringRef> &Filenames;
std::vector<CounterExpression> &Expressions;
std::vector<CounterMappingRegion> &MappingRegions;
public:
RawCoverageMappingReader(StringRef MappingData,
ArrayRef<std::string> &TranslationUnitFilenames,
std::vector<StringRef> &Filenames,
std::vector<CounterExpression> &Expressions,
std::vector<CounterMappingRegion> &MappingRegions)
: RawCoverageReader(MappingData),
TranslationUnitFilenames(TranslationUnitFilenames),
Filenames(Filenames), Expressions(Expressions),
MappingRegions(MappingRegions) {}
RawCoverageMappingReader(const RawCoverageMappingReader &) = delete;
RawCoverageMappingReader &
operator=(const RawCoverageMappingReader &) = delete;
Error read();
private:
Error decodeCounter(unsigned Value, Counter &C);
Error readCounter(Counter &C);
Error
readMappingRegionsSubArray(std::vector<CounterMappingRegion> &MappingRegions,
unsigned InferredFileID, size_t NumFileIDs);
};
class BinaryCoverageReader : public CoverageMappingReader {
public:
struct ProfileMappingRecord {
CovMapVersion Version;
StringRef FunctionName;
uint64_t FunctionHash;
StringRef CoverageMapping;
size_t FilenamesBegin;
size_t FilenamesSize;
ProfileMappingRecord(CovMapVersion Version, StringRef FunctionName,
uint64_t FunctionHash, StringRef CoverageMapping,
size_t FilenamesBegin, size_t FilenamesSize)
: Version(Version), FunctionName(FunctionName),
FunctionHash(FunctionHash), CoverageMapping(CoverageMapping),
FilenamesBegin(FilenamesBegin), FilenamesSize(FilenamesSize) {}
};
using FuncRecordsStorage = std::unique_ptr<MemoryBuffer>;
private:
std::vector<std::string> Filenames;
std::vector<ProfileMappingRecord> MappingRecords;
InstrProfSymtab ProfileNames;
size_t CurrentRecord = 0;
std::vector<StringRef> FunctionsFilenames;
std::vector<CounterExpression> Expressions;
std::vector<CounterMappingRegion> MappingRegions;
FuncRecordsStorage FuncRecords;
BinaryCoverageReader(FuncRecordsStorage &&FuncRecords)
: FuncRecords(std::move(FuncRecords)) {}
public:
BinaryCoverageReader(const BinaryCoverageReader &) = delete;
BinaryCoverageReader &operator=(const BinaryCoverageReader &) = delete;
static Expected<std::vector<std::unique_ptr<BinaryCoverageReader>>>
create(MemoryBufferRef ObjectBuffer, StringRef Arch,
SmallVectorImpl<std::unique_ptr<MemoryBuffer>> &ObjectFileBuffers,
StringRef CompilationDir = "");
static Expected<std::unique_ptr<BinaryCoverageReader>>
createCoverageReaderFromBuffer(StringRef Coverage,
FuncRecordsStorage &&FuncRecords,
InstrProfSymtab &&ProfileNames,
uint8_t BytesInAddress,
support::endianness Endian,
StringRef CompilationDir = "");
Error readNextRecord(CoverageMappingRecord &Record) override;
};
class RawCoverageFilenamesReader : public RawCoverageReader {
std::vector<std::string> &Filenames;
StringRef CompilationDir;
Error readUncompressed(CovMapVersion Version, uint64_t NumFilenames);
public:
RawCoverageFilenamesReader(StringRef Data,
std::vector<std::string> &Filenames,
StringRef CompilationDir = "")
: RawCoverageReader(Data), Filenames(Filenames),
CompilationDir(CompilationDir) {}
RawCoverageFilenamesReader(const RawCoverageFilenamesReader &) = delete;
RawCoverageFilenamesReader &
operator=(const RawCoverageFilenamesReader &) = delete;
Error read(CovMapVersion Version);
};
} }
#endif