Debian Codegraph @

lib/lcovutil.pm

raw
1 # some common utilities for lcov-related scripts
2
3 use strict;
4 use warnings;
5 require Exporter;
6
7 package lcovutil;
8
9 use File::Path qw(rmtree);
10 use File::Basename qw(basename dirname);
11 use File::Temp qw /tempdir/;
12 use File::Spec;
13 use Scalar::Util qw/looks_like_number/;
14 use Cwd qw/abs_path getcwd/;
15 use Storable qw(dclone);
16 use Capture::Tiny;
17 use Module::Load::Conditional qw(check_install);
18 use Digest::MD5 qw(md5_base64);
19 use FindBin;
20 use Getopt::Long;
21 use DateTime;
22 use Config;
23 use POSIX;
24 use Fcntl qw(:flock SEEK_END);
25
26 our @ISA = qw(Exporter);
27 our @EXPORT_OK = qw($tool_name $tool_dir $lcov_version $lcov_url $VERSION
28 @temp_dirs set_tool_name
29 info warn_once set_info_callback init_verbose_flag $verbose
30 debug $debug
31 append_tempdir create_temp_dir temp_cleanup folder_is_empty $tmp_dir $preserve_intermediates
32 summarize_messages define_errors
33 parse_ignore_errors ignorable_error ignorable_warning
34 is_ignored message_count explain_once
35 die_handler warn_handler abort_handler
36
37 $maxParallelism $maxMemory init_parallel_params current_process_size
38 $memoryPercentage $max_fork_fails $fork_fail_timeout
39 save_profile merge_child_profile save_cmd_line
40
41 @opt_rc apply_rc_params $split_char parseOptions
42 strip_directories
43 @file_subst_patterns subst_file_name
44 @comments
45
46 $br_coverage $func_coverage $mcdc_coverage
47 @cpp_demangle do_mangle_check $demangle_cpp_cmd
48 $cpp_demangle_tool $cpp_demangle_params
49 get_overall_line rate
50
51 $FILTER_BRANCH_NO_COND $FILTER_FUNCTION_ALIAS
52 $FILTER_EXCLUDE_REGION $FILTER_EXCLUDE_BRANCH $FILTER_LINE
53 $FILTER_LINE_CLOSE_BRACE $FILTER_BLANK_LINE $FILTER_LINE_RANGE
54 $FILTER_TRIVIAL_FUNCTION $FILTER_DIRECTIVE
55 $FILTER_MISSING_FILE $FILTER_INITIALIZER_LIST
56 $FILTER_EXCEPTION_BRANCH $FILTER_ORPHAN_BRANCH
57 @cov_filter
58 $EXCL_START $EXCL_STOP $EXCL_BR_START $EXCL_BR_STOP
59 $EXCL_EXCEPTION_BR_START $EXCL_EXCEPTION_BR_STOP
60 $EXCL_LINE $EXCL_BR_LINE $EXCL_EXCEPTION_LINE
61 @exclude_file_patterns @include_file_patterns %excluded_files
62 @omit_line_patterns @exclude_function_patterns $case_insensitive
63 munge_file_patterns warn_file_patterns transform_pattern
64 parse_cov_filters summarize_cov_filters
65 disable_cov_filters reenable_cov_filters is_filter_enabled
66 filterStringsAndComments simplifyCode balancedParens
67 set_extensions
68 $source_filter_lookahead $source_filter_bitwise_are_conditional
69 $exclude_exception_branch
70 $derive_function_end_line $derive_function_end_line_all_files
71 $trivial_function_threshold
72 $filter_blank_aggressive
73
74 $lcov_filter_parallel $lcov_filter_chunk_size
75
76 %lcovErrors $ERROR_GCOV $ERROR_SOURCE $ERROR_GRAPH $ERROR_MISMATCH
77 $ERROR_BRANCH $ERROR_EMPTY $ERROR_FORMAT $ERROR_VERSION $ERROR_UNUSED
78 $ERROR_PACKAGE $ERROR_CORRUPT $ERROR_NEGATIVE $ERROR_COUNT $ERROR_PATH
79 $ERROR_UNSUPPORTED $ERROR_DEPRECATED $ERROR_INCONSISTENT_DATA
80 $ERROR_CALLBACK $ERROR_RANGE $ERROR_UTILITY $ERROR_USAGE $ERROR_INTERNAL
81 $ERROR_PARALLEL $ERROR_PARENT $ERROR_CHILD $ERROR_FORK
82 $ERROR_EXCESSIVE_COUNT $ERROR_MISSING
83 report_parallel_error report_exit_status check_parent_process
84 report_unknown_child
85
86 $ERROR_UNMAPPED_LINE $ERROR_UNKNOWN_CATEGORY $ERROR_ANNOTATE_SCRIPT
87 $stop_on_error
88
89 @extractVersionScript $verify_checksum $compute_file_version
90
91 configure_callback cleanup_callbacks
92
93 is_external @internal_dirs $opt_no_external @build_directory
94 $default_precision check_precision
95
96 system_no_output $devnull $dirseparator
97
98 %tlaColor %tlaTextColor use_vanilla_color %pngChar %pngMap
99 %dark_palette %normal_palette parse_w3cdtf
100 );
101
102 our @ignore;
103 our @message_count;
104 our @expected_message_count;
105 our %message_types;
106 our $message_log;
107 our $message_filename;
108 our $suppressAfter = 100; # stop warning after this number of messages
109 our %ERROR_ID;
110 our %ERROR_NAME;
111 our $tool_dir = "$FindBin::RealBin";
112 our $tool_name = basename($0); # import from lcovutil module
113 our $VERSION = `"$tool_dir"/get_version.sh --full`;
114 chomp($VERSION);
115 our $lcov_version = 'LCOV version ' . $VERSION;
116 our $lcov_url = "https://github.com//linux-test-project/lcov";
117 our @temp_dirs;
118 our $tmp_dir = '/tmp'; # where to put temporary/intermediate files
119 our $preserve_intermediates; # this is useful only for debugging
120 our $sort_inputs; # sort input file lists - to reduce unpredictability
121 our $devnull = File::Spec->devnull(); # portable way to do it
122 our $dirseparator = ($^O =~ /Win/) ? '\\' : '/';
123 our $interp = ($^O =~ /Win/) ? $^X : undef;
124
125 our $debug = 0; # if set, emit debug messages
126 our $verbose = 0; # default level - higher to enable additional logging
127
128 our $split_char = ','; # by default: split on comma
129
130 # share common definition for all error types.
131 # Note that geninfo cannot produce some types produced by genhtml, and vice
132 # versa. Easier to maintain a common definition.
133 our $ERROR_GCOV;
134 our $ERROR_SOURCE;
135 our $ERROR_GRAPH;
136 our $ERROR_FORMAT; # bad record in .info file
137 our $ERROR_EMPTY; # no records found in info file
138 our $ERROR_VERSION;
139 our $ERROR_UNUSED; # exclude/include/substitute pattern not used
140 our $ERROR_MISMATCH;
141 our $ERROR_BRANCH; # branch numbering is not correct
142 our $ERROR_PACKAGE; # missing utility package
143 our $ERROR_CORRUPT; # corrupt file
144 our $ERROR_NEGATIVE; # unexpected negative count in coverage data
145 our $ERROR_COUNT; # too many messages of type
146 our $ERROR_UNSUPPORTED; # some unsupported feature or usage
147 our $ERROR_PARALLEL; # error in fork/join
148 our $ERROR_DEPRECATED; # deprecated feature
149 our $ERROR_CALLBACK; # callback produced an error
150 our $ERROR_INCONSISTENT_DATA; # something wrong with .info
151 our $ERROR_RANGE; # line number out of range
152 our $ERROR_UTILITY; # some tool failed - e.g., 'find'
153 our $ERROR_USAGE; # misusing some feature
154 our $ERROR_PATH; # path issues
155 our $ERROR_INTERNAL; # tool issue
156 our $ERROR_PARENT; # parent went away so child should die
157 our $ERROR_CHILD; # nonzero child exit status
158 our $ERROR_FORK; # fork failed
159 our $ERROR_EXCESSIVE_COUNT; # suspiciously large hit count
160 our $ERROR_MISSING; # file missing/not found
161 # genhtml errors
162 our $ERROR_UNMAPPED_LINE; # inconsistent coverage data
163 our $ERROR_UNKNOWN_CATEGORY; # we did something wrong with inconsistent data
164 our $ERROR_ANNOTATE_SCRIPT; # annotation failed somehow
165
166 my @lcovErrs = (["annotate", \$ERROR_ANNOTATE_SCRIPT],
167 ["branch", \$ERROR_BRANCH],
168 ["callback", \$ERROR_CALLBACK],
169 ["category", \$ERROR_UNKNOWN_CATEGORY],
170 ["child", \$ERROR_CHILD],
171 ["corrupt", \$ERROR_CORRUPT],
172 ["count", \$ERROR_COUNT],
173 ["deprecated", \$ERROR_DEPRECATED],
174 ["empty", \$ERROR_EMPTY],
175 ['excessive', \$ERROR_EXCESSIVE_COUNT],
176 ["format", \$ERROR_FORMAT],
177 ["fork", \$ERROR_FORK],
178 ["gcov", \$ERROR_GCOV],
179 ["graph", \$ERROR_GRAPH],
180 ["inconsistent", \$ERROR_INCONSISTENT_DATA],
181 ["internal", \$ERROR_INTERNAL],
182 ["mismatch", \$ERROR_MISMATCH],
183 ["missing", \$ERROR_MISSING],
184 ["negative", \$ERROR_NEGATIVE],
185 ["package", \$ERROR_PACKAGE],
186 ["parallel", \$ERROR_PARALLEL],
187 ["parent", \$ERROR_PARENT],
188 ["path", \$ERROR_PATH],
189 ["range", \$ERROR_RANGE],
190 ["source", \$ERROR_SOURCE],
191 ["unmapped", \$ERROR_UNMAPPED_LINE],
192 ["unsupported", \$ERROR_UNSUPPORTED],
193 ["unused", \$ERROR_UNUSED],
194 ['usage', \$ERROR_USAGE],
195 ['utility', \$ERROR_UTILITY],
196 ["version", \$ERROR_VERSION],);
197
198 our %lcovErrors;
199
200 our $stop_on_error; # attempt to keep going
201 our $treat_warning_as_error = 0;
202 our $warn_once_per_file = 1;
203 our $excessive_count_threshold; # default not set: don't check
204
205 our $br_coverage = 0; # If set, generate branch coverage statistics
206 our $mcdc_coverage = 0; # MC/DC
207 our $func_coverage = 1; # If set, generate function coverage statistics
208
209 # for external file filtering
210 our @internal_dirs;
211 our $opt_no_external;
212
213 # Where code was built/where .gcno files can be found
214 # (if .gcno files are in a different place than the .gcda files)
215 # also used by genhtml to match diff file entries to .info file
216 our @build_directory;
217
218 our @configured_callbacks;
219
220 # optional callback to keep track of whatever user decides is important
221 our @contextCallback;
222 our $contextCallback;
223
224 # filename substitutions
225 our @file_subst_patterns;
226 # resolve callback
227 our @resolveCallback;
228 our $resolveCallback;
229 our %resolveCache;
230
231 # C++ demangling
232 our @cpp_demangle; # the options passed in
233 our $demangle_cpp_cmd; # the computed command string
234 # deprecated: demangler for C++ function names is c++filt
235 our $cpp_demangle_tool;
236 # Deprecated: prefer -Xlinker approach with @cpp_dmangle_tool
237 our $cpp_demangle_params;
238
239 # version extract may be expensive - so only do it once
240 our %versionCache;
241 our @extractVersionScript; # script/callback to find version ID of file
242 our $versionCallback;
243 our $verify_checksum; # compute and/or check MD5 sum of source code lines
244
245 our $check_file_existence_before_callback = 1;
246 our $check_data_consistency = 1;
247
248 # Specify coverage rate default precision
249 our $default_precision = 1;
250
251 # undef indicates not set by command line or RC option - so default to
252 # sequential processing
253 our $maxParallelism;
254 our $max_fork_fails = 5; # consecutive failures
255 our $fork_fail_timeout = 10; # how long to wait, in seconds
256 our $maxMemory; # zero indicates no memory limit to parallelism
257 our $memoryPercentage;
258 our $in_child_process = 0;
259 our $max_tasks_per_core = 20; # maybe default to 0?
260
261 our $lcov_filter_parallel = 1; # enable by default
262 our $lcov_filter_chunk_size;
263
264 our $fail_under_lines;
265 our $fail_under_branches;
266
267 our $fix_inconsistency = 1;
268
269 sub default_info_impl(@);
270
271 our $info_callback = \&default_info_impl;
272
273 # filter classes that may be requested
274 # don't report BRDA data for line which seem to have no conditionals
275 # These may be from C++ exception handling (for example) - and are not
276 # interesting to users.
277 our $FILTER_BRANCH_NO_COND;
278 # don't report line coverage for closing brace of a function
279 # or basic block, if the immediate predecessor line has the same count.
280 our $FILTER_LINE_CLOSE_BRACE;
281 # merge functions which appear on same file/line - guess that that
282 # they are all the same
283 our $FILTER_FUNCTION_ALIAS;
284 # region between LCOV EXCL_START/STOP
285 our $FILTER_EXCLUDE_REGION;
286 # region between LCOV EXCL_BR_START/STOP
287 our $FILTER_EXCLUDE_BRANCH;
288 # empty line
289 our $FILTER_BLANK_LINE;
290 # out of range line - beyond end of file
291 our $FILTER_LINE_RANGE;
292 # backward compatibility: empty line, close brace
293 our $FILTER_LINE;
294 # filter initializer list-like stuff
295 our $FILTER_INITIALIZER_LIST;
296 # remove functions which have only a single line
297 our $FILTER_TRIVIAL_FUNCTION;
298 # remove compiler directive lines which llvm-cov seems to generate
299 our $FILTER_DIRECTIVE;
300 # remove missing source file
301 our $FILTER_MISSING_FILE;
302 # remove branches marked as related to exceptions
303 our $FILTER_EXCEPTION_BRANCH;
304 # remove lone branch in block - it can't be an actual conditional
305 our $FILTER_ORPHAN_BRANCH;
306 # MC/DC with single expression is identical to branch
307 our $FILTER_MCDC_SINGLE;
308 our $FILTER_OMIT_PATTERNS; # special/somewhat faked filter
309
310 our %COVERAGE_FILTERS = ("branch" => \$FILTER_BRANCH_NO_COND,
311 'brace' => \$FILTER_LINE_CLOSE_BRACE,
312 'blank' => \$FILTER_BLANK_LINE,
313 'directive' => \$FILTER_DIRECTIVE,
314 'range' => \$FILTER_LINE_RANGE,
315 'line' => \$FILTER_LINE,
316 'initializer' => \$FILTER_INITIALIZER_LIST,
317 'function' => \$FILTER_FUNCTION_ALIAS,
318 'missing' => \$FILTER_MISSING_FILE,
319 'region' => \$FILTER_EXCLUDE_REGION,
320 'branch_region' => \$FILTER_EXCLUDE_BRANCH,
321 'exception' => \$FILTER_EXCEPTION_BRANCH,
322 'orphan' => \$FILTER_ORPHAN_BRANCH,
323 'mcdc' => \$FILTER_MCDC_SINGLE,
324 "trivial" => \$FILTER_TRIVIAL_FUNCTION,);
325 our @cov_filter; # 'undef' if filter is not enabled,
326 # [line_count, coverpoint_count] histogram if
327 # filter is enabled: number of applications
328 # of this filter
329
330 our $EXCL_START = "LCOV_EXCL_START";
331 our $EXCL_STOP = "LCOV_EXCL_STOP";
332 # Marker to exclude branch coverage but keep function and line coverage
333 our $EXCL_BR_START = "LCOV_EXCL_BR_START";
334 our $EXCL_BR_STOP = "LCOV_EXCL_BR_STOP";
335 # marker to exclude exception branches but keep other branches
336 our $EXCL_EXCEPTION_BR_START = 'LCOV_EXCL_EXCEPTION_BR_START';
337 our $EXCL_EXCEPTION_BR_STOP = 'LCOV_EXCL_EXCEPTION_BR_STOP';
338 # exclude on this line
339 our $EXCL_LINE = 'LCOV_EXCL_LINE';
340 our $EXCL_BR_LINE = 'LCOV_EXCL_BR_LINE';
341 our $EXCL_EXCEPTION_LINE = 'LCOV_EXCL_EXCEPTION_BR_LINE';
342
343 our @exclude_file_patterns;
344 our @include_file_patterns;
345 our %excluded_files;
346 our $case_insensitive = 0;
347 our $exclude_exception_branch = 0;
348 our $derive_function_end_line = 1;
349 our $derive_function_end_line_all_files = 0; # by default, C only
350 our $trivial_function_threshold = 5;
351
352 # list of regexps applied to line text - if exclude if matched
353 our @omit_line_patterns;
354 our @exclude_function_patterns;
355 # need a pattern copy that we don't disable for function message suppressions
356 our @suppress_function_patterns;
357
358 our %languageExtensions = ('c' => 'c|h|i|C|H|I|icc|cpp|cc|cxx|hh|hpp|hxx',
359 'rtl' => 'v|vh|sv|vhdl?',
360 'perl' => 'pl|pm',
361 'python' => 'py',
362 'java' => 'java');
363
364 our $info_file_pattern = '*.info';
365
366 # don't look more than 10 lines ahead when filtering (default)
367 our $source_filter_lookahead = 10;
368 # by default, don't treat expressions containing bitwise operators '|', '&', '~'
369 # as conditional in bogus branch filtering
370 our $source_filter_bitwise_are_conditional = 0;
371 # filter out blank lines whether they are hit or not
372 our $filter_blank_aggressive = 0;
373
374 our %dark_palette = ('COLOR_00' => "e4e4e4",
375 'COLOR_01' => "58a6ff",
376 'COLOR_02' => "8b949e",
377 'COLOR_03' => "3b4c71",
378 'COLOR_04' => "006600",
379 'COLOR_05' => "4b6648",
380 'COLOR_06' => "495366",
381 'COLOR_07' => "143e4f",
382 'COLOR_08' => "1c1e23",
383 'COLOR_09' => "202020",
384 'COLOR_10' => "801b18",
385 'COLOR_11' => "66001a",
386 'COLOR_12' => "772d16",
387 'COLOR_13' => "796a25",
388 'COLOR_14' => "000000",
389 'COLOR_15' => "58a6ff",
390 'COLOR_16' => "eeeeee",
391 'COLOR_17' => "E5DBDB",
392 'COLOR_18' => "82E0AA",
393 'COLOR_19' => 'F9E79F',
394 'COLOR_20' => 'EC7063',);
395 our %normal_palette = ('COLOR_00' => "000000",
396 'COLOR_01' => "00cb40",
397 'COLOR_02' => "284fa8",
398 'COLOR_03' => "6688d4",
399 'COLOR_04' => "a7fc9d",
400 'COLOR_05' => "b5f7af",
401 'COLOR_06' => "b8d0ff",
402 'COLOR_07' => "cad7fe",
403 'COLOR_08' => "dae7fe",
404 'COLOR_09' => "efe383",
405 'COLOR_10' => "ff0000",
406 'COLOR_11' => "ff0040",
407 'COLOR_12' => "ff6230",
408 'COLOR_13' => "ffea20",
409 'COLOR_14' => "ffffff",
410 'COLOR_15' => "284fa8",
411 'COLOR_16' => "ffffff",
412 'COLOR_17' => "E5DBDB", # very light pale grey/blue
413 'COLOR_18' => "82E0AA", # light green
414 'COLOR_19' => 'F9E79F', # light yellow
415 'COLOR_20' => 'EC7063', # lighter red
416 );
417
418 our %tlaColor = ("UBC" => "#FDE007",
419 "GBC" => "#448844",
420 "LBC" => "#CC6666",
421 "CBC" => "#CAD7FE",
422 "GNC" => "#B5F7AF",
423 "UNC" => "#FF6230",
424 "ECB" => "#CC66FF",
425 "EUB" => "#DDDDDD",
426 "GIC" => "#30CC37",
427 "UIC" => "#EEAA30",
428 # we don't actually use a color for deleted code.
429 # ... it is deleted. Does not appear
430 "DUB" => "#FFFFFF",
431 "DCB" => "#FFFFFF",);
432 # colors for the text in the PNG image of the corresponding TLA line
433 our %tlaTextColor = ("UBC" => "#aaa005",
434 "GBC" => "#336633",
435 "LBC" => "#994444",
436 "CBC" => "#98a0aa",
437 "GNC" => "#90a380",
438 "UNC" => "#aa4020",
439 "ECB" => "#663388",
440 "EUB" => "#777777",
441 "GIC" => "#18661c",
442 "UIC" => "#aa7718",
443 # we don't actually use a color for deleted code.
444 # ... it is deleted. Does not appear
445 "DUB" => "#FFFFFF",
446 "DCB" => "#FFFFFF",);
447
448 our %pngChar = ('CBC' => '=',
449 'LBC' => '=',
450 'GBC' => '-',
451 'UBC' => '-',
452 'ECB' => '<',
453 'EUB' => '<',
454 'GIC' => '>',
455 'UIC' => '>',
456 'GNC' => '+',
457 'UNC' => '+',);
458
459 our %pngMap = ('=' => ['CBC', 'LBC']
460 , # 0th element 'covered', 1st element 'not covered
461 '-' => ['GBC', 'UBC'],
462 '<' => ['ECB', 'EUB'],
463 '>' => ['GIC', 'UIC'],
464 '+' => ['GNC', 'UNC'],);
465
466 our @opt_rc; # list of command line RC overrides
467
468 our %profileData;
469 our $profile; # the 'enable' flag/name of output file
470
471 # need to defer any errors until after the options have been
472 # processed as user might have suppressed the error we were
473 # trying to emit
474 my @deferred_rc_errors; # ([err|warn, key, string])
475
476 sub set_tool_name($)
477 {
478 $tool_name = shift;
479 }
480
481 #
482 # system_no_output(mode, parameters)
483 #
484 # Call an external program using PARAMETERS while suppressing depending on
485 # the value of MODE:
486 #
487 # MODE & 1: suppress STDOUT (return empty string)
488 # MODE & 2: suppress STDERR (return empty string)
489 # MODE & 4: redirect to string
490 #
491 # Return (stdout, stderr, rc):
492 # stdout: stdout string or ''
493 # stderr: stderr string or ''
494 # 0 on success, non-zero otherwise
495 #
496
497 sub system_no_output($@)
498 {
499 my $mode = shift;
500 # all current uses redirect both stdout and stderr
501 my @args = @_;
502 my ($stdout, $stderr, $code) = Capture::Tiny::capture {
503 system(@args);
504 };
505 if (0 == ($mode & 4)) {
506 $stdout = '' if $mode & 0x1;
507 $stderr = '' if $mode & 0x2;
508 } else {
509 print(STDOUT $stdout) unless $mode & 0x1;
510 print(STDERR $stderr) unless $mode & 0x2;
511 }
512 return ($stdout, $stderr, $code);
513 }
514
515 #
516 # info(printf_parameter)
517 #
518 # Use printf to write PRINTF_PARAMETER to stdout only when not --quiet
519 #
520
521 sub default_info_impl(@)
522 {
523 # Print info string
524 printf(@_);
525 }
526
527 sub set_info_callback($)
528 {
529 $info_callback = shift;
530 }
531
532 sub init_verbose_flag($)
533 {
534 my $quiet = shift;
535 $lcovutil::verbose -= $quiet;
536 }
537
538 sub info(@)
539 {
540 my $level = 0;
541 if ($_[0] =~ /^-?[0-9]+$/) {
542 $level = shift;
543 }
544 &{$info_callback}(@_)
545 if ($level <= $lcovutil::verbose);
546
547 }
548
549 sub debug
550 {
551 my $level = 0;
552 if ($_[0] =~ /^[0-9]+$/) {
553 $level = shift;
554 }
555 my $msg = shift;
556 print(STDERR "DEBUG: $msg")
557 if ($level < $lcovutil::debug);
558 }
559
560 sub temp_cleanup()
561 {
562 if (@temp_dirs) {
563 # Ensure temp directory is not in use by current process
564 my $cwd = Cwd::getcwd();
565 chdir(File::Spec->rootdir());
566 info("Removing temporary directories.\n");
567 foreach (@temp_dirs) {
568 rmtree($_);
569 }
570 @temp_dirs = ();
571 chdir($cwd);
572 }
573 }
574
575 #
576 # create_temp_dir()
577 #
578 # Create a temporary directory and return its path.
579 #
580 # Die on error.
581 #
582
583 sub create_temp_dir()
584 {
585 my $dir = tempdir(DIR => $lcovutil::tmp_dir,
586 CLEANUP => !defined($lcovutil::preserve_intermediates));
587 if (!defined($dir)) {
588 die("cannot create temporary directory\n");
589 }
590 append_tempdir($dir);
591 return $dir;
592 }
593
594 sub append_tempdir($)
595 {
596 push(@temp_dirs, @_);
597 }
598
599 sub _msg_handler
600 {
601 my ($msg, $error) = @_;
602
603 if (!($debug || $verbose > 0 || exists($ENV{LCOV_SHOW_LOCATION}))) {
604 $msg =~ s/ at \S+ line \d+\.$//;
605 }
606 # Enforce consistent "WARNING/ERROR:" message prefix
607 $msg =~ s/^(error|warning):\s+//i;
608 my $type = $error ? 'ERROR' : 'WARNING';
609
610 my $txt = "$tool_name: $type: $msg";
611 if ($message_log && 'GLOB' eq ref($message_log)) {
612 flock($message_log, LOCK_EX);
613 # don't bother to seek...assume modern O_APPEND semantics
614 #seek($message_log, 0, SEEK_END);
615 print $message_log $txt;
616 flock($message_log, LOCK_UN);
617 }
618 return $txt;
619 }
620
621 sub warn_handler($$)
622 {
623 print(STDERR _msg_handler(@_));
624 }
625
626 sub die_handler($)
627 {
628 die(_msg_handler(@_, 1));
629 }
630
631 sub abort_handler($)
632 {
633 temp_cleanup();
634 exit(1);
635 }
636
637 sub count_cores()
638 {
639 # how many cores?
640 $maxParallelism = 1;
641 #linux solution...
642 if (open my $handle, '/proc/cpuinfo') {
643 $maxParallelism = scalar(map /^processor/, <$handle>);
644 close($handle) or die("unable to close /proc/cpuinfo: $!\n");
645 }
646 }
647
648 our $use_MemoryProcess;
649
650 sub read_proc_vmsize
651 {
652 if (open(PROC, "<", '/proc/self/stat')) {
653 my $str = do { local $/; <PROC> }; # slurp whole thing
654 close(PROC) or die("unable to close /proc/self/stat: $!\n");
655 my @data = split(' ', $str);
656 return $data[23 - 1]; # man proc - vmsize is at index 22
657 } else {
658 lcovutil::ignorable_error($lcovutil::ERROR_PACKAGE,
659 "unable to open: $!");
660 return 0;
661 }
662 }
663
664 sub read_system_memory
665 {
666 # NOTE: not sure how to do this on windows...
667 my $total = 0;
668 eval {
669 my $f = InOutFile->in('/proc/meminfo');
670 my $h = $f->hdl();
671 while (<$h>) {
672 if (/MemTotal:\s+(\d+) kB/) {
673 $total = $1 * 1024; # read #kB
674 last;
675 }
676 }
677 };
678 if ($@) {
679 lcovutil::ignorable_error($lcovutil::ERROR_PACKAGE, $@);
680 }
681 return $total;
682 }
683
684 sub init_parallel_params()
685 {
686 if (!defined($lcovutil::maxParallelism)) {
687 $lcovutil::maxParallelism = 1;
688 } elsif (0 == $lcovutil::maxParallelism) {
689 lcovutil::count_cores();
690 info("Found $maxParallelism cores.\n");
691 }
692
693 if (1 != $lcovutil::maxParallelism &&
694 (defined($lcovutil::maxMemory) ||
695 defined($lcovutil::memoryPercentage))
696 ) {
697
698 # need Memory::Process to enable the maxMemory feature
699 my $cwd = Cwd::getcwd();
700 #debug("init: CWD is $cwd\n");
701
702 eval {
703 require Memory::Process;
704 Memory::Process->import();
705 $use_MemoryProcess = 1;
706 };
707 # will have done 'cd /' in the die_handler - if Mem::Process not found
708 #debug("init: chdir back to $cwd\n");
709 chdir($cwd);
710 if ($@) {
711 push(
712 @deferred_rc_errors,
713 [ 1,
714 $lcovutil::ERROR_PACKAGE,
715 "package Memory::Process is required to control memory consumption during parallel operations: $@"
716 ]);
717 $use_MemoryProcess = 0;
718 }
719 }
720
721 if (defined($lcovutil::maxMemory)) {
722 $lcovutil::maxMemory *= 1 << 20;
723 } elsif (defined($lcovutil::memoryPercentage)) {
724 if ($lcovutil::memoryPercentage !~ /^\d+\.?\d*$/ ||
725 $lcovutil::memoryPercentage <= 0) {
726 push(
727 @deferred_rc_errors,
728 [ 1,
729 $lcovutil::ERROR_USAGE,
730 "memory_percentage '$lcovutil::memoryPercentage' is not a valid value"
731 ]);
732 $lcovutil::memoryPercentage = 100;
733 }
734 $lcovutil::maxMemory =
735 read_system_memory() * ($lcovutil::memoryPercentage / 100.0);
736 if ($maxMemory) {
737 my $v = $maxMemory / ((1 << 30) * 1.0);
738 my $unit = 'Gb';
739 if ($v < 1.0) {
740 $unit = 'Mb';
741 $v = $maxMemory / ((1 << 20) * 1.0);
742 }
743 info(sprintf("Setting memory throttle limit to %0.1f %s.\n",
744 $v, $unit
745 ));
746 }
747 } else {
748 $lcovutil::maxMemory = 0;
749 }
750 if (1 != $lcovutil::maxParallelism && # no memory limits if not parallel
751 0 != $lcovutil::maxMemory
752 ) {
753 if (!$use_MemoryProcess) {
754 lcovutil::info(
755 "Attempting to retrieve memory size from /proc instead\n");
756 # check if we can get this from /proc (i.e., are we on linux?)
757 if (0 == read_proc_vmsize()) {
758 $lcovutil::maxMemory = 0; # turn off that feature
759 lcovutil::info(
760 "Continuing execution without Memory::Process or /proc. Note that your maximum memory constraint will be ignored\n"
761 );
762 }
763 }
764 }
765 InOutFile::checkGzip() # we know we are going to use gzip for intermediates
766 if 1 != $lcovutil::maxParallelism;
767 }
768
769 our $memoryObj;
770
771 sub current_process_size
772 {
773 if ($use_MemoryProcess) {
774 $memoryObj = Memory::Process->new
775 unless defined($memoryObj);
776 $memoryObj->record('size');
777 my $arr = $memoryObj->state;
778 $memoryObj->reset();
779 # current vmsize in kB is element 2 of array
780 return $arr->[0]->[2] * 1024; # return total in bytes
781 } else {
782 # assume we are on linux - and get it from /proc
783 return read_proc_vmsize();
784 }
785 }
786
787 sub merge_child_profile($)
788 {
789 my $profile = shift;
790 while (my ($key, $d) = each(%$profile)) {
791 if ('HASH' eq ref($d)) {
792 while (my ($f, $t) = each(%$d)) {
793 if ('HASH' eq ref($t)) {
794 while (my ($x, $y) = each(%$t)) {
795 lcovutil::ignorable_error($lcovutil::ERROR_INTERNAL,
796 "unexpected duplicate key $x=$y at $key->$f")
797 if exists($lcovutil::profileData{$key}{$f}{$x});
798 $lcovutil::profileData{$key}{$f}{$x} = $y;
799 }
800 } else {
801 # 'total' key appears in genhtml report
802 # the others in geninfo.
803 if (exists($lcovutil::profileData{$key}{$f})
804 &&
805 grep(/^$key$/,
806 ( 'version', 'parse',
807 'append', 'total',
808 'resolve', 'derive_end',
809 'check_consistency'))
810 ) {
811 $lcovutil::profileData{$key}{$f} += $t;
812 } else {
813 lcovutil::ignorable_error($lcovutil::ERROR_INTERNAL,
814 "unexpected duplicate key $f=$t in $key:$lcovutil::profileData{$key}{$f}"
815 ) if exists($lcovutil::profileData{$key}{$f});
816 $lcovutil::profileData{$key}{$f} = $t;
817 }
818 }
819 }
820 } else {
821 lcovutil::ignorable_error($lcovutil::ERROR_INTERNAL,
822 "unexpected duplicate key $key=$d in profileData")
823 if exists($lcovutil::profileData{$key});
824 $lcovutil::profileData{$key} = $d;
825 }
826 }
827 }
828
829 sub save_cmd_line($$)
830 {
831 my ($argv, $bin) = @_;
832 my $cmd = $lcovutil::tool_name;
833 $lcovutil::profileData{config}{bin} = "$FindBin::RealBin";
834 foreach my $arg (@$argv) {
835 $cmd .= ' ';
836 if ($arg =~ /\s/) {
837 $cmd .= "'$arg'";
838 } else {
839 $cmd .= $arg;
840 }
841 }
842 $lcovutil::profileData{config}{cmdLine} = $cmd;
843 $lcovutil::profileData{config}{buildDir} = Cwd::getcwd();
844 }
845
846 sub save_profile($@)
847 {
848 my ($dest, $html) = @_;
849
850 if (defined($lcovutil::profile)) {
851 $lcovutil::profileData{config}{maxParallel} = $maxParallelism;
852 $lcovutil::profileData{config}{tool} = $lcovutil::tool_name;
853 $lcovutil::profileData{config}{version} = $lcovutil::lcov_version;
854 $lcovutil::profileData{config}{tool_dir} = $lcovutil::tool_dir;
855 $lcovutil::profileData{config}{url} = $lcovutil::lcov_url;
856 foreach my $t ('date', 'uname -a', 'hostname') {
857 my $v = `$t`;
858 chomp($v);
859 $lcovutil::profileData{config}{(split(' ', $t))[0]} = $v;
860 }
861 my $save = $maxParallelism;
862 count_cores();
863 $lcovutil::profileData{config}{cores} = $maxParallelism;
864 $maxParallelism = $save;
865
866 my $json = JsonSupport::encode(\%lcovutil::profileData);
867
868 if ('' ne $lcovutil::profile) {
869 $dest = $lcovutil::profile;
870 } else {
871 $dest .= ".json";
872 }
873 if (open(JSON, ">", "$dest")) {
874 print(JSON $json);
875 close(JSON) or die("unable to close $dest: $!\n");
876 } else {
877 warn("unable to open profile output $dest: '$!'\n");
878 }
879
880 # only generate the extra data if profile enabled
881 if ($html) {
882
883 my $leader =
884 '<object data="https://www.w3.org/TR/PNG/iso_8859-1.txt" width="300" height="200">'
885 . "\n";
886 my $tail = "</object>\n";
887
888 my $outDir = File::Basename::dirname($html);
889 open(CMD, '>', File::Spec->catfile($outDir, 'cmdline.html')) or
890 die("unable to create cmdline.html: $!");
891 print(CMD $leader, $lcovutil::profileData{config}{cmdLine},
892 "\n", $tail);
893 close(CMD) or die("unable to close cmdline.html: $!");
894
895 # and the profile data
896 open(PROF, '>', $html) or die("unable to create $html: $!");
897 print(PROF $leader);
898
899 open(IN, '<', $dest) or die("unable to open $dest: $!");
900 while (<IN>) {
901 print(PROF $_);
902 }
903 close(IN) or die("unable to close $dest: $!");
904 print(PROF "\n", $tail);
905 close(PROF) or die("unable to close cmdline.html: $!");
906 }
907 }
908 }
909
910 sub set_extensions
911 {
912 my ($type, $str) = @_;
913 die("unknown language '$type'") unless exists($languageExtensions{$type});
914 $languageExtensions{$type} = join('|', split($split_char, $str));
915 }
916
917 sub do_mangle_check
918 {
919 return unless @lcovutil::cpp_demangle;
920
921 if (1 == scalar(@lcovutil::cpp_demangle)) {
922 if ('' eq $lcovutil::cpp_demangle[0]) {
923 # no demangler specified - use c++filt by default
924 if (defined($lcovutil::cpp_demangle_tool)) {
925 $lcovutil::cpp_demangle[0] = $lcovutil::cpp_demangle_tool;
926 } else {
927 $lcovutil::cpp_demangle[0] = 'c++filt';
928 }
929 }
930 } elsif (1 < scalar(@lcovutil::cpp_demangle)) {
931 die("unsupported usage: --demangle-cpp with genhtml_demangle_cpp_tool")
932 if (defined($lcovutil::cpp_demangle_tool));
933 die(
934 "unsupported usage: --demangle-cpp with genhtml_demangle_cpp_params")
935 if (defined($lcovutil::cpp_demangle_params));
936 }
937 if ($lcovutil::cpp_demangle_params) {
938 # deprecated usage
939 push(@lcovutil::cpp_demangle,
940 split(' ', $lcovutil::cpp_demangle_params));
941 }
942 # Extra flag necessary on OS X so that symbols listed by gcov get demangled
943 # properly.
944 push(@lcovutil::cpp_demangle, '--no-strip-underscores')
945 if ($^ eq "darwin");
946
947 $lcovutil::demangle_cpp_cmd = '';
948 foreach my $e (@lcovutil::cpp_demangle) {
949 $lcovutil::demangle_cpp_cmd .= (($e =~ /\s/) ? "'$e'" : $e) . ' ';
950 }
951 my $tool = $lcovutil::cpp_demangle[0];
952 die("could not find $tool tool needed for --demangle-cpp")
953 if (lcovutil::system_no_output(3, "echo \"\" | '$tool'"));
954 }
955
956 sub configure_callback
957 {
958 # if there is just one argument, then assume it might be a
959 # concatenation - otherwise, just use straight.
960 my $cb = shift;
961 my @args =
962 1 == scalar(@_) ?
963 split($lcovutil::split_char, join($lcovutil::split_char, @_)) :
964 @_;
965 my $script = $args[0];
966 if ($script =~ /\.pm$/) {
967 my $dir = File::Basename::dirname($script);
968 my $package = File::Basename::basename($script);
969 my $class = $package;
970 $class =~ s/\.pm$//;
971 unshift(@INC, $dir);
972 eval {
973 require $package;
974 #$package->import(qw(new));
975 # the first value in @_ is the script name
976 $$cb = $class->new(@args);
977 };
978 if ($@ ||
979 !defined($$cb)) {
980 lcovutil::ignorable_error($lcovutil::ERROR_PACKAGE,
981 "unable to create callback from module '$script'" .
982 (defined($@) ? ": $@" : ''));
983 }
984 shift(@INC);
985 } else {
986 # not module
987 $$cb = ScriptCaller->new(@args);
988 }
989 push(@configured_callbacks, $cb);
990 }
991
992 sub cleanup_callbacks
993 {
994 if ($lcovutil::contextCallback) {
995 my $ctx;
996 eval { $ctx = $lcovutil::contextCallback->context(); };
997 if ($@) {
998 lcovutil::ignorable_error($lcovutil::ERROR_CALLBACK,
999 "context callback '" .
1000 $lcovutil::contextCallback[0] .
1001 " ...' failed: $@");
1002 } else {
1003 die('unexpect context callback result: expected hash ref')
1004 unless 'HASH' eq ref($ctx);
1005 $lcovutil::profileData{context} = $ctx;
1006 }
1007 }
1008 foreach my $cb (@configured_callbacks) {
1009 undef $$cb;
1010 }
1011 }
1012
1013 # use these list values from the RC file unless the option is
1014 # passed on the command line
1015 my (@rc_filter, @rc_ignore, @rc_exclude_patterns,
1016 @rc_include_patterns, @rc_subst_patterns, @rc_omit_patterns,
1017 @rc_erase_patterns, @rc_version_script, @unsupported_config,
1018 @rc_source_directories, @rc_build_dir, %unsupported_rc,
1019 $keepGoing, $help, @rc_resolveCallback,
1020 @rc_expected_msg_counts, @rc_criteria_script, @rc_contextCallback,
1021 $rc_no_branch_coverage, $rc_no_func_coverage, $rc_no_checksum,
1022 $version);
1023 my $quiet = 0;
1024 our $tempdirname;
1025
1026 # these options used only by lcov - but moved here so that we can
1027 # share arg parsing
1028 our ($lcov_remove, # If set, removes parts of tracefile
1029 $lcov_capture, # If set, capture data
1030 $lcov_extract); # If set, extracts parts of tracefile
1031 our @opt_config_files;
1032 our @opt_ignore_errors;
1033 our @opt_expected_message_counts;
1034 our @opt_filter;
1035 our @comments;
1036
1037 my %deprecated_rc = ("genhtml_demangle_cpp" => "demangle_cpp",
1038 "genhtml_demangle_cpp_tool" => "demangle_cpp",
1039 "genhtml_demangle_cpp_params" => "demangle_cpp",
1040 "geninfo_checksum" => "checksum",
1041 "geninfo_no_exception_branch" => "no_exception_branch",
1042 'geninfo_adjust_src_path' => 'substitute',
1043 "lcov_branch_coverage" => "branch_coverage",
1044 "lcov_function_coverage" => "function_coverage",
1045 "genhtml_function_coverage" => "function_coverage",
1046 "genhtml_branch_coverage" => "branch_coverage",
1047 'genhtml_criteria_script' => 'criteria_script',
1048 "lcov_fail_under_lines" => 'fail_under_lines',
1049 'genhtml_highlight' => undef,);
1050
1051 my ($cExtensions, $rtlExtensions, $javaExtensions,
1052 $perlExtensions, $pythonExtensions);
1053
1054 my %rc_common = (
1055 'derive_function_end_line' => \$lcovutil::derive_function_end_line,
1056 'derive_function_end_line_all_files' =>
1057 \$derive_function_end_line_all_files,
1058 'trivial_function_threshold' => \$lcovutil::trivial_function_threshold,
1059 "lcov_tmp_dir" => \$lcovutil::tmp_dir,
1060 "lcov_json_module" => \$JsonSupport::rc_json_module,
1061 "branch_coverage" => \$lcovutil::br_coverage,
1062 'mcdc_coverage' => \$lcovutil::mcdc_coverage,
1063 "function_coverage" => \$lcovutil::func_coverage,
1064 "lcov_excl_line" => \$lcovutil::EXCL_LINE,
1065 "lcov_excl_br_line" => \$lcovutil::EXCL_BR_LINE,
1066 "lcov_excl_exception_br_line" => \$lcovutil::EXCL_EXCEPTION_LINE,
1067 "lcov_excl_start" => \$lcovutil::EXCL_START,
1068 "lcov_excl_stop" => \$lcovutil::EXCL_STOP,
1069 "lcov_excl_br_start" => \$lcovutil::EXCL_BR_START,
1070 "lcov_excl_br_stop" => \$lcovutil::EXCL_BR_STOP,
1071 "lcov_excl_exception_br_start" => \$lcovutil::EXCL_EXCEPTION_BR_START,
1072 "lcov_excl_exception_br_stop" => \$lcovutil::EXCL_EXCEPTION_BR_STOP,
1073 "lcov_function_coverage" => \$lcovutil::func_coverage,
1074 "lcov_branch_coverage" => \$lcovutil::br_coverage,
1075 "ignore_errors" => \@rc_ignore,
1076 "max_message_count" => \$lcovutil::suppressAfter,
1077 'expected_message_count' => \@rc_expected_msg_counts,
1078 'stop_on_error' => \$lcovutil::stop_on_error,
1079 'treat_warning_as_error' => \$lcovutil::treat_warning_as_error,
1080 'warn_once_per_file' => \$lcovutil::warn_once_per_file,
1081 'check_data_consistency' => \$lcovutil::check_data_consistency,
1082 "rtl_file_extensions" => \$rtlExtensions,
1083 "c_file_extensions" => \$cExtensions,
1084 "perl_file_extensions" => \$perlExtensions,
1085 "python_file_extensions" => \$pythonExtensions,
1086 "java_file_extensions" => \$javaExtensions,
1087 'info_file_pattern' => \$info_file_pattern,
1088 "filter_lookahead" => \$lcovutil::source_filter_lookahead,
1089 "filter_bitwise_conditional" =>
1090 \$lcovutil::source_filter_bitwise_are_conditional,
1091 'filter_blank_aggressive' => \$filter_blank_aggressive,
1092 "profile" => \$lcovutil::profile,
1093 "parallel" => \$lcovutil::maxParallelism,
1094 "memory" => \$lcovutil::maxMemory,
1095 "memory_percentage" => \$lcovutil::memoryPercentage,
1096 "max_fork_fails" => \$lcovutil::max_fork_fails,
1097 "max_tasks_per_core" => \$lcovutil::max_tasks_per_core,
1098 "fork_fail_timeout" => \$lcovutil::fork_fail_timeout,
1099 'source_directory' => \@rc_source_directories,
1100 'build_directory' => \@rc_build_dir,
1101
1102 "no_exception_branch" => \$lcovutil::exclude_exception_branch,
1103 'filter' => \@rc_filter,
1104 'exclude' => \@rc_exclude_patterns,
1105 'include' => \@rc_include_patterns,
1106 'substitute' => \@rc_subst_patterns,
1107 'omit_lines' => \@rc_omit_patterns,
1108 'erase_functions' => \@rc_erase_patterns,
1109 'context_script' => \@rc_contextCallback,
1110 "version_script" => \@rc_version_script,
1111 'resolve_script' => \@rc_resolveCallback,
1112 'criteria_callback_data' =>
1113 \@CoverageCriteria::criteriaCallbackTypes,
1114 'criteria_callback_levels' =>
1115 \@CoverageCriteria::criteriaCallbackLevels,
1116 'criteria_script' => \@rc_criteria_script,
1117
1118 "checksum" => \$lcovutil::verify_checksum,
1119 'compute_file_version' => \$lcovutil::compute_file_version,
1120 "case_insensitive" => \$lcovutil::case_insensitive,
1121 "forget_testcase_names" => \$TraceFile::ignore_testcase_name,
1122 "split_char" => \$lcovutil::split_char,
1123
1124 'check_existence_before_callback' =>
1125 \$check_file_existence_before_callback,
1126
1127 "demangle_cpp" => \@lcovutil::cpp_demangle,
1128 'excessive_count_threshold' => \$excessive_count_threshold,
1129
1130 'sort_input' => \$lcovutil::sort_inputs,
1131
1132 "fail_under_lines" => \$fail_under_lines,
1133 "fail_under_branches" => \$fail_under_branches,
1134 'lcov_filter_parallel' => \$lcovutil::lcov_filter_parallel,
1135 'lcov_filter_chunk_size' => \$lcovutil::lcov_filter_chunk_size,);
1136
1137 # lcov needs to know the options which might get passed to geninfo in --capture mode
1138 our $defaultChunkSize; # for performance tweaking
1139 our $defaultInterval; # for performance tweaking
1140 our @rc_gcov_tool;
1141 our $geninfo_adjust_testname;
1142 our $opt_external;
1143 our $opt_follow = 0;
1144 our $opt_follow_file_links = 0;
1145 our $opt_compat_libtool;
1146 our $opt_gcov_all_blocks = 1;
1147 our $opt_adjust_unexecuted_blocks = 0;
1148 our $geninfo_opt_compat;
1149 our $rc_adjust_src_path; # Regexp specifying parts to remove from source path
1150 our $rc_auto_base = 1;
1151 our $rc_intermediate = "auto";
1152 our $geninfo_captureAll; # look for both .gcda and lone .gcno files
1153
1154 our %geninfo_rc_opts = (
1155 "geninfo_gcov_tool" => \@rc_gcov_tool,
1156 "geninfo_adjust_testname" => \$geninfo_adjust_testname,
1157 "geninfo_checksum" => \$lcovutil::verify_checksum,
1158 "geninfo_compat_libtool" => \$opt_compat_libtool,
1159 "geninfo_external" => \$opt_external,
1160 "geninfo_follow_symlinks" => \$opt_follow,
1161 "geninfo_follow_file_links" => \$opt_follow_file_links,
1162 "geninfo_gcov_all_blocks" => \$opt_gcov_all_blocks,
1163 "geninfo_unexecuted_blocks" => \$opt_adjust_unexecuted_blocks,
1164 "geninfo_compat" => \$geninfo_opt_compat,
1165 "geninfo_adjust_src_path" => \$rc_adjust_src_path,
1166 "geninfo_auto_base" => \$rc_auto_base,
1167 "geninfo_intermediate" => \$rc_intermediate,
1168 "geninfo_no_exception_branch" => \$lcovutil::exclude_exception_branch,
1169 'geninfo_chunk_size' => \$defaultChunkSize,
1170 'geninfo_interval_update' => \$defaultInterval,
1171 'geninfo_capture_all' => \$geninfo_captureAll);
1172
1173 our %argCommon = ("tempdir=s" => \$tempdirname,
1174 "version-script=s" => \@lcovutil::extractVersionScript,
1175 "criteria-script=s" =>
1176 \@CoverageCriteria::coverageCriteriaScript,
1177
1178 "checksum" => \$lcovutil::verify_checksum,
1179 "no-checksum" => \$rc_no_checksum,
1180 "quiet|q+" => \$quiet,
1181 "verbose|v+" => \$lcovutil::verbose,
1182 "debug+" => \$lcovutil::debug,
1183 "help|h|?" => \$help,
1184 "version" => \$version,
1185 'comment=s' => \@comments,
1186 'toolname=s' => \$lcovutil::tool_name,
1187
1188 "function-coverage" => \$lcovutil::func_coverage,
1189 "branch-coverage" => \$lcovutil::br_coverage,
1190 'mcdc-coverage' => \$lcovutil::mcdc_coverage,
1191 "no-function-coverage" => \$rc_no_func_coverage,
1192 "no-branch-coverage" => \$rc_no_branch_coverage,
1193
1194 "fail-under-lines=s" => \$fail_under_lines,
1195 "fail-under-branches=s" => \$fail_under_branches,
1196 'source-directory=s' =>
1197 \@ReadCurrentSource::source_directories,
1198 'build-directory=s' => \@lcovutil::build_directory,
1199
1200 'resolve-script=s' => \@lcovutil::resolveCallback,
1201 'context-script=s' => \@lcovutil::contextCallback,
1202 "filter=s" => \@opt_filter,
1203 "demangle-cpp:s" => \@lcovutil::cpp_demangle,
1204 "ignore-errors=s" => \@opt_ignore_errors,
1205 "expect-message-count=s" => \@opt_expected_message_counts,
1206 'msg-log:s' => \$message_log,
1207 "keep-going" => \$keepGoing,
1208 "config-file=s" => \@unsupported_config,
1209 "rc=s%" => \%unsupported_rc,
1210 "profile:s" => \$lcovutil::profile,
1211 "exclude=s" => \@lcovutil::exclude_file_patterns,
1212 "include=s" => \@lcovutil::include_file_patterns,
1213 "erase-functions=s" => \@lcovutil::exclude_function_patterns,
1214 "omit-lines=s" => \@lcovutil::omit_line_patterns,
1215 "substitute=s" => \@lcovutil::file_subst_patterns,
1216 "parallel|j:i" => \$lcovutil::maxParallelism,
1217 "memory=i" => \$lcovutil::maxMemory,
1218 "forget-test-names" => \$TraceFile::ignore_testcase_name,
1219 "preserve" => \$lcovutil::preserve_intermediates,
1220 'sort-input' => \$lcovutil::sort_inputs,);
1221
1222 sub warnDeprecated
1223 {
1224 my ($key, $replacement) = @_;
1225 my $opt_used = defined($replacement);
1226 my $suffix =
1227 $opt_used ?
1228 ". Consider using '$replacement'. instead. (Backward-compatible support will be removed in the future.)"
1229 :
1230 ' and ignored.';
1231
1232 push(@deferred_rc_errors,
1233 [0, $lcovutil::ERROR_DEPRECATED,
1234 "RC option '$key' is deprecated$suffix"
1235 ]);
1236 return $opt_used;
1237 }
1238
1239 sub _set_config($$$)
1240 {
1241 # write an RC configuration value - array or scalar
1242 my ($ref, $key, $value) = @_;
1243 my $r = $ref->{$key};
1244 my $t = ref($r);
1245 if ('ARRAY' eq $t) {
1246 info(2, " append $value to list $key\n");
1247 if ('ARRAY' eq ref($value)) {
1248 push(@$r, @$value);
1249 } else {
1250 push(@$r, $value);
1251 }
1252 } else {
1253 # opt is a scalar or not defined
1254 # only way for $value to NOT be an array is if there is a bug in
1255 # the caller such that a scalar ref was passed where a prior call
1256 # had passed a list ref for the same RC option name
1257 die("unexpected ARRAY for $key value")
1258 if ('ARRAY' eq ref($value));
1259 $$r = $value;
1260 info(2, " assign $$r to $key\n");
1261 }
1262 }
1263
1264 #
1265 # read_config(filename, $optionsHash)
1266 #
1267 # Read configuration file FILENAME and write supported key/values into
1268 # RC options hash
1269 # Return: 1 if some config value was set, 0 if not (used for error messaging)
1270
1271 sub read_config($$); # forward decl, to make perl happy about recursive call
1272 my %included_config_files;
1273 my @include_stack;
1274
1275 sub read_config($$)
1276 {
1277 my ($filename, $opts) = @_;
1278 my $key;
1279 my $value;
1280 local *HANDLE;
1281
1282 my $set_value = 0;
1283 info(1, "read_config: $filename\n");
1284 if (exists($included_config_files{abs_path($filename)})) {
1285 lcovutil::ignorable_error($lcovutil::ERROR_USAGE,
1286 'config file inclusion loop detected: "' .
1287 join('" -> "', @include_stack) .
1288 '" -> "' . $filename . '"');
1289 return 0;
1290 }
1291
1292 if (!open(HANDLE, "<", $filename)) {
1293 lcovutil::ignorable_error($lcovutil::ERROR_USAGE,
1294 "cannot read configuration file '$filename': $!");
1295 return 0; # didn't set anything
1296 }
1297 $included_config_files{abs_path($filename)} = 1;
1298 push(@include_stack, $filename);
1299 VAR: while (<HANDLE>) {
1300 chomp;
1301 # Skip comments
1302 s/#.*//;
1303 # Remove leading blanks
1304 s/^\s+//;
1305 # Remove trailing blanks
1306 s/\s+$//;
1307 next unless length;
1308 ($key, $value) = split(/\s*=\s*/, $_, 2);
1309 # is this an environment variable?
1310 while (defined($value) &&
1311 $value =~ /\$ENV\{([^}]+)\}/) {
1312 my $varname = $1;
1313 if (!exists($ENV{$varname})) {
1314 push(
1315 @deferred_rc_errors,
1316 [ 1,
1317 $lcovutil::ERROR_USAGE,
1318 "\"$filename\": $.: variable '$key' uses environment variable '$varname' - which is not set (ignoring '$_')."
1319 ]);
1320 next VAR;
1321 }
1322 $value =~ s/^\$ENV\{$varname\}/$ENV{$varname}/g;
1323 }
1324 if (defined($key) &&
1325 exists($deprecated_rc{$key})) {
1326 next unless warnDeprecated($key, $deprecated_rc{$key});
1327 $key = $deprecated_rc{$key};
1328 }
1329 if (defined($key) && defined($value)) {
1330 info(2, " set: $key = $value\n");
1331 # special case: read included file
1332 if ($key eq 'config_file') {
1333 $set_value |= read_config($value, $opts);
1334 next;
1335 }
1336 # skip if application doesn't use this setting
1337 next unless exists($opts->{$key});
1338 _set_config($opts, $key, $value);
1339 $set_value = 1;
1340 } else {
1341 my $context = MessageContext::context();
1342 push(
1343 @deferred_rc_errors,
1344 [ 1,
1345 $lcovutil::ERROR_FORMAT,
1346 "\"$filename\": $.: malformed configuration file statement '$_': expected \"key = value\"/"
1347 ]);
1348 }
1349 }
1350 close(HANDLE) or die("unable to close $filename: $!\n");
1351 delete $included_config_files{abs_path($filename)};
1352 pop(@include_stack);
1353 return $set_value;
1354 }
1355
1356 # common utility used by genhtml, geninfo, lcov to clean up RC options,
1357 # check for various possible system-wide RC files, and apply the result
1358 # return 1 if we set something
1359 sub apply_rc_params($)
1360 {
1361 my $rcHash = shift;
1362
1363 # merge common RC values with the ones passed in
1364 my %rcHash = (%$rcHash, %rc_common);
1365
1366 # Check command line for a configuration file name
1367 # have to set 'verbosity' flag from environment - otherwise, it isn't
1368 # set (from GetOpt) when we parse the RC file
1369 Getopt::Long::Configure("pass_through", "no_auto_abbrev");
1370 my $quiet = 0;
1371 Getopt::Long::GetOptions("config-file=s" => \@opt_config_files,
1372 "rc=s%" => \@opt_rc,
1373 "quiet|q+" => \$quiet,
1374 "verbose|v+" => \$lcovutil::verbose,
1375 "debug+" => \$lcovutil::debug,);
1376 init_verbose_flag($quiet);
1377 Getopt::Long::Configure("default");
1378
1379 my $set_value = 0;
1380
1381 if (0 != scalar(@opt_config_files)) {
1382 foreach my $f (@opt_config_files) {
1383 $set_value |= read_config($f, \%rcHash);
1384 }
1385 } else {
1386 foreach my $v (['HOME', '.lcovrc'], ['LCOV_HOME', 'etc', 'lcovrc']) {
1387 next unless exists($ENV{$v->[0]});
1388 my $f = File::Spec->catfile($ENV{$v->[0]}, splice(@$v, 1));
1389 if (-r $f) {
1390 $set_value |= read_config($f, \%rcHash);
1391 last;
1392 }
1393 }
1394 }
1395
1396 my $first;
1397 foreach my $v (@opt_rc) {
1398 my $index = index($v, '=');
1399 if ($index == -1) {
1400 push(@deferred_rc_errors,
1401 [1, $lcovutil::ERROR_USAGE,
1402 "malformed --rc option '$v' - should be 'key=value'"
1403 ]);
1404 next;
1405 }
1406 my $key = substr($v, 0, $index);
1407 my $value = substr($v, $index + 1);
1408 $key =~ s/^\s+|\s+$//g;
1409 unless (exists($rcHash{$key})) {
1410 push(
1411 @deferred_rc_errors,
1412 [ 1,
1413 $lcovutil::ERROR_USAGE,
1414 "unknown/unsupported key '$key' found in '--rc $v' - see 'man lcovrc(5)' for the list of valid options"
1415 ]);
1416 next;
1417 }
1418 info(1, "apply --rc overrides\n")
1419 unless defined($first);
1420 $first = 1;
1421 # can't complain about deprecated uses here because the user
1422 # might have suppressed that message - but we haven't looked at
1423 # the suppressions in the parameter list yet.
1424 if (exists($deprecated_rc{$key})) {
1425 next unless warnDeprecated($key, $deprecated_rc{$key});
1426 }
1427 # strip spaces
1428 $value =~ s/^\s+|\s+$//g;
1429 _set_config(\%rcHash, $key, $value);
1430 $set_value = 1;
1431 }
1432 foreach my $d (['rtl', $rtlExtensions],
1433 ['c', $cExtensions],
1434 ['perl', $perlExtensions],
1435 ['python', $pythonExtensions],
1436 ['java', $javaExtensions]
1437 ) {
1438 lcovutil::set_extensions(@$d) if $d->[1];
1439 }
1440 return $set_value;
1441 }
1442
1443 sub parseOptions
1444 {
1445 my ($rcOptions, $cmdLineOpts, $output_arg) = @_;
1446
1447 apply_rc_params($rcOptions);
1448
1449 my %options = (%argCommon, %$cmdLineOpts);
1450 if (!GetOptions(%options)) {
1451 return 0;
1452 }
1453 foreach my $d (['--config-file', scalar(@unsupported_config)],
1454 ['--rc', scalar(%unsupported_rc)]) {
1455 die("'" . $d->[0] . "' option name cannot be abbreviated\n")
1456 if ($d->[1]);
1457 }
1458 if ($help) {
1459 main::print_usage(*STDOUT);
1460 exit(0);
1461 }
1462 # Check for version option
1463 if ($version) {
1464 print("$tool_name: $lcov_version\n");
1465 exit(0);
1466 }
1467 if (defined($message_log)) {
1468 if (!$message_log) {
1469 # base log file name on output arg (if specified) or tool name otherwise
1470 $message_log = (
1471 defined($$output_arg) ?
1472 substr($$output_arg, 0, rindex($$output_arg, '.')) :
1473 $tool_name) .
1474 ".msg";
1475 }
1476 $message_filename = $message_log;
1477 open(LOG, ">", $message_log) or
1478 die("unable to write message log '$message_log': $!");
1479 $message_log = \*LOG;
1480 }
1481
1482 lcovutil::init_verbose_flag($quiet);
1483 # apply the RC file settings if no command line arg
1484 foreach my $rc ([\@opt_filter, \@rc_filter],
1485 [\@opt_ignore_errors, \@rc_ignore],
1486 [\@opt_expected_message_counts, \@rc_expected_msg_counts],
1487 [\@lcovutil::exclude_file_patterns, \@rc_exclude_patterns],
1488 [\@lcovutil::include_file_patterns, \@rc_include_patterns],
1489 [\@lcovutil::file_subst_patterns, \@rc_subst_patterns],
1490 [\@lcovutil::omit_line_patterns, \@rc_omit_patterns],
1491 [\@lcovutil::exclude_function_patterns, \@rc_erase_patterns
1492 ],
1493 [\@lcovutil::extractVersionScript, \@rc_version_script],
1494 [\@CoverageCriteria::coverageCriteriaScript,
1495 \@rc_criteria_script
1496 ],
1497 [\@ReadCurrentSource::source_directories,
1498 \@rc_source_directories
1499 ],
1500 [\@lcovutil::build_directory, \@rc_build_dir],
1501 [\@lcovutil::resolveCallback, \@rc_resolveCallback],
1502 [\@lcovutil::contextCallback, \@rc_contextCallback],
1503 ) {
1504 @{$rc->[0]} = @{$rc->[1]} unless (@{$rc->[0]});
1505 }
1506
1507 $ReadCurrentSource::searchPath =
1508 SearchPath->new('source directory',
1509 @ReadCurrentSource::source_directories);
1510
1511 $lcovutil::stop_on_error = 0
1512 if (defined $keepGoing);
1513
1514 push(@lcovutil::exclude_file_patterns, @ARGV)
1515 if $lcov_remove;
1516 push(@lcovutil::include_file_patterns, @ARGV)
1517 if $lcov_extract;
1518
1519 # Merge options
1520 $lcovutil::func_coverage = 0
1521 if ($rc_no_func_coverage);
1522 $lcovutil::br_coverage = 0
1523 if ($rc_no_branch_coverage);
1524
1525 $lcovutil::verify_checksum = 0
1526 if (defined($rc_no_checksum));
1527
1528 foreach my $cb ([\$versionCallback, \@lcovutil::extractVersionScript],
1529 [\$resolveCallback, \@lcovutil::resolveCallback],
1530 [\$CoverageCriteria::criteriaCallback,
1531 \@CoverageCriteria::coverageCriteriaScript
1532 ],
1533 [\$contextCallback, \@lcovutil::contextCallback],
1534 ) {
1535 lcovutil::configure_callback($cb->[0], @{$cb->[1]})
1536 if (@{$cb->[1]});
1537 }
1538 # perhaps warn that date/owner and directory are only supported by genhtml?
1539 foreach my $data (['criteria_callback_levels',
1540 \@CoverageCriteria::criteriaCallbackLevels,
1541 ['top', 'directory', 'file']
1542 ],
1543 ['criteria_callback_data',
1544 \@CoverageCriteria::criteriaCallbackTypes,
1545 ['date', 'owner']
1546 ]
1547 ) {
1548 my ($rc, $user, $valid) = @$data;
1549 @$user = split(',', join(',', @$user));
1550 foreach my $x (@$user) {
1551 die("invalid '$rc' value \"$x\" - expected (" .
1552 join(", ", @$valid) . ")")
1553 unless grep(/^$x$/, @$valid);
1554 }
1555 }
1556 # context only gets grabbed/stored with '--profile'
1557 $lcovutil::profile = ''
1558 if ($contextCallback && !defined($lcovutil::profile));
1559
1560 if (!$lcov_capture) {
1561 if ($lcovutil::compute_file_version &&
1562 !defined($versionCallback)) {
1563 lcovutil::ignorable_warning($lcovutil::ERROR_USAGE,
1564 "'compute_file_version=1' option has no effect without either '--version-script' or 'version_script=...'."
1565 );
1566 }
1567 lcovutil::munge_file_patterns();
1568 lcovutil::init_parallel_params();
1569 # Determine which errors the user wants us to ignore
1570 parse_ignore_errors(@opt_ignore_errors);
1571 parse_expected_message_counts(@opt_expected_message_counts);
1572 # Determine what coverpoints the user wants to filter
1573 push(@opt_filter, 'exception') if $lcovutil::exclude_exception_branch;
1574 parse_cov_filters(@opt_filter);
1575
1576 # Ensure that the c++filt tool is available when using --demangle-cpp
1577 lcovutil::do_mangle_check();
1578
1579 foreach my $entry (@deferred_rc_errors) {
1580 my ($isErr, $type, $msg) = @$entry;
1581 if ($isErr) {
1582 lcovutil::ignorable_error($type, $msg);
1583 } else {
1584 lcovutil::ignorable_warning($type, $msg);
1585 }
1586 }
1587 }
1588
1589 return 1;
1590 }
1591
1592 #
1593 # transform_pattern(pattern)
1594 #
1595 # Transform shell wildcard expression to equivalent Perl regular expression.
1596 # Return transformed pattern.
1597 #
1598
1599 sub transform_pattern($)
1600 {
1601 my $pattern = $_[0];
1602
1603 # Escape special chars
1604
1605 $pattern =~ s/\\/\\\\/g;
1606 $pattern =~ s/\//\\\//g;
1607 $pattern =~ s/\^/\\\^/g;
1608 $pattern =~ s/\$/\\\$/g;
1609 $pattern =~ s/\(/\\\(/g;
1610 $pattern =~ s/\)/\\\)/g;
1611 $pattern =~ s/\[/\\\[/g;
1612 $pattern =~ s/\]/\\\]/g;
1613 $pattern =~ s/\{/\\\{/g;
1614 $pattern =~ s/\}/\\\}/g;
1615 $pattern =~ s/\./\\\./g;
1616 $pattern =~ s/\,/\\\,/g;
1617 $pattern =~ s/\|/\\\|/g;
1618 $pattern =~ s/\+/\\\+/g;
1619 $pattern =~ s/\!/\\\!/g;
1620
1621 # Transform ? => (.) and * => (.*)
1622
1623 $pattern =~ s/\*/\(\.\*\)/g;
1624 $pattern =~ s/\?/\(\.\)/g;
1625 $pattern = "/$pattern/i"
1626 if ($lcovutil::case_insensitive);
1627 return qr($pattern);
1628 }
1629
1630 sub munge_file_patterns
1631 {
1632 # Need perlreg expressions instead of shell pattern
1633 if (@exclude_file_patterns) {
1634 @exclude_file_patterns =
1635 map({ [transform_pattern($_), $_, 0]; } @exclude_file_patterns);
1636 }
1637
1638 if (@include_file_patterns) {
1639 @include_file_patterns =
1640 map({ [transform_pattern($_), $_, 0]; } @include_file_patterns);
1641 }
1642
1643 # precompile match patterns and check for validity
1644 foreach my $p (['omit-lines', \@omit_line_patterns],
1645 ['exclude-functions', \@exclude_function_patterns]) {
1646 my ($flag, $list) = @$p;
1647 next unless (@$list);
1648 # keep track of number of times pattern was applied
1649 # regexp compile will die if pattern is invalid
1650 eval {
1651 @$list = map({ [qr($_), $_, 0]; } @$list);
1652 };
1653 die("Invalid $flag regexp in ('" . join('\' \'', @$list) . "'):\n$@")
1654 if $@;
1655 }
1656 # sadly, substitutions aren't regexps and can't be precompiled
1657 foreach my $p (['substitute', \@file_subst_patterns]) {
1658 my ($flag, $list) = @$p;
1659 next unless @$list;
1660 PAT: foreach my $pat (@$list) {
1661 my $text = 'abc';
1662 my $str = eval { '$test =~ ' . $pat . ';' };
1663 die("Invalid regexp \"$flag $pat\":\n$@")
1664 if $@;
1665
1666 if ($lcovutil::case_insensitive) {
1667 for (my $i = length($pat) - 1; $i >= 0; --$i) {
1668 my $char = substr($pat, $i, 1);
1669 next PAT
1670 if ($char eq 'i');
1671 last # didn't see the 'i' character
1672 if ($char =~ /[\/#!@%]/);
1673 }
1674 lcovutil::ignorable_warning($lcovutil::ERROR_USAGE,
1675 "--$flag pattern '$pat' does not seem to be case insensitive - but you asked for case insensitive matching"
1676 );
1677 }
1678 }
1679 # keep track of number of times this was applied
1680 @$list = map({ [$_, 0]; } @$list);
1681 }
1682
1683 # and check for valid region patterns
1684 for my $regexp (['lcov_excl_line', $lcovutil::EXCL_LINE],
1685 ['lcov_excl_br_line', $lcovutil::EXCL_BR_LINE],
1686 ['lcov_excl_exception_br_line',
1687 $lcovutil::EXCL_EXCEPTION_LINE
1688 ],
1689 ["lcov_excl_start", \$lcovutil::EXCL_START],
1690 ["lcov_excl_stop", \$lcovutil::EXCL_STOP],
1691 ["lcov_excl_br_start", \$lcovutil::EXCL_BR_START],
1692 ["lcov_excl_br_stop", \$lcovutil::EXCL_BR_STOP],
1693 ["lcov_excl_exception_br_start",
1694 \$lcovutil::EXCL_EXCEPTION_BR_START
1695 ],
1696 ["lcov_excl_exception_br_stop",
1697 \$lcovutil::EXCL_EXCEPTION_BR_STOP
1698 ]
1699 ) {
1700 eval 'qr/' . $regexp->[1] . '/';
1701 my $error = $@;
1702 chomp($error);
1703 $error =~ s/at \(eval.*$//;
1704 die("invalid '" . $regexp->[0] . "' exclude pattern: $error")
1705 if $error;
1706 }
1707 @suppress_function_patterns = map({ $_->[0] } @exclude_function_patterns);
1708 }
1709
1710 sub warn_file_patterns
1711 {
1712 foreach my $p (['include', \@include_file_patterns],
1713 ['exclude', \@exclude_file_patterns],
1714 ['substitute', \@file_subst_patterns],
1715 ['omit-lines', \@omit_line_patterns],
1716 ['exclude-functions', \@exclude_function_patterns]
1717 ) {
1718 my ($type, $patterns) = @$p;
1719 foreach my $pat (@$patterns) {
1720 my $count = $pat->[scalar(@$pat) - 1];
1721 if (0 == $count) {
1722 my $str = $pat->[scalar(@$pat) - 2];
1723 lcovutil::ignorable_error($ERROR_UNUSED,
1724 "'$type' pattern '$str' is unused.");
1725 }
1726 }
1727 }
1728 }
1729
1730 #
1731 # subst_file_name($path)
1732 #
1733 # apply @file_subst_patterns to $path and return
1734 #
1735 sub subst_file_name($)
1736 {
1737 my $name = shift;
1738 foreach my $p (@file_subst_patterns) {
1739 my $old = $name;
1740 # sadly, no support for pre-compiled patterns
1741 eval '$name =~ ' . $p->[0] . ';'; # apply pattern that user provided...
1742 # $@ should never match: we already checked pattern validity during
1743 # initialization - above. Still: belt and braces.
1744 die("invalid 'subst' regexp '" . $p->[0] . "': $@")
1745 if ($@);
1746 $p->[-1] += 1
1747 if $old ne $name;
1748 }
1749 return $name;
1750 }
1751
1752 #
1753 # strip_directories($path, $depth)
1754 #
1755 # Remove DEPTH leading directory levels from PATH.
1756 #
1757
1758 sub strip_directories($$)
1759 {
1760 my $filename = $_[0];
1761 my $depth = $_[1];
1762 my $i;
1763
1764 if (!defined($depth) || ($depth < 1)) {
1765 return $filename;
1766 }
1767 my $d = $lcovutil::dirseparator;
1768 for ($i = 0; $i < $depth; $i++) {
1769 if ($lcovutil::case_insensitive) {
1770 $filename =~ s/^[^$d]*$d+(.*)$/$1/i;
1771 } else {
1772 $filename =~ s/^[^$d]*$d+(.*)$/$1/;
1773 }
1774 }
1775 return $filename;
1776 }
1777
1778 sub define_errors()
1779 {
1780 my $id = 0;
1781 foreach my $d (@lcovErrs) {
1782 my ($k, $ref) = @$d;
1783 $$ref = $id;
1784 $lcovErrors{$k} = $id;
1785 $ERROR_ID{$k} = $id;
1786 $ERROR_NAME{$id} = $k;
1787 $ignore[$id] = 0;
1788 $message_count[$id] = 0;
1789 $expected_message_count[$id] = undef; # no expected count, by default
1790 ++$id;
1791 }
1792 }
1793
1794 sub summarize_messages
1795 {
1796 my $silent = shift;
1797 return if $lcovutil::in_child_process;
1798
1799 # first check for expected message count constraints
1800 for (my $idx = 0; $idx <= $#expected_message_count; ++$idx) {
1801 my $expr = $expected_message_count[$idx];
1802 next unless defined($expr);
1803 my $t = $message_count[$idx];
1804 $expr =~ s/%C/$t/g;
1805 my $v;
1806 eval { $v = eval $expr; };
1807 if ($@ || !defined($v)) {
1808 # we checked the syntax of the message - so should not be able to fail
1809 lcovutil::ignorable_error($lcovutil::ERROR_CALLBACK,
1810 "evaluation of '$expr' failed: $@");
1811 next;
1812 }
1813 unless ($v) {
1814 my $type = $ERROR_NAME{$idx};
1815 lcovutil::ignorable_error($lcovutil::ERROR_COUNT,
1816 "'$type' constraint '$expr' is not true (see '--expect_message_count' for details)."
1817 );
1818 }
1819 }
1820
1821 # now summarize
1822 my %total = ('error' => 0,
1823 'warning' => 0,
1824 'ignore' => 0,);
1825 # use verbosity level -1: so print unless user says "-q -q"...really quiet
1826
1827 my $found = 0;
1828 while (my ($type, $hash) = each(%message_types)) {
1829 while (my ($name, $count) = each(%$hash)) {
1830 $total{$type} += $count;
1831 $found = 1;
1832 }
1833 }
1834 my $header = "Message summary:\n";
1835 foreach my $type ('error', 'warning', 'ignore') {
1836 next unless $total{$type};
1837 $found = 1;
1838 my $leader = $header . ' ' . $total{$type} . " $type message" .
1839 ($total{$type} > 1 ? 's' : '') . ":\n";
1840 my $h = $message_types{$type};
1841 foreach my $k (sort keys %$h) {
1842 info(-1, $leader . ' ' . $k . ": " . $h->{$k} . "\n");
1843 $leader = '';
1844 }
1845 $header = '';
1846 }
1847 info(-1, "$header no messages were reported\n") unless $found || $silent;
1848 }
1849
1850 sub parse_ignore_errors(@)
1851 {
1852 my @ignore_errors = split($split_char, join($split_char, @_));
1853
1854 # first, mark that all known errors are not ignored
1855 foreach my $item (keys(%ERROR_ID)) {
1856 my $id = $ERROR_ID{$item};
1857 $ignore[$id] = 0
1858 unless defined($ignore[$id]);
1859 }
1860
1861 return if (!@ignore_errors);
1862
1863 foreach my $item (@ignore_errors) {
1864 die("unknown argument for --ignore-errors: '$item'")
1865 unless exists($ERROR_ID{lc($item)});
1866 my $item_id = $ERROR_ID{lc($item)};
1867 $ignore[$item_id] += 1;
1868 }
1869 }
1870
1871 sub parse_expected_message_counts(@)
1872 {
1873 my @constraints = split($split_char, join($split_char, @_));
1874 # parse the list and look for errors..
1875 foreach my $c (@constraints) {
1876 if ($c =~ /^s*(\S+?)\s*:\s*((\d+)|(.+?))\s*$/) {
1877 unless (exists($ERROR_ID{lc($1)})) {
1878 lcovutil::ignorable_error($lcovutil::ERROR_USAGE,
1879 "unknown 'expected-message-count' message type \"$1\".");
1880 next;
1881 }
1882
1883 my $id = $ERROR_ID{lc($1)};
1884 if (defined($expected_message_count[$id])) {
1885 my $ignore = $lcovutil::ignore[$lcovutil::ERROR_USAGE];
1886 lcovutil::ignorable_error($lcovutil::ERROR_USAGE,
1887 "duplicate 'expected' constraint '$c'" .
1888 ($ignore ? ': ignoring.' : ''));
1889 next;
1890 }
1891 # check if syntax look reasonable
1892 my $expr = $2;
1893 if (Scalar::Util::looks_like_number($expr)) {
1894 $expected_message_count[$id] = "%C == $expr";
1895 next;
1896 }
1897 lcovutil::ignorable_error($lcovutil::ERROR_USAGE,
1898 "expect-message-count constraint '$c' does not appear to depend on message count: '%C' substitution not found."
1899 ) unless ($expr =~ /%C/);
1900
1901 # now lets try an eval
1902 my $v = $expr;
1903 $v =~ s/%C/0/g;
1904 $v = eval $v;
1905 if (defined($v)) {
1906 $expected_message_count[$id] = $expr;
1907 } else {
1908 my $ignore = $lcovutil::ignore[$lcovutil::ERROR_USAGE];
1909 lcovutil::ignorable_error($lcovutil::ERROR_USAGE,
1910 "eval error in 'expect-message-count' constraint '$c': $@"
1911 . ($ignore ? ': ignoring.' : ''));
1912 }
1913 } else {
1914 lcovutil::ignorable_error($lcovutil::ERROR_USAGE,
1915 "malformed expected-message-count constraint \"$c\". Expected 'msg_type = expr'."
1916 );
1917 }
1918 }
1919 }
1920
1921 sub message_count($)
1922 {
1923 my $code = shift;
1924
1925 return $message_count[$code];
1926 }
1927
1928 sub is_ignored($)
1929 {
1930 my $code = shift;
1931 die("invalid error code $code")
1932 unless 0 <= $code && $code < scalar(@ignore);
1933 return $ignore[$code] || (defined($stop_on_error) && 0 == $stop_on_error);
1934 }
1935
1936 our %explainOnce; # append explanation to first error/warning message (only)
1937
1938 sub explain_once
1939 {
1940 # NOTE: in parallel execution, the explanations may appear more than
1941 # once - e.g., when two or more child processes generate them
1942 # simultaneously.
1943 # They will eventually update the parent process state such that
1944 # subsequent children won't report the issues.
1945 my $key = shift;
1946 if (!exists($explainOnce{$key})) {
1947 $explainOnce{$key} = 1;
1948 my $msg = '';
1949 # each element is either a string or a pair of [string, predicate]
1950 foreach my $e (@_) {
1951 if ('ARRAY' eq ref($e)) {
1952 $msg .= $e->[0] if defined($e->[1]) && $e->[1];
1953 } else {
1954 $msg .= $e;
1955 }
1956 }
1957 return $msg;
1958 }
1959 return '';
1960 }
1961
1962 our %warnOnlyOnce;
1963 our $deferWarnings = 0;
1964 # if 'stop_on_error' is false, then certain errors should be emitted at most once
1965 # (not relevant if stop_on_error is true - as we will exit after the error.
1966 sub warn_once
1967 {
1968 my ($msgType, $key) = @_;
1969 return 0
1970 if (exists($warnOnlyOnce{$msgType}) &&
1971 exists($warnOnlyOnce{$msgType}{$key}));
1972 $warnOnlyOnce{$msgType}{$key} = 1;
1973 return 1;
1974 }
1975
1976 sub store_deferred_message
1977 {
1978 my ($msgType, $isError, $key, $msg) = @_;
1979 die(
1980 "unexpected deferred value of $msg->$key: $warnOnlyOnce{$msgType}{$key}")
1981 unless 1 == $warnOnlyOnce{$msgType}{$key};
1982 if ($deferWarnings) {
1983 $warnOnlyOnce{$msgType}{$key} = [$msg, $isError];
1984 } else {
1985 if ($isError) {
1986 lcovutil::ignorable_error($msgType, $msg);
1987 } else {
1988 lcovutil::ignorable_warning($msgType, $msg);
1989 }
1990 }
1991 }
1992
1993 sub merge_deferred_warnings
1994 {
1995 my $hash = shift;
1996 while (my ($type, $d) = each(%$hash)) {
1997 while (my ($key, $m) = each(%$d)) {
1998 if (!(exists($warnOnlyOnce{$type}) &&
1999 exists($warnOnlyOnce{$type}{$key}))) {
2000 if ('ARRAY' eq ref($m)) {
2001 # this is a
2002 my ($msg, $isError) = @$m;
2003 if ($isError) {
2004 lcovutil::ignorable_error($type, $msg);
2005 } else {
2006 lcovutil::ignorable_warning($type, $msg);
2007 }
2008 }
2009 $warnOnlyOnce{$type}{$key} = 1;
2010 }
2011 }
2012 }
2013 }
2014
2015 sub initial_state
2016 {
2017 # a bit of a hack: this method is called at the start of each
2018 # child process - so use it to record that we are executing in a
2019 # child.
2020 # The flag is used to reduce verbosity from children - and possibly
2021 # for other things later
2022 $lcovutil::in_child_process = 1;
2023
2024 # keep track of number of warnings, etc. generated in child -
2025 # so we can merge back into parent. This may prevent us from
2026 # complaining about the same thing in multiple children - but only
2027 # if those children don't execute in parallel.
2028 %message_types = (); #reset
2029 $ReadCurrentSource::searchPath->reset();
2030 # clear profile - want only my contribution
2031 %lcovutil::profileData = ();
2032 %lcovutil::warnOnlyOnce = ();
2033
2034 # clear pattern counts so we can update number found in children
2035 foreach my $patType (\@lcovutil::exclude_file_patterns,
2036 \@lcovutil::include_file_patterns,
2037 \@lcovutil::file_subst_patterns,
2038 \@lcovutil::omit_line_patterns,
2039 \@lcovutil::exclude_function_patterns
2040 ) {
2041 foreach my $p (@$patType) {
2042 $p->[-1] = 0;
2043 }
2044 }
2045
2046 return Storable::dclone([\@message_count, \%versionCache, \%resolveCache]);
2047 }
2048
2049 sub compute_update
2050 {
2051 my $state = shift;
2052 my @new_count;
2053 my ($initialCount, $initialVersionCache, $initialResolveCache) = @$state;
2054 my $id = 0;
2055 foreach my $count (@message_count) {
2056 my $v = $count - $initialCount->[$id++];
2057 push(@new_count, $v);
2058 }
2059 my %versionUpdate;
2060 while (my ($f, $v) = each(%versionCache)) {
2061 $versionUpdate{$f} = $v
2062 unless exists($initialVersionCache->{$f});
2063 }
2064 my %resolveUpdate;
2065 while (my ($f, $v) = each(%resolveCache)) {
2066 $resolveUpdate{$f} = $v
2067 unless exists($initialResolveCache->{$f});
2068 }
2069 my @rtn = (\@new_count,
2070 \%versionUpdate,
2071 \%resolveUpdate,
2072 \%message_types,
2073 $ReadCurrentSource::searchPath->current_count(),
2074 \%lcovutil::profileData,
2075 \%lcovutil::warnOnlyOnce,
2076 \%lcovutil::explainOnce);
2077
2078 foreach my $patType (\@lcovutil::exclude_file_patterns,
2079 \@lcovutil::include_file_patterns,
2080 \@lcovutil::file_subst_patterns,
2081 \@lcovutil::omit_line_patterns,
2082 \@lcovutil::exclude_function_patterns
2083 ) {
2084 my @count;
2085 foreach my $p (@$patType) {
2086 push(@count, $p->[-1]);
2087 }
2088 push(@rtn, \@count);
2089 }
2090
2091 return \@rtn;
2092 }
2093
2094 sub update_state
2095 {
2096 my $updateCount = shift;
2097 my $id = 0;
2098 foreach my $count (@$updateCount) {
2099 $message_count[$id++] += $count;
2100 }
2101 my $updateVersionCache = shift;
2102 while (my ($f, $v) = each(%$updateVersionCache)) {
2103 lcovutil::ignorable_error($lcovutil::ERROR_INTERNAL,
2104 "unexpected version entry")
2105 if exists($versionCache{$f}) && $versionCache{$f} ne $v;
2106 $versionCache{$f} = $v;
2107 }
2108 my $updateResolveCache = shift;
2109 while (my ($f, $v) = each(%$updateResolveCache)) {
2110 lcovutil::ignorable_error($lcovutil::ERROR_INTERNAL,
2111 "unexpected resolve entry")
2112 if exists($resolveCache{$f}) && $resolveCache{$f} ne $v;
2113 $resolveCache{$f} = $v;
2114 }
2115 my $msgTypes = shift;
2116 while (my ($type, $h) = each(%$msgTypes)) {
2117 while (my ($err, $count) = each(%$h)) {
2118 if (exists($message_types{$type}) &&
2119 exists($message_types{$type}{$err})) {
2120 $message_types{$type}{$err} += $count;
2121 } else {
2122 $message_types{$type}{$err} = $count;
2123 }
2124 }
2125 }
2126 my $searchCount = shift;
2127 $ReadCurrentSource::searchPath->update_count(@$searchCount);
2128
2129 my $profile = shift;
2130 lcovutil::merge_child_profile($profile);
2131 my $warnOnce = shift;
2132 lcovutil::merge_deferred_warnings($warnOnce);
2133 my $explainOnce = shift;
2134 while (my ($key, $v) = each(%$explainOnce)) {
2135 $lcovutil::explainOnce{$key} = $v;
2136 }
2137
2138 foreach my $patType (\@lcovutil::exclude_file_patterns,
2139 \@lcovutil::include_file_patterns,
2140 \@lcovutil::file_subst_patterns,
2141 \@lcovutil::omit_line_patterns,
2142 \@lcovutil::exclude_function_patterns
2143 ) {
2144 my $count = shift;
2145 die("unexpected pattern count") unless $#$count == $#$patType;
2146 foreach my $p (@$patType) {
2147 $p->[-1] += shift @$count;
2148 }
2149 }
2150 die("unexpected update data") unless -1 == $#_; # exhausted list
2151 }
2152
2153 sub warnSuppress($$)
2154 {
2155 my ($code, $errName) = @_;
2156
2157 if ($ignore[$code] <= 1 && # don't warn if already suppressed
2158 $message_count[$code] == ($suppressAfter + 1)
2159 ) {
2160 # explain once per error type, if verbose - else only once
2161 my $explain = explain_once(
2162 'error_count' . ($lcovutil::verbose ? $errName : ''),
2163 "\n\tTo increase or decrease this limit use '--rc max_message_count=value'."
2164 );
2165 ignorable_warning($ERROR_COUNT,
2166 "max_message_count=$suppressAfter reached for '$errName' messages: no more will be reported.$explain"
2167 );
2168 }
2169 }
2170
2171 sub _count_message($$)
2172 {
2173 my ($type, $name) = @_;
2174
2175 $message_types{$type}{$name} = 0
2176 unless (exists($message_types{$type}) &&
2177 exists($message_types{$type}{$name}));
2178 ++$message_types{$type}{$name};
2179 }
2180
2181 sub saw_error
2182 {
2183 # true if we saw at least one error when 'stop_on_error' is false
2184 # enables us to return non-zero exit status if any errors were detected
2185 return exists($message_types{error});
2186 }
2187
2188 sub ignorable_error($$;$)
2189 {
2190 my ($code, $msg, $quiet) = @_;
2191 die("undefined error code for '$msg'") unless defined($code);
2192
2193 my $errName = "code_$code";
2194 $errName = $ERROR_NAME{$code}
2195 if exists($ERROR_NAME{$code});
2196
2197 if ($message_count[$code]++ >= $suppressAfter &&
2198 0 < $suppressAfter) {
2199 # safe to just continue without checking anything else - as either
2200 # this message is not fatal and we emitted it some number of times,
2201 # or the message is fatal - and this is the first time we see it
2202
2203 _count_message('ignore', $errName);
2204 # warn that we are suppressing from here on - for the first skipped
2205 # message of this type
2206 warnSuppress($code, $errName);
2207 return;
2208 }
2209
2210 chomp($msg); # we insert the newline
2211 if ($code >= scalar(@ignore) ||
2212 !$ignore[$code]) {
2213 my $ignoreOpt =
2214 "\t(use \"$tool_name --ignore-errors $errName ...\" to bypass this error)\n";
2215 $ignoreOpt = ''
2216 if ($lcovutil::in_child_process ||
2217 !($lcovutil::verbose || $message_count[$code] == 1));
2218 if (defined($stop_on_error) && 0 == $stop_on_error) {
2219 _count_message('error', $errName);
2220 warn_handler("($errName) $msg\n$ignoreOpt", 1);
2221 return;
2222 }
2223 _count_message('error', $errName);
2224 die_handler("($errName) $msg\n$ignoreOpt");
2225 }
2226 # only tell the user how to suppress this on the first occurrence
2227 my $ignoreOpt =
2228 "\t(use \"$tool_name --ignore-errors $errName,$errName ...\" to suppress this warning)\n";
2229 $ignoreOpt = ''
2230 if ($lcovutil::in_child_process ||
2231 !($lcovutil::verbose || $message_count[$code] == 1));
2232
2233 if ($ignore[$code] > 1 || (defined($quiet) && $quiet)) {
2234 _count_message('ignore', $errName);
2235 } else {
2236 _count_message('warning', $errName);
2237 warn_handler("($errName) $msg\n$ignoreOpt", 0);
2238 }
2239 }
2240
2241 sub ignorable_warning($$;$)
2242 {
2243 my ($code, $msg, $quiet) = @_;
2244 if ($lcovutil::treat_warning_as_error) {
2245 ignorable_error($code, $msg, $quiet);
2246 return;
2247 }
2248 die("undefined error code for '$msg'") unless defined($code);
2249
2250 my $errName = "code_$code";
2251 $errName = $ERROR_NAME{$code}
2252 if exists($ERROR_NAME{$code});
2253 if ($message_count[$code]++ >= $suppressAfter &&
2254 0 < $suppressAfter) {
2255 # warn that we are suppressing from here on - for the first skipped
2256 # message of this type
2257 warnSuppress($code, $errName);
2258 _count_message('ignore', $errName);
2259 return;
2260 }
2261 chomp($msg); # we insert the newline
2262 if ($code >= scalar(@ignore) ||
2263 !$ignore[$code]) {
2264 # only tell the user how to suppress this on the first occurrence
2265 my $ignoreOpt =
2266 "\t(use \"$tool_name --ignore-errors $errName,$errName ...\" to suppress this warning)\n";
2267 $ignoreOpt = ''
2268 if ($lcovutil::in_child_process ||
2269 !($lcovutil::verbose || $message_count[$code] == 1));
2270 warn_handler("($errName) $msg\n$ignoreOpt", 0);
2271 _count_message('warning', $errName);
2272 } else {
2273 _count_message('ignore', $errName);
2274 }
2275 }
2276
2277 sub report_unknown_child
2278 {
2279 my $child = shift;
2280 # this can happen if the user loads a callback module which starts a chaild
2281 # process when it is loaded or initialized and fails to wait for that child
2282 # to finish. How it manifests is an orphan PID which is smaller (older)
2283 # than any of the children that this parent actually scheduled
2284 lcovutil::ignorable_error($lcovutil::ERROR_CHILD,
2285 "found unknown process $child while waiting for parallel child:\n perhaps you forgot to close a process in your callback?"
2286 );
2287 }
2288
2289 sub report_fork_failure
2290 {
2291 my ($when, $errcode, $failedAttempts) = @_;
2292 if ($failedAttempts > $lcovutil::max_fork_fails) {
2293 lcovutil::ignorable_error($lcovutil::ERROR_PARALLEL,
2294 "$failedAttempts consecutive fork() failures: consider reduced parallelism or increase the max_fork_fails limit. See man(5) lcovrc."
2295 );
2296 }
2297 my $explain = explain_once('fork_fail',
2298 ["\n\tUse '$tool_name --ignore_errors " .
2299 $ERROR_NAME{$ERROR_FORK} .
2300 "' to bypass error and retry.",
2301 $ignore[$lcovutil::ERROR_FORK] == 0
2302 ]);
2303 my $retry =
2304 lcovutil::is_ignored($lcovutil::ERROR_FORK) ? ' (retrying)' : '';
2305 lcovutil::ignorable_error($lcovutil::ERROR_FORK,
2306 "fork() syscall failed while trying to $when: " .
2307 $errcode . $retry . $explain);
2308 # if errors were ignored, then we wait for a while (in parent)
2309 # before re-trying.
2310 sleep($lcovutil::fork_fail_timeout);
2311 }
2312
2313 sub report_exit_status
2314 {
2315 my ($errType, $message, $exitstatus, $prefix, $suffix) = @_;
2316 my $status = $exitstatus >> 8;
2317 my $signal = $exitstatus & 0xFF;
2318 my $explain =
2319 "$prefix " .
2320 ($exitstatus ? "returned non-zero exit status $status" : 'failed') .
2321 MessageContext::context();
2322 if ($signal) {
2323 $explain =
2324 "$prefix died died due to signal $signal (SIG" .
2325 (split(' ', $Config{sig_name}))[$signal] .
2326 ')' . MessageContext::context() .
2327 ': possibly killed by OS due to out-of-memory';
2328 $explain .=
2329 lcovutil::explain_once('out_of_memory',
2330 ' - see --memory and --parallel options for throttling');
2331 }
2332 ignorable_error($errType, "$message: $explain$suffix");
2333 }
2334
2335 sub report_parallel_error
2336 {
2337 my $operation = shift;
2338 my $errno = shift;
2339 my $pid = shift;
2340 my $childstatus = shift;
2341 my $msg = shift;
2342 # kill all my remaining children so user doesn't see unexpected console
2343 # messages from dangling children (who cannot open files because the
2344 # temp directory has been deleted, and so forth)
2345 kill(9, @_) if @_ && !is_ignored($errno);
2346 report_exit_status($errno, "$operation: '$msg'",
2347 $childstatus, "child $pid",
2348 " (try removing the '--parallel' option)");
2349 }
2350
2351 sub report_format_error($$$$)
2352 {
2353 my ($errType, $countType, $count, $obj) = @_;
2354 my $context = MessageContext::context();
2355 my $explain =
2356 explain_once(
2357 'err_negative',
2358 ["\n\tPerhaps you need to compile with '-fprofile-update=atomic'.",
2359 ($lcovutil::ERROR_NEGATIVE == $errType &&
2360 'geninfo' eq $lcovutil::tool_name)
2361 ]);
2362 my $errStr =
2363 $lcovutil::ERROR_NEGATIVE == $errType ? 'negative' :
2364 ($lcovutil::ERROR_FORMAT == $errType ? 'non-integer' : 'excessive');
2365 lcovutil::ignorable_error($errType,
2366 "Unexpected $errStr $countType count '$count' for $obj$context.$explain"
2367 );
2368 }
2369
2370 sub check_parent_process
2371 {
2372 die("must call from child process") unless $lcovutil::in_child_process;
2373 # if parent PID changed to 1 (init) - then my parent went away so
2374 # I should exit now
2375 # for reasons which are unclear to me: the PPID is sometimes unchanged
2376 # after the parent process dies - to also check if we can send it a signal
2377 my $ppid = getppid();
2378 lcovutil::info(2, "check_parent_process($$) = $ppid\n");
2379 if (1 == getppid() ||
2380 1 != kill(0, $ppid)) {
2381 lcovutil::ignorable_error($lcovutil::ERROR_PARENT,
2382 "parent process died during '--parallel' execution - child $$ cannot continue."
2383 );
2384 exit(0);
2385 }
2386 }
2387
2388 sub is_filter_enabled
2389 {
2390 # return true of there is an opportunity for filtering
2391 return (grep({ defined($_) } @lcovutil::cov_filter) ||
2392 0 != scalar(@lcovutil::omit_line_patterns) ||
2393 0 != scalar(@lcovutil::exclude_function_patterns));
2394 }
2395
2396 sub init_filters
2397 {
2398 # initialize filter index numbers and mark that all filters are disabled.
2399 my $idx = 0;
2400 foreach my $item (sort keys(%COVERAGE_FILTERS)) {
2401 my $ref = $COVERAGE_FILTERS{$item};
2402 $COVERAGE_FILTERS{$item} = $idx;
2403 $$ref = $idx;
2404 $cov_filter[$idx++] = undef;
2405 }
2406 }
2407
2408 sub parse_cov_filters(@)
2409 {
2410 my @filters = split($split_char, join($split_char, @_));
2411
2412 goto final if (!@filters);
2413
2414 foreach my $item (@filters) {
2415 die("unknown argument for --filter: '$item'\n")
2416 unless exists($COVERAGE_FILTERS{lc($item)});
2417 my $item_id = $COVERAGE_FILTERS{lc($item)};
2418
2419 $cov_filter[$item_id] = [$item, 0, 0];
2420 }
2421 if ($cov_filter[$FILTER_LINE]) {
2422 # when line filtering is enabled, turn on brace and blank filtering as well
2423 # (backward compatibility)
2424 $cov_filter[$FILTER_LINE_CLOSE_BRACE] = ['brace', 0, 0];
2425 $cov_filter[$FILTER_BLANK_LINE] = ['blank', 0, 0];
2426 }
2427 if ((defined($cov_filter[$FILTER_BRANCH_NO_COND]) ||
2428 defined($cov_filter[$FILTER_EXCLUDE_BRANCH])) &&
2429 !($br_coverage || $mcdc_coverage)
2430 ) {
2431 lcovutil::ignorable_warning($ERROR_USAGE,
2432 "branch filter enabled but neither branch or condition coverage is enabled"
2433 );
2434 }
2435 lcovutil::ignorable_warning($ERROR_USAGE,
2436 "'mcdc' filter enabled but MC/DC coverage is not enabled.")
2437 if (defined($cov_filter[$FILTER_MCDC_SINGLE]) &&
2438 !$mcdc_coverage);
2439 if ($cov_filter[$FILTER_BRANCH_NO_COND]) {
2440 # turn on exception and orphan filtering too
2441 $cov_filter[$FILTER_EXCEPTION_BRANCH] = ['exception', 0, 0];
2442 $cov_filter[$FILTER_ORPHAN_BRANCH] = ['orphan', 0, 0];
2443 }
2444 final:
2445 if (@lcovutil::omit_line_patterns) {
2446 $lcovutil::FILTER_OMIT_PATTERNS = scalar(@lcovutil::cov_filter);
2447 push(@lcovutil::cov_filter, ['omit_lines', 0, 0]);
2448 $lcovutil::COVERAGE_FILTERS{'omit_lines'} =
2449 $lcovutil::FILTER_OMIT_PATTERNS;
2450 }
2451 }
2452
2453 sub summarize_cov_filters
2454 {
2455 # use verbosity level -1: so print unless user says "-q -q"...really quiet
2456
2457 my $leader = "Filter suppressions:\n";
2458 for my $key (keys(%COVERAGE_FILTERS)) {
2459 my $id = $COVERAGE_FILTERS{$key};
2460 next unless defined($lcovutil::cov_filter[$id]);
2461 my $histogram = $lcovutil::cov_filter[$id];
2462 next if 0 == $histogram->[-2];
2463 my $points = '';
2464 if ($histogram->[-2] != $histogram->[-1]) {
2465 $points = ' ' . $histogram->[-1] . ' coverpoint' .
2466 ($histogram->[-1] > 1 ? 's' : '') . "\n";
2467 }
2468 info(-1,
2469 "$leader $key:\n " . $histogram->[-2] . " instance" .
2470 ($histogram->[-2] > 1 ? "s" : "") . "\n" . $points);
2471 $leader = '';
2472 }
2473 foreach my $q (['omit-lines', 'line', \@omit_line_patterns],
2474 ['erase-functions', 'function', \@exclude_function_patterns]) {
2475 my ($opt, $type, $patterns) = @$q;
2476 my $patternCount = scalar(@$patterns);
2477 if ($patternCount) {
2478 my $omitCount = 0;
2479 foreach my $p (@$patterns) {
2480 $omitCount += $p->[-1];
2481 }
2482 info(-1,
2483 "Omitted %d total $type%s matching %d '--$opt' pattern%s\n",
2484 $omitCount,
2485 $omitCount == 1 ? '' : 's',
2486 $patternCount,
2487 $patternCount == 1 ? '' : 's');
2488 }
2489 }
2490 }
2491
2492 sub disable_cov_filters
2493 {
2494 # disable but return current status - so they can be re-enabled
2495 my @filters = @lcovutil::cov_filter;
2496 foreach my $f (@lcovutil::cov_filter) {
2497 $f = undef;
2498 }
2499 my @omit = @lcovutil::omit_line_patterns;
2500 @lcovutil::omit_line_patterns = ();
2501 my @erase = @lcovutil::exclude_function_patterns;
2502 @lcovutil::exclude_function_patterns = ();
2503 return [\@filters, \@omit, \@erase];
2504 }
2505
2506 sub reenable_cov_filters
2507 {
2508 my $data = shift;
2509 my $filters = $data->[0];
2510 # re-enable in the same order
2511 for (my $i = 0; $i < scalar(@$filters); $i++) {
2512 $cov_filter[$i] = $filters->[$i];
2513 }
2514 @lcovutil::omit_line_patterns = @{$data->[1]};
2515 @lcovutil::exclude_function_patterns = @{$data->[2]};
2516 }
2517
2518 sub filterStringsAndComments
2519 {
2520 my $src_line = shift;
2521
2522 # remove compiler directives
2523 $src_line =~ s/^\s*#.*$//g;
2524 # remove comments
2525 $src_line =~ s#(/\*.*?\*/|//.*$)##g;
2526 # remove strings
2527 $src_line =~ s/\\"//g;
2528 $src_line =~ s/"[^"]*"//g;
2529
2530 return $src_line;
2531 }
2532
2533 sub simplifyCode
2534 {
2535 my $src_line = shift;
2536
2537 # remove comments
2538 $src_line = filterStringsAndComments($src_line);
2539 # remove some keywords..
2540 $src_line =~ s/\b(const|volatile|typename)\b//g;
2541 #collapse nested class names
2542 # remove things that look like template names
2543 my $id = '(::)?\w+\s*(::\s*\w+\s*)*';
2544 while (1) {
2545 my $current = $src_line;
2546 $src_line =~ s/<\s*${id}(,\s*${id})*([*&]\s*)?>//g;
2547 last if $src_line eq $current;
2548 }
2549 # remove ref and pointer decl
2550 $src_line =~ s/^\s*$id[&*]\s*($id)/$3/g;
2551 # cast which contains optional location spec
2552 my $cast = "\\s*${id}(\\s+$id)?[*&]\\s*";
2553 # C-style cast - with optional location spec
2554 $src_line =~ s/\($cast\)//g;
2555 $src_line =~ s/\b(reinterpret|dynamic|const)_cast<$cast>//g;
2556 # remove addressOf that follows an open paren or a comma
2557 #$src_line =~ s/([(,])\s*[&*]\s*($id)/$1 $2/g;
2558
2559 # remove some characters which might look like conditionals
2560 $src_line =~ s/(->|>>|<<|::)//g;
2561
2562 return $src_line;
2563 }
2564
2565 sub balancedParens
2566 {
2567 my $line = shift;
2568
2569 my $open = 0;
2570 my $close = 0;
2571
2572 foreach my $char (split('', $line)) {
2573 if ($char eq '(') {
2574 ++$open;
2575 } elsif ($char eq ')') {
2576 ++$close;
2577 }
2578 }
2579 return ($open == $close ||
2580 # lambda code may have trailing parens after the function...
2581 ($close > $open && $line =~ /{lambda\(/)
2582 ); # this is a C++-specific check
2583 }
2584
2585 #
2586 # is_external(filename)
2587 #
2588 # Determine if a file is located outside of the specified data directories.
2589 #
2590
2591 sub is_external($)
2592 {
2593 my $filename = shift;
2594
2595 # nothing is 'external' unless the user has requested "--no-external"
2596 return 0 unless (defined($opt_no_external) && $opt_no_external);
2597
2598 foreach my $dir (@internal_dirs) {
2599 return 0
2600 if (($lcovutil::case_insensitive && $filename =~ /^\Q$dir\E/i) ||
2601 (!$lcovutil::case_insensitive && $filename =~ /^\Q$dir\E/));
2602 }
2603 return 1;
2604 }
2605
2606 #
2607 # rate(hit, found[, suffix, precision, width])
2608 #
2609 # Return the coverage rate [0..100] for HIT and FOUND values. 0 is only
2610 # returned when HIT is 0. 100 is only returned when HIT equals FOUND.
2611 # PRECISION specifies the precision of the result. SUFFIX defines a
2612 # string that is appended to the result if FOUND is non-zero. Spaces
2613 # are added to the start of the resulting string until it is at least WIDTH
2614 # characters wide.
2615 #
2616
2617 sub rate($$;$$$)
2618 {
2619 my ($hit, $found, $suffix, $precision, $width) = @_;
2620
2621 # Assign defaults if necessary
2622 $precision = $default_precision
2623 if (!defined($precision));
2624 $suffix = "" if (!defined($suffix));
2625 $width = 0 if (!defined($width));
2626
2627 return sprintf("%*s", $width, "-") if (!defined($found) || $found == 0);
2628 my $rate = sprintf("%.*f", $precision, $hit * 100 / $found);
2629
2630 # Adjust rates if necessary
2631 if ($rate == 0 && $hit > 0) {
2632 $rate = sprintf("%.*f", $precision, 1 / 10**$precision);
2633 } elsif ($rate == 100 && $hit != $found) {
2634 $rate = sprintf("%.*f", $precision, 100 - 1 / 10**$precision);
2635 }
2636
2637 return sprintf("%*s", $width, $rate . $suffix);
2638 }
2639
2640 #
2641 # get_overall_line(found, hit, type)
2642 #
2643 # Return a string containing overall information for the specified
2644 # found/hit data.
2645 #
2646
2647 sub get_overall_line($$$)
2648 {
2649 my ($found, $hit, $name) = @_;
2650 return "no data found" if (!defined($found) || $found == 0);
2651
2652 my $plural =
2653 ($found == 1) ? "" : (('ch' eq substr($name, -2, 2)) ? 'es' : 's');
2654
2655 return lcovutil::rate($hit, $found, "% ($hit of $found $name$plural)");
2656 }
2657
2658 # Make sure precision is within valid range [1:4]
2659 sub check_precision()
2660 {
2661 die("specified precision is out of range (1 to 4)\n")
2662 if ($default_precision < 1 || $default_precision > 4);
2663 }
2664
2665 # use vanilla color palette.
2666 sub use_vanilla_color()
2667 {
2668 for my $tla (('CBC', 'GNC', 'GIC', 'GBC')) {
2669 $lcovutil::tlaColor{$tla} = "#CAD7FE";
2670 $lcovutil::tlaTextColor{$tla} = "#98A0AA";
2671 }
2672 for my $tla (('UBC', 'UNC', 'UIC', 'LBC')) {
2673 $lcovutil::tlaColor{$tla} = "#FF6230";
2674 $lcovutil::tlaTextColor{$tla} = "#AA4020";
2675 }
2676 for my $tla (('EUB', 'ECB')) {
2677 $lcovutil::tlaColor{$tla} = "#FFFFFF";
2678 $lcovutil::tlaTextColor{$tla} = "#AAAAAA";
2679 }
2680 }
2681
2682 my $didFirstExistenceCheck;
2683
2684 sub fileExistenceBeforeCallbackError
2685 {
2686 my $filename = shift;
2687 if ($lcovutil::check_file_existence_before_callback &&
2688 !-e $filename) {
2689
2690 my $explanation =
2691 $didFirstExistenceCheck ? '' :
2692 ' Use \'check_existence_before_callback = 0\' config file option to remove this check.';
2693 lcovutil::ignorable_error($lcovutil::ERROR_SOURCE,
2694 "\"$filename\" does not exist." . $explanation);
2695 $didFirstExistenceCheck = 1;
2696 return 1;
2697 }
2698 return 0;
2699 }
2700
2701 # figure out what file version we see
2702 sub extractFileVersion
2703 {
2704 my $filename = shift;
2705
2706 return undef
2707 unless $versionCallback;
2708 return $versionCache{$filename} if exists($versionCache{$filename});
2709
2710 return undef if fileExistenceBeforeCallbackError($filename);
2711
2712 my $start = Time::HiRes::gettimeofday();
2713 my $version;
2714 eval { $version = $versionCallback->extract_version($filename); };
2715 if ($@) {
2716 my $context = MessageContext::context();
2717 lcovutil::ignorable_error($lcovutil::ERROR_CALLBACK,
2718 "extract_version($filename) failed$context: $@");
2719 }
2720 my $end = Time::HiRes::gettimeofday();
2721 if (exists($lcovutil::profileData{version}) &&
2722 exists($lcovutil::profileData{version}{$filename})) {
2723 $lcovutil::profileData{version}{$filename} += $end - $start;
2724 } else {
2725 $lcovutil::profileData{version}{$filename} = $end - $start;
2726 }
2727 $versionCache{$filename} = $version;
2728 return $version;
2729 }
2730
2731 sub checkVersionMatch
2732 {
2733 my ($filename, $me, $you, $reason, $silent) = @_;
2734
2735 return 1
2736 if defined($me) && defined($you) && $me eq $you; # simple string compare
2737
2738 if ($versionCallback) {
2739 # work harder
2740 my $status;
2741 eval {
2742 $status = $versionCallback->compare_version($you, $me, $filename);
2743 };
2744 if ($@) {
2745 my $context = MessageContext::context();
2746 lcovutil::ignorable_error($lcovutil::ERROR_CALLBACK,
2747 "compare_version($you, $me, $filename) failed$context: $@");
2748 $status = 1;
2749 }
2750 lcovutil::info(1, "compare_version: $status\n");
2751 return 1 unless $status; # match if return code was zero
2752 }
2753 lcovutil::ignorable_error($ERROR_VERSION,
2754 (defined($reason) ? ($reason . ' ') : '') .
2755 "$filename: revision control version mismatch: " .
2756 (defined($me) ? $me : 'undef') .
2757 ' <- ' . (defined($you) ? $you : 'undef'))
2758 unless $silent;
2759 # claim mismatch unless $me and $you are both undef
2760 return !(defined($me) || defined($you));
2761 }
2762
2763 #
2764 # parse_w3cdtf(date_string)
2765 #
2766 # Parse date string in W3CDTF format into DateTime object.
2767 #
2768 my $have_w3cdtf;
2769
2770 sub parse_w3cdtf($)
2771 {
2772 if (!defined($have_w3cdtf)) {
2773 # check to see if the package is here for us to use..
2774 $have_w3cdtf = 1;
2775 eval {
2776 require DateTime::Format::W3CDTF;
2777 DateTime::Format::W3CDTF->import();
2778 };
2779 if ($@) {
2780 # package not there - fall back
2781 lcovutil::ignorable_warning($lcovutil::ERROR_PACKAGE,
2782 'package DateTime::Format::W3CDTF is not available - falling back to local implementation'
2783 );
2784 $have_w3cdtf = 0;
2785 }
2786 }
2787 my $str = shift;
2788 if ($have_w3cdtf) {
2789 return DateTime::Format::W3CDTF->parse_datetime($str);
2790 }
2791
2792 my ($year, $month, $day, $hour, $min, $sec, $ns, $tz) =
2793 (0, 1, 1, 0, 0, 0, 0, "Z");
2794
2795 if ($str =~ /^(\d\d\d\d)$/) {
2796 # YYYY
2797 $year = $1;
2798 } elsif ($str =~ /^(\d\d\d\d)-(\d\d)$/) {
2799 # YYYY-MM
2800 $year = $1;
2801 $month = $2;
2802 } elsif ($str =~ /^(\d\d\d\d)-(\d\d)-(\d\d)$/) {
2803 # YYYY-MM-DD
2804 $year = $1;
2805 $month = $2;
2806 $day = $3;
2807 } elsif (
2808 $str =~ /^(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d)(Z|[+-]\d\d:\d\d)?$/) {
2809 # YYYY-MM-DDThh:mmTZD
2810 $year = $1;
2811 $month = $2;
2812 $day = $3;
2813 $hour = $4;
2814 $min = $5;
2815 $tz = $6 if defined($6);
2816 } elsif ($str =~
2817 /^(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)(Z|[+-]\d\d:\d\d)?$/) {
2818 # YYYY-MM-DDThh:mm:ssTZD
2819 $year = $1;
2820 $month = $2;
2821 $day = $3;
2822 $hour = $4;
2823 $min = $5;
2824 $sec = $6;
2825 $tz = $7 if (defined($7));
2826 } elsif ($str =~
2827 /^(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)\.(\d+)(Z|[+-]\d\d:\d\d)?$/
2828 ) {
2829 # YYYY-MM-DDThh:mm:ss.sTZD
2830 $year = $1;
2831 $month = $2;
2832 $day = $3;
2833 $hour = $4;
2834 $min = $5;
2835 $sec = $6;
2836 $ns = substr($7 . "00000000", 0, 9);
2837 $tz = $8 if (defined($8));
2838 } else {
2839 die("Invalid W3CDTF date format: $str\n");
2840 }
2841
2842 return
2843 DateTime->new(year => $year,
2844 month => $month,
2845 day => $day,
2846 hour => $hour,
2847 minute => $min,
2848 second => $sec,
2849 nanosecond => $ns,
2850 time_zone => $tz,);
2851 }
2852
2853 package HTML_fileData;
2854
2855 use constant {
2856 NAME => 0,
2857 PARENT => 1,
2858 HREFS => 2,
2859 ANCHORS => 3,
2860 };
2861
2862 sub new
2863 {
2864 my ($class, $parentDir, $filename) = @_;
2865
2866 my $self = [$parentDir, $filename, [], {}];
2867
2868 my $name = File::Spec->catfile($parentDir, $filename);
2869
2870 open(HTML, '<', $name) or die("unable to open $name: $!");
2871 while (<HTML>) {
2872 if (/<(a|span) .*id=\"([^\"]+)\"/) {
2873 lcovutil::ignorable_error($lcovutil::ERROR_USAGE,
2874 "\"$name\":$.: duplicate anchor '$2' original at " .
2875 $self->[ANCHORS]->{$2} . '.')
2876 if exists($self->[ANCHORS]->{$2});
2877 $self->[ANCHORS]->{$2} = $.;
2878 } elsif (/<a .*href=\"([^#\"]+)(#([^\"]+))?\"/) {
2879 next if 'http' eq substr($1, 0, 4);
2880 push(@{$self->[HREFS]}, [$., $1, $3]); # lineNo, filename, anchor
2881 } elsif (/<frame .*src=\"([^\"]+)\"/) {
2882 push(@{$self->[HREFS]}, [$., $1, $3]); # lineNo, filename, anchor
2883 }
2884 }
2885 close(HTML) or die("unable to close $name: $!");
2886
2887 return bless $self, $class;
2888 }
2889
2890 sub verifyAnchor
2891 {
2892 my ($self, $anchor) = @_;
2893
2894 return exists($self->[ANCHORS]->{$anchor});
2895 }
2896
2897 sub hrefs
2898 {
2899 my $self = shift;
2900 return $self->[HREFS];
2901 }
2902
2903 package ValidateHTML;
2904
2905 sub new
2906 {
2907 my ($class, $topDir, $htmlExt) = @_;
2908 my $self = {};
2909
2910 $htmlExt = '.html' unless defined($htmlExt);
2911
2912 my @dirstack = ($topDir);
2913 my %visited;
2914 while (@dirstack) {
2915 my $top = pop(@dirstack);
2916 die("unexpected link $top") if -l $top;
2917 opendir(my $dh, $top) or die("can't open directory $top: $!");
2918 while (my $e = readdir($dh)) {
2919 next if $e eq '.' || $e eq '..';
2920 my $p = File::Spec->catfile($top, $e);
2921 die("unexpected link $p") if -l $p;
2922 if (-d $p) {
2923 die("already visited $p") if exists($visited{$p});
2924 $visited{$p} = [$top, $e];
2925 push(@dirstack, $p);
2926 } elsif (-f $p &&
2927 $p =~ /.+$htmlExt$/) {
2928 die("duplicate file $p??") if exists($self->{$p});
2929 lcovutil::info(1, "schedule $p\n");
2930 $self->{$p} = HTML_fileData->new($top, $e);
2931 }
2932 }
2933 closedir($dh);
2934 }
2935 my %fileReferred;
2936 while (my ($filename, $data) = each(%$self)) {
2937 my $dir = File::Basename::dirname($filename);
2938 lcovutil::info(1, "verify $filename:\n");
2939 foreach my $href (@{$data->hrefs()}) {
2940 my ($lineNo, $link, $anchor) = @$href;
2941 my $path = File::Spec->catfile($dir, $link);
2942 $path = File::Spec->abs2rel(Cwd::realpath($path), $main::cwd)
2943 unless exists($self->{$path});
2944 lcovutil::info(1,
2945 " $lineNo: $link" . ($anchor ? "#$anchor" : '') . "\n");
2946 unless (exists($self->{$path})) {
2947 lcovutil::ignorable_error($lcovutil::ERROR_PATH,
2948 "\"$filename\":$lineNo: non-existent file '$link'.");
2949 next;
2950 }
2951 if (exists($fileReferred{$path})) {
2952 # keep only one use
2953 push(@{$fileReferred{$path}}, $filename)
2954 if ($fileReferred{$path}->[-1] ne $filename);
2955 } else {
2956 $fileReferred{$path} = [$filename];
2957 }
2958
2959 if (defined($anchor)) {
2960 my $a = $self->{$path};
2961 unless ($a->verifyAnchor($anchor)) {
2962 lcovutil::ignorable_error($lcovutil::ERROR_PATH,
2963 "\"$filename\":$lineNo: \"$link#$anchor\" doesn't point to valid anchor."
2964 );
2965 }
2966 }
2967 }
2968 }
2969
2970 while (my ($filename, $data) = each(%$self)) {
2971 lcovutil::ignorable_error($lcovutil::ERROR_UNUSED,
2972 "HTML file \"$filename\" is not referenced.")
2973 unless (exists($fileReferred{$filename}) ||
2974 ($topDir eq File::Basename::dirname($filename) &&
2975 "index$htmlExt" eq File::Basename::basename($filename)));
2976 }
2977 return bless $self, $class;
2978 }
2979
2980 package CoverageCriteria;
2981
2982 our @coverageCriteriaScript;
2983 our $criteriaCallback;
2984 our %coverageCriteria; # hash of name->(type, success 0/1, string)
2985 our $coverageCriteriaStatus = 0; # set to non-zero if we see any errors
2986 our @criteriaCallbackTypes; # include date, owner bin info
2987 our @criteriaCallbackLevels; # call back at (top, directory, file) levels
2988
2989 sub executeCallback
2990 {
2991 my ($type, $name, $data) = @_;
2992
2993 my ($status, $msgs);
2994 eval {
2995 ($status, $msgs) =
2996 $criteriaCallback->check_criteria($name, $type, $data);
2997 };
2998 if ($@) {
2999 my $context = MessageContext::context();
3000 lcovutil::ignorable_error($lcovutil::ERROR_CALLBACK,
3001 "check_criteria failed$context: $@");
3002 $status = 2;
3003 $msgs = [$@];
3004 }
3005
3006 $coverageCriteria{$name} = [$type, $status, $msgs]
3007 if (0 != $status ||
3008 (defined $msgs &&
3009 0 != scalar(@$msgs)));
3010 $coverageCriteriaStatus = $status
3011 if $status != 0;
3012 }
3013
3014 sub check_failUnder
3015 {
3016 my $info = shift;
3017 my $msg = $info->check_fail_under_criteria();
3018 if ($msg) {
3019 $coverageCriteriaStatus |= 1;
3020 $coverageCriteria{'top'} = ['top', 1, [$msg]];
3021 }
3022 }
3023
3024 sub summarize
3025 {
3026 # print the criteria summary to stdout:
3027 # all criteria fails + any non-empty messages
3028 # In addition: print fails to stderr
3029 # This way: Jenkins script can log failure if stderr is not empty
3030 my $leader = '';
3031 if ($coverageCriteriaStatus != 0) {
3032 print("Failed coverage criteria:\n");
3033 } else {
3034 $leader = "Coverage criteria:\n";
3035 }
3036 # sort to print top-level report first, then directories, then files.
3037 foreach my $name (sort({
3038 my $da = $coverageCriteria{$a};
3039 my $db = $coverageCriteria{$b};
3040 my $ta = $da->[0];
3041 my $tb = $db->[0];
3042 return -1 if ($ta eq 'top');
3043 return 1 if ($tb eq 'top');
3044 if ($ta ne $tb) {
3045 return $ta eq 'file' ? 1 : -1;
3046 }
3047 $a cmp $b
3048 }
3049 keys(%coverageCriteria))
3050 ) {
3051 my $criteria = $coverageCriteria{$name};
3052 my $v = $criteria->[1];
3053 next if (!$v || $v == 0) && 0 == scalar(@{$criteria->[2]}); # passed
3054
3055 my $msg = $criteria->[0];
3056 if ($criteria->[0] ne 'top') {
3057 $msg .= " \"" . $name . "\"";
3058 }
3059 $msg .= ": \"" . join(' ', @{$criteria->[2]}) . "\"\n";
3060 print($leader);
3061 $leader = '';
3062 print(" " . $msg);
3063 if (0 != $criteria->[1]) {
3064 print(STDERR $msg);
3065 }
3066 }
3067 }
3068
3069 package MessageContext;
3070
3071 our @message_context;
3072
3073 sub new
3074 {
3075 my ($class, $str) = @_;
3076 push(@message_context, $str);
3077 my $self = [$str];
3078 return bless $self, $class;
3079 }
3080
3081 sub context
3082 {
3083 my $context = join(' while ', @message_context);
3084 $context = ' while ' . $context if $context;
3085 return $context;
3086 }
3087
3088 sub DESTROY
3089 {
3090 my $self = shift;
3091 die('unbalanced context "' . $self->[0] . '" not head of ("' .
3092 join('" "', @message_context) . '")')
3093 unless scalar(@message_context) && $self->[0] eq $message_context[-1];
3094 pop(@message_context);
3095 }
3096
3097 package PipeHelper;
3098
3099 sub new
3100 {
3101 my $class = shift;
3102 my $reason = shift;
3103
3104 # backward compatibility: see if the arguments were passed in a
3105 # one long string
3106 my $args = \@_;
3107 my $arglen = 'criteria' eq $reason ? 4 : 2;
3108 if ($arglen == scalar(@_) && !-e $_[0]) {
3109 # two arguments: a string (which seems not to be executable) and the
3110 # file we are acting on
3111 # After next release, issue 'deprecated' warning here.
3112 my @args = split(' ', $_[0]);
3113 push(@args, splice(@_, 1)); # append the rest of the args
3114 $args = \@args;
3115 }
3116
3117 my $self = [$reason, join(' ', @$args)];
3118 bless $self, $class;
3119 if (open(PIPE, "-|", @$args)) {
3120 push(@$self, \*PIPE);
3121 } else {
3122 lcovutil::ignorable_error($lcovutil::ERROR_CALLBACK,
3123 "$reason: 'open(-| " . $self->[1] . ")' failed: \"$!\"");
3124 return undef;
3125 }
3126 return $self;
3127 }
3128
3129 sub next
3130 {
3131 my $self = shift;
3132 die("no handle") unless scalar(@$self) == 3;
3133 my $hdl = $self->[2];
3134 return scalar <$hdl>;
3135 }
3136
3137 sub close
3138 {
3139 # close pipe and return exit status
3140 my ($self, $checkError) = @_;
3141 close($self->[2]);
3142 if (0 != $? && $checkError) {
3143 # $reason: $cmd returned non-zero exit...
3144 lcovutil::ignorable_error($lcovutil::ERROR_CALLBACK,
3145 $self->[0] . ' \'' . $self->[1] .
3146 "\' returned non-zero exit code: '$!'");
3147 }
3148 pop(@$self);
3149 return $?;
3150 }
3151
3152 sub DESTROY
3153 {
3154 my $self = shift;
3155 # FD can be undef if 'open' failed for any reason (e.g., filesystem issues)
3156 # otherwise: don't close if FD was STDIN or STDOUT
3157 CORE::close($self->[2])
3158 if 3 == scalar(@$self);
3159 }
3160
3161 package ScriptCaller;
3162
3163 sub new
3164 {
3165 my $class = shift;
3166 my $self = [@_];
3167 return bless $self, $class;
3168 }
3169
3170 sub call
3171 {
3172 my ($self, $reason, @args) = @_;
3173 my $cmd = join(' ', @$self) . ' ' . join(' ', @args);
3174 lcovutil::info(1, "$reason: \"$cmd\"\n");
3175 my $rtn = `$cmd`;
3176 return $?;
3177 }
3178
3179 sub pipe
3180 {
3181 my $self = shift;
3182 my $reason = shift;
3183 return PipeHelper->new($reason, @$self, @_);
3184 }
3185
3186 sub context
3187 {
3188 my $self = shift;
3189 lcovutil::info(1, 'context ' . join(' ', @$self) . "\n");
3190 my $iter = $self->pipe('context');
3191 return unless defined($iter);
3192 my %context;
3193 while (my $line = $iter->next()) {
3194 chomp($line);
3195 $line =~ s/\r//g; # remove CR from line-end
3196 # first word on line is the key..
3197 my ($key, $value) = split(/ +/, $line, 2);
3198 if (exists($context{key})) {
3199 $context{key} .= "\n" . $value;
3200 } else {
3201 $context{key} = $value;
3202 }
3203 }
3204 my $status = $iter->close(1); # check error return
3205
3206 return \%context;
3207 }
3208
3209 sub extract_version
3210 {
3211 my ($self, $filename) = @_;
3212 my $version;
3213 my $pipe = $self->pipe('extract_version', $filename);
3214 if (defined $pipe &&
3215 ($version = $pipe->next())) {
3216 chomp($version);
3217 $version =~ s/\r//;
3218 lcovutil::info(1, " version: $version\n");
3219 }
3220 return $version;
3221 }
3222
3223 sub resolve
3224 {
3225 my ($self, $filename) = @_;
3226 my $path;
3227 my $pipe = $self->pipe('resolve_filename', $filename);
3228 if ($pipe &&
3229 ($path = $pipe->next())) {
3230 chomp($path);
3231 $path =~ s/\r//;
3232 lcovutil::info(1, " resolve: $path\n");
3233 }
3234 return $path;
3235 }
3236
3237 sub compare_version
3238 {
3239 my ($self, $yours, $mine, $file) = @_;
3240 return
3241 $self->call('compare_version', '--compare',
3242 "'$yours'", "'$mine'",
3243 "'$file'");
3244 }
3245
3246 # annotate callback is passed filename (as munged) -
3247 # should return reference to array of line data,
3248 # line data of the form list of:
3249 # source_text: the content on that line
3250 # abbreviated author name: (must be set to something - possibly NONE
3251 # full author name: some string or undef
3252 # date string: when this line was last changed
3253 # commit ID: something meaningful to you
3254 sub annotate
3255 {
3256 my ($self, $filename) = @_;
3257 lcovutil::info(1, 'annotate ' . join(' ', @$self) . ' ' . $filename . "\n");
3258 my $iter = $self->pipe('annotate', $filename);
3259 return unless defined($iter);
3260 my @lines;
3261 while (my $line = $iter->next()) {
3262 chomp $line;
3263 $line =~ s/\r//g; # remove CR from line-end
3264
3265 my ($commit, $author, $when, $text) = split(/\|/, $line, 4);
3266 # semicolon is not a legal character in email address -
3267 # so we use that to delimit the 'abbreviated name' and
3268 # the 'full name' - in case they are different.
3269 # this is an attempt to be backward-compatible with
3270 # existing annotation scripts which return only one name
3271 my ($abbrev, $full) = split(/;/, $author, 2);
3272 push(@lines, [$text, $abbrev, $full, $when, $commit]);
3273 }
3274 my $status = $iter->close();
3275
3276 return ($status, \@lines);
3277 }
3278
3279 sub check_criteria
3280 {
3281 my ($self, $name, $type, $data) = @_;
3282
3283 my $iter =
3284 $self->pipe('criteria', $name, $type, JsonSupport::encode($data));
3285 return (0) unless $iter; # constructor will have given error message
3286 my @messages;
3287 while (my $line = $iter->next()) {
3288 chomp $line;
3289 $line =~ s/\r//g; # remove CR from line-end
3290 next if '' eq $line;
3291 push(@messages, $line);
3292 }
3293 return ($iter->close(), \@messages);
3294 }
3295
3296 sub select
3297 {
3298 my ($self, $lineData, $annotateData, $filename, $lineNo) = @_;
3299
3300 my @params = ('select',
3301 defined($lineData) ?
3302 JsonSupport::encode($lineData->to_list()) : '',
3303 defined($annotateData) ?
3304 JsonSupport::encode($annotateData->to_list()) : '',
3305 $filename,
3306 $lineNo);
3307 return $self->call(@params);
3308 }
3309
3310 package JsonSupport;
3311
3312 our $rc_json_module = 'auto';
3313
3314 our $did_init;
3315
3316 #
3317 # load_json_module(rc)
3318 #
3319 # If RC is "auto", load best available JSON module from a list of alternatives,
3320 # otherwise load the module specified by RC.
3321 #
3322 sub load_json_module($)
3323 {
3324 my ($rc) = shift;
3325 # List of alternative JSON modules to try
3326 my @alternatives = ("JSON::XS", # Fast, but not always installed
3327 "Cpanel::JSON::XS", # Fast, a more recent fork
3328 "JSON::PP", # Slow, part of core-modules
3329 "JSON", # Not available in all distributions
3330 );
3331
3332 # Determine JSON module
3333 if (lc($rc) eq "auto") {
3334 for my $m (@alternatives) {
3335 if (Module::Load::Conditional::check_install(module => $m)) {
3336 $did_init = $m;
3337 last;
3338 }
3339 }
3340
3341 if (!defined($did_init)) {
3342 die("No Perl JSON module found on your system. Please install of of the following supported modules: "
3343 . join(" ", @alternatives)
3344 . " - for example (as root):\n \$ perl -MCPAN -e 'install "
3345 . $alternatives[0]
3346 . "'\n");
3347 }
3348 } else {
3349 $did_init = $rc;
3350 }
3351
3352 eval "use $did_init qw(encode_json decode_json);";
3353 if ($@) {
3354 die("Module is not installed: " . "'$did_init':$@\n");
3355 }
3356 lcovutil::info(1, "Using JSON module $did_init\n");
3357 my ($index) =
3358 grep { $alternatives[$_] eq $did_init } (0 .. @alternatives - 1);
3359 warn(
3360 "using JSON module \"$did_init\" - which is much slower than some alternatives. Consider installing one of "
3361 . join(" or ", @alternatives[0 .. $index - 1]))
3362 if (defined($index) && $index > 1);
3363 }
3364
3365 sub encode($)
3366 {
3367 my $data = shift;
3368
3369 load_json_module($rc_json_module)
3370 unless defined($did_init);
3371
3372 return encode_json($data);
3373 }
3374
3375 sub decode($)
3376 {
3377 my $text = shift;
3378 load_json_module($rc_json_module)
3379 unless defined($did_init);
3380
3381 return decode_json($text);
3382 }
3383
3384 sub load($)
3385 {
3386 my $filename = shift;
3387 my $f = InOutFile->in($filename);
3388 my $h = $f->hdl();
3389 my @lines = <$h>;
3390 return decode(join("\n", @lines));
3391 }
3392
3393 package InOutFile;
3394
3395 our $checkedGzipAvail;
3396
3397 sub checkGzip
3398 {
3399 # Check for availability of GZIP tool
3400 lcovutil::system_no_output(1, "gzip", "-h") and
3401 die("gzip command not available!\n");
3402 $checkedGzipAvail = 1;
3403 }
3404
3405 sub out
3406 {
3407 my ($class, $f, $mode, $demangle) = @_;
3408 $demangle = 0 unless defined($demangle);
3409
3410 my $self = [undef, $f];
3411 bless $self, $class;
3412 my $m = (defined($mode) && $mode eq 'append') ? ">>" : ">";
3413
3414 if (!defined($f) ||
3415 '-' eq $f) {
3416 if ($demangle) {
3417 open(HANDLE, '|-', $lcovutil::demangle_cpp_cmd) or
3418 die("unable to demangle: $!\n");
3419 $self->[0] = \*HANDLE;
3420 } else {
3421 $self->[0] = \*STDOUT;
3422 }
3423 } else {
3424 my $cmd = $demangle ? "$lcovutil::demangle_cpp_cmd " : '';
3425 if ($f =~ /\.gz$/) {
3426 checkGzip()
3427 unless defined($checkedGzipAvail);
3428 $cmd .= '| ' if $cmd;
3429 # Open compressed file
3430 $cmd .= "gzip -c $m'$f'";
3431 open(HANDLE, "|-", $cmd) or
3432 die("cannot start gzip to compress to file $f: $!\n");
3433 } else {
3434 if ($demangle) {
3435 $cmd .= "$m '$f'";
3436 } else {
3437 $cmd .= $f;
3438 }
3439 open(HANDLE, $demangle ? '|-' : $m, $cmd) or
3440 die("cannot write to $f: $!\n");
3441 }
3442 $self->[0] = \*HANDLE;
3443 }
3444 return $self;
3445 }
3446
3447 sub in
3448 {
3449 my ($class, $f, $demangle) = @_;
3450 $demangle = 0 unless defined($demangle);
3451
3452 my $self = [undef, $f];
3453 bless $self, $class;
3454
3455 if (!defined($f) ||
3456 '-' eq $f) {
3457 $self->[0] = \*STDIN;
3458 } else {
3459 if ($f =~ /\.gz$/) {
3460
3461 checkGzip()
3462 unless defined($checkedGzipAvail);
3463
3464 die("file '$f' does not exist\n")
3465 unless -f $f;
3466 die("'$f': unsupported empty gzipped file\n")
3467 if (-z $f);
3468 # Check integrity of compressed file - fails for zero size file
3469 lcovutil::system_no_output(1, "gzip", "-dt", $f) and
3470 die("integrity check failed for compressed file $f!\n");
3471
3472 # Open compressed file
3473 my $cmd = "gzip -cd '$f'";
3474 $cmd .= " | " . $lcovutil::demangle_cpp_cmd
3475 if ($demangle);
3476 open(HANDLE, "-|", $cmd) or
3477 die("cannot start gunzip to decompress file $f: $!\n");
3478
3479 } elsif ($demangle &&
3480 defined($lcovutil::demangle_cpp_cmd)) {
3481 open(HANDLE, "-|", "cat '$f' | $lcovutil::demangle_cpp_cmd") or
3482 die("cannot start demangler for file $f: $!\n");
3483 } else {
3484 # Open decompressed file
3485 open(HANDLE, "<", $f) or
3486 die("cannot read file $f: $!\n");
3487 }
3488 $self->[0] = \*HANDLE;
3489 }
3490 return $self;
3491 }
3492
3493 sub DESTROY
3494 {
3495 my $self = shift;
3496 # FD can be undef if 'open' failed for any reason (e.g., filesystem issues)
3497 # otherwise: don't close if FD was STDIN or STDOUT
3498 close($self->[0])
3499 unless !defined($self->[1]) ||
3500 '-' eq $self->[1] ||
3501 !defined($self->[0]);
3502 }
3503
3504 sub hdl
3505 {
3506 my $self = shift;
3507 return $self->[0];
3508 }
3509
3510 package SearchPath;
3511
3512 sub new
3513 {
3514 my $class = shift;
3515 my $option = shift;
3516 my $self = [];
3517 bless $self, $class;
3518 foreach my $p (@_) {
3519 if (-d $p) {
3520 push(@$self, [$p, 0]);
3521 } else {
3522 lcovutil::ignorable_error($lcovutil::ERROR_PATH,
3523 "$option '$p' is not a directory");
3524 }
3525 }
3526 return $self;
3527 }
3528
3529 sub patterns
3530 {
3531 my $self = shift;
3532 return $self;
3533 }
3534
3535 sub resolve
3536 {
3537 my ($self, $filename, $applySubstitutions) = @_;
3538 $filename = lcovutil::subst_file_name($filename) if $applySubstitutions;
3539 return $filename if -e $filename;
3540 if (!File::Spec->file_name_is_absolute($filename)) {
3541 foreach my $d (@$self) {
3542 my $path = File::Spec->catfile($d->[0], $filename);
3543 if (-e $path) {
3544 lcovutil::info(1, "found $filename at $path\n");
3545 ++$d->[1];
3546 return $path;
3547 }
3548 }
3549 }
3550 return resolveCallback($filename, 0);
3551 }
3552
3553 sub resolveCallback
3554 {
3555 my ($filename, $applySubstitutions, $returnCbValue) = @_;
3556 $filename = lcovutil::subst_file_name($filename) if $applySubstitutions;
3557
3558 if ($lcovutil::resolveCallback) {
3559 return $lcovutil::resolveCache{$filename}
3560 if exists($lcovutil::resolveCache{$filename});
3561 my $start = Time::HiRes::gettimeofday();
3562 my $path;
3563 eval { $path = $resolveCallback->resolve($filename); };
3564 if ($@) {
3565 my $context = MessageContext::context();
3566 lcovutil::ignorable_error($lcovutil::ERROR_CALLBACK,
3567 "resolve($filename) failed$context: $@");
3568 }
3569 # look up particular path at most once...
3570 $lcovutil::resolveCache{$filename} = $path if $path;
3571 my $cost = Time::HiRes::gettimeofday() - $start;
3572 if (!$returnCbValue) {
3573 $path = $filename unless $path;
3574 }
3575 my $p = $path ? $path : $filename;
3576 if (exists($lcovutil::profileData{resolve}) &&
3577 exists($lcovutil::profileData{resolve}{$p})) {
3578 # might see multiple aliases for the same source file
3579 $lcovutil::profileData{resolve}{$p} += $cost;
3580 } else {
3581 $lcovutil::profileData{resolve}{$p} = $cost;
3582 }
3583 return $path;
3584 }
3585 return $filename;
3586 }
3587
3588 sub warn_unused
3589 {
3590 my ($self, $optName) = @_;
3591 foreach my $d (@$self) {
3592 my $name = $d->[0];
3593 $name = "'$name'" if $name =~ /\s/;
3594 if (0 == $d->[1]) {
3595 lcovutil::ignorable_error($lcovutil::ERROR_UNUSED,
3596 "\"$optName $name\" is unused.");
3597 } else {
3598 lcovutil::info(1,
3599 "\"$optName $name\" used " . $d->[1] . " times\n");
3600 }
3601 }
3602 }
3603
3604 sub reset
3605 {
3606 my $self = shift;
3607 foreach my $d (@$self) {
3608 $d->[1] = 0;
3609 }
3610 }
3611
3612 sub current_count
3613 {
3614 my $self = shift;
3615 my @rtn;
3616 foreach my $d (@$self) {
3617 push(@rtn, $d->[1]);
3618 }
3619 return \@rtn;
3620 }
3621
3622 sub update_count
3623 {
3624 my $self = shift;
3625 die("invalid update count: " . scalar(@$self) . ' ' . scalar(@_))
3626 unless ($#$self == $#_);
3627 foreach my $d (@$self) {
3628 $d->[1] += shift;
3629 }
3630 }
3631
3632 package MapData;
3633
3634 sub new
3635 {
3636 my $class = shift;
3637 my $self = {};
3638 bless $self, $class;
3639
3640 return $self;
3641 }
3642
3643 sub is_empty
3644 {
3645 my $self = shift;
3646 return 0 == scalar(keys %$self);
3647 }
3648
3649 sub append_if_unset
3650 {
3651 my $self = shift;
3652 my $key = shift;
3653 my $data = shift;
3654
3655 if (!defined($self->{$key})) {
3656 $self->{$key} = $data;
3657 }
3658 return $self;
3659 }
3660
3661 sub replace
3662 {
3663 my $self = shift;
3664 my $key = shift;
3665 my $data = shift;
3666
3667 $self->{$key} = $data;
3668
3669 return $self;
3670 }
3671
3672 sub value
3673 {
3674 my $self = shift;
3675 my $key = shift;
3676
3677 if (!exists($self->{$key})) {
3678 return undef;
3679 }
3680
3681 return $self->{$key};
3682 }
3683
3684 sub remove
3685 {
3686 my ($self, $key, $check_is_present) = @_;
3687
3688 if (!defined($check_is_present) || exists($self->{$key})) {
3689 delete $self->{$key};
3690 return 1;
3691 }
3692 return 0;
3693 }
3694
3695 sub mapped
3696 {
3697 my $self = shift;
3698 my $key = shift;
3699
3700 return defined($self->{$key}) ? 1 : 0;
3701 }
3702
3703 sub keylist
3704 {
3705 my $self = shift;
3706 return keys(%$self);
3707 }
3708
3709 sub entries
3710 {
3711 my $self = shift;
3712 return scalar(keys(%$self));
3713 }
3714
3715 # Class definitions
3716 package CountData;
3717
3718 our $UNSORTED = 0;
3719 our $SORTED = 1;
3720
3721 use constant {
3722 HASH => 0,
3723 SORTABLE => 1,
3724 FOUND => 2,
3725 HIT => 3,
3726 FILENAME => 4,
3727 };
3728
3729 sub new
3730 {
3731 my $class = shift;
3732 my $filename = shift;
3733 my $sortable = defined($_[0]) ? shift : $UNSORTED;
3734 my $self = [{},
3735 $sortable,
3736 0, # found
3737 0, # hit
3738 $filename, # for error messaging
3739 ];
3740 bless $self, $class;
3741
3742 return $self;
3743 }
3744
3745 sub filename
3746 {
3747 my $self = shift;
3748 return $self->[FILENAME];
3749 }
3750
3751 sub append
3752 {
3753 # return 1 if we hit something new, 0 if not (count was already non-zero)
3754 # using $suppressErrMsg to avoid reporting same thing for bot the
3755 # 'testcase' entry and the 'summary' entry
3756 my ($self, $key, $count, $suppressErrMsg) = @_;
3757 my $changed = 0; # hit something new or not
3758
3759 if (!Scalar::Util::looks_like_number($count)) {
3760 lcovutil::report_format_error($lcovutil::ERROR_FORMAT, 'hit', $count,
3761 'line "' . $self->filename() . ":$key\"")
3762 unless $suppressErrMsg;
3763 $count = 0;
3764 } elsif ($count < 0) {
3765 lcovutil::report_format_error($lcovutil::ERROR_NEGATIVE,
3766 'hit',
3767 $count,
3768 'line ' . $self->filename() . ":$key\""
3769 ) unless $suppressErrMsg;
3770 $count = 0;
3771 } elsif (defined($lcovutil::excessive_count_threshold) &&
3772 $count > $lcovutil::excessive_count_threshold) {
3773 lcovutil::report_format_error($lcovutil::ERROR_EXCESSIVE_COUNT,
3774 'hit',
3775 $count,
3776 'line ' . $self->filename() . ":$key\""
3777 ) unless $suppressErrMsg;
3778 }
3779 my $data = $self->[HASH];
3780 if (!exists($data->{$key})) {
3781 $changed = 1; # something new - whether we hit it or not
3782 $data->{$key} = $count;
3783 ++$self->[FOUND]; # found
3784 ++$self->[HIT] if ($count > 0); # hit
3785 } else {
3786 my $current = $data->{$key};
3787 if ($count > 0 &&
3788 $current == 0) {
3789 ++$self->[HIT];
3790 $changed = 1;
3791 }
3792 $data->{$key} = $count + $current;
3793 }
3794 return $changed;
3795 }
3796
3797 sub value
3798 {
3799 my $self = shift;
3800 my $key = shift;
3801
3802 my $data = $self->[HASH];
3803 if (!exists($data->{$key})) {
3804 return undef;
3805 }
3806 return $data->{$key};
3807 }
3808
3809 sub remove
3810 {
3811 my ($self, $key, $check_if_present) = @_;
3812
3813 my $data = $self->[HASH];
3814 if (!defined($check_if_present) ||
3815 exists($data->{$key})) {
3816
3817 die("$key not found")
3818 unless exists($data->{$key});
3819 --$self->[FOUND]; # found;
3820 --$self->[HIT] # hit
3821 if ($data->{$key} > 0);
3822
3823 delete $data->{$key};
3824 return 1;
3825 }
3826
3827 return 0;
3828 }
3829
3830 sub found
3831 {
3832 my $self = shift;
3833
3834 return $self->[FOUND];
3835 }
3836
3837 sub hit
3838 {
3839 my $self = shift;
3840
3841 return $self->[HIT];
3842 }
3843
3844 sub keylist
3845 {
3846 my $self = shift;
3847 return keys(%{$self->[HASH]});
3848 }
3849
3850 sub entries
3851 {
3852 my $self = shift;
3853 return scalar(keys(%{$self->[HASH]}));
3854 }
3855
3856 sub union
3857 {
3858 my $self = shift;
3859 my $info = shift;
3860
3861 my $changed = 0;
3862 foreach my $key ($info->keylist()) {
3863 if ($self->append($key, $info->value($key))) {
3864 $changed = 1;
3865 }
3866 }
3867 return $changed;
3868 }
3869
3870 sub intersect
3871 {
3872 my $self = shift;
3873 my $you = shift;
3874 my $changed = 0;
3875 my $yourData = $you->[HASH];
3876 foreach my $key ($self->keylist()) {
3877 if (exists($yourData->{$key})) {
3878 # append your count to mine
3879 if ($self->append($key, $you->value($key))) {
3880 # returns true if appended count was not zero
3881 $changed = 1;
3882 }
3883 } else {
3884 $self->remove($key);
3885 $changed = 1;
3886 }
3887 }
3888 return $changed;
3889 }
3890
3891 sub difference
3892 {
3893 my $self = shift;
3894 my $you = shift;
3895 my $changed = 0;
3896 my $yourData = $you->[HASH];
3897 foreach my $key ($self->keylist()) {
3898 if (exists($yourData->{$key})) {
3899 $self->remove($key);
3900 $changed = 1;
3901 }
3902 }
3903 return $changed;
3904 }
3905
3906 #
3907 # get_found_and_hit(hash)
3908 #
3909 # Return the count for entries (found) and entries with an execution count
3910 # greater than zero (hit) in a hash (linenumber -> execution count) as
3911 # a list (found, hit)
3912 #
3913 sub get_found_and_hit
3914 {
3915 my $self = shift;
3916 return ($self->[FOUND], $self->[HIT]);
3917 }
3918
3919 package BranchBlock;
3920 # branch element: index, taken/not-taken count, optional expression
3921 # for baseline or current data, 'taken' is just a number (or '-')
3922 # for differential data: 'taken' is an array [$taken, tla]
3923
3924 use constant {
3925 ID => 0,
3926 TAKEN => 1,
3927 EXPR => 2,
3928 EXCEPTION => 3,
3929 };
3930
3931 sub new
3932 {
3933 my ($class, $id, $taken, $expr, $is_exception) = @_;
3934 # if branchID is not an expression - go back to legacy behaviour
3935 my $self = [$id, $taken,
3936 (defined($expr) && $expr eq $id) ? undef : $expr,
3937 defined($is_exception) && $is_exception ? 1 : 0
3938 ];
3939 bless $self, $class;
3940 my $c = $self->count();
3941 if (!Scalar::Util::looks_like_number($c)) {
3942 lcovutil::report_format_error($lcovutil::ERROR_FORMAT,
3943 'taken', $c, 'branch ' . $self->id());
3944 $self->[TAKEN] = 0;
3945
3946 } elsif ($c < 0) {
3947 lcovutil::report_format_error($lcovutil::ERROR_NEGATIVE,
3948 'taken', $c, 'branch ' . $self->id());
3949 $self->[TAKEN] = 0;
3950 } elsif (defined($lcovutil::excessive_count_threshold) &&
3951 $c > $lcovutil::excessive_count_threshold) {
3952 lcovutil::report_format_error($lcovutil::ERROR_EXCESSIVE_COUNT,
3953 'taken', $c, 'branch ' . $self->id());
3954 }
3955 return $self;
3956 }
3957
3958 sub isTaken
3959 {
3960 my $self = shift;
3961 return $self->[TAKEN] ne '-';
3962 }
3963
3964 sub id
3965 {
3966 my $self = shift;
3967 return $self->[ID];
3968 }
3969
3970 sub data
3971 {
3972 my $self = shift;
3973 return $self->[TAKEN];
3974 }
3975
3976 sub count
3977 {
3978 my $self = shift;
3979 return $self->[TAKEN] eq '-' ? 0 : $self->[TAKEN];
3980 }
3981
3982 sub expr
3983 {
3984 my $self = shift;
3985 return $self->[EXPR];
3986 }
3987
3988 sub exprString
3989 {
3990 my $self = shift;
3991 my $e = $self->[EXPR];
3992 return defined($e) ? $e : 'undef';
3993 }
3994
3995 sub is_exception
3996 {
3997 my $self = shift;
3998 return $self->[EXCEPTION];
3999 }
4000
4001 sub merge
4002 {
4003 # return 1 if something changed, 0 if nothing new covered or discovered
4004 my ($self, $that, $filename, $line) = @_;
4005 # should have called 'iscompatible' first
4006 die('attempt to merge incompatible expressions for id' .
4007 $self->id() . ', ' . $that->id() .
4008 ": '" . $self->exprString() . "' -> '" . $that->exprString() . "'")
4009 if ($self->exprString() ne $that->exprString());
4010
4011 if ($self->is_exception() != $that->is_exception()) {
4012 my $loc = defined($filename) ? "\"$filename\":$line: " : '';
4013 lcovutil::ignorable_error($lcovutil::ERROR_MISMATCH,
4014 "${loc}mismatched exception tag for id " .
4015 $self->id() . ", " . $that->id() .
4016 ": '" . $self->is_exception() .
4017 "' -> '" . $that->is_exception() . "'");
4018 # set 'self' to 'not related to exception' - to give a consistent
4019 # answer for the merge operation. Otherwise, we pick whatever
4020 # was seen first - which is unpredictable during threaded execution.
4021 $self->[EXCEPTION] = 0;
4022 }
4023 my $t = $that->[TAKEN];
4024 return 0 if $t eq '-'; # no new news
4025
4026 my $count = $self->[TAKEN];
4027 my $changed;
4028 if ($count ne '-') {
4029 $count += $t;
4030 $changed = $count == 0 && $t != 0;
4031 } else {
4032 $count = $t;
4033 $changed = $t != 0;
4034 }
4035 $self->[TAKEN] = $count;
4036 return $changed;
4037 }
4038
4039 package BranchEntry;
4040 # hash of blockID -> array of BranchBlock refs for each sequential branch ID
4041
4042 sub new
4043 {
4044 my ($class, $line) = @_;
4045 my $self = [$line, {}];
4046 bless $self, $class;
4047 return $self;
4048 }
4049
4050 sub line
4051 {
4052 my $self = shift;
4053 return $self->[0];
4054 }
4055
4056 sub hasBlock
4057 {
4058 my ($self, $id) = @_;
4059 return exists($self->[1]->{$id});
4060 }
4061
4062 sub removeBlock
4063 {
4064 my ($self, $id, $branchData) = @_;
4065 $self->hasBlock($id) or die("unknown block $id");
4066
4067 # remove list of branches and adjust counts
4068 $branchData->removeBranches($self->[1]->{$id});
4069 delete($self->[1]->{$id});
4070 }
4071
4072 sub getBlock
4073 {
4074 my ($self, $id) = @_;
4075 $self->hasBlock($id) or die("unknown block $id");
4076 return $self->[1]->{$id};
4077 }
4078
4079 sub blocks
4080 {
4081 my $self = shift;
4082 return keys %{$self->[1]};
4083 }
4084
4085 sub addBlock
4086 {
4087 my ($self, $blockId) = @_;
4088
4089 !exists($self->[1]->{$blockId}) or die "duplicate block $blockId";
4090 my $blockData = [];
4091 $self->[1]->{$blockId} = $blockData;
4092 return $blockData;
4093 }
4094
4095 sub totals
4096 {
4097 my $self = shift;
4098 # return (found, hit) counts of coverpoints in this entry
4099 my $found = 0;
4100 my $hit = 0;
4101 foreach my $blockId ($self->blocks()) {
4102 my $bdata = $self->getBlock($blockId);
4103
4104 foreach my $br (@$bdata) {
4105 my $count = $br->count();
4106 ++$found;
4107 ++$hit if (0 != $count);
4108 }
4109 }
4110 return ($found, $hit);
4111 }
4112
4113 package MCDC_Block;
4114
4115 # there may be more than one MCDC groups on a particular line -
4116 # we hold the groups in a hash, keyed by size (number of MCDC_expressions)
4117 # The particular group is a sorted list
4118 use constant {
4119 LINE => 0,
4120 GROUPS => 1,
4121 };
4122
4123 sub new
4124 {
4125 my ($class, $line) = @_;
4126 my $self = [$line, {}];
4127
4128 return bless $self, $class;
4129 }
4130
4131 sub insertExpr
4132 {
4133 my ($self, $filename, $groupSize, $sense, $count, $idx, $expr) = @_;
4134 my $groups = $self->[GROUPS];
4135 my $group;
4136 if (exists($groups->{$groupSize})) {
4137 $group = $groups->{$groupSize};
4138 } else {
4139 $group = [];
4140 $groups->{$groupSize} = $group;
4141 }
4142 my $cond;
4143 if ($idx < scalar(@$group)) {
4144 $cond = $group->[$idx];
4145 if ($cond->expression() ne $expr) {
4146 lcovutil::ignorable_error($lcovutil::ERROR_INCONSISTENT_DATA,
4147 "\"$filename\":" . $self->line() .
4148 ": MC/DC group $groupSize expression $idx changed from '" .
4149 $cond->expression() . "' to '$expr'");
4150 }
4151 } else {
4152 if ($idx != scalar(@$group)) {
4153 lcovutil::ignorable_error($lcovutil::ERROR_FORMAT,
4154 "\"$filename\":" . '":' . $self->line() .
4155 ": MC/DC group $groupSize: non-contiguous expression '$idx' found - should be '"
4156 . scalar(@$group)
4157 . "'.");
4158 }
4159 $cond = MCDC_Expression->new($self, $groupSize, $idx, $expr);
4160 push(@$group, $cond);
4161 }
4162 $cond->set($sense, $count);
4163 }
4164
4165 sub line
4166 {
4167 return $_[0]->[LINE];
4168 }
4169
4170 sub totals
4171 {
4172 my $self = shift;
4173 my $found = 0;
4174 my $hit = 0;
4175 while (my ($size, $group) = each(%{$self->groups()})) {
4176 foreach my $expr (@$group) {
4177 foreach my $sense (0, 1) {
4178 my $count = $expr->count($sense);
4179 if ('ARRAY' eq ref($count)) {
4180 # differential number - report 'current'
4181 next unless defined($count->[2]); # not in current
4182 $count = $count->[2];
4183 }
4184 ++$found;
4185 ++$hit if 0 != $count;
4186 }
4187 }
4188 }
4189 return ($found, $hit);
4190 }
4191
4192 sub groups
4193 {
4194 return $_[0]->[GROUPS];
4195 }
4196
4197 sub num_groups
4198 {
4199 return scalar(keys %{$_[0]->[GROUPS]});
4200 }
4201
4202 sub expressions
4203 {
4204 my ($self, $size) = @_;
4205 return exists($self->[GROUPS]->{$size}) ? $self->[GROUPS]->{$size} : undef;
4206 }
4207
4208 sub expr
4209 {
4210 my ($self, $groupSize, $idx) = @_;
4211 return $self->[GROUPS]->{$groupSize}->[$idx];
4212 }
4213
4214 sub is_compatible
4215 {
4216 my ($self, $you) = @_;
4217
4218 my $yours = $you->groups();
4219 my $groups = $self->groups();
4220 foreach my $size (keys %$groups) {
4221 next unless exists($yours->{$size});
4222 my $idx = 0;
4223 my $m = $groups->{$size};
4224 my $y = $yours->{$size};
4225 foreach my $e (@$m) {
4226 my $ye = $y->[$idx++];
4227 return 0 if $e->expression() ne $ye->expression();
4228 }
4229 }
4230 return 1;
4231 }
4232
4233 sub merge
4234 {
4235 # merge all groups from you into me
4236 my ($self, $you) = @_;
4237
4238 my $mine = $self->groups();
4239 my $yours = $you->groups();
4240 my $changed = 0;
4241 while (my ($size, $group) = each(%$yours)) {
4242 if (exists($mine->{$size})) {
4243 my $m = $mine->{$size};
4244 my $idx = 0;
4245 foreach my $e (@$m) {
4246 my $y = $group->[$idx++];
4247 $changed += $e->set(1, $y->count(1));
4248 $changed += $e->set(0, $y->count(0));
4249 }
4250 } else {
4251 $mine->{$size} = Storable::dclone($group);
4252 $changed = 1;
4253 }
4254 }
4255 return $changed;
4256 }
4257
4258 package MCDC_Expression;
4259
4260 use constant {
4261 PARENT => 0, # MCDC_BLOCK
4262 GROUP_SIZE => 1, # which group in parent
4263 INDEX => 2, # index of this expression
4264
4265 EXPRESSION => 3,
4266 TRUE => 4, # hit count of sensitization of 'true' sense of expr
4267 FALSE => 5, # hit count of sensitization of 'false' sense of expr
4268 };
4269
4270 sub new
4271 {
4272 my ($class, $parent, $groupSize, $idx, $expr) = @_;
4273
4274 my $self = [$parent, $groupSize, $idx, $expr, 0, 0];
4275 return bless $self, $class;
4276 }
4277
4278 sub set
4279 {
4280 # 'sense' should be 0 or 1 - for 'false' and 'true' sense, respectively
4281 my ($self, $sense, $count) = @_;
4282 return 0 if 0 == $count;
4283
4284 if ('ARRAY' eq ref($count)) {
4285 # recording a differential result
4286 $self->[$sense ? TRUE : FALSE] = $count;
4287 return 1; # assumed changed
4288 }
4289 my $changed = $count && $self->count($sense) == 0;
4290 $self->[$sense ? TRUE : FALSE] += $count;
4291 return $changed;
4292 }
4293
4294 sub parent
4295 {
4296 return $_[0]->[PARENT];
4297 }
4298
4299 sub groupSize
4300 {
4301 return $_[0]->[GROUP_SIZE];
4302 }
4303
4304 sub index
4305 {
4306 return $_[0]->[INDEX];
4307 }
4308
4309 sub expression
4310 {
4311 return $_[0]->[EXPRESSION];
4312 }
4313
4314 sub count
4315 {
4316 my ($self, $sense) = @_;
4317 return $_[0]->[$sense ? TRUE : FALSE];
4318 }
4319
4320 package FunctionEntry;
4321 # keep track of all the functions/all the function aliases
4322 # at a particular line in the file. THey must all be the
4323 # same function - perhaps just templatized differently.
4324
4325 use constant {
4326 NAME => 0,
4327 ALIASES => 1,
4328 MAP => 2,
4329 FIRST => 3, # start line
4330 COUNT => 4,
4331 LAST => 5,
4332 };
4333
4334 sub new
4335 {
4336 my ($class, $name, $map, $startLine, $endLine) = @_;
4337 die("unexpected type " . ref($map)) unless 'FunctionMap' eq ref($map);
4338 my %aliases = ($name => 0); # not hit, yet
4339 my $self = [$name, \%aliases, $map, $startLine, 0, $endLine];
4340
4341 bless $self, $class;
4342 return $self;
4343 }
4344
4345 sub cloneWithEndLine
4346 {
4347 my ($self, $withEnd, $cloneAliases) = @_;
4348 my $fn = FunctionEntry->new($self->[NAME], $self->[MAP], $self->[FIRST],
4349 $withEnd ? $self->[LAST] : undef);
4350 if ($cloneAliases) {
4351 my $count = 0;
4352 while (my ($alias, $hit) = each(%{$self->aliases()})) {
4353 $fn->[ALIASES]->{$alias} = $hit;
4354 $count += $hit;
4355 }
4356 $fn->[COUNT] = $count;
4357 }
4358 return $fn;
4359 }
4360
4361 sub name
4362 {
4363 my $self = shift;
4364 return $self->[NAME];
4365 }
4366
4367 sub filename
4368 {
4369 my $self = shift;
4370 return $self->[MAP]->filename();
4371 }
4372
4373 sub hit
4374 {
4375 # this is the hit count across all the aliases of the function
4376 my $self = shift;
4377 return $self->[COUNT];
4378 }
4379
4380 sub isLambda
4381 {
4382 my $self = shift;
4383 # jacoco may show both a lambda and a function on the same line - which
4384 # lcov then associates as an alias
4385 # alias name selection above ensures that the 'master' name is lambda
4386 # only if every alias is a lambda.
4387 # -> this is a lambda only if there is only one alias
4388 return ((TraceFile::is_language('c', $self->filename()) &&
4389 $self->name() =~ /{lambda\(/) ||
4390 (TraceFile::is_language('java', $self->filename()) &&
4391 $self->name() =~ /\.lambda\$/));
4392 }
4393
4394 sub count
4395 {
4396 my ($self, $alias, $merged) = @_;
4397
4398 exists($self->aliases()->{$alias}) or
4399 die("$alias is not an alias of " . $self->name());
4400
4401 return $self->[COUNT]
4402 if (defined($merged) && $merged);
4403
4404 return $self->aliases()->{$alias};
4405 }
4406
4407 sub aliases
4408 {
4409 my $self = shift;
4410 return $self->[ALIASES];
4411 }
4412
4413 sub numAliases
4414 {
4415 my $self = shift;
4416 return scalar(keys %{$self->[ALIASES]});
4417 }
4418
4419 sub file
4420 {
4421 my $self = shift;
4422 return $self->[MAP]->filename();
4423 }
4424
4425 sub line
4426 {
4427 my $self = shift;
4428 return $self->[FIRST];
4429 }
4430
4431 sub set_line
4432 {
4433 my ($self, $line) = @_;
4434 return $self->[FIRST] = $line;
4435 }
4436
4437 sub end_line
4438 {
4439 my $self = shift;
4440 return $self->[LAST];
4441 }
4442
4443 sub set_end_line
4444 {
4445 my ($self, $line) = @_;
4446 if ($line < $self->line()) {
4447 my $suffix =
4448 lcovutil::explain_once('derive_end_line',
4449 " See lcovrc man entry for 'derive_function_end_line'.");
4450 lcovutil::ignorable_error($lcovutil::ERROR_INCONSISTENT_DATA,
4451 '"' . $self->file() . '":' . $self->line() .
4452 ': function ' . $self->name() .
4453 " end line $line less than start line " .
4454 $self->line() .
4455 ". Cannot derive function end line.$suffix"
4456 );
4457 return;
4458 }
4459 $self->[LAST] = $line;
4460 }
4461
4462 sub _format_error
4463 {
4464 my ($self, $errno, $name, $count) = @_;
4465 my $alias =
4466 $name ne $self->name() ? " (alias of '" . $self->name() . "'" : "";
4467 lcovutil::report_format_error($errno, 'hit', $count,
4468 "function '$name'$alias in " . $self->file() . ':' . $self->line());
4469 }
4470
4471 sub addAlias
4472 {
4473 my ($self, $name, $count) = @_;
4474
4475 if (!Scalar::Util::looks_like_number($count)) {
4476 $self->_format_error($lcovutil::ERROR_FORMAT, $name, $count);
4477 $count = 0;
4478 } elsif ($count < 0) {
4479 $self->_format_error($lcovutil::ERROR_NEGATIVE, $name, $count);
4480 $count = 0;
4481 } elsif (defined($lcovutil::excessive_count_threshold) &&
4482 $count > $lcovutil::excessive_count_threshold) {
4483 $self->_format_error($lcovutil::ERROR_EXCESSIVE_COUNT, $name, $count)
4484 unless grep({ $name =~ $_ || $self->name() =~ $_ }
4485 @lcovutil::suppress_function_patterns);
4486 }
4487 my $changed;
4488 my $aliases = $self->[ALIASES];
4489 if (exists($aliases->{$name})) {
4490 $changed = 0 == $aliases->{$name} && 0 != $count;
4491 $aliases->{$name} += $count;
4492 } else {
4493 $aliases->{$name} = $count;
4494 $changed = 1;
4495 # keep track of the shortest name as the function representative
4496 my $curlen = length($self->[NAME]);
4497 my $len = length($name);
4498 # penalize lambda functions so that their name is not chosen
4499 # (java workaround or ugly hack, depending on your perspective)
4500 $curlen += 1000 if $self->[NAME] =~ /(\{lambda\(|\.lambda\$)/;
4501 $len += 1000 if $name =~ /(\{lambda\(|\.lambda\$)/;
4502 $self->[NAME] = $name
4503 if ($len < $curlen || # alias is shorter
4504 ($len == $curlen && # alias is same length but lexically first
4505 $name lt $self->[NAME]));
4506 }
4507 $self->[COUNT] += $count;
4508 # perhaps should remove lambda aliases, if they exist -
4509 # - Issue is that jacoco will show normal function and lambda on the
4510 # same line - which lcov takes to mean that they are aliases
4511 # could just delete the lambda in that case..pretend it doesn't exist.
4512 return $changed;
4513 }
4514
4515 sub merge
4516 {
4517 my ($self, $that) = @_;
4518 lcovutil::ignorable_error($lcovutil::ERROR_INCONSISTENT_DATA,
4519 $self->name() . " has different location than " .
4520 $that->name() . " during merge")
4521 if ($self->line() != $self->line());
4522 while (my ($name, $count) = each(%{$that->[ALIASES]})) {
4523 $self->addAlias($name, $count);
4524 }
4525 }
4526
4527 sub removeAliases
4528 {
4529 my $self = shift;
4530 my $aliases = $self->[ALIASES];
4531 my $rename = 0;
4532 foreach my $name (@_) {
4533 exists($aliases->{$name}) or die("removing non-existent alias $name");
4534
4535 my $count = $aliases->{$name};
4536 delete($aliases->{$name});
4537 $self->[COUNT] -= $count;
4538 if ($self->[NAME] eq $name) {
4539 $rename = 1;
4540 }
4541 }
4542 if ($rename &&
4543 %$aliases) {
4544 my $name;
4545 foreach my $alias (keys %$aliases) {
4546 $name = $alias if !defined($name) || length($alias) < length($name);
4547 }
4548 $self->[NAME] = $name;
4549 }
4550 return %$aliases; # true if this function still exists
4551 }
4552
4553 sub addAliasDifferential
4554 {
4555 my ($self, $name, $data) = @_;
4556 die("alias $name exists")
4557 if exists($self->[ALIASES]->{$name}) && $name ne $self->name();
4558 die("expected array")
4559 unless ref($data) eq "ARRAY" && 2 == scalar(@$data);
4560 $self->[ALIASES]->{$name} = $data;
4561 }
4562
4563 sub setCountDifferential
4564 {
4565 my ($self, $data) = @_;
4566 die("expected array")
4567 unless ref($data) eq "ARRAY" && 2 == scalar(@$data);
4568 $self->[COUNT] = $data;
4569 }
4570
4571 sub findMyLines
4572 {
4573 # use my start/end location to find my list of line coverpoints within
4574 # this function.
4575 # return sorted list of [ [lineNo, hitCount], ...]
4576 my ($self, $lineData) = @_;
4577 return undef unless $self->end_line();
4578 my @lines;
4579 for (my $lineNo = $self->line(); $lineNo <= $self->end_line(); ++$lineNo) {
4580 my $hit = $lineData->value($lineNo);
4581 push(@lines, [$lineNo, $hit])
4582 if (defined($hit));
4583 }
4584 return \@lines;
4585 }
4586
4587 sub _findConditionals
4588 {
4589 my ($self, $data) = @_;
4590 return undef unless $self->end_line();
4591 my @list;
4592 for (my $lineNo = $self->line(); $lineNo <= $self->end_line(); ++$lineNo) {
4593 my $entry = $data->value($lineNo);
4594 push(@list, $entry)
4595 if (defined($entry));
4596 }
4597 return \@list;
4598 }
4599
4600 sub findMyBranches
4601 {
4602 # use my start/end location to list of branch entries within this function
4603 # return sorted list [ branchEntry, ..] sorted by line
4604 my ($self, $branchData) = @_;
4605 die("expected BranchData") unless ref($branchData) eq "BranchData";
4606 return $self->_findConditionals($branchData);
4607 }
4608
4609 sub findMyMcdc
4610 {
4611 # use my start/end location to list of MC/DC entries within this function
4612 # return list [ MCDC_Block, ..] sorted by line
4613 my ($self, $mcdcData) = @_;
4614 die("expected MCDC_Data") unless ref($mcdcData) eq "MCDC_Data";
4615 return $self->_findConditionals($mcdcData);
4616 }
4617
4618 package FunctionMap;
4619
4620 sub new($$)
4621 {
4622 my ($class, $filename) = @_;
4623 my $self = [{}, {}, $filename]; # [locationMap, nameMap]
4624 bless $self, $class;
4625 }
4626
4627 sub filename
4628 {
4629 my $self = shift;
4630 return $self->[2];
4631 }
4632
4633 sub keylist
4634 {
4635 # return list of file:lineNo keys..
4636 my $self = shift;
4637 return keys(%{$self->[0]});
4638 }
4639
4640 sub valuelist
4641 {
4642 # return list of FunctionEntry elements we know about
4643 my $self = shift;
4644 return values(%{$self->[0]});
4645 }
4646
4647 sub list_functions
4648 {
4649 # return list of all the functions/function aliases that we know about
4650 my $self = shift;
4651 return keys(%{$self->[1]});
4652 }
4653
4654 sub define_function
4655 {
4656 my ($self, $fnName, $start_line, $end_line, $location) = @_;
4657 #lcovutil::info("define: $fnName " . $self->$filename() . ":$start_line->$end_line\n");
4658 # could check that function ranges within file are non-overlapping
4659 my ($locationMap, $nameMap) = @$self;
4660
4661 my $data = $self->findName($fnName);
4662 if (defined($data) &&
4663 #TraceFile::is_language('c', $self->filename()) &&
4664 $data->line() != $start_line
4665 ) {
4666 $location = '"' . $self->filename() . '":' . $start_line
4667 unless defined($location);
4668 lcovutil::ignorable_error($lcovutil::ERROR_INCONSISTENT_DATA,
4669 "$location: duplicate function '$fnName' starts on line \"" .
4670 $data->filename() .
4671 "\":$start_line but previous definition started on " .
4672 $data->line() . MessageContext::context() . '.')
4673 unless
4674 grep({ $fnName =~ $_ } @lcovutil::suppress_function_patterns);
4675 # if ignored, just return the function we already have -
4676 # record the function location as the smallest line number we saw
4677 if ($start_line < $data->line()) {
4678 delete $self->[0]->{$data->line()};
4679 $data->set_line($start_line);
4680 $self->[0]->{$start_line} = $data;
4681 }
4682 return $data;
4683 }
4684
4685 if (exists($locationMap->{$start_line})) {
4686 $data = $locationMap->{$start_line};
4687 unless ((defined($end_line) &&
4688 defined($data->end_line()) &&
4689 $end_line == $data->end_line()) ||
4690 (!defined($end_line) && !defined($data->end_line()))
4691 ) {
4692 lcovutil::ignorable_error($lcovutil::ERROR_INCONSISTENT_DATA,
4693 "mismatched end line for $fnName at " .
4694 $self->filename() . ":$start_line: "
4695 .
4696 (defined($data->end_line()) ?
4697 $data->end_line() : 'undef') .
4698 " -> "
4699 .
4700 (defined($end_line) ? $end_line :
4701 'undef') .
4702 MessageContext::context())
4703 unless
4704 grep({ $fnName =~ $_ } @lcovutil::suppress_function_patterns);
4705 # pick the highest end line if we didn't error out
4706 $data->set_end_line($end_line)
4707 if (defined($end_line) &&
4708 (!defined($data->end_line()) ||
4709 $end_line > $data->end_line()));
4710 }
4711 } else {
4712 $data = FunctionEntry->new($fnName, $self, $start_line, $end_line);
4713 $locationMap->{$start_line} = $data;
4714 }
4715 if (!exists($nameMap->{$fnName})) {
4716 $nameMap->{$fnName} = $data;
4717 $data->addAlias($fnName, 0);
4718 }
4719 return $data;
4720 }
4721
4722 sub findName
4723 {
4724 my ($self, $name) = @_;
4725 my $nameMap = $self->[1];
4726 return exists($nameMap->{$name}) ? $nameMap->{$name} : undef;
4727 }
4728
4729 sub findKey
4730 {
4731 my ($self, $key) = @_; # key is the start line of the function
4732 my $locationMap = $self->[0];
4733 return exists($locationMap->{$key}) ? $locationMap->{$key} : undef;
4734 }
4735
4736 sub numFunc
4737 {
4738 my ($self, $merged) = @_;
4739
4740 if (defined($merged) && $merged) {
4741 return scalar($self->keylist());
4742 }
4743 my $n = 0;
4744 foreach my $key ($self->keylist()) {
4745 my $data = $self->findKey($key);
4746 $n += $data->numAliases();
4747 }
4748 return $n;
4749 }
4750
4751 sub numHit
4752 {
4753 my ($self, $merged) = @_;
4754
4755 my $n = 0;
4756 foreach my $key ($self->keylist()) {
4757 my $data = $self->findKey($key);
4758 if (defined($merged) && $merged) {
4759 ++$n
4760 if $data->hit() > 0;
4761 } else {
4762 my $aliases = $data->aliases();
4763 foreach my $alias (keys(%$aliases)) {
4764 my $c = $aliases->{$alias};
4765 ++$n if $c > 0;
4766 }
4767 }
4768 }
4769 return $n;
4770 }
4771
4772 sub get_found_and_hit
4773 {
4774 my $self = shift;
4775 my $merged =
4776 defined($lcovutil::cov_filter[$lcovutil::FILTER_FUNCTION_ALIAS]);
4777 return ($self->numFunc($merged), $self->numHit($merged));
4778 }
4779
4780 sub add_count
4781 {
4782 my ($self, $fnName, $count) = @_;
4783 my $nameMap = $self->[1];
4784 if (exists($nameMap->{$fnName})) {
4785 my $data = $nameMap->{$fnName};
4786 $data->addAlias($fnName, $count);
4787 } else {
4788 lcovutil::ignorable_error($lcovutil::ERROR_MISMATCH,
4789 "unknown function '$fnName'");
4790 }
4791 }
4792
4793 sub union
4794 {
4795 my ($self, $that) = @_;
4796
4797 my $changed = 0;
4798 my $myData = $self->[0];
4799 my $yourData = $that->[0];
4800 while (my ($key, $thatData) = each(%$yourData)) {
4801 my $thisData;
4802 if (!exists($myData->{$key})) {
4803 $thisData =
4804 $self->define_function($thatData->name(),
4805 $thatData->line(), $thatData->end_line());
4806 $changed = 1; # something new...
4807 } else {
4808 $thisData = $myData->{$key};
4809 if (!($thisData->line() == $thatData->line()
4810 && ($thisData->file() eq $thatData->file() ||
4811 ($lcovutil::case_insensitive &&
4812 lc($thisData->file()) eq lc($thatData->file())))
4813 )) {
4814 lcovutil::ignorable_error($lcovutil::ERROR_INCONSISTENT_DATA,
4815 "function data mismatch at " .
4816 $thatData->file() . ":" . $thatData->line());
4817 next;
4818 }
4819 }
4820 # merge in all the new aliases
4821 while (my ($alias, $count) = each(%{$thatData->aliases()})) {
4822 if ($thisData->addAlias($alias, $count)) {
4823 $changed = 1;
4824 }
4825 }
4826 }
4827 return $changed;
4828 }
4829
4830 sub intersect
4831 {
4832 my ($self, $that) = @_;
4833
4834 my $changed = 0;
4835 my $myData = $self->[0];
4836 my $myNames = $self->[1];
4837 my $yourData = $that->[0];
4838 my $yourNames = $that->[1];
4839 foreach my $key (keys %$myData) {
4840 my $me = $myData->{$key};
4841 if (exists($yourData->{$key})) {
4842 my $yourFn = $yourData->{$key};
4843 # intersect operation: keep only the common aliases
4844 my @remove;
4845 my $yourAliases = $yourFn->aliases();
4846 while (my ($alias, $count) = each(%{$me->aliases()})) {
4847 if (exists($yourAliases->{$alias})) {
4848 if ($me->addAlias($alias, $yourAliases->{$alias})) {
4849 $changed = 1;
4850 }
4851 } else {
4852 # remove this alias from me..
4853 push(@remove, $alias);
4854 delete($myNames->{$alias});
4855 $changed = 1;
4856 }
4857 }
4858 if (!$me->removeAliases(@remove)) {
4859 # no aliases left (no common aliases) - so remove this function
4860 delete($myData->{$key});
4861 }
4862 } else {
4863 $self->remove($me);
4864 $changed = 1;
4865 }
4866 }
4867 return $changed;
4868 }
4869
4870 sub difference
4871 {
4872 my ($self, $that) = @_;
4873
4874 my $changed = 0;
4875 my $myData = $self->[0];
4876 my $yourData = $that->[0];
4877 foreach my $key (keys %$myData) {
4878 if (exists($yourData->{$key})) {
4879 # just remove the common aliases...
4880 my $me = $myData->{$key};
4881 my $you = $yourData->{$key};
4882 my @remove;
4883 while (my ($alias, $count) = each(%{$you->aliases()})) {
4884 if (exists($me->aliases()->{$alias})) {
4885 push(@remove, $alias);
4886 $changed = 1;
4887 }
4888 }
4889 if (!$me->removeAliases(@remove)) {
4890 # no aliases left (no disjoint aliases) - so remove this function
4891 delete($myData->{$key});
4892 }
4893 }
4894 }
4895 return $changed;
4896 }
4897
4898 sub remove
4899 {
4900 my ($self, $entry) = @_;
4901 die("expected FunctionEntry - " . ref($entry))
4902 unless 'FunctionEntry' eq ref($entry);
4903 my ($locationMap, $nameMap) = @$self;
4904 my $key = $entry->line();
4905 foreach my $alias (keys %{$entry->aliases()}) {
4906 delete($nameMap->{$alias});
4907 }
4908 delete($locationMap->{$key});
4909 }
4910
4911 package BranchMap;
4912
4913 use constant {
4914 DATA => 0,
4915 FOUND => 1,
4916 HIT => 2,
4917 };
4918
4919 sub new
4920 {
4921 my $class = shift;
4922 my $self = [{}, # hash of lineNo -> BranchEntry/MCDC_Element
4923 # BranchEntry:
4924 # hash of blockID ->
4925 # array of 'taken' entries for each sequential
4926 # branch ID
4927 # MCDC_Element:
4928 0, # branches found
4929 0, # branches executed
4930 ];
4931 return bless $self, $class;
4932 }
4933
4934 sub remove
4935 {
4936 my ($self, $line, $check_if_present) = @_;
4937 my $data = $self->[DATA];
4938
4939 return 0 if ($check_if_present && !exists($data->{$line}));
4940
4941 my $branch = $data->{$line};
4942 my ($f, $h) = $branch->totals();
4943 $self->[FOUND] -= $f;
4944 $self->[HIT] -= $h;
4945
4946 delete($data->{$line});
4947 return 1;
4948 }
4949
4950 sub found
4951 {
4952 my $self = shift;
4953
4954 return $self->[FOUND];
4955 }
4956
4957 sub hit
4958 {
4959 my $self = shift;
4960
4961 return $self->[HIT];
4962 }
4963
4964 # return BranchEntry struct (or undef)
4965 sub value
4966 {
4967 my ($self, $lineNo) = @_;
4968
4969 my $map = $self->[DATA];
4970 return exists($map->{$lineNo}) ? $map->{$lineNo} : undef;
4971 }
4972
4973 # return list of lines which contain branch data
4974 sub keylist
4975 {
4976 my $self = shift;
4977 return keys(%{$self->[DATA]});
4978 }
4979
4980 sub get_found_and_hit
4981 {
4982 my $self = shift;
4983
4984 return ($self->[FOUND], $self->[HIT]);
4985 }
4986
4987 package BranchData;
4988
4989 use base 'BranchMap';
4990
4991 sub new
4992 {
4993 my $class = shift;
4994 my $self = $class->SUPER::new();
4995 return $self;
4996 }
4997
4998 sub append
4999 {
5000 my ($self, $line, $block, $br, $filename) = @_;
5001 # HGC: might be good idea to pass filename so we could give better
5002 # error message if the data is inconsistent.
5003 # OTOH: unclear what a normal user could do about it anyway.
5004 # Maybe exclude that file?
5005 my $data = $self->[BranchMap::DATA];
5006 $filename = '<stdin>' if (defined($filename) && $filename eq '-');
5007 if (!defined($br)) {
5008 lcovutil::ignorable_error($lcovutil::ERROR_BRANCH,
5009 (defined $filename ? "\"$filename\":$line: " : "")
5010 . "expected 'BranchEntry' or 'integer, BranchBlock'"
5011 ) unless ('BranchEntry' eq ref($block));
5012
5013 die("line $line already contains element")
5014 if exists($data->{$line});
5015 # this gets called from 'apply_diff' method: the new line number
5016 # which was assigned might be different than the original - so we
5017 # have to fix up the branch entry.
5018 $block->[0] = $line;
5019 my ($f, $h) = $block->totals();
5020 $self->[BranchMap::FOUND] += $f;
5021 $self->[BranchMap::HIT] += $h;
5022 $data->{$line} = $block;
5023 return 1; # we added something
5024 }
5025
5026 # this cannot happen unless inconsistent branch data was generated by gcov
5027 die((defined $filename ? "\"$filename\":$line: " : "") .
5028 "BranchData::append expected BranchBlock got '" .
5029 ref($br) .
5030 "'.\nThis may be due to mismatched 'gcc' and 'gcov' versions.\n")
5031 unless ('BranchBlock' eq ref($br));
5032
5033 my $branch = $br->id();
5034 my $branchElem;
5035 my $changed = 0;
5036 if (exists($data->{$line})) {
5037 $branchElem = $data->{$line};
5038 $line == $branchElem->line() or die("wrong line mapping");
5039 } else {
5040 $branchElem = BranchEntry->new($line);
5041 $data->{$line} = $branchElem;
5042 $changed = 1; # something new
5043 }
5044
5045 if (!$branchElem->hasBlock($block)) {
5046 $branch == 0
5047 or
5048 lcovutil::ignorable_error($lcovutil::ERROR_BRANCH,
5049 "unexpected non-zero initial branch");
5050 $branch = 0;
5051 my $l = $branchElem->addBlock($block);
5052 push(@$l,
5053 BranchBlock->new($branch, $br->data(),
5054 $br->expr(), $br->is_exception()));
5055 ++$self->[BranchMap::FOUND]; # found one
5056 ++$self->[BranchMap::HIT] if 0 != $br->count(); # hit one
5057 $changed = 1; # something new..
5058 } else {
5059 $block = $branchElem->getBlock($block);
5060
5061 if ($branch > scalar(@$block)) {
5062 lcovutil::ignorable_error($lcovutil::ERROR_BRANCH,
5063 (defined $filename ? "\"$filename\":$line: " : "") .
5064 "unexpected non-sequential branch ID $branch for block $block"
5065 . (defined($filename) ? "" : " of line $line: ")
5066 . ": found " .
5067 scalar(@$block) . " blocks");
5068 $branch = scalar(@$block);
5069 }
5070
5071 if (!exists($block->[$branch])) {
5072 $block->[$branch] =
5073 BranchBlock->new($branch, $br->data(), $br->expr(),
5074 $br->is_exception());
5075 ++$self->[BranchMap::FOUND]; # found one
5076 ++$self->[BranchMap::HIT] if 0 != $br->count(); # hit one
5077
5078 $changed = 1;
5079 } else {
5080 my $me = $block->[$branch];
5081 if (0 == $me->count() && 0 != $br->count()) {
5082 ++$self->[BranchMap::HIT]; # hit one
5083 $changed = 1;
5084 }
5085 if ($me->merge($br, $filename, $line)) {
5086 $changed = 1;
5087 }
5088 }
5089 }
5090 return $changed;
5091 }
5092
5093 sub removeBranches
5094 {
5095 my ($self, $branchList) = @_;
5096
5097 foreach my $b (@$branchList) {
5098 --$self->[BranchMap::FOUND];
5099 --$self->[BranchMap::HIT] if 0 != $b->count();
5100 }
5101 }
5102
5103 sub _checkCounts
5104 {
5105 # some consistency checking
5106 my $self = shift;
5107
5108 my $data = $self->[BranchMap::DATA];
5109 my $found = 0;
5110 my $hit = 0;
5111
5112 while (my ($line, $branch) = each(%$data)) {
5113 $line == $branch->line() or die("lost track of line");
5114 my ($f, $h) = $branch->totals();
5115 $found += $f;
5116 $hit += $h;
5117 }
5118 die("invalid counts: found:" . $self->[BranchMap::FOUND] .
5119 "->$found, hit:" . $self->[BranchMap::HIT] . "->$hit")
5120 unless ($self->[BranchMap::FOUND] == $found &&
5121 $self->[BranchMap::HIT] == $hit);
5122 }
5123
5124 sub compatible($$)
5125 {
5126 my ($myBr, $yourBr) = @_;
5127
5128 # same number of branches
5129 return 0 unless ($#$myBr == $#$yourBr);
5130 for (my $i = 0; $i <= $#$myBr; ++$i) {
5131 my $me = $myBr->[$i];
5132 my $you = $yourBr->[$i];
5133 if ($me->exprString() ne $you->exprString()) {
5134 # this one doesn't match
5135 return 0;
5136 }
5137 }
5138 return 1;
5139 }
5140
5141 sub union
5142 {
5143 my ($self, $info, $filename) = @_;
5144 my $changed = 0;
5145
5146 my $mydata = $self->[BranchMap::DATA];
5147 while (my ($line, $yourBranch) = each(%{$info->[BranchMap::DATA]})) {
5148 # check if self has corresponding line:
5149 # no: just copy all the data for this line, from 'info'
5150 # yes: check for matching blocks
5151 my $myBranch = $self->value($line);
5152 if (!defined($myBranch)) {
5153 $mydata->{$line} = Storable::dclone($yourBranch);
5154 my ($f, $h) = $yourBranch->totals();
5155 $self->[BranchMap::FOUND] += $f;
5156 $self->[BranchMap::HIT] += $h;
5157 $changed = 1;
5158 next;
5159 }
5160 # keep track of which 'myBranch' blocks have already been merged in
5161 # this pass. We don't want to merge multiple distinct blocks from $info
5162 # into the same $self block (even if it appears compatible) - because
5163 # those blocks were distinct in the input data
5164 my %merged;
5165
5166 # we don't expect there to be a huge number of distinct blocks
5167 # in each branch: most often, just one -
5168 # Thus, we simply walk the list to find a matching block, if one exists
5169 # The matching block will have the same number of branches, and the
5170 # branch expressions will be the same.
5171 # - expression only used in Verilog code at the moment -
5172 # other languages will just have a (matching) integer
5173 # branch index
5174
5175 # first: merge your blocks which seem to exist in me:
5176 my @yourBlocks = sort($yourBranch->blocks());
5177 foreach my $yourId (@yourBlocks) {
5178 my $yourBr = $yourBranch->getBlock($yourId);
5179
5180 # Do I have a block with matching name, which is compatible?
5181 my $myBr = $myBranch->getBlock($yourId)
5182 if $myBranch->hasBlock($yourId);
5183 if (defined($myBr) && # I have this one
5184 compatible($myBr, $yourBr)
5185 ) {
5186 foreach my $br (@$yourBr) {
5187 if ($self->append($line, $yourId, $br, $filename)) {
5188 $changed = 1;
5189 }
5190 }
5191 $merged{$yourId} = 1;
5192 $yourId = undef;
5193 }
5194 }
5195 # now look for compatible blocks that aren't identical
5196 BLOCK: foreach my $yourId (@yourBlocks) {
5197 next unless defined($yourId);
5198 my $yourBr = $yourBranch->getBlock($yourId);
5199
5200 # See if we can find a compatible block in $self
5201 # if found: merge.
5202 # no match: this is a different block - assign new ID
5203
5204 foreach my $myId ($myBranch->blocks()) {
5205 next if exists($merged{$myId});
5206
5207 my $myBr = $myBranch->getBlock($myId);
5208 if (compatible($myBr, $yourBr)) {
5209 # we match - so merge our data
5210 $merged{$myId} = 1; # used this one
5211 foreach my $br (@$yourBr) {
5212 if ($self->append($line, $myId, $br, $filename)) {
5213 $changed = 1;
5214 }
5215 }
5216 next BLOCK; # merged this one - go to next
5217 }
5218 } # end search for your block in my blocklist
5219 # we didn't find a match - so this needs to be a new block
5220 my $newID = scalar($myBranch->blocks());
5221 $merged{$newID} = 1; # used this one
5222 foreach my $br (@$yourBr) {
5223 if ($self->append($line, $newID, $br, $filename)) {
5224 $changed = 1;
5225 }
5226 }
5227 }
5228 }
5229 if ($lcovutil::debug) {
5230 $self->_checkCounts(); # some paranoia
5231 }
5232 return $changed;
5233 }
5234
5235 sub intersect
5236 {
5237 my ($self, $info, $filename) = @_;
5238 my $changed = 0;
5239
5240 my $mydata = $self->[BranchMap::DATA];
5241 my $yourdata = $info->[BranchMap::DATA];
5242 foreach my $line (keys %$mydata) {
5243 if (exists($yourdata->{$line})) {
5244 # look at all my blocks. If you have a compatible block, merge them
5245 # - else delete mine
5246 my $myBranch = $mydata->{$line};
5247 my $yourBranch = $yourdata->{$line};
5248 my @myBlocks = $myBranch->blocks();
5249 foreach my $myId (@myBlocks) {
5250 my $myBr = $myBranch->getBlock($myId);
5251
5252 # Do you have a block with matching name, which is compatible?
5253 my $yourBlock = $yourBranch->getBlock($myId)
5254 if $yourBranch->hasBlock($myId);
5255 if (defined($yourBlock) && # you have this one
5256 compatible($myBr, $yourBlock)
5257 ) {
5258 foreach my $br (@$yourBlock) {
5259 if ($self->append($line, $myId, $br, $filename)) {
5260 $changed = 1;
5261 }
5262 }
5263 } else {
5264 # block not found...remove this one
5265 $myBranch->removeBlock($myId, $self);
5266 $changed = 1;
5267 }
5268 } # foreach block
5269 } else {
5270 # my line not found in your data - so remove this one
5271 $changed = 1;
5272 $self->remove($line);
5273 }
5274 }
5275 return $changed;
5276 }
5277
5278 sub difference
5279 {
5280 my ($self, $info, $filename) = @_;
5281 my $changed = 0;
5282
5283 my $mydata = $self->[BranchMap::DATA];
5284 my $yourdata = $info->[BranchMap::DATA];
5285 foreach my $line (keys %$mydata) {
5286 # keep everything here if you don't have this line
5287 next unless exists($yourdata->{$line});
5288
5289 # look at all my blocks. If you have a compatible block, remove it:
5290 my $myBranch = $mydata->{$line};
5291 my $yourBranch = $yourdata->{$line};
5292 my @myBlocks = $myBranch->blocks();
5293 foreach my $myId (@myBlocks) {
5294 my $myBr = $myBranch->getBlock($myId);
5295
5296 # Do you have a block with matching name, which is compatible?
5297 my $yourBlock = $yourBranch->getBlock($myId)
5298 if $yourBranch->hasBlock($myId);
5299 if (defined($yourBlock) && # you have this one
5300 compatible($myBr, $yourBlock)
5301 ) {
5302 # remove common block
5303 $myBranch->removeBlock($myId, $self);
5304 $changed = 1;
5305 }
5306 } # foreach block
5307 }
5308 return $changed;
5309 }
5310
5311 package MCDC_Data;
5312
5313 use base 'BranchMap';
5314
5315 sub new
5316 {
5317 my $class = shift;
5318 my $self = $class->SUPER::new();
5319 return $self;
5320 }
5321
5322 sub append_mcdc
5323 {
5324 my ($self, $mcdc) = @_;
5325 my $line = $mcdc->line();
5326 die("MCDC already defined for $line")
5327 if exists($self->[BranchMap::DATA]->{$line});
5328 $self->[BranchMap::DATA]->{$line} = $mcdc;
5329 }
5330
5331 sub new_mcdc
5332 {
5333 my ($self, $fileData, $line) = @_;
5334
5335 return $self->[BranchMap::DATA]->{$line}
5336 if exists($self->[BranchMap::DATA]->{$line});
5337
5338 my $mcdc = MCDC_Block->new($line);
5339 $self->[BranchMap::DATA]->{$line} = $mcdc;
5340 return $mcdc;
5341 }
5342
5343 sub close_mcdcBlock
5344 {
5345 my ($self, $mcdc) = @_;
5346 my $found = 0;
5347 my $hit = 0;
5348 while (my ($groupSize, $exprs) = each(%{$mcdc->groups()})) {
5349 foreach my $e (@$exprs) {
5350 $found += 2;
5351 ++$hit if $e->count(0);
5352 ++$hit if $e->count(1);
5353 }
5354 }
5355 $self->[BranchMap::FOUND] += $found;
5356 $self->[BranchMap::HIT] += $hit;
5357 }
5358
5359 sub _calculate_counts
5360 {
5361 my $self = shift;
5362 my $found = 0;
5363 my $hit = 0;
5364 while (my ($line, $block) = each(%{$self->[BranchMap::DATA]})) {
5365 my ($f, $h) = $block->totals();
5366 $found += $f;
5367 $hit += $h;
5368 }
5369 $self->[BranchMap::FOUND] = $found;
5370 $self->[BranchMap::HIT] = $hit;
5371 }
5372
5373 sub union
5374 {
5375 my ($self, $info) = @_;
5376 my $changed = 0;
5377
5378 my $mydata = $self->[BranchMap::DATA];
5379 while (my ($line, $yourBranch) = each(%{$info->[BranchMap::DATA]})) {
5380 # check if self has corresponding line:
5381 # no: just copy all the data for this line, from 'info'
5382 # yes: check for matching blocks
5383 my $myBranch = $self->value($line);
5384 if (!defined($myBranch)) {
5385 my $c = Storable::dclone($yourBranch);
5386 $mydata->{$line} = $c;
5387 $self->close_mcdcBlock($c);
5388 $changed = 1;
5389 next;
5390 }
5391
5392 # check if we are compatible.
5393 if ($myBranch->is_compatible($yourBranch)) {
5394 $changed += $myBranch->merge($yourBranch);
5395 } else {
5396 lcovutil::ignorable_error($lcovutil::ERROR_INCONSISTENT_DATA,
5397 "cannot merge iconsistent MC/DC record");
5398 # possibly remove this record?
5399 }
5400 }
5401 $self->_calculate_counts();
5402 return $changed;
5403 }
5404
5405 sub intersect
5406 {
5407 my ($self, $info, $filename) = @_;
5408 my $changed = 0;
5409
5410 my $yourData = $info->[BranchMap::DATA];
5411 my $mydata = $self->[BranchMap::DATA];
5412 foreach my $line (keys %$mydata) {
5413 if (exists($yourData->{$line})) {
5414 # append your count to mine
5415 my $yourBranch = $yourData->{$line};
5416 my $myBranch = $mydata->{$line};
5417
5418 if ($myBranch->is_compatible($yourBranch)) {
5419 $changed += $myBranch->merge($yourBranch);
5420 } else {
5421 lcovutil::ignorable_error($lcovutil::ERROR_INCONSISTENT_DATA,
5422 "cannot merge iconsistent MC/DC record");
5423 # possibly remove this record?
5424 }
5425 } else {
5426 $self->remove($line);
5427 $changed = 1;
5428 }
5429 }
5430 $self->_calculate_counts();
5431 return $changed;
5432 }
5433
5434 sub difference
5435 {
5436 my ($self, $info, $filename) = @_;
5437 my $changed = 0;
5438
5439 my $yourData = $info->[BranchMap::DATA];
5440 my $mydata = $self->[BranchMap::DATA];
5441 foreach my $line (keys %$mydata) {
5442 if (exists($yourData->{$line})) {
5443 $self->remove($line);
5444 $changed = 1;
5445 }
5446 }
5447 $self->_calculate_counts();
5448 return $changed;
5449 }
5450
5451 package FilterBranchExceptions;
5452
5453 sub new
5454 {
5455 my $class = shift;
5456 my $self = [$lcovutil::cov_filter[$lcovutil::FILTER_EXCEPTION_BRANCH],
5457 $lcovutil::cov_filter[$lcovutil::FILTER_ORPHAN_BRANCH],
5458 $lcovutil::cov_filter[$lcovutil::FILTER_EXCLUDE_REGION],
5459 $lcovutil::cov_filter[$lcovutil::FILTER_EXCLUDE_BRANCH]
5460 ];
5461 bless $self, $class;
5462 return grep({ defined($_) } @$self) ? $self : undef;
5463 }
5464
5465 sub removeBranches
5466 {
5467 my ($self, $line, $branches, $filter, $isMasterData) = @_;
5468
5469 my $brdata = $branches->value($line);
5470 return 0 unless defined($brdata);
5471
5472 my $modified = 0;
5473 my $count = 0;
5474 foreach my $block_id ($brdata->blocks()) {
5475 my $blockData = $brdata->getBlock($block_id);
5476 my @replace;
5477 foreach my $br (@$blockData) {
5478 if (defined($filter) && $br->is_exception()) {
5479 --$branches->[BranchMap::FOUND];
5480 --$branches->[BranchMap::HIT] if 0 != $br->count();
5481 #lcovutil::info($srcReader->fileanme() . ": $line: remove exception branch\n");
5482 $modified = 1;
5483 ++$count;
5484 } else {
5485 push(@replace, $br);
5486 }
5487 }
5488 if ($count) {
5489 @$blockData = @replace;
5490 ++$filter->[-2] if $isMasterData;
5491 lcovutil::info(2,
5492 "$line: remove $count exception branch" .
5493 (1 == $count ? '' : 'es') . "\n")
5494 if $isMasterData;
5495 $filter->[-1] += $count;
5496 }
5497 # If there is only one branch left - then this is not a conditional
5498 if (0 == scalar(@replace)) {
5499 lcovutil::info(2, "$line: remove exception block $block_id\n");
5500 lcovutil::info("$line: remove exception block $block_id\n");
5501 $brdata->removeBlock($block_id, $branches);
5502 } elsif (1 == scalar(@replace) &&
5503 defined($self->[1])) { # filter orphan
5504 lcovutil::info(2,
5505 "$line: remove orphan exception block $block_id\n");
5506 $brdata->removeBlock($block_id, $branches);
5507
5508 ++$self->[1]->[-2]
5509 if $isMasterData;
5510 ++$self->[1]->[-1];
5511 }
5512 }
5513 if (0 == scalar($brdata->blocks())) {
5514 lcovutil::info(2, "$line: no branches remain\n");
5515 $branches->remove($line);
5516 $modified = 1;
5517 }
5518 return $modified;
5519 }
5520
5521 sub applyFilter
5522 {
5523 my ($self, $filter, $line, $branches, $perTestBranches) = @_;
5524 my $modified = $self->removeBranches($line, $branches, $filter, 1);
5525 foreach my $tn ($perTestBranches->keylist()) {
5526 # want to remove matching branches everytwhere - so we don't want short-circuit evaluation
5527 my $m = $self->removeBranches($line, $perTestBranches->value($tn),
5528 $filter, 0);
5529 $modified ||= $m;
5530 }
5531 return $modified;
5532 }
5533
5534 sub filter
5535 {
5536 my ($self, $line, $srcReader, $branches, $perTestBranches) = @_;
5537
5538 if ($srcReader->isExcluded($line, 4)) {
5539 # exception branch excluded..
5540 if (defined($self->[2])) { # exclude region
5541 return
5542 $self->applyFilter($self->[2], $line, $branches,
5543 $perTestBranches);
5544 } elsif (defined($self->[3])) { # exclude branches
5545 return
5546 $self->applyFilter($self->[3], $line, $branches,
5547 $perTestBranches);
5548 }
5549 }
5550 # apply if filtering exceptions, orphans, or both
5551 if (defined($self->[0]) || defined($self->[1])) {
5552 return $self->applyFilter($self->[0], $line, $branches,
5553 $perTestBranches);
5554 }
5555 return 0;
5556 }
5557
5558 package TraceInfo;
5559 # coveage data for a particular source file
5560 use constant {
5561 VERSION => 0,
5562 LOCATION => 1,
5563 FILENAME => 2,
5564 CHECKSUM => 3,
5565 LINE_DATA => 4, # per-testcase data
5566 BRANCH_DATA => 5,
5567 FUNCTION_DATA => 6,
5568 MCDC_DATA => 7,
5569
5570 UNION => 0,
5571 INTERSECT => 1,
5572 DIFFERENCE => 2,
5573 };
5574
5575 sub new
5576 {
5577 my ($class, $filename) = @_;
5578 my $self = [];
5579 bless $self, $class;
5580
5581 $self->[VERSION] = undef; # version ID from revision control (if any)
5582
5583 # keep track of location in .info file that this file data was found
5584 # - useful in error messages
5585 $self->[LOCATION] = []; # will fill with file/line
5586
5587 $self->[FILENAME] = $filename;
5588 # _checkdata : line number -> source line checksum
5589 $self->[CHECKSUM] = MapData->new();
5590 # each line/branch/function element is a list of [summaryData, perTestcaseData]
5591
5592 # line: [ line number -> execution count - merged over all testcases,
5593 # testcase_name -> CountData -> line_number -> execution_count ]
5594 $self->[LINE_DATA] =
5595 [CountData->new($filename, $CountData::SORTED), MapData->new()];
5596
5597 # branch: [ BranchData: line number -> branch coverage - for all tests
5598 # testcase_name -> BranchData]
5599 $self->[BRANCH_DATA] = [BranchData->new(), MapData->new()];
5600
5601 # function: [FunctionMap: function_name->FunctionEntry,
5602 # tescase_name -> FucntionMap ]
5603 $self->[FUNCTION_DATA] = [FunctionMap->new($filename), MapData->new()];
5604
5605 $self->[MCDC_DATA] = [MCDC_Data->new(), MapData->new()];
5606
5607 return $self;
5608 }
5609
5610 sub filename
5611 {
5612 my $self = shift;
5613 return $self->[FILENAME];
5614 }
5615
5616 sub set_filename
5617 {
5618 my ($self, $name) = @_;
5619 $self->[FILENAME] = $name;
5620 }
5621
5622 # return true if no line, branch, or function coverage data
5623 sub is_empty
5624 {
5625 my $self = shift;
5626 return ($self->test()->is_empty() && # line cov
5627 $self->testbr()->is_empty() && $self->testfnc()->is_empty());
5628 }
5629
5630 sub location
5631 {
5632 my ($self, $filename, $lineNo) = @_;
5633 my $l = $self->[LOCATION];
5634 if (defined($filename)) {
5635 $l->[0] = $filename;
5636 $l->[1] = $lineNo;
5637 }
5638 return $l;
5639 }
5640
5641 sub version
5642 {
5643 # return the version ID that we found
5644 my ($self, $version) = @_;
5645 (!defined($version) || !defined($self->[VERSION])) or
5646 die("expected to set version ID at most once: " .
5647 (defined($version) ? $version : "undef") . " " .
5648 (defined($self->[VERSION]) ? $self->[VERSION] : "undef"));
5649 $self->[VERSION] = $version
5650 if defined($version);
5651 return $self->[VERSION];
5652 }
5653
5654 # line coverage data
5655 sub test
5656 {
5657 my ($self, $testname) = @_;
5658
5659 my $data = $self->[LINE_DATA]->[1];
5660 if (!defined($testname)) {
5661 return $data;
5662 }
5663
5664 if (!$data->mapped($testname)) {
5665 $data->append_if_unset($testname, CountData->new($self->filename(), 1));
5666 }
5667
5668 return $data->value($testname);
5669 }
5670
5671 sub sum
5672 {
5673 # return MapData of line -> hit count
5674 # data merged over all testcases
5675 my $self = shift;
5676 return $self->[LINE_DATA]->[0];
5677 }
5678
5679 sub func
5680 {
5681 # return FunctionMap of function name or location -> FunctionEntry
5682 # data is merged over all testcases
5683 my $self = shift;
5684 return $self->[FUNCTION_DATA]->[0];
5685 }
5686
5687 sub found
5688 {
5689 my $self = shift;
5690 return $self->sum()->found();
5691 }
5692
5693 sub hit
5694 {
5695 my $self = shift;
5696 return $self->sum()->hit();
5697 }
5698
5699 sub function_found
5700 {
5701 my $self = shift;
5702 return $self->func()
5703 ->numFunc(
5704 defined($lcovutil::cov_filter[$lcovutil::FILTER_FUNCTION_ALIAS]));
5705 }
5706
5707 sub function_hit
5708 {
5709 my $self = shift;
5710 return $self->func()
5711 ->numHit(
5712 defined($lcovutil::cov_filter[$lcovutil::FILTER_FUNCTION_ALIAS]));
5713 }
5714
5715 sub branch_found
5716 {
5717 my $self = shift;
5718 return $self->sumbr()->found();
5719 }
5720
5721 sub branch_hit
5722 {
5723 my $self = shift;
5724 return $self->sumbr()->hit();
5725 }
5726
5727 sub mcdc_found
5728 {
5729 return $_[0]->mcdc()->found();
5730 }
5731
5732 sub mcdc_hit
5733 {
5734 return $_[0]->mcdc()->hit();
5735 }
5736
5737 sub check
5738 {
5739 my $self = shift;
5740 return $self->[CHECKSUM];
5741 }
5742
5743 # function coverage
5744 sub testfnc
5745 {
5746 my ($self, $testname) = @_;
5747
5748 my $data = $self->[FUNCTION_DATA]->[1];
5749 if (!defined($testname)) {
5750 return $data;
5751 }
5752
5753 if (!$data->mapped($testname)) {
5754 $data->append_if_unset($testname, FunctionMap->new($self->filename()));
5755 }
5756
5757 return $data->value($testname);
5758 }
5759
5760 # branch coverage
5761 sub testbr
5762 {
5763 my ($self, $testname) = @_;
5764
5765 my $data = $self->[BRANCH_DATA]->[1];
5766 if (!defined($testname)) {
5767 return $data;
5768 }
5769
5770 if (!$data->mapped($testname)) {
5771 $data->append_if_unset($testname, BranchData->new());
5772 }
5773
5774 return $data->value($testname);
5775 }
5776
5777 sub sumbr
5778 {
5779 # return BranchData map of line number -> BranchEntry
5780 # data is merged over all testcases
5781 my $self = shift;
5782 return $self->[BRANCH_DATA]->[0];
5783 }
5784
5785 # MCDC coverage
5786 sub testcase_mcdc
5787 {
5788 my ($self, $testname) = @_;
5789
5790 my $data = $self->[MCDC_DATA]->[1];
5791 if (!defined($testname)) {
5792 return $data;
5793 }
5794
5795 if (!$data->mapped($testname)) {
5796 $data->append_if_unset($testname, MCDC_Data->new());
5797 }
5798
5799 return $data->value($testname);
5800 }
5801
5802 sub mcdc
5803 {
5804 # return MCDC_Data map of line number -> MCDC_Block
5805 # data is merged over all testcases
5806 my $self = shift;
5807 return $self->[MCDC_DATA]->[0];
5808 }
5809
5810 #
5811 # check_data
5812 # some paranoia checks
5813
5814 sub check_data($)
5815 {
5816 my $self = shift;
5817
5818 # some paranoia checking...
5819 if (1 || $lcovutil::debug) {
5820 my ($brSum, $brTest) = @{$self->[BRANCH_DATA]};
5821 $brSum->_checkCounts();
5822 foreach my $t ($brTest->keylist()) {
5823 $brTest->value($t)->_checkCounts();
5824 }
5825 }
5826 }
5827
5828 #
5829 # get_info(hash_ref)
5830 #
5831 # Retrieve data from an entry of the structure generated by TraceFile::_read_info().
5832 # Return a list of references to hashes:
5833 # (test data hash ref, sum count hash ref, funcdata hash ref, checkdata hash
5834 # ref, testfncdata hash ref, testbranchdata hash ref, branch summary hash ref)
5835 #
5836
5837 sub get_info($)
5838 {
5839 my $self = shift;
5840 my ($sumcount_ref, $testdata_ref) = @{$self->[LINE_DATA]};
5841 my ($funcdata_ref, $testfncdata) = @{$self->[FUNCTION_DATA]};
5842 my ($sumbrcount, $testbrdata) = @{$self->[BRANCH_DATA]};
5843 my ($mcdccount, $testcasemcdc) = @{$self->[MCDC_DATA]};
5844 my $checkdata_ref = $self->[CHECKSUM];
5845
5846 return ($testdata_ref, $sumcount_ref, $funcdata_ref,
5847 $checkdata_ref, $testfncdata, $testbrdata,
5848 $sumbrcount, $mcdccount, $testcasemcdc);
5849 }
5850
5851 sub _merge_checksums
5852 {
5853 my $self = shift;
5854 my $info = shift;
5855 my $filename = shift;
5856
5857 my $mine = $self->check();
5858 my $yours = $info->check();
5859 foreach my $line ($yours->keylist()) {
5860 if ($mine->mapped($line) &&
5861 $mine->value($line) ne $yours->value($line)) {
5862 lcovutil::ignorable_error($lcovutil::ERROR_MISMATCH,
5863 "checksum mismatch at $filename:$line: " .
5864 $mine->value($line),
5865 ' -> ' . $yours->value($line));
5866 }
5867 $mine->replace($line, $yours->value($line));
5868 }
5869 }
5870
5871 sub merge
5872 {
5873 my ($self, $info, $op, $filename) = @_;
5874
5875 my $me = defined($self->version()) ? $self->version() : "<no version>";
5876 my $you = defined($info->version()) ? $info->version() : "<no version>";
5877
5878 my ($countOp, $funcOp, $brOp, $mcdcOp);
5879
5880 if ($op == UNION) {
5881 $countOp = \&CountData::union;
5882 $funcOp = \&FunctionMap::union;
5883 $brOp = \&BranchData::union;
5884 $mcdcOp = \&MCDC_Data::union;
5885 } elsif ($op == INTERSECT) {
5886 $countOp = \&CountData::intersect;
5887 $funcOp = \&FunctionMap::intersect;
5888 $brOp = \&BranchData::intersect;
5889 $mcdcOp = \&MCDC_Data::intersect;
5890 } else {
5891 die("unexpected op $op") unless $op == DIFFERENCE;
5892 $countOp = \&CountData::difference;
5893 $funcOp = \&FunctionMap::difference;
5894 $brOp = \&BranchData::difference;
5895 $mcdcOp = \&MCDC_Data::difference;
5896 }
5897
5898 lcovutil::checkVersionMatch($filename, $me, $you, 'merge');
5899 my $changed = 0;
5900
5901 foreach my $name ($info->test()->keylist()) {
5902 if (&$countOp($self->test($name), $info->test($name))) {
5903 $changed = 1;
5904 }
5905 }
5906 # if intersect and I contain some test that you don't, need to remove my data
5907 if (&$countOp($self->sum(), $info->sum())) {
5908 $changed = 1;
5909 }
5910
5911 if (&$funcOp($self->func(), $info->func())) {
5912 $changed = 1;
5913 }
5914 $self->_merge_checksums($info, $filename);
5915
5916 foreach my $name ($info->testfnc()->keylist()) {
5917 if (&$funcOp($self->testfnc($name), $info->testfnc($name))) {
5918 $changed = 1;
5919 }
5920 }
5921
5922 foreach my $name ($info->testbr()->keylist()) {
5923 if (&$brOp($self->testbr($name), $info->testbr($name), $filename)) {
5924 $changed = 1;
5925 }
5926 }
5927 if (&$brOp($self->sumbr(), $info->sumbr(), $filename)) {
5928 $changed = 1;
5929 }
5930
5931 foreach my $name ($info->testcase_mcdc()->keylist()) {
5932 if (
5933 &$mcdcOp($self->testcase_mcdc($name), $info->testcase_mcdc($name),
5934 $filename)
5935 ) {
5936 $changed = 1;
5937 }
5938 }
5939 if (&$mcdcOp($self->mcdc(), $info->mcdc(), $filename)) {
5940 $changed = 1;
5941 }
5942 return $changed;
5943 }
5944
5945 # this package merely reads sourcefiles as they are found on the current
5946 # filesystem - ie., the baseline version might have been modified/might
5947 # have diffs - but the current version does not.
5948 package ReadCurrentSource;
5949
5950 our @source_directories;
5951 our $searchPath;
5952 our @dirs_used;
5953 use constant {
5954 FILENAME => 0,
5955 PATH => 1,
5956 SOURCE => 2,
5957 EXCLUDE => 3,
5958
5959 # reasons: (bitfield)
5960 EXCLUDE_REGION => 0x10,
5961 EXCLUDE_BRANCH_REGION => 0x20,
5962 EXCLUDE_DIRECTIVE => 0x40,
5963 OMIT_LINE => 0x80,
5964 };
5965
5966 sub new
5967 {
5968 my ($class, $filename) = @_;
5969
5970 # additional layer of indirection so derived class can hold its own data
5971 my $self = [[]];
5972 bless $self, $class;
5973
5974 $self->open($filename) if defined($filename);
5975 return $self;
5976 }
5977
5978 sub close
5979 {
5980 my $self = shift;
5981 my $data = $self->[0];
5982 while (scalar(@$data)) {
5983 pop(@$data);
5984 }
5985 }
5986
5987 sub resolve_path
5988 {
5989 my ($filename, $applySubstitutions) = @_;
5990 $filename = lcovutil::subst_file_name($filename) if $applySubstitutions;
5991 return $filename
5992 if (-e $filename ||
5993 (!@lcovutil::resolveCallback &&
5994 (File::Spec->file_name_is_absolute($filename) ||
5995 0 == scalar(@source_directories))));
5996
5997 # don't pass 'applySubstitutions' flag as we already did that, above
5998 return $searchPath->resolve($filename, 0);
5999 }
6000
6001 sub warn_sourcedir_patterns
6002 {
6003 $searchPath->warn_unused(
6004 @source_directories ? '--source-directory' : 'source_directory = ');
6005 }
6006
6007 sub _load
6008 {
6009 my ($self, $filename, $version) = @_;
6010 my $data = $self->[0];
6011
6012 $version = "" unless defined($version);
6013 my $path = resolve_path($filename);
6014 if (open(SRC, "<", $path)) {
6015 lcovutil::info(1,
6016 "read $version$filename" .
6017 ($path ne $filename ? " (at $path)" : '') . "\n");
6018 $data->[PATH] = $path;
6019 my @sourceLines = <SRC>;
6020 CORE::close(SRC) or die("unable to close $filename: $!\n");
6021 $data->[FILENAME] = $filename;
6022 return \@sourceLines;
6023 } else {
6024 lcovutil::ignorable_error($lcovutil::ERROR_SOURCE,
6025 "unable to open $filename: $!\n");
6026 $self->close();
6027 return undef;
6028 }
6029 }
6030
6031 sub open
6032 {
6033 my ($self, $filename, $version) = @_;
6034
6035 my $srcLines = $self->_load($filename, $version);
6036 if (defined($srcLines)) {
6037 return $self->parseLines($filename, $srcLines);
6038 }
6039 return undef;
6040 }
6041
6042 sub path
6043 {
6044 my $self = shift;
6045 return $self->[0]->[PATH];
6046 }
6047
6048 sub parseLines
6049 {
6050 my ($self, $filename, $sourceLines) = @_;
6051
6052 my @excluded;
6053 my $exclude_region;
6054 my $exclude_br_region;
6055 my $exclude_exception_region;
6056 my $line = 0;
6057 my $excl_start = qr($lcovutil::EXCL_START);
6058 my $excl_stop = qr($lcovutil::EXCL_STOP);
6059 my $excl_line = qr($lcovutil::EXCL_LINE);
6060 my $excl_br_start = qr($lcovutil::EXCL_BR_START);
6061 my $excl_br_stop = qr($lcovutil::EXCL_BR_STOP);
6062 my $excl_br_line = qr($lcovutil::EXCL_BR_LINE);
6063 my $excl_ex_start = qr($lcovutil::EXCL_EXCEPTION_BR_START);
6064 my $excl_ex_stop = qr($lcovutil::EXCL_EXCEPTION_BR_STOP);
6065 my $excl_ex_line = qr($lcovutil::EXCL_EXCEPTION_LINE);
6066 # @todo: if we had annotated data here, then we could whine at the
6067 # author of the unmatched start, extra end, etc.
6068
6069 my $exclude_directives =
6070 qr/^\s*#\s*((else|endif)|((ifdef|ifndef|if|elif|include|define|undef)\s+))/
6071 if (TraceFile::is_language('c', $filename) &&
6072 defined($lcovutil::cov_filter[$lcovutil::FILTER_DIRECTIVE]));
6073
6074 my @excludes;
6075 if (defined($lcovutil::cov_filter[$lcovutil::FILTER_EXCLUDE_REGION])) {
6076 push(@excludes,
6077 [$excl_start, $excl_stop,
6078 \$exclude_region, 3 | EXCLUDE_REGION,
6079 $lcovutil::EXCL_START, $lcovutil::EXCL_STOP
6080 ]);
6081 } else {
6082 $excl_line = undef;
6083 }
6084
6085 if (defined($lcovutil::cov_filter[$lcovutil::FILTER_EXCLUDE_BRANCH])) {
6086 push(@excludes,
6087 [$excl_ex_start, $excl_ex_stop,
6088 \$exclude_exception_region, 4 | EXCLUDE_BRANCH_REGION,
6089 $lcovutil::EXCL_BR_START, $lcovutil::EXCL_BR_STOP,
6090 ],
6091 [$excl_br_start,
6092 $excl_br_stop,
6093 \$exclude_br_region,
6094 2 | EXCLUDE_BRANCH_REGION,
6095 $lcovutil::EXCL_EXCEPTION_BR_START,
6096 $lcovutil::EXCL_EXCEPTION_BR_STOP,
6097 ]);
6098 } else {
6099 $excl_br_line = undef;
6100 $excl_ex_line = undef;
6101 }
6102 LINES: foreach (@$sourceLines) {
6103 $line += 1;
6104 my $exclude_branch_line = 0;
6105 my $exclude_exception_branch_line = 0
6106 ; # per-line exception excludion not implemented at present. Probably unnecessary.
6107 chomp($_);
6108 s/\r//; # remove carriage return
6109 if (defined($exclude_directives) &&
6110 $_ =~ $exclude_directives) {
6111 push(@excluded, 3 | EXCLUDE_DIRECTIVE); #everything excluded
6112 lcovutil::info(2, "directive '#$1' on $filename:$line\n");
6113 next;
6114 }
6115
6116 foreach my $d (@excludes) {
6117 # note: $d->[4] is the 'start' string (not converted to perl regexp)
6118 # $d->[5] is the 'stop' string
6119 my ($start, $stop, $ref, $reason) = @$d;
6120 if ($_ =~ $start) {
6121 lcovutil::ignorable_error($lcovutil::ERROR_MISMATCH,
6122 "$filename: overlapping exclude directives. Found " .
6123 $d->[4] .
6124 " at line $line - but no matching " . $d->[5] .
6125 ' for ' . $d->[4] . ' at line ' . $$ref->[0])
6126 if $$ref;
6127 $$ref = [$line, $reason];
6128 last;
6129 } elsif ($_ =~ $stop) {
6130 lcovutil::ignorable_error($lcovutil::ERROR_MISMATCH,
6131 "$filename: found " . $d->[5] .
6132 " directive at line $line without matching " .
6133 $d->[4] . ' directive')
6134 unless $$ref;
6135 $$ref = undef;
6136 last;
6137 }
6138 }
6139 if (defined($excl_line) &&
6140 $_ =~ $excl_line) {
6141 push(@excluded, 3 | EXCLUDE_REGION); #everything excluded
6142 next;
6143 } elsif (defined($excl_br_line) &&
6144 $_ =~ $excl_br_line) {
6145 $exclude_branch_line = 2 | EXCLUDE_BRANCH_REGION;
6146 } elsif (defined($excl_ex_line) &&
6147 $_ =~ $excl_ex_line) {
6148 $exclude_branch_line = 4 | EXCLUDE_BRANCH_REGION;
6149 } elsif (0 != scalar(@lcovutil::omit_line_patterns)) {
6150 foreach my $p (@lcovutil::omit_line_patterns) {
6151 my $pat = $p->[0];
6152 if ($_ =~ $pat) {
6153 push(@excluded, 3 | OMIT_LINE); #everything excluded
6154 #lcovutil::info("'" . $p->[-2] . "' matched \"$_\", line \"$filename\":"$line\n");
6155 ++$p->[-1];
6156 next LINES;
6157 }
6158 }
6159 }
6160 push(@excluded,
6161 ($exclude_region ? $exclude_region->[1] : 0) |
6162 ($exclude_br_region ? $exclude_br_region->[1] : 0) | (
6163 $exclude_exception_region ? $exclude_exception_region->[1] : 0
6164 ) | $exclude_branch_line | $exclude_exception_branch_line);
6165 }
6166 foreach my $t ([$exclude_region, $lcovutil::EXCL_START,
6167 $lcovutil::EXCL_STOP
6168 ],
6169 [$exclude_br_region, $lcovutil::EXCL_BR_START,
6170 $lcovutil::EXCL_BR_STOP
6171 ],
6172 [$exclude_exception_region,
6173 $lcovutil::EXCL_EXCEPTION_BR_START,
6174 $lcovutil::EXCL_EXCEPTION_BR_STOP
6175 ]
6176 ) {
6177 my ($key, $start, $stop) = @$t;
6178 lcovutil::ignorable_error($lcovutil::ERROR_MISMATCH,
6179 "$filename: unmatched $start at line " .
6180 $key->[0] .
6181 " - saw EOF while looking for matching $stop"
6182 ) if ($key);
6183 }
6184 my $data = $self->[0];
6185 $data->[FILENAME] = $filename;
6186 $data->[SOURCE] = $sourceLines;
6187 $data->[EXCLUDE] = \@excluded;
6188 return $self;
6189 }
6190
6191 sub notEmpty
6192 {
6193 my $self = shift;
6194 return 0 != scalar(@{$self->[0]});
6195 }
6196
6197 sub filename
6198 {
6199 return $_[0]->[0]->[FILENAME];
6200 }
6201
6202 sub numLines
6203 {
6204 my $self = shift;
6205 return scalar(@{$self->[0]->[SOURCE]});
6206 }
6207
6208 sub getLine
6209 {
6210 my ($self, $line) = @_;
6211
6212 return $self->isOutOfRange($line) ?
6213 undef :
6214 $self->[0]->[SOURCE]->[$line - 1];
6215 }
6216
6217 sub getExpr
6218 {
6219 my ($self, $startLine, $startCol, $endLine, $endCol) = @_;
6220 die("bad range [$startLine:$endLine]") unless $endLine >= $startLine;
6221 return 'NA' unless $endLine <= $self->numLines();
6222
6223 my $line = $self->getLine($startLine);
6224 my $expr;
6225 if ($startLine == $endLine) {
6226 $expr = substr($line, $startCol - 1, $endCol - $startCol);
6227 } else {
6228 $expr = substr($line, $startCol - 1);
6229 for (my $l = $startLine + 1; $l < $endLine; ++$l) {
6230 $expr .= $self->getLine($l);
6231 }
6232 $line = $self->getLine($endLine);
6233 $expr .= substr($line, 0, $endCol);
6234 }
6235 $expr =~ /^\s*(.+?)\s*$/;
6236 return $1;
6237 }
6238
6239 sub isOutOfRange
6240 {
6241 my ($self, $lineNo, $context) = @_;
6242 my $data = $self->[0];
6243 if (defined($data->[EXCLUDE]) &&
6244 scalar(@{$data->[EXCLUDE]}) < $lineNo) {
6245
6246 # Can happen due to version mismatches: data extracted with
6247 # version N of the file, then generating HTML with version M
6248 # "--version-script callback" option can be used to detect this.
6249 # Another case happens due to apparent bugs in some old 'gcov'
6250 # versions - which sometimes inserts out-of-range line numbers
6251 # when macro is used as last line in file.
6252
6253 my $filt = $lcovutil::cov_filter[$lcovutil::FILTER_LINE_RANGE];
6254 if (defined($filt)) {
6255 my $c = ($context eq 'line') ? 'line' : "$context at line";
6256 lcovutil::info(2,
6257 "filter out-of-range $c $lineNo in " .
6258 $self->filename() . " (" .
6259 scalar(@{$data->[EXCLUDE]}) .
6260 " lines in file)\n");
6261 ++$filt->[-2]; # applied in 1 location
6262 ++$filt->[-1]; # one coverpoint suppressed
6263 return 1;
6264 }
6265 my $key = $self->filename();
6266 $key .= $lineNo unless $lcovutil::warn_once_per_file;
6267 if (lcovutil::warn_once($lcovutil::ERROR_RANGE, $key)) {
6268 my $c = ($context eq 'line') ? 'line' : "$context at line";
6269 my $msg =
6270 "unknown $c '$lineNo' in " .
6271 $self->filename() . ": there are only " .
6272 scalar(@{$data->[EXCLUDE]}) . " lines in the file.";
6273 if ($lcovutil::verbose ||
6274 0 == lcovutil::message_count($lcovutil::ERROR_RANGE)) {
6275 # only print verbose addition on first message
6276 $msg .= lcovutil::explain_once(
6277 'version_script',
6278 [ "\n Issue can be caused by code changes/version mismatch: see the \"--version-script script_file\" discussion in the genhtml man page.",
6279 $lcovutil::tool_name ne 'geninfo'
6280 ],
6281 "\n Use '$lcovutil::tool_name --filter range' to remove out-of-range lines."
6282 );
6283 }
6284 # some versions of gcov seem to make up lines that do not exist -
6285 # this appears to be related to macros on last line in file
6286 lcovutil::store_deferred_message($lcovutil::ERROR_RANGE,
6287 1, $key, $msg);
6288 }
6289 # Note: if user ignored the error, then we return 'not out of range'.
6290 # The line is out of range/something is wrong - but the user did not
6291 # ask us to filter it out.
6292 }
6293 return 0;
6294 }
6295
6296 sub excludeReason
6297 {
6298 my ($self, $lineNo) = @_;
6299 my $data = $self->[0];
6300 die("missing data at $lineNo")
6301 unless (defined($data->[EXCLUDE]) &&
6302 scalar(@{$data->[EXCLUDE]}) >= $lineNo);
6303 return $data->[EXCLUDE]->[$lineNo - 1] & 0xFF0;
6304 }
6305
6306 sub isExcluded
6307 {
6308 my ($self, $lineNo, $branch) = @_;
6309 my $data = $self->[0];
6310 if (!defined($data->[EXCLUDE]) ||
6311 scalar(@{$data->[EXCLUDE]}) < $lineNo) {
6312 # this can happen due to version mismatches: data extracted with
6313 # version N of the file, then generating HTML with version M
6314 # "--version-script callback" option can be used to detect this
6315 my $key = $self->filename();
6316 $key .= $lineNo unless ($lcovutil::warn_once_per_file);
6317 my $suffix = lcovutil::explain_once(
6318 'version-script',
6319 [ "\n Issue can be caused by code changes/version mismatch; see the \"--version-script script_file\" discussion in the genhtml man page.",
6320 $lcovutil::verbose ||
6321 lcovutil::message_count($lcovutil::ERROR_RANGE) == 0
6322 ]);
6323 lcovutil::store_deferred_message(
6324 $lcovutil::ERROR_RANGE,
6325 1, $key,
6326 "unknown line '$lineNo' in " . $self->filename()
6327 .
6328 (
6329 defined($data->[EXCLUDE]) ?
6330 (" there are only " .
6331 scalar(@{$data->[EXCLUDE]}) . " lines in the file.") :
6332 "") .
6333 $suffix) if lcovutil::warn_once($lcovutil::ERROR_RANGE, $key);
6334 return 0; # even though out of range - this is not excluded by filter
6335 }
6336 return 1
6337 if ($branch &&
6338 0 != ($data->[EXCLUDE]->[$lineNo - 1] & $branch));
6339 return 0 != ($data->[EXCLUDE]->[$lineNo - 1] & 1);
6340 }
6341
6342 sub removeComments
6343 {
6344 my $line = shift;
6345 $line =~ s|//.*$||;
6346 $line =~ s|/\*.*\*/||g;
6347 return $line;
6348 }
6349
6350 sub isCharacter
6351 {
6352 my ($self, $line, $char) = @_;
6353
6354 my $code = $self->getLine($line);
6355 return 0
6356 unless defined($code);
6357 $code = removeComments($code);
6358 return ($code =~ /^\s*${char}\s*$/);
6359 }
6360
6361 # is line empty
6362 sub isBlank
6363 {
6364 my ($self, $line) = @_;
6365
6366 my $code = $self->getLine($line);
6367 return 0
6368 unless defined($code);
6369 $code = removeComments($code);
6370 return ($code =~ /^\s*$/);
6371 }
6372
6373 sub is_initializerList
6374 {
6375 my ($self, $line) = @_;
6376 return 0 unless defined($self->[0]->[SOURCE]) && $line < $self->numLines();
6377 my $code = '';
6378 my $l = $line;
6379 my $foundExpr = 0;
6380 while ($l < $self->numLines()) {
6381 my $src = $self->getLine($l);
6382 # append to string until we find close brace...then look for next one...
6383 $code = removeComments($code . $src);
6384 # believe that initialization expressions are either numeric or C strings
6385 while ($code =~
6386 s/\s+("[^"]*"|0x[0-9a-fA-F]+|[-+]?[0-9]+((\.[0-9]+)([eE][-+][0-9]+)?)?)\s*,?//
6387 ) {
6388 $foundExpr = 1;
6389 }
6390 # remove matching {} brace pairs - assume a sub-object initializer
6391 $code =~ s/\s*{\s*,?\s*}\s*,?\s*//;
6392 last if $code =~ /[};]/; # unmatched close or looks like statement
6393 last unless $code =~ /^\s*([{}]\s*)*$/;
6394 ++$l;
6395 }
6396 return $foundExpr ? $l - $line : 0; # return number of consecutive lines
6397 }
6398
6399 sub containsConditional
6400 {
6401 my ($self, $line) = @_;
6402
6403 # special case - maybe C++ exception handler on close brace at end of function?
6404 return 0
6405 if $self->isCharacter($line, '}');
6406 my $src = $self->getLine($line);
6407 return 1
6408 unless defined($src);
6409
6410 my $code = "";
6411 for (my $next = $line + 1;
6412 defined($src) && ($next - $line) < $lcovutil::source_filter_lookahead;
6413 ++$next) {
6414
6415 $src = lcovutil::simplifyCode($src);
6416
6417 my $bitwiseOperators =
6418 $lcovutil::source_filter_bitwise_are_conditional ? '&|~' : '';
6419
6420 return 1
6421 if ($src =~
6422 /([?!><$bitwiseOperators]|&&|\|\||==|!=|\b(if|switch|case|while|for)\b)/
6423 );
6424 $code = $code . $src;
6425
6426 if (lcovutil::balancedParens($code)) {
6427 return 0; # got to the end and didn't see conditional
6428 } elsif ($src =~ /[{;]\s*$/) {
6429 # assume we got to the end of the statement if we see semicolon
6430 # or brace.
6431 # parens weren't balanced though - so assume this might be
6432 # a conditional
6433 return 1;
6434 }
6435 $src = $self->getLine($next);
6436 $src = '' unless defined($src);
6437 }
6438 return 1; # not sure - so err on side of caution
6439 }
6440
6441 sub containsTrivialFunction
6442 {
6443 my ($self, $start, $end) = @_;
6444 return 0
6445 if (1 + $end - $start >= $lcovutil::trivial_function_threshold);
6446 my $text = '';
6447 for (my $line = $start; $line <= $end; ++$line) {
6448 my $src = $self->getLine($line);
6449 $src = '' unless defined($src);
6450 chomp($src);
6451 $src =~ s/\s+$//; # whitespace
6452 $src =~ s#//.*$##; # remove end-of-line comments
6453 $text .= $src;
6454 }
6455 # remove any multiline comments that were present:
6456 $text =~ s#/\*.*\*/##g;
6457 # remove whitespace
6458 $text =~ s/\s//g;
6459 # remove :: C++ separator
6460 $text =~ s/:://g;
6461 if ($text =~ /:/) {
6462 return 0;
6463 }
6464
6465 # does code end with '{}', '{;}' or '{};'?
6466 # Or: is this just a close brace?
6467 if ($text =~ /(\{;?|^)\};?$/) {
6468 return 1;
6469 }
6470 return 0;
6471 }
6472
6473 # check if this line is a close brace with zero hit count that should be
6474 # suppressed. We want to ignore spurious zero on close brace; depending
6475 # on what gcov did the last time (zero count, no count, nonzero count) -
6476 # it might be interpreted as UIC - which will violate our coverage criteria.
6477 # We want to ignore this line if:
6478 # - the line contain only a closing brace and
6479 # - previous line is hit, OR
6480 # - previous line is not an open-brace which has no associated
6481 # count - i.e., this is not an empty block where the zero
6482 # count is tagged to the closing brace, OR
6483 # is line empty (no code) and
6484 # - count is zero, and
6485 # - either previous or next non-blank lines have an associated count
6486 #
6487 sub suppressCloseBrace
6488 {
6489 my ($self, $lineNo, $count, $lineCountData) = @_;
6490
6491 my $suppress = 0;
6492 if ($self->isCharacter($lineNo, '}')) {
6493 for (my $prevLine = $lineNo - 1; $prevLine >= 0; --$prevLine) {
6494 my $prev = $lineCountData->value($prevLine);
6495 if (defined($prev)) {
6496 # previous line was executable
6497 $suppress = 1
6498 if ($prev == $count ||
6499 ($count == 0 &&
6500 $prev > 0));
6501
6502 lcovutil::info(3,
6503 "not skipping brace line $lineNo because previous line $prevLine hit count didn't match: $prev != $count"
6504 ) unless $suppress;
6505 last;
6506 } elsif ($count == 0 &&
6507 # previous line not executable - was it an open brace?
6508 $self->isCharacter($prevLine, '{')
6509 ) {
6510 # look 'up' from the open brace to find the first
6511 # line which has an associated count -
6512 my $code = "";
6513 for (my $l = $prevLine - 1; $l >= 0; --$l) {
6514 $code = $self->getLine($l) . $code;
6515 my $prevCount = $lineCountData->value($l);
6516 if (defined($prevCount)) {
6517 # don't suppress if previous line not hit either
6518 last
6519 if $prevCount == 0;
6520 # if first non-whitespace character is a colon -
6521 # then this looks like a C++ initialization list.
6522 # suppress.
6523 if ($code =~ /^\s*:(\s|[^:])/) {
6524 $suppress = 1;
6525 } else {
6526 $code = lcovutil::filterStringsAndComments($code);
6527 $code = lcovutil::simplifyCode($code);
6528 # don't suppress if this looks like a conditional
6529 $suppress = 1
6530 unless (
6531 $code =~ /\b(if|switch|case|while|for)\b/);
6532 }
6533 last;
6534 }
6535 } # for each prior line (looking for statement before block)
6536 last;
6537 } # if (line was an open brace)
6538 } # foreach prior line
6539 } # if line was close brace
6540 return $suppress;
6541 }
6542
6543 package TraceFile;
6544
6545 our $ignore_testcase_name; # use default name, if set
6546 use constant {
6547 FILES => 0,
6548 COMMENTS => 1,
6549 STATE => 2, # operations performed: don't do them again
6550
6551 DID_FILTER => 1,
6552 DID_DERIVE => 2,
6553 };
6554
6555 sub load
6556 {
6557 my ($class, $tracefile, $readSource, $verify_checksum,
6558 $ignore_function_exclusions)
6559 = @_;
6560 my $self = $class->new();
6561 my $context = MessageContext->new("loading $tracefile");
6562
6563 $self->_read_info($tracefile, $readSource, $verify_checksum);
6564
6565 $self->applyFilters($readSource);
6566 return $self;
6567 }
6568
6569 sub new
6570 {
6571 my $class = shift;
6572 my $self = [{}, [], 0];
6573 bless $self, $class;
6574
6575 return $self;
6576 }
6577
6578 sub serialize
6579 {
6580 my ($self, $filename) = @_;
6581
6582 my $data = Storable::store($self, $filename);
6583 die("serialize failed") unless defined($data);
6584 }
6585
6586 sub deserialize
6587 {
6588 my ($class, $file) = @_;
6589 my $self = Storable::retrieve($file) or
6590 die("unable to deserialize $file\n");
6591 ref($self) eq $class or die("did not deserialize a $class");
6592 return $self;
6593 }
6594
6595 sub empty
6596 {
6597 my $self = shift;
6598
6599 return !keys(%{$self->[FILES]});
6600 }
6601
6602 sub files
6603 {
6604 my $self = shift;
6605
6606 # for case-insensitive support: need to store the file keys in
6607 # lower case (so they can be found) - but return the actual
6608 # names of the files (mixed case)
6609
6610 return keys %{$self->[FILES]};
6611 }
6612
6613 sub directories
6614 {
6615 my $self = shift;
6616 # return hash of directories which contain source files
6617 my %dirs;
6618 foreach my $f ($self->files()) {
6619 my $d = File::Basename::dirname($f);
6620 $dirs{$d} = [] unless exists($dirs{$d});
6621 push(@{$dirs{$d}}, $f);
6622 }
6623 return \%dirs;
6624 }
6625
6626 sub file_exists
6627 {
6628 my ($self, $name) = @_;
6629 $name = lc($name) if $lcovutil::case_insensitive;
6630 return exists($self->[FILES]->{$name});
6631 }
6632
6633 sub count_totals
6634 {
6635 my $self = shift;
6636 # return list of (number files, [#lines, #hit], [#branches, #hit], [#functions,#hit])
6637 my @data = (0, [0, 0], [0, 0], [0, 0], [0, 0]);
6638 foreach my $filename ($self->files()) {
6639 my $entry = $self->data($filename);
6640 ++$data[0];
6641 $data[1]->[0] += $entry->found(); # lines
6642 $data[1]->[1] += $entry->hit();
6643 $data[2]->[0] += $entry->branch_found(); # branch
6644 $data[2]->[1] += $entry->branch_hit();
6645 $data[3]->[0] += $entry->function_found(); # function
6646 $data[3]->[1] += $entry->function_hit();
6647
6648 if ($lcovutil::mcdc_coverage) {
6649 $data[4]->[0] += $entry->mcdc_found(); # mcdc
6650 $data[4]->[1] += $entry->mcdc_hit();
6651 }
6652 }
6653 return @data;
6654 }
6655
6656 sub check_fail_under_criteria
6657 {
6658 my ($self, $type) = @_;
6659 my @types;
6660 if (!defined($type)) {
6661 push(@types, 'line');
6662 push(@types, 'branch', 'condition') if $lcovutil::br_coverage;
6663 } else {
6664 push(@types, $type);
6665 }
6666
6667 foreach my $t (@types) {
6668 my ($rate, $plural, $idx);
6669 if ($t eq 'line') {
6670 next unless defined($lcovutil::fail_under_lines);
6671 $rate = $lcovutil::fail_under_lines;
6672 $idx = 1; # lines
6673 $plural = 'lines';
6674 } else {
6675 next unless defined($lcovutil::fail_under_branches);
6676 $rate = $lcovutil::fail_under_branches;
6677 $idx = 2;
6678 $plural = 'branches';
6679 }
6680 next if $rate <= 0;
6681 my @counts = $self->count_totals();
6682 my ($found, $hit) = @{$counts[$idx]};
6683 if ($found == 0) {
6684 lcovutil::info(1, "No $plural found\n");
6685 return "No $plural found";
6686 }
6687 my $actual_rate = ($hit / $found);
6688 my $expected_rate = $rate / 100;
6689 if ($actual_rate < $expected_rate) {
6690 my $msg =
6691 sprintf("Failed '$t' coverage criteria: %0.2f < %0.2f",
6692 $actual_rate, $expected_rate);
6693 lcovutil::info("$msg\n");
6694 return $msg;
6695 }
6696 }
6697 return 0;
6698 }
6699
6700 sub checkCoverageCriteria
6701 {
6702 my $self = shift;
6703
6704 CoverageCriteria::check_failUnder($self);
6705
6706 return unless defined($CoverageCriteria::criteriaCallback);
6707
6708 my $perFile = 0 == scalar(@CoverageCriteria::criteriaCallbackLevels) ||
6709 grep(/file/, @CoverageCriteria::criteriaCallbackLevels);
6710 my %total = ('line' => {
6711 'found' => 0,
6712 'hit' => 0
6713 },
6714 'branch' => {
6715 'found' => 0,
6716 'hit' => 0
6717 },
6718 'condition' => {
6719 'found' => 0,
6720 'hit' => 0
6721 },
6722 'function' => {
6723 'found' => 0,
6724 'hit' => 0
6725 });
6726 my %data;
6727 foreach my $filename ($self->files()) {
6728 my $entry = $self->data($filename);
6729 my @data = ($entry->found(), $entry->hit(),
6730 $entry->branch_found(), $entry->branch_hit(),
6731 $entry->function_found(), $entry->function_hit());
6732 my $idx = 0;
6733 foreach my $t ('line', 'branch', 'function') {
6734 foreach my $x ('found', 'hit') {
6735 $data{$t}->{$t} = $data[$idx] if $perFile;
6736 $total{$t}->{$x} += $data[$idx++];
6737 }
6738 }
6739 if ($perFile) {
6740 CoverageCriteria::executeCallback('file', $filename, \%data);
6741 }
6742 }
6743 CoverageCriteria::executeCallback('top', 'top', \%total);
6744 }
6745
6746 #
6747 # print_summary(fn_do, br_do)
6748 #
6749 # Print overall coverage rates for the specified coverage types.
6750 # $countDat is the array returned by 'TraceFile->count_totals()'
6751
6752 sub print_summary
6753 {
6754 my ($self, $fn_do, $br_do, $mcdc_do) = @_;
6755
6756 $br_do = $lcovutil::br_coverage unless defined($br_do);
6757 $mcdc_do = $lcovutil::mcdc_coverage unless defined($mcdc_do);
6758 $fn_do = $lcovutil::func_coverage unless defined($fn_do);
6759 my @counts = $self->count_totals();
6760 lcovutil::info("Summary coverage rate:\n");
6761 lcovutil::info(" source files: %d\n", $counts[0]);
6762 lcovutil::info(" lines.......: %s\n",
6763 lcovutil::get_overall_line(
6764 $counts[1]->[0], $counts[1]->[1], "line"
6765 ));
6766 lcovutil::info(" functions...: %s\n",
6767 lcovutil::get_overall_line(
6768 $counts[3]->[0], $counts[3]->[1], "function"
6769 )) if ($fn_do);
6770 lcovutil::info(" branches....: %s\n",
6771 lcovutil::get_overall_line(
6772 $counts[2]->[0], $counts[2]->[1], "branch"
6773 )) if ($br_do);
6774 lcovutil::info(" conditions..: %s\n",
6775 lcovutil::get_overall_line(
6776 $counts[4]->[0], $counts[4]->[1], "conditions"
6777 )) if ($mcdc_do);
6778 }
6779
6780 sub skipCurrentFile
6781 {
6782 my $filename = shift;
6783
6784 my $filt = $lcovutil::cov_filter[$lcovutil::FILTER_MISSING_FILE];
6785 if ($filt) {
6786 my $missing = !-r $filename;
6787 if ($missing &&
6788 $lcovutil::resolveCallback) {
6789
6790 my $path = SearchPath::resolveCallback($filename, 0, 1);
6791 $missing = !defined($path) || '' eq $path;
6792 }
6793
6794 if ($missing) {
6795 lcovutil::info(
6796 "Excluding \"$filename\": does not exist/is not readable\n");
6797 ++$filt->[-2];
6798 ++$filt->[-1];
6799 return 1;
6800 }
6801 }
6802
6803 # check whether this file should be excluded or not...
6804 foreach my $p (@lcovutil::exclude_file_patterns) {
6805 my $pattern = $p->[0];
6806 if ($filename =~ $pattern) {
6807 lcovutil::info(1, "exclude $filename: matches '" . $p->[1] . "\n");
6808 ++$p->[-1];
6809 return 1; # all done - explicitly excluded
6810 }
6811 }
6812 if (@lcovutil::include_file_patterns) {
6813 foreach my $p (@lcovutil::include_file_patterns) {
6814 my $pattern = $p->[0];
6815 if ($filename =~ $pattern) {
6816 lcovutil::info(1,
6817 "include: $filename: matches '" . $p->[1] . "\n");
6818 ++$p->[-1];
6819 return 0; # explicitly included
6820 }
6821 }
6822 lcovutil::info(1, "exclude $filename: no include matches\n");
6823 return 1; # not explicitly included - so exclude
6824 }
6825 return 0;
6826 }
6827
6828 sub comments
6829 {
6830 my $self = shift;
6831 return @{$self->[COMMENTS]};
6832 }
6833
6834 sub add_comments
6835 {
6836 my $self = shift;
6837 foreach (@_) {
6838 push(@{$self->[COMMENTS]}, $_);
6839 }
6840 }
6841
6842 sub data
6843 {
6844 my $self = shift;
6845 my $file = shift;
6846 my $checkMatchingBasename = shift;
6847
6848 my $key = $lcovutil::case_insensitive ? lc($file) : $file;
6849 my $files = $self->[FILES];
6850 if (!exists($files->{$key})) {
6851 if (defined $checkMatchingBasename) {
6852 # check if there is a file in the map that has the same basename
6853 # as the lone we are looking for.
6854 # this can happen if the 'udiff' file refers to paths in the repo
6855 # whereas the .info files refer to paths in the build area.
6856 my $base = File::Basename::basename($file);
6857 $base = lc($base) if $lcovutil::case_insensitive;
6858 my $count = 0;
6859 my $found;
6860 foreach my $f (keys %$files) {
6861 my $b = File::Basename::basename($f);
6862 $b = lc($b) if $lcovutil::case_insensitive;
6863 if ($b eq $base) {
6864 $count++;
6865 $found = $files->{$f};
6866 }
6867 }
6868 return $found
6869 if $count == 1;
6870 }
6871 $files->{$key} = TraceInfo->new($file);
6872 }
6873
6874 return $files->{$key};
6875 }
6876
6877 sub contains
6878 {
6879 my ($self, $file) = @_;
6880 my $key = $lcovutil::case_insensitive ? lc($file) : $file;
6881 my $files = $self->[FILES];
6882 return exists($files->{$key});
6883 }
6884
6885 sub remove
6886 {
6887 my ($self, $filename) = @_;
6888 $filename = lc($filename) if $lcovutil::case_insensitive;
6889 $self->file_exists($filename) or
6890 die("remove nonexistent file $filename");
6891 delete($self->[FILES]->{$filename});
6892 }
6893
6894 sub insert
6895 {
6896 my ($self, $filename, $data) = @_;
6897 $filename = lc($filename) if $lcovutil::case_insensitive;
6898 die("insert existing file $filename")
6899 if $self->file_exists($filename);
6900 die("expected TraceInfo got '" . ref($data) . "'")
6901 unless (ref($data) eq 'TraceInfo');
6902 $self->[FILES]->{$filename} = $data;
6903 }
6904
6905 sub merge_tracefile
6906 {
6907 my ($self, $trace, $op) = @_;
6908 die("expected TraceFile")
6909 unless (defined($trace) && 'TraceFile' eq ref($trace));
6910
6911 my $changed = 0;
6912 my $mine = $self->[FILES];
6913 my $yours = $trace->[FILES];
6914 foreach my $filename (keys %$mine) {
6915
6916 if (exists($yours->{$filename})) {
6917 # this file in both me and you...merge as appropriate
6918 #lcovutil::info(1, "merge common $filename\n");
6919 if ($self->data($filename)
6920 ->merge($yours->{$filename}, $op, $filename)) {
6921 $changed = 1;
6922 }
6923 } else {
6924 # file in me and not you - remove mine if intersect operation
6925 if ($op == TraceInfo::INTERSECT) {
6926 #lcovutil::info(1, "removing my $filename: intersect\n");
6927 delete $mine->{$filename};
6928 $changed = 1;
6929 }
6930 }
6931 }
6932 if ($op == TraceInfo::UNION) {
6933 # now add in any files from you that are not present in me...
6934 while (my ($filename, $data) = each(%$yours)) {
6935 if (!exists($mine->{$filename})) {
6936 $mine->{$filename} = $data;
6937 $changed = 1;
6938 }
6939 }
6940 }
6941 $self->add_comments($trace->comments());
6942 return $changed;
6943 }
6944
6945 sub _eraseFunction
6946 {
6947 my ($fcn, $name, $end_line, $source_file, $functionMap,
6948 $lineData, $branchData, $mcdcData, $checksum) = @_;
6949 if (defined($end_line)) {
6950 for (my $line = $fcn->line(); $line <= $end_line; ++$line) {
6951
6952 if (defined($checksum)) {
6953 $checksum->remove($line, 1); # remove if present
6954 }
6955 if ($lineData->remove($line, 1)) {
6956 lcovutil::info(2,
6957 "exclude DA in FN '$name' on $source_file:$line\n");
6958 }
6959 if (defined($branchData) && $branchData->remove($line, 1)) {
6960 lcovutil::info(2,
6961 "exclude BRDA in FN '$name' on $source_file:$line\n");
6962 }
6963 if (defined($mcdcData) && $mcdcData->remove($line, 1)) {
6964 lcovutil::info(2,
6965 "exclude MCDC in FN '$name' on $source_file:$line\n");
6966 }
6967 } # foreach line
6968 }
6969 # remove this function and all its aliases...
6970 $functionMap->remove($fcn);
6971 }
6972
6973 sub _eraseFunctions
6974 {
6975 my ($source_file, $srcReader, $functionMap, $lineData, $branchData,
6976 $mcdcData, $checksum, $state, $isMasterList) = @_;
6977
6978 my $modified = 0;
6979 my $removeTrivial = $cov_filter[$FILTER_TRIVIAL_FUNCTION];
6980 FUNC: foreach my $key ($functionMap->keylist()) {
6981 my $fcn = $functionMap->findKey($key);
6982 my $end_line = $fcn->end_line();
6983 my $name = $fcn->name();
6984 if (!defined($end_line)) {
6985 ++$state->[0]->[1]; # mark that we don't have an end line
6986 # we can skip out of processing if we don't know the end line
6987 # - there is no way for us to remove line and branch points in
6988 # the function region
6989 # Or we can keep going and at least remove the matched function
6990 # coverpoint.
6991 #last; # at least for now: keep going
6992 lcovutil::info(1, "no end line for '$name' at $key\n");
6993 } elsif (
6994 defined($removeTrivial) &&
6995 is_language('c', $source_file) &&
6996 (defined($srcReader) &&
6997 $srcReader->containsTrivialFunction($fcn->line(), $end_line))
6998 ) {
6999 # remove single-line functions which has no body
7000 # Only count what we removed from the top level/master list -
7001 # - otherwise, we double count for every testcase.
7002 ++$removeTrivial->[-2] if $isMasterList;
7003 foreach my $alias (keys %{$fcn->aliases()}) {
7004 lcovutil::info(1,
7005 "\"$source_file\":$end_line: filter trivial FN $alias\n");
7006 _eraseFunction($fcn, $alias, $end_line,
7007 $source_file, $functionMap, $lineData,
7008 $branchData, $mcdcData, $checksum);
7009 ++$removeTrivial->[-1] if $isMasterList;
7010 }
7011 $modified = 1;
7012 next FUNC;
7013 }
7014 foreach my $p (@lcovutil::exclude_function_patterns) {
7015 my $pat = $p->[0];
7016 my $a = $fcn->aliases();
7017 foreach my $alias (keys %$a) {
7018 if ($alias =~ $pat) {
7019 ++$p->[-1] if $isMasterList;
7020 if (defined($end_line)) {
7021 # if user ignored the unsupported message, then the
7022 # best we can do is to remove the matched function -
7023 # and leave the lines and branches in place
7024 lcovutil::info(
7025 1 + (0 == $isMasterList),
7026 "exclude FN $name line range $source_file:[" .
7027 $fcn->line() .
7028 ":$end_line] due to '" . $p->[-2] . "'\n"
7029 );
7030 }
7031 _eraseFunction($fcn, $alias, $end_line,
7032 $source_file, $functionMap, $lineData,
7033 $branchData, $mcdcData, $checksum);
7034 $modified = 1;
7035 next FUNC;
7036 } # if match
7037 } # foreach alias
7038 } # foreach pattern
7039 } # foreach function
7040 return $modified;
7041 }
7042
7043 sub _deriveFunctionEndLines
7044 {
7045 my $traceInfo = shift;
7046 my $modified = 0;
7047
7048 my $start = Time::HiRes::gettimeofday();
7049 my $lineData = $traceInfo->sum();
7050 my @lines = sort { $a <=> $b } $lineData->keylist();
7051 # sort functions by start line number
7052 # ignore lambdas - which we don't process correctly at the moment
7053 # (would need to do syntactic search for the end line)
7054 my @functions = sort { $a->line() <=> $b->line() }
7055 grep({ !$_->isLambda() } $traceInfo->func()->valuelist());
7056
7057 my $currentLine = @lines ? shift(@lines) : 0;
7058 my $funcData = $traceInfo->testfnc();
7059 FUNC: while (@functions) {
7060 my $func = shift(@functions);
7061 my $first = $func->line();
7062 my $end = $func->end_line();
7063 #unless (defined($lineData->value($first))) {
7064 # lcovutil::ignorable_error($lcovutil::ERROR_INCONSISTENT_DATA,
7065 # '"' . $func->filename() .
7066 # "\":$first: first line of function has no linecov.");
7067 # $lineData->append($first, $func->hit());
7068 #}
7069 while ($first > $currentLine) {
7070 if (@lines) {
7071 last if $lines[0] > $first;
7072 $currentLine = shift @lines;
7073 } else {
7074 if (!defined($end)) {
7075 my $suffix =
7076 lcovutil::explain_once('derive_end_line',
7077 " See lcovrc man entry for 'derive_function_end_line'."
7078 );
7079 lcovutil::ignorable_error(
7080 $lcovutil::ERROR_INCONSISTENT_DATA,
7081 '"' . $traceInfo->filename() .
7082 "\":$first: function " . $func->name() .
7083 " found on line but no corresponding 'line' coverage data point. Cannot derive function end line."
7084 . $suffix);
7085 }
7086 next FUNC;
7087 }
7088 }
7089 if (!defined($end)) {
7090 # where is the next function? Find the last 'line' coverpoint
7091 # less than the start line of that function..
7092 if (@lines) {
7093 # if there are no more lines in this file - then everything
7094 # must be ending on the last line we saw
7095 if (@functions) {
7096 my $next_func = $functions[0];
7097 my $start = $next_func->line();
7098 while (@lines &&
7099 $lines[0] < $start) {
7100 $currentLine = shift @lines;
7101 }
7102 } else {
7103 # last line in the file must be the last line
7104 # of this function
7105 if (@lines) {
7106 $currentLine = $lines[-1];
7107 } else {
7108 my $suffix = lcovutil::explain_once('derive_end_line',
7109 " See lcovrc man entry for 'derive_function_end_line'."
7110 );
7111 lcovutil::ignorable_error(
7112 $lcovutil::ERROR_INCONSISTENT_DATA,
7113 '"' . $traceInfo->filename() .
7114 "\":$first: function " . $func->name() .
7115 ": last line in file is not last line of function.$suffix"
7116 );
7117 next FUNC;
7118 }
7119 }
7120 } elsif ($currentLine < $first) {
7121 # we ran out of lines in the data...check for inconsistency
7122 my $suffix =
7123 lcovutil::explain_once('derive_end_line',
7124 " See lcovrc man entry for 'derive_function_end_line'.");
7125 lcovutil::ignorable_error($lcovutil::ERROR_INCONSISTENT_DATA,
7126 '"' . $traceInfo->filename() .
7127 "\":$first: function " . $func->name() .
7128 " found on line but no corresponding 'line' coverage data point. Cannot derive function end line."
7129 . $suffix);
7130
7131 # last FUNC; # quit looking here - all the other functions after this one will have same issue
7132 next FUNC; # warn about them all
7133 }
7134 lcovutil::info(1,
7135 '"' . $traceInfo->filename() .
7136 "\":$currentLine: assign end_line " .
7137 $func->name() . "\n");
7138 # warn that we are deriving end lines
7139 _generate_end_line_message();
7140 $func->set_end_line($currentLine);
7141 $modified = 1;
7142 }
7143 # we may not have set the end line above due to inconsistency
7144 # but we also might not have line data
7145 # - see .../tests/lcov/extract with gcc/4.8
7146 if (!defined($func->end_line())) {
7147 my $suffix =
7148 lcovutil::explain_once('derive_end_line',
7149 " See lcovrc man entry for 'derive_function_end_line'.");
7150 lcovutil::ignorable_error($lcovutil::ERROR_INCONSISTENT_DATA,
7151 '"' . $func->filename() . '":' . $func->line() .
7152 ': failed to set end line for function ' .
7153 $func->name() . '.' . $suffix);
7154 next FUNC;
7155 }
7156
7157 # now look for this function in each testcase -
7158 # set the same endline (if not already set)
7159 my $key = $first;
7160 foreach my $tn ($funcData->keylist()) {
7161 my $d = $funcData->value($tn);
7162 my $f = $d->findKey($key);
7163 if (defined($f)) {
7164 if (!defined($f->end_line())) {
7165 $f->set_end_line($func->end_line());
7166 $modified = 1;
7167 } else {
7168 if ($f->end_line() != $func->end_line()) {
7169 lcovutil::ignorable_error(
7170 $lcovutil::ERROR_INCONSISTENT_DATA,
7171 '"' . $func->file() .
7172 '":' . $first . ': function \'' .
7173 $func->name() . ' last line is ' .
7174 $func->end_line() . ' but is ' .
7175 $f->end_line() . " in testcase '$tn'"
7176 );
7177 }
7178 }
7179 }
7180 } #foreach testcase
7181 } # for each function
7182 my $end = Time::HiRes::gettimeofday();
7183 $lcovutil::profileData{derive_end}{$traceInfo->filename()} = $end - $start;
7184 return $modified;
7185 }
7186
7187 sub _consistencySuffix
7188 {
7189 return lcovutil::explain_once('consistency_check',
7190 "\n\tTo skip consistency checks, see the 'check_data_consistency' section in man lcovrc(5)."
7191 );
7192 }
7193
7194 sub _fixFunction
7195 {
7196 my ($traceInfo, $func, $count) = @_;
7197
7198 my @fix = ($func);
7199 my $line = $func->line();
7200 my $per_testcase = $traceInfo->testfnc();
7201 foreach my $testname ($per_testcase->keylist()) {
7202 my $data = $traceInfo->testfnc($testname);
7203 my $f = $data->findKey($line);
7204 push(@fix, $f) if defined($f);
7205 }
7206
7207 foreach my $f (@fix) {
7208 $f->[FunctionEntry::COUNT] = $count;
7209
7210 # and mark that each alias was hit...
7211 my $aliases = $f->aliases();
7212 foreach my $alias (keys %$aliases) {
7213 $aliases->{$alias} += $count;
7214 }
7215 }
7216 }
7217
7218 sub _checkConsistency
7219 {
7220 return unless $lcovutil::check_data_consistency;
7221 my $traceInfo = shift;
7222 my $modified = 0;
7223
7224 my $start = Time::HiRes::gettimeofday();
7225
7226 my @functions = sort { $a->line() <=> $b->line() }
7227 grep({ defined($_->end_line()) } $traceInfo->func()->valuelist());
7228 my $lineData = $traceInfo->sum();
7229 my @lines = sort { $a <=> $b } $lineData->keylist()
7230 if @functions;
7231 my $currentLine = @lines ? shift(@lines) : 0;
7232 FUNC: while (@functions) {
7233 my $func = shift(@functions);
7234 my $first = $func->line();
7235 my $end = $func->end_line();
7236 my $imHit = $func->hit() != 0; # I'm hit if any aliases is hit
7237 my $lineHit = 0;
7238 while ($first > $currentLine) {
7239 # skip until we find the first line of the current function
7240 if (@lines) {
7241 $currentLine = shift(@lines);
7242 } else {
7243 # can only get here with really inconsistent data...would have
7244 lcovutil::ignorable_error($lcovutil::ERROR_INCONSISTENT_DATA,
7245 '"' . $func->filename() .
7246 "\":$first: file linecov does not match function cov data - skipping checks."
7247 );
7248 last FUNC;
7249 }
7250 }
7251 while ($end >= $currentLine) {
7252 # look for first covered line in this function -
7253 # sufficient to just look at the such line
7254 die("bug: " . $func->filename() . " [$first:$end]: $currentLine")
7255 unless $first <= $currentLine && $currentLine <= $end;
7256 my $hit = $lineData->value($currentLine);
7257 $lineHit = 1 if $hit;
7258 if ($hit && !$imHit) {
7259 # don't warn about the first line of a lambda:
7260 # - the decl may executed even if the lambda function itself is
7261 # not called
7262 # - if no other lines are hit, then then the function is not
7263 # covered, but the coverage DB is consistent
7264 # - if some other line _is_ hit, then, the data is inconsistent
7265 if ($func->isLambda() && $currentLine == $first) {
7266 $lineHit = 0;
7267 last unless @lines;
7268 $currentLine = shift(@lines);
7269 next;
7270 }
7271 my $suffix =
7272 ($lcovutil::fix_inconsistency && lcovutil::is_ignored(
7273 $lcovutil::ERROR_INCONSISTENT_DATA)
7274 ) ? ": function marked 'hit'" :
7275 '';
7276 lcovutil::ignorable_error($lcovutil::ERROR_INCONSISTENT_DATA,
7277 '"' . $func->filename() .
7278 "\":$first: function '" . $func->name() .
7279 "' is not hit but line $currentLine is$suffix." .
7280 _consistencySuffix());
7281 if ($lcovutil::fix_inconsistency) {
7282 # if message was ignored, then mark the function and all
7283 # its aliases hit
7284 $imHit = 1;
7285 $modified = 1;
7286 _fixFunction($traceInfo, $func, $hit);
7287 }
7288 last; # only warn on the first hit line in the function
7289 }
7290 last if $lineHit && $hit; # can stop looking at this function now
7291 last unless (@lines);
7292 $currentLine = shift @lines;
7293 }
7294 if ($imHit && !$lineHit) {
7295 my $suffix =
7296 ($lcovutil::fix_inconsistency &&
7297 lcovutil::is_ignored($lcovutil::ERROR_INCONSISTENT_DATA)) ?
7298 ": function marked 'not hit'" :
7299 '';
7300 lcovutil::ignorable_error($lcovutil::ERROR_INCONSISTENT_DATA,
7301 '"' . $traceInfo->filename() .
7302 "\":$first: function '" . $func->name() .
7303 "' is hit but no contained lines are hit$suffix." .
7304 _consistencySuffix());
7305 if ($lcovutil::fix_inconsistency) {
7306 # if message was ignored, then mark the function and its aliases
7307 # not hit
7308 $modified = 1;
7309 _fixFunction($traceInfo, $func, 0);
7310 }
7311 }
7312 }
7313
7314 # check MC/DC consistency -
7315 # Note that we might have an MC/DC block on a line which has no
7316 # linecov data
7317 # This can happen for template functions (and similar) where the
7318 # expression is statically determned to be true or false - and elided
7319 # by the compiler. In that case, generate a new line coverpoint
7320 if ($lcovutil::mcdc_coverage) {
7321 my $mcdc = $traceInfo->mcdc();
7322 my $testcase_mcdc = $traceInfo->testcase_mcdc();
7323 foreach my $line ($mcdc->keylist()) {
7324 my $lineHit = $lineData->value($line);
7325 next if defined($lineHit);
7326
7327 lcovutil::info(1,
7328 '"' . $traceInfo->filename() .
7329 "\":$line: generating DA entry for orphan MC/DC\n"
7330 );
7331 my $block = $mcdc->value($line);
7332 my ($found, $hit) = $block->totals();
7333 $lineData->append($line, $hit);
7334
7335 # create the entry in the per-testcase data
7336 foreach my $testcase ($testcase_mcdc->keylist()) {
7337 my $m = $testcase_mcdc->value($testcase);
7338 if ($m->value($line)) {
7339 $traceInfo->test($testcase)->append($line, $hit);
7340 }
7341 }
7342 }
7343 }
7344
7345 # also check branch data consistency...should not have non-zero branch hit
7346 # count if line is not hit - and vice versa
7347 my $checkBranchConsistency =
7348 !TraceFile::is_language('perl', $traceInfo->filename());
7349 if ($lcovutil::br_coverage) {
7350 my $brData = $traceInfo->sumbr();
7351
7352 foreach my $line ($brData->keylist()) {
7353 # we expect to find a line everywhere there is a branch
7354
7355 my $lineHit = $lineData->value($line);
7356 unless (defined($lineHit)) {
7357 lcovutil::ignorable_error($lcovutil::ERROR_INCONSISTENT_DATA,
7358 '"' . $traceInfo->filename() .
7359 "\":$line: location has branchcov but no linecov data"
7360 . _consistencySuffix());
7361 }
7362
7363 my $brHit = 0;
7364 my $brd = $brData->value($line);
7365 BLOCK: foreach my $id ($brd->blocks()) {
7366 my $block = $brd->getBlock($id);
7367 foreach my $br (@$block) {
7368 if (0 != $br->count()) {
7369 $brHit = 1;
7370 last BLOCK;
7371 }
7372 }
7373 }
7374 if (!defined($lineHit)) {
7375 # must have ignored the above error - so build fake line data here
7376 # (maybe should delete the branch instead?)
7377 $lineData->append($line, $brHit);
7378 next;
7379 }
7380 if ($lineHit && !$brHit) {
7381 lcovutil::ignorable_error($lcovutil::ERROR_INCONSISTENT_DATA,
7382 '"' . $traceInfo->filename() .
7383 "\":$line: line is hit but no branches on line have been evaluated."
7384 . _consistencySuffix())
7385 if $checkBranchConsistency;
7386 } elsif (!$lineHit && $brHit) {
7387 lcovutil::ignorable_error($lcovutil::ERROR_INCONSISTENT_DATA,
7388 '"' . $traceInfo->filename() .
7389 "\":$line: line is not hit but at least one branch on line has been evaluated."
7390 . _consistencySuffix());
7391 }
7392 }
7393 }
7394
7395 # @todo expect to have a branch everywhere we have an MCDC -
7396 # further, expect the number of branches and conditions to match
7397
7398 my $end = Time::HiRes::gettimeofday();
7399 $lcovutil::profileData{check_consistency}{$traceInfo->filename()} =
7400 $end - $start;
7401 return $modified;
7402 }
7403
7404 sub _filterFile
7405 {
7406 my ($traceInfo, $source_file, $actions, $srcReader, $state) = @_;
7407
7408 my $modified = 0;
7409 if (0 != ($actions & DID_DERIVE)) {
7410 $modified = _deriveFunctionEndLines($traceInfo);
7411 $modified ||= _checkConsistency($traceInfo);
7412 if (0 == ($actions & DID_FILTER)) {
7413 return [$traceInfo, $modified];
7414 }
7415 }
7416 # @todo: if MCDC has just one expression, then drop it -
7417 # it is equivalent to branch coverage.
7418 my $region = $cov_filter[$FILTER_EXCLUDE_REGION];
7419 my $branch_region = $cov_filter[$FILTER_EXCLUDE_BRANCH];
7420 my $range = $cov_filter[$lcovutil::FILTER_LINE_RANGE];
7421 my $branch_histogram = $cov_filter[$FILTER_BRANCH_NO_COND]
7422 if (is_language('c', $source_file));
7423 my $brace_histogram = $cov_filter[$FILTER_LINE_CLOSE_BRACE]
7424 if (is_language('c', $source_file));
7425 my $blank_histogram = $cov_filter[$FILTER_BLANK_LINE];
7426 my $function_alias_histogram = $cov_filter[$FILTER_FUNCTION_ALIAS];
7427 my $trivial_histogram = $cov_filter[$FILTER_TRIVIAL_FUNCTION];
7428 my $filter_initializer_list = $cov_filter[$FILTER_INITIALIZER_LIST]
7429 if (is_language('c', $source_file));
7430 my $directive = $cov_filter[$FILTER_DIRECTIVE];
7431 my $omit = $cov_filter[$FILTER_OMIT_PATTERNS]
7432 if defined($FILTER_OMIT_PATTERNS);
7433 my $mcdc_single = $cov_filter[$FILTER_MCDC_SINGLE]
7434 if defined($FILTER_MCDC_SINGLE && $lcovutil::mcdc_coverage);
7435
7436 my $context = MessageContext->new("filtering $source_file");
7437 if (lcovutil::is_filter_enabled()) {
7438 lcovutil::info(1, "reading $source_file for lcov filtering\n");
7439 $srcReader->open($source_file);
7440 } else {
7441 $srcReader->close();
7442 }
7443 my $path = ReadCurrentSource::resolve_path($source_file);
7444 lcovutil::info(1, "extractVersion($path) for $source_file\n")
7445 if $path ne $source_file;
7446 # Note: this is checking the version of the 'current' file - even if
7447 # we are actually reading the baseline version.
7448 # - This is what we want, as the 'baseline read' is actually recovering/
7449 # recreating the baseline source from the current source and the diff.
7450 # - We already checked that the diff and the coverage DB baseline/current
7451 # version data is consistent - so filtering will be accurate as long as
7452 # we see the right 'current' source version.
7453 my $fileVersion = lcovutil::extractFileVersion($path)
7454 if $srcReader->notEmpty();
7455 if (defined($fileVersion) &&
7456 defined($traceInfo->version())
7457 &&
7458 !lcovutil::checkVersionMatch($source_file, $traceInfo->version(),
7459 $fileVersion, 'filter')
7460 ) {
7461 lcovutil::info(1,
7462 '$source_file: skip filtering due to version mismatch\n');
7463 return ($traceInfo, 0);
7464 }
7465
7466 if (defined($lcovutil::func_coverage) &&
7467 (0 != scalar(@lcovutil::exclude_function_patterns) ||
7468 defined($trivial_histogram))
7469 ) {
7470 # filter excluded function line ranges
7471 my $funcData = $traceInfo->testfnc();
7472 my $lineData = $traceInfo->test();
7473 my $branchData = $traceInfo->testbr();
7474 my $mcdcData = $traceInfo->testcase_mcdc();
7475 my $checkData = $traceInfo->check();
7476 my $reader = defined($trivial_histogram) &&
7477 $srcReader->notEmpty() ? $srcReader : undef;
7478
7479 foreach my $tn ($lineData->keylist()) {
7480 my $m =
7481 _eraseFunctions($source_file, $reader,
7482 $funcData->value($tn), $lineData->value($tn),
7483 $branchData->value($tn), $mcdcData->value($tn),
7484 $checkData->value($tn), $state,
7485 0);
7486 $modified ||= $m;
7487 }
7488 my $m =
7489 _eraseFunctions($source_file, $reader,
7490 $traceInfo->func(), $traceInfo->sum(),
7491 $traceInfo->sumbr(), $traceInfo->mcdc(),
7492 $traceInfo->check(), $state,
7493 1);
7494 $modified ||= $m;
7495 }
7496
7497 return
7498 unless ($srcReader->notEmpty() &&
7499 lcovutil::is_filter_enabled());
7500
7501 my $filterExceptionBranches = FilterBranchExceptions->new();
7502
7503 my ($testdata, $sumcount, $funcdata, $checkdata, $testfncdata,
7504 $testbrdata, $sumbrcount, $mcdc, $testmcdc) = $traceInfo->get_info();
7505
7506 foreach my $testname (sort($testdata->keylist())) {
7507 my $testcount = $testdata->value($testname);
7508 my $testfnccount = $testfncdata->value($testname);
7509 my $testbrcount = $testbrdata->value($testname);
7510 my $mcdc_count = $testmcdc->value($testname);
7511
7512 my $functionMap = $testfncdata->{$testname};
7513 if ($lcovutil::func_coverage &&
7514 $functionMap &&
7515 ($region || $range)) {
7516 # Write function related data - sort by line number
7517
7518 foreach my $key ($functionMap->keylist()) {
7519 my $data = $functionMap->findKey($key);
7520 my $line = $data->line();
7521
7522 my $remove;
7523 if ($srcReader->isOutOfRange($line, 'line')) {
7524 $remove = 1;
7525 lcovutil::info(1,
7526 "filter FN " . $data->name() .
7527 ' ' . $data->file() . ":$line\n");
7528 ++$range->[-2]; # one location where this applied
7529 } elsif ($srcReader->isExcluded($line)) {
7530 $remove = 1;
7531 my $reason = $srcReader->excludeReason($line);
7532 foreach my $f ([ReadCurrentSource::EXCLUDE_REGION, $region],
7533 [ReadCurrentSource::OMIT_LINE, $omit]) {
7534 if ($reason & $f->[0]) {
7535 $f->[1]->[-2] += scalar(keys %{$data->aliases()});
7536 last;
7537 }
7538 }
7539 }
7540 if ($remove) {
7541 #remove this function from everywhere
7542 foreach my $tn ($testfncdata->keylist()) {
7543 my $d = $testfncdata->value($tn);
7544 my $f = $d->findKey($key);
7545 next unless $f;
7546 $d->remove($f);
7547 }
7548 # and remove from the master table
7549 $funcdata->remove($funcdata->findKey($key));
7550 $modified = 1;
7551 next;
7552 } # if excluded
7553 } # foreach function
7554 } # if func_coverage
7555 # $testbrcount is undef if there are no branches in the scope
7556 if (($lcovutil::br_coverage || $lcovutil::mcdc_coverage) &&
7557 (defined($testbrcount) ||
7558 defined($mcdc_count)) &&
7559 ($branch_histogram ||
7560 $region ||
7561 $branch_region ||
7562 $range ||
7563 $filterExceptionBranches ||
7564 $omit)
7565 ) {
7566 my %uniq;
7567 # check MC/DC lines which are not also branch lines
7568 foreach
7569 my $line (defined($mcdc_count) ? $mcdc_count->keylist() : (),
7570 defined($testbrcount) ? $testbrcount->keylist() : ()) {
7571 next if exists($uniq{$line});
7572 $uniq{$line} = 1;
7573
7574 # for counting: keep track filter which triggered exclusion -
7575 my $remove;
7576 # omit if line excluded or branches excluded on this line
7577 if ($srcReader->isOutOfRange($line, 'branch')) {
7578 # only counting line coverpoints that got excluded
7579 die("inconsistent state") unless $range;
7580 $remove = $range;
7581 } elsif ($srcReader->isExcluded($line, 2)) {
7582 # all branches here
7583 my $reason = $srcReader->excludeReason($line);
7584 foreach my $f ([ReadCurrentSource::EXCLUDE_REGION, $region],
7585 [ReadCurrentSource::OMIT_LINE, $omit],
7586 [ReadCurrentSource::EXCLUDE_DIRECTIVE,
7587 $directive
7588 ],
7589 [ReadCurrentSource::EXCLUDE_BRANCH_REGION,
7590 $branch_region
7591 ]
7592 ) {
7593 if ($reason & $f->[0]) {
7594 $remove = $f->[1];
7595 last;
7596 }
7597 }
7598 die("inconsistent reason $reason") unless $remove;
7599 } elsif ($branch_histogram &&
7600 !$srcReader->containsConditional($line)) {
7601 $remove = $branch_histogram;
7602 }
7603 if ($remove) {
7604 foreach my $t ([$testbrdata, $sumbrcount, 'BRDA'],
7605 [$testmcdc, $mcdc, 'MCDC']) {
7606 my ($testCount, $sumCount, $str) = @$t;
7607 next unless $sumCount;
7608 my $brdata = $sumCount->value($line);
7609 # might not be MCDC here, even if there is a branch
7610 next unless $brdata;
7611 ++$remove->[-2]; # one line where we skip
7612 $remove->[-1] += ($brdata->totals())[0];
7613 lcovutil::info(2,
7614 "filter $str '"
7615 .
7616 ($line < $srcReader->numLines() ?
7617 $srcReader->getLine($line) :
7618 '<-->') .
7619 "' $source_file:$line\n");
7620 # now remove this branch everywhere...
7621 foreach my $tn ($testCount->keylist()) {
7622 my $d = $testCount->value($tn);
7623 $d->remove($line, 1); # remove if present
7624 }
7625 # remove at top
7626 $sumCount->remove($line);
7627 $modified = 1;
7628 }
7629 } elsif (defined($filterExceptionBranches) &&
7630 defined($sumbrcount) &&
7631 defined($sumbrcount->value($line))) {
7632 # exclude exception branches here
7633 my $m =
7634 $filterExceptionBranches->filter($line, $srcReader,
7635 $sumbrcount, $testbrdata, $mcdc, $mcdc_count);
7636 $modified ||= $m;
7637 }
7638 } # foreach line
7639 } # if branch_coverage
7640 if ($mcdc_single) {
7641 # find single-expression MC/DC's - if there is a matching branch
7642 # expression on the same line, then remove the MC/DC
7643 foreach my $line ($mcdc_count->keylist()) {
7644 my $block = $mcdc_count->value($line);
7645 my $groups = $block->groups();
7646 if (exists($groups->{1}) &&
7647 scalar(keys %$groups) == 1) {
7648 my $branch = $testbrcount->value($line);
7649 next unless $branch && ($branch->totals())[0] == 2;
7650 $mcdc_count->remove($line);
7651 ++$mcdc_single->[-2]; # one MC/DC skipped
7652
7653 $mcdc->remove($line); # remove at top
7654 }
7655 }
7656 }
7657 next
7658 unless $region ||
7659 $range ||
7660 $brace_histogram ||
7661 $branch_histogram ||
7662 $directive ||
7663 $omit ||
7664 $filter_initializer_list;
7665
7666 # Line related data
7667 my %initializerListRange;
7668 foreach my $line ($testcount->keylist()) {
7669 # don't suppresss if this line has associated branch or MC/DC data
7670 next
7671 if (
7672 (defined($sumbrcount) && defined($sumbrcount->value($line))) ||
7673 (defined($mcdc_count) &&
7674 defined($mcdc_count->value($line))));
7675
7676 my $is_initializer;
7677 my $is_filtered = undef;
7678 if (exists($initializerListRange{$line})) {
7679 $is_initializer = 1;
7680 $is_filtered = $filter_initializer_list;
7681 delete $initializerListRange{$line};
7682 } elsif ($filter_initializer_list) {
7683 # check if this line looks like a complete statement (balanced
7684 # parens, ending with semicolon, etc -
7685 # or whether subsequent lines are required for completion.
7686 # If those subsequent lines have associated coverpoints,
7687 # then those points should be filtered out (see issue #1222)
7688 my $count = $srcReader->is_initializerList($line);
7689 if (0 != $count) {
7690 $is_initializer = 1;
7691 $is_filtered = $filter_initializer_list;
7692 for (my $l = $line + $count - 1; $l > $line; --$l) {
7693 # record start of range
7694 $initializerListRange{$l} = $line;
7695 }
7696 }
7697 }
7698
7699 my $outOfRange = $srcReader->isOutOfRange($line, 'line')
7700 unless $is_filtered;
7701 $is_filtered = $lcovutil::cov_filter[$lcovutil::FILTER_LINE_RANGE]
7702 if !defined($is_filtered) &&
7703 defined($outOfRange) &&
7704 $outOfRange;
7705 my $excluded = $srcReader->isExcluded($line)
7706 unless $is_filtered;
7707 if (defined($excluded) && $excluded) {
7708 my $reason = $srcReader->excludeReason($line);
7709 foreach my $f ([ReadCurrentSource::EXCLUDE_REGION, $region],
7710 [ReadCurrentSource::OMIT_LINE, $omit],
7711 [ReadCurrentSource::EXCLUDE_DIRECTIVE,
7712 $directive
7713 ]
7714 ) {
7715 if ($reason & $f->[0]) {
7716 $is_filtered = $f->[1];
7717 last;
7718 }
7719 }
7720 }
7721 my $l_hit = $testcount->value($line);
7722 my $isCloseBrace =
7723 ($brace_histogram &&
7724 $srcReader->suppressCloseBrace($line, $l_hit, $testcount))
7725 unless $is_filtered;
7726 $is_filtered = $brace_histogram
7727 if !defined($is_filtered) &&
7728 defined($isCloseBrace) &&
7729 $isCloseBrace;
7730 my $isBlank =
7731 ($blank_histogram &&
7732 ($lcovutil::filter_blank_aggressive || $l_hit == 0) &&
7733 $srcReader->isBlank($line))
7734 unless $is_filtered;
7735 $is_filtered = $blank_histogram
7736 if !defined($is_filtered) && defined($isBlank) && $isBlank;
7737
7738 next unless $is_filtered;
7739
7740 $modified = 1;
7741 lcovutil::info(2,
7742 'filter DA (' . $is_filtered->[0] . ') '
7743 .
7744 ($line < $srcReader->numLines() ?
7745 ("'" . $srcReader->getLine($line) . "'") :
7746 "") .
7747 " $source_file:$line\n");
7748
7749 unless (defined($outOfRange) && $outOfRange) {
7750 # some filters already counted...
7751 ++$is_filtered->[-2]; # one location where this applied
7752 ++$is_filtered->[-1]; # one coverpoint suppressed
7753 }
7754
7755 # now remove everywhere
7756 foreach my $tn ($testdata->keylist()) {
7757 my $d = $testdata->value($tn);
7758 $d->remove($line, 1); # remove if present
7759 }
7760 $sumcount->remove($line);
7761 if (exists($checkdata->{$line})) {
7762 delete($checkdata->{$line});
7763 }
7764 } # foreach line
7765 } #foreach test
7766 # count the number of function aliases..
7767 if ($function_alias_histogram) {
7768 $function_alias_histogram->[-2] += $funcdata->numFunc(1);
7769 $function_alias_histogram->[-1] += $funcdata->numFunc(0);
7770 }
7771 return ($traceInfo, $modified);
7772 }
7773
7774 sub _mergeParallelChunk
7775 {
7776 # called from parent
7777 my ($self, $tmp, $child, $children, $childstatus, $store, $worklist,
7778 $childRetryCounts)
7779 = @_;
7780
7781 my ($chunk, $forkAt, $chunkId) = @{$children->{$child}};
7782 my $dumped = File::Spec->catfile($tmp, "dumper_$child");
7783 my $childLog = File::Spec->catfile($tmp, "filter_$child.log");
7784 my $childErr = File::Spec->catfile($tmp, "filter_$child.err");
7785
7786 lcovutil::debug(1, "merge:$child ID $chunkId\n");
7787 my $start = Time::HiRes::gettimeofday();
7788 foreach my $f ($childLog, $childErr) {
7789 if (!-f $f) {
7790 $f = ''; # there was no output
7791 next;
7792 }
7793 if (open(RESTORE, "<", $f)) {
7794 # slurp into a string and eval..
7795 my $str = do { local $/; <RESTORE> }; # slurp whole thing
7796 close(RESTORE) or die("unable to close $f: $!\n");
7797 unlink $f;
7798 $f = $str;
7799 } else {
7800 $f = "unable to open $f: $!";
7801 if (0 == $childstatus) {
7802 lcovutil::report_parallel_error('filter',
7803 $ERROR_PARALLEL, $child, 0, $f, keys(%$children));
7804 }
7805 }
7806 }
7807 my $signal = $childstatus & 0xFF;
7808 print(STDOUT $childLog)
7809 if ((0 != $childstatus &&
7810 $signal != POSIX::SIGKILL &&
7811 $lcovutil::max_fork_fails != 0) ||
7812 $lcovutil::verbose);
7813 print(STDERR $childErr);
7814 my $data = Storable::retrieve($dumped)
7815 if (-f $dumped && $childstatus == 0);
7816 if (defined($data)) {
7817 my ($updates, $save, $state, $childFinish, $update) = @$data;
7818
7819 lcovutil::update_state(@$update);
7820 #my $childCpuTime = $lcovutil::profileData{filt_child}{$chunkId};
7821 #$totalFilterCpuTime += $childCpuTime;
7822 #$intervalFilterCpuTime += $childCpuTime;
7823
7824 my $now = Time::HiRes::gettimeofday();
7825 $lcovutil::profileData{filt_undump}{$chunkId} = $now - $start;
7826
7827 foreach my $patType (@{$store->[0]}) {
7828 my $svType = shift(@{$save->[0]});
7829 foreach my $p (@$patType) {
7830 $p->[-1] += shift(@$svType);
7831 }
7832 }
7833 for (my $i = scalar(@{$store->[1]}) - 1; $i >= 0; --$i) {
7834 $store->[1]->[$i]->[-2] += $save->[1]->[$i]->[0];
7835 $store->[1]->[$i]->[-1] += $save->[1]->[$i]->[1];
7836 }
7837 foreach my $d (@$updates) {
7838 $self->_updateModifiedFile(@$d, $state);
7839 }
7840
7841 my $final = Time::HiRes::gettimeofday();
7842 $lcovutil::profileData{filt_merge}{$chunkId} = $final - $now;
7843 $lcovutil::profileData{filt_queue}{$chunkId} = $start - $childFinish;
7844
7845 #$intervalMonitor->checkUpdate($processedFiles);
7846
7847 } else {
7848 if (!-f $dumped ||
7849 POSIX::SIGKILL == $signal) {
7850
7851 if (exists($childRetryCounts->{$chunkId})) {
7852 $childRetryCounts->{$chunkId} += 1;
7853 } else {
7854 $childRetryCounts->{$chunkId} = 1;
7855 }
7856 lcovutil::report_fork_failure(
7857 "filter segment $chunkId",
7858 (POSIX::SIGKILL == $signal ?
7859 "killed by OS - possibly due to out-of-memory" :
7860 "serialized data $dumped not found"),
7861 $childRetryCounts->{$chunkId});
7862 push(@$worklist, $chunk);
7863 } else {
7864 lcovutil::report_parallel_error('filter',
7865 $ERROR_PARALLEL, $child, $childstatus,
7866 "unable to filter segment $chunkId: $@",
7867 keys(%$children));
7868 }
7869 }
7870 foreach my $f ($dumped) {
7871 unlink $f
7872 if -f $f;
7873 }
7874 my $to = Time::HiRes::gettimeofday();
7875 $lcovutil::profileData{filt_chunk}{$chunkId} = $to - $forkAt;
7876 }
7877
7878 sub _generate_end_line_message
7879 {
7880 # don't generate gcov warnings for tools that don't use gcov
7881 return if grep({ /(llvm|perl|py|xml)2lcov/ } $lcovutil::tool_name);
7882 if (lcovutil::warn_once('compiler_version', 1)) {
7883 my $msg =
7884 'Function begin/end line exclusions not supported with this version of GCC/gcov; require gcc/9 or newer';
7885 if ((defined($lcovutil::derive_function_end_line) &&
7886 $lcovutil::derive_function_end_line != 0) ||
7887 (defined($lcovutil::derive_function_end_line_all_files) &&
7888 $lcovutil::derive_function_end_line_all_files != 0)
7889 ) {
7890 lcovutil::ignorable_warning($lcovutil::ERROR_UNSUPPORTED,
7891 $msg .
7892 ": attempting to derive function end lines - see lcovrc man entry for 'derive_function_end_line'."
7893 );
7894 } else {
7895 lcovutil::ignorable_error($lcovutil::ERROR_UNSUPPORTED,
7896 $msg .
7897 ". See lcovrc man entry for 'derive_function_end_line'."
7898 );
7899 }
7900 }
7901 }
7902
7903 sub _updateModifiedFile
7904 {
7905 my ($self, $name, $traceFile, $state) = @_;
7906 $self->[FILES]->{$name} = $traceFile;
7907
7908 _generate_end_line_message()
7909 if $state->[0]->[1] != 0;
7910 }
7911
7912 sub _processParallelChunk
7913 {
7914 # called from child
7915 my $childStart = Time::HiRes::gettimeofday();
7916 my ($tmp, $chunk, $srcReader, $save, $state, $forkAt, $chunkId) = @_;
7917 # clear profile - want only my contribution
7918 my $currentState = lcovutil::initial_state();
7919 my $stdout_file = File::Spec->catfile($tmp, "filter_$$.log");
7920 my $stderr_file = File::Spec->catfile($tmp, "filter_$$.err");
7921 my $childInfo;
7922 # set count to zero so we know how many got created in
7923 # the child process
7924 my $now = Time::HiRes::gettimeofday();
7925 my $status = 0;
7926
7927 # clear current status so we see updates from this child
7928 # pattern counts
7929 foreach my $l (@{$save->[0]}) {
7930 foreach my $p (@$l) {
7931 $p->[-1] = 0;
7932 }
7933 }
7934 # filter counts
7935 foreach my $f (@{$save->[1]}) {
7936 $f->[-1] = 0;
7937 $f->[-2] = 0;
7938 }
7939 # using 'capture' here so that we can both capture/redirect geninfo
7940 # messages from a child process during parallel execution AND
7941 # redirect stdout/stderr from gcov calls.
7942 # It does not work to directly open/reopen the STDOUT and STDERR
7943 # descriptors due to interactions between the child and parent
7944 # processes (see the Capture::Tiny doc for some details)
7945 my $start = Time::HiRes::gettimeofday();
7946 my @updates;
7947 my ($stdout, $stderr, $code) = Capture::Tiny::capture {
7948
7949 eval {
7950 foreach my $d (@$chunk) {
7951 # could keep track of individual file time if we wanted to
7952 my ($data, $modified) = _filterFile(@$d, $srcReader, $state);
7953
7954 lcovutil::info(1,
7955 $d->[1] . ' is ' .
7956 ($modified ? '' : 'NOT ') . "modified\n");
7957 if ($modified) {
7958 push(@updates, [$d->[1], $data]);
7959 }
7960 }
7961 };
7962 if ($@) {
7963 print(STDERR $@);
7964 $status = 1;
7965 }
7966 };
7967 my $end = Time::HiRes::gettimeofday();
7968 # collect pattern counts
7969 my @pcounts;
7970 foreach my $l (@{$save->[0]}) {
7971 my @c = map({ $_->[-1] } @$l); # grab the counts
7972 push(@pcounts, \@c);
7973 }
7974 $save->[0] = \@pcounts;
7975 # filter counts
7976 foreach my $f (@{$save->[1]}) {
7977 $f = [$f->[-2], $f->[-1]];
7978 }
7979
7980 # parent might have already caught an error, cleaned up and
7981 # removed the tempdir and exited.
7982 lcovutil::check_parent_process();
7983
7984 # print stdout and stderr ...
7985 foreach my $d ([$stdout_file, $stdout], [$stderr_file, $stderr]) {
7986 next unless ($d->[1]); # only print if there is something to print
7987 my $f = InOutFile->out($d->[0]);
7988 my $h = $f->hdl();
7989 print($h $d->[1]);
7990 }
7991 my $dumpf = File::Spec->catfile($tmp, "dumper_$$");
7992 my $then = Time::HiRes::gettimeofday();
7993 $lcovutil::profileData{filt_proc}{$chunkId} = $then - $forkAt;
7994 $lcovutil::profileData{filt_child}{$chunkId} = $end - $start;
7995 my $data;
7996 eval {
7997 $data = Storable::store([\@updates, $save, $state, $then,
7998 lcovutil::compute_update($currentState)
7999 ],
8000 $dumpf);
8001 };
8002 if ($@ || !defined($data)) {
8003 lcovutil::ignorable_error($lcovutil::ERROR_PARALLEL,
8004 "Child $$ serialize failed" . ($@ ? ": $@" : ''));
8005 }
8006 return $status;
8007 }
8008
8009 # chunkID is only used for uniquification and as a key in profile data.
8010 # We want this umber to be unique - even if we process more than one TraceFile
8011 our $masterChunkID = 0;
8012
8013 sub _processFilterWorklist
8014 {
8015 my ($self, $srcReader, $fileList) = @_;
8016
8017 my $chunkSize;
8018 my $parallel = $lcovutil::lcov_filter_parallel;
8019 # not much point in parallel calculation if the number of files is small
8020 my $workList = $fileList;
8021 if (exists($ENV{LCOV_FORCE_PARALLEL}) ||
8022 (scalar(@$fileList) > 50 &&
8023 $parallel &&
8024 1 < $lcovutil::maxParallelism)
8025 ) {
8026
8027 $parallel = $lcovutil::maxParallelism;
8028
8029 if (defined($lcovutil::lcov_filter_chunk_size)) {
8030 if ($lcovutil::lcov_filter_chunk_size =~ /^(\d+)\s*(%?)$/) {
8031 if (defined($2) && $2) {
8032 # a percentage
8033 $chunkSize = int(scalar(@$fileList) * $1 / 100);
8034 } else {
8035 # an absolute value
8036 $chunkSize = $1;
8037 }
8038 } else {
8039 lcovutil::ignorable_warning($lcovutil::ERROR_FORMAT,
8040 "lcov_filter_chunk_size '$lcovutil::lcov_filter_chunk_size not recognized - ignoring\n"
8041 );
8042 }
8043 }
8044
8045 if (!defined($chunkSize)) {
8046 $chunkSize =
8047 $maxParallelism ?
8048 (int(0.8 * scalar(@$fileList) / $lcovutil::maxParallelism)) :
8049 1;
8050 if ($chunkSize > 100) {
8051 $chunkSize = 100;
8052 } elsif ($chunkSize < 2) {
8053 $chunkSize = 1;
8054 }
8055 }
8056 if ($chunkSize != 1 ||
8057 exists($ENV{LCOV_FORCE_PARALLEL})) {
8058 $workList = [];
8059 my $idx = 0;
8060 my $current = [];
8061 # maybe sort files by number of lines, then distribute larger ones
8062 # across chunks? Or sort so total number of lines is balanced
8063 foreach my $f (@$fileList) {
8064 push(@$current, $f);
8065 if (++$idx == $chunkSize) {
8066 $idx = 0;
8067 push(@$workList, $current);
8068 $current = [];
8069 }
8070 }
8071 push(@$workList, $current) if (@$current);
8072 lcovutil::info("Filter: chunkSize $chunkSize nChunks " .
8073 scalar(@$workList) . "\n");
8074 }
8075 }
8076
8077 my @state = (['saw_unsupported_end_line', 0],);
8078 # keep track of patterns application counts before we fork children
8079 my @pats = grep { @$_ }
8080 (\@lcovutil::exclude_function_patterns, \@lcovutil::omit_line_patterns);
8081 # and also filter application counts
8082 my @filters = grep { defined($_) } @lcovutil::cov_filter;
8083 my @save = (\@pats, \@filters);
8084
8085 my $processedChunks = 0;
8086 my $currentParallel = 0;
8087 my %children;
8088 my $tmp = File::Temp->newdir(
8089 "filter_datXXXX",
8090 DIR => $lcovutil::tmp_dir,
8091 CLEANUP => !defined($lcovutil::preserve_intermediates)
8092 )
8093 if (exists($ENV{LCOV_FORCE_PARALLEL}) ||
8094 $parallel > 1);
8095
8096 my $failedAttempts = 0;
8097 my %childRetryCounts;
8098 do {
8099 CHUNK: while (@$workList) {
8100 my $d = pop(@$workList);
8101 ++$processedChunks;
8102 # save current counts...
8103 $state[0]->[1] = 0;
8104 if (ref($d->[0]) eq 'TraceInfo') {
8105 # serial processing...
8106 my ($data, $modified) = _filterFile(@$d, $srcReader, \@state);
8107 $self->_updateModifiedFile($d->[1], $data, \@state)
8108 if $modified;
8109 } else {
8110
8111 my $currentSize = 0;
8112 if (0 != $lcovutil::maxMemory) {
8113 $currentSize = lcovutil::current_process_size();
8114 }
8115 while ($currentParallel >= $lcovutil::maxParallelism ||
8116 ($currentParallel > 1 &&
8117 (($currentParallel + 1) * $currentSize) >
8118 $lcovutil::maxMemory)
8119 ) {
8120 lcovutil::info(1,
8121 "memory constraint ($currentParallel + 1) * $currentSize > $lcovutil::maxMemory violated: waiting. "
8122 . (scalar(@$workList) - $processedChunks + 1)
8123 . " remaining\n")
8124 if ((($currentParallel + 1) * $currentSize) >
8125 $lcovutil::maxMemory);
8126 my $child = wait();
8127 my $childstatus = $?;
8128 unless (exists($children{$child})) {
8129 lcovutil::report_unknown_child($child);
8130 next;
8131 }
8132 eval {
8133 $self->_mergeParallelChunk($tmp, $child, \%children,
8134 $childstatus, \@save, $workList,
8135 \%childRetryCounts);
8136 };
8137 if ($@) {
8138 $childstatus = 1 << 8 unless $childstatus;
8139 lcovutil::report_parallel_error('filter',
8140 $lcovutil::ERROR_CHILD, $child, $childstatus, $@);
8141 }
8142 --$currentParallel;
8143 }
8144
8145 # parallel processing...
8146 $lcovutil::deferWarnings = 1;
8147 my $now = Time::HiRes::gettimeofday();
8148 my $pid = fork();
8149 if (!defined($pid)) {
8150 # fork failed
8151 ++$failedAttempts;
8152 lcovutil::report_fork_failure('process filter chunk',
8153 $!, $failedAttempts);
8154 --$processedChunks;
8155 push(@$workList, $d);
8156 next CHUNK;
8157 }
8158 $failedAttempts = 0;
8159 if (0 == $pid) {
8160 # I'm the child
8161 my $status =
8162 _processParallelChunk($tmp, $d, $srcReader, \@save,
8163 \@state, $now, $masterChunkID);
8164 exit($status); # normal return
8165 } else {
8166 # parent
8167 $children{$pid} = [$d, $now, $masterChunkID];
8168 lcovutil::debug(1, "fork:$pid ID $masterChunkID\n");
8169 ++$currentParallel;
8170 }
8171 ++$masterChunkID;
8172 }
8173
8174 } # while (each segment in worklist)
8175 while ($currentParallel != 0) {
8176 my $child = wait();
8177 my $childstatus = $?;
8178 unless (exists($children{$child})) {
8179 lcovutil::report_unknown_child($child);
8180 next;
8181 }
8182 --$currentParallel;
8183 eval {
8184 $self->_mergeParallelChunk($tmp, $child, \%children,
8185 $childstatus, \@save, $workList, \%childRetryCounts);
8186 };
8187 if ($@) {
8188 $childstatus = 1 << 8 unless $childstatus;
8189 lcovutil::report_parallel_error('filter',
8190 $lcovutil::ERROR_CHILD, $child, $childstatus, $@);
8191 }
8192
8193 }
8194 } while (@$workList); # outer do/while - to catch spaceouts
8195 lcovutil::info("Finished filter file processing\n");
8196 }
8197
8198 sub applyFilters
8199 {
8200 my $self = shift;
8201 my $srcReader = shift;
8202
8203 $srcReader = ReadCurrentSource->new()
8204 unless defined($srcReader);
8205
8206 my $mask = DID_FILTER;
8207 $mask |= DID_DERIVE
8208 if (defined($lcovutil::derive_function_end_line) &&
8209 $lcovutil::derive_function_end_line != 0);
8210 return
8211 if ($mask == ($self->[STATE] & $mask));
8212
8213 # have to look through each file in each testcase; they may be different
8214 # due to differences in #ifdefs when the corresponding tests were compiled.
8215 my @filter_workList;
8216
8217 my $computeEndLine =
8218 (0 == ($self->[STATE] & DID_DERIVE) &&
8219 defined($lcovutil::derive_function_end_line) &&
8220 $lcovutil::derive_function_end_line != 0 &&
8221 defined($lcovutil::func_coverage));
8222
8223 foreach my $name ($self->files()) {
8224
8225 my $traceInfo = $self->data($name);
8226 die("expected TraceInfo, got '" . ref($traceInfo) . "'")
8227 unless ('TraceInfo' eq ref($traceInfo));
8228 my $source_file = $traceInfo->filename();
8229 if (TraceFile::skipCurrentFile($source_file)) {
8230 $self->remove($source_file);
8231 next;
8232 }
8233 if (lcovutil::is_external($source_file)) {
8234 lcovutil::info("excluding 'external' file '$source_file'\n");
8235 $self->remove($source_file);
8236 next;
8237 }
8238 # derive function end line for C/C++ and java code if requested
8239 # (not trying to handle python nested functions, etc.)
8240 # However, see indent handling in the py2lcov script. Arguably, that
8241 # could/should be done here/in Perl rather than in Python.)
8242 # Jacoco pretends to report function end line - but it appears
8243 # to be the last line executed - not the actual last line of
8244 # the function - so broken/completely useless.
8245 my $actions = 0;
8246 if ($computeEndLine &&
8247 ($lcovutil::derive_function_end_line_all_files ||
8248 is_language('c|java|perl', $source_file))
8249 ) {
8250 # try to derive end lines if at least one is unknown.
8251 # can't compute for lambdas because we can't distinguish
8252 # the last line reliably.
8253 $actions = DID_DERIVE
8254 if grep({ !($_->isLambda() || defined($_->end_line())) }
8255 $traceInfo->func()->valuelist());
8256 }
8257
8258 if ((defined($lcovutil::func_coverage) &&
8259 (0 != scalar(@lcovutil::exclude_function_patterns) ||
8260 defined($lcovutil::cov_filter[$FILTER_TRIVIAL_FUNCTION]))) ||
8261 (is_language('c|perl|python|java', $source_file) &&
8262 lcovutil::is_filter_enabled())
8263 ) {
8264 # we are forking anyway - so also compute end lines there
8265 $actions |= DID_FILTER;
8266 push(@filter_workList, [$traceInfo, $name, $actions]);
8267 } else {
8268 if (0 != $actions) {
8269 # all we are doing is deriving function end lines - which doesn't
8270 # take long enough to be worth forking
8271 TraceFile::_deriveFunctionEndLines($traceInfo);
8272 }
8273 TraceFile::_checkConsistency($traceInfo);
8274 }
8275
8276 } # foreach file
8277 $self->[STATE] |= DID_DERIVE;
8278
8279 if (@filter_workList) {
8280 lcovutil::info("Apply filtering..\n");
8281 $self->_processFilterWorklist($srcReader, \@filter_workList);
8282 # keep track - so we don't do this again
8283 $self->[STATE] |= DID_FILTER;
8284 }
8285 }
8286
8287 sub is_language
8288 {
8289 my ($lang, $filename) = @_;
8290 my $idx = index($filename, '.');
8291 my $ext = $idx == -1 ? '' : substr($filename, $idx);
8292 foreach my $l (split('\|', $lang)) {
8293 die("unknown language '$l'")
8294 unless exists($lcovutil::languageExtensions{$l});
8295 my $extensions = $lcovutil::languageExtensions{$l};
8296 return 1 if ($ext =~ /\.($extensions)$/);
8297 }
8298 return 0;
8299 }
8300
8301 # Read in the contents of the .info file specified by INFO_FILENAME. Data will
8302 # be returned as a reference to a hash containing the following mappings:
8303 #
8304 # %result: for each filename found in file -> \%data
8305 #
8306 # %data: "test" -> \%testdata
8307 # "sum" -> \%sumcount
8308 # "func" -> \%funcdata
8309 # "found" -> $lines_found (number of instrumented lines found in file)
8310 # "hit" -> $lines_hit (number of executed lines in file)
8311 # "function_found" -> $fn_found (number of instrumented functions found in file)
8312 # "function_hit" -> $fn_hit (number of executed functions in file)
8313 # "branch_found" -> $br_found (number of instrumented branches found in file)
8314 # "branch_hit" -> $br_hit (number of executed branches in file)
8315 # "check" -> \%checkdata
8316 # "testfnc" -> \%testfncdata
8317 # "testbr" -> \%testbrdata
8318 # "sumbr" -> \%sumbrcount
8319 #
8320 # %testdata : name of test affecting this file -> \%testcount
8321 # %testfncdata: name of test affecting this file -> \%testfnccount
8322 # %testbrdata: name of test affecting this file -> \%testbrcount
8323 #
8324 # %testcount : line number -> execution count for a single test
8325 # %testfnccount: function name -> execution count for a single test
8326 # %testbrcount : line number -> branch coverage data for a single test
8327 # %sumcount : line number -> execution count for all tests
8328 # %sumbrcount : line number -> branch coverage data for all tests
8329 # %funcdata : FunctionMap: function name -> FunctionEntry
8330 # %checkdata : line number -> checksum of source code line
8331 # $brdata : BranchData vector of items: block, branch, taken
8332 #
8333 # Note that .info file sections referring to the same file and test name
8334 # will automatically be combined by adding all execution counts.
8335 #
8336 # Note that if INFO_FILENAME ends with ".gz", it is assumed that the file
8337 # is compressed using GZIP. If available, GUNZIP will be used to decompress
8338 # this file.
8339 #
8340 # Die on error.
8341 #
8342 sub _read_info
8343 {
8344 my ($self, $tracefile, $readSourceCallback, $verify_checksum) = @_;
8345 $verify_checksum = 0 unless defined($verify_checksum);
8346
8347 if (!defined($readSourceCallback)) {
8348 $readSourceCallback = ReadCurrentSource->new();
8349 }
8350
8351 # per file data
8352 my %perfile;
8353 my $sumcount; # line total counts in this file
8354 my $funcdata; # function total counts in this file
8355 my $sumbrcount; # branch total counts
8356 my $mcdcCount; # MD/DC total counts
8357
8358 my $checkdata; # line checksums
8359 my %perTestData;
8360 my %summaryData;
8361 # hash of per-testcase coverage data per testcase, in this file
8362 my $testdata; # hash of testname -> line coverage
8363 my $testfncdata; # hash of testname -> function coverage
8364 my $testbrdata; # hash of testname -> branch data
8365 my $testMcdc; # -> MC/DC data
8366
8367 my $testcount; # line coverage for particular testcase
8368 my $testfnccount; # func coverage " "
8369 my $testbrcount; # branch coverage " "
8370 my $testcase_mcdc; # MC/DC coverage " "
8371
8372 my $testname; # Current test name
8373 my $filename; # Current filename
8374 my $current_mcdc;
8375 my $changed_testname; # If set, warn about changed testname
8376
8377 lcovutil::info(1, "Reading data file $tracefile\n");
8378
8379 # Check if file exists and is readable
8380 stat($tracefile);
8381 if (!(-r _)) {
8382 die("cannot read file $tracefile!\n");
8383 }
8384
8385 # Check if this is really a plain file
8386 if (!(-f _)) {
8387 die("not a plain file: $tracefile!\n");
8388 }
8389
8390 # Check for .gz extension
8391 my $inFile = InOutFile->in($tracefile, $lcovutil::demangle_cpp_cmd);
8392 my $infoHdl = $inFile->hdl();
8393
8394 $testname = "";
8395 my $fileData;
8396 # HGC: somewhat of a hack.
8397 # There are duplicate lines in the geninfo output result - for example,
8398 # line '2095' may have multiple DA (line) entries, and may have multiple
8399 # 'BRDA' entries - each with a different number of branches and different
8400 # count
8401 # The hack is to put branches into a hash keyed by branch ID - and
8402 # merge elements with the same key if we run into them in the multiple
8403 # times in the same 'file' data (within an SF entry).
8404 my %nextBranchId; # line -> integer ID
8405 my ($currentBranchLine, $skipBranch);
8406 my $functionMap;
8407 my %excludedFunction;
8408 my $skipCurrentFile = 0;
8409 my %fnIdxMap;
8410 while (<$infoHdl>) {
8411 chomp($_);
8412 my $line = $_;
8413 $line =~ s/\s+$//; # whitespace
8414
8415 next if $line =~ /^#/; # skip comment
8416
8417 if ($line =~ /^[SK]F:(.*)/) {
8418 # Filename information found
8419 if ($1 =~ /^\s*$/) {
8420 lcovutil::ignorable_error($lcovutil::ERROR_FORMAT,
8421 "\"$tracefile\":$.: unexpected empty file name in record '$line'"
8422 );
8423 $skipCurrentFile = 1;
8424 next;
8425 }
8426 #if ($self->contains($filename)) {
8427 # # we expect there to be only one entry for each source file in each section
8428 # lcovutil::ignorable_warning($lcovutil::ERROR_FORMAT,
8429 # "Duplicate entries for \"$filename\""
8430 # . ($testname ? " in testcase '$testname'" : '') . '.');
8431 #}
8432 $filename = ReadCurrentSource::resolve_path($1, 1);
8433 # should this one be skipped?
8434 $skipCurrentFile = skipCurrentFile($filename);
8435 if ($skipCurrentFile) {
8436 if (!exists($lcovutil::excluded_files{$filename})) {
8437 $lcovutil::excluded_files{$filename} = 1;
8438 lcovutil::info("Excluding $filename\n");
8439 }
8440 next;
8441 }
8442
8443 # Retrieve data for new entry
8444 %nextBranchId = ();
8445 %excludedFunction = ();
8446 %fnIdxMap = ();
8447
8448 if ($verify_checksum) {
8449 # unconditionally 'close' the current file - in case we don't
8450 # open a new one. If that happened, then we would be looking
8451 # at the source for some previous file.
8452 $readSourceCallback->close();
8453 undef $currentBranchLine;
8454 if (is_language('c', $filename)) {
8455 $readSourceCallback->open($filename);
8456 }
8457 }
8458 $fileData = $self->data($filename);
8459 # record line number where file entry found - can use it in error messages
8460 $fileData->location($tracefile, $.);
8461 ($testdata, $sumcount, $funcdata,
8462 $checkdata, $testfncdata, $testbrdata,
8463 $sumbrcount, $mcdcCount, $testMcdc) = $fileData->get_info();
8464
8465 if (defined($testname)) {
8466 $testcount = $fileData->test($testname);
8467 $functionMap = $fileData->testfnc($testname);
8468 $testbrcount = $fileData->testbr($testname);
8469 $testcase_mcdc = $fileData->testcase_mcdc($testname);
8470 } else {
8471 $testcount = CountData->new($filename, 1);
8472 $testfnccount = CountData->new($filename, 0);
8473 $testbrcount = BranchData->new();
8474 $testcase_mcdc = MCDC_Data->new();
8475 $functionMap = FunctionMap->new($filename);
8476 }
8477 next;
8478 }
8479 next if $skipCurrentFile;
8480
8481 # Switch statement
8482 # Please note: if you add or change something here (lcov info file format) -
8483 # then please make corresponding changes to the 'write_info' method, below
8484 # and update the format description found in .../man/geninfo.1.
8485 foreach ($line) {
8486 next if $line =~ /^#/; # skip comment
8487
8488 /^VER:(.+)$/ && do {
8489 # revision control version string found
8490 # we might try to set the version multiple times if the
8491 # file appears multiple times in the .info file
8492 if (defined($fileData->version()) &&
8493 $fileData->version() eq $1) {
8494 # this is OK -
8495 # we might try to set the version multiple times if the
8496 # file appears multiple times in the .info file.
8497 # This can happen, with some translators
8498 last;
8499 }
8500 $fileData->version($1);
8501 last;
8502 };
8503
8504 /^TN:([^,]*)(,diff)?/ && do {
8505 # Test name information found
8506 $testname = defined($1) ? $1 : "";
8507 my $orig = $testname;
8508 if ($testname =~ s/\W/_/g) {
8509 $changed_testname = $orig;
8510 }
8511 $testname .= $2 if (defined($2));
8512 if (defined($ignore_testcase_name) &&
8513 $ignore_testcase_name) {
8514 lcovutil::debug(1,
8515 "using default testcase rather than $testname at $tracefile:$.\n"
8516 );
8517
8518 $testname = '';
8519 }
8520 last;
8521 };
8522
8523 /^DA:(\d+),([^,]+)(,([^,\s]+))?/ && do {
8524 my ($line, $count, $checksum) = ($1, $2, $4);
8525 if ($line <= 0) {
8526 lcovutil::ignorable_error($lcovutil::ERROR_FORMAT,
8527 "\"$tracefile\":$.: unexpected line number '$line' in .info file record '$_'"
8528 );
8529 # just keep invalid number - if error ignored
8530 # last;
8531 }
8532 if ($readSourceCallback->notEmpty()) {
8533 # does the source checksum match the recorded checksum?
8534 if ($verify_checksum) {
8535 if (defined($checksum)) {
8536 my $content = $readSourceCallback->getLine($line);
8537 my $chk =
8538 defined($content) ?
8539 Digest::MD5::md5_base64($content) :
8540 0;
8541 if ($chk ne $checksum) {
8542 lcovutil::ignorable_error(
8543 $lcovutil::ERROR_VERSION,
8544 "checksum mismatch at between source $filename:$line and $tracefile: $checksum -> $chk"
8545 );
8546 }
8547 } else {
8548 # no checksum there
8549 lcovutil::ignorable_error($lcovutil::ERROR_VERSION,
8550 "no checksum for $filename:$line in $tracefile"
8551 );
8552 }
8553 }
8554 }
8555
8556 # hold line, count and testname for postprocessing?
8557 my $linesum = $fileData->sum();
8558
8559 # Execution count found, add to structure
8560 # Add summary counts
8561 $linesum->append($line, $count);
8562
8563 # Add test-specific counts
8564 if (defined($testname)) {
8565 $fileData->test($testname)->append($line, $count, 1);
8566 }
8567
8568 # Store line checksum if available
8569 if (defined($checksum) &&
8570 $lcovutil::verify_checksum) {
8571 # Does it match a previous definition
8572 if ($fileData->check()->mapped($line) &&
8573 ($fileData->check()->value($line) ne $checksum)) {
8574 lcovutil::ignorable_error($lcovutil::ERROR_VERSION,
8575 "checksum mismatch at $filename:$line in $tracefile"
8576 );
8577 }
8578 $fileData->check()->replace($line, $checksum);
8579 }
8580 last;
8581 };
8582
8583 /^FN:(\d+),((\d+),)?(.+)$/ && do {
8584 last if (!$lcovutil::func_coverage);
8585 # Function data found, add to structure
8586 my $lineNo = $1;
8587 my $fnName = $4;
8588 my $end_line = $3;
8589 if (!grep({ $fnName =~ $_ }
8590 @lcovutil::suppress_function_patterns) &&
8591 ($lineNo <= 0 ||
8592 (defined($end_line) && $end_line <= 0))
8593 ) {
8594 lcovutil::ignorable_error($lcovutil::ERROR_FORMAT,
8595 "\"$tracefile\":$.: unexpected function line '$lineNo' in .info file record '$_'"
8596 ) if $lineNo <= 0;
8597 lcovutil::ignorable_error($lcovutil::ERROR_FORMAT,
8598 "\"$tracefile\":$.: unexpected function end line '$end_line' in .info file record '$_'"
8599 ) if defined($end_line) && $end_line <= 0;
8600 }
8601 # the function may already be defined by another testcase
8602 # (for the same file)
8603 $functionMap->define_function($fnName, $lineNo,
8604 $end_line ? $end_line : undef,
8605 , "\"$tracefile\":$.");
8606 last;
8607 };
8608
8609 # Hit count may be float if Perl decided to convert it
8610 /^FNDA:([^,]+),(.+)$/ && do {
8611 last if (!$lcovutil::func_coverage);
8612 my $fnName = $2;
8613 my $hit = $1;
8614 # error checking is in the addAlias method
8615 $functionMap->add_count($fnName, $hit);
8616 last;
8617 };
8618
8619 # new format...
8620 /^FNL:(\d+),(\d+)(,(\d+))?$/ && do {
8621 last if (!$lcovutil::func_coverage);
8622 my $fnIndex = $1;
8623 my $lineNo = $2;
8624 my $end_line = $4;
8625 die("unexpected duplicate index $fnIndex")
8626 if exists($fnIdxMap{$fnIndex});
8627 $fnIdxMap{$fnIndex} = [$lineNo, $end_line];
8628 last;
8629 };
8630
8631 /^FNA:(\d+),([^,]+),(.+)$/ && do {
8632 last if (!$lcovutil::func_coverage);
8633 my $fnIndex = $1;
8634 my $hit = $2;
8635 my $alias = $3;
8636 die("unknown index $fnIndex")
8637 unless exists($fnIdxMap{$fnIndex});
8638 my ($lineNo, $end_line) = @{$fnIdxMap{$fnIndex}};
8639 my $fn =
8640 $functionMap->define_function($alias, $lineNo, $end_line,
8641 "\"$tracefile\":$.");
8642 $fn->addAlias($alias, $hit);
8643 last;
8644 };
8645
8646 /^BRDA:(\d+),(e?)(\d+),(.+)$/ && do {
8647 last if (!$lcovutil::br_coverage);
8648
8649 # Branch coverage data found
8650 # line data is "lineNo,blockId,(branchIdx|branchExpr),taken
8651 # - so grab the last two elements, split on the last comma,
8652 # and check whether we found an integer or an expression
8653 my ($line, $is_exception, $block, $d) =
8654 ($1, defined($2) && 'e' eq $2, $3, $4);
8655
8656 if ($line <= 0) {
8657 # Python coverage.py emits line number 0 (zero) for branches
8658 # - which is bogus, as there is no line number zero,
8659 # and the corresponding branch expression is not there in
8660 # any case.
8661 # Meantime: this confuses the lcov DB - so we simply skip
8662 # such data.
8663 # Note that we only need to check while reading .info files.
8664 # - if we wrote one from geninfo, then we will not have
8665 # produced bogus data - so no need to check.
8666 # - only some (broken) external tool could have the issue
8667 lcovutil::ignorable_error($lcovutil::ERROR_FORMAT,
8668 "\"$tracefile\":$.: unexpected line number '$line' in .info file record '$_'"
8669 );
8670 # just keep invalid line number if error ignored
8671 # last;
8672 }
8673
8674 last if $is_exception && $lcovutil::exclude_exception_branch;
8675 my $comma = rindex($d, ',');
8676 my $taken = substr($d, $comma + 1);
8677 my $expr = substr($d, 0, $comma);
8678
8679 # Notes:
8680 # - there may be other branches on the same line (..the next
8681 # contiguous BRDA entry).
8682 # There should always be at least 2.
8683 # - $block is generally '0' - but is used to distinguish cases
8684 # where different branch constructs appear on the same line -
8685 # e.g., due to template instantiation or funky macro usage -
8686 # see .../tests/lcov/branch
8687 # - $taken can be a number or '-'
8688 # '-' means that the first clause of the branch short-circuited -
8689 # so this branch was not evaluated at all.
8690 # In any branch pair, either all should have a 'taken' of '-'
8691 # or at least one should have a non-zero taken count and
8692 # the others should be zero.
8693 # - in order to support Verilog expressions, we treat the
8694 # 'branchId' as an arbitrary string (e.g., ModelSim will
8695 # generate an CNF or truth-table like entry corresponding
8696 # to the branch.
8697
8698 my $key = "$line,$block";
8699 my $branch =
8700 exists($nextBranchId{$key}) ? $nextBranchId{$key} :
8701 0;
8702 $nextBranchId{$key} = $branch + 1;
8703
8704 my $br =
8705 BranchBlock->new($branch, $taken, $expr, $is_exception);
8706 $fileData->sumbr()->append($line, $block, $br, $filename);
8707
8708 # Add test-specific counts
8709 if (defined($testname)) {
8710 $fileData->testbr($testname)
8711 ->append($line, $block, $br, $filename);
8712 }
8713 last;
8714 };
8715
8716 /^MCDC:(\d+),(\d+),([tf]),(\d+),(\d+),(.+)$/ && do {
8717 # line number, groupSize, sense, count, index, expression
8718 # 'sense' is t/f: was this expression sensitized
8719 last unless $lcovutil::mcdc_coverage;
8720
8721 my ($line, $groupSize, $sense, $count, $idx, $expr) =
8722 ($1, $2, $3, $4, $5, $6);
8723 if ($line <= 0) {
8724 lcovutil::ignorable_error($lcovutil::ERROR_FORMAT,
8725 "\"$tracefile\":$.: unexpected line number '$line' in condition data record '$_'."
8726 );
8727 # keep invalid line number
8728 #last;
8729 }
8730
8731 if (!defined($current_mcdc) ||
8732 $current_mcdc->line() != $line) {
8733 if ($current_mcdc) {
8734 $fileData->mcdc()->close_mcdcBlock($current_mcdc);
8735
8736 $fileData->testcase_mcdc($testname)
8737 ->append_mcdc(Storable::dclone($current_mcdc))
8738 if (defined($testname));
8739 }
8740 $current_mcdc =
8741 $fileData->mcdc()->new_mcdc($fileData, $line);
8742 }
8743 $current_mcdc->insertExpr($filename, $groupSize, $sense eq 't',
8744 $count, $idx, $expr);
8745 last;
8746 };
8747
8748 /^end_of_record/ && do {
8749 # Found end of section marker
8750 if ($filename) {
8751 if (!defined($fileData->version()) &&
8752 $lcovutil::compute_file_version &&
8753 @lcovutil::extractVersionScript) {
8754 my $version = lcovutil::extractFileVersion($filename);
8755 $fileData->version($version)
8756 if (defined($version) && $version ne "");
8757 }
8758 if ($lcovutil::func_coverage) {
8759
8760 if ($funcdata != $functionMap) {
8761 $funcdata->union($functionMap);
8762 }
8763 }
8764 if ($current_mcdc) {
8765 # close the current expression in case the next file
8766 # has an expression on the same line
8767 $fileData->mcdc()->close_mcdcBlock($current_mcdc);
8768 $fileData->testcase_mcdc($testname)
8769 ->append_mcdc(Storable::dclone($current_mcdc))
8770 if (defined($testname));
8771 $current_mcdc = undef;
8772 }
8773
8774 # some paranoic checks
8775 $self->data($filename)->check_data();
8776 last;
8777 }
8778 };
8779 /^(FN|BR|L|MC)[HF]/ && do {
8780 last; # ignore count records
8781 };
8782 /^\s*$/ && do {
8783 last; # ignore empty line
8784 };
8785
8786 lcovutil::ignorable_error($lcovutil::ERROR_FORMAT,
8787 "\"$tracefile\":$.: unexpected .info file record '$_'");
8788 # default
8789 last;
8790 }
8791 }
8792
8793 # Calculate lines_found and lines_hit for each file
8794 foreach $filename ($self->files()) {
8795 #$data = $result{$filename};
8796
8797 ($testdata, $sumcount, undef, undef, $testfncdata, $testbrdata,
8798 $sumbrcount) = $self->data($filename)->get_info();
8799
8800 # Filter out empty files
8801 if ($self->data($filename)->sum()->entries() == 0) {
8802 delete($self->[FILES]->{$filename});
8803 next;
8804 }
8805 my $filedata = $self->data($filename);
8806 # Filter out empty test cases
8807 foreach $testname ($filedata->test()->keylist()) {
8808 if (!$filedata->test()->mapped($testname) ||
8809 scalar($filedata->test($testname)->keylist()) == 0) {
8810 $filedata->test()->remove($testname);
8811 $filedata->testfnc()->remove($testname);
8812 $filedata->testbr()->remove($testname);
8813 $filedata->testcase_mcdc()->remove($testname);
8814 }
8815 }
8816 }
8817
8818 if (scalar($self->files()) == 0) {
8819 lcovutil::ignorable_error($lcovutil::ERROR_EMPTY,
8820 "no valid records found in tracefile $tracefile");
8821 }
8822 if (defined($changed_testname)) {
8823 lcovutil::ignorable_warning($lcovutil::ERROR_FORMAT,
8824 "invalid characters removed from testname in " .
8825 "tracefile $tracefile: '$changed_testname'->'$testname'\n"
8826 );
8827 }
8828 }
8829
8830 # write data to filename (stdout if '-')
8831 # returns nothing
8832 sub write_info_file($$$)
8833 {
8834 my ($self, $filename, $do_checksum) = @_;
8835
8836 if ($self->empty()) {
8837 lcovutil::ignorable_error($lcovutil::ERROR_EMPTY,
8838 "coverage DB is empty");
8839 }
8840 my $file = InOutFile->out($filename);
8841 my $hdl = $file->hdl();
8842 $self->write_info($hdl, $do_checksum);
8843 }
8844
8845 #
8846 # write data in .info format
8847 # returns array of (lines found, lines hit, functions found, functions hit,
8848 # branches found, branches_hit)
8849
8850 sub write_info($$$)
8851 {
8852 my $self = $_[0];
8853 local *INFO_HANDLE = $_[1];
8854 my $verify_checksum = defined($_[2]) ? $_[2] : 0;
8855 my $br_found;
8856 my $br_hit;
8857
8858 my $srcReader = ReadCurrentSource->new()
8859 if ($verify_checksum);
8860 foreach my $comment ($self->comments()) {
8861 print(INFO_HANDLE '#', $comment, "\n");
8862 }
8863 foreach my $filename (sort($self->files())) {
8864 my $entry = $self->data($filename);
8865 my $source_file = $entry->filename();
8866 die("expected to have have filtered $source_file out")
8867 if lcovutil::is_external($source_file);
8868 die("expected TraceInfo, got '" . ref($entry) . "'")
8869 unless ('TraceInfo' eq ref($entry));
8870
8871 my ($testdata, $sumcount, $funcdata,
8872 $checkdata, $testfncdata, $testbrdata,
8873 $sumbrcount, $sum_mcdc, $testmcdc) = $entry->get_info();
8874 # munge the source file name, if requested
8875 $source_file = ReadCurrentSource::resolve_path($source_file, 1);
8876
8877 # Please note: if you add or change something here (lcov info file format) -
8878 # then please make corresponding changes to the '_read_info' method, above
8879 # and update the format description found in .../man/geninfo.1.
8880 foreach my $testname (sort($testdata->keylist())) {
8881 my $testcount = $testdata->value($testname);
8882 my $testfnccount = $testfncdata->value($testname);
8883 my $testbrcount = $testbrdata->value($testname);
8884 my $mcdc = $testmcdc->value($testname);
8885
8886 print(INFO_HANDLE "TN:$testname\n");
8887 print(INFO_HANDLE "SF:$source_file\n");
8888 print(INFO_HANDLE "VER:" . $entry->version() . "\n")
8889 if defined($entry->version());
8890 if (defined($srcReader)) {
8891 lcovutil::info(1, "reading $source_file for lcov checksum\n");
8892 $srcReader->open($source_file);
8893 }
8894
8895 my $functionMap = $testfncdata->{$testname};
8896 if ($lcovutil::func_coverage &&
8897 $functionMap) {
8898 # Write function related data - sort by line number then
8899 # by name (compiler-generated functions may have same line)
8900 # sort enables diff of output data files, for testing
8901 my @functionOrder =
8902 sort({ $functionMap->findKey($a)->line()
8903 cmp $functionMap->findKey($b)->line() or
8904 $a cmp $b } $functionMap->keylist());
8905
8906 my $fnIndex = -1;
8907 my $f_found = 0;
8908 my $f_hit = 0;
8909 foreach my $key (@functionOrder) {
8910 my $data = $functionMap->findKey($key);
8911 my $aliases = $data->aliases();
8912 my $line = $data->line();
8913
8914 if ($line <= 0) {
8915 my $alias = (sort keys %$aliases)[0];
8916 lcovutil::ignorable_error($lcovutil::ERROR_FORMAT,
8917 "\"$source_file\": unexpected line number '$line' for function $alias"
8918 );
8919 # if message is ignored, leave bogus entry in the data
8920 }
8921 ++$fnIndex;
8922 my $endLine =
8923 defined($data->end_line()) ?
8924 ',' . $data->end_line() :
8925 '';
8926 # print function leader
8927 print(INFO_HANDLE "FNL:$fnIndex,$line$endLine\n");
8928 ++$f_found;
8929 my $counted = 0;
8930 foreach my $alias (sort keys %$aliases) {
8931 my $hit = $aliases->{$alias};
8932 ++$f_hit if $hit > 0 && !$counted;
8933 $counted ||= $hit > 0;
8934 # print the alias
8935 print(INFO_HANDLE "FNA:$fnIndex,$hit,$alias\n");
8936 }
8937 }
8938 print(INFO_HANDLE "FNF:$f_found\n");
8939 print(INFO_HANDLE "FNH:$f_hit\n");
8940 }
8941 # $testbrcount is undef if there are no branches in the scope
8942 if ($lcovutil::br_coverage &&
8943 defined($testbrcount)) {
8944 # Write branch related data
8945 my $br_found = 0;
8946 my $br_hit = 0;
8947
8948 foreach my $line (sort({ $a <=> $b } $testbrcount->keylist())) {
8949
8950 if ($line <= 0) {
8951 lcovutil::ignorable_error($lcovutil::ERROR_FORMAT,
8952 "\"$source_file\": unexpected line number '$line' in branch data record."
8953 );
8954 # keep bogus data if error ignored
8955 # last;
8956 }
8957 my $brdata = $testbrcount->value($line);
8958 # want the block_id to be treated as 32-bit unsigned integer
8959 # (need masking to match regression tests)
8960 my $mask = (1 << 32) - 1;
8961 foreach my $block_id (sort(($brdata->blocks()))) {
8962 my $blockData = $brdata->getBlock($block_id);
8963 $block_id &= $mask;
8964 foreach my $br (@$blockData) {
8965 my $taken = $br->data();
8966 my $branch_id = $br->id();
8967 my $branch_expr = $br->expr();
8968 # mostly for Verilog: if there is a branch expression: use it.
8969 printf(INFO_HANDLE "BRDA:%u,%s%u,%s,%s\n",
8970 $line,
8971 $br->is_exception() ? 'e' : '',
8972 $block_id,
8973 defined($branch_expr) ? $branch_expr :
8974 $branch_id,
8975 $taken);
8976 $br_found++;
8977 $br_hit++
8978 if ($taken ne '-' && $taken > 0);
8979 }
8980 }
8981 }
8982 if ($br_found > 0) {
8983 print(INFO_HANDLE "BRF:$br_found\n");
8984 print(INFO_HANDLE "BRH:$br_hit\n");
8985 }
8986 }
8987 if ($mcdc &&
8988 $lcovutil::mcdc_coverage) {
8989
8990 my $mcdc_found = 0;
8991 my $mcdc_hit = 0;
8992 foreach my $line (sort({ $a <=> $b } $mcdc->keylist())) {
8993 if ($line <= 0) {
8994 lcovutil::ignorable_error($lcovutil::ERROR_FORMAT,
8995 "\"$source_file\": unexpected line number '$line' in MC/DC data record."
8996 );
8997 }
8998 my $m = $mcdc->value($line);
8999 my $groups = $m->groups();
9000 foreach my $groupSize (sort keys %$groups) {
9001 my $exprs = $groups->{$groupSize};
9002 my $index = -1;
9003 foreach my $e (@$exprs) {
9004 $mcdc_found += 2;
9005 ++$index;
9006 foreach my $sense ('t', 'f') {
9007 my $count = $e->count($sense eq 't');
9008 ++$mcdc_hit if 0 != $count;
9009 print(INFO_HANDLE
9010 "MCDC:$line,$groupSize,$sense,$count,$index,"
9011 . $e->expression(),
9012 "\n");
9013 }
9014 }
9015 }
9016 }
9017 if ($mcdc_found != 0) {
9018 print(INFO_HANDLE "MCF:$mcdc_found\n");
9019 print(INFO_HANDLE "MCH:$mcdc_hit\n");
9020 }
9021 }
9022 # Write line related data
9023 my $found = 0;
9024 my $hit = 0;
9025 foreach my $line (sort({ $a <=> $b } $testcount->keylist())) {
9026 if ($line <= 0) {
9027 lcovutil::ignorable_error($lcovutil::ERROR_FORMAT,
9028 "\"$source_file\": unexpected line number '$line' in 'line' data record."
9029 );
9030 }
9031 my $l_hit = $testcount->value($line);
9032 my $chk = '';
9033 if ($verify_checksum) {
9034 if (exists($checkdata->{$line})) {
9035 $chk = $checkdata->{$line};
9036 } elsif (defined($srcReader) &&
9037 $srcReader->notEmpty()) {
9038 my $content = $srcReader->getLine($line);
9039 $chk =
9040 defined($content) ?
9041 Digest::MD5::md5_base64($content) :
9042 0;
9043 }
9044 $chk = ',' . $chk if ($chk);
9045 }
9046 print(INFO_HANDLE "DA:$line,$l_hit$chk\n");
9047 $found++;
9048 $hit++
9049 if ($l_hit > 0);
9050 }
9051 print(INFO_HANDLE "LF:$found\n");
9052 print(INFO_HANDLE "LH:$hit\n");
9053 print(INFO_HANDLE "end_of_record\n");
9054 }
9055 }
9056 }
9057
9058 package AggregateTraces;
9059 # parse sna merge TraceFiles - possibly in parallel
9060 # - common utility, used by lcov 'add_trace' and genhtml multi-file read
9061
9062 # If set, create map of unique function to list of testcase/info
9063 # files which hit that function at least once
9064 our $function_mapping;
9065 # need a static external segment index lest the exe aggregate multiple groups of data
9066 our $segmentIdx = 0;
9067
9068 sub find_from_glob
9069 {
9070 my @merge;
9071 die("no files specified") unless (@_);
9072 foreach my $pattern (@_) {
9073
9074 if (-f $pattern) {
9075 # this is a glob match...
9076 push(@merge, $pattern);
9077 next;
9078 }
9079 $pattern =~ s/([^\\]) /$1\\ /g # explicitly escape spaces
9080 unless $^O =~ /Win/;
9081
9082 my @files = glob($pattern); # perl returns files in ASCII sorted order
9083
9084 lcovutil::ignorable_error($lcovutil::ERROR_EMPTY,
9085 "no files matching pattern $pattern")
9086 unless scalar(@files);
9087 for (my $i = 0; $i <= $#files; ++$i) {
9088 my $f = $files[$i];
9089 if (-d $f) {
9090 my $cmd =
9091 "find '$f' -name '$lcovutil::info_file_pattern' -type f";
9092 my ($stdout, $stderr, $code) = Capture::Tiny::capture {
9093 system($cmd);
9094 };
9095 # can fail due to unreadable entry - but might still
9096 # have returned data to process
9097 lcovutil::ignorable_error($lcovutil::ERROR_UTILITY,
9098 "error in \"$cmd\": $stderr")
9099 if $code;
9100 my @found = split(' ', $stdout);
9101 lcovutil::ignorable_error($lcovutil::ERROR_EMPTY,
9102 "no files matching '$lcovutil::info_file_pattern' found in $f"
9103 ) unless (@found);
9104 push(@files, @found);
9105 next;
9106 }
9107
9108 unless (-r $f || -f $f) {
9109 lcovutil::ignorable_error($lcovutil::ERROR_MISSING,
9110 "'$f' found from pattern '$pattern' is not a readable file"
9111 );
9112 next;
9113 }
9114 push(@merge, $f);
9115 }
9116 }
9117 lcovutil::ignorable_error($lcovutil::ERROR_EMPTY,
9118 "no matching file found in '['" . join(', ', @_) . "]'")
9119 unless (@merge);
9120
9121 return @merge;
9122 }
9123
9124 sub _process_segment($$$)
9125 {
9126 my ($total_trace, $readSourceFile, $segment) = @_;
9127
9128 my @interesting;
9129 my $total = scalar(@$segment);
9130 foreach my $tracefile (@$segment) {
9131 my $now = Time::HiRes::gettimeofday();
9132 --$total;
9133 lcovutil::info("Merging $tracefile..$total remaining"
9134 .
9135 ($lcovutil::debug ?
9136 (' mem:' . lcovutil::current_process_size()) :
9137 '') .
9138 "\n"
9139 ) if (1 != scalar(@$segment)); # ...in segment $segId
9140 my $context = MessageContext->new("merging $tracefile");
9141 if (!-f $tracefile ||
9142 -z $tracefile) {
9143 lcovutil::ignorable_error($lcovutil::ERROR_EMPTY,
9144 "trace file '$tracefile' "
9145 .
9146 (-z $tracefile ? 'is empty' :
9147 'does not exist'));
9148 next;
9149 }
9150 my $current;
9151 eval {
9152 $current = TraceFile->load($tracefile, $readSourceFile,
9153 $lcovutil::verify_checksum, 1);
9154 lcovutil::debug("after load $tracefile: memory: " .
9155 lcovutil::current_process_size() . "\n")
9156 if $lcovutil::debug; # predicate to avoid function call...
9157 };
9158 my $then = Time::HiRes::gettimeofday();
9159 $lcovutil::profileData{parse}{$tracefile} = $then - $now;
9160 if ($@) {
9161 lcovutil::ignorable_error($lcovutil::ERROR_CORRUPT,
9162 "unable to read trace file '$tracefile': $@");
9163 next;
9164 }
9165 if ($function_mapping) {
9166 foreach my $srcFileName ($current->files()) {
9167 my $traceInfo = $current->data($srcFileName);
9168 my $funcData = $traceInfo->func();
9169 foreach my $funcKey ($funcData->keylist()) {
9170 my $funcEntry = $funcData->findKey($funcKey);
9171 if (0 != $funcEntry->hit()) {
9172 # function is hit in this file
9173 my $key = $funcEntry->file() . ":$funcKey";
9174 $function_mapping->{$key} = [$funcEntry->name(), []]
9175 unless exists($function_mapping->{$key});
9176 die("mismatched function name for " .
9177 $funcEntry->name() .
9178 " at $funcKey in $tracefile")
9179 unless $funcEntry->name() eq
9180 $function_mapping->{$key}->[0];
9181 push(@{$function_mapping->{$key}->[1]}, $tracefile);
9182 }
9183 }
9184 }
9185 } else {
9186 if ($total_trace->merge_tracefile($current, TraceInfo::UNION)) {
9187 push(@interesting, $tracefile);
9188 }
9189 }
9190 my $end = Time::HiRes::gettimeofday();
9191 $lcovutil::profileData{append}{$tracefile} = $end - $then;
9192 }
9193 return @interesting;
9194 }
9195
9196 sub merge
9197 {
9198 my $readSourceFile;
9199 my $t = ref($_[0]);
9200 if (!defined($_[0]) || '' eq $t) {
9201 # backward compatibility - arg is undefined or is a filename
9202 $readSourceFile = ReadCurrentSource->new();
9203 shift unless defined($_[0]);
9204 } else {
9205 $readSourceFile = shift;
9206 die("unexpected arg $t")
9207 unless grep(/^$t$/, ('ReadCurrentSource', 'ReadBaselineSource'));
9208 }
9209 my $nTests = scalar(@_);
9210 if (1 < $nTests) {
9211 lcovutil::info("Combining tracefiles.\n");
9212 } else {
9213 lcovutil::info("Reading tracefile $_[0].\n");
9214 }
9215
9216 $lcovutil::profileData{parse} = {}
9217 unless exists($lcovutil::profileData{parse});
9218 $lcovutil::profileData{append} = {}
9219 unless exists($lcovutil::profileData{append});
9220
9221 my @effective;
9222 # source-based filters are somewhat expensive - so we turn them
9223 # off for file read and only re-enable when we write the data back out
9224 my $save_filters = lcovutil::disable_cov_filters();
9225
9226 my $total_trace = TraceFile->new();
9227 if (!(defined($lcovutil::maxParallelism) && defined($lcovutil::maxMemory)
9228 )) {
9229 lcovutil::init_parallel_params();
9230 }
9231 if (0 != $lcovutil::maxMemory &&
9232 1 != $lcovutil::maxParallelism) {
9233 # estimate the number of processes we think we can run..
9234 my $currentSize = lcovutil::current_process_size();
9235 # guess that the data size is no smaller than one of the files we will be reading
9236 # which one is largest?
9237 my $fileSize = 0;
9238 foreach my $n (@_) {
9239 my $s = (stat($n))[7];
9240 $fileSize = $s if $s > $fileSize;
9241 }
9242 my $size = $currentSize + $fileSize;
9243 my $num = int($lcovutil::maxMemory / $size);
9244 lcovutil::debug(
9245 "Sizes: self:$currentSize file:$fileSize total:$size num:$num paralled:$lcovutil::maxParallelism\n"
9246 );
9247 if ($num < $lcovutil::maxParallelism) {
9248 $num = $num > 1 ? $num : 1;
9249 lcovutil::info(
9250 "Throttling to '--parallel $num' due to memory constraint\n");
9251 $lcovutil::maxParallelism = $num;
9252 }
9253 }
9254 # use a particular file sort order - to somewhat minimize order effects
9255 my $filelist = \@_;
9256 my @sorted_filelist;
9257 if ($lcovutil::sort_inputs) {
9258 @sorted_filelist = sort({ $a cmp $b } @_);
9259 $filelist = \@sorted_filelist;
9260 }
9261
9262 if (1 != $lcovutil::maxParallelism &&
9263 (exists($ENV{LCOV_FORCE_PARALLEL}) ||
9264 1 < $nTests)
9265 ) {
9266 # parallel implementation is to segment the file list into N
9267 # segments, then parse-and-merge scalar(@merge)/N files in each slave,
9268 # then merge the slave result.
9269 # The reasoning is that one of our examples appears to take 1.3s to
9270 # load the trace file, and 0.8s to merge it into the master list.
9271 # We thus want to parallelize both the load and the merge, as much as
9272 # possible.
9273 # Note that we try to keep the files in the order they were specified
9274 # in the segments (i.e., so adjacent files go in order, into the same
9275 # segment). This plays more nicely with the "--prune-tests" option
9276 # because we expect that files with similar names (e.g., as returned
9277 # by 'glob' have similar coverage profiles and are thus not likely to
9278 # all be 'effective'. If we had put them into different segments,
9279 # then each segment might think that their variant is 'effective' -
9280 # whereas we will notice that only one is effective if they are all
9281 # in the same segment.
9282
9283 my @segments;
9284 my $testsPerSegment =
9285 ($nTests > $lcovutil::maxParallelism) ?
9286 int(($nTests + $lcovutil::maxParallelism - 1) /
9287 $lcovutil::maxParallelism) :
9288 1;
9289 my $idx = 0;
9290 foreach my $tracefile (@$filelist) {
9291 my $seg = $idx / $testsPerSegment;
9292 $seg -= 1 if $seg == $lcovutil::maxParallelism;
9293 push(@segments, [])
9294 if ($seg >= scalar(@segments));
9295 push(@{$segments[$seg]}, $tracefile);
9296 ++$idx;
9297 }
9298 lcovutil::info("Using " .
9299 scalar(@segments) .
9300 ' segment' . (scalar(@segments) > 1 ? 's' : '') .
9301 " of $testsPerSegment test" .
9302 ($testsPerSegment > 1 ? 's' : '') . "\n");
9303 $lcovutil::profileData{config} = {}
9304 unless exists($lcovutil::profileData{config});
9305 $lcovutil::profileData{config}{segments} = scalar(@segments);
9306
9307 # kind of a hack...write to the named directory that the user gave
9308 # us rather than to a funny generated name
9309 my $tempDir = defined($lcovutil::tempdirname) ? $lcovutil::tempdirname :
9310 lcovutil::create_temp_dir();
9311 my %children;
9312 my @pending;
9313 my $patterns;
9314 my $failedAttempts = 0;
9315 my %childRetryCounts;
9316 do {
9317 while (my $segment = pop(@segments)) {
9318 $lcovutil::deferWarnings = 1;
9319 my $now = Time::HiRes::gettimeofday();
9320 my $pid = fork();
9321 if (!defined($pid)) {
9322 ++$failedAttempts;
9323 lcovutil::report_fork_failure('process segment',
9324 $!, $failedAttempts);
9325 push(@segments, $segment);
9326 next;
9327 }
9328 $failedAttempts = 0;
9329
9330 if (0 == $pid) {
9331 # I'm the child
9332 my $stdout_file =
9333 File::Spec->catfile($tempDir, "lcov_$$.log");
9334 my $stderr_file =
9335 File::Spec->catfile($tempDir, "lcov_$$.err");
9336
9337 my $currentState = lcovutil::initial_state();
9338 my $status = 0;
9339 my @interesting;
9340 my ($stdout, $stderr, $code) = Capture::Tiny::capture {
9341 eval {
9342 @interesting =
9343 _process_segment($total_trace,
9344 $readSourceFile, $segment);
9345 };
9346 if ($@) {
9347 print(STDERR $@);
9348 $status = 1;
9349 }
9350
9351 my $then = Time::HiRes::gettimeofday();
9352 $lcovutil::profileData{$segmentIdx}{total} =
9353 $then - $now;
9354 };
9355 # print stdout and stderr ...
9356 foreach
9357 my $d ([$stdout_file, $stdout], [$stderr_file, $stderr])
9358 {
9359 next
9360 unless ($d->[1])
9361 ; # only print if there is something to print
9362 my $f = InOutFile->out($d->[0]);
9363 my $h = $f->hdl();
9364 print($h $d->[1]);
9365 }
9366 my $file = File::Spec->catfile($tempDir, "dumper_$$");
9367 my $data;
9368 eval {
9369 $data =
9370 Storable::store(
9371 [$total_trace,
9372 \@interesting,
9373 $function_mapping,
9374 lcovutil::compute_update($currentState)
9375 ],
9376 $file);
9377 };
9378 if ($@ || !defined($data)) {
9379 lcovutil::ignorable_error($lcovutil::ERROR_PARALLEL,
9380 "Child $$ serialize failed" . ($@ ? ": $@" : ''));
9381 }
9382 exit($status);
9383 } else {
9384 $children{$pid} = [$now, $segmentIdx, $segment];
9385 push(@pending, $segment);
9386 }
9387 $segmentIdx++;
9388 }
9389 # now wait for all the children to finish...
9390 foreach (@pending) {
9391 my $child = wait();
9392 my $now = Time::HiRes::gettimeofday();
9393 my $childstatus = $? >> 8;
9394 unless (exists($children{$child})) {
9395 lcovutil::report_unknown_child($child);
9396 next;
9397 }
9398 my ($start, $idx, $segment) = @{$children{$child}};
9399 lcovutil::info(
9400 1,
9401 "Merging segment $idx, status $childstatus"
9402 .
9403 (
9404 $lcovutil::debug ?
9405 (' mem:' . lcovutil::current_process_size()) :
9406 '') .
9407 "\n");
9408 my $dumpfile = File::Spec->catfile($tempDir, "dumper_$child");
9409 my $childLog = File::Spec->catfile($tempDir, "lcov_$child.log");
9410 my $childErr = File::Spec->catfile($tempDir, "lcov_$child.err");
9411
9412 foreach my $f ($childLog, $childErr) {
9413 if (!-f $f) {
9414 $f = ''; # there was no output
9415 next;
9416 }
9417 if (open(RESTORE, "<", $f)) {
9418 # slurp into a string and eval..
9419 my $str =
9420 do { local $/; <RESTORE> }; # slurp whole thing
9421 close(RESTORE) or die("unable to close $f: $!\n");
9422 unlink $f
9423 unless ($str && $lcovutil::preserve_intermediates);
9424 $f = $str;
9425 } else {
9426 $f = "unable to open $f: $!";
9427 if (0 == $childstatus) {
9428 lcovutil::report_parallel_error('aggregate',
9429 $ERROR_PARALLEL, $child, 0, $f,
9430 keys(%children));
9431 }
9432 }
9433 }
9434 my $signal = $childstatus & 0xFF;
9435
9436 print(STDOUT $childLog)
9437 if ((0 != $childstatus &&
9438 $signal != POSIX::SIGKILL &&
9439 $lcovutil::max_fork_fails != 0) ||
9440 $lcovutil::verbose);
9441 print(STDERR $childErr);
9442
9443 # undump the data
9444 my $data = Storable::retrieve($dumpfile)
9445 if (-f $dumpfile && 0 == $childstatus);
9446 if (defined($data)) {
9447 eval {
9448 my ($current, $changed, $func_map, $update) = @$data;
9449 my $then = Time::HiRes::gettimeofday();
9450 $lcovutil::profileData{$idx}{undump} = $then - $now;
9451 lcovutil::update_state(@$update);
9452 if ($function_mapping) {
9453 if (!defined($func_map)) {
9454 lcovutil::report_parallel_error(
9455 'aggregate',
9456 $ERROR_PARALLEL,
9457 $child,
9458 0,
9459 "segment $idx returned empty function data",
9460 keys(%children));
9461 next;
9462 }
9463 while (my ($key, $data) = each(%$func_map)) {
9464 $function_mapping->{$key} = [$data->[0], []]
9465 unless exists($function_mapping->{$key});
9466 die("mismatched function name '" .
9467 $data->[0] . "' at $key")
9468 unless ($data->[0] eq
9469 $function_mapping->{$key}->[0]);
9470 push(@{$function_mapping->{$key}->[1]},
9471 @{$data->[1]});
9472 }
9473 } else {
9474 if (!defined($current)) {
9475 lcovutil::report_parallel_error(
9476 'aggregate',
9477 $ERROR_PARALLEL,
9478 $child,
9479 0,
9480 "segment $idx returned empty trace data",
9481 keys(%children));
9482 next;
9483 }
9484 if ($total_trace->merge_tracefile(
9485 $current, TraceInfo::UNION
9486 )) {
9487 # something in this segment improved coverage...so save
9488 # the effective input files from this one
9489 push(@effective, @$changed);
9490 }
9491 }
9492 }; # end eval
9493 if ($@) {
9494 $childstatus = 1 << 8 unless $childstatus;
9495 lcovutil::report_parallel_error(
9496 'aggregate',
9497 $ERROR_PARALLEL,
9498 $child,
9499 $childstatus,
9500 "unable to deserialize segment $idx $dumpfile:$@",
9501 keys(%children));
9502 }
9503 }
9504 if (!defined($data) || 0 != $childstatus) {
9505 if (!-f $dumpfile ||
9506 POSIX::SIGKILL == $signal) {
9507
9508 if (exists($childRetryCounts{$idx})) {
9509 $childRetryCounts{$idx} += 1;
9510 } else {
9511 $childRetryCounts{$idx} = 1;
9512 }
9513 lcovutil::report_fork_failure(
9514 "aggregate segment $idx",
9515 (POSIX::SIGKILL == $signal ?
9516 "killed by OS - possibly due to out-of-memory"
9517 :
9518 "serialized data $dumpfile not found"),
9519 $childRetryCounts{$idx});
9520 push(@segments, $segment);
9521 } else {
9522
9523 lcovutil::report_parallel_error('aggregate',
9524 $ERROR_CHILD, $child, $childstatus,
9525 "while processing segment $idx",
9526 keys(%children));
9527 }
9528 }
9529 my $end = Time::HiRes::gettimeofday();
9530 $lcovutil::profileData{$idx}{merge} = $end - $start;
9531 unlink $dumpfile
9532 if -f $dumpfile;
9533 }
9534 } while (@segments);
9535 } else {
9536 # sequential
9537 @effective = _process_segment($total_trace, $readSourceFile, $filelist);
9538 }
9539 if (defined($lcovutil::tempdirname) &&
9540 !$lcovutil::preserve_intermediates) {
9541 # won't remove if directory not empty...probably what I want, for debugging
9542 rmdir($lcovutil::tempdirname);
9543 }
9544 #...and turn any enabled filters back on...
9545 lcovutil::reenable_cov_filters($save_filters);
9546 # filters had been disabled - need to explicitly exclude function bodies
9547 $total_trace->applyFilters($readSourceFile);
9548
9549 return ($total_trace, \@effective);
9550 }
9551
9552 # call the common initialization functions
9553
9554 lcovutil::define_errors();
9555 lcovutil::init_filters();
9556
9557 1;