2014-07-24 23:57:54 +00:00
|
|
|
//=-- CoverageMappingReader.h - Code coverage mapping reader ------*- C++ -*-=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains support for reading coverage mapping data for
|
|
|
|
// instrumentation based coverage.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-13 16:26:38 +00:00
|
|
|
#ifndef LLVM_PROFILEDATA_COVERAGEMAPPINGREADER_H
|
|
|
|
#define LLVM_PROFILEDATA_COVERAGEMAPPINGREADER_H
|
2014-07-24 23:57:54 +00:00
|
|
|
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2015-01-14 11:23:27 +00:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
|
|
|
#include "llvm/ProfileData/CoverageMapping.h"
|
|
|
|
#include "llvm/ProfileData/InstrProf.h"
|
2014-07-24 23:57:54 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2015-01-14 11:23:27 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2014-07-24 23:57:54 +00:00
|
|
|
#include <iterator>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace coverage {
|
|
|
|
|
2015-02-18 18:01:14 +00:00
|
|
|
class CoverageMappingReader;
|
2014-07-24 23:57:54 +00:00
|
|
|
|
|
|
|
/// \brief Coverage mapping information for a single function.
|
|
|
|
struct CoverageMappingRecord {
|
|
|
|
StringRef FunctionName;
|
2014-08-21 19:23:25 +00:00
|
|
|
uint64_t FunctionHash;
|
2014-07-24 23:57:54 +00:00
|
|
|
ArrayRef<StringRef> Filenames;
|
|
|
|
ArrayRef<CounterExpression> Expressions;
|
|
|
|
ArrayRef<CounterMappingRegion> MappingRegions;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief A file format agnostic iterator over coverage mapping data.
|
|
|
|
class CoverageMappingIterator
|
|
|
|
: public std::iterator<std::input_iterator_tag, CoverageMappingRecord> {
|
2015-02-18 18:01:14 +00:00
|
|
|
CoverageMappingReader *Reader;
|
2014-07-24 23:57:54 +00:00
|
|
|
CoverageMappingRecord Record;
|
|
|
|
|
|
|
|
void increment();
|
|
|
|
|
|
|
|
public:
|
|
|
|
CoverageMappingIterator() : Reader(nullptr) {}
|
2015-02-18 18:01:14 +00:00
|
|
|
CoverageMappingIterator(CoverageMappingReader *Reader) : Reader(Reader) {
|
2014-07-24 23:57:54 +00:00
|
|
|
increment();
|
|
|
|
}
|
|
|
|
|
|
|
|
CoverageMappingIterator &operator++() {
|
|
|
|
increment();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
bool operator==(const CoverageMappingIterator &RHS) {
|
|
|
|
return Reader == RHS.Reader;
|
|
|
|
}
|
|
|
|
bool operator!=(const CoverageMappingIterator &RHS) {
|
|
|
|
return Reader != RHS.Reader;
|
|
|
|
}
|
|
|
|
CoverageMappingRecord &operator*() { return Record; }
|
|
|
|
CoverageMappingRecord *operator->() { return &Record; }
|
|
|
|
};
|
|
|
|
|
2015-02-18 18:01:14 +00:00
|
|
|
class CoverageMappingReader {
|
|
|
|
public:
|
|
|
|
virtual std::error_code readNextRecord(CoverageMappingRecord &Record) = 0;
|
|
|
|
CoverageMappingIterator begin() { return CoverageMappingIterator(this); }
|
|
|
|
CoverageMappingIterator end() { return CoverageMappingIterator(); }
|
|
|
|
virtual ~CoverageMappingReader() {}
|
|
|
|
};
|
|
|
|
|
2014-07-24 23:57:54 +00:00
|
|
|
/// \brief Base class for the raw coverage mapping and filenames data readers.
|
|
|
|
class RawCoverageReader {
|
|
|
|
protected:
|
|
|
|
StringRef Data;
|
|
|
|
|
|
|
|
/// \brief Return the error code.
|
|
|
|
std::error_code error(std::error_code EC) { return EC; }
|
|
|
|
|
|
|
|
/// \brief Clear the current error code and return a successful one.
|
|
|
|
std::error_code success() { return error(instrprof_error::success); }
|
|
|
|
|
|
|
|
RawCoverageReader(StringRef Data) : Data(Data) {}
|
|
|
|
|
|
|
|
std::error_code readULEB128(uint64_t &Result);
|
|
|
|
std::error_code readIntMax(uint64_t &Result, uint64_t MaxPlus1);
|
|
|
|
std::error_code readSize(uint64_t &Result);
|
|
|
|
std::error_code readString(StringRef &Result);
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Reader for the raw coverage filenames.
|
|
|
|
class RawCoverageFilenamesReader : public RawCoverageReader {
|
|
|
|
std::vector<StringRef> &Filenames;
|
|
|
|
|
2015-02-15 22:54:22 +00:00
|
|
|
RawCoverageFilenamesReader(const RawCoverageFilenamesReader &) = delete;
|
2014-07-24 23:57:54 +00:00
|
|
|
RawCoverageFilenamesReader &
|
2015-02-15 22:54:22 +00:00
|
|
|
operator=(const RawCoverageFilenamesReader &) = delete;
|
2014-07-24 23:57:54 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
RawCoverageFilenamesReader(StringRef Data, std::vector<StringRef> &Filenames)
|
|
|
|
: RawCoverageReader(Data), Filenames(Filenames) {}
|
|
|
|
|
|
|
|
std::error_code read();
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Reader for the raw coverage mapping data.
|
|
|
|
class RawCoverageMappingReader : public RawCoverageReader {
|
|
|
|
ArrayRef<StringRef> TranslationUnitFilenames;
|
|
|
|
std::vector<StringRef> &Filenames;
|
|
|
|
std::vector<CounterExpression> &Expressions;
|
|
|
|
std::vector<CounterMappingRegion> &MappingRegions;
|
|
|
|
|
2015-02-15 22:54:22 +00:00
|
|
|
RawCoverageMappingReader(const RawCoverageMappingReader &) = delete;
|
2014-07-24 23:57:54 +00:00
|
|
|
RawCoverageMappingReader &
|
2015-02-15 22:54:22 +00:00
|
|
|
operator=(const RawCoverageMappingReader &) = delete;
|
2014-07-24 23:57:54 +00:00
|
|
|
|
|
|
|
public:
|
2015-02-03 00:20:11 +00:00
|
|
|
RawCoverageMappingReader(StringRef MappingData,
|
2014-07-24 23:57:54 +00:00
|
|
|
ArrayRef<StringRef> TranslationUnitFilenames,
|
|
|
|
std::vector<StringRef> &Filenames,
|
|
|
|
std::vector<CounterExpression> &Expressions,
|
|
|
|
std::vector<CounterMappingRegion> &MappingRegions)
|
2015-02-03 00:20:11 +00:00
|
|
|
: RawCoverageReader(MappingData),
|
2014-07-24 23:57:54 +00:00
|
|
|
TranslationUnitFilenames(TranslationUnitFilenames),
|
|
|
|
Filenames(Filenames), Expressions(Expressions),
|
|
|
|
MappingRegions(MappingRegions) {}
|
|
|
|
|
2015-02-03 00:20:11 +00:00
|
|
|
std::error_code read();
|
2014-07-24 23:57:54 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::error_code decodeCounter(unsigned Value, Counter &C);
|
|
|
|
std::error_code readCounter(Counter &C);
|
|
|
|
std::error_code
|
|
|
|
readMappingRegionsSubArray(std::vector<CounterMappingRegion> &MappingRegions,
|
|
|
|
unsigned InferredFileID, size_t NumFileIDs);
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Reader for the coverage mapping data that is emitted by the
|
|
|
|
/// frontend and stored in an object file.
|
2015-02-26 20:06:24 +00:00
|
|
|
class BinaryCoverageReader : public CoverageMappingReader {
|
2014-07-24 23:57:54 +00:00
|
|
|
public:
|
|
|
|
struct ProfileMappingRecord {
|
|
|
|
CoverageMappingVersion Version;
|
|
|
|
StringRef FunctionName;
|
2014-08-21 19:23:25 +00:00
|
|
|
uint64_t FunctionHash;
|
2014-07-24 23:57:54 +00:00
|
|
|
StringRef CoverageMapping;
|
|
|
|
size_t FilenamesBegin;
|
|
|
|
size_t FilenamesSize;
|
|
|
|
|
|
|
|
ProfileMappingRecord(CoverageMappingVersion Version, StringRef FunctionName,
|
2014-08-21 19:23:25 +00:00
|
|
|
uint64_t FunctionHash, StringRef CoverageMapping,
|
|
|
|
size_t FilenamesBegin, size_t FilenamesSize)
|
2014-07-24 23:57:54 +00:00
|
|
|
: Version(Version), FunctionName(FunctionName),
|
2014-08-21 19:23:25 +00:00
|
|
|
FunctionHash(FunctionHash), CoverageMapping(CoverageMapping),
|
|
|
|
FilenamesBegin(FilenamesBegin), FilenamesSize(FilenamesSize) {}
|
2014-07-24 23:57:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<StringRef> Filenames;
|
|
|
|
std::vector<ProfileMappingRecord> MappingRecords;
|
|
|
|
size_t CurrentRecord;
|
|
|
|
std::vector<StringRef> FunctionsFilenames;
|
|
|
|
std::vector<CounterExpression> Expressions;
|
|
|
|
std::vector<CounterMappingRegion> MappingRegions;
|
|
|
|
|
2015-02-26 20:06:24 +00:00
|
|
|
BinaryCoverageReader(const BinaryCoverageReader &) = delete;
|
|
|
|
BinaryCoverageReader &operator=(const BinaryCoverageReader &) = delete;
|
2014-07-24 23:57:54 +00:00
|
|
|
|
2015-02-26 20:06:28 +00:00
|
|
|
BinaryCoverageReader() : CurrentRecord(0) {}
|
2014-07-24 23:57:54 +00:00
|
|
|
|
|
|
|
public:
|
2015-02-26 20:06:28 +00:00
|
|
|
static ErrorOr<std::unique_ptr<BinaryCoverageReader>>
|
|
|
|
create(std::unique_ptr<MemoryBuffer> &ObjectBuffer);
|
2014-07-24 23:57:54 +00:00
|
|
|
|
2015-02-18 18:01:14 +00:00
|
|
|
std::error_code readNextRecord(CoverageMappingRecord &Record) override;
|
2014-07-24 23:57:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace coverage
|
|
|
|
} // end namespace llvm
|
|
|
|
|
2014-08-13 16:26:38 +00:00
|
|
|
#endif
|