LLVM  10.0.0svn
CoverageMappingReader.h
Go to the documentation of this file.
1 //===- CoverageMappingReader.h - Code coverage mapping reader ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for reading coverage mapping data for
10 // instrumentation based coverage.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPINGREADER_H
15 #define LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPINGREADER_H
16 
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/Error.h"
23 #include <cstddef>
24 #include <cstdint>
25 #include <iterator>
26 #include <memory>
27 #include <vector>
28 
29 namespace llvm {
30 namespace coverage {
31 
32 class CoverageMappingReader;
33 
34 /// Coverage mapping information for a single function.
37  uint64_t FunctionHash;
41 };
42 
43 /// A file format agnostic iterator over coverage mapping data.
45  : public std::iterator<std::input_iterator_tag, CoverageMappingRecord> {
46  CoverageMappingReader *Reader;
48  coveragemap_error ReadErr;
49 
50  void increment();
51 
52 public:
54  : Reader(nullptr), Record(), ReadErr(coveragemap_error::success) {}
55 
57  : Reader(Reader), Record(), ReadErr(coveragemap_error::success) {
58  increment();
59  }
60 
62  if (ReadErr != coveragemap_error::success)
63  llvm_unreachable("Unexpected error in coverage mapping iterator");
64  }
65 
67  increment();
68  return *this;
69  }
71  return Reader == RHS.Reader;
72  }
74  return Reader != RHS.Reader;
75  }
77  if (ReadErr != coveragemap_error::success) {
78  auto E = make_error<CoverageMapError>(ReadErr);
80  return std::move(E);
81  }
82  return Record;
83  }
85  if (ReadErr != coveragemap_error::success) {
86  auto E = make_error<CoverageMapError>(ReadErr);
88  return std::move(E);
89  }
90  return &Record;
91  }
92 };
93 
95 public:
96  virtual ~CoverageMappingReader() = default;
97 
98  virtual Error readNextRecord(CoverageMappingRecord &Record) = 0;
101 };
102 
103 /// Base class for the raw coverage mapping and filenames data readers.
105 protected:
107 
108  RawCoverageReader(StringRef Data) : Data(Data) {}
109 
110  Error readULEB128(uint64_t &Result);
111  Error readIntMax(uint64_t &Result, uint64_t MaxPlus1);
112  Error readSize(uint64_t &Result);
113  Error readString(StringRef &Result);
114 };
115 
116 /// Reader for the raw coverage filenames.
118  std::vector<StringRef> &Filenames;
119 
120 public:
121  RawCoverageFilenamesReader(StringRef Data, std::vector<StringRef> &Filenames)
122  : RawCoverageReader(Data), Filenames(Filenames) {}
125  operator=(const RawCoverageFilenamesReader &) = delete;
126 
127  Error read();
128 };
129 
130 /// Checks if the given coverage mapping data is exported for
131 /// an unused function.
133 public:
135  : RawCoverageReader(MappingData) {}
136 
137  Expected<bool> isDummy();
138 };
139 
140 /// Reader for the raw coverage mapping data.
142  ArrayRef<StringRef> TranslationUnitFilenames;
143  std::vector<StringRef> &Filenames;
144  std::vector<CounterExpression> &Expressions;
145  std::vector<CounterMappingRegion> &MappingRegions;
146 
147 public:
149  ArrayRef<StringRef> TranslationUnitFilenames,
150  std::vector<StringRef> &Filenames,
151  std::vector<CounterExpression> &Expressions,
152  std::vector<CounterMappingRegion> &MappingRegions)
153  : RawCoverageReader(MappingData),
154  TranslationUnitFilenames(TranslationUnitFilenames),
155  Filenames(Filenames), Expressions(Expressions),
156  MappingRegions(MappingRegions) {}
159  operator=(const RawCoverageMappingReader &) = delete;
160 
161  Error read();
162 
163 private:
164  Error decodeCounter(unsigned Value, Counter &C);
165  Error readCounter(Counter &C);
166  Error
167  readMappingRegionsSubArray(std::vector<CounterMappingRegion> &MappingRegions,
168  unsigned InferredFileID, size_t NumFileIDs);
169 };
170 
171 /// Reader for the coverage mapping data that is emitted by the
172 /// frontend and stored in an object file.
174 public:
178  uint64_t FunctionHash;
182 
184  uint64_t FunctionHash, StringRef CoverageMapping,
185  size_t FilenamesBegin, size_t FilenamesSize)
186  : Version(Version), FunctionName(FunctionName),
187  FunctionHash(FunctionHash), CoverageMapping(CoverageMapping),
188  FilenamesBegin(FilenamesBegin), FilenamesSize(FilenamesSize) {}
189  };
190 
191 private:
192  std::vector<StringRef> Filenames;
193  std::vector<ProfileMappingRecord> MappingRecords;
194  InstrProfSymtab ProfileNames;
195  size_t CurrentRecord = 0;
196  std::vector<StringRef> FunctionsFilenames;
197  std::vector<CounterExpression> Expressions;
198  std::vector<CounterMappingRegion> MappingRegions;
199 
200  BinaryCoverageReader() = default;
201 
202 public:
203  BinaryCoverageReader(const BinaryCoverageReader &) = delete;
204  BinaryCoverageReader &operator=(const BinaryCoverageReader &) = delete;
205 
207  create(MemoryBufferRef ObjectBuffer, StringRef Arch,
208  SmallVectorImpl<std::unique_ptr<MemoryBuffer>> &ObjectFileBuffers);
209 
211  createCoverageReaderFromBuffer(StringRef Coverage,
212  InstrProfSymtab &&ProfileNames,
213  uint8_t BytesInAddress,
214  support::endianness Endian);
215 
216  Error readNextRecord(CoverageMappingRecord &Record) override;
217 };
218 
219 } // end namespace coverage
220 } // end namespace llvm
221 
222 #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:406
Reader for the raw coverage mapping data.
RawCoverageFilenamesReader(StringRef Data, std::vector< StringRef > &Filenames)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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
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
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
static uint64_t readULEB128(WasmObjectFile::ReadContext &Ctx)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
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*()
Expected< CoverageMappingRecord * > operator->()
static StringRef readString(WasmObjectFile::ReadContext &Ctx)
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition: Endian.h:65
ProfileMappingRecord(CovMapVersion Version, StringRef FunctionName, uint64_t FunctionHash, StringRef CoverageMapping, size_t FilenamesBegin, size_t FilenamesSize)
LLVM Value Representation.
Definition: Value.h:74
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
bool operator!=(const CoverageMappingIterator &RHS)
A Counter is an abstract value that describes how to compute the execution count for a region of code...