1 //=-- CoverageMappingReader.h - Code coverage mapping reader ------*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for reading coverage mapping data for
11 // instrumentation based coverage.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_PROFILEDATA_COVERAGEMAPPINGREADER_H
16 #define LLVM_PROFILEDATA_COVERAGEMAPPINGREADER_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/ProfileData/CoverageMapping.h"
23 #include "llvm/ProfileData/InstrProf.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/MemoryBuffer.h"
31 class CoverageMappingReader;
33 /// \brief Coverage mapping information for a single function.
34 struct CoverageMappingRecord {
35 StringRef FunctionName;
36 uint64_t FunctionHash;
37 ArrayRef<StringRef> Filenames;
38 ArrayRef<CounterExpression> Expressions;
39 ArrayRef<CounterMappingRegion> MappingRegions;
42 /// \brief A file format agnostic iterator over coverage mapping data.
43 class CoverageMappingIterator
44 : public std::iterator<std::input_iterator_tag, CoverageMappingRecord> {
45 CoverageMappingReader *Reader;
46 CoverageMappingRecord Record;
51 CoverageMappingIterator() : Reader(nullptr) {}
52 CoverageMappingIterator(CoverageMappingReader *Reader) : Reader(Reader) {
56 CoverageMappingIterator &operator++() {
60 bool operator==(const CoverageMappingIterator &RHS) {
61 return Reader == RHS.Reader;
63 bool operator!=(const CoverageMappingIterator &RHS) {
64 return Reader != RHS.Reader;
66 CoverageMappingRecord &operator*() { return Record; }
67 CoverageMappingRecord *operator->() { return &Record; }
70 class CoverageMappingReader {
72 virtual std::error_code readNextRecord(CoverageMappingRecord &Record) = 0;
73 CoverageMappingIterator begin() { return CoverageMappingIterator(this); }
74 CoverageMappingIterator end() { return CoverageMappingIterator(); }
75 virtual ~CoverageMappingReader() {}
78 /// \brief Base class for the raw coverage mapping and filenames data readers.
79 class RawCoverageReader {
83 RawCoverageReader(StringRef Data) : Data(Data) {}
85 std::error_code readULEB128(uint64_t &Result);
86 std::error_code readIntMax(uint64_t &Result, uint64_t MaxPlus1);
87 std::error_code readSize(uint64_t &Result);
88 std::error_code readString(StringRef &Result);
91 /// \brief Reader for the raw coverage filenames.
92 class RawCoverageFilenamesReader : public RawCoverageReader {
93 std::vector<StringRef> &Filenames;
95 RawCoverageFilenamesReader(const RawCoverageFilenamesReader &) = delete;
96 RawCoverageFilenamesReader &
97 operator=(const RawCoverageFilenamesReader &) = delete;
100 RawCoverageFilenamesReader(StringRef Data, std::vector<StringRef> &Filenames)
101 : RawCoverageReader(Data), Filenames(Filenames) {}
103 std::error_code read();
106 /// \brief Reader for the raw coverage mapping data.
107 class RawCoverageMappingReader : public RawCoverageReader {
108 ArrayRef<StringRef> TranslationUnitFilenames;
109 std::vector<StringRef> &Filenames;
110 std::vector<CounterExpression> &Expressions;
111 std::vector<CounterMappingRegion> &MappingRegions;
113 RawCoverageMappingReader(const RawCoverageMappingReader &) = delete;
114 RawCoverageMappingReader &
115 operator=(const RawCoverageMappingReader &) = delete;
118 RawCoverageMappingReader(StringRef MappingData,
119 ArrayRef<StringRef> TranslationUnitFilenames,
120 std::vector<StringRef> &Filenames,
121 std::vector<CounterExpression> &Expressions,
122 std::vector<CounterMappingRegion> &MappingRegions)
123 : RawCoverageReader(MappingData),
124 TranslationUnitFilenames(TranslationUnitFilenames),
125 Filenames(Filenames), Expressions(Expressions),
126 MappingRegions(MappingRegions) {}
128 std::error_code read();
131 std::error_code decodeCounter(unsigned Value, Counter &C);
132 std::error_code readCounter(Counter &C);
134 readMappingRegionsSubArray(std::vector<CounterMappingRegion> &MappingRegions,
135 unsigned InferredFileID, size_t NumFileIDs);
138 /// \brief Reader for the coverage mapping data that is emitted by the
139 /// frontend and stored in an object file.
140 class BinaryCoverageReader : public CoverageMappingReader {
142 struct ProfileMappingRecord {
143 CoverageMappingVersion Version;
144 StringRef FunctionName;
145 uint64_t FunctionHash;
146 StringRef CoverageMapping;
147 size_t FilenamesBegin;
148 size_t FilenamesSize;
150 ProfileMappingRecord(CoverageMappingVersion Version, StringRef FunctionName,
151 uint64_t FunctionHash, StringRef CoverageMapping,
152 size_t FilenamesBegin, size_t FilenamesSize)
153 : Version(Version), FunctionName(FunctionName),
154 FunctionHash(FunctionHash), CoverageMapping(CoverageMapping),
155 FilenamesBegin(FilenamesBegin), FilenamesSize(FilenamesSize) {}
159 std::vector<StringRef> Filenames;
160 std::vector<ProfileMappingRecord> MappingRecords;
161 size_t CurrentRecord;
162 std::vector<StringRef> FunctionsFilenames;
163 std::vector<CounterExpression> Expressions;
164 std::vector<CounterMappingRegion> MappingRegions;
166 BinaryCoverageReader(const BinaryCoverageReader &) = delete;
167 BinaryCoverageReader &operator=(const BinaryCoverageReader &) = delete;
169 BinaryCoverageReader() : CurrentRecord(0) {}
172 static ErrorOr<std::unique_ptr<BinaryCoverageReader>>
173 create(std::unique_ptr<MemoryBuffer> &ObjectBuffer,
176 std::error_code readNextRecord(CoverageMappingRecord &Record) override;
179 } // end namespace coverage
180 } // end namespace llvm