]>
Commit | Line | Data |
---|---|---|
cb77f0d6 | 1 | #!/usr/bin/env perl |
1da177e4 | 2 | # |
92f3f19c LR |
3 | # checkincludes: find/remove files included more than once |
4 | # | |
1da177e4 | 5 | # Copyright abandoned, 2000, Niels Kristian Bech Jensen <[email protected]>. |
92f3f19c LR |
6 | # Copyright 2009 Luis R. Rodriguez <[email protected]> |
7 | # | |
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. | |
1da177e4 | 13 | |
3da27157 SH |
14 | use strict; |
15 | ||
f9d490ab | 16 | sub usage { |
92f3f19c LR |
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"; | |
f9d490ab LR |
20 | exit 1; |
21 | } | |
22 | ||
92f3f19c LR |
23 | my $remove = 0; |
24 | ||
f9d490ab | 25 | if ($#ARGV < 0) { |
92f3f19c LR |
26 | usage(); |
27 | } | |
28 | ||
29 | if ($#ARGV >= 1) { | |
30 | if ($ARGV[0] =~ /^-/) { | |
31 | if ($ARGV[0] eq "-r") { | |
32 | $remove = 1; | |
33 | shift; | |
34 | } else { | |
35 | usage(); | |
36 | } | |
37 | } | |
f9d490ab LR |
38 | } |
39 | ||
8087a560 CKC |
40 | my $dup_counter = 0; |
41 | ||
3da27157 SH |
42 | foreach my $file (@ARGV) { |
43 | open(my $f, '<', $file) | |
44 | or die "Cannot open $file: $!.\n"; | |
1da177e4 LT |
45 | |
46 | my %includedfiles = (); | |
92f3f19c | 47 | my @file_lines = (); |
1da177e4 | 48 | |
3da27157 | 49 | while (<$f>) { |
1da177e4 LT |
50 | if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { |
51 | ++$includedfiles{$1}; | |
52 | } | |
92f3f19c | 53 | push(@file_lines, $_); |
1da177e4 | 54 | } |
d9a7a2bd | 55 | |
3da27157 | 56 | close($f); |
92f3f19c LR |
57 | |
58 | if (!$remove) { | |
3da27157 | 59 | foreach my $filename (keys %includedfiles) { |
92f3f19c LR |
60 | if ($includedfiles{$filename} > 1) { |
61 | print "$file: $filename is included more than once.\n"; | |
8087a560 | 62 | ++$dup_counter; |
92f3f19c | 63 | } |
1da177e4 | 64 | } |
92f3f19c | 65 | next; |
1da177e4 | 66 | } |
92f3f19c | 67 | |
3da27157 SH |
68 | open($f, '>', $file) |
69 | or die("Cannot write to $file: $!"); | |
92f3f19c LR |
70 | |
71 | my $dups = 0; | |
72 | foreach (@file_lines) { | |
73 | if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { | |
3da27157 | 74 | foreach my $filename (keys %includedfiles) { |
92f3f19c LR |
75 | if ($1 eq $filename) { |
76 | if ($includedfiles{$filename} > 1) { | |
77 | $includedfiles{$filename}--; | |
78 | $dups++; | |
8087a560 | 79 | ++$dup_counter; |
92f3f19c | 80 | } else { |
3da27157 | 81 | print {$f} $_; |
92f3f19c LR |
82 | } |
83 | } | |
84 | } | |
85 | } else { | |
3da27157 | 86 | print {$f} $_; |
92f3f19c LR |
87 | } |
88 | } | |
89 | if ($dups > 0) { | |
90 | print "$file: removed $dups duplicate includes\n"; | |
91 | } | |
3da27157 | 92 | close($f); |
1da177e4 | 93 | } |
8087a560 CKC |
94 | |
95 | if ($dup_counter == 0) { | |
96 | print "No duplicate includes found.\n"; | |
97 | } |