diff options
| author | Joe Perches <joe@perches.com> | 2012-03-23 18:02:16 -0400 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-03-23 19:58:36 -0400 |
| commit | d1fe9c099cecc6e49324355f1b15573e9dbbe0f9 (patch) | |
| tree | 3b9fc1b4ac53907623b73e4967fed8b1ead12a58 /scripts | |
| parent | 6061d949dd984c762ee272a88e77699fa675d1c8 (diff) | |
checkpatch: add some --strict coding style checks
Argument alignment across multiple lines should match the open
parenthesis.
Logical continuations should be at the end of the previous line, not the
start of a new line.
These are not required by CodingStyle so make the tests active only when
using --strict.
Improved by some examples from Bruce Allen.
Signed-off-by: Joe Perches <joe@perches.com>
Cc: "Bruce W. Allen" <bruce.w.allan@intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/checkpatch.pl | 94 |
1 files changed, 84 insertions, 10 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 89d24b3ea43..308e4015bd8 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
| @@ -330,10 +330,15 @@ sub build_types { | |||
| 330 | } | 330 | } |
| 331 | build_types(); | 331 | build_types(); |
| 332 | 332 | ||
| 333 | our $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/; | ||
| 334 | 333 | ||
| 335 | our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; | 334 | our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; |
| 336 | our $LvalOrFunc = qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*}; | 335 | |
| 336 | # Using $balanced_parens, $LvalOrFunc, or $FuncArg | ||
| 337 | # requires at least perl version v5.10.0 | ||
| 338 | # Any use must be runtime checked with $^V | ||
| 339 | |||
| 340 | our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; | ||
| 341 | our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*}; | ||
| 337 | our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)}; | 342 | our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)}; |
| 338 | 343 | ||
| 339 | sub deparenthesize { | 344 | sub deparenthesize { |
| @@ -1330,6 +1335,36 @@ sub check_absolute_file { | |||
| 1330 | } | 1335 | } |
| 1331 | } | 1336 | } |
| 1332 | 1337 | ||
| 1338 | sub pos_last_openparen { | ||
| 1339 | my ($line) = @_; | ||
| 1340 | |||
| 1341 | my $pos = 0; | ||
| 1342 | |||
| 1343 | my $opens = $line =~ tr/\(/\(/; | ||
| 1344 | my $closes = $line =~ tr/\)/\)/; | ||
| 1345 | |||
| 1346 | my $last_openparen = 0; | ||
| 1347 | |||
| 1348 | if (($opens == 0) || ($closes >= $opens)) { | ||
| 1349 | return -1; | ||
| 1350 | } | ||
| 1351 | |||
| 1352 | my $len = length($line); | ||
| 1353 | |||
| 1354 | for ($pos = 0; $pos < $len; $pos++) { | ||
| 1355 | my $string = substr($line, $pos); | ||
| 1356 | if ($string =~ /^($FuncArg|$balanced_parens)/) { | ||
| 1357 | $pos += length($1) - 1; | ||
| 1358 | } elsif (substr($line, $pos, 1) eq '(') { | ||
| 1359 | $last_openparen = $pos; | ||
| 1360 | } elsif (index($string, '(') == -1) { | ||
| 1361 | last; | ||
| 1362 | } | ||
| 1363 | } | ||
| 1364 | |||
| 1365 | return $last_openparen + 1; | ||
| 1366 | } | ||
| 1367 | |||
| 1333 | sub process { | 1368 | sub process { |
| 1334 | my $filename = shift; | 1369 | my $filename = shift; |
| 1335 | 1370 | ||
| @@ -1783,6 +1818,37 @@ sub process { | |||
| 1783 | "please, no space before tabs\n" . $herevet); | 1818 | "please, no space before tabs\n" . $herevet); |
| 1784 | } | 1819 | } |
| 1785 | 1820 | ||
| 1821 | # check for && or || at the start of a line | ||
| 1822 | if ($rawline =~ /^\+\s*(&&|\|\|)/) { | ||
| 1823 | CHK("LOGICAL_CONTINUATIONS", | ||
| 1824 | "Logical continuations should be on the previous line\n" . $hereprev); | ||
| 1825 | } | ||
| 1826 | |||
| 1827 | # check multi-line statement indentation matches previous line | ||
| 1828 | if ($^V && $^V ge 5.10.0 && | ||
| 1829 | $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) { | ||
| 1830 | $prevline =~ /^\+(\t*)(.*)$/; | ||
| 1831 | my $oldindent = $1; | ||
| 1832 | my $rest = $2; | ||
| 1833 | |||
| 1834 | my $pos = pos_last_openparen($rest); | ||
| 1835 | if ($pos >= 0) { | ||
| 1836 | $line =~ /^\+([ \t]*)/; | ||
| 1837 | my $newindent = $1; | ||
| 1838 | |||
| 1839 | my $goodtabindent = $oldindent . | ||
| 1840 | "\t" x ($pos / 8) . | ||
| 1841 | " " x ($pos % 8); | ||
| 1842 | my $goodspaceindent = $oldindent . " " x $pos; | ||
| 1843 | |||
| 1844 | if ($newindent ne $goodtabindent && | ||
| 1845 | $newindent ne $goodspaceindent) { | ||
| 1846 | CHK("PARENTHESIS_ALIGNMENT", | ||
| 1847 | "Alignment should match open parenthesis\n" . $hereprev); | ||
| 1848 | } | ||
| 1849 | } | ||
| 1850 | } | ||
| 1851 | |||
| 1786 | # check for spaces at the beginning of a line. | 1852 | # check for spaces at the beginning of a line. |
| 1787 | # Exceptions: | 1853 | # Exceptions: |
| 1788 | # 1) within comments | 1854 | # 1) within comments |
| @@ -3142,12 +3208,13 @@ sub process { | |||
| 3142 | } | 3208 | } |
| 3143 | 3209 | ||
| 3144 | # Check for misused memsets | 3210 | # Check for misused memsets |
| 3145 | if (defined $stat && | 3211 | if ($^V && $^V ge 5.10.0 && |
| 3212 | defined $stat && | ||
| 3146 | $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) { | 3213 | $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) { |
| 3147 | 3214 | ||
| 3148 | my $ms_addr = $2; | 3215 | my $ms_addr = $2; |
| 3149 | my $ms_val = $8; | 3216 | my $ms_val = $7; |
| 3150 | my $ms_size = $14; | 3217 | my $ms_size = $12; |
| 3151 | 3218 | ||
| 3152 | if ($ms_size =~ /^(0x|)0$/i) { | 3219 | if ($ms_size =~ /^(0x|)0$/i) { |
| 3153 | ERROR("MEMSET", | 3220 | ERROR("MEMSET", |
| @@ -3159,17 +3226,18 @@ sub process { | |||
| 3159 | } | 3226 | } |
| 3160 | 3227 | ||
| 3161 | # typecasts on min/max could be min_t/max_t | 3228 | # typecasts on min/max could be min_t/max_t |
| 3162 | if (defined $stat && | 3229 | if ($^V && $^V ge 5.10.0 && |
| 3230 | defined $stat && | ||
| 3163 | $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { | 3231 | $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { |
| 3164 | if (defined $2 || defined $8) { | 3232 | if (defined $2 || defined $7) { |
| 3165 | my $call = $1; | 3233 | my $call = $1; |
| 3166 | my $cast1 = deparenthesize($2); | 3234 | my $cast1 = deparenthesize($2); |
| 3167 | my $arg1 = $3; | 3235 | my $arg1 = $3; |
| 3168 | my $cast2 = deparenthesize($8); | 3236 | my $cast2 = deparenthesize($7); |
| 3169 | my $arg2 = $9; | 3237 | my $arg2 = $8; |
| 3170 | my $cast; | 3238 | my $cast; |
| 3171 | 3239 | ||
| 3172 | if ($cast1 ne "" && $cast2 ne "") { | 3240 | if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { |
| 3173 | $cast = "$cast1 or $cast2"; | 3241 | $cast = "$cast1 or $cast2"; |
| 3174 | } elsif ($cast1 ne "") { | 3242 | } elsif ($cast1 ne "") { |
| 3175 | $cast = $cast1; | 3243 | $cast = $cast1; |
| @@ -3391,6 +3459,12 @@ sub process { | |||
| 3391 | } | 3459 | } |
| 3392 | 3460 | ||
| 3393 | if ($quiet == 0) { | 3461 | if ($quiet == 0) { |
| 3462 | |||
| 3463 | if ($^V lt 5.10.0) { | ||
| 3464 | print("NOTE: perl $^V is not modern enough to detect all possible issues.\n"); | ||
| 3465 | print("An upgrade to at least perl v5.10.0 is suggested.\n\n"); | ||
| 3466 | } | ||
| 3467 | |||
| 3394 | # If there were whitespace errors which cleanpatch can fix | 3468 | # If there were whitespace errors which cleanpatch can fix |
| 3395 | # then suggest that. | 3469 | # then suggest that. |
| 3396 | if ($rpt_cleaners) { | 3470 | if ($rpt_cleaners) { |
