LLVM API Documentation
00001 //=-- CoverageMappingReader.h - Code coverage mapping reader ------*- C++ -*-=// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains support for reading coverage mapping data for 00011 // instrumentation based coverage. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_PROFILEDATA_COVERAGEMAPPINGREADER_H 00016 #define LLVM_PROFILEDATA_COVERAGEMAPPINGREADER_H 00017 00018 #include "llvm/ProfileData/InstrProf.h" 00019 #include "llvm/ProfileData/CoverageMapping.h" 00020 #include "llvm/Object/ObjectFile.h" 00021 #include "llvm/ADT/ArrayRef.h" 00022 #include "llvm/ADT/StringRef.h" 00023 #include "llvm/Support/MemoryBuffer.h" 00024 #include "llvm/Support/FileSystem.h" 00025 00026 #include <iterator> 00027 00028 namespace llvm { 00029 namespace coverage { 00030 00031 class ObjectFileCoverageMappingReader; 00032 00033 /// \brief Coverage mapping information for a single function. 00034 struct CoverageMappingRecord { 00035 StringRef FunctionName; 00036 uint64_t FunctionHash; 00037 ArrayRef<StringRef> Filenames; 00038 ArrayRef<CounterExpression> Expressions; 00039 ArrayRef<CounterMappingRegion> MappingRegions; 00040 }; 00041 00042 /// \brief A file format agnostic iterator over coverage mapping data. 00043 class CoverageMappingIterator 00044 : public std::iterator<std::input_iterator_tag, CoverageMappingRecord> { 00045 ObjectFileCoverageMappingReader *Reader; 00046 CoverageMappingRecord Record; 00047 00048 void increment(); 00049 00050 public: 00051 CoverageMappingIterator() : Reader(nullptr) {} 00052 CoverageMappingIterator(ObjectFileCoverageMappingReader *Reader) 00053 : Reader(Reader) { 00054 increment(); 00055 } 00056 00057 CoverageMappingIterator &operator++() { 00058 increment(); 00059 return *this; 00060 } 00061 bool operator==(const CoverageMappingIterator &RHS) { 00062 return Reader == RHS.Reader; 00063 } 00064 bool operator!=(const CoverageMappingIterator &RHS) { 00065 return Reader != RHS.Reader; 00066 } 00067 CoverageMappingRecord &operator*() { return Record; } 00068 CoverageMappingRecord *operator->() { return &Record; } 00069 }; 00070 00071 /// \brief Base class for the raw coverage mapping and filenames data readers. 00072 class RawCoverageReader { 00073 protected: 00074 StringRef Data; 00075 00076 /// \brief Return the error code. 00077 std::error_code error(std::error_code EC) { return EC; } 00078 00079 /// \brief Clear the current error code and return a successful one. 00080 std::error_code success() { return error(instrprof_error::success); } 00081 00082 RawCoverageReader(StringRef Data) : Data(Data) {} 00083 00084 std::error_code readULEB128(uint64_t &Result); 00085 std::error_code readIntMax(uint64_t &Result, uint64_t MaxPlus1); 00086 std::error_code readSize(uint64_t &Result); 00087 std::error_code readString(StringRef &Result); 00088 }; 00089 00090 /// \brief Reader for the raw coverage filenames. 00091 class RawCoverageFilenamesReader : public RawCoverageReader { 00092 std::vector<StringRef> &Filenames; 00093 00094 RawCoverageFilenamesReader(const RawCoverageFilenamesReader &) 00095 LLVM_DELETED_FUNCTION; 00096 RawCoverageFilenamesReader & 00097 operator=(const RawCoverageFilenamesReader &) LLVM_DELETED_FUNCTION; 00098 00099 public: 00100 RawCoverageFilenamesReader(StringRef Data, std::vector<StringRef> &Filenames) 00101 : RawCoverageReader(Data), Filenames(Filenames) {} 00102 00103 std::error_code read(); 00104 }; 00105 00106 /// \brief Reader for the raw coverage mapping data. 00107 class RawCoverageMappingReader : public RawCoverageReader { 00108 StringRef FunctionName; 00109 ArrayRef<StringRef> TranslationUnitFilenames; 00110 std::vector<StringRef> &Filenames; 00111 std::vector<CounterExpression> &Expressions; 00112 std::vector<CounterMappingRegion> &MappingRegions; 00113 00114 RawCoverageMappingReader(const RawCoverageMappingReader &) 00115 LLVM_DELETED_FUNCTION; 00116 RawCoverageMappingReader & 00117 operator=(const RawCoverageMappingReader &) LLVM_DELETED_FUNCTION; 00118 00119 public: 00120 RawCoverageMappingReader(StringRef FunctionName, StringRef MappingData, 00121 ArrayRef<StringRef> TranslationUnitFilenames, 00122 std::vector<StringRef> &Filenames, 00123 std::vector<CounterExpression> &Expressions, 00124 std::vector<CounterMappingRegion> &MappingRegions) 00125 : RawCoverageReader(MappingData), FunctionName(FunctionName), 00126 TranslationUnitFilenames(TranslationUnitFilenames), 00127 Filenames(Filenames), Expressions(Expressions), 00128 MappingRegions(MappingRegions) {} 00129 00130 std::error_code read(CoverageMappingRecord &Record); 00131 00132 private: 00133 std::error_code decodeCounter(unsigned Value, Counter &C); 00134 std::error_code readCounter(Counter &C); 00135 std::error_code 00136 readMappingRegionsSubArray(std::vector<CounterMappingRegion> &MappingRegions, 00137 unsigned InferredFileID, size_t NumFileIDs); 00138 }; 00139 00140 /// \brief Reader for the coverage mapping data that is emitted by the 00141 /// frontend and stored in an object file. 00142 class ObjectFileCoverageMappingReader { 00143 public: 00144 struct ProfileMappingRecord { 00145 CoverageMappingVersion Version; 00146 StringRef FunctionName; 00147 uint64_t FunctionHash; 00148 StringRef CoverageMapping; 00149 size_t FilenamesBegin; 00150 size_t FilenamesSize; 00151 00152 ProfileMappingRecord(CoverageMappingVersion Version, StringRef FunctionName, 00153 uint64_t FunctionHash, StringRef CoverageMapping, 00154 size_t FilenamesBegin, size_t FilenamesSize) 00155 : Version(Version), FunctionName(FunctionName), 00156 FunctionHash(FunctionHash), CoverageMapping(CoverageMapping), 00157 FilenamesBegin(FilenamesBegin), FilenamesSize(FilenamesSize) {} 00158 }; 00159 00160 private: 00161 std::error_code LastError; 00162 object::OwningBinary<object::ObjectFile> Object; 00163 std::vector<StringRef> Filenames; 00164 std::vector<ProfileMappingRecord> MappingRecords; 00165 size_t CurrentRecord; 00166 std::vector<StringRef> FunctionsFilenames; 00167 std::vector<CounterExpression> Expressions; 00168 std::vector<CounterMappingRegion> MappingRegions; 00169 00170 ObjectFileCoverageMappingReader(const ObjectFileCoverageMappingReader &) 00171 LLVM_DELETED_FUNCTION; 00172 ObjectFileCoverageMappingReader & 00173 operator=(const ObjectFileCoverageMappingReader &) LLVM_DELETED_FUNCTION; 00174 00175 /// \brief Set the current error_code and return same. 00176 std::error_code error(std::error_code EC) { 00177 LastError = EC; 00178 return EC; 00179 } 00180 00181 /// \brief Clear the current error code and return a successful one. 00182 std::error_code success() { return error(instrprof_error::success); } 00183 00184 public: 00185 ObjectFileCoverageMappingReader(StringRef FileName); 00186 ObjectFileCoverageMappingReader( 00187 std::unique_ptr<MemoryBuffer> &ObjectBuffer, 00188 sys::fs::file_magic Type = sys::fs::file_magic::unknown); 00189 00190 std::error_code readHeader(); 00191 std::error_code readNextRecord(CoverageMappingRecord &Record); 00192 00193 /// Iterator over profile data. 00194 CoverageMappingIterator begin() { return CoverageMappingIterator(this); } 00195 CoverageMappingIterator end() { return CoverageMappingIterator(); } 00196 00197 /// \brief Return true if the reader has finished reading the profile data. 00198 bool isEOF() { return LastError == instrprof_error::eof; } 00199 /// \brief Return true if the reader encountered an error reading profiling 00200 /// data. 00201 bool hasError() { return LastError && !isEOF(); } 00202 /// \brief Get the current error code. 00203 std::error_code getError() { return LastError; } 00204 }; 00205 00206 } // end namespace coverage 00207 } // end namespace llvm 00208 00209 #endif