Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | # |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 3 | # checkincludes: find/remove files included more than once |
| 4 | # |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | # Copyright abandoned, 2000, Niels Kristian Bech Jensen <nkbj@image.dk>. |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 6 | # Copyright 2009 Luis R. Rodriguez <mcgrof@gmail.com> |
| 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 14 | use strict; |
| 15 | |
Luis R. Rodriguez | f9d490a | 2009-09-18 12:49:26 -0700 | [diff] [blame] | 16 | sub usage { |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 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"; |
Luis R. Rodriguez | f9d490a | 2009-09-18 12:49:26 -0700 | [diff] [blame] | 20 | exit 1; |
| 21 | } |
| 22 | |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 23 | my $remove = 0; |
| 24 | |
Luis R. Rodriguez | f9d490a | 2009-09-18 12:49:26 -0700 | [diff] [blame] | 25 | if ($#ARGV < 0) { |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 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 | } |
Luis R. Rodriguez | f9d490a | 2009-09-18 12:49:26 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Cheah Kok Cheong | 8087a56 | 2017-02-22 15:40:26 -0800 | [diff] [blame^] | 40 | my $dup_counter = 0; |
| 41 | |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 42 | foreach my $file (@ARGV) { |
| 43 | open(my $f, '<', $file) |
| 44 | or die "Cannot open $file: $!.\n"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | my %includedfiles = (); |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 47 | my @file_lines = (); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 49 | while (<$f>) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { |
| 51 | ++$includedfiles{$1}; |
| 52 | } |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 53 | push(@file_lines, $_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
Luis R. Rodriguez | d9a7a2b | 2009-09-18 12:49:25 -0700 | [diff] [blame] | 55 | |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 56 | close($f); |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 57 | |
| 58 | if (!$remove) { |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 59 | foreach my $filename (keys %includedfiles) { |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 60 | if ($includedfiles{$filename} > 1) { |
| 61 | print "$file: $filename is included more than once.\n"; |
Cheah Kok Cheong | 8087a56 | 2017-02-22 15:40:26 -0800 | [diff] [blame^] | 62 | ++$dup_counter; |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | next; |
| 66 | } |
| 67 | |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 68 | open($f, '>', $file) |
| 69 | or die("Cannot write to $file: $!"); |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 70 | |
| 71 | my $dups = 0; |
| 72 | foreach (@file_lines) { |
| 73 | if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 74 | foreach my $filename (keys %includedfiles) { |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 75 | if ($1 eq $filename) { |
| 76 | if ($includedfiles{$filename} > 1) { |
| 77 | $includedfiles{$filename}--; |
| 78 | $dups++; |
Cheah Kok Cheong | 8087a56 | 2017-02-22 15:40:26 -0800 | [diff] [blame^] | 79 | ++$dup_counter; |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 80 | } else { |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 81 | print {$f} $_; |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | } |
| 85 | } else { |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 86 | print {$f} $_; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 89 | if ($dups > 0) { |
| 90 | print "$file: removed $dups duplicate includes\n"; |
| 91 | } |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 92 | close($f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
Cheah Kok Cheong | 8087a56 | 2017-02-22 15:40:26 -0800 | [diff] [blame^] | 94 | |
| 95 | if ($dup_counter == 0) { |
| 96 | print "No duplicate includes found.\n"; |
| 97 | } |