LLVM  6.0.0svn
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_COVERAGE_COVERAGEMAPPINGREADER_H
16 #define LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPINGREADER_H
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/StringRef.h"
22 #include "llvm/Support/Error.h"
24 #include <cstddef>
25 #include <cstdint>
26 #include <iterator>
27 #include <memory>
28 #include <vector>
29 
30 namespace llvm {
31 namespace coverage {
32 
33 class CoverageMappingReader;
34 
35 /// \brief Coverage mapping information for a single function.
38  uint64_t FunctionHash;
42 };
43 
44 /// \brief A file format agnostic iterator over coverage mapping data.
46  : public std::iterator<std::input_iterator_tag, CoverageMappingRecord> {
47  CoverageMappingReader *Reader;
49  coveragemap_error ReadErr;
50 
51  void increment();
52 
53 public:
55  : Reader(nullptr), Record(), ReadErr(coveragemap_error::success) {}
56 
58  : Reader(Reader), Record(), ReadErr(coveragemap_error::success) {
59  increment();
60  }
61 
63  if (ReadErr != coveragemap_error::success)
64  llvm_unreachable("Unexpected error in coverage mapping iterator");
65  }
66 
68  increment();
69  return *this;
70  }
72  return Reader == RHS.Reader;
73  }
75  return Reader != RHS.Reader;
76  }
78  if (ReadErr != coveragemap_error::success) {
79  auto E = make_error<CoverageMapError>(ReadErr);
81  return std::move(E);
82  }
83  return Record;
84  }
86  if (ReadErr != coveragemap_error::success) {
87  auto E = make_error<CoverageMapError>(ReadErr);
89  return std::move(E);
90  }
91  return &Record;
92  }
93 };
94 
96 public:
97  virtual ~CoverageMappingReader() = default;
98 
99  virtual Error readNextRecord(CoverageMappingRecord &Record) = 0;
102 };
103 
104 /// \brief Base class for the raw coverage mapping and filenames data readers.
106 protected:
108 
109  RawCoverageReader(StringRef Data) : Data(Data) {}
110 
111  Error readULEB128(uint64_t &Result);
112  Error readIntMax(uint64_t &Result, uint64_t MaxPlus1);
113  Error readSize(uint64_t &Result);
114  Error readString(StringRef &Result);
115 };
116 
117 /// \brief Reader for the raw coverage filenames.
119  std::vector<StringRef> &Filenames;
120 
121 public:
122  RawCoverageFilenamesReader(StringRef Data, std::vector<StringRef> &Filenames)
123  : RawCoverageReader(Data), Filenames(Filenames) {}
126  operator=(const RawCoverageFilenamesReader &) = delete;
127 
128  Error read();
129 };
130 
131 /// \brief Checks if the given coverage mapping data is exported for
132 /// an unused function.
134 public:
136  : RawCoverageReader(MappingData) {}
137 
138  Expected<bool> isDummy();
139 };
140 
141 /// \brief Reader for the raw coverage mapping data.
143  ArrayRef<StringRef> TranslationUnitFilenames;
144  std::vector<StringRef> &Filenames;
145  std::vector<CounterExpression> &Expressions;
146  std::vector<CounterMappingRegion> &MappingRegions;
147 
148 public:
150  ArrayRef<StringRef> TranslationUnitFilenames,
151  std::vector<StringRef> &Filenames,
152  std::vector<CounterExpression> &Expressions,
153  std::vector<CounterMappingRegion> &MappingRegions)
154  : RawCoverageReader(MappingData),
155  TranslationUnitFilenames(TranslationUnitFilenames),
156  Filenames(Filenames), Expressions(Expressions),
157  MappingRegions(MappingRegions) {}
160  operator=(const RawCoverageMappingReader &) = delete;
161 
162  Error read();
163 
164 private:
165  Error decodeCounter(unsigned Value, Counter &C);
166  Error readCounter(Counter &C);
167  Error
168  readMappingRegionsSubArray(std::vector<CounterMappingRegion> &MappingRegions,
169  unsigned InferredFileID, size_t NumFileIDs);
170 };
171 
172 /// \brief Reader for the coverage mapping data that is emitted by the
173 /// frontend and stored in an object file.
175 public:
179  uint64_t FunctionHash;
183 
185  uint64_t FunctionHash, StringRef CoverageMapping,
186  size_t FilenamesBegin, size_t FilenamesSize)
187  : Version(Version), FunctionName(FunctionName),
188  FunctionHash(FunctionHash), CoverageMapping(CoverageMapping),
189  FilenamesBegin(FilenamesBegin), FilenamesSize(FilenamesSize) {}
190  };
191 
192 private:
193  std::vector<StringRef> Filenames;
194  std::vector<ProfileMappingRecord> MappingRecords;
195  InstrProfSymtab ProfileNames;
196  size_t CurrentRecord = 0;
197  std::vector<StringRef> FunctionsFilenames;
198  std::vector<CounterExpression> Expressions;
199  std::vector<CounterMappingRegion> MappingRegions;
200 
201  BinaryCoverageReader() = default;
202 
203 public:
204  BinaryCoverageReader(const BinaryCoverageReader &) = delete;
205  BinaryCoverageReader &operator=(const BinaryCoverageReader &) = delete;
206 
208  create(std::unique_ptr<MemoryBuffer> &ObjectBuffer,
209  StringRef Arch);
210 
211  Error readNextRecord(CoverageMappingRecord &Record) override;
212 };
213 
214 } // end namespace coverage
215 } // end namespace llvm
216 
217 #endif // LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPINGREADER_H
uint64_t CallInst * C
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Definition: InstrProf.h:411
Reader for the raw coverage mapping data.
RawCoverageFilenamesReader(StringRef Data, std::vector< StringRef > &Filenames)
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Base class for the raw coverage mapping and filenames data readers.
CoverageMappingIterator(CoverageMappingReader *Reader)
Checks if the given coverage mapping data is exported for an unused function.
ArrayRef< CounterMappingRegion > MappingRegions
static StringRef readString(const uint8_t *&Ptr)
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
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
Coverage mapping information for a single function.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Reader for the raw coverage filenames.
bool operator==(const CoverageMappingIterator &RHS)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A file format agnostic iterator over coverage mapping data.
Expected< CoverageMappingRecord & > operator*()
static uint64_t readULEB128(const uint8_t *&Ptr)
Expected< CoverageMappingRecord * > operator->()
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition: Endian.h:66
ProfileMappingRecord(CovMapVersion Version, StringRef FunctionName, uint64_t FunctionHash, StringRef CoverageMapping, size_t FilenamesBegin, size_t FilenamesSize)
LLVM Value Representation.
Definition: Value.h:73
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
bool operator!=(const CoverageMappingIterator &RHS)
A Counter is an abstract value that describes how to compute the execution count for a region of code...