b4a57cc3864630b60e110e19164bb686445f9c46
[oota-llvm.git] / include / llvm / ProfileData / CoverageMappingReader.h
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/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"
25 #include <iterator>
26
27 namespace llvm {
28 namespace coverage {
29
30 class ObjectFileCoverageMappingReader;
31
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;
39 };
40
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;
46
47   void increment();
48
49 public:
50   CoverageMappingIterator() : Reader(nullptr) {}
51   CoverageMappingIterator(ObjectFileCoverageMappingReader *Reader)
52       : Reader(Reader) {
53     increment();
54   }
55
56   CoverageMappingIterator &operator++() {
57     increment();
58     return *this;
59   }
60   bool operator==(const CoverageMappingIterator &RHS) {
61     return Reader == RHS.Reader;
62   }
63   bool operator!=(const CoverageMappingIterator &RHS) {
64     return Reader != RHS.Reader;
65   }
66   CoverageMappingRecord &operator*() { return Record; }
67   CoverageMappingRecord *operator->() { return &Record; }
68 };
69
70 /// \brief Base class for the raw coverage mapping and filenames data readers.
71 class RawCoverageReader {
72 protected:
73   StringRef Data;
74
75   /// \brief Return the error code.
76   std::error_code error(std::error_code EC) { return EC; }
77
78   /// \brief Clear the current error code and return a successful one.
79   std::error_code success() { return error(instrprof_error::success); }
80
81   RawCoverageReader(StringRef Data) : Data(Data) {}
82
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);
87 };
88
89 /// \brief Reader for the raw coverage filenames.
90 class RawCoverageFilenamesReader : public RawCoverageReader {
91   std::vector<StringRef> &Filenames;
92
93   RawCoverageFilenamesReader(const RawCoverageFilenamesReader &)
94       LLVM_DELETED_FUNCTION;
95   RawCoverageFilenamesReader &
96   operator=(const RawCoverageFilenamesReader &) LLVM_DELETED_FUNCTION;
97
98 public:
99   RawCoverageFilenamesReader(StringRef Data, std::vector<StringRef> &Filenames)
100       : RawCoverageReader(Data), Filenames(Filenames) {}
101
102   std::error_code read();
103 };
104
105 /// \brief Reader for the raw coverage mapping data.
106 class RawCoverageMappingReader : public RawCoverageReader {
107   ArrayRef<StringRef> TranslationUnitFilenames;
108   std::vector<StringRef> &Filenames;
109   std::vector<CounterExpression> &Expressions;
110   std::vector<CounterMappingRegion> &MappingRegions;
111
112   RawCoverageMappingReader(const RawCoverageMappingReader &)
113       LLVM_DELETED_FUNCTION;
114   RawCoverageMappingReader &
115   operator=(const RawCoverageMappingReader &) LLVM_DELETED_FUNCTION;
116
117 public:
118   RawCoverageMappingReader(StringRef MappingData,
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.
140 class ObjectFileCoverageMappingReader {
141 public:
142   struct ProfileMappingRecord {
143     CoverageMappingVersion Version;
144     StringRef FunctionName;
145     uint64_t FunctionHash;
146     StringRef CoverageMapping;
147     size_t FilenamesBegin;
148     size_t FilenamesSize;
149
150     ProfileMappingRecord(CoverageMappingVersion Version, StringRef FunctionName,
151                          uint64_t FunctionHash, StringRef CoverageMapping,
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::error_code LastError;
160   object::OwningBinary<object::ObjectFile> Object;
161   std::vector<StringRef> Filenames;
162   std::vector<ProfileMappingRecord> MappingRecords;
163   size_t CurrentRecord;
164   std::vector<StringRef> FunctionsFilenames;
165   std::vector<CounterExpression> Expressions;
166   std::vector<CounterMappingRegion> MappingRegions;
167
168   ObjectFileCoverageMappingReader(const ObjectFileCoverageMappingReader &)
169       LLVM_DELETED_FUNCTION;
170   ObjectFileCoverageMappingReader &
171   operator=(const ObjectFileCoverageMappingReader &) LLVM_DELETED_FUNCTION;
172
173   /// \brief Set the current error_code and return same.
174   std::error_code error(std::error_code EC) {
175     LastError = EC;
176     return EC;
177   }
178
179   /// \brief Clear the current error code and return a successful one.
180   std::error_code success() { return error(instrprof_error::success); }
181
182 public:
183   ObjectFileCoverageMappingReader(StringRef FileName);
184   ObjectFileCoverageMappingReader(
185       std::unique_ptr<MemoryBuffer> &ObjectBuffer,
186       sys::fs::file_magic Type = sys::fs::file_magic::unknown);
187
188   std::error_code readHeader();
189   std::error_code readNextRecord(CoverageMappingRecord &Record);
190
191   /// Iterator over profile data.
192   CoverageMappingIterator begin() { return CoverageMappingIterator(this); }
193   CoverageMappingIterator end() { return CoverageMappingIterator(); }
194
195   /// \brief Return true if the reader has finished reading the profile data.
196   bool isEOF() { return LastError == instrprof_error::eof; }
197   /// \brief Return true if the reader encountered an error reading profiling
198   /// data.
199   bool hasError() { return LastError && !isEOF(); }
200   /// \brief Get the current error code.
201   std::error_code getError() { return LastError; }
202 };
203
204 } // end namespace coverage
205 } // end namespace llvm
206
207 #endif