2 # (c) 2001, Dave Jones. (the file handling bit)
3 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4 # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5 # (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
6 # Licensed under the terms of the GNU GPL License version 2
15 use Getopt::Long qw(:config no_auto_abbrev);
37 Usage: $P [OPTION]... [FILE]...
42 --no-tree run without a kernel tree
43 --no-signoff do not check for 'Signed-off-by' line
44 --patch treat FILE as patchfile (default)
45 --emacs emacs compile window format
46 --terse one line per report
47 -f, --file treat FILE as regular source file
48 --subjective, --strict enable more subjective tests
49 --root=PATH PATH to the kernel tree root
50 --no-summary suppress the per-file summary
51 --mailback only produce a report in case of warnings/errors
52 --summary-file include the filename in summary
53 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
54 'values', 'possible', 'type', and 'attr' (default
56 --test-only=WORD report only warnings/errors containing WORD
58 -h, --help, --version display this help and exit
60 When FILE is - read standard input.
67 'q|quiet+' => \$quiet,
69 'signoff!' => \$chk_signoff,
70 'patch!' => \$chk_patch,
74 'subjective!' => \$check,
77 'summary!' => \$summary,
78 'mailback!' => \$mailback,
79 'summary-file!' => \$summary_file,
82 'test-only=s' => \$tst_only,
92 print "$P: no input files\n";
101 for my $key (keys %debug) {
103 eval "\${dbg_$key} = '$debug{$key}';";
107 my $rpt_cleaners = 0;
116 if (!top_of_kernel_tree($root)) {
117 die "$P: $root: --root does not point at a valid tree\n";
120 if (top_of_kernel_tree('.')) {
122 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
123 top_of_kernel_tree($1)) {
128 if (!defined $root) {
129 print "Must be run from the top-level dir. of a kernel tree\n";
134 my $emitted_corrupt = 0;
137 [A-Za-z_][A-Za-z\d_]*
138 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
140 our $Storage = qr{extern|static|asmlinkage};
152 # Notes to $Attribute:
153 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
172 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
173 ____cacheline_aligned|
174 ____cacheline_aligned_in_smp|
175 ____cacheline_internodealigned_in_smp|
179 our $Inline = qr{inline|__always_inline|noinline};
180 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
181 our $Lval = qr{$Ident(?:$Member)*};
183 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
184 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
185 our $Compare = qr{<=|>=|==|!=|<|>};
189 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
197 [\x09\x0A\x0D\x20-\x7E] # ASCII
198 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
199 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
200 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
201 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
202 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
203 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
204 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
207 our $typeTypedefs = qr{(?x:
208 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
212 our $logFunctions = qr{(?x:
214 pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)|
215 (dev|netdev|netif)_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)|
222 qr{(?:unsigned\s+)?char},
223 qr{(?:unsigned\s+)?short},
224 qr{(?:unsigned\s+)?int},
225 qr{(?:unsigned\s+)?long},
226 qr{(?:unsigned\s+)?long\s+int},
227 qr{(?:unsigned\s+)?long\s+long},
228 qr{(?:unsigned\s+)?long\s+long\s+int},
237 qr{${Ident}_handler},
238 qr{${Ident}_handler_fn},
240 our @modifierList = (
244 our $allowed_asm_includes = qr{(?x:
248 # memory.h: ARM has a custom one
251 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
252 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
253 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
255 (?:$Modifier\s+|const\s+)*
257 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
261 (?:\s+$Modifier|\s+const)*
265 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
266 (?:\s+$Inline|\s+$Modifier)*
268 $Declare = qr{(?:$Storage\s+)?$Type};
272 $chk_signoff = 0 if ($file);
274 my @dep_includes = ();
275 my @dep_functions = ();
276 my $removal = "Documentation/feature-removal-schedule.txt";
277 if ($tree && -f "$root/$removal") {
278 open(my $REMOVE, '<', "$root/$removal") ||
279 die "$P: $removal: open failed - $!\n";
281 if (/^Check:\s+(.*\S)/) {
282 for my $entry (split(/[, ]+/, $1)) {
283 if ($entry =~ m@include/(.*)@) {
284 push(@dep_includes, $1);
286 } elsif ($entry !~ m@/@) {
287 push(@dep_functions, $entry);
298 for my $filename (@ARGV) {
301 open($FILE, '-|', "diff -u /dev/null $filename") ||
302 die "$P: $filename: diff failed - $!\n";
303 } elsif ($filename eq '-') {
304 open($FILE, '<&STDIN');
306 open($FILE, '<', "$filename") ||
307 die "$P: $filename: open failed - $!\n";
309 if ($filename eq '-') {
310 $vname = 'Your patch';
319 if (!process($filename)) {
328 sub top_of_kernel_tree {
332 "COPYING", "MAINTAINERS", "Makefile",
333 "README", "docs", "VERSION",
337 foreach my $check (@tree_check) {
338 if (! -e $root . '/' . $check) {
350 for my $c (split(//, $str)) {
354 for (; ($n % 8) != 0; $n++) {
366 (my $res = shift) =~ tr/\t/ /c;
373 # Drop the diff line leader and expand tabs
375 $line = expand_tabs($line);
377 # Pick the indent from the front of the line.
378 my ($white) = ($line =~ /^(\s*)/);
380 return (length($line), length($white));
383 my $sanitise_quote = '';
385 sub sanitise_line_reset {
386 my ($in_comment) = @_;
389 $sanitise_quote = '*/';
391 $sanitise_quote = '';
404 # Always copy over the diff marker.
405 $res = substr($line, 0, 1);
407 for ($off = 1; $off < length($line); $off++) {
408 $c = substr($line, $off, 1);
410 # Comments we are wacking completly including the begin
411 # and end, all to $;.
412 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
413 $sanitise_quote = '*/';
415 substr($res, $off, 2, "$;$;");
419 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
420 $sanitise_quote = '';
421 substr($res, $off, 2, "$;$;");
425 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
426 $sanitise_quote = '//';
428 substr($res, $off, 2, $sanitise_quote);
433 # A \ in a string means ignore the next character.
434 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
436 substr($res, $off, 2, 'XX');
441 if ($c eq "'" || $c eq '"') {
442 if ($sanitise_quote eq '') {
443 $sanitise_quote = $c;
445 substr($res, $off, 1, $c);
447 } elsif ($sanitise_quote eq $c) {
448 $sanitise_quote = '';
452 #print "c<$c> SQ<$sanitise_quote>\n";
453 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
454 substr($res, $off, 1, $;);
455 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
456 substr($res, $off, 1, $;);
457 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
458 substr($res, $off, 1, 'X');
460 substr($res, $off, 1, $c);
464 if ($sanitise_quote eq '//') {
465 $sanitise_quote = '';
468 # The pathname on a #include may be surrounded by '<' and '>'.
469 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
470 my $clean = 'X' x length($1);
471 $res =~ s@\<.*\>@<$clean>@;
473 # The whole of a #error is a string.
474 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
475 my $clean = 'X' x length($1);
476 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
482 sub ctx_statement_block {
483 my ($linenr, $remain, $off) = @_;
484 my $line = $linenr - 1;
501 @stack = (['', 0]) if ($#stack == -1);
503 #warn "CSB: blk<$blk> remain<$remain>\n";
504 # If we are about to drop off the end, pull in more
507 for (; $remain > 0; $line++) {
508 last if (!defined $lines[$line]);
509 next if ($lines[$line] =~ /^-/);
512 $blk .= $lines[$line] . "\n";
517 # Bail if there is no further context.
518 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
524 $c = substr($blk, $off, 1);
525 $remainder = substr($blk, $off);
527 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
529 # Handle nested #if/#else.
530 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
531 push(@stack, [ $type, $level ]);
532 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
533 ($type, $level) = @{$stack[$#stack - 1]};
534 } elsif ($remainder =~ /^#\s*endif\b/) {
535 ($type, $level) = @{pop(@stack)};
538 # Statement ends at the ';' or a close '}' at the
540 if ($level == 0 && $c eq ';') {
544 # An else is really a conditional as long as its not else if
545 if ($level == 0 && $coff_set == 0 &&
546 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
547 $remainder =~ /^(else)(?:\s|{)/ &&
548 $remainder !~ /^else\s+if\b/) {
549 $coff = $off + length($1) - 1;
551 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
552 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
555 if (($type eq '' || $type eq '(') && $c eq '(') {
559 if ($type eq '(' && $c eq ')') {
561 $type = ($level != 0)? '(' : '';
563 if ($level == 0 && $coff < $soff) {
566 #warn "CSB: mark coff<$coff>\n";
569 if (($type eq '' || $type eq '{') && $c eq '{') {
573 if ($type eq '{' && $c eq '}') {
575 $type = ($level != 0)? '{' : '';
578 if (substr($blk, $off + 1, 1) eq ';') {
586 # We are truly at the end, so shuffle to the next line.
593 my $statement = substr($blk, $soff, $off - $soff + 1);
594 my $condition = substr($blk, $soff, $coff - $soff + 1);
596 #warn "STATEMENT<$statement>\n";
597 #warn "CONDITION<$condition>\n";
599 #print "coff<$coff> soff<$off> loff<$loff>\n";
601 return ($statement, $condition,
602 $line, $remain + 1, $off - $loff + 1, $level);
605 sub statement_lines {
608 # Strip the diff line prefixes and rip blank lines at start and end.
609 $stmt =~ s/(^|\n)./$1/g;
613 my @stmt_lines = ($stmt =~ /\n/g);
615 return $#stmt_lines + 2;
618 sub statement_rawlines {
621 my @stmt_lines = ($stmt =~ /\n/g);
623 return $#stmt_lines + 2;
626 sub statement_block_size {
629 $stmt =~ s/(^|\n)./$1/g;
635 my @stmt_lines = ($stmt =~ /\n/g);
636 my @stmt_statements = ($stmt =~ /;/g);
638 my $stmt_lines = $#stmt_lines + 2;
639 my $stmt_statements = $#stmt_statements + 1;
641 if ($stmt_lines > $stmt_statements) {
644 return $stmt_statements;
648 sub ctx_statement_full {
649 my ($linenr, $remain, $off) = @_;
650 my ($statement, $condition, $level);
654 # Grab the first conditional/block pair.
655 ($statement, $condition, $linenr, $remain, $off, $level) =
656 ctx_statement_block($linenr, $remain, $off);
657 #print "F: c<$condition> s<$statement> remain<$remain>\n";
658 push(@chunks, [ $condition, $statement ]);
659 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
660 return ($level, $linenr, @chunks);
663 # Pull in the following conditional/block pairs and see if they
664 # could continue the statement.
666 ($statement, $condition, $linenr, $remain, $off, $level) =
667 ctx_statement_block($linenr, $remain, $off);
668 #print "C: c<$condition> s<$statement> remain<$remain>\n";
669 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
671 push(@chunks, [ $condition, $statement ]);
674 return ($level, $linenr, @chunks);
678 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
680 my $start = $linenr - 1;
687 my @stack = ($level);
688 for ($line = $start; $remain > 0; $line++) {
689 next if ($rawlines[$line] =~ /^-/);
692 $blk .= $rawlines[$line];
694 # Handle nested #if/#else.
695 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
696 push(@stack, $level);
697 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
698 $level = $stack[$#stack - 1];
699 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
700 $level = pop(@stack);
703 foreach my $c (split(//, $lines[$line])) {
704 ##print "C<$c>L<$level><$open$close>O<$off>\n";
710 if ($c eq $close && $level > 0) {
712 last if ($level == 0);
713 } elsif ($c eq $open) {
718 if (!$outer || $level <= 1) {
719 push(@res, $rawlines[$line]);
722 last if ($level == 0);
725 return ($level, @res);
727 sub ctx_block_outer {
728 my ($linenr, $remain) = @_;
730 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
734 my ($linenr, $remain) = @_;
736 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
740 my ($linenr, $remain, $off) = @_;
742 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
745 sub ctx_block_level {
746 my ($linenr, $remain) = @_;
748 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
750 sub ctx_statement_level {
751 my ($linenr, $remain, $off) = @_;
753 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
756 sub ctx_locate_comment {
757 my ($first_line, $end_line) = @_;
759 # Catch a comment on the end of the line itself.
760 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
761 return $current_comment if (defined $current_comment);
763 # Look through the context and try and figure out if there is a
766 $current_comment = '';
767 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
768 my $line = $rawlines[$linenr - 1];
770 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
773 if ($line =~ m@/\*@) {
776 if (!$in_comment && $current_comment ne '') {
777 $current_comment = '';
779 $current_comment .= $line . "\n" if ($in_comment);
780 if ($line =~ m@\*/@) {
785 chomp($current_comment);
786 return($current_comment);
788 sub ctx_has_comment {
789 my ($first_line, $end_line) = @_;
790 my $cmt = ctx_locate_comment($first_line, $end_line);
792 ##print "LINE: $rawlines[$end_line - 1 ]\n";
793 ##print "CMMT: $cmt\n";
799 my ($linenr, $cnt) = @_;
801 my $offset = $linenr - 1;
806 $line = $rawlines[$offset++];
807 next if (defined($line) && $line =~ /^-/);
819 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
822 $coded = sprintf("^%c", unpack('C', $2) + 64);
831 my $av_preprocessor = 0;
837 $av_preprocessor = 0;
839 @av_paren_type = ('E');
840 $av_pend_colon = 'O';
843 sub annotate_values {
844 my ($stream, $type) = @_;
847 my $var = '_' x length($stream);
850 print "$stream\n" if ($dbg_values > 1);
852 while (length($cur)) {
853 @av_paren_type = ('E') if ($#av_paren_type < 0);
854 print " <" . join('', @av_paren_type) .
855 "> <$type> <$av_pending>" if ($dbg_values > 1);
856 if ($cur =~ /^(\s+)/o) {
857 print "WS($1)\n" if ($dbg_values > 1);
858 if ($1 =~ /\n/ && $av_preprocessor) {
859 $type = pop(@av_paren_type);
860 $av_preprocessor = 0;
863 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
864 print "CAST($1)\n" if ($dbg_values > 1);
865 push(@av_paren_type, $type);
868 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
869 print "DECLARE($1)\n" if ($dbg_values > 1);
872 } elsif ($cur =~ /^($Modifier)\s*/) {
873 print "MODIFIER($1)\n" if ($dbg_values > 1);
876 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
877 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
878 $av_preprocessor = 1;
879 push(@av_paren_type, $type);
885 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
886 print "UNDEF($1)\n" if ($dbg_values > 1);
887 $av_preprocessor = 1;
888 push(@av_paren_type, $type);
890 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
891 print "PRE_START($1)\n" if ($dbg_values > 1);
892 $av_preprocessor = 1;
894 push(@av_paren_type, $type);
895 push(@av_paren_type, $type);
898 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
899 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
900 $av_preprocessor = 1;
902 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
906 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
907 print "PRE_END($1)\n" if ($dbg_values > 1);
909 $av_preprocessor = 1;
911 # Assume all arms of the conditional end as this
912 # one does, and continue as if the #endif was not here.
914 push(@av_paren_type, $type);
917 } elsif ($cur =~ /^(\\\n)/o) {
918 print "PRECONT($1)\n" if ($dbg_values > 1);
920 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
921 print "ATTR($1)\n" if ($dbg_values > 1);
925 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
926 print "SIZEOF($1)\n" if ($dbg_values > 1);
932 } elsif ($cur =~ /^(if|while|for)\b/o) {
933 print "COND($1)\n" if ($dbg_values > 1);
937 } elsif ($cur =~/^(case)/o) {
938 print "CASE($1)\n" if ($dbg_values > 1);
939 $av_pend_colon = 'C';
942 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
943 print "KEYWORD($1)\n" if ($dbg_values > 1);
946 } elsif ($cur =~ /^(\()/o) {
947 print "PAREN('$1')\n" if ($dbg_values > 1);
948 push(@av_paren_type, $av_pending);
952 } elsif ($cur =~ /^(\))/o) {
953 my $new_type = pop(@av_paren_type);
954 if ($new_type ne '_') {
956 print "PAREN('$1') -> $type\n"
957 if ($dbg_values > 1);
959 print "PAREN('$1')\n" if ($dbg_values > 1);
962 } elsif ($cur =~ /^($Ident)\s*\(/o) {
963 print "FUNC($1)\n" if ($dbg_values > 1);
967 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
968 if (defined $2 && $type eq 'C' || $type eq 'T') {
969 $av_pend_colon = 'B';
970 } elsif ($type eq 'E') {
971 $av_pend_colon = 'L';
973 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
976 } elsif ($cur =~ /^($Ident|$Constant)/o) {
977 print "IDENT($1)\n" if ($dbg_values > 1);
980 } elsif ($cur =~ /^($Assignment)/o) {
981 print "ASSIGN($1)\n" if ($dbg_values > 1);
984 } elsif ($cur =~/^(;|{|})/) {
985 print "END($1)\n" if ($dbg_values > 1);
987 $av_pend_colon = 'O';
989 } elsif ($cur =~/^(,)/) {
990 print "COMMA($1)\n" if ($dbg_values > 1);
993 } elsif ($cur =~ /^(\?)/o) {
994 print "QUESTION($1)\n" if ($dbg_values > 1);
997 } elsif ($cur =~ /^(:)/o) {
998 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1000 substr($var, length($res), 1, $av_pend_colon);
1001 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1006 $av_pend_colon = 'O';
1008 } elsif ($cur =~ /^(\[)/o) {
1009 print "CLOSE($1)\n" if ($dbg_values > 1);
1012 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1015 print "OPV($1)\n" if ($dbg_values > 1);
1022 substr($var, length($res), 1, $variant);
1025 } elsif ($cur =~ /^($Operators)/o) {
1026 print "OP($1)\n" if ($dbg_values > 1);
1027 if ($1 ne '++' && $1 ne '--') {
1031 } elsif ($cur =~ /(^.)/o) {
1032 print "C($1)\n" if ($dbg_values > 1);
1035 $cur = substr($cur, length($1));
1036 $res .= $type x length($1);
1040 return ($res, $var);
1044 my ($possible, $line) = @_;
1045 my $notPermitted = qr{(?:
1060 ^(?:typedef|struct|enum)\b
1062 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1063 if ($possible !~ $notPermitted) {
1064 # Check for modifiers.
1065 $possible =~ s/\s*$Storage\s*//g;
1066 $possible =~ s/\s*$Sparse\s*//g;
1067 if ($possible =~ /^\s*$/) {
1069 } elsif ($possible =~ /\s/) {
1070 $possible =~ s/\s*$Type\s*//g;
1071 for my $modifier (split(' ', $possible)) {
1072 if ($modifier !~ $notPermitted) {
1073 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1074 push(@modifierList, $modifier);
1079 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1080 push(@typeList, $possible);
1084 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1091 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1094 my $line = $prefix . $_[0];
1096 $line = (split('\n', $line))[0] . "\n" if ($terse);
1098 push(our @report, $line);
1106 if (report("ERROR: $_[0]\n")) {
1112 if (report("WARNING: $_[0]\n")) {
1118 if ($check && report("CHECK: $_[0]\n")) {
1124 sub check_absolute_file {
1125 my ($absolute, $herecurr) = @_;
1126 my $file = $absolute;
1128 ##print "absolute<$absolute>\n";
1130 # See if any suffix of this path is a path within the tree.
1131 while ($file =~ s@^[^/]*/@@) {
1132 if (-f "$root/$file") {
1133 ##print "file<$file>\n";
1141 # It is, so see if the prefix is acceptable.
1142 my $prefix = $absolute;
1143 substr($prefix, -length($file)) = '';
1145 ##print "prefix<$prefix>\n";
1146 if ($prefix ne ".../") {
1147 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1152 my $filename = shift;
1158 my $stashrawline="";
1175 # Trace the real file/line as we go.
1181 my $comment_edge = 0;
1185 my $prev_values = 'E';
1188 my %suppress_ifbraces;
1189 my %suppress_whiletrailers;
1190 my %suppress_export;
1192 # Pre-scan the patch sanitizing the lines.
1193 # Pre-scan the patch looking for any __setup documentation.
1195 my @setup_docs = ();
1198 sanitise_line_reset();
1200 foreach my $rawline (@rawlines) {
1204 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1206 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1211 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1220 # Guestimate if this is a continuing comment. Run
1221 # the context looking for a comment "edge". If this
1222 # edge is a close comment then we must be in a comment
1226 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1227 next if (defined $rawlines[$ln - 1] &&
1228 $rawlines[$ln - 1] =~ /^-/);
1230 #print "RAW<$rawlines[$ln - 1]>\n";
1231 last if (!defined $rawlines[$ln - 1]);
1232 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1233 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1238 if (defined $edge && $edge eq '*/') {
1242 # Guestimate if this is a continuing comment. If this
1243 # is the start of a diff block and this line starts
1244 # ' *' then it is very likely a comment.
1245 if (!defined $edge &&
1246 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1251 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1252 sanitise_line_reset($in_comment);
1254 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1255 # Standardise the strings and chars within the input to
1256 # simplify matching -- only bother with positive lines.
1257 $line = sanitise_line($rawline);
1259 push(@lines, $line);
1262 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1267 #print "==>$rawline\n";
1268 #print "-->$line\n";
1270 if ($setup_docs && $line =~ /^\+/) {
1271 push(@setup_docs, $line);
1279 foreach my $line (@lines) {
1282 my $rawline = $rawlines[$linenr - 1];
1284 #extract the line range in the file after the patch is applied
1285 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1287 $first_line = $linenr + 1;
1297 %suppress_ifbraces = ();
1298 %suppress_whiletrailers = ();
1299 %suppress_export = ();
1302 # track the line number as we move through the hunk, note that
1303 # new versions of GNU diff omit the leading space on completely
1304 # blank context lines so we need to count that too.
1305 } elsif ($line =~ /^( |\+|$)/) {
1307 $realcnt-- if ($realcnt != 0);
1309 # Measure the line length and indent.
1310 ($length, $indent) = line_stats($rawline);
1312 # Track the previous line.
1313 ($prevline, $stashline) = ($stashline, $line);
1314 ($previndent, $stashindent) = ($stashindent, $indent);
1315 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1317 #warn "line<$line>\n";
1319 } elsif ($realcnt == 1) {
1323 my $hunk_line = ($realcnt != 0);
1325 #make up the handle for any error we report on this line
1326 $prefix = "$filename:$realline: " if ($emacs && $file);
1327 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1329 $here = "#$linenr: " if (!$file);
1330 $here = "#$realline: " if ($file);
1332 # extract the filename as it passes
1333 if ($line =~ /^diff --git.*?(\S+)$/) {
1335 $realfile =~ s@^([^/]*)/@@;
1337 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1339 $realfile =~ s@^([^/]*)/@@;
1342 if (!$file && $tree && $p1_prefix ne '' &&
1343 -e "$root/$p1_prefix") {
1344 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1347 if ($realfile =~ m@^include/asm/@) {
1348 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1353 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1355 my $hereline = "$here\n$rawline\n";
1356 my $herecurr = "$here\n$rawline\n";
1357 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1359 $cnt_lines++ if ($realcnt != 0);
1361 # Check for incorrect file permissions
1362 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1363 my $permhere = $here . "FILE: $realfile\n";
1364 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1365 ERROR("do not set execute permissions for source files\n" . $permhere);
1369 #check the patch for a signoff:
1370 if ($line =~ /^\s*signed-off-by:/i) {
1371 # This is a signoff, if ugly, so do not double report.
1373 if (!($line =~ /^\s*Signed-off-by:/)) {
1374 WARN("Signed-off-by: is the preferred form\n" .
1377 if ($line =~ /^\s*signed-off-by:\S/i) {
1378 WARN("space required after Signed-off-by:\n" .
1383 # Check for wrappage within a valid hunk of the file
1384 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1385 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1386 $herecurr) if (!$emitted_corrupt++);
1389 # Check for absolute kernel paths.
1391 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1394 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1395 check_absolute_file($1, $herecurr)) {
1398 check_absolute_file($file, $herecurr);
1403 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1404 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1405 $rawline !~ m/^$UTF8*$/) {
1406 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1408 my $blank = copy_spacing($rawline);
1409 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1410 my $hereptr = "$hereline$ptr\n";
1412 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1415 # ignore non-hunk lines and lines being removed
1416 next if (!$hunk_line || $line =~ /^-/);
1418 #trailing whitespace
1419 if ($line =~ /^\+.*\015/) {
1420 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1421 ERROR("DOS line endings\n" . $herevet);
1423 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1424 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1425 ERROR("trailing whitespace\n" . $herevet);
1429 # check for Kconfig help text having a real description
1430 # Only applies when adding the entry originally, after that we do not have
1431 # sufficient context to determine whether it is indeed long enough.
1432 if ($realfile =~ /Kconfig/ &&
1433 $line =~ /\+\s*(?:---)?help(?:---)?$/) {
1436 my $ln = $linenr + 1;
1439 while ($cnt > 0 && defined $lines[$ln - 1]) {
1440 $f = $lines[$ln - 1];
1441 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1442 $is_end = $lines[$ln - 1] =~ /^\+/;
1445 next if ($f =~ /^-/);
1449 next if ($f =~ /^$/);
1450 if ($f =~ /^\s*config\s/) {
1456 WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
1457 #print "is_end<$is_end> length<$length>\n";
1460 # check we are in a valid source file if not then ignore this hunk
1461 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1464 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1465 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1466 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ ||
1467 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1470 WARN("line over 80 characters\n" . $herecurr);
1473 # check for spaces before a quoted newline
1474 if ($rawline =~ /^.*\".*\s\\n/) {
1475 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1478 # check for adding lines without a newline.
1479 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1480 WARN("adding a line without newline at end of file\n" . $herecurr);
1483 # Blackfin: use hi/lo macros
1484 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1485 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1486 my $herevet = "$here\n" . cat_vet($line) . "\n";
1487 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1489 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1490 my $herevet = "$here\n" . cat_vet($line) . "\n";
1491 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1495 # check we are in a valid source file C or perl if not then ignore this hunk
1496 next if ($realfile !~ /\.(h|c|pl)$/);
1498 # in QEMU, no tabs are allowed
1499 if ($rawline =~ /^\+.*\t/) {
1500 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1501 ERROR("code indent should never use tabs\n" . $herevet);
1505 # check we are in a valid C source file if not then ignore this hunk
1506 next if ($realfile !~ /\.(h|c)$/);
1508 # check for RCS/CVS revision markers
1509 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1510 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1513 # Blackfin: don't use __builtin_bfin_[cs]sync
1514 if ($line =~ /__builtin_bfin_csync/) {
1515 my $herevet = "$here\n" . cat_vet($line) . "\n";
1516 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1518 if ($line =~ /__builtin_bfin_ssync/) {
1519 my $herevet = "$here\n" . cat_vet($line) . "\n";
1520 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1523 # Check for potential 'bare' types
1524 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1526 if ($realcnt && $line =~ /.\s*\S/) {
1527 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1528 ctx_statement_block($linenr, $realcnt, 0);
1529 $stat =~ s/\n./\n /g;
1530 $cond =~ s/\n./\n /g;
1532 # Find the real next line.
1533 $realline_next = $line_nr_next;
1534 if (defined $realline_next &&
1535 (!defined $lines[$realline_next - 1] ||
1536 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1543 # Ignore goto labels.
1544 if ($s =~ /$Ident:\*$/s) {
1546 # Ignore functions being called
1547 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1549 } elsif ($s =~ /^.\s*else\b/s) {
1551 # declarations always start with types
1552 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) {
1555 possible($type, "A:" . $s);
1557 # definitions in global scope can only start with types
1558 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1559 possible($1, "B:" . $s);
1562 # any (foo ... *) is a pointer cast, and foo is a type
1563 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1564 possible($1, "C:" . $s);
1567 # Check for any sort of function declaration.
1568 # int foo(something bar, other baz);
1569 # void (*store_gdt)(x86_descr_ptr *);
1570 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1571 my ($name_len) = length($1);
1574 substr($ctx, 0, $name_len + 1, '');
1575 $ctx =~ s/\)[^\)]*$//;
1577 for my $arg (split(/\s*,\s*/, $ctx)) {
1578 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1580 possible($1, "D:" . $s);
1588 # Checks which may be anchored in the context.
1591 # Check for switch () and associated case and default
1592 # statements should be at the same indent.
1593 if ($line=~/\bswitch\s*\(.*\)/) {
1596 my @ctx = ctx_block_outer($linenr, $realcnt);
1598 for my $ctx (@ctx) {
1599 my ($clen, $cindent) = line_stats($ctx);
1600 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1601 $indent != $cindent) {
1602 $err .= "$sep$ctx\n";
1609 ERROR("switch and case should be at the same indent\n$hereline$err");
1613 # if/while/etc brace do not go on next line, unless defining a do while loop,
1614 # or if that brace on the next line is for something else
1615 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1616 my $pre_ctx = "$1$2";
1618 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1619 my $ctx_cnt = $realcnt - $#ctx - 1;
1620 my $ctx = join("\n", @ctx);
1622 my $ctx_ln = $linenr;
1623 my $ctx_skip = $realcnt;
1625 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1626 defined $lines[$ctx_ln - 1] &&
1627 $lines[$ctx_ln - 1] =~ /^-/)) {
1628 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1629 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1633 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1634 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1636 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1637 ERROR("that open brace { should be on the previous line\n" .
1638 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1640 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1641 $ctx =~ /\)\s*\;\s*$/ &&
1642 defined $lines[$ctx_ln - 1])
1644 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1645 if ($nindent > $indent) {
1646 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1647 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1652 # Check relative indent for conditionals and blocks.
1653 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1654 my ($s, $c) = ($stat, $cond);
1656 substr($s, 0, length($c), '');
1658 # Make sure we remove the line prefixes as we have
1659 # none on the first line, and are going to readd them
1663 # Find out how long the conditional actually is.
1664 my @newlines = ($c =~ /\n/gs);
1665 my $cond_lines = 1 + $#newlines;
1667 # We want to check the first line inside the block
1668 # starting at the end of the conditional, so remove:
1669 # 1) any blank line termination
1670 # 2) any opening brace { on end of the line
1672 my $continuation = 0;
1674 $s =~ s/^.*\bdo\b//;
1676 if ($s =~ s/^\s*\\//) {
1679 if ($s =~ s/^\s*?\n//) {
1684 # Also ignore a loop construct at the end of a
1685 # preprocessor statement.
1686 if (($prevline =~ /^.\s*#\s*define\s/ ||
1687 $prevline =~ /\\\s*$/) && $continuation == 0) {
1693 while ($cond_ptr != $cond_lines) {
1694 $cond_ptr = $cond_lines;
1696 # If we see an #else/#elif then the code
1698 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1703 # 1) blank lines, they should be at 0,
1704 # 2) preprocessor lines, and
1706 if ($continuation ||
1708 $s =~ /^\s*#\s*?/ ||
1709 $s =~ /^\s*$Ident\s*:/) {
1710 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1711 if ($s =~ s/^.*?\n//) {
1717 my (undef, $sindent) = line_stats("+" . $s);
1718 my $stat_real = raw_line($linenr, $cond_lines);
1720 # Check if either of these lines are modified, else
1721 # this is not this patch's fault.
1722 if (!defined($stat_real) ||
1723 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1726 if (defined($stat_real) && $cond_lines > 1) {
1727 $stat_real = "[...]\n$stat_real";
1730 #print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
1732 if ($check && (($sindent % 4) != 0 ||
1733 ($sindent <= $indent && $s ne ''))) {
1734 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1738 # Track the 'values' across context and added lines.
1739 my $opline = $line; $opline =~ s/^./ /;
1740 my ($curr_values, $curr_vars) =
1741 annotate_values($opline . "\n", $prev_values);
1742 $curr_values = $prev_values . $curr_values;
1744 my $outline = $opline; $outline =~ s/\t/ /g;
1745 print "$linenr > .$outline\n";
1746 print "$linenr > $curr_values\n";
1747 print "$linenr > $curr_vars\n";
1749 $prev_values = substr($curr_values, -1);
1751 #ignore lines not being added
1752 if ($line=~/^[^\+]/) {next;}
1754 # TEST: allow direct testing of the type matcher.
1756 if ($line =~ /^.\s*$Declare\s*$/) {
1757 ERROR("TEST: is type\n" . $herecurr);
1758 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1759 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1763 # TEST: allow direct testing of the attribute matcher.
1765 if ($line =~ /^.\s*$Modifier\s*$/) {
1766 ERROR("TEST: is attr\n" . $herecurr);
1767 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1768 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1773 # check for initialisation to aggregates open brace on the next line
1774 if ($line =~ /^.\s*{/ &&
1775 $prevline =~ /(?:^|[^=])=\s*$/) {
1776 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1780 # Checks which are anchored on the added line.
1783 # check for malformed paths in #include statements (uses RAW line)
1784 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1786 if ($path =~ m{//}) {
1787 ERROR("malformed #include filename\n" .
1792 # no C99 // comments
1793 if ($line =~ m{//}) {
1794 ERROR("do not use C99 // comments\n" . $herecurr);
1796 # Remove C99 comments.
1798 $opline =~ s@//.*@@;
1800 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1801 # the whole statement.
1802 #print "APW <$lines[$realline_next - 1]>\n";
1803 if (defined $realline_next &&
1804 exists $lines[$realline_next - 1] &&
1805 !defined $suppress_export{$realline_next} &&
1806 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1807 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1808 # Handle definitions which produce identifiers with
1811 # EXPORT_SYMBOL(something_foo);
1813 if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
1814 $name =~ /^${Ident}_$2/) {
1815 #print "FOO C name<$name>\n";
1816 $suppress_export{$realline_next} = 1;
1818 } elsif ($stat !~ /(?:
1820 ^.DEFINE_$Ident\(\Q$name\E\)|
1821 ^.DECLARE_$Ident\(\Q$name\E\)|
1822 ^.LIST_HEAD\(\Q$name\E\)|
1823 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1824 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
1826 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1827 $suppress_export{$realline_next} = 2;
1829 $suppress_export{$realline_next} = 1;
1832 if (!defined $suppress_export{$linenr} &&
1833 $prevline =~ /^.\s*$/ &&
1834 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1835 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1836 #print "FOO B <$lines[$linenr - 1]>\n";
1837 $suppress_export{$linenr} = 2;
1839 if (defined $suppress_export{$linenr} &&
1840 $suppress_export{$linenr} == 2) {
1841 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1844 # check for global initialisers.
1845 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1846 ERROR("do not initialise globals to 0 or NULL\n" .
1849 # check for static initialisers.
1850 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1851 ERROR("do not initialise statics to 0 or NULL\n" .
1855 # * goes on variable not on type
1857 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1858 my ($from, $to) = ($1, $1);
1860 # Should start with a space.
1861 $to =~ s/^(\S)/ $1/;
1862 # Should not end with a space.
1864 # '*'s should not have spaces between.
1865 while ($to =~ s/\*\s+\*/\*\*/) {
1868 #print "from<$from> to<$to>\n";
1870 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1872 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1873 my ($from, $to, $ident) = ($1, $1, $2);
1875 # Should start with a space.
1876 $to =~ s/^(\S)/ $1/;
1877 # Should not end with a space.
1879 # '*'s should not have spaces between.
1880 while ($to =~ s/\*\s+\*/\*\*/) {
1882 # Modifiers should have spaces.
1883 $to =~ s/(\b$Modifier$)/$1 /;
1885 #print "from<$from> to<$to> ident<$ident>\n";
1886 if ($from ne $to && $ident !~ /^$Modifier$/) {
1887 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1891 # # no BUG() or BUG_ON()
1892 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1893 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1894 # print "$herecurr";
1898 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1899 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1902 # printk should use KERN_* levels. Note that follow on printk's on the
1903 # same line do not need a level, so we use the current block context
1904 # to try and find and validate the current printk. In summary the current
1905 # printk includes all preceding printk's which have no newline on the end.
1906 # we assume the first bad printk is the one to report.
1907 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1909 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1910 #print "CHECK<$lines[$ln - 1]\n";
1911 # we have a preceding printk if it ends
1912 # with "\n" ignore it, else it is to blame
1913 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1914 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1921 WARN("printk() should include KERN_ facility level\n" . $herecurr);
1925 # function brace can't be on same line, except for #defines of do while,
1926 # or if closed on same line
1927 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1928 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1929 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1932 # open braces for enum, union and struct go on the same line.
1933 if ($line =~ /^.\s*{/ &&
1934 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1935 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1938 # missing space after union, struct or enum definition
1939 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1940 WARN("missing space after $1 definition\n" . $herecurr);
1943 # check for spacing round square brackets; allowed:
1944 # 1. with a type on the left -- int [] a;
1945 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1946 # 3. inside a curly brace -- = { [0...10] = 5 }
1947 while ($line =~ /(.*?\s)\[/g) {
1948 my ($where, $prefix) = ($-[1], $1);
1949 if ($prefix !~ /$Type\s+$/ &&
1950 ($where != 0 || $prefix !~ /^.\s+$/) &&
1951 $prefix !~ /{\s+$/) {
1952 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1956 # check for spaces between functions and their parentheses.
1957 while ($line =~ /($Ident)\s+\(/g) {
1959 my $ctx_before = substr($line, 0, $-[1]);
1960 my $ctx = "$ctx_before$name";
1962 # Ignore those directives where spaces _are_ permitted.
1964 if|for|while|switch|return|case|
1965 volatile|__volatile__|
1966 __attribute__|format|__extension__|
1970 # cpp #define statements have non-optional spaces, ie
1971 # if there is a space between the name and the open
1972 # parenthesis it is simply not a parameter group.
1973 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1975 # cpp #elif statement condition may start with a (
1976 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1978 # If this whole things ends with a type its most
1979 # likely a typedef for a function.
1980 } elsif ($ctx =~ /$Type$/) {
1983 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1986 # Check operator spacing.
1987 if (!($line=~/\#\s*include/)) {
1989 <<=|>>=|<=|>=|==|!=|
1990 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1991 =>|->|<<|>>|<|>|=|!|~|
1992 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1995 my @elements = split(/($ops|;)/, $opline);
1998 my $blank = copy_spacing($opline);
2000 for (my $n = 0; $n < $#elements; $n += 2) {
2001 $off += length($elements[$n]);
2003 # Pick up the preceding and succeeding characters.
2004 my $ca = substr($opline, 0, $off);
2006 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2007 $cc = substr($opline, $off + length($elements[$n + 1]));
2009 my $cb = "$ca$;$cc";
2012 $a = 'V' if ($elements[$n] ne '');
2013 $a = 'W' if ($elements[$n] =~ /\s$/);
2014 $a = 'C' if ($elements[$n] =~ /$;$/);
2015 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2016 $a = 'O' if ($elements[$n] eq '');
2017 $a = 'E' if ($ca =~ /^\s*$/);
2019 my $op = $elements[$n + 1];
2022 if (defined $elements[$n + 2]) {
2023 $c = 'V' if ($elements[$n + 2] ne '');
2024 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2025 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2026 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2027 $c = 'O' if ($elements[$n + 2] eq '');
2028 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2033 my $ctx = "${a}x${c}";
2035 my $at = "(ctx:$ctx)";
2037 my $ptr = substr($blank, 0, $off) . "^";
2038 my $hereptr = "$hereline$ptr\n";
2040 # Pull out the value of this operator.
2041 my $op_type = substr($curr_values, $off + 1, 1);
2043 # Get the full operator variant.
2044 my $opv = $op . substr($curr_vars, $off, 1);
2046 # Ignore operators passed as parameters.
2047 if ($op_type ne 'V' &&
2048 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2051 # } elsif ($op =~ /^$;+$/) {
2053 # ; should have either the end of line or a space or \ after it
2054 } elsif ($op eq ';') {
2055 if ($ctx !~ /.x[WEBC]/ &&
2056 $cc !~ /^\\/ && $cc !~ /^;/) {
2057 ERROR("space required after that '$op' $at\n" . $hereptr);
2061 } elsif ($op eq '//') {
2065 # : when part of a bitfield
2066 } elsif ($op eq '->' || $opv eq ':B') {
2067 if ($ctx =~ /Wx.|.xW/) {
2068 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
2071 # , must have a space on the right.
2072 # not required when having a single },{ on one line
2073 } elsif ($op eq ',') {
2074 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
2075 ($elements[$n] . $elements[$n + 2]) !~ " *}{") {
2076 ERROR("space required after that '$op' $at\n" . $hereptr);
2079 # '*' as part of a type definition -- reported already.
2080 } elsif ($opv eq '*_') {
2081 #warn "'*' is part of type\n";
2083 # unary operators should have a space before and
2084 # none after. May be left adjacent to another
2085 # unary operator, or a cast
2086 } elsif ($op eq '!' || $op eq '~' ||
2087 $opv eq '*U' || $opv eq '-U' ||
2088 $opv eq '&U' || $opv eq '&&U') {
2089 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2090 ERROR("space required before that '$op' $at\n" . $hereptr);
2092 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2093 # A unary '*' may be const
2095 } elsif ($ctx =~ /.xW/) {
2096 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2099 # unary ++ and unary -- are allowed no space on one side.
2100 } elsif ($op eq '++' or $op eq '--') {
2101 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2102 ERROR("space required one side of that '$op' $at\n" . $hereptr);
2104 if ($ctx =~ /Wx[BE]/ ||
2105 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2106 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2108 if ($ctx =~ /ExW/) {
2109 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2113 # << and >> may either have or not have spaces both sides
2114 } elsif ($op eq '<<' or $op eq '>>' or
2115 $op eq '&' or $op eq '^' or $op eq '|' or
2116 $op eq '+' or $op eq '-' or
2117 $op eq '*' or $op eq '/' or
2120 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2121 ERROR("need consistent spacing around '$op' $at\n" .
2125 # A colon needs no spaces before when it is
2126 # terminating a case value or a label.
2127 } elsif ($opv eq ':C' || $opv eq ':L') {
2128 if ($ctx =~ /Wx./) {
2129 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2132 # All the others need spaces both sides.
2133 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2136 # Ignore email addresses <foo@bar>
2138 $cc =~ /^\S+\@\S+>/) ||
2140 $ca =~ /<\S+\@\S+$/))
2146 if (($opv eq ':O' && $ca =~ /\?$/) ||
2147 ($op eq '?' && $cc =~ /^:/)) {
2152 ERROR("spaces required around that '$op' $at\n" . $hereptr);
2155 $off += length($elements[$n + 1]);
2159 # check for multiple assignments
2160 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2161 CHK("multiple assignments should be avoided\n" . $herecurr);
2164 ## # check for multiple declarations, allowing for a function declaration
2166 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2167 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2169 ## # Remove any bracketed sections to ensure we do not
2170 ## # falsly report the parameters of functions.
2172 ## while ($ln =~ s/\([^\(\)]*\)//g) {
2174 ## if ($ln =~ /,/) {
2175 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2179 #need space before brace following if, while, etc
2180 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2182 ERROR("space required before the open brace '{'\n" . $herecurr);
2185 # closing brace should have a space following it when it has anything
2187 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2188 ERROR("space required after that close brace '}'\n" . $herecurr);
2191 # check spacing on square brackets
2192 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2193 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2195 if ($line =~ /\s\]/) {
2196 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2199 # check spacing on parentheses
2200 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2201 $line !~ /for\s*\(\s+;/) {
2202 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2204 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2205 $line !~ /for\s*\(.*;\s+\)/ &&
2206 $line !~ /:\s+\)/) {
2207 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2210 # Return is not a function.
2211 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2215 # Flatten any parentheses
2216 $value =~ s/\(/ \(/g;
2217 $value =~ s/\)/\) /g;
2218 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2219 $value !~ /(?:$Ident|-?$Constant)\s*
2221 (?:$Ident|-?$Constant)/x &&
2222 $value =~ s/\([^\(\)]*\)/1/) {
2224 #print "value<$value>\n";
2225 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2226 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2228 } elsif ($spacing !~ /\s+/) {
2229 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2232 # Return of what appears to be an errno should normally be -'ve
2233 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2235 if ($name ne 'EOF' && $name ne 'ERROR') {
2236 CHK("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2240 # Need a space before open parenthesis after if, while etc
2241 if ($line=~/\b(if|while|for|switch)\(/) {
2242 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2245 # Check for illegal assignment in if conditional -- and check for trailing
2246 # statements after the conditional.
2247 if ($line =~ /do\s*(?!{)/) {
2248 my ($stat_next) = ctx_statement_block($line_nr_next,
2249 $remain_next, $off_next);
2250 $stat_next =~ s/\n./\n /g;
2251 ##print "stat<$stat> stat_next<$stat_next>\n";
2253 if ($stat_next =~ /^\s*while\b/) {
2254 # If the statement carries leading newlines,
2255 # then count those as offsets.
2257 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2259 statement_rawlines($whitespace) - 1;
2261 $suppress_whiletrailers{$line_nr_next +
2265 if (!defined $suppress_whiletrailers{$linenr} &&
2266 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2267 my ($s, $c) = ($stat, $cond);
2269 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2270 ERROR("do not use assignment in if condition\n" . $herecurr);
2273 # Find out what is on the end of the line after the
2275 substr($s, 0, length($c), '');
2277 $s =~ s/$;//g; # Remove any comments
2278 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2279 $c !~ /}\s*while\s*/)
2281 # Find out how long the conditional actually is.
2282 my @newlines = ($c =~ /\n/gs);
2283 my $cond_lines = 1 + $#newlines;
2286 $stat_real = raw_line($linenr, $cond_lines)
2287 . "\n" if ($cond_lines);
2288 if (defined($stat_real) && $cond_lines > 1) {
2289 $stat_real = "[...]\n$stat_real";
2292 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2296 # Check for bitwise tests written as boolean
2308 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2311 # if and else should not have general statements after it
2312 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2314 $s =~ s/$;//g; # Remove any comments
2315 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2316 ERROR("trailing statements should be on next line\n" . $herecurr);
2319 # if should not continue a brace
2320 if ($line =~ /}\s*if\b/) {
2321 ERROR("trailing statements should be on next line\n" .
2324 # case and default should not have general statements after them
2325 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2327 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2331 ERROR("trailing statements should be on next line\n" . $herecurr);
2334 # Check for }<nl>else {, these must be at the same
2335 # indent level to be relevant to each other.
2336 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2337 $previndent == $indent) {
2338 ERROR("else should follow close brace '}'\n" . $hereprev);
2341 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2342 $previndent == $indent) {
2343 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2345 # Find out what is on the end of the line after the
2347 substr($s, 0, length($c), '');
2350 if ($s =~ /^\s*;/) {
2351 ERROR("while should follow close brace '}'\n" . $hereprev);
2355 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2356 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2357 # print "No studly caps, use _\n";
2358 # print "$herecurr";
2362 #no spaces allowed after \ in define
2363 if ($line=~/\#\s*define.*\\\s$/) {
2364 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
2367 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2368 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2370 my $checkfile = "include/linux/$file";
2371 if (-f "$root/$checkfile" &&
2372 $realfile ne $checkfile &&
2373 $1 !~ /$allowed_asm_includes/)
2375 if ($realfile =~ m{^arch/}) {
2376 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2378 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2383 # multi-statement macros should be enclosed in a do while loop, grab the
2384 # first statement and ensure its the whole macro if its not enclosed
2385 # in a known good container
2386 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2387 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2390 my ($off, $dstat, $dcond, $rest);
2393 my $args = defined($1);
2395 # Find the end of the macro and limit our statement
2397 while ($cnt > 0 && defined $lines[$ln - 1] &&
2398 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2400 $ctx .= $rawlines[$ln - 1] . "\n";
2401 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2404 $ctx .= $rawlines[$ln - 1];
2406 ($dstat, $dcond, $ln, $cnt, $off) =
2407 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2408 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2409 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2411 # Extract the remainder of the define (if any) and
2412 # rip off surrounding spaces, and trailing \'s.
2414 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2415 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2416 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2417 $rest .= substr($lines[$ln - 1], $off) . "\n";
2423 $rest =~ s/\\\n.//g;
2427 # Clean up the original statement.
2429 substr($dstat, 0, length($dcond), '');
2431 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2434 $dstat =~ s/\\\n.//g;
2435 $dstat =~ s/^\s*//s;
2436 $dstat =~ s/\s*$//s;
2438 # Flatten any parentheses and braces
2439 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2440 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2441 $dstat =~ s/\[[^\{\}]*\]/1/)
2445 my $exceptions = qr{
2457 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2458 if ($rest ne '' && $rest ne ',') {
2459 if ($rest !~ /while\s*\(/ &&
2460 $dstat !~ /$exceptions/)
2462 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2465 } elsif ($ctx !~ /;/) {
2467 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2468 $dstat !~ /$exceptions/ &&
2469 $dstat !~ /^\.$Ident\s*=/ &&
2470 $dstat =~ /$Operators/)
2472 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2477 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2478 # all assignments may have only one of the following with an assignment:
2481 # VMLINUX_SYMBOL(...)
2482 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2483 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2486 # check for missing bracing round if etc
2487 if ($line =~ /(^.*)\bif\b/ && $line !~ /\#\s*if/) {
2488 my ($level, $endln, @chunks) =
2489 ctx_statement_full($linenr, $realcnt, 1);
2490 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2491 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2492 if ($#chunks >= 0 && $level == 0) {
2495 my $herectx = $here . "\n";
2496 my $ln = $linenr - 1;
2497 for my $chunk (@chunks) {
2498 my ($cond, $block) = @{$chunk};
2500 # If the condition carries leading newlines, then count those as offsets.
2501 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2502 my $offset = statement_rawlines($whitespace) - 1;
2504 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2506 # We have looked at and allowed this specific line.
2507 $suppress_ifbraces{$ln + $offset} = 1;
2509 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2510 $ln += statement_rawlines($block) - 1;
2512 substr($block, 0, length($cond), '');
2514 $seen++ if ($block =~ /^\s*{/);
2516 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2517 if (statement_lines($cond) > 1) {
2518 #print "APW: ALLOWED: cond<$cond>\n";
2521 if ($block =~/\b(?:if|for|while)\b/) {
2522 #print "APW: ALLOWED: block<$block>\n";
2525 if (statement_block_size($block) > 1) {
2526 #print "APW: ALLOWED: lines block<$block>\n";
2530 if ($seen != ($#chunks + 1)) {
2531 WARN("braces {} are necessary for all arms of this statement\n" . $herectx);
2535 if (!defined $suppress_ifbraces{$linenr - 1} &&
2536 $line =~ /\b(if|while|for|else)\b/ &&
2537 $line !~ /\#\s*if/ &&
2538 $line !~ /\#\s*else/) {
2541 # Check the pre-context.
2542 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2543 #print "APW: ALLOWED: pre<$1>\n";
2547 my ($level, $endln, @chunks) =
2548 ctx_statement_full($linenr, $realcnt, $-[0]);
2550 # Check the condition.
2551 my ($cond, $block) = @{$chunks[0]};
2552 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2553 if (defined $cond) {
2554 substr($block, 0, length($cond), '');
2556 if (statement_lines($cond) > 1) {
2557 #print "APW: ALLOWED: cond<$cond>\n";
2560 if ($block =~/\b(?:if|for|while)\b/) {
2561 #print "APW: ALLOWED: block<$block>\n";
2564 if (statement_block_size($block) > 1) {
2565 #print "APW: ALLOWED: lines block<$block>\n";
2568 # Check the post-context.
2569 if (defined $chunks[1]) {
2570 my ($cond, $block) = @{$chunks[1]};
2571 if (defined $cond) {
2572 substr($block, 0, length($cond), '');
2574 if ($block =~ /^\s*\{/) {
2575 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2579 print "DCS: level=$level block<$block> allowed=$allowed\n"
2581 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
2582 my $herectx = $here . "\n";;
2583 my $cnt = statement_rawlines($block);
2585 for (my $n = 0; $n < $cnt; $n++) {
2586 $herectx .= raw_line($linenr, $n) . "\n";;
2589 WARN("braces {} are necessary even for single statement blocks\n" . $herectx);
2593 # don't include deprecated include files (uses RAW line)
2594 for my $inc (@dep_includes) {
2595 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2596 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2600 # don't use deprecated functions
2601 for my $func (@dep_functions) {
2602 if ($line =~ /\b$func\b/) {
2603 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2607 # no volatiles please
2608 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2609 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2610 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2613 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2614 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2615 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2619 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2620 CHK("if this code is redundant consider removing it\n" .
2624 # check for needless kfree() checks
2625 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2627 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2628 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2631 # check for needless usb_free_urb() checks
2632 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2634 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2635 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2639 # prefer usleep_range over udelay
2640 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2641 # ignore udelay's < 10, however
2642 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2643 CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2647 # warn about unexpectedly long msleep's
2648 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2650 WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2654 # warn about #ifdefs in C files
2655 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2656 # print "#ifdef in C files should be avoided\n";
2657 # print "$herecurr";
2661 # warn about spacing in #ifdefs
2662 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2663 ERROR("exactly one space required after that #$1\n" . $herecurr);
2666 # check for spinlock_t definitions without a comment.
2667 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2668 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
2670 if (!ctx_has_comment($first_line, $linenr)) {
2671 CHK("$1 definition without comment\n" . $herecurr);
2674 # check for memory barriers without a comment.
2675 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2676 if (!ctx_has_comment($first_line, $linenr)) {
2677 CHK("memory barrier without comment\n" . $herecurr);
2680 # check of hardware specific defines
2681 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2682 CHK("architecture specific defines should be avoided\n" . $herecurr);
2685 # Check that the storage class is at the beginning of a declaration
2686 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2687 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2690 # check the location of the inline attribute, that it is between
2691 # storage class and type.
2692 if ($line =~ /\b$Type\s+$Inline\b/ ||
2693 $line =~ /\b$Inline\s+$Storage\b/) {
2694 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2697 # Check for __inline__ and __inline, prefer inline
2698 if ($line =~ /\b(__inline__|__inline)\b/) {
2699 WARN("plain inline is preferred over $1\n" . $herecurr);
2702 # check for sizeof(&)
2703 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2704 WARN("sizeof(& should be avoided\n" . $herecurr);
2707 # check for new externs in .c files.
2708 if ($realfile =~ /\.c$/ && defined $stat &&
2709 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2711 my $function_name = $1;
2712 my $paren_space = $2;
2715 if (defined $cond) {
2716 substr($s, 0, length($cond), '');
2718 if ($s =~ /^\s*;/ &&
2719 $function_name ne 'uninitialized_var')
2721 WARN("externs should be avoided in .c files\n" . $herecurr);
2724 if ($paren_space =~ /\n/) {
2725 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2728 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2729 $stat =~ /^.\s*extern\s+/)
2731 WARN("externs should be avoided in .c files\n" . $herecurr);
2734 # checks for new __setup's
2735 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2738 if (!grep(/$name/, @setup_docs)) {
2739 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2743 # check for pointless casting of kmalloc return
2744 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2745 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2748 # check for gcc specific __FUNCTION__
2749 if ($line =~ /__FUNCTION__/) {
2750 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2753 # check for semaphores used as mutexes
2754 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2755 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2757 # check for semaphores used as mutexes
2758 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2759 WARN("consider using a completion\n" . $herecurr);
2762 # recommend strict_strto* over simple_strto*
2763 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2764 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2766 # check for __initcall(), use device_initcall() explicitly please
2767 if ($line =~ /^.\s*__initcall\s*\(/) {
2768 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2770 # check for various ops structs, ensure they are const.
2771 my $struct_ops = qr{acpi_dock_ops|
2772 address_space_operations|
2774 block_device_operations|
2779 file_lock_operations|
2789 lock_manager_operations|
2795 pipe_buf_operations|
2796 platform_hibernation_ops|
2797 platform_suspend_ops|
2802 soc_pcmcia_socket_ops|
2808 if ($line !~ /\bconst\b/ &&
2809 $line =~ /\bstruct\s+($struct_ops)\b/) {
2810 WARN("struct $1 should normally be const\n" .
2814 # use of NR_CPUS is usually wrong
2815 # ignore definitions of NR_CPUS and usage to define arrays as likely right
2816 if ($line =~ /\bNR_CPUS\b/ &&
2817 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2818 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2819 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2820 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2821 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2823 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2826 # check for %L{u,d,i} in strings
2828 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2829 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2830 $string =~ s/%%/__/g;
2831 if ($string =~ /(?<!%)%L[udi]/) {
2832 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2837 # whine mightly about in_atomic
2838 if ($line =~ /\bin_atomic\s*\(/) {
2839 if ($realfile =~ m@^drivers/@) {
2840 ERROR("do not use in_atomic in drivers\n" . $herecurr);
2841 } elsif ($realfile !~ m@^kernel/@) {
2842 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2846 # check for lockdep_set_novalidate_class
2847 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
2848 $line =~ /__lockdep_no_validate__\s*\)/ ) {
2849 if ($realfile !~ m@^kernel/lockdep@ &&
2850 $realfile !~ m@^include/linux/lockdep@ &&
2851 $realfile !~ m@^drivers/base/core@) {
2852 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
2856 # QEMU specific tests
2857 if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
2858 WARN("use QEMU instead of Qemu or QEmu\n" . $herecurr);
2862 # If we have no input at all, then there is nothing to report on
2863 # so just keep quiet.
2864 if ($#rawlines == -1) {
2868 # In mailback mode only produce a report in the negative, for
2869 # things that appear to be patches.
2870 if ($mailback && ($clean == 1 || !$is_patch)) {
2874 # This is not a patch, and we are are in 'no-patch' mode so
2876 if (!$chk_patch && !$is_patch) {
2881 ERROR("Does not appear to be a unified-diff format patch\n");
2883 if ($is_patch && $chk_signoff && $signoff == 0) {
2884 ERROR("Missing Signed-off-by: line(s)\n");
2887 print report_dump();
2888 if ($summary && !($clean == 1 && $quiet == 1)) {
2889 print "$filename " if ($summary_file);
2890 print "total: $cnt_error errors, $cnt_warn warnings, " .
2891 (($check)? "$cnt_chk checks, " : "") .
2892 "$cnt_lines lines checked\n";
2893 print "\n" if ($quiet == 0);
2897 # If there were whitespace errors which cleanpatch can fix
2898 # then suggest that.
2899 # if ($rpt_cleaners) {
2900 # print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2901 # print " scripts/cleanfile\n\n";
2905 if ($clean == 1 && $quiet == 0) {
2906 print "$vname has no obvious style problems and is ready for submission.\n"
2908 if ($clean == 0 && $quiet == 0) {
2909 print "$vname has style problems, please review. If any of these errors\n";
2910 print "are false positives report them to the maintainer, see\n";
2911 print "CHECKPATCH in MAINTAINERS.\n";