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);
32 'q|quiet+' => \
$quiet,
34 'signoff!' => \
$chk_signoff,
35 'patch!' => \
$chk_patch,
39 'subjective!' => \
$check,
42 'summary!' => \
$summary,
43 'mailback!' => \
$mailback,
44 'summary-file!' => \
$summary_file,
47 'test-type!' => \
$tst_type,
48 'test-only=s' => \
$tst_only,
54 print "usage: $P [options] patchfile\n";
55 print "version: $V\n";
56 print "options: -q => quiet\n";
57 print " --no-tree => run without a kernel tree\n";
58 print " --terse => one line per report\n";
59 print " --emacs => emacs compile window format\n";
60 print " --file => check a source file\n";
61 print " --strict => enable more subjective tests\n";
62 print " --root => path to the kernel tree root\n";
63 print " --no-summary => suppress the per-file summary\n";
64 print " --summary-file => include the filename in summary\n";
70 for my $key (keys %debug) {
71 eval "\${dbg_$key} = '$debug{$key}';"
81 if (!top_of_kernel_tree
($root)) {
82 die "$P: $root: --root does not point at a valid tree\n";
85 if (top_of_kernel_tree
('.')) {
87 } elsif ($0 =~ m@
(.*)/scripts/[^/]*$@
&&
88 top_of_kernel_tree
($1)) {
94 print "Must be run from the top-level dir. of a kernel tree\n";
99 my $emitted_corrupt = 0;
101 our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
102 our $Storage = qr{extern|static|asmlinkage};
116 __
(?
:mem
|cpu
|dev
|)(?
:initdata
|init
)
118 our $Inline = qr{inline|__always_inline|noinline};
119 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
120 our $Lval = qr{$Ident(?:$Member)*};
122 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
123 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
127 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%
146 qr{long\s+long\s+int},
147 qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
152 qr{${Ident}_handler
},
153 qr{${Ident}_handler_fn
},
157 my $all = "(?: \n" . join("|\n ", @typeList) . "\n)";
164 (?
:typeof
|__typeof__
)\s
*\
(\s
*\
**\s
*$Ident\s
*\
)
166 (?
:\s
+$Sparse|\s
+const
)*
171 (?
:\s
*\
*+\s
*const
|\s
*\
*+|(?
:\s
*\
[\s
*\
])+)?
172 (?
:\s
+$Inline|\s
+$Sparse|\s
+$Attribute)*
174 $Declare = qr{(?:$Storage\s+)?$Type};
178 $chk_signoff = 0 if ($file);
180 my @dep_includes = ();
181 my @dep_functions = ();
182 my $removal = "Documentation/feature-removal-schedule.txt";
183 if ($tree && -f
"$root/$removal") {
184 open(REMOVE
, "<$root/$removal") ||
185 die "$P: $removal: open failed - $!\n";
187 if (/^Check:\s+(.*\S)/) {
188 for my $entry (split(/[, ]+/, $1)) {
189 if ($entry =~ m
@include/(.*)@
) {
190 push(@dep_includes, $1);
192 } elsif ($entry !~ m@
/@
) {
193 push(@dep_functions, $entry);
203 for my $filename (@ARGV) {
205 open(FILE
, "diff -u /dev/null $filename|") ||
206 die "$P: $filename: diff failed - $!\n";
208 open(FILE
, "<$filename") ||
209 die "$P: $filename: open failed - $!\n";
211 if ($filename eq '-') {
212 $vname = 'Your patch';
221 if (!process
($filename)) {
230 sub top_of_kernel_tree
{
234 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
235 "README", "Documentation", "arch", "include", "drivers",
236 "fs", "init", "ipc", "kernel", "lib", "scripts",
239 foreach my $check (@tree_check) {
240 if (! -e
$root . '/' . $check) {
252 for my $c (split(//, $str)) {
256 for (; ($n % 8) != 0; $n++) {
268 (my $res = shift) =~ tr/\t/ /c;
275 # Drop the diff line leader and expand tabs
277 $line = expand_tabs
($line);
279 # Pick the indent from the front of the line.
280 my ($white) = ($line =~ /^(\s*)/);
282 return (length($line), length($white));
285 my $sanitise_quote = '';
287 sub sanitise_line_reset
{
288 my ($in_comment) = @_;
291 $sanitise_quote = '*/';
293 $sanitise_quote = '';
306 # Always copy over the diff marker.
307 $res = substr($line, 0, 1);
309 for ($off = 1; $off < length($line); $off++) {
310 $c = substr($line, $off, 1);
312 # Comments we are wacking completly including the begin
313 # and end, all to $;.
314 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
315 $sanitise_quote = '*/';
317 substr($res, $off, 2, "$;$;");
321 if (substr($line, $off, 2) eq $sanitise_quote) {
322 $sanitise_quote = '';
323 substr($res, $off, 2, "$;$;");
328 # A \ in a string means ignore the next character.
329 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
331 substr($res, $off, 2, 'XX');
336 if ($c eq "'" || $c eq '"') {
337 if ($sanitise_quote eq '') {
338 $sanitise_quote = $c;
340 substr($res, $off, 1, $c);
342 } elsif ($sanitise_quote eq $c) {
343 $sanitise_quote = '';
347 #print "SQ:$sanitise_quote\n";
348 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
349 substr($res, $off, 1, $;);
350 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
351 substr($res, $off, 1, 'X');
353 substr($res, $off, 1, $c);
357 # The pathname on a #include may be surrounded by '<' and '>'.
358 if ($res =~ /^.#\s*include\s+\<(.*)\>/) {
359 my $clean = 'X' x
length($1);
360 $res =~ s@\
<.*\
>@
<$clean>@
;
362 # The whole of a #error is a string.
363 } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) {
364 my $clean = 'X' x
length($1);
365 $res =~ s@
(#\s*(?:error|warning)\s+).*@$1$clean@;
371 sub ctx_statement_block
{
372 my ($linenr, $remain, $off) = @_;
373 my $line = $linenr - 1;
389 #warn "CSB: blk<$blk> remain<$remain>\n";
390 # If we are about to drop off the end, pull in more
393 for (; $remain > 0; $line++) {
394 next if ($lines[$line] =~ /^-/);
397 $blk .= $lines[$line] . "\n";
402 # Bail if there is no further context.
403 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
409 $c = substr($blk, $off, 1);
410 $remainder = substr($blk, $off);
412 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
413 # Statement ends at the ';' or a close '}' at the
415 if ($level == 0 && $c eq ';') {
419 # An else is really a conditional as long as its not else if
420 if ($level == 0 && $coff_set == 0 &&
421 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
422 $remainder =~ /^(else)(?:\s|{)/ &&
423 $remainder !~ /^else\s+if\b/) {
424 $coff = $off + length($1) - 1;
426 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
427 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
430 if (($type eq '' || $type eq '(') && $c eq '(') {
434 if ($type eq '(' && $c eq ')') {
436 $type = ($level != 0)?
'(' : '';
438 if ($level == 0 && $coff < $soff) {
441 #warn "CSB: mark coff<$coff>\n";
444 if (($type eq '' || $type eq '{') && $c eq '{') {
448 if ($type eq '{' && $c eq '}') {
450 $type = ($level != 0)?
'{' : '';
463 my $statement = substr($blk, $soff, $off - $soff + 1);
464 my $condition = substr($blk, $soff, $coff - $soff + 1);
466 #warn "STATEMENT<$statement>\n";
467 #warn "CONDITION<$condition>\n";
469 #print "coff<$coff> soff<$off> loff<$loff>\n";
471 return ($statement, $condition,
472 $line, $remain + 1, $off - $loff + 1, $level);
475 sub statement_lines
{
478 # Strip the diff line prefixes and rip blank lines at start and end.
479 $stmt =~ s/(^|\n)./$1/g;
483 my @stmt_lines = ($stmt =~ /\n/g);
485 return $#stmt_lines + 2;
488 sub statement_rawlines
{
491 my @stmt_lines = ($stmt =~ /\n/g);
493 return $#stmt_lines + 2;
496 sub statement_block_size
{
499 $stmt =~ s/(^|\n)./$1/g;
505 my @stmt_lines = ($stmt =~ /\n/g);
506 my @stmt_statements = ($stmt =~ /;/g);
508 my $stmt_lines = $#stmt_lines + 2;
509 my $stmt_statements = $#stmt_statements + 1;
511 if ($stmt_lines > $stmt_statements) {
514 return $stmt_statements;
518 sub ctx_statement_full
{
519 my ($linenr, $remain, $off) = @_;
520 my ($statement, $condition, $level);
524 # Grab the first conditional/block pair.
525 ($statement, $condition, $linenr, $remain, $off, $level) =
526 ctx_statement_block
($linenr, $remain, $off);
527 #print "F: c<$condition> s<$statement> remain<$remain>\n";
528 push(@chunks, [ $condition, $statement ]);
529 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
530 return ($level, $linenr, @chunks);
533 # Pull in the following conditional/block pairs and see if they
534 # could continue the statement.
536 ($statement, $condition, $linenr, $remain, $off, $level) =
537 ctx_statement_block
($linenr, $remain, $off);
538 #print "C: c<$condition> s<$statement> remain<$remain>\n";
539 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
541 push(@chunks, [ $condition, $statement ]);
544 return ($level, $linenr, @chunks);
548 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
550 my $start = $linenr - 1;
557 for ($line = $start; $remain > 0; $line++) {
558 next if ($rawlines[$line] =~ /^-/);
561 $blk .= $rawlines[$line];
562 foreach my $c (split(//, $rawlines[$line])) {
563 ##print "C<$c>L<$level><$open$close>O<$off>\n";
569 if ($c eq $close && $level > 0) {
571 last if ($level == 0);
572 } elsif ($c eq $open) {
577 if (!$outer || $level <= 1) {
578 push(@res, $rawlines[$line]);
581 last if ($level == 0);
584 return ($level, @res);
586 sub ctx_block_outer
{
587 my ($linenr, $remain) = @_;
589 my ($level, @r) = ctx_block_get
($linenr, $remain, 1, '{', '}', 0);
593 my ($linenr, $remain) = @_;
595 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
599 my ($linenr, $remain, $off) = @_;
601 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
604 sub ctx_block_level
{
605 my ($linenr, $remain) = @_;
607 return ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
609 sub ctx_statement_level
{
610 my ($linenr, $remain, $off) = @_;
612 return ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
615 sub ctx_locate_comment
{
616 my ($first_line, $end_line) = @_;
618 # Catch a comment on the end of the line itself.
619 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@
.*(/\*.*\*/)\s
*$@
);
620 return $current_comment if (defined $current_comment);
622 # Look through the context and try and figure out if there is a
625 $current_comment = '';
626 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
627 my $line = $rawlines[$linenr - 1];
629 if ($linenr == $first_line and $line =~ m@
^.\s
*\
*@
) {
632 if ($line =~ m@
/\
*@
) {
635 if (!$in_comment && $current_comment ne '') {
636 $current_comment = '';
638 $current_comment .= $line . "\n" if ($in_comment);
639 if ($line =~ m@\
*/@
) {
644 chomp($current_comment);
645 return($current_comment);
647 sub ctx_has_comment
{
648 my ($first_line, $end_line) = @_;
649 my $cmt = ctx_locate_comment
($first_line, $end_line);
651 ##print "LINE: $rawlines[$end_line - 1 ]\n";
652 ##print "CMMT: $cmt\n";
662 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
665 $coded = sprintf("^%c", unpack('C', $2) + 64);
674 my $av_preprocessor = 0;
679 $av_preprocessor = 0;
681 @av_paren_type = ('E');
684 sub annotate_values
{
685 my ($stream, $type) = @_;
690 print "$stream\n" if ($dbg_values > 1);
692 while (length($cur)) {
693 @av_paren_type = ('E') if ($#av_paren_type < 0);
694 print " <" . join('', @av_paren_type) .
695 "> <$type> " if ($dbg_values > 1);
696 if ($cur =~ /^(\s+)/o) {
697 print "WS($1)\n" if ($dbg_values > 1);
698 if ($1 =~ /\n/ && $av_preprocessor) {
699 $type = pop(@av_paren_type);
700 $av_preprocessor = 0;
703 } elsif ($cur =~ /^($Type)/) {
704 print "DECLARE($1)\n" if ($dbg_values > 1);
707 } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
708 print "DEFINE($1)\n" if ($dbg_values > 1);
709 $av_preprocessor = 1;
712 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if))/o) {
713 print "PRE_START($1)\n" if ($dbg_values > 1);
714 $av_preprocessor = 1;
716 push(@av_paren_type, $type);
717 push(@av_paren_type, $type);
720 } elsif ($cur =~ /^(#\s*(?:else|elif))/o) {
721 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
722 $av_preprocessor = 1;
724 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
728 } elsif ($cur =~ /^(#\s*(?:endif))/o) {
729 print "PRE_END($1)\n" if ($dbg_values > 1);
731 $av_preprocessor = 1;
733 # Assume all arms of the conditional end as this
734 # one does, and continue as if the #endif was not here.
736 push(@av_paren_type, $type);
739 } elsif ($cur =~ /^(\\\n)/o) {
740 print "PRECONT($1)\n" if ($dbg_values > 1);
742 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
743 print "SIZEOF($1)\n" if ($dbg_values > 1);
749 } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
750 print "COND($1)\n" if ($dbg_values > 1);
754 } elsif ($cur =~/^(return|case|else)/o) {
755 print "KEYWORD($1)\n" if ($dbg_values > 1);
758 } elsif ($cur =~ /^(\()/o) {
759 print "PAREN('$1')\n" if ($dbg_values > 1);
760 push(@av_paren_type, $av_pending);
764 } elsif ($cur =~ /^(\))/o) {
765 my $new_type = pop(@av_paren_type);
766 if ($new_type ne '_') {
768 print "PAREN('$1') -> $type\n"
769 if ($dbg_values > 1);
771 print "PAREN('$1')\n" if ($dbg_values > 1);
774 } elsif ($cur =~ /^($Ident)\(/o) {
775 print "FUNC($1)\n" if ($dbg_values > 1);
778 } elsif ($cur =~ /^($Ident|$Constant)/o) {
779 print "IDENT($1)\n" if ($dbg_values > 1);
782 } elsif ($cur =~ /^($Assignment)/o) {
783 print "ASSIGN($1)\n" if ($dbg_values > 1);
786 } elsif ($cur =~/^(;|{|})/) {
787 print "END($1)\n" if ($dbg_values > 1);
790 } elsif ($cur =~ /^(;|\?|:|\[)/o) {
791 print "CLOSE($1)\n" if ($dbg_values > 1);
794 } elsif ($cur =~ /^($Operators)/o) {
795 print "OP($1)\n" if ($dbg_values > 1);
796 if ($1 ne '++' && $1 ne '--') {
800 } elsif ($cur =~ /(^.)/o) {
801 print "C($1)\n" if ($dbg_values > 1);
804 $cur = substr($cur, length($1));
805 $res .= $type x
length($1);
813 my ($possible, $line) = @_;
815 #print "CHECK<$possible>\n";
816 if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
817 $possible ne 'goto' && $possible ne 'return' &&
818 $possible ne 'struct' && $possible ne 'enum' &&
819 $possible ne 'case' && $possible ne 'else' &&
820 $possible ne 'typedef') {
821 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
822 push(@typeList, $possible);
830 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
833 my $line = $prefix . $_[0];
835 $line = (split('\n', $line))[0] . "\n" if ($terse);
837 push(our @report, $line);
845 if (report
("ERROR: $_[0]\n")) {
851 if (report
("WARNING: $_[0]\n")) {
857 if ($check && report
("CHECK: $_[0]\n")) {
864 my $filename = shift;
887 # Trace the real file/line as we go.
893 my $comment_edge = 0;
896 my $prev_values = 'E';
899 my %suppress_ifbraces;
901 # Pre-scan the patch sanitizing the lines.
902 # Pre-scan the patch looking for any __setup documentation.
907 sanitise_line_reset
();
909 foreach my $rawline (@rawlines) {
913 if ($rawline=~/^\+\+\+\s+(\S+)/) {
915 if ($1 =~ m
@Documentation/kernel
-parameters
.txt
$@
) {
920 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
928 # Guestimate if this is a continuing comment. Run
929 # the context looking for a comment "edge". If this
930 # edge is a close comment then we must be in a comment
933 for (my $ln = $linenr; $ln < ($linenr + $realcnt); $ln++) {
934 next if ($line =~ /^-/);
935 ($edge) = ($rawlines[$ln - 1] =~ m@
(/\*|\*/)@
);
936 last if (defined $edge);
938 if (defined $edge && $edge eq '*/') {
942 # Guestimate if this is a continuing comment. If this
943 # is the start of a diff block and this line starts
944 # ' *' then it is very likely a comment.
945 if (!defined $edge &&
946 $rawlines[$linenr] =~ m@
^.\s
* \
*(?
:\s
|$)@
)
951 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
952 sanitise_line_reset
($in_comment);
955 # Standardise the strings and chars within the input to
957 $line = sanitise_line
($rawline);
962 $realcnt-- if ($line =~ /^(?:\+| |$)/);
967 #print "==>$rawline\n";
970 if ($setup_docs && $line =~ /^\+/) {
971 push(@setup_docs, $line);
979 foreach my $line (@lines) {
982 my $rawline = $rawlines[$linenr - 1];
984 #extract the line range in the file after the patch is applied
985 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
987 $first_line = $linenr + 1;
997 %suppress_ifbraces = ();
1000 # track the line number as we move through the hunk, note that
1001 # new versions of GNU diff omit the leading space on completely
1002 # blank context lines so we need to count that too.
1003 } elsif ($line =~ /^( |\+|$)/) {
1005 $realcnt-- if ($realcnt != 0);
1007 # Measure the line length and indent.
1008 ($length, $indent) = line_stats
($rawline);
1010 # Track the previous line.
1011 ($prevline, $stashline) = ($stashline, $line);
1012 ($previndent, $stashindent) = ($stashindent, $indent);
1013 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1015 #warn "line<$line>\n";
1017 } elsif ($realcnt == 1) {
1021 #make up the handle for any error we report on this line
1022 $prefix = "$filename:$realline: " if ($emacs && $file);
1023 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1025 $here = "#$linenr: " if (!$file);
1026 $here = "#$realline: " if ($file);
1028 # extract the filename as it passes
1029 if ($line=~/^\+\+\+\s+(\S+)/) {
1031 $realfile =~ s@
^[^/]*/@@
;
1033 if ($realfile =~ m
@include/asm/@
) {
1034 ERROR
("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1039 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1041 my $hereline = "$here\n$rawline\n";
1042 my $herecurr = "$here\n$rawline\n";
1043 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1045 $cnt_lines++ if ($realcnt != 0);
1047 #check the patch for a signoff:
1048 if ($line =~ /^\s*signed-off-by:/i) {
1049 # This is a signoff, if ugly, so do not double report.
1051 if (!($line =~ /^\s*Signed-off-by:/)) {
1052 WARN
("Signed-off-by: is the preferred form\n" .
1055 if ($line =~ /^\s*signed-off-by:\S/i) {
1056 WARN
("space required after Signed-off-by:\n" .
1061 # Check for wrappage within a valid hunk of the file
1062 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1063 ERROR
("patch seems to be corrupt (line wrapped?)\n" .
1064 $herecurr) if (!$emitted_corrupt++);
1067 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1068 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1070 [\x09\x0A\x0D\x20-\x7E] # ASCII
1071 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
1072 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
1073 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
1074 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
1075 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
1076 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
1077 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
1079 ERROR
("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $herecurr);
1082 #ignore lines being removed
1083 if ($line=~/^-/) {next;}
1085 # check we are in a valid source file if not then ignore this hunk
1086 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1088 #trailing whitespace
1089 if ($line =~ /^\+.*\015/) {
1090 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1091 ERROR
("DOS line endings\n" . $herevet);
1093 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1094 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1095 ERROR
("trailing whitespace\n" . $herevet);
1098 if ($line =~ /^\+/ && !($prevrawline=~/\/\
*\
*/) && $length > 80) {
1099 WARN
("line over 80 characters\n" . $herecurr);
1102 # check for adding lines without a newline.
1103 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1104 WARN
("adding a line without newline at end of file\n" . $herecurr);
1107 # check we are in a valid source file *.[hc] if not then ignore this hunk
1108 next if ($realfile !~ /\.[hc]$/);
1110 # at the beginning of a line any tabs must come first and anything
1111 # more than 8 must use tabs.
1112 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1113 $rawline =~ /^\+\s* \s*/) {
1114 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1115 ERROR
("use tabs not spaces\n" . $herevet);
1118 # check for RCS/CVS revision markers
1119 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1120 WARN
("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1123 # Check for potential 'bare' types
1125 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
1129 # Ignore goto labels.
1130 if ($s =~ /$Ident:\*$/) {
1132 # Ignore functions being called
1133 } elsif ($s =~ /^.\s*$Ident\s*\(/) {
1135 # definitions in global scope can only start with types
1136 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) {
1139 # declarations always start with types
1140 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/) {
1144 # any (foo ... *) is a pointer cast, and foo is a type
1145 while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) {
1149 # Check for any sort of function declaration.
1150 # int foo(something bar, other baz);
1151 # void (*store_gdt)(x86_descr_ptr *);
1152 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) {
1153 my ($name_len) = length($1);
1156 substr($ctx, 0, $name_len + 1, '');
1157 $ctx =~ s/\)[^\)]*$//;
1159 for my $arg (split(/\s*,\s*/, $ctx)) {
1160 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
1170 # Checks which may be anchored in the context.
1173 # Check for switch () and associated case and default
1174 # statements should be at the same indent.
1175 if ($line=~/\bswitch\s*\(.*\)/) {
1178 my @ctx = ctx_block_outer
($linenr, $realcnt);
1180 for my $ctx (@ctx) {
1181 my ($clen, $cindent) = line_stats
($ctx);
1182 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1183 $indent != $cindent) {
1184 $err .= "$sep$ctx\n";
1191 ERROR
("switch and case should be at the same indent\n$hereline$err");
1195 # if/while/etc brace do not go on next line, unless defining a do while loop,
1196 # or if that brace on the next line is for something else
1197 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
1198 my $pre_ctx = "$1$2";
1200 my ($level, @ctx) = ctx_statement_level
($linenr, $realcnt, 0);
1201 my $ctx_ln = $linenr + $#ctx + 1;
1202 my $ctx_cnt = $realcnt - $#ctx - 1;
1203 my $ctx = join("\n", @ctx);
1205 ##warn "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1207 # Skip over any removed lines in the context following statement.
1208 while (defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^-/) {
1211 ##warn "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1213 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1214 ERROR
("that open brace { should be on the previous line\n" .
1215 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1217 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1218 $ctx =~ /\)\s*\;\s*$/ &&
1219 defined $lines[$ctx_ln - 1])
1221 my ($nlength, $nindent) = line_stats
($lines[$ctx_ln - 1]);
1222 if ($nindent > $indent) {
1223 WARN
("trailing semicolon indicates no statements, indent implies otherwise\n" .
1224 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1229 # Track the 'values' across context and added lines.
1230 my $opline = $line; $opline =~ s/^./ /;
1231 my $curr_values = annotate_values
($opline . "\n", $prev_values);
1232 $curr_values = $prev_values . $curr_values;
1234 my $outline = $opline; $outline =~ s/\t/ /g;
1235 print "$linenr > .$outline\n";
1236 print "$linenr > $curr_values\n";
1238 $prev_values = substr($curr_values, -1);
1240 #ignore lines not being added
1241 if ($line=~/^[^\+]/) {next;}
1243 # TEST: allow direct testing of the type matcher.
1244 if ($tst_type && $line =~ /^.$Declare$/) {
1245 ERROR
("TEST: is type $Declare\n" . $herecurr);
1249 # check for initialisation to aggregates open brace on the next line
1250 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1251 $line =~ /^.\s*{/) {
1252 ERROR
("that open brace { should be on the previous line\n" . $hereprev);
1256 # Checks which are anchored on the added line.
1259 # check for malformed paths in #include statements (uses RAW line)
1260 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
1262 if ($path =~ m{//}) {
1263 ERROR
("malformed #include filename\n" .
1268 # no C99 // comments
1269 if ($line =~ m{//}) {
1270 ERROR
("do not use C99 // comments\n" . $herecurr);
1272 # Remove C99 comments.
1274 $opline =~ s@
//.*@@
;
1276 #EXPORT_SYMBOL should immediately follow its function closing }.
1277 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1278 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1280 if (($prevline !~ /^}/) &&
1281 ($prevline !~ /^\+}/) &&
1282 ($prevline !~ /^ }/) &&
1283 ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1284 ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
1285 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
1286 WARN
("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1290 # check for external initialisers.
1291 if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
1292 ERROR
("do not initialise externals to 0 or NULL\n" .
1295 # check for static initialisers.
1296 if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
1297 ERROR
("do not initialise statics to 0 or NULL\n" .
1301 # check for new typedefs, only function parameters and sparse annotations
1303 if ($line =~ /\btypedef\s/ &&
1304 $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1305 $line !~ /\b__bitwise(?:__|)\b/) {
1306 WARN
("do not add new typedefs\n" . $herecurr);
1309 # * goes on variable not on type
1310 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
1311 ERROR
("\"(foo$1)\" should be \"(foo $1)\"\n" .
1314 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
1315 ERROR
("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1318 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
1319 ERROR
("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1322 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
1323 ERROR
("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1327 # # no BUG() or BUG_ON()
1328 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1329 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1330 # print "$herecurr";
1334 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1335 WARN
("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1338 # printk should use KERN_* levels. Note that follow on printk's on the
1339 # same line do not need a level, so we use the current block context
1340 # to try and find and validate the current printk. In summary the current
1341 # printk includes all preceeding printk's which have no newline on the end.
1342 # we assume the first bad printk is the one to report.
1343 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1345 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1346 #print "CHECK<$lines[$ln - 1]\n";
1347 # we have a preceeding printk if it ends
1348 # with "\n" ignore it, else it is to blame
1349 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1350 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1357 WARN
("printk() should include KERN_ facility level\n" . $herecurr);
1361 # function brace can't be on same line, except for #defines of do while,
1362 # or if closed on same line
1363 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and
1364 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
1365 ERROR
("open brace '{' following function declarations go on the next line\n" . $herecurr);
1368 # open braces for enum, union and struct go on the same line.
1369 if ($line =~ /^.\s*{/ &&
1370 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1371 ERROR
("open brace '{' following $1 go on the same line\n" . $hereprev);
1374 # check for spaces between functions and their parentheses.
1375 while ($line =~ /($Ident)\s+\(/g) {
1377 my $ctx_before = substr($line, 0, $-[1]);
1378 my $ctx = "$ctx_before$name";
1380 # Ignore those directives where spaces _are_ permitted.
1382 if|for|while|switch
|return|case
|
1383 volatile
|__volatile__
|
1384 __attribute__
|format
|__extension__
|
1388 # cpp #define statements have non-optional spaces, ie
1389 # if there is a space between the name and the open
1390 # parenthesis it is simply not a parameter group.
1391 } elsif ($ctx_before =~ /^.\#\s*define\s*$/) {
1393 # cpp #elif statement condition may start with a (
1394 } elsif ($ctx =~ /^.\#\s*elif\s*$/) {
1396 # If this whole things ends with a type its most
1397 # likely a typedef for a function.
1398 } elsif ($ctx =~ /$Type$/) {
1401 WARN
("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1404 # Check operator spacing.
1405 if (!($line=~/\#\s*include/)) {
1407 <<=|>>=|<=|>=|==|!=|
1408 \
+=|-=|\
*=|\
/=|%=|\
^=|\
|=|&=|
1409 =>|->|<<|>>|<|>|=|!|~|
1410 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%
1412 my @elements = split(/($ops|;)/, $opline);
1415 my $blank = copy_spacing
($opline);
1417 for (my $n = 0; $n < $#elements; $n += 2) {
1418 $off += length($elements[$n]);
1420 # Pick up the preceeding and succeeding characters.
1421 my $ca = substr($opline, 0, $off);
1423 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1424 $cc = substr($opline, $off + length($elements[$n + 1]));
1426 my $cb = "$ca$;$cc";
1429 $a = 'V' if ($elements[$n] ne '');
1430 $a = 'W' if ($elements[$n] =~ /\s$/);
1431 $a = 'C' if ($elements[$n] =~ /$;$/);
1432 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1433 $a = 'O' if ($elements[$n] eq '');
1434 $a = 'E' if ($ca =~ /^\s*$/);
1436 my $op = $elements[$n + 1];
1439 if (defined $elements[$n + 2]) {
1440 $c = 'V' if ($elements[$n + 2] ne '');
1441 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1442 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1443 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1444 $c = 'O' if ($elements[$n + 2] eq '');
1445 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
1450 my $ctx = "${a}x${c}";
1452 my $at = "(ctx:$ctx)";
1454 my $ptr = substr($blank, 0, $off) . "^";
1455 my $hereptr = "$hereline$ptr\n";
1457 # Classify operators into binary, unary, or
1458 # definitions (* only) where they have more
1460 my $op_type = substr($curr_values, $off + 1, 1);
1461 my $op_left = substr($curr_values, $off, 1);
1463 if ($op_type eq 'T') {
1465 } elsif ($op_left eq 'V') {
1470 #if ($op eq '-' || $op eq '&' || $op eq '*') {
1471 # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
1474 # Ignore operators passed as parameters.
1475 if ($op_type ne 'V' &&
1476 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1479 # } elsif ($op =~ /^$;+$/) {
1481 # ; should have either the end of line or a space or \ after it
1482 } elsif ($op eq ';') {
1483 if ($ctx !~ /.x[WEBC]/ &&
1484 $cc !~ /^\\/ && $cc !~ /^;/) {
1485 ERROR
("space required after that '$op' $at\n" . $hereptr);
1489 } elsif ($op eq '//') {
1491 # -> should have no spaces
1492 } elsif ($op eq '->') {
1493 if ($ctx =~ /Wx.|.xW/) {
1494 ERROR
("spaces prohibited around that '$op' $at\n" . $hereptr);
1497 # , must have a space on the right.
1498 } elsif ($op eq ',') {
1499 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1500 ERROR
("space required after that '$op' $at\n" . $hereptr);
1503 # '*' as part of a type definition -- reported already.
1504 } elsif ($op eq '*' && $is_unary == 2) {
1505 #warn "'*' is part of type\n";
1507 # unary operators should have a space before and
1508 # none after. May be left adjacent to another
1509 # unary operator, or a cast
1510 } elsif ($op eq '!' || $op eq '~' ||
1511 ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
1512 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1513 ERROR
("space required before that '$op' $at\n" . $hereptr);
1515 if ($ctx =~ /.xW/) {
1516 ERROR
("space prohibited after that '$op' $at\n" . $hereptr);
1519 # unary ++ and unary -- are allowed no space on one side.
1520 } elsif ($op eq '++' or $op eq '--') {
1521 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1522 ERROR
("space required one side of that '$op' $at\n" . $hereptr);
1524 if ($ctx =~ /Wx[BE]/ ||
1525 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1526 ERROR
("space prohibited before that '$op' $at\n" . $hereptr);
1528 if ($ctx =~ /ExW/) {
1529 ERROR
("space prohibited after that '$op' $at\n" . $hereptr);
1533 # << and >> may either have or not have spaces both sides
1534 } elsif ($op eq '<<' or $op eq '>>' or
1535 $op eq '&' or $op eq '^' or $op eq '|' or
1536 $op eq '+' or $op eq '-' or
1537 $op eq '*' or $op eq '/' or
1540 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1541 ERROR
("need consistent spacing around '$op' $at\n" .
1545 # All the others need spaces both sides.
1546 } elsif ($ctx !~ /[EWC]x[CWE]/) {
1547 # Ignore email addresses <foo@bar>
1548 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
1549 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
1550 ERROR
("spaces required around that '$op' $at\n" . $hereptr);
1553 $off += length($elements[$n + 1]);
1557 # check for multiple assignments
1558 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
1559 CHK
("multiple assignments should be avoided\n" . $herecurr);
1562 ## # check for multiple declarations, allowing for a function declaration
1564 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1565 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1567 ## # Remove any bracketed sections to ensure we do not
1568 ## # falsly report the parameters of functions.
1570 ## while ($ln =~ s/\([^\(\)]*\)//g) {
1572 ## if ($ln =~ /,/) {
1573 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1577 #need space before brace following if, while, etc
1578 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1580 ERROR
("space required before the open brace '{'\n" . $herecurr);
1583 # closing brace should have a space following it when it has anything
1585 if ($line =~ /}(?!(?:,|;|\)))\S/) {
1586 ERROR
("space required after that close brace '}'\n" . $herecurr);
1589 # check spacing on square brackets
1590 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1591 ERROR
("space prohibited after that open square bracket '['\n" . $herecurr);
1593 if ($line =~ /\s\]/) {
1594 ERROR
("space prohibited before that close square bracket ']'\n" . $herecurr);
1597 # check spacing on paretheses
1598 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1599 $line !~ /for\s*\(\s+;/) {
1600 ERROR
("space prohibited after that open parenthesis '('\n" . $herecurr);
1602 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1603 $line !~ /for\s*\(.*;\s+\)/) {
1604 ERROR
("space prohibited before that close parenthesis ')'\n" . $herecurr);
1607 #goto labels aren't indented, allow a single space however
1608 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
1609 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1610 WARN
("labels should not be indented\n" . $herecurr);
1613 # Need a space before open parenthesis after if, while etc
1614 if ($line=~/\b(if|while|for|switch)\(/) {
1615 ERROR
("space required before the open parenthesis '('\n" . $herecurr);
1618 # Check for illegal assignment in if conditional.
1619 if ($line =~ /\bif\s*\(/) {
1620 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
1622 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
1623 ERROR
("do not use assignment in if condition\n" . $herecurr);
1626 # Find out what is on the end of the line after the
1628 substr($s, 0, length($c), '');
1630 $s =~ s/$;//g; # Remove any comments
1631 if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/ &&
1634 ERROR
("trailing statements should be on next line\n" . $herecurr);
1638 # Check for bitwise tests written as boolean
1650 WARN
("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
1653 # if and else should not have general statements after it
1654 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
1656 $s =~ s/$;//g; # Remove any comments
1657 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
1658 ERROR
("trailing statements should be on next line\n" . $herecurr);
1662 # Check for }<nl>else {, these must be at the same
1663 # indent level to be relevant to each other.
1664 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
1665 $previndent == $indent) {
1666 ERROR
("else should follow close brace '}'\n" . $hereprev);
1669 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1670 $previndent == $indent) {
1671 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
1673 # Find out what is on the end of the line after the
1675 substr($s, 0, length($c), '');
1678 if ($s =~ /^\s*;/) {
1679 ERROR
("while should follow close brace '}'\n" . $hereprev);
1683 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
1684 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
1685 # print "No studly caps, use _\n";
1686 # print "$herecurr";
1690 #no spaces allowed after \ in define
1691 if ($line=~/\#define.*\\\s$/) {
1692 WARN
("Whitepspace after \\ makes next lines useless\n" . $herecurr);
1695 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1696 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
1697 my $checkfile = "$root/include/linux/$1.h";
1698 if (-f
$checkfile && $1 ne 'irq.h') {
1699 WARN
("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
1704 # multi-statement macros should be enclosed in a do while loop, grab the
1705 # first statement and ensure its the whole macro if its not enclosed
1706 # in a known good container
1707 if ($prevline =~ /\#define.*\\/ &&
1708 $prevline !~/(?:do\s+{|\(\{|\{)/ &&
1709 $line !~ /(?:do\s+{|\(\{|\{)/ &&
1710 $line !~ /^.\s*$Declare\s/) {
1711 # Grab the first statement, if that is the entire macro
1712 # its ok. This may start either on the #define line
1718 # If the macro starts on the define line start
1719 # grabbing the statement after the identifier
1720 $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
1721 ##print "1<$1> 2<$2>\n";
1722 if (defined $2 && $2 ne '') {
1726 while ($lines[$ln - 1] =~ /^-/) {
1731 my @ctx = ctx_statement
($ln, $cnt, $off);
1732 my $ctx_ln = $ln + $#ctx + 1;
1733 my $ctx = join("\n", @ctx);
1735 # Pull in any empty extension lines.
1736 while ($ctx =~ /\\$/ &&
1737 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
1738 $ctx .= $lines[$ctx_ln - 1];
1742 if ($ctx =~ /\\$/) {
1744 ERROR
("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
1746 ERROR
("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
1751 # check for redundant bracing round if etc
1752 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
1753 my ($level, $endln, @chunks) =
1754 ctx_statement_full
($linenr, $realcnt, 1);
1755 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
1756 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
1757 if ($#chunks > 0 && $level == 0) {
1760 my $herectx = $here . "\n";
1761 my $ln = $linenr - 1;
1762 for my $chunk (@chunks) {
1763 my ($cond, $block) = @
{$chunk};
1765 # If the condition carries leading newlines, then count those as offsets.
1766 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
1767 my $offset = statement_rawlines
($whitespace) - 1;
1769 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
1771 # We have looked at and allowed this specific line.
1772 $suppress_ifbraces{$ln + $offset} = 1;
1774 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
1775 $ln += statement_rawlines
($block) - 1;
1777 substr($block, 0, length($cond), '');
1779 $seen++ if ($block =~ /^\s*{/);
1781 #print "cond<$cond> block<$block> allowed<$allowed>\n";
1782 if (statement_lines
($cond) > 1) {
1783 #print "APW: ALLOWED: cond<$cond>\n";
1786 if ($block =~/\b(?:if|for|while)\b/) {
1787 #print "APW: ALLOWED: block<$block>\n";
1790 if (statement_block_size
($block) > 1) {
1791 #print "APW: ALLOWED: lines block<$block>\n";
1795 if ($seen && !$allowed) {
1796 WARN
("braces {} are not necessary for any arm of this statement\n" . $herectx);
1800 if (!defined $suppress_ifbraces{$linenr - 1} &&
1801 $line =~ /\b(if|while|for|else)\b/) {
1804 # Check the pre-context.
1805 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
1806 #print "APW: ALLOWED: pre<$1>\n";
1810 my ($level, $endln, @chunks) =
1811 ctx_statement_full
($linenr, $realcnt, $-[0]);
1813 # Check the condition.
1814 my ($cond, $block) = @
{$chunks[0]};
1815 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
1816 if (defined $cond) {
1817 substr($block, 0, length($cond), '');
1819 if (statement_lines
($cond) > 1) {
1820 #print "APW: ALLOWED: cond<$cond>\n";
1823 if ($block =~/\b(?:if|for|while)\b/) {
1824 #print "APW: ALLOWED: block<$block>\n";
1827 if (statement_block_size
($block) > 1) {
1828 #print "APW: ALLOWED: lines block<$block>\n";
1831 # Check the post-context.
1832 if (defined $chunks[1]) {
1833 my ($cond, $block) = @
{$chunks[1]};
1834 if (defined $cond) {
1835 substr($block, 0, length($cond), '');
1837 if ($block =~ /^\s*\{/) {
1838 #print "APW: ALLOWED: chunk-1 block<$block>\n";
1842 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
1843 my $herectx = $here . "\n";;
1844 my $end = $linenr + statement_rawlines
($block) - 1;
1846 for (my $ln = $linenr - 1; $ln < $end; $ln++) {
1847 $herectx .= $rawlines[$ln] . "\n";;
1850 WARN
("braces {} are not necessary for single statement blocks\n" . $herectx);
1854 # don't include deprecated include files (uses RAW line)
1855 for my $inc (@dep_includes) {
1856 if ($rawline =~ m@\#\s
*include\s
*\
<$inc>@
) {
1857 ERROR
("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
1861 # don't use deprecated functions
1862 for my $func (@dep_functions) {
1863 if ($line =~ /\b$func\b/) {
1864 ERROR
("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
1868 # no volatiles please
1869 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
1870 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
1871 WARN
("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
1874 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
1875 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
1876 ERROR
("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
1880 if ($line =~ /^.#\s*if\s+0\b/) {
1881 CHK
("if this code is redundant consider removing it\n" .
1885 # check for needless kfree() checks
1886 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1888 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1889 WARN
("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1893 # warn about #ifdefs in C files
1894 # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
1895 # print "#ifdef in C files should be avoided\n";
1896 # print "$herecurr";
1900 # warn about spacing in #ifdefs
1901 if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
1902 ERROR
("exactly one space required after that #$1\n" . $herecurr);
1905 # check for spinlock_t definitions without a comment.
1906 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
1908 if (!ctx_has_comment
($first_line, $linenr)) {
1909 CHK
("$1 definition without comment\n" . $herecurr);
1912 # check for memory barriers without a comment.
1913 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
1914 if (!ctx_has_comment
($first_line, $linenr)) {
1915 CHK
("memory barrier without comment\n" . $herecurr);
1918 # check of hardware specific defines
1919 if ($line =~ m@
^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
1920 CHK
("architecture specific defines should be avoided\n" . $herecurr);
1923 # check the location of the inline attribute, that it is between
1924 # storage class and type.
1925 if ($line =~ /\b$Type\s+$Inline\b/ ||
1926 $line =~ /\b$Inline\s+$Storage\b/) {
1927 ERROR
("inline keyword should sit between storage class and type\n" . $herecurr);
1930 # Check for __inline__ and __inline, prefer inline
1931 if ($line =~ /\b(__inline__|__inline)\b/) {
1932 WARN
("plain inline is preferred over $1\n" . $herecurr);
1935 # check for new externs in .c files.
1936 if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
1937 WARN
("externs should be avoided in .c files\n" . $herecurr);
1940 # checks for new __setup's
1941 if ($rawline =~ /\b__setup\("([^"]*)"/) {
1944 if (!grep(/$name/, @setup_docs)) {
1945 CHK
("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
1949 # check for pointless casting of kmalloc return
1950 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
1951 WARN
("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
1954 # check for gcc specific __FUNCTION__
1955 if ($line =~ /__FUNCTION__/) {
1956 WARN
("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
1959 # check for semaphores used as mutexes
1960 if ($line =~ /\b(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
1961 WARN
("mutexes are preferred for single holder semaphores\n" . $herecurr);
1963 # check for semaphores used as mutexes
1964 if ($line =~ /\binit_MUTEX_LOCKED\s*\(/) {
1965 WARN
("consider using a completion\n" . $herecurr);
1967 # recommend strict_strto* over simple_strto*
1968 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
1969 WARN
("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
1972 # use of NR_CPUS is usually wrong
1973 # ignore definitions of NR_CPUS and usage to define arrays as likely right
1974 if ($line =~ /\bNR_CPUS\b/ &&
1975 $line !~ /^.#\s*define\s+NR_CPUS\s+/ &&
1976 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/)
1978 WARN
("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
1982 # If we have no input at all, then there is nothing to report on
1983 # so just keep quiet.
1984 if ($#rawlines == -1) {
1988 # In mailback mode only produce a report in the negative, for
1989 # things that appear to be patches.
1990 if ($mailback && ($clean == 1 || !$is_patch)) {
1994 # This is not a patch, and we are are in 'no-patch' mode so
1996 if (!$chk_patch && !$is_patch) {
2001 ERROR
("Does not appear to be a unified-diff format patch\n");
2003 if ($is_patch && $chk_signoff && $signoff == 0) {
2004 ERROR
("Missing Signed-off-by: line(s)\n");
2007 print report_dump
();
2008 if ($summary && !($clean == 1 && $quiet == 1)) {
2009 print "$filename " if ($summary_file);
2010 print "total: $cnt_error errors, $cnt_warn warnings, " .
2011 (($check)?
"$cnt_chk checks, " : "") .
2012 "$cnt_lines lines checked\n";
2013 print "\n" if ($quiet == 0);
2016 if ($clean == 1 && $quiet == 0) {
2017 print "$vname has no obvious style problems and is ready for submission.\n"
2019 if ($clean == 0 && $quiet == 0) {
2020 print "$vname has style problems, please review. If any of these errors\n";
2021 print "are false positives report them to the maintainer, see\n";
2022 print "CHECKPATCH in MAINTAINERS.\n";