2017-05-08 00:25:26 +03:00
#!/usr/bin/env perl
2005-04-17 02:20:36 +04:00
#
2009-09-18 23:49:27 +04:00
# checkincludes: find/remove files included more than once
#
2005-04-17 02:20:36 +04:00
# Copyright abandoned, 2000, Niels Kristian Bech Jensen <nkbj@image.dk>.
2009-09-18 23:49:27 +04: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-17 02:20:36 +04:00
2010-02-23 02:17:12 +03:00
use strict ;
2009-09-18 23:49:26 +04:00
sub usage {
2009-09-18 23:49:27 +04: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 23:49:26 +04:00
exit 1 ;
}
2009-09-18 23:49:27 +04:00
my $ remove = 0 ;
2009-09-18 23:49:26 +04:00
if ( $# ARGV < 0 ) {
2009-09-18 23:49:27 +04:00
usage ( ) ;
}
if ( $# ARGV >= 1 ) {
if ( $ ARGV [ 0 ] =~ /^-/ ) {
if ( $ ARGV [ 0 ] eq "-r" ) {
$ remove = 1 ;
shift ;
} else {
usage ( ) ;
}
}
2009-09-18 23:49:26 +04:00
}
2017-02-23 02:40:26 +03:00
my $ dup_counter = 0 ;
2010-02-23 02:17:12 +03:00
foreach my $ file ( @ ARGV ) {
open ( my $ f , '<' , $ file )
or die "Cannot open $file: $!.\n" ;
2005-04-17 02:20:36 +04:00
my % includedfiles = ( ) ;
2009-09-18 23:49:27 +04:00
my @ file_lines = ( ) ;
2005-04-17 02:20:36 +04:00
2010-02-23 02:17:12 +03:00
while ( <$f> ) {
2005-04-17 02:20:36 +04:00
if ( m/^\s*#\s*include\s*[<"](\S*)[>"]/o ) {
+ + $ includedfiles { $ 1 } ;
}
2009-09-18 23:49:27 +04:00
push ( @ file_lines , $ _ ) ;
2005-04-17 02:20:36 +04:00
}
2009-09-18 23:49:25 +04:00
2010-02-23 02:17:12 +03:00
close ( $ f ) ;
2009-09-18 23:49:27 +04:00
if ( ! $ remove ) {
2010-02-23 02:17:12 +03:00
foreach my $ filename ( keys % includedfiles ) {
2009-09-18 23:49:27 +04:00
if ( $ includedfiles { $ filename } > 1 ) {
print "$file: $filename is included more than once.\n" ;
2017-02-23 02:40:26 +03:00
+ + $ dup_counter ;
2009-09-18 23:49:27 +04:00
}
2005-04-17 02:20:36 +04:00
}
2009-09-18 23:49:27 +04:00
next ;
2005-04-17 02:20:36 +04:00
}
2009-09-18 23:49:27 +04:00
2010-02-23 02:17:12 +03:00
open ( $ f , '>' , $ file )
or die ( "Cannot write to $file: $!" ) ;
2009-09-18 23:49:27 +04:00
my $ dups = 0 ;
foreach ( @ file_lines ) {
if ( m/^\s*#\s*include\s*[<"](\S*)[>"]/o ) {
2010-02-23 02:17:12 +03:00
foreach my $ filename ( keys % includedfiles ) {
2009-09-18 23:49:27 +04:00
if ( $ 1 eq $ filename ) {
if ( $ includedfiles { $ filename } > 1 ) {
$ includedfiles { $ filename } - - ;
$ dups + + ;
2017-02-23 02:40:26 +03:00
+ + $ dup_counter ;
2009-09-18 23:49:27 +04:00
} else {
2010-02-23 02:17:12 +03:00
print { $ f } $ _ ;
2009-09-18 23:49:27 +04:00
}
}
}
} else {
2010-02-23 02:17:12 +03:00
print { $ f } $ _ ;
2009-09-18 23:49:27 +04:00
}
}
if ( $ dups > 0 ) {
print "$file: removed $dups duplicate includes\n" ;
}
2010-02-23 02:17:12 +03:00
close ( $ f ) ;
2005-04-17 02:20:36 +04:00
}
2017-02-23 02:40:26 +03:00
if ( $ dup_counter == 0 ) {
print "No duplicate includes found.\n" ;
}