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/Object/ObjectFile.h"
21 #include "llvm/ProfileData/CoverageMapping.h"
22 #include "llvm/ProfileData/InstrProf.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/MemoryBuffer.h"
30 class ObjectFileCoverageMappingReader;
32 /// \brief Coverage mapping information for a single function.
33 struct CoverageMappingRecord {
34 StringRef FunctionName;
35 uint64_t FunctionHash;
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 &) = delete;
94 RawCoverageFilenamesReader &
95 operator=(const RawCoverageFilenamesReader &) = delete;
98 RawCoverageFilenamesReader(StringRef Data, std::vector<StringRef> &Filenames)
99 : RawCoverageReader(Data), Filenames(Filenames) {}
101 std::error_code read();
104 /// \brief Reader for the raw coverage mapping data.
105 class RawCoverageMappingReader : public RawCoverageReader {
106 ArrayRef<StringRef> TranslationUnitFilenames;
107 std::vector<StringRef> &Filenames;
108 std::vector<CounterExpression> &Expressions;
109 std::vector<CounterMappingRegion> &MappingRegions;
111 RawCoverageMappingReader(const RawCoverageMappingReader &) = delete;
112 RawCoverageMappingReader &
113 operator=(const RawCoverageMappingReader &) = delete;
116 RawCoverageMappingReader(StringRef MappingData,
117 ArrayRef<StringRef> TranslationUnitFilenames,
118 std::vector<StringRef> &Filenames,
119 std::vector<CounterExpression> &Expressions,
120 std::vector<CounterMappingRegion> &MappingRegions)
121 : RawCoverageReader(MappingData),
122 TranslationUnitFilenames(TranslationUnitFilenames),
123 Filenames(Filenames), Expressions(Expressions),
124 MappingRegions(MappingRegions) {}
126 std::error_code read();
129 std::error_code decodeCounter(unsigned Value, Counter &C);
130 std::error_code readCounter(Counter &C);
132 readMappingRegionsSubArray(std::vector<CounterMappingRegion> &MappingRegions,
133 unsigned InferredFileID, size_t NumFileIDs);
136 /// \brief Reader for the coverage mapping data that is emitted by the
137 /// frontend and stored in an object file.
138 class ObjectFileCoverageMappingReader {
140 struct ProfileMappingRecord {
141 CoverageMappingVersion Version;
142 StringRef FunctionName;
143 uint64_t FunctionHash;
144 StringRef CoverageMapping;
145 size_t FilenamesBegin;
146 size_t FilenamesSize;
148 ProfileMappingRecord(CoverageMappingVersion Version, StringRef FunctionName,
149 uint64_t FunctionHash, StringRef CoverageMapping,
150 size_t FilenamesBegin, size_t FilenamesSize)
151 : Version(Version), FunctionName(FunctionName),
152 FunctionHash(FunctionHash), CoverageMapping(CoverageMapping),
153 FilenamesBegin(FilenamesBegin), FilenamesSize(FilenamesSize) {}
157 std::error_code LastError;
158 object::OwningBinary<object::ObjectFile> Object;
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 ObjectFileCoverageMappingReader(const ObjectFileCoverageMappingReader &)
168 ObjectFileCoverageMappingReader &
169 operator=(const ObjectFileCoverageMappingReader &) = delete;
171 /// \brief Set the current error_code and return same.
172 std::error_code error(std::error_code EC) {
177 /// \brief Clear the current error code and return a successful one.
178 std::error_code success() { return error(instrprof_error::success); }
181 ObjectFileCoverageMappingReader(StringRef FileName);
182 ObjectFileCoverageMappingReader(
183 std::unique_ptr<MemoryBuffer> &ObjectBuffer,
184 sys::fs::file_magic Type = sys::fs::file_magic::unknown);
186 std::error_code readHeader();
187 std::error_code readNextRecord(CoverageMappingRecord &Record);
189 /// Iterator over profile data.
190 CoverageMappingIterator begin() { return CoverageMappingIterator(this); }
191 CoverageMappingIterator end() { return CoverageMappingIterator(); }
193 /// \brief Return true if the reader has finished reading the profile data.
194 bool isEOF() { return LastError == instrprof_error::eof; }
195 /// \brief Return true if the reader encountered an error reading profiling
197 bool hasError() { return LastError && !isEOF(); }
198 /// \brief Get the current error code.
199 std::error_code getError() { return LastError; }
202 } // end namespace coverage
203 } // end namespace llvm