update checkpatch to v0.31
Andy Whitcroft
maintainercover update checkpatch to v0.31
After a long hiatus here is a batch of checkpatch updates from my tree. I have resynced with the version in v2.6.36-rc1 and added tests for all of the changes therein. This has thrown up a couple of regressions introduced in those changes. This update includes fixes for those, as below:
checkpatch: fix regressions in "fix handling of leading spaces"
checkpatch: ensure kconfig help checks only apply when we are adding help
The rest of my commits mostly fixes for false positives reported directly to me. There are also a couple of fixes from Joe and Rabin which I've integrated and tested.
Joe et al thanks for chiming in on many of the pending threads, most helpful.
Complete changelog below.
-apw
Andy Whitcroft (14):
checkpatch: fix regressions in "fix handling of leading spaces"
checkpatch: types may sit on a line on their own
checkpatch: suggest cleanpatch and cleanfile when appropriate
checkpatch: ensure we do not collapse bracketed sections into constants
checkpatch: handle casts better fixing false categorisation of : as binary
checkpatch: returning errno typically should be negative
checkpatch: add check for space after struct, union, and enum
checkpatch: simplify and consolidate "missing space after" checks
checkpatch: ensure kconfig help checks only apply when we are adding help
checkpatch: update copyright dates
checkpatch: clean up structure definition macro handline
checkpatch: handle EXPORT_SYMBOL for DEVICE_ATTR and similar
checkpatch: statement/block context analyser should look at sanitised lines
checkpatch: version 0.31
Joe Perches (1):
checkpatch: Add additional attribute #defines
Rabin Vincent (1):
checkpatch: check for incorrect permissions
scripts/checkpatch.pl | 133 ++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 109 insertions(+), 24 deletions(-)
Andy Whitcroft
maintainerPATCH 1/16 checkpatch: fix regressions in "fix handling of leading spaces"
The patch below added checks for leading spaces on lines, but this introduces regressions. Firstly it does not correctly detect when we are in a comment. Secondly it does not allow for preprocessor command spacing. Finally it does not allow for label indentation which is required to be less than one tab. Fix these up:
checkpatch: fix handling of leading spaces:
--- scripts/checkpatch.pl | 9 ++++++--- 1 files changed, 6 insertions(+), 3 deletions(-)
scripts/checkpatch.pl +6 -3
@@ -1459,10 +1459,13 @@ sub process { } # check for spaces at the beginning of a line. - if ($rawline =~ /^\+ / && $rawline !~ /\+ +\*/) { +# Exceptions: +# 1) within comments +# 2) indented preprocessor commands +# 3) hanging labels + if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) { my $herevet = "$here\n" . cat_vet($rawline) . "\n"; - WARN("please, no space for starting a line, \ - excluding comments\n" . $herevet); + WARN("please, no spaces at the start of a line\n" . $herevet); } # check we are in a valid C source file if not then ignore this hunk
Signature · 1 line
1.7.0.4
Andy Whitcroft
maintainerPATCH 2/16 checkpatch: types may sit on a line on their own
In reply to Andy WhitcroftWhen the following form is used we have a type which fully fills a line. This means that a type may end at the end of line as well as at the following identifier.
int ** foo;
--- scripts/checkpatch.pl | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
scripts/checkpatch.pl +1 -1
@@ -843,7 +843,7 @@ sub annotate_values { $av_preprocessor = 0; } - } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) { + } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { print "DECLARE($1)\n" if ($dbg_values > 1); $type = 'T';
Signature · 1 line
1.7.0.4
Andy Whitcroft
maintainerPATCH 3/16 checkpatch: suggest cleanpatch and cleanfile when appropriate
In reply to Andy WhitcroftWhen we hit types of whitespace which may be fixed by scripts/cleanpatch and scripts/cleanfile suggest their use in our report.
--- scripts/checkpatch.pl | 13 +++++++++++++ 1 files changed, 13 insertions(+), 0 deletions(-)
scripts/checkpatch.pl +13 -0
@@ -103,6 +103,8 @@ for my $key (keys %debug) { die "$@" if ($@); } +my $rpt_cleaners = 0; + if ($terse) { $emacs = 1; $quiet++;
@@ -1389,6 +1391,7 @@ sub process { } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { my $herevet = "$here\n" . cat_vet($rawline) . "\n"; ERROR("trailing whitespace\n" . $herevet); + $rpt_cleaners = 1; } # check for Kconfig help text having a real description
@@ -1450,6 +1453,7 @@ sub process { $rawline =~ /^\+\s* \s*/) { my $herevet = "$here\n" . cat_vet($rawline) . "\n"; ERROR("code indent should use tabs where possible\n" . $herevet); + $rpt_cleaners = 1; } # check for space before tabs.
@@ -2842,6 +2846,15 @@ sub process { print "\n" if ($quiet == 0); } + if ($quiet == 0) { + # If there were whitespace errors which cleanpatch can fix + # then suggest that. + if ($rpt_cleaners) { + print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; + print " scripts/cleanfile\n\n"; + } + } + if ($clean == 1 && $quiet == 0) { print "$vname has no obvious style problems and is ready for submission.\n" }
Signature · 1 line
1.7.0.4
Andy Whitcroft
maintainerPATCH 4/16 checkpatch: ensure we do not collapse bracketed sections into constants
In reply to Andy WhitcroftWhen determining if a return () sequence is a function style bracketing we simplify the expression one bracket at a time replacing each with a constant. However this can trigger a false merge with expressions as below:
return (foo)0;
Prevent this false merging.
--- scripts/checkpatch.pl | 7 ++++--- 1 files changed, 4 insertions(+), 3 deletions(-)
scripts/checkpatch.pl +4 -3
@@ -2183,15 +2183,16 @@ sub process { my $value = $2; # Flatten any parentheses - $value =~ s/\)\(/\) \(/g; + $value =~ s/\(/ \(/g; + $value =~ s/\)/\) /g; while ($value =~ s/\[[^\{\}]*\]/1/ || $value !~ /(?:$Ident|-?$Constant)\s* $Compare\s* (?:$Ident|-?$Constant)/x && $value =~ s/\([^\(\)]*\)/1/) { } - - if ($value =~ /^(?:$Ident|-?$Constant)$/) { +#print "value<$value>\n"; + if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) { ERROR("return is not a function, parentheses are not required\n" . $herecurr); } elsif ($spacing !~ /\s+/) {
Signature · 1 line
1.7.0.4
Andy Whitcroft
maintainerPATCH 5/16 checkpatch: handle casts better fixing false categorisation of : as binary
In reply to Andy WhitcroftThe following incantation is triggering categorisation of its colon (:) as a binary form, which it is not:
return foo ? (s8)bar : baz;
Handle casts differently from types in the categoriser, allowing us to better track (s8)bar as a value and not a declaration.
--- scripts/checkpatch.pl | 5 +++++ 1 files changed, 5 insertions(+), 0 deletions(-)
scripts/checkpatch.pl +5 -0
@@ -845,6 +845,11 @@ sub annotate_values { $av_preprocessor = 0; } + } elsif ($cur =~ /^(\(\s*$Type\s*)\)/) { + print "CAST($1)\n" if ($dbg_values > 1); + push(@av_paren_type, $type); + $type = 'C'; + } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { print "DECLARE($1)\n" if ($dbg_values > 1); $type = 'T';
Signature · 1 line
1.7.0.4
Andy Whitcroft
maintainerPATCH 6/16 checkpatch: returning errno typically should be negative
In reply to Andy WhitcroftAdd a (strict mode only) test to check for non-negative returns of what appear to be errno values as the majority case these should indeed be negative.
--- scripts/checkpatch.pl | 7 +++++++ 1 files changed, 7 insertions(+), 0 deletions(-)
scripts/checkpatch.pl +7 -0
@@ -2204,6 +2204,13 @@ sub process { ERROR("space required before the open parenthesis '('\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; + if ($name ne 'EOF' && $name ne 'ERROR') { + WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr); + } + } # Need a space before open parenthesis after if, while etc if ($line=~/\b(if|while|for|switch)\(/) {
Signature · 1 line
1.7.0.4
Andy Whitcroft
maintainerPATCH 7/16 checkpatch: add check for space after struct, union, and enum
In reply to Andy WhitcroftAdd spacing checks for struct, union, and enum definitions. Check the spacing after type and before the equals (=) and open brace ({).
Based on a patch by Joe Perches <[email protected]>.
--- scripts/checkpatch.pl | 10 ++++++++++ 1 files changed, 10 insertions(+), 0 deletions(-)
scripts/checkpatch.pl +10 -0
@@ -1905,6 +1905,16 @@ sub process { ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); } +# missing space after union or struct definition + if ($rawline =~ /^\+\s*(union|struct)\s+$Ident[=\{]/) { + WARN("Missing space after struct or union definition\n" . $herecurr); + } + +# missing space after enum definition + if ($rawline =~ /^\+\s*enum\{/) { + WARN("Missing space after enum definition\n" . $herecurr); + } + # open braces for enum, union and struct go on the same line. if ($line =~ /^.\s*{/ && $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Signature · 1 line
1.7.0.4
Andy Whitcroft
maintainerPATCH 8/16 checkpatch: simplify and consolidate "missing space after" checks
In reply to Andy WhitcroftCommonise the code for missing spaces after struct, union, and enum such that they share the same code. Ensure we cover all the common cases in each case. Check against the sanitised line to ensure we do not report on comments and strings.
--- scripts/checkpatch.pl | 15 +++++---------- 1 files changed, 5 insertions(+), 10 deletions(-)
scripts/checkpatch.pl +5 -10
@@ -1905,22 +1905,17 @@ sub process { ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); } -# missing space after union or struct definition - if ($rawline =~ /^\+\s*(union|struct)\s+$Ident[=\{]/) { - WARN("Missing space after struct or union definition\n" . $herecurr); - } - -# missing space after enum definition - if ($rawline =~ /^\+\s*enum\{/) { - WARN("Missing space after enum definition\n" . $herecurr); - } - # open braces for enum, union and struct go on the same line. if ($line =~ /^.\s*{/ && $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { ERROR("open brace '{' following $1 go on the same line\n" . $hereprev); } +# missing space after union, struct or enum definition + if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) { + WARN("missing space after $1 definition\n" . $herecurr); + } + # 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.7.0.4
Andy Whitcroft
maintainerPATCH 9/16 checkpatch: ensure kconfig help checks only apply when we are adding help
In reply to Andy WhitcroftWhen checking the length of the help we need to be sure we are seeing the whole story before erroring. Firstly we only want to check when adding the help in the first place. Second we need to be sure that we are seeing the end of the entry, nominally when there is no context below or that context shows the start of the next entry.
--- scripts/checkpatch.pl | 26 +++++++++++++++++++++----- 1 files changed, 21 insertions(+), 5 deletions(-)
scripts/checkpatch.pl +21 -5
@@ -1400,18 +1400,34 @@ sub process { } # check for Kconfig help text having a real description +# Only applies when adding the entry originally, after that we do not have +# sufficient context to determine whether it is indeed long enough. if ($realfile =~ /Kconfig/ && - $line =~ /\+?\s*(---)?help(---)?$/) { + $line =~ /\+\s*(?:---)?help(?:---)?$/) { my $length = 0; - for (my $l = $linenr; defined($lines[$l]); $l++) { - my $f = $lines[$l]; + my $cnt = $realcnt; + my $ln = $linenr + 1; + my $f; + my $is_end = 0; + while ($cnt > 0 && defined $lines[$ln - 1]) { + $f = $lines[$ln - 1]; + $cnt-- if ($lines[$ln - 1] !~ /^-/); + $is_end = $lines[$ln - 1] =~ /^\+/; + $ln++; + + next if ($f =~ /^-/); + $f =~ s/^.//; $f =~ s/#.*//; $f =~ s/^\s+//; next if ($f =~ /^$/); - last if ($f =~ /^\s*config\s/); + if ($f =~ /^\s*config\s/) { + $is_end = 1; + last; + } $length++; } - WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($length < 4); + WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4); + #print "is_end<$is_end> length<$length>\n"; } # check we are in a valid source file if not then ignore this hunk
Signature · 1 line
1.7.0.4
Andy Whitcroft
maintainerPATCH 10/16 checkpatch: check for incorrect permissions
In reply to Andy WhitcroftFrom: Rabin Vincent <[email protected]>
Throw an error when a source file has been given execute permissions using the mode change line present in git diffs. Also alow the filename matching to use the "diff" line in addition to the "+++" line, since the mode change lines appear before any "+++" lines.
--- scripts/checkpatch.pl | 14 +++++++++++++- 1 files changed, 13 insertions(+), 1 deletions(-)
scripts/checkpatch.pl +13 -1
@@ -1315,7 +1315,11 @@ sub process { $here = "#$realline: " if ($file); # extract the filename as it passes - if ($line=~/^\+\+\+\s+(\S+)/) { + if ($line =~ /^diff --git.*?(\S+)$/) { + $realfile = $1; + $realfile =~ s@^([^/]*)/@@; + + } elsif ($line =~ /^\+\+\+\s+(\S+)/) { $realfile = $1; $realfile =~ s@^([^/]*)/@@;
@@ -1339,6 +1343,14 @@ sub process { $cnt_lines++ if ($realcnt != 0); +# Check for incorrect file permissions + if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { + my $permhere = $here . "FILE: $realfile\n"; + if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) { + ERROR("do not set execute permissions for source files\n" . $permhere); + } + } + #check the patch for a signoff: if ($line =~ /^\s*signed-off-by:/i) { # This is a signoff, if ugly, so do not double report.
Signature · 1 line
1.7.0.4
Andy Whitcroft
maintainerPATCH 11/16 checkpatch: Add additional attribute #defines
In reply to Andy WhitcroftFrom: Joe Perches <[email protected]>
Quoted 7 lines · Dave Jones
I just got this from a patch I merged..
ERROR: need consistent spacing around '*' (ctx:WxV) #121: FILE:
arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c:113:+static struct pcc_cpu __percpu *pcc_cpu_info; ^which doesn't seem right.
Perhaps these need to be added to checkpatch.
--- scripts/checkpatch.pl | 14 ++++++++++++++ 1 files changed, 14 insertions(+), 0 deletions(-)
scripts/checkpatch.pl +14 -0
@@ -152,6 +152,20 @@ our $Sparse = qr{ # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check our $Attribute = qr{ const| + __percpu| + __nocast| + __safe| + __bitwise__| + __packed__| + __packed2__| + __naked| + __maybe_unused| + __always_unused| + __noreturn| + __used| + __cold| + __noclone| + __deprecated| __read_mostly| __kprobes| __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Signature · 1 line
1.7.0.4
Andy Whitcroft
maintainerPATCH 12/16 checkpatch: update copyright dates
In reply to Andy Whitcroft--- scripts/checkpatch.pl | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
scripts/checkpatch.pl +1 -1
@@ -2,7 +2,7 @@ # (c) 2001, Dave Jones. (the file handling bit) # (c) 2005, Joel Schopp <[email protected]> (the ugly bit) # (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite) -# (c) 2008,2009, Andy Whitcroft <[email protected]> +# (c) 2008-2010 Andy Whitcroft <[email protected]> # Licensed under the terms of the GNU GPL License version 2 use strict;
Signature · 1 line
1.7.0.4
Andy Whitcroft
maintainerPATCH 13/16 checkpatch: clean up structure definition macro handline
In reply to Andy WhitcroftHandle definitions such as the following correctly, it is not a complex statement:
#define PREALLOC(NAME, START, END, FLAGS) { \
.name = (NAME), \
.start = (START), \
.end = (END), \
.flags = (FLAGS) \
},--- scripts/checkpatch.pl | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +2 -2
@@ -2476,8 +2476,8 @@ sub process { \.$Ident\s*=\s*| ^\"|\"$ }x; - #print "REST<$rest> dstat<$dstat>\n"; - if ($rest ne '') { + #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; + if ($rest ne '' && $rest ne ',') { if ($rest !~ /while\s*\(/ && $dstat !~ /$exceptions/) {
Signature · 1 line
1.7.0.4
Andy Whitcroft
maintainerPATCH 14/16 checkpatch: handle EXPORT_SYMBOL for DEVICE_ATTR and similar
In reply to Andy WhitcroftHandly definitions similar to below. The definition macro spits out a symbol with a prefix. Add matching of any identifier prefix:
DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
ata_scsi_lpm_show, ata_scsi_lpm_put);
EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);--- scripts/checkpatch.pl | 11 ++++++++++- 1 files changed, 10 insertions(+), 1 deletions(-)
scripts/checkpatch.pl +10 -1
@@ -1822,8 +1822,17 @@ sub process { !defined $suppress_export{$realline_next} && ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { + # Handle definitions which produce identifiers with + # a prefix: + # XXX(foo); + # EXPORT_SYMBOL(something_foo); my $name = $1; - if ($stat !~ /(?: + if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ && + $name =~ /^${Ident}_$2/) { +#print "FOO C name<$name>\n"; + $suppress_export{$realline_next} = 1; + + } elsif ($stat !~ /(?: \n.}\s*$| ^.DEFINE_$Ident\(\Q$name\E\)| ^.DECLARE_$Ident\(\Q$name\E\)|
Signature · 1 line
1.7.0.4
Andy Whitcroft
maintainerPATCH 15/16 checkpatch: statement/block context analyser should look at sanitised lines
In reply to Andy WhitcroftWhen tracking context to find a block or statement we need to use the sanitised lines, else perentheses '(' & ')' and braces '{' & '}' can throw the scanner out. Also fix up a couple of error outputs which include those sanitised lines incorrectly.
--- scripts/checkpatch.pl | 12 ++++++------ 1 files changed, 6 insertions(+), 6 deletions(-)
scripts/checkpatch.pl +6 -6
@@ -691,15 +691,15 @@ sub ctx_block_get { $blk .= $rawlines[$line]; # Handle nested #if/#else. - if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { + if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { push(@stack, $level); - } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { + } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { $level = $stack[$#stack - 1]; - } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) { + } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { $level = pop(@stack); } - foreach my $c (split(//, $rawlines[$line])) { + foreach my $c (split(//, $lines[$line])) { ##print "C<$c>L<$level><$open$close>O<$off>\n"; if ($off > 0) { $off--;
@@ -1652,7 +1652,7 @@ sub process { if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { ERROR("that open brace { should be on the previous line\n" . - "$here\n$ctx\n$lines[$ctx_ln - 1]\n"); + "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); } if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && $ctx =~ /\)\s*\;\s*$/ &&
@@ -1661,7 +1661,7 @@ sub process { my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); if ($nindent > $indent) { WARN("trailing semicolon indicates no statements, indent implies otherwise\n" . - "$here\n$ctx\n$lines[$ctx_ln - 1]\n"); + "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); } } }
Signature · 1 line
1.7.0.4
Andy Whitcroft
maintainerPATCH 16/16 checkpatch: version 0.31
In reply to Andy Whitcroft--- scripts/checkpatch.pl | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
scripts/checkpatch.pl +1 -1
@@ -10,7 +10,7 @@ use strict; my $P = $0; $P =~ s@.*/@@g; -my $V = '0.30'; +my $V = '0.31'; use Getopt::Long qw(:config no_auto_abbrev);
Signature · 1 line
1.7.0.4