diff options
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/checkpatch.pl | 329 |
1 files changed, 245 insertions, 84 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index e216d49624b7..914e45a63731 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
@@ -8,7 +8,7 @@ use strict; | |||
8 | 8 | ||
9 | my $P = $0; | 9 | my $P = $0; |
10 | 10 | ||
11 | my $V = '0.01'; | 11 | my $V = '0.03'; |
12 | 12 | ||
13 | use Getopt::Long qw(:config no_auto_abbrev); | 13 | use Getopt::Long qw(:config no_auto_abbrev); |
14 | 14 | ||
@@ -38,7 +38,8 @@ if ($tree && !top_of_kernel_tree()) { | |||
38 | exit(2); | 38 | exit(2); |
39 | } | 39 | } |
40 | 40 | ||
41 | my @deprecated = (); | 41 | my @dep_includes = (); |
42 | my @dep_functions = (); | ||
42 | my $removal = 'Documentation/feature-removal-schedule.txt'; | 43 | my $removal = 'Documentation/feature-removal-schedule.txt'; |
43 | if ($tree && -f $removal) { | 44 | if ($tree && -f $removal) { |
44 | open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n"; | 45 | open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n"; |
@@ -46,9 +47,14 @@ if ($tree && -f $removal) { | |||
46 | if (/^Files:\s+(.*\S)/) { | 47 | if (/^Files:\s+(.*\S)/) { |
47 | for my $file (split(/[, ]+/, $1)) { | 48 | for my $file (split(/[, ]+/, $1)) { |
48 | if ($file =~ m@include/(.*)@) { | 49 | if ($file =~ m@include/(.*)@) { |
49 | push(@deprecated, $1); | 50 | push(@dep_includes, $1); |
50 | } | 51 | } |
51 | } | 52 | } |
53 | |||
54 | } elsif (/^Funcs:\s+(.*\S)/) { | ||
55 | for my $func (split(/[, ]+/, $1)) { | ||
56 | push(@dep_functions, $func); | ||
57 | } | ||
52 | } | 58 | } |
53 | } | 59 | } |
54 | } | 60 | } |
@@ -99,6 +105,97 @@ sub expand_tabs { | |||
99 | return $res; | 105 | return $res; |
100 | } | 106 | } |
101 | 107 | ||
108 | sub line_stats { | ||
109 | my ($line) = @_; | ||
110 | |||
111 | # Drop the diff line leader and expand tabs | ||
112 | $line =~ s/^.//; | ||
113 | $line = expand_tabs($line); | ||
114 | |||
115 | # Pick the indent from the front of the line. | ||
116 | my ($white) = ($line =~ /^(\s*)/); | ||
117 | |||
118 | return (length($line), length($white)); | ||
119 | } | ||
120 | |||
121 | sub ctx_block_get { | ||
122 | my ($linenr, $remain, $outer) = @_; | ||
123 | my $line; | ||
124 | my $start = $linenr - 1; | ||
125 | my $end = $linenr - 1 + $remain; | ||
126 | my $blk = ''; | ||
127 | my @o; | ||
128 | my @c; | ||
129 | my @res = (); | ||
130 | |||
131 | for ($line = $start; $line < $end; $line++) { | ||
132 | $blk .= $lines[$line]; | ||
133 | |||
134 | @o = ($blk =~ /\{/g); | ||
135 | @c = ($blk =~ /\}/g); | ||
136 | |||
137 | if (!$outer || (scalar(@o) - scalar(@c)) == 1) { | ||
138 | push(@res, $lines[$line]); | ||
139 | } | ||
140 | |||
141 | last if (scalar(@o) == scalar(@c)); | ||
142 | } | ||
143 | |||
144 | return @res; | ||
145 | } | ||
146 | sub ctx_block_outer { | ||
147 | my ($linenr, $remain) = @_; | ||
148 | |||
149 | return ctx_block_get($linenr, $remain, 1); | ||
150 | } | ||
151 | sub ctx_block { | ||
152 | my ($linenr, $remain) = @_; | ||
153 | |||
154 | return ctx_block_get($linenr, $remain, 0); | ||
155 | } | ||
156 | |||
157 | sub ctx_locate_comment { | ||
158 | my ($first_line, $end_line) = @_; | ||
159 | |||
160 | # Catch a comment on the end of the line itself. | ||
161 | my ($current_comment) = ($lines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@); | ||
162 | return $current_comment if (defined $current_comment); | ||
163 | |||
164 | # Look through the context and try and figure out if there is a | ||
165 | # comment. | ||
166 | my $in_comment = 0; | ||
167 | $current_comment = ''; | ||
168 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { | ||
169 | my $line = $lines[$linenr - 1]; | ||
170 | ##warn " $line\n"; | ||
171 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { | ||
172 | $in_comment = 1; | ||
173 | } | ||
174 | if ($line =~ m@/\*@) { | ||
175 | $in_comment = 1; | ||
176 | } | ||
177 | if (!$in_comment && $current_comment ne '') { | ||
178 | $current_comment = ''; | ||
179 | } | ||
180 | $current_comment .= $line . "\n" if ($in_comment); | ||
181 | if ($line =~ m@\*/@) { | ||
182 | $in_comment = 0; | ||
183 | } | ||
184 | } | ||
185 | |||
186 | chomp($current_comment); | ||
187 | return($current_comment); | ||
188 | } | ||
189 | sub ctx_has_comment { | ||
190 | my ($first_line, $end_line) = @_; | ||
191 | my $cmt = ctx_locate_comment($first_line, $end_line); | ||
192 | |||
193 | ##print "LINE: $lines[$end_line - 1 ]\n"; | ||
194 | ##print "CMMT: $cmt\n"; | ||
195 | |||
196 | return ($cmt ne ''); | ||
197 | } | ||
198 | |||
102 | sub cat_vet { | 199 | sub cat_vet { |
103 | my ($vet) = @_; | 200 | my ($vet) = @_; |
104 | 201 | ||
@@ -108,6 +205,10 @@ sub cat_vet { | |||
108 | return $vet; | 205 | return $vet; |
109 | } | 206 | } |
110 | 207 | ||
208 | sub has_non_quoted { | ||
209 | return ($_[0] =~ m{$_[1]} and $_[0] !~ m{\".*$_[1].*\"}); | ||
210 | } | ||
211 | |||
111 | sub process { | 212 | sub process { |
112 | my $filename = shift; | 213 | my $filename = shift; |
113 | my @lines = @_; | 214 | my @lines = @_; |
@@ -116,7 +217,7 @@ sub process { | |||
116 | my $prevline=""; | 217 | my $prevline=""; |
117 | my $stashline=""; | 218 | my $stashline=""; |
118 | 219 | ||
119 | my $lineforcounting=''; | 220 | my $length; |
120 | my $indent; | 221 | my $indent; |
121 | my $previndent=0; | 222 | my $previndent=0; |
122 | my $stashindent=0; | 223 | my $stashindent=0; |
@@ -145,7 +246,7 @@ sub process { | |||
145 | #extract the line range in the file after the patch is applied | 246 | #extract the line range in the file after the patch is applied |
146 | if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) { | 247 | if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) { |
147 | $is_patch = 1; | 248 | $is_patch = 1; |
148 | $first_line = 1; | 249 | $first_line = $linenr + 1; |
149 | $in_comment = 0; | 250 | $in_comment = 0; |
150 | $realline=$1-1; | 251 | $realline=$1-1; |
151 | if (defined $2) { | 252 | if (defined $2) { |
@@ -156,8 +257,10 @@ sub process { | |||
156 | next; | 257 | next; |
157 | } | 258 | } |
158 | 259 | ||
159 | #track the line number as we move through the hunk | 260 | # track the line number as we move through the hunk, note that |
160 | if ($line=~/^[ \+]/) { | 261 | # new versions of GNU diff omit the leading space on completely |
262 | # blank context lines so we need to count that too. | ||
263 | if ($line =~ /^( |\+|$)/) { | ||
161 | $realline++; | 264 | $realline++; |
162 | $realcnt-- if ($realcnt != 0); | 265 | $realcnt-- if ($realcnt != 0); |
163 | 266 | ||
@@ -168,7 +271,7 @@ sub process { | |||
168 | # Guestimate if this is a continuing comment. If this | 271 | # Guestimate if this is a continuing comment. If this |
169 | # is the start of a diff block and this line starts | 272 | # is the start of a diff block and this line starts |
170 | # ' *' then it is very likely a comment. | 273 | # ' *' then it is very likely a comment. |
171 | if ($first_line and $line =~ m@^.\s*\*@) { | 274 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
172 | $in_comment = 1; | 275 | $in_comment = 1; |
173 | } | 276 | } |
174 | if ($line =~ m@/\*@) { | 277 | if ($line =~ m@/\*@) { |
@@ -178,17 +281,12 @@ sub process { | |||
178 | $in_comment = 0; | 281 | $in_comment = 0; |
179 | } | 282 | } |
180 | 283 | ||
181 | $lineforcounting = $line; | 284 | # Measure the line length and indent. |
182 | $lineforcounting =~ s/^\+//; | 285 | ($length, $indent) = line_stats($line); |
183 | $lineforcounting = expand_tabs($lineforcounting); | ||
184 | |||
185 | my ($white) = ($lineforcounting =~ /^(\s*)/); | ||
186 | $indent = length($white); | ||
187 | 286 | ||
188 | # Track the previous line. | 287 | # Track the previous line. |
189 | ($prevline, $stashline) = ($stashline, $line); | 288 | ($prevline, $stashline) = ($stashline, $line); |
190 | ($previndent, $stashindent) = ($stashindent, $indent); | 289 | ($previndent, $stashindent) = ($stashindent, $indent); |
191 | $first_line = 0; | ||
192 | } | 290 | } |
193 | 291 | ||
194 | #make up the handle for any error we report on this line | 292 | #make up the handle for any error we report on this line |
@@ -203,6 +301,8 @@ sub process { | |||
203 | $signoff++; | 301 | $signoff++; |
204 | 302 | ||
205 | } elsif ($line =~ /^\s*signed-off-by:/i) { | 303 | } elsif ($line =~ /^\s*signed-off-by:/i) { |
304 | # This is a signoff, if ugly, so do not double report. | ||
305 | $signoff++; | ||
206 | if (!($line =~ /^\s*Signed-off-by:/)) { | 306 | if (!($line =~ /^\s*Signed-off-by:/)) { |
207 | print "use Signed-off-by:\n"; | 307 | print "use Signed-off-by:\n"; |
208 | print "$herecurr"; | 308 | print "$herecurr"; |
@@ -229,7 +329,7 @@ sub process { | |||
229 | $clean = 0; | 329 | $clean = 0; |
230 | } | 330 | } |
231 | #80 column limit | 331 | #80 column limit |
232 | if (!($prevline=~/\/\*\*/) && length($lineforcounting) > 80) { | 332 | if (!($prevline=~/\/\*\*/) && $length > 80) { |
233 | print "line over 80 characters\n"; | 333 | print "line over 80 characters\n"; |
234 | print "$herecurr"; | 334 | print "$herecurr"; |
235 | $clean = 0; | 335 | $clean = 0; |
@@ -254,7 +354,7 @@ sub process { | |||
254 | next if ($in_comment); | 354 | next if ($in_comment); |
255 | 355 | ||
256 | # no C99 // comments | 356 | # no C99 // comments |
257 | if ($line =~ m@//@ and !($line =~ m@\".*//.*\"@)) { | 357 | if (has_non_quoted($line, '//')) { |
258 | print "do not use C99 // comments\n"; | 358 | print "do not use C99 // comments\n"; |
259 | print "$herecurr"; | 359 | print "$herecurr"; |
260 | $clean = 0; | 360 | $clean = 0; |
@@ -320,44 +420,44 @@ sub process { | |||
320 | print "$herecurr"; | 420 | print "$herecurr"; |
321 | $clean = 0; | 421 | $clean = 0; |
322 | } | 422 | } |
423 | # Note we expand the line with the leading + as the real | ||
424 | # line will be displayed with the leading + and the tabs | ||
425 | # will therefore also expand that way. | ||
323 | my $opline = $line; | 426 | my $opline = $line; |
427 | $opline = expand_tabs($opline); | ||
324 | $opline =~ s/^.//; | 428 | $opline =~ s/^.//; |
325 | if (!($line=~/\#\s*include/)) { | 429 | if (!($line=~/\#\s*include/)) { |
326 | # Check operator spacing. | 430 | # Check operator spacing. |
327 | my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline); | 431 | my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline); |
432 | my $off = 1; | ||
328 | for (my $n = 0; $n < $#elements; $n += 2) { | 433 | for (my $n = 0; $n < $#elements; $n += 2) { |
329 | # $wN says we have white-space before or after | 434 | $off += length($elements[$n]); |
330 | # $sN says we have a separator before or after | 435 | |
331 | # $oN says we have another operator before or after | 436 | my $a = ''; |
332 | my $w1 = $elements[$n] =~ /\s$/; | 437 | $a = 'V' if ($elements[$n] ne ''); |
333 | my $s1 = $elements[$n] =~ /(\[|\(|\s)$/; | 438 | $a = 'W' if ($elements[$n] =~ /\s$/); |
334 | my $o1 = $elements[$n] eq ''; | 439 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
440 | $a = 'O' if ($elements[$n] eq ''); | ||
441 | $a = 'E' if ($elements[$n] eq '' && $n == 0); | ||
442 | |||
335 | my $op = $elements[$n + 1]; | 443 | my $op = $elements[$n + 1]; |
336 | my $w2 = 1; | 444 | |
337 | my $s2 = 1; | 445 | my $c = ''; |
338 | my $o2 = 0; | ||
339 | # If we have something after the operator handle it. | ||
340 | if (defined $elements[$n + 2]) { | 446 | if (defined $elements[$n + 2]) { |
341 | $w2 = $elements[$n + 2] =~ /^\s/; | 447 | $c = 'V' if ($elements[$n + 2] ne ''); |
342 | $s2 = $elements[$n + 2] =~ /^(\s|\)|\]|;)/; | 448 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
343 | $o2 = $elements[$n + 2] eq ''; | 449 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
450 | $c = 'O' if ($elements[$n + 2] eq ''); | ||
451 | } else { | ||
452 | $c = 'E'; | ||
344 | } | 453 | } |
345 | 454 | ||
346 | # Generate the context. | 455 | my $ctx = "${a}x${c}"; |
347 | my $at = "here: "; | 456 | |
348 | for (my $m = $n; $m >= 0; $m--) { | 457 | my $at = "(ctx:$ctx)"; |
349 | if ($elements[$m] ne '') { | 458 | |
350 | $at .= $elements[$m]; | 459 | my $ptr = (" " x $off) . "^"; |
351 | last; | 460 | my $hereptr = "$here\n$line\n$ptr\n\n"; |
352 | } | ||
353 | } | ||
354 | $at .= $op; | ||
355 | for (my $m = $n + 2; defined $elements[$m]; $m++) { | ||
356 | if ($elements[$m] ne '') { | ||
357 | $at .= $elements[$m]; | ||
358 | last; | ||
359 | } | ||
360 | } | ||
361 | 461 | ||
362 | ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n"; | 462 | ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n"; |
363 | # Skip things apparently in quotes. | 463 | # Skip things apparently in quotes. |
@@ -368,38 +468,38 @@ sub process { | |||
368 | 468 | ||
369 | # -> should have no spaces | 469 | # -> should have no spaces |
370 | } elsif ($op eq '->') { | 470 | } elsif ($op eq '->') { |
371 | if ($s1 or $s2) { | 471 | if ($ctx =~ /Wx.|.xW/) { |
372 | print "no spaces around that '$op' $at\n"; | 472 | print "no spaces around that '$op' $at\n"; |
373 | print "$herecurr"; | 473 | print "$hereptr"; |
374 | $clean = 0; | 474 | $clean = 0; |
375 | } | 475 | } |
376 | 476 | ||
377 | # , must have a space on the right. | 477 | # , must have a space on the right. |
378 | } elsif ($op eq ',') { | 478 | } elsif ($op eq ',') { |
379 | if (!$s2) { | 479 | if ($ctx !~ /.xW|.xE/) { |
380 | print "need space after that '$op' $at\n"; | 480 | print "need space after that '$op' $at\n"; |
381 | print "$herecurr"; | 481 | print "$hereptr"; |
382 | $clean = 0; | 482 | $clean = 0; |
383 | } | 483 | } |
384 | 484 | ||
385 | # unary ! and unary ~ are allowed no space on the right | 485 | # unary ! and unary ~ are allowed no space on the right |
386 | } elsif ($op eq '!' or $op eq '~') { | 486 | } elsif ($op eq '!' or $op eq '~') { |
387 | if (!$s1 && !$o1) { | 487 | if ($ctx !~ /[WOEB]x./) { |
388 | print "need space before that '$op' $at\n"; | 488 | print "need space before that '$op' $at\n"; |
389 | print "$herecurr"; | 489 | print "$hereptr"; |
390 | $clean = 0; | 490 | $clean = 0; |
391 | } | 491 | } |
392 | if ($s2) { | 492 | if ($ctx =~ /.xW/) { |
393 | print "no space after that '$op' $at\n"; | 493 | print "no space after that '$op' $at\n"; |
394 | print "$herecurr"; | 494 | print "$hereptr"; |
395 | $clean = 0; | 495 | $clean = 0; |
396 | } | 496 | } |
397 | 497 | ||
398 | # unary ++ and unary -- are allowed no space on one side. | 498 | # unary ++ and unary -- are allowed no space on one side. |
399 | } elsif ($op eq '++' or $op eq '--') { | 499 | } elsif ($op eq '++' or $op eq '--') { |
400 | if (($s1 && $s2) || ((!$s1 && !$o1) && (!$s2 && !$o2))) { | 500 | if ($ctx !~ /[WOB]x[^W]|[^W]x[WOB]/) { |
401 | print "need space one side of that '$op' $at\n"; | 501 | print "need space one side of that '$op' $at\n"; |
402 | print "$herecurr"; | 502 | print "$hereptr"; |
403 | $clean = 0; | 503 | $clean = 0; |
404 | } | 504 | } |
405 | 505 | ||
@@ -420,10 +520,17 @@ sub process { | |||
420 | # (foo *) | 520 | # (foo *) |
421 | # (foo **) | 521 | # (foo **) |
422 | # | 522 | # |
423 | } elsif ($op eq '&' or $op eq '-' or $op eq '*') { | 523 | } elsif ($op eq '&' or $op eq '-') { |
424 | if ($w2 and !$w1) { | 524 | if ($ctx !~ /VxV|[EWB]x[WE]|[EWB]x[VO]/) { |
525 | print "need space before that '$op' $at\n"; | ||
526 | print "$hereptr"; | ||
527 | $clean = 0; | ||
528 | } | ||
529 | |||
530 | } elsif ($op eq '*') { | ||
531 | if ($ctx !~ /VxV|[EWB]x[WE]|[EWB]x[VO]|[EWO]x[OBV]/) { | ||
425 | print "need space before that '$op' $at\n"; | 532 | print "need space before that '$op' $at\n"; |
426 | print "$herecurr"; | 533 | print "$hereptr"; |
427 | $clean = 0; | 534 | $clean = 0; |
428 | } | 535 | } |
429 | 536 | ||
@@ -431,18 +538,19 @@ sub process { | |||
431 | } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or | 538 | } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or |
432 | $op eq '^' or $op eq '|') | 539 | $op eq '^' or $op eq '|') |
433 | { | 540 | { |
434 | if ($s1 != $s2) { | 541 | if ($ctx !~ /VxV|WxW|VxE|WxE/) { |
435 | print "need consistent spacing around '$op' $at\n"; | 542 | print "need consistent spacing around '$op' $at\n"; |
436 | print "$herecurr"; | 543 | print "$hereptr"; |
437 | $clean = 0; | 544 | $clean = 0; |
438 | } | 545 | } |
439 | 546 | ||
440 | # All the others need spaces both sides. | 547 | # All the others need spaces both sides. |
441 | } elsif (!$s1 or !$s2) { | 548 | } elsif ($ctx !~ /[EW]x[WE]/) { |
442 | print "need spaces around that '$op' $at\n"; | 549 | print "need spaces around that '$op' $at\n"; |
443 | print "$herecurr"; | 550 | print "$hereptr"; |
444 | $clean = 0; | 551 | $clean = 0; |
445 | } | 552 | } |
553 | $off += length($elements[$n + 1]); | ||
446 | } | 554 | } |
447 | } | 555 | } |
448 | 556 | ||
@@ -454,7 +562,7 @@ sub process { | |||
454 | } | 562 | } |
455 | 563 | ||
456 | #goto labels aren't indented, allow a single space however | 564 | #goto labels aren't indented, allow a single space however |
457 | if ($line=~/^.\s+[A-Za-z\d_]+:/ and | 565 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
458 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { | 566 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
459 | print "labels should not be indented\n"; | 567 | print "labels should not be indented\n"; |
460 | print "$herecurr"; | 568 | print "$herecurr"; |
@@ -462,15 +570,15 @@ sub process { | |||
462 | } | 570 | } |
463 | 571 | ||
464 | # Need a space before open parenthesis after if, while etc | 572 | # Need a space before open parenthesis after if, while etc |
465 | if ($line=~/(if|while|for|switch)\(/) { | 573 | if ($line=~/\b(if|while|for|switch)\(/) { |
466 | print "need a space before the open parenthesis\n"; | 574 | print "need a space before the open parenthesis\n"; |
467 | print "$herecurr"; | 575 | print "$herecurr"; |
468 | $clean = 0; | 576 | $clean = 0; |
469 | } | 577 | } |
470 | 578 | ||
471 | # Check for illegal assignment in if conditional. | 579 | # Check for illegal assignment in if conditional. |
472 | if ($line=~/(if|while)\s*\(.*[^<>!=]=[^=].*\)/) { | 580 | if ($line=~/\b(if|while)\s*\(.*[^<>!=]=[^=].*\)/) { |
473 | print "do not use assignment in if condition\n"; | 581 | print "do not use assignment in condition\n"; |
474 | print "$herecurr"; | 582 | print "$herecurr"; |
475 | $clean = 0; | 583 | $clean = 0; |
476 | } | 584 | } |
@@ -484,15 +592,28 @@ sub process { | |||
484 | $clean = 0; | 592 | $clean = 0; |
485 | } | 593 | } |
486 | 594 | ||
487 | # Check for switch () {<nl>case, these must be at the | 595 | # Check for switch () and associated case and default |
488 | # same indent. We will only catch the first one, as our | 596 | # statements should be at the same indent. |
489 | # context is very small but people tend to be consistent | 597 | if ($line=~/\bswitch\s*\(.*\)/) { |
490 | # so we will catch them out more often than not. | 598 | my $err = ''; |
491 | if ($prevline=~/\s*switch\s*\(.*\)/ and $line=~/\s*case\s+/ | 599 | my $sep = ''; |
492 | and $previndent != $indent) { | 600 | my @ctx = ctx_block_outer($linenr, $realcnt); |
493 | print "switch and case should be at the same indent\n"; | 601 | shift(@ctx); |
494 | print "$hereprev"; | 602 | for my $ctx (@ctx) { |
495 | $clean = 0; | 603 | my ($clen, $cindent) = line_stats($ctx); |
604 | if ($ctx =~ /\s*(case\s+|default:)/ && | ||
605 | $indent != $cindent) { | ||
606 | $err .= "$sep$ctx\n"; | ||
607 | $sep = ''; | ||
608 | } else { | ||
609 | $sep = "[...]\n"; | ||
610 | } | ||
611 | } | ||
612 | if ($err ne '') { | ||
613 | print "switch and case should be at the same indent\n"; | ||
614 | print "$here\n$line\n$err\n"; | ||
615 | $clean = 0; | ||
616 | } | ||
496 | } | 617 | } |
497 | 618 | ||
498 | #studly caps, commented out until figure out how to distinguish between use of existing and adding new | 619 | #studly caps, commented out until figure out how to distinguish between use of existing and adding new |
@@ -520,7 +641,7 @@ sub process { | |||
520 | } | 641 | } |
521 | 642 | ||
522 | #if/while/etc brace do not go on next line, unless #defining a do while loop, or if that brace on the next line is for something else | 643 | #if/while/etc brace do not go on next line, unless #defining a do while loop, or if that brace on the next line is for something else |
523 | if ($prevline=~/(if|while|for|switch)\s*\(/) { | 644 | if ($prevline=~/\b(if|while|for|switch)\s*\(/) { |
524 | my @opened = $prevline=~/\(/g; | 645 | my @opened = $prevline=~/\(/g; |
525 | my @closed = $prevline=~/\)/g; | 646 | my @closed = $prevline=~/\)/g; |
526 | my $nr_line = $linenr; | 647 | my $nr_line = $linenr; |
@@ -529,7 +650,7 @@ sub process { | |||
529 | my $extra_lines = 0; | 650 | my $extra_lines = 0; |
530 | my $display_segment = $prevline; | 651 | my $display_segment = $prevline; |
531 | 652 | ||
532 | while ($remaining > 0 && scalar @opened > scalar @closed) { | 653 | while ($remaining > 1 && scalar @opened > scalar @closed) { |
533 | $prevline .= $next_line; | 654 | $prevline .= $next_line; |
534 | $display_segment .= "\n" . $next_line; | 655 | $display_segment .= "\n" . $next_line; |
535 | $next_line = $lines[$nr_line]; | 656 | $next_line = $lines[$nr_line]; |
@@ -540,10 +661,10 @@ sub process { | |||
540 | @closed = $prevline=~/\)/g; | 661 | @closed = $prevline=~/\)/g; |
541 | } | 662 | } |
542 | 663 | ||
543 | if (($prevline=~/(if|while|for|switch)\s*\(.*\)\s*$/) and ($next_line=~/{/) and | 664 | if (($prevline=~/\b(if|while|for|switch)\s*\(.*\)\s*$/) and ($next_line=~/{/) and |
544 | !($next_line=~/(if|while|for)/) and !($next_line=~/\#define.*do.*while/)) { | 665 | !($next_line=~/\b(if|while|for)/) and !($next_line=~/\#define.*do.*while/)) { |
545 | print "That { should be on the previous line\n"; | 666 | print "That { should be on the previous line\n"; |
546 | print "$display_segment\n$next_line\n\n"; | 667 | print "$here\n$display_segment\n$next_line\n\n"; |
547 | $clean = 0; | 668 | $clean = 0; |
548 | } | 669 | } |
549 | } | 670 | } |
@@ -558,7 +679,7 @@ sub process { | |||
558 | } | 679 | } |
559 | 680 | ||
560 | # don't include deprecated include files | 681 | # don't include deprecated include files |
561 | for my $inc (@deprecated) { | 682 | for my $inc (@dep_includes) { |
562 | if ($line =~ m@\#\s*include\s*\<$inc>@) { | 683 | if ($line =~ m@\#\s*include\s*\<$inc>@) { |
563 | print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n"; | 684 | print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n"; |
564 | print "$herecurr"; | 685 | print "$herecurr"; |
@@ -566,9 +687,49 @@ sub process { | |||
566 | } | 687 | } |
567 | } | 688 | } |
568 | 689 | ||
569 | # don't use kernel_thread() | 690 | # don't use deprecated functions |
570 | if ($line =~ /\bkernel_thread\b/) { | 691 | for my $func (@dep_functions) { |
571 | print "Don't use kernel_thread(), use kthread(): see Documentation/feature-removal-schedule.txt\n"; | 692 | if (has_non_quoted($line, '\b' . $func . '\b')) { |
693 | print "Don't use $func(): see Documentation/feature-removal-schedule.txt\n"; | ||
694 | print "$herecurr"; | ||
695 | $clean = 0; | ||
696 | } | ||
697 | } | ||
698 | |||
699 | # no volatiles please | ||
700 | if (has_non_quoted($line, '\bvolatile\b')) { | ||
701 | print "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n"; | ||
702 | print "$herecurr"; | ||
703 | $clean = 0; | ||
704 | } | ||
705 | |||
706 | # warn about #ifdefs in C files | ||
707 | if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { | ||
708 | print "#ifdef in C files should be avoided\n"; | ||
709 | print "$herecurr"; | ||
710 | $clean = 0; | ||
711 | } | ||
712 | |||
713 | # check for spinlock_t definitions without a comment. | ||
714 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) { | ||
715 | my $which = $1; | ||
716 | if (!ctx_has_comment($first_line, $linenr)) { | ||
717 | print "$1 definition without comment\n"; | ||
718 | print "$herecurr"; | ||
719 | $clean = 0; | ||
720 | } | ||
721 | } | ||
722 | # check for memory barriers without a comment. | ||
723 | if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { | ||
724 | if (!ctx_has_comment($first_line, $linenr)) { | ||
725 | print "memory barrier without comment\n"; | ||
726 | print "$herecurr"; | ||
727 | $clean = 0; | ||
728 | } | ||
729 | } | ||
730 | # check of hardware specific defines | ||
731 | if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) { | ||
732 | print "architecture specific defines should be avoided\n"; | ||
572 | print "$herecurr"; | 733 | print "$herecurr"; |
573 | $clean = 0; | 734 | $clean = 0; |
574 | } | 735 | } |