forked from angband/angband
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodestats
executable file
·52 lines (46 loc) · 1.12 KB
/
codestats
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
#!/usr/bin/env perl
use warnings qw(all);
use strict;
use autodie;
my @cfiles = @ARGV;
my $fns = {};
sub funname($$) {
my ($cf, $fn) = @_;
return undef if ($fn =~ /^#/);
return undef if ($fn =~ /^\//);
return $cf . '/' . $1 if ($fn =~ /^static \S.*?([A-Za-z0-9_]+)\(.*\)(\s*{)?$/);
return $1 if ($fn =~ /^\S.*?([A-Za-z0-9_]+)\(.*\)(\s*{)?$/);
return undef;
}
for my $cfile (@cfiles) {
open(my $fh, '<', $cfile);
my @lines = <$fh>;
close($fh);
my $curfn = undef;
my $lineno = 0;
foreach my $line (@lines) {
$lineno++;
my $fn = funname($cfile, $line);
if (not defined $curfn and defined $fn) {
if (exists $fns->{$fn}) {
printf("redef %s %s:%d %s:%d\n", $fn,
$fns->{$fn}->{file},
$fns->{$fn}->{start},
$cfile, $lineno);
next;
}
$curfn = $fn;
$fns->{$curfn} = {
file => $cfile,
start => $lineno,
};
} elsif (defined $curfn and $line =~ /^}/) {
$fns->{$curfn}->{end} = $lineno;
$curfn = undef;
}
}
}
for my $fn (keys %$fns) {
$fns->{$fn}->{len} = $fns->{$fn}->{end} - $fns->{$fn}->{start};
printf("%d %d %s\n", $fns->{$fn}->{len}, $fns->{$fn}->{start}, $fn);
}