3 # checkincludes: find/remove files included more than once
5 # Copyright abandoned, 2000, Niels Kristian Bech Jensen <nkbj@image.dk>.
6 # Copyright 2009 Luis R. Rodriguez <mcgrof@gmail.com>
8 # This script checks for duplicate includes. It also has support
9 # to remove them in place. Note that this will not take into
10 # consideration macros so you should run this only if you know
11 # you do have real dups and do not have them under #ifdef's. You
12 # could also just review the results.
17 print "Usage: checkincludes.pl [-r]\n";
18 print "By default we just warn of duplicates\n";
19 print "To remove duplicated includes in place use -r\n";
30 if ($ARGV[0] =~ /^-/) {
31 if ($ARGV[0] eq "-r") {
42 foreach my $file (@ARGV) {
43 open(my $f, '<', $file)
44 or die "Cannot open $file: $!.\n";
46 my %includedfiles = ();
50 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
53 push(@file_lines, $_);
59 foreach my $filename (keys %includedfiles) {
60 if ($includedfiles{$filename} > 1) {
61 print "$file: $filename is included more than once.\n";
69 or die("Cannot write to $file: $!");
72 foreach (@file_lines) {
73 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
74 foreach my $filename (keys %includedfiles) {
75 if ($1 eq $filename) {
76 if ($includedfiles{$filename} > 1) {
77 $includedfiles{$filename}--;
90 print "$file: removed $dups duplicate includes\n";
95 if ($dup_counter == 0) {
96 print "No duplicate includes found.\n";