Skip to content

Commit 223521b

Browse files
authored
llvm-cov: Introduce --binary-counters (#120841)
In `llvm-cov show`, this option rounds counters (line, branch) to `[1,0]` at rendering. This will be useful when the number of counts doesn't interest but **Covered/uncoverd** does.
1 parent ac8bb73 commit 223521b

File tree

7 files changed

+36
-10
lines changed

7 files changed

+36
-10
lines changed

llvm/test/tools/llvm-cov/branch-macros.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: llvm-profdata merge %S/Inputs/branch-macros.proftext -o %t.profdata
22
// RUN: llvm-cov show --show-expansions --show-branches=count %S/Inputs/branch-macros.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs | FileCheck %S/Inputs/branch-macros.cpp -check-prefixes=CHECK,BRCOV -D#C=999
3+
// RUN: llvm-cov show --binary-counters --show-expansions --show-branches=count %S/Inputs/branch-macros.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs | FileCheck %S/Inputs/branch-macros.cpp -check-prefixes=CHECK,BRCOV -D#C=1
34
// RUN: llvm-cov report --show-branch-summary %S/Inputs/branch-macros.o32l -instr-profile %t.profdata -show-functions -path-equivalence=/tmp,%S/Inputs %S/Inputs/branch-macros.cpp | FileCheck %s -check-prefix=REPORT
45

56
// RUN: yaml2obj %S/Inputs/branch-macros-single.yaml -o %t.o

llvm/test/tools/llvm-cov/showLineExecutionCounts.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// RUN: llvm-profdata merge %S/Inputs/lineExecutionCounts.proftext -o %t.profdata
44

55
// RUN: llvm-cov show %S/Inputs/lineExecutionCounts.covmapping -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs | FileCheck -check-prefixes=TEXT,WHOLE-FILE -D#C=999 -DC16K2=16.2k -DC16K1=16.1k %S/Inputs/showLineExecutionCounts.cpp
6+
// RUN: llvm-cov show %S/Inputs/lineExecutionCounts.covmapping -binary-counters -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs | FileCheck -check-prefixes=TEXT,WHOLE-FILE -D#C=1 -DC16K2=1 -DC16K1=1 %S/Inputs/showLineExecutionCounts.cpp
67
// RUN: llvm-cov show %S/Inputs/lineExecutionCounts.covmapping -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs -name=main | FileCheck -check-prefixes=TEXT,FILTER -D#C=999 -DC16K2=16.2k -DC16K1=16.1k %S/Inputs/showLineExecutionCounts.cpp
78

89
// Test -output-dir.
@@ -16,8 +17,10 @@
1617
//
1718
// Test html output.
1819
// RUN: llvm-cov show %S/Inputs/lineExecutionCounts.covmapping -format html -o %t.dir/html -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs
20+
// RUN: llvm-cov show %S/Inputs/lineExecutionCounts.covmapping -format html -o %t.dir/html.binary -binary-counters -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs
1921
// RUN: llvm-cov show %S/Inputs/lineExecutionCounts.covmapping -format html -o %t.dir/html.filtered -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs -name=main
2022
// RUN: FileCheck -check-prefixes=HTML,HTML-WHOLE-FILE -input-file %t.dir/html/coverage/tmp/showLineExecutionCounts.cpp.html %S/Inputs/showLineExecutionCounts.cpp
23+
// RUN: FileCheck -check-prefixes=HTML-BINARY,HTML-WHOLE-FILE -input-file %t.dir/html.binary/coverage/tmp/showLineExecutionCounts.cpp.html %S/Inputs/showLineExecutionCounts.cpp
2124
// RUN: FileCheck -check-prefixes=HTML,HTML-FILTER -input-file %t.dir/html.filtered/coverage/tmp/showLineExecutionCounts.cpp.html %S/Inputs/showLineExecutionCounts.cpp
2225
//
2326
// Test index creation.

llvm/tools/llvm-cov/CodeCoverage.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,12 @@ int CodeCoverageTool::doShow(int argc, const char **argv,
10231023
cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"),
10241024
cl::aliasopt(ShowOutputDirectory));
10251025

1026+
cl::opt<bool> BinaryCounters(
1027+
"binary-counters", cl::Optional,
1028+
cl::desc("Show binary counters (1/0) in lines and branches instead of "
1029+
"integer execution counts"),
1030+
cl::cat(ViewCategory));
1031+
10261032
cl::opt<uint32_t> TabSize(
10271033
"tab-size", cl::init(2),
10281034
cl::desc(
@@ -1100,6 +1106,7 @@ int CodeCoverageTool::doShow(int argc, const char **argv,
11001106
ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
11011107
ViewOpts.ShowDirectoryCoverage = ShowDirectoryCoverage;
11021108
ViewOpts.ShowOutputDirectory = ShowOutputDirectory;
1109+
ViewOpts.BinaryCounters = BinaryCounters;
11031110
ViewOpts.TabSize = TabSize;
11041111
ViewOpts.ProjectTitle = ProjectTitle;
11051112

llvm/tools/llvm-cov/CoverageViewOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct CoverageViewOptions {
4545
bool SkipExpansions;
4646
bool SkipFunctions;
4747
bool SkipBranches;
48+
bool BinaryCounters;
4849
OutputFormat Format;
4950
BranchOutputType ShowBranches;
5051
std::string ShowOutputDirectory;

llvm/tools/llvm-cov/SourceCoverageView.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ class SourceCoverageView {
180180
/// on display.
181181
std::vector<InstantiationView> InstantiationSubViews;
182182

183+
bool BinaryCounters;
184+
183185
/// Get the first uncovered line number for the source file.
184186
unsigned getFirstUncoveredLineNo();
185187

@@ -266,6 +268,14 @@ class SourceCoverageView {
266268
/// digits.
267269
static std::string formatCount(uint64_t N);
268270

271+
uint64_t BinaryCount(uint64_t N) const {
272+
return (N && BinaryCounters ? 1 : N);
273+
}
274+
275+
std::string formatBinaryCount(uint64_t N) const {
276+
return formatCount(BinaryCount(N));
277+
}
278+
269279
/// Check if region marker output is expected for a line.
270280
bool shouldRenderRegionMarkers(const LineCoverageStats &LCS) const;
271281

@@ -276,7 +286,8 @@ class SourceCoverageView {
276286
const CoverageViewOptions &Options,
277287
CoverageData &&CoverageInfo)
278288
: SourceName(SourceName), File(File), Options(Options),
279-
CoverageInfo(std::move(CoverageInfo)) {}
289+
CoverageInfo(std::move(CoverageInfo)),
290+
BinaryCounters(Options.BinaryCounters) {}
280291

281292
public:
282293
static std::unique_ptr<SourceCoverageView>

llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,19 +1019,22 @@ void SourceCoverageViewHTML::renderLine(raw_ostream &OS, LineRef L,
10191019
// Just consider the segments which start *and* end on this line.
10201020
for (unsigned I = 0, E = Segments.size() - 1; I < E; ++I) {
10211021
const auto *CurSeg = Segments[I];
1022+
auto CurSegCount = BinaryCount(CurSeg->Count);
1023+
auto LCSCount = BinaryCount(LCS.getExecutionCount());
10221024
if (!CurSeg->IsRegionEntry)
10231025
continue;
1024-
if (CurSeg->Count == LCS.getExecutionCount())
1026+
if (CurSegCount == LCSCount)
10251027
continue;
10261028

10271029
Snippets[I + 1] =
1028-
tag("div", Snippets[I + 1] + tag("span", formatCount(CurSeg->Count),
1029-
"tooltip-content"),
1030+
tag("div",
1031+
Snippets[I + 1] +
1032+
tag("span", formatCount(CurSegCount), "tooltip-content"),
10301033
"tooltip");
10311034

10321035
if (getOptions().Debug)
10331036
errs() << "Marker at " << CurSeg->Line << ":" << CurSeg->Col << " = "
1034-
<< formatCount(CurSeg->Count) << "\n";
1037+
<< formatCount(CurSegCount) << "\n";
10351038
}
10361039
}
10371040

@@ -1051,7 +1054,7 @@ void SourceCoverageViewHTML::renderLineCoverageColumn(
10511054
raw_ostream &OS, const LineCoverageStats &Line) {
10521055
std::string Count;
10531056
if (Line.isMapped())
1054-
Count = tag("pre", formatCount(Line.getExecutionCount()));
1057+
Count = tag("pre", formatBinaryCount(Line.getExecutionCount()));
10551058
std::string CoverageClass =
10561059
(Line.getExecutionCount() > 0)
10571060
? "covered-line"
@@ -1106,7 +1109,7 @@ void SourceCoverageViewHTML::renderBranchView(raw_ostream &OS, BranchView &BRV,
11061109

11071110
OS << tag("span", Label, (Count ? "None" : "red branch")) << ": ";
11081111
if (getOptions().ShowBranchCounts)
1109-
OS << tag("span", formatCount(Count),
1112+
OS << tag("span", formatBinaryCount(Count),
11101113
(Count ? "covered-line" : "uncovered-line"));
11111114
else
11121115
OS << format("%0.2f", (Total != 0 ? 100.0 * Count / Total : 0.0)) << "%";

llvm/tools/llvm-cov/SourceCoverageViewText.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ void SourceCoverageViewText::renderLineCoverageColumn(
216216
OS.indent(LineCoverageColumnWidth) << '|';
217217
return;
218218
}
219-
std::string C = formatCount(Line.getExecutionCount());
219+
std::string C = formatBinaryCount(Line.getExecutionCount());
220220
OS.indent(LineCoverageColumnWidth - C.size());
221221
colored_ostream(OS, raw_ostream::MAGENTA,
222222
Line.hasMultipleRegions() && getOptions().Colors)
@@ -263,7 +263,7 @@ void SourceCoverageViewText::renderRegionMarkers(raw_ostream &OS,
263263

264264
if (getOptions().Debug)
265265
errs() << "Marker at " << S->Line << ":" << S->Col << " = "
266-
<< formatCount(S->Count) << "\n";
266+
<< formatBinaryCount(S->Count) << "\n";
267267
}
268268
OS << '\n';
269269
}
@@ -307,7 +307,7 @@ void SourceCoverageViewText::renderBranchView(raw_ostream &OS, BranchView &BRV,
307307
<< Label;
308308

309309
if (getOptions().ShowBranchCounts)
310-
OS << ": " << formatCount(Count);
310+
OS << ": " << formatBinaryCount(Count);
311311
else
312312
OS << ": " << format("%0.2f", (Total != 0 ? 100.0 * Count / Total : 0.0))
313313
<< "%";

0 commit comments

Comments
 (0)