V4L/DVB (5825): Alter the tuner type for the WinTV USB UK PAL model.
[linux-2.6/sactl.git] / scripts / checkpatch.pl
blob25e20a27fc59b79b4c42d63b284564dd36c1becf
1 #!/usr/bin/perl -w
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
7 use strict;
9 my $P = $0;
10 $P =~ s@.*/@@g;
12 my $V = '0.07';
14 use Getopt::Long qw(:config no_auto_abbrev);
16 my $quiet = 0;
17 my $tree = 1;
18 my $chk_signoff = 1;
19 my $chk_patch = 1;
20 my $tst_type = 0;
21 GetOptions(
22 'q|quiet' => \$quiet,
23 'tree!' => \$tree,
24 'signoff!' => \$chk_signoff,
25 'patch!' => \$chk_patch,
26 'test-type!' => \$tst_type,
27 ) or exit;
29 my $exit = 0;
31 if ($#ARGV < 0) {
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";
36 exit(1);
39 if ($tree && !top_of_kernel_tree()) {
40 print "Must be run from the top-level dir. of a kernel tree\n";
41 exit(2);
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";
49 while (<REMOVE>) {
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);
65 my @rawlines = ();
66 while (<>) {
67 chomp;
68 push(@rawlines, $_);
69 if (eof(ARGV)) {
70 if (!process($ARGV, @rawlines)) {
71 $exit = 1;
73 @rawlines = ();
77 exit($exit);
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")) {
85 return 1;
87 return 0;
90 sub expand_tabs {
91 my ($str) = @_;
93 my $res = '';
94 my $n = 0;
95 for my $c (split(//, $str)) {
96 if ($c eq "\t") {
97 $res .= ' ';
98 $n++;
99 for (; ($n % 8) != 0; $n++) {
100 $res .= ' ';
102 next;
104 $res .= $c;
105 $n++;
108 return $res;
111 sub line_stats {
112 my ($line) = @_;
114 # Drop the diff line leader and expand tabs
115 $line =~ s/^.//;
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));
124 sub sanitise_line {
125 my ($line) = @_;
127 my $res = '';
128 my $l = '';
130 my $quote = '';
132 foreach my $c (split(//, $line)) {
133 if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
134 if ($quote eq '') {
135 $quote = $c;
136 $res .= $c;
137 $l = $c;
138 next;
139 } elsif ($quote eq $c) {
140 $quote = '';
143 if ($quote && $c ne "\t") {
144 $res .= "X";
145 } else {
146 $res .= $c;
149 $l = $c;
152 return $res;
155 sub ctx_block_get {
156 my ($linenr, $remain, $outer, $open, $close) = @_;
157 my $line;
158 my $start = $linenr - 1;
159 my $blk = '';
160 my @o;
161 my @c;
162 my @res = ();
164 for ($line = $start; $remain > 0; $line++) {
165 next if ($rawlines[$line] =~ /^-/);
166 $remain--;
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));
180 return @res;
182 sub ctx_block_outer {
183 my ($linenr, $remain) = @_;
185 return ctx_block_get($linenr, $remain, 1, '\{', '\}');
187 sub ctx_block {
188 my ($linenr, $remain) = @_;
190 return ctx_block_get($linenr, $remain, 0, '\{', '\}');
192 sub ctx_statement {
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
206 # comment.
207 my $in_comment = 0;
208 $current_comment = '';
209 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
210 my $line = $rawlines[$linenr - 1];
211 #warn " $line\n";
212 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
213 $in_comment = 1;
215 if ($line =~ m@/\*@) {
216 $in_comment = 1;
218 if (!$in_comment && $current_comment ne '') {
219 $current_comment = '';
221 $current_comment .= $line . "\n" if ($in_comment);
222 if ($line =~ m@\*/@) {
223 $in_comment = 0;
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";
237 return ($cmt ne '');
240 sub cat_vet {
241 my ($vet) = @_;
243 $vet =~ s/\t/^I/;
244 $vet =~ s/$/\$/;
246 return $vet;
249 sub ERROR {
250 print "ERROR: $_[0]\n";
251 our $clean = 0;
253 sub WARN {
254 print "WARNING: $_[0]\n";
255 our $clean = 0;
257 sub CHK {
258 print "CHECK: $_[0]\n";
259 our $clean = 0;
262 sub process {
263 my $filename = shift;
264 my @lines = @_;
266 my $linenr=0;
267 my $prevline="";
268 my $stashline="";
270 my $length;
271 my $indent;
272 my $previndent=0;
273 my $stashindent=0;
275 our $clean = 1;
276 my $signoff = 0;
277 my $is_patch = 0;
279 # Trace the real file/line as we go.
280 my $realfile = '';
281 my $realline = 0;
282 my $realcnt = 0;
283 my $here = '';
284 my $in_comment = 0;
285 my $first_line = 0;
287 my $Ident = qr{[A-Za-z\d_]+};
288 my $Storage = qr{extern|static};
289 my $Sparse = qr{__user|__kernel|__force|__iomem};
290 my $NonptrType = qr{
292 (?:const\s+)?
293 (?:unsigned\s+)?
295 void|
296 char|
297 short|
298 int|
299 long|
300 unsigned|
301 float|
302 double|
303 long\s+int|
304 long\s+long|
305 long\s+long\s+int|
306 u8|u16|u32|u64|
307 s8|s16|s32|s64|
308 struct\s+$Ident|
309 union\s+$Ident|
310 enum\s+$Ident|
311 ${Ident}_t
313 (?:\s+$Sparse)*
316 my $Type = qr{
317 \b$NonptrType\b
318 (?:\s*\*+\s*const|\s*\*+)?
320 my $Declare = qr{(?:$Storage\s+)?$Type};
321 my $Attribute = qr{__read_mostly|__init|__initdata};
323 # Pre-scan the patch looking for any __setup documentation.
324 my @setup_docs = ();
325 my $setup_docs = 0;
326 foreach my $line (@lines) {
327 if ($line=~/^\+\+\+\s+(\S+)/) {
328 $setup_docs = 0;
329 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
330 $setup_docs = 1;
332 next;
335 if ($setup_docs && $line =~ /^\+/) {
336 push(@setup_docs, $line);
340 foreach my $line (@lines) {
341 $linenr++;
343 my $rawline = $line;
345 #extract the filename as it passes
346 if ($line=~/^\+\+\+\s+(\S+)/) {
347 $realfile=$1;
348 $realfile =~ s@^[^/]*/@@;
349 $in_comment = 0;
350 next;
352 #extract the line range in the file after the patch is applied
353 if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
354 $is_patch = 1;
355 $first_line = $linenr + 1;
356 $in_comment = 0;
357 $realline=$1-1;
358 if (defined $2) {
359 $realcnt=$3+1;
360 } else {
361 $realcnt=1+1;
363 next;
366 # track the line number as we move through the hunk, note that
367 # new versions of GNU diff omit the leading space on completely
368 # blank context lines so we need to count that too.
369 if ($line =~ /^( |\+|$)/) {
370 $realline++;
371 $realcnt-- if ($realcnt != 0);
373 # track any sort of multi-line comment. Obviously if
374 # the added text or context do not include the whole
375 # comment we will not see it. Such is life.
377 # Guestimate if this is a continuing comment. If this
378 # is the start of a diff block and this line starts
379 # ' *' then it is very likely a comment.
380 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
381 $in_comment = 1;
383 if ($line =~ m@/\*@) {
384 $in_comment = 1;
386 if ($line =~ m@\*/@) {
387 $in_comment = 0;
390 # Measure the line length and indent.
391 ($length, $indent) = line_stats($line);
393 # Track the previous line.
394 ($prevline, $stashline) = ($stashline, $line);
395 ($previndent, $stashindent) = ($stashindent, $indent);
396 } elsif ($realcnt == 1) {
397 $realcnt--;
400 #make up the handle for any error we report on this line
401 $here = "#$linenr: ";
402 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
404 my $hereline = "$here\n$line\n";
405 my $herecurr = "$here\n$line\n";
406 my $hereprev = "$here\n$prevline\n$line\n";
408 #check the patch for a signoff:
409 if ($line =~ /^\s*signed-off-by:/i) {
410 # This is a signoff, if ugly, so do not double report.
411 $signoff++;
412 if (!($line =~ /^\s*Signed-off-by:/)) {
413 WARN("Signed-off-by: is the preferred form\n" .
414 $herecurr);
416 if ($line =~ /^\s*signed-off-by:\S/i) {
417 WARN("need space after Signed-off-by:\n" .
418 $herecurr);
422 # Check for wrappage within a valid hunk of the file
423 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) {
424 ERROR("patch seems to be corrupt (line wrapped?)\n" .
425 $herecurr);
428 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
429 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
430 !($line =~ m/^(
431 [\x09\x0A\x0D\x20-\x7E] # ASCII
432 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
433 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
434 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
435 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
436 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
437 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
438 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
439 )*$/x )) {
440 ERROR("Invalid UTF-8\n" . $herecurr);
443 #ignore lines being removed
444 if ($line=~/^-/) {next;}
446 # check we are in a valid source file if not then ignore this hunk
447 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
449 #trailing whitespace
450 if ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) {
451 my $herevet = "$here\n" . cat_vet($line) . "\n";
452 ERROR("trailing whitespace\n" . $herevet);
454 #80 column limit
455 if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) {
456 WARN("line over 80 characters\n" . $herecurr);
459 # check we are in a valid source file *.[hc] if not then ignore this hunk
460 next if ($realfile !~ /\.[hc]$/);
462 # at the beginning of a line any tabs must come first and anything
463 # more than 8 must use tabs.
464 if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) {
465 my $herevet = "$here\n" . cat_vet($line) . "\n";
466 ERROR("use tabs not spaces\n" . $herevet);
470 # The rest of our checks refer specifically to C style
471 # only apply those _outside_ comments.
473 next if ($in_comment);
475 # Remove comments from the line before processing.
476 $line =~ s@/\*.*\*/@@g;
477 $line =~ s@/\*.*@@;
478 $line =~ s@.*\*/@@;
480 # Standardise the strings and chars within the input to simplify matching.
481 $line = sanitise_line($line);
484 # Checks which may be anchored in the context.
487 # Check for switch () and associated case and default
488 # statements should be at the same indent.
489 if ($line=~/\bswitch\s*\(.*\)/) {
490 my $err = '';
491 my $sep = '';
492 my @ctx = ctx_block_outer($linenr, $realcnt);
493 shift(@ctx);
494 for my $ctx (@ctx) {
495 my ($clen, $cindent) = line_stats($ctx);
496 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
497 $indent != $cindent) {
498 $err .= "$sep$ctx\n";
499 $sep = '';
500 } else {
501 $sep = "[...]\n";
504 if ($err ne '') {
505 ERROR("switch and case should be at the same indent\n$hereline\n$err\n");
509 # if/while/etc brace do not go on next line, unless defining a do while loop,
510 # or if that brace on the next line is for something else
511 if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
512 my @ctx = ctx_statement($linenr, $realcnt);
513 my $ctx_ln = $linenr + $#ctx + 1;
514 my $ctx_cnt = $realcnt - $#ctx - 1;
515 my $ctx = join("\n", @ctx);
517 while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
518 $ctx_ln++;
519 $ctx_cnt--;
521 ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
523 if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
524 ERROR("That { should be on the previous line\n" .
525 "$here\n$ctx\n$lines[$ctx_ln - 1]");
529 #ignore lines not being added
530 if ($line=~/^[^\+]/) {next;}
532 # TEST: allow direct testing of the type matcher.
533 if ($tst_type && $line =~ /^.$Declare$/) {
534 ERROR("TEST: is type $Declare\n" . $herecurr);
535 next;
539 # Checks which are anchored on the added line.
542 # check for malformed paths in #include statements (uses RAW line)
543 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
544 my $path = $1;
545 if ($path =~ m{//}) {
546 ERROR("malformed #include filename\n" .
547 $herecurr);
549 # Sanitise this special form of string.
550 $path = 'X' x length($path);
551 $line =~ s{\<.*\>}{<$path>};
554 # no C99 // comments
555 if ($line =~ m{//}) {
556 ERROR("do not use C99 // comments\n" . $herecurr);
558 # Remove C99 comments.
559 $line =~ s@//.*@@;
561 #EXPORT_SYMBOL should immediately follow its function closing }.
562 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
563 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
564 my $name = $1;
565 if (($prevline !~ /^}/) &&
566 ($prevline !~ /^\+}/) &&
567 ($prevline !~ /^ }/) &&
568 ($prevline !~ /\s$name(?:\s+$Attribute)?\s*(?:;|=)/)) {
569 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
573 # check for static initialisers.
574 if ($line=~/\s*static\s.*=\s+(0|NULL);/) {
575 ERROR("do not initialise statics to 0 or NULL\n" .
576 $herecurr);
579 # check for new typedefs, only function parameters and sparse annotations
580 # make sense.
581 if ($line =~ /\btypedef\s/ &&
582 $line !~ /\btypedef\s+$Type\s+\(\s*\*$Ident\s*\)\s*\(/ &&
583 $line !~ /\b__bitwise(?:__|)\b/) {
584 WARN("do not add new typedefs\n" . $herecurr);
587 # * goes on variable not on type
588 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
589 ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
590 $herecurr);
592 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
593 ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
594 $herecurr);
596 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+const)?\s+[A-Za-z\d_]+}) {
597 ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
598 $herecurr);
600 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+const)\s+[A-Za-z\d_]+}) {
601 ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
602 $herecurr);
605 # # no BUG() or BUG_ON()
606 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
607 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
608 # print "$herecurr";
609 # $clean = 0;
612 # printk should use KERN_* levels. Note that follow on printk's on the
613 # same line do not need a level, so we use the current block context
614 # to try and find and validate the current printk. In summary the current
615 # printk includes all preceeding printk's which have no newline on the end.
616 # we assume the first bad printk is the one to report.
617 if ($line =~ /\bprintk\((?!KERN_)/) {
618 my $ok = 0;
619 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
620 #print "CHECK<$lines[$ln - 1]\n";
621 # we have a preceeding printk if it ends
622 # with "\n" ignore it, else it is to blame
623 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
624 if ($rawlines[$ln - 1] !~ m{\\n"}) {
625 $ok = 1;
627 last;
630 if ($ok == 0) {
631 WARN("printk() should include KERN_ facility level\n" . $herecurr);
635 # function brace can't be on same line, except for #defines of do while,
636 # or if closed on same line
637 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and
638 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
639 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
642 # Check operator spacing.
643 # Note we expand the line with the leading + as the real
644 # line will be displayed with the leading + and the tabs
645 # will therefore also expand that way.
646 my $opline = $line;
647 $opline = expand_tabs($opline);
648 $opline =~ s/^./ /;
649 if (!($line=~/\#\s*include/)) {
650 my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline);
651 my $off = 0;
652 for (my $n = 0; $n < $#elements; $n += 2) {
653 $off += length($elements[$n]);
655 my $a = '';
656 $a = 'V' if ($elements[$n] ne '');
657 $a = 'W' if ($elements[$n] =~ /\s$/);
658 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
659 $a = 'O' if ($elements[$n] eq '');
660 $a = 'E' if ($elements[$n] eq '' && $n == 0);
662 my $op = $elements[$n + 1];
664 my $c = '';
665 if (defined $elements[$n + 2]) {
666 $c = 'V' if ($elements[$n + 2] ne '');
667 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
668 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
669 $c = 'O' if ($elements[$n + 2] eq '');
670 } else {
671 $c = 'E';
674 # Pick up the preceeding and succeeding characters.
675 my $ca = substr($opline, 0, $off);
676 my $cc = '';
677 if (length($opline) >= ($off + length($elements[$n + 1]))) {
678 $cc = substr($opline, $off + length($elements[$n + 1]));
680 my $cb = "$ca$;$cc";
682 my $ctx = "${a}x${c}";
684 my $at = "(ctx:$ctx)";
686 my $ptr = (" " x $off) . "^";
687 my $hereptr = "$hereline$ptr\n";
689 ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n";
691 # ; should have either the end of line or a space or \ after it
692 if ($op eq ';') {
693 if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ &&
694 $cc !~ /^;/) {
695 ERROR("need space after that '$op' $at\n" . $hereptr);
698 # // is a comment
699 } elsif ($op eq '//') {
701 # -> should have no spaces
702 } elsif ($op eq '->') {
703 if ($ctx =~ /Wx.|.xW/) {
704 ERROR("no spaces around that '$op' $at\n" . $hereptr);
707 # , must have a space on the right.
708 } elsif ($op eq ',') {
709 if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
710 ERROR("need space after that '$op' $at\n" . $hereptr);
713 # unary ! and unary ~ are allowed no space on the right
714 } elsif ($op eq '!' or $op eq '~') {
715 if ($ctx !~ /[WOEB]x./) {
716 ERROR("need space before that '$op' $at\n" . $hereptr);
718 if ($ctx =~ /.xW/) {
719 ERROR("no space after that '$op' $at\n" . $hereptr);
722 # unary ++ and unary -- are allowed no space on one side.
723 } elsif ($op eq '++' or $op eq '--') {
724 if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
725 ERROR("need space one side of that '$op' $at\n" . $hereptr);
727 if ($ctx =~ /Wx./ && $cc =~ /^;/) {
728 ERROR("no space before that '$op' $at\n" . $hereptr);
731 # & is both unary and binary
732 # unary:
733 # a &b
734 # binary (consistent spacing):
735 # a&b OK
736 # a & b OK
738 # boiling down to: if there is a space on the right then there
739 # should be one on the left.
741 # - is the same
743 } elsif ($op eq '&' or $op eq '-') {
744 if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) {
745 ERROR("need space before that '$op' $at\n" . $hereptr);
748 # * is the same as & only adding:
749 # type:
750 # (foo *)
751 # (foo **)
753 } elsif ($op eq '*') {
754 if ($ca !~ /$Type$/ && $cb !~ /(\*$;|$;\*)/ &&
755 $ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB|BxB/) {
756 ERROR("need space before that '$op' $at\n" . $hereptr);
759 # << and >> may either have or not have spaces both sides
760 } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
761 $op eq '^' or $op eq '|')
763 if ($ctx !~ /VxV|WxW|VxE|WxE/) {
764 ERROR("need consistent spacing around '$op' $at\n" .
765 $hereptr);
768 # All the others need spaces both sides.
769 } elsif ($ctx !~ /[EW]x[WE]/) {
770 ERROR("need spaces around that '$op' $at\n" . $hereptr);
772 $off += length($elements[$n + 1]);
776 #need space before brace following if, while, etc
777 if ($line =~ /\(.*\){/ || $line =~ /do{/) {
778 ERROR("need a space before the open brace '{'\n" . $herecurr);
781 # closing brace should have a space following it when it has anything
782 # on the line
783 if ($line =~ /}(?!(?:,|;|\)))\S/) {
784 ERROR("need a space after that close brace '}'\n" . $herecurr);
787 #goto labels aren't indented, allow a single space however
788 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
789 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
790 WARN("labels should not be indented\n" . $herecurr);
793 # Need a space before open parenthesis after if, while etc
794 if ($line=~/\b(if|while|for|switch)\(/) {
795 ERROR("need a space before the open parenthesis '('\n" . $herecurr);
798 # Check for illegal assignment in if conditional.
799 if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) {
800 #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
801 ERROR("do not use assignment in if condition\n" . $herecurr);
804 # Check for }<nl>else {, these must be at the same
805 # indent level to be relevant to each other.
806 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
807 $previndent == $indent) {
808 ERROR("else should follow close brace '}'\n" . $hereprev);
811 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
812 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
813 # print "No studly caps, use _\n";
814 # print "$herecurr";
815 # $clean = 0;
818 #no spaces allowed after \ in define
819 if ($line=~/\#define.*\\\s$/) {
820 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
823 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
824 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
825 my $checkfile = "include/linux/$1.h";
826 if (-f $checkfile) {
827 CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
828 $herecurr);
832 # if and else should not have general statements after it
833 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ &&
834 $1 !~ /^\s*(?:\sif|{|\\|$)/) {
835 ERROR("trailing statements should be on next line\n" . $herecurr);
838 # multi-statement macros should be enclosed in a do while loop, grab the
839 # first statement and ensure its the whole macro if its not enclosed
840 # in a known goot container
841 if (($prevline=~/\#define.*\\/) and
842 !($prevline=~/do\s+{/) and !($prevline=~/\(\{/) and
843 !($line=~/do.*{/) and !($line=~/\(\{/) and
844 !($line=~/^.\s*$Declare\s/)) {
845 # Grab the first statement, if that is the entire macro
846 # its ok. This may start either on the #define line
847 # or the one below.
848 my $ln = $linenr;
849 my $cnt = $realcnt;
851 # If the macro starts on the define line start there.
852 if ($prevline !~ m{^.#\s*define\s*$Ident(?:\([^\)]*\))?\s*\\\s*$}) {
853 $ln--;
854 $cnt++;
856 my @ctx = ctx_statement($ln, $cnt);
857 my $ctx_ln = $ln + $#ctx + 1;
858 my $ctx = join("\n", @ctx);
860 # Pull in any empty extension lines.
861 while ($ctx =~ /\\$/ &&
862 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
863 $ctx .= $lines[$ctx_ln - 1];
864 $ctx_ln++;
867 if ($ctx =~ /\\$/) {
868 if ($ctx =~ /;/) {
869 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
870 } else {
871 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
876 # don't include deprecated include files (uses RAW line)
877 for my $inc (@dep_includes) {
878 if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
879 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
883 # don't use deprecated functions
884 for my $func (@dep_functions) {
885 if ($line =~ /\b$func\b/) {
886 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
890 # no volatiles please
891 if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) {
892 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
895 # warn about #if 0
896 if ($line =~ /^.#\s*if\s+0\b/) {
897 CHK("if this code is redundant consider removing it\n" .
898 $herecurr);
901 # warn about #ifdefs in C files
902 # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
903 # print "#ifdef in C files should be avoided\n";
904 # print "$herecurr";
905 # $clean = 0;
908 # check for spinlock_t definitions without a comment.
909 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
910 my $which = $1;
911 if (!ctx_has_comment($first_line, $linenr)) {
912 CHK("$1 definition without comment\n" . $herecurr);
915 # check for memory barriers without a comment.
916 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
917 if (!ctx_has_comment($first_line, $linenr)) {
918 CHK("memory barrier without comment\n" . $herecurr);
921 # check of hardware specific defines
922 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) {
923 CHK("architecture specific defines should be avoided\n" . $herecurr);
926 # check the location of the inline attribute, that it is between
927 # storage class and type.
928 if ($line =~ /$Type\s+(?:inline|__always_inline)\b/ ||
929 $line =~ /\b(?:inline|always_inline)\s+$Storage/) {
930 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
933 # check for new externs in .c files.
934 if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
935 WARN("externs should be avoided in .c files\n" . $herecurr);
938 # checks for new __setup's
939 if ($rawline =~ /\b__setup\("([^"]*)"/) {
940 my $name = $1;
942 if (!grep(/$name/, @setup_docs)) {
943 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
948 if ($chk_patch && !$is_patch) {
949 ERROR("Does not appear to be a unified-diff format patch\n");
951 if ($is_patch && $chk_signoff && $signoff == 0) {
952 ERROR("Missing Signed-off-by: line(s)\n");
955 if ($clean == 1 && $quiet == 0) {
956 print "Your patch has no obvious style problems and is ready for submission.\n"
958 if ($clean == 0 && $quiet == 0) {
959 print "Your patch has style problems, please review. If any of these errors\n";
960 print "are false positives report them to the maintainer, see\n";
961 print "CHECKPATCH in MAINTAINERS.\n";
963 return $clean;