[llvm-cov] New parameters to set coverage coverage_watermark

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
This commit is contained in:
Petr Hosek 2022-03-04 22:20:42 -08:00
parent fa9c8bab0c
commit b5f1a8cfc3
4 changed files with 73 additions and 18 deletions

View File

@ -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:

View File

@ -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;
@ -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;

View File

@ -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,

View File

@ -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;