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);
23 'signoff!' => \
$chk_signoff,
24 'patch!' => \
$chk_patch,
30 print "usage: $P [options] patchfile\n";
31 print "version: $V\n";
32 print "options: -q => quiet\n";
33 print " --no-tree => run without a kernel tree\n";
37 if ($tree && !top_of_kernel_tree
()) {
38 print "Must be run from the top-level dir. of a kernel tree\n";
42 my @dep_includes = ();
43 my @dep_functions = ();
44 my $removal = 'Documentation/feature-removal-schedule.txt';
45 if ($tree && -f
$removal) {
46 open(REMOVE
, "<$removal") || die "$P: $removal: open failed - $!\n";
48 if (/^Files:\s+(.*\S)/) {
49 for my $file (split(/[, ]+/, $1)) {
50 if ($file =~ m
@include/(.*)@
) {
51 push(@dep_includes, $1);
55 } elsif (/^Funcs:\s+(.*\S)/) {
56 for my $func (split(/[, ]+/, $1)) {
57 push(@dep_functions, $func);
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) = @_;
156 my $start = $linenr - 1;
162 for ($line = $start; $remain > 0; $line++) {
163 next if ($rawlines[$line] =~ /^-/);
166 $blk .= $rawlines[$line];
168 @o = ($blk =~ /\{/g);
169 @c = ($blk =~ /\}/g);
171 if (!$outer || (scalar(@o) - scalar(@c)) == 1) {
172 push(@res, $rawlines[$line]);
175 last if (scalar(@o) == scalar(@c));
180 sub ctx_block_outer
{
181 my ($linenr, $remain) = @_;
183 return ctx_block_get
($linenr, $remain, 1);
186 my ($linenr, $remain) = @_;
188 return ctx_block_get
($linenr, $remain, 0);
191 sub ctx_locate_comment
{
192 my ($first_line, $end_line) = @_;
194 # Catch a comment on the end of the line itself.
195 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@
.*(/\*.*\*/)\s
*$@
);
196 return $current_comment if (defined $current_comment);
198 # Look through the context and try and figure out if there is a
201 $current_comment = '';
202 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
203 my $line = $rawlines[$linenr - 1];
205 if ($linenr == $first_line and $line =~ m@
^.\s
*\
*@
) {
208 if ($line =~ m@
/\
*@
) {
211 if (!$in_comment && $current_comment ne '') {
212 $current_comment = '';
214 $current_comment .= $line . "\n" if ($in_comment);
215 if ($line =~ m@\
*/@
) {
220 chomp($current_comment);
221 return($current_comment);
223 sub ctx_has_comment
{
224 my ($first_line, $end_line) = @_;
225 my $cmt = ctx_locate_comment
($first_line, $end_line);
227 ##print "LINE: $rawlines[$end_line - 1 ]\n";
228 ##print "CMMT: $cmt\n";
243 my $filename = shift;
259 # Trace the real file/line as we go.
267 foreach my $line (@lines) {
270 #extract the filename as it passes
271 if ($line=~/^\+\+\+\s+(\S+)/) {
273 $realfile =~ s@
^[^/]*/@@
;
277 #extract the line range in the file after the patch is applied
278 if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
280 $first_line = $linenr + 1;
291 # track the line number as we move through the hunk, note that
292 # new versions of GNU diff omit the leading space on completely
293 # blank context lines so we need to count that too.
294 if ($line =~ /^( |\+|$)/) {
297 # track any sort of multi-line comment. Obviously if
298 # the added text or context do not include the whole
299 # comment we will not see it. Such is life.
301 # Guestimate if this is a continuing comment. If this
302 # is the start of a diff block and this line starts
303 # ' *' then it is very likely a comment.
304 if ($linenr == $first_line and $line =~ m@
^.\s
*\
*@
) {
307 if ($line =~ m@
/\
*@
) {
310 if ($line =~ m@\
*/@
) {
314 # Measure the line length and indent.
315 ($length, $indent) = line_stats
($line);
317 # Track the previous line.
318 ($prevline, $stashline) = ($stashline, $line);
319 ($previndent, $stashindent) = ($stashindent, $indent);
321 $realcnt-- if ($realcnt != 0);
323 #make up the handle for any error we report on this line
324 $here = "#$linenr: ";
325 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
327 my $hereline = "$here\n$line\n";
328 my $herecurr = "$here\n$line\n\n";
329 my $hereprev = "$here\n$prevline\n$line\n\n";
331 #check the patch for a signoff:
332 if ($line =~ /^\s*Signed-off-by:\s/) {
335 } elsif ($line =~ /^\s*signed-off-by:/i) {
336 # This is a signoff, if ugly, so do not double report.
338 if (!($line =~ /^\s*Signed-off-by:/)) {
339 print "use Signed-off-by:\n";
343 if ($line =~ /^\s*signed-off-by:\S/i) {
344 print "need space after Signed-off-by:\n";
350 # Check for wrappage within a valid hunk of the file
351 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) {
352 print "patch seems to be corrupt (line wrapped?) [$realcnt]\n";
357 #ignore lines being removed
358 if ($line=~/^-/) {next;}
360 # check we are in a valid source file if not then ignore this hunk
361 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
364 if ($line=~/\+.*\S\s+$/) {
365 my $herevet = "$here\n" . cat_vet
($line) . "\n\n";
366 print "trailing whitespace\n";
371 if ($line =~ /^\+/ && !($prevline=~/\/\
*\
*/) && $length > 80) {
372 print "line over 80 characters\n";
377 # check we are in a valid source file *.[hc] if not then ignore this hunk
378 next if ($realfile !~ /\.[hc]$/);
380 # at the beginning of a line any tabs must come first and anything
381 # more than 8 must use tabs.
382 if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) {
383 my $herevet = "$here\n" . cat_vet
($line) . "\n\n";
384 print "use tabs not spaces\n";
390 # The rest of our checks refer specifically to C style
391 # only apply those _outside_ comments.
393 next if ($in_comment);
395 # Remove comments from the line before processing.
396 $line =~ s@
/\*.*\*/@
@g;
401 # Checks which may be anchored in the context.
404 # Check for switch () and associated case and default
405 # statements should be at the same indent.
406 if ($line=~/\bswitch\s*\(.*\)/) {
409 my @ctx = ctx_block_outer
($linenr, $realcnt);
412 my ($clen, $cindent) = line_stats
($ctx);
413 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
414 $indent != $cindent) {
415 $err .= "$sep$ctx\n";
422 print "switch and case should be at the same indent\n";
423 print "$here\n$line\n$err\n";
428 #ignore lines not being added
429 if ($line=~/^[^\+]/) {next;}
432 # Checks which are anchored on the added line.
436 if ($line =~ m{//}) {
437 print "do not use C99 // comments\n";
441 # Remove C99 comments.
444 # Standardise the strings and chars within the input
445 # to simplify matching.
446 $line = sanitise_line
($line);
448 #EXPORT_SYMBOL should immediately follow its function closing }.
449 if (($line =~ /EXPORT_SYMBOL.*\(.*\)/) ||
450 ($line =~ /EXPORT_UNUSED_SYMBOL.*\(.*\)/)) {
451 if (($prevline !~ /^}/) &&
452 ($prevline !~ /^\+}/) &&
453 ($prevline !~ /^ }/)) {
454 print "EXPORT_SYMBOL(func); should immediately follow its function\n";
460 # check for static initialisers.
461 if ($line=~/\s*static\s.*=\s+(0|NULL);/) {
462 print "do not initialise statics to 0 or NULL\n";
467 # check for new typedefs.
468 if ($line=~/\s*typedef\s/) {
469 print "do not add new typedefs\n";
474 # * goes on variable not on type
475 my $type = '(?:char|short|int|long|unsigned|float|double|' .
476 'struct\s+[A-Za-z\d_]+|' .
477 'union\s+[A-Za-z\d_]+)';
479 if ($line =~ m{[A-Za-z\d_]+(\*+) [A-Za-z\d_]+}) {
480 print "\"foo$1 bar\" should be \"foo $1bar\"\n";
484 if ($line =~ m{$type (\*) [A-Za-z\d_]+} ||
485 $line =~ m{[A-Za-z\d_]+ (\*\*+) [A-Za-z\d_]+}) {
486 print "\"foo $1 bar\" should be \"foo $1bar\"\n";
490 if ($line =~ m{\([A-Za-z\d_\s]+[A-Za-z\d_](\*+)\)}) {
491 print "\"(foo$1)\" should be \"(foo $1)\"\n";
495 if ($line =~ m{\([A-Za-z\d_\s]+[A-Za-z\d_]\s+(\*+)\s+\)}) {
496 print "\"(foo $1 )\" should be \"(foo $1)\"\n";
501 # # no BUG() or BUG_ON()
502 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
503 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
508 # printk should use KERN_* levels. Note that follow on printk's on the
509 # same line do not need a level, so we use the current block context
510 # to try and find and validate the current printk. In summary the current
511 # printk includes all preceeding printk's which have no newline on the end.
512 # we assume the first bad printk is the one to report.
513 if ($line =~ /\bprintk\((?!KERN_)/) {
515 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
516 #print "CHECK<$lines[$ln - 1]\n";
517 # we have a preceeding printk if it ends
518 # with "\n" ignore it, else it is to blame
519 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
520 if ($rawlines[$ln - 1] !~ m{\\n"}) {
527 print "printk() should include KERN_ facility level\n";
533 #function brace can't be on same line, except for #defines of do while, or if closed on same line
534 if (($line=~/[A-Za-z\d_]+\**\s+\**[A-Za-z\d_]+\(.*\).* {/) and
535 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
536 print "braces following function declarations go on the next line\n";
540 # Note we expand the line with the leading + as the real
541 # line will be displayed with the leading + and the tabs
542 # will therefore also expand that way.
544 $opline = expand_tabs
($opline);
546 if (!($line=~/\#\s*include/)) {
547 # Check operator spacing.
548 my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\
^=|\
|=|&=|->|<<|>>|<|>|=|!|~|&&|\
|\
||,|\
^|\
+\
+|--|;|&|\
||\
+|-|\
*|\
/\/|\
/)/, $opline);
550 for (my $n = 0; $n < $#elements; $n += 2) {
551 $off += length($elements[$n]);
554 $a = 'V' if ($elements[$n] ne '');
555 $a = 'W' if ($elements[$n] =~ /\s$/);
556 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
557 $a = 'O' if ($elements[$n] eq '');
558 $a = 'E' if ($elements[$n] eq '' && $n == 0);
560 my $op = $elements[$n + 1];
563 if (defined $elements[$n + 2]) {
564 $c = 'V' if ($elements[$n + 2] ne '');
565 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
566 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
567 $c = 'O' if ($elements[$n + 2] eq '');
572 # Pick up the preceeding and succeeding characters.
573 my $ca = substr($opline, $off - 1, 1);
575 if (length($opline) > ($off + length($elements[$n]))) {
576 $cc = substr($opline, $off + 1 + length($elements[$n]), 1);
579 my $ctx = "${a}x${c}";
581 my $at = "(ctx:$ctx)";
583 my $ptr = (" " x
$off) . "^";
584 my $hereptr = "$hereline$ptr\n\n";
586 ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n";
588 # We need ; as an operator. // is a comment.
589 if ($op eq ';' or $op eq '//') {
591 # -> should have no spaces
592 } elsif ($op eq '->') {
593 if ($ctx =~ /Wx.|.xW/) {
594 print "no spaces around that '$op' $at\n";
599 # , must have a space on the right.
600 } elsif ($op eq ',') {
601 if ($ctx !~ /.xW|.xE/) {
602 print "need space after that '$op' $at\n";
607 # unary ! and unary ~ are allowed no space on the right
608 } elsif ($op eq '!' or $op eq '~') {
609 if ($ctx !~ /[WOEB]x./) {
610 print "need space before that '$op' $at\n";
615 print "no space after that '$op' $at\n";
620 # unary ++ and unary -- are allowed no space on one side.
621 } elsif ($op eq '++' or $op eq '--') {
622 if ($ctx !~ /[WOB]x[^W]|[^W]x[WOB]/) {
623 print "need space one side of that '$op' $at\n";
628 # & is both unary and binary
631 # binary (consistent spacing):
635 # boiling down to: if there is a space on the right then there
636 # should be one on the left.
640 } elsif ($op eq '&' or $op eq '-') {
641 if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) {
642 print "need space before that '$op' $at\n";
647 # * is the same as & only adding:
652 } elsif ($op eq '*') {
655 print "no space after that '$op' $at\n";
659 } elsif ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB/) {
660 print "need space before that '$op' $at\n";
665 # << and >> may either have or not have spaces both sides
666 } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
667 $op eq '^' or $op eq '|')
669 if ($ctx !~ /VxV|WxW|VxE|WxE/) {
670 print "need consistent spacing around '$op' $at\n";
675 # All the others need spaces both sides.
676 } elsif ($ctx !~ /[EW]x[WE]/) {
677 print "need spaces around that '$op' $at\n";
681 $off += length($elements[$n + 1]);
685 #need space before brace following if, while, etc
686 if ($line=~/\(.*\){/) {
687 print "need a space before the brace\n";
692 #goto labels aren't indented, allow a single space however
693 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
694 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
695 print "labels should not be indented\n";
700 # Need a space before open parenthesis after if, while etc
701 if ($line=~/\b(if|while|for|switch)\(/) {
702 print "need a space before the open parenthesis\n";
707 # Check for illegal assignment in if conditional.
708 if ($line=~/\b(if|while)\s*\(.*[^<>!=]=[^=].*\)/) {
709 #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
710 print "do not use assignment in condition\n";
715 # Check for }<nl>else {, these must be at the same
716 # indent level to be relevant to each other.
717 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
718 $previndent == $indent) {
719 print "else should follow close brace\n";
724 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
725 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
726 # print "No studly caps, use _\n";
731 #no spaces allowed after \ in define
732 if ($line=~/\#define.*\\\s$/) {
733 print("Whitepspace after \\ makes next lines useless\n");
738 #warn if <asm/foo.h> is #included and <linux/foo.h> is available.
739 if ($tree && $line =~ qr
|\s
*\#\s
*include\s
*\
<asm\
/(.*)\
.h\
>|) {
740 my $checkfile = "include/linux/$1.h";
742 print "Use #include <linux/$1.h> instead of <asm/$1.h>\n";
748 #if/while/etc brace do not go on next line, unless #defining a do while loop, or if that brace on the next line is for something else
749 if ($prevline=~/\b(if|while|for|switch)\s*\(/) {
750 my @opened = $prevline=~/\(/g;
751 my @closed = $prevline=~/\)/g;
752 my $nr_line = $linenr;
753 my $remaining = $realcnt - 1;
754 my $next_line = $line;
756 my $display_segment = $prevline;
758 while ($remaining > 0 && scalar @opened > scalar @closed) {
759 $prevline .= $next_line;
760 $display_segment .= "\n" . $next_line;
761 $next_line = $lines[$nr_line];
765 @opened = $prevline=~/\(/g;
766 @closed = $prevline=~/\)/g;
769 if (($prevline=~/\b(if|while|for|switch)\s*\(.*\)\s*$/) and ($next_line=~/{/) and
770 !($next_line=~/\b(if|while|for)/) and !($next_line=~/\#define.*do.*while/)) {
771 print "That { should be on the previous line\n";
772 print "$here\n$display_segment\n$next_line\n\n";
777 #multiline macros should be enclosed in a do while loop
778 if (($prevline=~/\#define.*\\/) and !($prevline=~/do\s+{/) and
779 !($prevline=~/\(\{/) and ($line=~/;\s*\\/) and
780 !($line=~/do.*{/) and !($line=~/\(\{/)) {
781 print "Macros with multiple statements should be enclosed in a do - while loop\n";
786 # don't include deprecated include files
787 for my $inc (@dep_includes) {
788 if ($line =~ m@\#\s
*include\s
*\
<$inc>@
) {
789 print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n";
795 # don't use deprecated functions
796 for my $func (@dep_functions) {
797 if ($line =~ /\b$func\b/) {
798 print "Don't use $func(): see Documentation/feature-removal-schedule.txt\n";
804 # no volatiles please
805 if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) {
806 print "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n";
812 if ($line =~ /^.#\s*if\s+0\b/) {
813 print "#if 0 -- if this code redundant remove it\n";
818 # warn about #ifdefs in C files
819 # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
820 # print "#ifdef in C files should be avoided\n";
825 # check for spinlock_t definitions without a comment.
826 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
828 if (!ctx_has_comment
($first_line, $linenr)) {
829 print "$1 definition without comment\n";
834 # check for memory barriers without a comment.
835 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
836 if (!ctx_has_comment
($first_line, $linenr)) {
837 print "memory barrier without comment\n";
842 # check of hardware specific defines
843 if ($line =~ m@
^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) {
844 print "architecture specific defines should be avoided\n";
850 if ($chk_patch && !$is_patch) {
852 print "Does not appear to be a unified-diff format patch\n";
854 if ($is_patch && $chk_signoff && $signoff == 0) {
856 print "Missing Signed-off-by: line(s)\n";
859 if ($clean == 1 && $quiet == 0) {
860 print "Your patch has no obvious style problems and is ready for submission.\n"
862 if ($clean == 0 && $quiet == 0) {
863 print "Your patch has style problems, please review. If any of these errors\n";
864 print "are false positives report them to the maintainer, see\n";
865 print "CHECKPATCH in MAINTAINERS.\n";
This page took 0.066478 seconds and 6 git commands to generate.