#!/usr/bin/perl -w # # Simple tool which will report on each source file which includes # the same header more than once. # # It would be trivial to automatically just remove duplicate headers # but that might cause breakage. Consider the case # # #ifdef CONFIG_FOO # #include "a.h" # #endif # # #ifdef CONFIG_BAR # #include "a.h" # #endif # # Removing the second header automatically would result in a compilation # failure. # # Still human eyeballs can do the right thing. My rule of thumb? If there # is any "ifdef" leave multiple headers in place. If not remove the second # inclusion... # # Too trivial a script to care about licensing. Use as you wish... # # Steve # -- # use strict; use File::Find; # # Starting directory may be specified upon command line, otherwise # cwd is used. # my $dir = shift || "."; # # Find all the files beneath the specified directory # find( { wanted => \&checkFile, no_chdir => 1 }, $dir ); =begin doc Report on files containing the same header file more than once. =end doc =cut sub checkFile { my $file = $File::Find::name; # We don't care about directories or non-source return unless ( ( !-d $file ) && ( $file =~ /\.[ch]$/ ) ); # hash to record included files my %included; open( FILE, "<", $file ) or die "Failed to read $file $!"; while ( my $line = ) { next if ( !$line ); if ( $line =~ /#include (.*)/ ) { my $inc = $1; if ( $included{ $inc } ) { print "$file: includes $inc more than once\n"; } else { $included{ $inc } += 1; } } } close(FILE); }