Skip to content

Commit a9df1f6

Browse files
committed
llvm-cov: Refactor SourceCoverageView::renderBranchView().
NFC except for calculating `Total`. I've replaced `(uint64_t)+(uint64_t)` with `(double)+(double)`. This is still inexact with large numbers `(1LL << 53)` but will be expected to prevent possible overflow.
1 parent 5a5838f commit a9df1f6

File tree

2 files changed

+53
-82
lines changed

2 files changed

+53
-82
lines changed

llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,20 +1096,31 @@ void SourceCoverageViewHTML::renderBranchView(raw_ostream &OS, BranchView &BRV,
10961096
if (getOptions().Debug)
10971097
errs() << "Branch at line " << BRV.getLine() << '\n';
10981098

1099+
auto BranchCount = [&](StringRef Label, uint64_t Count, bool Folded,
1100+
double Total) {
1101+
if (Folded)
1102+
return std::string{"Folded"};
1103+
1104+
std::string Str;
1105+
raw_string_ostream OS(Str);
1106+
1107+
OS << tag("span", Label, (Count ? "None" : "red branch")) << ": ";
1108+
if (getOptions().ShowBranchCounts)
1109+
OS << tag("span", formatCount(Count),
1110+
(Count ? "covered-line" : "uncovered-line"));
1111+
else
1112+
OS << format("%0.2f", (Total != 0 ? 100.0 * Count / Total : 0.0)) << "%";
1113+
1114+
return Str;
1115+
};
1116+
10991117
OS << BeginExpansionDiv;
11001118
OS << BeginPre;
11011119
for (const auto &R : BRV.Regions) {
1102-
// Calculate TruePercent and False Percent.
1103-
double TruePercent = 0.0;
1104-
double FalsePercent = 0.0;
1105-
// FIXME: It may overflow when the data is too large, but I have not
1106-
// encountered it in actual use, and not sure whether to use __uint128_t.
1107-
uint64_t Total = R.ExecutionCount + R.FalseExecutionCount;
1108-
1109-
if (!getOptions().ShowBranchCounts && Total != 0) {
1110-
TruePercent = ((double)(R.ExecutionCount) / (double)Total) * 100.0;
1111-
FalsePercent = ((double)(R.FalseExecutionCount) / (double)Total) * 100.0;
1112-
}
1120+
// This can be `double` since it is only used as a denominator.
1121+
// FIXME: It is still inaccurate if Count is greater than (1LL << 53).
1122+
double Total =
1123+
static_cast<double>(R.ExecutionCount) + R.FalseExecutionCount;
11131124

11141125
// Display Line + Column.
11151126
std::string LineNoStr = utostr(uint64_t(R.LineStart));
@@ -1128,40 +1139,9 @@ void SourceCoverageViewHTML::renderBranchView(raw_ostream &OS, BranchView &BRV,
11281139
continue;
11291140
}
11301141

1131-
// Display TrueCount or TruePercent.
1132-
std::string TrueColor =
1133-
(R.TrueFolded || R.ExecutionCount ? "None" : "red branch");
1134-
std::string TrueCovClass =
1135-
(R.TrueFolded || R.ExecutionCount > 0 ? "covered-line"
1136-
: "uncovered-line");
1137-
1138-
if (R.TrueFolded)
1139-
OS << "Folded, ";
1140-
else {
1141-
OS << tag("span", "True", TrueColor) << ": ";
1142-
if (getOptions().ShowBranchCounts)
1143-
OS << tag("span", formatCount(R.ExecutionCount), TrueCovClass) << ", ";
1144-
else
1145-
OS << format("%0.2f", TruePercent) << "%, ";
1146-
}
1147-
1148-
// Display FalseCount or FalsePercent.
1149-
std::string FalseColor =
1150-
(R.FalseFolded || R.FalseExecutionCount ? "None" : "red branch");
1151-
std::string FalseCovClass =
1152-
(R.FalseFolded || R.FalseExecutionCount > 0 ? "covered-line"
1153-
: "uncovered-line");
1154-
1155-
if (R.FalseFolded)
1156-
OS << "Folded]\n";
1157-
else {
1158-
OS << tag("span", "False", FalseColor) << ": ";
1159-
if (getOptions().ShowBranchCounts)
1160-
OS << tag("span", formatCount(R.FalseExecutionCount), FalseCovClass)
1161-
<< "]\n";
1162-
else
1163-
OS << format("%0.2f", FalsePercent) << "%]\n";
1164-
}
1142+
OS << BranchCount("True", R.ExecutionCount, R.TrueFolded, Total) << ", "
1143+
<< BranchCount("False", R.FalseExecutionCount, R.FalseFolded, Total)
1144+
<< "]\n";
11651145
}
11661146
OS << EndPre;
11671147
OS << EndExpansionDiv;

llvm/tools/llvm-cov/SourceCoverageViewText.cpp

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,32 @@ void SourceCoverageViewText::renderBranchView(raw_ostream &OS, BranchView &BRV,
294294
if (getOptions().Debug)
295295
errs() << "Branch at line " << BRV.getLine() << '\n';
296296

297+
auto BranchCount = [&](StringRef Label, uint64_t Count, bool Folded,
298+
double Total) {
299+
if (Folded)
300+
return std::string{"Folded"};
301+
302+
std::string Str;
303+
raw_string_ostream OS(Str);
304+
305+
colored_ostream(OS, raw_ostream::RED, getOptions().Colors && !Count,
306+
/*Bold=*/false, /*BG=*/true)
307+
<< Label;
308+
309+
if (getOptions().ShowBranchCounts)
310+
OS << ": " << formatCount(Count);
311+
else
312+
OS << ": " << format("%0.2f", (Total != 0 ? 100.0 * Count / Total : 0.0))
313+
<< "%";
314+
315+
return Str;
316+
};
317+
297318
for (const auto &R : BRV.Regions) {
298-
double TruePercent = 0.0;
299-
double FalsePercent = 0.0;
300-
// FIXME: It may overflow when the data is too large, but I have not
301-
// encountered it in actual use, and not sure whether to use __uint128_t.
302-
uint64_t Total = R.ExecutionCount + R.FalseExecutionCount;
303-
304-
if (!getOptions().ShowBranchCounts && Total != 0) {
305-
TruePercent = ((double)(R.ExecutionCount) / (double)Total) * 100.0;
306-
FalsePercent = ((double)(R.FalseExecutionCount) / (double)Total) * 100.0;
307-
}
319+
// This can be `double` since it is only used as a denominator.
320+
// FIXME: It is still inaccurate if Count is greater than (1LL << 53).
321+
double Total =
322+
static_cast<double>(R.ExecutionCount) + R.FalseExecutionCount;
308323

309324
renderLinePrefix(OS, ViewDepth);
310325
OS << " Branch (" << R.LineStart << ":" << R.ColumnStart << "): [";
@@ -314,33 +329,9 @@ void SourceCoverageViewText::renderBranchView(raw_ostream &OS, BranchView &BRV,
314329
continue;
315330
}
316331

317-
if (R.TrueFolded)
318-
OS << "Folded, ";
319-
else {
320-
colored_ostream(OS, raw_ostream::RED,
321-
getOptions().Colors && !R.ExecutionCount,
322-
/*Bold=*/false, /*BG=*/true)
323-
<< "True";
324-
325-
if (getOptions().ShowBranchCounts)
326-
OS << ": " << formatCount(R.ExecutionCount) << ", ";
327-
else
328-
OS << ": " << format("%0.2f", TruePercent) << "%, ";
329-
}
330-
331-
if (R.FalseFolded)
332-
OS << "Folded]\n";
333-
else {
334-
colored_ostream(OS, raw_ostream::RED,
335-
getOptions().Colors && !R.FalseExecutionCount,
336-
/*Bold=*/false, /*BG=*/true)
337-
<< "False";
338-
339-
if (getOptions().ShowBranchCounts)
340-
OS << ": " << formatCount(R.FalseExecutionCount) << "]\n";
341-
else
342-
OS << ": " << format("%0.2f", FalsePercent) << "%]\n";
343-
}
332+
OS << BranchCount("True", R.ExecutionCount, R.TrueFolded, Total) << ", "
333+
<< BranchCount("False", R.FalseExecutionCount, R.FalseFolded, Total)
334+
<< "]\n";
344335
}
345336
}
346337

0 commit comments

Comments
 (0)