65d7acd7b47f0216756f848f0ceb9dc5ffe9ff0b
[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 &) = delete;
94   RawCoverageFilenamesReader &
95   operator=(const RawCoverageFilenamesReader &) = delete;
96
97 public:
98   RawCoverageFilenamesReader(StringRef Data, std::vector<StringRef> &Filenames)
99       : RawCoverageReader(Data), Filenames(Filenames) {}
100
101   std::error_code read();
102 };
103
104 /// \brief Reader for the raw coverage mapping data.
105 class RawCoverageMappingReader : public RawCoverageReader {
106   ArrayRef<StringRef> TranslationUnitFilenames;
107   std::vector<StringRef> &Filenames;
108   std::vector<CounterExpression> &Expressions;
109   std::vector<CounterMappingRegion> &MappingRegions;
110
111   RawCoverageMappingReader(const RawCoverageMappingReader &) = delete;
112   RawCoverageMappingReader &
113   operator=(const RawCoverageMappingReader &) = delete;
114
115 public:
116   RawCoverageMappingReader(StringRef MappingData,
117                            ArrayRef<StringRef> TranslationUnitFilenames,
118                            std::vector<StringRef> &Filenames,
119                            std::vector<CounterExpression> &Expressions,
120                            std::vector<CounterMappingRegion> &MappingRegions)
121       : RawCoverageReader(MappingData),
122         TranslationUnitFilenames(TranslationUnitFilenames),
123         Filenames(Filenames), Expressions(Expressions),
124         MappingRegions(MappingRegions) {}
125
126   std::error_code read();
127
128 private:
129   std::error_code decodeCounter(unsigned Value, Counter &C);
130   std::error_code readCounter(Counter &C);
131   std::error_code
132   readMappingRegionsSubArray(std::vector<CounterMappingRegion> &MappingRegions,
133                              unsigned InferredFileID, size_t NumFileIDs);
134 };
135
136 /// \brief Reader for the coverage mapping data that is emitted by the
137 /// frontend and stored in an object file.
138 class ObjectFileCoverageMappingReader {
139 public:
140   struct ProfileMappingRecord {
141     CoverageMappingVersion Version;
142     StringRef FunctionName;
143     uint64_t FunctionHash;
144     StringRef CoverageMapping;
145     size_t FilenamesBegin;
146     size_t FilenamesSize;
147
148     ProfileMappingRecord(CoverageMappingVersion Version, StringRef FunctionName,
149                          uint64_t FunctionHash, StringRef CoverageMapping,
150                          size_t FilenamesBegin, size_t FilenamesSize)
151         : Version(Version), FunctionName(FunctionName),
152           FunctionHash(FunctionHash), CoverageMapping(CoverageMapping),
153           FilenamesBegin(FilenamesBegin), FilenamesSize(FilenamesSize) {}
154   };
155
156 private:
157   std::error_code LastError;
158   object::OwningBinary<object::ObjectFile> Object;
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   ObjectFileCoverageMappingReader(const ObjectFileCoverageMappingReader &)
167       = delete;
168   ObjectFileCoverageMappingReader &
169   operator=(const ObjectFileCoverageMappingReader &) = delete;
170
171   /// \brief Set the current error_code and return same.
172   std::error_code error(std::error_code EC) {
173     LastError = EC;
174     return EC;
175   }
176
177   /// \brief Clear the current error code and return a successful one.
178   std::error_code success() { return error(instrprof_error::success); }
179
180 public:
181   ObjectFileCoverageMappingReader(StringRef FileName);
182   ObjectFileCoverageMappingReader(
183       std::unique_ptr<MemoryBuffer> &ObjectBuffer,
184       sys::fs::file_magic Type = sys::fs::file_magic::unknown);
185
186   std::error_code readHeader();
187   std::error_code readNextRecord(CoverageMappingRecord &Record);
188
189   /// Iterator over profile data.
190   CoverageMappingIterator begin() { return CoverageMappingIterator(this); }
191   CoverageMappingIterator end() { return CoverageMappingIterator(); }
192
193   /// \brief Return true if the reader has finished reading the profile data.
194   bool isEOF() { return LastError == instrprof_error::eof; }
195   /// \brief Return true if the reader encountered an error reading profiling
196   /// data.
197   bool hasError() { return LastError && !isEOF(); }
198   /// \brief Get the current error code.
199   std::error_code getError() { return LastError; }
200 };
201
202 } // end namespace coverage
203 } // end namespace llvm
204
205 #endif