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