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 my $dbg_adv_checking = 0;
102 for my $key (keys %debug) {
104 eval "\${dbg_$key} = '$debug{$key}';";
108 my $rpt_cleaners = 0;
117 if (!top_of_kernel_tree($root)) {
118 die "$P: $root: --root does not point at a valid tree\n";
121 if (top_of_kernel_tree('.')) {
123 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
124 top_of_kernel_tree($1)) {
129 if (!defined $root) {
130 print "Must be run from the top-level dir. of a kernel tree\n";
135 my $emitted_corrupt = 0;
138 [A-Za-z_][A-Za-z\d_]*
139 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
141 our $Storage = qr{extern|static|asmlinkage};
153 # Notes to $Attribute:
154 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
173 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
174 ____cacheline_aligned|
175 ____cacheline_aligned_in_smp|
176 ____cacheline_internodealigned_in_smp|
180 our $Inline = qr{inline|__always_inline|noinline};
181 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
182 our $Lval = qr{$Ident(?:$Member)*};
184 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
185 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
186 our $Compare = qr{<=|>=|==|!=|<|>};
190 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
198 [\x09\x0A\x0D\x20-\x7E] # ASCII
199 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
200 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
201 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
202 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
203 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
204 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
205 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
208 our $typeTypedefs = qr{(?x:
209 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
213 our $logFunctions = qr{(?x:
215 pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)|
216 (dev|netdev|netif)_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)|
223 qr{(?:unsigned\s+)?char},
224 qr{(?:unsigned\s+)?short},
225 qr{(?:unsigned\s+)?int},
226 qr{(?:unsigned\s+)?long},
227 qr{(?:unsigned\s+)?long\s+int},
228 qr{(?:unsigned\s+)?long\s+long},
229 qr{(?:unsigned\s+)?long\s+long\s+int},
238 qr{${Ident}_handler},
239 qr{${Ident}_handler_fn},
241 our @modifierList = (
245 our $allowed_asm_includes = qr{(?x:
249 # memory.h: ARM has a custom one
252 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
253 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
254 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
256 (?:$Modifier\s+|const\s+)*
258 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
262 (?:\s+$Modifier|\s+const)*
266 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
267 (?:\s+$Inline|\s+$Modifier)*
269 $Declare = qr{(?:$Storage\s+)?$Type};
273 $chk_signoff = 0 if ($file);
275 my @dep_includes = ();
276 my @dep_functions = ();
277 my $removal = "Documentation/feature-removal-schedule.txt";
278 if ($tree && -f "$root/$removal") {
279 open(my $REMOVE, '<', "$root/$removal") ||
280 die "$P: $removal: open failed - $!\n";
282 if (/^Check:\s+(.*\S)/) {
283 for my $entry (split(/[, ]+/, $1)) {
284 if ($entry =~ m@include/(.*)@) {
285 push(@dep_includes, $1);
287 } elsif ($entry !~ m@/@) {
288 push(@dep_functions, $entry);
299 for my $filename (@ARGV) {
302 open($FILE, '-|', "diff -u /dev/null $filename") ||
303 die "$P: $filename: diff failed - $!\n";
304 } elsif ($filename eq '-') {
305 open($FILE, '<&STDIN');
307 open($FILE, '<', "$filename") ||
308 die "$P: $filename: open failed - $!\n";
310 if ($filename eq '-') {
311 $vname = 'Your patch';
320 if (!process($filename)) {
329 sub top_of_kernel_tree {
333 "COPYING", "MAINTAINERS", "Makefile",
334 "README", "docs", "VERSION",
338 foreach my $check (@tree_check) {
339 if (! -e $root . '/' . $check) {
351 for my $c (split(//, $str)) {
355 for (; ($n % 8) != 0; $n++) {
367 (my $res = shift) =~ tr/\t/ /c;
374 # Drop the diff line leader and expand tabs
376 $line = expand_tabs($line);
378 # Pick the indent from the front of the line.
379 my ($white) = ($line =~ /^(\s*)/);
381 return (length($line), length($white));
384 my $sanitise_quote = '';
386 sub sanitise_line_reset {
387 my ($in_comment) = @_;
390 $sanitise_quote = '*/';
392 $sanitise_quote = '';
405 # Always copy over the diff marker.
406 $res = substr($line, 0, 1);
408 for ($off = 1; $off < length($line); $off++) {
409 $c = substr($line, $off, 1);
411 # Comments we are wacking completly including the begin
412 # and end, all to $;.
413 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
414 $sanitise_quote = '*/';
416 substr($res, $off, 2, "$;$;");
420 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
421 $sanitise_quote = '';
422 substr($res, $off, 2, "$;$;");
426 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
427 $sanitise_quote = '//';
429 substr($res, $off, 2, $sanitise_quote);
434 # A \ in a string means ignore the next character.
435 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
437 substr($res, $off, 2, 'XX');
442 if ($c eq "'" || $c eq '"') {
443 if ($sanitise_quote eq '') {
444 $sanitise_quote = $c;
446 substr($res, $off, 1, $c);
448 } elsif ($sanitise_quote eq $c) {
449 $sanitise_quote = '';
453 #print "c<$c> SQ<$sanitise_quote>\n";
454 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
455 substr($res, $off, 1, $;);
456 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
457 substr($res, $off, 1, $;);
458 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
459 substr($res, $off, 1, 'X');
461 substr($res, $off, 1, $c);
465 if ($sanitise_quote eq '//') {
466 $sanitise_quote = '';
469 # The pathname on a #include may be surrounded by '<' and '>'.
470 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
471 my $clean = 'X' x length($1);
472 $res =~ s@\<.*\>@<$clean>@;
474 # The whole of a #error is a string.
475 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
476 my $clean = 'X' x length($1);
477 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
483 sub ctx_statement_block {
484 my ($linenr, $remain, $off) = @_;
485 my $line = $linenr - 1;
502 @stack = (['', 0]) if ($#stack == -1);
504 #warn "CSB: blk<$blk> remain<$remain>\n";
505 # If we are about to drop off the end, pull in more
508 for (; $remain > 0; $line++) {
509 last if (!defined $lines[$line]);
510 next if ($lines[$line] =~ /^-/);
513 $blk .= $lines[$line] . "\n";
518 # Bail if there is no further context.
519 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
525 $c = substr($blk, $off, 1);
526 $remainder = substr($blk, $off);
528 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
530 # Handle nested #if/#else.
531 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
532 push(@stack, [ $type, $level ]);
533 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
534 ($type, $level) = @{$stack[$#stack - 1]};
535 } elsif ($remainder =~ /^#\s*endif\b/) {
536 ($type, $level) = @{pop(@stack)};
539 # Statement ends at the ';' or a close '}' at the
541 if ($level == 0 && $c eq ';') {
545 # An else is really a conditional as long as its not else if
546 if ($level == 0 && $coff_set == 0 &&
547 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
548 $remainder =~ /^(else)(?:\s|{)/ &&
549 $remainder !~ /^else\s+if\b/) {
550 $coff = $off + length($1) - 1;
552 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
553 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
556 if (($type eq '' || $type eq '(') && $c eq '(') {
560 if ($type eq '(' && $c eq ')') {
562 $type = ($level != 0)? '(' : '';
564 if ($level == 0 && $coff < $soff) {
567 #warn "CSB: mark coff<$coff>\n";
570 if (($type eq '' || $type eq '{') && $c eq '{') {
574 if ($type eq '{' && $c eq '}') {
576 $type = ($level != 0)? '{' : '';
579 if (substr($blk, $off + 1, 1) eq ';') {
587 # We are truly at the end, so shuffle to the next line.
594 my $statement = substr($blk, $soff, $off - $soff + 1);
595 my $condition = substr($blk, $soff, $coff - $soff + 1);
597 #warn "STATEMENT<$statement>\n";
598 #warn "CONDITION<$condition>\n";
600 #print "coff<$coff> soff<$off> loff<$loff>\n";
602 return ($statement, $condition,
603 $line, $remain + 1, $off - $loff + 1, $level);
606 sub statement_lines {
609 # Strip the diff line prefixes and rip blank lines at start and end.
610 $stmt =~ s/(^|\n)./$1/g;
614 my @stmt_lines = ($stmt =~ /\n/g);
616 return $#stmt_lines + 2;
619 sub statement_rawlines {
622 my @stmt_lines = ($stmt =~ /\n/g);
624 return $#stmt_lines + 2;
627 sub statement_block_size {
630 $stmt =~ s/(^|\n)./$1/g;
636 my @stmt_lines = ($stmt =~ /\n/g);
637 my @stmt_statements = ($stmt =~ /;/g);
639 my $stmt_lines = $#stmt_lines + 2;
640 my $stmt_statements = $#stmt_statements + 1;
642 if ($stmt_lines > $stmt_statements) {
645 return $stmt_statements;
649 sub ctx_statement_full {
650 my ($linenr, $remain, $off) = @_;
651 my ($statement, $condition, $level);
655 # Grab the first conditional/block pair.
656 ($statement, $condition, $linenr, $remain, $off, $level) =
657 ctx_statement_block($linenr, $remain, $off);
658 #print "F: c<$condition> s<$statement> remain<$remain>\n";
659 push(@chunks, [ $condition, $statement ]);
660 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
661 return ($level, $linenr, @chunks);
664 # Pull in the following conditional/block pairs and see if they
665 # could continue the statement.
667 ($statement, $condition, $linenr, $remain, $off, $level) =
668 ctx_statement_block($linenr, $remain, $off);
669 #print "C: c<$condition> s<$statement> remain<$remain>\n";
670 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
672 push(@chunks, [ $condition, $statement ]);
675 return ($level, $linenr, @chunks);
679 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
681 my $start = $linenr - 1;
688 my @stack = ($level);
689 for ($line = $start; $remain > 0; $line++) {
690 next if ($rawlines[$line] =~ /^-/);
693 $blk .= $rawlines[$line];
695 # Handle nested #if/#else.
696 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
697 push(@stack, $level);
698 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
699 $level = $stack[$#stack - 1];
700 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
701 $level = pop(@stack);
704 foreach my $c (split(//, $lines[$line])) {
705 ##print "C<$c>L<$level><$open$close>O<$off>\n";
711 if ($c eq $close && $level > 0) {
713 last if ($level == 0);
714 } elsif ($c eq $open) {
719 if (!$outer || $level <= 1) {
720 push(@res, $rawlines[$line]);
723 last if ($level == 0);
726 return ($level, @res);
728 sub ctx_block_outer {
729 my ($linenr, $remain) = @_;
731 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
735 my ($linenr, $remain) = @_;
737 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
741 my ($linenr, $remain, $off) = @_;
743 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
746 sub ctx_block_level {
747 my ($linenr, $remain) = @_;
749 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
751 sub ctx_statement_level {
752 my ($linenr, $remain, $off) = @_;
754 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
757 sub ctx_locate_comment {
758 my ($first_line, $end_line) = @_;
760 # Catch a comment on the end of the line itself.
761 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
762 return $current_comment if (defined $current_comment);
764 # Look through the context and try and figure out if there is a
767 $current_comment = '';
768 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
769 my $line = $rawlines[$linenr - 1];
771 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
774 if ($line =~ m@/\*@) {
777 if (!$in_comment && $current_comment ne '') {
778 $current_comment = '';
780 $current_comment .= $line . "\n" if ($in_comment);
781 if ($line =~ m@\*/@) {
786 chomp($current_comment);
787 return($current_comment);
789 sub ctx_has_comment {
790 my ($first_line, $end_line) = @_;
791 my $cmt = ctx_locate_comment($first_line, $end_line);
793 ##print "LINE: $rawlines[$end_line - 1 ]\n";
794 ##print "CMMT: $cmt\n";
800 my ($linenr, $cnt) = @_;
802 my $offset = $linenr - 1;
807 $line = $rawlines[$offset++];
808 next if (defined($line) && $line =~ /^-/);
820 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
823 $coded = sprintf("^%c", unpack('C', $2) + 64);
832 my $av_preprocessor = 0;
838 $av_preprocessor = 0;
840 @av_paren_type = ('E');
841 $av_pend_colon = 'O';
844 sub annotate_values {
845 my ($stream, $type) = @_;
848 my $var = '_' x length($stream);
851 print "$stream\n" if ($dbg_values > 1);
853 while (length($cur)) {
854 @av_paren_type = ('E') if ($#av_paren_type < 0);
855 print " <" . join('', @av_paren_type) .
856 "> <$type> <$av_pending>" if ($dbg_values > 1);
857 if ($cur =~ /^(\s+)/o) {
858 print "WS($1)\n" if ($dbg_values > 1);
859 if ($1 =~ /\n/ && $av_preprocessor) {
860 $type = pop(@av_paren_type);
861 $av_preprocessor = 0;
864 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
865 print "CAST($1)\n" if ($dbg_values > 1);
866 push(@av_paren_type, $type);
869 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
870 print "DECLARE($1)\n" if ($dbg_values > 1);
873 } elsif ($cur =~ /^($Modifier)\s*/) {
874 print "MODIFIER($1)\n" if ($dbg_values > 1);
877 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
878 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
879 $av_preprocessor = 1;
880 push(@av_paren_type, $type);
886 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
887 print "UNDEF($1)\n" if ($dbg_values > 1);
888 $av_preprocessor = 1;
889 push(@av_paren_type, $type);
891 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
892 print "PRE_START($1)\n" if ($dbg_values > 1);
893 $av_preprocessor = 1;
895 push(@av_paren_type, $type);
896 push(@av_paren_type, $type);
899 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
900 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
901 $av_preprocessor = 1;
903 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
907 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
908 print "PRE_END($1)\n" if ($dbg_values > 1);
910 $av_preprocessor = 1;
912 # Assume all arms of the conditional end as this
913 # one does, and continue as if the #endif was not here.
915 push(@av_paren_type, $type);
918 } elsif ($cur =~ /^(\\\n)/o) {
919 print "PRECONT($1)\n" if ($dbg_values > 1);
921 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
922 print "ATTR($1)\n" if ($dbg_values > 1);
926 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
927 print "SIZEOF($1)\n" if ($dbg_values > 1);
933 } elsif ($cur =~ /^(if|while|for)\b/o) {
934 print "COND($1)\n" if ($dbg_values > 1);
938 } elsif ($cur =~/^(case)/o) {
939 print "CASE($1)\n" if ($dbg_values > 1);
940 $av_pend_colon = 'C';
943 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
944 print "KEYWORD($1)\n" if ($dbg_values > 1);
947 } elsif ($cur =~ /^(\()/o) {
948 print "PAREN('$1')\n" if ($dbg_values > 1);
949 push(@av_paren_type, $av_pending);
953 } elsif ($cur =~ /^(\))/o) {
954 my $new_type = pop(@av_paren_type);
955 if ($new_type ne '_') {
957 print "PAREN('$1') -> $type\n"
958 if ($dbg_values > 1);
960 print "PAREN('$1')\n" if ($dbg_values > 1);
963 } elsif ($cur =~ /^($Ident)\s*\(/o) {
964 print "FUNC($1)\n" if ($dbg_values > 1);
968 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
969 if (defined $2 && $type eq 'C' || $type eq 'T') {
970 $av_pend_colon = 'B';
971 } elsif ($type eq 'E') {
972 $av_pend_colon = 'L';
974 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
977 } elsif ($cur =~ /^($Ident|$Constant)/o) {
978 print "IDENT($1)\n" if ($dbg_values > 1);
981 } elsif ($cur =~ /^($Assignment)/o) {
982 print "ASSIGN($1)\n" if ($dbg_values > 1);
985 } elsif ($cur =~/^(;|{|})/) {
986 print "END($1)\n" if ($dbg_values > 1);
988 $av_pend_colon = 'O';
990 } elsif ($cur =~/^(,)/) {
991 print "COMMA($1)\n" if ($dbg_values > 1);
994 } elsif ($cur =~ /^(\?)/o) {
995 print "QUESTION($1)\n" if ($dbg_values > 1);
998 } elsif ($cur =~ /^(:)/o) {
999 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1001 substr($var, length($res), 1, $av_pend_colon);
1002 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1007 $av_pend_colon = 'O';
1009 } elsif ($cur =~ /^(\[)/o) {
1010 print "CLOSE($1)\n" if ($dbg_values > 1);
1013 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1016 print "OPV($1)\n" if ($dbg_values > 1);
1023 substr($var, length($res), 1, $variant);
1026 } elsif ($cur =~ /^($Operators)/o) {
1027 print "OP($1)\n" if ($dbg_values > 1);
1028 if ($1 ne '++' && $1 ne '--') {
1032 } elsif ($cur =~ /(^.)/o) {
1033 print "C($1)\n" if ($dbg_values > 1);
1036 $cur = substr($cur, length($1));
1037 $res .= $type x length($1);
1041 return ($res, $var);
1045 my ($possible, $line) = @_;
1046 my $notPermitted = qr{(?:
1061 ^(?:typedef|struct|enum)\b
1063 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1064 if ($possible !~ $notPermitted) {
1065 # Check for modifiers.
1066 $possible =~ s/\s*$Storage\s*//g;
1067 $possible =~ s/\s*$Sparse\s*//g;
1068 if ($possible =~ /^\s*$/) {
1070 } elsif ($possible =~ /\s/) {
1071 $possible =~ s/\s*$Type\s*//g;
1072 for my $modifier (split(' ', $possible)) {
1073 if ($modifier !~ $notPermitted) {
1074 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1075 push(@modifierList, $modifier);
1080 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1081 push(@typeList, $possible);
1085 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1092 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1095 my $line = $prefix . $_[0];
1097 $line = (split('\n', $line))[0] . "\n" if ($terse);
1099 push(our @report, $line);
1107 if (report("ERROR: $_[0]\n")) {
1113 if (report("WARNING: $_[0]\n")) {
1119 if ($check && report("CHECK: $_[0]\n")) {
1125 sub check_absolute_file {
1126 my ($absolute, $herecurr) = @_;
1127 my $file = $absolute;
1129 ##print "absolute<$absolute>\n";
1131 # See if any suffix of this path is a path within the tree.
1132 while ($file =~ s@^[^/]*/@@) {
1133 if (-f "$root/$file") {
1134 ##print "file<$file>\n";
1142 # It is, so see if the prefix is acceptable.
1143 my $prefix = $absolute;
1144 substr($prefix, -length($file)) = '';
1146 ##print "prefix<$prefix>\n";
1147 if ($prefix ne ".../") {
1148 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1153 my $filename = shift;
1159 my $stashrawline="";
1176 # Trace the real file/line as we go.
1182 my $comment_edge = 0;
1186 my $prev_values = 'E';
1189 my %suppress_ifbraces;
1190 my %suppress_whiletrailers;
1191 my %suppress_export;
1193 # Pre-scan the patch sanitizing the lines.
1194 # Pre-scan the patch looking for any __setup documentation.
1196 my @setup_docs = ();
1199 sanitise_line_reset();
1201 foreach my $rawline (@rawlines) {
1205 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1207 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1212 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1221 # Guestimate if this is a continuing comment. Run
1222 # the context looking for a comment "edge". If this
1223 # edge is a close comment then we must be in a comment
1227 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1228 next if (defined $rawlines[$ln - 1] &&
1229 $rawlines[$ln - 1] =~ /^-/);
1231 #print "RAW<$rawlines[$ln - 1]>\n";
1232 last if (!defined $rawlines[$ln - 1]);
1233 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1234 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1239 if (defined $edge && $edge eq '*/') {
1243 # Guestimate if this is a continuing comment. If this
1244 # is the start of a diff block and this line starts
1245 # ' *' then it is very likely a comment.
1246 if (!defined $edge &&
1247 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1252 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1253 sanitise_line_reset($in_comment);
1255 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1256 # Standardise the strings and chars within the input to
1257 # simplify matching -- only bother with positive lines.
1258 $line = sanitise_line($rawline);
1260 push(@lines, $line);
1263 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1268 #print "==>$rawline\n";
1269 #print "-->$line\n";
1271 if ($setup_docs && $line =~ /^\+/) {
1272 push(@setup_docs, $line);
1280 foreach my $line (@lines) {
1283 my $rawline = $rawlines[$linenr - 1];
1285 #extract the line range in the file after the patch is applied
1286 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1288 $first_line = $linenr + 1;
1298 %suppress_ifbraces = ();
1299 %suppress_whiletrailers = ();
1300 %suppress_export = ();
1303 # track the line number as we move through the hunk, note that
1304 # new versions of GNU diff omit the leading space on completely
1305 # blank context lines so we need to count that too.
1306 } elsif ($line =~ /^( |\+|$)/) {
1308 $realcnt-- if ($realcnt != 0);
1310 # Measure the line length and indent.
1311 ($length, $indent) = line_stats($rawline);
1313 # Track the previous line.
1314 ($prevline, $stashline) = ($stashline, $line);
1315 ($previndent, $stashindent) = ($stashindent, $indent);
1316 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1318 #warn "line<$line>\n";
1320 } elsif ($realcnt == 1) {
1324 my $hunk_line = ($realcnt != 0);
1326 #make up the handle for any error we report on this line
1327 $prefix = "$filename:$realline: " if ($emacs && $file);
1328 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1330 $here = "#$linenr: " if (!$file);
1331 $here = "#$realline: " if ($file);
1333 # extract the filename as it passes
1334 if ($line =~ /^diff --git.*?(\S+)$/) {
1336 $realfile =~ s@^([^/]*)/@@;
1338 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1340 $realfile =~ s@^([^/]*)/@@;
1343 if (!$file && $tree && $p1_prefix ne '' &&
1344 -e "$root/$p1_prefix") {
1345 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1348 if ($realfile =~ m@^include/asm/@) {
1349 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1354 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1356 my $hereline = "$here\n$rawline\n";
1357 my $herecurr = "$here\n$rawline\n";
1358 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1360 $cnt_lines++ if ($realcnt != 0);
1362 # Check for incorrect file permissions
1363 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1364 my $permhere = $here . "FILE: $realfile\n";
1365 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1366 ERROR("do not set execute permissions for source files\n" . $permhere);
1370 #check the patch for a signoff:
1371 if ($line =~ /^\s*signed-off-by:/i) {
1372 # This is a signoff, if ugly, so do not double report.
1374 if (!($line =~ /^\s*Signed-off-by:/)) {
1375 WARN("Signed-off-by: is the preferred form\n" .
1378 if ($line =~ /^\s*signed-off-by:\S/i) {
1379 WARN("space required after Signed-off-by:\n" .
1384 # Check for wrappage within a valid hunk of the file
1385 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1386 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1387 $herecurr) if (!$emitted_corrupt++);
1390 # Check for absolute kernel paths.
1392 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1395 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1396 check_absolute_file($1, $herecurr)) {
1399 check_absolute_file($file, $herecurr);
1404 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1405 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1406 $rawline !~ m/^$UTF8*$/) {
1407 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1409 my $blank = copy_spacing($rawline);
1410 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1411 my $hereptr = "$hereline$ptr\n";
1413 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1416 # ignore non-hunk lines and lines being removed
1417 next if (!$hunk_line || $line =~ /^-/);
1419 #trailing whitespace
1420 if ($line =~ /^\+.*\015/) {
1421 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1422 ERROR("DOS line endings\n" . $herevet);
1424 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1425 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1426 ERROR("trailing whitespace\n" . $herevet);
1430 # check for Kconfig help text having a real description
1431 # Only applies when adding the entry originally, after that we do not have
1432 # sufficient context to determine whether it is indeed long enough.
1433 if ($realfile =~ /Kconfig/ &&
1434 $line =~ /\+\s*(?:---)?help(?:---)?$/) {
1437 my $ln = $linenr + 1;
1440 while ($cnt > 0 && defined $lines[$ln - 1]) {
1441 $f = $lines[$ln - 1];
1442 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1443 $is_end = $lines[$ln - 1] =~ /^\+/;
1446 next if ($f =~ /^-/);
1450 next if ($f =~ /^$/);
1451 if ($f =~ /^\s*config\s/) {
1457 WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
1458 #print "is_end<$is_end> length<$length>\n";
1461 # check we are in a valid source file if not then ignore this hunk
1462 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1465 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1466 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1467 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ ||
1468 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1471 WARN("line over 80 characters\n" . $herecurr);
1474 # check for spaces before a quoted newline
1475 if ($rawline =~ /^.*\".*\s\\n/) {
1476 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1479 # check for adding lines without a newline.
1480 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1481 WARN("adding a line without newline at end of file\n" . $herecurr);
1484 # Blackfin: use hi/lo macros
1485 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1486 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1487 my $herevet = "$here\n" . cat_vet($line) . "\n";
1488 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1490 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1491 my $herevet = "$here\n" . cat_vet($line) . "\n";
1492 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1496 # check we are in a valid source file C or perl if not then ignore this hunk
1497 next if ($realfile !~ /\.(h|c|pl)$/);
1499 # in QEMU, no tabs are allowed
1500 if ($rawline =~ /^\+.*\t/) {
1501 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1502 ERROR("code indent should never use tabs\n" . $herevet);
1506 # check we are in a valid C source file if not then ignore this hunk
1507 next if ($realfile !~ /\.(h|c)$/);
1509 # check for RCS/CVS revision markers
1510 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1511 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1514 # Blackfin: don't use __builtin_bfin_[cs]sync
1515 if ($line =~ /__builtin_bfin_csync/) {
1516 my $herevet = "$here\n" . cat_vet($line) . "\n";
1517 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1519 if ($line =~ /__builtin_bfin_ssync/) {
1520 my $herevet = "$here\n" . cat_vet($line) . "\n";
1521 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1524 # Check for potential 'bare' types
1525 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1527 if ($realcnt && $line =~ /.\s*\S/) {
1528 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1529 ctx_statement_block($linenr, $realcnt, 0);
1530 $stat =~ s/\n./\n /g;
1531 $cond =~ s/\n./\n /g;
1533 # Find the real next line.
1534 $realline_next = $line_nr_next;
1535 if (defined $realline_next &&
1536 (!defined $lines[$realline_next - 1] ||
1537 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1544 # Ignore goto labels.
1545 if ($s =~ /$Ident:\*$/s) {
1547 # Ignore functions being called
1548 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1550 } elsif ($s =~ /^.\s*else\b/s) {
1552 # declarations always start with types
1553 } 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) {
1556 possible($type, "A:" . $s);
1558 # definitions in global scope can only start with types
1559 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1560 possible($1, "B:" . $s);
1563 # any (foo ... *) is a pointer cast, and foo is a type
1564 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1565 possible($1, "C:" . $s);
1568 # Check for any sort of function declaration.
1569 # int foo(something bar, other baz);
1570 # void (*store_gdt)(x86_descr_ptr *);
1571 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1572 my ($name_len) = length($1);
1575 substr($ctx, 0, $name_len + 1, '');
1576 $ctx =~ s/\)[^\)]*$//;
1578 for my $arg (split(/\s*,\s*/, $ctx)) {
1579 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1581 possible($1, "D:" . $s);
1589 # Checks which may be anchored in the context.
1592 # Check for switch () and associated case and default
1593 # statements should be at the same indent.
1594 if ($line=~/\bswitch\s*\(.*\)/) {
1597 my @ctx = ctx_block_outer($linenr, $realcnt);
1599 for my $ctx (@ctx) {
1600 my ($clen, $cindent) = line_stats($ctx);
1601 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1602 $indent != $cindent) {
1603 $err .= "$sep$ctx\n";
1610 ERROR("switch and case should be at the same indent\n$hereline$err");
1614 # if/while/etc brace do not go on next line, unless defining a do while loop,
1615 # or if that brace on the next line is for something else
1616 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1617 my $pre_ctx = "$1$2";
1619 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1620 my $ctx_cnt = $realcnt - $#ctx - 1;
1621 my $ctx = join("\n", @ctx);
1623 my $ctx_ln = $linenr;
1624 my $ctx_skip = $realcnt;
1626 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1627 defined $lines[$ctx_ln - 1] &&
1628 $lines[$ctx_ln - 1] =~ /^-/)) {
1629 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1630 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1634 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1635 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1637 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1638 ERROR("that open brace { should be on the previous line\n" .
1639 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1641 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1642 $ctx =~ /\)\s*\;\s*$/ &&
1643 defined $lines[$ctx_ln - 1])
1645 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1646 if ($nindent > $indent) {
1647 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1648 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1653 # Check relative indent for conditionals and blocks.
1654 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1655 my ($s, $c) = ($stat, $cond);
1657 substr($s, 0, length($c), '');
1659 # Make sure we remove the line prefixes as we have
1660 # none on the first line, and are going to readd them
1664 # Find out how long the conditional actually is.
1665 my @newlines = ($c =~ /\n/gs);
1666 my $cond_lines = 1 + $#newlines;
1668 # We want to check the first line inside the block
1669 # starting at the end of the conditional, so remove:
1670 # 1) any blank line termination
1671 # 2) any opening brace { on end of the line
1673 my $continuation = 0;
1675 $s =~ s/^.*\bdo\b//;
1677 if ($s =~ s/^\s*\\//) {
1680 if ($s =~ s/^\s*?\n//) {
1685 # Also ignore a loop construct at the end of a
1686 # preprocessor statement.
1687 if (($prevline =~ /^.\s*#\s*define\s/ ||
1688 $prevline =~ /\\\s*$/) && $continuation == 0) {
1694 while ($cond_ptr != $cond_lines) {
1695 $cond_ptr = $cond_lines;
1697 # If we see an #else/#elif then the code
1699 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1704 # 1) blank lines, they should be at 0,
1705 # 2) preprocessor lines, and
1707 if ($continuation ||
1709 $s =~ /^\s*#\s*?/ ||
1710 $s =~ /^\s*$Ident\s*:/) {
1711 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1712 if ($s =~ s/^.*?\n//) {
1718 my (undef, $sindent) = line_stats("+" . $s);
1719 my $stat_real = raw_line($linenr, $cond_lines);
1721 # Check if either of these lines are modified, else
1722 # this is not this patch's fault.
1723 if (!defined($stat_real) ||
1724 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1727 if (defined($stat_real) && $cond_lines > 1) {
1728 $stat_real = "[...]\n$stat_real";
1731 #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";
1733 if ($check && (($sindent % 4) != 0 ||
1734 ($sindent <= $indent && $s ne ''))) {
1735 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1739 # Track the 'values' across context and added lines.
1740 my $opline = $line; $opline =~ s/^./ /;
1741 my ($curr_values, $curr_vars) =
1742 annotate_values($opline . "\n", $prev_values);
1743 $curr_values = $prev_values . $curr_values;
1745 my $outline = $opline; $outline =~ s/\t/ /g;
1746 print "$linenr > .$outline\n";
1747 print "$linenr > $curr_values\n";
1748 print "$linenr > $curr_vars\n";
1750 $prev_values = substr($curr_values, -1);
1752 #ignore lines not being added
1753 if ($line=~/^[^\+]/) {next;}
1755 # TEST: allow direct testing of the type matcher.
1757 if ($line =~ /^.\s*$Declare\s*$/) {
1758 ERROR("TEST: is type\n" . $herecurr);
1759 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1760 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1764 # TEST: allow direct testing of the attribute matcher.
1766 if ($line =~ /^.\s*$Modifier\s*$/) {
1767 ERROR("TEST: is attr\n" . $herecurr);
1768 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1769 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1774 # check for initialisation to aggregates open brace on the next line
1775 if ($line =~ /^.\s*{/ &&
1776 $prevline =~ /(?:^|[^=])=\s*$/) {
1777 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1781 # Checks which are anchored on the added line.
1784 # check for malformed paths in #include statements (uses RAW line)
1785 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1787 if ($path =~ m{//}) {
1788 ERROR("malformed #include filename\n" .
1793 # no C99 // comments
1794 if ($line =~ m{//}) {
1795 ERROR("do not use C99 // comments\n" . $herecurr);
1797 # Remove C99 comments.
1799 $opline =~ s@//.*@@;
1801 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1802 # the whole statement.
1803 #print "APW <$lines[$realline_next - 1]>\n";
1804 if (defined $realline_next &&
1805 exists $lines[$realline_next - 1] &&
1806 !defined $suppress_export{$realline_next} &&
1807 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1808 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1809 # Handle definitions which produce identifiers with
1812 # EXPORT_SYMBOL(something_foo);
1814 if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
1815 $name =~ /^${Ident}_$2/) {
1816 #print "FOO C name<$name>\n";
1817 $suppress_export{$realline_next} = 1;
1819 } elsif ($stat !~ /(?:
1821 ^.DEFINE_$Ident\(\Q$name\E\)|
1822 ^.DECLARE_$Ident\(\Q$name\E\)|
1823 ^.LIST_HEAD\(\Q$name\E\)|
1824 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1825 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
1827 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1828 $suppress_export{$realline_next} = 2;
1830 $suppress_export{$realline_next} = 1;
1833 if (!defined $suppress_export{$linenr} &&
1834 $prevline =~ /^.\s*$/ &&
1835 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1836 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1837 #print "FOO B <$lines[$linenr - 1]>\n";
1838 $suppress_export{$linenr} = 2;
1840 if (defined $suppress_export{$linenr} &&
1841 $suppress_export{$linenr} == 2) {
1842 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1845 # check for global initialisers.
1846 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1847 ERROR("do not initialise globals to 0 or NULL\n" .
1850 # check for static initialisers.
1851 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1852 ERROR("do not initialise statics to 0 or NULL\n" .
1856 # * goes on variable not on type
1858 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1859 my ($from, $to) = ($1, $1);
1861 # Should start with a space.
1862 $to =~ s/^(\S)/ $1/;
1863 # Should not end with a space.
1865 # '*'s should not have spaces between.
1866 while ($to =~ s/\*\s+\*/\*\*/) {
1869 #print "from<$from> to<$to>\n";
1871 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1873 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1874 my ($from, $to, $ident) = ($1, $1, $2);
1876 # Should start with a space.
1877 $to =~ s/^(\S)/ $1/;
1878 # Should not end with a space.
1880 # '*'s should not have spaces between.
1881 while ($to =~ s/\*\s+\*/\*\*/) {
1883 # Modifiers should have spaces.
1884 $to =~ s/(\b$Modifier$)/$1 /;
1886 #print "from<$from> to<$to> ident<$ident>\n";
1887 if ($from ne $to && $ident !~ /^$Modifier$/) {
1888 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1892 # # no BUG() or BUG_ON()
1893 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1894 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1895 # print "$herecurr";
1899 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1900 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1903 # printk should use KERN_* levels. Note that follow on printk's on the
1904 # same line do not need a level, so we use the current block context
1905 # to try and find and validate the current printk. In summary the current
1906 # printk includes all preceding printk's which have no newline on the end.
1907 # we assume the first bad printk is the one to report.
1908 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1910 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1911 #print "CHECK<$lines[$ln - 1]\n";
1912 # we have a preceding printk if it ends
1913 # with "\n" ignore it, else it is to blame
1914 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1915 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1922 WARN("printk() should include KERN_ facility level\n" . $herecurr);
1926 # function brace can't be on same line, except for #defines of do while,
1927 # or if closed on same line
1928 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1929 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1930 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1933 # open braces for enum, union and struct go on the same line.
1934 if ($line =~ /^.\s*{/ &&
1935 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1936 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1939 # missing space after union, struct or enum definition
1940 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1941 WARN("missing space after $1 definition\n" . $herecurr);
1944 # check for spacing round square brackets; allowed:
1945 # 1. with a type on the left -- int [] a;
1946 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1947 # 3. inside a curly brace -- = { [0...10] = 5 }
1948 while ($line =~ /(.*?\s)\[/g) {
1949 my ($where, $prefix) = ($-[1], $1);
1950 if ($prefix !~ /$Type\s+$/ &&
1951 ($where != 0 || $prefix !~ /^.\s+$/) &&
1952 $prefix !~ /{\s+$/) {
1953 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1957 # check for spaces between functions and their parentheses.
1958 while ($line =~ /($Ident)\s+\(/g) {
1960 my $ctx_before = substr($line, 0, $-[1]);
1961 my $ctx = "$ctx_before$name";
1963 # Ignore those directives where spaces _are_ permitted.
1965 if|for|while|switch|return|case|
1966 volatile|__volatile__|
1967 __attribute__|format|__extension__|
1971 # cpp #define statements have non-optional spaces, ie
1972 # if there is a space between the name and the open
1973 # parenthesis it is simply not a parameter group.
1974 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1976 # cpp #elif statement condition may start with a (
1977 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1979 # If this whole things ends with a type its most
1980 # likely a typedef for a function.
1981 } elsif ($ctx =~ /$Type$/) {
1984 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1987 # Check operator spacing.
1988 if (!($line=~/\#\s*include/)) {
1990 <<=|>>=|<=|>=|==|!=|
1991 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1992 =>|->|<<|>>|<|>|=|!|~|
1993 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1996 my @elements = split(/($ops|;)/, $opline);
1999 my $blank = copy_spacing($opline);
2001 for (my $n = 0; $n < $#elements; $n += 2) {
2002 $off += length($elements[$n]);
2004 # Pick up the preceding and succeeding characters.
2005 my $ca = substr($opline, 0, $off);
2007 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2008 $cc = substr($opline, $off + length($elements[$n + 1]));
2010 my $cb = "$ca$;$cc";
2013 $a = 'V' if ($elements[$n] ne '');
2014 $a = 'W' if ($elements[$n] =~ /\s$/);
2015 $a = 'C' if ($elements[$n] =~ /$;$/);
2016 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2017 $a = 'O' if ($elements[$n] eq '');
2018 $a = 'E' if ($ca =~ /^\s*$/);
2020 my $op = $elements[$n + 1];
2023 if (defined $elements[$n + 2]) {
2024 $c = 'V' if ($elements[$n + 2] ne '');
2025 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2026 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2027 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2028 $c = 'O' if ($elements[$n + 2] eq '');
2029 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2034 my $ctx = "${a}x${c}";
2036 my $at = "(ctx:$ctx)";
2038 my $ptr = substr($blank, 0, $off) . "^";
2039 my $hereptr = "$hereline$ptr\n";
2041 # Pull out the value of this operator.
2042 my $op_type = substr($curr_values, $off + 1, 1);
2044 # Get the full operator variant.
2045 my $opv = $op . substr($curr_vars, $off, 1);
2047 # Ignore operators passed as parameters.
2048 if ($op_type ne 'V' &&
2049 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2052 # } elsif ($op =~ /^$;+$/) {
2054 # ; should have either the end of line or a space or \ after it
2055 } elsif ($op eq ';') {
2056 if ($ctx !~ /.x[WEBC]/ &&
2057 $cc !~ /^\\/ && $cc !~ /^;/) {
2058 ERROR("space required after that '$op' $at\n" . $hereptr);
2062 } elsif ($op eq '//') {
2066 # : when part of a bitfield
2067 } elsif ($op eq '->' || $opv eq ':B') {
2068 if ($ctx =~ /Wx.|.xW/) {
2069 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
2072 # , must have a space on the right.
2073 # not required when having a single },{ on one line
2074 } elsif ($op eq ',') {
2075 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
2076 ($elements[$n] . $elements[$n + 2]) !~ " *}{") {
2077 ERROR("space required after that '$op' $at\n" . $hereptr);
2080 # '*' as part of a type definition -- reported already.
2081 } elsif ($opv eq '*_') {
2082 #warn "'*' is part of type\n";
2084 # unary operators should have a space before and
2085 # none after. May be left adjacent to another
2086 # unary operator, or a cast
2087 } elsif ($op eq '!' || $op eq '~' ||
2088 $opv eq '*U' || $opv eq '-U' ||
2089 $opv eq '&U' || $opv eq '&&U') {
2090 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2091 ERROR("space required before that '$op' $at\n" . $hereptr);
2093 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2094 # A unary '*' may be const
2096 } elsif ($ctx =~ /.xW/) {
2097 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2100 # unary ++ and unary -- are allowed no space on one side.
2101 } elsif ($op eq '++' or $op eq '--') {
2102 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2103 ERROR("space required one side of that '$op' $at\n" . $hereptr);
2105 if ($ctx =~ /Wx[BE]/ ||
2106 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2107 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2109 if ($ctx =~ /ExW/) {
2110 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2114 # << and >> may either have or not have spaces both sides
2115 } elsif ($op eq '<<' or $op eq '>>' or
2116 $op eq '&' or $op eq '^' or $op eq '|' or
2117 $op eq '+' or $op eq '-' or
2118 $op eq '*' or $op eq '/' or
2121 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2122 ERROR("need consistent spacing around '$op' $at\n" .
2126 # A colon needs no spaces before when it is
2127 # terminating a case value or a label.
2128 } elsif ($opv eq ':C' || $opv eq ':L') {
2129 if ($ctx =~ /Wx./) {
2130 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2133 # All the others need spaces both sides.
2134 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2137 # Ignore email addresses <foo@bar>
2139 $cc =~ /^\S+\@\S+>/) ||
2141 $ca =~ /<\S+\@\S+$/))
2147 if (($opv eq ':O' && $ca =~ /\?$/) ||
2148 ($op eq '?' && $cc =~ /^:/)) {
2153 ERROR("spaces required around that '$op' $at\n" . $hereptr);
2156 $off += length($elements[$n + 1]);
2160 # check for multiple assignments
2161 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2162 CHK("multiple assignments should be avoided\n" . $herecurr);
2165 ## # check for multiple declarations, allowing for a function declaration
2167 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2168 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2170 ## # Remove any bracketed sections to ensure we do not
2171 ## # falsly report the parameters of functions.
2173 ## while ($ln =~ s/\([^\(\)]*\)//g) {
2175 ## if ($ln =~ /,/) {
2176 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2180 #need space before brace following if, while, etc
2181 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2183 ERROR("space required before the open brace '{'\n" . $herecurr);
2186 # closing brace should have a space following it when it has anything
2188 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2189 ERROR("space required after that close brace '}'\n" . $herecurr);
2192 # check spacing on square brackets
2193 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2194 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2196 if ($line =~ /\s\]/) {
2197 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2200 # check spacing on parentheses
2201 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2202 $line !~ /for\s*\(\s+;/) {
2203 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2205 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2206 $line !~ /for\s*\(.*;\s+\)/ &&
2207 $line !~ /:\s+\)/) {
2208 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2211 # Return is not a function.
2212 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2216 # Flatten any parentheses
2217 $value =~ s/\(/ \(/g;
2218 $value =~ s/\)/\) /g;
2219 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2220 $value !~ /(?:$Ident|-?$Constant)\s*
2222 (?:$Ident|-?$Constant)/x &&
2223 $value =~ s/\([^\(\)]*\)/1/) {
2225 #print "value<$value>\n";
2226 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2227 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2229 } elsif ($spacing !~ /\s+/) {
2230 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2233 # Return of what appears to be an errno should normally be -'ve
2234 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2236 if ($name ne 'EOF' && $name ne 'ERROR') {
2237 CHK("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2241 # Need a space before open parenthesis after if, while etc
2242 if ($line=~/\b(if|while|for|switch)\(/) {
2243 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2246 # Check for illegal assignment in if conditional -- and check for trailing
2247 # statements after the conditional.
2248 if ($line =~ /do\s*(?!{)/) {
2249 my ($stat_next) = ctx_statement_block($line_nr_next,
2250 $remain_next, $off_next);
2251 $stat_next =~ s/\n./\n /g;
2252 ##print "stat<$stat> stat_next<$stat_next>\n";
2254 if ($stat_next =~ /^\s*while\b/) {
2255 # If the statement carries leading newlines,
2256 # then count those as offsets.
2258 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2260 statement_rawlines($whitespace) - 1;
2262 $suppress_whiletrailers{$line_nr_next +
2266 if (!defined $suppress_whiletrailers{$linenr} &&
2267 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2268 my ($s, $c) = ($stat, $cond);
2270 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2271 ERROR("do not use assignment in if condition\n" . $herecurr);
2274 # Find out what is on the end of the line after the
2276 substr($s, 0, length($c), '');
2278 $s =~ s/$;//g; # Remove any comments
2279 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2280 $c !~ /}\s*while\s*/)
2282 # Find out how long the conditional actually is.
2283 my @newlines = ($c =~ /\n/gs);
2284 my $cond_lines = 1 + $#newlines;
2287 $stat_real = raw_line($linenr, $cond_lines)
2288 . "\n" if ($cond_lines);
2289 if (defined($stat_real) && $cond_lines > 1) {
2290 $stat_real = "[...]\n$stat_real";
2293 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2297 # Check for bitwise tests written as boolean
2309 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2312 # if and else should not have general statements after it
2313 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2315 $s =~ s/$;//g; # Remove any comments
2316 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2317 ERROR("trailing statements should be on next line\n" . $herecurr);
2320 # if should not continue a brace
2321 if ($line =~ /}\s*if\b/) {
2322 ERROR("trailing statements should be on next line\n" .
2325 # case and default should not have general statements after them
2326 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2328 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2332 ERROR("trailing statements should be on next line\n" . $herecurr);
2335 # Check for }<nl>else {, these must be at the same
2336 # indent level to be relevant to each other.
2337 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2338 $previndent == $indent) {
2339 ERROR("else should follow close brace '}'\n" . $hereprev);
2342 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2343 $previndent == $indent) {
2344 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2346 # Find out what is on the end of the line after the
2348 substr($s, 0, length($c), '');
2351 if ($s =~ /^\s*;/) {
2352 ERROR("while should follow close brace '}'\n" . $hereprev);
2356 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2357 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2358 # print "No studly caps, use _\n";
2359 # print "$herecurr";
2363 #no spaces allowed after \ in define
2364 if ($line=~/\#\s*define.*\\\s$/) {
2365 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
2368 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2369 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2371 my $checkfile = "include/linux/$file";
2372 if (-f "$root/$checkfile" &&
2373 $realfile ne $checkfile &&
2374 $1 !~ /$allowed_asm_includes/)
2376 if ($realfile =~ m{^arch/}) {
2377 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2379 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2384 # multi-statement macros should be enclosed in a do while loop, grab the
2385 # first statement and ensure its the whole macro if its not enclosed
2386 # in a known good container
2387 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2388 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2391 my ($off, $dstat, $dcond, $rest);
2394 my $args = defined($1);
2396 # Find the end of the macro and limit our statement
2398 while ($cnt > 0 && defined $lines[$ln - 1] &&
2399 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2401 $ctx .= $rawlines[$ln - 1] . "\n";
2402 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2405 $ctx .= $rawlines[$ln - 1];
2407 ($dstat, $dcond, $ln, $cnt, $off) =
2408 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2409 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2410 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2412 # Extract the remainder of the define (if any) and
2413 # rip off surrounding spaces, and trailing \'s.
2415 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2416 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2417 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2418 $rest .= substr($lines[$ln - 1], $off) . "\n";
2424 $rest =~ s/\\\n.//g;
2428 # Clean up the original statement.
2430 substr($dstat, 0, length($dcond), '');
2432 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2435 $dstat =~ s/\\\n.//g;
2436 $dstat =~ s/^\s*//s;
2437 $dstat =~ s/\s*$//s;
2439 # Flatten any parentheses and braces
2440 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2441 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2442 $dstat =~ s/\[[^\{\}]*\]/1/)
2446 my $exceptions = qr{
2458 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2459 if ($rest ne '' && $rest ne ',') {
2460 if ($rest !~ /while\s*\(/ &&
2461 $dstat !~ /$exceptions/)
2463 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2466 } elsif ($ctx !~ /;/) {
2468 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2469 $dstat !~ /$exceptions/ &&
2470 $dstat !~ /^\.$Ident\s*=/ &&
2471 $dstat =~ /$Operators/)
2473 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2478 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2479 # all assignments may have only one of the following with an assignment:
2482 # VMLINUX_SYMBOL(...)
2483 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2484 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2487 # check for missing bracing round if etc
2488 if ($line =~ /(^.*)\bif\b/ && $line !~ /\#\s*if/) {
2489 my ($level, $endln, @chunks) =
2490 ctx_statement_full($linenr, $realcnt, 1);
2491 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2492 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2493 if ($#chunks >= 0 && $level == 0) {
2496 my $herectx = $here . "\n";
2497 my $ln = $linenr - 1;
2498 for my $chunk (@chunks) {
2499 my ($cond, $block) = @{$chunk};
2501 # If the condition carries leading newlines, then count those as offsets.
2502 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2503 my $offset = statement_rawlines($whitespace) - 1;
2505 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2507 # We have looked at and allowed this specific line.
2508 $suppress_ifbraces{$ln + $offset} = 1;
2510 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2511 $ln += statement_rawlines($block) - 1;
2513 substr($block, 0, length($cond), '');
2515 $seen++ if ($block =~ /^\s*{/);
2517 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2518 if (statement_lines($cond) > 1) {
2519 #print "APW: ALLOWED: cond<$cond>\n";
2522 if ($block =~/\b(?:if|for|while)\b/) {
2523 #print "APW: ALLOWED: block<$block>\n";
2526 if (statement_block_size($block) > 1) {
2527 #print "APW: ALLOWED: lines block<$block>\n";
2531 if ($seen != ($#chunks + 1)) {
2532 WARN("braces {} are necessary for all arms of this statement\n" . $herectx);
2536 if (!defined $suppress_ifbraces{$linenr - 1} &&
2537 $line =~ /\b(if|while|for|else)\b/ &&
2538 $line !~ /\#\s*if/ &&
2539 $line !~ /\#\s*else/) {
2542 # Check the pre-context.
2543 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2544 #print "APW: ALLOWED: pre<$1>\n";
2548 my ($level, $endln, @chunks) =
2549 ctx_statement_full($linenr, $realcnt, $-[0]);
2551 # Check the condition.
2552 my ($cond, $block) = @{$chunks[0]};
2553 print "CHECKING<$linenr> cond<$cond> block<$block>\n"
2554 if $dbg_adv_checking;
2555 if (defined $cond) {
2556 substr($block, 0, length($cond), '');
2558 if (statement_lines($cond) > 1) {
2559 #print "APW: ALLOWED: cond<$cond>\n";
2562 if ($block =~/\b(?:if|for|while)\b/) {
2563 #print "APW: ALLOWED: block<$block>\n";
2566 if (statement_block_size($block) > 1) {
2567 #print "APW: ALLOWED: lines block<$block>\n";
2570 # Check the post-context.
2571 if (defined $chunks[1]) {
2572 my ($cond, $block) = @{$chunks[1]};
2573 if (defined $cond) {
2574 substr($block, 0, length($cond), '');
2576 if ($block =~ /^\s*\{/) {
2577 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2581 print "DCS: level=$level block<$block> allowed=$allowed\n"
2583 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
2584 my $herectx = $here . "\n";;
2585 my $cnt = statement_rawlines($block);
2587 for (my $n = 0; $n < $cnt; $n++) {
2588 $herectx .= raw_line($linenr, $n) . "\n";;
2591 WARN("braces {} are necessary even for single statement blocks\n" . $herectx);
2595 # don't include deprecated include files (uses RAW line)
2596 for my $inc (@dep_includes) {
2597 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2598 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2602 # don't use deprecated functions
2603 for my $func (@dep_functions) {
2604 if ($line =~ /\b$func\b/) {
2605 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2609 # no volatiles please
2610 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2611 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2612 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2615 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2616 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2617 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2621 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2622 CHK("if this code is redundant consider removing it\n" .
2626 # check for needless kfree() checks
2627 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2629 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2630 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2633 # check for needless usb_free_urb() checks
2634 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2636 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2637 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2641 # prefer usleep_range over udelay
2642 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2643 # ignore udelay's < 10, however
2644 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2645 CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2649 # warn about unexpectedly long msleep's
2650 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2652 WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2656 # warn about #ifdefs in C files
2657 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2658 # print "#ifdef in C files should be avoided\n";
2659 # print "$herecurr";
2663 # warn about spacing in #ifdefs
2664 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2665 ERROR("exactly one space required after that #$1\n" . $herecurr);
2668 # check for spinlock_t definitions without a comment.
2669 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2670 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
2672 if (!ctx_has_comment($first_line, $linenr)) {
2673 CHK("$1 definition without comment\n" . $herecurr);
2676 # check for memory barriers without a comment.
2677 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2678 if (!ctx_has_comment($first_line, $linenr)) {
2679 CHK("memory barrier without comment\n" . $herecurr);
2682 # check of hardware specific defines
2683 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2684 CHK("architecture specific defines should be avoided\n" . $herecurr);
2687 # Check that the storage class is at the beginning of a declaration
2688 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2689 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2692 # check the location of the inline attribute, that it is between
2693 # storage class and type.
2694 if ($line =~ /\b$Type\s+$Inline\b/ ||
2695 $line =~ /\b$Inline\s+$Storage\b/) {
2696 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2699 # Check for __inline__ and __inline, prefer inline
2700 if ($line =~ /\b(__inline__|__inline)\b/) {
2701 WARN("plain inline is preferred over $1\n" . $herecurr);
2704 # check for sizeof(&)
2705 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2706 WARN("sizeof(& should be avoided\n" . $herecurr);
2709 # check for new externs in .c files.
2710 if ($realfile =~ /\.c$/ && defined $stat &&
2711 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2713 my $function_name = $1;
2714 my $paren_space = $2;
2717 if (defined $cond) {
2718 substr($s, 0, length($cond), '');
2720 if ($s =~ /^\s*;/ &&
2721 $function_name ne 'uninitialized_var')
2723 WARN("externs should be avoided in .c files\n" . $herecurr);
2726 if ($paren_space =~ /\n/) {
2727 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2730 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2731 $stat =~ /^.\s*extern\s+/)
2733 WARN("externs should be avoided in .c files\n" . $herecurr);
2736 # checks for new __setup's
2737 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2740 if (!grep(/$name/, @setup_docs)) {
2741 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2745 # check for pointless casting of kmalloc return
2746 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2747 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2750 # check for gcc specific __FUNCTION__
2751 if ($line =~ /__FUNCTION__/) {
2752 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2755 # check for semaphores used as mutexes
2756 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2757 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2759 # check for semaphores used as mutexes
2760 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2761 WARN("consider using a completion\n" . $herecurr);
2764 # recommend strict_strto* over simple_strto*
2765 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2766 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2768 # check for __initcall(), use device_initcall() explicitly please
2769 if ($line =~ /^.\s*__initcall\s*\(/) {
2770 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2772 # check for various ops structs, ensure they are const.
2773 my $struct_ops = qr{acpi_dock_ops|
2774 address_space_operations|
2776 block_device_operations|
2781 file_lock_operations|
2791 lock_manager_operations|
2797 pipe_buf_operations|
2798 platform_hibernation_ops|
2799 platform_suspend_ops|
2804 soc_pcmcia_socket_ops|
2810 if ($line !~ /\bconst\b/ &&
2811 $line =~ /\bstruct\s+($struct_ops)\b/) {
2812 WARN("struct $1 should normally be const\n" .
2816 # use of NR_CPUS is usually wrong
2817 # ignore definitions of NR_CPUS and usage to define arrays as likely right
2818 if ($line =~ /\bNR_CPUS\b/ &&
2819 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2820 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2821 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2822 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2823 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2825 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2828 # check for %L{u,d,i} in strings
2830 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2831 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2832 $string =~ s/%%/__/g;
2833 if ($string =~ /(?<!%)%L[udi]/) {
2834 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2839 # whine mightly about in_atomic
2840 if ($line =~ /\bin_atomic\s*\(/) {
2841 if ($realfile =~ m@^drivers/@) {
2842 ERROR("do not use in_atomic in drivers\n" . $herecurr);
2843 } elsif ($realfile !~ m@^kernel/@) {
2844 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2848 # check for lockdep_set_novalidate_class
2849 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
2850 $line =~ /__lockdep_no_validate__\s*\)/ ) {
2851 if ($realfile !~ m@^kernel/lockdep@ &&
2852 $realfile !~ m@^include/linux/lockdep@ &&
2853 $realfile !~ m@^drivers/base/core@) {
2854 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
2858 # QEMU specific tests
2859 if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
2860 WARN("use QEMU instead of Qemu or QEmu\n" . $herecurr);
2864 # If we have no input at all, then there is nothing to report on
2865 # so just keep quiet.
2866 if ($#rawlines == -1) {
2870 # In mailback mode only produce a report in the negative, for
2871 # things that appear to be patches.
2872 if ($mailback && ($clean == 1 || !$is_patch)) {
2876 # This is not a patch, and we are are in 'no-patch' mode so
2878 if (!$chk_patch && !$is_patch) {
2883 ERROR("Does not appear to be a unified-diff format patch\n");
2885 if ($is_patch && $chk_signoff && $signoff == 0) {
2886 ERROR("Missing Signed-off-by: line(s)\n");
2889 print report_dump();
2890 if ($summary && !($clean == 1 && $quiet == 1)) {
2891 print "$filename " if ($summary_file);
2892 print "total: $cnt_error errors, $cnt_warn warnings, " .
2893 (($check)? "$cnt_chk checks, " : "") .
2894 "$cnt_lines lines checked\n";
2895 print "\n" if ($quiet == 0);
2899 # If there were whitespace errors which cleanpatch can fix
2900 # then suggest that.
2901 # if ($rpt_cleaners) {
2902 # print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2903 # print " scripts/cleanfile\n\n";
2907 if ($clean == 1 && $quiet == 0) {
2908 print "$vname has no obvious style problems and is ready for submission.\n"
2910 if ($clean == 0 && $quiet == 0) {
2911 print "$vname has style problems, please review. If any of these errors\n";
2912 print "are false positives report them to the maintainer, see\n";
2913 print "CHECKPATCH in MAINTAINERS.\n";