script/librelease: display the permission on the server
[ira/wip.git] / script / configure_check_unused.pl
1 #!/usr/bin/perl
2 # Script that finds macros in a configure script that are not 
3 # used in a set of C files.
4 # Copyright Jelmer Vernooij <jelmer@samba.org>, GPL
5 #
6 # Usage: ./$ARGV[0] configure.in [c-files...]
7
8 use strict;
9
10 sub autoconf_parse($$$$)
11 {
12         my $in = shift;
13         my $defines = shift;
14         my $functions = shift;
15         my $headers = shift;
16
17         open(IN, $in) or die("Can't open $in");
18
19         my $ln = 0;
20
21         foreach(<IN>) {
22                 $ln++;
23
24                 if(/AC_DEFINE\(([^,]+),/) { 
25                         $defines->{$1} = "$in:$ln";
26                 } 
27         
28                 if(/AC_CHECK_FUNCS\(\[*(.[^],)]+)/) {
29                         foreach(split / /, $1) { 
30                                 $functions->{$_} = "$in:$ln";
31                         }
32                 }
33
34                 if(/AC_CHECK_FUNC\(([^,)]+)/) {
35                         $functions->{$1} = "$in:$ln";
36                 }
37
38                 if(/AC_CHECK_HEADERS\(\[*([^],)]+)/) {
39                         foreach(split / /, $1) { 
40                                 $headers->{$_} = "$in:$ln";
41                         }
42                 }
43
44                 if(/AC_CHECK_HEADER\(([^,)]+)/) {
45                         $headers->{$1} = "$in:$ln";
46                 }
47
48                 if(/sinclude\(([^,]+)\)/) {
49                         autoconf_parse($1, $defines, $functions, $headers);
50                 }
51         }
52
53         close IN;
54 }
55
56 # Return the symbols and headers used by a C file
57 sub cfile_parse($$$)
58 {
59         my $in = shift;
60         my $symbols = shift;
61         my $headers = shift;
62
63         open(FI, $in) or die("Can't open $in");
64         my $ln = 0;     
65         my $line;
66         while($line = <FI>) { 
67                 $ln++;
68                 $_ = $line;
69                 if (/\#([ \t]*)include ["<]([^">]+)/) { 
70                         $headers->{$2} = "$in:$ln";
71                 }
72
73                 $_ = $line;
74                 while(/([A-Za-z0-9_]+)/g) { 
75                         $symbols->{$1} = "$in:$ln";
76                 }
77         }
78         close FI;
79 }
80
81 my %ac_defines = ();
82 my %ac_func_checks = ();
83 my %ac_headers = ();
84 my %symbols = ();
85 my %headers = ();
86
87 if (scalar(@ARGV) <= 1) {
88         print("Usage: configure_find_unused.pl configure.in [CFILE...]\n");
89         exit 0;
90 }
91
92 autoconf_parse(shift(@ARGV), \%ac_defines, \%ac_func_checks, \%ac_headers);
93 cfile_parse($_, \%symbols, \%headers) foreach(@ARGV);
94
95 (keys %ac_defines) or warn("No defines found in configure.in file, parse error?");
96
97 foreach (keys %ac_defines) {
98         if (not defined($symbols{$_})) {
99                 print "$ac_defines{$_}: Autoconf-defined $_ is unused\n";
100         }
101 }
102
103 (keys %ac_func_checks) or warn("No function checks found in configure.in file, parse error?");
104
105 foreach (keys %ac_func_checks) {
106         my $def = "HAVE_".uc($_);
107         if (not defined($symbols{$_})) {
108                 print "$ac_func_checks{$_}: Autoconf-checked function `$_' is unused\n";
109         } elsif (not defined($symbols{$def})) {
110                 print "$ac_func_checks{$_}: Autoconf-define `$def' for function `$_' is unused\n";
111         }
112 }
113
114 (keys %ac_headers) or warn("No headers found in configure.in file, parse error?");
115
116 foreach (keys %ac_headers) {
117         my $def = "HAVE_".uc($_);
118         $def =~ s/[\/\.]/_/g;
119         if (not defined($headers{$_})) {
120                 print "$ac_headers{$_}: Autoconf-checked header `$_' is unused\n";
121         } elsif (not defined($symbols{$def})) {
122                 print "$ac_headers{$_}: Autoconf-define `$def' for header `$_' is unused\n"; 
123         }
124 }