2 # (c) 2001, Dave Jones. <davej@redhat.com> (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,2009, 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}';";
113 if (!top_of_kernel_tree($root)) {
114 die "$P: $root: --root does not point at a valid tree\n";
117 if (top_of_kernel_tree('.')) {
119 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
120 top_of_kernel_tree($1)) {
125 if (!defined $root) {
126 print "Must be run from the top-level dir. of a kernel tree\n";
131 my $emitted_corrupt = 0;
133 our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
134 our $Storage = qr{extern|static|asmlinkage};
149 __(?:mem|cpu|dev|)(?:initdata|init)|
150 ____cacheline_aligned|
151 ____cacheline_aligned_in_smp|
152 ____cacheline_internodealigned_in_smp|
156 our $Inline = qr{inline|__always_inline|noinline};
157 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
158 our $Lval = qr{$Ident(?:$Member)*};
160 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
161 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
162 our $Compare = qr{<=|>=|==|!=|<|>};
166 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
174 [\x09\x0A\x0D\x20-\x7E] # ASCII
175 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
176 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
177 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
178 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
179 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
180 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
181 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
184 our $typeTypedefs = qr{(?x:
185 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
191 qr{(?:unsigned\s+)?char},
192 qr{(?:unsigned\s+)?short},
193 qr{(?:unsigned\s+)?int},
194 qr{(?:unsigned\s+)?long},
195 qr{(?:unsigned\s+)?long\s+int},
196 qr{(?:unsigned\s+)?long\s+long},
197 qr{(?:unsigned\s+)?long\s+long\s+int},
206 qr{${Ident}_handler},
207 qr{${Ident}_handler_fn},
209 our @modifierList = (
214 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
215 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
216 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
218 (?:$Modifier\s+|const\s+)*
220 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
224 (?:\s+$Modifier|\s+const)*
228 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
229 (?:\s+$Inline|\s+$Modifier)*
231 $Declare = qr{(?:$Storage\s+)?$Type};
235 $chk_signoff = 0 if ($file);
237 my @dep_includes = ();
238 my @dep_functions = ();
239 my $removal = "Documentation/feature-removal-schedule.txt";
240 if ($tree && -f "$root/$removal") {
241 open(my $REMOVE, '<', "$root/$removal") ||
242 die "$P: $removal: open failed - $!\n";
244 if (/^Check:\s+(.*\S)/) {
245 for my $entry (split(/[, ]+/, $1)) {
246 if ($entry =~ m@include/(.*)@) {
247 push(@dep_includes, $1);
249 } elsif ($entry !~ m@/@) {
250 push(@dep_functions, $entry);
261 for my $filename (@ARGV) {
264 open($FILE, '-|', "diff -u /dev/null $filename") ||
265 die "$P: $filename: diff failed - $!\n";
266 } elsif ($filename eq '-') {
267 open($FILE, '<&STDIN');
269 open($FILE, '<', "$filename") ||
270 die "$P: $filename: open failed - $!\n";
272 if ($filename eq '-') {
273 $vname = 'Your patch';
282 if (!process($filename)) {
291 sub top_of_kernel_tree {
295 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
296 "README", "Documentation", "arch", "include", "drivers",
297 "fs", "init", "ipc", "kernel", "lib", "scripts",
300 foreach my $check (@tree_check) {
301 if (! -e $root . '/' . $check) {
313 for my $c (split(//, $str)) {
317 for (; ($n % 8) != 0; $n++) {
329 (my $res = shift) =~ tr/\t/ /c;
336 # Drop the diff line leader and expand tabs
338 $line = expand_tabs($line);
340 # Pick the indent from the front of the line.
341 my ($white) = ($line =~ /^(\s*)/);
343 return (length($line), length($white));
346 my $sanitise_quote = '';
348 sub sanitise_line_reset {
349 my ($in_comment) = @_;
352 $sanitise_quote = '*/';
354 $sanitise_quote = '';
367 # Always copy over the diff marker.
368 $res = substr($line, 0, 1);
370 for ($off = 1; $off < length($line); $off++) {
371 $c = substr($line, $off, 1);
373 # Comments we are wacking completly including the begin
374 # and end, all to $;.
375 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
376 $sanitise_quote = '*/';
378 substr($res, $off, 2, "$;$;");
382 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
383 $sanitise_quote = '';
384 substr($res, $off, 2, "$;$;");
388 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
389 $sanitise_quote = '//';
391 substr($res, $off, 2, $sanitise_quote);
396 # A \ in a string means ignore the next character.
397 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
399 substr($res, $off, 2, 'XX');
404 if ($c eq "'" || $c eq '"') {
405 if ($sanitise_quote eq '') {
406 $sanitise_quote = $c;
408 substr($res, $off, 1, $c);
410 } elsif ($sanitise_quote eq $c) {
411 $sanitise_quote = '';
415 #print "c<$c> SQ<$sanitise_quote>\n";
416 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
417 substr($res, $off, 1, $;);
418 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
419 substr($res, $off, 1, $;);
420 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
421 substr($res, $off, 1, 'X');
423 substr($res, $off, 1, $c);
427 if ($sanitise_quote eq '//') {
428 $sanitise_quote = '';
431 # The pathname on a #include may be surrounded by '<' and '>'.
432 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
433 my $clean = 'X' x length($1);
434 $res =~ s@\<.*\>@<$clean>@;
436 # The whole of a #error is a string.
437 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
438 my $clean = 'X' x length($1);
439 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
445 sub ctx_statement_block {
446 my ($linenr, $remain, $off) = @_;
447 my $line = $linenr - 1;
464 @stack = (['', 0]) if ($#stack == -1);
466 #warn "CSB: blk<$blk> remain<$remain>\n";
467 # If we are about to drop off the end, pull in more
470 for (; $remain > 0; $line++) {
471 last if (!defined $lines[$line]);
472 next if ($lines[$line] =~ /^-/);
475 $blk .= $lines[$line] . "\n";
480 # Bail if there is no further context.
481 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
487 $c = substr($blk, $off, 1);
488 $remainder = substr($blk, $off);
490 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
492 # Handle nested #if/#else.
493 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
494 push(@stack, [ $type, $level ]);
495 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
496 ($type, $level) = @{$stack[$#stack - 1]};
497 } elsif ($remainder =~ /^#\s*endif\b/) {
498 ($type, $level) = @{pop(@stack)};
501 # Statement ends at the ';' or a close '}' at the
503 if ($level == 0 && $c eq ';') {
507 # An else is really a conditional as long as its not else if
508 if ($level == 0 && $coff_set == 0 &&
509 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
510 $remainder =~ /^(else)(?:\s|{)/ &&
511 $remainder !~ /^else\s+if\b/) {
512 $coff = $off + length($1) - 1;
514 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
515 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
518 if (($type eq '' || $type eq '(') && $c eq '(') {
522 if ($type eq '(' && $c eq ')') {
524 $type = ($level != 0)? '(' : '';
526 if ($level == 0 && $coff < $soff) {
529 #warn "CSB: mark coff<$coff>\n";
532 if (($type eq '' || $type eq '{') && $c eq '{') {
536 if ($type eq '{' && $c eq '}') {
538 $type = ($level != 0)? '{' : '';
546 # We are truly at the end, so shuffle to the next line.
553 my $statement = substr($blk, $soff, $off - $soff + 1);
554 my $condition = substr($blk, $soff, $coff - $soff + 1);
556 #warn "STATEMENT<$statement>\n";
557 #warn "CONDITION<$condition>\n";
559 #print "coff<$coff> soff<$off> loff<$loff>\n";
561 return ($statement, $condition,
562 $line, $remain + 1, $off - $loff + 1, $level);
565 sub statement_lines {
568 # Strip the diff line prefixes and rip blank lines at start and end.
569 $stmt =~ s/(^|\n)./$1/g;
573 my @stmt_lines = ($stmt =~ /\n/g);
575 return $#stmt_lines + 2;
578 sub statement_rawlines {
581 my @stmt_lines = ($stmt =~ /\n/g);
583 return $#stmt_lines + 2;
586 sub statement_block_size {
589 $stmt =~ s/(^|\n)./$1/g;
595 my @stmt_lines = ($stmt =~ /\n/g);
596 my @stmt_statements = ($stmt =~ /;/g);
598 my $stmt_lines = $#stmt_lines + 2;
599 my $stmt_statements = $#stmt_statements + 1;
601 if ($stmt_lines > $stmt_statements) {
604 return $stmt_statements;
608 sub ctx_statement_full {
609 my ($linenr, $remain, $off) = @_;
610 my ($statement, $condition, $level);
614 # Grab the first conditional/block pair.
615 ($statement, $condition, $linenr, $remain, $off, $level) =
616 ctx_statement_block($linenr, $remain, $off);
617 #print "F: c<$condition> s<$statement> remain<$remain>\n";
618 push(@chunks, [ $condition, $statement ]);
619 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
620 return ($level, $linenr, @chunks);
623 # Pull in the following conditional/block pairs and see if they
624 # could continue the statement.
626 ($statement, $condition, $linenr, $remain, $off, $level) =
627 ctx_statement_block($linenr, $remain, $off);
628 #print "C: c<$condition> s<$statement> remain<$remain>\n";
629 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
631 push(@chunks, [ $condition, $statement ]);
634 return ($level, $linenr, @chunks);
638 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
640 my $start = $linenr - 1;
647 my @stack = ($level);
648 for ($line = $start; $remain > 0; $line++) {
649 next if ($rawlines[$line] =~ /^-/);
652 $blk .= $rawlines[$line];
654 # Handle nested #if/#else.
655 if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
656 push(@stack, $level);
657 } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
658 $level = $stack[$#stack - 1];
659 } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
660 $level = pop(@stack);
663 foreach my $c (split(//, $rawlines[$line])) {
664 ##print "C<$c>L<$level><$open$close>O<$off>\n";
670 if ($c eq $close && $level > 0) {
672 last if ($level == 0);
673 } elsif ($c eq $open) {
678 if (!$outer || $level <= 1) {
679 push(@res, $rawlines[$line]);
682 last if ($level == 0);
685 return ($level, @res);
687 sub ctx_block_outer {
688 my ($linenr, $remain) = @_;
690 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
694 my ($linenr, $remain) = @_;
696 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
700 my ($linenr, $remain, $off) = @_;
702 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
705 sub ctx_block_level {
706 my ($linenr, $remain) = @_;
708 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
710 sub ctx_statement_level {
711 my ($linenr, $remain, $off) = @_;
713 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
716 sub ctx_locate_comment {
717 my ($first_line, $end_line) = @_;
719 # Catch a comment on the end of the line itself.
720 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
721 return $current_comment if (defined $current_comment);
723 # Look through the context and try and figure out if there is a
726 $current_comment = '';
727 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
728 my $line = $rawlines[$linenr - 1];
730 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
733 if ($line =~ m@/\*@) {
736 if (!$in_comment && $current_comment ne '') {
737 $current_comment = '';
739 $current_comment .= $line . "\n" if ($in_comment);
740 if ($line =~ m@\*/@) {
745 chomp($current_comment);
746 return($current_comment);
748 sub ctx_has_comment {
749 my ($first_line, $end_line) = @_;
750 my $cmt = ctx_locate_comment($first_line, $end_line);
752 ##print "LINE: $rawlines[$end_line - 1 ]\n";
753 ##print "CMMT: $cmt\n";
759 my ($linenr, $cnt) = @_;
761 my $offset = $linenr - 1;
766 $line = $rawlines[$offset++];
767 next if (defined($line) && $line =~ /^-/);
779 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
782 $coded = sprintf("^%c", unpack('C', $2) + 64);
791 my $av_preprocessor = 0;
797 $av_preprocessor = 0;
799 @av_paren_type = ('E');
800 $av_pend_colon = 'O';
803 sub annotate_values {
804 my ($stream, $type) = @_;
807 my $var = '_' x length($stream);
810 print "$stream\n" if ($dbg_values > 1);
812 while (length($cur)) {
813 @av_paren_type = ('E') if ($#av_paren_type < 0);
814 print " <" . join('', @av_paren_type) .
815 "> <$type> <$av_pending>" if ($dbg_values > 1);
816 if ($cur =~ /^(\s+)/o) {
817 print "WS($1)\n" if ($dbg_values > 1);
818 if ($1 =~ /\n/ && $av_preprocessor) {
819 $type = pop(@av_paren_type);
820 $av_preprocessor = 0;
823 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
824 print "DECLARE($1)\n" if ($dbg_values > 1);
827 } elsif ($cur =~ /^($Modifier)\s*/) {
828 print "MODIFIER($1)\n" if ($dbg_values > 1);
831 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
832 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
833 $av_preprocessor = 1;
834 push(@av_paren_type, $type);
840 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
841 print "UNDEF($1)\n" if ($dbg_values > 1);
842 $av_preprocessor = 1;
843 push(@av_paren_type, $type);
845 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
846 print "PRE_START($1)\n" if ($dbg_values > 1);
847 $av_preprocessor = 1;
849 push(@av_paren_type, $type);
850 push(@av_paren_type, $type);
853 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
854 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
855 $av_preprocessor = 1;
857 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
861 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
862 print "PRE_END($1)\n" if ($dbg_values > 1);
864 $av_preprocessor = 1;
866 # Assume all arms of the conditional end as this
867 # one does, and continue as if the #endif was not here.
869 push(@av_paren_type, $type);
872 } elsif ($cur =~ /^(\\\n)/o) {
873 print "PRECONT($1)\n" if ($dbg_values > 1);
875 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
876 print "ATTR($1)\n" if ($dbg_values > 1);
880 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
881 print "SIZEOF($1)\n" if ($dbg_values > 1);
887 } elsif ($cur =~ /^(if|while|for)\b/o) {
888 print "COND($1)\n" if ($dbg_values > 1);
892 } elsif ($cur =~/^(case)/o) {
893 print "CASE($1)\n" if ($dbg_values > 1);
894 $av_pend_colon = 'C';
897 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
898 print "KEYWORD($1)\n" if ($dbg_values > 1);
901 } elsif ($cur =~ /^(\()/o) {
902 print "PAREN('$1')\n" if ($dbg_values > 1);
903 push(@av_paren_type, $av_pending);
907 } elsif ($cur =~ /^(\))/o) {
908 my $new_type = pop(@av_paren_type);
909 if ($new_type ne '_') {
911 print "PAREN('$1') -> $type\n"
912 if ($dbg_values > 1);
914 print "PAREN('$1')\n" if ($dbg_values > 1);
917 } elsif ($cur =~ /^($Ident)\s*\(/o) {
918 print "FUNC($1)\n" if ($dbg_values > 1);
922 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
923 if (defined $2 && $type eq 'C' || $type eq 'T') {
924 $av_pend_colon = 'B';
925 } elsif ($type eq 'E') {
926 $av_pend_colon = 'L';
928 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
931 } elsif ($cur =~ /^($Ident|$Constant)/o) {
932 print "IDENT($1)\n" if ($dbg_values > 1);
935 } elsif ($cur =~ /^($Assignment)/o) {
936 print "ASSIGN($1)\n" if ($dbg_values > 1);
939 } elsif ($cur =~/^(;|{|})/) {
940 print "END($1)\n" if ($dbg_values > 1);
942 $av_pend_colon = 'O';
944 } elsif ($cur =~/^(,)/) {
945 print "COMMA($1)\n" if ($dbg_values > 1);
948 } elsif ($cur =~ /^(\?)/o) {
949 print "QUESTION($1)\n" if ($dbg_values > 1);
952 } elsif ($cur =~ /^(:)/o) {
953 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
955 substr($var, length($res), 1, $av_pend_colon);
956 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
961 $av_pend_colon = 'O';
963 } elsif ($cur =~ /^(\[)/o) {
964 print "CLOSE($1)\n" if ($dbg_values > 1);
967 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
970 print "OPV($1)\n" if ($dbg_values > 1);
977 substr($var, length($res), 1, $variant);
980 } elsif ($cur =~ /^($Operators)/o) {
981 print "OP($1)\n" if ($dbg_values > 1);
982 if ($1 ne '++' && $1 ne '--') {
986 } elsif ($cur =~ /(^.)/o) {
987 print "C($1)\n" if ($dbg_values > 1);
990 $cur = substr($cur, length($1));
991 $res .= $type x length($1);
999 my ($possible, $line) = @_;
1000 my $notPermitted = qr{(?:
1015 ^(?:typedef|struct|enum)\b
1017 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1018 if ($possible !~ $notPermitted) {
1019 # Check for modifiers.
1020 $possible =~ s/\s*$Storage\s*//g;
1021 $possible =~ s/\s*$Sparse\s*//g;
1022 if ($possible =~ /^\s*$/) {
1024 } elsif ($possible =~ /\s/) {
1025 $possible =~ s/\s*$Type\s*//g;
1026 for my $modifier (split(' ', $possible)) {
1027 if ($modifier !~ $notPermitted) {
1028 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1029 push(@modifierList, $modifier);
1034 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1035 push(@typeList, $possible);
1039 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1046 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1049 my $line = $prefix . $_[0];
1051 $line = (split('\n', $line))[0] . "\n" if ($terse);
1053 push(our @report, $line);
1061 if (report("ERROR: $_[0]\n")) {
1067 if (report("WARNING: $_[0]\n")) {
1073 if ($check && report("CHECK: $_[0]\n")) {
1079 sub check_absolute_file {
1080 my ($absolute, $herecurr) = @_;
1081 my $file = $absolute;
1083 ##print "absolute<$absolute>\n";
1085 # See if any suffix of this path is a path within the tree.
1086 while ($file =~ s@^[^/]*/@@) {
1087 if (-f "$root/$file") {
1088 ##print "file<$file>\n";
1096 # It is, so see if the prefix is acceptable.
1097 my $prefix = $absolute;
1098 substr($prefix, -length($file)) = '';
1100 ##print "prefix<$prefix>\n";
1101 if ($prefix ne ".../") {
1102 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1107 my $filename = shift;
1113 my $stashrawline="";
1130 # Trace the real file/line as we go.
1136 my $comment_edge = 0;
1140 my $prev_values = 'E';
1143 my %suppress_ifbraces;
1144 my %suppress_whiletrailers;
1146 # Pre-scan the patch sanitizing the lines.
1147 # Pre-scan the patch looking for any __setup documentation.
1149 my @setup_docs = ();
1152 sanitise_line_reset();
1154 foreach my $rawline (@rawlines) {
1158 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1160 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1165 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1174 # Guestimate if this is a continuing comment. Run
1175 # the context looking for a comment "edge". If this
1176 # edge is a close comment then we must be in a comment
1180 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1181 next if (defined $rawlines[$ln - 1] &&
1182 $rawlines[$ln - 1] =~ /^-/);
1184 #print "RAW<$rawlines[$ln - 1]>\n";
1185 last if (!defined $rawlines[$ln - 1]);
1186 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1187 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1192 if (defined $edge && $edge eq '*/') {
1196 # Guestimate if this is a continuing comment. If this
1197 # is the start of a diff block and this line starts
1198 # ' *' then it is very likely a comment.
1199 if (!defined $edge &&
1200 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1205 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1206 sanitise_line_reset($in_comment);
1208 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1209 # Standardise the strings and chars within the input to
1210 # simplify matching -- only bother with positive lines.
1211 $line = sanitise_line($rawline);
1213 push(@lines, $line);
1216 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1221 #print "==>$rawline\n";
1222 #print "-->$line\n";
1224 if ($setup_docs && $line =~ /^\+/) {
1225 push(@setup_docs, $line);
1233 foreach my $line (@lines) {
1236 my $rawline = $rawlines[$linenr - 1];
1238 #extract the line range in the file after the patch is applied
1239 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1241 $first_line = $linenr + 1;
1251 %suppress_ifbraces = ();
1252 %suppress_whiletrailers = ();
1255 # track the line number as we move through the hunk, note that
1256 # new versions of GNU diff omit the leading space on completely
1257 # blank context lines so we need to count that too.
1258 } elsif ($line =~ /^( |\+|$)/) {
1260 $realcnt-- if ($realcnt != 0);
1262 # Measure the line length and indent.
1263 ($length, $indent) = line_stats($rawline);
1265 # Track the previous line.
1266 ($prevline, $stashline) = ($stashline, $line);
1267 ($previndent, $stashindent) = ($stashindent, $indent);
1268 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1270 #warn "line<$line>\n";
1272 } elsif ($realcnt == 1) {
1276 my $hunk_line = ($realcnt != 0);
1278 #make up the handle for any error we report on this line
1279 $prefix = "$filename:$realline: " if ($emacs && $file);
1280 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1282 $here = "#$linenr: " if (!$file);
1283 $here = "#$realline: " if ($file);
1285 # extract the filename as it passes
1286 if ($line=~/^\+\+\+\s+(\S+)/) {
1288 $realfile =~ s@^([^/]*)/@@;
1291 if (!$file && $tree && $p1_prefix ne '' &&
1292 -e "$root/$p1_prefix") {
1293 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1296 if ($realfile =~ m@^include/asm/@) {
1297 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1302 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1304 my $hereline = "$here\n$rawline\n";
1305 my $herecurr = "$here\n$rawline\n";
1306 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1308 $cnt_lines++ if ($realcnt != 0);
1310 #check the patch for a signoff:
1311 if ($line =~ /^\s*signed-off-by:/i) {
1312 # This is a signoff, if ugly, so do not double report.
1314 if (!($line =~ /^\s*Signed-off-by:/)) {
1315 WARN("Signed-off-by: is the preferred form\n" .
1318 if ($line =~ /^\s*signed-off-by:\S/i) {
1319 WARN("space required after Signed-off-by:\n" .
1324 # Check for wrappage within a valid hunk of the file
1325 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1326 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1327 $herecurr) if (!$emitted_corrupt++);
1330 # Check for absolute kernel paths.
1332 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1335 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1336 check_absolute_file($1, $herecurr)) {
1339 check_absolute_file($file, $herecurr);
1344 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1345 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1346 $rawline !~ m/^$UTF8*$/) {
1347 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1349 my $blank = copy_spacing($rawline);
1350 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1351 my $hereptr = "$hereline$ptr\n";
1353 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1356 # ignore non-hunk lines and lines being removed
1357 next if (!$hunk_line || $line =~ /^-/);
1359 #trailing whitespace
1360 if ($line =~ /^\+.*\015/) {
1361 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1362 ERROR("DOS line endings\n" . $herevet);
1364 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1365 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1366 ERROR("trailing whitespace\n" . $herevet);
1369 # check we are in a valid source file if not then ignore this hunk
1370 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1373 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1374 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1375 $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1378 WARN("line over 80 characters\n" . $herecurr);
1381 # check for adding lines without a newline.
1382 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1383 WARN("adding a line without newline at end of file\n" . $herecurr);
1386 # Blackfin: use hi/lo macros
1387 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1388 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1389 my $herevet = "$here\n" . cat_vet($line) . "\n";
1390 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1392 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1393 my $herevet = "$here\n" . cat_vet($line) . "\n";
1394 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1398 # check we are in a valid source file C or perl if not then ignore this hunk
1399 next if ($realfile !~ /\.(h|c|pl)$/);
1401 # at the beginning of a line any tabs must come first and anything
1402 # more than 8 must use tabs.
1403 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1404 $rawline =~ /^\+\s* \s*/) {
1405 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1406 ERROR("code indent should use tabs where possible\n" . $herevet);
1409 # check we are in a valid C source file if not then ignore this hunk
1410 next if ($realfile !~ /\.(h|c)$/);
1412 # check for RCS/CVS revision markers
1413 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1414 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1417 # Blackfin: don't use __builtin_bfin_[cs]sync
1418 if ($line =~ /__builtin_bfin_csync/) {
1419 my $herevet = "$here\n" . cat_vet($line) . "\n";
1420 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1422 if ($line =~ /__builtin_bfin_ssync/) {
1423 my $herevet = "$here\n" . cat_vet($line) . "\n";
1424 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1427 # Check for potential 'bare' types
1428 my ($stat, $cond, $line_nr_next, $remain_next, $off_next);
1429 if ($realcnt && $line =~ /.\s*\S/) {
1430 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1431 ctx_statement_block($linenr, $realcnt, 0);
1432 $stat =~ s/\n./\n /g;
1433 $cond =~ s/\n./\n /g;
1438 # Ignore goto labels.
1439 if ($s =~ /$Ident:\*$/s) {
1441 # Ignore functions being called
1442 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1444 } elsif ($s =~ /^.\s*else\b/s) {
1446 # declarations always start with types
1447 } 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) {
1450 possible($type, "A:" . $s);
1452 # definitions in global scope can only start with types
1453 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1454 possible($1, "B:" . $s);
1457 # any (foo ... *) is a pointer cast, and foo is a type
1458 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1459 possible($1, "C:" . $s);
1462 # Check for any sort of function declaration.
1463 # int foo(something bar, other baz);
1464 # void (*store_gdt)(x86_descr_ptr *);
1465 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1466 my ($name_len) = length($1);
1469 substr($ctx, 0, $name_len + 1, '');
1470 $ctx =~ s/\)[^\)]*$//;
1472 for my $arg (split(/\s*,\s*/, $ctx)) {
1473 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1475 possible($1, "D:" . $s);
1483 # Checks which may be anchored in the context.
1486 # Check for switch () and associated case and default
1487 # statements should be at the same indent.
1488 if ($line=~/\bswitch\s*\(.*\)/) {
1491 my @ctx = ctx_block_outer($linenr, $realcnt);
1493 for my $ctx (@ctx) {
1494 my ($clen, $cindent) = line_stats($ctx);
1495 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1496 $indent != $cindent) {
1497 $err .= "$sep$ctx\n";
1504 ERROR("switch and case should be at the same indent\n$hereline$err");
1508 # if/while/etc brace do not go on next line, unless defining a do while loop,
1509 # or if that brace on the next line is for something else
1510 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1511 my $pre_ctx = "$1$2";
1513 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1514 my $ctx_cnt = $realcnt - $#ctx - 1;
1515 my $ctx = join("\n", @ctx);
1517 my $ctx_ln = $linenr;
1518 my $ctx_skip = $realcnt;
1520 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1521 defined $lines[$ctx_ln - 1] &&
1522 $lines[$ctx_ln - 1] =~ /^-/)) {
1523 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1524 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1528 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1529 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1531 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1532 ERROR("that open brace { should be on the previous line\n" .
1533 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
1535 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1536 $ctx =~ /\)\s*\;\s*$/ &&
1537 defined $lines[$ctx_ln - 1])
1539 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1540 if ($nindent > $indent) {
1541 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1542 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
1547 # Check relative indent for conditionals and blocks.
1548 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1549 my ($s, $c) = ($stat, $cond);
1551 substr($s, 0, length($c), '');
1553 # Make sure we remove the line prefixes as we have
1554 # none on the first line, and are going to readd them
1558 # Find out how long the conditional actually is.
1559 my @newlines = ($c =~ /\n/gs);
1560 my $cond_lines = 1 + $#newlines;
1562 # We want to check the first line inside the block
1563 # starting at the end of the conditional, so remove:
1564 # 1) any blank line termination
1565 # 2) any opening brace { on end of the line
1567 my $continuation = 0;
1569 $s =~ s/^.*\bdo\b//;
1571 if ($s =~ s/^\s*\\//) {
1574 if ($s =~ s/^\s*?\n//) {
1579 # Also ignore a loop construct at the end of a
1580 # preprocessor statement.
1581 if (($prevline =~ /^.\s*#\s*define\s/ ||
1582 $prevline =~ /\\\s*$/) && $continuation == 0) {
1588 while ($cond_ptr != $cond_lines) {
1589 $cond_ptr = $cond_lines;
1591 # If we see an #else/#elif then the code
1593 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1598 # 1) blank lines, they should be at 0,
1599 # 2) preprocessor lines, and
1601 if ($continuation ||
1603 $s =~ /^\s*#\s*?/ ||
1604 $s =~ /^\s*$Ident\s*:/) {
1605 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1606 if ($s =~ s/^.*?\n//) {
1612 my (undef, $sindent) = line_stats("+" . $s);
1613 my $stat_real = raw_line($linenr, $cond_lines);
1615 # Check if either of these lines are modified, else
1616 # this is not this patch's fault.
1617 if (!defined($stat_real) ||
1618 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1621 if (defined($stat_real) && $cond_lines > 1) {
1622 $stat_real = "[...]\n$stat_real";
1625 #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";
1627 if ($check && (($sindent % 8) != 0 ||
1628 ($sindent <= $indent && $s ne ''))) {
1629 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1633 # Track the 'values' across context and added lines.
1634 my $opline = $line; $opline =~ s/^./ /;
1635 my ($curr_values, $curr_vars) =
1636 annotate_values($opline . "\n", $prev_values);
1637 $curr_values = $prev_values . $curr_values;
1639 my $outline = $opline; $outline =~ s/\t/ /g;
1640 print "$linenr > .$outline\n";
1641 print "$linenr > $curr_values\n";
1642 print "$linenr > $curr_vars\n";
1644 $prev_values = substr($curr_values, -1);
1646 #ignore lines not being added
1647 if ($line=~/^[^\+]/) {next;}
1649 # TEST: allow direct testing of the type matcher.
1651 if ($line =~ /^.\s*$Declare\s*$/) {
1652 ERROR("TEST: is type\n" . $herecurr);
1653 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1654 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1658 # TEST: allow direct testing of the attribute matcher.
1660 if ($line =~ /^.\s*$Modifier\s*$/) {
1661 ERROR("TEST: is attr\n" . $herecurr);
1662 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1663 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1668 # check for initialisation to aggregates open brace on the next line
1669 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1670 $line =~ /^.\s*{/) {
1671 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1675 # Checks which are anchored on the added line.
1678 # check for malformed paths in #include statements (uses RAW line)
1679 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1681 if ($path =~ m{//}) {
1682 ERROR("malformed #include filename\n" .
1687 # no C99 // comments
1688 if ($line =~ m{//}) {
1689 ERROR("do not use C99 // comments\n" . $herecurr);
1691 # Remove C99 comments.
1693 $opline =~ s@//.*@@;
1695 #EXPORT_SYMBOL should immediately follow its function closing }.
1696 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1697 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1699 if ($prevline !~ /(?:
1701 ^.DEFINE_$Ident\(\Q$name\E\)|
1702 ^.DECLARE_$Ident\(\Q$name\E\)|
1703 ^.LIST_HEAD\(\Q$name\E\)|
1704 ^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1705 \b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)
1707 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1711 # check for external initialisers.
1712 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1713 ERROR("do not initialise externals to 0 or NULL\n" .
1716 # check for static initialisers.
1717 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1718 ERROR("do not initialise statics to 0 or NULL\n" .
1722 # check for new typedefs, only function parameters and sparse annotations
1724 if ($line =~ /\btypedef\s/ &&
1725 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1726 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
1727 $line !~ /\b$typeTypedefs\b/ &&
1728 $line !~ /\b__bitwise(?:__|)\b/) {
1729 WARN("do not add new typedefs\n" . $herecurr);
1732 # * goes on variable not on type
1734 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1735 my ($from, $to) = ($1, $1);
1737 # Should start with a space.
1738 $to =~ s/^(\S)/ $1/;
1739 # Should not end with a space.
1741 # '*'s should not have spaces between.
1742 while ($to =~ s/\*\s+\*/\*\*/) {
1745 #print "from<$from> to<$to>\n";
1747 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1749 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1750 my ($from, $to, $ident) = ($1, $1, $2);
1752 # Should start with a space.
1753 $to =~ s/^(\S)/ $1/;
1754 # Should not end with a space.
1756 # '*'s should not have spaces between.
1757 while ($to =~ s/\*\s+\*/\*\*/) {
1759 # Modifiers should have spaces.
1760 $to =~ s/(\b$Modifier$)/$1 /;
1762 #print "from<$from> to<$to> ident<$ident>\n";
1763 if ($from ne $to && $ident !~ /^$Modifier$/) {
1764 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1768 # # no BUG() or BUG_ON()
1769 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1770 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1771 # print "$herecurr";
1775 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1776 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1779 # printk should use KERN_* levels. Note that follow on printk's on the
1780 # same line do not need a level, so we use the current block context
1781 # to try and find and validate the current printk. In summary the current
1782 # printk includes all preceeding printk's which have no newline on the end.
1783 # we assume the first bad printk is the one to report.
1784 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1786 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1787 #print "CHECK<$lines[$ln - 1]\n";
1788 # we have a preceeding printk if it ends
1789 # with "\n" ignore it, else it is to blame
1790 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1791 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1798 WARN("printk() should include KERN_ facility level\n" . $herecurr);
1802 # function brace can't be on same line, except for #defines of do while,
1803 # or if closed on same line
1804 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1805 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1806 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1809 # open braces for enum, union and struct go on the same line.
1810 if ($line =~ /^.\s*{/ &&
1811 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1812 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1815 # check for spacing round square brackets; allowed:
1816 # 1. with a type on the left -- int [] a;
1817 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1818 # 3. inside a curly brace -- = { [0...10] = 5 }
1819 while ($line =~ /(.*?\s)\[/g) {
1820 my ($where, $prefix) = ($-[1], $1);
1821 if ($prefix !~ /$Type\s+$/ &&
1822 ($where != 0 || $prefix !~ /^.\s+$/) &&
1823 $prefix !~ /{\s+$/) {
1824 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1828 # check for spaces between functions and their parentheses.
1829 while ($line =~ /($Ident)\s+\(/g) {
1831 my $ctx_before = substr($line, 0, $-[1]);
1832 my $ctx = "$ctx_before$name";
1834 # Ignore those directives where spaces _are_ permitted.
1836 if|for|while|switch|return|case|
1837 volatile|__volatile__|
1838 __attribute__|format|__extension__|
1842 # cpp #define statements have non-optional spaces, ie
1843 # if there is a space between the name and the open
1844 # parenthesis it is simply not a parameter group.
1845 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1847 # cpp #elif statement condition may start with a (
1848 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1850 # If this whole things ends with a type its most
1851 # likely a typedef for a function.
1852 } elsif ($ctx =~ /$Type$/) {
1855 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1858 # Check operator spacing.
1859 if (!($line=~/\#\s*include/)) {
1861 <<=|>>=|<=|>=|==|!=|
1862 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1863 =>|->|<<|>>|<|>|=|!|~|
1864 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1867 my @elements = split(/($ops|;)/, $opline);
1870 my $blank = copy_spacing($opline);
1872 for (my $n = 0; $n < $#elements; $n += 2) {
1873 $off += length($elements[$n]);
1875 # Pick up the preceeding and succeeding characters.
1876 my $ca = substr($opline, 0, $off);
1878 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1879 $cc = substr($opline, $off + length($elements[$n + 1]));
1881 my $cb = "$ca$;$cc";
1884 $a = 'V' if ($elements[$n] ne '');
1885 $a = 'W' if ($elements[$n] =~ /\s$/);
1886 $a = 'C' if ($elements[$n] =~ /$;$/);
1887 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1888 $a = 'O' if ($elements[$n] eq '');
1889 $a = 'E' if ($ca =~ /^\s*$/);
1891 my $op = $elements[$n + 1];
1894 if (defined $elements[$n + 2]) {
1895 $c = 'V' if ($elements[$n + 2] ne '');
1896 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1897 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1898 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1899 $c = 'O' if ($elements[$n + 2] eq '');
1900 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
1905 my $ctx = "${a}x${c}";
1907 my $at = "(ctx:$ctx)";
1909 my $ptr = substr($blank, 0, $off) . "^";
1910 my $hereptr = "$hereline$ptr\n";
1912 # Pull out the value of this operator.
1913 my $op_type = substr($curr_values, $off + 1, 1);
1915 # Get the full operator variant.
1916 my $opv = $op . substr($curr_vars, $off, 1);
1918 # Ignore operators passed as parameters.
1919 if ($op_type ne 'V' &&
1920 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1923 # } elsif ($op =~ /^$;+$/) {
1925 # ; should have either the end of line or a space or \ after it
1926 } elsif ($op eq ';') {
1927 if ($ctx !~ /.x[WEBC]/ &&
1928 $cc !~ /^\\/ && $cc !~ /^;/) {
1929 ERROR("space required after that '$op' $at\n" . $hereptr);
1933 } elsif ($op eq '//') {
1937 # : when part of a bitfield
1938 } elsif ($op eq '->' || $opv eq ':B') {
1939 if ($ctx =~ /Wx.|.xW/) {
1940 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
1943 # , must have a space on the right.
1944 } elsif ($op eq ',') {
1945 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1946 ERROR("space required after that '$op' $at\n" . $hereptr);
1949 # '*' as part of a type definition -- reported already.
1950 } elsif ($opv eq '*_') {
1951 #warn "'*' is part of type\n";
1953 # unary operators should have a space before and
1954 # none after. May be left adjacent to another
1955 # unary operator, or a cast
1956 } elsif ($op eq '!' || $op eq '~' ||
1957 $opv eq '*U' || $opv eq '-U' ||
1958 $opv eq '&U' || $opv eq '&&U') {
1959 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1960 ERROR("space required before that '$op' $at\n" . $hereptr);
1962 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
1963 # A unary '*' may be const
1965 } elsif ($ctx =~ /.xW/) {
1966 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1969 # unary ++ and unary -- are allowed no space on one side.
1970 } elsif ($op eq '++' or $op eq '--') {
1971 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1972 ERROR("space required one side of that '$op' $at\n" . $hereptr);
1974 if ($ctx =~ /Wx[BE]/ ||
1975 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1976 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1978 if ($ctx =~ /ExW/) {
1979 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1983 # << and >> may either have or not have spaces both sides
1984 } elsif ($op eq '<<' or $op eq '>>' or
1985 $op eq '&' or $op eq '^' or $op eq '|' or
1986 $op eq '+' or $op eq '-' or
1987 $op eq '*' or $op eq '/' or
1990 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1991 ERROR("need consistent spacing around '$op' $at\n" .
1995 # A colon needs no spaces before when it is
1996 # terminating a case value or a label.
1997 } elsif ($opv eq ':C' || $opv eq ':L') {
1998 if ($ctx =~ /Wx./) {
1999 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2002 # All the others need spaces both sides.
2003 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2006 # Ignore email addresses <foo@bar>
2008 $cc =~ /^\S+\@\S+>/) ||
2010 $ca =~ /<\S+\@\S+$/))
2016 if (($opv eq ':O' && $ca =~ /\?$/) ||
2017 ($op eq '?' && $cc =~ /^:/)) {
2022 ERROR("spaces required around that '$op' $at\n" . $hereptr);
2025 $off += length($elements[$n + 1]);
2029 # check for multiple assignments
2030 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2031 CHK("multiple assignments should be avoided\n" . $herecurr);
2034 ## # check for multiple declarations, allowing for a function declaration
2036 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2037 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2039 ## # Remove any bracketed sections to ensure we do not
2040 ## # falsly report the parameters of functions.
2042 ## while ($ln =~ s/\([^\(\)]*\)//g) {
2044 ## if ($ln =~ /,/) {
2045 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2049 #need space before brace following if, while, etc
2050 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2052 ERROR("space required before the open brace '{'\n" . $herecurr);
2055 # closing brace should have a space following it when it has anything
2057 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2058 ERROR("space required after that close brace '}'\n" . $herecurr);
2061 # check spacing on square brackets
2062 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2063 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2065 if ($line =~ /\s\]/) {
2066 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2069 # check spacing on parentheses
2070 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2071 $line !~ /for\s*\(\s+;/) {
2072 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2074 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2075 $line !~ /for\s*\(.*;\s+\)/ &&
2076 $line !~ /:\s+\)/) {
2077 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2080 #goto labels aren't indented, allow a single space however
2081 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2082 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2083 WARN("labels should not be indented\n" . $herecurr);
2086 # Return is not a function.
2087 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2091 # Flatten any parentheses
2092 $value =~ s/\)\(/\) \(/g;
2093 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2094 $value !~ /(?:$Ident|-?$Constant)\s*
2096 (?:$Ident|-?$Constant)/x &&
2097 $value =~ s/\([^\(\)]*\)/1/) {
2100 if ($value =~ /^(?:$Ident|-?$Constant)$/) {
2101 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2103 } elsif ($spacing !~ /\s+/) {
2104 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2108 # Need a space before open parenthesis after if, while etc
2109 if ($line=~/\b(if|while|for|switch)\(/) {
2110 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2113 # Check for illegal assignment in if conditional -- and check for trailing
2114 # statements after the conditional.
2115 if ($line =~ /do\s*(?!{)/) {
2116 my ($stat_next) = ctx_statement_block($line_nr_next,
2117 $remain_next, $off_next);
2118 $stat_next =~ s/\n./\n /g;
2119 ##print "stat<$stat> stat_next<$stat_next>\n";
2121 if ($stat_next =~ /^\s*while\b/) {
2122 # If the statement carries leading newlines,
2123 # then count those as offsets.
2125 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2127 statement_rawlines($whitespace) - 1;
2129 $suppress_whiletrailers{$line_nr_next +
2133 if (!defined $suppress_whiletrailers{$linenr} &&
2134 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2135 my ($s, $c) = ($stat, $cond);
2137 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2138 ERROR("do not use assignment in if condition\n" . $herecurr);
2141 # Find out what is on the end of the line after the
2143 substr($s, 0, length($c), '');
2145 $s =~ s/$;//g; # Remove any comments
2146 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2147 $c !~ /}\s*while\s*/)
2149 # Find out how long the conditional actually is.
2150 my @newlines = ($c =~ /\n/gs);
2151 my $cond_lines = 1 + $#newlines;
2153 my $stat_real = raw_line($linenr, $cond_lines);
2154 if (defined($stat_real) && $cond_lines > 1) {
2155 $stat_real = "[...]\n$stat_real";
2158 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2162 # Check for bitwise tests written as boolean
2174 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2177 # if and else should not have general statements after it
2178 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2180 $s =~ s/$;//g; # Remove any comments
2181 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2182 ERROR("trailing statements should be on next line\n" . $herecurr);
2185 # if should not continue a brace
2186 if ($line =~ /}\s*if\b/) {
2187 ERROR("trailing statements should be on next line\n" .
2190 # case and default should not have general statements after them
2191 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2193 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2197 ERROR("trailing statements should be on next line\n" . $herecurr);
2200 # Check for }<nl>else {, these must be at the same
2201 # indent level to be relevant to each other.
2202 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2203 $previndent == $indent) {
2204 ERROR("else should follow close brace '}'\n" . $hereprev);
2207 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2208 $previndent == $indent) {
2209 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2211 # Find out what is on the end of the line after the
2213 substr($s, 0, length($c), '');
2216 if ($s =~ /^\s*;/) {
2217 ERROR("while should follow close brace '}'\n" . $hereprev);
2221 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2222 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2223 # print "No studly caps, use _\n";
2224 # print "$herecurr";
2228 #no spaces allowed after \ in define
2229 if ($line=~/\#\s*define.*\\\s$/) {
2230 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
2233 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2234 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2236 my $checkfile = "include/linux/$file";
2237 if (-f "$root/$checkfile" &&
2238 $realfile ne $checkfile &&
2241 if ($realfile =~ m{^arch/}) {
2242 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2244 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2249 # multi-statement macros should be enclosed in a do while loop, grab the
2250 # first statement and ensure its the whole macro if its not enclosed
2251 # in a known good container
2252 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2253 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2256 my ($off, $dstat, $dcond, $rest);
2259 my $args = defined($1);
2261 # Find the end of the macro and limit our statement
2263 while ($cnt > 0 && defined $lines[$ln - 1] &&
2264 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2266 $ctx .= $rawlines[$ln - 1] . "\n";
2267 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2270 $ctx .= $rawlines[$ln - 1];
2272 ($dstat, $dcond, $ln, $cnt, $off) =
2273 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2274 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2275 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2277 # Extract the remainder of the define (if any) and
2278 # rip off surrounding spaces, and trailing \'s.
2280 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2281 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2282 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2283 $rest .= substr($lines[$ln - 1], $off) . "\n";
2289 $rest =~ s/\\\n.//g;
2293 # Clean up the original statement.
2295 substr($dstat, 0, length($dcond), '');
2297 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2300 $dstat =~ s/\\\n.//g;
2301 $dstat =~ s/^\s*//s;
2302 $dstat =~ s/\s*$//s;
2304 # Flatten any parentheses and braces
2305 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2306 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2307 $dstat =~ s/\[[^\{\}]*\]/1/)
2311 my $exceptions = qr{
2321 #print "REST<$rest> dstat<$dstat>\n";
2323 if ($rest !~ /while\s*\(/ &&
2324 $dstat !~ /$exceptions/)
2326 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2329 } elsif ($ctx !~ /;/) {
2331 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2332 $dstat !~ /$exceptions/ &&
2333 $dstat !~ /^\.$Ident\s*=/ &&
2334 $dstat =~ /$Operators/)
2336 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2341 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2342 # all assignments may have only one of the following with an assignment:
2345 # VMLINUX_SYMBOL(...)
2346 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2347 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2350 # check for redundant bracing round if etc
2351 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2352 my ($level, $endln, @chunks) =
2353 ctx_statement_full($linenr, $realcnt, 1);
2354 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2355 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2356 if ($#chunks > 0 && $level == 0) {
2359 my $herectx = $here . "\n";
2360 my $ln = $linenr - 1;
2361 for my $chunk (@chunks) {
2362 my ($cond, $block) = @{$chunk};
2364 # If the condition carries leading newlines, then count those as offsets.
2365 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2366 my $offset = statement_rawlines($whitespace) - 1;
2368 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2370 # We have looked at and allowed this specific line.
2371 $suppress_ifbraces{$ln + $offset} = 1;
2373 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2374 $ln += statement_rawlines($block) - 1;
2376 substr($block, 0, length($cond), '');
2378 $seen++ if ($block =~ /^\s*{/);
2380 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2381 if (statement_lines($cond) > 1) {
2382 #print "APW: ALLOWED: cond<$cond>\n";
2385 if ($block =~/\b(?:if|for|while)\b/) {
2386 #print "APW: ALLOWED: block<$block>\n";
2389 if (statement_block_size($block) > 1) {
2390 #print "APW: ALLOWED: lines block<$block>\n";
2394 if ($seen && !$allowed) {
2395 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
2399 if (!defined $suppress_ifbraces{$linenr - 1} &&
2400 $line =~ /\b(if|while|for|else)\b/) {
2403 # Check the pre-context.
2404 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2405 #print "APW: ALLOWED: pre<$1>\n";
2409 my ($level, $endln, @chunks) =
2410 ctx_statement_full($linenr, $realcnt, $-[0]);
2412 # Check the condition.
2413 my ($cond, $block) = @{$chunks[0]};
2414 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2415 if (defined $cond) {
2416 substr($block, 0, length($cond), '');
2418 if (statement_lines($cond) > 1) {
2419 #print "APW: ALLOWED: cond<$cond>\n";
2422 if ($block =~/\b(?:if|for|while)\b/) {
2423 #print "APW: ALLOWED: block<$block>\n";
2426 if (statement_block_size($block) > 1) {
2427 #print "APW: ALLOWED: lines block<$block>\n";
2430 # Check the post-context.
2431 if (defined $chunks[1]) {
2432 my ($cond, $block) = @{$chunks[1]};
2433 if (defined $cond) {
2434 substr($block, 0, length($cond), '');
2436 if ($block =~ /^\s*\{/) {
2437 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2441 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2442 my $herectx = $here . "\n";;
2443 my $cnt = statement_rawlines($block);
2445 for (my $n = 0; $n < $cnt; $n++) {
2446 $herectx .= raw_line($linenr, $n) . "\n";;
2449 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2453 # don't include deprecated include files (uses RAW line)
2454 for my $inc (@dep_includes) {
2455 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2456 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2460 # don't use deprecated functions
2461 for my $func (@dep_functions) {
2462 if ($line =~ /\b$func\b/) {
2463 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2467 # no volatiles please
2468 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2469 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2470 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2473 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2474 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2475 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2479 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2480 CHK("if this code is redundant consider removing it\n" .
2484 # check for needless kfree() checks
2485 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2487 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2488 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2491 # check for needless usb_free_urb() checks
2492 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2494 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2495 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2499 # warn about #ifdefs in C files
2500 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2501 # print "#ifdef in C files should be avoided\n";
2502 # print "$herecurr";
2506 # warn about spacing in #ifdefs
2507 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2508 ERROR("exactly one space required after that #$1\n" . $herecurr);
2511 # check for spinlock_t definitions without a comment.
2512 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2513 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
2515 if (!ctx_has_comment($first_line, $linenr)) {
2516 CHK("$1 definition without comment\n" . $herecurr);
2519 # check for memory barriers without a comment.
2520 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2521 if (!ctx_has_comment($first_line, $linenr)) {
2522 CHK("memory barrier without comment\n" . $herecurr);
2525 # check of hardware specific defines
2526 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2527 CHK("architecture specific defines should be avoided\n" . $herecurr);
2530 # check the location of the inline attribute, that it is between
2531 # storage class and type.
2532 if ($line =~ /\b$Type\s+$Inline\b/ ||
2533 $line =~ /\b$Inline\s+$Storage\b/) {
2534 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2537 # Check for __inline__ and __inline, prefer inline
2538 if ($line =~ /\b(__inline__|__inline)\b/) {
2539 WARN("plain inline is preferred over $1\n" . $herecurr);
2542 # check for new externs in .c files.
2543 if ($realfile =~ /\.c$/ && defined $stat &&
2544 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2546 my $function_name = $1;
2547 my $paren_space = $2;
2550 if (defined $cond) {
2551 substr($s, 0, length($cond), '');
2553 if ($s =~ /^\s*;/ &&
2554 $function_name ne 'uninitialized_var')
2556 WARN("externs should be avoided in .c files\n" . $herecurr);
2559 if ($paren_space =~ /\n/) {
2560 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2563 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2564 $stat =~ /^.\s*extern\s+/)
2566 WARN("externs should be avoided in .c files\n" . $herecurr);
2569 # checks for new __setup's
2570 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2573 if (!grep(/$name/, @setup_docs)) {
2574 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2578 # check for pointless casting of kmalloc return
2579 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2580 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2583 # check for gcc specific __FUNCTION__
2584 if ($line =~ /__FUNCTION__/) {
2585 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2588 # check for semaphores used as mutexes
2589 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2590 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2592 # check for semaphores used as mutexes
2593 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2594 WARN("consider using a completion\n" . $herecurr);
2596 # recommend strict_strto* over simple_strto*
2597 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2598 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2600 # check for __initcall(), use device_initcall() explicitly please
2601 if ($line =~ /^.\s*__initcall\s*\(/) {
2602 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2604 # check for struct file_operations, ensure they are const.
2605 if ($line !~ /\bconst\b/ &&
2606 $line =~ /\bstruct\s+(file_operations|seq_operations)\b/) {
2607 WARN("struct $1 should normally be const\n" .
2611 # use of NR_CPUS is usually wrong
2612 # ignore definitions of NR_CPUS and usage to define arrays as likely right
2613 if ($line =~ /\bNR_CPUS\b/ &&
2614 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2615 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2616 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2617 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2618 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2620 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2623 # check for %L{u,d,i} in strings
2625 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2626 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2627 $string =~ s/%%/__/g;
2628 if ($string =~ /(?<!%)%L[udi]/) {
2629 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2634 # whine mightly about in_atomic
2635 if ($line =~ /\bin_atomic\s*\(/) {
2636 if ($realfile =~ m@^drivers/@) {
2637 ERROR("do not use in_atomic in drivers\n" . $herecurr);
2638 } elsif ($realfile !~ m@^kernel/@) {
2639 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2644 # If we have no input at all, then there is nothing to report on
2645 # so just keep quiet.
2646 if ($#rawlines == -1) {
2650 # In mailback mode only produce a report in the negative, for
2651 # things that appear to be patches.
2652 if ($mailback && ($clean == 1 || !$is_patch)) {
2656 # This is not a patch, and we are are in 'no-patch' mode so
2658 if (!$chk_patch && !$is_patch) {
2663 ERROR("Does not appear to be a unified-diff format patch\n");
2665 if ($is_patch && $chk_signoff && $signoff == 0) {
2666 ERROR("Missing Signed-off-by: line(s)\n");
2669 print report_dump();
2670 if ($summary && !($clean == 1 && $quiet == 1)) {
2671 print "$filename " if ($summary_file);
2672 print "total: $cnt_error errors, $cnt_warn warnings, " .
2673 (($check)? "$cnt_chk checks, " : "") .
2674 "$cnt_lines lines checked\n";
2675 print "\n" if ($quiet == 0);
2678 if ($clean == 1 && $quiet == 0) {
2679 print "$vname has no obvious style problems and is ready for submission.\n"
2681 if ($clean == 0 && $quiet == 0) {
2682 print "$vname has style problems, please review. If any of these errors\n";
2683 print "are false positives report them to the maintainer, see\n";
2684 print "CHECKPATCH in MAINTAINERS.\n";