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";
100 for my $key (keys %debug) {
102 eval "\${dbg_$key} = '$debug{$key}';";
106 my $rpt_cleaners = 0;
115 if (!top_of_kernel_tree($root)) {
116 die "$P: $root: --root does not point at a valid tree\n";
119 if (top_of_kernel_tree('.')) {
121 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
122 top_of_kernel_tree($1)) {
127 if (!defined $root) {
128 print "Must be run from the top-level dir. of a kernel tree\n";
133 my $emitted_corrupt = 0;
136 [A-Za-z_][A-Za-z\d_]*
137 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
139 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 [a-z]+_(emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|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 our $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/;
274 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
275 our $LvalOrFunc = qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*};
279 return "" if (!defined($string));
280 $string =~ s@^\s*\(\s*@@g;
281 $string =~ s@\s*\)\s*$@@g;
282 $string =~ s@\s+@ @g;
286 $chk_signoff = 0 if ($file);
288 my @dep_includes = ();
289 my @dep_functions = ();
290 my $removal = "Documentation/feature-removal-schedule.txt";
291 if ($tree && -f "$root/$removal") {
292 open(my $REMOVE, '<', "$root/$removal") ||
293 die "$P: $removal: open failed - $!\n";
295 if (/^Check:\s+(.*\S)/) {
296 for my $entry (split(/[, ]+/, $1)) {
297 if ($entry =~ m@include/(.*)@) {
298 push(@dep_includes, $1);
300 } elsif ($entry !~ m@/@) {
301 push(@dep_functions, $entry);
312 for my $filename (@ARGV) {
315 open($FILE, '-|', "diff -u /dev/null $filename") ||
316 die "$P: $filename: diff failed - $!\n";
317 } elsif ($filename eq '-') {
318 open($FILE, '<&STDIN');
320 open($FILE, '<', "$filename") ||
321 die "$P: $filename: open failed - $!\n";
323 if ($filename eq '-') {
324 $vname = 'Your patch';
333 if (!process($filename)) {
342 sub top_of_kernel_tree {
346 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
347 "README", "Documentation", "arch", "include", "drivers",
348 "fs", "init", "ipc", "kernel", "lib", "scripts",
351 foreach my $check (@tree_check) {
352 if (! -e $root . '/' . $check) {
364 for my $c (split(//, $str)) {
368 for (; ($n % 8) != 0; $n++) {
380 (my $res = shift) =~ tr/\t/ /c;
387 # Drop the diff line leader and expand tabs
389 $line = expand_tabs($line);
391 # Pick the indent from the front of the line.
392 my ($white) = ($line =~ /^(\s*)/);
394 return (length($line), length($white));
397 my $sanitise_quote = '';
399 sub sanitise_line_reset {
400 my ($in_comment) = @_;
403 $sanitise_quote = '*/';
405 $sanitise_quote = '';
418 # Always copy over the diff marker.
419 $res = substr($line, 0, 1);
421 for ($off = 1; $off < length($line); $off++) {
422 $c = substr($line, $off, 1);
424 # Comments we are wacking completly including the begin
425 # and end, all to $;.
426 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
427 $sanitise_quote = '*/';
429 substr($res, $off, 2, "$;$;");
433 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
434 $sanitise_quote = '';
435 substr($res, $off, 2, "$;$;");
439 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
440 $sanitise_quote = '//';
442 substr($res, $off, 2, $sanitise_quote);
447 # A \ in a string means ignore the next character.
448 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
450 substr($res, $off, 2, 'XX');
455 if ($c eq "'" || $c eq '"') {
456 if ($sanitise_quote eq '') {
457 $sanitise_quote = $c;
459 substr($res, $off, 1, $c);
461 } elsif ($sanitise_quote eq $c) {
462 $sanitise_quote = '';
466 #print "c<$c> SQ<$sanitise_quote>\n";
467 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
468 substr($res, $off, 1, $;);
469 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
470 substr($res, $off, 1, $;);
471 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
472 substr($res, $off, 1, 'X');
474 substr($res, $off, 1, $c);
478 if ($sanitise_quote eq '//') {
479 $sanitise_quote = '';
482 # The pathname on a #include may be surrounded by '<' and '>'.
483 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
484 my $clean = 'X' x length($1);
485 $res =~ s@\<.*\>@<$clean>@;
487 # The whole of a #error is a string.
488 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
489 my $clean = 'X' x length($1);
490 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
496 sub ctx_statement_block {
497 my ($linenr, $remain, $off) = @_;
498 my $line = $linenr - 1;
515 @stack = (['', 0]) if ($#stack == -1);
517 #warn "CSB: blk<$blk> remain<$remain>\n";
518 # If we are about to drop off the end, pull in more
521 for (; $remain > 0; $line++) {
522 last if (!defined $lines[$line]);
523 next if ($lines[$line] =~ /^-/);
526 $blk .= $lines[$line] . "\n";
531 # Bail if there is no further context.
532 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
538 $c = substr($blk, $off, 1);
539 $remainder = substr($blk, $off);
541 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
543 # Handle nested #if/#else.
544 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
545 push(@stack, [ $type, $level ]);
546 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
547 ($type, $level) = @{$stack[$#stack - 1]};
548 } elsif ($remainder =~ /^#\s*endif\b/) {
549 ($type, $level) = @{pop(@stack)};
552 # Statement ends at the ';' or a close '}' at the
554 if ($level == 0 && $c eq ';') {
558 # An else is really a conditional as long as its not else if
559 if ($level == 0 && $coff_set == 0 &&
560 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
561 $remainder =~ /^(else)(?:\s|{)/ &&
562 $remainder !~ /^else\s+if\b/) {
563 $coff = $off + length($1) - 1;
565 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
566 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
569 if (($type eq '' || $type eq '(') && $c eq '(') {
573 if ($type eq '(' && $c eq ')') {
575 $type = ($level != 0)? '(' : '';
577 if ($level == 0 && $coff < $soff) {
580 #warn "CSB: mark coff<$coff>\n";
583 if (($type eq '' || $type eq '{') && $c eq '{') {
587 if ($type eq '{' && $c eq '}') {
589 $type = ($level != 0)? '{' : '';
592 if (substr($blk, $off + 1, 1) eq ';') {
600 # We are truly at the end, so shuffle to the next line.
607 my $statement = substr($blk, $soff, $off - $soff + 1);
608 my $condition = substr($blk, $soff, $coff - $soff + 1);
610 #warn "STATEMENT<$statement>\n";
611 #warn "CONDITION<$condition>\n";
613 #print "coff<$coff> soff<$off> loff<$loff>\n";
615 return ($statement, $condition,
616 $line, $remain + 1, $off - $loff + 1, $level);
619 sub statement_lines {
622 # Strip the diff line prefixes and rip blank lines at start and end.
623 $stmt =~ s/(^|\n)./$1/g;
627 my @stmt_lines = ($stmt =~ /\n/g);
629 return $#stmt_lines + 2;
632 sub statement_rawlines {
635 my @stmt_lines = ($stmt =~ /\n/g);
637 return $#stmt_lines + 2;
640 sub statement_block_size {
643 $stmt =~ s/(^|\n)./$1/g;
649 my @stmt_lines = ($stmt =~ /\n/g);
650 my @stmt_statements = ($stmt =~ /;/g);
652 my $stmt_lines = $#stmt_lines + 2;
653 my $stmt_statements = $#stmt_statements + 1;
655 if ($stmt_lines > $stmt_statements) {
658 return $stmt_statements;
662 sub ctx_statement_full {
663 my ($linenr, $remain, $off) = @_;
664 my ($statement, $condition, $level);
668 # Grab the first conditional/block pair.
669 ($statement, $condition, $linenr, $remain, $off, $level) =
670 ctx_statement_block($linenr, $remain, $off);
671 #print "F: c<$condition> s<$statement> remain<$remain>\n";
672 push(@chunks, [ $condition, $statement ]);
673 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
674 return ($level, $linenr, @chunks);
677 # Pull in the following conditional/block pairs and see if they
678 # could continue the statement.
680 ($statement, $condition, $linenr, $remain, $off, $level) =
681 ctx_statement_block($linenr, $remain, $off);
682 #print "C: c<$condition> s<$statement> remain<$remain>\n";
683 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
685 push(@chunks, [ $condition, $statement ]);
688 return ($level, $linenr, @chunks);
692 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
694 my $start = $linenr - 1;
701 my @stack = ($level);
702 for ($line = $start; $remain > 0; $line++) {
703 next if ($rawlines[$line] =~ /^-/);
706 $blk .= $rawlines[$line];
708 # Handle nested #if/#else.
709 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
710 push(@stack, $level);
711 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
712 $level = $stack[$#stack - 1];
713 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
714 $level = pop(@stack);
717 foreach my $c (split(//, $lines[$line])) {
718 ##print "C<$c>L<$level><$open$close>O<$off>\n";
724 if ($c eq $close && $level > 0) {
726 last if ($level == 0);
727 } elsif ($c eq $open) {
732 if (!$outer || $level <= 1) {
733 push(@res, $rawlines[$line]);
736 last if ($level == 0);
739 return ($level, @res);
741 sub ctx_block_outer {
742 my ($linenr, $remain) = @_;
744 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
748 my ($linenr, $remain) = @_;
750 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
754 my ($linenr, $remain, $off) = @_;
756 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
759 sub ctx_block_level {
760 my ($linenr, $remain) = @_;
762 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
764 sub ctx_statement_level {
765 my ($linenr, $remain, $off) = @_;
767 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
770 sub ctx_locate_comment {
771 my ($first_line, $end_line) = @_;
773 # Catch a comment on the end of the line itself.
774 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
775 return $current_comment if (defined $current_comment);
777 # Look through the context and try and figure out if there is a
780 $current_comment = '';
781 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
782 my $line = $rawlines[$linenr - 1];
784 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
787 if ($line =~ m@/\*@) {
790 if (!$in_comment && $current_comment ne '') {
791 $current_comment = '';
793 $current_comment .= $line . "\n" if ($in_comment);
794 if ($line =~ m@\*/@) {
799 chomp($current_comment);
800 return($current_comment);
802 sub ctx_has_comment {
803 my ($first_line, $end_line) = @_;
804 my $cmt = ctx_locate_comment($first_line, $end_line);
806 ##print "LINE: $rawlines[$end_line - 1 ]\n";
807 ##print "CMMT: $cmt\n";
813 my ($linenr, $cnt) = @_;
815 my $offset = $linenr - 1;
820 $line = $rawlines[$offset++];
821 next if (defined($line) && $line =~ /^-/);
833 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
836 $coded = sprintf("^%c", unpack('C', $2) + 64);
845 my $av_preprocessor = 0;
851 $av_preprocessor = 0;
853 @av_paren_type = ('E');
854 $av_pend_colon = 'O';
857 sub annotate_values {
858 my ($stream, $type) = @_;
861 my $var = '_' x length($stream);
864 print "$stream\n" if ($dbg_values > 1);
866 while (length($cur)) {
867 @av_paren_type = ('E') if ($#av_paren_type < 0);
868 print " <" . join('', @av_paren_type) .
869 "> <$type> <$av_pending>" if ($dbg_values > 1);
870 if ($cur =~ /^(\s+)/o) {
871 print "WS($1)\n" if ($dbg_values > 1);
872 if ($1 =~ /\n/ && $av_preprocessor) {
873 $type = pop(@av_paren_type);
874 $av_preprocessor = 0;
877 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
878 print "CAST($1)\n" if ($dbg_values > 1);
879 push(@av_paren_type, $type);
882 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
883 print "DECLARE($1)\n" if ($dbg_values > 1);
886 } elsif ($cur =~ /^($Modifier)\s*/) {
887 print "MODIFIER($1)\n" if ($dbg_values > 1);
890 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
891 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
892 $av_preprocessor = 1;
893 push(@av_paren_type, $type);
899 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
900 print "UNDEF($1)\n" if ($dbg_values > 1);
901 $av_preprocessor = 1;
902 push(@av_paren_type, $type);
904 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
905 print "PRE_START($1)\n" if ($dbg_values > 1);
906 $av_preprocessor = 1;
908 push(@av_paren_type, $type);
909 push(@av_paren_type, $type);
912 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
913 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
914 $av_preprocessor = 1;
916 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
920 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
921 print "PRE_END($1)\n" if ($dbg_values > 1);
923 $av_preprocessor = 1;
925 # Assume all arms of the conditional end as this
926 # one does, and continue as if the #endif was not here.
928 push(@av_paren_type, $type);
931 } elsif ($cur =~ /^(\\\n)/o) {
932 print "PRECONT($1)\n" if ($dbg_values > 1);
934 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
935 print "ATTR($1)\n" if ($dbg_values > 1);
939 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
940 print "SIZEOF($1)\n" if ($dbg_values > 1);
946 } elsif ($cur =~ /^(if|while|for)\b/o) {
947 print "COND($1)\n" if ($dbg_values > 1);
951 } elsif ($cur =~/^(case)/o) {
952 print "CASE($1)\n" if ($dbg_values > 1);
953 $av_pend_colon = 'C';
956 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
957 print "KEYWORD($1)\n" if ($dbg_values > 1);
960 } elsif ($cur =~ /^(\()/o) {
961 print "PAREN('$1')\n" if ($dbg_values > 1);
962 push(@av_paren_type, $av_pending);
966 } elsif ($cur =~ /^(\))/o) {
967 my $new_type = pop(@av_paren_type);
968 if ($new_type ne '_') {
970 print "PAREN('$1') -> $type\n"
971 if ($dbg_values > 1);
973 print "PAREN('$1')\n" if ($dbg_values > 1);
976 } elsif ($cur =~ /^($Ident)\s*\(/o) {
977 print "FUNC($1)\n" if ($dbg_values > 1);
981 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
982 if (defined $2 && $type eq 'C' || $type eq 'T') {
983 $av_pend_colon = 'B';
984 } elsif ($type eq 'E') {
985 $av_pend_colon = 'L';
987 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
990 } elsif ($cur =~ /^($Ident|$Constant)/o) {
991 print "IDENT($1)\n" if ($dbg_values > 1);
994 } elsif ($cur =~ /^($Assignment)/o) {
995 print "ASSIGN($1)\n" if ($dbg_values > 1);
998 } elsif ($cur =~/^(;|{|})/) {
999 print "END($1)\n" if ($dbg_values > 1);
1001 $av_pend_colon = 'O';
1003 } elsif ($cur =~/^(,)/) {
1004 print "COMMA($1)\n" if ($dbg_values > 1);
1007 } elsif ($cur =~ /^(\?)/o) {
1008 print "QUESTION($1)\n" if ($dbg_values > 1);
1011 } elsif ($cur =~ /^(:)/o) {
1012 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1014 substr($var, length($res), 1, $av_pend_colon);
1015 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1020 $av_pend_colon = 'O';
1022 } elsif ($cur =~ /^(\[)/o) {
1023 print "CLOSE($1)\n" if ($dbg_values > 1);
1026 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1029 print "OPV($1)\n" if ($dbg_values > 1);
1036 substr($var, length($res), 1, $variant);
1039 } elsif ($cur =~ /^($Operators)/o) {
1040 print "OP($1)\n" if ($dbg_values > 1);
1041 if ($1 ne '++' && $1 ne '--') {
1045 } elsif ($cur =~ /(^.)/o) {
1046 print "C($1)\n" if ($dbg_values > 1);
1049 $cur = substr($cur, length($1));
1050 $res .= $type x length($1);
1054 return ($res, $var);
1058 my ($possible, $line) = @_;
1059 my $notPermitted = qr{(?:
1074 ^(?:typedef|struct|enum)\b
1076 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1077 if ($possible !~ $notPermitted) {
1078 # Check for modifiers.
1079 $possible =~ s/\s*$Storage\s*//g;
1080 $possible =~ s/\s*$Sparse\s*//g;
1081 if ($possible =~ /^\s*$/) {
1083 } elsif ($possible =~ /\s/) {
1084 $possible =~ s/\s*$Type\s*//g;
1085 for my $modifier (split(' ', $possible)) {
1086 if ($modifier !~ $notPermitted) {
1087 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1088 push(@modifierList, $modifier);
1093 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1094 push(@typeList, $possible);
1098 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1105 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1108 my $line = $prefix . $_[0];
1110 $line = (split('\n', $line))[0] . "\n" if ($terse);
1112 push(our @report, $line);
1120 if (report("ERROR: $_[0]\n")) {
1126 if (report("WARNING: $_[0]\n")) {
1132 if ($check && report("CHECK: $_[0]\n")) {
1138 sub check_absolute_file {
1139 my ($absolute, $herecurr) = @_;
1140 my $file = $absolute;
1142 ##print "absolute<$absolute>\n";
1144 # See if any suffix of this path is a path within the tree.
1145 while ($file =~ s@^[^/]*/@@) {
1146 if (-f "$root/$file") {
1147 ##print "file<$file>\n";
1155 # It is, so see if the prefix is acceptable.
1156 my $prefix = $absolute;
1157 substr($prefix, -length($file)) = '';
1159 ##print "prefix<$prefix>\n";
1160 if ($prefix ne ".../") {
1161 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1166 my $filename = shift;
1172 my $stashrawline="";
1189 # Trace the real file/line as we go.
1195 my $comment_edge = 0;
1199 my $prev_values = 'E';
1202 my %suppress_ifbraces;
1203 my %suppress_whiletrailers;
1204 my %suppress_export;
1206 # Pre-scan the patch sanitizing the lines.
1207 # Pre-scan the patch looking for any __setup documentation.
1209 my @setup_docs = ();
1212 sanitise_line_reset();
1214 foreach my $rawline (@rawlines) {
1218 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1220 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1225 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1234 # Guestimate if this is a continuing comment. Run
1235 # the context looking for a comment "edge". If this
1236 # edge is a close comment then we must be in a comment
1240 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1241 next if (defined $rawlines[$ln - 1] &&
1242 $rawlines[$ln - 1] =~ /^-/);
1244 #print "RAW<$rawlines[$ln - 1]>\n";
1245 last if (!defined $rawlines[$ln - 1]);
1246 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1247 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1252 if (defined $edge && $edge eq '*/') {
1256 # Guestimate if this is a continuing comment. If this
1257 # is the start of a diff block and this line starts
1258 # ' *' then it is very likely a comment.
1259 if (!defined $edge &&
1260 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1265 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1266 sanitise_line_reset($in_comment);
1268 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1269 # Standardise the strings and chars within the input to
1270 # simplify matching -- only bother with positive lines.
1271 $line = sanitise_line($rawline);
1273 push(@lines, $line);
1276 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1281 #print "==>$rawline\n";
1282 #print "-->$line\n";
1284 if ($setup_docs && $line =~ /^\+/) {
1285 push(@setup_docs, $line);
1293 foreach my $line (@lines) {
1296 my $rawline = $rawlines[$linenr - 1];
1298 #extract the line range in the file after the patch is applied
1299 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1301 $first_line = $linenr + 1;
1311 %suppress_ifbraces = ();
1312 %suppress_whiletrailers = ();
1313 %suppress_export = ();
1316 # track the line number as we move through the hunk, note that
1317 # new versions of GNU diff omit the leading space on completely
1318 # blank context lines so we need to count that too.
1319 } elsif ($line =~ /^( |\+|$)/) {
1321 $realcnt-- if ($realcnt != 0);
1323 # Measure the line length and indent.
1324 ($length, $indent) = line_stats($rawline);
1326 # Track the previous line.
1327 ($prevline, $stashline) = ($stashline, $line);
1328 ($previndent, $stashindent) = ($stashindent, $indent);
1329 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1331 #warn "line<$line>\n";
1333 } elsif ($realcnt == 1) {
1337 my $hunk_line = ($realcnt != 0);
1339 #make up the handle for any error we report on this line
1340 $prefix = "$filename:$realline: " if ($emacs && $file);
1341 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1343 $here = "#$linenr: " if (!$file);
1344 $here = "#$realline: " if ($file);
1346 # extract the filename as it passes
1347 if ($line =~ /^diff --git.*?(\S+)$/) {
1349 $realfile =~ s@^([^/]*)/@@;
1351 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1353 $realfile =~ s@^([^/]*)/@@;
1356 if (!$file && $tree && $p1_prefix ne '' &&
1357 -e "$root/$p1_prefix") {
1358 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1361 if ($realfile =~ m@^include/asm/@) {
1362 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1367 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1369 my $hereline = "$here\n$rawline\n";
1370 my $herecurr = "$here\n$rawline\n";
1371 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1373 $cnt_lines++ if ($realcnt != 0);
1375 # Check for incorrect file permissions
1376 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1377 my $permhere = $here . "FILE: $realfile\n";
1378 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1379 ERROR("do not set execute permissions for source files\n" . $permhere);
1383 #check the patch for a signoff:
1384 if ($line =~ /^\s*signed-off-by:/i) {
1385 # This is a signoff, if ugly, so do not double report.
1387 if (!($line =~ /^\s*Signed-off-by:/)) {
1388 WARN("Signed-off-by: is the preferred form\n" .
1391 if ($line =~ /^\s*signed-off-by:\S/i) {
1392 WARN("space required after Signed-off-by:\n" .
1397 # Check for wrappage within a valid hunk of the file
1398 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1399 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1400 $herecurr) if (!$emitted_corrupt++);
1403 # Check for absolute kernel paths.
1405 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1408 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1409 check_absolute_file($1, $herecurr)) {
1412 check_absolute_file($file, $herecurr);
1417 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1418 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1419 $rawline !~ m/^$UTF8*$/) {
1420 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1422 my $blank = copy_spacing($rawline);
1423 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1424 my $hereptr = "$hereline$ptr\n";
1426 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1429 # ignore non-hunk lines and lines being removed
1430 next if (!$hunk_line || $line =~ /^-/);
1432 #trailing whitespace
1433 if ($line =~ /^\+.*\015/) {
1434 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1435 ERROR("DOS line endings\n" . $herevet);
1437 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1438 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1439 ERROR("trailing whitespace\n" . $herevet);
1443 # check for Kconfig help text having a real description
1444 # Only applies when adding the entry originally, after that we do not have
1445 # sufficient context to determine whether it is indeed long enough.
1446 if ($realfile =~ /Kconfig/ &&
1447 $line =~ /\+\s*(?:---)?help(?:---)?$/) {
1450 my $ln = $linenr + 1;
1453 while ($cnt > 0 && defined $lines[$ln - 1]) {
1454 $f = $lines[$ln - 1];
1455 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1456 $is_end = $lines[$ln - 1] =~ /^\+/;
1459 next if ($f =~ /^-/);
1463 next if ($f =~ /^$/);
1464 if ($f =~ /^\s*config\s/) {
1470 WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
1471 #print "is_end<$is_end> length<$length>\n";
1474 # check we are in a valid source file if not then ignore this hunk
1475 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1478 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1479 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1480 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
1481 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1484 WARN("line over 80 characters\n" . $herecurr);
1487 # check for spaces before a quoted newline
1488 if ($rawline =~ /^.*\".*\s\\n/) {
1489 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1492 # check for adding lines without a newline.
1493 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1494 WARN("adding a line without newline at end of file\n" . $herecurr);
1497 # Blackfin: use hi/lo macros
1498 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1499 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1500 my $herevet = "$here\n" . cat_vet($line) . "\n";
1501 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1503 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1504 my $herevet = "$here\n" . cat_vet($line) . "\n";
1505 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1509 # check we are in a valid source file C or perl if not then ignore this hunk
1510 next if ($realfile !~ /\.(h|c|pl)$/);
1512 # at the beginning of a line any tabs must come first and anything
1513 # more than 8 must use tabs.
1514 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1515 $rawline =~ /^\+\s* \s*/) {
1516 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1517 ERROR("code indent should use tabs where possible\n" . $herevet);
1521 # check for space before tabs.
1522 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1523 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1524 WARN("please, no space before tabs\n" . $herevet);
1527 # check for spaces at the beginning of a line.
1529 # 1) within comments
1530 # 2) indented preprocessor commands
1532 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
1533 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1534 WARN("please, no spaces at the start of a line\n" . $herevet);
1537 # check we are in a valid C source file if not then ignore this hunk
1538 next if ($realfile !~ /\.(h|c)$/);
1540 # check for RCS/CVS revision markers
1541 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1542 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1545 # Blackfin: don't use __builtin_bfin_[cs]sync
1546 if ($line =~ /__builtin_bfin_csync/) {
1547 my $herevet = "$here\n" . cat_vet($line) . "\n";
1548 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1550 if ($line =~ /__builtin_bfin_ssync/) {
1551 my $herevet = "$here\n" . cat_vet($line) . "\n";
1552 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1555 # Check for potential 'bare' types
1556 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1558 if ($realcnt && $line =~ /.\s*\S/) {
1559 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1560 ctx_statement_block($linenr, $realcnt, 0);
1561 $stat =~ s/\n./\n /g;
1562 $cond =~ s/\n./\n /g;
1564 # Find the real next line.
1565 $realline_next = $line_nr_next;
1566 if (defined $realline_next &&
1567 (!defined $lines[$realline_next - 1] ||
1568 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1575 # Ignore goto labels.
1576 if ($s =~ /$Ident:\*$/s) {
1578 # Ignore functions being called
1579 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1581 } elsif ($s =~ /^.\s*else\b/s) {
1583 # declarations always start with types
1584 } 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) {
1587 possible($type, "A:" . $s);
1589 # definitions in global scope can only start with types
1590 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1591 possible($1, "B:" . $s);
1594 # any (foo ... *) is a pointer cast, and foo is a type
1595 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1596 possible($1, "C:" . $s);
1599 # Check for any sort of function declaration.
1600 # int foo(something bar, other baz);
1601 # void (*store_gdt)(x86_descr_ptr *);
1602 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1603 my ($name_len) = length($1);
1606 substr($ctx, 0, $name_len + 1, '');
1607 $ctx =~ s/\)[^\)]*$//;
1609 for my $arg (split(/\s*,\s*/, $ctx)) {
1610 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1612 possible($1, "D:" . $s);
1620 # Checks which may be anchored in the context.
1623 # Check for switch () and associated case and default
1624 # statements should be at the same indent.
1625 if ($line=~/\bswitch\s*\(.*\)/) {
1628 my @ctx = ctx_block_outer($linenr, $realcnt);
1630 for my $ctx (@ctx) {
1631 my ($clen, $cindent) = line_stats($ctx);
1632 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1633 $indent != $cindent) {
1634 $err .= "$sep$ctx\n";
1641 ERROR("switch and case should be at the same indent\n$hereline$err");
1645 # if/while/etc brace do not go on next line, unless defining a do while loop,
1646 # or if that brace on the next line is for something else
1647 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1648 my $pre_ctx = "$1$2";
1650 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1651 my $ctx_cnt = $realcnt - $#ctx - 1;
1652 my $ctx = join("\n", @ctx);
1654 my $ctx_ln = $linenr;
1655 my $ctx_skip = $realcnt;
1657 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1658 defined $lines[$ctx_ln - 1] &&
1659 $lines[$ctx_ln - 1] =~ /^-/)) {
1660 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1661 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1665 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1666 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1668 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1669 ERROR("that open brace { should be on the previous line\n" .
1670 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1672 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1673 $ctx =~ /\)\s*\;\s*$/ &&
1674 defined $lines[$ctx_ln - 1])
1676 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1677 if ($nindent > $indent) {
1678 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1679 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1684 # Check relative indent for conditionals and blocks.
1685 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1686 my ($s, $c) = ($stat, $cond);
1688 substr($s, 0, length($c), '');
1690 # Make sure we remove the line prefixes as we have
1691 # none on the first line, and are going to readd them
1695 # Find out how long the conditional actually is.
1696 my @newlines = ($c =~ /\n/gs);
1697 my $cond_lines = 1 + $#newlines;
1699 # We want to check the first line inside the block
1700 # starting at the end of the conditional, so remove:
1701 # 1) any blank line termination
1702 # 2) any opening brace { on end of the line
1704 my $continuation = 0;
1706 $s =~ s/^.*\bdo\b//;
1708 if ($s =~ s/^\s*\\//) {
1711 if ($s =~ s/^\s*?\n//) {
1716 # Also ignore a loop construct at the end of a
1717 # preprocessor statement.
1718 if (($prevline =~ /^.\s*#\s*define\s/ ||
1719 $prevline =~ /\\\s*$/) && $continuation == 0) {
1725 while ($cond_ptr != $cond_lines) {
1726 $cond_ptr = $cond_lines;
1728 # If we see an #else/#elif then the code
1730 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1735 # 1) blank lines, they should be at 0,
1736 # 2) preprocessor lines, and
1738 if ($continuation ||
1740 $s =~ /^\s*#\s*?/ ||
1741 $s =~ /^\s*$Ident\s*:/) {
1742 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1743 if ($s =~ s/^.*?\n//) {
1749 my (undef, $sindent) = line_stats("+" . $s);
1750 my $stat_real = raw_line($linenr, $cond_lines);
1752 # Check if either of these lines are modified, else
1753 # this is not this patch's fault.
1754 if (!defined($stat_real) ||
1755 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1758 if (defined($stat_real) && $cond_lines > 1) {
1759 $stat_real = "[...]\n$stat_real";
1762 #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";
1764 if ($check && (($sindent % 8) != 0 ||
1765 ($sindent <= $indent && $s ne ''))) {
1766 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1770 # Track the 'values' across context and added lines.
1771 my $opline = $line; $opline =~ s/^./ /;
1772 my ($curr_values, $curr_vars) =
1773 annotate_values($opline . "\n", $prev_values);
1774 $curr_values = $prev_values . $curr_values;
1776 my $outline = $opline; $outline =~ s/\t/ /g;
1777 print "$linenr > .$outline\n";
1778 print "$linenr > $curr_values\n";
1779 print "$linenr > $curr_vars\n";
1781 $prev_values = substr($curr_values, -1);
1783 #ignore lines not being added
1784 if ($line=~/^[^\+]/) {next;}
1786 # TEST: allow direct testing of the type matcher.
1788 if ($line =~ /^.\s*$Declare\s*$/) {
1789 ERROR("TEST: is type\n" . $herecurr);
1790 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1791 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1795 # TEST: allow direct testing of the attribute matcher.
1797 if ($line =~ /^.\s*$Modifier\s*$/) {
1798 ERROR("TEST: is attr\n" . $herecurr);
1799 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1800 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1805 # check for initialisation to aggregates open brace on the next line
1806 if ($line =~ /^.\s*{/ &&
1807 $prevline =~ /(?:^|[^=])=\s*$/) {
1808 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1812 # Checks which are anchored on the added line.
1815 # check for malformed paths in #include statements (uses RAW line)
1816 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1818 if ($path =~ m{//}) {
1819 ERROR("malformed #include filename\n" .
1824 # no C99 // comments
1825 if ($line =~ m{//}) {
1826 ERROR("do not use C99 // comments\n" . $herecurr);
1828 # Remove C99 comments.
1830 $opline =~ s@//.*@@;
1832 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1833 # the whole statement.
1834 #print "APW <$lines[$realline_next - 1]>\n";
1835 if (defined $realline_next &&
1836 exists $lines[$realline_next - 1] &&
1837 !defined $suppress_export{$realline_next} &&
1838 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1839 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1840 # Handle definitions which produce identifiers with
1843 # EXPORT_SYMBOL(something_foo);
1845 if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
1846 $name =~ /^${Ident}_$2/) {
1847 #print "FOO C name<$name>\n";
1848 $suppress_export{$realline_next} = 1;
1850 } elsif ($stat !~ /(?:
1852 ^.DEFINE_$Ident\(\Q$name\E\)|
1853 ^.DECLARE_$Ident\(\Q$name\E\)|
1854 ^.LIST_HEAD\(\Q$name\E\)|
1855 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1856 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
1858 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1859 $suppress_export{$realline_next} = 2;
1861 $suppress_export{$realline_next} = 1;
1864 if (!defined $suppress_export{$linenr} &&
1865 $prevline =~ /^.\s*$/ &&
1866 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1867 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1868 #print "FOO B <$lines[$linenr - 1]>\n";
1869 $suppress_export{$linenr} = 2;
1871 if (defined $suppress_export{$linenr} &&
1872 $suppress_export{$linenr} == 2) {
1873 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1876 # check for global initialisers.
1877 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1878 ERROR("do not initialise globals to 0 or NULL\n" .
1881 # check for static initialisers.
1882 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1883 ERROR("do not initialise statics to 0 or NULL\n" .
1887 # check for static const char * arrays.
1888 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
1889 WARN("static const char * array should probably be static const char * const\n" .
1893 # check for static char foo[] = "bar" declarations.
1894 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
1895 WARN("static char array declaration should probably be static const char\n" .
1899 # check for declarations of struct pci_device_id
1900 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
1901 WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
1904 # check for new typedefs, only function parameters and sparse annotations
1906 if ($line =~ /\btypedef\s/ &&
1907 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1908 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
1909 $line !~ /\b$typeTypedefs\b/ &&
1910 $line !~ /\b__bitwise(?:__|)\b/) {
1911 WARN("do not add new typedefs\n" . $herecurr);
1914 # * goes on variable not on type
1916 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1917 my ($from, $to) = ($1, $1);
1919 # Should start with a space.
1920 $to =~ s/^(\S)/ $1/;
1921 # Should not end with a space.
1923 # '*'s should not have spaces between.
1924 while ($to =~ s/\*\s+\*/\*\*/) {
1927 #print "from<$from> to<$to>\n";
1929 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1931 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1932 my ($from, $to, $ident) = ($1, $1, $2);
1934 # Should start with a space.
1935 $to =~ s/^(\S)/ $1/;
1936 # Should not end with a space.
1938 # '*'s should not have spaces between.
1939 while ($to =~ s/\*\s+\*/\*\*/) {
1941 # Modifiers should have spaces.
1942 $to =~ s/(\b$Modifier$)/$1 /;
1944 #print "from<$from> to<$to> ident<$ident>\n";
1945 if ($from ne $to && $ident !~ /^$Modifier$/) {
1946 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1950 # # no BUG() or BUG_ON()
1951 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1952 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1953 # print "$herecurr";
1957 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1958 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1961 # check for uses of printk_ratelimit
1962 if ($line =~ /\bprintk_ratelimit\s*\(/) {
1963 WARN("Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
1966 # printk should use KERN_* levels. Note that follow on printk's on the
1967 # same line do not need a level, so we use the current block context
1968 # to try and find and validate the current printk. In summary the current
1969 # printk includes all preceding printk's which have no newline on the end.
1970 # we assume the first bad printk is the one to report.
1971 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1973 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1974 #print "CHECK<$lines[$ln - 1]\n";
1975 # we have a preceding printk if it ends
1976 # with "\n" ignore it, else it is to blame
1977 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1978 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1985 WARN("printk() should include KERN_ facility level\n" . $herecurr);
1989 # function brace can't be on same line, except for #defines of do while,
1990 # or if closed on same line
1991 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1992 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1993 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1996 # open braces for enum, union and struct go on the same line.
1997 if ($line =~ /^.\s*{/ &&
1998 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1999 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
2002 # missing space after union, struct or enum definition
2003 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2004 WARN("missing space after $1 definition\n" . $herecurr);
2007 # check for spacing round square brackets; allowed:
2008 # 1. with a type on the left -- int [] a;
2009 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2010 # 3. inside a curly brace -- = { [0...10] = 5 }
2011 while ($line =~ /(.*?\s)\[/g) {
2012 my ($where, $prefix) = ($-[1], $1);
2013 if ($prefix !~ /$Type\s+$/ &&
2014 ($where != 0 || $prefix !~ /^.\s+$/) &&
2015 $prefix !~ /{\s+$/) {
2016 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
2020 # check for spaces between functions and their parentheses.
2021 while ($line =~ /($Ident)\s+\(/g) {
2023 my $ctx_before = substr($line, 0, $-[1]);
2024 my $ctx = "$ctx_before$name";
2026 # Ignore those directives where spaces _are_ permitted.
2028 if|for|while|switch|return|case|
2029 volatile|__volatile__|
2030 __attribute__|format|__extension__|
2034 # cpp #define statements have non-optional spaces, ie
2035 # if there is a space between the name and the open
2036 # parenthesis it is simply not a parameter group.
2037 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2039 # cpp #elif statement condition may start with a (
2040 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2042 # If this whole things ends with a type its most
2043 # likely a typedef for a function.
2044 } elsif ($ctx =~ /$Type$/) {
2047 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
2050 # Check operator spacing.
2051 if (!($line=~/\#\s*include/)) {
2053 <<=|>>=|<=|>=|==|!=|
2054 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2055 =>|->|<<|>>|<|>|=|!|~|
2056 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2059 my @elements = split(/($ops|;)/, $opline);
2062 my $blank = copy_spacing($opline);
2064 for (my $n = 0; $n < $#elements; $n += 2) {
2065 $off += length($elements[$n]);
2067 # Pick up the preceding and succeeding characters.
2068 my $ca = substr($opline, 0, $off);
2070 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2071 $cc = substr($opline, $off + length($elements[$n + 1]));
2073 my $cb = "$ca$;$cc";
2076 $a = 'V' if ($elements[$n] ne '');
2077 $a = 'W' if ($elements[$n] =~ /\s$/);
2078 $a = 'C' if ($elements[$n] =~ /$;$/);
2079 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2080 $a = 'O' if ($elements[$n] eq '');
2081 $a = 'E' if ($ca =~ /^\s*$/);
2083 my $op = $elements[$n + 1];
2086 if (defined $elements[$n + 2]) {
2087 $c = 'V' if ($elements[$n + 2] ne '');
2088 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2089 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2090 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2091 $c = 'O' if ($elements[$n + 2] eq '');
2092 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2097 my $ctx = "${a}x${c}";
2099 my $at = "(ctx:$ctx)";
2101 my $ptr = substr($blank, 0, $off) . "^";
2102 my $hereptr = "$hereline$ptr\n";
2104 # Pull out the value of this operator.
2105 my $op_type = substr($curr_values, $off + 1, 1);
2107 # Get the full operator variant.
2108 my $opv = $op . substr($curr_vars, $off, 1);
2110 # Ignore operators passed as parameters.
2111 if ($op_type ne 'V' &&
2112 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2115 # } elsif ($op =~ /^$;+$/) {
2117 # ; should have either the end of line or a space or \ after it
2118 } elsif ($op eq ';') {
2119 if ($ctx !~ /.x[WEBC]/ &&
2120 $cc !~ /^\\/ && $cc !~ /^;/) {
2121 ERROR("space required after that '$op' $at\n" . $hereptr);
2125 } elsif ($op eq '//') {
2129 # : when part of a bitfield
2130 } elsif ($op eq '->' || $opv eq ':B') {
2131 if ($ctx =~ /Wx.|.xW/) {
2132 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
2135 # , must have a space on the right.
2136 } elsif ($op eq ',') {
2137 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2138 ERROR("space required after that '$op' $at\n" . $hereptr);
2141 # '*' as part of a type definition -- reported already.
2142 } elsif ($opv eq '*_') {
2143 #warn "'*' is part of type\n";
2145 # unary operators should have a space before and
2146 # none after. May be left adjacent to another
2147 # unary operator, or a cast
2148 } elsif ($op eq '!' || $op eq '~' ||
2149 $opv eq '*U' || $opv eq '-U' ||
2150 $opv eq '&U' || $opv eq '&&U') {
2151 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2152 ERROR("space required before that '$op' $at\n" . $hereptr);
2154 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2155 # A unary '*' may be const
2157 } elsif ($ctx =~ /.xW/) {
2158 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2161 # unary ++ and unary -- are allowed no space on one side.
2162 } elsif ($op eq '++' or $op eq '--') {
2163 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2164 ERROR("space required one side of that '$op' $at\n" . $hereptr);
2166 if ($ctx =~ /Wx[BE]/ ||
2167 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2168 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2170 if ($ctx =~ /ExW/) {
2171 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2175 # << and >> may either have or not have spaces both sides
2176 } elsif ($op eq '<<' or $op eq '>>' or
2177 $op eq '&' or $op eq '^' or $op eq '|' or
2178 $op eq '+' or $op eq '-' or
2179 $op eq '*' or $op eq '/' or
2182 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2183 ERROR("need consistent spacing around '$op' $at\n" .
2187 # A colon needs no spaces before when it is
2188 # terminating a case value or a label.
2189 } elsif ($opv eq ':C' || $opv eq ':L') {
2190 if ($ctx =~ /Wx./) {
2191 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2194 # All the others need spaces both sides.
2195 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2198 # Ignore email addresses <foo@bar>
2200 $cc =~ /^\S+\@\S+>/) ||
2202 $ca =~ /<\S+\@\S+$/))
2208 if (($opv eq ':O' && $ca =~ /\?$/) ||
2209 ($op eq '?' && $cc =~ /^:/)) {
2214 ERROR("spaces required around that '$op' $at\n" . $hereptr);
2217 $off += length($elements[$n + 1]);
2221 # check for multiple assignments
2222 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2223 CHK("multiple assignments should be avoided\n" . $herecurr);
2226 ## # check for multiple declarations, allowing for a function declaration
2228 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2229 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2231 ## # Remove any bracketed sections to ensure we do not
2232 ## # falsly report the parameters of functions.
2234 ## while ($ln =~ s/\([^\(\)]*\)//g) {
2236 ## if ($ln =~ /,/) {
2237 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2241 #need space before brace following if, while, etc
2242 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2244 ERROR("space required before the open brace '{'\n" . $herecurr);
2247 # closing brace should have a space following it when it has anything
2249 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2250 ERROR("space required after that close brace '}'\n" . $herecurr);
2253 # check spacing on square brackets
2254 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2255 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2257 if ($line =~ /\s\]/) {
2258 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2261 # check spacing on parentheses
2262 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2263 $line !~ /for\s*\(\s+;/) {
2264 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2266 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2267 $line !~ /for\s*\(.*;\s+\)/ &&
2268 $line !~ /:\s+\)/) {
2269 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2272 #goto labels aren't indented, allow a single space however
2273 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2274 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2275 WARN("labels should not be indented\n" . $herecurr);
2278 # Return is not a function.
2279 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2283 # Flatten any parentheses
2284 $value =~ s/\(/ \(/g;
2285 $value =~ s/\)/\) /g;
2286 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2287 $value !~ /(?:$Ident|-?$Constant)\s*
2289 (?:$Ident|-?$Constant)/x &&
2290 $value =~ s/\([^\(\)]*\)/1/) {
2292 #print "value<$value>\n";
2293 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2294 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2296 } elsif ($spacing !~ /\s+/) {
2297 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2300 # Return of what appears to be an errno should normally be -'ve
2301 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2303 if ($name ne 'EOF' && $name ne 'ERROR') {
2304 WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2308 # typecasts on min/max could be min_t/max_t
2309 if ($line =~ /^\+(?:.*?)\b(min|max)\s*\($Typecast{0,1}($LvalOrFunc)\s*,\s*$Typecast{0,1}($LvalOrFunc)\s*\)/) {
2310 if (defined $2 || defined $8) {
2312 my $cast1 = deparenthesize($2);
2314 my $cast2 = deparenthesize($8);
2318 if ($cast1 ne "" && $cast2 ne "") {
2319 $cast = "$cast1 or $cast2";
2320 } elsif ($cast1 ne "") {
2325 WARN("$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . $herecurr);
2329 # Need a space before open parenthesis after if, while etc
2330 if ($line=~/\b(if|while|for|switch)\(/) {
2331 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2334 # Check for illegal assignment in if conditional -- and check for trailing
2335 # statements after the conditional.
2336 if ($line =~ /do\s*(?!{)/) {
2337 my ($stat_next) = ctx_statement_block($line_nr_next,
2338 $remain_next, $off_next);
2339 $stat_next =~ s/\n./\n /g;
2340 ##print "stat<$stat> stat_next<$stat_next>\n";
2342 if ($stat_next =~ /^\s*while\b/) {
2343 # If the statement carries leading newlines,
2344 # then count those as offsets.
2346 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2348 statement_rawlines($whitespace) - 1;
2350 $suppress_whiletrailers{$line_nr_next +
2354 if (!defined $suppress_whiletrailers{$linenr} &&
2355 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2356 my ($s, $c) = ($stat, $cond);
2358 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2359 ERROR("do not use assignment in if condition\n" . $herecurr);
2362 # Find out what is on the end of the line after the
2364 substr($s, 0, length($c), '');
2366 $s =~ s/$;//g; # Remove any comments
2367 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2368 $c !~ /}\s*while\s*/)
2370 # Find out how long the conditional actually is.
2371 my @newlines = ($c =~ /\n/gs);
2372 my $cond_lines = 1 + $#newlines;
2375 $stat_real = raw_line($linenr, $cond_lines)
2376 . "\n" if ($cond_lines);
2377 if (defined($stat_real) && $cond_lines > 1) {
2378 $stat_real = "[...]\n$stat_real";
2381 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2385 # Check for bitwise tests written as boolean
2397 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2400 # if and else should not have general statements after it
2401 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2403 $s =~ s/$;//g; # Remove any comments
2404 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2405 ERROR("trailing statements should be on next line\n" . $herecurr);
2408 # if should not continue a brace
2409 if ($line =~ /}\s*if\b/) {
2410 ERROR("trailing statements should be on next line\n" .
2413 # case and default should not have general statements after them
2414 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2416 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2420 ERROR("trailing statements should be on next line\n" . $herecurr);
2423 # Check for }<nl>else {, these must be at the same
2424 # indent level to be relevant to each other.
2425 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2426 $previndent == $indent) {
2427 ERROR("else should follow close brace '}'\n" . $hereprev);
2430 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2431 $previndent == $indent) {
2432 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2434 # Find out what is on the end of the line after the
2436 substr($s, 0, length($c), '');
2439 if ($s =~ /^\s*;/) {
2440 ERROR("while should follow close brace '}'\n" . $hereprev);
2444 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2445 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2446 # print "No studly caps, use _\n";
2447 # print "$herecurr";
2451 #no spaces allowed after \ in define
2452 if ($line=~/\#\s*define.*\\\s$/) {
2453 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
2456 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2457 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2459 my $checkfile = "include/linux/$file";
2460 if (-f "$root/$checkfile" &&
2461 $realfile ne $checkfile &&
2462 $1 !~ /$allowed_asm_includes/)
2464 if ($realfile =~ m{^arch/}) {
2465 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2467 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2472 # multi-statement macros should be enclosed in a do while loop, grab the
2473 # first statement and ensure its the whole macro if its not enclosed
2474 # in a known good container
2475 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2476 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2479 my ($off, $dstat, $dcond, $rest);
2482 my $args = defined($1);
2484 # Find the end of the macro and limit our statement
2486 while ($cnt > 0 && defined $lines[$ln - 1] &&
2487 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2489 $ctx .= $rawlines[$ln - 1] . "\n";
2490 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2493 $ctx .= $rawlines[$ln - 1];
2495 ($dstat, $dcond, $ln, $cnt, $off) =
2496 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2497 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2498 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2500 # Extract the remainder of the define (if any) and
2501 # rip off surrounding spaces, and trailing \'s.
2503 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2504 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2505 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2506 $rest .= substr($lines[$ln - 1], $off) . "\n";
2512 $rest =~ s/\\\n.//g;
2516 # Clean up the original statement.
2518 substr($dstat, 0, length($dcond), '');
2520 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2523 $dstat =~ s/\\\n.//g;
2524 $dstat =~ s/^\s*//s;
2525 $dstat =~ s/\s*$//s;
2527 # Flatten any parentheses and braces
2528 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2529 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2530 $dstat =~ s/\[[^\{\}]*\]/1/)
2534 my $exceptions = qr{
2546 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2547 if ($rest ne '' && $rest ne ',') {
2548 if ($rest !~ /while\s*\(/ &&
2549 $dstat !~ /$exceptions/)
2551 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2554 } elsif ($ctx !~ /;/) {
2556 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2557 $dstat !~ /$exceptions/ &&
2558 $dstat !~ /^\.$Ident\s*=/ &&
2559 $dstat =~ /$Operators/)
2561 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2566 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2567 # all assignments may have only one of the following with an assignment:
2570 # VMLINUX_SYMBOL(...)
2571 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2572 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2575 # check for redundant bracing round if etc
2576 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2577 my ($level, $endln, @chunks) =
2578 ctx_statement_full($linenr, $realcnt, 1);
2579 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2580 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2581 if ($#chunks > 0 && $level == 0) {
2584 my $herectx = $here . "\n";
2585 my $ln = $linenr - 1;
2586 for my $chunk (@chunks) {
2587 my ($cond, $block) = @{$chunk};
2589 # If the condition carries leading newlines, then count those as offsets.
2590 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2591 my $offset = statement_rawlines($whitespace) - 1;
2593 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2595 # We have looked at and allowed this specific line.
2596 $suppress_ifbraces{$ln + $offset} = 1;
2598 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2599 $ln += statement_rawlines($block) - 1;
2601 substr($block, 0, length($cond), '');
2603 $seen++ if ($block =~ /^\s*{/);
2605 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2606 if (statement_lines($cond) > 1) {
2607 #print "APW: ALLOWED: cond<$cond>\n";
2610 if ($block =~/\b(?:if|for|while)\b/) {
2611 #print "APW: ALLOWED: block<$block>\n";
2614 if (statement_block_size($block) > 1) {
2615 #print "APW: ALLOWED: lines block<$block>\n";
2619 if ($seen && !$allowed) {
2620 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
2624 if (!defined $suppress_ifbraces{$linenr - 1} &&
2625 $line =~ /\b(if|while|for|else)\b/) {
2628 # Check the pre-context.
2629 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2630 #print "APW: ALLOWED: pre<$1>\n";
2634 my ($level, $endln, @chunks) =
2635 ctx_statement_full($linenr, $realcnt, $-[0]);
2637 # Check the condition.
2638 my ($cond, $block) = @{$chunks[0]};
2639 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2640 if (defined $cond) {
2641 substr($block, 0, length($cond), '');
2643 if (statement_lines($cond) > 1) {
2644 #print "APW: ALLOWED: cond<$cond>\n";
2647 if ($block =~/\b(?:if|for|while)\b/) {
2648 #print "APW: ALLOWED: block<$block>\n";
2651 if (statement_block_size($block) > 1) {
2652 #print "APW: ALLOWED: lines block<$block>\n";
2655 # Check the post-context.
2656 if (defined $chunks[1]) {
2657 my ($cond, $block) = @{$chunks[1]};
2658 if (defined $cond) {
2659 substr($block, 0, length($cond), '');
2661 if ($block =~ /^\s*\{/) {
2662 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2666 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2667 my $herectx = $here . "\n";;
2668 my $cnt = statement_rawlines($block);
2670 for (my $n = 0; $n < $cnt; $n++) {
2671 $herectx .= raw_line($linenr, $n) . "\n";;
2674 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2678 # don't include deprecated include files (uses RAW line)
2679 for my $inc (@dep_includes) {
2680 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2681 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2685 # don't use deprecated functions
2686 for my $func (@dep_functions) {
2687 if ($line =~ /\b$func\b/) {
2688 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2692 # no volatiles please
2693 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2694 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2695 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2699 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2700 CHK("if this code is redundant consider removing it\n" .
2704 # check for needless kfree() checks
2705 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2707 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2708 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2711 # check for needless usb_free_urb() checks
2712 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2714 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2715 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2719 # prefer usleep_range over udelay
2720 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2721 # ignore udelay's < 10, however
2722 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2723 CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2727 # warn about unexpectedly long msleep's
2728 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2730 WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2734 # warn about #ifdefs in C files
2735 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2736 # print "#ifdef in C files should be avoided\n";
2737 # print "$herecurr";
2741 # warn about spacing in #ifdefs
2742 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2743 ERROR("exactly one space required after that #$1\n" . $herecurr);
2746 # check for spinlock_t definitions without a comment.
2747 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2748 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
2750 if (!ctx_has_comment($first_line, $linenr)) {
2751 CHK("$1 definition without comment\n" . $herecurr);
2754 # check for memory barriers without a comment.
2755 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2756 if (!ctx_has_comment($first_line, $linenr)) {
2757 CHK("memory barrier without comment\n" . $herecurr);
2760 # check of hardware specific defines
2761 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2762 CHK("architecture specific defines should be avoided\n" . $herecurr);
2765 # Check that the storage class is at the beginning of a declaration
2766 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2767 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2770 # check the location of the inline attribute, that it is between
2771 # storage class and type.
2772 if ($line =~ /\b$Type\s+$Inline\b/ ||
2773 $line =~ /\b$Inline\s+$Storage\b/) {
2774 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2777 # Check for __inline__ and __inline, prefer inline
2778 if ($line =~ /\b(__inline__|__inline)\b/) {
2779 WARN("plain inline is preferred over $1\n" . $herecurr);
2782 # Check for __attribute__ packed, prefer __packed
2783 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
2784 WARN("__packed is preferred over __attribute__((packed))\n" . $herecurr);
2787 # check for sizeof(&)
2788 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2789 WARN("sizeof(& should be avoided\n" . $herecurr);
2792 # check for line continuations in quoted strings with odd counts of "
2793 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
2794 WARN("Avoid line continuations in quoted strings\n" . $herecurr);
2797 # check for new externs in .c files.
2798 if ($realfile =~ /\.c$/ && defined $stat &&
2799 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2801 my $function_name = $1;
2802 my $paren_space = $2;
2805 if (defined $cond) {
2806 substr($s, 0, length($cond), '');
2808 if ($s =~ /^\s*;/ &&
2809 $function_name ne 'uninitialized_var')
2811 WARN("externs should be avoided in .c files\n" . $herecurr);
2814 if ($paren_space =~ /\n/) {
2815 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2818 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2819 $stat =~ /^.\s*extern\s+/)
2821 WARN("externs should be avoided in .c files\n" . $herecurr);
2824 # checks for new __setup's
2825 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2828 if (!grep(/$name/, @setup_docs)) {
2829 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2833 # check for pointless casting of kmalloc return
2834 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
2835 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2838 # check for multiple semicolons
2839 if ($line =~ /;\s*;\s*$/) {
2840 WARN("Statements terminations use 1 semicolon\n" . $herecurr);
2843 # check for gcc specific __FUNCTION__
2844 if ($line =~ /__FUNCTION__/) {
2845 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2848 # check for semaphores initialized locked
2849 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
2850 WARN("consider using a completion\n" . $herecurr);
2853 # recommend kstrto* over simple_strto*
2854 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2855 WARN("consider using kstrto* in preference to simple_$1\n" . $herecurr);
2857 # check for __initcall(), use device_initcall() explicitly please
2858 if ($line =~ /^.\s*__initcall\s*\(/) {
2859 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2861 # check for various ops structs, ensure they are const.
2862 my $struct_ops = qr{acpi_dock_ops|
2863 address_space_operations|
2865 block_device_operations|
2870 file_lock_operations|
2880 lock_manager_operations|
2886 pipe_buf_operations|
2887 platform_hibernation_ops|
2888 platform_suspend_ops|
2893 soc_pcmcia_socket_ops|
2899 if ($line !~ /\bconst\b/ &&
2900 $line =~ /\bstruct\s+($struct_ops)\b/) {
2901 WARN("struct $1 should normally be const\n" .
2905 # use of NR_CPUS is usually wrong
2906 # ignore definitions of NR_CPUS and usage to define arrays as likely right
2907 if ($line =~ /\bNR_CPUS\b/ &&
2908 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2909 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2910 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2911 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2912 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2914 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2917 # check for %L{u,d,i} in strings
2919 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2920 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2921 $string =~ s/%%/__/g;
2922 if ($string =~ /(?<!%)%L[udi]/) {
2923 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2928 # whine mightly about in_atomic
2929 if ($line =~ /\bin_atomic\s*\(/) {
2930 if ($realfile =~ m@^drivers/@) {
2931 ERROR("do not use in_atomic in drivers\n" . $herecurr);
2932 } elsif ($realfile !~ m@^kernel/@) {
2933 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2937 # check for lockdep_set_novalidate_class
2938 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
2939 $line =~ /__lockdep_no_validate__\s*\)/ ) {
2940 if ($realfile !~ m@^kernel/lockdep@ &&
2941 $realfile !~ m@^include/linux/lockdep@ &&
2942 $realfile !~ m@^drivers/base/core@) {
2943 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
2947 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
2948 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
2949 WARN("Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
2952 # Check for memset with swapped arguments
2953 if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
2954 ERROR("memset size is 3rd argument, not the second.\n" . $herecurr);
2958 # If we have no input at all, then there is nothing to report on
2959 # so just keep quiet.
2960 if ($#rawlines == -1) {
2964 # In mailback mode only produce a report in the negative, for
2965 # things that appear to be patches.
2966 if ($mailback && ($clean == 1 || !$is_patch)) {
2970 # This is not a patch, and we are are in 'no-patch' mode so
2972 if (!$chk_patch && !$is_patch) {
2977 ERROR("Does not appear to be a unified-diff format patch\n");
2979 if ($is_patch && $chk_signoff && $signoff == 0) {
2980 ERROR("Missing Signed-off-by: line(s)\n");
2983 print report_dump();
2984 if ($summary && !($clean == 1 && $quiet == 1)) {
2985 print "$filename " if ($summary_file);
2986 print "total: $cnt_error errors, $cnt_warn warnings, " .
2987 (($check)? "$cnt_chk checks, " : "") .
2988 "$cnt_lines lines checked\n";
2989 print "\n" if ($quiet == 0);
2993 # If there were whitespace errors which cleanpatch can fix
2994 # then suggest that.
2995 if ($rpt_cleaners) {
2996 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2997 print " scripts/cleanfile\n\n";
3002 if ($clean == 1 && $quiet == 0) {
3003 print "$vname has no obvious style problems and is ready for submission.\n"
3005 if ($clean == 0 && $quiet == 0) {
3006 print "$vname has style problems, please review. If any of these errors\n";
3007 print "are false positives report them to the maintainer, see\n";
3008 print "CHECKPATCH in MAINTAINERS.\n";