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 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 ObjectFileCoverageMappingReader *Reader;
46 CoverageMappingRecord Record;
51 CoverageMappingIterator() : Reader(nullptr) {}
52 CoverageMappingIterator(ObjectFileCoverageMappingReader *Reader)
57 CoverageMappingIterator &operator++() {
61 bool operator==(const CoverageMappingIterator &RHS) {
62 return Reader == RHS.Reader;
64 bool operator!=(const CoverageMappingIterator &RHS) {
65 return Reader != RHS.Reader;
67 CoverageMappingRecord &operator*() { return Record; }
68 CoverageMappingRecord *operator->() { return &Record; }
71 /// \brief Base class for the raw coverage mapping and filenames data readers.
72 class RawCoverageReader {
76 /// \brief Return the error code.
77 std::error_code error(std::error_code EC) { return EC; }
79 /// \brief Clear the current error code and return a successful one.
80 std::error_code success() { return error(instrprof_error::success); }
82 RawCoverageReader(StringRef Data) : Data(Data) {}
84 std::error_code readULEB128(uint64_t &Result);
85 std::error_code readIntMax(uint64_t &Result, uint64_t MaxPlus1);
86 std::error_code readSize(uint64_t &Result);
87 std::error_code readString(StringRef &Result);
90 /// \brief Reader for the raw coverage filenames.
91 class RawCoverageFilenamesReader : public RawCoverageReader {
92 std::vector<StringRef> &Filenames;
94 RawCoverageFilenamesReader(const RawCoverageFilenamesReader &)
95 LLVM_DELETED_FUNCTION;
96 RawCoverageFilenamesReader &
97 operator=(const RawCoverageFilenamesReader &) LLVM_DELETED_FUNCTION;
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 StringRef FunctionName;
109 ArrayRef<StringRef> TranslationUnitFilenames;
110 std::vector<StringRef> &Filenames;
111 std::vector<CounterExpression> &Expressions;
112 std::vector<CounterMappingRegion> &MappingRegions;
114 RawCoverageMappingReader(const RawCoverageMappingReader &)
115 LLVM_DELETED_FUNCTION;
116 RawCoverageMappingReader &
117 operator=(const RawCoverageMappingReader &) LLVM_DELETED_FUNCTION;
120 RawCoverageMappingReader(StringRef FunctionName, StringRef MappingData,
121 ArrayRef<StringRef> TranslationUnitFilenames,
122 std::vector<StringRef> &Filenames,
123 std::vector<CounterExpression> &Expressions,
124 std::vector<CounterMappingRegion> &MappingRegions)
125 : RawCoverageReader(MappingData), FunctionName(FunctionName),
126 TranslationUnitFilenames(TranslationUnitFilenames),
127 Filenames(Filenames), Expressions(Expressions),
128 MappingRegions(MappingRegions) {}
130 std::error_code read(CoverageMappingRecord &Record);
133 std::error_code decodeCounter(unsigned Value, Counter &C);
134 std::error_code readCounter(Counter &C);
136 readMappingRegionsSubArray(std::vector<CounterMappingRegion> &MappingRegions,
137 unsigned InferredFileID, size_t NumFileIDs);
140 /// \brief Reader for the coverage mapping data that is emitted by the
141 /// frontend and stored in an object file.
142 class ObjectFileCoverageMappingReader {
144 struct ProfileMappingRecord {
145 CoverageMappingVersion Version;
146 StringRef FunctionName;
147 uint64_t FunctionHash;
148 StringRef CoverageMapping;
149 size_t FilenamesBegin;
150 size_t FilenamesSize;
152 ProfileMappingRecord(CoverageMappingVersion Version, StringRef FunctionName,
153 uint64_t FunctionHash, StringRef CoverageMapping,
154 size_t FilenamesBegin, size_t FilenamesSize)
155 : Version(Version), FunctionName(FunctionName),
156 FunctionHash(FunctionHash), CoverageMapping(CoverageMapping),
157 FilenamesBegin(FilenamesBegin), FilenamesSize(FilenamesSize) {}
161 std::error_code LastError;
162 object::OwningBinary<object::ObjectFile> Object;
163 std::vector<StringRef> Filenames;
164 std::vector<ProfileMappingRecord> MappingRecords;
165 size_t CurrentRecord;
166 std::vector<StringRef> FunctionsFilenames;
167 std::vector<CounterExpression> Expressions;
168 std::vector<CounterMappingRegion> MappingRegions;
170 ObjectFileCoverageMappingReader(const ObjectFileCoverageMappingReader &)
171 LLVM_DELETED_FUNCTION;
172 ObjectFileCoverageMappingReader &
173 operator=(const ObjectFileCoverageMappingReader &) LLVM_DELETED_FUNCTION;
175 /// \brief Set the current error_code and return same.
176 std::error_code error(std::error_code EC) {
181 /// \brief Clear the current error code and return a successful one.
182 std::error_code success() { return error(instrprof_error::success); }
185 ObjectFileCoverageMappingReader(StringRef FileName);
186 ObjectFileCoverageMappingReader(
187 std::unique_ptr<MemoryBuffer> &ObjectBuffer,
188 sys::fs::file_magic Type = sys::fs::file_magic::unknown);
190 std::error_code readHeader();
191 std::error_code readNextRecord(CoverageMappingRecord &Record);
193 /// Iterator over profile data.
194 CoverageMappingIterator begin() { return CoverageMappingIterator(this); }
195 CoverageMappingIterator end() { return CoverageMappingIterator(); }
197 /// \brief Return true if the reader has finished reading the profile data.
198 bool isEOF() { return LastError == instrprof_error::eof; }
199 /// \brief Return true if the reader encountered an error reading profiling
201 bool hasError() { return LastError && !isEOF(); }
202 /// \brief Get the current error code.
203 std::error_code getError() { return LastError; }
206 } // end namespace coverage
207 } // end namespace llvm