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)|
220 our $signature_tags = qr{(?xi:
232 qr{(?:unsigned\s+)?char},
233 qr{(?:unsigned\s+)?short},
234 qr{(?:unsigned\s+)?int},
235 qr{(?:unsigned\s+)?long},
236 qr{(?:unsigned\s+)?long\s+int},
237 qr{(?:unsigned\s+)?long\s+long},
238 qr{(?:unsigned\s+)?long\s+long\s+int},
247 qr{${Ident}_handler},
248 qr{${Ident}_handler_fn},
250 our @modifierList = (
254 our $allowed_asm_includes = qr{(?x:
258 # memory.h: ARM has a custom one
261 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
262 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
263 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
265 (?:$Modifier\s+|const\s+)*
267 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
271 (?:\s+$Modifier|\s+const)*
275 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
276 (?:\s+$Inline|\s+$Modifier)*
278 $Declare = qr{(?:$Storage\s+)?$Type};
282 our $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/;
284 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
285 our $LvalOrFunc = qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*};
289 return "" if (!defined($string));
290 $string =~ s@^\s*\(\s*@@g;
291 $string =~ s@\s*\)\s*$@@g;
292 $string =~ s@\s+@ @g;
296 $chk_signoff = 0 if ($file);
298 my @dep_includes = ();
299 my @dep_functions = ();
300 my $removal = "Documentation/feature-removal-schedule.txt";
301 if ($tree && -f "$root/$removal") {
302 open(my $REMOVE, '<', "$root/$removal") ||
303 die "$P: $removal: open failed - $!\n";
305 if (/^Check:\s+(.*\S)/) {
306 for my $entry (split(/[, ]+/, $1)) {
307 if ($entry =~ m@include/(.*)@) {
308 push(@dep_includes, $1);
310 } elsif ($entry !~ m@/@) {
311 push(@dep_functions, $entry);
322 for my $filename (@ARGV) {
325 open($FILE, '-|', "diff -u /dev/null $filename") ||
326 die "$P: $filename: diff failed - $!\n";
327 } elsif ($filename eq '-') {
328 open($FILE, '<&STDIN');
330 open($FILE, '<', "$filename") ||
331 die "$P: $filename: open failed - $!\n";
333 if ($filename eq '-') {
334 $vname = 'Your patch';
343 if (!process($filename)) {
352 sub top_of_kernel_tree {
356 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
357 "README", "Documentation", "arch", "include", "drivers",
358 "fs", "init", "ipc", "kernel", "lib", "scripts",
361 foreach my $check (@tree_check) {
362 if (! -e $root . '/' . $check) {
370 my ($formatted_email) = @_;
376 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
379 $comment = $3 if defined $3;
380 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
382 $comment = $2 if defined $2;
383 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
385 $comment = $2 if defined $2;
386 $formatted_email =~ s/$address.*$//;
387 $name = $formatted_email;
388 $name =~ s/^\s+|\s+$//g;
389 $name =~ s/^\"|\"$//g;
390 # If there's a name left after stripping spaces and
391 # leading quotes, and the address doesn't have both
392 # leading and trailing angle brackets, the address
394 # "joe smith joe@smith.com" bad
395 # "joe smith <joe@smith.com" bad
396 if ($name ne "" && $address !~ /^<[^>]+>$/) {
403 $name =~ s/^\s+|\s+$//g;
404 $name =~ s/^\"|\"$//g;
405 $address =~ s/^\s+|\s+$//g;
406 $address =~ s/^\<|\>$//g;
408 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
409 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
413 return ($name, $address, $comment);
417 my ($name, $address) = @_;
421 $name =~ s/^\s+|\s+$//g;
422 $name =~ s/^\"|\"$//g;
423 $address =~ s/^\s+|\s+$//g;
425 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
426 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
431 $formatted_email = "$address";
433 $formatted_email = "$name <$address>";
436 return $formatted_email;
444 for my $c (split(//, $str)) {
448 for (; ($n % 8) != 0; $n++) {
460 (my $res = shift) =~ tr/\t/ /c;
467 # Drop the diff line leader and expand tabs
469 $line = expand_tabs($line);
471 # Pick the indent from the front of the line.
472 my ($white) = ($line =~ /^(\s*)/);
474 return (length($line), length($white));
477 my $sanitise_quote = '';
479 sub sanitise_line_reset {
480 my ($in_comment) = @_;
483 $sanitise_quote = '*/';
485 $sanitise_quote = '';
498 # Always copy over the diff marker.
499 $res = substr($line, 0, 1);
501 for ($off = 1; $off < length($line); $off++) {
502 $c = substr($line, $off, 1);
504 # Comments we are wacking completly including the begin
505 # and end, all to $;.
506 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
507 $sanitise_quote = '*/';
509 substr($res, $off, 2, "$;$;");
513 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
514 $sanitise_quote = '';
515 substr($res, $off, 2, "$;$;");
519 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
520 $sanitise_quote = '//';
522 substr($res, $off, 2, $sanitise_quote);
527 # A \ in a string means ignore the next character.
528 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
530 substr($res, $off, 2, 'XX');
535 if ($c eq "'" || $c eq '"') {
536 if ($sanitise_quote eq '') {
537 $sanitise_quote = $c;
539 substr($res, $off, 1, $c);
541 } elsif ($sanitise_quote eq $c) {
542 $sanitise_quote = '';
546 #print "c<$c> SQ<$sanitise_quote>\n";
547 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
548 substr($res, $off, 1, $;);
549 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
550 substr($res, $off, 1, $;);
551 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
552 substr($res, $off, 1, 'X');
554 substr($res, $off, 1, $c);
558 if ($sanitise_quote eq '//') {
559 $sanitise_quote = '';
562 # The pathname on a #include may be surrounded by '<' and '>'.
563 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
564 my $clean = 'X' x length($1);
565 $res =~ s@\<.*\>@<$clean>@;
567 # The whole of a #error is a string.
568 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
569 my $clean = 'X' x length($1);
570 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
576 sub ctx_statement_block {
577 my ($linenr, $remain, $off) = @_;
578 my $line = $linenr - 1;
595 @stack = (['', 0]) if ($#stack == -1);
597 #warn "CSB: blk<$blk> remain<$remain>\n";
598 # If we are about to drop off the end, pull in more
601 for (; $remain > 0; $line++) {
602 last if (!defined $lines[$line]);
603 next if ($lines[$line] =~ /^-/);
606 $blk .= $lines[$line] . "\n";
611 # Bail if there is no further context.
612 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
618 $c = substr($blk, $off, 1);
619 $remainder = substr($blk, $off);
621 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
623 # Handle nested #if/#else.
624 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
625 push(@stack, [ $type, $level ]);
626 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
627 ($type, $level) = @{$stack[$#stack - 1]};
628 } elsif ($remainder =~ /^#\s*endif\b/) {
629 ($type, $level) = @{pop(@stack)};
632 # Statement ends at the ';' or a close '}' at the
634 if ($level == 0 && $c eq ';') {
638 # An else is really a conditional as long as its not else if
639 if ($level == 0 && $coff_set == 0 &&
640 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
641 $remainder =~ /^(else)(?:\s|{)/ &&
642 $remainder !~ /^else\s+if\b/) {
643 $coff = $off + length($1) - 1;
645 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
646 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
649 if (($type eq '' || $type eq '(') && $c eq '(') {
653 if ($type eq '(' && $c eq ')') {
655 $type = ($level != 0)? '(' : '';
657 if ($level == 0 && $coff < $soff) {
660 #warn "CSB: mark coff<$coff>\n";
663 if (($type eq '' || $type eq '{') && $c eq '{') {
667 if ($type eq '{' && $c eq '}') {
669 $type = ($level != 0)? '{' : '';
672 if (substr($blk, $off + 1, 1) eq ';') {
680 # We are truly at the end, so shuffle to the next line.
687 my $statement = substr($blk, $soff, $off - $soff + 1);
688 my $condition = substr($blk, $soff, $coff - $soff + 1);
690 #warn "STATEMENT<$statement>\n";
691 #warn "CONDITION<$condition>\n";
693 #print "coff<$coff> soff<$off> loff<$loff>\n";
695 return ($statement, $condition,
696 $line, $remain + 1, $off - $loff + 1, $level);
699 sub statement_lines {
702 # Strip the diff line prefixes and rip blank lines at start and end.
703 $stmt =~ s/(^|\n)./$1/g;
707 my @stmt_lines = ($stmt =~ /\n/g);
709 return $#stmt_lines + 2;
712 sub statement_rawlines {
715 my @stmt_lines = ($stmt =~ /\n/g);
717 return $#stmt_lines + 2;
720 sub statement_block_size {
723 $stmt =~ s/(^|\n)./$1/g;
729 my @stmt_lines = ($stmt =~ /\n/g);
730 my @stmt_statements = ($stmt =~ /;/g);
732 my $stmt_lines = $#stmt_lines + 2;
733 my $stmt_statements = $#stmt_statements + 1;
735 if ($stmt_lines > $stmt_statements) {
738 return $stmt_statements;
742 sub ctx_statement_full {
743 my ($linenr, $remain, $off) = @_;
744 my ($statement, $condition, $level);
748 # Grab the first conditional/block pair.
749 ($statement, $condition, $linenr, $remain, $off, $level) =
750 ctx_statement_block($linenr, $remain, $off);
751 #print "F: c<$condition> s<$statement> remain<$remain>\n";
752 push(@chunks, [ $condition, $statement ]);
753 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
754 return ($level, $linenr, @chunks);
757 # Pull in the following conditional/block pairs and see if they
758 # could continue the statement.
760 ($statement, $condition, $linenr, $remain, $off, $level) =
761 ctx_statement_block($linenr, $remain, $off);
762 #print "C: c<$condition> s<$statement> remain<$remain>\n";
763 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
765 push(@chunks, [ $condition, $statement ]);
768 return ($level, $linenr, @chunks);
772 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
774 my $start = $linenr - 1;
781 my @stack = ($level);
782 for ($line = $start; $remain > 0; $line++) {
783 next if ($rawlines[$line] =~ /^-/);
786 $blk .= $rawlines[$line];
788 # Handle nested #if/#else.
789 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
790 push(@stack, $level);
791 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
792 $level = $stack[$#stack - 1];
793 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
794 $level = pop(@stack);
797 foreach my $c (split(//, $lines[$line])) {
798 ##print "C<$c>L<$level><$open$close>O<$off>\n";
804 if ($c eq $close && $level > 0) {
806 last if ($level == 0);
807 } elsif ($c eq $open) {
812 if (!$outer || $level <= 1) {
813 push(@res, $rawlines[$line]);
816 last if ($level == 0);
819 return ($level, @res);
821 sub ctx_block_outer {
822 my ($linenr, $remain) = @_;
824 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
828 my ($linenr, $remain) = @_;
830 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
834 my ($linenr, $remain, $off) = @_;
836 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
839 sub ctx_block_level {
840 my ($linenr, $remain) = @_;
842 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
844 sub ctx_statement_level {
845 my ($linenr, $remain, $off) = @_;
847 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
850 sub ctx_locate_comment {
851 my ($first_line, $end_line) = @_;
853 # Catch a comment on the end of the line itself.
854 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
855 return $current_comment if (defined $current_comment);
857 # Look through the context and try and figure out if there is a
860 $current_comment = '';
861 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
862 my $line = $rawlines[$linenr - 1];
864 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
867 if ($line =~ m@/\*@) {
870 if (!$in_comment && $current_comment ne '') {
871 $current_comment = '';
873 $current_comment .= $line . "\n" if ($in_comment);
874 if ($line =~ m@\*/@) {
879 chomp($current_comment);
880 return($current_comment);
882 sub ctx_has_comment {
883 my ($first_line, $end_line) = @_;
884 my $cmt = ctx_locate_comment($first_line, $end_line);
886 ##print "LINE: $rawlines[$end_line - 1 ]\n";
887 ##print "CMMT: $cmt\n";
893 my ($linenr, $cnt) = @_;
895 my $offset = $linenr - 1;
900 $line = $rawlines[$offset++];
901 next if (defined($line) && $line =~ /^-/);
913 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
916 $coded = sprintf("^%c", unpack('C', $2) + 64);
925 my $av_preprocessor = 0;
931 $av_preprocessor = 0;
933 @av_paren_type = ('E');
934 $av_pend_colon = 'O';
937 sub annotate_values {
938 my ($stream, $type) = @_;
941 my $var = '_' x length($stream);
944 print "$stream\n" if ($dbg_values > 1);
946 while (length($cur)) {
947 @av_paren_type = ('E') if ($#av_paren_type < 0);
948 print " <" . join('', @av_paren_type) .
949 "> <$type> <$av_pending>" if ($dbg_values > 1);
950 if ($cur =~ /^(\s+)/o) {
951 print "WS($1)\n" if ($dbg_values > 1);
952 if ($1 =~ /\n/ && $av_preprocessor) {
953 $type = pop(@av_paren_type);
954 $av_preprocessor = 0;
957 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
958 print "CAST($1)\n" if ($dbg_values > 1);
959 push(@av_paren_type, $type);
962 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
963 print "DECLARE($1)\n" if ($dbg_values > 1);
966 } elsif ($cur =~ /^($Modifier)\s*/) {
967 print "MODIFIER($1)\n" if ($dbg_values > 1);
970 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
971 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
972 $av_preprocessor = 1;
973 push(@av_paren_type, $type);
979 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
980 print "UNDEF($1)\n" if ($dbg_values > 1);
981 $av_preprocessor = 1;
982 push(@av_paren_type, $type);
984 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
985 print "PRE_START($1)\n" if ($dbg_values > 1);
986 $av_preprocessor = 1;
988 push(@av_paren_type, $type);
989 push(@av_paren_type, $type);
992 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
993 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
994 $av_preprocessor = 1;
996 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1000 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1001 print "PRE_END($1)\n" if ($dbg_values > 1);
1003 $av_preprocessor = 1;
1005 # Assume all arms of the conditional end as this
1006 # one does, and continue as if the #endif was not here.
1007 pop(@av_paren_type);
1008 push(@av_paren_type, $type);
1011 } elsif ($cur =~ /^(\\\n)/o) {
1012 print "PRECONT($1)\n" if ($dbg_values > 1);
1014 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1015 print "ATTR($1)\n" if ($dbg_values > 1);
1016 $av_pending = $type;
1019 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1020 print "SIZEOF($1)\n" if ($dbg_values > 1);
1026 } elsif ($cur =~ /^(if|while|for)\b/o) {
1027 print "COND($1)\n" if ($dbg_values > 1);
1031 } elsif ($cur =~/^(case)/o) {
1032 print "CASE($1)\n" if ($dbg_values > 1);
1033 $av_pend_colon = 'C';
1036 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1037 print "KEYWORD($1)\n" if ($dbg_values > 1);
1040 } elsif ($cur =~ /^(\()/o) {
1041 print "PAREN('$1')\n" if ($dbg_values > 1);
1042 push(@av_paren_type, $av_pending);
1046 } elsif ($cur =~ /^(\))/o) {
1047 my $new_type = pop(@av_paren_type);
1048 if ($new_type ne '_') {
1050 print "PAREN('$1') -> $type\n"
1051 if ($dbg_values > 1);
1053 print "PAREN('$1')\n" if ($dbg_values > 1);
1056 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1057 print "FUNC($1)\n" if ($dbg_values > 1);
1061 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1062 if (defined $2 && $type eq 'C' || $type eq 'T') {
1063 $av_pend_colon = 'B';
1064 } elsif ($type eq 'E') {
1065 $av_pend_colon = 'L';
1067 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1070 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1071 print "IDENT($1)\n" if ($dbg_values > 1);
1074 } elsif ($cur =~ /^($Assignment)/o) {
1075 print "ASSIGN($1)\n" if ($dbg_values > 1);
1078 } elsif ($cur =~/^(;|{|})/) {
1079 print "END($1)\n" if ($dbg_values > 1);
1081 $av_pend_colon = 'O';
1083 } elsif ($cur =~/^(,)/) {
1084 print "COMMA($1)\n" if ($dbg_values > 1);
1087 } elsif ($cur =~ /^(\?)/o) {
1088 print "QUESTION($1)\n" if ($dbg_values > 1);
1091 } elsif ($cur =~ /^(:)/o) {
1092 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1094 substr($var, length($res), 1, $av_pend_colon);
1095 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1100 $av_pend_colon = 'O';
1102 } elsif ($cur =~ /^(\[)/o) {
1103 print "CLOSE($1)\n" if ($dbg_values > 1);
1106 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1109 print "OPV($1)\n" if ($dbg_values > 1);
1116 substr($var, length($res), 1, $variant);
1119 } elsif ($cur =~ /^($Operators)/o) {
1120 print "OP($1)\n" if ($dbg_values > 1);
1121 if ($1 ne '++' && $1 ne '--') {
1125 } elsif ($cur =~ /(^.)/o) {
1126 print "C($1)\n" if ($dbg_values > 1);
1129 $cur = substr($cur, length($1));
1130 $res .= $type x length($1);
1134 return ($res, $var);
1138 my ($possible, $line) = @_;
1139 my $notPermitted = qr{(?:
1154 ^(?:typedef|struct|enum)\b
1156 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1157 if ($possible !~ $notPermitted) {
1158 # Check for modifiers.
1159 $possible =~ s/\s*$Storage\s*//g;
1160 $possible =~ s/\s*$Sparse\s*//g;
1161 if ($possible =~ /^\s*$/) {
1163 } elsif ($possible =~ /\s/) {
1164 $possible =~ s/\s*$Type\s*//g;
1165 for my $modifier (split(' ', $possible)) {
1166 if ($modifier !~ $notPermitted) {
1167 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1168 push(@modifierList, $modifier);
1173 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1174 push(@typeList, $possible);
1178 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1185 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1188 my $line = $prefix . $_[0];
1190 $line = (split('\n', $line))[0] . "\n" if ($terse);
1192 push(our @report, $line);
1200 if (report("ERROR: $_[0]\n")) {
1206 if (report("WARNING: $_[0]\n")) {
1212 if ($check && report("CHECK: $_[0]\n")) {
1218 sub check_absolute_file {
1219 my ($absolute, $herecurr) = @_;
1220 my $file = $absolute;
1222 ##print "absolute<$absolute>\n";
1224 # See if any suffix of this path is a path within the tree.
1225 while ($file =~ s@^[^/]*/@@) {
1226 if (-f "$root/$file") {
1227 ##print "file<$file>\n";
1235 # It is, so see if the prefix is acceptable.
1236 my $prefix = $absolute;
1237 substr($prefix, -length($file)) = '';
1239 ##print "prefix<$prefix>\n";
1240 if ($prefix ne ".../") {
1241 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1246 my $filename = shift;
1252 my $stashrawline="";
1269 # Trace the real file/line as we go.
1275 my $comment_edge = 0;
1279 my $prev_values = 'E';
1282 my %suppress_ifbraces;
1283 my %suppress_whiletrailers;
1284 my %suppress_export;
1286 # Pre-scan the patch sanitizing the lines.
1287 # Pre-scan the patch looking for any __setup documentation.
1289 my @setup_docs = ();
1292 sanitise_line_reset();
1294 foreach my $rawline (@rawlines) {
1298 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1300 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1305 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1314 # Guestimate if this is a continuing comment. Run
1315 # the context looking for a comment "edge". If this
1316 # edge is a close comment then we must be in a comment
1320 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1321 next if (defined $rawlines[$ln - 1] &&
1322 $rawlines[$ln - 1] =~ /^-/);
1324 #print "RAW<$rawlines[$ln - 1]>\n";
1325 last if (!defined $rawlines[$ln - 1]);
1326 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1327 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1332 if (defined $edge && $edge eq '*/') {
1336 # Guestimate if this is a continuing comment. If this
1337 # is the start of a diff block and this line starts
1338 # ' *' then it is very likely a comment.
1339 if (!defined $edge &&
1340 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1345 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1346 sanitise_line_reset($in_comment);
1348 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1349 # Standardise the strings and chars within the input to
1350 # simplify matching -- only bother with positive lines.
1351 $line = sanitise_line($rawline);
1353 push(@lines, $line);
1356 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1361 #print "==>$rawline\n";
1362 #print "-->$line\n";
1364 if ($setup_docs && $line =~ /^\+/) {
1365 push(@setup_docs, $line);
1373 foreach my $line (@lines) {
1376 my $rawline = $rawlines[$linenr - 1];
1378 #extract the line range in the file after the patch is applied
1379 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1381 $first_line = $linenr + 1;
1391 %suppress_ifbraces = ();
1392 %suppress_whiletrailers = ();
1393 %suppress_export = ();
1396 # track the line number as we move through the hunk, note that
1397 # new versions of GNU diff omit the leading space on completely
1398 # blank context lines so we need to count that too.
1399 } elsif ($line =~ /^( |\+|$)/) {
1401 $realcnt-- if ($realcnt != 0);
1403 # Measure the line length and indent.
1404 ($length, $indent) = line_stats($rawline);
1406 # Track the previous line.
1407 ($prevline, $stashline) = ($stashline, $line);
1408 ($previndent, $stashindent) = ($stashindent, $indent);
1409 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1411 #warn "line<$line>\n";
1413 } elsif ($realcnt == 1) {
1417 my $hunk_line = ($realcnt != 0);
1419 #make up the handle for any error we report on this line
1420 $prefix = "$filename:$realline: " if ($emacs && $file);
1421 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1423 $here = "#$linenr: " if (!$file);
1424 $here = "#$realline: " if ($file);
1426 # extract the filename as it passes
1427 if ($line =~ /^diff --git.*?(\S+)$/) {
1429 $realfile =~ s@^([^/]*)/@@;
1431 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1433 $realfile =~ s@^([^/]*)/@@;
1436 if (!$file && $tree && $p1_prefix ne '' &&
1437 -e "$root/$p1_prefix") {
1438 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1441 if ($realfile =~ m@^include/asm/@) {
1442 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1447 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1449 my $hereline = "$here\n$rawline\n";
1450 my $herecurr = "$here\n$rawline\n";
1451 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1453 $cnt_lines++ if ($realcnt != 0);
1455 # Check for incorrect file permissions
1456 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1457 my $permhere = $here . "FILE: $realfile\n";
1458 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1459 ERROR("do not set execute permissions for source files\n" . $permhere);
1463 # Check the patch for a signoff:
1464 if ($line =~ /^\s*signed-off-by:/i) {
1468 # Check signature styles
1469 if ($line =~ /^(\s*)($signature_tags)(\s*)(.*)/) {
1470 my $space_before = $1;
1472 my $space_after = $3;
1474 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1476 if (defined $space_before && $space_before ne "") {
1477 WARN("Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
1479 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1480 WARN("'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
1482 if (!defined $space_after || $space_after ne " ") {
1483 WARN("Use a single space after $ucfirst_sign_off\n" . $herecurr);
1486 my ($email_name, $email_address, $comment) = parse_email($email);
1487 my $suggested_email = format_email(($email_name, $email_address));
1488 if ($suggested_email eq "") {
1489 ERROR("Unrecognized email address: '$email'\n" . $herecurr);
1491 my $dequoted = $suggested_email;
1492 $dequoted =~ s/^"//;
1493 $dequoted =~ s/" </ </;
1494 # Don't force email to have quotes
1495 # Allow just an angle bracketed address
1496 if ("$dequoted$comment" ne $email &&
1497 "<$email_address>$comment" ne $email &&
1498 "$suggested_email$comment" ne $email) {
1499 WARN("email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
1504 # Check for wrappage within a valid hunk of the file
1505 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1506 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1507 $herecurr) if (!$emitted_corrupt++);
1510 # Check for absolute kernel paths.
1512 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1515 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1516 check_absolute_file($1, $herecurr)) {
1519 check_absolute_file($file, $herecurr);
1524 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1525 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1526 $rawline !~ m/^$UTF8*$/) {
1527 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1529 my $blank = copy_spacing($rawline);
1530 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1531 my $hereptr = "$hereline$ptr\n";
1533 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1536 # ignore non-hunk lines and lines being removed
1537 next if (!$hunk_line || $line =~ /^-/);
1539 #trailing whitespace
1540 if ($line =~ /^\+.*\015/) {
1541 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1542 ERROR("DOS line endings\n" . $herevet);
1544 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1545 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1546 ERROR("trailing whitespace\n" . $herevet);
1550 # check for Kconfig help text having a real description
1551 # Only applies when adding the entry originally, after that we do not have
1552 # sufficient context to determine whether it is indeed long enough.
1553 if ($realfile =~ /Kconfig/ &&
1554 $line =~ /\+\s*(?:---)?help(?:---)?$/) {
1557 my $ln = $linenr + 1;
1560 while ($cnt > 0 && defined $lines[$ln - 1]) {
1561 $f = $lines[$ln - 1];
1562 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1563 $is_end = $lines[$ln - 1] =~ /^\+/;
1566 next if ($f =~ /^-/);
1570 next if ($f =~ /^$/);
1571 if ($f =~ /^\s*config\s/) {
1577 WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
1578 #print "is_end<$is_end> length<$length>\n";
1581 # check we are in a valid source file if not then ignore this hunk
1582 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1585 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1586 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1587 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
1588 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1591 WARN("line over 80 characters\n" . $herecurr);
1594 # check for spaces before a quoted newline
1595 if ($rawline =~ /^.*\".*\s\\n/) {
1596 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1599 # check for adding lines without a newline.
1600 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1601 WARN("adding a line without newline at end of file\n" . $herecurr);
1604 # Blackfin: use hi/lo macros
1605 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1606 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1607 my $herevet = "$here\n" . cat_vet($line) . "\n";
1608 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1610 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1611 my $herevet = "$here\n" . cat_vet($line) . "\n";
1612 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1616 # check we are in a valid source file C or perl if not then ignore this hunk
1617 next if ($realfile !~ /\.(h|c|pl)$/);
1619 # at the beginning of a line any tabs must come first and anything
1620 # more than 8 must use tabs.
1621 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1622 $rawline =~ /^\+\s* \s*/) {
1623 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1624 ERROR("code indent should use tabs where possible\n" . $herevet);
1628 # check for space before tabs.
1629 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1630 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1631 WARN("please, no space before tabs\n" . $herevet);
1634 # check for spaces at the beginning of a line.
1636 # 1) within comments
1637 # 2) indented preprocessor commands
1639 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
1640 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1641 WARN("please, no spaces at the start of a line\n" . $herevet);
1644 # check we are in a valid C source file if not then ignore this hunk
1645 next if ($realfile !~ /\.(h|c)$/);
1647 # check for RCS/CVS revision markers
1648 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1649 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1652 # Blackfin: don't use __builtin_bfin_[cs]sync
1653 if ($line =~ /__builtin_bfin_csync/) {
1654 my $herevet = "$here\n" . cat_vet($line) . "\n";
1655 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1657 if ($line =~ /__builtin_bfin_ssync/) {
1658 my $herevet = "$here\n" . cat_vet($line) . "\n";
1659 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1662 # Check for potential 'bare' types
1663 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1665 if ($realcnt && $line =~ /.\s*\S/) {
1666 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1667 ctx_statement_block($linenr, $realcnt, 0);
1668 $stat =~ s/\n./\n /g;
1669 $cond =~ s/\n./\n /g;
1671 # Find the real next line.
1672 $realline_next = $line_nr_next;
1673 if (defined $realline_next &&
1674 (!defined $lines[$realline_next - 1] ||
1675 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1682 # Ignore goto labels.
1683 if ($s =~ /$Ident:\*$/s) {
1685 # Ignore functions being called
1686 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1688 } elsif ($s =~ /^.\s*else\b/s) {
1690 # declarations always start with types
1691 } 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) {
1694 possible($type, "A:" . $s);
1696 # definitions in global scope can only start with types
1697 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1698 possible($1, "B:" . $s);
1701 # any (foo ... *) is a pointer cast, and foo is a type
1702 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1703 possible($1, "C:" . $s);
1706 # Check for any sort of function declaration.
1707 # int foo(something bar, other baz);
1708 # void (*store_gdt)(x86_descr_ptr *);
1709 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1710 my ($name_len) = length($1);
1713 substr($ctx, 0, $name_len + 1, '');
1714 $ctx =~ s/\)[^\)]*$//;
1716 for my $arg (split(/\s*,\s*/, $ctx)) {
1717 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1719 possible($1, "D:" . $s);
1727 # Checks which may be anchored in the context.
1730 # Check for switch () and associated case and default
1731 # statements should be at the same indent.
1732 if ($line=~/\bswitch\s*\(.*\)/) {
1735 my @ctx = ctx_block_outer($linenr, $realcnt);
1737 for my $ctx (@ctx) {
1738 my ($clen, $cindent) = line_stats($ctx);
1739 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1740 $indent != $cindent) {
1741 $err .= "$sep$ctx\n";
1748 ERROR("switch and case should be at the same indent\n$hereline$err");
1752 # if/while/etc brace do not go on next line, unless defining a do while loop,
1753 # or if that brace on the next line is for something else
1754 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1755 my $pre_ctx = "$1$2";
1757 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1758 my $ctx_cnt = $realcnt - $#ctx - 1;
1759 my $ctx = join("\n", @ctx);
1761 my $ctx_ln = $linenr;
1762 my $ctx_skip = $realcnt;
1764 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1765 defined $lines[$ctx_ln - 1] &&
1766 $lines[$ctx_ln - 1] =~ /^-/)) {
1767 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1768 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1772 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1773 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1775 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1776 ERROR("that open brace { should be on the previous line\n" .
1777 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1779 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1780 $ctx =~ /\)\s*\;\s*$/ &&
1781 defined $lines[$ctx_ln - 1])
1783 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1784 if ($nindent > $indent) {
1785 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1786 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1791 # Check relative indent for conditionals and blocks.
1792 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1793 my ($s, $c) = ($stat, $cond);
1795 substr($s, 0, length($c), '');
1797 # Make sure we remove the line prefixes as we have
1798 # none on the first line, and are going to readd them
1802 # Find out how long the conditional actually is.
1803 my @newlines = ($c =~ /\n/gs);
1804 my $cond_lines = 1 + $#newlines;
1806 # We want to check the first line inside the block
1807 # starting at the end of the conditional, so remove:
1808 # 1) any blank line termination
1809 # 2) any opening brace { on end of the line
1811 my $continuation = 0;
1813 $s =~ s/^.*\bdo\b//;
1815 if ($s =~ s/^\s*\\//) {
1818 if ($s =~ s/^\s*?\n//) {
1823 # Also ignore a loop construct at the end of a
1824 # preprocessor statement.
1825 if (($prevline =~ /^.\s*#\s*define\s/ ||
1826 $prevline =~ /\\\s*$/) && $continuation == 0) {
1832 while ($cond_ptr != $cond_lines) {
1833 $cond_ptr = $cond_lines;
1835 # If we see an #else/#elif then the code
1837 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1842 # 1) blank lines, they should be at 0,
1843 # 2) preprocessor lines, and
1845 if ($continuation ||
1847 $s =~ /^\s*#\s*?/ ||
1848 $s =~ /^\s*$Ident\s*:/) {
1849 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1850 if ($s =~ s/^.*?\n//) {
1856 my (undef, $sindent) = line_stats("+" . $s);
1857 my $stat_real = raw_line($linenr, $cond_lines);
1859 # Check if either of these lines are modified, else
1860 # this is not this patch's fault.
1861 if (!defined($stat_real) ||
1862 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1865 if (defined($stat_real) && $cond_lines > 1) {
1866 $stat_real = "[...]\n$stat_real";
1869 #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";
1871 if ($check && (($sindent % 8) != 0 ||
1872 ($sindent <= $indent && $s ne ''))) {
1873 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1877 # Track the 'values' across context and added lines.
1878 my $opline = $line; $opline =~ s/^./ /;
1879 my ($curr_values, $curr_vars) =
1880 annotate_values($opline . "\n", $prev_values);
1881 $curr_values = $prev_values . $curr_values;
1883 my $outline = $opline; $outline =~ s/\t/ /g;
1884 print "$linenr > .$outline\n";
1885 print "$linenr > $curr_values\n";
1886 print "$linenr > $curr_vars\n";
1888 $prev_values = substr($curr_values, -1);
1890 #ignore lines not being added
1891 if ($line=~/^[^\+]/) {next;}
1893 # TEST: allow direct testing of the type matcher.
1895 if ($line =~ /^.\s*$Declare\s*$/) {
1896 ERROR("TEST: is type\n" . $herecurr);
1897 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1898 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1902 # TEST: allow direct testing of the attribute matcher.
1904 if ($line =~ /^.\s*$Modifier\s*$/) {
1905 ERROR("TEST: is attr\n" . $herecurr);
1906 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1907 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1912 # check for initialisation to aggregates open brace on the next line
1913 if ($line =~ /^.\s*{/ &&
1914 $prevline =~ /(?:^|[^=])=\s*$/) {
1915 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1919 # Checks which are anchored on the added line.
1922 # check for malformed paths in #include statements (uses RAW line)
1923 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1925 if ($path =~ m{//}) {
1926 ERROR("malformed #include filename\n" .
1931 # no C99 // comments
1932 if ($line =~ m{//}) {
1933 ERROR("do not use C99 // comments\n" . $herecurr);
1935 # Remove C99 comments.
1937 $opline =~ s@//.*@@;
1939 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1940 # the whole statement.
1941 #print "APW <$lines[$realline_next - 1]>\n";
1942 if (defined $realline_next &&
1943 exists $lines[$realline_next - 1] &&
1944 !defined $suppress_export{$realline_next} &&
1945 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1946 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1947 # Handle definitions which produce identifiers with
1950 # EXPORT_SYMBOL(something_foo);
1952 if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
1953 $name =~ /^${Ident}_$2/) {
1954 #print "FOO C name<$name>\n";
1955 $suppress_export{$realline_next} = 1;
1957 } elsif ($stat !~ /(?:
1959 ^.DEFINE_$Ident\(\Q$name\E\)|
1960 ^.DECLARE_$Ident\(\Q$name\E\)|
1961 ^.LIST_HEAD\(\Q$name\E\)|
1962 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1963 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
1965 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1966 $suppress_export{$realline_next} = 2;
1968 $suppress_export{$realline_next} = 1;
1971 if (!defined $suppress_export{$linenr} &&
1972 $prevline =~ /^.\s*$/ &&
1973 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1974 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1975 #print "FOO B <$lines[$linenr - 1]>\n";
1976 $suppress_export{$linenr} = 2;
1978 if (defined $suppress_export{$linenr} &&
1979 $suppress_export{$linenr} == 2) {
1980 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1983 # check for global initialisers.
1984 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1985 ERROR("do not initialise globals to 0 or NULL\n" .
1988 # check for static initialisers.
1989 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1990 ERROR("do not initialise statics to 0 or NULL\n" .
1994 # check for static const char * arrays.
1995 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
1996 WARN("static const char * array should probably be static const char * const\n" .
2000 # check for static char foo[] = "bar" declarations.
2001 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2002 WARN("static char array declaration should probably be static const char\n" .
2006 # check for declarations of struct pci_device_id
2007 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2008 WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
2011 # check for new typedefs, only function parameters and sparse annotations
2013 if ($line =~ /\btypedef\s/ &&
2014 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2015 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
2016 $line !~ /\b$typeTypedefs\b/ &&
2017 $line !~ /\b__bitwise(?:__|)\b/) {
2018 WARN("do not add new typedefs\n" . $herecurr);
2021 # * goes on variable not on type
2023 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
2024 my ($from, $to) = ($1, $1);
2026 # Should start with a space.
2027 $to =~ s/^(\S)/ $1/;
2028 # Should not end with a space.
2030 # '*'s should not have spaces between.
2031 while ($to =~ s/\*\s+\*/\*\*/) {
2034 #print "from<$from> to<$to>\n";
2036 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
2038 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
2039 my ($from, $to, $ident) = ($1, $1, $2);
2041 # Should start with a space.
2042 $to =~ s/^(\S)/ $1/;
2043 # Should not end with a space.
2045 # '*'s should not have spaces between.
2046 while ($to =~ s/\*\s+\*/\*\*/) {
2048 # Modifiers should have spaces.
2049 $to =~ s/(\b$Modifier$)/$1 /;
2051 #print "from<$from> to<$to> ident<$ident>\n";
2052 if ($from ne $to && $ident !~ /^$Modifier$/) {
2053 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
2057 # # no BUG() or BUG_ON()
2058 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
2059 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2060 # print "$herecurr";
2064 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2065 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
2068 # check for uses of printk_ratelimit
2069 if ($line =~ /\bprintk_ratelimit\s*\(/) {
2070 WARN("Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
2073 # printk should use KERN_* levels. Note that follow on printk's on the
2074 # same line do not need a level, so we use the current block context
2075 # to try and find and validate the current printk. In summary the current
2076 # printk includes all preceding printk's which have no newline on the end.
2077 # we assume the first bad printk is the one to report.
2078 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
2080 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2081 #print "CHECK<$lines[$ln - 1]\n";
2082 # we have a preceding printk if it ends
2083 # with "\n" ignore it, else it is to blame
2084 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2085 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2092 WARN("printk() should include KERN_ facility level\n" . $herecurr);
2096 # function brace can't be on same line, except for #defines of do while,
2097 # or if closed on same line
2098 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2099 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2100 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
2103 # open braces for enum, union and struct go on the same line.
2104 if ($line =~ /^.\s*{/ &&
2105 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2106 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
2109 # missing space after union, struct or enum definition
2110 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2111 WARN("missing space after $1 definition\n" . $herecurr);
2114 # check for spacing round square brackets; allowed:
2115 # 1. with a type on the left -- int [] a;
2116 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2117 # 3. inside a curly brace -- = { [0...10] = 5 }
2118 while ($line =~ /(.*?\s)\[/g) {
2119 my ($where, $prefix) = ($-[1], $1);
2120 if ($prefix !~ /$Type\s+$/ &&
2121 ($where != 0 || $prefix !~ /^.\s+$/) &&
2122 $prefix !~ /{\s+$/) {
2123 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
2127 # check for spaces between functions and their parentheses.
2128 while ($line =~ /($Ident)\s+\(/g) {
2130 my $ctx_before = substr($line, 0, $-[1]);
2131 my $ctx = "$ctx_before$name";
2133 # Ignore those directives where spaces _are_ permitted.
2135 if|for|while|switch|return|case|
2136 volatile|__volatile__|
2137 __attribute__|format|__extension__|
2141 # cpp #define statements have non-optional spaces, ie
2142 # if there is a space between the name and the open
2143 # parenthesis it is simply not a parameter group.
2144 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2146 # cpp #elif statement condition may start with a (
2147 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2149 # If this whole things ends with a type its most
2150 # likely a typedef for a function.
2151 } elsif ($ctx =~ /$Type$/) {
2154 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
2157 # Check operator spacing.
2158 if (!($line=~/\#\s*include/)) {
2160 <<=|>>=|<=|>=|==|!=|
2161 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2162 =>|->|<<|>>|<|>|=|!|~|
2163 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2166 my @elements = split(/($ops|;)/, $opline);
2169 my $blank = copy_spacing($opline);
2171 for (my $n = 0; $n < $#elements; $n += 2) {
2172 $off += length($elements[$n]);
2174 # Pick up the preceding and succeeding characters.
2175 my $ca = substr($opline, 0, $off);
2177 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2178 $cc = substr($opline, $off + length($elements[$n + 1]));
2180 my $cb = "$ca$;$cc";
2183 $a = 'V' if ($elements[$n] ne '');
2184 $a = 'W' if ($elements[$n] =~ /\s$/);
2185 $a = 'C' if ($elements[$n] =~ /$;$/);
2186 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2187 $a = 'O' if ($elements[$n] eq '');
2188 $a = 'E' if ($ca =~ /^\s*$/);
2190 my $op = $elements[$n + 1];
2193 if (defined $elements[$n + 2]) {
2194 $c = 'V' if ($elements[$n + 2] ne '');
2195 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2196 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2197 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2198 $c = 'O' if ($elements[$n + 2] eq '');
2199 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2204 my $ctx = "${a}x${c}";
2206 my $at = "(ctx:$ctx)";
2208 my $ptr = substr($blank, 0, $off) . "^";
2209 my $hereptr = "$hereline$ptr\n";
2211 # Pull out the value of this operator.
2212 my $op_type = substr($curr_values, $off + 1, 1);
2214 # Get the full operator variant.
2215 my $opv = $op . substr($curr_vars, $off, 1);
2217 # Ignore operators passed as parameters.
2218 if ($op_type ne 'V' &&
2219 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2222 # } elsif ($op =~ /^$;+$/) {
2224 # ; should have either the end of line or a space or \ after it
2225 } elsif ($op eq ';') {
2226 if ($ctx !~ /.x[WEBC]/ &&
2227 $cc !~ /^\\/ && $cc !~ /^;/) {
2228 ERROR("space required after that '$op' $at\n" . $hereptr);
2232 } elsif ($op eq '//') {
2236 # : when part of a bitfield
2237 } elsif ($op eq '->' || $opv eq ':B') {
2238 if ($ctx =~ /Wx.|.xW/) {
2239 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
2242 # , must have a space on the right.
2243 } elsif ($op eq ',') {
2244 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2245 ERROR("space required after that '$op' $at\n" . $hereptr);
2248 # '*' as part of a type definition -- reported already.
2249 } elsif ($opv eq '*_') {
2250 #warn "'*' is part of type\n";
2252 # unary operators should have a space before and
2253 # none after. May be left adjacent to another
2254 # unary operator, or a cast
2255 } elsif ($op eq '!' || $op eq '~' ||
2256 $opv eq '*U' || $opv eq '-U' ||
2257 $opv eq '&U' || $opv eq '&&U') {
2258 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2259 ERROR("space required before that '$op' $at\n" . $hereptr);
2261 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2262 # A unary '*' may be const
2264 } elsif ($ctx =~ /.xW/) {
2265 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2268 # unary ++ and unary -- are allowed no space on one side.
2269 } elsif ($op eq '++' or $op eq '--') {
2270 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2271 ERROR("space required one side of that '$op' $at\n" . $hereptr);
2273 if ($ctx =~ /Wx[BE]/ ||
2274 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2275 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2277 if ($ctx =~ /ExW/) {
2278 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2282 # << and >> may either have or not have spaces both sides
2283 } elsif ($op eq '<<' or $op eq '>>' or
2284 $op eq '&' or $op eq '^' or $op eq '|' or
2285 $op eq '+' or $op eq '-' or
2286 $op eq '*' or $op eq '/' or
2289 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2290 ERROR("need consistent spacing around '$op' $at\n" .
2294 # A colon needs no spaces before when it is
2295 # terminating a case value or a label.
2296 } elsif ($opv eq ':C' || $opv eq ':L') {
2297 if ($ctx =~ /Wx./) {
2298 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2301 # All the others need spaces both sides.
2302 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2305 # Ignore email addresses <foo@bar>
2307 $cc =~ /^\S+\@\S+>/) ||
2309 $ca =~ /<\S+\@\S+$/))
2315 if (($opv eq ':O' && $ca =~ /\?$/) ||
2316 ($op eq '?' && $cc =~ /^:/)) {
2321 ERROR("spaces required around that '$op' $at\n" . $hereptr);
2324 $off += length($elements[$n + 1]);
2328 # check for multiple assignments
2329 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2330 CHK("multiple assignments should be avoided\n" . $herecurr);
2333 ## # check for multiple declarations, allowing for a function declaration
2335 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2336 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2338 ## # Remove any bracketed sections to ensure we do not
2339 ## # falsly report the parameters of functions.
2341 ## while ($ln =~ s/\([^\(\)]*\)//g) {
2343 ## if ($ln =~ /,/) {
2344 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2348 #need space before brace following if, while, etc
2349 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2351 ERROR("space required before the open brace '{'\n" . $herecurr);
2354 # closing brace should have a space following it when it has anything
2356 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2357 ERROR("space required after that close brace '}'\n" . $herecurr);
2360 # check spacing on square brackets
2361 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2362 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2364 if ($line =~ /\s\]/) {
2365 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2368 # check spacing on parentheses
2369 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2370 $line !~ /for\s*\(\s+;/) {
2371 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2373 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2374 $line !~ /for\s*\(.*;\s+\)/ &&
2375 $line !~ /:\s+\)/) {
2376 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2379 #goto labels aren't indented, allow a single space however
2380 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2381 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2382 WARN("labels should not be indented\n" . $herecurr);
2385 # Return is not a function.
2386 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2390 # Flatten any parentheses
2391 $value =~ s/\(/ \(/g;
2392 $value =~ s/\)/\) /g;
2393 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2394 $value !~ /(?:$Ident|-?$Constant)\s*
2396 (?:$Ident|-?$Constant)/x &&
2397 $value =~ s/\([^\(\)]*\)/1/) {
2399 #print "value<$value>\n";
2400 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2401 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2403 } elsif ($spacing !~ /\s+/) {
2404 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2407 # Return of what appears to be an errno should normally be -'ve
2408 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2410 if ($name ne 'EOF' && $name ne 'ERROR') {
2411 WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2415 # typecasts on min/max could be min_t/max_t
2416 if ($line =~ /^\+(?:.*?)\b(min|max)\s*\($Typecast{0,1}($LvalOrFunc)\s*,\s*$Typecast{0,1}($LvalOrFunc)\s*\)/) {
2417 if (defined $2 || defined $8) {
2419 my $cast1 = deparenthesize($2);
2421 my $cast2 = deparenthesize($8);
2425 if ($cast1 ne "" && $cast2 ne "") {
2426 $cast = "$cast1 or $cast2";
2427 } elsif ($cast1 ne "") {
2432 WARN("$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . $herecurr);
2436 # Need a space before open parenthesis after if, while etc
2437 if ($line=~/\b(if|while|for|switch)\(/) {
2438 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2441 # Check for illegal assignment in if conditional -- and check for trailing
2442 # statements after the conditional.
2443 if ($line =~ /do\s*(?!{)/) {
2444 my ($stat_next) = ctx_statement_block($line_nr_next,
2445 $remain_next, $off_next);
2446 $stat_next =~ s/\n./\n /g;
2447 ##print "stat<$stat> stat_next<$stat_next>\n";
2449 if ($stat_next =~ /^\s*while\b/) {
2450 # If the statement carries leading newlines,
2451 # then count those as offsets.
2453 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2455 statement_rawlines($whitespace) - 1;
2457 $suppress_whiletrailers{$line_nr_next +
2461 if (!defined $suppress_whiletrailers{$linenr} &&
2462 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2463 my ($s, $c) = ($stat, $cond);
2465 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2466 ERROR("do not use assignment in if condition\n" . $herecurr);
2469 # Find out what is on the end of the line after the
2471 substr($s, 0, length($c), '');
2473 $s =~ s/$;//g; # Remove any comments
2474 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2475 $c !~ /}\s*while\s*/)
2477 # Find out how long the conditional actually is.
2478 my @newlines = ($c =~ /\n/gs);
2479 my $cond_lines = 1 + $#newlines;
2482 $stat_real = raw_line($linenr, $cond_lines)
2483 . "\n" if ($cond_lines);
2484 if (defined($stat_real) && $cond_lines > 1) {
2485 $stat_real = "[...]\n$stat_real";
2488 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2492 # Check for bitwise tests written as boolean
2504 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2507 # if and else should not have general statements after it
2508 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2510 $s =~ s/$;//g; # Remove any comments
2511 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2512 ERROR("trailing statements should be on next line\n" . $herecurr);
2515 # if should not continue a brace
2516 if ($line =~ /}\s*if\b/) {
2517 ERROR("trailing statements should be on next line\n" .
2520 # case and default should not have general statements after them
2521 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2523 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2527 ERROR("trailing statements should be on next line\n" . $herecurr);
2530 # Check for }<nl>else {, these must be at the same
2531 # indent level to be relevant to each other.
2532 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2533 $previndent == $indent) {
2534 ERROR("else should follow close brace '}'\n" . $hereprev);
2537 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2538 $previndent == $indent) {
2539 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2541 # Find out what is on the end of the line after the
2543 substr($s, 0, length($c), '');
2546 if ($s =~ /^\s*;/) {
2547 ERROR("while should follow close brace '}'\n" . $hereprev);
2551 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2552 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2553 # print "No studly caps, use _\n";
2554 # print "$herecurr";
2558 #no spaces allowed after \ in define
2559 if ($line=~/\#\s*define.*\\\s$/) {
2560 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
2563 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2564 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2566 my $checkfile = "include/linux/$file";
2567 if (-f "$root/$checkfile" &&
2568 $realfile ne $checkfile &&
2569 $1 !~ /$allowed_asm_includes/)
2571 if ($realfile =~ m{^arch/}) {
2572 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2574 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2579 # multi-statement macros should be enclosed in a do while loop, grab the
2580 # first statement and ensure its the whole macro if its not enclosed
2581 # in a known good container
2582 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2583 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2586 my ($off, $dstat, $dcond, $rest);
2589 my $args = defined($1);
2591 # Find the end of the macro and limit our statement
2593 while ($cnt > 0 && defined $lines[$ln - 1] &&
2594 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2596 $ctx .= $rawlines[$ln - 1] . "\n";
2597 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2600 $ctx .= $rawlines[$ln - 1];
2602 ($dstat, $dcond, $ln, $cnt, $off) =
2603 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2604 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2605 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2607 # Extract the remainder of the define (if any) and
2608 # rip off surrounding spaces, and trailing \'s.
2610 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2611 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2612 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2613 $rest .= substr($lines[$ln - 1], $off) . "\n";
2619 $rest =~ s/\\\n.//g;
2623 # Clean up the original statement.
2625 substr($dstat, 0, length($dcond), '');
2627 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2630 $dstat =~ s/\\\n.//g;
2631 $dstat =~ s/^\s*//s;
2632 $dstat =~ s/\s*$//s;
2634 # Flatten any parentheses and braces
2635 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2636 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2637 $dstat =~ s/\[[^\{\}]*\]/1/)
2641 my $exceptions = qr{
2653 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2654 if ($rest ne '' && $rest ne ',') {
2655 if ($rest !~ /while\s*\(/ &&
2656 $dstat !~ /$exceptions/)
2658 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2661 } elsif ($ctx !~ /;/) {
2663 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2664 $dstat !~ /$exceptions/ &&
2665 $dstat !~ /^\.$Ident\s*=/ &&
2666 $dstat =~ /$Operators/)
2668 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2673 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2674 # all assignments may have only one of the following with an assignment:
2677 # VMLINUX_SYMBOL(...)
2678 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2679 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2682 # check for redundant bracing round if etc
2683 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2684 my ($level, $endln, @chunks) =
2685 ctx_statement_full($linenr, $realcnt, 1);
2686 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2687 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2688 if ($#chunks > 0 && $level == 0) {
2691 my $herectx = $here . "\n";
2692 my $ln = $linenr - 1;
2693 for my $chunk (@chunks) {
2694 my ($cond, $block) = @{$chunk};
2696 # If the condition carries leading newlines, then count those as offsets.
2697 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2698 my $offset = statement_rawlines($whitespace) - 1;
2700 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2702 # We have looked at and allowed this specific line.
2703 $suppress_ifbraces{$ln + $offset} = 1;
2705 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2706 $ln += statement_rawlines($block) - 1;
2708 substr($block, 0, length($cond), '');
2710 $seen++ if ($block =~ /^\s*{/);
2712 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2713 if (statement_lines($cond) > 1) {
2714 #print "APW: ALLOWED: cond<$cond>\n";
2717 if ($block =~/\b(?:if|for|while)\b/) {
2718 #print "APW: ALLOWED: block<$block>\n";
2721 if (statement_block_size($block) > 1) {
2722 #print "APW: ALLOWED: lines block<$block>\n";
2726 if ($seen && !$allowed) {
2727 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
2731 if (!defined $suppress_ifbraces{$linenr - 1} &&
2732 $line =~ /\b(if|while|for|else)\b/) {
2735 # Check the pre-context.
2736 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2737 #print "APW: ALLOWED: pre<$1>\n";
2741 my ($level, $endln, @chunks) =
2742 ctx_statement_full($linenr, $realcnt, $-[0]);
2744 # Check the condition.
2745 my ($cond, $block) = @{$chunks[0]};
2746 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2747 if (defined $cond) {
2748 substr($block, 0, length($cond), '');
2750 if (statement_lines($cond) > 1) {
2751 #print "APW: ALLOWED: cond<$cond>\n";
2754 if ($block =~/\b(?:if|for|while)\b/) {
2755 #print "APW: ALLOWED: block<$block>\n";
2758 if (statement_block_size($block) > 1) {
2759 #print "APW: ALLOWED: lines block<$block>\n";
2762 # Check the post-context.
2763 if (defined $chunks[1]) {
2764 my ($cond, $block) = @{$chunks[1]};
2765 if (defined $cond) {
2766 substr($block, 0, length($cond), '');
2768 if ($block =~ /^\s*\{/) {
2769 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2773 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2774 my $herectx = $here . "\n";;
2775 my $cnt = statement_rawlines($block);
2777 for (my $n = 0; $n < $cnt; $n++) {
2778 $herectx .= raw_line($linenr, $n) . "\n";;
2781 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2785 # don't include deprecated include files (uses RAW line)
2786 for my $inc (@dep_includes) {
2787 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2788 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2792 # don't use deprecated functions
2793 for my $func (@dep_functions) {
2794 if ($line =~ /\b$func\b/) {
2795 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2799 # no volatiles please
2800 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2801 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2802 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2806 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2807 CHK("if this code is redundant consider removing it\n" .
2811 # check for needless kfree() checks
2812 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2814 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2815 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2818 # check for needless usb_free_urb() checks
2819 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2821 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2822 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2826 # prefer usleep_range over udelay
2827 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2828 # ignore udelay's < 10, however
2829 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2830 CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2834 # warn about unexpectedly long msleep's
2835 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2837 WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2841 # warn about #ifdefs in C files
2842 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2843 # print "#ifdef in C files should be avoided\n";
2844 # print "$herecurr";
2848 # warn about spacing in #ifdefs
2849 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2850 ERROR("exactly one space required after that #$1\n" . $herecurr);
2853 # check for spinlock_t definitions without a comment.
2854 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2855 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
2857 if (!ctx_has_comment($first_line, $linenr)) {
2858 CHK("$1 definition without comment\n" . $herecurr);
2861 # check for memory barriers without a comment.
2862 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2863 if (!ctx_has_comment($first_line, $linenr)) {
2864 CHK("memory barrier without comment\n" . $herecurr);
2867 # check of hardware specific defines
2868 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2869 CHK("architecture specific defines should be avoided\n" . $herecurr);
2872 # Check that the storage class is at the beginning of a declaration
2873 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2874 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2877 # check the location of the inline attribute, that it is between
2878 # storage class and type.
2879 if ($line =~ /\b$Type\s+$Inline\b/ ||
2880 $line =~ /\b$Inline\s+$Storage\b/) {
2881 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2884 # Check for __inline__ and __inline, prefer inline
2885 if ($line =~ /\b(__inline__|__inline)\b/) {
2886 WARN("plain inline is preferred over $1\n" . $herecurr);
2889 # Check for __attribute__ packed, prefer __packed
2890 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
2891 WARN("__packed is preferred over __attribute__((packed))\n" . $herecurr);
2894 # Check for __attribute__ aligned, prefer __aligned
2895 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
2896 WARN("__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
2899 # check for sizeof(&)
2900 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2901 WARN("sizeof(& should be avoided\n" . $herecurr);
2904 # check for line continuations in quoted strings with odd counts of "
2905 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
2906 WARN("Avoid line continuations in quoted strings\n" . $herecurr);
2909 # check for new externs in .c files.
2910 if ($realfile =~ /\.c$/ && defined $stat &&
2911 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2913 my $function_name = $1;
2914 my $paren_space = $2;
2917 if (defined $cond) {
2918 substr($s, 0, length($cond), '');
2920 if ($s =~ /^\s*;/ &&
2921 $function_name ne 'uninitialized_var')
2923 WARN("externs should be avoided in .c files\n" . $herecurr);
2926 if ($paren_space =~ /\n/) {
2927 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2930 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2931 $stat =~ /^.\s*extern\s+/)
2933 WARN("externs should be avoided in .c files\n" . $herecurr);
2936 # checks for new __setup's
2937 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2940 if (!grep(/$name/, @setup_docs)) {
2941 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2945 # check for pointless casting of kmalloc return
2946 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
2947 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2950 # check for multiple semicolons
2951 if ($line =~ /;\s*;\s*$/) {
2952 WARN("Statements terminations use 1 semicolon\n" . $herecurr);
2955 # check for gcc specific __FUNCTION__
2956 if ($line =~ /__FUNCTION__/) {
2957 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2960 # check for semaphores initialized locked
2961 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
2962 WARN("consider using a completion\n" . $herecurr);
2965 # recommend kstrto* over simple_strto*
2966 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2967 WARN("consider using kstrto* in preference to simple_$1\n" . $herecurr);
2969 # check for __initcall(), use device_initcall() explicitly please
2970 if ($line =~ /^.\s*__initcall\s*\(/) {
2971 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2973 # check for various ops structs, ensure they are const.
2974 my $struct_ops = qr{acpi_dock_ops|
2975 address_space_operations|
2977 block_device_operations|
2982 file_lock_operations|
2992 lock_manager_operations|
2998 pipe_buf_operations|
2999 platform_hibernation_ops|
3000 platform_suspend_ops|
3005 soc_pcmcia_socket_ops|
3011 if ($line !~ /\bconst\b/ &&
3012 $line =~ /\bstruct\s+($struct_ops)\b/) {
3013 WARN("struct $1 should normally be const\n" .
3017 # use of NR_CPUS is usually wrong
3018 # ignore definitions of NR_CPUS and usage to define arrays as likely right
3019 if ($line =~ /\bNR_CPUS\b/ &&
3020 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3021 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
3022 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3023 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3024 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
3026 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3029 # check for %L{u,d,i} in strings
3031 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3032 $string = substr($rawline, $-[1], $+[1] - $-[1]);
3033 $string =~ s/%%/__/g;
3034 if ($string =~ /(?<!%)%L[udi]/) {
3035 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
3040 # whine mightly about in_atomic
3041 if ($line =~ /\bin_atomic\s*\(/) {
3042 if ($realfile =~ m@^drivers/@) {
3043 ERROR("do not use in_atomic in drivers\n" . $herecurr);
3044 } elsif ($realfile !~ m@^kernel/@) {
3045 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
3049 # check for lockdep_set_novalidate_class
3050 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3051 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3052 if ($realfile !~ m@^kernel/lockdep@ &&
3053 $realfile !~ m@^include/linux/lockdep@ &&
3054 $realfile !~ m@^drivers/base/core@) {
3055 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
3059 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3060 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
3061 WARN("Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
3064 # Check for memset with swapped arguments
3065 if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
3066 ERROR("memset size is 3rd argument, not the second.\n" . $herecurr);
3070 # If we have no input at all, then there is nothing to report on
3071 # so just keep quiet.
3072 if ($#rawlines == -1) {
3076 # In mailback mode only produce a report in the negative, for
3077 # things that appear to be patches.
3078 if ($mailback && ($clean == 1 || !$is_patch)) {
3082 # This is not a patch, and we are are in 'no-patch' mode so
3084 if (!$chk_patch && !$is_patch) {
3089 ERROR("Does not appear to be a unified-diff format patch\n");
3091 if ($is_patch && $chk_signoff && $signoff == 0) {
3092 ERROR("Missing Signed-off-by: line(s)\n");
3095 print report_dump();
3096 if ($summary && !($clean == 1 && $quiet == 1)) {
3097 print "$filename " if ($summary_file);
3098 print "total: $cnt_error errors, $cnt_warn warnings, " .
3099 (($check)? "$cnt_chk checks, " : "") .
3100 "$cnt_lines lines checked\n";
3101 print "\n" if ($quiet == 0);
3105 # If there were whitespace errors which cleanpatch can fix
3106 # then suggest that.
3107 if ($rpt_cleaners) {
3108 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3109 print " scripts/cleanfile\n\n";
3114 if ($clean == 1 && $quiet == 0) {
3115 print "$vname has no obvious style problems and is ready for submission.\n"
3117 if ($clean == 0 && $quiet == 0) {
3118 print "$vname has style problems, please review. If any of these errors\n";
3119 print "are false positives report them to the maintainer, see\n";
3120 print "CHECKPATCH in MAINTAINERS.\n";