File: gcov-upd.pl

package info (click to toggle)
safeclib 3.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,832 kB
  • sloc: ansic: 52,639; makefile: 1,271; perl: 528; sh: 518
file content (121 lines) | stat: -rwxr-xr-x 3,103 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/perl

=head1 DESCRIPTION

parse the stderr output of gcov for each src file:

    File 'str/sprintf_s.c'
    Lines executed:90.32% of 31

and update each test file with the new coverage,
printing changes from previous runs.

=cut

use File::Basename qw(dirname basename);

my $file = shift || die "missing input file";
open my $f, "<", $file || die "cannot read input file: $!";
my ($src, $test, $gcov, %warned);
while (<$f>) {
  if (/^File '(.*)'/) {
    $src = $1;
    if ($src =~ /test_/) {
      $src = undef;
    }
  }
  elsif (/^Lines executed:(.*)\n/) {
    $gcov = $1;
    $test = basename $src;
    $test = "tests/test_$test";
    if (-e $test && $src && $gcov) {
      upd_test($test, $src, $gcov);
    } elsif (!exists $warned{$test}) {
      if (!-e $test) {
        warn "Missing $test for $src $gcov\n";
      } elsif (!$src) {
        warn "Missing src for $test $gcov\n";
      } elsif ($gcov) {
        warn "Missing gcov for $test $src\n";
      }
      $warned{$test}++;
    }
  }
}
close $f;

sub upd_test {
  my ($file, $src, $gcov) = @_; # 2 lines, like
#str/sprintf_s.c
#90.32% of 31
  my $t;
  if (!open $t, "<", $file) {
    warn "cannot read test file $file\n";
    return;
  }
  my $base = basename $file;
  $base =~ s/\.c$//;
  my $out = $file . ".tmp";
  open my $o, ">", $out || die "cannot write tmp file $file\n";
  my $found;
  $. = 0;
  while (<$t>) {
    print $o $_;
    if (!$found) {
      if ($. < 3 && /^\s*\*\s*\Q$base\E/) {
        # read next line to see if its already there
        $_ = <$t>;
        if (/^ \* File/) {
          print $o $_;
          $_ = <$t>; # read cov
          my ($old) = $_ =~ /Lines executed:(.*)\n/;
          if ($old ne $gcov) {
            printf STDERR "%-24s\t%s => %s\n", $src, $old, $gcov;
            if ($gcov =~ /^0.00/) {
              $gcov = $old;
            } else {
              $changed++;
            }
          } else {
            printf STDERR "%-24s\t%s\n", $src, $gcov;
          }
          print $o " * Lines executed:$gcov\n";
        } else {
          $changed++;
          print $o " * File '$src'\n";
          print $o " * Lines executed:$gcov\n";
          print $o $_;
          printf STDERR "%-24s\t%s\n", $src, $gcov;
        }
        $found++;
        next;
      } elsif ($. >= 3) {
        $changed++;
        if (/^\s*\*/) {
          print $o " * File '$src'\n";
          print $o " * Lines executed:$gcov\n";
          printf STDERR "%-24s\t%s\n", $src, $gcov;
          $found++;
          next;
        } else {
          print $o "/*------------------------------------------------------------------\n";
          print $o " * $base\n";
          print $o " * File '$src'\n";
          print $o " * Lines executed:$gcov\n";
          print $o " *\n *------------------------------------------------------------------\n*/\n";
          printf STDERR "%-24s\t%s\n", $src, $gcov;
          $found++;
          next;
        }
      }
    }
  }
  close $t;
  close $o;
  if ($changed) {
    rename $file, $file."~";
    rename $out, $file;
  } else {
    unlink $out;
  }
}