2 # (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit)
3 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4 # (c) 2007, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite, etc)
5 # Licensed under the terms of the GNU GPL License version 2
14 use Getopt::Long qw(:config no_auto_abbrev);
24 'signoff!' => \$chk_signoff,
25 'patch!' => \$chk_patch,
26 'test-type!' => \$tst_type,
32 print "usage: $P [options] patchfile\n";
33 print "version: $V\n";
34 print "options: -q => quiet\n";
35 print " --no-tree => run without a kernel tree\n";
39 if ($tree && !top_of_kernel_tree()) {
40 print "Must be run from the top-level dir. of a kernel tree\n";
44 my @dep_includes = ();
45 my @dep_functions = ();
46 my $removal = 'Documentation/feature-removal-schedule.txt';
47 if ($tree && -f $removal) {
48 open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n";
50 if (/^Check:\s+(.*\S)/) {
51 for my $entry (split(/[, ]+/, $1)) {
52 if ($entry =~ m@include/(.*)@) {
53 push(@dep_includes, $1);
55 } elsif ($entry !~ m@/@) {
56 push(@dep_functions, $entry);
68 if (!process($ARGV, @rawlines)) {
77 sub top_of_kernel_tree {
78 if ((-f "COPYING") && (-f "CREDITS") && (-f "Kbuild") &&
79 (-f "MAINTAINERS") && (-f "Makefile") && (-f "README") &&
80 (-d "Documentation") && (-d "arch") && (-d "include") &&
81 (-d "drivers") && (-d "fs") && (-d "init") && (-d "ipc") &&
82 (-d "kernel") && (-d "lib") && (-d "scripts")) {
93 for my $c (split(//, $str)) {
97 for (; ($n % 8) != 0; $n++) {
112 # Drop the diff line leader and expand tabs
114 $line = expand_tabs($line);
116 # Pick the indent from the front of the line.
117 my ($white) = ($line =~ /^(\s*)/);
119 return (length($line), length($white));
130 foreach my $c (split(//, $line)) {
131 if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
137 } elsif ($quote eq $c) {
141 if ($quote && $c ne "\t") {
154 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
156 my $start = $linenr - 1;
163 for ($line = $start; $remain > 0; $line++) {
164 next if ($rawlines[$line] =~ /^-/);
167 $blk .= $rawlines[$line];
168 foreach my $c (split(//, $rawlines[$line])) {
169 ##print "C<$c>L<$level><$open$close>O<$off>\n";
175 if ($c eq $close && $level > 0) {
177 last if ($level == 0);
178 } elsif ($c eq $open) {
183 if (!$outer || $level <= 1) {
184 push(@res, $rawlines[$line]);
187 last if ($level == 0);
190 return ($level, @res);
192 sub ctx_block_outer {
193 my ($linenr, $remain) = @_;
195 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
199 my ($linenr, $remain) = @_;
201 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
205 my ($linenr, $remain, $off) = @_;
207 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
210 sub ctx_block_level {
211 my ($linenr, $remain) = @_;
213 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
216 sub ctx_locate_comment {
217 my ($first_line, $end_line) = @_;
219 # Catch a comment on the end of the line itself.
220 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
221 return $current_comment if (defined $current_comment);
223 # Look through the context and try and figure out if there is a
226 $current_comment = '';
227 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
228 my $line = $rawlines[$linenr - 1];
230 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
233 if ($line =~ m@/\*@) {
236 if (!$in_comment && $current_comment ne '') {
237 $current_comment = '';
239 $current_comment .= $line . "\n" if ($in_comment);
240 if ($line =~ m@\*/@) {
245 chomp($current_comment);
246 return($current_comment);
248 sub ctx_has_comment {
249 my ($first_line, $end_line) = @_;
250 my $cmt = ctx_locate_comment($first_line, $end_line);
252 ##print "LINE: $rawlines[$end_line - 1 ]\n";
253 ##print "CMMT: $cmt\n";
269 push(@report, $_[0]);
275 report("ERROR: $_[0]\n");
279 report("WARNING: $_[0]\n");
283 report("CHECK: $_[0]\n");
288 my $filename = shift;
304 # Trace the real file/line as we go.
312 my $Ident = qr{[A-Za-z\d_]+};
313 my $Storage = qr{extern|static};
314 my $Sparse = qr{__user|__kernel|__force|__iomem|__must_check|__init_refok};
344 (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
347 my $Declare = qr{(?:$Storage\s+)?$Type};
348 my $Attribute = qr{const|__read_mostly|__init|__initdata|__meminit};
350 my $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
351 my $Lval = qr{$Ident(?:$Member)*};
353 # Pre-scan the patch looking for any __setup documentation.
356 foreach my $line (@lines) {
357 if ($line=~/^\+\+\+\s+(\S+)/) {
359 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
365 if ($setup_docs && $line =~ /^\+/) {
366 push(@setup_docs, $line);
370 foreach my $line (@lines) {
375 #extract the filename as it passes
376 if ($line=~/^\+\+\+\s+(\S+)/) {
378 $realfile =~ s@^[^/]*/@@;
382 #extract the line range in the file after the patch is applied
383 if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
385 $first_line = $linenr + 1;
396 # track the line number as we move through the hunk, note that
397 # new versions of GNU diff omit the leading space on completely
398 # blank context lines so we need to count that too.
399 if ($line =~ /^( |\+|$)/) {
401 $realcnt-- if ($realcnt != 0);
403 # track any sort of multi-line comment. Obviously if
404 # the added text or context do not include the whole
405 # comment we will not see it. Such is life.
407 # Guestimate if this is a continuing comment. If this
408 # is the start of a diff block and this line starts
409 # ' *' then it is very likely a comment.
410 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
413 if ($line =~ m@/\*@) {
416 if ($line =~ m@\*/@) {
420 # Measure the line length and indent.
421 ($length, $indent) = line_stats($line);
423 # Track the previous line.
424 ($prevline, $stashline) = ($stashline, $line);
425 ($previndent, $stashindent) = ($stashindent, $indent);
426 } elsif ($realcnt == 1) {
430 #make up the handle for any error we report on this line
431 $here = "#$linenr: ";
432 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
434 my $hereline = "$here\n$line\n";
435 my $herecurr = "$here\n$line\n";
436 my $hereprev = "$here\n$prevline\n$line\n";
438 #check the patch for a signoff:
439 if ($line =~ /^\s*signed-off-by:/i) {
440 # This is a signoff, if ugly, so do not double report.
442 if (!($line =~ /^\s*Signed-off-by:/)) {
443 WARN("Signed-off-by: is the preferred form\n" .
446 if ($line =~ /^\s*signed-off-by:\S/i) {
447 WARN("need space after Signed-off-by:\n" .
452 # Check for wrappage within a valid hunk of the file
453 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) {
454 ERROR("patch seems to be corrupt (line wrapped?)\n" .
458 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
459 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
461 [\x09\x0A\x0D\x20-\x7E] # ASCII
462 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
463 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
464 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
465 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
466 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
467 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
468 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
470 ERROR("Invalid UTF-8\n" . $herecurr);
473 #ignore lines being removed
474 if ($line=~/^-/) {next;}
476 # check we are in a valid source file if not then ignore this hunk
477 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
480 if ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) {
481 my $herevet = "$here\n" . cat_vet($line) . "\n";
482 ERROR("trailing whitespace\n" . $herevet);
485 if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) {
486 WARN("line over 80 characters\n" . $herecurr);
489 # check we are in a valid source file *.[hc] if not then ignore this hunk
490 next if ($realfile !~ /\.[hc]$/);
492 # at the beginning of a line any tabs must come first and anything
493 # more than 8 must use tabs.
494 if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) {
495 my $herevet = "$here\n" . cat_vet($line) . "\n";
496 ERROR("use tabs not spaces\n" . $herevet);
499 # Remove comments from the line before processing.
500 my $comment_edge = ($line =~ s@/\*.*\*/@@g) +
501 ($line =~ s@/\*.*@@) +
502 ($line =~ s@^(.).*\*/@$1@);
504 # The rest of our checks refer specifically to C style
505 # only apply those _outside_ comments. Only skip
506 # lines in the middle of comments.
507 next if (!$comment_edge && $in_comment);
509 # Standardise the strings and chars within the input to simplify matching.
510 $line = sanitise_line($line);
513 # Checks which may be anchored in the context.
516 # Check for switch () and associated case and default
517 # statements should be at the same indent.
518 if ($line=~/\bswitch\s*\(.*\)/) {
521 my @ctx = ctx_block_outer($linenr, $realcnt);
524 my ($clen, $cindent) = line_stats($ctx);
525 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
526 $indent != $cindent) {
527 $err .= "$sep$ctx\n";
534 ERROR("switch and case should be at the same indent\n$hereline\n$err\n");
538 # if/while/etc brace do not go on next line, unless defining a do while loop,
539 # or if that brace on the next line is for something else
540 if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
541 my @ctx = ctx_statement($linenr, $realcnt, 0);
542 my $ctx_ln = $linenr + $#ctx + 1;
543 my $ctx_cnt = $realcnt - $#ctx - 1;
544 my $ctx = join("\n", @ctx);
546 while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
550 ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
552 if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
553 ERROR("That open brace { should be on the previous line\n" .
554 "$here\n$ctx\n$lines[$ctx_ln - 1]");
558 #ignore lines not being added
559 if ($line=~/^[^\+]/) {next;}
561 # TEST: allow direct testing of the type matcher.
562 if ($tst_type && $line =~ /^.$Declare$/) {
563 ERROR("TEST: is type $Declare\n" . $herecurr);
567 # check for initialisation to aggregates open brace on the next line
568 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
570 ERROR("That open brace { should be on the previous line\n" . $hereprev);
574 # Checks which are anchored on the added line.
577 # check for malformed paths in #include statements (uses RAW line)
578 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
580 if ($path =~ m{//}) {
581 ERROR("malformed #include filename\n" .
584 # Sanitise this special form of string.
585 $path = 'X' x length($path);
586 $line =~ s{\<.*\>}{<$path>};
590 if ($line =~ m{//}) {
591 ERROR("do not use C99 // comments\n" . $herecurr);
593 # Remove C99 comments.
596 #EXPORT_SYMBOL should immediately follow its function closing }.
597 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
598 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
600 if (($prevline !~ /^}/) &&
601 ($prevline !~ /^\+}/) &&
602 ($prevline !~ /^ }/) &&
603 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) {
604 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
608 # check for external initialisers.
609 if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
610 ERROR("do not initialise externals to 0 or NULL\n" .
613 # check for static initialisers.
614 if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
615 ERROR("do not initialise statics to 0 or NULL\n" .
619 # check for new typedefs, only function parameters and sparse annotations
621 if ($line =~ /\btypedef\s/ &&
622 $line !~ /\btypedef\s+$Type\s+\(\s*\*$Ident\s*\)\s*\(/ &&
623 $line !~ /\b__bitwise(?:__|)\b/) {
624 WARN("do not add new typedefs\n" . $herecurr);
627 # * goes on variable not on type
628 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
629 ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
632 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
633 ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
636 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+$Attribute)?\s+[A-Za-z\d_]+}) {
637 ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
640 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+$Attribute)\s+[A-Za-z\d_]+}) {
641 ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
645 # # no BUG() or BUG_ON()
646 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
647 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
652 # printk should use KERN_* levels. Note that follow on printk's on the
653 # same line do not need a level, so we use the current block context
654 # to try and find and validate the current printk. In summary the current
655 # printk includes all preceeding printk's which have no newline on the end.
656 # we assume the first bad printk is the one to report.
657 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
659 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
660 #print "CHECK<$lines[$ln - 1]\n";
661 # we have a preceeding printk if it ends
662 # with "\n" ignore it, else it is to blame
663 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
664 if ($rawlines[$ln - 1] !~ m{\\n"}) {
671 WARN("printk() should include KERN_ facility level\n" . $herecurr);
675 # function brace can't be on same line, except for #defines of do while,
676 # or if closed on same line
677 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and
678 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
679 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
682 # check for spaces between functions and their parentheses.
683 if ($line =~ /($Ident)\s+\(/ &&
684 $1 !~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright)$/ &&
685 $line !~ /$Type\s+\(/ && $line !~ /^.\#\s*define\b/) {
686 WARN("no space between function name and open parenthesis '('\n" . $herecurr);
688 # Check operator spacing.
689 # Note we expand the line with the leading + as the real
690 # line will be displayed with the leading + and the tabs
691 # will therefore also expand that way.
693 $opline = expand_tabs($opline);
695 if (!($line=~/\#\s*include/)) {
696 my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|=>|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline);
698 for (my $n = 0; $n < $#elements; $n += 2) {
699 $off += length($elements[$n]);
702 $a = 'V' if ($elements[$n] ne '');
703 $a = 'W' if ($elements[$n] =~ /\s$/);
704 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
705 $a = 'O' if ($elements[$n] eq '');
706 $a = 'E' if ($elements[$n] eq '' && $n == 0);
708 my $op = $elements[$n + 1];
711 if (defined $elements[$n + 2]) {
712 $c = 'V' if ($elements[$n + 2] ne '');
713 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
714 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
715 $c = 'O' if ($elements[$n + 2] eq '');
716 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
721 # Pick up the preceeding and succeeding characters.
722 my $ca = substr($opline, 0, $off);
724 if (length($opline) >= ($off + length($elements[$n + 1]))) {
725 $cc = substr($opline, $off + length($elements[$n + 1]));
729 my $ctx = "${a}x${c}";
731 my $at = "(ctx:$ctx)";
733 my $ptr = (" " x $off) . "^";
734 my $hereptr = "$hereline$ptr\n";
736 ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n";
738 # ; should have either the end of line or a space or \ after it
740 if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ &&
742 ERROR("need space after that '$op' $at\n" . $hereptr);
746 } elsif ($op eq '//') {
748 # -> should have no spaces
749 } elsif ($op eq '->') {
750 if ($ctx =~ /Wx.|.xW/) {
751 ERROR("no spaces around that '$op' $at\n" . $hereptr);
754 # , must have a space on the right.
755 } elsif ($op eq ',') {
756 if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
757 ERROR("need space after that '$op' $at\n" . $hereptr);
760 # unary ! and unary ~ are allowed no space on the right
761 } elsif ($op eq '!' or $op eq '~') {
762 if ($ctx !~ /[WOEB]x./) {
763 ERROR("need space before that '$op' $at\n" . $hereptr);
766 ERROR("no space after that '$op' $at\n" . $hereptr);
769 # unary ++ and unary -- are allowed no space on one side.
770 } elsif ($op eq '++' or $op eq '--') {
771 if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
772 ERROR("need space one side of that '$op' $at\n" . $hereptr);
774 if ($ctx =~ /Wx./ && $cc =~ /^;/) {
775 ERROR("no space before that '$op' $at\n" . $hereptr);
778 # & is both unary and binary
781 # binary (consistent spacing):
785 # boiling down to: if there is a space on the right then there
786 # should be one on the left.
790 } elsif ($op eq '&' or $op eq '-') {
791 if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) {
792 ERROR("need space before that '$op' $at\n" . $hereptr);
795 # * is the same as & only adding:
800 } elsif ($op eq '*') {
801 if ($ca !~ /$Type$/ && $cb !~ /(\*$;|$;\*)/ &&
802 $ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB|BxB/) {
803 ERROR("need space before that '$op' $at\n" . $hereptr);
806 # << and >> may either have or not have spaces both sides
807 } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
808 $op eq '^' or $op eq '|')
810 if ($ctx !~ /VxV|WxW|VxE|WxE/) {
811 ERROR("need consistent spacing around '$op' $at\n" .
815 # All the others need spaces both sides.
816 } elsif ($ctx !~ /[EW]x[WE]/) {
817 # Ignore email addresses <foo@bar>
818 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
819 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
820 ERROR("need spaces around that '$op' $at\n" . $hereptr);
823 $off += length($elements[$n + 1]);
827 # check for multiple assignments
828 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
829 WARN("multiple assignments should be avoided\n" . $herecurr);
832 ## # check for multiple declarations, allowing for a function declaration
834 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
835 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
837 ## # Remove any bracketed sections to ensure we do not
838 ## # falsly report the parameters of functions.
840 ## while ($ln =~ s/\([^\(\)]*\)//g) {
843 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
847 #need space before brace following if, while, etc
848 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
850 ERROR("need a space before the open brace '{'\n" . $herecurr);
853 # closing brace should have a space following it when it has anything
855 if ($line =~ /}(?!(?:,|;|\)))\S/) {
856 ERROR("need a space after that close brace '}'\n" . $herecurr);
859 # check spacing on square brackets
860 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
861 ERROR("no space after that open square bracket '['\n" . $herecurr);
863 if ($line =~ /\s\]/) {
864 ERROR("no space before that close square bracket ']'\n" . $herecurr);
867 # check spacing on paretheses
868 if ($line =~ /\(\s/ && $line !~ /\(\s*$/) {
869 ERROR("no space after that open parenthesis '('\n" . $herecurr);
871 if ($line =~ /\s\)/) {
872 ERROR("no space before that close parenthesis ')'\n" . $herecurr);
875 #goto labels aren't indented, allow a single space however
876 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
877 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
878 WARN("labels should not be indented\n" . $herecurr);
881 # Need a space before open parenthesis after if, while etc
882 if ($line=~/\b(if|while|for|switch)\(/) {
883 ERROR("need a space before the open parenthesis '('\n" . $herecurr);
886 # Check for illegal assignment in if conditional.
887 if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) {
888 #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
889 ERROR("do not use assignment in if condition\n" . $herecurr);
892 # Check for }<nl>else {, these must be at the same
893 # indent level to be relevant to each other.
894 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
895 $previndent == $indent) {
896 ERROR("else should follow close brace '}'\n" . $hereprev);
899 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
900 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
901 # print "No studly caps, use _\n";
906 #no spaces allowed after \ in define
907 if ($line=~/\#define.*\\\s$/) {
908 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
911 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
912 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
913 my $checkfile = "include/linux/$1.h";
915 CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
920 # if and else should not have general statements after it
921 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ &&
922 $1 !~ /^\s*(?:\sif|{|\\|$)/) {
923 ERROR("trailing statements should be on next line\n" . $herecurr);
926 # multi-statement macros should be enclosed in a do while loop, grab the
927 # first statement and ensure its the whole macro if its not enclosed
928 # in a known goot container
929 if (($prevline=~/\#define.*\\/) and
930 !($prevline=~/do\s+{/) and !($prevline=~/\(\{/) and
931 !($line=~/do.*{/) and !($line=~/\(\{/) and
932 !($line=~/^.\s*$Declare\s/)) {
933 # Grab the first statement, if that is the entire macro
934 # its ok. This may start either on the #define line
940 # If the macro starts on the define line start
941 # grabbing the statement after the identifier
942 $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
943 ##print "1<$1> 2<$2>\n";
944 if (defined $2 && $2 ne '') {
949 my @ctx = ctx_statement($ln, $cnt, $off);
950 my $ctx_ln = $ln + $#ctx + 1;
951 my $ctx = join("\n", @ctx);
953 # Pull in any empty extension lines.
954 while ($ctx =~ /\\$/ &&
955 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
956 $ctx .= $lines[$ctx_ln - 1];
962 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
964 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
969 # check for redundant bracing round if etc
970 if ($line =~ /\b(if|while|for|else)\b/) {
971 # Locate the end of the opening statement.
972 my @control = ctx_statement($linenr, $realcnt, 0);
973 my $nr = $linenr + (scalar(@control) - 1);
974 my $cnt = $realcnt - (scalar(@control) - 1);
976 my $off = $realcnt - $cnt;
977 #print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n";
979 # If this is is a braced statement group check it
980 if ($lines[$nr - 1] =~ /{\s*$/) {
981 my ($lvl, @block) = ctx_block_level($nr, $cnt);
983 my $stmt = join(' ', @block);
984 $stmt =~ s/(^[^{]*){//;
986 $stmt =~ s/}([^}]*$)//;
989 #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n";
990 #print "stmt<$stmt>\n\n";
992 # Count the ;'s if there is fewer than two
993 # then there can only be one statement,
994 # if there is a brace inside we cannot
995 # trivially detect if its one statement.
996 # Also nested if's often require braces to
997 # disambiguate the else binding so shhh there.
998 my @semi = ($stmt =~ /;/g);
999 push(@semi, "/**/") if ($stmt =~ m@/\*@);
1000 ##print "semi<" . scalar(@semi) . ">\n";
1001 if ($lvl == 0 && scalar(@semi) < 2 &&
1002 $stmt !~ /{/ && $stmt !~ /\bif\b/ &&
1003 $before !~ /}/ && $after !~ /{/) {
1004 my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n";
1006 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
1011 # don't include deprecated include files (uses RAW line)
1012 for my $inc (@dep_includes) {
1013 if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
1014 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
1018 # don't use deprecated functions
1019 for my $func (@dep_functions) {
1020 if ($line =~ /\b$func\b/) {
1021 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
1025 # no volatiles please
1026 if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) {
1027 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
1031 if ($line =~ /^.#\s*if\s+0\b/) {
1032 CHK("if this code is redundant consider removing it\n" .
1036 # check for needless kfree() checks
1037 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1039 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1040 WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1044 # warn about #ifdefs in C files
1045 # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
1046 # print "#ifdef in C files should be avoided\n";
1047 # print "$herecurr";
1051 # warn about spacing in #ifdefs
1052 if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
1053 ERROR("exactly one space required after that #$1\n" . $herecurr);
1056 # check for spinlock_t definitions without a comment.
1057 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
1059 if (!ctx_has_comment($first_line, $linenr)) {
1060 CHK("$1 definition without comment\n" . $herecurr);
1063 # check for memory barriers without a comment.
1064 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
1065 if (!ctx_has_comment($first_line, $linenr)) {
1066 CHK("memory barrier without comment\n" . $herecurr);
1069 # check of hardware specific defines
1070 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
1071 CHK("architecture specific defines should be avoided\n" . $herecurr);
1074 # check the location of the inline attribute, that it is between
1075 # storage class and type.
1076 if ($line =~ /$Type\s+(?:inline|__always_inline|noinline)\b/ ||
1077 $line =~ /\b(?:inline|__always_inline|noinline)\s+$Storage/) {
1078 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
1081 # check for new externs in .c files.
1082 if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
1083 WARN("externs should be avoided in .c files\n" . $herecurr);
1086 # checks for new __setup's
1087 if ($rawline =~ /\b__setup\("([^"]*)"/) {
1090 if (!grep(/$name/, @setup_docs)) {
1091 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
1096 if ($chk_patch && !$is_patch) {
1097 ERROR("Does not appear to be a unified-diff format patch\n");
1099 if ($is_patch && $chk_signoff && $signoff == 0) {
1100 ERROR("Missing Signed-off-by: line(s)\n");
1103 if ($clean == 0 && ($chk_patch || $is_patch)) {
1104 print report_dump();
1106 if ($clean == 1 && $quiet == 0) {
1107 print "Your patch has no obvious style problems and is ready for submission.\n"
1109 if ($clean == 0 && $quiet == 0) {
1110 print "Your patch has style problems, please review. If any of these errors\n";
1111 print "are false positives report them to the maintainer, see\n";
1112 print "CHECKPATCH in MAINTAINERS.\n";