aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl374
1 files changed, 178 insertions, 196 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 277c32647f36..25e20a27fc59 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.06'; 12my $V = '0.07';
13 13
14use Getopt::Long qw(:config no_auto_abbrev); 14use Getopt::Long qw(:config no_auto_abbrev);
15 15
@@ -246,6 +246,19 @@ sub cat_vet {
246 return $vet; 246 return $vet;
247} 247}
248 248
249sub ERROR {
250 print "ERROR: $_[0]\n";
251 our $clean = 0;
252}
253sub WARN {
254 print "WARNING: $_[0]\n";
255 our $clean = 0;
256}
257sub CHK {
258 print "CHECK: $_[0]\n";
259 our $clean = 0;
260}
261
249sub process { 262sub process {
250 my $filename = shift; 263 my $filename = shift;
251 my @lines = @_; 264 my @lines = @_;
@@ -259,7 +272,7 @@ sub process {
259 my $previndent=0; 272 my $previndent=0;
260 my $stashindent=0; 273 my $stashindent=0;
261 274
262 my $clean = 1; 275 our $clean = 1;
263 my $signoff = 0; 276 my $signoff = 0;
264 my $is_patch = 0; 277 my $is_patch = 0;
265 278
@@ -290,8 +303,11 @@ sub process {
290 long\s+int| 303 long\s+int|
291 long\s+long| 304 long\s+long|
292 long\s+long\s+int| 305 long\s+long\s+int|
306 u8|u16|u32|u64|
307 s8|s16|s32|s64|
293 struct\s+$Ident| 308 struct\s+$Ident|
294 union\s+$Ident| 309 union\s+$Ident|
310 enum\s+$Ident|
295 ${Ident}_t 311 ${Ident}_t
296 ) 312 )
297 (?:\s+$Sparse)* 313 (?:\s+$Sparse)*
@@ -304,6 +320,23 @@ sub process {
304 my $Declare = qr{(?:$Storage\s+)?$Type}; 320 my $Declare = qr{(?:$Storage\s+)?$Type};
305 my $Attribute = qr{__read_mostly|__init|__initdata}; 321 my $Attribute = qr{__read_mostly|__init|__initdata};
306 322
323 # Pre-scan the patch looking for any __setup documentation.
324 my @setup_docs = ();
325 my $setup_docs = 0;
326 foreach my $line (@lines) {
327 if ($line=~/^\+\+\+\s+(\S+)/) {
328 $setup_docs = 0;
329 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
330 $setup_docs = 1;
331 }
332 next;
333 }
334
335 if ($setup_docs && $line =~ /^\+/) {
336 push(@setup_docs, $line);
337 }
338 }
339
307 foreach my $line (@lines) { 340 foreach my $line (@lines) {
308 $linenr++; 341 $linenr++;
309 342
@@ -369,30 +402,42 @@ sub process {
369 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 402 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
370 403
371 my $hereline = "$here\n$line\n"; 404 my $hereline = "$here\n$line\n";
372 my $herecurr = "$here\n$line\n\n"; 405 my $herecurr = "$here\n$line\n";
373 my $hereprev = "$here\n$prevline\n$line\n\n"; 406 my $hereprev = "$here\n$prevline\n$line\n";
374 407
375#check the patch for a signoff: 408#check the patch for a signoff:
376 if ($line =~ /^\s*signed-off-by:/i) { 409 if ($line =~ /^\s*signed-off-by:/i) {
377 # This is a signoff, if ugly, so do not double report. 410 # This is a signoff, if ugly, so do not double report.
378 $signoff++; 411 $signoff++;
379 if (!($line =~ /^\s*Signed-off-by:/)) { 412 if (!($line =~ /^\s*Signed-off-by:/)) {
380 print "Signed-off-by: is the preferred form\n"; 413 WARN("Signed-off-by: is the preferred form\n" .
381 print "$herecurr"; 414 $herecurr);
382 $clean = 0;
383 } 415 }
384 if ($line =~ /^\s*signed-off-by:\S/i) { 416 if ($line =~ /^\s*signed-off-by:\S/i) {
385 print "need space after Signed-off-by:\n"; 417 WARN("need space after Signed-off-by:\n" .
386 print "$herecurr"; 418 $herecurr);
387 $clean = 0;
388 } 419 }
389 } 420 }
390 421
391# Check for wrappage within a valid hunk of the file 422# Check for wrappage within a valid hunk of the file
392 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) { 423 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) {
393 print "patch seems to be corrupt (line wrapped?) [$realcnt]\n"; 424 ERROR("patch seems to be corrupt (line wrapped?)\n" .
394 print "$herecurr"; 425 $herecurr);
395 $clean = 0; 426 }
427
428# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
429 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
430 !($line =~ m/^(
431 [\x09\x0A\x0D\x20-\x7E] # ASCII
432 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
433 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
434 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
435 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
436 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
437 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
438 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
439 )*$/x )) {
440 ERROR("Invalid UTF-8\n" . $herecurr);
396 } 441 }
397 442
398#ignore lines being removed 443#ignore lines being removed
@@ -403,16 +448,12 @@ sub process {
403 448
404#trailing whitespace 449#trailing whitespace
405 if ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) { 450 if ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) {
406 my $herevet = "$here\n" . cat_vet($line) . "\n\n"; 451 my $herevet = "$here\n" . cat_vet($line) . "\n";
407 print "trailing whitespace\n"; 452 ERROR("trailing whitespace\n" . $herevet);
408 print "$herevet";
409 $clean = 0;
410 } 453 }
411#80 column limit 454#80 column limit
412 if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) { 455 if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) {
413 print "line over 80 characters\n"; 456 WARN("line over 80 characters\n" . $herecurr);
414 print "$herecurr";
415 $clean = 0;
416 } 457 }
417 458
418# check we are in a valid source file *.[hc] if not then ignore this hunk 459# check we are in a valid source file *.[hc] if not then ignore this hunk
@@ -421,10 +462,8 @@ sub process {
421# at the beginning of a line any tabs must come first and anything 462# at the beginning of a line any tabs must come first and anything
422# more than 8 must use tabs. 463# more than 8 must use tabs.
423 if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) { 464 if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) {
424 my $herevet = "$here\n" . cat_vet($line) . "\n\n"; 465 my $herevet = "$here\n" . cat_vet($line) . "\n";
425 print "use tabs not spaces\n"; 466 ERROR("use tabs not spaces\n" . $herevet);
426 print "$herevet";
427 $clean = 0;
428 } 467 }
429 468
430 # 469 #
@@ -463,9 +502,27 @@ sub process {
463 } 502 }
464 } 503 }
465 if ($err ne '') { 504 if ($err ne '') {
466 print "switch and case should be at the same indent\n"; 505 ERROR("switch and case should be at the same indent\n$hereline\n$err\n");
467 print "$here\n$line\n$err\n"; 506 }
468 $clean = 0; 507 }
508
509# if/while/etc brace do not go on next line, unless defining a do while loop,
510# or if that brace on the next line is for something else
511 if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
512 my @ctx = ctx_statement($linenr, $realcnt);
513 my $ctx_ln = $linenr + $#ctx + 1;
514 my $ctx_cnt = $realcnt - $#ctx - 1;
515 my $ctx = join("\n", @ctx);
516
517 while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
518 $ctx_ln++;
519 $ctx_cnt--;
520 }
521 ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
522
523 if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
524 ERROR("That { should be on the previous line\n" .
525 "$here\n$ctx\n$lines[$ctx_ln - 1]");
469 } 526 }
470 } 527 }
471 528
@@ -474,9 +531,7 @@ sub process {
474 531
475# TEST: allow direct testing of the type matcher. 532# TEST: allow direct testing of the type matcher.
476 if ($tst_type && $line =~ /^.$Declare$/) { 533 if ($tst_type && $line =~ /^.$Declare$/) {
477 print "TEST: is type $Declare\n"; 534 ERROR("TEST: is type $Declare\n" . $herecurr);
478 print "$herecurr";
479 $clean = 0;
480 next; 535 next;
481 } 536 }
482 537
@@ -488,9 +543,8 @@ sub process {
488 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) { 543 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
489 my $path = $1; 544 my $path = $1;
490 if ($path =~ m{//}) { 545 if ($path =~ m{//}) {
491 print "malformed #include filename\n"; 546 ERROR("malformed #include filename\n" .
492 print "$herecurr"; 547 $herecurr);
493 $clean = 0;
494 } 548 }
495 # Sanitise this special form of string. 549 # Sanitise this special form of string.
496 $path = 'X' x length($path); 550 $path = 'X' x length($path);
@@ -499,9 +553,7 @@ sub process {
499 553
500# no C99 // comments 554# no C99 // comments
501 if ($line =~ m{//}) { 555 if ($line =~ m{//}) {
502 print "do not use C99 // comments\n"; 556 ERROR("do not use C99 // comments\n" . $herecurr);
503 print "$herecurr";
504 $clean = 0;
505 } 557 }
506 # Remove C99 comments. 558 # Remove C99 comments.
507 $line =~ s@//.*@@; 559 $line =~ s@//.*@@;
@@ -514,49 +566,40 @@ sub process {
514 ($prevline !~ /^\+}/) && 566 ($prevline !~ /^\+}/) &&
515 ($prevline !~ /^ }/) && 567 ($prevline !~ /^ }/) &&
516 ($prevline !~ /\s$name(?:\s+$Attribute)?\s*(?:;|=)/)) { 568 ($prevline !~ /\s$name(?:\s+$Attribute)?\s*(?:;|=)/)) {
517 print "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n"; 569 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
518 print "$herecurr";
519 $clean = 0;
520 } 570 }
521 } 571 }
522 572
523# check for static initialisers. 573# check for static initialisers.
524 if ($line=~/\s*static\s.*=\s+(0|NULL);/) { 574 if ($line=~/\s*static\s.*=\s+(0|NULL);/) {
525 print "do not initialise statics to 0 or NULL\n"; 575 ERROR("do not initialise statics to 0 or NULL\n" .
526 print "$herecurr"; 576 $herecurr);
527 $clean = 0;
528 } 577 }
529 578
530# check for new typedefs, only function parameters and sparse annotations 579# check for new typedefs, only function parameters and sparse annotations
531# make sense. 580# make sense.
532 if ($line =~ /\btypedef\s/ && 581 if ($line =~ /\btypedef\s/ &&
533 $line !~ /\btypedef\s+$Type\s+\(\s*$Ident\s*\)\s*\(/ && 582 $line !~ /\btypedef\s+$Type\s+\(\s*\*$Ident\s*\)\s*\(/ &&
534 $line !~ /\b__bitwise(?:__|)\b/) { 583 $line !~ /\b__bitwise(?:__|)\b/) {
535 print "do not add new typedefs\n"; 584 WARN("do not add new typedefs\n" . $herecurr);
536 print "$herecurr";
537 $clean = 0;
538 } 585 }
539 586
540# * goes on variable not on type 587# * goes on variable not on type
541 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) { 588 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
542 print "\"(foo$1)\" should be \"(foo $1)\"\n"; 589 ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
543 print "$herecurr"; 590 $herecurr);
544 $clean = 0;
545 591
546 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) { 592 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
547 print "\"(foo $1 )\" should be \"(foo $1)\"\n"; 593 ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
548 print "$herecurr"; 594 $herecurr);
549 $clean = 0;
550 595
551 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+const)?\s+[A-Za-z\d_]+}) { 596 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+const)?\s+[A-Za-z\d_]+}) {
552 print "\"foo$1 bar\" should be \"foo $1bar\"\n"; 597 ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
553 print "$herecurr"; 598 $herecurr);
554 $clean = 0;
555 599
556 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+const)\s+[A-Za-z\d_]+}) { 600 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+const)\s+[A-Za-z\d_]+}) {
557 print "\"foo $1 bar\" should be \"foo $1bar\"\n"; 601 ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
558 print "$herecurr"; 602 $herecurr);
559 $clean = 0;
560 } 603 }
561 604
562# # no BUG() or BUG_ON() 605# # no BUG() or BUG_ON()
@@ -585,9 +628,7 @@ sub process {
585 } 628 }
586 } 629 }
587 if ($ok == 0) { 630 if ($ok == 0) {
588 print "printk() should include KERN_ facility level\n"; 631 WARN("printk() should include KERN_ facility level\n" . $herecurr);
589 print "$herecurr";
590 $clean = 0;
591 } 632 }
592 } 633 }
593 634
@@ -595,9 +636,7 @@ sub process {
595# or if closed on same line 636# or if closed on same line
596 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and 637 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and
597 !($line=~/\#define.*do\s{/) and !($line=~/}/)) { 638 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
598 print "braces following function declarations go on the next line\n"; 639 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
599 print "$herecurr";
600 $clean = 0;
601 } 640 }
602 641
603# Check operator spacing. 642# Check operator spacing.
@@ -633,27 +672,27 @@ sub process {
633 } 672 }
634 673
635 # Pick up the preceeding and succeeding characters. 674 # Pick up the preceeding and succeeding characters.
636 my $ca = substr($opline, $off - 1, 1); 675 my $ca = substr($opline, 0, $off);
637 my $cc = ''; 676 my $cc = '';
638 if (length($opline) >= ($off + length($elements[$n + 1]))) { 677 if (length($opline) >= ($off + length($elements[$n + 1]))) {
639 $cc = substr($opline, $off + length($elements[$n + 1])); 678 $cc = substr($opline, $off + length($elements[$n + 1]));
640 } 679 }
680 my $cb = "$ca$;$cc";
641 681
642 my $ctx = "${a}x${c}"; 682 my $ctx = "${a}x${c}";
643 683
644 my $at = "(ctx:$ctx)"; 684 my $at = "(ctx:$ctx)";
645 685
646 my $ptr = (" " x $off) . "^"; 686 my $ptr = (" " x $off) . "^";
647 my $hereptr = "$hereline$ptr\n\n"; 687 my $hereptr = "$hereline$ptr\n";
648 688
649 ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n"; 689 ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n";
650 690
651 # ; should have either the end of line or a space or \ after it 691 # ; should have either the end of line or a space or \ after it
652 if ($op eq ';') { 692 if ($op eq ';') {
653 if ($ctx !~ /.x[WE]/ && $cc !~ /^\\/) { 693 if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ &&
654 print "need space after that '$op' $at\n"; 694 $cc !~ /^;/) {
655 print "$hereptr"; 695 ERROR("need space after that '$op' $at\n" . $hereptr);
656 $clean = 0;
657 } 696 }
658 697
659 # // is a comment 698 # // is a comment
@@ -662,43 +701,31 @@ sub process {
662 # -> should have no spaces 701 # -> should have no spaces
663 } elsif ($op eq '->') { 702 } elsif ($op eq '->') {
664 if ($ctx =~ /Wx.|.xW/) { 703 if ($ctx =~ /Wx.|.xW/) {
665 print "no spaces around that '$op' $at\n"; 704 ERROR("no spaces around that '$op' $at\n" . $hereptr);
666 print "$hereptr";
667 $clean = 0;
668 } 705 }
669 706
670 # , must have a space on the right. 707 # , must have a space on the right.
671 } elsif ($op eq ',') { 708 } elsif ($op eq ',') {
672 if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) { 709 if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
673 print "need space after that '$op' $at\n"; 710 ERROR("need space after that '$op' $at\n" . $hereptr);
674 print "$hereptr";
675 $clean = 0;
676 } 711 }
677 712
678 # unary ! and unary ~ are allowed no space on the right 713 # unary ! and unary ~ are allowed no space on the right
679 } elsif ($op eq '!' or $op eq '~') { 714 } elsif ($op eq '!' or $op eq '~') {
680 if ($ctx !~ /[WOEB]x./) { 715 if ($ctx !~ /[WOEB]x./) {
681 print "need space before that '$op' $at\n"; 716 ERROR("need space before that '$op' $at\n" . $hereptr);
682 print "$hereptr";
683 $clean = 0;
684 } 717 }
685 if ($ctx =~ /.xW/) { 718 if ($ctx =~ /.xW/) {
686 print "no space after that '$op' $at\n"; 719 ERROR("no space after that '$op' $at\n" . $hereptr);
687 print "$hereptr";
688 $clean = 0;
689 } 720 }
690 721
691 # unary ++ and unary -- are allowed no space on one side. 722 # unary ++ and unary -- are allowed no space on one side.
692 } elsif ($op eq '++' or $op eq '--') { 723 } elsif ($op eq '++' or $op eq '--') {
693 if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) { 724 if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
694 print "need space one side of that '$op' $at\n"; 725 ERROR("need space one side of that '$op' $at\n" . $hereptr);
695 print "$hereptr";
696 $clean = 0;
697 } 726 }
698 if ($ctx =~ /Wx./ && $cc =~ /^;/) { 727 if ($ctx =~ /Wx./ && $cc =~ /^;/) {
699 print "no space before that '$op' $at\n"; 728 ERROR("no space before that '$op' $at\n" . $hereptr);
700 print "$hereptr";
701 $clean = 0;
702 } 729 }
703 730
704 # & is both unary and binary 731 # & is both unary and binary
@@ -715,9 +742,7 @@ sub process {
715 # 742 #
716 } elsif ($op eq '&' or $op eq '-') { 743 } elsif ($op eq '&' or $op eq '-') {
717 if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) { 744 if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) {
718 print "need space before that '$op' $at\n"; 745 ERROR("need space before that '$op' $at\n" . $hereptr);
719 print "$hereptr";
720 $clean = 0;
721 } 746 }
722 747
723 # * is the same as & only adding: 748 # * is the same as & only adding:
@@ -726,16 +751,9 @@ sub process {
726 # (foo **) 751 # (foo **)
727 # 752 #
728 } elsif ($op eq '*') { 753 } elsif ($op eq '*') {
729 if ($ca eq '*') { 754 if ($ca !~ /$Type$/ && $cb !~ /(\*$;|$;\*)/ &&
730 if ($cc =~ /^\s(?!\s*const)/) { 755 $ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB|BxB/) {
731 print "no space after that '$op' $at\n"; 756 ERROR("need space before that '$op' $at\n" . $hereptr);
732 print "$hereptr";
733 $clean = 0;
734 }
735 } elsif ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB|BxB/) {
736 print "need space before that '$op' $at\n";
737 print "$hereptr";
738 $clean = 0;
739 } 757 }
740 758
741 # << and >> may either have or not have spaces both sides 759 # << and >> may either have or not have spaces both sides
@@ -743,58 +761,51 @@ sub process {
743 $op eq '^' or $op eq '|') 761 $op eq '^' or $op eq '|')
744 { 762 {
745 if ($ctx !~ /VxV|WxW|VxE|WxE/) { 763 if ($ctx !~ /VxV|WxW|VxE|WxE/) {
746 print "need consistent spacing around '$op' $at\n"; 764 ERROR("need consistent spacing around '$op' $at\n" .
747 print "$hereptr"; 765 $hereptr);
748 $clean = 0;
749 } 766 }
750 767
751 # All the others need spaces both sides. 768 # All the others need spaces both sides.
752 } elsif ($ctx !~ /[EW]x[WE]/) { 769 } elsif ($ctx !~ /[EW]x[WE]/) {
753 print "need spaces around that '$op' $at\n"; 770 ERROR("need spaces around that '$op' $at\n" . $hereptr);
754 print "$hereptr";
755 $clean = 0;
756 } 771 }
757 $off += length($elements[$n + 1]); 772 $off += length($elements[$n + 1]);
758 } 773 }
759 } 774 }
760 775
761#need space before brace following if, while, etc 776#need space before brace following if, while, etc
762 if ($line=~/\(.*\){/) { 777 if ($line =~ /\(.*\){/ || $line =~ /do{/) {
763 print "need a space before the brace\n"; 778 ERROR("need a space before the open brace '{'\n" . $herecurr);
764 print "$herecurr"; 779 }
765 $clean = 0; 780
781# closing brace should have a space following it when it has anything
782# on the line
783 if ($line =~ /}(?!(?:,|;|\)))\S/) {
784 ERROR("need a space after that close brace '}'\n" . $herecurr);
766 } 785 }
767 786
768#goto labels aren't indented, allow a single space however 787#goto labels aren't indented, allow a single space however
769 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 788 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
770 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 789 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
771 print "labels should not be indented\n"; 790 WARN("labels should not be indented\n" . $herecurr);
772 print "$herecurr";
773 $clean = 0;
774 } 791 }
775 792
776# Need a space before open parenthesis after if, while etc 793# Need a space before open parenthesis after if, while etc
777 if ($line=~/\b(if|while|for|switch)\(/) { 794 if ($line=~/\b(if|while|for|switch)\(/) {
778 print "need a space before the open parenthesis\n"; 795 ERROR("need a space before the open parenthesis '('\n" . $herecurr);
779 print "$herecurr";
780 $clean = 0;
781 } 796 }
782 797
783# Check for illegal assignment in if conditional. 798# Check for illegal assignment in if conditional.
784 if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) { 799 if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) {
785 #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/); 800 #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
786 print "do not use assignment in if condition\n"; 801 ERROR("do not use assignment in if condition\n" . $herecurr);
787 print "$herecurr";
788 $clean = 0;
789 } 802 }
790 803
791 # Check for }<nl>else {, these must be at the same 804 # Check for }<nl>else {, these must be at the same
792 # indent level to be relevant to each other. 805 # indent level to be relevant to each other.
793 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 806 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
794 $previndent == $indent) { 807 $previndent == $indent) {
795 print "else should follow close brace\n"; 808 ERROR("else should follow close brace '}'\n" . $hereprev);
796 print "$hereprev";
797 $clean = 0;
798 } 809 }
799 810
800#studly caps, commented out until figure out how to distinguish between use of existing and adding new 811#studly caps, commented out until figure out how to distinguish between use of existing and adding new
@@ -806,57 +817,22 @@ sub process {
806 817
807#no spaces allowed after \ in define 818#no spaces allowed after \ in define
808 if ($line=~/\#define.*\\\s$/) { 819 if ($line=~/\#define.*\\\s$/) {
809 print("Whitepspace after \\ makes next lines useless\n"); 820 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
810 print "$herecurr";
811 $clean = 0;
812 } 821 }
813 822
814#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 823#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
815 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) { 824 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
816 my $checkfile = "include/linux/$1.h"; 825 my $checkfile = "include/linux/$1.h";
817 if (-f $checkfile) { 826 if (-f $checkfile) {
818 print "Use #include <linux/$1.h> instead of <asm/$1.h>\n"; 827 CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
819 print $herecurr; 828 $herecurr);
820 $clean = 0;
821 }
822 }
823
824# if/while/etc brace do not go on next line, unless defining a do while loop,
825# or if that brace on the next line is for something else
826 if ($prevline=~/\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/) {
827 my @opened = $prevline=~/\(/g;
828 my @closed = $prevline=~/\)/g;
829 my $nr_line = $linenr;
830 my $remaining = $realcnt - 1;
831 my $next_line = $line;
832 my $extra_lines = 0;
833 my $display_segment = $prevline;
834
835 while ($remaining > 0 && scalar @opened > scalar @closed) {
836 $prevline .= $next_line;
837 $display_segment .= "\n" . $next_line;
838 $next_line = $lines[$nr_line];
839 $nr_line++;
840 $remaining--;
841
842 @opened = $prevline=~/\(/g;
843 @closed = $prevline=~/\)/g;
844 }
845
846 if (($prevline=~/\b(?:(if|while|for|switch)\s*\(.*\)|do|else)\s*$/) and ($next_line=~/{/) and
847 !($next_line=~/\b(?:if|while|for|switch|do|else)\b/) and !($next_line=~/\#define.*do.*while/)) {
848 print "That { should be on the previous line\n";
849 print "$here\n$display_segment\n$next_line\n\n";
850 $clean = 0;
851 } 829 }
852 } 830 }
853 831
854# if and else should not have general statements after it 832# if and else should not have general statements after it
855 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ && 833 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ &&
856 $1 !~ /^\s*(?:\sif|{|$)/) { 834 $1 !~ /^\s*(?:\sif|{|\\|$)/) {
857 print "trailing statements should be on next line\n"; 835 ERROR("trailing statements should be on next line\n" . $herecurr);
858 print "$herecurr";
859 $clean = 0;
860 } 836 }
861 837
862# multi-statement macros should be enclosed in a do while loop, grab the 838# multi-statement macros should be enclosed in a do while loop, grab the
@@ -877,49 +853,49 @@ sub process {
877 $ln--; 853 $ln--;
878 $cnt++; 854 $cnt++;
879 } 855 }
880 my $ctx = join('', ctx_statement($ln, $cnt)); 856 my @ctx = ctx_statement($ln, $cnt);
857 my $ctx_ln = $ln + $#ctx + 1;
858 my $ctx = join("\n", @ctx);
859
860 # Pull in any empty extension lines.
861 while ($ctx =~ /\\$/ &&
862 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
863 $ctx .= $lines[$ctx_ln - 1];
864 $ctx_ln++;
865 }
881 866
882 if ($ctx =~ /\\$/) { 867 if ($ctx =~ /\\$/) {
883 if ($ctx =~ /;/) { 868 if ($ctx =~ /;/) {
884 print "Macros with multiple statements should be enclosed in a do - while loop\n"; 869 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
885 } else { 870 } else {
886 print "Macros with complex values should be enclosed in parenthesis\n"; 871 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
887 } 872 }
888 print "$hereprev";
889 $clean = 0;
890 } 873 }
891 } 874 }
892 875
893# don't include deprecated include files (uses RAW line) 876# don't include deprecated include files (uses RAW line)
894 for my $inc (@dep_includes) { 877 for my $inc (@dep_includes) {
895 if ($rawline =~ m@\#\s*include\s*\<$inc>@) { 878 if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
896 print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n"; 879 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
897 print "$herecurr";
898 $clean = 0;
899 } 880 }
900 } 881 }
901 882
902# don't use deprecated functions 883# don't use deprecated functions
903 for my $func (@dep_functions) { 884 for my $func (@dep_functions) {
904 if ($line =~ /\b$func\b/) { 885 if ($line =~ /\b$func\b/) {
905 print "Don't use $func(): see Documentation/feature-removal-schedule.txt\n"; 886 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
906 print "$herecurr";
907 $clean = 0;
908 } 887 }
909 } 888 }
910 889
911# no volatiles please 890# no volatiles please
912 if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) { 891 if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) {
913 print "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n"; 892 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
914 print "$herecurr";
915 $clean = 0;
916 } 893 }
917 894
918# warn about #if 0 895# warn about #if 0
919 if ($line =~ /^.#\s*if\s+0\b/) { 896 if ($line =~ /^.#\s*if\s+0\b/) {
920 print "#if 0 -- if this code redundant remove it\n"; 897 CHK("if this code is redundant consider removing it\n" .
921 print "$herecurr"; 898 $herecurr);
922 $clean = 0;
923 } 899 }
924 900
925# warn about #ifdefs in C files 901# warn about #ifdefs in C files
@@ -933,41 +909,47 @@ sub process {
933 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) { 909 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
934 my $which = $1; 910 my $which = $1;
935 if (!ctx_has_comment($first_line, $linenr)) { 911 if (!ctx_has_comment($first_line, $linenr)) {
936 print "$1 definition without comment\n"; 912 CHK("$1 definition without comment\n" . $herecurr);
937 print "$herecurr";
938 $clean = 0;
939 } 913 }
940 } 914 }
941# check for memory barriers without a comment. 915# check for memory barriers without a comment.
942 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 916 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
943 if (!ctx_has_comment($first_line, $linenr)) { 917 if (!ctx_has_comment($first_line, $linenr)) {
944 print "memory barrier without comment\n"; 918 CHK("memory barrier without comment\n" . $herecurr);
945 print "$herecurr";
946 $clean = 0;
947 } 919 }
948 } 920 }
949# check of hardware specific defines 921# check of hardware specific defines
950 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) { 922 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) {
951 print "architecture specific defines should be avoided\n"; 923 CHK("architecture specific defines should be avoided\n" . $herecurr);
952 print "$herecurr";
953 $clean = 0;
954 } 924 }
955 925
926# check the location of the inline attribute, that it is between
927# storage class and type.
956 if ($line =~ /$Type\s+(?:inline|__always_inline)\b/ || 928 if ($line =~ /$Type\s+(?:inline|__always_inline)\b/ ||
957 $line =~ /\b(?:inline|always_inline)\s+$Storage/) { 929 $line =~ /\b(?:inline|always_inline)\s+$Storage/) {
958 print "inline keyword should sit between storage class and type\n"; 930 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
959 print "$herecurr"; 931 }
960 $clean = 0; 932
933# check for new externs in .c files.
934 if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
935 WARN("externs should be avoided in .c files\n" . $herecurr);
936 }
937
938# checks for new __setup's
939 if ($rawline =~ /\b__setup\("([^"]*)"/) {
940 my $name = $1;
941
942 if (!grep(/$name/, @setup_docs)) {
943 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
944 }
961 } 945 }
962 } 946 }
963 947
964 if ($chk_patch && !$is_patch) { 948 if ($chk_patch && !$is_patch) {
965 $clean = 0; 949 ERROR("Does not appear to be a unified-diff format patch\n");
966 print "Does not appear to be a unified-diff format patch\n";
967 } 950 }
968 if ($is_patch && $chk_signoff && $signoff == 0) { 951 if ($is_patch && $chk_signoff && $signoff == 0) {
969 $clean = 0; 952 ERROR("Missing Signed-off-by: line(s)\n");
970 print "Missing Signed-off-by: line(s)\n";
971 } 953 }
972 954
973 if ($clean == 1 && $quiet == 0) { 955 if ($clean == 1 && $quiet == 0) {