Blame view
scripts/checkincludes.pl
1.77 KB
1da177e4c Linux-2.6.12-rc2 |
1 2 |
#!/usr/bin/perl # |
92f3f19c5 checkincludes.pl:... |
3 4 |
# checkincludes: find/remove files included more than once # |
1da177e4c Linux-2.6.12-rc2 |
5 |
# Copyright abandoned, 2000, Niels Kristian Bech Jensen <nkbj@image.dk>. |
92f3f19c5 checkincludes.pl:... |
6 7 8 9 10 11 12 |
# Copyright 2009 Luis R. Rodriguez <mcgrof@gmail.com> # # This script checks for duplicate includes. It also has support # to remove them in place. Note that this will not take into # consideration macros so you should run this only if you know # you do have real dups and do not have them under #ifdef's. You # could also just review the results. |
1da177e4c Linux-2.6.12-rc2 |
13 |
|
3da271573 checkincludes: fi... |
14 |
use strict; |
f9d490ab3 checkincludes.pl:... |
15 |
sub usage { |
92f3f19c5 checkincludes.pl:... |
16 17 18 19 20 21 |
print "Usage: checkincludes.pl [-r] "; print "By default we just warn of duplicates "; print "To remove duplicated includes in place use -r "; |
f9d490ab3 checkincludes.pl:... |
22 23 |
exit 1; } |
92f3f19c5 checkincludes.pl:... |
24 |
my $remove = 0; |
f9d490ab3 checkincludes.pl:... |
25 |
if ($#ARGV < 0) { |
92f3f19c5 checkincludes.pl:... |
26 27 28 29 30 31 32 33 34 35 36 37 |
usage(); } if ($#ARGV >= 1) { if ($ARGV[0] =~ /^-/) { if ($ARGV[0] eq "-r") { $remove = 1; shift; } else { usage(); } } |
f9d490ab3 checkincludes.pl:... |
38 |
} |
3da271573 checkincludes: fi... |
39 40 41 42 |
foreach my $file (@ARGV) { open(my $f, '<', $file) or die "Cannot open $file: $!. "; |
1da177e4c Linux-2.6.12-rc2 |
43 44 |
my %includedfiles = (); |
92f3f19c5 checkincludes.pl:... |
45 |
my @file_lines = (); |
1da177e4c Linux-2.6.12-rc2 |
46 |
|
3da271573 checkincludes: fi... |
47 |
while (<$f>) { |
1da177e4c Linux-2.6.12-rc2 |
48 49 50 |
if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { ++$includedfiles{$1}; } |
92f3f19c5 checkincludes.pl:... |
51 |
push(@file_lines, $_); |
1da177e4c Linux-2.6.12-rc2 |
52 |
} |
d9a7a2bd0 checkincludes.pl:... |
53 |
|
3da271573 checkincludes: fi... |
54 |
close($f); |
92f3f19c5 checkincludes.pl:... |
55 56 |
if (!$remove) { |
3da271573 checkincludes: fi... |
57 |
foreach my $filename (keys %includedfiles) { |
92f3f19c5 checkincludes.pl:... |
58 59 60 61 |
if ($includedfiles{$filename} > 1) { print "$file: $filename is included more than once. "; } |
1da177e4c Linux-2.6.12-rc2 |
62 |
} |
92f3f19c5 checkincludes.pl:... |
63 |
next; |
1da177e4c Linux-2.6.12-rc2 |
64 |
} |
92f3f19c5 checkincludes.pl:... |
65 |
|
3da271573 checkincludes: fi... |
66 67 |
open($f, '>', $file) or die("Cannot write to $file: $!"); |
92f3f19c5 checkincludes.pl:... |
68 69 70 71 |
my $dups = 0; foreach (@file_lines) { if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { |
3da271573 checkincludes: fi... |
72 |
foreach my $filename (keys %includedfiles) { |
92f3f19c5 checkincludes.pl:... |
73 74 75 76 77 |
if ($1 eq $filename) { if ($includedfiles{$filename} > 1) { $includedfiles{$filename}--; $dups++; } else { |
3da271573 checkincludes: fi... |
78 |
print {$f} $_; |
92f3f19c5 checkincludes.pl:... |
79 80 81 82 |
} } } } else { |
3da271573 checkincludes: fi... |
83 |
print {$f} $_; |
92f3f19c5 checkincludes.pl:... |
84 85 86 87 88 89 |
} } if ($dups > 0) { print "$file: removed $dups duplicate includes "; } |
3da271573 checkincludes: fi... |
90 |
close($f); |
1da177e4c Linux-2.6.12-rc2 |
91 |
} |