scripts/checkincludes.pl: Simplify and shorten argument logic
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
In my last submission, Joe Perches (is that right?) suggested using a function that is built in to Perl.
I haven't had a chance to test it yet, but if it turns out to be okay, I'll make a separate merge request at a later date.
Quoted 125 lines · Cruz Julian Bishop
This patch allows for much easier implementation of arguments when modifying checkincludes.pl
The variable $file is also initially named $arg - I wasn't sure if memory would be a problem (I know it would be in Java/C#) if another $file variable was initialized with the value of $arg, so I just left it.
Overall, this still works nicely. There are some more potential simplifications, but I will cover those in another patch if I decide to implement them tonight.
Signed-off-by Cruz Julian Bishop <[email protected]>--- scripts/checkincludes.pl | 85 ++++++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 46 deletions(-)scripts/checkincludes.pl +39
-46@@ -30,64 +30,57 @@ if ($#ARGV < 0) { usage(); } -if ($#ARGV >= 1) { - if ($ARGV[0] =~ /^-/) { - if ($ARGV[0] eq "-r") { - $remove = 1; - shift; - } else { - usage(); - } - } -} +foreach my $arg (@ARGV) { + if ($arg eq "-r") { + $remove = 1; + } else { + open(my $f, '<', $arg) + or die "Cannot open $arg: $!.\n"; -foreach my $file (@ARGV) { - open(my $f, '<', $file) - or die "Cannot open $file: $!.\n"; + my %includedfiles = (); + my @file_lines = (); - my %includedfiles = (); - my @file_lines = (); - - while (<$f>) { - if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { - ++$includedfiles{$1}; + while (<$f>) { + if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { + ++$includedfiles{$1}; + } + push(@file_lines, $_); } - push(@file_lines, $_); - } - close($f); + close($f); - if (!$remove) { - foreach my $filename (keys %includedfiles) { - if ($includedfiles{$filename} > 1) { - print "$file: $filename is included more than once.\n"; + if (!$remove) { + foreach my $filename (keys %includedfiles) { + if ($includedfiles{$filename} > 1) { + print "$arg: $filename is included more than once.\n"; + } } + next; } - next; - } - open($f, '>', $file) - or die("Cannot write to $file: $!"); + open($f, '>', $arg) + or die("Cannot write to $arg: $!"); - my $dups = 0; - foreach (@file_lines) { - if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { - foreach my $filename (keys %includedfiles) { - if ($1 eq $filename) { - if ($includedfiles{$filename} > 1) { - $includedfiles{$filename}--; - $dups++; - } else { - print {$f} $_; + my $dups = 0; + foreach(@file_lines) { + if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { + foreach my $filename (keys %includedfiles) { + if ($1 eq $filename) { + if ($includedfiles{$filename} > 1) { + $includedfiles{$filename}--; + $dups++; + } else { + print {$f} $_; + } } } + } else { + print {$f} $_; } - } else { - print {$f} $_; } + if ($dups > 0) { + print "$arg: removed $dups duplicate includes \n"; + } + close($f); } - if ($dups > 0) { - print "$file: removed $dups duplicate includes\n"; - } - close($f); }
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH 5/5] scripts/checkincludes.pl: Replace tabs with spaces
My bad, misread the documentation files.
I'll resubmit without this
Quoted 165 lines · Cruz Julian Bishop
This is required by the CodingStyle kernel documentation file
Signed-off-by Cruz Julian Bishop <[email protected]>--- scripts/checkincludes.pl | 122 +++++++++++++++++++++++------------------------ 1 file changed, 61 insertions(+), 61 deletions(-)scripts/checkincludes.pl +61
-61@@ -14,7 +14,7 @@ use strict; sub usage { - print <<EOM; + print <<EOM; Usage: checkincludes.pl [OPTIONS]... FILE... By default, we just warn of duplicates.@@ -23,84 +23,84 @@ Options: -q Do not show messages for files that do not have duplicated includes EOM - exit 1; + exit 1; } my $remove = 0; my $quiet = 0; if ($#ARGV < 0) { - usage(); + usage(); } my $argc = 0; foreach my $arg (@ARGV) { - if ($arg eq "-r") { - $remove = 1; - } elsif($arg eq "-q") { - $quiet = 1; - } else { - open(my $f, '<', $arg) - or die "Cannot open $arg: $!.\n"; - - $argc++; + if ($arg eq "-r") { + $remove = 1; + } elsif($arg eq "-q") { + $quiet = 1; + } else { + open(my $f, '<', $arg) + or die "Cannot open $arg: $!.\n"; + + $argc++; - my %includedfiles = (); - my @file_lines = (); + my %includedfiles = (); + my @file_lines = (); - while (<$f>) { - if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { - ++$includedfiles{$1}; - } - push(@file_lines, $_); - } + while (<$f>) { + if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { + ++$includedfiles{$1}; + } + push(@file_lines, $_); + } - close($f); + close($f); - if (!$remove) { - my $detected = 0; - foreach my $filename (keys %includedfiles) { - if ($includedfiles{$filename} > 1) { - $detected++; - print "$arg: $filename is included more than once.\n"; - } - } - if (!$detected && !$quiet) { - print "$arg: No duplicated includes detected.\n"; - } - next; - } + if (!$remove) { + my $detected = 0; + foreach my $filename (keys %includedfiles) { + if ($includedfiles{$filename} > 1) { + $detected++; + print "$arg: $filename is included more than once.\n"; + } + } + if (!$detected && !$quiet) { + print "$arg: No duplicated includes detected.\n"; + } + next; + } - open($f, '>', $arg) - or die("Cannot write to $arg: $!"); + open($f, '>', $arg) + or die("Cannot write to $arg: $!"); - my $dups = 0; - foreach(@file_lines) { - if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { - foreach my $filename (keys %includedfiles) { - if ($1 eq $filename) { - if ($includedfiles{$filename} > 1) { - $includedfiles{$filename}--; - $dups++; - } else { - print {$f} $_; - } - } - } - } else { - print {$f} $_; - } - } - if ($dups > 0) { - print "$arg: removed $dups duplicate includes \n"; - } elsif (!$quiet) { - print "$arg: no duplicated includes to remove \n"; - } - close($f); - } + my $dups = 0; + foreach(@file_lines) { + if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { + foreach my $filename (keys %includedfiles) { + if ($1 eq $filename) { + if ($includedfiles{$filename} > 1) { + $includedfiles{$filename}--; + $dups++; + } else { + print {$f} $_; + } + } + } + } else { + print {$f} $_; + } + } + if ($dups > 0) { + print "$arg: removed $dups duplicate includes \n"; + } elsif (!$quiet) { + print "$arg: no duplicated includes to remove \n"; + } + close($f); + } } if ($argc == 0) { - usage(); + usage(); }