#ifndef LLVM_COV_SOURCECOVERAGEVIEW_H
#define LLVM_COV_SOURCECOVERAGEVIEW_H
#include "CoverageViewOptions.h"
#include "CoverageSummaryInfo.h"
#include "llvm/ProfileData/Coverage/CoverageMapping.h"
#include "llvm/Support/MemoryBuffer.h"
#include <vector>
namespace llvm {
using namespace coverage;
class CoverageFiltersMatchAll;
class SourceCoverageView;
struct ExpansionView {
CounterMappingRegion Region;
std::unique_ptr<SourceCoverageView> View;
ExpansionView(const CounterMappingRegion &Region,
std::unique_ptr<SourceCoverageView> View)
: Region(Region), View(std::move(View)) {}
ExpansionView(ExpansionView &&RHS)
: Region(std::move(RHS.Region)), View(std::move(RHS.View)) {}
ExpansionView &operator=(ExpansionView &&RHS) {
Region = std::move(RHS.Region);
View = std::move(RHS.View);
return *this;
}
unsigned getLine() const { return Region.LineStart; }
unsigned getStartCol() const { return Region.ColumnStart; }
unsigned getEndCol() const { return Region.ColumnEnd; }
friend bool operator<(const ExpansionView &LHS, const ExpansionView &RHS) {
return LHS.Region.startLoc() < RHS.Region.startLoc();
}
};
struct InstantiationView {
StringRef FunctionName;
unsigned Line;
std::unique_ptr<SourceCoverageView> View;
InstantiationView(StringRef FunctionName, unsigned Line,
std::unique_ptr<SourceCoverageView> View)
: FunctionName(FunctionName), Line(Line), View(std::move(View)) {}
friend bool operator<(const InstantiationView &LHS,
const InstantiationView &RHS) {
return LHS.Line < RHS.Line;
}
};
struct BranchView {
std::vector<CountedRegion> Regions;
std::unique_ptr<SourceCoverageView> View;
unsigned Line;
BranchView(unsigned Line, ArrayRef<CountedRegion> Regions,
std::unique_ptr<SourceCoverageView> View)
: Regions(Regions), View(std::move(View)), Line(Line) {}
unsigned getLine() const { return Line; }
friend bool operator<(const BranchView &LHS, const BranchView &RHS) {
return LHS.Line < RHS.Line;
}
};
class CoveragePrinter {
public:
struct StreamDestructor {
void operator()(raw_ostream *OS) const;
};
using OwnedStream = std::unique_ptr<raw_ostream, StreamDestructor>;
protected:
const CoverageViewOptions &Opts;
CoveragePrinter(const CoverageViewOptions &Opts) : Opts(Opts) {}
std::string getOutputPath(StringRef Path, StringRef Extension,
bool InToplevel, bool Relative = true) const;
Expected<OwnedStream> createOutputStream(StringRef Path, StringRef Extension,
bool InToplevel) const;
static StringRef getCoverageDir() { return "coverage"; }
public:
static std::unique_ptr<CoveragePrinter>
create(const CoverageViewOptions &Opts);
virtual ~CoveragePrinter() {}
virtual Expected<OwnedStream> createViewFile(StringRef Path,
bool InToplevel) = 0;
virtual void closeViewFile(OwnedStream OS) = 0;
virtual Error createIndexFile(ArrayRef<std::string> SourceFiles,
const CoverageMapping &Coverage,
const CoverageFiltersMatchAll &Filters) = 0;
};
class SourceCoverageView {
StringRef SourceName;
const MemoryBuffer &File;
const CoverageViewOptions &Options;
CoverageData CoverageInfo;
std::vector<ExpansionView> ExpansionSubViews;
std::vector<BranchView> BranchSubViews;
std::vector<InstantiationView> InstantiationSubViews;
unsigned getFirstUncoveredLineNo();
protected:
struct LineRef {
StringRef Line;
int64_t LineNo;
LineRef(StringRef Line, int64_t LineNo) : Line(Line), LineNo(LineNo) {}
};
using CoverageSegmentArray = ArrayRef<const CoverageSegment *>;
virtual void renderViewHeader(raw_ostream &OS) = 0;
virtual void renderViewFooter(raw_ostream &OS) = 0;
virtual void renderSourceName(raw_ostream &OS, bool WholeFile) = 0;
virtual void renderLinePrefix(raw_ostream &OS, unsigned ViewDepth) = 0;
virtual void renderLineSuffix(raw_ostream &OS, unsigned ViewDepth) = 0;
virtual void renderViewDivider(raw_ostream &OS, unsigned ViewDepth) = 0;
virtual void renderLine(raw_ostream &OS, LineRef L,
const LineCoverageStats &LCS, unsigned ExpansionCol,
unsigned ViewDepth) = 0;
virtual void renderLineCoverageColumn(raw_ostream &OS,
const LineCoverageStats &Line) = 0;
virtual void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo) = 0;
virtual void renderRegionMarkers(raw_ostream &OS,
const LineCoverageStats &Line,
unsigned ViewDepth) = 0;
virtual void renderExpansionSite(raw_ostream &OS, LineRef L,
const LineCoverageStats &LCS,
unsigned ExpansionCol,
unsigned ViewDepth) = 0;
virtual void renderExpansionView(raw_ostream &OS, ExpansionView &ESV,
unsigned ViewDepth) = 0;
virtual void renderInstantiationView(raw_ostream &OS, InstantiationView &ISV,
unsigned ViewDepth) = 0;
virtual void renderBranchView(raw_ostream &OS, BranchView &BRV,
unsigned ViewDepth) = 0;
virtual void renderTitle(raw_ostream &OS, StringRef CellText) = 0;
virtual void renderTableHeader(raw_ostream &OS, unsigned FirstUncoveredLineNo,
unsigned IndentLevel) = 0;
static std::string formatCount(uint64_t N);
bool shouldRenderRegionMarkers(const LineCoverageStats &LCS) const;
bool hasSubViews() const;
SourceCoverageView(StringRef SourceName, const MemoryBuffer &File,
const CoverageViewOptions &Options,
CoverageData &&CoverageInfo)
: SourceName(SourceName), File(File), Options(Options),
CoverageInfo(std::move(CoverageInfo)) {}
public:
static std::unique_ptr<SourceCoverageView>
create(StringRef SourceName, const MemoryBuffer &File,
const CoverageViewOptions &Options, CoverageData &&CoverageInfo);
virtual ~SourceCoverageView() {}
std::string getSourceName() const;
const CoverageViewOptions &getOptions() const { return Options; }
void addExpansion(const CounterMappingRegion &Region,
std::unique_ptr<SourceCoverageView> View);
void addInstantiation(StringRef FunctionName, unsigned Line,
std::unique_ptr<SourceCoverageView> View);
void addBranch(unsigned Line, ArrayRef<CountedRegion> Regions,
std::unique_ptr<SourceCoverageView> View);
void print(raw_ostream &OS, bool WholeFile, bool ShowSourceName,
bool ShowTitle, unsigned ViewDepth = 0);
};
}
#endif