3 # This script has been copied from Linux Kernel sources.
5 # (c) 2001, Dave Jones. (the file handling bit)
6 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
7 # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
8 # (c) 2008,2009, Andy Whitcroft <apw@canonical.com>
9 # Licensed under the terms of the GNU GPL License version 2
18 use Getopt::Long qw(:config no_auto_abbrev);
40 Usage: $P [OPTION]... [FILE]...
45 --no-tree run without a kernel tree
46 --no-signoff do not check for 'Signed-off-by' line
47 --patch treat FILE as patchfile (default)
48 --emacs emacs compile window format
49 --terse one line per report
50 -f, --file treat FILE as regular source file
51 --subjective, --strict enable more subjective tests
52 --root=PATH PATH to the kernel tree root
53 --no-summary suppress the per-file summary
54 --mailback only produce a report in case of warnings/errors
55 --summary-file include the filename in summary
56 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
57 'values', 'possible', 'type', and 'attr' (default
59 --test-only=WORD report only warnings/errors containing WORD
61 -h, --help, --version display this help and exit
63 When FILE is - read standard input.
70 'q|quiet+' => \$quiet,
72 'signoff!' => \$chk_signoff,
73 'patch!' => \$chk_patch,
77 'subjective!' => \$check,
80 'summary!' => \$summary,
81 'mailback!' => \$mailback,
82 'summary-file!' => \$summary_file,
85 'test-only=s' => \$tst_only,
95 print "$P: no input files\n";
100 my $dbg_possible = 0;
103 for my $key (keys %debug) {
105 eval "\${dbg_$key} = '$debug{$key}';";
116 if (!top_of_kernel_tree($root)) {
117 die "$P: $root: --root does not point at a valid tree\n";
120 if (top_of_kernel_tree('.')) {
122 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
123 top_of_kernel_tree($1)) {
128 if (!defined $root) {
129 print "Must be run from the top-level dir. of a kernel tree\n";
134 my $emitted_corrupt = 0;
137 [A-Za-z_][A-Za-z\d_]*
138 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
140 our $Storage = qr{extern|static|asmlinkage};
152 # Notes to $Attribute:
153 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
158 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
159 ____cacheline_aligned|
160 ____cacheline_aligned_in_smp|
161 ____cacheline_internodealigned_in_smp|
165 our $Inline = qr{inline|__always_inline|noinline};
166 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
167 our $Lval = qr{$Ident(?:$Member)*};
169 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
170 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
171 our $Compare = qr{<=|>=|==|!=|<|>};
175 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
183 [\x09\x0A\x0D\x20-\x7E] # ASCII
184 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
185 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
186 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
187 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
188 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
189 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
190 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
193 our $typeTypedefs = qr{(?x:
194 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
198 our $logFunctions = qr{(?x:
200 pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)|
201 dev_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)|
208 qr{(?:unsigned\s+)?char},
209 qr{(?:unsigned\s+)?short},
210 qr{(?:unsigned\s+)?int},
211 qr{(?:unsigned\s+)?long},
212 qr{(?:unsigned\s+)?long\s+int},
213 qr{(?:unsigned\s+)?long\s+long},
214 qr{(?:unsigned\s+)?long\s+long\s+int},
223 qr{${Ident}_handler},
224 qr{${Ident}_handler_fn},
226 our @modifierList = (
231 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
232 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
233 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
235 (?:$Modifier\s+|const\s+)*
237 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
241 (?:\s+$Modifier|\s+const)*
245 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
246 (?:\s+$Inline|\s+$Modifier)*
248 $Declare = qr{(?:$Storage\s+)?$Type};
252 $chk_signoff = 0 if ($file);
254 my @dep_includes = ();
255 my @dep_functions = ();
256 my $removal = "Documentation/feature-removal-schedule.txt";
257 if ($tree && -f "$root/$removal") {
258 open(my $REMOVE, '<', "$root/$removal") ||
259 die "$P: $removal: open failed - $!\n";
261 if (/^Check:\s+(.*\S)/) {
262 for my $entry (split(/[, ]+/, $1)) {
263 if ($entry =~ m@include/(.*)@) {
264 push(@dep_includes, $1);
266 } elsif ($entry !~ m@/@) {
267 push(@dep_functions, $entry);
278 for my $filename (@ARGV) {
281 open($FILE, '-|', "diff -u /dev/null $filename") ||
282 die "$P: $filename: diff failed - $!\n";
283 } elsif ($filename eq '-') {
284 open($FILE, '<&STDIN');
286 open($FILE, '<', "$filename") ||
287 die "$P: $filename: open failed - $!\n";
289 if ($filename eq '-') {
290 $vname = 'Your patch';
299 if (!process($filename)) {
308 sub top_of_kernel_tree {
312 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
313 "README", "Documentation", "arch", "include", "drivers",
314 "fs", "init", "ipc", "kernel", "lib", "scripts",
317 foreach my $check (@tree_check) {
318 if (! -e $root . '/' . $check) {
330 for my $c (split(//, $str)) {
334 for (; ($n % 8) != 0; $n++) {
346 (my $res = shift) =~ tr/\t/ /c;
353 # Drop the diff line leader and expand tabs
355 $line = expand_tabs($line);
357 # Pick the indent from the front of the line.
358 my ($white) = ($line =~ /^(\s*)/);
360 return (length($line), length($white));
363 my $sanitise_quote = '';
365 sub sanitise_line_reset {
366 my ($in_comment) = @_;
369 $sanitise_quote = '*/';
371 $sanitise_quote = '';
384 # Always copy over the diff marker.
385 $res = substr($line, 0, 1);
387 for ($off = 1; $off < length($line); $off++) {
388 $c = substr($line, $off, 1);
390 # Comments we are wacking completly including the begin
391 # and end, all to $;.
392 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
393 $sanitise_quote = '*/';
395 substr($res, $off, 2, "$;$;");
399 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
400 $sanitise_quote = '';
401 substr($res, $off, 2, "$;$;");
405 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
406 $sanitise_quote = '//';
408 substr($res, $off, 2, $sanitise_quote);
413 # A \ in a string means ignore the next character.
414 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
416 substr($res, $off, 2, 'XX');
421 if ($c eq "'" || $c eq '"') {
422 if ($sanitise_quote eq '') {
423 $sanitise_quote = $c;
425 substr($res, $off, 1, $c);
427 } elsif ($sanitise_quote eq $c) {
428 $sanitise_quote = '';
432 #print "c<$c> SQ<$sanitise_quote>\n";
433 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
434 substr($res, $off, 1, $;);
435 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
436 substr($res, $off, 1, $;);
437 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
438 substr($res, $off, 1, 'X');
440 substr($res, $off, 1, $c);
444 if ($sanitise_quote eq '//') {
445 $sanitise_quote = '';
448 # The pathname on a #include may be surrounded by '<' and '>'.
449 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
450 my $clean = 'X' x length($1);
451 $res =~ s@\<.*\>@<$clean>@;
453 # The whole of a #error is a string.
454 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
455 my $clean = 'X' x length($1);
456 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
462 sub ctx_statement_block {
463 my ($linenr, $remain, $off) = @_;
464 my $line = $linenr - 1;
481 @stack = (['', 0]) if ($#stack == -1);
483 #warn "CSB: blk<$blk> remain<$remain>\n";
484 # If we are about to drop off the end, pull in more
487 for (; $remain > 0; $line++) {
488 last if (!defined $lines[$line]);
489 next if ($lines[$line] =~ /^-/);
492 $blk .= $lines[$line] . "\n";
497 # Bail if there is no further context.
498 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
504 $c = substr($blk, $off, 1);
505 $remainder = substr($blk, $off);
507 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
509 # Handle nested #if/#else.
510 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
511 push(@stack, [ $type, $level ]);
512 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
513 ($type, $level) = @{$stack[$#stack - 1]};
514 } elsif ($remainder =~ /^#\s*endif\b/) {
515 ($type, $level) = @{pop(@stack)};
518 # Statement ends at the ';' or a close '}' at the
520 if ($level == 0 && $c eq ';') {
524 # An else is really a conditional as long as its not else if
525 if ($level == 0 && $coff_set == 0 &&
526 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
527 $remainder =~ /^(else)(?:\s|{)/ &&
528 $remainder !~ /^else\s+if\b/) {
529 $coff = $off + length($1) - 1;
531 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
532 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
535 if (($type eq '' || $type eq '(') && $c eq '(') {
539 if ($type eq '(' && $c eq ')') {
541 $type = ($level != 0)? '(' : '';
543 if ($level == 0 && $coff < $soff) {
546 #warn "CSB: mark coff<$coff>\n";
549 if (($type eq '' || $type eq '{') && $c eq '{') {
553 if ($type eq '{' && $c eq '}') {
555 $type = ($level != 0)? '{' : '';
563 # We are truly at the end, so shuffle to the next line.
570 my $statement = substr($blk, $soff, $off - $soff + 1);
571 my $condition = substr($blk, $soff, $coff - $soff + 1);
573 #warn "STATEMENT<$statement>\n";
574 #warn "CONDITION<$condition>\n";
576 #print "coff<$coff> soff<$off> loff<$loff>\n";
578 return ($statement, $condition,
579 $line, $remain + 1, $off - $loff + 1, $level);
582 sub statement_lines {
585 # Strip the diff line prefixes and rip blank lines at start and end.
586 $stmt =~ s/(^|\n)./$1/g;
590 my @stmt_lines = ($stmt =~ /\n/g);
592 return $#stmt_lines + 2;
595 sub statement_rawlines {
598 my @stmt_lines = ($stmt =~ /\n/g);
600 return $#stmt_lines + 2;
603 sub statement_block_size {
606 $stmt =~ s/(^|\n)./$1/g;
612 my @stmt_lines = ($stmt =~ /\n/g);
613 my @stmt_statements = ($stmt =~ /;/g);
615 my $stmt_lines = $#stmt_lines + 2;
616 my $stmt_statements = $#stmt_statements + 1;
618 if ($stmt_lines > $stmt_statements) {
621 return $stmt_statements;
625 sub ctx_statement_full {
626 my ($linenr, $remain, $off) = @_;
627 my ($statement, $condition, $level);
631 # Grab the first conditional/block pair.
632 ($statement, $condition, $linenr, $remain, $off, $level) =
633 ctx_statement_block($linenr, $remain, $off);
634 #print "F: c<$condition> s<$statement> remain<$remain>\n";
635 push(@chunks, [ $condition, $statement ]);
636 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
637 return ($level, $linenr, @chunks);
640 # Pull in the following conditional/block pairs and see if they
641 # could continue the statement.
643 ($statement, $condition, $linenr, $remain, $off, $level) =
644 ctx_statement_block($linenr, $remain, $off);
645 #print "C: c<$condition> s<$statement> remain<$remain>\n";
646 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
648 push(@chunks, [ $condition, $statement ]);
651 return ($level, $linenr, @chunks);
655 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
657 my $start = $linenr - 1;
664 my @stack = ($level);
665 for ($line = $start; $remain > 0; $line++) {
666 next if ($rawlines[$line] =~ /^-/);
669 $blk .= $rawlines[$line];
671 # Handle nested #if/#else.
672 if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
673 push(@stack, $level);
674 } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
675 $level = $stack[$#stack - 1];
676 } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
677 $level = pop(@stack);
680 foreach my $c (split(//, $rawlines[$line])) {
681 ##print "C<$c>L<$level><$open$close>O<$off>\n";
687 if ($c eq $close && $level > 0) {
689 last if ($level == 0);
690 } elsif ($c eq $open) {
695 if (!$outer || $level <= 1) {
696 push(@res, $rawlines[$line]);
699 last if ($level == 0);
702 return ($level, @res);
704 sub ctx_block_outer {
705 my ($linenr, $remain) = @_;
707 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
711 my ($linenr, $remain) = @_;
713 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
717 my ($linenr, $remain, $off) = @_;
719 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
722 sub ctx_block_level {
723 my ($linenr, $remain) = @_;
725 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
727 sub ctx_statement_level {
728 my ($linenr, $remain, $off) = @_;
730 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
733 sub ctx_locate_comment {
734 my ($first_line, $end_line) = @_;
736 # Catch a comment on the end of the line itself.
737 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
738 return $current_comment if (defined $current_comment);
740 # Look through the context and try and figure out if there is a
743 $current_comment = '';
744 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
745 my $line = $rawlines[$linenr - 1];
747 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
750 if ($line =~ m@/\*@) {
753 if (!$in_comment && $current_comment ne '') {
754 $current_comment = '';
756 $current_comment .= $line . "\n" if ($in_comment);
757 if ($line =~ m@\*/@) {
762 chomp($current_comment);
763 return($current_comment);
765 sub ctx_has_comment {
766 my ($first_line, $end_line) = @_;
767 my $cmt = ctx_locate_comment($first_line, $end_line);
769 ##print "LINE: $rawlines[$end_line - 1 ]\n";
770 ##print "CMMT: $cmt\n";
776 my ($linenr, $cnt) = @_;
778 my $offset = $linenr - 1;
783 $line = $rawlines[$offset++];
784 next if (defined($line) && $line =~ /^-/);
796 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
799 $coded = sprintf("^%c", unpack('C', $2) + 64);
808 my $av_preprocessor = 0;
814 $av_preprocessor = 0;
816 @av_paren_type = ('E');
817 $av_pend_colon = 'O';
820 sub annotate_values {
821 my ($stream, $type) = @_;
824 my $var = '_' x length($stream);
827 print "$stream\n" if ($dbg_values > 1);
829 while (length($cur)) {
830 @av_paren_type = ('E') if ($#av_paren_type < 0);
831 print " <" . join('', @av_paren_type) .
832 "> <$type> <$av_pending>" if ($dbg_values > 1);
833 if ($cur =~ /^(\s+)/o) {
834 print "WS($1)\n" if ($dbg_values > 1);
835 if ($1 =~ /\n/ && $av_preprocessor) {
836 $type = pop(@av_paren_type);
837 $av_preprocessor = 0;
840 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
841 print "DECLARE($1)\n" if ($dbg_values > 1);
844 } elsif ($cur =~ /^($Modifier)\s*/) {
845 print "MODIFIER($1)\n" if ($dbg_values > 1);
848 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
849 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
850 $av_preprocessor = 1;
851 push(@av_paren_type, $type);
857 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
858 print "UNDEF($1)\n" if ($dbg_values > 1);
859 $av_preprocessor = 1;
860 push(@av_paren_type, $type);
862 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
863 print "PRE_START($1)\n" if ($dbg_values > 1);
864 $av_preprocessor = 1;
866 push(@av_paren_type, $type);
867 push(@av_paren_type, $type);
870 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
871 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
872 $av_preprocessor = 1;
874 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
878 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
879 print "PRE_END($1)\n" if ($dbg_values > 1);
881 $av_preprocessor = 1;
883 # Assume all arms of the conditional end as this
884 # one does, and continue as if the #endif was not here.
886 push(@av_paren_type, $type);
889 } elsif ($cur =~ /^(\\\n)/o) {
890 print "PRECONT($1)\n" if ($dbg_values > 1);
892 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
893 print "ATTR($1)\n" if ($dbg_values > 1);
897 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
898 print "SIZEOF($1)\n" if ($dbg_values > 1);
904 } elsif ($cur =~ /^(if|while|for)\b/o) {
905 print "COND($1)\n" if ($dbg_values > 1);
909 } elsif ($cur =~/^(case)/o) {
910 print "CASE($1)\n" if ($dbg_values > 1);
911 $av_pend_colon = 'C';
914 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
915 print "KEYWORD($1)\n" if ($dbg_values > 1);
918 } elsif ($cur =~ /^(\()/o) {
919 print "PAREN('$1')\n" if ($dbg_values > 1);
920 push(@av_paren_type, $av_pending);
924 } elsif ($cur =~ /^(\))/o) {
925 my $new_type = pop(@av_paren_type);
926 if ($new_type ne '_') {
928 print "PAREN('$1') -> $type\n"
929 if ($dbg_values > 1);
931 print "PAREN('$1')\n" if ($dbg_values > 1);
934 } elsif ($cur =~ /^($Ident)\s*\(/o) {
935 print "FUNC($1)\n" if ($dbg_values > 1);
939 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
940 if (defined $2 && $type eq 'C' || $type eq 'T') {
941 $av_pend_colon = 'B';
942 } elsif ($type eq 'E') {
943 $av_pend_colon = 'L';
945 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
948 } elsif ($cur =~ /^($Ident|$Constant)/o) {
949 print "IDENT($1)\n" if ($dbg_values > 1);
952 } elsif ($cur =~ /^($Assignment)/o) {
953 print "ASSIGN($1)\n" if ($dbg_values > 1);
956 } elsif ($cur =~/^(;|{|})/) {
957 print "END($1)\n" if ($dbg_values > 1);
959 $av_pend_colon = 'O';
961 } elsif ($cur =~/^(,)/) {
962 print "COMMA($1)\n" if ($dbg_values > 1);
965 } elsif ($cur =~ /^(\?)/o) {
966 print "QUESTION($1)\n" if ($dbg_values > 1);
969 } elsif ($cur =~ /^(:)/o) {
970 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
972 substr($var, length($res), 1, $av_pend_colon);
973 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
978 $av_pend_colon = 'O';
980 } elsif ($cur =~ /^(\[)/o) {
981 print "CLOSE($1)\n" if ($dbg_values > 1);
984 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
987 print "OPV($1)\n" if ($dbg_values > 1);
994 substr($var, length($res), 1, $variant);
997 } elsif ($cur =~ /^($Operators)/o) {
998 print "OP($1)\n" if ($dbg_values > 1);
999 if ($1 ne '++' && $1 ne '--') {
1003 } elsif ($cur =~ /(^.)/o) {
1004 print "C($1)\n" if ($dbg_values > 1);
1007 $cur = substr($cur, length($1));
1008 $res .= $type x length($1);
1012 return ($res, $var);
1016 my ($possible, $line) = @_;
1017 my $notPermitted = qr{(?:
1032 ^(?:typedef|struct|enum)\b
1034 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1035 if ($possible !~ $notPermitted) {
1036 # Check for modifiers.
1037 $possible =~ s/\s*$Storage\s*//g;
1038 $possible =~ s/\s*$Sparse\s*//g;
1039 if ($possible =~ /^\s*$/) {
1041 } elsif ($possible =~ /\s/) {
1042 $possible =~ s/\s*$Type\s*//g;
1043 for my $modifier (split(' ', $possible)) {
1044 if ($modifier !~ $notPermitted) {
1045 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1046 push(@modifierList, $modifier);
1051 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1052 push(@typeList, $possible);
1056 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1063 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1066 my $line = $prefix . $_[0];
1068 $line = (split('\n', $line))[0] . "\n" if ($terse);
1070 push(our @report, $line);
1078 if (report("ERROR: $_[0]\n")) {
1084 if (report("WARNING: $_[0]\n")) {
1090 if ($check && report("CHECK: $_[0]\n")) {
1096 sub check_absolute_file {
1097 my ($absolute, $herecurr) = @_;
1098 my $file = $absolute;
1100 ##print "absolute<$absolute>\n";
1102 # See if any suffix of this path is a path within the tree.
1103 while ($file =~ s@^[^/]*/@@) {
1104 if (-f "$root/$file") {
1105 ##print "file<$file>\n";
1113 # It is, so see if the prefix is acceptable.
1114 my $prefix = $absolute;
1115 substr($prefix, -length($file)) = '';
1117 ##print "prefix<$prefix>\n";
1118 if ($prefix ne ".../") {
1119 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1124 my $filename = shift;
1130 my $stashrawline="";
1147 # Trace the real file/line as we go.
1153 my $comment_edge = 0;
1157 my $prev_values = 'E';
1160 my %suppress_ifbraces;
1161 my %suppress_whiletrailers;
1162 my %suppress_export;
1164 # Pre-scan the patch sanitizing the lines.
1165 # Pre-scan the patch looking for any __setup documentation.
1167 my @setup_docs = ();
1170 sanitise_line_reset();
1172 foreach my $rawline (@rawlines) {
1176 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1178 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1183 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1192 # Guestimate if this is a continuing comment. Run
1193 # the context looking for a comment "edge". If this
1194 # edge is a close comment then we must be in a comment
1198 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1199 next if (defined $rawlines[$ln - 1] &&
1200 $rawlines[$ln - 1] =~ /^-/);
1202 #print "RAW<$rawlines[$ln - 1]>\n";
1203 last if (!defined $rawlines[$ln - 1]);
1204 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1205 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1210 if (defined $edge && $edge eq '*/') {
1214 # Guestimate if this is a continuing comment. If this
1215 # is the start of a diff block and this line starts
1216 # ' *' then it is very likely a comment.
1217 if (!defined $edge &&
1218 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1223 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1224 sanitise_line_reset($in_comment);
1226 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1227 # Standardise the strings and chars within the input to
1228 # simplify matching -- only bother with positive lines.
1229 $line = sanitise_line($rawline);
1231 push(@lines, $line);
1234 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1239 #print "==>$rawline\n";
1240 #print "-->$line\n";
1242 if ($setup_docs && $line =~ /^\+/) {
1243 push(@setup_docs, $line);
1251 foreach my $line (@lines) {
1254 my $rawline = $rawlines[$linenr - 1];
1256 #extract the line range in the file after the patch is applied
1257 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1259 $first_line = $linenr + 1;
1269 %suppress_ifbraces = ();
1270 %suppress_whiletrailers = ();
1271 %suppress_export = ();
1274 # track the line number as we move through the hunk, note that
1275 # new versions of GNU diff omit the leading space on completely
1276 # blank context lines so we need to count that too.
1277 } elsif ($line =~ /^( |\+|$)/) {
1279 $realcnt-- if ($realcnt != 0);
1281 # Measure the line length and indent.
1282 ($length, $indent) = line_stats($rawline);
1284 # Track the previous line.
1285 ($prevline, $stashline) = ($stashline, $line);
1286 ($previndent, $stashindent) = ($stashindent, $indent);
1287 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1289 #warn "line<$line>\n";
1291 } elsif ($realcnt == 1) {
1295 my $hunk_line = ($realcnt != 0);
1297 #make up the handle for any error we report on this line
1298 $prefix = "$filename:$realline: " if ($emacs && $file);
1299 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1301 $here = "#$linenr: " if (!$file);
1302 $here = "#$realline: " if ($file);
1304 # extract the filename as it passes
1305 if ($line=~/^\+\+\+\s+(\S+)/) {
1307 $realfile =~ s@^([^/]*)/@@;
1310 if (!$file && $tree && $p1_prefix ne '' &&
1311 -e "$root/$p1_prefix") {
1312 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1315 if ($realfile =~ m@^include/asm/@) {
1316 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1321 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1323 my $hereline = "$here\n$rawline\n";
1324 my $herecurr = "$here\n$rawline\n";
1325 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1327 $cnt_lines++ if ($realcnt != 0);
1329 #check the patch for a signoff:
1330 if ($line =~ /^\s*signed-off-by:/i) {
1331 # This is a signoff, if ugly, so do not double report.
1333 if (!($line =~ /^\s*Signed-off-by:/)) {
1334 WARN("Signed-off-by: is the preferred form\n" .
1337 if ($line =~ /^\s*signed-off-by:\S/i) {
1338 WARN("space required after Signed-off-by:\n" .
1343 # Check for wrappage within a valid hunk of the file
1344 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1345 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1346 $herecurr) if (!$emitted_corrupt++);
1349 # Check for absolute kernel paths.
1351 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1354 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1355 check_absolute_file($1, $herecurr)) {
1358 check_absolute_file($file, $herecurr);
1363 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1364 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1365 $rawline !~ m/^$UTF8*$/) {
1366 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1368 my $blank = copy_spacing($rawline);
1369 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1370 my $hereptr = "$hereline$ptr\n";
1372 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1375 # ignore non-hunk lines and lines being removed
1376 next if (!$hunk_line || $line =~ /^-/);
1378 #trailing whitespace
1379 if ($line =~ /^\+.*\015/) {
1380 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1381 ERROR("DOS line endings\n" . $herevet);
1383 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1384 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1385 ERROR("trailing whitespace\n" . $herevet);
1388 # check for Kconfig help text having a real description
1389 if ($realfile =~ /Kconfig/ &&
1390 $line =~ /\+?\s*(---)?help(---)?$/) {
1392 for (my $l = $linenr; defined($lines[$l]); $l++) {
1396 next if ($f =~ /^$/);
1397 last if ($f =~ /^\s*config\s/);
1400 WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($length < 4);
1403 # check we are in a valid source file if not then ignore this hunk
1404 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1407 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1408 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1409 $line !~ /^\+\s*$logFunctions\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1412 WARN("line over 80 characters\n" . $herecurr);
1415 # check for spaces before a quoted newline
1416 if ($rawline =~ /^.*\".*\s\\n/) {
1417 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1420 # check for adding lines without a newline.
1421 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1422 WARN("adding a line without newline at end of file\n" . $herecurr);
1425 # Blackfin: use hi/lo macros
1426 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1427 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1428 my $herevet = "$here\n" . cat_vet($line) . "\n";
1429 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1431 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1432 my $herevet = "$here\n" . cat_vet($line) . "\n";
1433 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1437 # check we are in a valid source file C or perl if not then ignore this hunk
1438 next if ($realfile !~ /\.(h|c|pl)$/);
1440 # at the beginning of a line any tabs must come first and anything
1441 # more than 8 must use tabs.
1442 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1443 $rawline =~ /^\+\s* \s*/) {
1444 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1445 ERROR("code indent should use tabs where possible\n" . $herevet);
1448 # check for space before tabs.
1449 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1450 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1451 WARN("please, no space before tabs\n" . $herevet);
1454 # check we are in a valid C source file if not then ignore this hunk
1455 next if ($realfile !~ /\.(h|c)$/);
1457 # check for RCS/CVS revision markers
1458 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1459 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1462 # Blackfin: don't use __builtin_bfin_[cs]sync
1463 if ($line =~ /__builtin_bfin_csync/) {
1464 my $herevet = "$here\n" . cat_vet($line) . "\n";
1465 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1467 if ($line =~ /__builtin_bfin_ssync/) {
1468 my $herevet = "$here\n" . cat_vet($line) . "\n";
1469 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1472 # Check for potential 'bare' types
1473 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1475 if ($realcnt && $line =~ /.\s*\S/) {
1476 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1477 ctx_statement_block($linenr, $realcnt, 0);
1478 $stat =~ s/\n./\n /g;
1479 $cond =~ s/\n./\n /g;
1481 # Find the real next line.
1482 $realline_next = $line_nr_next;
1483 if (defined $realline_next &&
1484 (!defined $lines[$realline_next - 1] ||
1485 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1492 # Ignore goto labels.
1493 if ($s =~ /$Ident:\*$/s) {
1495 # Ignore functions being called
1496 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1498 } elsif ($s =~ /^.\s*else\b/s) {
1500 # declarations always start with types
1501 } 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) {
1504 possible($type, "A:" . $s);
1506 # definitions in global scope can only start with types
1507 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1508 possible($1, "B:" . $s);
1511 # any (foo ... *) is a pointer cast, and foo is a type
1512 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1513 possible($1, "C:" . $s);
1516 # Check for any sort of function declaration.
1517 # int foo(something bar, other baz);
1518 # void (*store_gdt)(x86_descr_ptr *);
1519 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1520 my ($name_len) = length($1);
1523 substr($ctx, 0, $name_len + 1, '');
1524 $ctx =~ s/\)[^\)]*$//;
1526 for my $arg (split(/\s*,\s*/, $ctx)) {
1527 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1529 possible($1, "D:" . $s);
1537 # Checks which may be anchored in the context.
1540 # Check for switch () and associated case and default
1541 # statements should be at the same indent.
1542 if ($line=~/\bswitch\s*\(.*\)/) {
1545 my @ctx = ctx_block_outer($linenr, $realcnt);
1547 for my $ctx (@ctx) {
1548 my ($clen, $cindent) = line_stats($ctx);
1549 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1550 $indent != $cindent) {
1551 $err .= "$sep$ctx\n";
1558 ERROR("switch and case should be at the same indent\n$hereline$err");
1562 # if/while/etc brace do not go on next line, unless defining a do while loop,
1563 # or if that brace on the next line is for something else
1564 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1565 my $pre_ctx = "$1$2";
1567 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1568 my $ctx_cnt = $realcnt - $#ctx - 1;
1569 my $ctx = join("\n", @ctx);
1571 my $ctx_ln = $linenr;
1572 my $ctx_skip = $realcnt;
1574 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1575 defined $lines[$ctx_ln - 1] &&
1576 $lines[$ctx_ln - 1] =~ /^-/)) {
1577 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1578 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1582 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1583 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1585 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1586 ERROR("that open brace { should be on the previous line\n" .
1587 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
1589 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1590 $ctx =~ /\)\s*\;\s*$/ &&
1591 defined $lines[$ctx_ln - 1])
1593 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1594 if ($nindent > $indent) {
1595 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1596 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
1601 # Check relative indent for conditionals and blocks.
1602 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1603 my ($s, $c) = ($stat, $cond);
1605 substr($s, 0, length($c), '');
1607 # Make sure we remove the line prefixes as we have
1608 # none on the first line, and are going to readd them
1612 # Find out how long the conditional actually is.
1613 my @newlines = ($c =~ /\n/gs);
1614 my $cond_lines = 1 + $#newlines;
1616 # We want to check the first line inside the block
1617 # starting at the end of the conditional, so remove:
1618 # 1) any blank line termination
1619 # 2) any opening brace { on end of the line
1621 my $continuation = 0;
1623 $s =~ s/^.*\bdo\b//;
1625 if ($s =~ s/^\s*\\//) {
1628 if ($s =~ s/^\s*?\n//) {
1633 # Also ignore a loop construct at the end of a
1634 # preprocessor statement.
1635 if (($prevline =~ /^.\s*#\s*define\s/ ||
1636 $prevline =~ /\\\s*$/) && $continuation == 0) {
1642 while ($cond_ptr != $cond_lines) {
1643 $cond_ptr = $cond_lines;
1645 # If we see an #else/#elif then the code
1647 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1652 # 1) blank lines, they should be at 0,
1653 # 2) preprocessor lines, and
1655 if ($continuation ||
1657 $s =~ /^\s*#\s*?/ ||
1658 $s =~ /^\s*$Ident\s*:/) {
1659 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1660 if ($s =~ s/^.*?\n//) {
1666 my (undef, $sindent) = line_stats("+" . $s);
1667 my $stat_real = raw_line($linenr, $cond_lines);
1669 # Check if either of these lines are modified, else
1670 # this is not this patch's fault.
1671 if (!defined($stat_real) ||
1672 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1675 if (defined($stat_real) && $cond_lines > 1) {
1676 $stat_real = "[...]\n$stat_real";
1679 #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";
1681 if ($check && (($sindent % 8) != 0 ||
1682 ($sindent <= $indent && $s ne ''))) {
1683 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1687 # Track the 'values' across context and added lines.
1688 my $opline = $line; $opline =~ s/^./ /;
1689 my ($curr_values, $curr_vars) =
1690 annotate_values($opline . "\n", $prev_values);
1691 $curr_values = $prev_values . $curr_values;
1693 my $outline = $opline; $outline =~ s/\t/ /g;
1694 print "$linenr > .$outline\n";
1695 print "$linenr > $curr_values\n";
1696 print "$linenr > $curr_vars\n";
1698 $prev_values = substr($curr_values, -1);
1700 #ignore lines not being added
1701 if ($line=~/^[^\+]/) {next;}
1703 # TEST: allow direct testing of the type matcher.
1705 if ($line =~ /^.\s*$Declare\s*$/) {
1706 ERROR("TEST: is type\n" . $herecurr);
1707 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1708 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1712 # TEST: allow direct testing of the attribute matcher.
1714 if ($line =~ /^.\s*$Modifier\s*$/) {
1715 ERROR("TEST: is attr\n" . $herecurr);
1716 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1717 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1722 # check for initialisation to aggregates open brace on the next line
1723 if ($line =~ /^.\s*{/ &&
1724 $prevline =~ /(?:^|[^=])=\s*$/) {
1725 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1729 # Checks which are anchored on the added line.
1732 # check for malformed paths in #include statements (uses RAW line)
1733 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1735 if ($path =~ m{//}) {
1736 ERROR("malformed #include filename\n" .
1741 # no C99 // comments
1742 if ($line =~ m{//}) {
1743 ERROR("do not use C99 // comments\n" . $herecurr);
1745 # Remove C99 comments.
1747 $opline =~ s@//.*@@;
1749 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1750 # the whole statement.
1751 #print "APW <$lines[$realline_next - 1]>\n";
1752 if (defined $realline_next &&
1753 exists $lines[$realline_next - 1] &&
1754 !defined $suppress_export{$realline_next} &&
1755 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1756 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1760 ^.DEFINE_$Ident\(\Q$name\E\)|
1761 ^.DECLARE_$Ident\(\Q$name\E\)|
1762 ^.LIST_HEAD\(\Q$name\E\)|
1763 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1764 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
1766 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1767 $suppress_export{$realline_next} = 2;
1769 $suppress_export{$realline_next} = 1;
1772 if (!defined $suppress_export{$linenr} &&
1773 $prevline =~ /^.\s*$/ &&
1774 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1775 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1776 #print "FOO B <$lines[$linenr - 1]>\n";
1777 $suppress_export{$linenr} = 2;
1779 if (defined $suppress_export{$linenr} &&
1780 $suppress_export{$linenr} == 2) {
1781 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1784 # check for external initialisers.
1785 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1786 ERROR("do not initialise externals to 0 or NULL\n" .
1789 # check for static initialisers.
1790 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1791 ERROR("do not initialise statics to 0 or NULL\n" .
1795 # check for new typedefs, only function parameters and sparse annotations
1797 if ($line =~ /\btypedef\s/ &&
1798 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1799 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
1800 $line !~ /\b$typeTypedefs\b/ &&
1801 $line !~ /\b__bitwise(?:__|)\b/) {
1802 WARN("do not add new typedefs\n" . $herecurr);
1805 # * goes on variable not on type
1807 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1808 my ($from, $to) = ($1, $1);
1810 # Should start with a space.
1811 $to =~ s/^(\S)/ $1/;
1812 # Should not end with a space.
1814 # '*'s should not have spaces between.
1815 while ($to =~ s/\*\s+\*/\*\*/) {
1818 #print "from<$from> to<$to>\n";
1820 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1822 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1823 my ($from, $to, $ident) = ($1, $1, $2);
1825 # Should start with a space.
1826 $to =~ s/^(\S)/ $1/;
1827 # Should not end with a space.
1829 # '*'s should not have spaces between.
1830 while ($to =~ s/\*\s+\*/\*\*/) {
1832 # Modifiers should have spaces.
1833 $to =~ s/(\b$Modifier$)/$1 /;
1835 #print "from<$from> to<$to> ident<$ident>\n";
1836 if ($from ne $to && $ident !~ /^$Modifier$/) {
1837 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1841 # # no BUG() or BUG_ON()
1842 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1843 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1844 # print "$herecurr";
1848 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1849 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1852 # printk should use KERN_* levels. Note that follow on printk's on the
1853 # same line do not need a level, so we use the current block context
1854 # to try and find and validate the current printk. In summary the current
1855 # printk includes all preceeding printk's which have no newline on the end.
1856 # we assume the first bad printk is the one to report.
1857 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1859 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1860 #print "CHECK<$lines[$ln - 1]\n";
1861 # we have a preceeding printk if it ends
1862 # with "\n" ignore it, else it is to blame
1863 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1864 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1871 WARN("printk() should include KERN_ facility level\n" . $herecurr);
1875 # function brace can't be on same line, except for #defines of do while,
1876 # or if closed on same line
1877 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1878 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1879 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1882 # open braces for enum, union and struct go on the same line.
1883 if ($line =~ /^.\s*{/ &&
1884 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1885 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1888 # check for spacing round square brackets; allowed:
1889 # 1. with a type on the left -- int [] a;
1890 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1891 # 3. inside a curly brace -- = { [0...10] = 5 }
1892 while ($line =~ /(.*?\s)\[/g) {
1893 my ($where, $prefix) = ($-[1], $1);
1894 if ($prefix !~ /$Type\s+$/ &&
1895 ($where != 0 || $prefix !~ /^.\s+$/) &&
1896 $prefix !~ /{\s+$/) {
1897 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1901 # check for spaces between functions and their parentheses.
1902 while ($line =~ /($Ident)\s+\(/g) {
1904 my $ctx_before = substr($line, 0, $-[1]);
1905 my $ctx = "$ctx_before$name";
1907 # Ignore those directives where spaces _are_ permitted.
1909 if|for|while|switch|return|case|
1910 volatile|__volatile__|
1911 __attribute__|format|__extension__|
1915 # cpp #define statements have non-optional spaces, ie
1916 # if there is a space between the name and the open
1917 # parenthesis it is simply not a parameter group.
1918 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1920 # cpp #elif statement condition may start with a (
1921 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1923 # If this whole things ends with a type its most
1924 # likely a typedef for a function.
1925 } elsif ($ctx =~ /$Type$/) {
1928 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1931 # Check operator spacing.
1932 if (!($line=~/\#\s*include/)) {
1934 <<=|>>=|<=|>=|==|!=|
1935 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1936 =>|->|<<|>>|<|>|=|!|~|
1937 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1940 my @elements = split(/($ops|;)/, $opline);
1943 my $blank = copy_spacing($opline);
1945 for (my $n = 0; $n < $#elements; $n += 2) {
1946 $off += length($elements[$n]);
1948 # Pick up the preceeding and succeeding characters.
1949 my $ca = substr($opline, 0, $off);
1951 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1952 $cc = substr($opline, $off + length($elements[$n + 1]));
1954 my $cb = "$ca$;$cc";
1957 $a = 'V' if ($elements[$n] ne '');
1958 $a = 'W' if ($elements[$n] =~ /\s$/);
1959 $a = 'C' if ($elements[$n] =~ /$;$/);
1960 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1961 $a = 'O' if ($elements[$n] eq '');
1962 $a = 'E' if ($ca =~ /^\s*$/);
1964 my $op = $elements[$n + 1];
1967 if (defined $elements[$n + 2]) {
1968 $c = 'V' if ($elements[$n + 2] ne '');
1969 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1970 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1971 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1972 $c = 'O' if ($elements[$n + 2] eq '');
1973 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
1978 my $ctx = "${a}x${c}";
1980 my $at = "(ctx:$ctx)";
1982 my $ptr = substr($blank, 0, $off) . "^";
1983 my $hereptr = "$hereline$ptr\n";
1985 # Pull out the value of this operator.
1986 my $op_type = substr($curr_values, $off + 1, 1);
1988 # Get the full operator variant.
1989 my $opv = $op . substr($curr_vars, $off, 1);
1991 # Ignore operators passed as parameters.
1992 if ($op_type ne 'V' &&
1993 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1996 # } elsif ($op =~ /^$;+$/) {
1998 # ; should have either the end of line or a space or \ after it
1999 } elsif ($op eq ';') {
2000 if ($ctx !~ /.x[WEBC]/ &&
2001 $cc !~ /^\\/ && $cc !~ /^;/) {
2002 ERROR("space required after that '$op' $at\n" . $hereptr);
2006 } elsif ($op eq '//') {
2010 # : when part of a bitfield
2011 } elsif ($op eq '->' || $opv eq ':B') {
2012 if ($ctx =~ /Wx.|.xW/) {
2013 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
2016 # , must have a space on the right.
2017 } elsif ($op eq ',') {
2018 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2019 ERROR("space required after that '$op' $at\n" . $hereptr);
2022 # '*' as part of a type definition -- reported already.
2023 } elsif ($opv eq '*_') {
2024 #warn "'*' is part of type\n";
2026 # unary operators should have a space before and
2027 # none after. May be left adjacent to another
2028 # unary operator, or a cast
2029 } elsif ($op eq '!' || $op eq '~' ||
2030 $opv eq '*U' || $opv eq '-U' ||
2031 $opv eq '&U' || $opv eq '&&U') {
2032 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2033 ERROR("space required before that '$op' $at\n" . $hereptr);
2035 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2036 # A unary '*' may be const
2038 } elsif ($ctx =~ /.xW/) {
2039 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2042 # unary ++ and unary -- are allowed no space on one side.
2043 } elsif ($op eq '++' or $op eq '--') {
2044 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2045 ERROR("space required one side of that '$op' $at\n" . $hereptr);
2047 if ($ctx =~ /Wx[BE]/ ||
2048 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2049 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2051 if ($ctx =~ /ExW/) {
2052 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2056 # << and >> may either have or not have spaces both sides
2057 } elsif ($op eq '<<' or $op eq '>>' or
2058 $op eq '&' or $op eq '^' or $op eq '|' or
2059 $op eq '+' or $op eq '-' or
2060 $op eq '*' or $op eq '/' or
2063 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2064 ERROR("need consistent spacing around '$op' $at\n" .
2068 # A colon needs no spaces before when it is
2069 # terminating a case value or a label.
2070 } elsif ($opv eq ':C' || $opv eq ':L') {
2071 if ($ctx =~ /Wx./) {
2072 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2075 # All the others need spaces both sides.
2076 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2079 # Ignore email addresses <foo@bar>
2081 $cc =~ /^\S+\@\S+>/) ||
2083 $ca =~ /<\S+\@\S+$/))
2089 if (($opv eq ':O' && $ca =~ /\?$/) ||
2090 ($op eq '?' && $cc =~ /^:/)) {
2095 ERROR("spaces required around that '$op' $at\n" . $hereptr);
2098 $off += length($elements[$n + 1]);
2102 # check for multiple assignments
2103 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2104 CHK("multiple assignments should be avoided\n" . $herecurr);
2107 ## # check for multiple declarations, allowing for a function declaration
2109 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2110 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2112 ## # Remove any bracketed sections to ensure we do not
2113 ## # falsly report the parameters of functions.
2115 ## while ($ln =~ s/\([^\(\)]*\)//g) {
2117 ## if ($ln =~ /,/) {
2118 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2122 #need space before brace following if, while, etc
2123 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2125 ERROR("space required before the open brace '{'\n" . $herecurr);
2128 # closing brace should have a space following it when it has anything
2130 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2131 ERROR("space required after that close brace '}'\n" . $herecurr);
2134 # check spacing on square brackets
2135 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2136 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2138 if ($line =~ /\s\]/) {
2139 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2142 # check spacing on parentheses
2143 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2144 $line !~ /for\s*\(\s+;/) {
2145 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2147 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2148 $line !~ /for\s*\(.*;\s+\)/ &&
2149 $line !~ /:\s+\)/) {
2150 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2153 #goto labels aren't indented, allow a single space however
2154 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2155 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2156 WARN("labels should not be indented\n" . $herecurr);
2159 # Return is not a function.
2160 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2164 # Flatten any parentheses
2165 $value =~ s/\)\(/\) \(/g;
2166 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2167 $value !~ /(?:$Ident|-?$Constant)\s*
2169 (?:$Ident|-?$Constant)/x &&
2170 $value =~ s/\([^\(\)]*\)/1/) {
2173 if ($value =~ /^(?:$Ident|-?$Constant)$/) {
2174 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2176 } elsif ($spacing !~ /\s+/) {
2177 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2181 # Need a space before open parenthesis after if, while etc
2182 if ($line=~/\b(if|while|for|switch)\(/) {
2183 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2186 # Check for illegal assignment in if conditional -- and check for trailing
2187 # statements after the conditional.
2188 if ($line =~ /do\s*(?!{)/) {
2189 my ($stat_next) = ctx_statement_block($line_nr_next,
2190 $remain_next, $off_next);
2191 $stat_next =~ s/\n./\n /g;
2192 ##print "stat<$stat> stat_next<$stat_next>\n";
2194 if ($stat_next =~ /^\s*while\b/) {
2195 # If the statement carries leading newlines,
2196 # then count those as offsets.
2198 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2200 statement_rawlines($whitespace) - 1;
2202 $suppress_whiletrailers{$line_nr_next +
2206 if (!defined $suppress_whiletrailers{$linenr} &&
2207 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2208 my ($s, $c) = ($stat, $cond);
2210 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2211 ERROR("do not use assignment in if condition\n" . $herecurr);
2214 # Find out what is on the end of the line after the
2216 substr($s, 0, length($c), '');
2218 $s =~ s/$;//g; # Remove any comments
2219 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2220 $c !~ /}\s*while\s*/)
2222 # Find out how long the conditional actually is.
2223 my @newlines = ($c =~ /\n/gs);
2224 my $cond_lines = 1 + $#newlines;
2227 $stat_real = raw_line($linenr, $cond_lines)
2228 . "\n" if ($cond_lines);
2229 if (defined($stat_real) && $cond_lines > 1) {
2230 $stat_real = "[...]\n$stat_real";
2233 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2237 # Check for bitwise tests written as boolean
2249 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2252 # if and else should not have general statements after it
2253 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2255 $s =~ s/$;//g; # Remove any comments
2256 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2257 ERROR("trailing statements should be on next line\n" . $herecurr);
2260 # if should not continue a brace
2261 if ($line =~ /}\s*if\b/) {
2262 ERROR("trailing statements should be on next line\n" .
2265 # case and default should not have general statements after them
2266 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2268 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2272 ERROR("trailing statements should be on next line\n" . $herecurr);
2275 # Check for }<nl>else {, these must be at the same
2276 # indent level to be relevant to each other.
2277 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2278 $previndent == $indent) {
2279 ERROR("else should follow close brace '}'\n" . $hereprev);
2282 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2283 $previndent == $indent) {
2284 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2286 # Find out what is on the end of the line after the
2288 substr($s, 0, length($c), '');
2291 if ($s =~ /^\s*;/) {
2292 ERROR("while should follow close brace '}'\n" . $hereprev);
2296 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2297 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2298 # print "No studly caps, use _\n";
2299 # print "$herecurr";
2303 #no spaces allowed after \ in define
2304 if ($line=~/\#\s*define.*\\\s$/) {
2305 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
2308 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2309 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2311 my $checkfile = "include/linux/$file";
2312 if (-f "$root/$checkfile" &&
2313 $realfile ne $checkfile &&
2316 if ($realfile =~ m{^arch/}) {
2317 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2319 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2324 # multi-statement macros should be enclosed in a do while loop, grab the
2325 # first statement and ensure its the whole macro if its not enclosed
2326 # in a known good container
2327 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2328 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2331 my ($off, $dstat, $dcond, $rest);
2334 my $args = defined($1);
2336 # Find the end of the macro and limit our statement
2338 while ($cnt > 0 && defined $lines[$ln - 1] &&
2339 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2341 $ctx .= $rawlines[$ln - 1] . "\n";
2342 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2345 $ctx .= $rawlines[$ln - 1];
2347 ($dstat, $dcond, $ln, $cnt, $off) =
2348 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2349 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2350 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2352 # Extract the remainder of the define (if any) and
2353 # rip off surrounding spaces, and trailing \'s.
2355 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2356 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2357 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2358 $rest .= substr($lines[$ln - 1], $off) . "\n";
2364 $rest =~ s/\\\n.//g;
2368 # Clean up the original statement.
2370 substr($dstat, 0, length($dcond), '');
2372 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2375 $dstat =~ s/\\\n.//g;
2376 $dstat =~ s/^\s*//s;
2377 $dstat =~ s/\s*$//s;
2379 # Flatten any parentheses and braces
2380 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2381 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2382 $dstat =~ s/\[[^\{\}]*\]/1/)
2386 my $exceptions = qr{
2398 #print "REST<$rest> dstat<$dstat>\n";
2400 if ($rest !~ /while\s*\(/ &&
2401 $dstat !~ /$exceptions/)
2403 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2406 } elsif ($ctx !~ /;/) {
2408 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2409 $dstat !~ /$exceptions/ &&
2410 $dstat !~ /^\.$Ident\s*=/ &&
2411 $dstat =~ /$Operators/)
2413 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2418 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2419 # all assignments may have only one of the following with an assignment:
2422 # VMLINUX_SYMBOL(...)
2423 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2424 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2427 # check for redundant bracing round if etc
2428 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2429 my ($level, $endln, @chunks) =
2430 ctx_statement_full($linenr, $realcnt, 1);
2431 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2432 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2433 if ($#chunks > 0 && $level == 0) {
2436 my $herectx = $here . "\n";
2437 my $ln = $linenr - 1;
2438 for my $chunk (@chunks) {
2439 my ($cond, $block) = @{$chunk};
2441 # If the condition carries leading newlines, then count those as offsets.
2442 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2443 my $offset = statement_rawlines($whitespace) - 1;
2445 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2447 # We have looked at and allowed this specific line.
2448 $suppress_ifbraces{$ln + $offset} = 1;
2450 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2451 $ln += statement_rawlines($block) - 1;
2453 substr($block, 0, length($cond), '');
2455 $seen++ if ($block =~ /^\s*{/);
2457 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2458 if (statement_lines($cond) > 1) {
2459 #print "APW: ALLOWED: cond<$cond>\n";
2462 if ($block =~/\b(?:if|for|while)\b/) {
2463 #print "APW: ALLOWED: block<$block>\n";
2466 if (statement_block_size($block) > 1) {
2467 #print "APW: ALLOWED: lines block<$block>\n";
2471 if ($seen && !$allowed) {
2472 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
2476 if (!defined $suppress_ifbraces{$linenr - 1} &&
2477 $line =~ /\b(if|while|for|else)\b/) {
2480 # Check the pre-context.
2481 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2482 #print "APW: ALLOWED: pre<$1>\n";
2486 my ($level, $endln, @chunks) =
2487 ctx_statement_full($linenr, $realcnt, $-[0]);
2489 # Check the condition.
2490 my ($cond, $block) = @{$chunks[0]};
2491 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2492 if (defined $cond) {
2493 substr($block, 0, length($cond), '');
2495 if (statement_lines($cond) > 1) {
2496 #print "APW: ALLOWED: cond<$cond>\n";
2499 if ($block =~/\b(?:if|for|while)\b/) {
2500 #print "APW: ALLOWED: block<$block>\n";
2503 if (statement_block_size($block) > 1) {
2504 #print "APW: ALLOWED: lines block<$block>\n";
2507 # Check the post-context.
2508 if (defined $chunks[1]) {
2509 my ($cond, $block) = @{$chunks[1]};
2510 if (defined $cond) {
2511 substr($block, 0, length($cond), '');
2513 if ($block =~ /^\s*\{/) {
2514 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2518 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2519 my $herectx = $here . "\n";;
2520 my $cnt = statement_rawlines($block);
2522 for (my $n = 0; $n < $cnt; $n++) {
2523 $herectx .= raw_line($linenr, $n) . "\n";;
2526 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2530 # don't include deprecated include files (uses RAW line)
2531 for my $inc (@dep_includes) {
2532 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2533 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2537 # don't use deprecated functions
2538 for my $func (@dep_functions) {
2539 if ($line =~ /\b$func\b/) {
2540 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2544 # no volatiles please
2545 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2546 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2547 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2550 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2551 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2552 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2556 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2557 CHK("if this code is redundant consider removing it\n" .
2561 # check for needless kfree() checks
2562 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2564 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2565 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2568 # check for needless usb_free_urb() checks
2569 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2571 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2572 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2576 # warn about #ifdefs in C files
2577 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2578 # print "#ifdef in C files should be avoided\n";
2579 # print "$herecurr";
2583 # warn about spacing in #ifdefs
2584 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2585 ERROR("exactly one space required after that #$1\n" . $herecurr);
2588 # check for spinlock_t definitions without a comment.
2589 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2590 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
2592 if (!ctx_has_comment($first_line, $linenr)) {
2593 CHK("$1 definition without comment\n" . $herecurr);
2596 # check for memory barriers without a comment.
2597 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2598 if (!ctx_has_comment($first_line, $linenr)) {
2599 CHK("memory barrier without comment\n" . $herecurr);
2602 # check of hardware specific defines
2603 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2604 CHK("architecture specific defines should be avoided\n" . $herecurr);
2607 # Check that the storage class is at the beginning of a declaration
2608 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2609 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2612 # check the location of the inline attribute, that it is between
2613 # storage class and type.
2614 if ($line =~ /\b$Type\s+$Inline\b/ ||
2615 $line =~ /\b$Inline\s+$Storage\b/) {
2616 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2619 # Check for __inline__ and __inline, prefer inline
2620 if ($line =~ /\b(__inline__|__inline)\b/) {
2621 WARN("plain inline is preferred over $1\n" . $herecurr);
2624 # check for sizeof(&)
2625 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2626 WARN("sizeof(& should be avoided\n" . $herecurr);
2629 # check for new externs in .c files.
2630 if ($realfile =~ /\.c$/ && defined $stat &&
2631 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2633 my $function_name = $1;
2634 my $paren_space = $2;
2637 if (defined $cond) {
2638 substr($s, 0, length($cond), '');
2640 if ($s =~ /^\s*;/ &&
2641 $function_name ne 'uninitialized_var')
2643 WARN("externs should be avoided in .c files\n" . $herecurr);
2646 if ($paren_space =~ /\n/) {
2647 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2650 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2651 $stat =~ /^.\s*extern\s+/)
2653 WARN("externs should be avoided in .c files\n" . $herecurr);
2656 # checks for new __setup's
2657 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2660 if (!grep(/$name/, @setup_docs)) {
2661 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2665 # check for pointless casting of kmalloc return
2666 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2667 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2670 # check for gcc specific __FUNCTION__
2671 if ($line =~ /__FUNCTION__/) {
2672 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2675 # check for semaphores used as mutexes
2676 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2677 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2679 # check for semaphores used as mutexes
2680 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2681 WARN("consider using a completion\n" . $herecurr);
2684 # recommend strict_strto* over simple_strto*
2685 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2686 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2688 # check for __initcall(), use device_initcall() explicitly please
2689 if ($line =~ /^.\s*__initcall\s*\(/) {
2690 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2692 # check for various ops structs, ensure they are const.
2693 my $struct_ops = qr{acpi_dock_ops|
2694 address_space_operations|
2696 block_device_operations|
2701 file_lock_operations|
2711 lock_manager_operations|
2717 pipe_buf_operations|
2718 platform_hibernation_ops|
2719 platform_suspend_ops|
2724 soc_pcmcia_socket_ops|
2730 if ($line !~ /\bconst\b/ &&
2731 $line =~ /\bstruct\s+($struct_ops)\b/) {
2732 WARN("struct $1 should normally be const\n" .
2736 # use of NR_CPUS is usually wrong
2737 # ignore definitions of NR_CPUS and usage to define arrays as likely right
2738 if ($line =~ /\bNR_CPUS\b/ &&
2739 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2740 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2741 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2742 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2743 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2745 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2748 # check for %L{u,d,i} in strings
2750 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2751 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2752 $string =~ s/%%/__/g;
2753 if ($string =~ /(?<!%)%L[udi]/) {
2754 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2759 # whine mightly about in_atomic
2760 if ($line =~ /\bin_atomic\s*\(/) {
2761 if ($realfile =~ m@^drivers/@) {
2762 ERROR("do not use in_atomic in drivers\n" . $herecurr);
2763 } elsif ($realfile !~ m@^kernel/@) {
2764 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2768 # check for lockdep_set_novalidate_class
2769 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
2770 $line =~ /__lockdep_no_validate__\s*\)/ ) {
2771 if ($realfile !~ m@^kernel/lockdep@ &&
2772 $realfile !~ m@^include/linux/lockdep@ &&
2773 $realfile !~ m@^drivers/base/core@) {
2774 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
2779 # If we have no input at all, then there is nothing to report on
2780 # so just keep quiet.
2781 if ($#rawlines == -1) {
2785 # In mailback mode only produce a report in the negative, for
2786 # things that appear to be patches.
2787 if ($mailback && ($clean == 1 || !$is_patch)) {
2791 # This is not a patch, and we are are in 'no-patch' mode so
2793 if (!$chk_patch && !$is_patch) {
2798 ERROR("Does not appear to be a unified-diff format patch\n");
2800 if ($is_patch && $chk_signoff && $signoff == 0) {
2801 ERROR("Missing Signed-off-by: line(s)\n");
2804 print report_dump();
2805 if ($summary && !($clean == 1 && $quiet == 1)) {
2806 print "$filename " if ($summary_file);
2807 print "total: $cnt_error errors, $cnt_warn warnings, " .
2808 (($check)? "$cnt_chk checks, " : "") .
2809 "$cnt_lines lines checked\n";
2810 print "\n" if ($quiet == 0);
2813 if ($clean == 1 && $quiet == 0) {
2814 print "$vname has no obvious style problems and is ready for submission.\n"
2816 if ($clean == 0 && $quiet == 0) {
2817 print "$vname has style problems, please review. If any of these errors\n";
2818 print "are false positives report them to the maintainer, see\n";
2819 print "CHECKPATCH in MAINTAINERS.\n";