Commit 3da27157316cbcce326d56faa0a7a5cadc7ae507
Committed by
Michal Marek
1 parent
1f2a144f5a
Exists in
master
and in
20 other branches
checkincludes: fix perlcritic warnings
Turn on strict checking. Use local file handles. Use three argument open. Signed-off-by: Stephen Hemminger <shemminger@vyatta.com> Cc: Cong Wang <amwang@redhat.com> Cc: Michal Marek <mmarek@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Michal Marek <mmarek@suse.cz>
Showing 1 changed file with 14 additions and 10 deletions Inline Diff
scripts/checkincludes.pl
1 | #!/usr/bin/perl | 1 | #!/usr/bin/perl |
2 | # | 2 | # |
3 | # checkincludes: find/remove files included more than once | 3 | # checkincludes: find/remove files included more than once |
4 | # | 4 | # |
5 | # 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> | 6 | # Copyright 2009 Luis R. Rodriguez <mcgrof@gmail.com> |
7 | # | 7 | # |
8 | # This script checks for duplicate includes. It also has support | 8 | # This script checks for duplicate includes. It also has support |
9 | # to remove them in place. Note that this will not take into | 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 | 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 | 11 | # you do have real dups and do not have them under #ifdef's. You |
12 | # could also just review the results. | 12 | # could also just review the results. |
13 | 13 | ||
14 | use strict; | ||
15 | |||
14 | sub usage { | 16 | sub usage { |
15 | print "Usage: checkincludes.pl [-r]\n"; | 17 | print "Usage: checkincludes.pl [-r]\n"; |
16 | print "By default we just warn of duplicates\n"; | 18 | print "By default we just warn of duplicates\n"; |
17 | print "To remove duplicated includes in place use -r\n"; | 19 | print "To remove duplicated includes in place use -r\n"; |
18 | exit 1; | 20 | exit 1; |
19 | } | 21 | } |
20 | 22 | ||
21 | my $remove = 0; | 23 | my $remove = 0; |
22 | 24 | ||
23 | if ($#ARGV < 0) { | 25 | if ($#ARGV < 0) { |
24 | usage(); | 26 | usage(); |
25 | } | 27 | } |
26 | 28 | ||
27 | if ($#ARGV >= 1) { | 29 | if ($#ARGV >= 1) { |
28 | if ($ARGV[0] =~ /^-/) { | 30 | if ($ARGV[0] =~ /^-/) { |
29 | if ($ARGV[0] eq "-r") { | 31 | if ($ARGV[0] eq "-r") { |
30 | $remove = 1; | 32 | $remove = 1; |
31 | shift; | 33 | shift; |
32 | } else { | 34 | } else { |
33 | usage(); | 35 | usage(); |
34 | } | 36 | } |
35 | } | 37 | } |
36 | } | 38 | } |
37 | 39 | ||
38 | foreach $file (@ARGV) { | 40 | foreach my $file (@ARGV) { |
39 | open(FILE, $file) or die "Cannot open $file: $!.\n"; | 41 | open(my $f, '<', $file) |
42 | or die "Cannot open $file: $!.\n"; | ||
40 | 43 | ||
41 | my %includedfiles = (); | 44 | my %includedfiles = (); |
42 | my @file_lines = (); | 45 | my @file_lines = (); |
43 | 46 | ||
44 | while (<FILE>) { | 47 | while (<$f>) { |
45 | if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { | 48 | if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { |
46 | ++$includedfiles{$1}; | 49 | ++$includedfiles{$1}; |
47 | } | 50 | } |
48 | push(@file_lines, $_); | 51 | push(@file_lines, $_); |
49 | } | 52 | } |
50 | 53 | ||
51 | close(FILE); | 54 | close($f); |
52 | 55 | ||
53 | if (!$remove) { | 56 | if (!$remove) { |
54 | foreach $filename (keys %includedfiles) { | 57 | foreach my $filename (keys %includedfiles) { |
55 | if ($includedfiles{$filename} > 1) { | 58 | if ($includedfiles{$filename} > 1) { |
56 | print "$file: $filename is included more than once.\n"; | 59 | print "$file: $filename is included more than once.\n"; |
57 | } | 60 | } |
58 | } | 61 | } |
59 | next; | 62 | next; |
60 | } | 63 | } |
61 | 64 | ||
62 | open(FILE,">$file") || die("Cannot write to $file: $!"); | 65 | open($f, '>', $file) |
66 | or die("Cannot write to $file: $!"); | ||
63 | 67 | ||
64 | my $dups = 0; | 68 | my $dups = 0; |
65 | foreach (@file_lines) { | 69 | foreach (@file_lines) { |
66 | if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { | 70 | if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { |
67 | foreach $filename (keys %includedfiles) { | 71 | foreach my $filename (keys %includedfiles) { |
68 | if ($1 eq $filename) { | 72 | if ($1 eq $filename) { |
69 | if ($includedfiles{$filename} > 1) { | 73 | if ($includedfiles{$filename} > 1) { |
70 | $includedfiles{$filename}--; | 74 | $includedfiles{$filename}--; |
71 | $dups++; | 75 | $dups++; |
72 | } else { | 76 | } else { |
73 | print FILE $_; | 77 | print {$f} $_; |
74 | } | 78 | } |
75 | } | 79 | } |
76 | } | 80 | } |
77 | } else { | 81 | } else { |
78 | print FILE $_; | 82 | print {$f} $_; |
79 | } | 83 | } |
80 | } | 84 | } |
81 | if ($dups > 0) { | 85 | if ($dups > 0) { |
82 | print "$file: removed $dups duplicate includes\n"; | 86 | print "$file: removed $dups duplicate includes\n"; |
83 | } | 87 | } |
84 | close(FILE); | 88 | close($f); |
85 | } | 89 | } |
86 | 90 |