Skip to content

Commit 999abf2

Browse files
adsk-belleyboberpar
authored andcommitted
Adding the --include and --exclude options to lcov and geninfo
* The --include command-line option allows the user to specify a regular expression for the source files to be included. The command-line option can be repeated to specify multiple patterns. The coverage information is only included for the source files matching at least one of the patterns. The "lcov --capture --include" (or "geninfo --include") option is similar in functionality to the "lcov --extract" command-line option. But, by directly using applying the pattern while capturing coverage data one can often avoid having to run "lcov --extract" as a second pass. * The --exclude command-line option allows the user to specify a regular expression for the source files to be excluded. The command-line option can be repeated to specify multiple patterns. The coverage information is excluded for source files matching at least one of the patterns. The "lcov --capture --exclude" (or "geninfo --exclude") option is similar in functionality to the "lcov --extract" command-line option. But, by directly using applying the pattern while capturing coverage data one can often avoid having to run "lcov --remove" as a second pass. * On one of our code base at Autodesk, this speeds-up the generation of HTML code coverage reports by a factor of 3X. Signed-off-by: Benoit Belley <benoit.belley@autodesk.com>
1 parent b6a1136 commit 999abf2

File tree

4 files changed

+198
-1
lines changed

4 files changed

+198
-1
lines changed

bin/geninfo

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ our $UNNAMED_BLOCK = -1;
151151

152152
# Prototypes
153153
sub print_usage(*);
154+
sub transform_pattern($);
154155
sub gen_info($);
155156
sub process_dafile($$);
156157
sub match_filename($@);
@@ -239,6 +240,9 @@ our $config; # Configuration file contents
239240
our @ignore_errors; # List of errors to ignore (parameter)
240241
our @ignore; # List of errors to ignore (array)
241242
our $initial;
243+
our @include_patterns; # List of source file patterns to include
244+
our @exclude_patterns; # List of source file patterns to exclude
245+
our %excluded_files; # Files excluded due to include/exclude options
242246
our $no_recursion = 0;
243247
our $maxdepth;
244248
our $no_markers = 0;
@@ -382,11 +386,13 @@ if (!GetOptions("test-name|t=s" => \$test_name,
382386
"gcov-tool=s" => \$gcov_tool,
383387
"ignore-errors=s" => \@ignore_errors,
384388
"initial|i" => \$initial,
389+
"include=s" => \@include_patterns,
390+
"exclude=s" => \@exclude_patterns,
385391
"no-recursion" => \$no_recursion,
386392
"no-markers" => \$no_markers,
387393
"derive-func-data" => \$opt_derive_func_data,
388394
"debug" => \$debug,
389-
"external" => \$opt_external,
395+
"external|e" => \$opt_external,
390396
"no-external" => \$opt_no_external,
391397
"compat=s" => \$opt_compat,
392398
"config-file=s" => \$opt_config_file,
@@ -415,6 +421,16 @@ else
415421
$opt_external = 0;
416422
$opt_no_external = undef;
417423
}
424+
425+
if(@include_patterns) {
426+
# Need perlreg expressions instead of shell pattern
427+
@include_patterns = map({ transform_pattern($_); } @include_patterns);
428+
}
429+
430+
if(@exclude_patterns) {
431+
# Need perlreg expressions instead of shell pattern
432+
@exclude_patterns = map({ transform_pattern($_); } @exclude_patterns);
433+
}
418434
}
419435

420436
@data_directory = @ARGV;
@@ -622,12 +638,53 @@ sequentially.
622638
--config-file FILENAME Specify configuration file location
623639
--rc SETTING=VALUE Override configuration file setting
624640
--compat MODE=on|off|auto Set compat MODE (libtool, hammer, split_crc)
641+
--include PATTERN Include files matching PATTERN
642+
--exclude PATTERN Exclude files matching PATTERN
625643
626644
For more information see: $lcov_url
627645
END_OF_USAGE
628646
;
629647
}
630648

649+
650+
#
651+
# transform_pattern(pattern)
652+
#
653+
# Transform shell wildcard expression to equivalent Perl regular expression.
654+
# Return transformed pattern.
655+
#
656+
657+
sub transform_pattern($)
658+
{
659+
my $pattern = $_[0];
660+
661+
# Escape special chars
662+
663+
$pattern =~ s/\\/\\\\/g;
664+
$pattern =~ s/\//\\\//g;
665+
$pattern =~ s/\^/\\\^/g;
666+
$pattern =~ s/\$/\\\$/g;
667+
$pattern =~ s/\(/\\\(/g;
668+
$pattern =~ s/\)/\\\)/g;
669+
$pattern =~ s/\[/\\\[/g;
670+
$pattern =~ s/\]/\\\]/g;
671+
$pattern =~ s/\{/\\\{/g;
672+
$pattern =~ s/\}/\\\}/g;
673+
$pattern =~ s/\./\\\./g;
674+
$pattern =~ s/\,/\\\,/g;
675+
$pattern =~ s/\|/\\\|/g;
676+
$pattern =~ s/\+/\\\+/g;
677+
$pattern =~ s/\!/\\\!/g;
678+
679+
# Transform ? => (.) and * => (.*)
680+
681+
$pattern =~ s/\*/\(\.\*\)/g;
682+
$pattern =~ s/\?/\(\.\)/g;
683+
684+
return $pattern;
685+
}
686+
687+
631688
#
632689
# get_common_prefix(min_dir, filenames)
633690
#
@@ -743,6 +800,12 @@ sub gen_info($)
743800
process_dafile($file, $prefix);
744801
}
745802
}
803+
804+
# Report whether files were excluded.
805+
if (%excluded_files) {
806+
info("Excluded data for %d files due to include/exclude options\n",
807+
scalar keys %excluded_files);
808+
}
746809
}
747810

748811

@@ -1176,6 +1239,40 @@ sub process_dafile($$)
11761239
\@matches, \@gcov_content);
11771240
}
11781241

1242+
if (@include_patterns)
1243+
{
1244+
my $keep = 0;
1245+
1246+
foreach my $pattern (@include_patterns)
1247+
{
1248+
$keep ||= ($source_filename =~ (/^$pattern$/));
1249+
}
1250+
1251+
if (!$keep)
1252+
{
1253+
$excluded_files{$source_filename} = ();
1254+
unlink($gcov_file);
1255+
next;
1256+
}
1257+
}
1258+
1259+
if (@exclude_patterns)
1260+
{
1261+
my $exclude = 0;
1262+
1263+
foreach my $pattern (@exclude_patterns)
1264+
{
1265+
$exclude ||= ($source_filename =~ (/^$pattern$/));
1266+
}
1267+
1268+
if ($exclude)
1269+
{
1270+
$excluded_files{$source_filename} = ();
1271+
unlink($gcov_file);
1272+
next;
1273+
}
1274+
}
1275+
11791276
# Skip external files if requested
11801277
if (!$opt_external) {
11811278
if (is_external($source_filename)) {

bin/lcov

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ our $no_compat_libtool; # If set, indicates that libtool mode is to be disabled
166166
our $gcov_tool;
167167
our @opt_ignore_errors;
168168
our $initial;
169+
our @include_patterns; # List of source file patterns to include
170+
our @exclude_patterns; # List of source file patterns to exclude
169171
our $no_recursion = 0;
170172
our $to_package;
171173
our $from_package;
@@ -281,6 +283,8 @@ if (!GetOptions("directory|d|di=s" => \@directory,
281283
"gcov-tool=s" => \$gcov_tool,
282284
"ignore-errors=s" => \@opt_ignore_errors,
283285
"initial|i" => \$initial,
286+
"include=s" => \@include_patterns,
287+
"exclude=s" => \@exclude_patterns,
284288
"no-recursion" => \$no_recursion,
285289
"to-package=s" => \$to_package,
286290
"from-package=s" => \$from_package,
@@ -538,6 +542,8 @@ Options:
538542
--config-file FILENAME Specify configuration file location
539543
--rc SETTING=VALUE Override configuration file setting
540544
--compat MODE=on|off|auto Set compat MODE (libtool, hammer, split_crc)
545+
--include PATTERN Include files matching PATTERN
546+
--exclude PATTERN Exclude files matching PATTERN
541547
542548
For more information see: $lcov_url
543549
END_OF_USAGE
@@ -885,6 +891,12 @@ sub lcov_geninfo(@)
885891
if (defined($opt_config_file)) {
886892
@param = (@param, "--config-file", $opt_config_file);
887893
}
894+
foreach (@include_patterns) {
895+
@param = (@param, "--include", $_);
896+
}
897+
foreach (@exclude_patterns) {
898+
@param = (@param, "--exclude", $_);
899+
}
888900

889901
system(@param) and exit($? >> 8);
890902
}

man/geninfo.1

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ geninfo \- Generate tracefiles from .da files
4545
.br
4646
.RB [ \-\-rc
4747
.IR keyword = value ]
48+
.br
49+
.RB [ \-\-include
50+
.IR pattern ]
51+
.RB [ \-\-exclude
52+
.IR pattern ]
4853
.RE
4954
.SH DESCRIPTION
5055
.B geninfo
@@ -286,6 +291,28 @@ instead derive function coverage data from line coverage data and
286291
information about which lines belong to a function.
287292
.RE
288293

294+
.B \-\-exclude
295+
.I pattern
296+
.br
297+
.RS
298+
Exclude source files matching
299+
.IR pattern .
300+
301+
Use this switch if you want to exclude coverage data for a particular set
302+
of source files matching any of the given patterns. Multiple patterns can be
303+
specified by using multiple
304+
.B --exclude
305+
command line switches. The
306+
.I patterns
307+
will be interpreted as shell wildcard patterns (note that they may need to be
308+
escaped accordingly to prevent the shell from expanding them first).
309+
310+
Can be combined with the
311+
.B --include
312+
command line switch. If a given file matches both the include pattern and the
313+
exclude pattern, the exclude pattern will take precedence.
314+
.RE
315+
289316
.B \-\-external
290317
.br
291318
.B \-\-no\-external
@@ -324,6 +351,23 @@ Specify the location of the gcov tool.
324351
Print a short help text, then exit.
325352
.RE
326353

354+
.B \-\-include
355+
.I pattern
356+
.br
357+
.RS
358+
Include source files matching
359+
.IR pattern .
360+
361+
Use this switch if you want to include coverage data for only a particular set
362+
of source files matching any of the given patterns. Multiple patterns can be
363+
specified by using multiple
364+
.B --include
365+
command line switches. The
366+
.I patterns
367+
will be interpreted as shell wildcard patterns (note that they may need to be
368+
escaped accordingly to prevent the shell from expanding them first).
369+
.RE
370+
327371
.B \-\-ignore\-errors
328372
.I errors
329373
.br

man/lcov.1

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ lcov \- a graphical GCOV front\-end
5050
.RB [ \-\-compat
5151
.IR mode =on|off|auto]
5252
.br
53+
.RB [ \-\-include
54+
.IR pattern ]
55+
.RB [ \-\-exclude
56+
.IR pattern ]
57+
.br
5358
.RE
5459

5560
.B lcov
@@ -476,6 +481,28 @@ where the counter files ending with .da will be stored).
476481
Note that you may specify this option more than once.
477482
.RE
478483

484+
.B \-\-exclude
485+
.I pattern
486+
.br
487+
.RS
488+
Exclude source files matching
489+
.IR pattern .
490+
491+
Use this switch if you want to exclude coverage data for a particular set
492+
of source files matching any of the given patterns. Multiple patterns can be
493+
specified by using multiple
494+
.B --exclude
495+
command line switches. The
496+
.I patterns
497+
will be interpreted as shell wildcard patterns (note that they may need to be
498+
escaped accordingly to prevent the shell from expanding them first).
499+
500+
Can be combined with the
501+
.B --include
502+
command line switch. If a given file matches both the include pattern and the
503+
exclude pattern, the exclude pattern will take precedence.
504+
.RE
505+
479506
.B \-\-external
480507
.br
481508
.B \-\-no\-external
@@ -556,6 +583,23 @@ Specify the location of the gcov tool.
556583
Print a short help text, then exit.
557584
.RE
558585

586+
.B \-\-include
587+
.I pattern
588+
.br
589+
.RS
590+
Include source files matching
591+
.IR pattern .
592+
593+
Use this switch if you want to include coverage data for only a particular set
594+
of source files matching any of the given patterns. Multiple patterns can be
595+
specified by using multiple
596+
.B --include
597+
command line switches. The
598+
.I patterns
599+
will be interpreted as shell wildcard patterns (note that they may need to be
600+
escaped accordingly to prevent the shell from expanding them first).
601+
.RE
602+
559603
.B \-\-ignore\-errors
560604
.I errors
561605
.br

0 commit comments

Comments
 (0)