LLVM  3.7.0
CoverageMappingReader.h
Go to the documentation of this file.
1 //=-- CoverageMappingReader.h - Code coverage mapping reader ------*- C++ -*-=//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for reading coverage mapping data for
11 // instrumentation based coverage.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_PROFILEDATA_COVERAGEMAPPINGREADER_H
16 #define LLVM_PROFILEDATA_COVERAGEMAPPINGREADER_H
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Object/ObjectFile.h"
26 #include <iterator>
27 
28 namespace llvm {
29 namespace coverage {
30 
31 class CoverageMappingReader;
32 
33 /// \brief Coverage mapping information for a single function.
36  uint64_t FunctionHash;
40 };
41 
42 /// \brief A file format agnostic iterator over coverage mapping data.
44  : public std::iterator<std::input_iterator_tag, CoverageMappingRecord> {
45  CoverageMappingReader *Reader;
47 
48  void increment();
49 
50 public:
51  CoverageMappingIterator() : Reader(nullptr) {}
52  CoverageMappingIterator(CoverageMappingReader *Reader) : Reader(Reader) {
53  increment();
54  }
55 
57  increment();
58  return *this;
59  }
61  return Reader == RHS.Reader;
62  }
64  return Reader != RHS.Reader;
65  }
68 };
69 
71 public:
72  virtual std::error_code readNextRecord(CoverageMappingRecord &Record) = 0;
76 };
77 
78 /// \brief Base class for the raw coverage mapping and filenames data readers.
80 protected:
82 
83  RawCoverageReader(StringRef Data) : Data(Data) {}
84 
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);
89 };
90 
91 /// \brief Reader for the raw coverage filenames.
93  std::vector<StringRef> &Filenames;
94 
97  operator=(const RawCoverageFilenamesReader &) = delete;
98 
99 public:
100  RawCoverageFilenamesReader(StringRef Data, std::vector<StringRef> &Filenames)
101  : RawCoverageReader(Data), Filenames(Filenames) {}
102 
103  std::error_code read();
104 };
105 
106 /// \brief Reader for the raw coverage mapping data.
108  ArrayRef<StringRef> TranslationUnitFilenames;
109  std::vector<StringRef> &Filenames;
110  std::vector<CounterExpression> &Expressions;
111  std::vector<CounterMappingRegion> &MappingRegions;
112 
115  operator=(const RawCoverageMappingReader &) = delete;
116 
117 public:
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) {}
127 
128  std::error_code read();
129 
130 private:
131  std::error_code decodeCounter(unsigned Value, Counter &C);
132  std::error_code readCounter(Counter &C);
133  std::error_code
134  readMappingRegionsSubArray(std::vector<CounterMappingRegion> &MappingRegions,
135  unsigned InferredFileID, size_t NumFileIDs);
136 };
137 
138 /// \brief Reader for the coverage mapping data that is emitted by the
139 /// frontend and stored in an object file.
141 public:
145  uint64_t FunctionHash;
149 
152  size_t FilenamesBegin, size_t FilenamesSize)
153  : Version(Version), FunctionName(FunctionName),
154  FunctionHash(FunctionHash), CoverageMapping(CoverageMapping),
155  FilenamesBegin(FilenamesBegin), FilenamesSize(FilenamesSize) {}
156  };
157 
158 private:
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;
165 
166  BinaryCoverageReader(const BinaryCoverageReader &) = delete;
167  BinaryCoverageReader &operator=(const BinaryCoverageReader &) = delete;
168 
169  BinaryCoverageReader() : CurrentRecord(0) {}
170 
171 public:
173  create(std::unique_ptr<MemoryBuffer> &ObjectBuffer,
174  StringRef Arch);
175 
176  std::error_code readNextRecord(CoverageMappingRecord &Record) override;
177 };
178 
179 } // end namespace coverage
180 } // end namespace llvm
181 
182 #endif
Represents either an error or a value T.
Definition: ErrorOr.h:82
std::error_code readNextRecord(CoverageMappingRecord &Record) override
Reader for the raw coverage mapping data.
RawCoverageFilenamesReader(StringRef Data, std::vector< StringRef > &Filenames)
Base class for the raw coverage mapping and filenames data readers.
CoverageMappingIterator(CoverageMappingReader *Reader)
ArrayRef< CounterMappingRegion > MappingRegions
std::error_code readULEB128(uint64_t &Result)
Reader for the coverage mapping data that is emitted by the frontend and stored in an object file...
RawCoverageMappingReader(StringRef MappingData, ArrayRef< StringRef > TranslationUnitFilenames, std::vector< StringRef > &Filenames, std::vector< CounterExpression > &Expressions, std::vector< CounterMappingRegion > &MappingRegions)
ArrayRef< CounterExpression > Expressions
ProfileMappingRecord(CoverageMappingVersion Version, StringRef FunctionName, uint64_t FunctionHash, StringRef CoverageMapping, size_t FilenamesBegin, size_t FilenamesSize)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
Coverage mapping information for a single function.
Reader for the raw coverage filenames.
static ErrorOr< std::unique_ptr< BinaryCoverageReader > > create(std::unique_ptr< MemoryBuffer > &ObjectBuffer, StringRef Arch)
std::error_code readString(StringRef &Result)
bool operator==(const CoverageMappingIterator &RHS)
A file format agnostic iterator over coverage mapping data.
The mapping of profile information to coverage data.
LLVM Value Representation.
Definition: Value.h:69
std::error_code readIntMax(uint64_t &Result, uint64_t MaxPlus1)
std::error_code readSize(uint64_t &Result)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
virtual std::error_code readNextRecord(CoverageMappingRecord &Record)=0
bool operator!=(const CoverageMappingIterator &RHS)
A Counter is an abstract value that describes how to compute the execution count for a region of code...