2 # Read a module description file and derive the set of files
3 # included directly by any .c or .h file listed in the `Files:' section.
4 # Take the union of all such sets for any dependent modules.
5 # Then, compare that set with the set derived from the names
6 # listed in the various Files: sections.
8 # This script makes no attempt to diagnose invalid or empty
9 # module-description files.
11 # Written by Jim Meyering
17 (my $VERSION = '$Revision: 1.2 $ ') =~ tr/[0-9].//cd;
18 (my $ME = $0) =~ s|.*/||;
20 use constant ST_INIT => 1;
21 use constant ST_FILES => 2;
22 use constant ST_DEPENDENTS => 3;
24 # Parse a module file (returning list of Files: names and
25 # list of dependent-modules.
26 # my ($file, $dep) = parse_module_file $module_file;
27 sub parse_module_file ($)
29 my ($module_file) = @_;
31 open FH, '<', $module_file
32 or die "$ME: can't open `$module_file' for reading: $!\n";
38 while (defined (my $line = <FH>))
40 if ($state eq ST_INIT)
42 if ($line =~ /^Files:$/)
46 elsif ($line =~ /^Depends-on:$/)
48 $state = ST_DEPENDENTS;
62 if ($state eq ST_FILES)
66 elsif ($state eq ST_DEPENDENTS)
74 # my @t = sort keys %file_set;
75 # print "files: @t\n";
76 # my @u = sort keys %dep_set;
77 # print "dependents: @u\n";
79 return (\%file_set, \%dep_set);
82 # Extract the set of files required for this module, including
83 # those required via dependent modules.
96 my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
99 print $STREAM "Try `$ME --help' for more information.\n";
104 Usage: $ME [OPTIONS] FILE...
106 Read a module description file and derive the set of files
107 included directly by any .c or .h file listed in the `Files:' section.
108 Take the union of all such sets for any dependent modules.
109 Then, compare that set with the set derived from the names
110 listed in the various Files: sections.
114 --help display this help and exit
115 --version output version information and exit
122 sub find_included_lib_files ($)
127 my %special_non_dup = ( 'fnmatch_loop.c' => 1, 'regex.c' => 1 );
131 or die "$ME: can't open `$file' for reading: $!\n";
133 while (defined (my $line = <FH>))
135 # Ignore test-driver code at end of file.
136 $line =~ m!^\#if(def)? TEST_!
139 $line =~ m!^\s*\#\s*include\s+"!
144 exists $inc{$line} && ! exists $special_non_dup{$line}
145 and warn "$ME: $file: duplicate inclusion of $line\n";
147 # Some known exceptions.
148 $file =~ /\bfull-write\.c$/ && $line eq 'full-read.h'
150 $file =~ /\bsafe-read.c$/ && $line eq 'safe-write.h'
152 $file =~ /\bhash\.c$/ && $line eq 'obstack.h'
154 $file =~ /\bfts\.c$/ &&
155 ($line eq 'fts-cycle.c' || $line eq 'unistd-safer.h')
168 help => sub { usage 0 },
169 version => sub { print "$ME version $VERSION\n"; exit },
173 and (warn "$ME: missing FILE argument\n"), usage 1;
176 my %module_all_files;
186 exists $seen_module{$m}
188 $seen_module{$m} = 1;
189 my ($file, $dep) = parse_module_file $m;
191 foreach my $f (keys %$file)
193 $module_all_files{$f} = 1;
199 # Exempt headers like unlocked-io.h that are `#include'd
200 # but not necessarily used.
201 'unlocked-io.h' => 1,
203 # Give gettext.h a free pass only when included from lib/error.c,
204 # since we've made that exception solely to make the error
205 # module easier to use -- at RMS's request.
206 'lib/error.c:gettext.h' => 1,
209 my @t = sort keys %module_all_files;
210 # warn "ALL files: @t\n";
212 # Derive from %module_all_files (by parsing the .c and .h files therein),
213 # the list of all #include'd files that reside in lib/.
214 foreach my $f (keys %module_all_files)
218 # FIXME: this is too naive
219 my $inc = find_included_lib_files "../$f";
220 foreach my $i (sort keys %$inc)
222 my $lib_file = "lib/$i";
223 exists $exempt_header{"$f:$i"}
224 || exists $exempt_header{$i}
226 !exists $module_all_files{$lib_file} && -f "../lib/$i"
227 and warn "$f: $i is `#include'd, but not "
228 . "listed in module's Files: section\n";
230 #my @t = sort keys %$inc;
231 #print "** $f: @t\n";