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 (/^Files:\s+(.*\S)/) {
51 for my $file (split(/[, ]+/, $1)) {
52 if ($file =~ m@include/(.*)@) {
53 push(@dep_includes, $1);
57 } elsif (/^Funcs:\s+(.*\S)/) {
58 for my $func (split(/[, ]+/, $1)) {
59 push(@dep_functions, $func);
70 if (!process($ARGV, @rawlines)) {
79 sub top_of_kernel_tree {
80 if ((-f "COPYING") && (-f "CREDITS") && (-f "Kbuild") &&
81 (-f "MAINTAINERS") && (-f "Makefile") && (-f "README") &&
82 (-d "Documentation") && (-d "arch") && (-d "include") &&
83 (-d "drivers") && (-d "fs") && (-d "init") && (-d "ipc") &&
84 (-d "kernel") && (-d "lib") && (-d "scripts")) {
95 for my $c (split(//, $str)) {
99 for (; ($n % 8) != 0; $n++) {
114 # Drop the diff line leader and expand tabs
116 $line = expand_tabs($line);
118 # Pick the indent from the front of the line.
119 my ($white) = ($line =~ /^(\s*)/);
121 return (length($line), length($white));
132 foreach my $c (split(//, $line)) {
133 if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
139 } elsif ($quote eq $c) {
143 if ($quote && $c ne "\t") {
156 my ($linenr, $remain, $outer, $open, $close) = @_;
158 my $start = $linenr - 1;
164 for ($line = $start; $remain > 0; $line++) {
165 next if ($rawlines[$line] =~ /^-/);
168 $blk .= $rawlines[$line];
170 @o = ($blk =~ /$open/g);
171 @c = ($blk =~ /$close/g);
173 if (!$outer || (scalar(@o) - scalar(@c)) == 1) {
174 push(@res, $rawlines[$line]);
177 last if (scalar(@o) == scalar(@c));
182 sub ctx_block_outer {
183 my ($linenr, $remain) = @_;
185 return ctx_block_get($linenr, $remain, 1, '\{', '\}');
188 my ($linenr, $remain) = @_;
190 return ctx_block_get($linenr, $remain, 0, '\{', '\}');
193 my ($linenr, $remain) = @_;
195 return ctx_block_get($linenr, $remain, 0, '\(', '\)');
198 sub ctx_locate_comment {
199 my ($first_line, $end_line) = @_;
201 # Catch a comment on the end of the line itself.
202 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
203 return $current_comment if (defined $current_comment);
205 # Look through the context and try and figure out if there is a
208 $current_comment = '';
209 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
210 my $line = $rawlines[$linenr - 1];
212 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
215 if ($line =~ m@/\*@) {
218 if (!$in_comment && $current_comment ne '') {
219 $current_comment = '';
221 $current_comment .= $line . "\n" if ($in_comment);
222 if ($line =~ m@\*/@) {
227 chomp($current_comment);
228 return($current_comment);
230 sub ctx_has_comment {
231 my ($first_line, $end_line) = @_;
232 my $cmt = ctx_locate_comment($first_line, $end_line);
234 ##print "LINE: $rawlines[$end_line - 1 ]\n";
235 ##print "CMMT: $cmt\n";
250 my $filename = shift;
266 # Trace the real file/line as we go.
274 my $ident = '[A-Za-z\d_]+';
275 my $storage = '(?:extern|static)';
276 my $sparse = '(?:__user|__kernel|__force|__iomem)';
277 my $type = '(?:unsigned\s+)?' .
278 '(?:void|char|short|int|long|unsigned|float|double|' .
280 "struct\\s+${ident}|" .
281 "union\\s+${ident}|" .
285 my $attribute = '(?:__read_mostly|__init|__initdata)';
289 my $Storage = $storage;
290 my $Declare = "(?:$storage\\s+)?$type";
291 my $Attribute = $attribute;
293 foreach my $line (@lines) {
298 #extract the filename as it passes
299 if ($line=~/^\+\+\+\s+(\S+)/) {
301 $realfile =~ s@^[^/]*/@@;
305 #extract the line range in the file after the patch is applied
306 if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
308 $first_line = $linenr + 1;
319 # track the line number as we move through the hunk, note that
320 # new versions of GNU diff omit the leading space on completely
321 # blank context lines so we need to count that too.
322 if ($line =~ /^( |\+|$)/) {
325 # track any sort of multi-line comment. Obviously if
326 # the added text or context do not include the whole
327 # comment we will not see it. Such is life.
329 # Guestimate if this is a continuing comment. If this
330 # is the start of a diff block and this line starts
331 # ' *' then it is very likely a comment.
332 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
335 if ($line =~ m@/\*@) {
338 if ($line =~ m@\*/@) {
342 # Measure the line length and indent.
343 ($length, $indent) = line_stats($line);
345 # Track the previous line.
346 ($prevline, $stashline) = ($stashline, $line);
347 ($previndent, $stashindent) = ($stashindent, $indent);
349 $realcnt-- if ($realcnt != 0);
351 #make up the handle for any error we report on this line
352 $here = "#$linenr: ";
353 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
355 my $hereline = "$here\n$line\n";
356 my $herecurr = "$here\n$line\n\n";
357 my $hereprev = "$here\n$prevline\n$line\n\n";
359 #check the patch for a signoff:
360 if ($line =~ /^\s*Signed-off-by:\s/) {
363 } elsif ($line =~ /^\s*signed-off-by:/i) {
364 # This is a signoff, if ugly, so do not double report.
366 if (!($line =~ /^\s*Signed-off-by:/)) {
367 print "use Signed-off-by:\n";
371 if ($line =~ /^\s*signed-off-by:\S/i) {
372 print "need space after Signed-off-by:\n";
378 # Check for wrappage within a valid hunk of the file
379 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) {
380 print "patch seems to be corrupt (line wrapped?) [$realcnt]\n";
385 #ignore lines being removed
386 if ($line=~/^-/) {next;}
388 # check we are in a valid source file if not then ignore this hunk
389 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
392 if ($line=~/^\+.*\S\s+$/) {
393 my $herevet = "$here\n" . cat_vet($line) . "\n\n";
394 print "trailing whitespace\n";
399 if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) {
400 print "line over 80 characters\n";
405 # check we are in a valid source file *.[hc] if not then ignore this hunk
406 next if ($realfile !~ /\.[hc]$/);
408 # at the beginning of a line any tabs must come first and anything
409 # more than 8 must use tabs.
410 if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) {
411 my $herevet = "$here\n" . cat_vet($line) . "\n\n";
412 print "use tabs not spaces\n";
418 # The rest of our checks refer specifically to C style
419 # only apply those _outside_ comments.
421 next if ($in_comment);
423 # Remove comments from the line before processing.
424 $line =~ s@/\*.*\*/@@g;
428 # Standardise the strings and chars within the input to simplify matching.
429 $line = sanitise_line($line);
432 # Checks which may be anchored in the context.
435 # Check for switch () and associated case and default
436 # statements should be at the same indent.
437 if ($line=~/\bswitch\s*\(.*\)/) {
440 my @ctx = ctx_block_outer($linenr, $realcnt);
443 my ($clen, $cindent) = line_stats($ctx);
444 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
445 $indent != $cindent) {
446 $err .= "$sep$ctx\n";
453 print "switch and case should be at the same indent\n";
454 print "$here\n$line\n$err\n";
459 #ignore lines not being added
460 if ($line=~/^[^\+]/) {next;}
462 # TEST: allow direct testing of the type matcher.
463 if ($tst_type && $line =~ /^.$Declare$/) {
464 print "TEST: is type $Declare\n";
471 # Checks which are anchored on the added line.
474 # check for malformed paths in #include statements (uses RAW line)
475 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
477 if ($path =~ m{//}) {
478 print "malformed #include filename\n";
482 # Sanitise this special form of string.
483 $path = 'X' x length($path);
484 $line =~ s{\<.*\>}{<$path>};
488 if ($line =~ m{//}) {
489 print "do not use C99 // comments\n";
493 # Remove C99 comments.
496 #EXPORT_SYMBOL should immediately follow its function closing }.
497 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
498 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
500 if (($prevline !~ /^}/) &&
501 ($prevline !~ /^\+}/) &&
502 ($prevline !~ /^ }/) &&
503 ($prevline !~ /\s$name(?:\s+$Attribute)?\s*(?:;|=)/)) {
504 print "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n";
510 # check for static initialisers.
511 if ($line=~/\s*static\s.*=\s+(0|NULL);/) {
512 print "do not initialise statics to 0 or NULL\n";
517 # check for new typedefs, only function parameters and sparse annotations
519 if ($line =~ /\btypedef\s/ &&
520 $line !~ /\btypedef\s+$Type\s+\(\s*$Ident\s*\)\s*\(/ &&
521 $line !~ /\b__bitwise(?:__|)\b/) {
522 print "do not add new typedefs\n";
527 # * goes on variable not on type
528 if ($line =~ m{[A-Za-z\d_]+(\*+) [A-Za-z\d_]+}) {
529 print "\"foo$1 bar\" should be \"foo $1bar\"\n";
533 if ($line =~ m{$Type (\*) [A-Za-z\d_]+} ||
534 $line =~ m{[A-Za-z\d_]+ (\*\*+) [A-Za-z\d_]+}) {
535 print "\"foo $1 bar\" should be \"foo $1bar\"\n";
539 if ($line =~ m{\([A-Za-z\d_\s]+[A-Za-z\d_](\*+)\)}) {
540 print "\"(foo$1)\" should be \"(foo $1)\"\n";
544 if ($line =~ m{\([A-Za-z\d_\s]+[A-Za-z\d_]\s+(\*+)\s+\)}) {
545 print "\"(foo $1 )\" should be \"(foo $1)\"\n";
550 # # no BUG() or BUG_ON()
551 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
552 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
557 # printk should use KERN_* levels. Note that follow on printk's on the
558 # same line do not need a level, so we use the current block context
559 # to try and find and validate the current printk. In summary the current
560 # printk includes all preceeding printk's which have no newline on the end.
561 # we assume the first bad printk is the one to report.
562 if ($line =~ /\bprintk\((?!KERN_)/) {
564 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
565 #print "CHECK<$lines[$ln - 1]\n";
566 # we have a preceeding printk if it ends
567 # with "\n" ignore it, else it is to blame
568 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
569 if ($rawlines[$ln - 1] !~ m{\\n"}) {
576 print "printk() should include KERN_ facility level\n";
582 # function brace can't be on same line, except for #defines of do while,
583 # or if closed on same line
584 if (($line=~/[A-Za-z\d_]+\**\s+\**[A-Za-z\d_]+\(.*\).* {/) and
585 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
586 print "braces following function declarations go on the next line\n";
591 # Check operator spacing.
592 # Note we expand the line with the leading + as the real
593 # line will be displayed with the leading + and the tabs
594 # will therefore also expand that way.
596 $opline = expand_tabs($opline);
598 if (!($line=~/\#\s*include/)) {
599 my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline);
601 for (my $n = 0; $n < $#elements; $n += 2) {
602 $off += length($elements[$n]);
605 $a = 'V' if ($elements[$n] ne '');
606 $a = 'W' if ($elements[$n] =~ /\s$/);
607 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
608 $a = 'O' if ($elements[$n] eq '');
609 $a = 'E' if ($elements[$n] eq '' && $n == 0);
611 my $op = $elements[$n + 1];
614 if (defined $elements[$n + 2]) {
615 $c = 'V' if ($elements[$n + 2] ne '');
616 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
617 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
618 $c = 'O' if ($elements[$n + 2] eq '');
623 # Pick up the preceeding and succeeding characters.
624 my $ca = substr($opline, $off - 1, 1);
626 if (length($opline) >= ($off + length($elements[$n + 1]))) {
627 $cc = substr($opline, $off + length($elements[$n + 1]), 1);
630 my $ctx = "${a}x${c}";
632 my $at = "(ctx:$ctx)";
634 my $ptr = (" " x $off) . "^";
635 my $hereptr = "$hereline$ptr\n\n";
637 ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n";
639 # We need ; as an operator. // is a comment.
640 if ($op eq ';' or $op eq '//') {
642 # -> should have no spaces
643 } elsif ($op eq '->') {
644 if ($ctx =~ /Wx.|.xW/) {
645 print "no spaces around that '$op' $at\n";
650 # , must have a space on the right.
651 } elsif ($op eq ',') {
652 if ($ctx !~ /.xW|.xE/ && $cc ne '}') {
653 print "need space after that '$op' $at\n";
658 # unary ! and unary ~ are allowed no space on the right
659 } elsif ($op eq '!' or $op eq '~') {
660 if ($ctx !~ /[WOEB]x./) {
661 print "need space before that '$op' $at\n";
666 print "no space after that '$op' $at\n";
671 # unary ++ and unary -- are allowed no space on one side.
672 } elsif ($op eq '++' or $op eq '--') {
673 if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOB]/) {
674 print "need space one side of that '$op' $at\n";
678 if ($ctx =~ /Wx./ && $cc eq ';') {
679 print "no space before that '$op' $at\n";
684 # & is both unary and binary
687 # binary (consistent spacing):
691 # boiling down to: if there is a space on the right then there
692 # should be one on the left.
696 } elsif ($op eq '&' or $op eq '-') {
697 if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) {
698 print "need space before that '$op' $at\n";
703 # * is the same as & only adding:
708 } elsif ($op eq '*') {
711 print "no space after that '$op' $at\n";
715 } elsif ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB|BxB/) {
716 print "need space before that '$op' $at\n";
721 # << and >> may either have or not have spaces both sides
722 } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
723 $op eq '^' or $op eq '|')
725 if ($ctx !~ /VxV|WxW|VxE|WxE/) {
726 print "need consistent spacing around '$op' $at\n";
731 # All the others need spaces both sides.
732 } elsif ($ctx !~ /[EW]x[WE]/) {
733 print "need spaces around that '$op' $at\n";
737 $off += length($elements[$n + 1]);
741 #need space before brace following if, while, etc
742 if ($line=~/\(.*\){/) {
743 print "need a space before the brace\n";
748 #goto labels aren't indented, allow a single space however
749 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
750 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
751 print "labels should not be indented\n";
756 # Need a space before open parenthesis after if, while etc
757 if ($line=~/\b(if|while|for|switch)\(/) {
758 print "need a space before the open parenthesis\n";
763 # Check for illegal assignment in if conditional.
764 if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) {
765 #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
766 print "do not use assignment in if condition\n";
771 # Check for }<nl>else {, these must be at the same
772 # indent level to be relevant to each other.
773 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
774 $previndent == $indent) {
775 print "else should follow close brace\n";
780 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
781 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
782 # print "No studly caps, use _\n";
787 #no spaces allowed after \ in define
788 if ($line=~/\#define.*\\\s$/) {
789 print("Whitepspace after \\ makes next lines useless\n");
794 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
795 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
796 my $checkfile = "include/linux/$1.h";
798 print "Use #include <linux/$1.h> instead of <asm/$1.h>\n";
804 # if/while/etc brace do not go on next line, unless defining a do while loop,
805 # or if that brace on the next line is for something else
806 if ($prevline=~/\b(if|while|for|switch)\s*\(/) {
807 my @opened = $prevline=~/\(/g;
808 my @closed = $prevline=~/\)/g;
809 my $nr_line = $linenr;
810 my $remaining = $realcnt - 1;
811 my $next_line = $line;
813 my $display_segment = $prevline;
815 while ($remaining > 0 && scalar @opened > scalar @closed) {
816 $prevline .= $next_line;
817 $display_segment .= "\n" . $next_line;
818 $next_line = $lines[$nr_line];
822 @opened = $prevline=~/\(/g;
823 @closed = $prevline=~/\)/g;
826 if (($prevline=~/\b(if|while|for|switch)\s*\(.*\)\s*$/) and ($next_line=~/{/) and
827 !($next_line=~/\b(if|while|for|switch)/) and !($next_line=~/\#define.*do.*while/)) {
828 print "That { should be on the previous line\n";
829 print "$here\n$display_segment\n$next_line\n\n";
834 # multi-statement macros should be enclosed in a do while loop, grab the
835 # first statement and ensure its the whole macro if its not enclosed
836 # in a known goot container
837 if (($prevline=~/\#define.*\\/) and
838 !($prevline=~/do\s+{/) and !($prevline=~/\(\{/) and
839 !($line=~/do.*{/) and !($line=~/\(\{/) and
840 !($line=~/^.\s*$Declare\s/)) {
841 # Grab the first statement, if that is the entire macro
842 # its ok. This may start either on the #define line
844 my $ctx1 = join('', ctx_statement($linenr - 1, $realcnt + 1));
845 my $ctx2 = join('', ctx_statement($linenr, $realcnt));
847 if ($ctx1 =~ /\\$/ && $ctx2 =~ /\\$/) {
848 print "Macros with multiple statements should be enclosed in a do - while loop\n";
854 # don't include deprecated include files (uses RAW line)
855 for my $inc (@dep_includes) {
856 if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
857 print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n";
863 # don't use deprecated functions
864 for my $func (@dep_functions) {
865 if ($line =~ /\b$func\b/) {
866 print "Don't use $func(): see Documentation/feature-removal-schedule.txt\n";
872 # no volatiles please
873 if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) {
874 print "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n";
880 if ($line =~ /^.#\s*if\s+0\b/) {
881 print "#if 0 -- if this code redundant remove it\n";
886 # warn about #ifdefs in C files
887 # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
888 # print "#ifdef in C files should be avoided\n";
893 # check for spinlock_t definitions without a comment.
894 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
896 if (!ctx_has_comment($first_line, $linenr)) {
897 print "$1 definition without comment\n";
902 # check for memory barriers without a comment.
903 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
904 if (!ctx_has_comment($first_line, $linenr)) {
905 print "memory barrier without comment\n";
910 # check of hardware specific defines
911 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) {
912 print "architecture specific defines should be avoided\n";
917 if ($line =~ /$Type\s+(?:inline|__always_inline)\b/ ||
918 $line =~ /\b(?:inline|always_inline)\s+$Storage/) {
919 print "inline keyword should sit between storage class and type\n";
925 if ($chk_patch && !$is_patch) {
927 print "Does not appear to be a unified-diff format patch\n";
929 if ($is_patch && $chk_signoff && $signoff == 0) {
931 print "Missing Signed-off-by: line(s)\n";
934 if ($clean == 1 && $quiet == 0) {
935 print "Your patch has no obvious style problems and is ready for submission.\n"
937 if ($clean == 0 && $quiet == 0) {
938 print "Your patch has style problems, please review. If any of these errors\n";
939 print "are false positives report them to the maintainer, see\n";
940 print "CHECKPATCH in MAINTAINERS.\n";