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/ProfileData/InstrProf.h"
19 #include "llvm/ProfileData/CoverageMapping.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/FileSystem.h"
31 class ObjectFileCoverageMappingReader;
33 /// \brief Coverage mapping information for a single function.
34 struct CoverageMappingRecord {
35 StringRef FunctionName;
36 ArrayRef<StringRef> Filenames;
37 ArrayRef<CounterExpression> Expressions;
38 ArrayRef<CounterMappingRegion> MappingRegions;
41 /// \brief A file format agnostic iterator over coverage mapping data.
42 class CoverageMappingIterator
43 : public std::iterator<std::input_iterator_tag, CoverageMappingRecord> {
44 ObjectFileCoverageMappingReader *Reader;
45 CoverageMappingRecord Record;
50 CoverageMappingIterator() : Reader(nullptr) {}
51 CoverageMappingIterator(ObjectFileCoverageMappingReader *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 /// \brief Base class for the raw coverage mapping and filenames data readers.
71 class RawCoverageReader {
75 /// \brief Return the error code.
76 std::error_code error(std::error_code EC) { return EC; }
78 /// \brief Clear the current error code and return a successful one.
79 std::error_code success() { return error(instrprof_error::success); }
81 RawCoverageReader(StringRef Data) : Data(Data) {}
83 std::error_code readULEB128(uint64_t &Result);
84 std::error_code readIntMax(uint64_t &Result, uint64_t MaxPlus1);
85 std::error_code readSize(uint64_t &Result);
86 std::error_code readString(StringRef &Result);
89 /// \brief Reader for the raw coverage filenames.
90 class RawCoverageFilenamesReader : public RawCoverageReader {
91 std::vector<StringRef> &Filenames;
93 RawCoverageFilenamesReader(const RawCoverageFilenamesReader &)
94 LLVM_DELETED_FUNCTION;
95 RawCoverageFilenamesReader &
96 operator=(const RawCoverageFilenamesReader &) LLVM_DELETED_FUNCTION;
99 RawCoverageFilenamesReader(StringRef Data, std::vector<StringRef> &Filenames)
100 : RawCoverageReader(Data), Filenames(Filenames) {}
102 std::error_code read();
105 /// \brief Reader for the raw coverage mapping data.
106 class RawCoverageMappingReader : public RawCoverageReader {
107 StringRef FunctionName;
108 ArrayRef<StringRef> TranslationUnitFilenames;
109 std::vector<StringRef> &Filenames;
110 std::vector<CounterExpression> &Expressions;
111 std::vector<CounterMappingRegion> &MappingRegions;
113 RawCoverageMappingReader(const RawCoverageMappingReader &)
114 LLVM_DELETED_FUNCTION;
115 RawCoverageMappingReader &
116 operator=(const RawCoverageMappingReader &) LLVM_DELETED_FUNCTION;
119 RawCoverageMappingReader(StringRef FunctionName, StringRef MappingData,
120 ArrayRef<StringRef> TranslationUnitFilenames,
121 std::vector<StringRef> &Filenames,
122 std::vector<CounterExpression> &Expressions,
123 std::vector<CounterMappingRegion> &MappingRegions)
124 : RawCoverageReader(MappingData), FunctionName(FunctionName),
125 TranslationUnitFilenames(TranslationUnitFilenames),
126 Filenames(Filenames), Expressions(Expressions),
127 MappingRegions(MappingRegions) {}
129 std::error_code read(CoverageMappingRecord &Record);
132 std::error_code decodeCounter(unsigned Value, Counter &C);
133 std::error_code readCounter(Counter &C);
135 readMappingRegionsSubArray(std::vector<CounterMappingRegion> &MappingRegions,
136 unsigned InferredFileID, size_t NumFileIDs);
139 /// \brief Reader for the coverage mapping data that is emitted by the
140 /// frontend and stored in an object file.
141 class ObjectFileCoverageMappingReader {
143 struct ProfileMappingRecord {
144 CoverageMappingVersion Version;
145 StringRef FunctionName;
146 StringRef CoverageMapping;
147 size_t FilenamesBegin;
148 size_t FilenamesSize;
150 ProfileMappingRecord(CoverageMappingVersion Version, StringRef FunctionName,
151 StringRef CoverageMapping, size_t FilenamesBegin,
152 size_t FilenamesSize)
153 : Version(Version), FunctionName(FunctionName),
154 CoverageMapping(CoverageMapping), FilenamesBegin(FilenamesBegin),
155 FilenamesSize(FilenamesSize) {}
159 std::error_code LastError;
160 object::OwningBinary<object::ObjectFile> Object;
161 std::vector<StringRef> Filenames;
162 std::vector<ProfileMappingRecord> MappingRecords;
163 size_t CurrentRecord;
164 std::vector<StringRef> FunctionsFilenames;
165 std::vector<CounterExpression> Expressions;
166 std::vector<CounterMappingRegion> MappingRegions;
168 ObjectFileCoverageMappingReader(const ObjectFileCoverageMappingReader &)
169 LLVM_DELETED_FUNCTION;
170 ObjectFileCoverageMappingReader &
171 operator=(const ObjectFileCoverageMappingReader &) LLVM_DELETED_FUNCTION;
173 /// \brief Set the current error_code and return same.
174 std::error_code error(std::error_code EC) {
179 /// \brief Clear the current error code and return a successful one.
180 std::error_code success() { return error(instrprof_error::success); }
183 ObjectFileCoverageMappingReader(StringRef FileName);
184 ObjectFileCoverageMappingReader(
185 std::unique_ptr<MemoryBuffer> &ObjectBuffer,
186 sys::fs::file_magic Type = sys::fs::file_magic::unknown);
188 std::error_code readHeader();
189 std::error_code readNextRecord(CoverageMappingRecord &Record);
191 /// Iterator over profile data.
192 CoverageMappingIterator begin() { return CoverageMappingIterator(this); }
193 CoverageMappingIterator end() { return CoverageMappingIterator(); }
195 /// \brief Return true if the reader has finished reading the profile data.
196 bool isEOF() { return LastError == instrprof_error::eof; }
197 /// \brief Return true if the reader encountered an error reading profiling
199 bool hasError() { return LastError && !isEOF(); }
200 /// \brief Get the current error code.
201 std::error_code getError() { return LastError; }
204 } // end namespace coverage
205 } // end namespace llvm