1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
//===- SourceCoverageView.h - Code coverage view for source code ----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file This class implements rendering for code coverage of source code.
///
//===----------------------------------------------------------------------===//
#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;
/// A view that represents a macro or include expansion.
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();
}
};
/// A view that represents a function instantiation.
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;
}
};
/// A view that represents one or more branch regions on a given source 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;
}
};
/// A file manager that handles format-aware file creation.
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) {}
/// Return `OutputDir/ToplevelDir/Path.Extension`. If \p InToplevel is
/// false, skip the ToplevelDir component. If \p Relative is false, skip the
/// OutputDir component.
std::string getOutputPath(StringRef Path, StringRef Extension,
bool InToplevel, bool Relative = true) const;
/// If directory output is enabled, create a file in that directory
/// at the path given by getOutputPath(). Otherwise, return stdout.
Expected<OwnedStream> createOutputStream(StringRef Path, StringRef Extension,
bool InToplevel) const;
/// Return the sub-directory name for file coverage reports.
static StringRef getCoverageDir() { return "coverage"; }
public:
static std::unique_ptr<CoveragePrinter>
create(const CoverageViewOptions &Opts);
virtual ~CoveragePrinter() {}
/// @name File Creation Interface
/// @{
/// Create a file to print a coverage view into.
virtual Expected<OwnedStream> createViewFile(StringRef Path,
bool InToplevel) = 0;
/// Close a file which has been used to print a coverage view.
virtual void closeViewFile(OwnedStream OS) = 0;
/// Create an index which lists reports for the given source files.
virtual Error createIndexFile(ArrayRef<std::string> SourceFiles,
const CoverageMapping &Coverage,
const CoverageFiltersMatchAll &Filters) = 0;
/// @}
};
/// A code coverage view of a source file or function.
///
/// A source coverage view and its nested sub-views form a file-oriented
/// representation of code coverage data. This view can be printed out by a
/// renderer which implements the Rendering Interface.
class SourceCoverageView {
/// A function or file name.
StringRef SourceName;
/// A memory buffer backing the source on display.
const MemoryBuffer &File;
/// Various options to guide the coverage renderer.
const CoverageViewOptions &Options;
/// Complete coverage information about the source on display.
CoverageData CoverageInfo;
/// A container for all expansions (e.g macros) in the source on display.
std::vector<ExpansionView> ExpansionSubViews;
/// A container for all branches in the source on display.
std::vector<BranchView> BranchSubViews;
/// A container for all instantiations (e.g template functions) in the source
/// on display.
std::vector<InstantiationView> InstantiationSubViews;
/// Get the first uncovered line number for the source file.
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 *>;
/// @name Rendering Interface
/// @{
/// Render a header for the view.
virtual void renderViewHeader(raw_ostream &OS) = 0;
/// Render a footer for the view.
virtual void renderViewFooter(raw_ostream &OS) = 0;
/// Render the source name for the view.
virtual void renderSourceName(raw_ostream &OS, bool WholeFile) = 0;
/// Render the line prefix at the given \p ViewDepth.
virtual void renderLinePrefix(raw_ostream &OS, unsigned ViewDepth) = 0;
/// Render the line suffix at the given \p ViewDepth.
virtual void renderLineSuffix(raw_ostream &OS, unsigned ViewDepth) = 0;
/// Render a view divider at the given \p ViewDepth.
virtual void renderViewDivider(raw_ostream &OS, unsigned ViewDepth) = 0;
/// Render a source line with highlighting.
virtual void renderLine(raw_ostream &OS, LineRef L,
const LineCoverageStats &LCS, unsigned ExpansionCol,
unsigned ViewDepth) = 0;
/// Render the line's execution count column.
virtual void renderLineCoverageColumn(raw_ostream &OS,
const LineCoverageStats &Line) = 0;
/// Render the line number column.
virtual void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo) = 0;
/// Render all the region's execution counts on a line.
virtual void renderRegionMarkers(raw_ostream &OS,
const LineCoverageStats &Line,
unsigned ViewDepth) = 0;
/// Render the site of an expansion.
virtual void renderExpansionSite(raw_ostream &OS, LineRef L,
const LineCoverageStats &LCS,
unsigned ExpansionCol,
unsigned ViewDepth) = 0;
/// Render an expansion view and any nested views.
virtual void renderExpansionView(raw_ostream &OS, ExpansionView &ESV,
unsigned ViewDepth) = 0;
/// Render an instantiation view and any nested views.
virtual void renderInstantiationView(raw_ostream &OS, InstantiationView &ISV,
unsigned ViewDepth) = 0;
/// Render a branch view and any nested views.
virtual void renderBranchView(raw_ostream &OS, BranchView &BRV,
unsigned ViewDepth) = 0;
/// Render \p Title, a project title if one is available, and the
/// created time.
virtual void renderTitle(raw_ostream &OS, StringRef CellText) = 0;
/// Render the table header for a given source file.
virtual void renderTableHeader(raw_ostream &OS, unsigned FirstUncoveredLineNo,
unsigned IndentLevel) = 0;
/// @}
/// Format a count using engineering notation with 3 significant
/// digits.
static std::string formatCount(uint64_t N);
/// Check if region marker output is expected for a line.
bool shouldRenderRegionMarkers(const LineCoverageStats &LCS) const;
/// Check if there are any sub-views attached to this view.
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() {}
/// Return the source name formatted for the host OS.
std::string getSourceName() const;
const CoverageViewOptions &getOptions() const { return Options; }
/// Add an expansion subview to this view.
void addExpansion(const CounterMappingRegion &Region,
std::unique_ptr<SourceCoverageView> View);
/// Add a function instantiation subview to this view.
void addInstantiation(StringRef FunctionName, unsigned Line,
std::unique_ptr<SourceCoverageView> View);
/// Add a branch subview to this view.
void addBranch(unsigned Line, ArrayRef<CountedRegion> Regions,
std::unique_ptr<SourceCoverageView> View);
/// Print the code coverage information for a specific portion of a
/// source file to the output stream.
void print(raw_ostream &OS, bool WholeFile, bool ShowSourceName,
bool ShowTitle, unsigned ViewDepth = 0);
};
} // namespace llvm
#endif // LLVM_COV_SOURCECOVERAGEVIEW_H
|