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