Kamil Rytarowski | cb77f0d | 2017-05-07 23:25:26 +0200 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | # |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 4 | # checkincludes: find/remove files included more than once |
| 5 | # |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | # Copyright abandoned, 2000, Niels Kristian Bech Jensen <nkbj@image.dk>. |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 7 | # Copyright 2009 Luis R. Rodriguez <mcgrof@gmail.com> |
| 8 | # |
| 9 | # This script checks for duplicate includes. It also has support |
| 10 | # to remove them in place. Note that this will not take into |
| 11 | # consideration macros so you should run this only if you know |
| 12 | # you do have real dups and do not have them under #ifdef's. You |
| 13 | # could also just review the results. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 15 | use strict; |
| 16 | |
Luis R. Rodriguez | f9d490a | 2009-09-18 12:49:26 -0700 | [diff] [blame] | 17 | sub usage { |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 18 | print "Usage: checkincludes.pl [-r]\n"; |
| 19 | print "By default we just warn of duplicates\n"; |
| 20 | print "To remove duplicated includes in place use -r\n"; |
Luis R. Rodriguez | f9d490a | 2009-09-18 12:49:26 -0700 | [diff] [blame] | 21 | exit 1; |
| 22 | } |
| 23 | |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 24 | my $remove = 0; |
| 25 | |
Luis R. Rodriguez | f9d490a | 2009-09-18 12:49:26 -0700 | [diff] [blame] | 26 | if ($#ARGV < 0) { |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 27 | usage(); |
| 28 | } |
| 29 | |
| 30 | if ($#ARGV >= 1) { |
| 31 | if ($ARGV[0] =~ /^-/) { |
| 32 | if ($ARGV[0] eq "-r") { |
| 33 | $remove = 1; |
| 34 | shift; |
| 35 | } else { |
| 36 | usage(); |
| 37 | } |
| 38 | } |
Luis R. Rodriguez | f9d490a | 2009-09-18 12:49:26 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Cheah Kok Cheong | 8087a56 | 2017-02-22 15:40:26 -0800 | [diff] [blame] | 41 | my $dup_counter = 0; |
| 42 | |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 43 | foreach my $file (@ARGV) { |
| 44 | open(my $f, '<', $file) |
| 45 | or die "Cannot open $file: $!.\n"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | my %includedfiles = (); |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 48 | my @file_lines = (); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 50 | while (<$f>) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { |
| 52 | ++$includedfiles{$1}; |
| 53 | } |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 54 | push(@file_lines, $_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
Luis R. Rodriguez | d9a7a2b | 2009-09-18 12:49:25 -0700 | [diff] [blame] | 56 | |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 57 | close($f); |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 58 | |
| 59 | if (!$remove) { |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 60 | foreach my $filename (keys %includedfiles) { |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 61 | if ($includedfiles{$filename} > 1) { |
| 62 | print "$file: $filename is included more than once.\n"; |
Cheah Kok Cheong | 8087a56 | 2017-02-22 15:40:26 -0800 | [diff] [blame] | 63 | ++$dup_counter; |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | next; |
| 67 | } |
| 68 | |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 69 | open($f, '>', $file) |
| 70 | or die("Cannot write to $file: $!"); |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 71 | |
| 72 | my $dups = 0; |
| 73 | foreach (@file_lines) { |
| 74 | if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 75 | foreach my $filename (keys %includedfiles) { |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 76 | if ($1 eq $filename) { |
| 77 | if ($includedfiles{$filename} > 1) { |
| 78 | $includedfiles{$filename}--; |
| 79 | $dups++; |
Cheah Kok Cheong | 8087a56 | 2017-02-22 15:40:26 -0800 | [diff] [blame] | 80 | ++$dup_counter; |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 81 | } else { |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 82 | print {$f} $_; |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |
| 86 | } else { |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 87 | print {$f} $_; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
Luis R. Rodriguez | 92f3f19 | 2009-09-18 12:49:27 -0700 | [diff] [blame] | 90 | if ($dups > 0) { |
| 91 | print "$file: removed $dups duplicate includes\n"; |
| 92 | } |
Stephen Hemminger | 3da2715 | 2010-02-22 15:17:12 -0800 | [diff] [blame] | 93 | close($f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
Cheah Kok Cheong | 8087a56 | 2017-02-22 15:40:26 -0800 | [diff] [blame] | 95 | |
| 96 | if ($dup_counter == 0) { |
| 97 | print "No duplicate includes found.\n"; |
| 98 | } |