73b02488f2cbaca6a53e12a81c22ee10e1cabd32
[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/ProfileData/InstrProf.h"
19 #include "llvm/ProfileData/CoverageMapping.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/FileSystem.h"
25
26 #include <iterator>
27
28 namespace llvm {
29 namespace coverage {
30
31 class ObjectFileCoverageMappingReader;
32
33 /// \brief Coverage mapping information for a single function.
34 struct CoverageMappingRecord {
35   StringRef FunctionName;
36   uint64_t FunctionHash;
37   ArrayRef<StringRef> Filenames;
38   ArrayRef<CounterExpression> Expressions;
39   ArrayRef<CounterMappingRegion> MappingRegions;
40 };
41
42 /// \brief A file format agnostic iterator over coverage mapping data.
43 class CoverageMappingIterator
44     : public std::iterator<std::input_iterator_tag, CoverageMappingRecord> {
45   ObjectFileCoverageMappingReader *Reader;
46   CoverageMappingRecord Record;
47
48   void increment();
49
50 public:
51   CoverageMappingIterator() : Reader(nullptr) {}
52   CoverageMappingIterator(ObjectFileCoverageMappingReader *Reader)
53       : Reader(Reader) {
54     increment();
55   }
56
57   CoverageMappingIterator &operator++() {
58     increment();
59     return *this;
60   }
61   bool operator==(const CoverageMappingIterator &RHS) {
62     return Reader == RHS.Reader;
63   }
64   bool operator!=(const CoverageMappingIterator &RHS) {
65     return Reader != RHS.Reader;
66   }
67   CoverageMappingRecord &operator*() { return Record; }
68   CoverageMappingRecord *operator->() { return &Record; }
69 };
70
71 /// \brief Base class for the raw coverage mapping and filenames data readers.
72 class RawCoverageReader {
73 protected:
74   StringRef Data;
75
76   /// \brief Return the error code.
77   std::error_code error(std::error_code EC) { return EC; }
78
79   /// \brief Clear the current error code and return a successful one.
80   std::error_code success() { return error(instrprof_error::success); }
81
82   RawCoverageReader(StringRef Data) : Data(Data) {}
83
84   std::error_code readULEB128(uint64_t &Result);
85   std::error_code readIntMax(uint64_t &Result, uint64_t MaxPlus1);
86   std::error_code readSize(uint64_t &Result);
87   std::error_code readString(StringRef &Result);
88 };
89
90 /// \brief Reader for the raw coverage filenames.
91 class RawCoverageFilenamesReader : public RawCoverageReader {
92   std::vector<StringRef> &Filenames;
93
94   RawCoverageFilenamesReader(const RawCoverageFilenamesReader &)
95       LLVM_DELETED_FUNCTION;
96   RawCoverageFilenamesReader &
97   operator=(const RawCoverageFilenamesReader &) LLVM_DELETED_FUNCTION;
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.
107 class RawCoverageMappingReader : public RawCoverageReader {
108   StringRef FunctionName;
109   ArrayRef<StringRef> TranslationUnitFilenames;
110   std::vector<StringRef> &Filenames;
111   std::vector<CounterExpression> &Expressions;
112   std::vector<CounterMappingRegion> &MappingRegions;
113
114   RawCoverageMappingReader(const RawCoverageMappingReader &)
115       LLVM_DELETED_FUNCTION;
116   RawCoverageMappingReader &
117   operator=(const RawCoverageMappingReader &) LLVM_DELETED_FUNCTION;
118
119 public:
120   RawCoverageMappingReader(StringRef FunctionName, StringRef MappingData,
121                            ArrayRef<StringRef> TranslationUnitFilenames,
122                            std::vector<StringRef> &Filenames,
123                            std::vector<CounterExpression> &Expressions,
124                            std::vector<CounterMappingRegion> &MappingRegions)
125       : RawCoverageReader(MappingData), FunctionName(FunctionName),
126         TranslationUnitFilenames(TranslationUnitFilenames),
127         Filenames(Filenames), Expressions(Expressions),
128         MappingRegions(MappingRegions) {}
129
130   std::error_code read(CoverageMappingRecord &Record);
131
132 private:
133   std::error_code decodeCounter(unsigned Value, Counter &C);
134   std::error_code readCounter(Counter &C);
135   std::error_code
136   readMappingRegionsSubArray(std::vector<CounterMappingRegion> &MappingRegions,
137                              unsigned InferredFileID, size_t NumFileIDs);
138 };
139
140 /// \brief Reader for the coverage mapping data that is emitted by the
141 /// frontend and stored in an object file.
142 class ObjectFileCoverageMappingReader {
143 public:
144   struct ProfileMappingRecord {
145     CoverageMappingVersion Version;
146     StringRef FunctionName;
147     uint64_t FunctionHash;
148     StringRef CoverageMapping;
149     size_t FilenamesBegin;
150     size_t FilenamesSize;
151
152     ProfileMappingRecord(CoverageMappingVersion Version, StringRef FunctionName,
153                          uint64_t FunctionHash, StringRef CoverageMapping,
154                          size_t FilenamesBegin, size_t FilenamesSize)
155         : Version(Version), FunctionName(FunctionName),
156           FunctionHash(FunctionHash), CoverageMapping(CoverageMapping),
157           FilenamesBegin(FilenamesBegin), FilenamesSize(FilenamesSize) {}
158   };
159
160 private:
161   std::error_code LastError;
162   object::OwningBinary<object::ObjectFile> Object;
163   std::vector<StringRef> Filenames;
164   std::vector<ProfileMappingRecord> MappingRecords;
165   size_t CurrentRecord;
166   std::vector<StringRef> FunctionsFilenames;
167   std::vector<CounterExpression> Expressions;
168   std::vector<CounterMappingRegion> MappingRegions;
169
170   ObjectFileCoverageMappingReader(const ObjectFileCoverageMappingReader &)
171       LLVM_DELETED_FUNCTION;
172   ObjectFileCoverageMappingReader &
173   operator=(const ObjectFileCoverageMappingReader &) LLVM_DELETED_FUNCTION;
174
175   /// \brief Set the current error_code and return same.
176   std::error_code error(std::error_code EC) {
177     LastError = EC;
178     return EC;
179   }
180
181   /// \brief Clear the current error code and return a successful one.
182   std::error_code success() { return error(instrprof_error::success); }
183
184 public:
185   ObjectFileCoverageMappingReader(StringRef FileName);
186   ObjectFileCoverageMappingReader(
187       std::unique_ptr<MemoryBuffer> &ObjectBuffer,
188       sys::fs::file_magic Type = sys::fs::file_magic::unknown);
189
190   std::error_code readHeader();
191   std::error_code readNextRecord(CoverageMappingRecord &Record);
192
193   /// Iterator over profile data.
194   CoverageMappingIterator begin() { return CoverageMappingIterator(this); }
195   CoverageMappingIterator end() { return CoverageMappingIterator(); }
196
197   /// \brief Return true if the reader has finished reading the profile data.
198   bool isEOF() { return LastError == instrprof_error::eof; }
199   /// \brief Return true if the reader encountered an error reading profiling
200   /// data.
201   bool hasError() { return LastError && !isEOF(); }
202   /// \brief Get the current error code.
203   std::error_code getError() { return LastError; }
204 };
205
206 } // end namespace coverage
207 } // end namespace llvm
208
209 #endif