aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
authorAndy Whitcroft <apw@shadowen.org>2008-03-28 17:15:58 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2008-03-28 17:45:22 -0400
commit773647a09add08a6e8165843a338220a6f833705 (patch)
tree392eadf865d0d23248ca4d72f461fd9dca08a991 /scripts/checkpatch.pl
parent3387b804d8850494bdf91d16800925a3fd46e37d (diff)
update checkpatch.pl to version 0.16
This version brings proper quote tracking across lines, and brings the handling of comments into the same mechanism ensuring nesting is correctly handled. It brings the usual flurry of fixes for false positives. It also brings a number of new checks. The most contentious change will likely be the checks for NR_CPUS as this throws some new warnings in kernel/sched.c. Of note: - all new quote tracking across lines - all new comment tracking - new more direct, less ambigious wording for some warnings - recommends mutexes and completions over semaphores - recommends strict_strto* over simple_strto* - report on direct use of NR_CPUS Andy Whitcroft (22): Version: 0.16 string quote tracking should cross line boundaries check spacing round -> correctly across newlines checks for linux/ against asm/ include files should be warnings standardise on 'required' and 'prohibited' take the first end of condition when parsing statements values: cope with unbalanced brackets preprocessor #elif is not a function preprocessor #if should not trigger trailing statement checks test: allow us to limit output to a single error recommend real mutexes over semaphores asm checks should mirror those for __asm__ warn on semaphores being used in place of completions trailing ; on control structure should ignore do {} while (); recommend strict_strtoX over simple_strtoX redo comment handling as a quote type use of NR_CPUS is normally wrong consistant spacing should only be about spaces if brace check suppression should only apply to the top-levels use tr/// to align spacing for operators move to using four parameter form of substr check and report modifications to include/asm Signed-off-by: Andy Whitcroft <apw@shadowen.org> Cc: Ingo Molnar <mingo@elte.hu> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl463
1 files changed, 279 insertions, 184 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 2a7cef9726e4..58a94947d655 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -9,7 +9,7 @@ use strict;
9my $P = $0; 9my $P = $0;
10$P =~ s@.*/@@g; 10$P =~ s@.*/@@g;
11 11
12my $V = '0.15'; 12my $V = '0.16';
13 13
14use Getopt::Long qw(:config no_auto_abbrev); 14use Getopt::Long qw(:config no_auto_abbrev);
15 15
@@ -18,6 +18,7 @@ my $tree = 1;
18my $chk_signoff = 1; 18my $chk_signoff = 1;
19my $chk_patch = 1; 19my $chk_patch = 1;
20my $tst_type = 0; 20my $tst_type = 0;
21my $tst_only;
21my $emacs = 0; 22my $emacs = 0;
22my $terse = 0; 23my $terse = 0;
23my $file = 0; 24my $file = 0;
@@ -44,6 +45,7 @@ GetOptions(
44 45
45 'debug=s' => \%debug, 46 'debug=s' => \%debug,
46 'test-type!' => \$tst_type, 47 'test-type!' => \$tst_type,
48 'test-only=s' => \$tst_only,
47) or exit; 49) or exit;
48 50
49my $exit = 0; 51my $exit = 0;
@@ -263,17 +265,7 @@ sub expand_tabs {
263 return $res; 265 return $res;
264} 266}
265sub copy_spacing { 267sub copy_spacing {
266 my ($str) = @_; 268 (my $res = shift) =~ tr/\t/ /c;
267
268 my $res = '';
269 for my $c (split(//, $str)) {
270 if ($c eq "\t") {
271 $res .= $c;
272 } else {
273 $res .= ' ';
274 }
275 }
276
277 return $res; 269 return $res;
278} 270}
279 271
@@ -290,53 +282,76 @@ sub line_stats {
290 return (length($line), length($white)); 282 return (length($line), length($white));
291} 283}
292 284
285my $sanitise_quote = '';
286
287sub sanitise_line_reset {
288 my ($in_comment) = @_;
289
290 if ($in_comment) {
291 $sanitise_quote = '*/';
292 } else {
293 $sanitise_quote = '';
294 }
295}
293sub sanitise_line { 296sub sanitise_line {
294 my ($line) = @_; 297 my ($line) = @_;
295 298
296 my $res = ''; 299 my $res = '';
297 my $l = ''; 300 my $l = '';
298 301
299 my $quote = '';
300 my $qlen = 0; 302 my $qlen = 0;
303 my $off = 0;
304 my $c;
301 305
302 foreach my $c (split(//, $line)) { 306 # Always copy over the diff marker.
303 # The second backslash of a pair is not a "quote". 307 $res = substr($line, 0, 1);
304 if ($l eq "\\" && $c eq "\\") { 308
305 $c = 'X'; 309 for ($off = 1; $off < length($line); $off++) {
306 } 310 $c = substr($line, $off, 1);
307 if ($l ne "\\" && ($c eq "'" || $c eq '"')) { 311
308 if ($quote eq '') { 312 # Comments we are wacking completly including the begin
309 $quote = $c; 313 # and end, all to $;.
310 $res .= $c; 314 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
311 $l = $c; 315 $sanitise_quote = '*/';
312 $qlen = 0; 316
313 next; 317 substr($res, $off, 2, "$;$;");
314 } elsif ($quote eq $c) { 318 $off++;
315 $quote = ''; 319 next;
316 }
317 } 320 }
318 if ($quote eq "'" && $qlen > 1) { 321 if (substr($line, $off, 2) eq $sanitise_quote) {
319 $quote = ''; 322 $sanitise_quote = '';
323 substr($res, $off, 2, "$;$;");
324 $off++;
325 next;
320 } 326 }
321 if ($quote && $c ne "\t") { 327
322 $res .= "X"; 328 # A \ in a string means ignore the next character.
323 $qlen++; 329 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
324 } else { 330 $c eq "\\") {
325 $res .= $c; 331 substr($res, $off, 2, 'XX');
332 $off++;
333 next;
326 } 334 }
335 # Regular quotes.
336 if ($c eq "'" || $c eq '"') {
337 if ($sanitise_quote eq '') {
338 $sanitise_quote = $c;
327 339
328 $l = $c; 340 substr($res, $off, 1, $c);
329 } 341 next;
342 } elsif ($sanitise_quote eq $c) {
343 $sanitise_quote = '';
344 }
345 }
330 346
331 # Clear out the comments. 347 #print "SQ:$sanitise_quote\n";
332 while ($res =~ m@(/\*.*?\*/)@g) { 348 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
333 substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]); 349 substr($res, $off, 1, $;);
334 } 350 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
335 if ($res =~ m@(/\*.*)@) { 351 substr($res, $off, 1, 'X');
336 substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]); 352 } else {
337 } 353 substr($res, $off, 1, $c);
338 if ($res =~ m@^.(.*\*/)@) { 354 }
339 substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
340 } 355 }
341 356
342 # The pathname on a #include may be surrounded by '<' and '>'. 357 # The pathname on a #include may be surrounded by '<' and '>'.
@@ -359,6 +374,7 @@ sub ctx_statement_block {
359 my $blk = ''; 374 my $blk = '';
360 my $soff = $off; 375 my $soff = $off;
361 my $coff = $off - 1; 376 my $coff = $off - 1;
377 my $coff_set = 0;
362 378
363 my $loff = 0; 379 my $loff = 0;
364 380
@@ -370,7 +386,7 @@ sub ctx_statement_block {
370 386
371 my $remainder; 387 my $remainder;
372 while (1) { 388 while (1) {
373 #warn "CSB: blk<$blk>\n"; 389 #warn "CSB: blk<$blk> remain<$remain>\n";
374 # If we are about to drop off the end, pull in more 390 # If we are about to drop off the end, pull in more
375 # context. 391 # context.
376 if ($off >= $len) { 392 if ($off >= $len) {
@@ -393,7 +409,7 @@ sub ctx_statement_block {
393 $c = substr($blk, $off, 1); 409 $c = substr($blk, $off, 1);
394 $remainder = substr($blk, $off); 410 $remainder = substr($blk, $off);
395 411
396 #warn "CSB: c<$c> type<$type> level<$level>\n"; 412 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
397 # Statement ends at the ';' or a close '}' at the 413 # Statement ends at the ';' or a close '}' at the
398 # outermost level. 414 # outermost level.
399 if ($level == 0 && $c eq ';') { 415 if ($level == 0 && $c eq ';') {
@@ -401,10 +417,14 @@ sub ctx_statement_block {
401 } 417 }
402 418
403 # An else is really a conditional as long as its not else if 419 # An else is really a conditional as long as its not else if
404 if ($level == 0 && (!defined($p) || $p =~ /(?:\s|\})/) && 420 if ($level == 0 && $coff_set == 0 &&
405 $remainder =~ /(else)(?:\s|{)/ && 421 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
406 $remainder !~ /else\s+if\b/) { 422 $remainder =~ /^(else)(?:\s|{)/ &&
407 $coff = $off + length($1); 423 $remainder !~ /^else\s+if\b/) {
424 $coff = $off + length($1) - 1;
425 $coff_set = 1;
426 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
427 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
408 } 428 }
409 429
410 if (($type eq '' || $type eq '(') && $c eq '(') { 430 if (($type eq '' || $type eq '(') && $c eq '(') {
@@ -417,6 +437,8 @@ sub ctx_statement_block {
417 437
418 if ($level == 0 && $coff < $soff) { 438 if ($level == 0 && $coff < $soff) {
419 $coff = $off; 439 $coff = $off;
440 $coff_set = 1;
441 #warn "CSB: mark coff<$coff>\n";
420 } 442 }
421 } 443 }
422 if (($type eq '' || $type eq '{') && $c eq '{') { 444 if (($type eq '' || $type eq '{') && $c eq '{') {
@@ -444,7 +466,7 @@ sub ctx_statement_block {
444 #warn "STATEMENT<$statement>\n"; 466 #warn "STATEMENT<$statement>\n";
445 #warn "CONDITION<$condition>\n"; 467 #warn "CONDITION<$condition>\n";
446 468
447 #print "off<$off> loff<$loff>\n"; 469 #print "coff<$coff> soff<$off> loff<$loff>\n";
448 470
449 return ($statement, $condition, 471 return ($statement, $condition,
450 $line, $remain + 1, $off - $loff + 1, $level); 472 $line, $remain + 1, $off - $loff + 1, $level);
@@ -502,7 +524,7 @@ sub ctx_statement_full {
502 # Grab the first conditional/block pair. 524 # Grab the first conditional/block pair.
503 ($statement, $condition, $linenr, $remain, $off, $level) = 525 ($statement, $condition, $linenr, $remain, $off, $level) =
504 ctx_statement_block($linenr, $remain, $off); 526 ctx_statement_block($linenr, $remain, $off);
505 #print "F: c<$condition> s<$statement>\n"; 527 #print "F: c<$condition> s<$statement> remain<$remain>\n";
506 push(@chunks, [ $condition, $statement ]); 528 push(@chunks, [ $condition, $statement ]);
507 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 529 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
508 return ($level, $linenr, @chunks); 530 return ($level, $linenr, @chunks);
@@ -514,7 +536,7 @@ sub ctx_statement_full {
514 ($statement, $condition, $linenr, $remain, $off, $level) = 536 ($statement, $condition, $linenr, $remain, $off, $level) =
515 ctx_statement_block($linenr, $remain, $off); 537 ctx_statement_block($linenr, $remain, $off);
516 #print "C: c<$condition> s<$statement> remain<$remain>\n"; 538 #print "C: c<$condition> s<$statement> remain<$remain>\n";
517 last if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:else|do)\b/s)); 539 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
518 #print "C: push\n"; 540 #print "C: push\n";
519 push(@chunks, [ $condition, $statement ]); 541 push(@chunks, [ $condition, $statement ]);
520 } 542 }
@@ -668,6 +690,7 @@ sub annotate_values {
668 print "$stream\n" if ($dbg_values > 1); 690 print "$stream\n" if ($dbg_values > 1);
669 691
670 while (length($cur)) { 692 while (length($cur)) {
693 @av_paren_type = ('E') if ($#av_paren_type < 0);
671 print " <" . join('', @av_paren_type) . 694 print " <" . join('', @av_paren_type) .
672 "> <$type> " if ($dbg_values > 1); 695 "> <$type> " if ($dbg_values > 1);
673 if ($cur =~ /^(\s+)/o) { 696 if ($cur =~ /^(\s+)/o) {
@@ -804,28 +827,34 @@ sub possible {
804my $prefix = ''; 827my $prefix = '';
805 828
806sub report { 829sub report {
830 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
831 return 0;
832 }
807 my $line = $prefix . $_[0]; 833 my $line = $prefix . $_[0];
808 834
809 $line = (split('\n', $line))[0] . "\n" if ($terse); 835 $line = (split('\n', $line))[0] . "\n" if ($terse);
810 836
811 push(our @report, $line); 837 push(our @report, $line);
838
839 return 1;
812} 840}
813sub report_dump { 841sub report_dump {
814 our @report; 842 our @report;
815} 843}
816sub ERROR { 844sub ERROR {
817 report("ERROR: $_[0]\n"); 845 if (report("ERROR: $_[0]\n")) {
818 our $clean = 0; 846 our $clean = 0;
819 our $cnt_error++; 847 our $cnt_error++;
848 }
820} 849}
821sub WARN { 850sub WARN {
822 report("WARNING: $_[0]\n"); 851 if (report("WARNING: $_[0]\n")) {
823 our $clean = 0; 852 our $clean = 0;
824 our $cnt_warn++; 853 our $cnt_warn++;
854 }
825} 855}
826sub CHK { 856sub CHK {
827 if ($check) { 857 if ($check && report("CHECK: $_[0]\n")) {
828 report("CHECK: $_[0]\n");
829 our $clean = 0; 858 our $clean = 0;
830 our $cnt_chk++; 859 our $cnt_chk++;
831 } 860 }
@@ -867,30 +896,76 @@ sub process {
867 my $prev_values = 'E'; 896 my $prev_values = 'E';
868 897
869 # suppression flags 898 # suppression flags
870 my $suppress_ifbraces = 0; 899 my %suppress_ifbraces;
871 900
872 # Pre-scan the patch sanitizing the lines. 901 # Pre-scan the patch sanitizing the lines.
873 # Pre-scan the patch looking for any __setup documentation. 902 # Pre-scan the patch looking for any __setup documentation.
874 # 903 #
875 my @setup_docs = (); 904 my @setup_docs = ();
876 my $setup_docs = 0; 905 my $setup_docs = 0;
906
907 sanitise_line_reset();
877 my $line; 908 my $line;
878 foreach my $rawline (@rawlines) { 909 foreach my $rawline (@rawlines) {
879 # Standardise the strings and chars within the input to 910 $linenr++;
880 # simplify matching. 911 $line = $rawline;
881 $line = sanitise_line($rawline);
882 push(@lines, $line);
883
884 ##print "==>$rawline\n";
885 ##print "-->$line\n";
886 912
887 if ($line=~/^\+\+\+\s+(\S+)/) { 913 if ($rawline=~/^\+\+\+\s+(\S+)/) {
888 $setup_docs = 0; 914 $setup_docs = 0;
889 if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 915 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
890 $setup_docs = 1; 916 $setup_docs = 1;
891 } 917 }
892 next; 918 #next;
919 }
920 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
921 $realline=$1-1;
922 if (defined $2) {
923 $realcnt=$3+1;
924 } else {
925 $realcnt=1+1;
926 }
927
928 # Guestimate if this is a continuing comment. Run
929 # the context looking for a comment "edge". If this
930 # edge is a close comment then we must be in a comment
931 # at context start.
932 my $edge;
933 for (my $ln = $linenr; $ln < ($linenr + $realcnt); $ln++) {
934 next if ($line =~ /^-/);
935 ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
936 last if (defined $edge);
937 }
938 if (defined $edge && $edge eq '*/') {
939 $in_comment = 1;
940 }
941
942 # Guestimate if this is a continuing comment. If this
943 # is the start of a diff block and this line starts
944 # ' *' then it is very likely a comment.
945 if (!defined $edge &&
946 $rawlines[$linenr] =~ m@^.\s* \*(?:\s|$)@)
947 {
948 $in_comment = 1;
949 }
950
951 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
952 sanitise_line_reset($in_comment);
953
954 } elsif ($realcnt) {
955 # Standardise the strings and chars within the input to
956 # simplify matching.
957 $line = sanitise_line($rawline);
893 } 958 }
959 push(@lines, $line);
960
961 if ($realcnt > 1) {
962 $realcnt-- if ($line =~ /^(?:\+| |$)/);
963 } else {
964 $realcnt = 0;
965 }
966
967 #print "==>$rawline\n";
968 #print "-->$line\n";
894 969
895 if ($setup_docs && $line =~ /^\+/) { 970 if ($setup_docs && $line =~ /^\+/) {
896 push(@setup_docs, $line); 971 push(@setup_docs, $line);
@@ -899,23 +974,17 @@ sub process {
899 974
900 $prefix = ''; 975 $prefix = '';
901 976
977 $realcnt = 0;
978 $linenr = 0;
902 foreach my $line (@lines) { 979 foreach my $line (@lines) {
903 $linenr++; 980 $linenr++;
904 981
905 my $rawline = $rawlines[$linenr - 1]; 982 my $rawline = $rawlines[$linenr - 1];
906 983
907#extract the filename as it passes
908 if ($line=~/^\+\+\+\s+(\S+)/) {
909 $realfile=$1;
910 $realfile =~ s@^[^/]*/@@;
911 $in_comment = 0;
912 next;
913 }
914#extract the line range in the file after the patch is applied 984#extract the line range in the file after the patch is applied
915 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 985 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
916 $is_patch = 1; 986 $is_patch = 1;
917 $first_line = $linenr + 1; 987 $first_line = $linenr + 1;
918 $in_comment = 0;
919 $realline=$1-1; 988 $realline=$1-1;
920 if (defined $2) { 989 if (defined $2) {
921 $realcnt=$3+1; 990 $realcnt=$3+1;
@@ -925,50 +994,16 @@ sub process {
925 annotate_reset(); 994 annotate_reset();
926 $prev_values = 'E'; 995 $prev_values = 'E';
927 996
928 $suppress_ifbraces = $linenr - 1; 997 %suppress_ifbraces = ();
929 next; 998 next;
930 }
931 999
932# track the line number as we move through the hunk, note that 1000# track the line number as we move through the hunk, note that
933# new versions of GNU diff omit the leading space on completely 1001# new versions of GNU diff omit the leading space on completely
934# blank context lines so we need to count that too. 1002# blank context lines so we need to count that too.
935 if ($line =~ /^( |\+|$)/) { 1003 } elsif ($line =~ /^( |\+|$)/) {
936 $realline++; 1004 $realline++;
937 $realcnt-- if ($realcnt != 0); 1005 $realcnt-- if ($realcnt != 0);
938 1006
939 # Guestimate if this is a continuing comment. Run
940 # the context looking for a comment "edge". If this
941 # edge is a close comment then we must be in a comment
942 # at context start.
943 if ($linenr == $first_line) {
944 my $edge;
945 for (my $ln = $first_line; $ln < ($linenr + $realcnt); $ln++) {
946 ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
947 last if (defined $edge);
948 }
949 if (defined $edge && $edge eq '*/') {
950 $in_comment = 1;
951 }
952 }
953
954 # Guestimate if this is a continuing comment. If this
955 # is the start of a diff block and this line starts
956 # ' *' then it is very likely a comment.
957 if ($linenr == $first_line and $rawline =~ m@^.\s* \*(?:\s|$)@) {
958 $in_comment = 1;
959 }
960
961 # Find the last comment edge on _this_ line.
962 $comment_edge = 0;
963 while (($rawline =~ m@(/\*|\*/)@g)) {
964 if ($1 eq '/*') {
965 $in_comment = 1;
966 } else {
967 $in_comment = 0;
968 }
969 $comment_edge = 1;
970 }
971
972 # Measure the line length and indent. 1007 # Measure the line length and indent.
973 ($length, $indent) = line_stats($rawline); 1008 ($length, $indent) = line_stats($rawline);
974 1009
@@ -977,23 +1012,36 @@ sub process {
977 ($previndent, $stashindent) = ($stashindent, $indent); 1012 ($previndent, $stashindent) = ($stashindent, $indent);
978 ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 1013 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
979 1014
980 #warn "ic<$in_comment> ce<$comment_edge> line<$line>\n"; 1015 #warn "line<$line>\n";
981 1016
982 } elsif ($realcnt == 1) { 1017 } elsif ($realcnt == 1) {
983 $realcnt--; 1018 $realcnt--;
984 } 1019 }
985 1020
986#make up the handle for any error we report on this line 1021#make up the handle for any error we report on this line
1022 $prefix = "$filename:$realline: " if ($emacs && $file);
1023 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1024
987 $here = "#$linenr: " if (!$file); 1025 $here = "#$linenr: " if (!$file);
988 $here = "#$realline: " if ($file); 1026 $here = "#$realline: " if ($file);
1027
1028 # extract the filename as it passes
1029 if ($line=~/^\+\+\+\s+(\S+)/) {
1030 $realfile = $1;
1031 $realfile =~ s@^[^/]*/@@;
1032
1033 if ($realfile =~ m@include/asm/@) {
1034 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1035 }
1036 next;
1037 }
1038
989 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 1039 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
990 1040
991 my $hereline = "$here\n$rawline\n"; 1041 my $hereline = "$here\n$rawline\n";
992 my $herecurr = "$here\n$rawline\n"; 1042 my $herecurr = "$here\n$rawline\n";
993 my $hereprev = "$here\n$prevrawline\n$rawline\n"; 1043 my $hereprev = "$here\n$prevrawline\n$rawline\n";
994 1044
995 $prefix = "$filename:$realline: " if ($emacs && $file);
996 $prefix = "$filename:$linenr: " if ($emacs && !$file);
997 $cnt_lines++ if ($realcnt != 0); 1045 $cnt_lines++ if ($realcnt != 0);
998 1046
999#check the patch for a signoff: 1047#check the patch for a signoff:
@@ -1005,7 +1053,7 @@ sub process {
1005 $herecurr); 1053 $herecurr);
1006 } 1054 }
1007 if ($line =~ /^\s*signed-off-by:\S/i) { 1055 if ($line =~ /^\s*signed-off-by:\S/i) {
1008 WARN("need space after Signed-off-by:\n" . 1056 WARN("space required after Signed-off-by:\n" .
1009 $herecurr); 1057 $herecurr);
1010 } 1058 }
1011 } 1059 }
@@ -1072,11 +1120,6 @@ sub process {
1072 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr); 1120 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1073 } 1121 }
1074 1122
1075# The rest of our checks refer specifically to C style
1076# only apply those _outside_ comments. Only skip
1077# lines in the middle of comments.
1078 next if (!$comment_edge && $in_comment);
1079
1080# Check for potential 'bare' types 1123# Check for potential 'bare' types
1081 if ($realcnt) { 1124 if ($realcnt) {
1082 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 1125 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
@@ -1110,7 +1153,7 @@ sub process {
1110 my ($name_len) = length($1); 1153 my ($name_len) = length($1);
1111 1154
1112 my $ctx = $s; 1155 my $ctx = $s;
1113 substr($ctx, 0, $name_len + 1) = ''; 1156 substr($ctx, 0, $name_len + 1, '');
1114 $ctx =~ s/\)[^\)]*$//; 1157 $ctx =~ s/\)[^\)]*$//;
1115 1158
1116 for my $arg (split(/\s*,\s*/, $ctx)) { 1159 for my $arg (split(/\s*,\s*/, $ctx)) {
@@ -1151,27 +1194,33 @@ sub process {
1151 1194
1152# if/while/etc brace do not go on next line, unless defining a do while loop, 1195# if/while/etc brace do not go on next line, unless defining a do while loop,
1153# or if that brace on the next line is for something else 1196# or if that brace on the next line is for something else
1154 if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) { 1197 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
1198 my $pre_ctx = "$1$2";
1199
1155 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 1200 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1156 my $ctx_ln = $linenr + $#ctx + 1; 1201 my $ctx_ln = $linenr + $#ctx + 1;
1157 my $ctx_cnt = $realcnt - $#ctx - 1; 1202 my $ctx_cnt = $realcnt - $#ctx - 1;
1158 my $ctx = join("\n", @ctx); 1203 my $ctx = join("\n", @ctx);
1159 1204
1205 ##warn "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1206
1160 # Skip over any removed lines in the context following statement. 1207 # Skip over any removed lines in the context following statement.
1161 while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) { 1208 while (defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^-/) {
1162 $ctx_ln++; 1209 $ctx_ln++;
1163 $ctx_cnt--;
1164 } 1210 }
1165 ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>"; 1211 ##warn "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1166 1212
1167 if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 1213 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1168 ERROR("That open brace { should be on the previous line\n" . 1214 ERROR("that open brace { should be on the previous line\n" .
1169 "$here\n$ctx\n$lines[$ctx_ln - 1]"); 1215 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1170 } 1216 }
1171 if ($level == 0 && $ctx =~ /\)\s*\;\s*$/ && defined $lines[$ctx_ln - 1]) { 1217 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1218 $ctx =~ /\)\s*\;\s*$/ &&
1219 defined $lines[$ctx_ln - 1])
1220 {
1172 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 1221 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1173 if ($nindent > $indent) { 1222 if ($nindent > $indent) {
1174 WARN("Trailing semicolon indicates no statements, indent implies otherwise\n" . 1223 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1175 "$here\n$ctx\n$lines[$ctx_ln - 1]"); 1224 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1176 } 1225 }
1177 } 1226 }
@@ -1200,7 +1249,7 @@ sub process {
1200# check for initialisation to aggregates open brace on the next line 1249# check for initialisation to aggregates open brace on the next line
1201 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ && 1250 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1202 $line =~ /^.\s*{/) { 1251 $line =~ /^.\s*{/) {
1203 ERROR("That open brace { should be on the previous line\n" . $hereprev); 1252 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1204 } 1253 }
1205 1254
1206# 1255#
@@ -1325,22 +1374,31 @@ sub process {
1325# check for spaces between functions and their parentheses. 1374# check for spaces between functions and their parentheses.
1326 while ($line =~ /($Ident)\s+\(/g) { 1375 while ($line =~ /($Ident)\s+\(/g) {
1327 my $name = $1; 1376 my $name = $1;
1328 my $ctx = substr($line, 0, $-[1]); 1377 my $ctx_before = substr($line, 0, $-[1]);
1378 my $ctx = "$ctx_before$name";
1329 1379
1330 # Ignore those directives where spaces _are_ permitted. 1380 # Ignore those directives where spaces _are_ permitted.
1331 if ($name =~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case|__asm__)$/) { 1381 if ($name =~ /^(?:
1382 if|for|while|switch|return|case|
1383 volatile|__volatile__|
1384 __attribute__|format|__extension__|
1385 asm|__asm__)$/x)
1386 {
1332 1387
1333 # cpp #define statements have non-optional spaces, ie 1388 # cpp #define statements have non-optional spaces, ie
1334 # if there is a space between the name and the open 1389 # if there is a space between the name and the open
1335 # parenthesis it is simply not a parameter group. 1390 # parenthesis it is simply not a parameter group.
1336 } elsif ($ctx =~ /^.\#\s*define\s*$/) { 1391 } elsif ($ctx_before =~ /^.\#\s*define\s*$/) {
1392
1393 # cpp #elif statement condition may start with a (
1394 } elsif ($ctx =~ /^.\#\s*elif\s*$/) {
1337 1395
1338 # If this whole things ends with a type its most 1396 # If this whole things ends with a type its most
1339 # likely a typedef for a function. 1397 # likely a typedef for a function.
1340 } elsif ("$ctx$name" =~ /$Type$/) { 1398 } elsif ($ctx =~ /$Type$/) {
1341 1399
1342 } else { 1400 } else {
1343 WARN("no space between function name and open parenthesis '('\n" . $herecurr); 1401 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1344 } 1402 }
1345 } 1403 }
1346# Check operator spacing. 1404# Check operator spacing.
@@ -1359,13 +1417,21 @@ sub process {
1359 for (my $n = 0; $n < $#elements; $n += 2) { 1417 for (my $n = 0; $n < $#elements; $n += 2) {
1360 $off += length($elements[$n]); 1418 $off += length($elements[$n]);
1361 1419
1420 # Pick up the preceeding and succeeding characters.
1421 my $ca = substr($opline, 0, $off);
1422 my $cc = '';
1423 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1424 $cc = substr($opline, $off + length($elements[$n + 1]));
1425 }
1426 my $cb = "$ca$;$cc";
1427
1362 my $a = ''; 1428 my $a = '';
1363 $a = 'V' if ($elements[$n] ne ''); 1429 $a = 'V' if ($elements[$n] ne '');
1364 $a = 'W' if ($elements[$n] =~ /\s$/); 1430 $a = 'W' if ($elements[$n] =~ /\s$/);
1365 $a = 'C' if ($elements[$n] =~ /$;$/); 1431 $a = 'C' if ($elements[$n] =~ /$;$/);
1366 $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 1432 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1367 $a = 'O' if ($elements[$n] eq ''); 1433 $a = 'O' if ($elements[$n] eq '');
1368 $a = 'E' if ($elements[$n] eq '' && $n == 0); 1434 $a = 'E' if ($ca =~ /^\s*$/);
1369 1435
1370 my $op = $elements[$n + 1]; 1436 my $op = $elements[$n + 1];
1371 1437
@@ -1381,14 +1447,6 @@ sub process {
1381 $c = 'E'; 1447 $c = 'E';
1382 } 1448 }
1383 1449
1384 # Pick up the preceeding and succeeding characters.
1385 my $ca = substr($opline, 0, $off);
1386 my $cc = '';
1387 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1388 $cc = substr($opline, $off + length($elements[$n + 1]));
1389 }
1390 my $cb = "$ca$;$cc";
1391
1392 my $ctx = "${a}x${c}"; 1450 my $ctx = "${a}x${c}";
1393 1451
1394 my $at = "(ctx:$ctx)"; 1452 my $at = "(ctx:$ctx)";
@@ -1424,7 +1482,7 @@ sub process {
1424 } elsif ($op eq ';') { 1482 } elsif ($op eq ';') {
1425 if ($ctx !~ /.x[WEBC]/ && 1483 if ($ctx !~ /.x[WEBC]/ &&
1426 $cc !~ /^\\/ && $cc !~ /^;/) { 1484 $cc !~ /^\\/ && $cc !~ /^;/) {
1427 ERROR("need space after that '$op' $at\n" . $hereptr); 1485 ERROR("space required after that '$op' $at\n" . $hereptr);
1428 } 1486 }
1429 1487
1430 # // is a comment 1488 # // is a comment
@@ -1433,13 +1491,13 @@ sub process {
1433 # -> should have no spaces 1491 # -> should have no spaces
1434 } elsif ($op eq '->') { 1492 } elsif ($op eq '->') {
1435 if ($ctx =~ /Wx.|.xW/) { 1493 if ($ctx =~ /Wx.|.xW/) {
1436 ERROR("no spaces around that '$op' $at\n" . $hereptr); 1494 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
1437 } 1495 }
1438 1496
1439 # , must have a space on the right. 1497 # , must have a space on the right.
1440 } elsif ($op eq ',') { 1498 } elsif ($op eq ',') {
1441 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 1499 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1442 ERROR("need space after that '$op' $at\n" . $hereptr); 1500 ERROR("space required after that '$op' $at\n" . $hereptr);
1443 } 1501 }
1444 1502
1445 # '*' as part of a type definition -- reported already. 1503 # '*' as part of a type definition -- reported already.
@@ -1452,21 +1510,26 @@ sub process {
1452 } elsif ($op eq '!' || $op eq '~' || 1510 } elsif ($op eq '!' || $op eq '~' ||
1453 ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) { 1511 ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
1454 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 1512 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1455 ERROR("need space before that '$op' $at\n" . $hereptr); 1513 ERROR("space required before that '$op' $at\n" . $hereptr);
1456 } 1514 }
1457 if ($ctx =~ /.xW/) { 1515 if ($ctx =~ /.xW/) {
1458 ERROR("no space after that '$op' $at\n" . $hereptr); 1516 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1459 } 1517 }
1460 1518
1461 # unary ++ and unary -- are allowed no space on one side. 1519 # unary ++ and unary -- are allowed no space on one side.
1462 } elsif ($op eq '++' or $op eq '--') { 1520 } elsif ($op eq '++' or $op eq '--') {
1463 if ($ctx !~ /[WOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 1521 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1464 ERROR("need space one side of that '$op' $at\n" . $hereptr); 1522 ERROR("space required one side of that '$op' $at\n" . $hereptr);
1523 }
1524 if ($ctx =~ /Wx[BE]/ ||
1525 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1526 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1465 } 1527 }
1466 if ($ctx =~ /WxB/ || ($ctx =~ /Wx./ && $cc =~ /^;/)) { 1528 if ($ctx =~ /ExW/) {
1467 ERROR("no space before that '$op' $at\n" . $hereptr); 1529 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1468 } 1530 }
1469 1531
1532
1470 # << and >> may either have or not have spaces both sides 1533 # << and >> may either have or not have spaces both sides
1471 } elsif ($op eq '<<' or $op eq '>>' or 1534 } elsif ($op eq '<<' or $op eq '>>' or
1472 $op eq '&' or $op eq '^' or $op eq '|' or 1535 $op eq '&' or $op eq '^' or $op eq '|' or
@@ -1474,7 +1537,7 @@ sub process {
1474 $op eq '*' or $op eq '/' or 1537 $op eq '*' or $op eq '/' or
1475 $op eq '%') 1538 $op eq '%')
1476 { 1539 {
1477 if ($ctx !~ /VxV|WxW|VxE|WxE|VxO|Cx.|.xC/) { 1540 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1478 ERROR("need consistent spacing around '$op' $at\n" . 1541 ERROR("need consistent spacing around '$op' $at\n" .
1479 $hereptr); 1542 $hereptr);
1480 } 1543 }
@@ -1484,7 +1547,7 @@ sub process {
1484 # Ignore email addresses <foo@bar> 1547 # Ignore email addresses <foo@bar>
1485 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) && 1548 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
1486 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) { 1549 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
1487 ERROR("need spaces around that '$op' $at\n" . $hereptr); 1550 ERROR("spaces required around that '$op' $at\n" . $hereptr);
1488 } 1551 }
1489 } 1552 }
1490 $off += length($elements[$n + 1]); 1553 $off += length($elements[$n + 1]);
@@ -1514,31 +1577,31 @@ sub process {
1514#need space before brace following if, while, etc 1577#need space before brace following if, while, etc
1515 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 1578 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1516 $line =~ /do{/) { 1579 $line =~ /do{/) {
1517 ERROR("need a space before the open brace '{'\n" . $herecurr); 1580 ERROR("space required before the open brace '{'\n" . $herecurr);
1518 } 1581 }
1519 1582
1520# closing brace should have a space following it when it has anything 1583# closing brace should have a space following it when it has anything
1521# on the line 1584# on the line
1522 if ($line =~ /}(?!(?:,|;|\)))\S/) { 1585 if ($line =~ /}(?!(?:,|;|\)))\S/) {
1523 ERROR("need a space after that close brace '}'\n" . $herecurr); 1586 ERROR("space required after that close brace '}'\n" . $herecurr);
1524 } 1587 }
1525 1588
1526# check spacing on square brackets 1589# check spacing on square brackets
1527 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 1590 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1528 ERROR("no space after that open square bracket '['\n" . $herecurr); 1591 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
1529 } 1592 }
1530 if ($line =~ /\s\]/) { 1593 if ($line =~ /\s\]/) {
1531 ERROR("no space before that close square bracket ']'\n" . $herecurr); 1594 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
1532 } 1595 }
1533 1596
1534# check spacing on paretheses 1597# check spacing on paretheses
1535 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 1598 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1536 $line !~ /for\s*\(\s+;/) { 1599 $line !~ /for\s*\(\s+;/) {
1537 ERROR("no space after that open parenthesis '('\n" . $herecurr); 1600 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
1538 } 1601 }
1539 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 1602 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1540 $line !~ /for\s*\(.*;\s+\)/) { 1603 $line !~ /for\s*\(.*;\s+\)/) {
1541 ERROR("no space before that close parenthesis ')'\n" . $herecurr); 1604 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
1542 } 1605 }
1543 1606
1544#goto labels aren't indented, allow a single space however 1607#goto labels aren't indented, allow a single space however
@@ -1549,7 +1612,7 @@ sub process {
1549 1612
1550# Need a space before open parenthesis after if, while etc 1613# Need a space before open parenthesis after if, while etc
1551 if ($line=~/\b(if|while|for|switch)\(/) { 1614 if ($line=~/\b(if|while|for|switch)\(/) {
1552 ERROR("need a space before the open parenthesis '('\n" . $herecurr); 1615 ERROR("space required before the open parenthesis '('\n" . $herecurr);
1553 } 1616 }
1554 1617
1555# Check for illegal assignment in if conditional. 1618# Check for illegal assignment in if conditional.
@@ -1562,10 +1625,12 @@ sub process {
1562 1625
1563 # Find out what is on the end of the line after the 1626 # Find out what is on the end of the line after the
1564 # conditional. 1627 # conditional.
1565 substr($s, 0, length($c)) = ''; 1628 substr($s, 0, length($c), '');
1566 $s =~ s/\n.*//g; 1629 $s =~ s/\n.*//g;
1567 $s =~ s/$;//g; # Remove any comments 1630 $s =~ s/$;//g; # Remove any comments
1568 if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/) { 1631 if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/ &&
1632 $c !~ /^.\#\s*if/)
1633 {
1569 ERROR("trailing statements should be on next line\n" . $herecurr); 1634 ERROR("trailing statements should be on next line\n" . $herecurr);
1570 } 1635 }
1571 } 1636 }
@@ -1607,7 +1672,7 @@ sub process {
1607 1672
1608 # Find out what is on the end of the line after the 1673 # Find out what is on the end of the line after the
1609 # conditional. 1674 # conditional.
1610 substr($s, 0, length($c)) = ''; 1675 substr($s, 0, length($c), '');
1611 $s =~ s/\n.*//g; 1676 $s =~ s/\n.*//g;
1612 1677
1613 if ($s =~ /^\s*;/) { 1678 if ($s =~ /^\s*;/) {
@@ -1631,7 +1696,7 @@ sub process {
1631 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) { 1696 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
1632 my $checkfile = "$root/include/linux/$1.h"; 1697 my $checkfile = "$root/include/linux/$1.h";
1633 if (-f $checkfile && $1 ne 'irq.h') { 1698 if (-f $checkfile && $1 ne 'irq.h') {
1634 CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" . 1699 WARN("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
1635 $herecurr); 1700 $herecurr);
1636 } 1701 }
1637 } 1702 }
@@ -1692,15 +1757,24 @@ sub process {
1692 if ($#chunks > 0 && $level == 0) { 1757 if ($#chunks > 0 && $level == 0) {
1693 my $allowed = 0; 1758 my $allowed = 0;
1694 my $seen = 0; 1759 my $seen = 0;
1695 my $herectx = $here . "\n";; 1760 my $herectx = $here . "\n";
1696 my $ln = $linenr - 1; 1761 my $ln = $linenr - 1;
1697 for my $chunk (@chunks) { 1762 for my $chunk (@chunks) {
1698 my ($cond, $block) = @{$chunk}; 1763 my ($cond, $block) = @{$chunk};
1699 1764
1700 $herectx .= "$rawlines[$ln]\n[...]\n"; 1765 # If the condition carries leading newlines, then count those as offsets.
1766 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
1767 my $offset = statement_rawlines($whitespace) - 1;
1768
1769 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
1770
1771 # We have looked at and allowed this specific line.
1772 $suppress_ifbraces{$ln + $offset} = 1;
1773
1774 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
1701 $ln += statement_rawlines($block) - 1; 1775 $ln += statement_rawlines($block) - 1;
1702 1776
1703 substr($block, 0, length($cond)) = ''; 1777 substr($block, 0, length($cond), '');
1704 1778
1705 $seen++ if ($block =~ /^\s*{/); 1779 $seen++ if ($block =~ /^\s*{/);
1706 1780
@@ -1721,16 +1795,10 @@ sub process {
1721 if ($seen && !$allowed) { 1795 if ($seen && !$allowed) {
1722 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx); 1796 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
1723 } 1797 }
1724 # Either way we have looked over this whole
1725 # statement and said what needs to be said.
1726 $suppress_ifbraces = $endln;
1727 } 1798 }
1728 } 1799 }
1729 if ($linenr > $suppress_ifbraces && 1800 if (!defined $suppress_ifbraces{$linenr - 1} &&
1730 $line =~ /\b(if|while|for|else)\b/) { 1801 $line =~ /\b(if|while|for|else)\b/) {
1731 my ($level, $endln, @chunks) =
1732 ctx_statement_full($linenr, $realcnt, $-[0]);
1733
1734 my $allowed = 0; 1802 my $allowed = 0;
1735 1803
1736 # Check the pre-context. 1804 # Check the pre-context.
@@ -1738,10 +1806,15 @@ sub process {
1738 #print "APW: ALLOWED: pre<$1>\n"; 1806 #print "APW: ALLOWED: pre<$1>\n";
1739 $allowed = 1; 1807 $allowed = 1;
1740 } 1808 }
1809
1810 my ($level, $endln, @chunks) =
1811 ctx_statement_full($linenr, $realcnt, $-[0]);
1812
1741 # Check the condition. 1813 # Check the condition.
1742 my ($cond, $block) = @{$chunks[0]}; 1814 my ($cond, $block) = @{$chunks[0]};
1815 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
1743 if (defined $cond) { 1816 if (defined $cond) {
1744 substr($block, 0, length($cond)) = ''; 1817 substr($block, 0, length($cond), '');
1745 } 1818 }
1746 if (statement_lines($cond) > 1) { 1819 if (statement_lines($cond) > 1) {
1747 #print "APW: ALLOWED: cond<$cond>\n"; 1820 #print "APW: ALLOWED: cond<$cond>\n";
@@ -1759,7 +1832,7 @@ sub process {
1759 if (defined $chunks[1]) { 1832 if (defined $chunks[1]) {
1760 my ($cond, $block) = @{$chunks[1]}; 1833 my ($cond, $block) = @{$chunks[1]};
1761 if (defined $cond) { 1834 if (defined $cond) {
1762 substr($block, 0, length($cond)) = ''; 1835 substr($block, 0, length($cond), '');
1763 } 1836 }
1764 if ($block =~ /^\s*\{/) { 1837 if ($block =~ /^\s*\{/) {
1765 #print "APW: ALLOWED: chunk-1 block<$block>\n"; 1838 #print "APW: ALLOWED: chunk-1 block<$block>\n";
@@ -1882,6 +1955,28 @@ sub process {
1882 if ($line =~ /__FUNCTION__/) { 1955 if ($line =~ /__FUNCTION__/) {
1883 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); 1956 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
1884 } 1957 }
1958
1959# check for semaphores used as mutexes
1960 if ($line =~ /\b(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
1961 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
1962 }
1963# check for semaphores used as mutexes
1964 if ($line =~ /\binit_MUTEX_LOCKED\s*\(/) {
1965 WARN("consider using a completion\n" . $herecurr);
1966 }
1967# recommend strict_strto* over simple_strto*
1968 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
1969 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
1970 }
1971
1972# use of NR_CPUS is usually wrong
1973# ignore definitions of NR_CPUS and usage to define arrays as likely right
1974 if ($line =~ /\bNR_CPUS\b/ &&
1975 $line !~ /^.#\s*define\s+NR_CPUS\s+/ &&
1976 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/)
1977 {
1978 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
1979 }
1885 } 1980 }
1886 1981
1887 # If we have no input at all, then there is nothing to report on 1982 # If we have no input at all, then there is nothing to report on