Skip to content

Commit

Permalink
[llvm-cov] New parameters to set coverage coverage_watermark
Browse files Browse the repository at this point in the history
Add a pairs of parameters to set coverage watermark for llvm-cov, and
user can change the percentage thresholds marked with different colors
in the report.

Patch By: tanjinhua

Differential Revision: https://reviews.llvm.org/D116876
  • Loading branch information
petrhosek committed Mar 5, 2022
1 parent fa9c8ba commit b5f1a8c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 18 deletions.
7 changes: 7 additions & 0 deletions llvm/docs/CommandGuide/llvm-cov.rst
Expand Up @@ -349,6 +349,13 @@ OPTIONS
to generate the coverage data on one machine, and then use llvm-cov on a
different machine where you have the same files on a different path.

.. option:: -coverage-watermark=<high>,<low>

Set high and low watermarks for coverage in html format output. This allows you
to set the high and low watermark of coverage as desired, green when
coverage >= high, red when coverage < low, and yellow otherwise. Both high and
low should be between 0-100 and high > low.

.. program:: llvm-cov report

.. _llvm-cov-report:
Expand Down
46 changes: 46 additions & 0 deletions llvm/tools/llvm-cov/CodeCoverage.cpp
Expand Up @@ -973,6 +973,11 @@ int CodeCoverageTool::doShow(int argc, const char **argv,
"project-title", cl::Optional,
cl::desc("Set project title for the coverage report"));

cl::opt<std::string> CovWatermark(
"coverage-watermark", cl::Optional,
cl::desc("<high>,<low> value indicate thresholds for high and low"
"coverage watermark"));

auto Err = commandLineParser(argc, argv);
if (Err)
return Err;
Expand All @@ -982,6 +987,47 @@ int CodeCoverageTool::doShow(int argc, const char **argv,
return 1;
}

ViewOpts.HighCovWatermark = 100.0;
ViewOpts.LowCovWatermark = 80.0;
if (!CovWatermark.empty()) {
auto WaterMarkPair = StringRef(CovWatermark).split(',');
if (WaterMarkPair.first.empty() || WaterMarkPair.second.empty()) {
error("invalid argument '" + CovWatermark +
"', must be in format 'high,low'",
"-coverage-watermark");
return 1;
}

char *EndPointer = nullptr;
ViewOpts.HighCovWatermark =
strtod(WaterMarkPair.first.begin(), &EndPointer);
if (EndPointer != WaterMarkPair.first.end()) {
error("invalid number '" + WaterMarkPair.first +
"', invalid value for 'high'",
"-coverage-watermark");
return 1;
}

ViewOpts.LowCovWatermark =
strtod(WaterMarkPair.second.begin(), &EndPointer);
if (EndPointer != WaterMarkPair.second.end()) {
error("invalid number '" + WaterMarkPair.second +
"', invalid value for 'low'",
"-coverage-watermark");
return 1;
}

if (ViewOpts.HighCovWatermark > 100 || ViewOpts.LowCovWatermark < 0 ||
ViewOpts.HighCovWatermark <= ViewOpts.LowCovWatermark) {
error(
"invalid number range '" + CovWatermark +
"', must be both high and low should be between 0-100, and high "
"> low",
"-coverage-watermark");
return 1;
}
}

ViewOpts.ShowLineNumbers = true;
ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
!ShowRegions || ShowBestLineRegionsCounts;
Expand Down
2 changes: 2 additions & 0 deletions llvm/tools/llvm-cov/CoverageViewOptions.h
Expand Up @@ -50,6 +50,8 @@ struct CoverageViewOptions {
std::string CreatedTimeStr;
unsigned NumThreads;
std::string CompilationDirectory;
float HighCovWatermark;
float LowCovWatermark;

/// Change the output's stream color if the colors are enabled.
ColoredRawOstream colored_ostream(raw_ostream &OS,
Expand Down
36 changes: 18 additions & 18 deletions llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp
Expand Up @@ -338,24 +338,24 @@ void CoveragePrinterHTML::emitFileSummary(raw_ostream &OS, StringRef SF,
SmallVector<std::string, 8> Columns;

// Format a coverage triple and add the result to the list of columns.
auto AddCoverageTripleToColumn = [&Columns](unsigned Hit, unsigned Total,
float Pctg) {
std::string S;
{
raw_string_ostream RSO{S};
if (Total)
RSO << format("%*.2f", 7, Pctg) << "% ";
else
RSO << "- ";
RSO << '(' << Hit << '/' << Total << ')';
}
const char *CellClass = "column-entry-yellow";
if (Hit == Total)
CellClass = "column-entry-green";
else if (Pctg < 80.0)
CellClass = "column-entry-red";
Columns.emplace_back(tag("td", tag("pre", S), CellClass));
};
auto AddCoverageTripleToColumn =
[&Columns, this](unsigned Hit, unsigned Total, float Pctg) {
std::string S;
{
raw_string_ostream RSO{S};
if (Total)
RSO << format("%*.2f", 7, Pctg) << "% ";
else
RSO << "- ";
RSO << '(' << Hit << '/' << Total << ')';
}
const char *CellClass = "column-entry-yellow";
if (Pctg >= Opts.HighCovWatermark)
CellClass = "column-entry-green";
else if (Pctg < Opts.LowCovWatermark)
CellClass = "column-entry-red";
Columns.emplace_back(tag("td", tag("pre", S), CellClass));
};

// Simplify the display file path, and wrap it in a link if requested.
std::string Filename;
Expand Down

0 comments on commit b5f1a8c

Please sign in to comment.