2005-04-16 22:20:36 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
2009-09-18 19:49:27 +00:00
|
|
|
# checkincludes: find/remove files included more than once
|
|
|
|
#
|
2005-04-16 22:20:36 +00:00
|
|
|
# Copyright abandoned, 2000, Niels Kristian Bech Jensen <nkbj@image.dk>.
|
2009-09-18 19:49:27 +00:00
|
|
|
# Copyright 2009 Luis R. Rodriguez <mcgrof@gmail.com>
|
|
|
|
#
|
|
|
|
# This script checks for duplicate includes. It also has support
|
|
|
|
# to remove them in place. Note that this will not take into
|
|
|
|
# consideration macros so you should run this only if you know
|
|
|
|
# you do have real dups and do not have them under #ifdef's. You
|
|
|
|
# could also just review the results.
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2009-09-18 19:49:26 +00:00
|
|
|
sub usage {
|
2009-09-18 19:49:27 +00:00
|
|
|
print "Usage: checkincludes.pl [-r]\n";
|
|
|
|
print "By default we just warn of duplicates\n";
|
|
|
|
print "To remove duplicated includes in place use -r\n";
|
2009-09-18 19:49:26 +00:00
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
2009-09-18 19:49:27 +00:00
|
|
|
my $remove = 0;
|
|
|
|
|
2009-09-18 19:49:26 +00:00
|
|
|
if ($#ARGV < 0) {
|
2009-09-18 19:49:27 +00:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($#ARGV >= 1) {
|
|
|
|
if ($ARGV[0] =~ /^-/) {
|
|
|
|
if ($ARGV[0] eq "-r") {
|
|
|
|
$remove = 1;
|
|
|
|
shift;
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
2009-09-18 19:49:26 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
foreach $file (@ARGV) {
|
|
|
|
open(FILE, $file) or die "Cannot open $file: $!.\n";
|
|
|
|
|
|
|
|
my %includedfiles = ();
|
2009-09-18 19:49:27 +00:00
|
|
|
my @file_lines = ();
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
while (<FILE>) {
|
|
|
|
if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
|
|
|
|
++$includedfiles{$1};
|
|
|
|
}
|
2009-09-18 19:49:27 +00:00
|
|
|
push(@file_lines, $_);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2009-09-18 19:49:25 +00:00
|
|
|
|
|
|
|
close(FILE);
|
2009-09-18 19:49:27 +00:00
|
|
|
|
|
|
|
if (!$remove) {
|
|
|
|
foreach $filename (keys %includedfiles) {
|
|
|
|
if ($includedfiles{$filename} > 1) {
|
|
|
|
print "$file: $filename is included more than once.\n";
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2009-09-18 19:49:27 +00:00
|
|
|
next;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2009-09-18 19:49:27 +00:00
|
|
|
|
|
|
|
open(FILE,">$file") || die("Cannot write to $file: $!");
|
|
|
|
|
|
|
|
my $dups = 0;
|
|
|
|
foreach (@file_lines) {
|
|
|
|
if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
|
|
|
|
foreach $filename (keys %includedfiles) {
|
|
|
|
if ($1 eq $filename) {
|
|
|
|
if ($includedfiles{$filename} > 1) {
|
|
|
|
$includedfiles{$filename}--;
|
|
|
|
$dups++;
|
|
|
|
} else {
|
|
|
|
print FILE $_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print FILE $_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($dups > 0) {
|
|
|
|
print "$file: removed $dups duplicate includes\n";
|
|
|
|
}
|
|
|
|
close(FILE);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|