2 # (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit)
3 # (c) 2005, Joel Scohpp <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
13 use Getopt
::Long
qw(:config no_auto_abbrev);
22 'signoff!' => \
$chk_signoff,
23 'patch!' => \
$chk_patch,
29 print "usage: patchstylecheckemail.pl [options] patchfile\n";
30 print "version: $V\n";
31 print "options: -q => quiet\n";
32 print " --no-tree => run without a kernel tree\n";
36 if ($tree && !top_of_kernel_tree
()) {
37 print "Must be run from the top-level dir. of a kernel tree\n";
42 my $removal = 'Documentation/feature-removal-schedule.txt';
43 if ($tree && -f
$removal) {
44 open(REMOVE
, "<$removal") || die "$P: $removal: open failed - $!\n";
46 if (/^Files:\s+(.*\S)/) {
47 for my $file (split(/[, ]+/, $1)) {
48 if ($file =~ m
@include/(.*)@) {
49 push(@deprecated, $1);
61 if (!process
($ARGV, @lines)) {
70 sub top_of_kernel_tree
{
71 if ((-f
"COPYING") && (-f
"CREDITS") && (-f
"Kbuild") &&
72 (-f
"MAINTAINERS") && (-f
"Makefile") && (-f
"README") &&
73 (-d
"Documentation") && (-d
"arch") && (-d
"include") &&
74 (-d
"drivers") && (-d
"fs") && (-d
"init") && (-d
"ipc") &&
75 (-d
"kernel") && (-d
"lib") && (-d
"scripts")) {
86 for my $c (split(//, $str)) {
90 for (; ($n % 8) != 0; $n++) {
112 my $filename = shift;
119 my $lineforcounting='';
128 # Trace the real file/line as we go.
136 foreach my $line (@lines) {
139 #extract the filename as it passes
140 if ($line=~/^\+\+\+\s+(\S+)/) {
145 #extract the line range in the file after the patch is applied
146 if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
159 #track the line number as we move through the hunk
160 if ($line=~/^[ \+]/) {
162 $realcnt-- if ($realcnt != 0);
164 # track any sort of multi-line comment. Obviously if
165 # the added text or context do not include the whole
166 # comment we will not see it. Such is life.
168 # Guestimate if this is a continuing comment. If this
169 # is the start of a diff block and this line starts
170 # ' *' then it is very likely a comment.
171 if ($first_line and $line =~ m
@^.\s
*\
*@) {
174 if ($line =~ m
@/\*@) {
177 if ($line =~ m
@\*/@) {
181 $lineforcounting = $line;
182 $lineforcounting =~ s/^\+//;
183 $lineforcounting = expand_tabs
($lineforcounting);
185 my ($white) = ($lineforcounting =~ /^(\s*)/);
186 $indent = length($white);
188 # Track the previous line.
189 ($prevline, $stashline) = ($stashline, $line);
190 ($previndent, $stashindent) = ($stashindent, $indent);
194 #make up the handle for any error we report on this line
195 $here = "PATCH: $ARGV:$linenr:";
196 $here .= "\nFILE: $realfile:$realline:" if ($realcnt != 0);
198 my $herecurr = "$here\n$line\n\n";
199 my $hereprev = "$here\n$prevline\n$line\n\n";
201 #check the patch for a signoff:
202 if ($line =~ /^\s*Signed-off-by:\s/) {
205 } elsif ($line =~ /^\s*signed-off-by:/i) {
206 if (!($line =~ /^\s*Signed-off-by:/)) {
207 print "use Signed-off-by:\n";
211 if ($line =~ /^\s*signed-off-by:\S/i) {
212 print "need space after Signed-off-by:\n";
218 #ignore lines not being added
219 if ($line=~/^[^\+]/) {next;}
221 # check we are in a valid source file *.[hcsS] if not then ignore this hunk
222 next if ($realfile !~ /\.[hcsS]$/);
225 if ($line=~/\S\s+$/) {
226 my $herevet = "$here\n" . cat_vet
($line) . "\n\n";
227 print "trailing whitespace\n";
232 if (!($prevline=~/\/\
*\
*/) && length($lineforcounting) > 80) {
233 print "line over 80 characters\n";
238 # check we are in a valid source file *.[hc] if not then ignore this hunk
239 next if ($realfile !~ /\.[hc]$/);
241 # at the beginning of a line any tabs must come first and anything
242 # more than 8 must use tabs.
243 if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) {
244 my $herevet = "$here\n" . cat_vet
($line) . "\n\n";
245 print "use tabs not spaces\n";
251 # The rest of our checks refer specifically to C style
252 # only apply those _outside_ comments.
254 next if ($in_comment);
257 if ($line =~ m
@//@ and !($line =~ m
@\".*//.*\"@)) {
258 print "do not use C99
// comments
\n";
263 # Remove comments from the line before processing.
264 $line =~ s@/\*.*\*/@@g;
269 #EXPORT_SYMBOL should immediately follow its function closing }.
270 if (($line =~ /EXPORT_SYMBOL.*\(.*\)/) ||
271 ($line =~ /EXPORT_UNUSED_SYMBOL.*\(.*\)/)) {
272 if (($prevline !~ /^}/) &&
273 ($prevline !~ /^\+}/) &&
274 ($prevline !~ /^ }/)) {
275 print "EXPORT_SYMBOL
(func
); should immediately follow its function
\n";
281 # check for static initialisers.
282 if ($line=~/\s*static\s.*=\s+(0|NULL);/) {
283 print "do not initialise statics to
0 or NULL
\n";
288 # check for new typedefs.
289 if ($line=~/\s*typedef\s/) {
290 print "do not add new typedefs
\n";
295 # * goes on variable not on type
296 if ($line=~/[A-Za-z\d_]+\* [A-Za-z\d_]+/) {
297 print "\"foo
* bar
\" should be
\"foo
*bar
\"\n";
302 # # no BUG() or BUG_ON()
303 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
304 # print "Try to
use WARN_ON
& Recovery code rather than BUG
() or BUG_ON
()\n";
309 # printk should use KERN_* levels
310 if ($line =~ /\bprintk\((?!KERN_)/) {
311 print "printk
() should include KERN_ facility level
\n";
316 #function brace can't be on same line, except for #defines of do while, or if closed on same line
317 if (($line=~/[A-Za-z\d_]+\**\s+\**[A-Za-z\d_]+\(.*\).* {/) and
318 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
319 print "braces following function declarations go on the
next line
\n";
325 if (!($line=~/\#\s*include/)) {
326 # Check operator spacing.
327 my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline);
328 for (my $n = 0; $n < $#elements; $n += 2) {
329 # $wN says we have white-space before or after
330 # $sN says we have a separator before or after
331 # $oN says we have another operator before or after
332 my $w1 = $elements[$n] =~ /\s$/;
333 my $s1 = $elements[$n] =~ /(\[|\(|\s)$/;
334 my $o1 = $elements[$n] eq '';
335 my $op = $elements[$n + 1];
339 # If we have something after the operator handle it.
340 if (defined $elements[$n + 2]) {
341 $w2 = $elements[$n + 2] =~ /^\s/;
342 $s2 = $elements[$n + 2] =~ /^(\s|\)|\]|;)/;
343 $o2 = $elements[$n + 2] eq '';
346 # Generate the context.
348 for (my $m = $n; $m >= 0; $m--) {
349 if ($elements[$m] ne '') {
350 $at .= $elements[$m];
355 for (my $m = $n + 2; defined $elements[$m]; $m++) {
356 if ($elements[$m] ne '') {
357 $at .= $elements[$m];
362 ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n";
363 # Skip things apparently in quotes.
364 next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
366 # We need ; as an operator. // is a comment.
367 if ($op eq ';' or $op eq '//') {
369 # -> should have no spaces
370 } elsif ($op eq '->') {
372 print "no spaces around that
'$op' $at\n";
377 # , must have a space on the right.
378 } elsif ($op eq ',') {
380 print "need space after that
'$op' $at\n";
385 # unary ! and unary ~ are allowed no space on the right
386 } elsif ($op eq '!' or $op eq '~') {
388 print "need space before that
'$op' $at\n";
393 print "no space after that
'$op' $at\n";
398 # unary ++ and unary -- are allowed no space on one side.
399 } elsif ($op eq '++' or $op eq '--') {
400 if (($s1 && $s2) || ((!$s1 && !$o1) && (!$s2 && !$o2))) {
401 print "need space one side of that
'$op' $at\n";
406 # & is both unary and binary
409 # binary (consistent spacing):
413 # boiling down to: if there is a space on the right then there
414 # should be one on the left.
418 # * is the same only adding:
423 } elsif ($op eq '&' or $op eq '-' or $op eq '*') {
425 print "need space before that
'$op' $at\n";
430 # << and >> may either have or not have spaces both sides
431 } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
432 $op eq '^' or $op eq '|')
435 print "need consistent spacing around
'$op' $at\n";
440 # All the others need spaces both sides.
441 } elsif (!$s1 or !$s2) {
442 print "need spaces around that
'$op' $at\n";
449 #need space before brace following if, while, etc
450 if ($line=~/\(.*\){/) {
451 print "need a space before the brace
\n";
456 #goto labels aren't indented, allow a single space however
457 if ($line=~/^.\s+[A-Za-z\d_]+:/ and
458 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
459 print "labels should
not be indented
\n";
464 # Need a space before open parenthesis after if, while etc
465 if ($line=~/(if|while|for|switch)\(/) {
466 print "need a space before the
open parenthesis
\n";
471 # Check for illegal assignment in if conditional.
472 if ($line=~/(if|while)\s*\(.*[^<>!=]=[^=].*\)/) {
473 print "do not use assignment
in if condition
\n";
478 # Check for }<nl>else {, these must be at the same
479 # indent level to be relevant to each other.
480 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
481 $previndent == $indent) {
482 print "else should follow
close brace
\n";
487 # Check for switch () {<nl>case, these must be at the
488 # same indent. We will only catch the first one, as our
489 # context is very small but people tend to be consistent
490 # so we will catch them out more often than not.
491 if ($prevline=~/\s*switch\s*\(.*\)/ and $line=~/\s*case\s+/
492 and $previndent != $indent) {
493 print "switch
and case should be at the same indent
\n";
498 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
499 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
500 # print "No studly caps
, use _
\n";
505 #no spaces allowed after \ in define
506 if ($line=~/\#define.*\\\s$/) {
507 print("Whitepspace after
\\ makes
next lines useless
\n");
512 #warn if <asm/foo.h> is #included and <linux/foo.h> is available.
513 if ($tree && $line =~ qr|\s*\#\s*include\s*\<asm\/(.*)\.h\>|) {
514 my $checkfile = "include
/linux/$1.h
";
516 print "Use
#include <linux/$1.h> instead of <asm/$1.h>\n";
522 #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
523 if ($prevline=~/(if|while|for|switch)\s*\(/) {
524 my @opened = $prevline=~/\(/g;
525 my @closed = $prevline=~/\)/g;
526 my $nr_line = $linenr;
527 my $remaining = $realcnt;
528 my $next_line = $line;
530 my $display_segment = $prevline;
532 while ($remaining > 0 && scalar @opened > scalar @closed) {
533 $prevline .= $next_line;
534 $display_segment .= "\n" . $next_line;
535 $next_line = $lines[$nr_line];
539 @opened = $prevline=~/\(/g;
540 @closed = $prevline=~/\)/g;
543 if (($prevline=~/(if|while|for|switch)\s*\(.*\)\s*$/) and ($next_line=~/{/) and
544 !($next_line=~/(if|while|for)/) and !($next_line=~/\#define.*do.*while/)) {
545 print "That { should be on the previous line\n";
546 print "$display_segment\n$next_line\n\n";
551 #multiline macros should be enclosed in a do while loop
552 if (($prevline=~/\#define.*\\/) and !($prevline=~/do\s+{/) and
553 !($prevline=~/\(\{/) and ($line=~/;\s*\\/) and
554 !($line=~/do.*{/) and !($line=~/\(\{/)) {
555 print "Macros with multiple statements should be enclosed in a do - while loop\n";
560 # don't include deprecated include files
561 for my $inc (@deprecated) {
562 if ($line =~ m
@\#\s*include\s*\<$inc>@) {
563 print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n";
569 # don't use kernel_thread()
570 if ($line =~ /\bkernel_thread\b/) {
571 print "Don't use kernel_thread(), use kthread(): see Documentation/feature-removal-schedule.txt\n";
577 if ($chk_patch && !$is_patch) {
579 print "Does not appear to be a unified-diff format patch\n";
581 if ($is_patch && $chk_signoff && $signoff == 0) {
583 print "Missing Signed-off-by: line(s)\n";
586 if ($clean == 1 && $quiet == 0) {
587 print "Your patch has no obvious style problems and is ready for submission.\n"
589 if ($clean == 0 && $quiet == 0) {
590 print "Your patch has style problems, please review. If any of these errors\n";
591 print "are false positives report them to the maintainer, see\n";
592 print "CHECKPATCH in MAINTAINERS.\n";