Skip to content

Commit 6b8acb3

Browse files
authored
Add 'unreachable region' feature. (#433)
This is similiar to 'excluded regions' - except that lcov will generate an 'unreachable' message if any coverpoints in the region are hit. Signed-off-by: Henry Cox <[email protected]>
1 parent 50d6597 commit 6b8acb3

File tree

10 files changed

+635
-135
lines changed

10 files changed

+635
-135
lines changed

lcovrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,15 @@ lcov_json_module = auto
510510
# override end of exclude region regexp
511511
#lcov_excl_stop = LCOV_EXCL_STOP
512512

513+
# override unreachable line default line exclusion regexp
514+
#lcov_unreachable_line = LCOV_UNREACHABLE_LINE
515+
516+
# override start of unreachable region regexp
517+
#lcov_unreachable_start = LCOV_UNREACHABLE_START
518+
519+
# override end of unreachable region regexp
520+
#lcov_unreachable_stop = LCOV_UNREACHABLE_STOP
521+
513522
# override start of branch exclude region regexp
514523
#lcov_excl_br_start = LCOV_EXCL_BR_START
515524

lib/lcovutil.pm

Lines changed: 216 additions & 62 deletions
Large diffs are not rendered by default.

man/genhtml.1

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,12 +2301,12 @@ Ignore line and branch coverpoints on lines which are out-of range/whose line nu
23012301
.PP
23022302

23032303
.IP region: 3
2304-
apply LCOV_EXCL_START/LCOV_EXCL_STOP directives found in source text to the coverpoints found in the current and baseline .info files.
2304+
apply LCOV_EXCL_START/LCOV_EXCL_STOP/LCOV_EXCL_LINE and LCOV_UNREACHABLE_START/LCOV_UNREACHABLE_STOP/LCOV_UNREACHABLE_LINE directives found in source text to the coverpoints found in the current and baseline .info files.
23052305
This option may be useful in cases that the source code was not found during 'lcov \-\-capture ...' but is accessible now.
23062306
.PP
23072307

23082308
.IP branch_region: 3
2309-
apply LCOV_EXCL_BR_START/LCOV_EXCL_BR_STOP directives found in source text to the coverpoints found in the current and baseline .info files.
2309+
apply LCOV_EXCL_BR_START/LCOV_EXCL_BR_STOP/LCOV_EXCL_BR_LINE directives found in source text to the coverpoints found in the current and baseline .info files.
23102310
This is similar to the 'region option, above - but applies to branch coverpoints only.
23112311
.PP
23122312

@@ -3130,6 +3130,20 @@ Coverage data for a particular line cannot be found, possibly because the source
31303130
This can happen if the source file used in HTML generation is not the same as the file used to generate the coverage data - for example, lines have been added or removed.
31313131
.PP
31323132

3133+
.IP unreachable: 3
3134+
a coverpoint (line, branch, function, or MC/DC) within an "unreachable" region is executed (hit); either the code, directive placement, or both are wrong.
3135+
If the error is ignored, the offending coverpoint is retained (not excluded) or not, depending on the value of the
3136+
.I retain_unreachable_coverpoints_if_executed
3137+
configuration parameter.
3138+
See man
3139+
.B lcovrc(5)
3140+
and the
3141+
.I "Exclusion markers"
3142+
section of man
3143+
.B geninfo(1)
3144+
for more information.
3145+
.PP
3146+
31333147
.IP unsupported: 3
31343148
The requested feature is not supported for this tool configuration. For example, function begin/end line range exclusions use some GCOV features that are not available in older GCC releases.
31353149
.PP

man/geninfo.1

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,29 @@ man page for more information on .gcda, .gcno and .gcov output formats.
233233
.SS "Exclusion markers"
234234

235235
To exclude specific lines of code from a tracefile, you can add exclusion
236-
markers to the source code. Additionally you can exclude specific branches from
237-
branch coverage without excluding the involved lines from line and function
238-
coverage. Exclusion markers are keywords which can for example be added in the
236+
markers to the source code. Similarly, you can mark specific regions of code
237+
as "unreachable". An "unreachable" error message is generated if any
238+
coverpoints in unreachable regions are executed (
239+
.I i.e.,
240+
have non-zero hit counts.
241+
See the
242+
.I retain_unreachable_coverpoints_if_executed
243+
section in man
244+
.B lcovrc(1)
245+
for a description of the actions taken in this case.
246+
247+
Additionally you can exclude specific branches or MC/DC expressions from
248+
without excluding the involved lines from line and function
249+
coverage.
250+
251+
Exclusion markers are keywords which can for example be added in the
239252
form of a comment.
240253
See man
241254
.BR lcovrc (5)
242-
how to override some of them.
255+
how to override the exclusion keywords (
256+
.I e.g.,
257+
to reuse markers inserted for other tools or to generate reports with different
258+
sets of excluded regions).
243259

244260
The following markers are recognized by geninfo:
245261

@@ -266,6 +282,34 @@ section.
266282
.br
267283
.RE
268284

285+
.B LCOV_UNREACHABLE_LINE
286+
.br
287+
.RS
288+
If the marked line is 'hit', then generate an error: we believe the marked code
289+
is unreachable and so there is a bug in the code, the plaement of the directive, or both.
290+
Lines containing this marker will be excluded from reporting.
291+
.br
292+
Apart from error reporting, this directive is equivalent to
293+
.I LCOV_EXCL_LINE.
294+
.RE
295+
296+
.B LCOV_UNREACHABLE_START
297+
.br
298+
.RS
299+
Marks the beginning of an unreachable section of code. The current line in part of this region.
300+
.br
301+
As described in the
302+
.I LCOV_UNREACHABLE_LINE
303+
section, above: an error is generated if any code in the region is hit, but the code is excluded from reporting.
304+
.RE
305+
306+
.B LCOV_UNREACHABLE_STOP
307+
.br
308+
.RS
309+
Marks the end of the region of unreachable code. The current line not part of this
310+
section.
311+
.RE
312+
269313
.B LCOV_EXCL_BR_LINE
270314
.br
271315
.RS
@@ -319,7 +363,9 @@ The current line not part of this section
319363
In general, (almost) all
320364
.B geninfo
321365
options can also be specified in your personal, group, project, or site
322-
configuration file - see man
366+
configuration file - see the
367+
.I \-\-config\-file
368+
section, below, and man
323369
.B lcovrc(5)
324370
for details.
325371

@@ -1013,7 +1059,19 @@ data.
10131059
.IP source: 3
10141060
the source code file for a data set could not be found.
10151061
.PP
1016-
`<
1062+
1063+
.IP unreachable: 3
1064+
a coverpoint (line, branch, function, or MC/DC) within an "unreachable" region is executed (hit); either the code, directive placement, or both are wrong.
1065+
If the error is ignored, the offending coverpoint is retained (not excluded) or not, depending on the value of the
1066+
.I retain_unreachable_coverpoints_if_executed
1067+
configuration parameter.
1068+
See man
1069+
.B lcovrc(5)
1070+
and the
1071+
.I "Exclusion markers"
1072+
section, above.
1073+
.PP
1074+
10171075
.IP unsupported: 3
10181076
the requested feature is not supported for this tool configuration. For example, function begin/end line range exclusions use some GCOV features that are not available in older GCC releases.
10191077
.PP

man/lcov.1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,20 @@ data.
14451445
the source code file for a data set could not be found.
14461446
.PP
14471447

1448+
.IP unreachable: 3
1449+
a coverpoint (line, branch, function, or MC/DC) within an "unreachable" region is executed (hit); either the code, directive placement, or both are wrong.
1450+
If the error is ignored, the offending coverpoint is retained (not excluded) or not, depending on the value of the
1451+
.I retain_unreachable_coverpoints_if_executed
1452+
configuration parameter.
1453+
See man
1454+
.B lcovrc(5)
1455+
and the
1456+
.I "Exclusion markers"
1457+
section of man
1458+
.B geninfo(1)
1459+
for more information.
1460+
.PP
1461+
14481462
.IP unsupported: 3
14491463
the requested feature is not supported for this tool configuration. For example, function begin/end line range exclusions use some GCOV features that are not available in older GCC releases.
14501464
.PP

man/lcovrc.5

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,24 @@ maximum during parallel processing.
714714
#lcov_excl_stop = LCOV_EXCL_STOP
715715
.br
716716

717+
.br
718+
# override unreachable line default line exclusion regexp
719+
.br
720+
#lcov_unreachable_line = LCOV_UNREACHABLE_LINE
721+
.br
722+
.br
723+
# override start of unreachable region regexp
724+
.br
725+
#lcov_unreachable_start = LCOV_UNREACHABLE_START
726+
.br
727+
728+
.br
729+
# override end of unreachable region regexp
730+
.br
731+
#lcov_unreachable_stop = LCOV_UNREACHABLE_STOP
732+
.br
733+
734+
717735
.br
718736
# override start of branch exclude region regexp
719737
.br
@@ -2551,6 +2569,37 @@ Specify the regexp used to mark the end of a region where exception-related bran
25512569

25522570
Default is 'LCOV_EXCL_EXECEPTION_BR_STOP'.
25532571

2572+
.BR lcov_unreachable_line " ="
2573+
.I expression
2574+
.IP
2575+
Specify the regular expression of unreachable line which should be excluded from reporting, but should generate an "inconsistent" error if hit.
2576+
That is: we believe that the marked code is unreachable, so there is a bug in the code, the placement of the directive, or both if the "unreachable" code is executed.
2577+
Line, branch, and function coverpoints are associated with lines where this regexp is found are dropped.
2578+
.br
2579+
2580+
Default is 'LCOV_UNREACHABLE_LINE'.
2581+
.PP
2582+
2583+
.BR lcov_unreachable_start " ="
2584+
.IR expression
2585+
.IP
2586+
Specify the regexp mark the start of an unreachable code block.
2587+
All coverpoints within exception regions are dropped, but the tool will generate
2588+
an "inconsistent" error if any code in the block is executed.
2589+
.br
2590+
2591+
Default is 'LCOV_UNREACHABLE_START'.
2592+
2593+
.PP
2594+
2595+
.BR lcov_unreachable_stop " ="
2596+
.IR expression
2597+
.IP
2598+
Specify the regexp mark the end of the unreachable code block.
2599+
.br
2600+
2601+
Default is 'LCOV_UNREACHABLE_STOP'.
2602+
25542603
.PP
25552604

25562605
.BR fail_under_branches " ="
@@ -2565,7 +2614,21 @@ for more detailes.
25652614

25662615
.br
25672616
The default is 0 (no threshold).
2617+
.PP
2618+
2619+
.BR retain_unreachable_coverpoints_if_executed " ="
2620+
.I [0 | 1]
2621+
.IP
2622+
Specify whether coverpoints in "unreachable" regions which are 'hit' are
2623+
dropped (0) - because the region is excluded - or retained (1) - because
2624+
the directive appears to be incorrect.
2625+
.br
2626+
See the "Exclusion markers" section in man
2627+
.B geninfo(1)
2628+
for more details.
2629+
25682630

2631+
The default is 1 (retain the coverpoints).
25692632
.PP
25702633

25712634
.BR fail_under_lines " ="

tests/lcov/exception/exception.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ if [ $NO_INITIAL_CAPTURE != $? ] ; then
4848
fi
4949
fi
5050
51+
# enable branch filter without line/region filter - to hit some additional code
52+
$COVER $CAPTURE $LCOV_OPTS --initial -o initial_br.info $IGNORE_EMPTY $IGNORE_USAGE --filter branch_region
53+
if [ $NO_INITIAL_CAPTURE != $? ] ; then
54+
echo "Error: unexpected error code from lcov --initial"
55+
if [ $KEEP_GOING == 0 ] ; then
56+
exit 1
57+
fi
58+
fi
59+
for info in initial.info initial_br.info ; do
60+
grep 'BRDA:8,e0' $info
61+
if [ 0 == $? ] ; then
62+
echo "Error: exception branch should be filtered out of $info"
63+
if [ $KEEP_GOING == 0 ] ; then
64+
exit 1
65+
fi
66+
fi
67+
done
68+
5169
./a.out
5270
if [ 0 != $? ] ; then
5371
echo "Error: unexpected error return from a.out"

tests/lcov/extract/extract.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <iostream>
44
#include <string>
55

6-
int main(int argc, const char *argv[])
6+
int main(int argc, const char *argv[]) // TEST_UNREACH_FUNCTION
77
{
88
bool b = false;
99
if (strcmp(argv[1], "1") == 0)
@@ -18,17 +18,19 @@ int main(int argc, const char *argv[])
1818

1919
// TEST_OVERLAP_START
2020
// TEST_OVERLAP_START
21+
// TEST_UNREACHABLE_START
2122
std::string str("asdads");
23+
// TEST_UNREACHABLE_END
2224
str = "cd";
2325
// TEST_OVERLAP_END
2426

2527
//TEST_DANGLING_START
2628
//TEST_UNMATCHED_END
2729

28-
std::cout << str << std::endl;
30+
std::cout << str << std::endl; // TEST_UNREACHABLE_LINE
2931

30-
// LCOV_EXCL_START
32+
// LCOV_EXCL_START_1
3133
std::cout << "adding some code to ignore" << std::endl;
32-
// LCOV_EXCL_STOP
34+
// LCOV_EXCL_STOP_1
3335
return 0;
3436
}

0 commit comments

Comments
 (0)