aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkincludes.pl
diff options
context:
space:
mode:
authorLuis R. Rodriguez <lrodriguez@Atheros.com>2009-09-18 15:49:27 -0400
committerSam Ravnborg <sam@ravnborg.org>2009-09-20 06:27:43 -0400
commit92f3f19c519d2eb18812a38312b05075cf0407db (patch)
treee74c7e9678ba944adba0995ac1b374ab5282edfe /scripts/checkincludes.pl
parent82fa39552fd858468a5647fa58d14955ecd50eb4 (diff)
checkincludes.pl: add option to remove duplicates in place
checkincludes.pl is more useful if it actually removed the lines. This adds support for that with -r. [akpm@linux-foundation.org: improve usage message] Signed-off-by: Luis R. Rodriguez <lrodriguez@atheros.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
Diffstat (limited to 'scripts/checkincludes.pl')
-rwxr-xr-xscripts/checkincludes.pl66
1 files changed, 59 insertions, 7 deletions
diff --git a/scripts/checkincludes.pl b/scripts/checkincludes.pl
index 4bff13985ebd..676ddc07d6fa 100755
--- a/scripts/checkincludes.pl
+++ b/scripts/checkincludes.pl
@@ -1,33 +1,85 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2# 2#
3# checkincludes: Find files included more than once in (other) files. 3# checkincludes: find/remove files included more than once
4#
4# Copyright abandoned, 2000, Niels Kristian Bech Jensen <nkbj@image.dk>. 5# Copyright abandoned, 2000, Niels Kristian Bech Jensen <nkbj@image.dk>.
6# Copyright 2009 Luis R. Rodriguez <mcgrof@gmail.com>
7#
8# This script checks for duplicate includes. It also has support
9# to remove them in place. Note that this will not take into
10# consideration macros so you should run this only if you know
11# you do have real dups and do not have them under #ifdef's. You
12# could also just review the results.
5 13
6sub usage { 14sub usage {
7 print "Usage: checkincludes.pl <file list>\n"; 15 print "Usage: checkincludes.pl [-r]\n";
16 print "By default we just warn of duplicates\n";
17 print "To remove duplicated includes in place use -r\n";
8 exit 1; 18 exit 1;
9} 19}
10 20
21my $remove = 0;
22
11if ($#ARGV < 0) { 23if ($#ARGV < 0) {
12 usage(); 24 usage();
25}
26
27if ($#ARGV >= 1) {
28 if ($ARGV[0] =~ /^-/) {
29 if ($ARGV[0] eq "-r") {
30 $remove = 1;
31 shift;
32 } else {
33 usage();
34 }
35 }
13} 36}
14 37
15foreach $file (@ARGV) { 38foreach $file (@ARGV) {
16 open(FILE, $file) or die "Cannot open $file: $!.\n"; 39 open(FILE, $file) or die "Cannot open $file: $!.\n";
17 40
18 my %includedfiles = (); 41 my %includedfiles = ();
42 my @file_lines = ();
19 43
20 while (<FILE>) { 44 while (<FILE>) {
21 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { 45 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
22 ++$includedfiles{$1}; 46 ++$includedfiles{$1};
23 } 47 }
48 push(@file_lines, $_);
24 } 49 }
25 50
26 close(FILE); 51 close(FILE);
27 52
28 foreach $filename (keys %includedfiles) { 53 if (!$remove) {
29 if ($includedfiles{$filename} > 1) { 54 foreach $filename (keys %includedfiles) {
30 print "$file: $filename is included more than once.\n"; 55 if ($includedfiles{$filename} > 1) {
56 print "$file: $filename is included more than once.\n";
57 }
31 } 58 }
59 next;
32 } 60 }
61
62 open(FILE,">$file") || die("Cannot write to $file: $!");
63
64 my $dups = 0;
65 foreach (@file_lines) {
66 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
67 foreach $filename (keys %includedfiles) {
68 if ($1 eq $filename) {
69 if ($includedfiles{$filename} > 1) {
70 $includedfiles{$filename}--;
71 $dups++;
72 } else {
73 print FILE $_;
74 }
75 }
76 }
77 } else {
78 print FILE $_;
79 }
80 }
81 if ($dups > 0) {
82 print "$file: removed $dups duplicate includes\n";
83 }
84 close(FILE);
33} 85}