2 # This script checks the *.c files for extraneous "extern" variables,
3 # for vars that are defined but not used, and for inconsistent array
4 # sizes. Run it from inside the main rsync directory.
8 my %add_syscall_c = map { $_ => 1 } qw( t_stub.c t_unsafe.c tls.c trimslash.c );
9 my %add_util_c = map { $_ => 1 } qw( t_stub.c t_unsafe.c );
12 open(IN, '<', 'syscall.c') or die $!;
13 undef $/; my $syscall_c = <IN>; $/ = "\n";
15 $syscall_c =~ s/^extern\s.*//mg;
17 open(IN, '<', 'util.c') or die $!;
18 undef $/; my $util_c = <IN>; $/ = "\n";
20 $util_c =~ s/^extern\s.*//mg;
22 my @files = glob('*.c');
24 foreach my $fn (@files) {
25 open(IN, '<', $fn) or die $!;
26 undef $/; $_ = <IN>; $/ = "\n";
29 my @vars = /^(?!(?:extern|enum)\s)([a-zA-Z]\S*\s+.*);/mg;
30 my @externs = /^extern\s+(.*);/mg;
32 $_ .= $syscall_c if $add_syscall_c{$fn};
33 $_ .= $util_c if $add_util_c{$fn};
34 s/INFO_GTE/info_levels/g;
35 s/DEBUG_GTE/debug_levels/g;
37 check_vars($fn, 'var', @vars);
38 check_vars($fn, 'extern', @externs);
43 # The file's contents are in $_.
49 foreach my $line (@_) {
50 $line =~ s/\s*\{.*\}//;
51 $line =~ s/\s*\(.*\)//;
52 foreach my $item (split(/\s*,\s*/, $line)) {
54 my $sz = $item =~ s/(\[.*?\])// ? $1 : '';
55 my($var) = $item =~ /([^*\s]+)$/;
57 print "Bogus match? ($item)\n";
61 if (defined $sizes{$var}) {
62 if ($sizes{$var} ne $sz) {
63 print $fn, ' has inconsistent size for "', $var,
64 "\": $sizes{$var} vs $sz\n";
70 my @matches = /(?<!\sstruct )\b(\Q$var\E)(?!\w)/g;
71 push(@matches, /(\QSIGACTION(\E)/g) if $var eq 'sigact';
72 print $fn, " has extraneous $type: \"", $var, "\"\n" if @matches == 1;