Annotation of embedaddon/rsync/packaging/var-checker, revision 1.1
1.1 ! misho 1: #!/usr/bin/perl
! 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.
! 5:
! 6: use strict;
! 7: use warnings;
! 8:
! 9: my %add_syscall_c = map { $_ => 1 } qw( t_stub.c t_unsafe.c tls.c trimslash.c );
! 10: my %add_compat_c = map { $_ => 1 } qw( t_stub.c tls.c trimslash.c wildtest.c );
! 11: my %add_util_c = map { $_ => 1 } qw( t_stub.c t_unsafe.c );
! 12: my %sizes;
! 13:
! 14: open(IN, '<', 'syscall.c') or die $!;
! 15: undef $/; my $syscall_c = <IN>; $/ = "\n";
! 16: close IN;
! 17: $syscall_c =~ s/^extern\s.*//mg;
! 18:
! 19: open(IN, '<', 'lib/compat.c') or die $!;
! 20: undef $/; my $compat_c = <IN>; $/ = "\n";
! 21: close IN;
! 22: $compat_c =~ s/^extern\s.*//mg;
! 23:
! 24: open(IN, '<', 'util.c') or die $!;
! 25: undef $/; my $util_c = <IN>; $/ = "\n";
! 26: close IN;
! 27: $util_c =~ s/^extern\s.*//mg;
! 28:
! 29: my @files = glob('*.c');
! 30:
! 31: foreach my $fn (@files) {
! 32: open(IN, '<', $fn) or die $!;
! 33: undef $/; $_ = <IN>; $/ = "\n";
! 34: close IN;
! 35:
! 36: my @vars = /^(?!(?:extern|enum)\s)([a-zA-Z]\S*\s+.*);/mg;
! 37: my @externs = /^extern\s+(.*);/mg;
! 38:
! 39: $_ .= $syscall_c if $add_syscall_c{$fn};
! 40: $_ .= $compat_c if $add_compat_c{$fn};
! 41: $_ .= $util_c if $add_util_c{$fn};
! 42: s/INFO_GTE/info_levels/g;
! 43: s/DEBUG_GTE/debug_levels/g;
! 44:
! 45: check_vars($fn, 'var', @vars);
! 46: check_vars($fn, 'extern', @externs);
! 47: }
! 48:
! 49: exit;
! 50:
! 51: # The file's contents are in $_.
! 52: sub check_vars
! 53: {
! 54: my $fn = shift;
! 55: my $type = shift;
! 56:
! 57: foreach my $line (@_) {
! 58: $line =~ s/\s*\{.*\}//;
! 59: $line =~ s/\s*\(.*\)//;
! 60: foreach my $item (split(/\s*,\s*/, $line)) {
! 61: $item =~ s/\s*=.*//;
! 62: my $sz = $item =~ s/(\[.*?\])// ? $1 : '';
! 63: my($var) = $item =~ /([^*\s]+)$/;
! 64: if (!defined $var) {
! 65: print "Bogus match? ($item)\n";
! 66: next;
! 67: }
! 68: if ($sz) {
! 69: if (defined $sizes{$var}) {
! 70: if ($sizes{$var} ne $sz) {
! 71: print $fn, ' has inconsistent size for "', $var,
! 72: "\": $sizes{$var} vs $sz\n";
! 73: }
! 74: } else {
! 75: $sizes{$var} = $sz;
! 76: }
! 77: }
! 78: my @matches = /(?<!\sstruct )\b(\Q$var\E)(?!\w)/g;
! 79: push(@matches, /(\QSIGACTION(\E)/g) if $var eq 'sigact';
! 80: print $fn, " has extraneous $type: \"", $var, "\"\n" if @matches == 1;
! 81: }
! 82: }
! 83: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>