USB: replace remaining __FUNCTION__ occurrences
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / scripts / checkpatch.pl
blob64ec4b8a51b502cca0b37638f4443813d65d35ae
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.16';
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 my $tst_only;
22 my $emacs = 0;
23 my $terse = 0;
24 my $file = 0;
25 my $check = 0;
26 my $summary = 1;
27 my $mailback = 0;
28 my $summary_file = 0;
29 my $root;
30 my %debug;
31 GetOptions(
32 'q|quiet+' => \$quiet,
33 'tree!' => \$tree,
34 'signoff!' => \$chk_signoff,
35 'patch!' => \$chk_patch,
36 'emacs!' => \$emacs,
37 'terse!' => \$terse,
38 'file!' => \$file,
39 'subjective!' => \$check,
40 'strict!' => \$check,
41 'root=s' => \$root,
42 'summary!' => \$summary,
43 'mailback!' => \$mailback,
44 'summary-file!' => \$summary_file,
46 'debug=s' => \%debug,
47 'test-type!' => \$tst_type,
48 'test-only=s' => \$tst_only,
49 ) or exit;
51 my $exit = 0;
53 if ($#ARGV < 0) {
54 print "usage: $P [options] patchfile\n";
55 print "version: $V\n";
56 print "options: -q => quiet\n";
57 print " --no-tree => run without a kernel tree\n";
58 print " --terse => one line per report\n";
59 print " --emacs => emacs compile window format\n";
60 print " --file => check a source file\n";
61 print " --strict => enable more subjective tests\n";
62 print " --root => path to the kernel tree root\n";
63 print " --no-summary => suppress the per-file summary\n";
64 print " --summary-file => include the filename in summary\n";
65 exit(1);
68 my $dbg_values = 0;
69 my $dbg_possible = 0;
70 for my $key (keys %debug) {
71 eval "\${dbg_$key} = '$debug{$key}';"
74 if ($terse) {
75 $emacs = 1;
76 $quiet++;
79 if ($tree) {
80 if (defined $root) {
81 if (!top_of_kernel_tree($root)) {
82 die "$P: $root: --root does not point at a valid tree\n";
84 } else {
85 if (top_of_kernel_tree('.')) {
86 $root = '.';
87 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
88 top_of_kernel_tree($1)) {
89 $root = $1;
93 if (!defined $root) {
94 print "Must be run from the top-level dir. of a kernel tree\n";
95 exit(2);
99 my $emitted_corrupt = 0;
101 our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
102 our $Storage = qr{extern|static|asmlinkage};
103 our $Sparse = qr{
104 __user|
105 __kernel|
106 __force|
107 __iomem|
108 __must_check|
109 __init_refok|
110 __kprobes
112 our $Attribute = qr{
113 const|
114 __read_mostly|
115 __kprobes|
116 __(?:mem|cpu|dev|)(?:initdata|init)
118 our $Inline = qr{inline|__always_inline|noinline};
119 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
120 our $Lval = qr{$Ident(?:$Member)*};
122 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
123 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
124 our $Operators = qr{
125 <=|>=|==|!=|
126 =>|->|<<|>>|<|>|!|~|
127 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
130 our $NonptrType;
131 our $Type;
132 our $Declare;
134 our @typeList = (
135 qr{void},
136 qr{char},
137 qr{short},
138 qr{int},
139 qr{long},
140 qr{unsigned},
141 qr{float},
142 qr{double},
143 qr{bool},
144 qr{long\s+int},
145 qr{long\s+long},
146 qr{long\s+long\s+int},
147 qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
148 qr{struct\s+$Ident},
149 qr{union\s+$Ident},
150 qr{enum\s+$Ident},
151 qr{${Ident}_t},
152 qr{${Ident}_handler},
153 qr{${Ident}_handler_fn},
156 sub build_types {
157 my $all = "(?: \n" . join("|\n ", @typeList) . "\n)";
158 $NonptrType = qr{
160 (?:const\s+)?
161 (?:unsigned\s+)?
163 $all|
164 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)
166 (?:\s+$Sparse|\s+const)*
169 $Type = qr{
170 \b$NonptrType\b
171 (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
172 (?:\s+$Inline|\s+$Sparse|\s+$Attribute)*
174 $Declare = qr{(?:$Storage\s+)?$Type};
176 build_types();
178 $chk_signoff = 0 if ($file);
180 my @dep_includes = ();
181 my @dep_functions = ();
182 my $removal = "Documentation/feature-removal-schedule.txt";
183 if ($tree && -f "$root/$removal") {
184 open(REMOVE, "<$root/$removal") ||
185 die "$P: $removal: open failed - $!\n";
186 while (<REMOVE>) {
187 if (/^Check:\s+(.*\S)/) {
188 for my $entry (split(/[, ]+/, $1)) {
189 if ($entry =~ m@include/(.*)@) {
190 push(@dep_includes, $1);
192 } elsif ($entry !~ m@/@) {
193 push(@dep_functions, $entry);
200 my @rawlines = ();
201 my @lines = ();
202 my $vname;
203 for my $filename (@ARGV) {
204 if ($file) {
205 open(FILE, "diff -u /dev/null $filename|") ||
206 die "$P: $filename: diff failed - $!\n";
207 } else {
208 open(FILE, "<$filename") ||
209 die "$P: $filename: open failed - $!\n";
211 if ($filename eq '-') {
212 $vname = 'Your patch';
213 } else {
214 $vname = $filename;
216 while (<FILE>) {
217 chomp;
218 push(@rawlines, $_);
220 close(FILE);
221 if (!process($filename)) {
222 $exit = 1;
224 @rawlines = ();
225 @lines = ();
228 exit($exit);
230 sub top_of_kernel_tree {
231 my ($root) = @_;
233 my @tree_check = (
234 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
235 "README", "Documentation", "arch", "include", "drivers",
236 "fs", "init", "ipc", "kernel", "lib", "scripts",
239 foreach my $check (@tree_check) {
240 if (! -e $root . '/' . $check) {
241 return 0;
244 return 1;
247 sub expand_tabs {
248 my ($str) = @_;
250 my $res = '';
251 my $n = 0;
252 for my $c (split(//, $str)) {
253 if ($c eq "\t") {
254 $res .= ' ';
255 $n++;
256 for (; ($n % 8) != 0; $n++) {
257 $res .= ' ';
259 next;
261 $res .= $c;
262 $n++;
265 return $res;
267 sub copy_spacing {
268 (my $res = shift) =~ tr/\t/ /c;
269 return $res;
272 sub line_stats {
273 my ($line) = @_;
275 # Drop the diff line leader and expand tabs
276 $line =~ s/^.//;
277 $line = expand_tabs($line);
279 # Pick the indent from the front of the line.
280 my ($white) = ($line =~ /^(\s*)/);
282 return (length($line), length($white));
285 my $sanitise_quote = '';
287 sub sanitise_line_reset {
288 my ($in_comment) = @_;
290 if ($in_comment) {
291 $sanitise_quote = '*/';
292 } else {
293 $sanitise_quote = '';
296 sub sanitise_line {
297 my ($line) = @_;
299 my $res = '';
300 my $l = '';
302 my $qlen = 0;
303 my $off = 0;
304 my $c;
306 # Always copy over the diff marker.
307 $res = substr($line, 0, 1);
309 for ($off = 1; $off < length($line); $off++) {
310 $c = substr($line, $off, 1);
312 # Comments we are wacking completly including the begin
313 # and end, all to $;.
314 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
315 $sanitise_quote = '*/';
317 substr($res, $off, 2, "$;$;");
318 $off++;
319 next;
321 if (substr($line, $off, 2) eq $sanitise_quote) {
322 $sanitise_quote = '';
323 substr($res, $off, 2, "$;$;");
324 $off++;
325 next;
328 # A \ in a string means ignore the next character.
329 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
330 $c eq "\\") {
331 substr($res, $off, 2, 'XX');
332 $off++;
333 next;
335 # Regular quotes.
336 if ($c eq "'" || $c eq '"') {
337 if ($sanitise_quote eq '') {
338 $sanitise_quote = $c;
340 substr($res, $off, 1, $c);
341 next;
342 } elsif ($sanitise_quote eq $c) {
343 $sanitise_quote = '';
347 #print "SQ:$sanitise_quote\n";
348 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
349 substr($res, $off, 1, $;);
350 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
351 substr($res, $off, 1, 'X');
352 } else {
353 substr($res, $off, 1, $c);
357 # The pathname on a #include may be surrounded by '<' and '>'.
358 if ($res =~ /^.#\s*include\s+\<(.*)\>/) {
359 my $clean = 'X' x length($1);
360 $res =~ s@\<.*\>@<$clean>@;
362 # The whole of a #error is a string.
363 } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) {
364 my $clean = 'X' x length($1);
365 $res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@;
368 return $res;
371 sub ctx_statement_block {
372 my ($linenr, $remain, $off) = @_;
373 my $line = $linenr - 1;
374 my $blk = '';
375 my $soff = $off;
376 my $coff = $off - 1;
377 my $coff_set = 0;
379 my $loff = 0;
381 my $type = '';
382 my $level = 0;
383 my $p;
384 my $c;
385 my $len = 0;
387 my $remainder;
388 while (1) {
389 #warn "CSB: blk<$blk> remain<$remain>\n";
390 # If we are about to drop off the end, pull in more
391 # context.
392 if ($off >= $len) {
393 for (; $remain > 0; $line++) {
394 next if ($lines[$line] =~ /^-/);
395 $remain--;
396 $loff = $len;
397 $blk .= $lines[$line] . "\n";
398 $len = length($blk);
399 $line++;
400 last;
402 # Bail if there is no further context.
403 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
404 if ($off >= $len) {
405 last;
408 $p = $c;
409 $c = substr($blk, $off, 1);
410 $remainder = substr($blk, $off);
412 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
413 # Statement ends at the ';' or a close '}' at the
414 # outermost level.
415 if ($level == 0 && $c eq ';') {
416 last;
419 # An else is really a conditional as long as its not else if
420 if ($level == 0 && $coff_set == 0 &&
421 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
422 $remainder =~ /^(else)(?:\s|{)/ &&
423 $remainder !~ /^else\s+if\b/) {
424 $coff = $off + length($1) - 1;
425 $coff_set = 1;
426 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
427 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
430 if (($type eq '' || $type eq '(') && $c eq '(') {
431 $level++;
432 $type = '(';
434 if ($type eq '(' && $c eq ')') {
435 $level--;
436 $type = ($level != 0)? '(' : '';
438 if ($level == 0 && $coff < $soff) {
439 $coff = $off;
440 $coff_set = 1;
441 #warn "CSB: mark coff<$coff>\n";
444 if (($type eq '' || $type eq '{') && $c eq '{') {
445 $level++;
446 $type = '{';
448 if ($type eq '{' && $c eq '}') {
449 $level--;
450 $type = ($level != 0)? '{' : '';
452 if ($level == 0) {
453 last;
456 $off++;
458 if ($off == $len) {
459 $line++;
460 $remain--;
463 my $statement = substr($blk, $soff, $off - $soff + 1);
464 my $condition = substr($blk, $soff, $coff - $soff + 1);
466 #warn "STATEMENT<$statement>\n";
467 #warn "CONDITION<$condition>\n";
469 #print "coff<$coff> soff<$off> loff<$loff>\n";
471 return ($statement, $condition,
472 $line, $remain + 1, $off - $loff + 1, $level);
475 sub statement_lines {
476 my ($stmt) = @_;
478 # Strip the diff line prefixes and rip blank lines at start and end.
479 $stmt =~ s/(^|\n)./$1/g;
480 $stmt =~ s/^\s*//;
481 $stmt =~ s/\s*$//;
483 my @stmt_lines = ($stmt =~ /\n/g);
485 return $#stmt_lines + 2;
488 sub statement_rawlines {
489 my ($stmt) = @_;
491 my @stmt_lines = ($stmt =~ /\n/g);
493 return $#stmt_lines + 2;
496 sub statement_block_size {
497 my ($stmt) = @_;
499 $stmt =~ s/(^|\n)./$1/g;
500 $stmt =~ s/^\s*{//;
501 $stmt =~ s/}\s*$//;
502 $stmt =~ s/^\s*//;
503 $stmt =~ s/\s*$//;
505 my @stmt_lines = ($stmt =~ /\n/g);
506 my @stmt_statements = ($stmt =~ /;/g);
508 my $stmt_lines = $#stmt_lines + 2;
509 my $stmt_statements = $#stmt_statements + 1;
511 if ($stmt_lines > $stmt_statements) {
512 return $stmt_lines;
513 } else {
514 return $stmt_statements;
518 sub ctx_statement_full {
519 my ($linenr, $remain, $off) = @_;
520 my ($statement, $condition, $level);
522 my (@chunks);
524 # Grab the first conditional/block pair.
525 ($statement, $condition, $linenr, $remain, $off, $level) =
526 ctx_statement_block($linenr, $remain, $off);
527 #print "F: c<$condition> s<$statement> remain<$remain>\n";
528 push(@chunks, [ $condition, $statement ]);
529 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
530 return ($level, $linenr, @chunks);
533 # Pull in the following conditional/block pairs and see if they
534 # could continue the statement.
535 for (;;) {
536 ($statement, $condition, $linenr, $remain, $off, $level) =
537 ctx_statement_block($linenr, $remain, $off);
538 #print "C: c<$condition> s<$statement> remain<$remain>\n";
539 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
540 #print "C: push\n";
541 push(@chunks, [ $condition, $statement ]);
544 return ($level, $linenr, @chunks);
547 sub ctx_block_get {
548 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
549 my $line;
550 my $start = $linenr - 1;
551 my $blk = '';
552 my @o;
553 my @c;
554 my @res = ();
556 my $level = 0;
557 for ($line = $start; $remain > 0; $line++) {
558 next if ($rawlines[$line] =~ /^-/);
559 $remain--;
561 $blk .= $rawlines[$line];
562 foreach my $c (split(//, $rawlines[$line])) {
563 ##print "C<$c>L<$level><$open$close>O<$off>\n";
564 if ($off > 0) {
565 $off--;
566 next;
569 if ($c eq $close && $level > 0) {
570 $level--;
571 last if ($level == 0);
572 } elsif ($c eq $open) {
573 $level++;
577 if (!$outer || $level <= 1) {
578 push(@res, $rawlines[$line]);
581 last if ($level == 0);
584 return ($level, @res);
586 sub ctx_block_outer {
587 my ($linenr, $remain) = @_;
589 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
590 return @r;
592 sub ctx_block {
593 my ($linenr, $remain) = @_;
595 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
596 return @r;
598 sub ctx_statement {
599 my ($linenr, $remain, $off) = @_;
601 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
602 return @r;
604 sub ctx_block_level {
605 my ($linenr, $remain) = @_;
607 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
609 sub ctx_statement_level {
610 my ($linenr, $remain, $off) = @_;
612 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
615 sub ctx_locate_comment {
616 my ($first_line, $end_line) = @_;
618 # Catch a comment on the end of the line itself.
619 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
620 return $current_comment if (defined $current_comment);
622 # Look through the context and try and figure out if there is a
623 # comment.
624 my $in_comment = 0;
625 $current_comment = '';
626 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
627 my $line = $rawlines[$linenr - 1];
628 #warn " $line\n";
629 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
630 $in_comment = 1;
632 if ($line =~ m@/\*@) {
633 $in_comment = 1;
635 if (!$in_comment && $current_comment ne '') {
636 $current_comment = '';
638 $current_comment .= $line . "\n" if ($in_comment);
639 if ($line =~ m@\*/@) {
640 $in_comment = 0;
644 chomp($current_comment);
645 return($current_comment);
647 sub ctx_has_comment {
648 my ($first_line, $end_line) = @_;
649 my $cmt = ctx_locate_comment($first_line, $end_line);
651 ##print "LINE: $rawlines[$end_line - 1 ]\n";
652 ##print "CMMT: $cmt\n";
654 return ($cmt ne '');
657 sub cat_vet {
658 my ($vet) = @_;
659 my ($res, $coded);
661 $res = '';
662 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
663 $res .= $1;
664 if ($2 ne '') {
665 $coded = sprintf("^%c", unpack('C', $2) + 64);
666 $res .= $coded;
669 $res =~ s/$/\$/;
671 return $res;
674 my $av_preprocessor = 0;
675 my $av_pending;
676 my @av_paren_type;
678 sub annotate_reset {
679 $av_preprocessor = 0;
680 $av_pending = '_';
681 @av_paren_type = ('E');
684 sub annotate_values {
685 my ($stream, $type) = @_;
687 my $res;
688 my $cur = $stream;
690 print "$stream\n" if ($dbg_values > 1);
692 while (length($cur)) {
693 @av_paren_type = ('E') if ($#av_paren_type < 0);
694 print " <" . join('', @av_paren_type) .
695 "> <$type> " if ($dbg_values > 1);
696 if ($cur =~ /^(\s+)/o) {
697 print "WS($1)\n" if ($dbg_values > 1);
698 if ($1 =~ /\n/ && $av_preprocessor) {
699 $type = pop(@av_paren_type);
700 $av_preprocessor = 0;
703 } elsif ($cur =~ /^($Type)/) {
704 print "DECLARE($1)\n" if ($dbg_values > 1);
705 $type = 'T';
707 } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
708 print "DEFINE($1)\n" if ($dbg_values > 1);
709 $av_preprocessor = 1;
710 $av_pending = 'N';
712 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if))/o) {
713 print "PRE_START($1)\n" if ($dbg_values > 1);
714 $av_preprocessor = 1;
716 push(@av_paren_type, $type);
717 push(@av_paren_type, $type);
718 $type = 'N';
720 } elsif ($cur =~ /^(#\s*(?:else|elif))/o) {
721 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
722 $av_preprocessor = 1;
724 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
726 $type = 'N';
728 } elsif ($cur =~ /^(#\s*(?:endif))/o) {
729 print "PRE_END($1)\n" if ($dbg_values > 1);
731 $av_preprocessor = 1;
733 # Assume all arms of the conditional end as this
734 # one does, and continue as if the #endif was not here.
735 pop(@av_paren_type);
736 push(@av_paren_type, $type);
737 $type = 'N';
739 } elsif ($cur =~ /^(\\\n)/o) {
740 print "PRECONT($1)\n" if ($dbg_values > 1);
742 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
743 print "SIZEOF($1)\n" if ($dbg_values > 1);
744 if (defined $2) {
745 $av_pending = 'V';
747 $type = 'N';
749 } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
750 print "COND($1)\n" if ($dbg_values > 1);
751 $av_pending = 'N';
752 $type = 'N';
754 } elsif ($cur =~/^(return|case|else)/o) {
755 print "KEYWORD($1)\n" if ($dbg_values > 1);
756 $type = 'N';
758 } elsif ($cur =~ /^(\()/o) {
759 print "PAREN('$1')\n" if ($dbg_values > 1);
760 push(@av_paren_type, $av_pending);
761 $av_pending = '_';
762 $type = 'N';
764 } elsif ($cur =~ /^(\))/o) {
765 my $new_type = pop(@av_paren_type);
766 if ($new_type ne '_') {
767 $type = $new_type;
768 print "PAREN('$1') -> $type\n"
769 if ($dbg_values > 1);
770 } else {
771 print "PAREN('$1')\n" if ($dbg_values > 1);
774 } elsif ($cur =~ /^($Ident)\(/o) {
775 print "FUNC($1)\n" if ($dbg_values > 1);
776 $av_pending = 'V';
778 } elsif ($cur =~ /^($Ident|$Constant)/o) {
779 print "IDENT($1)\n" if ($dbg_values > 1);
780 $type = 'V';
782 } elsif ($cur =~ /^($Assignment)/o) {
783 print "ASSIGN($1)\n" if ($dbg_values > 1);
784 $type = 'N';
786 } elsif ($cur =~/^(;|{|})/) {
787 print "END($1)\n" if ($dbg_values > 1);
788 $type = 'E';
790 } elsif ($cur =~ /^(;|\?|:|\[)/o) {
791 print "CLOSE($1)\n" if ($dbg_values > 1);
792 $type = 'N';
794 } elsif ($cur =~ /^($Operators)/o) {
795 print "OP($1)\n" if ($dbg_values > 1);
796 if ($1 ne '++' && $1 ne '--') {
797 $type = 'N';
800 } elsif ($cur =~ /(^.)/o) {
801 print "C($1)\n" if ($dbg_values > 1);
803 if (defined $1) {
804 $cur = substr($cur, length($1));
805 $res .= $type x length($1);
809 return $res;
812 sub possible {
813 my ($possible, $line) = @_;
815 #print "CHECK<$possible>\n";
816 if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
817 $possible ne 'goto' && $possible ne 'return' &&
818 $possible ne 'struct' && $possible ne 'enum' &&
819 $possible ne 'case' && $possible ne 'else' &&
820 $possible ne 'typedef') {
821 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
822 push(@typeList, $possible);
823 build_types();
827 my $prefix = '';
829 sub report {
830 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
831 return 0;
833 my $line = $prefix . $_[0];
835 $line = (split('\n', $line))[0] . "\n" if ($terse);
837 push(our @report, $line);
839 return 1;
841 sub report_dump {
842 our @report;
844 sub ERROR {
845 if (report("ERROR: $_[0]\n")) {
846 our $clean = 0;
847 our $cnt_error++;
850 sub WARN {
851 if (report("WARNING: $_[0]\n")) {
852 our $clean = 0;
853 our $cnt_warn++;
856 sub CHK {
857 if ($check && report("CHECK: $_[0]\n")) {
858 our $clean = 0;
859 our $cnt_chk++;
863 sub process {
864 my $filename = shift;
866 my $linenr=0;
867 my $prevline="";
868 my $prevrawline="";
869 my $stashline="";
870 my $stashrawline="";
872 my $length;
873 my $indent;
874 my $previndent=0;
875 my $stashindent=0;
877 our $clean = 1;
878 my $signoff = 0;
879 my $is_patch = 0;
881 our @report = ();
882 our $cnt_lines = 0;
883 our $cnt_error = 0;
884 our $cnt_warn = 0;
885 our $cnt_chk = 0;
887 # Trace the real file/line as we go.
888 my $realfile = '';
889 my $realline = 0;
890 my $realcnt = 0;
891 my $here = '';
892 my $in_comment = 0;
893 my $comment_edge = 0;
894 my $first_line = 0;
896 my $prev_values = 'E';
898 # suppression flags
899 my %suppress_ifbraces;
901 # Pre-scan the patch sanitizing the lines.
902 # Pre-scan the patch looking for any __setup documentation.
904 my @setup_docs = ();
905 my $setup_docs = 0;
907 sanitise_line_reset();
908 my $line;
909 foreach my $rawline (@rawlines) {
910 $linenr++;
911 $line = $rawline;
913 if ($rawline=~/^\+\+\+\s+(\S+)/) {
914 $setup_docs = 0;
915 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
916 $setup_docs = 1;
918 #next;
920 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
921 $realline=$1-1;
922 if (defined $2) {
923 $realcnt=$3+1;
924 } else {
925 $realcnt=1+1;
928 # Guestimate if this is a continuing comment. Run
929 # the context looking for a comment "edge". If this
930 # edge is a close comment then we must be in a comment
931 # at context start.
932 my $edge;
933 for (my $ln = $linenr; $ln < ($linenr + $realcnt); $ln++) {
934 next if ($line =~ /^-/);
935 ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
936 last if (defined $edge);
938 if (defined $edge && $edge eq '*/') {
939 $in_comment = 1;
942 # Guestimate if this is a continuing comment. If this
943 # is the start of a diff block and this line starts
944 # ' *' then it is very likely a comment.
945 if (!defined $edge &&
946 $rawlines[$linenr] =~ m@^.\s* \*(?:\s|$)@)
948 $in_comment = 1;
951 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
952 sanitise_line_reset($in_comment);
954 } elsif ($realcnt) {
955 # Standardise the strings and chars within the input to
956 # simplify matching.
957 $line = sanitise_line($rawline);
959 push(@lines, $line);
961 if ($realcnt > 1) {
962 $realcnt-- if ($line =~ /^(?:\+| |$)/);
963 } else {
964 $realcnt = 0;
967 #print "==>$rawline\n";
968 #print "-->$line\n";
970 if ($setup_docs && $line =~ /^\+/) {
971 push(@setup_docs, $line);
975 $prefix = '';
977 $realcnt = 0;
978 $linenr = 0;
979 foreach my $line (@lines) {
980 $linenr++;
982 my $rawline = $rawlines[$linenr - 1];
984 #extract the line range in the file after the patch is applied
985 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
986 $is_patch = 1;
987 $first_line = $linenr + 1;
988 $realline=$1-1;
989 if (defined $2) {
990 $realcnt=$3+1;
991 } else {
992 $realcnt=1+1;
994 annotate_reset();
995 $prev_values = 'E';
997 %suppress_ifbraces = ();
998 next;
1000 # track the line number as we move through the hunk, note that
1001 # new versions of GNU diff omit the leading space on completely
1002 # blank context lines so we need to count that too.
1003 } elsif ($line =~ /^( |\+|$)/) {
1004 $realline++;
1005 $realcnt-- if ($realcnt != 0);
1007 # Measure the line length and indent.
1008 ($length, $indent) = line_stats($rawline);
1010 # Track the previous line.
1011 ($prevline, $stashline) = ($stashline, $line);
1012 ($previndent, $stashindent) = ($stashindent, $indent);
1013 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1015 #warn "line<$line>\n";
1017 } elsif ($realcnt == 1) {
1018 $realcnt--;
1021 #make up the handle for any error we report on this line
1022 $prefix = "$filename:$realline: " if ($emacs && $file);
1023 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1025 $here = "#$linenr: " if (!$file);
1026 $here = "#$realline: " if ($file);
1028 # extract the filename as it passes
1029 if ($line=~/^\+\+\+\s+(\S+)/) {
1030 $realfile = $1;
1031 $realfile =~ s@^[^/]*/@@;
1033 if ($realfile =~ m@include/asm/@) {
1034 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1036 next;
1039 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1041 my $hereline = "$here\n$rawline\n";
1042 my $herecurr = "$here\n$rawline\n";
1043 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1045 $cnt_lines++ if ($realcnt != 0);
1047 #check the patch for a signoff:
1048 if ($line =~ /^\s*signed-off-by:/i) {
1049 # This is a signoff, if ugly, so do not double report.
1050 $signoff++;
1051 if (!($line =~ /^\s*Signed-off-by:/)) {
1052 WARN("Signed-off-by: is the preferred form\n" .
1053 $herecurr);
1055 if ($line =~ /^\s*signed-off-by:\S/i) {
1056 WARN("space required after Signed-off-by:\n" .
1057 $herecurr);
1061 # Check for wrappage within a valid hunk of the file
1062 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1063 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1064 $herecurr) if (!$emitted_corrupt++);
1067 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1068 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1069 !($rawline =~ m/^(
1070 [\x09\x0A\x0D\x20-\x7E] # ASCII
1071 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
1072 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
1073 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
1074 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
1075 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
1076 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
1077 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
1078 )*$/x )) {
1079 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $herecurr);
1082 #ignore lines being removed
1083 if ($line=~/^-/) {next;}
1085 # check we are in a valid source file if not then ignore this hunk
1086 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1088 #trailing whitespace
1089 if ($line =~ /^\+.*\015/) {
1090 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1091 ERROR("DOS line endings\n" . $herevet);
1093 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1094 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1095 ERROR("trailing whitespace\n" . $herevet);
1097 #80 column limit
1098 if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) {
1099 WARN("line over 80 characters\n" . $herecurr);
1102 # check for adding lines without a newline.
1103 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1104 WARN("adding a line without newline at end of file\n" . $herecurr);
1107 # check we are in a valid source file *.[hc] if not then ignore this hunk
1108 next if ($realfile !~ /\.[hc]$/);
1110 # at the beginning of a line any tabs must come first and anything
1111 # more than 8 must use tabs.
1112 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1113 $rawline =~ /^\+\s* \s*/) {
1114 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1115 ERROR("use tabs not spaces\n" . $herevet);
1118 # check for RCS/CVS revision markers
1119 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1120 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1123 # Check for potential 'bare' types
1124 if ($realcnt) {
1125 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1126 $s =~ s/\n./ /g;
1127 $s =~ s/{.*$//;
1129 # Ignore goto labels.
1130 if ($s =~ /$Ident:\*$/) {
1132 # Ignore functions being called
1133 } elsif ($s =~ /^.\s*$Ident\s*\(/) {
1135 # definitions in global scope can only start with types
1136 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) {
1137 possible($1, $s);
1139 # declarations always start with types
1140 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/) {
1141 possible($1, $s);
1144 # any (foo ... *) is a pointer cast, and foo is a type
1145 while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) {
1146 possible($1, $s);
1149 # Check for any sort of function declaration.
1150 # int foo(something bar, other baz);
1151 # void (*store_gdt)(x86_descr_ptr *);
1152 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) {
1153 my ($name_len) = length($1);
1155 my $ctx = $s;
1156 substr($ctx, 0, $name_len + 1, '');
1157 $ctx =~ s/\)[^\)]*$//;
1159 for my $arg (split(/\s*,\s*/, $ctx)) {
1160 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
1162 possible($1, $s);
1170 # Checks which may be anchored in the context.
1173 # Check for switch () and associated case and default
1174 # statements should be at the same indent.
1175 if ($line=~/\bswitch\s*\(.*\)/) {
1176 my $err = '';
1177 my $sep = '';
1178 my @ctx = ctx_block_outer($linenr, $realcnt);
1179 shift(@ctx);
1180 for my $ctx (@ctx) {
1181 my ($clen, $cindent) = line_stats($ctx);
1182 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1183 $indent != $cindent) {
1184 $err .= "$sep$ctx\n";
1185 $sep = '';
1186 } else {
1187 $sep = "[...]\n";
1190 if ($err ne '') {
1191 ERROR("switch and case should be at the same indent\n$hereline$err");
1195 # if/while/etc brace do not go on next line, unless defining a do while loop,
1196 # or if that brace on the next line is for something else
1197 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
1198 my $pre_ctx = "$1$2";
1200 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1201 my $ctx_ln = $linenr + $#ctx + 1;
1202 my $ctx_cnt = $realcnt - $#ctx - 1;
1203 my $ctx = join("\n", @ctx);
1205 ##warn "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1207 # Skip over any removed lines in the context following statement.
1208 while (defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^-/) {
1209 $ctx_ln++;
1211 ##warn "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1213 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1214 ERROR("that open brace { should be on the previous line\n" .
1215 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1217 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1218 $ctx =~ /\)\s*\;\s*$/ &&
1219 defined $lines[$ctx_ln - 1])
1221 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1222 if ($nindent > $indent) {
1223 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1224 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1229 # Track the 'values' across context and added lines.
1230 my $opline = $line; $opline =~ s/^./ /;
1231 my $curr_values = annotate_values($opline . "\n", $prev_values);
1232 $curr_values = $prev_values . $curr_values;
1233 if ($dbg_values) {
1234 my $outline = $opline; $outline =~ s/\t/ /g;
1235 print "$linenr > .$outline\n";
1236 print "$linenr > $curr_values\n";
1238 $prev_values = substr($curr_values, -1);
1240 #ignore lines not being added
1241 if ($line=~/^[^\+]/) {next;}
1243 # TEST: allow direct testing of the type matcher.
1244 if ($tst_type && $line =~ /^.$Declare$/) {
1245 ERROR("TEST: is type $Declare\n" . $herecurr);
1246 next;
1249 # check for initialisation to aggregates open brace on the next line
1250 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1251 $line =~ /^.\s*{/) {
1252 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1256 # Checks which are anchored on the added line.
1259 # check for malformed paths in #include statements (uses RAW line)
1260 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
1261 my $path = $1;
1262 if ($path =~ m{//}) {
1263 ERROR("malformed #include filename\n" .
1264 $herecurr);
1268 # no C99 // comments
1269 if ($line =~ m{//}) {
1270 ERROR("do not use C99 // comments\n" . $herecurr);
1272 # Remove C99 comments.
1273 $line =~ s@//.*@@;
1274 $opline =~ s@//.*@@;
1276 #EXPORT_SYMBOL should immediately follow its function closing }.
1277 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1278 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1279 my $name = $1;
1280 if (($prevline !~ /^}/) &&
1281 ($prevline !~ /^\+}/) &&
1282 ($prevline !~ /^ }/) &&
1283 ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1284 ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
1285 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
1286 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1290 # check for external initialisers.
1291 if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
1292 ERROR("do not initialise externals to 0 or NULL\n" .
1293 $herecurr);
1295 # check for static initialisers.
1296 if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
1297 ERROR("do not initialise statics to 0 or NULL\n" .
1298 $herecurr);
1301 # check for new typedefs, only function parameters and sparse annotations
1302 # make sense.
1303 if ($line =~ /\btypedef\s/ &&
1304 $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1305 $line !~ /\b__bitwise(?:__|)\b/) {
1306 WARN("do not add new typedefs\n" . $herecurr);
1309 # * goes on variable not on type
1310 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
1311 ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1312 $herecurr);
1314 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
1315 ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1316 $herecurr);
1318 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
1319 ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1320 $herecurr);
1322 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
1323 ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1324 $herecurr);
1327 # # no BUG() or BUG_ON()
1328 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1329 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1330 # print "$herecurr";
1331 # $clean = 0;
1334 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1335 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1338 # printk should use KERN_* levels. Note that follow on printk's on the
1339 # same line do not need a level, so we use the current block context
1340 # to try and find and validate the current printk. In summary the current
1341 # printk includes all preceeding printk's which have no newline on the end.
1342 # we assume the first bad printk is the one to report.
1343 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1344 my $ok = 0;
1345 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1346 #print "CHECK<$lines[$ln - 1]\n";
1347 # we have a preceeding printk if it ends
1348 # with "\n" ignore it, else it is to blame
1349 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1350 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1351 $ok = 1;
1353 last;
1356 if ($ok == 0) {
1357 WARN("printk() should include KERN_ facility level\n" . $herecurr);
1361 # function brace can't be on same line, except for #defines of do while,
1362 # or if closed on same line
1363 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and
1364 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
1365 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1368 # open braces for enum, union and struct go on the same line.
1369 if ($line =~ /^.\s*{/ &&
1370 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1371 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1374 # check for spaces between functions and their parentheses.
1375 while ($line =~ /($Ident)\s+\(/g) {
1376 my $name = $1;
1377 my $ctx_before = substr($line, 0, $-[1]);
1378 my $ctx = "$ctx_before$name";
1380 # Ignore those directives where spaces _are_ permitted.
1381 if ($name =~ /^(?:
1382 if|for|while|switch|return|case|
1383 volatile|__volatile__|
1384 __attribute__|format|__extension__|
1385 asm|__asm__)$/x)
1388 # cpp #define statements have non-optional spaces, ie
1389 # if there is a space between the name and the open
1390 # parenthesis it is simply not a parameter group.
1391 } elsif ($ctx_before =~ /^.\#\s*define\s*$/) {
1393 # cpp #elif statement condition may start with a (
1394 } elsif ($ctx =~ /^.\#\s*elif\s*$/) {
1396 # If this whole things ends with a type its most
1397 # likely a typedef for a function.
1398 } elsif ($ctx =~ /$Type$/) {
1400 } else {
1401 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1404 # Check operator spacing.
1405 if (!($line=~/\#\s*include/)) {
1406 my $ops = qr{
1407 <<=|>>=|<=|>=|==|!=|
1408 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1409 =>|->|<<|>>|<|>|=|!|~|
1410 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1412 my @elements = split(/($ops|;)/, $opline);
1413 my $off = 0;
1415 my $blank = copy_spacing($opline);
1417 for (my $n = 0; $n < $#elements; $n += 2) {
1418 $off += length($elements[$n]);
1420 # Pick up the preceeding and succeeding characters.
1421 my $ca = substr($opline, 0, $off);
1422 my $cc = '';
1423 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1424 $cc = substr($opline, $off + length($elements[$n + 1]));
1426 my $cb = "$ca$;$cc";
1428 my $a = '';
1429 $a = 'V' if ($elements[$n] ne '');
1430 $a = 'W' if ($elements[$n] =~ /\s$/);
1431 $a = 'C' if ($elements[$n] =~ /$;$/);
1432 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1433 $a = 'O' if ($elements[$n] eq '');
1434 $a = 'E' if ($ca =~ /^\s*$/);
1436 my $op = $elements[$n + 1];
1438 my $c = '';
1439 if (defined $elements[$n + 2]) {
1440 $c = 'V' if ($elements[$n + 2] ne '');
1441 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1442 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1443 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1444 $c = 'O' if ($elements[$n + 2] eq '');
1445 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
1446 } else {
1447 $c = 'E';
1450 my $ctx = "${a}x${c}";
1452 my $at = "(ctx:$ctx)";
1454 my $ptr = substr($blank, 0, $off) . "^";
1455 my $hereptr = "$hereline$ptr\n";
1457 # Classify operators into binary, unary, or
1458 # definitions (* only) where they have more
1459 # than one mode.
1460 my $op_type = substr($curr_values, $off + 1, 1);
1461 my $op_left = substr($curr_values, $off, 1);
1462 my $is_unary;
1463 if ($op_type eq 'T') {
1464 $is_unary = 2;
1465 } elsif ($op_left eq 'V') {
1466 $is_unary = 0;
1467 } else {
1468 $is_unary = 1;
1470 #if ($op eq '-' || $op eq '&' || $op eq '*') {
1471 # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
1474 # Ignore operators passed as parameters.
1475 if ($op_type ne 'V' &&
1476 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1478 # # Ignore comments
1479 # } elsif ($op =~ /^$;+$/) {
1481 # ; should have either the end of line or a space or \ after it
1482 } elsif ($op eq ';') {
1483 if ($ctx !~ /.x[WEBC]/ &&
1484 $cc !~ /^\\/ && $cc !~ /^;/) {
1485 ERROR("space required after that '$op' $at\n" . $hereptr);
1488 # // is a comment
1489 } elsif ($op eq '//') {
1491 # -> should have no spaces
1492 } elsif ($op eq '->') {
1493 if ($ctx =~ /Wx.|.xW/) {
1494 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
1497 # , must have a space on the right.
1498 } elsif ($op eq ',') {
1499 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1500 ERROR("space required after that '$op' $at\n" . $hereptr);
1503 # '*' as part of a type definition -- reported already.
1504 } elsif ($op eq '*' && $is_unary == 2) {
1505 #warn "'*' is part of type\n";
1507 # unary operators should have a space before and
1508 # none after. May be left adjacent to another
1509 # unary operator, or a cast
1510 } elsif ($op eq '!' || $op eq '~' ||
1511 ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
1512 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1513 ERROR("space required before that '$op' $at\n" . $hereptr);
1515 if ($ctx =~ /.xW/) {
1516 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1519 # unary ++ and unary -- are allowed no space on one side.
1520 } elsif ($op eq '++' or $op eq '--') {
1521 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1522 ERROR("space required one side of that '$op' $at\n" . $hereptr);
1524 if ($ctx =~ /Wx[BE]/ ||
1525 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1526 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1528 if ($ctx =~ /ExW/) {
1529 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1533 # << and >> may either have or not have spaces both sides
1534 } elsif ($op eq '<<' or $op eq '>>' or
1535 $op eq '&' or $op eq '^' or $op eq '|' or
1536 $op eq '+' or $op eq '-' or
1537 $op eq '*' or $op eq '/' or
1538 $op eq '%')
1540 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1541 ERROR("need consistent spacing around '$op' $at\n" .
1542 $hereptr);
1545 # All the others need spaces both sides.
1546 } elsif ($ctx !~ /[EWC]x[CWE]/) {
1547 # Ignore email addresses <foo@bar>
1548 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
1549 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
1550 ERROR("spaces required around that '$op' $at\n" . $hereptr);
1553 $off += length($elements[$n + 1]);
1557 # check for multiple assignments
1558 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
1559 CHK("multiple assignments should be avoided\n" . $herecurr);
1562 ## # check for multiple declarations, allowing for a function declaration
1563 ## # continuation.
1564 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1565 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1567 ## # Remove any bracketed sections to ensure we do not
1568 ## # falsly report the parameters of functions.
1569 ## my $ln = $line;
1570 ## while ($ln =~ s/\([^\(\)]*\)//g) {
1571 ## }
1572 ## if ($ln =~ /,/) {
1573 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1574 ## }
1575 ## }
1577 #need space before brace following if, while, etc
1578 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1579 $line =~ /do{/) {
1580 ERROR("space required before the open brace '{'\n" . $herecurr);
1583 # closing brace should have a space following it when it has anything
1584 # on the line
1585 if ($line =~ /}(?!(?:,|;|\)))\S/) {
1586 ERROR("space required after that close brace '}'\n" . $herecurr);
1589 # check spacing on square brackets
1590 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1591 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
1593 if ($line =~ /\s\]/) {
1594 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
1597 # check spacing on paretheses
1598 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1599 $line !~ /for\s*\(\s+;/) {
1600 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
1602 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1603 $line !~ /for\s*\(.*;\s+\)/) {
1604 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
1607 #goto labels aren't indented, allow a single space however
1608 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
1609 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1610 WARN("labels should not be indented\n" . $herecurr);
1613 # Need a space before open parenthesis after if, while etc
1614 if ($line=~/\b(if|while|for|switch)\(/) {
1615 ERROR("space required before the open parenthesis '('\n" . $herecurr);
1618 # Check for illegal assignment in if conditional.
1619 if ($line =~ /\bif\s*\(/) {
1620 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1622 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
1623 ERROR("do not use assignment in if condition\n" . $herecurr);
1626 # Find out what is on the end of the line after the
1627 # conditional.
1628 substr($s, 0, length($c), '');
1629 $s =~ s/\n.*//g;
1630 $s =~ s/$;//g; # Remove any comments
1631 if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/ &&
1632 $c !~ /^.\#\s*if/)
1634 ERROR("trailing statements should be on next line\n" . $herecurr);
1638 # Check for bitwise tests written as boolean
1639 if ($line =~ /
1641 (?:\[|\(|\&\&|\|\|)
1642 \s*0[xX][0-9]+\s*
1643 (?:\&\&|\|\|)
1645 (?:\&\&|\|\|)
1646 \s*0[xX][0-9]+\s*
1647 (?:\&\&|\|\||\)|\])
1648 )/x)
1650 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
1653 # if and else should not have general statements after it
1654 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
1655 my $s = $1;
1656 $s =~ s/$;//g; # Remove any comments
1657 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
1658 ERROR("trailing statements should be on next line\n" . $herecurr);
1662 # Check for }<nl>else {, these must be at the same
1663 # indent level to be relevant to each other.
1664 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
1665 $previndent == $indent) {
1666 ERROR("else should follow close brace '}'\n" . $hereprev);
1669 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1670 $previndent == $indent) {
1671 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1673 # Find out what is on the end of the line after the
1674 # conditional.
1675 substr($s, 0, length($c), '');
1676 $s =~ s/\n.*//g;
1678 if ($s =~ /^\s*;/) {
1679 ERROR("while should follow close brace '}'\n" . $hereprev);
1683 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
1684 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
1685 # print "No studly caps, use _\n";
1686 # print "$herecurr";
1687 # $clean = 0;
1690 #no spaces allowed after \ in define
1691 if ($line=~/\#define.*\\\s$/) {
1692 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
1695 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1696 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
1697 my $checkfile = "$root/include/linux/$1.h";
1698 if (-f $checkfile && $1 ne 'irq.h') {
1699 WARN("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
1700 $herecurr);
1704 # multi-statement macros should be enclosed in a do while loop, grab the
1705 # first statement and ensure its the whole macro if its not enclosed
1706 # in a known good container
1707 if ($prevline =~ /\#define.*\\/ &&
1708 $prevline !~/(?:do\s+{|\(\{|\{)/ &&
1709 $line !~ /(?:do\s+{|\(\{|\{)/ &&
1710 $line !~ /^.\s*$Declare\s/) {
1711 # Grab the first statement, if that is the entire macro
1712 # its ok. This may start either on the #define line
1713 # or the one below.
1714 my $ln = $linenr;
1715 my $cnt = $realcnt;
1716 my $off = 0;
1718 # If the macro starts on the define line start
1719 # grabbing the statement after the identifier
1720 $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
1721 ##print "1<$1> 2<$2>\n";
1722 if (defined $2 && $2 ne '') {
1723 $off = length($1);
1724 $ln--;
1725 $cnt++;
1726 while ($lines[$ln - 1] =~ /^-/) {
1727 $ln--;
1728 $cnt++;
1731 my @ctx = ctx_statement($ln, $cnt, $off);
1732 my $ctx_ln = $ln + $#ctx + 1;
1733 my $ctx = join("\n", @ctx);
1735 # Pull in any empty extension lines.
1736 while ($ctx =~ /\\$/ &&
1737 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
1738 $ctx .= $lines[$ctx_ln - 1];
1739 $ctx_ln++;
1742 if ($ctx =~ /\\$/) {
1743 if ($ctx =~ /;/) {
1744 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
1745 } else {
1746 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
1751 # check for redundant bracing round if etc
1752 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
1753 my ($level, $endln, @chunks) =
1754 ctx_statement_full($linenr, $realcnt, 1);
1755 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
1756 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
1757 if ($#chunks > 0 && $level == 0) {
1758 my $allowed = 0;
1759 my $seen = 0;
1760 my $herectx = $here . "\n";
1761 my $ln = $linenr - 1;
1762 for my $chunk (@chunks) {
1763 my ($cond, $block) = @{$chunk};
1765 # If the condition carries leading newlines, then count those as offsets.
1766 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
1767 my $offset = statement_rawlines($whitespace) - 1;
1769 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
1771 # We have looked at and allowed this specific line.
1772 $suppress_ifbraces{$ln + $offset} = 1;
1774 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
1775 $ln += statement_rawlines($block) - 1;
1777 substr($block, 0, length($cond), '');
1779 $seen++ if ($block =~ /^\s*{/);
1781 #print "cond<$cond> block<$block> allowed<$allowed>\n";
1782 if (statement_lines($cond) > 1) {
1783 #print "APW: ALLOWED: cond<$cond>\n";
1784 $allowed = 1;
1786 if ($block =~/\b(?:if|for|while)\b/) {
1787 #print "APW: ALLOWED: block<$block>\n";
1788 $allowed = 1;
1790 if (statement_block_size($block) > 1) {
1791 #print "APW: ALLOWED: lines block<$block>\n";
1792 $allowed = 1;
1795 if ($seen && !$allowed) {
1796 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
1800 if (!defined $suppress_ifbraces{$linenr - 1} &&
1801 $line =~ /\b(if|while|for|else)\b/) {
1802 my $allowed = 0;
1804 # Check the pre-context.
1805 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
1806 #print "APW: ALLOWED: pre<$1>\n";
1807 $allowed = 1;
1810 my ($level, $endln, @chunks) =
1811 ctx_statement_full($linenr, $realcnt, $-[0]);
1813 # Check the condition.
1814 my ($cond, $block) = @{$chunks[0]};
1815 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
1816 if (defined $cond) {
1817 substr($block, 0, length($cond), '');
1819 if (statement_lines($cond) > 1) {
1820 #print "APW: ALLOWED: cond<$cond>\n";
1821 $allowed = 1;
1823 if ($block =~/\b(?:if|for|while)\b/) {
1824 #print "APW: ALLOWED: block<$block>\n";
1825 $allowed = 1;
1827 if (statement_block_size($block) > 1) {
1828 #print "APW: ALLOWED: lines block<$block>\n";
1829 $allowed = 1;
1831 # Check the post-context.
1832 if (defined $chunks[1]) {
1833 my ($cond, $block) = @{$chunks[1]};
1834 if (defined $cond) {
1835 substr($block, 0, length($cond), '');
1837 if ($block =~ /^\s*\{/) {
1838 #print "APW: ALLOWED: chunk-1 block<$block>\n";
1839 $allowed = 1;
1842 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
1843 my $herectx = $here . "\n";;
1844 my $end = $linenr + statement_rawlines($block) - 1;
1846 for (my $ln = $linenr - 1; $ln < $end; $ln++) {
1847 $herectx .= $rawlines[$ln] . "\n";;
1850 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
1854 # don't include deprecated include files (uses RAW line)
1855 for my $inc (@dep_includes) {
1856 if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
1857 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
1861 # don't use deprecated functions
1862 for my $func (@dep_functions) {
1863 if ($line =~ /\b$func\b/) {
1864 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
1868 # no volatiles please
1869 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
1870 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
1871 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
1874 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
1875 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
1876 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
1879 # warn about #if 0
1880 if ($line =~ /^.#\s*if\s+0\b/) {
1881 CHK("if this code is redundant consider removing it\n" .
1882 $herecurr);
1885 # check for needless kfree() checks
1886 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1887 my $expr = $1;
1888 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1889 WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1892 # check for needless usb_free_urb() checks
1893 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1894 my $expr = $1;
1895 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
1896 WARN("usb_free_urb(NULL) is safe this check is probabally not required\n" . $hereprev);
1900 # warn about #ifdefs in C files
1901 # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
1902 # print "#ifdef in C files should be avoided\n";
1903 # print "$herecurr";
1904 # $clean = 0;
1907 # warn about spacing in #ifdefs
1908 if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
1909 ERROR("exactly one space required after that #$1\n" . $herecurr);
1912 # check for spinlock_t definitions without a comment.
1913 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
1914 my $which = $1;
1915 if (!ctx_has_comment($first_line, $linenr)) {
1916 CHK("$1 definition without comment\n" . $herecurr);
1919 # check for memory barriers without a comment.
1920 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
1921 if (!ctx_has_comment($first_line, $linenr)) {
1922 CHK("memory barrier without comment\n" . $herecurr);
1925 # check of hardware specific defines
1926 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
1927 CHK("architecture specific defines should be avoided\n" . $herecurr);
1930 # check the location of the inline attribute, that it is between
1931 # storage class and type.
1932 if ($line =~ /\b$Type\s+$Inline\b/ ||
1933 $line =~ /\b$Inline\s+$Storage\b/) {
1934 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
1937 # Check for __inline__ and __inline, prefer inline
1938 if ($line =~ /\b(__inline__|__inline)\b/) {
1939 WARN("plain inline is preferred over $1\n" . $herecurr);
1942 # check for new externs in .c files.
1943 if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
1944 WARN("externs should be avoided in .c files\n" . $herecurr);
1947 # checks for new __setup's
1948 if ($rawline =~ /\b__setup\("([^"]*)"/) {
1949 my $name = $1;
1951 if (!grep(/$name/, @setup_docs)) {
1952 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
1956 # check for pointless casting of kmalloc return
1957 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
1958 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
1961 # check for gcc specific __FUNCTION__
1962 if ($line =~ /__FUNCTION__/) {
1963 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
1966 # check for semaphores used as mutexes
1967 if ($line =~ /\b(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
1968 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
1970 # check for semaphores used as mutexes
1971 if ($line =~ /\binit_MUTEX_LOCKED\s*\(/) {
1972 WARN("consider using a completion\n" . $herecurr);
1974 # recommend strict_strto* over simple_strto*
1975 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
1976 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
1979 # use of NR_CPUS is usually wrong
1980 # ignore definitions of NR_CPUS and usage to define arrays as likely right
1981 if ($line =~ /\bNR_CPUS\b/ &&
1982 $line !~ /^.#\s*define\s+NR_CPUS\s+/ &&
1983 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/)
1985 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
1989 # If we have no input at all, then there is nothing to report on
1990 # so just keep quiet.
1991 if ($#rawlines == -1) {
1992 exit(0);
1995 # In mailback mode only produce a report in the negative, for
1996 # things that appear to be patches.
1997 if ($mailback && ($clean == 1 || !$is_patch)) {
1998 exit(0);
2001 # This is not a patch, and we are are in 'no-patch' mode so
2002 # just keep quiet.
2003 if (!$chk_patch && !$is_patch) {
2004 exit(0);
2007 if (!$is_patch) {
2008 ERROR("Does not appear to be a unified-diff format patch\n");
2010 if ($is_patch && $chk_signoff && $signoff == 0) {
2011 ERROR("Missing Signed-off-by: line(s)\n");
2014 print report_dump();
2015 if ($summary && !($clean == 1 && $quiet == 1)) {
2016 print "$filename " if ($summary_file);
2017 print "total: $cnt_error errors, $cnt_warn warnings, " .
2018 (($check)? "$cnt_chk checks, " : "") .
2019 "$cnt_lines lines checked\n";
2020 print "\n" if ($quiet == 0);
2023 if ($clean == 1 && $quiet == 0) {
2024 print "$vname has no obvious style problems and is ready for submission.\n"
2026 if ($clean == 0 && $quiet == 0) {
2027 print "$vname has style problems, please review. If any of these errors\n";
2028 print "are false positives report them to the maintainer, see\n";
2029 print "CHECKPATCH in MAINTAINERS.\n";
2032 return $clean;