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
&)
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 uint64_t FunctionHash
;
147 StringRef CoverageMapping
;
148 size_t FilenamesBegin
;
149 size_t FilenamesSize
;
151 ProfileMappingRecord(CoverageMappingVersion Version
, StringRef FunctionName
,
152 uint64_t FunctionHash
, StringRef CoverageMapping
,
153 size_t FilenamesBegin
, size_t FilenamesSize
)
154 : Version(Version
), FunctionName(FunctionName
),
155 FunctionHash(FunctionHash
), CoverageMapping(CoverageMapping
),
156 FilenamesBegin(FilenamesBegin
), FilenamesSize(FilenamesSize
) {}
160 std::error_code LastError
;
161 object::OwningBinary
<object::ObjectFile
> Object
;
162 std::vector
<StringRef
> Filenames
;
163 std::vector
<ProfileMappingRecord
> MappingRecords
;
164 size_t CurrentRecord
;
165 std::vector
<StringRef
> FunctionsFilenames
;
166 std::vector
<CounterExpression
> Expressions
;
167 std::vector
<CounterMappingRegion
> MappingRegions
;
169 ObjectFileCoverageMappingReader(const ObjectFileCoverageMappingReader
&)
170 LLVM_DELETED_FUNCTION
;
171 ObjectFileCoverageMappingReader
&
172 operator=(const ObjectFileCoverageMappingReader
&) LLVM_DELETED_FUNCTION
;
174 /// \brief Set the current error_code and return same.
175 std::error_code
error(std::error_code EC
) {
180 /// \brief Clear the current error code and return a successful one.
181 std::error_code
success() { return error(instrprof_error::success
); }
184 ObjectFileCoverageMappingReader(StringRef FileName
);
185 ObjectFileCoverageMappingReader(
186 std::unique_ptr
<MemoryBuffer
> &ObjectBuffer
,
187 sys::fs::file_magic Type
= sys::fs::file_magic::unknown
);
189 std::error_code
readHeader();
190 std::error_code
readNextRecord(CoverageMappingRecord
&Record
);
192 /// Iterator over profile data.
193 CoverageMappingIterator
begin() { return CoverageMappingIterator(this); }
194 CoverageMappingIterator
end() { return CoverageMappingIterator(); }
196 /// \brief Return true if the reader has finished reading the profile data.
197 bool isEOF() { return LastError
== instrprof_error::eof
; }
198 /// \brief Return true if the reader encountered an error reading profiling
200 bool hasError() { return LastError
&& !isEOF(); }
201 /// \brief Get the current error code.
202 std::error_code
getError() { return LastError
; }
205 } // end namespace coverage
206 } // end namespace llvm