Skip to content

Commit 75a42b8

Browse files
committed
Cleaner 'lcov --list' implementation.
Make testcase insensitive to compiler output differences. Signed-off-by: Henry Cox <[email protected]>
1 parent 8261ed8 commit 75a42b8

File tree

4 files changed

+59
-78
lines changed

4 files changed

+59
-78
lines changed

bin/lcov

Lines changed: 42 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,16 +1722,7 @@ sub list()
17221722
my $readSourceFile = ReadCurrentSource->new();
17231723
my $data =
17241724
TraceFile->load($list, $readSourceFile, $lcovutil::verify_checksum);
1725-
my $total_found = 0;
1726-
my $total_hit = 0;
1727-
my $fn_total_found = 0;
1728-
my $fn_total_hit = 0;
1729-
my $br_total_found = 0;
1730-
my $br_total_hit = 0;
1731-
my $total_mcdc = 0;
1732-
my $total_mcdc_hit = 0;
1733-
my $strlen = length("Filename");
1734-
my @footer;
1725+
my $strlen = length("Filename");
17351726
my $lastpath;
17361727
my $F_LN_NUM = 0;
17371728
my $F_LN_RATE = 1;
@@ -1812,24 +1803,36 @@ sub list()
18121803
my $heading2 = sprintf("%-*s|", $w, "Filename");
18131804
my $barlen = $w + 1;
18141805

1815-
foreach my $d (['Lines', $F_LN_NUM, $F_LN_RATE],
1816-
['Functions', $F_FN_NUM, $F_FN_RATE],
1817-
['Branches', $F_BR_NUM, $F_BR_RATE],
1818-
['MC/DC', $F_MCDC_NUM, $F_MCDC_RATE],
1819-
) {
1820-
my ($type, $n, $r) = @$d;
1806+
# name, total_found, total_hit, total_column_width, rate_column_width
1807+
my @types = (['Lines', \&TraceInfo::sum, 0, 0, $F_LN_NUM, $F_LN_RATE],
1808+
[$lcovutil::func_coverage ? 'Functions' : undef,
1809+
\&TraceInfo::func, 0, 0, $F_FN_NUM, $F_FN_RATE
1810+
],
1811+
[$lcovutil::br_coverage ? 'Branches' : undef,
1812+
\&TraceInfo::sumbr, 0, 0, $F_BR_NUM, $F_BR_RATE
1813+
],
1814+
[$lcovutil::mcdc_coverage ? 'MC/DC' : undef,
1815+
\&TraceInfo::mcdc, 0, 0, $F_MCDC_NUM, $F_MCDC_RATE
1816+
],);
1817+
1818+
my $sep = '';
1819+
foreach my $d (@types) {
1820+
my ($type, $cb, $found, $hit, $n, $r) = @$d;
1821+
next unless $type;
18211822
$w = $fwidth[$r];
1822-
$format .= "%${w}s ";
1823-
$heading1 .= sprintf("%-*s |", $w + $fwidth[$n], $type);
1824-
$heading2 .= sprintf("%-*s ", $w, "Rate");
1823+
$format .= "$sep%${w}s ";
1824+
$heading1 .= sprintf("$sep%-*s ", $w + $fwidth[$n], $type);
1825+
$heading2 .= sprintf("$sep%-*s ", $w, "Rate");
18251826
$barlen += $w + 1;
18261827
# Number of coverpoints
18271828
$w = $fwidth[$n];
1828-
$format .= "%${w}s|";
1829-
$heading2 .= sprintf("%*s|", $w, "Num");
1829+
$format .= "%${w}s";
1830+
$heading2 .= sprintf("%*s", $w, "Num");
18301831
$barlen += $w + 1;
1832+
$sep = '|';
18311833
}
1832-
# Line end
1834+
--$barlen; # no separator for last column
1835+
# Line end
18331836
$format .= "\n";
18341837
$heading1 .= "\n";
18351838
$heading2 .= "\n";
@@ -1869,40 +1872,19 @@ sub list()
18691872
}
18701873
$print_filename = shorten_filename($print_filename, $strlen);
18711874
}
1872-
my $lineData = $entry->sum();
1873-
my $branchData = $entry->sumbr();
1874-
my $funcData = $entry->func();
1875-
my $mcdcData = $entry->mcdc();
1876-
my ($found, $hit, $br_found, $br_hit, $fn_found, $fn_hit, $mcdc_found,
1877-
$mcdc_hit)
1878-
= ($lineData->found(), $lineData->hit(),
1879-
$branchData->found(), $branchData->hit(),
1880-
$funcData->numFunc(), $funcData->numHit(),
1881-
$mcdcData->found(), $mcdcData->hit());
1882-
1883-
# Add line coverage totals
1884-
$total_found += $found;
1885-
$total_hit += $hit;
1886-
# Add function coverage totals
1887-
$fn_total_found += $fn_found;
1888-
$fn_total_hit += $fn_hit;
1889-
# Add branch coverage totals
1890-
$br_total_found += $br_found;
1891-
$br_total_hit += $br_hit;
1892-
$total_mcdc += $mcdc_found;
1893-
$total_mcdc_hit += $mcdc_hit;
1894-
18951875
my @file_data;
18961876
push(@file_data, $print_filename);
1897-
foreach my $d ([$hit, $found, $F_LN_RATE, $F_LN_NUM],
1898-
[$fn_hit, $fn_found, $F_FN_RATE, $F_FN_NUM],
1899-
[$br_hit, $br_found, $F_BR_RATE, $F_BR_NUM],
1900-
[$mcdc_hit, $mcdc_found, $F_MCDC_RATE, $F_MCDC_NUM],
1901-
) {
1902-
my ($h, $f, $r, $n) = @$d;
1903-
1904-
push(@file_data, shorten_rate($h, $f, $fwidth[$r]));
1905-
push(@file_data, shorten_number($f, $fwidth[$n]));
1877+
foreach my $d (@types) {
1878+
my ($type, $cb, $total_found, $total_hit, $n, $r) = @$d;
1879+
next unless defined($type);
1880+
my $data = &{$cb}($entry);
1881+
1882+
my ($found, $hit) = $data->get_found_and_hit();
1883+
# add to totals
1884+
$d->[2] += $found, $d->[3] += $hit;
1885+
1886+
push(@file_data, shorten_rate($hit, $found, $fwidth[$r]));
1887+
push(@file_data, shorten_number($found, $fwidth[$n]));
19061888
}
19071889
# Print assembled line
19081890
printf($format, @file_data);
@@ -1912,19 +1894,15 @@ sub list()
19121894
print(("=" x $barlen) . "\n");
19131895

19141896
# Assemble line parameters
1897+
my @footer;
19151898
push(@footer, sprintf("%*s", $strlen, "Total:"));
1899+
foreach my $d (@types) {
1900+
my ($type, $cb, $total_found, $total_hit, $n, $r) = @$d;
1901+
next unless defined($type);
19161902

1917-
foreach my $d ([$total_hit, $total_found, $F_LN_RATE, $F_LN_NUM],
1918-
[$fn_total_hit, $fn_total_found, $F_FN_RATE, $F_FN_NUM],
1919-
[$br_total_hit, $br_total_found, $F_BR_RATE, $F_BR_NUM],
1920-
[$total_mcdc_hit, $total_mcdc, $F_MCDC_RATE, $F_MCDC_NUM],
1921-
) {
1922-
my ($h, $f, $r, $n) = @$d;
1923-
1924-
push(@footer, shorten_rate($h, $f, $fwidth[$r]));
1925-
push(@footer, shorten_number($f, $fwidth[$n]));
1903+
push(@footer, shorten_rate($total_hit, $total_found, $fwidth[$r]));
1904+
push(@footer, shorten_number($total_found, $fwidth[$n]));
19261905
}
1927-
19281906
# Print assembled line
19291907
printf($format, @footer);
19301908
}

tests/lcov/extract/extract.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,15 @@ $COVER $CAPTURE . $LCOV_OPTS --no-external -o internal.info
392392
393393
# substiture PWD so the test isn't dependent on directory layout.
394394
# quiet, to suppress core count and (empty) message summary
395-
$COVER $LCOV_TOOL $LCOV_OPTS --list internal.info --subst "s#$PWD#.#" -q -q > list.dat
395+
$COVER $LCOV_TOOL $LCOV_OPTS --list internal.info --subst "s#$PWD#.#" -q -q --filter function > list.dat
396396
397397
if [ "$ENABLE_MCDC" == 1 ] ; then
398398
diff list.dat list_mcdc.gold
399399
else
400-
diff list.dat list.gold
400+
# substitute the actual numbers - to become insensitive to compiler version
401+
# which produce different numbers of coverpoints
402+
sed -E 's/[1-9][0-9]*\b/N/g' list.dat > munged.dat
403+
diff munged.dat list.gold
401404
fi
402405
if [ 0 != $? ] ; then
403406
echo "Error: unexpected list difference"

tests/lcov/extract/list.gold

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
|Lines |Functions |Branches |MC/DC |
2-
Filename |Rate Num|Rate Num|Rate Num|Rate Num|
3-
====================================================================
1+
|Lines |Functions |Branches
2+
Filename |Rate Num|Rate Num|Rate Num
3+
======================================================
44
[./]
5-
extract.cpp | 100% 13| 100% 3|50.0% 20| - 0|
6-
====================================================================
7-
Total:| 100% 13| 100% 3|50.0% 20| - 0|
5+
extract.cpp | N% N| N% N|N.0% N
6+
======================================================
7+
Total:| N% N| N% N|N.0% N

tests/lcov/extract/list_mcdc.gold

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
|Lines |Functions |Branches |MC/DC |
2-
Filename |Rate Num|Rate Num|Rate Num|Rate Num|
3-
====================================================================
1+
|Lines |Functions |Branches |MC/DC
2+
Filename |Rate Num|Rate Num|Rate Num|Rate Num
3+
===================================================================
44
[./]
5-
extract.cpp | 100% 13| 100% 1|50.0% 16|50.0% 6|
6-
====================================================================
7-
Total:| 100% 13| 100% 1|50.0% 16|50.0% 6|
5+
extract.cpp | 100% 13| 100% 1|50.0% 16|50.0% 6
6+
===================================================================
7+
Total:| 100% 13| 100% 1|50.0% 16|50.0% 6

0 commit comments

Comments
 (0)