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.1 $ ') =~ 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'
165 help => sub { usage 0 },
166 version => sub { print "$ME version $VERSION\n"; exit },
170 and (warn "$ME: missing FILE argument\n"), usage 1;
173 my %module_all_files;
183 exists $seen_module{$m}
185 $seen_module{$m} = 1;
186 my ($file, $dep) = parse_module_file $m;
188 foreach my $f (keys %$file)
190 $module_all_files{$f} = 1;
196 # Exempt headers like unlocked-io.h that are `#include'd
197 # but not necessarily used.
198 'unlocked-io.h' => 1,
200 # Give gettext.h a free pass only when included from lib/error.c,
201 # since that we've made that exception solely to make the error
202 # module easier to use -- at RMS's request.
203 'lib/error.c:gettext.h' => 1,
206 my @t = sort keys %module_all_files;
207 # warn "ALL files: @t\n";
209 # Derive from %module_all_files (by parsing the .c and .h files therein),
210 # the list of all #include'd files that reside in lib/.
211 foreach my $f (keys %module_all_files)
215 # FIXME: this is too naive
216 my $inc = find_included_lib_files "../$f";
217 foreach my $i (sort keys %$inc)
219 my $lib_file = "lib/$i";
220 exists $exempt_header{"$f:$i"}
221 || exists $exempt_header{$i}
223 !exists $module_all_files{$lib_file} && -f "../lib/$i"
224 and warn "$f: $i is `#include'd, but not "
225 . "listed in module's Files: section\n";
227 #my @t = sort keys %$inc;
228 #print "** $f: @t\n";