Skip to content

Commit b5e8736

Browse files
JoePerchestorvalds
authored andcommitted
checkpatch: improve blank line after declaration test
Avoid multiple false positives by ignoring attributes. Various attributes like volatile and ____cacheline_aligned_in_smp cause checkpatch to emit invalid "Missing a blank line after declarations" messages. Use copies of $sline and $prevline, remove $Attribute and $Sparse, and use the existing tests to avoid these false positives. Miscellanea: o Add volatile to $Attribute This also reduces checkpatch runtime a bit by moving the indentation comparison test to the start of the block to avoid multiple unnecessary regex tests. Link: https://lkml.kernel.org/r/9015fd00742bf4e5b824ad6d7fd7189530958548.camel@perches.com Signed-off-by: Joe Perches <joe@perches.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 4945cca commit b5e8736

1 file changed

Lines changed: 29 additions & 23 deletions

File tree

‎scripts/checkpatch.pl‎

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ sub hash_show_words {
382382
# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
383383
our $Attribute = qr{
384384
const|
385+
volatile|
385386
__percpu|
386387
__nocast|
387388
__safe|
@@ -3776,43 +3777,48 @@ sub process {
37763777
}
37773778

37783779
# check for missing blank lines after declarations
3779-
if ($sline =~ /^\+\s+\S/ && #Not at char 1
3780-
# actual declarations
3781-
($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3780+
# (declarations must have the same indentation and not be at the start of line)
3781+
if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) {
3782+
# use temporaries
3783+
my $sl = $sline;
3784+
my $pl = $prevline;
3785+
# remove $Attribute/$Sparse uses to simplify comparisons
3786+
$sl =~ s/\b(?:$Attribute|$Sparse)\b//g;
3787+
$pl =~ s/\b(?:$Attribute|$Sparse)\b//g;
3788+
if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
37823789
# function pointer declarations
3783-
$prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3790+
$pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
37843791
# foo bar; where foo is some local typedef or #define
3785-
$prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3792+
$pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
37863793
# known declaration macros
3787-
$prevline =~ /^\+\s+$declaration_macros/) &&
3794+
$pl =~ /^\+\s+$declaration_macros/) &&
37883795
# for "else if" which can look like "$Ident $Ident"
3789-
!($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3796+
!($pl =~ /^\+\s+$c90_Keywords\b/ ||
37903797
# other possible extensions of declaration lines
3791-
$prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3798+
$pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
37923799
# not starting a section or a macro "\" extended line
3793-
$prevline =~ /(?:\{\s*|\\)$/) &&
3800+
$pl =~ /(?:\{\s*|\\)$/) &&
37943801
# looks like a declaration
3795-
!($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3802+
!($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
37963803
# function pointer declarations
3797-
$sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3804+
$sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
37983805
# foo bar; where foo is some local typedef or #define
3799-
$sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3806+
$sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
38003807
# known declaration macros
3801-
$sline =~ /^\+\s+$declaration_macros/ ||
3808+
$sl =~ /^\+\s+$declaration_macros/ ||
38023809
# start of struct or union or enum
3803-
$sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
3810+
$sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
38043811
# start or end of block or continuation of declaration
3805-
$sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3812+
$sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
38063813
# bitfield continuation
3807-
$sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3814+
$sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
38083815
# other possible extensions of declaration lines
3809-
$sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3810-
# indentation of previous and current line are the same
3811-
(($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3812-
if (WARN("LINE_SPACING",
3813-
"Missing a blank line after declarations\n" . $hereprev) &&
3814-
$fix) {
3815-
fix_insert_line($fixlinenr, "\+");
3816+
$sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) {
3817+
if (WARN("LINE_SPACING",
3818+
"Missing a blank line after declarations\n" . $hereprev) &&
3819+
$fix) {
3820+
fix_insert_line($fixlinenr, "\+");
3821+
}
38163822
}
38173823
}
38183824

0 commit comments

Comments
 (0)