scripts/checkpatch.pl | 70 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 14 deletions(-) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index fdacd759078e..dd344ac77cb8 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -2114,6 +2114,29 @@ sub pos_last_openparen { return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; } +sub get_decl { + my ($line) = @_; + + # typical declarations + if ($line =~ /^\+\s+($Declare\s*$Ident)\s*[=,;:\[]/) { + return $1; + } + # function pointer declarations + if ($line =~ /^\+\s+($Declare\s*\(\s*\*\s*$Ident\s*\))\s*[=,;:\[\(]/) { + return $1; + } + # foo bar; where foo is some local typedef or #define + if ($line =~ /^\+\s+($Ident(?:\s+|\s*\*\s*)$Ident)\s*[=,;\[]/) { + return $1; + } + # known declaration macros + if ($line =~ /^\+\s+$(declaration_macros)/) { + return $1; + } + + return undef; +} + sub process { my $filename = shift; @@ -3063,9 +3086,7 @@ sub process { $last_blank_line = $linenr; } -# check for missing blank lines after declarations - if ($sline =~ /^\+\s+\S/ && #Not at char 1 - # actual declarations + my $prev_decl = ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || # function pointer declarations $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || @@ -3078,25 +3099,30 @@ sub process { # other possible extensions of declaration lines $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || # not starting a section or a macro "\" extended line - $prevline =~ /(?:\{\s*|\\)$/) && - # looks like a declaration - !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || + $prevline =~ /(?:\{\s*|\\)$/); + my $sline_decl = + $sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || # function pointer declarations - $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || + $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || # foo bar; where foo is some local typedef or #define - $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || + $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || # known declaration macros - $sline =~ /^\+\s+$declaration_macros/ || + $sline =~ /^\+\s+$declaration_macros/ || # start of struct or union or enum - $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ || + $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ || # start or end of block or continuation of declaration - $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || + $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || # bitfield continuation - $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || + $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || # other possible extensions of declaration lines - $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && + $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/; + +# check for missing blank lines after declarations + if ($sline =~ /^\+\s+\S/ && #Not at char 1 + # actual declarations + $prev_decl && !$sline_decl && # indentation of previous and current line are the same - (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { + ($prevline =~ /\+(\s+)\S/ && $sline =~ /^\+$1\S/)) { if (WARN("LINE_SPACING", "Missing a blank line after declarations\n" . $hereprev) && $fix) { @@ -3104,6 +3130,22 @@ sub process { } } +# check for reverse christmas tree declarations in net/ and drivers/net/ + if ($realfile =~ m@^(?:drivers/net/|net/)@ && + $sline =~ /^\+\s+\S/ && #Not at char 1 + # actual declarations + $prev_decl && $sline_decl && + # indentation of previous and current line are the same + (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { + my $p = get_decl($prevline); + my $l = get_decl($sline); + if (defined($p) && defined($l) && length($p) < length($l) && + CHK("REVERSE_XMAS_TREE", + "Prefer ordering declarations longest to shortest\n" . $hereprev) && + $fix) { + } + } + # check for spaces at the beginning of a line. # Exceptions: # 1) within comments