[llvm] b5f1a8c - [llvm-cov] New parameters to set coverage coverage_watermark
Petr Hosek via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 4 22:21:47 PST 2022
Author: Petr Hosek
Date: 2022-03-04T22:21:40-08:00
New Revision: b5f1a8cfc37a80027dbf005da4bf7da144bd9087
URL: https://github.com/llvm/llvm-project/commit/b5f1a8cfc37a80027dbf005da4bf7da144bd9087
DIFF: https://github.com/llvm/llvm-project/commit/b5f1a8cfc37a80027dbf005da4bf7da144bd9087.diff
LOG: [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
Added:
Modified:
llvm/docs/CommandGuide/llvm-cov.rst
llvm/tools/llvm-cov/CodeCoverage.cpp
llvm/tools/llvm-cov/CoverageViewOptions.h
llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp
Removed:
################################################################################
diff --git a/llvm/docs/CommandGuide/llvm-cov.rst b/llvm/docs/CommandGuide/llvm-cov.rst
index e1f827b176226..4d3d4517397f7 100644
--- a/llvm/docs/CommandGuide/llvm-cov.rst
+++ b/llvm/docs/CommandGuide/llvm-cov.rst
@@ -349,6 +349,13 @@ OPTIONS
to generate the coverage data on one machine, and then use llvm-cov on a
diff erent machine where you have the same files on a
diff erent 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:
diff --git a/llvm/tools/llvm-cov/CodeCoverage.cpp b/llvm/tools/llvm-cov/CodeCoverage.cpp
index ef801287c1be4..720a71e9a8b63 100644
--- a/llvm/tools/llvm-cov/CodeCoverage.cpp
+++ b/llvm/tools/llvm-cov/CodeCoverage.cpp
@@ -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;
diff --git a/llvm/tools/llvm-cov/CoverageViewOptions.h b/llvm/tools/llvm-cov/CoverageViewOptions.h
index 045fb1787bcea..c6e99819f3190 100644
--- a/llvm/tools/llvm-cov/CoverageViewOptions.h
+++ b/llvm/tools/llvm-cov/CoverageViewOptions.h
@@ -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,
diff --git a/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp b/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp
index 56efc40b9349c..a70b97b4082fb 100644
--- a/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp
+++ b/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp
@@ -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;
More information about the llvm-commits
mailing list