aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl323
1 files changed, 206 insertions, 117 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 2086a856400a..2a7cef9726e4 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.14'; 12my $V = '0.15';
13 13
14use Getopt::Long qw(:config no_auto_abbrev); 14use Getopt::Long qw(:config no_auto_abbrev);
15 15
@@ -105,8 +105,7 @@ our $Sparse = qr{
105 __iomem| 105 __iomem|
106 __must_check| 106 __must_check|
107 __init_refok| 107 __init_refok|
108 __kprobes| 108 __kprobes
109 fastcall
110 }x; 109 }x;
111our $Attribute = qr{ 110our $Attribute = qr{
112 const| 111 const|
@@ -158,7 +157,10 @@ sub build_types {
158 \b 157 \b
159 (?:const\s+)? 158 (?:const\s+)?
160 (?:unsigned\s+)? 159 (?:unsigned\s+)?
161 $all 160 (?:
161 $all|
162 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)
163 )
162 (?:\s+$Sparse|\s+const)* 164 (?:\s+$Sparse|\s+const)*
163 \b 165 \b
164 }x; 166 }x;
@@ -362,6 +364,7 @@ sub ctx_statement_block {
362 364
363 my $type = ''; 365 my $type = '';
364 my $level = 0; 366 my $level = 0;
367 my $p;
365 my $c; 368 my $c;
366 my $len = 0; 369 my $len = 0;
367 370
@@ -386,6 +389,7 @@ sub ctx_statement_block {
386 last; 389 last;
387 } 390 }
388 } 391 }
392 $p = $c;
389 $c = substr($blk, $off, 1); 393 $c = substr($blk, $off, 1);
390 $remainder = substr($blk, $off); 394 $remainder = substr($blk, $off);
391 395
@@ -397,8 +401,9 @@ sub ctx_statement_block {
397 } 401 }
398 402
399 # An else is really a conditional as long as its not else if 403 # An else is really a conditional as long as its not else if
400 if ($level == 0 && $remainder =~ /(\s+else)(?:\s|{)/ && 404 if ($level == 0 && (!defined($p) || $p =~ /(?:\s|\})/) &&
401 $remainder !~ /\s+else\s+if\b/) { 405 $remainder =~ /(else)(?:\s|{)/ &&
406 $remainder !~ /else\s+if\b/) {
402 $coff = $off + length($1); 407 $coff = $off + length($1);
403 } 408 }
404 409
@@ -445,21 +450,73 @@ sub ctx_statement_block {
445 $line, $remain + 1, $off - $loff + 1, $level); 450 $line, $remain + 1, $off - $loff + 1, $level);
446} 451}
447 452
453sub statement_lines {
454 my ($stmt) = @_;
455
456 # Strip the diff line prefixes and rip blank lines at start and end.
457 $stmt =~ s/(^|\n)./$1/g;
458 $stmt =~ s/^\s*//;
459 $stmt =~ s/\s*$//;
460
461 my @stmt_lines = ($stmt =~ /\n/g);
462
463 return $#stmt_lines + 2;
464}
465
466sub statement_rawlines {
467 my ($stmt) = @_;
468
469 my @stmt_lines = ($stmt =~ /\n/g);
470
471 return $#stmt_lines + 2;
472}
473
474sub statement_block_size {
475 my ($stmt) = @_;
476
477 $stmt =~ s/(^|\n)./$1/g;
478 $stmt =~ s/^\s*{//;
479 $stmt =~ s/}\s*$//;
480 $stmt =~ s/^\s*//;
481 $stmt =~ s/\s*$//;
482
483 my @stmt_lines = ($stmt =~ /\n/g);
484 my @stmt_statements = ($stmt =~ /;/g);
485
486 my $stmt_lines = $#stmt_lines + 2;
487 my $stmt_statements = $#stmt_statements + 1;
488
489 if ($stmt_lines > $stmt_statements) {
490 return $stmt_lines;
491 } else {
492 return $stmt_statements;
493 }
494}
495
448sub ctx_statement_full { 496sub ctx_statement_full {
449 my ($linenr, $remain, $off) = @_; 497 my ($linenr, $remain, $off) = @_;
450 my ($statement, $condition, $level); 498 my ($statement, $condition, $level);
451 499
452 my (@chunks); 500 my (@chunks);
453 501
502 # Grab the first conditional/block pair.
454 ($statement, $condition, $linenr, $remain, $off, $level) = 503 ($statement, $condition, $linenr, $remain, $off, $level) =
455 ctx_statement_block($linenr, $remain, $off); 504 ctx_statement_block($linenr, $remain, $off);
456 #print "F: c<$condition> s<$statement>\n"; 505 #print "F: c<$condition> s<$statement>\n";
506 push(@chunks, [ $condition, $statement ]);
507 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
508 return ($level, $linenr, @chunks);
509 }
510
511 # Pull in the following conditional/block pairs and see if they
512 # could continue the statement.
457 for (;;) { 513 for (;;) {
458 push(@chunks, [ $condition, $statement ]);
459 last if (!($remain > 0 && $condition =~ /^.\s*(?:if|else|do)/));
460 ($statement, $condition, $linenr, $remain, $off, $level) = 514 ($statement, $condition, $linenr, $remain, $off, $level) =
461 ctx_statement_block($linenr, $remain, $off); 515 ctx_statement_block($linenr, $remain, $off);
462 #print "C: c<$condition> s<$statement>\n"; 516 #print "C: c<$condition> s<$statement> remain<$remain>\n";
517 last if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:else|do)\b/s));
518 #print "C: push\n";
519 push(@chunks, [ $condition, $statement ]);
463 } 520 }
464 521
465 return ($level, $linenr, @chunks); 522 return ($level, $linenr, @chunks);
@@ -593,13 +650,13 @@ sub cat_vet {
593} 650}
594 651
595my $av_preprocessor = 0; 652my $av_preprocessor = 0;
596my $av_paren = 0; 653my $av_pending;
597my @av_paren_type; 654my @av_paren_type;
598 655
599sub annotate_reset { 656sub annotate_reset {
600 $av_preprocessor = 0; 657 $av_preprocessor = 0;
601 $av_paren = 0; 658 $av_pending = '_';
602 @av_paren_type = (); 659 @av_paren_type = ('E');
603} 660}
604 661
605sub annotate_values { 662sub annotate_values {
@@ -611,12 +668,13 @@ sub annotate_values {
611 print "$stream\n" if ($dbg_values > 1); 668 print "$stream\n" if ($dbg_values > 1);
612 669
613 while (length($cur)) { 670 while (length($cur)) {
614 print " <$type> " if ($dbg_values > 1); 671 print " <" . join('', @av_paren_type) .
672 "> <$type> " if ($dbg_values > 1);
615 if ($cur =~ /^(\s+)/o) { 673 if ($cur =~ /^(\s+)/o) {
616 print "WS($1)\n" if ($dbg_values > 1); 674 print "WS($1)\n" if ($dbg_values > 1);
617 if ($1 =~ /\n/ && $av_preprocessor) { 675 if ($1 =~ /\n/ && $av_preprocessor) {
676 $type = pop(@av_paren_type);
618 $av_preprocessor = 0; 677 $av_preprocessor = 0;
619 $type = 'N';
620 } 678 }
621 679
622 } elsif ($cur =~ /^($Type)/) { 680 } elsif ($cur =~ /^($Type)/) {
@@ -626,11 +684,33 @@ sub annotate_values {
626 } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) { 684 } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
627 print "DEFINE($1)\n" if ($dbg_values > 1); 685 print "DEFINE($1)\n" if ($dbg_values > 1);
628 $av_preprocessor = 1; 686 $av_preprocessor = 1;
629 $av_paren_type[$av_paren] = 'N'; 687 $av_pending = 'N';
630 688
631 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if|else|elif|endif))/o) { 689 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if))/o) {
632 print "PRE($1)\n" if ($dbg_values > 1); 690 print "PRE_START($1)\n" if ($dbg_values > 1);
633 $av_preprocessor = 1; 691 $av_preprocessor = 1;
692
693 push(@av_paren_type, $type);
694 push(@av_paren_type, $type);
695 $type = 'N';
696
697 } elsif ($cur =~ /^(#\s*(?:else|elif))/o) {
698 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
699 $av_preprocessor = 1;
700
701 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
702
703 $type = 'N';
704
705 } elsif ($cur =~ /^(#\s*(?:endif))/o) {
706 print "PRE_END($1)\n" if ($dbg_values > 1);
707
708 $av_preprocessor = 1;
709
710 # Assume all arms of the conditional end as this
711 # one does, and continue as if the #endif was not here.
712 pop(@av_paren_type);
713 push(@av_paren_type, $type);
634 $type = 'N'; 714 $type = 'N';
635 715
636 } elsif ($cur =~ /^(\\\n)/o) { 716 } elsif ($cur =~ /^(\\\n)/o) {
@@ -639,13 +719,13 @@ sub annotate_values {
639 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 719 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
640 print "SIZEOF($1)\n" if ($dbg_values > 1); 720 print "SIZEOF($1)\n" if ($dbg_values > 1);
641 if (defined $2) { 721 if (defined $2) {
642 $av_paren_type[$av_paren] = 'V'; 722 $av_pending = 'V';
643 } 723 }
644 $type = 'N'; 724 $type = 'N';
645 725
646 } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) { 726 } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
647 print "COND($1)\n" if ($dbg_values > 1); 727 print "COND($1)\n" if ($dbg_values > 1);
648 $av_paren_type[$av_paren] = 'N'; 728 $av_pending = 'N';
649 $type = 'N'; 729 $type = 'N';
650 730
651 } elsif ($cur =~/^(return|case|else)/o) { 731 } elsif ($cur =~/^(return|case|else)/o) {
@@ -654,14 +734,14 @@ sub annotate_values {
654 734
655 } elsif ($cur =~ /^(\()/o) { 735 } elsif ($cur =~ /^(\()/o) {
656 print "PAREN('$1')\n" if ($dbg_values > 1); 736 print "PAREN('$1')\n" if ($dbg_values > 1);
657 $av_paren++; 737 push(@av_paren_type, $av_pending);
738 $av_pending = '_';
658 $type = 'N'; 739 $type = 'N';
659 740
660 } elsif ($cur =~ /^(\))/o) { 741 } elsif ($cur =~ /^(\))/o) {
661 $av_paren-- if ($av_paren > 0); 742 my $new_type = pop(@av_paren_type);
662 if (defined $av_paren_type[$av_paren]) { 743 if ($new_type ne '_') {
663 $type = $av_paren_type[$av_paren]; 744 $type = $new_type;
664 undef $av_paren_type[$av_paren];
665 print "PAREN('$1') -> $type\n" 745 print "PAREN('$1') -> $type\n"
666 if ($dbg_values > 1); 746 if ($dbg_values > 1);
667 } else { 747 } else {
@@ -670,7 +750,7 @@ sub annotate_values {
670 750
671 } elsif ($cur =~ /^($Ident)\(/o) { 751 } elsif ($cur =~ /^($Ident)\(/o) {
672 print "FUNC($1)\n" if ($dbg_values > 1); 752 print "FUNC($1)\n" if ($dbg_values > 1);
673 $av_paren_type[$av_paren] = 'V'; 753 $av_pending = 'V';
674 754
675 } elsif ($cur =~ /^($Ident|$Constant)/o) { 755 } elsif ($cur =~ /^($Ident|$Constant)/o) {
676 print "IDENT($1)\n" if ($dbg_values > 1); 756 print "IDENT($1)\n" if ($dbg_values > 1);
@@ -680,11 +760,11 @@ sub annotate_values {
680 print "ASSIGN($1)\n" if ($dbg_values > 1); 760 print "ASSIGN($1)\n" if ($dbg_values > 1);
681 $type = 'N'; 761 $type = 'N';
682 762
683 } elsif ($cur =~/^(;)/) { 763 } elsif ($cur =~/^(;|{|})/) {
684 print "END($1)\n" if ($dbg_values > 1); 764 print "END($1)\n" if ($dbg_values > 1);
685 $type = 'E'; 765 $type = 'E';
686 766
687 } elsif ($cur =~ /^(;|{|}|\?|:|\[)/o) { 767 } elsif ($cur =~ /^(;|\?|:|\[)/o) {
688 print "CLOSE($1)\n" if ($dbg_values > 1); 768 print "CLOSE($1)\n" if ($dbg_values > 1);
689 $type = 'N'; 769 $type = 'N';
690 770
@@ -988,7 +1068,7 @@ sub process {
988 } 1068 }
989 1069
990# check for RCS/CVS revision markers 1070# check for RCS/CVS revision markers
991 if ($rawline =~ /\$(Revision|Log|Id)(?:\$|)/) { 1071 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
992 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr); 1072 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
993 } 1073 }
994 1074
@@ -999,41 +1079,44 @@ sub process {
999 1079
1000# Check for potential 'bare' types 1080# Check for potential 'bare' types
1001 if ($realcnt) { 1081 if ($realcnt) {
1082 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1083 $s =~ s/\n./ /g;
1084 $s =~ s/{.*$//;
1085
1002 # Ignore goto labels. 1086 # Ignore goto labels.
1003 if ($line =~ /$Ident:\*$/) { 1087 if ($s =~ /$Ident:\*$/) {
1004 1088
1005 # Ignore functions being called 1089 # Ignore functions being called
1006 } elsif ($line =~ /^.\s*$Ident\s*\(/) { 1090 } elsif ($s =~ /^.\s*$Ident\s*\(/) {
1007 1091
1008 # definitions in global scope can only start with types 1092 # definitions in global scope can only start with types
1009 } elsif ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) { 1093 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) {
1010 possible($1, $line); 1094 possible($1, $s);
1011 1095
1012 # declarations always start with types 1096 # declarations always start with types
1013 } elsif ($prev_values eq 'E' && $line =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/) { 1097 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/) {
1014 possible($1); 1098 possible($1, $s);
1015 } 1099 }
1016 1100
1017 # any (foo ... *) is a pointer cast, and foo is a type 1101 # any (foo ... *) is a pointer cast, and foo is a type
1018 while ($line =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) { 1102 while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) {
1019 possible($1, $line); 1103 possible($1, $s);
1020 } 1104 }
1021 1105
1022 # Check for any sort of function declaration. 1106 # Check for any sort of function declaration.
1023 # int foo(something bar, other baz); 1107 # int foo(something bar, other baz);
1024 # void (*store_gdt)(x86_descr_ptr *); 1108 # void (*store_gdt)(x86_descr_ptr *);
1025 if ($prev_values eq 'E' && $line =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) { 1109 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) {
1026 my ($name_len) = length($1); 1110 my ($name_len) = length($1);
1027 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, $name_len);
1028 my $ctx = join("\n", @ctx);
1029 1111
1030 $ctx =~ s/\n.//; 1112 my $ctx = $s;
1031 substr($ctx, 0, $name_len + 1) = ''; 1113 substr($ctx, 0, $name_len + 1) = '';
1032 $ctx =~ s/\)[^\)]*$//; 1114 $ctx =~ s/\)[^\)]*$//;
1115
1033 for my $arg (split(/\s*,\s*/, $ctx)) { 1116 for my $arg (split(/\s*,\s*/, $ctx)) {
1034 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) { 1117 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
1035 1118
1036 possible($1, $line); 1119 possible($1, $s);
1037 } 1120 }
1038 } 1121 }
1039 } 1122 }
@@ -1100,8 +1183,8 @@ sub process {
1100 $curr_values = $prev_values . $curr_values; 1183 $curr_values = $prev_values . $curr_values;
1101 if ($dbg_values) { 1184 if ($dbg_values) {
1102 my $outline = $opline; $outline =~ s/\t/ /g; 1185 my $outline = $opline; $outline =~ s/\t/ /g;
1103 warn "--> .$outline\n"; 1186 print "$linenr > .$outline\n";
1104 warn "--> $curr_values\n"; 1187 print "$linenr > $curr_values\n";
1105 } 1188 }
1106 $prev_values = substr($curr_values, -1); 1189 $prev_values = substr($curr_values, -1);
1107 1190
@@ -1148,7 +1231,9 @@ sub process {
1148 if (($prevline !~ /^}/) && 1231 if (($prevline !~ /^}/) &&
1149 ($prevline !~ /^\+}/) && 1232 ($prevline !~ /^\+}/) &&
1150 ($prevline !~ /^ }/) && 1233 ($prevline !~ /^ }/) &&
1151 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) { 1234 ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1235 ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
1236 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
1152 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 1237 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1153 } 1238 }
1154 } 1239 }
@@ -1266,7 +1351,7 @@ sub process {
1266 =>|->|<<|>>|<|>|=|!|~| 1351 =>|->|<<|>>|<|>|=|!|~|
1267 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% 1352 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1268 }x; 1353 }x;
1269 my @elements = split(/($;+|$ops|;)/, $opline); 1354 my @elements = split(/($ops|;)/, $opline);
1270 my $off = 0; 1355 my $off = 0;
1271 1356
1272 my $blank = copy_spacing($opline); 1357 my $blank = copy_spacing($opline);
@@ -1277,6 +1362,7 @@ sub process {
1277 my $a = ''; 1362 my $a = '';
1278 $a = 'V' if ($elements[$n] ne ''); 1363 $a = 'V' if ($elements[$n] ne '');
1279 $a = 'W' if ($elements[$n] =~ /\s$/); 1364 $a = 'W' if ($elements[$n] =~ /\s$/);
1365 $a = 'C' if ($elements[$n] =~ /$;$/);
1280 $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 1366 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1281 $a = 'O' if ($elements[$n] eq ''); 1367 $a = 'O' if ($elements[$n] eq '');
1282 $a = 'E' if ($elements[$n] eq '' && $n == 0); 1368 $a = 'E' if ($elements[$n] eq '' && $n == 0);
@@ -1287,6 +1373,7 @@ sub process {
1287 if (defined $elements[$n + 2]) { 1373 if (defined $elements[$n + 2]) {
1288 $c = 'V' if ($elements[$n + 2] ne ''); 1374 $c = 'V' if ($elements[$n + 2] ne '');
1289 $c = 'W' if ($elements[$n + 2] =~ /^\s/); 1375 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1376 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1290 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 1377 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1291 $c = 'O' if ($elements[$n + 2] eq ''); 1378 $c = 'O' if ($elements[$n + 2] eq '');
1292 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/); 1379 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
@@ -1330,13 +1417,13 @@ sub process {
1330 if ($op_type ne 'V' && 1417 if ($op_type ne 'V' &&
1331 $ca =~ /\s$/ && $cc =~ /^\s*,/) { 1418 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1332 1419
1333 # Ignore comments 1420# # Ignore comments
1334 } elsif ($op =~ /^$;+$/) { 1421# } elsif ($op =~ /^$;+$/) {
1335 1422
1336 # ; should have either the end of line or a space or \ after it 1423 # ; should have either the end of line or a space or \ after it
1337 } elsif ($op eq ';') { 1424 } elsif ($op eq ';') {
1338 if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ && 1425 if ($ctx !~ /.x[WEBC]/ &&
1339 $cc !~ /^;/) { 1426 $cc !~ /^\\/ && $cc !~ /^;/) {
1340 ERROR("need space after that '$op' $at\n" . $hereptr); 1427 ERROR("need space after that '$op' $at\n" . $hereptr);
1341 } 1428 }
1342 1429
@@ -1351,7 +1438,7 @@ sub process {
1351 1438
1352 # , must have a space on the right. 1439 # , must have a space on the right.
1353 } elsif ($op eq ',') { 1440 } elsif ($op eq ',') {
1354 if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) { 1441 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1355 ERROR("need space after that '$op' $at\n" . $hereptr); 1442 ERROR("need space after that '$op' $at\n" . $hereptr);
1356 } 1443 }
1357 1444
@@ -1364,7 +1451,7 @@ sub process {
1364 # unary operator, or a cast 1451 # unary operator, or a cast
1365 } elsif ($op eq '!' || $op eq '~' || 1452 } elsif ($op eq '!' || $op eq '~' ||
1366 ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) { 1453 ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
1367 if ($ctx !~ /[WEB]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 1454 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1368 ERROR("need space before that '$op' $at\n" . $hereptr); 1455 ERROR("need space before that '$op' $at\n" . $hereptr);
1369 } 1456 }
1370 if ($ctx =~ /.xW/) { 1457 if ($ctx =~ /.xW/) {
@@ -1373,7 +1460,7 @@ sub process {
1373 1460
1374 # unary ++ and unary -- are allowed no space on one side. 1461 # unary ++ and unary -- are allowed no space on one side.
1375 } elsif ($op eq '++' or $op eq '--') { 1462 } elsif ($op eq '++' or $op eq '--') {
1376 if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) { 1463 if ($ctx !~ /[WOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1377 ERROR("need space one side of that '$op' $at\n" . $hereptr); 1464 ERROR("need space one side of that '$op' $at\n" . $hereptr);
1378 } 1465 }
1379 if ($ctx =~ /WxB/ || ($ctx =~ /Wx./ && $cc =~ /^;/)) { 1466 if ($ctx =~ /WxB/ || ($ctx =~ /Wx./ && $cc =~ /^;/)) {
@@ -1387,13 +1474,13 @@ sub process {
1387 $op eq '*' or $op eq '/' or 1474 $op eq '*' or $op eq '/' or
1388 $op eq '%') 1475 $op eq '%')
1389 { 1476 {
1390 if ($ctx !~ /VxV|WxW|VxE|WxE|VxO/) { 1477 if ($ctx !~ /VxV|WxW|VxE|WxE|VxO|Cx.|.xC/) {
1391 ERROR("need consistent spacing around '$op' $at\n" . 1478 ERROR("need consistent spacing around '$op' $at\n" .
1392 $hereptr); 1479 $hereptr);
1393 } 1480 }
1394 1481
1395 # All the others need spaces both sides. 1482 # All the others need spaces both sides.
1396 } elsif ($ctx !~ /[EW]x[WE]/) { 1483 } elsif ($ctx !~ /[EWC]x[CWE]/) {
1397 # Ignore email addresses <foo@bar> 1484 # Ignore email addresses <foo@bar>
1398 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) && 1485 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
1399 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) { 1486 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
@@ -1551,7 +1638,7 @@ sub process {
1551 1638
1552# multi-statement macros should be enclosed in a do while loop, grab the 1639# multi-statement macros should be enclosed in a do while loop, grab the
1553# first statement and ensure its the whole macro if its not enclosed 1640# first statement and ensure its the whole macro if its not enclosed
1554# in a known goot container 1641# in a known good container
1555 if ($prevline =~ /\#define.*\\/ && 1642 if ($prevline =~ /\#define.*\\/ &&
1556 $prevline !~/(?:do\s+{|\(\{|\{)/ && 1643 $prevline !~/(?:do\s+{|\(\{|\{)/ &&
1557 $line !~ /(?:do\s+{|\(\{|\{)/ && 1644 $line !~ /(?:do\s+{|\(\{|\{)/ &&
@@ -1599,84 +1686,95 @@ sub process {
1599# check for redundant bracing round if etc 1686# check for redundant bracing round if etc
1600 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 1687 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
1601 my ($level, $endln, @chunks) = 1688 my ($level, $endln, @chunks) =
1602 ctx_statement_full($linenr, $realcnt, 0); 1689 ctx_statement_full($linenr, $realcnt, 1);
1603 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 1690 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
1604 if ($#chunks > 1 && $level == 0) { 1691 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
1692 if ($#chunks > 0 && $level == 0) {
1605 my $allowed = 0; 1693 my $allowed = 0;
1606 my $seen = 0; 1694 my $seen = 0;
1695 my $herectx = $here . "\n";;
1696 my $ln = $linenr - 1;
1607 for my $chunk (@chunks) { 1697 for my $chunk (@chunks) {
1608 my ($cond, $block) = @{$chunk}; 1698 my ($cond, $block) = @{$chunk};
1609 1699
1700 $herectx .= "$rawlines[$ln]\n[...]\n";
1701 $ln += statement_rawlines($block) - 1;
1702
1610 substr($block, 0, length($cond)) = ''; 1703 substr($block, 0, length($cond)) = '';
1611 1704
1612 $seen++ if ($block =~ /^\s*{/); 1705 $seen++ if ($block =~ /^\s*{/);
1613 1706
1614 $block =~ s/(^|\n)./$1/g; 1707 #print "cond<$cond> block<$block> allowed<$allowed>\n";
1615 $block =~ s/^\s*{//; 1708 if (statement_lines($cond) > 1) {
1616 $block =~ s/}\s*$//; 1709 #print "APW: ALLOWED: cond<$cond>\n";
1617 $block =~ s/^\s*//;
1618 $block =~ s/\s*$//;
1619
1620 my @lines = ($block =~ /\n/g);
1621 my @statements = ($block =~ /;/g);
1622
1623 #print "cond<$cond> block<$block> lines<" . scalar(@lines) . "> statements<" . scalar(@statements) . "> seen<$seen> allowed<$allowed>\n";
1624 if (scalar(@lines) != 0) {
1625 $allowed = 1; 1710 $allowed = 1;
1626 } 1711 }
1627 if ($block =~/\b(?:if|for|while)\b/) { 1712 if ($block =~/\b(?:if|for|while)\b/) {
1713 #print "APW: ALLOWED: block<$block>\n";
1628 $allowed = 1; 1714 $allowed = 1;
1629 } 1715 }
1630 if (scalar(@statements) > 1) { 1716 if (statement_block_size($block) > 1) {
1717 #print "APW: ALLOWED: lines block<$block>\n";
1631 $allowed = 1; 1718 $allowed = 1;
1632 } 1719 }
1633 } 1720 }
1634 if ($seen && !$allowed) { 1721 if ($seen && !$allowed) {
1635 WARN("braces {} are not necessary for any arm of this statement\n" . $herecurr); 1722 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
1636 $suppress_ifbraces = $endln;
1637 } 1723 }
1724 # Either way we have looked over this whole
1725 # statement and said what needs to be said.
1726 $suppress_ifbraces = $endln;
1638 } 1727 }
1639 } 1728 }
1640 if ($linenr > $suppress_ifbraces && 1729 if ($linenr > $suppress_ifbraces &&
1641 $line =~ /\b(if|while|for|else)\b/) { 1730 $line =~ /\b(if|while|for|else)\b/) {
1642 # Locate the end of the opening statement. 1731 my ($level, $endln, @chunks) =
1643 my @control = ctx_statement($linenr, $realcnt, 0); 1732 ctx_statement_full($linenr, $realcnt, $-[0]);
1644 my $nr = $linenr + (scalar(@control) - 1); 1733
1645 my $cnt = $realcnt - (scalar(@control) - 1); 1734 my $allowed = 0;
1646 1735
1647 my $off = $realcnt - $cnt; 1736 # Check the pre-context.
1648 #print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n"; 1737 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
1649 1738 #print "APW: ALLOWED: pre<$1>\n";
1650 # If this is is a braced statement group check it 1739 $allowed = 1;
1651 if ($lines[$nr - 1] =~ /{\s*$/) { 1740 }
1652 my ($lvl, @block) = ctx_block_level($nr, $cnt); 1741 # Check the condition.
1653 1742 my ($cond, $block) = @{$chunks[0]};
1654 my $stmt = join("\n", @block); 1743 if (defined $cond) {
1655 # Drop the diff line leader. 1744 substr($block, 0, length($cond)) = '';
1656 $stmt =~ s/\n./\n/g; 1745 }
1657 # Drop the code outside the block. 1746 if (statement_lines($cond) > 1) {
1658 $stmt =~ s/(^[^{]*){\s*//; 1747 #print "APW: ALLOWED: cond<$cond>\n";
1659 my $before = $1; 1748 $allowed = 1;
1660 $stmt =~ s/\s*}([^}]*$)//; 1749 }
1661 my $after = $1; 1750 if ($block =~/\b(?:if|for|while)\b/) {
1662 1751 #print "APW: ALLOWED: block<$block>\n";
1663 #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n"; 1752 $allowed = 1;
1664 #print "before<$before> stmt<$stmt> after<$after>\n\n"; 1753 }
1665 1754 if (statement_block_size($block) > 1) {
1666 # Count the newlines, if there is only one 1755 #print "APW: ALLOWED: lines block<$block>\n";
1667 # then the block should not have {}'s. 1756 $allowed = 1;
1668 my @lines = ($stmt =~ /\n/g); 1757 }
1669 my @statements = ($stmt =~ /;/g); 1758 # Check the post-context.
1670 #print "lines<" . scalar(@lines) . ">\n"; 1759 if (defined $chunks[1]) {
1671 #print "statements<" . scalar(@statements) . ">\n"; 1760 my ($cond, $block) = @{$chunks[1]};
1672 if ($lvl == 0 && scalar(@lines) == 0 && 1761 if (defined $cond) {
1673 scalar(@statements) < 2 && 1762 substr($block, 0, length($cond)) = '';
1674 $stmt !~ /{/ && $stmt !~ /\bif\b/ && 1763 }
1675 $before !~ /}/ && $after !~ /{/) { 1764 if ($block =~ /^\s*\{/) {
1676 my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n"; 1765 #print "APW: ALLOWED: chunk-1 block<$block>\n";
1677 shift(@block); 1766 $allowed = 1;
1678 WARN("braces {} are not necessary for single statement blocks\n" . $herectx); 1767 }
1768 }
1769 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
1770 my $herectx = $here . "\n";;
1771 my $end = $linenr + statement_rawlines($block) - 1;
1772
1773 for (my $ln = $linenr - 1; $ln < $end; $ln++) {
1774 $herectx .= $rawlines[$ln] . "\n";;
1679 } 1775 }
1776
1777 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
1680 } 1778 }
1681 } 1779 }
1682 1780
@@ -1828,15 +1926,6 @@ sub process {
1828 print "are false positives report them to the maintainer, see\n"; 1926 print "are false positives report them to the maintainer, see\n";
1829 print "CHECKPATCH in MAINTAINERS.\n"; 1927 print "CHECKPATCH in MAINTAINERS.\n";
1830 } 1928 }
1831 print <<EOL if ($file == 1 && $quiet == 0);
1832
1833WARNING: Using --file mode. Please do not send patches to linux-kernel
1834that change whole existing files if you did not significantly change most
1835of the the file for other reasons anyways or just wrote the file newly
1836from scratch. Pure code style patches have a significant cost in a
1837quickly changing code base like Linux because they cause rejects
1838with other changes.
1839EOL
1840 1929
1841 return $clean; 1930 return $clean;
1842} 1931}