2 # (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit)
3 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4 # (c) 2007, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite, etc)
5 # Licensed under the terms of the GNU GPL License version 2
14 use Getopt
::Long
qw(:config no_auto_abbrev);
31 'q|quiet+' => \
$quiet,
33 'signoff!' => \
$chk_signoff,
34 'patch!' => \
$chk_patch,
38 'subjective!' => \
$check,
41 'summary!' => \
$summary,
42 'mailback!' => \
$mailback,
43 'summary-file!' => \
$summary_file,
46 'test-type!' => \
$tst_type,
52 print "usage: $P [options] patchfile\n";
53 print "version: $V\n";
54 print "options: -q => quiet\n";
55 print " --no-tree => run without a kernel tree\n";
56 print " --terse => one line per report\n";
57 print " --emacs => emacs compile window format\n";
58 print " --file => check a source file\n";
59 print " --strict => enable more subjective tests\n";
60 print " --root => path to the kernel tree root\n";
61 print " --no-summary => suppress the per-file summary\n";
62 print " --summary-file => include the filename in summary\n";
68 for my $key (keys %debug) {
69 eval "\${dbg_$key} = '$debug{$key}';"
79 if (!top_of_kernel_tree
($root)) {
80 die "$P: $root: --root does not point at a valid tree\n";
83 if (top_of_kernel_tree
('.')) {
85 } elsif ($0 =~ m@
(.*)/scripts/[^/]*$@
&&
86 top_of_kernel_tree
($1)) {
92 print "Must be run from the top-level dir. of a kernel tree\n";
97 my $emitted_corrupt = 0;
99 our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
100 our $Storage = qr{extern|static|asmlinkage};
114 __
(?
:mem
|cpu
|dev
|)(?
:initdata
|init
)
116 our $Inline = qr{inline|__always_inline|noinline};
117 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
118 our $Lval = qr{$Ident(?:$Member)*};
120 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
121 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
125 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%
144 qr{long\s+long\s+int},
145 qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
150 qr{${Ident}_handler
},
151 qr{${Ident}_handler_fn
},
155 my $all = "(?: \n" . join("|\n ", @typeList) . "\n)";
162 (?
:typeof
|__typeof__
)\s
*\
(\s
*\
**\s
*$Ident\s
*\
)
164 (?
:\s
+$Sparse|\s
+const
)*
169 (?
:\s
*\
*+\s
*const
|\s
*\
*+|(?
:\s
*\
[\s
*\
])+)?
170 (?
:\s
+$Inline|\s
+$Sparse|\s
+$Attribute)*
172 $Declare = qr{(?:$Storage\s+)?$Type};
176 $chk_signoff = 0 if ($file);
178 my @dep_includes = ();
179 my @dep_functions = ();
180 my $removal = "Documentation/feature-removal-schedule.txt";
181 if ($tree && -f
"$root/$removal") {
182 open(REMOVE
, "<$root/$removal") ||
183 die "$P: $removal: open failed - $!\n";
185 if (/^Check:\s+(.*\S)/) {
186 for my $entry (split(/[, ]+/, $1)) {
187 if ($entry =~ m
@include/(.*)@
) {
188 push(@dep_includes, $1);
190 } elsif ($entry !~ m@
/@
) {
191 push(@dep_functions, $entry);
201 for my $filename (@ARGV) {
203 open(FILE
, "diff -u /dev/null $filename|") ||
204 die "$P: $filename: diff failed - $!\n";
206 open(FILE
, "<$filename") ||
207 die "$P: $filename: open failed - $!\n";
209 if ($filename eq '-') {
210 $vname = 'Your patch';
219 if (!process
($filename)) {
228 sub top_of_kernel_tree
{
232 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
233 "README", "Documentation", "arch", "include", "drivers",
234 "fs", "init", "ipc", "kernel", "lib", "scripts",
237 foreach my $check (@tree_check) {
238 if (! -e
$root . '/' . $check) {
250 for my $c (split(//, $str)) {
254 for (; ($n % 8) != 0; $n++) {
269 for my $c (split(//, $str)) {
283 # Drop the diff line leader and expand tabs
285 $line = expand_tabs
($line);
287 # Pick the indent from the front of the line.
288 my ($white) = ($line =~ /^(\s*)/);
290 return (length($line), length($white));
302 foreach my $c (split(//, $line)) {
303 # The second backslash of a pair is not a "quote".
304 if ($l eq "\\" && $c eq "\\") {
307 if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
314 } elsif ($quote eq $c) {
318 if ($quote eq "'" && $qlen > 1) {
321 if ($quote && $c ne "\t") {
331 # Clear out the comments.
332 while ($res =~ m@
(/\*.*?\*/)@g) {
333 substr($res, $-[1], $+[1] - $-[1]) = $; x
($+[1] - $-[1]);
335 if ($res =~ m@
(/\
*.*)@
) {
336 substr($res, $-[1], $+[1] - $-[1]) = $; x
($+[1] - $-[1]);
338 if ($res =~ m@
^.(.*\
*/)@
) {
339 substr($res, $-[1], $+[1] - $-[1]) = $; x
($+[1] - $-[1]);
342 # The pathname on a #include may be surrounded by '<' and '>'.
343 if ($res =~ /^.#\s*include\s+\<(.*)\>/) {
344 my $clean = 'X' x
length($1);
345 $res =~ s@\
<.*\
>@
<$clean>@
;
347 # The whole of a #error is a string.
348 } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) {
349 my $clean = 'X' x
length($1);
350 $res =~ s@
(#\s*(?:error|warning)\s+).*@$1$clean@;
356 sub ctx_statement_block
{
357 my ($linenr, $remain, $off) = @_;
358 my $line = $linenr - 1;
373 #warn "CSB: blk<$blk>\n";
374 # If we are about to drop off the end, pull in more
377 for (; $remain > 0; $line++) {
378 next if ($lines[$line] =~ /^-/);
381 $blk .= $lines[$line] . "\n";
386 # Bail if there is no further context.
387 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
393 $c = substr($blk, $off, 1);
394 $remainder = substr($blk, $off);
396 #warn "CSB: c<$c> type<$type> level<$level>\n";
397 # Statement ends at the ';' or a close '}' at the
399 if ($level == 0 && $c eq ';') {
403 # An else is really a conditional as long as its not else if
404 if ($level == 0 && (!defined($p) || $p =~ /(?:\s|\})/) &&
405 $remainder =~ /(else)(?:\s|{)/ &&
406 $remainder !~ /else\s+if\b/) {
407 $coff = $off + length($1);
410 if (($type eq '' || $type eq '(') && $c eq '(') {
414 if ($type eq '(' && $c eq ')') {
416 $type = ($level != 0)?
'(' : '';
418 if ($level == 0 && $coff < $soff) {
422 if (($type eq '' || $type eq '{') && $c eq '{') {
426 if ($type eq '{' && $c eq '}') {
428 $type = ($level != 0)?
'{' : '';
441 my $statement = substr($blk, $soff, $off - $soff + 1);
442 my $condition = substr($blk, $soff, $coff - $soff + 1);
444 #warn "STATEMENT<$statement>\n";
445 #warn "CONDITION<$condition>\n";
447 #print "off<$off> loff<$loff>\n";
449 return ($statement, $condition,
450 $line, $remain + 1, $off - $loff + 1, $level);
453 sub statement_lines
{
456 # Strip the diff line prefixes and rip blank lines at start and end.
457 $stmt =~ s/(^|\n)./$1/g;
461 my @stmt_lines = ($stmt =~ /\n/g);
463 return $#stmt_lines + 2;
466 sub statement_rawlines
{
469 my @stmt_lines = ($stmt =~ /\n/g);
471 return $#stmt_lines + 2;
474 sub statement_block_size
{
477 $stmt =~ s/(^|\n)./$1/g;
483 my @stmt_lines = ($stmt =~ /\n/g);
484 my @stmt_statements = ($stmt =~ /;/g);
486 my $stmt_lines = $#stmt_lines + 2;
487 my $stmt_statements = $#stmt_statements + 1;
489 if ($stmt_lines > $stmt_statements) {
492 return $stmt_statements;
496 sub ctx_statement_full
{
497 my ($linenr, $remain, $off) = @_;
498 my ($statement, $condition, $level);
502 # Grab the first conditional/block pair.
503 ($statement, $condition, $linenr, $remain, $off, $level) =
504 ctx_statement_block
($linenr, $remain, $off);
505 #print "F: c<$condition> s<$statement>\n";
506 push(@chunks, [ $condition, $statement ]);
507 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
508 return ($level, $linenr, @chunks);
511 # Pull in the following conditional/block pairs and see if they
512 # could continue the statement.
514 ($statement, $condition, $linenr, $remain, $off, $level) =
515 ctx_statement_block
($linenr, $remain, $off);
516 #print "C: c<$condition> s<$statement> remain<$remain>\n";
517 last if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:else|do)\b/s));
519 push(@chunks, [ $condition, $statement ]);
522 return ($level, $linenr, @chunks);
526 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
528 my $start = $linenr - 1;
535 for ($line = $start; $remain > 0; $line++) {
536 next if ($rawlines[$line] =~ /^-/);
539 $blk .= $rawlines[$line];
540 foreach my $c (split(//, $rawlines[$line])) {
541 ##print "C<$c>L<$level><$open$close>O<$off>\n";
547 if ($c eq $close && $level > 0) {
549 last if ($level == 0);
550 } elsif ($c eq $open) {
555 if (!$outer || $level <= 1) {
556 push(@res, $rawlines[$line]);
559 last if ($level == 0);
562 return ($level, @res);
564 sub ctx_block_outer
{
565 my ($linenr, $remain) = @_;
567 my ($level, @r) = ctx_block_get
($linenr, $remain, 1, '{', '}', 0);
571 my ($linenr, $remain) = @_;
573 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
577 my ($linenr, $remain, $off) = @_;
579 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
582 sub ctx_block_level
{
583 my ($linenr, $remain) = @_;
585 return ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
587 sub ctx_statement_level
{
588 my ($linenr, $remain, $off) = @_;
590 return ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
593 sub ctx_locate_comment
{
594 my ($first_line, $end_line) = @_;
596 # Catch a comment on the end of the line itself.
597 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@
.*(/\*.*\*/)\s
*$@
);
598 return $current_comment if (defined $current_comment);
600 # Look through the context and try and figure out if there is a
603 $current_comment = '';
604 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
605 my $line = $rawlines[$linenr - 1];
607 if ($linenr == $first_line and $line =~ m@
^.\s
*\
*@
) {
610 if ($line =~ m@
/\
*@
) {
613 if (!$in_comment && $current_comment ne '') {
614 $current_comment = '';
616 $current_comment .= $line . "\n" if ($in_comment);
617 if ($line =~ m@\
*/@
) {
622 chomp($current_comment);
623 return($current_comment);
625 sub ctx_has_comment
{
626 my ($first_line, $end_line) = @_;
627 my $cmt = ctx_locate_comment
($first_line, $end_line);
629 ##print "LINE: $rawlines[$end_line - 1 ]\n";
630 ##print "CMMT: $cmt\n";
640 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
643 $coded = sprintf("^%c", unpack('C', $2) + 64);
652 my $av_preprocessor = 0;
657 $av_preprocessor = 0;
659 @av_paren_type = ('E');
662 sub annotate_values
{
663 my ($stream, $type) = @_;
668 print "$stream\n" if ($dbg_values > 1);
670 while (length($cur)) {
671 print " <" . join('', @av_paren_type) .
672 "> <$type> " if ($dbg_values > 1);
673 if ($cur =~ /^(\s+)/o) {
674 print "WS($1)\n" if ($dbg_values > 1);
675 if ($1 =~ /\n/ && $av_preprocessor) {
676 $type = pop(@av_paren_type);
677 $av_preprocessor = 0;
680 } elsif ($cur =~ /^($Type)/) {
681 print "DECLARE($1)\n" if ($dbg_values > 1);
684 } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
685 print "DEFINE($1)\n" if ($dbg_values > 1);
686 $av_preprocessor = 1;
689 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if))/o) {
690 print "PRE_START($1)\n" if ($dbg_values > 1);
691 $av_preprocessor = 1;
693 push(@av_paren_type, $type);
694 push(@av_paren_type, $type);
697 } elsif ($cur =~ /^(#\s*(?:else|elif))/o) {
698 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
699 $av_preprocessor = 1;
701 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
705 } elsif ($cur =~ /^(#\s*(?:endif))/o) {
706 print "PRE_END($1)\n" if ($dbg_values > 1);
708 $av_preprocessor = 1;
710 # Assume all arms of the conditional end as this
711 # one does, and continue as if the #endif was not here.
713 push(@av_paren_type, $type);
716 } elsif ($cur =~ /^(\\\n)/o) {
717 print "PRECONT($1)\n" if ($dbg_values > 1);
719 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
720 print "SIZEOF($1)\n" if ($dbg_values > 1);
726 } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
727 print "COND($1)\n" if ($dbg_values > 1);
731 } elsif ($cur =~/^(return|case|else)/o) {
732 print "KEYWORD($1)\n" if ($dbg_values > 1);
735 } elsif ($cur =~ /^(\()/o) {
736 print "PAREN('$1')\n" if ($dbg_values > 1);
737 push(@av_paren_type, $av_pending);
741 } elsif ($cur =~ /^(\))/o) {
742 my $new_type = pop(@av_paren_type);
743 if ($new_type ne '_') {
745 print "PAREN('$1') -> $type\n"
746 if ($dbg_values > 1);
748 print "PAREN('$1')\n" if ($dbg_values > 1);
751 } elsif ($cur =~ /^($Ident)\(/o) {
752 print "FUNC($1)\n" if ($dbg_values > 1);
755 } elsif ($cur =~ /^($Ident|$Constant)/o) {
756 print "IDENT($1)\n" if ($dbg_values > 1);
759 } elsif ($cur =~ /^($Assignment)/o) {
760 print "ASSIGN($1)\n" if ($dbg_values > 1);
763 } elsif ($cur =~/^(;|{|})/) {
764 print "END($1)\n" if ($dbg_values > 1);
767 } elsif ($cur =~ /^(;|\?|:|\[)/o) {
768 print "CLOSE($1)\n" if ($dbg_values > 1);
771 } elsif ($cur =~ /^($Operators)/o) {
772 print "OP($1)\n" if ($dbg_values > 1);
773 if ($1 ne '++' && $1 ne '--') {
777 } elsif ($cur =~ /(^.)/o) {
778 print "C($1)\n" if ($dbg_values > 1);
781 $cur = substr($cur, length($1));
782 $res .= $type x
length($1);
790 my ($possible, $line) = @_;
792 #print "CHECK<$possible>\n";
793 if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
794 $possible ne 'goto' && $possible ne 'return' &&
795 $possible ne 'struct' && $possible ne 'enum' &&
796 $possible ne 'case' && $possible ne 'else' &&
797 $possible ne 'typedef') {
798 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
799 push(@typeList, $possible);
807 my $line = $prefix . $_[0];
809 $line = (split('\n', $line))[0] . "\n" if ($terse);
811 push(our @report, $line);
817 report
("ERROR: $_[0]\n");
822 report
("WARNING: $_[0]\n");
828 report
("CHECK: $_[0]\n");
835 my $filename = shift;
858 # Trace the real file/line as we go.
864 my $comment_edge = 0;
867 my $prev_values = 'E';
870 my $suppress_ifbraces = 0;
872 # Pre-scan the patch sanitizing the lines.
873 # Pre-scan the patch looking for any __setup documentation.
878 foreach my $rawline (@rawlines) {
879 # Standardise the strings and chars within the input to
881 $line = sanitise_line
($rawline);
884 ##print "==>$rawline\n";
885 ##print "-->$line\n";
887 if ($line=~/^\+\+\+\s+(\S+)/) {
889 if ($1 =~ m
@Documentation/kernel
-parameters
.txt
$@
) {
895 if ($setup_docs && $line =~ /^\+/) {
896 push(@setup_docs, $line);
902 foreach my $line (@lines) {
905 my $rawline = $rawlines[$linenr - 1];
907 #extract the filename as it passes
908 if ($line=~/^\+\+\+\s+(\S+)/) {
910 $realfile =~ s@
^[^/]*/@@
;
914 #extract the line range in the file after the patch is applied
915 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
917 $first_line = $linenr + 1;
928 $suppress_ifbraces = $linenr - 1;
932 # track the line number as we move through the hunk, note that
933 # new versions of GNU diff omit the leading space on completely
934 # blank context lines so we need to count that too.
935 if ($line =~ /^( |\+|$)/) {
937 $realcnt-- if ($realcnt != 0);
939 # Guestimate if this is a continuing comment. Run
940 # the context looking for a comment "edge". If this
941 # edge is a close comment then we must be in a comment
943 if ($linenr == $first_line) {
945 for (my $ln = $first_line; $ln < ($linenr + $realcnt); $ln++) {
946 ($edge) = ($rawlines[$ln - 1] =~ m@
(/\*|\*/)@
);
947 last if (defined $edge);
949 if (defined $edge && $edge eq '*/') {
954 # Guestimate if this is a continuing comment. If this
955 # is the start of a diff block and this line starts
956 # ' *' then it is very likely a comment.
957 if ($linenr == $first_line and $rawline =~ m@
^.\s
* \
*(?
:\s
|$)@
) {
961 # Find the last comment edge on _this_ line.
963 while (($rawline =~ m@
(/\*|\*/)@g)) {
972 # Measure the line length and indent.
973 ($length, $indent) = line_stats
($rawline);
975 # Track the previous line.
976 ($prevline, $stashline) = ($stashline, $line);
977 ($previndent, $stashindent) = ($stashindent, $indent);
978 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
980 #warn "ic<$in_comment> ce<$comment_edge> line<$line>\n";
982 } elsif ($realcnt == 1) {
986 #make up the handle for any error we report on this line
987 $here = "#$linenr: " if (!$file);
988 $here = "#$realline: " if ($file);
989 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
991 my $hereline = "$here\n$rawline\n";
992 my $herecurr = "$here\n$rawline\n";
993 my $hereprev = "$here\n$prevrawline\n$rawline\n";
995 $prefix = "$filename:$realline: " if ($emacs && $file);
996 $prefix = "$filename:$linenr: " if ($emacs && !$file);
997 $cnt_lines++ if ($realcnt != 0);
999 #check the patch for a signoff:
1000 if ($line =~ /^\s*signed-off-by:/i) {
1001 # This is a signoff, if ugly, so do not double report.
1003 if (!($line =~ /^\s*Signed-off-by:/)) {
1004 WARN
("Signed-off-by: is the preferred form\n" .
1007 if ($line =~ /^\s*signed-off-by:\S/i) {
1008 WARN
("need space after Signed-off-by:\n" .
1013 # Check for wrappage within a valid hunk of the file
1014 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1015 ERROR
("patch seems to be corrupt (line wrapped?)\n" .
1016 $herecurr) if (!$emitted_corrupt++);
1019 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1020 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1022 [\x09\x0A\x0D\x20-\x7E] # ASCII
1023 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
1024 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
1025 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
1026 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
1027 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
1028 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
1029 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
1031 ERROR
("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $herecurr);
1034 #ignore lines being removed
1035 if ($line=~/^-/) {next;}
1037 # check we are in a valid source file if not then ignore this hunk
1038 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1040 #trailing whitespace
1041 if ($line =~ /^\+.*\015/) {
1042 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1043 ERROR
("DOS line endings\n" . $herevet);
1045 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1046 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1047 ERROR
("trailing whitespace\n" . $herevet);
1050 if ($line =~ /^\+/ && !($prevrawline=~/\/\
*\
*/) && $length > 80) {
1051 WARN
("line over 80 characters\n" . $herecurr);
1054 # check for adding lines without a newline.
1055 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1056 WARN
("adding a line without newline at end of file\n" . $herecurr);
1059 # check we are in a valid source file *.[hc] if not then ignore this hunk
1060 next if ($realfile !~ /\.[hc]$/);
1062 # at the beginning of a line any tabs must come first and anything
1063 # more than 8 must use tabs.
1064 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1065 $rawline =~ /^\+\s* \s*/) {
1066 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1067 ERROR
("use tabs not spaces\n" . $herevet);
1070 # check for RCS/CVS revision markers
1071 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1072 WARN
("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1075 # The rest of our checks refer specifically to C style
1076 # only apply those _outside_ comments. Only skip
1077 # lines in the middle of comments.
1078 next if (!$comment_edge && $in_comment);
1080 # Check for potential 'bare' types
1082 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
1086 # Ignore goto labels.
1087 if ($s =~ /$Ident:\*$/) {
1089 # Ignore functions being called
1090 } elsif ($s =~ /^.\s*$Ident\s*\(/) {
1092 # definitions in global scope can only start with types
1093 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) {
1096 # declarations always start with types
1097 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/) {
1101 # any (foo ... *) is a pointer cast, and foo is a type
1102 while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) {
1106 # Check for any sort of function declaration.
1107 # int foo(something bar, other baz);
1108 # void (*store_gdt)(x86_descr_ptr *);
1109 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) {
1110 my ($name_len) = length($1);
1113 substr($ctx, 0, $name_len + 1) = '';
1114 $ctx =~ s/\)[^\)]*$//;
1116 for my $arg (split(/\s*,\s*/, $ctx)) {
1117 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
1127 # Checks which may be anchored in the context.
1130 # Check for switch () and associated case and default
1131 # statements should be at the same indent.
1132 if ($line=~/\bswitch\s*\(.*\)/) {
1135 my @ctx = ctx_block_outer
($linenr, $realcnt);
1137 for my $ctx (@ctx) {
1138 my ($clen, $cindent) = line_stats
($ctx);
1139 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1140 $indent != $cindent) {
1141 $err .= "$sep$ctx\n";
1148 ERROR
("switch and case should be at the same indent\n$hereline$err");
1152 # if/while/etc brace do not go on next line, unless defining a do while loop,
1153 # or if that brace on the next line is for something else
1154 if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
1155 my ($level, @ctx) = ctx_statement_level
($linenr, $realcnt, 0);
1156 my $ctx_ln = $linenr + $#ctx + 1;
1157 my $ctx_cnt = $realcnt - $#ctx - 1;
1158 my $ctx = join("\n", @ctx);
1160 # Skip over any removed lines in the context following statement.
1161 while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
1165 ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
1167 if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1168 ERROR
("That open brace { should be on the previous line\n" .
1169 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1171 if ($level == 0 && $ctx =~ /\)\s*\;\s*$/ && defined $lines[$ctx_ln - 1]) {
1172 my ($nlength, $nindent) = line_stats
($lines[$ctx_ln - 1]);
1173 if ($nindent > $indent) {
1174 WARN
("Trailing semicolon indicates no statements, indent implies otherwise\n" .
1175 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1180 # Track the 'values' across context and added lines.
1181 my $opline = $line; $opline =~ s/^./ /;
1182 my $curr_values = annotate_values
($opline . "\n", $prev_values);
1183 $curr_values = $prev_values . $curr_values;
1185 my $outline = $opline; $outline =~ s/\t/ /g;
1186 print "$linenr > .$outline\n";
1187 print "$linenr > $curr_values\n";
1189 $prev_values = substr($curr_values, -1);
1191 #ignore lines not being added
1192 if ($line=~/^[^\+]/) {next;}
1194 # TEST: allow direct testing of the type matcher.
1195 if ($tst_type && $line =~ /^.$Declare$/) {
1196 ERROR
("TEST: is type $Declare\n" . $herecurr);
1200 # check for initialisation to aggregates open brace on the next line
1201 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1202 $line =~ /^.\s*{/) {
1203 ERROR
("That open brace { should be on the previous line\n" . $hereprev);
1207 # Checks which are anchored on the added line.
1210 # check for malformed paths in #include statements (uses RAW line)
1211 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
1213 if ($path =~ m{//}) {
1214 ERROR
("malformed #include filename\n" .
1219 # no C99 // comments
1220 if ($line =~ m{//}) {
1221 ERROR
("do not use C99 // comments\n" . $herecurr);
1223 # Remove C99 comments.
1225 $opline =~ s@
//.*@@
;
1227 #EXPORT_SYMBOL should immediately follow its function closing }.
1228 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1229 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1231 if (($prevline !~ /^}/) &&
1232 ($prevline !~ /^\+}/) &&
1233 ($prevline !~ /^ }/) &&
1234 ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1235 ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
1236 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
1237 WARN
("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1241 # check for external initialisers.
1242 if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
1243 ERROR
("do not initialise externals to 0 or NULL\n" .
1246 # check for static initialisers.
1247 if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
1248 ERROR
("do not initialise statics to 0 or NULL\n" .
1252 # check for new typedefs, only function parameters and sparse annotations
1254 if ($line =~ /\btypedef\s/ &&
1255 $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1256 $line !~ /\b__bitwise(?:__|)\b/) {
1257 WARN
("do not add new typedefs\n" . $herecurr);
1260 # * goes on variable not on type
1261 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
1262 ERROR
("\"(foo$1)\" should be \"(foo $1)\"\n" .
1265 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
1266 ERROR
("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1269 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
1270 ERROR
("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1273 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
1274 ERROR
("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1278 # # no BUG() or BUG_ON()
1279 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1280 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1281 # print "$herecurr";
1285 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1286 WARN
("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1289 # printk should use KERN_* levels. Note that follow on printk's on the
1290 # same line do not need a level, so we use the current block context
1291 # to try and find and validate the current printk. In summary the current
1292 # printk includes all preceeding printk's which have no newline on the end.
1293 # we assume the first bad printk is the one to report.
1294 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1296 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1297 #print "CHECK<$lines[$ln - 1]\n";
1298 # we have a preceeding printk if it ends
1299 # with "\n" ignore it, else it is to blame
1300 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1301 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1308 WARN
("printk() should include KERN_ facility level\n" . $herecurr);
1312 # function brace can't be on same line, except for #defines of do while,
1313 # or if closed on same line
1314 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and
1315 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
1316 ERROR
("open brace '{' following function declarations go on the next line\n" . $herecurr);
1319 # open braces for enum, union and struct go on the same line.
1320 if ($line =~ /^.\s*{/ &&
1321 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1322 ERROR
("open brace '{' following $1 go on the same line\n" . $hereprev);
1325 # check for spaces between functions and their parentheses.
1326 while ($line =~ /($Ident)\s+\(/g) {
1328 my $ctx = substr($line, 0, $-[1]);
1330 # Ignore those directives where spaces _are_ permitted.
1331 if ($name =~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case|__asm__)$/) {
1333 # cpp #define statements have non-optional spaces, ie
1334 # if there is a space between the name and the open
1335 # parenthesis it is simply not a parameter group.
1336 } elsif ($ctx =~ /^.\#\s*define\s*$/) {
1338 # If this whole things ends with a type its most
1339 # likely a typedef for a function.
1340 } elsif ("$ctx$name" =~ /$Type$/) {
1343 WARN
("no space between function name and open parenthesis '('\n" . $herecurr);
1346 # Check operator spacing.
1347 if (!($line=~/\#\s*include/)) {
1349 <<=|>>=|<=|>=|==|!=|
1350 \
+=|-=|\
*=|\
/=|%=|\
^=|\
|=|&=|
1351 =>|->|<<|>>|<|>|=|!|~|
1352 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%
1354 my @elements = split(/($ops|;)/, $opline);
1357 my $blank = copy_spacing
($opline);
1359 for (my $n = 0; $n < $#elements; $n += 2) {
1360 $off += length($elements[$n]);
1363 $a = 'V' if ($elements[$n] ne '');
1364 $a = 'W' if ($elements[$n] =~ /\s$/);
1365 $a = 'C' if ($elements[$n] =~ /$;$/);
1366 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1367 $a = 'O' if ($elements[$n] eq '');
1368 $a = 'E' if ($elements[$n] eq '' && $n == 0);
1370 my $op = $elements[$n + 1];
1373 if (defined $elements[$n + 2]) {
1374 $c = 'V' if ($elements[$n + 2] ne '');
1375 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1376 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1377 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1378 $c = 'O' if ($elements[$n + 2] eq '');
1379 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
1384 # Pick up the preceeding and succeeding characters.
1385 my $ca = substr($opline, 0, $off);
1387 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1388 $cc = substr($opline, $off + length($elements[$n + 1]));
1390 my $cb = "$ca$;$cc";
1392 my $ctx = "${a}x${c}";
1394 my $at = "(ctx:$ctx)";
1396 my $ptr = substr($blank, 0, $off) . "^";
1397 my $hereptr = "$hereline$ptr\n";
1399 # Classify operators into binary, unary, or
1400 # definitions (* only) where they have more
1402 my $op_type = substr($curr_values, $off + 1, 1);
1403 my $op_left = substr($curr_values, $off, 1);
1405 if ($op_type eq 'T') {
1407 } elsif ($op_left eq 'V') {
1412 #if ($op eq '-' || $op eq '&' || $op eq '*') {
1413 # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
1416 # Ignore operators passed as parameters.
1417 if ($op_type ne 'V' &&
1418 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1421 # } elsif ($op =~ /^$;+$/) {
1423 # ; should have either the end of line or a space or \ after it
1424 } elsif ($op eq ';') {
1425 if ($ctx !~ /.x[WEBC]/ &&
1426 $cc !~ /^\\/ && $cc !~ /^;/) {
1427 ERROR
("need space after that '$op' $at\n" . $hereptr);
1431 } elsif ($op eq '//') {
1433 # -> should have no spaces
1434 } elsif ($op eq '->') {
1435 if ($ctx =~ /Wx.|.xW/) {
1436 ERROR
("no spaces around that '$op' $at\n" . $hereptr);
1439 # , must have a space on the right.
1440 } elsif ($op eq ',') {
1441 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1442 ERROR
("need space after that '$op' $at\n" . $hereptr);
1445 # '*' as part of a type definition -- reported already.
1446 } elsif ($op eq '*' && $is_unary == 2) {
1447 #warn "'*' is part of type\n";
1449 # unary operators should have a space before and
1450 # none after. May be left adjacent to another
1451 # unary operator, or a cast
1452 } elsif ($op eq '!' || $op eq '~' ||
1453 ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
1454 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1455 ERROR
("need space before that '$op' $at\n" . $hereptr);
1457 if ($ctx =~ /.xW/) {
1458 ERROR
("no space after that '$op' $at\n" . $hereptr);
1461 # unary ++ and unary -- are allowed no space on one side.
1462 } elsif ($op eq '++' or $op eq '--') {
1463 if ($ctx !~ /[WOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1464 ERROR
("need space one side of that '$op' $at\n" . $hereptr);
1466 if ($ctx =~ /WxB/ || ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1467 ERROR
("no space before that '$op' $at\n" . $hereptr);
1470 # << and >> may either have or not have spaces both sides
1471 } elsif ($op eq '<<' or $op eq '>>' or
1472 $op eq '&' or $op eq '^' or $op eq '|' or
1473 $op eq '+' or $op eq '-' or
1474 $op eq '*' or $op eq '/' or
1477 if ($ctx !~ /VxV|WxW|VxE|WxE|VxO|Cx.|.xC/) {
1478 ERROR
("need consistent spacing around '$op' $at\n" .
1482 # All the others need spaces both sides.
1483 } elsif ($ctx !~ /[EWC]x[CWE]/) {
1484 # Ignore email addresses <foo@bar>
1485 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
1486 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
1487 ERROR
("need spaces around that '$op' $at\n" . $hereptr);
1490 $off += length($elements[$n + 1]);
1494 # check for multiple assignments
1495 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
1496 CHK
("multiple assignments should be avoided\n" . $herecurr);
1499 ## # check for multiple declarations, allowing for a function declaration
1501 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1502 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1504 ## # Remove any bracketed sections to ensure we do not
1505 ## # falsly report the parameters of functions.
1507 ## while ($ln =~ s/\([^\(\)]*\)//g) {
1509 ## if ($ln =~ /,/) {
1510 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1514 #need space before brace following if, while, etc
1515 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1517 ERROR
("need a space before the open brace '{'\n" . $herecurr);
1520 # closing brace should have a space following it when it has anything
1522 if ($line =~ /}(?!(?:,|;|\)))\S/) {
1523 ERROR
("need a space after that close brace '}'\n" . $herecurr);
1526 # check spacing on square brackets
1527 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1528 ERROR
("no space after that open square bracket '['\n" . $herecurr);
1530 if ($line =~ /\s\]/) {
1531 ERROR
("no space before that close square bracket ']'\n" . $herecurr);
1534 # check spacing on paretheses
1535 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1536 $line !~ /for\s*\(\s+;/) {
1537 ERROR
("no space after that open parenthesis '('\n" . $herecurr);
1539 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1540 $line !~ /for\s*\(.*;\s+\)/) {
1541 ERROR
("no space before that close parenthesis ')'\n" . $herecurr);
1544 #goto labels aren't indented, allow a single space however
1545 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
1546 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1547 WARN
("labels should not be indented\n" . $herecurr);
1550 # Need a space before open parenthesis after if, while etc
1551 if ($line=~/\b(if|while|for|switch)\(/) {
1552 ERROR
("need a space before the open parenthesis '('\n" . $herecurr);
1555 # Check for illegal assignment in if conditional.
1556 if ($line =~ /\bif\s*\(/) {
1557 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
1559 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
1560 ERROR
("do not use assignment in if condition\n" . $herecurr);
1563 # Find out what is on the end of the line after the
1565 substr($s, 0, length($c)) = '';
1567 $s =~ s/$;//g; # Remove any comments
1568 if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/) {
1569 ERROR
("trailing statements should be on next line\n" . $herecurr);
1573 # Check for bitwise tests written as boolean
1585 WARN
("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
1588 # if and else should not have general statements after it
1589 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
1591 $s =~ s/$;//g; # Remove any comments
1592 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
1593 ERROR
("trailing statements should be on next line\n" . $herecurr);
1597 # Check for }<nl>else {, these must be at the same
1598 # indent level to be relevant to each other.
1599 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
1600 $previndent == $indent) {
1601 ERROR
("else should follow close brace '}'\n" . $hereprev);
1604 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1605 $previndent == $indent) {
1606 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
1608 # Find out what is on the end of the line after the
1610 substr($s, 0, length($c)) = '';
1613 if ($s =~ /^\s*;/) {
1614 ERROR
("while should follow close brace '}'\n" . $hereprev);
1618 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
1619 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
1620 # print "No studly caps, use _\n";
1621 # print "$herecurr";
1625 #no spaces allowed after \ in define
1626 if ($line=~/\#define.*\\\s$/) {
1627 WARN
("Whitepspace after \\ makes next lines useless\n" . $herecurr);
1630 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1631 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
1632 my $checkfile = "$root/include/linux/$1.h";
1633 if (-f
$checkfile && $1 ne 'irq.h') {
1634 CHK
("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
1639 # multi-statement macros should be enclosed in a do while loop, grab the
1640 # first statement and ensure its the whole macro if its not enclosed
1641 # in a known good container
1642 if ($prevline =~ /\#define.*\\/ &&
1643 $prevline !~/(?:do\s+{|\(\{|\{)/ &&
1644 $line !~ /(?:do\s+{|\(\{|\{)/ &&
1645 $line !~ /^.\s*$Declare\s/) {
1646 # Grab the first statement, if that is the entire macro
1647 # its ok. This may start either on the #define line
1653 # If the macro starts on the define line start
1654 # grabbing the statement after the identifier
1655 $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
1656 ##print "1<$1> 2<$2>\n";
1657 if (defined $2 && $2 ne '') {
1661 while ($lines[$ln - 1] =~ /^-/) {
1666 my @ctx = ctx_statement
($ln, $cnt, $off);
1667 my $ctx_ln = $ln + $#ctx + 1;
1668 my $ctx = join("\n", @ctx);
1670 # Pull in any empty extension lines.
1671 while ($ctx =~ /\\$/ &&
1672 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
1673 $ctx .= $lines[$ctx_ln - 1];
1677 if ($ctx =~ /\\$/) {
1679 ERROR
("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
1681 ERROR
("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
1686 # check for redundant bracing round if etc
1687 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
1688 my ($level, $endln, @chunks) =
1689 ctx_statement_full
($linenr, $realcnt, 1);
1690 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
1691 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
1692 if ($#chunks > 0 && $level == 0) {
1695 my $herectx = $here . "\n";;
1696 my $ln = $linenr - 1;
1697 for my $chunk (@chunks) {
1698 my ($cond, $block) = @
{$chunk};
1700 $herectx .= "$rawlines[$ln]\n[...]\n";
1701 $ln += statement_rawlines
($block) - 1;
1703 substr($block, 0, length($cond)) = '';
1705 $seen++ if ($block =~ /^\s*{/);
1707 #print "cond<$cond> block<$block> allowed<$allowed>\n";
1708 if (statement_lines
($cond) > 1) {
1709 #print "APW: ALLOWED: cond<$cond>\n";
1712 if ($block =~/\b(?:if|for|while)\b/) {
1713 #print "APW: ALLOWED: block<$block>\n";
1716 if (statement_block_size
($block) > 1) {
1717 #print "APW: ALLOWED: lines block<$block>\n";
1721 if ($seen && !$allowed) {
1722 WARN
("braces {} are not necessary for any arm of this statement\n" . $herectx);
1724 # Either way we have looked over this whole
1725 # statement and said what needs to be said.
1726 $suppress_ifbraces = $endln;
1729 if ($linenr > $suppress_ifbraces &&
1730 $line =~ /\b(if|while|for|else)\b/) {
1731 my ($level, $endln, @chunks) =
1732 ctx_statement_full
($linenr, $realcnt, $-[0]);
1736 # Check the pre-context.
1737 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
1738 #print "APW: ALLOWED: pre<$1>\n";
1741 # Check the condition.
1742 my ($cond, $block) = @
{$chunks[0]};
1743 if (defined $cond) {
1744 substr($block, 0, length($cond)) = '';
1746 if (statement_lines
($cond) > 1) {
1747 #print "APW: ALLOWED: cond<$cond>\n";
1750 if ($block =~/\b(?:if|for|while)\b/) {
1751 #print "APW: ALLOWED: block<$block>\n";
1754 if (statement_block_size
($block) > 1) {
1755 #print "APW: ALLOWED: lines block<$block>\n";
1758 # Check the post-context.
1759 if (defined $chunks[1]) {
1760 my ($cond, $block) = @
{$chunks[1]};
1761 if (defined $cond) {
1762 substr($block, 0, length($cond)) = '';
1764 if ($block =~ /^\s*\{/) {
1765 #print "APW: ALLOWED: chunk-1 block<$block>\n";
1769 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
1770 my $herectx = $here . "\n";;
1771 my $end = $linenr + statement_rawlines
($block) - 1;
1773 for (my $ln = $linenr - 1; $ln < $end; $ln++) {
1774 $herectx .= $rawlines[$ln] . "\n";;
1777 WARN
("braces {} are not necessary for single statement blocks\n" . $herectx);
1781 # don't include deprecated include files (uses RAW line)
1782 for my $inc (@dep_includes) {
1783 if ($rawline =~ m@\#\s
*include\s
*\
<$inc>@
) {
1784 ERROR
("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
1788 # don't use deprecated functions
1789 for my $func (@dep_functions) {
1790 if ($line =~ /\b$func\b/) {
1791 ERROR
("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
1795 # no volatiles please
1796 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
1797 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
1798 WARN
("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
1801 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
1802 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
1803 ERROR
("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
1807 if ($line =~ /^.#\s*if\s+0\b/) {
1808 CHK
("if this code is redundant consider removing it\n" .
1812 # check for needless kfree() checks
1813 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1815 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1816 WARN
("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1820 # warn about #ifdefs in C files
1821 # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
1822 # print "#ifdef in C files should be avoided\n";
1823 # print "$herecurr";
1827 # warn about spacing in #ifdefs
1828 if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
1829 ERROR
("exactly one space required after that #$1\n" . $herecurr);
1832 # check for spinlock_t definitions without a comment.
1833 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
1835 if (!ctx_has_comment
($first_line, $linenr)) {
1836 CHK
("$1 definition without comment\n" . $herecurr);
1839 # check for memory barriers without a comment.
1840 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
1841 if (!ctx_has_comment
($first_line, $linenr)) {
1842 CHK
("memory barrier without comment\n" . $herecurr);
1845 # check of hardware specific defines
1846 if ($line =~ m@
^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
1847 CHK
("architecture specific defines should be avoided\n" . $herecurr);
1850 # check the location of the inline attribute, that it is between
1851 # storage class and type.
1852 if ($line =~ /\b$Type\s+$Inline\b/ ||
1853 $line =~ /\b$Inline\s+$Storage\b/) {
1854 ERROR
("inline keyword should sit between storage class and type\n" . $herecurr);
1857 # Check for __inline__ and __inline, prefer inline
1858 if ($line =~ /\b(__inline__|__inline)\b/) {
1859 WARN
("plain inline is preferred over $1\n" . $herecurr);
1862 # check for new externs in .c files.
1863 if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
1864 WARN
("externs should be avoided in .c files\n" . $herecurr);
1867 # checks for new __setup's
1868 if ($rawline =~ /\b__setup\("([^"]*)"/) {
1871 if (!grep(/$name/, @setup_docs)) {
1872 CHK
("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
1876 # check for pointless casting of kmalloc return
1877 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
1878 WARN
("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
1881 # check for gcc specific __FUNCTION__
1882 if ($line =~ /__FUNCTION__/) {
1883 WARN
("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
1887 # If we have no input at all, then there is nothing to report on
1888 # so just keep quiet.
1889 if ($#rawlines == -1) {
1893 # In mailback mode only produce a report in the negative, for
1894 # things that appear to be patches.
1895 if ($mailback && ($clean == 1 || !$is_patch)) {
1899 # This is not a patch, and we are are in 'no-patch' mode so
1901 if (!$chk_patch && !$is_patch) {
1906 ERROR
("Does not appear to be a unified-diff format patch\n");
1908 if ($is_patch && $chk_signoff && $signoff == 0) {
1909 ERROR
("Missing Signed-off-by: line(s)\n");
1912 print report_dump
();
1913 if ($summary && !($clean == 1 && $quiet == 1)) {
1914 print "$filename " if ($summary_file);
1915 print "total: $cnt_error errors, $cnt_warn warnings, " .
1916 (($check)?
"$cnt_chk checks, " : "") .
1917 "$cnt_lines lines checked\n";
1918 print "\n" if ($quiet == 0);
1921 if ($clean == 1 && $quiet == 0) {
1922 print "$vname has no obvious style problems and is ready for submission.\n"
1924 if ($clean == 0 && $quiet == 0) {
1925 print "$vname has style problems, please review. If any of these errors\n";
1926 print "are false positives report them to the maintainer, see\n";
1927 print "CHECKPATCH in MAINTAINERS.\n";