checkpatch: A few corrections and updates
Joe Perches
maintainercover checkpatch: A few corrections and updates
Joe Perches (5):
checkpatch: Add tests for function pointer style misuses
checkpatch: Add a --fix-inplace option
checkpatch: Improve space before tab --fix option
checkpatch: check for if's with unnecessary parentheses
checkpatch: Update the FSF/GPL address check
scripts/checkpatch.pl | 100 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 92 insertions(+), 8 deletions(-)
Signature · 1 line
1.8.1.2.459.gbcd45b4.dirty
Joe Perches
maintainerPATCH 1/5 checkpatch: Add tests for function pointer style misuses
Kernel style uses function pointers in this form:
"type (*funcptr)(args...)"
Emit warnings when this function pointer form isn't used.
--- scripts/checkpatch.pl | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+)
scripts/checkpatch.pl +59 -0
@@ -2810,6 +2810,65 @@ sub process { } } +# Function pointer declarations +# check spacing between type, funcptr, and args +# canonical declaration is "type (*funcptr)(args...)" +# +# the $Declare variable will capture all spaces after the type +# so check it for multiple spaces + if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)$Ident(\s*)\)(\s*)\(/) { + my $declare = $1; + my $pre_pointer_space = $2; + my $post_pointer_space = $3; + my $funcname = $4; + my $post_funcname_space = $5; + my $pre_args_space = $6; + + if ($declare !~ /\s$/) { + WARN("SPACING", + "missing space after return type\n" . $herecurr); + } + +# unnecessary space "type (*funcptr)(args...)" + elsif ($declare =~ /\s{2,}$/) { + WARN("SPACING", + "Multiple spaces after return type\n" . $herecurr); + } + +# unnecessary space "type ( *funcptr)(args...)" + if (defined $pre_pointer_space && + $pre_pointer_space =~ /^\s/) { + WARN("SPACING", + "Unnecessary space after function pointer open parenthesis\n" . $herecurr); + } + +# unnecessary space "type (* funcptr)(args...)" + if (defined $post_pointer_space && + $post_pointer_space =~ /^\s/) { + WARN("SPACING", + "Unnecessary space before function pointer name\n" . $herecurr); + } + +# unnecessary space "type (*funcptr )(args...)" + if (defined $post_funcname_space && + $post_funcname_space =~ /^\s/) { + WARN("SPACING", + "Unnecessary space after function pointer name\n" . $herecurr); + } + +# unnecessary space "type (*funcptr) (args...)" + if (defined $pre_args_space && + $pre_args_space =~ /^\s/) { + WARN("SPACING", + "Unnecessary space before function pointer name\n" . $herecurr); + } + + if (show_type("SPACING") && $fix) { + $fixed[$linenr - 1] =~ + s/^(.\s*$Declare)\(\s*\*\s*($Ident)\s*\)\s*\(/rtrim($1) . " " . "\(\*$2\)\("/ex; + } + } + # check for spacing round square brackets; allowed: # 1. with a type on the left -- int [] a; # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
Signature · 1 line
1.8.1.2.459.gbcd45b4.dirty
Joe Perches
maintainerPATCH 3/5 checkpatch: Improve space before tab --fix option
In reply to Joe PerchesThis test should remove all the spaces before a tab not just one space.
Substitute a tab for each 8 space block before a tab and remove less than 8 spaces before a tab.
This SPACE_BEFORE_TAB test is done after CODE_INDENT.
If there are spaces used at the beginning of a line that should be converted to tabs, please make sure that the CODE_INDENT test and conversion is done before this SPACE_BEFORE_TAB test and conversion.
--- scripts/checkpatch.pl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +4 -2
@@ -2118,8 +2118,10 @@ sub process { if (WARN("SPACE_BEFORE_TAB", "please, no space before tabs\n" . $herevet) && $fix) { - $fixed[$linenr - 1] =~ - s/(^\+.*) +\t/$1\t/; + while ($fixed[$linenr - 1] =~ + s/(^\+.*) {8,8}+\t/$1\t\t/) {} + while ($fixed[$linenr - 1] =~ + s/(^\+.*) +\t/$1\t/) {} } }
Signature · 1 line
1.8.1.2.459.gbcd45b4.dirty
Joe Perches
maintainerPATCH 4/5 checkpatch: check for if's with unnecessary parentheses
In reply to Joe PerchesIf statements don't need multiple parentheses around tested comparisons like "if ((foo == bar))".
An == comparison maybe a sign of an intended assignment, so emit a slightly different message if so.
--- scripts/checkpatch.pl | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
scripts/checkpatch.pl +14 -0
@@ -3322,6 +3322,20 @@ sub process { } } +# if statements using unnecessary parentheses - ie: if ((foo == bar)) + if ($^V && $^V ge 5.10.0 && + $line =~ /\bif\s*((?:\(\s*){2,})/) { + my $openparens = $1; + my $count = $openparens =~ tr@\(@\(@; + my $msg = ""; + if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { + my $comp = $4; #Not $1 because of $LvalOrFunc + $msg = " - maybe == should be = ?" if ($comp eq "=="); + WARN("UNNECESSARY_PARENTHESES", + "Unnecessary parentheses$msg\n" . $herecurr); + } + } + # Return of what appears to be an errno should normally be -'ve if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { my $name = $1;
Signature · 1 line
1.8.1.2.459.gbcd45b4.dirty
Joe Perches
maintainerPATCH 5/5 checkpatch: Update the FSF/GPL address check
In reply to Joe PerchesThe FSF address check is a bit too verbose looking for the GPL text. Quiet it a bit by requiring --strict for the GPL bit.
Also make the address tests match a few uses of abbreviations for street names and make it case insensitive.
--- scripts/checkpatch.pl | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
scripts/checkpatch.pl +6 -5
@@ -1970,15 +1970,16 @@ sub process { } # Check for FSF mailing addresses. - if ($rawline =~ /You should have received a copy/ || - $rawline =~ /write to the Free Software/ || - $rawline =~ /59 Temple Place/ || - $rawline =~ /51 Franklin Street/) { + if ($rawline =~ /\bYou should have received a copy/i || + $rawline =~ /\bwrite to the Free/i || + $rawline =~ /\b59\s+Temple\s+Pl/i || + $rawline =~ /\b51\s+Franklin\s+St/i) { my $herevet = "$here\n" . cat_vet($rawline) . "\n"; my $msg_type = \&ERROR; $msg_type = \&CHK if ($file); + $msg_type = \&CHK if ($rawline =~ /\bYou should have received a copy/i); &{$msg_type}("FSF_MAILING_ADDRESS", - "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet) + "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet) } # check for Kconfig help text having a real description
Signature · 1 line
1.8.1.2.459.gbcd45b4.dirty
Joe Perches
maintainerPATCH 2/5 checkpatch: Add a --fix-inplace option
In reply to Joe PerchesAdd the ability to fix and overwrite existing files/patches instead of creating a new file "<filename>.EXPERIMENTAL-checkpatch-fixes".
--- scripts/checkpatch.pl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
scripts/checkpatch.pl +9 -1
@@ -29,6 +29,7 @@ my $mailback = 0; my $summary_file = 0; my $show_types = 0; my $fix = 0; +my $fix_inplace = 0; my $root; my %debug; my %camelcase = ();
@@ -76,6 +77,9 @@ Options: "<inputfile>.EXPERIMENTAL-checkpatch-fixes" with potential errors corrected to the preferred checkpatch style + --fix-inplace EXPERIMENTAL - may create horrible results + Is the same as --fix, but overwrites the input + file. It's your fault if there's no backup or git --ignore-perl-version override checking of perl version. expect runtime errors. -h, --help, --version display this help and exit
@@ -131,6 +135,7 @@ GetOptions( 'mailback!' => \$mailback, 'summary-file!' => \$summary_file, 'fix!' => \$fix, + 'fix-inplace!' => \$fix_inplace, 'ignore-perl-version!' => \$ignore_perl_version, 'debug=s' => \%debug, 'test-only=s' => \$tst_only,
@@ -140,6 +145,8 @@ GetOptions( help(0) if ($help); +$fix = 1 if ($fix_inplace); + my $exit = 0; if ($^V && $^V lt $minimum_perl_version) {
@@ -4456,7 +4463,8 @@ sub process { hash_show_words(\%ignore_type, "Ignored"); if ($clean == 0 && $fix && "@rawlines" ne "@fixed") { - my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes"; + my $newfile = $filename; + $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); my $linecount = 0; my $f;
Signature · 1 line
1.8.1.2.459.gbcd45b4.dirty
Re: [PATCH 1/5] checkpatch: Add tests for function pointer style misuses
In reply to Joe PerchesQuoted 6 lines · Joe Perches
Kernel style uses function pointers in this form:
"type (*funcptr)(args...)"Emit warnings when this function pointer form isn't used.
Signed-off-by Joe Perches <[email protected]>
Two comments below.
Quoted 31 lines
scripts/checkpatch.pl | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+)scripts/checkpatch.pl +20
-0@@ -2810,6 +2810,65 @@ sub process { } } +# Function pointer declarations +# check spacing between type, funcptr, and args +# canonical declaration is "type (*funcptr)(args...)" +# +# the $Declare variable will capture all spaces after the type +# so check it for multiple spaces + if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)$Ident(\s*)\)(\s*)\(/) { + my $declare = $1; + my $pre_pointer_space = $2; + my $post_pointer_space = $3; + my $funcname = $4; + my $post_funcname_space = $5; + my $pre_args_space = $6; + + if ($declare !~ /\s$/) { + WARN("SPACING", + "missing space after return type\n" . $herecurr); + } + +# unnecessary space "type (*funcptr)(args...)"
scripts/checkpatch.pl sub process { · line 2842
Quoted 10 lines
scripts/checkpatch.pl +10
-0+ elsif ($declare =~ /\s{2,}$/) { + WARN("SPACING", + "Multiple spaces after return type\n" . $herecurr); + } + +# unnecessary space "type ( *funcptr)(args...)" + if (defined $pre_pointer_space && + $pre_pointer_space =~ /^\s/) { + WARN("SPACING", + "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
There are two parentheticals in a proper function pointer declaration, so this is slightly ambiguous. Perhaps "between open parenthesis and '*' in function pointer declaration"?
Quoted 9 lines
scripts/checkpatch.pl +9
-0+# unnecessary space "type (* funcptr)(args...)" + if (defined $post_pointer_space && + $post_pointer_space =~ /^\s/) { + WARN("SPACING", + "Unnecessary space before function pointer name\n" . $herecurr); + } + +# unnecessary space "type (*funcptr )(args...)" + if (defined $post_funcname_space &&
scripts/checkpatch.pl sub process { · line 2863
Quoted 10 lines
scripts/checkpatch.pl +10
-0+ $post_funcname_space =~ /^\s/) { + WARN("SPACING", + "Unnecessary space after function pointer name\n" . $herecurr); + } + +# unnecessary space "type (*funcptr) (args...)" + if (defined $pre_args_space && + $pre_args_space =~ /^\s/) { + WARN("SPACING", + "Unnecessary space before function pointer name\n" . $herecurr);
Copy/paste problem here? I think this should be "Unnecessary space before function pointer argument types" or similar.
With both of the above two issues fixed:
Re: [PATCH 2/5] checkpatch: Add a --fix-inplace option
In reply to Joe PerchesQuoted 5 lines · Joe Perches
Add the ability to fix and overwrite existing files/patches instead of creating a new file "<filename>.EXPERIMENTAL-checkpatch-fixes".
Suggested-by Manfred Spraul <[email protected]>Signed-off-by Joe Perches <[email protected]>
Quoted 55 lines
scripts/checkpatch.pl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)scripts/checkpatch.pl +9
-1@@ -29,6 +29,7 @@ my $mailback = 0; my $summary_file = 0; my $show_types = 0; my $fix = 0; +my $fix_inplace = 0; my $root; my %debug; my %camelcase = ();@@ -76,6 +77,9 @@ Options: "<inputfile>.EXPERIMENTAL-checkpatch-fixes" with potential errors corrected to the preferred checkpatch style + --fix-inplace EXPERIMENTAL - may create horrible results + Is the same as --fix, but overwrites the input + file. It's your fault if there's no backup or git --ignore-perl-version override checking of perl version. expect runtime errors. -h, --help, --version display this help and exit@@ -131,6 +135,7 @@ GetOptions( 'mailback!' => \$mailback, 'summary-file!' => \$summary_file, 'fix!' => \$fix, + 'fix-inplace!' => \$fix_inplace, 'ignore-perl-version!' => \$ignore_perl_version, 'debug=s' => \%debug, 'test-only=s' => \$tst_only,@@ -140,6 +145,8 @@ GetOptions( help(0) if ($help); +$fix = 1 if ($fix_inplace); + my $exit = 0; if ($^V && $^V lt $minimum_perl_version) {@@ -4456,7 +4463,8 @@ sub process { hash_show_words(\%ignore_type, "Ignored"); if ($clean == 0 && $fix && "@rawlines" ne "@fixed") { - my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes"; + my $newfile = $filename; + $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); my $linecount = 0; my $f;Signature · 1 line
1.8.1.2.459.gbcd45b4.dirty
Re: [PATCH 4/5] checkpatch: check for if's with unnecessary parentheses
In reply to Joe PerchesQuoted 7 lines · Joe Perches
If statements don't need multiple parentheses around tested comparisons like "if ((foo == bar))".
An == comparison maybe a sign of an intended assignment, so emit a slightly different message if so.
Signed-off-by Joe Perches <[email protected]>
Quoted 31 lines
scripts/checkpatch.pl | 14 ++++++++++++++ 1 file changed, 14 insertions(+)scripts/checkpatch.pl +14
-0@@ -3322,6 +3322,20 @@ sub process { } } +# if statements using unnecessary parentheses - ie: if ((foo == bar)) + if ($^V && $^V ge 5.10.0 && + $line =~ /\bif\s*((?:\(\s*){2,})/) { + my $openparens = $1; + my $count = $openparens =~ tr@\(@\(@; + my $msg = ""; + if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { + my $comp = $4; #Not $1 because of $LvalOrFunc + $msg = " - maybe == should be = ?" if ($comp eq "=="); + WARN("UNNECESSARY_PARENTHESES", + "Unnecessary parentheses$msg\n" . $herecurr); + } + } + # Return of what appears to be an errno should normally be -'ve if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { my $name = $1;Signature · 1 line
1.8.1.2.459.gbcd45b4.dirty