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 = qr{[A-Za-z\d_]+};
275 my $Storage = qr{extern|static};
276 my $Sparse = qr{__user|__kernel|__force|__iomem};
302 (?:\s*\*+\s*const|\s*\*+)?
304 my $Declare = qr{(?:$Storage\s+)?$Type};
305 my $Attribute = qr{__read_mostly|__init|__initdata};
307 foreach my $line (@lines) {
312 #extract the filename as it passes
313 if ($line=~/^\+\+\+\s+(\S+)/) {
315 $realfile =~ s@^[^/]*/@@;
319 #extract the line range in the file after the patch is applied
320 if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
322 $first_line = $linenr + 1;
333 # track the line number as we move through the hunk, note that
334 # new versions of GNU diff omit the leading space on completely
335 # blank context lines so we need to count that too.
336 if ($line =~ /^( |\+|$)/) {
338 $realcnt-- if ($realcnt != 0);
340 # track any sort of multi-line comment. Obviously if
341 # the added text or context do not include the whole
342 # comment we will not see it. Such is life.
344 # Guestimate if this is a continuing comment. If this
345 # is the start of a diff block and this line starts
346 # ' *' then it is very likely a comment.
347 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
350 if ($line =~ m@/\*@) {
353 if ($line =~ m@\*/@) {
357 # Measure the line length and indent.
358 ($length, $indent) = line_stats($line);
360 # Track the previous line.
361 ($prevline, $stashline) = ($stashline, $line);
362 ($previndent, $stashindent) = ($stashindent, $indent);
363 } elsif ($realcnt == 1) {
367 #make up the handle for any error we report on this line
368 $here = "#$linenr: ";
369 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
371 my $hereline = "$here\n$line\n";
372 my $herecurr = "$here\n$line\n\n";
373 my $hereprev = "$here\n$prevline\n$line\n\n";
375 #check the patch for a signoff:
376 if ($line =~ /^\s*signed-off-by:/i) {
377 # This is a signoff, if ugly, so do not double report.
379 if (!($line =~ /^\s*Signed-off-by:/)) {
380 print "Signed-off-by: is the preferred form\n";
384 if ($line =~ /^\s*signed-off-by:\S/i) {
385 print "need space after Signed-off-by:\n";
391 # Check for wrappage within a valid hunk of the file
392 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) {
393 print "patch seems to be corrupt (line wrapped?) [$realcnt]\n";
398 #ignore lines being removed
399 if ($line=~/^-/) {next;}
401 # check we are in a valid source file if not then ignore this hunk
402 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
405 if ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) {
406 my $herevet = "$here\n" . cat_vet($line) . "\n\n";
407 print "trailing whitespace\n";
412 if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) {
413 print "line over 80 characters\n";
418 # check we are in a valid source file *.[hc] if not then ignore this hunk
419 next if ($realfile !~ /\.[hc]$/);
421 # at the beginning of a line any tabs must come first and anything
422 # more than 8 must use tabs.
423 if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) {
424 my $herevet = "$here\n" . cat_vet($line) . "\n\n";
425 print "use tabs not spaces\n";
431 # The rest of our checks refer specifically to C style
432 # only apply those _outside_ comments.
434 next if ($in_comment);
436 # Remove comments from the line before processing.
437 $line =~ s@/\*.*\*/@@g;
441 # Standardise the strings and chars within the input to simplify matching.
442 $line = sanitise_line($line);
445 # Checks which may be anchored in the context.
448 # Check for switch () and associated case and default
449 # statements should be at the same indent.
450 if ($line=~/\bswitch\s*\(.*\)/) {
453 my @ctx = ctx_block_outer($linenr, $realcnt);
456 my ($clen, $cindent) = line_stats($ctx);
457 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
458 $indent != $cindent) {
459 $err .= "$sep$ctx\n";
466 print "switch and case should be at the same indent\n";
467 print "$here\n$line\n$err\n";
472 #ignore lines not being added
473 if ($line=~/^[^\+]/) {next;}
475 # TEST: allow direct testing of the type matcher.
476 if ($tst_type && $line =~ /^.$Declare$/) {
477 print "TEST: is type $Declare\n";
484 # Checks which are anchored on the added line.
487 # check for malformed paths in #include statements (uses RAW line)
488 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
490 if ($path =~ m{//}) {
491 print "malformed #include filename\n";
495 # Sanitise this special form of string.
496 $path = 'X' x length($path);
497 $line =~ s{\<.*\>}{<$path>};
501 if ($line =~ m{//}) {
502 print "do not use C99 // comments\n";
506 # Remove C99 comments.
509 #EXPORT_SYMBOL should immediately follow its function closing }.
510 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
511 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
513 if (($prevline !~ /^}/) &&
514 ($prevline !~ /^\+}/) &&
515 ($prevline !~ /^ }/) &&
516 ($prevline !~ /\s$name(?:\s+$Attribute)?\s*(?:;|=)/)) {
517 print "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n";
523 # check for static initialisers.
524 if ($line=~/\s*static\s.*=\s+(0|NULL);/) {
525 print "do not initialise statics to 0 or NULL\n";
530 # check for new typedefs, only function parameters and sparse annotations
532 if ($line =~ /\btypedef\s/ &&
533 $line !~ /\btypedef\s+$Type\s+\(\s*$Ident\s*\)\s*\(/ &&
534 $line !~ /\b__bitwise(?:__|)\b/) {
535 print "do not add new typedefs\n";
540 # * goes on variable not on type
541 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
542 print "\"(foo$1)\" should be \"(foo $1)\"\n";
546 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
547 print "\"(foo $1 )\" should be \"(foo $1)\"\n";
551 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+const)?\s+[A-Za-z\d_]+}) {
552 print "\"foo$1 bar\" should be \"foo $1bar\"\n";
556 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+const)\s+[A-Za-z\d_]+}) {
557 print "\"foo $1 bar\" should be \"foo $1bar\"\n";
562 # # no BUG() or BUG_ON()
563 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
564 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
569 # printk should use KERN_* levels. Note that follow on printk's on the
570 # same line do not need a level, so we use the current block context
571 # to try and find and validate the current printk. In summary the current
572 # printk includes all preceeding printk's which have no newline on the end.
573 # we assume the first bad printk is the one to report.
574 if ($line =~ /\bprintk\((?!KERN_)/) {
576 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
577 #print "CHECK<$lines[$ln - 1]\n";
578 # we have a preceeding printk if it ends
579 # with "\n" ignore it, else it is to blame
580 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
581 if ($rawlines[$ln - 1] !~ m{\\n"}) {
588 print "printk() should include KERN_ facility level\n";
594 # function brace can't be on same line, except for #defines of do while,
595 # or if closed on same line
596 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and
597 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
598 print "braces following function declarations go on the next line\n";
603 # Check operator spacing.
604 # Note we expand the line with the leading + as the real
605 # line will be displayed with the leading + and the tabs
606 # will therefore also expand that way.
608 $opline = expand_tabs($opline);
610 if (!($line=~/\#\s*include/)) {
611 my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline);
613 for (my $n = 0; $n < $#elements; $n += 2) {
614 $off += length($elements[$n]);
617 $a = 'V' if ($elements[$n] ne '');
618 $a = 'W' if ($elements[$n] =~ /\s$/);
619 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
620 $a = 'O' if ($elements[$n] eq '');
621 $a = 'E' if ($elements[$n] eq '' && $n == 0);
623 my $op = $elements[$n + 1];
626 if (defined $elements[$n + 2]) {
627 $c = 'V' if ($elements[$n + 2] ne '');
628 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
629 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
630 $c = 'O' if ($elements[$n + 2] eq '');
635 # Pick up the preceeding and succeeding characters.
636 my $ca = substr($opline, $off - 1, 1);
638 if (length($opline) >= ($off + length($elements[$n + 1]))) {
639 $cc = substr($opline, $off + length($elements[$n + 1]));
642 my $ctx = "${a}x${c}";
644 my $at = "(ctx:$ctx)";
646 my $ptr = (" " x $off) . "^";
647 my $hereptr = "$hereline$ptr\n\n";
649 ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n";
651 # ; should have either the end of line or a space or \ after it
653 if ($ctx !~ /.x[WE]/ && $cc !~ /^\\/) {
654 print "need space after that '$op' $at\n";
660 } elsif ($op eq '//') {
662 # -> should have no spaces
663 } elsif ($op eq '->') {
664 if ($ctx =~ /Wx.|.xW/) {
665 print "no spaces around that '$op' $at\n";
670 # , must have a space on the right.
671 } elsif ($op eq ',') {
672 if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
673 print "need space after that '$op' $at\n";
678 # unary ! and unary ~ are allowed no space on the right
679 } elsif ($op eq '!' or $op eq '~') {
680 if ($ctx !~ /[WOEB]x./) {
681 print "need space before that '$op' $at\n";
686 print "no space after that '$op' $at\n";
691 # unary ++ and unary -- are allowed no space on one side.
692 } elsif ($op eq '++' or $op eq '--') {
693 if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
694 print "need space one side of that '$op' $at\n";
698 if ($ctx =~ /Wx./ && $cc =~ /^;/) {
699 print "no space before that '$op' $at\n";
704 # & is both unary and binary
707 # binary (consistent spacing):
711 # boiling down to: if there is a space on the right then there
712 # should be one on the left.
716 } elsif ($op eq '&' or $op eq '-') {
717 if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) {
718 print "need space before that '$op' $at\n";
723 # * is the same as & only adding:
728 } elsif ($op eq '*') {
730 if ($cc =~ /^\s(?!\s*const)/) {
731 print "no space after that '$op' $at\n";
735 } elsif ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB|BxB/) {
736 print "need space before that '$op' $at\n";
741 # << and >> may either have or not have spaces both sides
742 } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
743 $op eq '^' or $op eq '|')
745 if ($ctx !~ /VxV|WxW|VxE|WxE/) {
746 print "need consistent spacing around '$op' $at\n";
751 # All the others need spaces both sides.
752 } elsif ($ctx !~ /[EW]x[WE]/) {
753 print "need spaces around that '$op' $at\n";
757 $off += length($elements[$n + 1]);
761 #need space before brace following if, while, etc
762 if ($line=~/\(.*\){/) {
763 print "need a space before the brace\n";
768 #goto labels aren't indented, allow a single space however
769 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
770 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
771 print "labels should not be indented\n";
776 # Need a space before open parenthesis after if, while etc
777 if ($line=~/\b(if|while|for|switch)\(/) {
778 print "need a space before the open parenthesis\n";
783 # Check for illegal assignment in if conditional.
784 if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) {
785 #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
786 print "do not use assignment in if condition\n";
791 # Check for }<nl>else {, these must be at the same
792 # indent level to be relevant to each other.
793 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
794 $previndent == $indent) {
795 print "else should follow close brace\n";
800 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
801 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
802 # print "No studly caps, use _\n";
807 #no spaces allowed after \ in define
808 if ($line=~/\#define.*\\\s$/) {
809 print("Whitepspace after \\ makes next lines useless\n");
814 #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\>}) {
816 my $checkfile = "include/linux/$1.h";
818 print "Use #include <linux/$1.h> instead of <asm/$1.h>\n";
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;
833 my $display_segment = $prevline;
835 while ($remaining > 0 && scalar @opened > scalar @closed) {
836 $prevline .= $next_line;
837 $display_segment .= "\n" . $next_line;
838 $next_line = $lines[$nr_line];
842 @opened = $prevline=~/\(/g;
843 @closed = $prevline=~/\)/g;
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";
854 # if and else should not have general statements after it
855 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ &&
856 $1 !~ /^\s*(?:\sif|{|$)/) {
857 print "trailing statements should be on next line\n";
862 # multi-statement macros should be enclosed in a do while loop, grab the
863 # first statement and ensure its the whole macro if its not enclosed
864 # in a known goot container
865 if (($prevline=~/\#define.*\\/) and
866 !($prevline=~/do\s+{/) and !($prevline=~/\(\{/) and
867 !($line=~/do.*{/) and !($line=~/\(\{/) and
868 !($line=~/^.\s*$Declare\s/)) {
869 # Grab the first statement, if that is the entire macro
870 # its ok. This may start either on the #define line
875 # If the macro starts on the define line start there.
876 if ($prevline !~ m{^.#\s*define\s*$Ident(?:\([^\)]*\))?\s*\\\s*$}) {
880 my $ctx = join('', ctx_statement($ln, $cnt));
884 print "Macros with multiple statements should be enclosed in a do - while loop\n";
886 print "Macros with complex values should be enclosed in parenthesis\n";
893 # don't include deprecated include files (uses RAW line)
894 for my $inc (@dep_includes) {
895 if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
896 print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n";
902 # don't use deprecated functions
903 for my $func (@dep_functions) {
904 if ($line =~ /\b$func\b/) {
905 print "Don't use $func(): see Documentation/feature-removal-schedule.txt\n";
911 # no volatiles please
912 if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) {
913 print "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n";
919 if ($line =~ /^.#\s*if\s+0\b/) {
920 print "#if 0 -- if this code redundant remove it\n";
925 # warn about #ifdefs in C files
926 # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
927 # print "#ifdef in C files should be avoided\n";
932 # check for spinlock_t definitions without a comment.
933 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
935 if (!ctx_has_comment($first_line, $linenr)) {
936 print "$1 definition without comment\n";
941 # 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)\(/) {
943 if (!ctx_has_comment($first_line, $linenr)) {
944 print "memory barrier without comment\n";
949 # check of hardware specific defines
950 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) {
951 print "architecture specific defines should be avoided\n";
956 if ($line =~ /$Type\s+(?:inline|__always_inline)\b/ ||
957 $line =~ /\b(?:inline|always_inline)\s+$Storage/) {
958 print "inline keyword should sit between storage class and type\n";
964 if ($chk_patch && !$is_patch) {
966 print "Does not appear to be a unified-diff format patch\n";
968 if ($is_patch && $chk_signoff && $signoff == 0) {
970 print "Missing Signed-off-by: line(s)\n";
973 if ($clean == 1 && $quiet == 0) {
974 print "Your patch has no obvious style problems and is ready for submission.\n"
976 if ($clean == 0 && $quiet == 0) {
977 print "Your patch has style problems, please review. If any of these errors\n";
978 print "are false positives report them to the maintainer, see\n";
979 print "CHECKPATCH in MAINTAINERS.\n";