Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:23

0001 //===- CoverageMappingReader.h - Code coverage mapping reader ---*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file contains support for reading coverage mapping data for
0010 // instrumentation based coverage.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPINGREADER_H
0015 #define LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPINGREADER_H
0016 
0017 #include "llvm/ADT/ArrayRef.h"
0018 #include "llvm/ADT/StringRef.h"
0019 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
0020 #include "llvm/ProfileData/InstrProf.h"
0021 #include "llvm/Support/Error.h"
0022 #include "llvm/Support/MemoryBuffer.h"
0023 #include <cstddef>
0024 #include <cstdint>
0025 #include <iterator>
0026 #include <memory>
0027 #include <vector>
0028 
0029 namespace llvm {
0030 namespace coverage {
0031 
0032 class CoverageMappingReader;
0033 
0034 /// Coverage mapping information for a single function.
0035 struct CoverageMappingRecord {
0036   StringRef FunctionName;
0037   uint64_t FunctionHash;
0038   ArrayRef<StringRef> Filenames;
0039   ArrayRef<CounterExpression> Expressions;
0040   ArrayRef<CounterMappingRegion> MappingRegions;
0041 };
0042 
0043 /// A file format agnostic iterator over coverage mapping data.
0044 class CoverageMappingIterator {
0045   CoverageMappingReader *Reader;
0046   CoverageMappingRecord Record;
0047   coveragemap_error ReadErr;
0048 
0049   void increment();
0050 
0051 public:
0052   using iterator_category = std::input_iterator_tag;
0053   using value_type = CoverageMappingRecord;
0054   using difference_type = std::ptrdiff_t;
0055   using pointer = value_type *;
0056   using reference = value_type &;
0057 
0058   CoverageMappingIterator()
0059       : Reader(nullptr), ReadErr(coveragemap_error::success) {}
0060 
0061   CoverageMappingIterator(CoverageMappingReader *Reader)
0062       : Reader(Reader), ReadErr(coveragemap_error::success) {
0063     increment();
0064   }
0065 
0066   ~CoverageMappingIterator() {
0067     if (ReadErr != coveragemap_error::success)
0068       llvm_unreachable("Unexpected error in coverage mapping iterator");
0069   }
0070 
0071   CoverageMappingIterator &operator++() {
0072     increment();
0073     return *this;
0074   }
0075   bool operator==(const CoverageMappingIterator &RHS) const {
0076     return Reader == RHS.Reader;
0077   }
0078   bool operator!=(const CoverageMappingIterator &RHS) const {
0079     return Reader != RHS.Reader;
0080   }
0081   Expected<CoverageMappingRecord &> operator*() {
0082     if (ReadErr != coveragemap_error::success) {
0083       auto E = make_error<CoverageMapError>(ReadErr);
0084       ReadErr = coveragemap_error::success;
0085       return std::move(E);
0086     }
0087     return Record;
0088   }
0089   Expected<CoverageMappingRecord *> operator->() {
0090     if (ReadErr != coveragemap_error::success) {
0091       auto E = make_error<CoverageMapError>(ReadErr);
0092       ReadErr = coveragemap_error::success;
0093       return std::move(E);
0094     }
0095     return &Record;
0096   }
0097 };
0098 
0099 class CoverageMappingReader {
0100 public:
0101   virtual ~CoverageMappingReader() = default;
0102 
0103   virtual Error readNextRecord(CoverageMappingRecord &Record) = 0;
0104   CoverageMappingIterator begin() { return CoverageMappingIterator(this); }
0105   CoverageMappingIterator end() { return CoverageMappingIterator(); }
0106 };
0107 
0108 /// Base class for the raw coverage mapping and filenames data readers.
0109 class RawCoverageReader {
0110 protected:
0111   StringRef Data;
0112 
0113   RawCoverageReader(StringRef Data) : Data(Data) {}
0114 
0115   Error readULEB128(uint64_t &Result);
0116   Error readIntMax(uint64_t &Result, uint64_t MaxPlus1);
0117   Error readSize(uint64_t &Result);
0118   Error readString(StringRef &Result);
0119 };
0120 
0121 /// Checks if the given coverage mapping data is exported for
0122 /// an unused function.
0123 class RawCoverageMappingDummyChecker : public RawCoverageReader {
0124 public:
0125   RawCoverageMappingDummyChecker(StringRef MappingData)
0126       : RawCoverageReader(MappingData) {}
0127 
0128   Expected<bool> isDummy();
0129 };
0130 
0131 /// Reader for the raw coverage mapping data.
0132 class RawCoverageMappingReader : public RawCoverageReader {
0133   ArrayRef<std::string> &TranslationUnitFilenames;
0134   std::vector<StringRef> &Filenames;
0135   std::vector<CounterExpression> &Expressions;
0136   std::vector<CounterMappingRegion> &MappingRegions;
0137 
0138 public:
0139   RawCoverageMappingReader(StringRef MappingData,
0140                            ArrayRef<std::string> &TranslationUnitFilenames,
0141                            std::vector<StringRef> &Filenames,
0142                            std::vector<CounterExpression> &Expressions,
0143                            std::vector<CounterMappingRegion> &MappingRegions)
0144       : RawCoverageReader(MappingData),
0145         TranslationUnitFilenames(TranslationUnitFilenames),
0146         Filenames(Filenames), Expressions(Expressions),
0147         MappingRegions(MappingRegions) {}
0148   RawCoverageMappingReader(const RawCoverageMappingReader &) = delete;
0149   RawCoverageMappingReader &
0150   operator=(const RawCoverageMappingReader &) = delete;
0151 
0152   Error read();
0153 
0154 private:
0155   Error decodeCounter(unsigned Value, Counter &C);
0156   Error readCounter(Counter &C);
0157   Error
0158   readMappingRegionsSubArray(std::vector<CounterMappingRegion> &MappingRegions,
0159                              unsigned InferredFileID, size_t NumFileIDs);
0160 };
0161 
0162 /// Reader for the coverage mapping data that is emitted by the
0163 /// frontend and stored in an object file.
0164 class BinaryCoverageReader : public CoverageMappingReader {
0165 public:
0166   struct ProfileMappingRecord {
0167     CovMapVersion Version;
0168     StringRef FunctionName;
0169     uint64_t FunctionHash;
0170     StringRef CoverageMapping;
0171     size_t FilenamesBegin;
0172     size_t FilenamesSize;
0173 
0174     ProfileMappingRecord(CovMapVersion Version, StringRef FunctionName,
0175                          uint64_t FunctionHash, StringRef CoverageMapping,
0176                          size_t FilenamesBegin, size_t FilenamesSize)
0177         : Version(Version), FunctionName(FunctionName),
0178           FunctionHash(FunctionHash), CoverageMapping(CoverageMapping),
0179           FilenamesBegin(FilenamesBegin), FilenamesSize(FilenamesSize) {}
0180   };
0181 
0182   using FuncRecordsStorage = std::unique_ptr<MemoryBuffer>;
0183   using CoverageMapCopyStorage = std::unique_ptr<MemoryBuffer>;
0184 
0185 private:
0186   std::vector<std::string> Filenames;
0187   std::vector<ProfileMappingRecord> MappingRecords;
0188   std::unique_ptr<InstrProfSymtab> ProfileNames;
0189   size_t CurrentRecord = 0;
0190   std::vector<StringRef> FunctionsFilenames;
0191   std::vector<CounterExpression> Expressions;
0192   std::vector<CounterMappingRegion> MappingRegions;
0193 
0194   // Used to tie the lifetimes of coverage function records to the lifetime of
0195   // this BinaryCoverageReader instance. Needed to support the format change in
0196   // D69471, which can split up function records into multiple sections on ELF.
0197   FuncRecordsStorage FuncRecords;
0198 
0199   // Used to tie the lifetimes of an optional copy of the coverage mapping data
0200   // to the lifetime of this BinaryCoverageReader instance. Needed to support
0201   // Wasm object format, which might require realignment of section contents.
0202   CoverageMapCopyStorage CoverageMapCopy;
0203 
0204   BinaryCoverageReader(std::unique_ptr<InstrProfSymtab> Symtab,
0205                        FuncRecordsStorage &&FuncRecords,
0206                        CoverageMapCopyStorage &&CoverageMapCopy)
0207       : ProfileNames(std::move(Symtab)), FuncRecords(std::move(FuncRecords)),
0208         CoverageMapCopy(std::move(CoverageMapCopy)) {}
0209 
0210 public:
0211   BinaryCoverageReader(const BinaryCoverageReader &) = delete;
0212   BinaryCoverageReader &operator=(const BinaryCoverageReader &) = delete;
0213 
0214   static Expected<std::vector<std::unique_ptr<BinaryCoverageReader>>>
0215   create(MemoryBufferRef ObjectBuffer, StringRef Arch,
0216          SmallVectorImpl<std::unique_ptr<MemoryBuffer>> &ObjectFileBuffers,
0217          StringRef CompilationDir = "",
0218          SmallVectorImpl<object::BuildIDRef> *BinaryIDs = nullptr);
0219 
0220   static Expected<std::unique_ptr<BinaryCoverageReader>>
0221   createCoverageReaderFromBuffer(
0222       StringRef Coverage, FuncRecordsStorage &&FuncRecords,
0223       CoverageMapCopyStorage &&CoverageMap,
0224       std::unique_ptr<InstrProfSymtab> ProfileNamesPtr, uint8_t BytesInAddress,
0225       llvm::endianness Endian, StringRef CompilationDir = "");
0226 
0227   Error readNextRecord(CoverageMappingRecord &Record) override;
0228 };
0229 
0230 /// Reader for the raw coverage filenames.
0231 class RawCoverageFilenamesReader : public RawCoverageReader {
0232   std::vector<std::string> &Filenames;
0233   StringRef CompilationDir;
0234 
0235   // Read an uncompressed sequence of filenames.
0236   Error readUncompressed(CovMapVersion Version, uint64_t NumFilenames);
0237 
0238 public:
0239   RawCoverageFilenamesReader(StringRef Data,
0240                              std::vector<std::string> &Filenames,
0241                              StringRef CompilationDir = "")
0242       : RawCoverageReader(Data), Filenames(Filenames),
0243         CompilationDir(CompilationDir) {}
0244   RawCoverageFilenamesReader(const RawCoverageFilenamesReader &) = delete;
0245   RawCoverageFilenamesReader &
0246   operator=(const RawCoverageFilenamesReader &) = delete;
0247 
0248   Error read(CovMapVersion Version);
0249 };
0250 
0251 } // end namespace coverage
0252 } // end namespace llvm
0253 
0254 #endif // LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPINGREADER_H