Blame view
scripts/checkincludes.pl
1.91 KB
cb77f0d62 scripts: Switch t... |
1 |
#!/usr/bin/env perl |
1da177e4c Linux-2.6.12-rc2 |
2 |
# |
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 |
} |
8087a5609 scripts/checkincl... |
39 |
my $dup_counter = 0; |
3da271573 checkincludes: fi... |
40 41 42 43 |
foreach my $file (@ARGV) { open(my $f, '<', $file) or die "Cannot open $file: $!. "; |
1da177e4c Linux-2.6.12-rc2 |
44 45 |
my %includedfiles = (); |
92f3f19c5 checkincludes.pl:... |
46 |
my @file_lines = (); |
1da177e4c Linux-2.6.12-rc2 |
47 |
|
3da271573 checkincludes: fi... |
48 |
while (<$f>) { |
1da177e4c Linux-2.6.12-rc2 |
49 50 51 |
if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { ++$includedfiles{$1}; } |
92f3f19c5 checkincludes.pl:... |
52 |
push(@file_lines, $_); |
1da177e4c Linux-2.6.12-rc2 |
53 |
} |
d9a7a2bd0 checkincludes.pl:... |
54 |
|
3da271573 checkincludes: fi... |
55 |
close($f); |
92f3f19c5 checkincludes.pl:... |
56 57 |
if (!$remove) { |
3da271573 checkincludes: fi... |
58 |
foreach my $filename (keys %includedfiles) { |
92f3f19c5 checkincludes.pl:... |
59 60 61 |
if ($includedfiles{$filename} > 1) { print "$file: $filename is included more than once. "; |
8087a5609 scripts/checkincl... |
62 |
++$dup_counter; |
92f3f19c5 checkincludes.pl:... |
63 |
} |
1da177e4c Linux-2.6.12-rc2 |
64 |
} |
92f3f19c5 checkincludes.pl:... |
65 |
next; |
1da177e4c Linux-2.6.12-rc2 |
66 |
} |
92f3f19c5 checkincludes.pl:... |
67 |
|
3da271573 checkincludes: fi... |
68 69 |
open($f, '>', $file) or die("Cannot write to $file: $!"); |
92f3f19c5 checkincludes.pl:... |
70 71 72 73 |
my $dups = 0; foreach (@file_lines) { if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { |
3da271573 checkincludes: fi... |
74 |
foreach my $filename (keys %includedfiles) { |
92f3f19c5 checkincludes.pl:... |
75 76 77 78 |
if ($1 eq $filename) { if ($includedfiles{$filename} > 1) { $includedfiles{$filename}--; $dups++; |
8087a5609 scripts/checkincl... |
79 |
++$dup_counter; |
92f3f19c5 checkincludes.pl:... |
80 |
} else { |
3da271573 checkincludes: fi... |
81 |
print {$f} $_; |
92f3f19c5 checkincludes.pl:... |
82 83 84 85 |
} } } } else { |
3da271573 checkincludes: fi... |
86 |
print {$f} $_; |
92f3f19c5 checkincludes.pl:... |
87 88 89 90 91 92 |
} } if ($dups > 0) { print "$file: removed $dups duplicate includes "; } |
3da271573 checkincludes: fi... |
93 |
close($f); |
1da177e4c Linux-2.6.12-rc2 |
94 |
} |
8087a5609 scripts/checkincl... |
95 96 97 98 99 |
if ($dup_counter == 0) { print "No duplicate includes found. "; } |