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 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%
135 [\x09\x0A\x0D\x20-\x7E] # ASCII
136 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
137 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
138 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
139 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
140 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
141 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
142 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
157 qr{long\s+long\s+int},
158 qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
163 qr{${Ident}_handler
},
164 qr{${Ident}_handler_fn
},
168 my $all = "(?: \n" . join("|\n ", @typeList) . "\n)";
175 (?
:typeof
|__typeof__
)\s
*\
(\s
*\
**\s
*$Ident\s
*\
)
177 (?
:\s
+$Sparse|\s
+const
)*
182 (?
:\s
*\
*+\s
*const
|\s
*\
*+|(?
:\s
*\
[\s
*\
])+)?
183 (?
:\s
+$Inline|\s
+$Sparse|\s
+$Attribute)*
185 $Declare = qr{(?:$Storage\s+)?$Type};
189 $chk_signoff = 0 if ($file);
191 my @dep_includes = ();
192 my @dep_functions = ();
193 my $removal = "Documentation/feature-removal-schedule.txt";
194 if ($tree && -f
"$root/$removal") {
195 open(REMOVE
, "<$root/$removal") ||
196 die "$P: $removal: open failed - $!\n";
198 if (/^Check:\s+(.*\S)/) {
199 for my $entry (split(/[, ]+/, $1)) {
200 if ($entry =~ m
@include/(.*)@
) {
201 push(@dep_includes, $1);
203 } elsif ($entry !~ m@
/@
) {
204 push(@dep_functions, $entry);
214 for my $filename (@ARGV) {
216 open(FILE
, "diff -u /dev/null $filename|") ||
217 die "$P: $filename: diff failed - $!\n";
219 open(FILE
, "<$filename") ||
220 die "$P: $filename: open failed - $!\n";
222 if ($filename eq '-') {
223 $vname = 'Your patch';
232 if (!process
($filename)) {
241 sub top_of_kernel_tree
{
245 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
246 "README", "Documentation", "arch", "include", "drivers",
247 "fs", "init", "ipc", "kernel", "lib", "scripts",
250 foreach my $check (@tree_check) {
251 if (! -e
$root . '/' . $check) {
263 for my $c (split(//, $str)) {
267 for (; ($n % 8) != 0; $n++) {
279 (my $res = shift) =~ tr/\t/ /c;
286 # Drop the diff line leader and expand tabs
288 $line = expand_tabs
($line);
290 # Pick the indent from the front of the line.
291 my ($white) = ($line =~ /^(\s*)/);
293 return (length($line), length($white));
296 my $sanitise_quote = '';
298 sub sanitise_line_reset
{
299 my ($in_comment) = @_;
302 $sanitise_quote = '*/';
304 $sanitise_quote = '';
317 # Always copy over the diff marker.
318 $res = substr($line, 0, 1);
320 for ($off = 1; $off < length($line); $off++) {
321 $c = substr($line, $off, 1);
323 # Comments we are wacking completly including the begin
324 # and end, all to $;.
325 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
326 $sanitise_quote = '*/';
328 substr($res, $off, 2, "$;$;");
332 if (substr($line, $off, 2) eq $sanitise_quote) {
333 $sanitise_quote = '';
334 substr($res, $off, 2, "$;$;");
339 # A \ in a string means ignore the next character.
340 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
342 substr($res, $off, 2, 'XX');
347 if ($c eq "'" || $c eq '"') {
348 if ($sanitise_quote eq '') {
349 $sanitise_quote = $c;
351 substr($res, $off, 1, $c);
353 } elsif ($sanitise_quote eq $c) {
354 $sanitise_quote = '';
358 #print "SQ:$sanitise_quote\n";
359 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
360 substr($res, $off, 1, $;);
361 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
362 substr($res, $off, 1, 'X');
364 substr($res, $off, 1, $c);
368 # The pathname on a #include may be surrounded by '<' and '>'.
369 if ($res =~ /^.#\s*include\s+\<(.*)\>/) {
370 my $clean = 'X' x
length($1);
371 $res =~ s@\
<.*\
>@
<$clean>@
;
373 # The whole of a #error is a string.
374 } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) {
375 my $clean = 'X' x
length($1);
376 $res =~ s@
(#\s*(?:error|warning)\s+).*@$1$clean@;
382 sub ctx_statement_block
{
383 my ($linenr, $remain, $off) = @_;
384 my $line = $linenr - 1;
400 #warn "CSB: blk<$blk> remain<$remain>\n";
401 # If we are about to drop off the end, pull in more
404 for (; $remain > 0; $line++) {
405 next if ($lines[$line] =~ /^-/);
408 $blk .= $lines[$line] . "\n";
413 # Bail if there is no further context.
414 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
420 $c = substr($blk, $off, 1);
421 $remainder = substr($blk, $off);
423 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
424 # Statement ends at the ';' or a close '}' at the
426 if ($level == 0 && $c eq ';') {
430 # An else is really a conditional as long as its not else if
431 if ($level == 0 && $coff_set == 0 &&
432 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
433 $remainder =~ /^(else)(?:\s|{)/ &&
434 $remainder !~ /^else\s+if\b/) {
435 $coff = $off + length($1) - 1;
437 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
438 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
441 if (($type eq '' || $type eq '(') && $c eq '(') {
445 if ($type eq '(' && $c eq ')') {
447 $type = ($level != 0)?
'(' : '';
449 if ($level == 0 && $coff < $soff) {
452 #warn "CSB: mark coff<$coff>\n";
455 if (($type eq '' || $type eq '{') && $c eq '{') {
459 if ($type eq '{' && $c eq '}') {
461 $type = ($level != 0)?
'{' : '';
474 my $statement = substr($blk, $soff, $off - $soff + 1);
475 my $condition = substr($blk, $soff, $coff - $soff + 1);
477 #warn "STATEMENT<$statement>\n";
478 #warn "CONDITION<$condition>\n";
480 #print "coff<$coff> soff<$off> loff<$loff>\n";
482 return ($statement, $condition,
483 $line, $remain + 1, $off - $loff + 1, $level);
486 sub statement_lines
{
489 # Strip the diff line prefixes and rip blank lines at start and end.
490 $stmt =~ s/(^|\n)./$1/g;
494 my @stmt_lines = ($stmt =~ /\n/g);
496 return $#stmt_lines + 2;
499 sub statement_rawlines
{
502 my @stmt_lines = ($stmt =~ /\n/g);
504 return $#stmt_lines + 2;
507 sub statement_block_size
{
510 $stmt =~ s/(^|\n)./$1/g;
516 my @stmt_lines = ($stmt =~ /\n/g);
517 my @stmt_statements = ($stmt =~ /;/g);
519 my $stmt_lines = $#stmt_lines + 2;
520 my $stmt_statements = $#stmt_statements + 1;
522 if ($stmt_lines > $stmt_statements) {
525 return $stmt_statements;
529 sub ctx_statement_full
{
530 my ($linenr, $remain, $off) = @_;
531 my ($statement, $condition, $level);
535 # Grab the first conditional/block pair.
536 ($statement, $condition, $linenr, $remain, $off, $level) =
537 ctx_statement_block
($linenr, $remain, $off);
538 #print "F: c<$condition> s<$statement> remain<$remain>\n";
539 push(@chunks, [ $condition, $statement ]);
540 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
541 return ($level, $linenr, @chunks);
544 # Pull in the following conditional/block pairs and see if they
545 # could continue the statement.
547 ($statement, $condition, $linenr, $remain, $off, $level) =
548 ctx_statement_block
($linenr, $remain, $off);
549 #print "C: c<$condition> s<$statement> remain<$remain>\n";
550 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
552 push(@chunks, [ $condition, $statement ]);
555 return ($level, $linenr, @chunks);
559 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
561 my $start = $linenr - 1;
568 for ($line = $start; $remain > 0; $line++) {
569 next if ($rawlines[$line] =~ /^-/);
572 $blk .= $rawlines[$line];
573 foreach my $c (split(//, $rawlines[$line])) {
574 ##print "C<$c>L<$level><$open$close>O<$off>\n";
580 if ($c eq $close && $level > 0) {
582 last if ($level == 0);
583 } elsif ($c eq $open) {
588 if (!$outer || $level <= 1) {
589 push(@res, $rawlines[$line]);
592 last if ($level == 0);
595 return ($level, @res);
597 sub ctx_block_outer
{
598 my ($linenr, $remain) = @_;
600 my ($level, @r) = ctx_block_get
($linenr, $remain, 1, '{', '}', 0);
604 my ($linenr, $remain) = @_;
606 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
610 my ($linenr, $remain, $off) = @_;
612 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
615 sub ctx_block_level
{
616 my ($linenr, $remain) = @_;
618 return ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
620 sub ctx_statement_level
{
621 my ($linenr, $remain, $off) = @_;
623 return ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
626 sub ctx_locate_comment
{
627 my ($first_line, $end_line) = @_;
629 # Catch a comment on the end of the line itself.
630 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@
.*(/\*.*\*/)\s
*$@
);
631 return $current_comment if (defined $current_comment);
633 # Look through the context and try and figure out if there is a
636 $current_comment = '';
637 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
638 my $line = $rawlines[$linenr - 1];
640 if ($linenr == $first_line and $line =~ m@
^.\s
*\
*@
) {
643 if ($line =~ m@
/\
*@
) {
646 if (!$in_comment && $current_comment ne '') {
647 $current_comment = '';
649 $current_comment .= $line . "\n" if ($in_comment);
650 if ($line =~ m@\
*/@
) {
655 chomp($current_comment);
656 return($current_comment);
658 sub ctx_has_comment
{
659 my ($first_line, $end_line) = @_;
660 my $cmt = ctx_locate_comment
($first_line, $end_line);
662 ##print "LINE: $rawlines[$end_line - 1 ]\n";
663 ##print "CMMT: $cmt\n";
673 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
676 $coded = sprintf("^%c", unpack('C', $2) + 64);
685 my $av_preprocessor = 0;
690 $av_preprocessor = 0;
692 @av_paren_type = ('E');
695 sub annotate_values
{
696 my ($stream, $type) = @_;
701 print "$stream\n" if ($dbg_values > 1);
703 while (length($cur)) {
704 @av_paren_type = ('E') if ($#av_paren_type < 0);
705 print " <" . join('', @av_paren_type) .
706 "> <$type> <$av_pending>" if ($dbg_values > 1);
707 if ($cur =~ /^(\s+)/o) {
708 print "WS($1)\n" if ($dbg_values > 1);
709 if ($1 =~ /\n/ && $av_preprocessor) {
710 $type = pop(@av_paren_type);
711 $av_preprocessor = 0;
714 } elsif ($cur =~ /^($Type)/) {
715 print "DECLARE($1)\n" if ($dbg_values > 1);
718 } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
719 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
720 $av_preprocessor = 1;
721 push(@av_paren_type, $type);
727 } elsif ($cur =~ /^(#\s*undef\s*$Ident)/o) {
728 print "UNDEF($1)\n" if ($dbg_values > 1);
729 $av_preprocessor = 1;
730 push(@av_paren_type, $type);
732 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if))/o) {
733 print "PRE_START($1)\n" if ($dbg_values > 1);
734 $av_preprocessor = 1;
736 push(@av_paren_type, $type);
737 push(@av_paren_type, $type);
740 } elsif ($cur =~ /^(#\s*(?:else|elif))/o) {
741 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
742 $av_preprocessor = 1;
744 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
748 } elsif ($cur =~ /^(#\s*(?:endif))/o) {
749 print "PRE_END($1)\n" if ($dbg_values > 1);
751 $av_preprocessor = 1;
753 # Assume all arms of the conditional end as this
754 # one does, and continue as if the #endif was not here.
756 push(@av_paren_type, $type);
759 } elsif ($cur =~ /^(\\\n)/o) {
760 print "PRECONT($1)\n" if ($dbg_values > 1);
762 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
763 print "ATTR($1)\n" if ($dbg_values > 1);
767 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
768 print "SIZEOF($1)\n" if ($dbg_values > 1);
774 } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
775 print "COND($1)\n" if ($dbg_values > 1);
779 } elsif ($cur =~/^(return|case|else)/o) {
780 print "KEYWORD($1)\n" if ($dbg_values > 1);
783 } elsif ($cur =~ /^(\()/o) {
784 print "PAREN('$1')\n" if ($dbg_values > 1);
785 push(@av_paren_type, $av_pending);
789 } elsif ($cur =~ /^(\))/o) {
790 my $new_type = pop(@av_paren_type);
791 if ($new_type ne '_') {
793 print "PAREN('$1') -> $type\n"
794 if ($dbg_values > 1);
796 print "PAREN('$1')\n" if ($dbg_values > 1);
799 } elsif ($cur =~ /^($Ident)\(/o) {
800 print "FUNC($1)\n" if ($dbg_values > 1);
803 } elsif ($cur =~ /^($Ident|$Constant)/o) {
804 print "IDENT($1)\n" if ($dbg_values > 1);
807 } elsif ($cur =~ /^($Assignment)/o) {
808 print "ASSIGN($1)\n" if ($dbg_values > 1);
811 } elsif ($cur =~/^(;|{|})/) {
812 print "END($1)\n" if ($dbg_values > 1);
815 } elsif ($cur =~ /^(;|\?|:|\[)/o) {
816 print "CLOSE($1)\n" if ($dbg_values > 1);
819 } elsif ($cur =~ /^($Operators)/o) {
820 print "OP($1)\n" if ($dbg_values > 1);
821 if ($1 ne '++' && $1 ne '--') {
825 } elsif ($cur =~ /(^.)/o) {
826 print "C($1)\n" if ($dbg_values > 1);
829 $cur = substr($cur, length($1));
830 $res .= $type x
length($1);
838 my ($possible, $line) = @_;
840 #print "CHECK<$possible>\n";
841 if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
842 $possible ne 'goto' && $possible ne 'return' &&
843 $possible ne 'struct' && $possible ne 'enum' &&
844 $possible ne 'case' && $possible ne 'else' &&
845 $possible ne 'typedef') {
846 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
847 push(@typeList, $possible);
855 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
858 my $line = $prefix . $_[0];
860 $line = (split('\n', $line))[0] . "\n" if ($terse);
862 push(our @report, $line);
870 if (report
("ERROR: $_[0]\n")) {
876 if (report
("WARNING: $_[0]\n")) {
882 if ($check && report
("CHECK: $_[0]\n")) {
889 my $filename = shift;
912 # Trace the real file/line as we go.
918 my $comment_edge = 0;
921 my $prev_values = 'E';
924 my %suppress_ifbraces;
926 # Pre-scan the patch sanitizing the lines.
927 # Pre-scan the patch looking for any __setup documentation.
932 sanitise_line_reset
();
934 foreach my $rawline (@rawlines) {
938 if ($rawline=~/^\+\+\+\s+(\S+)/) {
940 if ($1 =~ m
@Documentation/kernel
-parameters
.txt
$@
) {
945 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
953 # Guestimate if this is a continuing comment. Run
954 # the context looking for a comment "edge". If this
955 # edge is a close comment then we must be in a comment
958 for (my $ln = $linenr + 1; $ln < ($linenr + $realcnt); $ln++) {
959 next if ($line =~ /^-/);
960 ($edge) = ($rawlines[$ln - 1] =~ m@
(/\*|\*/)@
);
961 last if (defined $edge);
963 if (defined $edge && $edge eq '*/') {
967 # Guestimate if this is a continuing comment. If this
968 # is the start of a diff block and this line starts
969 # ' *' then it is very likely a comment.
970 if (!defined $edge &&
971 $rawlines[$linenr] =~ m@
^.\s
* \
*(?
:\s
|$)@
)
976 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
977 sanitise_line_reset
($in_comment);
979 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
980 # Standardise the strings and chars within the input to
981 # simplify matching -- only bother with positive lines.
982 $line = sanitise_line
($rawline);
987 $realcnt-- if ($line =~ /^(?:\+| |$)/);
992 #print "==>$rawline\n";
995 if ($setup_docs && $line =~ /^\+/) {
996 push(@setup_docs, $line);
1004 foreach my $line (@lines) {
1007 my $rawline = $rawlines[$linenr - 1];
1009 #extract the line range in the file after the patch is applied
1010 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1012 $first_line = $linenr + 1;
1022 %suppress_ifbraces = ();
1025 # track the line number as we move through the hunk, note that
1026 # new versions of GNU diff omit the leading space on completely
1027 # blank context lines so we need to count that too.
1028 } elsif ($line =~ /^( |\+|$)/) {
1030 $realcnt-- if ($realcnt != 0);
1032 # Measure the line length and indent.
1033 ($length, $indent) = line_stats
($rawline);
1035 # Track the previous line.
1036 ($prevline, $stashline) = ($stashline, $line);
1037 ($previndent, $stashindent) = ($stashindent, $indent);
1038 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1040 #warn "line<$line>\n";
1042 } elsif ($realcnt == 1) {
1046 #make up the handle for any error we report on this line
1047 $prefix = "$filename:$realline: " if ($emacs && $file);
1048 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1050 $here = "#$linenr: " if (!$file);
1051 $here = "#$realline: " if ($file);
1053 # extract the filename as it passes
1054 if ($line=~/^\+\+\+\s+(\S+)/) {
1056 $realfile =~ s@
^[^/]*/@@
;
1058 if ($realfile =~ m
@include/asm/@
) {
1059 ERROR
("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1064 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1066 my $hereline = "$here\n$rawline\n";
1067 my $herecurr = "$here\n$rawline\n";
1068 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1070 $cnt_lines++ if ($realcnt != 0);
1072 #check the patch for a signoff:
1073 if ($line =~ /^\s*signed-off-by:/i) {
1074 # This is a signoff, if ugly, so do not double report.
1076 if (!($line =~ /^\s*Signed-off-by:/)) {
1077 WARN
("Signed-off-by: is the preferred form\n" .
1080 if ($line =~ /^\s*signed-off-by:\S/i) {
1081 WARN
("space required after Signed-off-by:\n" .
1086 # Check for wrappage within a valid hunk of the file
1087 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1088 ERROR
("patch seems to be corrupt (line wrapped?)\n" .
1089 $herecurr) if (!$emitted_corrupt++);
1092 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1093 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1094 $rawline !~ m/^$UTF8*$/) {
1095 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1097 my $blank = copy_spacing
($rawline);
1098 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1099 my $hereptr = "$hereline$ptr\n";
1101 ERROR
("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1104 #ignore lines being removed
1105 if ($line=~/^-/) {next;}
1107 # check we are in a valid source file if not then ignore this hunk
1108 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1110 #trailing whitespace
1111 if ($line =~ /^\+.*\015/) {
1112 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1113 ERROR
("DOS line endings\n" . $herevet);
1115 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1116 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1117 ERROR
("trailing whitespace\n" . $herevet);
1120 if ($line =~ /^\+/ && !($prevrawline=~/\/\
*\
*/) && $length > 80) {
1121 WARN
("line over 80 characters\n" . $herecurr);
1124 # check for adding lines without a newline.
1125 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1126 WARN
("adding a line without newline at end of file\n" . $herecurr);
1129 # check we are in a valid source file *.[hc] if not then ignore this hunk
1130 next if ($realfile !~ /\.[hc]$/);
1132 # at the beginning of a line any tabs must come first and anything
1133 # more than 8 must use tabs.
1134 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1135 $rawline =~ /^\+\s* \s*/) {
1136 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1137 ERROR
("code indent should use tabs where possible\n" . $herevet);
1140 # check for RCS/CVS revision markers
1141 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1142 WARN
("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1145 # Check for potential 'bare' types
1147 if ($realcnt && $line =~ /.\s*\S/) {
1148 ($stat, $cond) = ctx_statement_block
($linenr,
1150 $stat =~ s/\n./\n /g;
1151 $cond =~ s/\n./\n /g;
1156 # Ignore goto labels.
1157 if ($s =~ /$Ident:\*$/s) {
1159 # Ignore functions being called
1160 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1162 # definitions in global scope can only start with types
1163 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/s) {
1166 # declarations always start with types
1167 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/s) {
1171 # any (foo ... *) is a pointer cast, and foo is a type
1172 while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/sg) {
1176 # Check for any sort of function declaration.
1177 # int foo(something bar, other baz);
1178 # void (*store_gdt)(x86_descr_ptr *);
1179 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1180 my ($name_len) = length($1);
1183 substr($ctx, 0, $name_len + 1, '');
1184 $ctx =~ s/\)[^\)]*$//;
1186 for my $arg (split(/\s*,\s*/, $ctx)) {
1187 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
1197 # Checks which may be anchored in the context.
1200 # Check for switch () and associated case and default
1201 # statements should be at the same indent.
1202 if ($line=~/\bswitch\s*\(.*\)/) {
1205 my @ctx = ctx_block_outer
($linenr, $realcnt);
1207 for my $ctx (@ctx) {
1208 my ($clen, $cindent) = line_stats
($ctx);
1209 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1210 $indent != $cindent) {
1211 $err .= "$sep$ctx\n";
1218 ERROR
("switch and case should be at the same indent\n$hereline$err");
1222 # if/while/etc brace do not go on next line, unless defining a do while loop,
1223 # or if that brace on the next line is for something else
1224 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
1225 my $pre_ctx = "$1$2";
1227 my ($level, @ctx) = ctx_statement_level
($linenr, $realcnt, 0);
1228 my $ctx_ln = $linenr + $#ctx + 1;
1229 my $ctx_cnt = $realcnt - $#ctx - 1;
1230 my $ctx = join("\n", @ctx);
1232 ##warn "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1234 # Skip over any removed lines in the context following statement.
1235 while (defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^-/) {
1238 ##warn "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1240 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1241 ERROR
("that open brace { should be on the previous line\n" .
1242 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1244 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1245 $ctx =~ /\)\s*\;\s*$/ &&
1246 defined $lines[$ctx_ln - 1])
1248 my ($nlength, $nindent) = line_stats
($lines[$ctx_ln - 1]);
1249 if ($nindent > $indent) {
1250 WARN
("trailing semicolon indicates no statements, indent implies otherwise\n" .
1251 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1256 # Track the 'values' across context and added lines.
1257 my $opline = $line; $opline =~ s/^./ /;
1258 my $curr_values = annotate_values
($opline . "\n", $prev_values);
1259 $curr_values = $prev_values . $curr_values;
1261 my $outline = $opline; $outline =~ s/\t/ /g;
1262 print "$linenr > .$outline\n";
1263 print "$linenr > $curr_values\n";
1265 $prev_values = substr($curr_values, -1);
1267 #ignore lines not being added
1268 if ($line=~/^[^\+]/) {next;}
1270 # TEST: allow direct testing of the type matcher.
1271 if ($tst_type && $line =~ /^.$Declare$/) {
1272 ERROR
("TEST: is type $Declare\n" . $herecurr);
1276 # check for initialisation to aggregates open brace on the next line
1277 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1278 $line =~ /^.\s*{/) {
1279 ERROR
("that open brace { should be on the previous line\n" . $hereprev);
1283 # Checks which are anchored on the added line.
1286 # check for malformed paths in #include statements (uses RAW line)
1287 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
1289 if ($path =~ m{//}) {
1290 ERROR
("malformed #include filename\n" .
1295 # no C99 // comments
1296 if ($line =~ m{//}) {
1297 ERROR
("do not use C99 // comments\n" . $herecurr);
1299 # Remove C99 comments.
1301 $opline =~ s@
//.*@@
;
1303 #EXPORT_SYMBOL should immediately follow its function closing }.
1304 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1305 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1307 if (($prevline !~ /^}/) &&
1308 ($prevline !~ /^\+}/) &&
1309 ($prevline !~ /^ }/) &&
1310 ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1311 ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
1312 ($prevline !~ /^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(/) &&
1313 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
1314 WARN
("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1318 # check for external initialisers.
1319 if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL|false)\s*;/) {
1320 ERROR
("do not initialise externals to 0 or NULL\n" .
1323 # check for static initialisers.
1324 if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
1325 ERROR
("do not initialise statics to 0 or NULL\n" .
1329 # check for new typedefs, only function parameters and sparse annotations
1331 if ($line =~ /\btypedef\s/ &&
1332 $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1333 $line !~ /\b__bitwise(?:__|)\b/) {
1334 WARN
("do not add new typedefs\n" . $herecurr);
1337 # * goes on variable not on type
1338 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
1339 ERROR
("\"(foo$1)\" should be \"(foo $1)\"\n" .
1342 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
1343 ERROR
("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1346 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
1347 ERROR
("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1350 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
1351 ERROR
("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1355 # # no BUG() or BUG_ON()
1356 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1357 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1358 # print "$herecurr";
1362 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1363 WARN
("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1366 # printk should use KERN_* levels. Note that follow on printk's on the
1367 # same line do not need a level, so we use the current block context
1368 # to try and find and validate the current printk. In summary the current
1369 # printk includes all preceeding printk's which have no newline on the end.
1370 # we assume the first bad printk is the one to report.
1371 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1373 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1374 #print "CHECK<$lines[$ln - 1]\n";
1375 # we have a preceeding printk if it ends
1376 # with "\n" ignore it, else it is to blame
1377 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1378 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1385 WARN
("printk() should include KERN_ facility level\n" . $herecurr);
1389 # function brace can't be on same line, except for #defines of do while,
1390 # or if closed on same line
1391 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and
1392 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
1393 ERROR
("open brace '{' following function declarations go on the next line\n" . $herecurr);
1396 # open braces for enum, union and struct go on the same line.
1397 if ($line =~ /^.\s*{/ &&
1398 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1399 ERROR
("open brace '{' following $1 go on the same line\n" . $hereprev);
1402 # check for spaces between functions and their parentheses.
1403 while ($line =~ /($Ident)\s+\(/g) {
1405 my $ctx_before = substr($line, 0, $-[1]);
1406 my $ctx = "$ctx_before$name";
1408 # Ignore those directives where spaces _are_ permitted.
1410 if|for|while|switch
|return|case
|
1411 volatile
|__volatile__
|
1412 __attribute__
|format
|__extension__
|
1416 # cpp #define statements have non-optional spaces, ie
1417 # if there is a space between the name and the open
1418 # parenthesis it is simply not a parameter group.
1419 } elsif ($ctx_before =~ /^.\#\s*define\s*$/) {
1421 # cpp #elif statement condition may start with a (
1422 } elsif ($ctx =~ /^.\#\s*elif\s*$/) {
1424 # If this whole things ends with a type its most
1425 # likely a typedef for a function.
1426 } elsif ($ctx =~ /$Type$/) {
1429 WARN
("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1432 # Check operator spacing.
1433 if (!($line=~/\#\s*include/)) {
1435 <<=|>>=|<=|>=|==|!=|
1436 \
+=|-=|\
*=|\
/=|%=|\
^=|\
|=|&=|
1437 =>|->|<<|>>|<|>|=|!|~|
1438 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%
1440 my @elements = split(/($ops|;)/, $opline);
1443 my $blank = copy_spacing
($opline);
1445 for (my $n = 0; $n < $#elements; $n += 2) {
1446 $off += length($elements[$n]);
1448 # Pick up the preceeding and succeeding characters.
1449 my $ca = substr($opline, 0, $off);
1451 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1452 $cc = substr($opline, $off + length($elements[$n + 1]));
1454 my $cb = "$ca$;$cc";
1457 $a = 'V' if ($elements[$n] ne '');
1458 $a = 'W' if ($elements[$n] =~ /\s$/);
1459 $a = 'C' if ($elements[$n] =~ /$;$/);
1460 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1461 $a = 'O' if ($elements[$n] eq '');
1462 $a = 'E' if ($ca =~ /^\s*$/);
1464 my $op = $elements[$n + 1];
1467 if (defined $elements[$n + 2]) {
1468 $c = 'V' if ($elements[$n + 2] ne '');
1469 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1470 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1471 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1472 $c = 'O' if ($elements[$n + 2] eq '');
1473 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
1478 my $ctx = "${a}x${c}";
1480 my $at = "(ctx:$ctx)";
1482 my $ptr = substr($blank, 0, $off) . "^";
1483 my $hereptr = "$hereline$ptr\n";
1485 # Classify operators into binary, unary, or
1486 # definitions (* only) where they have more
1488 my $op_type = substr($curr_values, $off + 1, 1);
1489 my $op_left = substr($curr_values, $off, 1);
1491 if ($op_type eq 'T') {
1493 } elsif ($op_left eq 'V') {
1498 #if ($op eq '-' || $op eq '&' || $op eq '*') {
1499 # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
1502 # Ignore operators passed as parameters.
1503 if ($op_type ne 'V' &&
1504 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1507 # } elsif ($op =~ /^$;+$/) {
1509 # ; should have either the end of line or a space or \ after it
1510 } elsif ($op eq ';') {
1511 if ($ctx !~ /.x[WEBC]/ &&
1512 $cc !~ /^\\/ && $cc !~ /^;/) {
1513 ERROR
("space required after that '$op' $at\n" . $hereptr);
1517 } elsif ($op eq '//') {
1519 # -> should have no spaces
1520 } elsif ($op eq '->') {
1521 if ($ctx =~ /Wx.|.xW/) {
1522 ERROR
("spaces prohibited around that '$op' $at\n" . $hereptr);
1525 # , must have a space on the right.
1526 } elsif ($op eq ',') {
1527 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1528 ERROR
("space required after that '$op' $at\n" . $hereptr);
1531 # '*' as part of a type definition -- reported already.
1532 } elsif ($op eq '*' && $is_unary == 2) {
1533 #warn "'*' is part of type\n";
1535 # unary operators should have a space before and
1536 # none after. May be left adjacent to another
1537 # unary operator, or a cast
1538 } elsif ($op eq '!' || $op eq '~' ||
1539 ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
1540 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1541 ERROR
("space required before that '$op' $at\n" . $hereptr);
1543 if ($op eq '*' && $cc =~/\s*const\b/) {
1544 # A unary '*' may be const
1546 } elsif ($ctx =~ /.xW/) {
1547 ERROR
("space prohibited after that '$op' $at\n" . $hereptr);
1550 # unary ++ and unary -- are allowed no space on one side.
1551 } elsif ($op eq '++' or $op eq '--') {
1552 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1553 ERROR
("space required one side of that '$op' $at\n" . $hereptr);
1555 if ($ctx =~ /Wx[BE]/ ||
1556 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1557 ERROR
("space prohibited before that '$op' $at\n" . $hereptr);
1559 if ($ctx =~ /ExW/) {
1560 ERROR
("space prohibited after that '$op' $at\n" . $hereptr);
1564 # << and >> may either have or not have spaces both sides
1565 } elsif ($op eq '<<' or $op eq '>>' or
1566 $op eq '&' or $op eq '^' or $op eq '|' or
1567 $op eq '+' or $op eq '-' or
1568 $op eq '*' or $op eq '/' or
1571 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1572 ERROR
("need consistent spacing around '$op' $at\n" .
1576 # All the others need spaces both sides.
1577 } elsif ($ctx !~ /[EWC]x[CWE]/) {
1578 # Ignore email addresses <foo@bar>
1579 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
1580 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
1581 ERROR
("spaces required around that '$op' $at\n" . $hereptr);
1584 $off += length($elements[$n + 1]);
1588 # check for multiple assignments
1589 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
1590 CHK
("multiple assignments should be avoided\n" . $herecurr);
1593 ## # check for multiple declarations, allowing for a function declaration
1595 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1596 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1598 ## # Remove any bracketed sections to ensure we do not
1599 ## # falsly report the parameters of functions.
1601 ## while ($ln =~ s/\([^\(\)]*\)//g) {
1603 ## if ($ln =~ /,/) {
1604 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1608 #need space before brace following if, while, etc
1609 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1611 ERROR
("space required before the open brace '{'\n" . $herecurr);
1614 # closing brace should have a space following it when it has anything
1616 if ($line =~ /}(?!(?:,|;|\)))\S/) {
1617 ERROR
("space required after that close brace '}'\n" . $herecurr);
1620 # check spacing on square brackets
1621 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1622 ERROR
("space prohibited after that open square bracket '['\n" . $herecurr);
1624 if ($line =~ /\s\]/) {
1625 ERROR
("space prohibited before that close square bracket ']'\n" . $herecurr);
1628 # check spacing on paretheses
1629 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1630 $line !~ /for\s*\(\s+;/) {
1631 ERROR
("space prohibited after that open parenthesis '('\n" . $herecurr);
1633 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1634 $line !~ /for\s*\(.*;\s+\)/) {
1635 ERROR
("space prohibited before that close parenthesis ')'\n" . $herecurr);
1638 #goto labels aren't indented, allow a single space however
1639 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
1640 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1641 WARN
("labels should not be indented\n" . $herecurr);
1644 # Need a space before open parenthesis after if, while etc
1645 if ($line=~/\b(if|while|for|switch)\(/) {
1646 ERROR
("space required before the open parenthesis '('\n" . $herecurr);
1649 # Check for illegal assignment in if conditional.
1650 if ($line =~ /\bif\s*\(/) {
1651 my ($s, $c) = ($stat, $cond);
1653 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
1654 ERROR
("do not use assignment in if condition\n" . $herecurr);
1657 # Find out what is on the end of the line after the
1659 substr($s, 0, length($c), '');
1661 $s =~ s/$;//g; # Remove any comments
1662 if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/ &&
1665 ERROR
("trailing statements should be on next line\n" . $herecurr);
1669 # Check for bitwise tests written as boolean
1681 WARN
("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
1684 # if and else should not have general statements after it
1685 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
1687 $s =~ s/$;//g; # Remove any comments
1688 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
1689 ERROR
("trailing statements should be on next line\n" . $herecurr);
1693 # Check for }<nl>else {, these must be at the same
1694 # indent level to be relevant to each other.
1695 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
1696 $previndent == $indent) {
1697 ERROR
("else should follow close brace '}'\n" . $hereprev);
1700 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1701 $previndent == $indent) {
1702 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
1704 # Find out what is on the end of the line after the
1706 substr($s, 0, length($c), '');
1709 if ($s =~ /^\s*;/) {
1710 ERROR
("while should follow close brace '}'\n" . $hereprev);
1714 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
1715 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
1716 # print "No studly caps, use _\n";
1717 # print "$herecurr";
1721 #no spaces allowed after \ in define
1722 if ($line=~/\#define.*\\\s$/) {
1723 WARN
("Whitepspace after \\ makes next lines useless\n" . $herecurr);
1726 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1727 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
1728 my $checkfile = "$root/include/linux/$1.h";
1729 if (-f
$checkfile && $1 ne 'irq') {
1730 WARN
("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
1735 # multi-statement macros should be enclosed in a do while loop, grab the
1736 # first statement and ensure its the whole macro if its not enclosed
1737 # in a known good container
1738 if ($prevline =~ /\#define.*\\/ &&
1739 $prevline !~/(?:do\s+{|\(\{|\{)/ &&
1740 $line !~ /(?:do\s+{|\(\{|\{)/ &&
1741 $line !~ /^.\s*$Declare\s/) {
1742 # Grab the first statement, if that is the entire macro
1743 # its ok. This may start either on the #define line
1749 # If the macro starts on the define line start
1750 # grabbing the statement after the identifier
1751 $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
1752 ##print "1<$1> 2<$2>\n";
1753 if (defined $2 && $2 ne '') {
1757 while ($lines[$ln - 1] =~ /^-/) {
1762 my @ctx = ctx_statement
($ln, $cnt, $off);
1763 my $ctx_ln = $ln + $#ctx + 1;
1764 my $ctx = join("\n", @ctx);
1766 # Pull in any empty extension lines.
1767 while ($ctx =~ /\\$/ &&
1768 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
1769 $ctx .= $lines[$ctx_ln - 1];
1773 if ($ctx =~ /\\$/) {
1775 ERROR
("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
1777 ERROR
("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
1782 # check for redundant bracing round if etc
1783 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
1784 my ($level, $endln, @chunks) =
1785 ctx_statement_full
($linenr, $realcnt, 1);
1786 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
1787 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
1788 if ($#chunks > 0 && $level == 0) {
1791 my $herectx = $here . "\n";
1792 my $ln = $linenr - 1;
1793 for my $chunk (@chunks) {
1794 my ($cond, $block) = @
{$chunk};
1796 # If the condition carries leading newlines, then count those as offsets.
1797 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
1798 my $offset = statement_rawlines
($whitespace) - 1;
1800 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
1802 # We have looked at and allowed this specific line.
1803 $suppress_ifbraces{$ln + $offset} = 1;
1805 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
1806 $ln += statement_rawlines
($block) - 1;
1808 substr($block, 0, length($cond), '');
1810 $seen++ if ($block =~ /^\s*{/);
1812 #print "cond<$cond> block<$block> allowed<$allowed>\n";
1813 if (statement_lines
($cond) > 1) {
1814 #print "APW: ALLOWED: cond<$cond>\n";
1817 if ($block =~/\b(?:if|for|while)\b/) {
1818 #print "APW: ALLOWED: block<$block>\n";
1821 if (statement_block_size
($block) > 1) {
1822 #print "APW: ALLOWED: lines block<$block>\n";
1826 if ($seen && !$allowed) {
1827 WARN
("braces {} are not necessary for any arm of this statement\n" . $herectx);
1831 if (!defined $suppress_ifbraces{$linenr - 1} &&
1832 $line =~ /\b(if|while|for|else)\b/) {
1835 # Check the pre-context.
1836 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
1837 #print "APW: ALLOWED: pre<$1>\n";
1841 my ($level, $endln, @chunks) =
1842 ctx_statement_full
($linenr, $realcnt, $-[0]);
1844 # Check the condition.
1845 my ($cond, $block) = @
{$chunks[0]};
1846 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
1847 if (defined $cond) {
1848 substr($block, 0, length($cond), '');
1850 if (statement_lines
($cond) > 1) {
1851 #print "APW: ALLOWED: cond<$cond>\n";
1854 if ($block =~/\b(?:if|for|while)\b/) {
1855 #print "APW: ALLOWED: block<$block>\n";
1858 if (statement_block_size
($block) > 1) {
1859 #print "APW: ALLOWED: lines block<$block>\n";
1862 # Check the post-context.
1863 if (defined $chunks[1]) {
1864 my ($cond, $block) = @
{$chunks[1]};
1865 if (defined $cond) {
1866 substr($block, 0, length($cond), '');
1868 if ($block =~ /^\s*\{/) {
1869 #print "APW: ALLOWED: chunk-1 block<$block>\n";
1873 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
1874 my $herectx = $here . "\n";;
1875 my $end = $linenr + statement_rawlines
($block) - 1;
1877 for (my $ln = $linenr - 1; $ln < $end; $ln++) {
1878 $herectx .= $rawlines[$ln] . "\n";;
1881 WARN
("braces {} are not necessary for single statement blocks\n" . $herectx);
1885 # don't include deprecated include files (uses RAW line)
1886 for my $inc (@dep_includes) {
1887 if ($rawline =~ m@\#\s
*include\s
*\
<$inc>@
) {
1888 ERROR
("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
1892 # don't use deprecated functions
1893 for my $func (@dep_functions) {
1894 if ($line =~ /\b$func\b/) {
1895 ERROR
("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
1899 # no volatiles please
1900 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
1901 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
1902 WARN
("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
1905 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
1906 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
1907 ERROR
("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
1911 if ($line =~ /^.#\s*if\s+0\b/) {
1912 CHK
("if this code is redundant consider removing it\n" .
1916 # check for needless kfree() checks
1917 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1919 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1920 WARN
("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1923 # check for needless usb_free_urb() checks
1924 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1926 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
1927 WARN
("usb_free_urb(NULL) is safe this check is probabally not required\n" . $hereprev);
1931 # warn about #ifdefs in C files
1932 # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
1933 # print "#ifdef in C files should be avoided\n";
1934 # print "$herecurr";
1938 # warn about spacing in #ifdefs
1939 if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
1940 ERROR
("exactly one space required after that #$1\n" . $herecurr);
1943 # check for spinlock_t definitions without a comment.
1944 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
1945 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
1947 if (!ctx_has_comment
($first_line, $linenr)) {
1948 CHK
("$1 definition without comment\n" . $herecurr);
1951 # check for memory barriers without a comment.
1952 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
1953 if (!ctx_has_comment
($first_line, $linenr)) {
1954 CHK
("memory barrier without comment\n" . $herecurr);
1957 # check of hardware specific defines
1958 if ($line =~ m@
^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
1959 CHK
("architecture specific defines should be avoided\n" . $herecurr);
1962 # check the location of the inline attribute, that it is between
1963 # storage class and type.
1964 if ($line =~ /\b$Type\s+$Inline\b/ ||
1965 $line =~ /\b$Inline\s+$Storage\b/) {
1966 ERROR
("inline keyword should sit between storage class and type\n" . $herecurr);
1969 # Check for __inline__ and __inline, prefer inline
1970 if ($line =~ /\b(__inline__|__inline)\b/) {
1971 WARN
("plain inline is preferred over $1\n" . $herecurr);
1974 # check for new externs in .c files.
1975 if ($realfile =~ /\.c$/ && defined $stat &&
1976 $stat =~ /^.\s*(?:extern\s+)?$Type\s+$Ident(\s*)\(/s)
1978 my $paren_space = $1;
1981 if (defined $cond) {
1982 substr($s, 0, length($cond), '');
1984 if ($s =~ /^\s*;/) {
1985 WARN
("externs should be avoided in .c files\n" . $herecurr);
1988 if ($paren_space =~ /\n/) {
1989 WARN
("arguments for function declarations should follow identifier\n" . $herecurr);
1992 } elsif ($realfile =~ /\.c$/ && defined $stat &&
1993 $stat =~ /^.\s*extern\s+/)
1995 WARN
("externs should be avoided in .c files\n" . $herecurr);
1998 # checks for new __setup's
1999 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2002 if (!grep(/$name/, @setup_docs)) {
2003 CHK
("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2007 # check for pointless casting of kmalloc return
2008 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2009 WARN
("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2012 # check for gcc specific __FUNCTION__
2013 if ($line =~ /__FUNCTION__/) {
2014 WARN
("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2017 # check for semaphores used as mutexes
2018 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2019 WARN
("mutexes are preferred for single holder semaphores\n" . $herecurr);
2021 # check for semaphores used as mutexes
2022 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2023 WARN
("consider using a completion\n" . $herecurr);
2025 # recommend strict_strto* over simple_strto*
2026 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2027 WARN
("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2030 # use of NR_CPUS is usually wrong
2031 # ignore definitions of NR_CPUS and usage to define arrays as likely right
2032 if ($line =~ /\bNR_CPUS\b/ &&
2033 $line !~ /^.#\s*if\b.*\bNR_CPUS\b/ &&
2034 $line !~ /^.#\s*define\b.*\bNR_CPUS\b/ &&
2035 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2036 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2037 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2039 WARN
("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2042 # check for %L{u,d,i} in strings
2044 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2045 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2046 if ($string =~ /(?<!%)%L[udi]/) {
2047 WARN
("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2053 # If we have no input at all, then there is nothing to report on
2054 # so just keep quiet.
2055 if ($#rawlines == -1) {
2059 # In mailback mode only produce a report in the negative, for
2060 # things that appear to be patches.
2061 if ($mailback && ($clean == 1 || !$is_patch)) {
2065 # This is not a patch, and we are are in 'no-patch' mode so
2067 if (!$chk_patch && !$is_patch) {
2072 ERROR
("Does not appear to be a unified-diff format patch\n");
2074 if ($is_patch && $chk_signoff && $signoff == 0) {
2075 ERROR
("Missing Signed-off-by: line(s)\n");
2078 print report_dump
();
2079 if ($summary && !($clean == 1 && $quiet == 1)) {
2080 print "$filename " if ($summary_file);
2081 print "total: $cnt_error errors, $cnt_warn warnings, " .
2082 (($check)?
"$cnt_chk checks, " : "") .
2083 "$cnt_lines lines checked\n";
2084 print "\n" if ($quiet == 0);
2087 if ($clean == 1 && $quiet == 0) {
2088 print "$vname has no obvious style problems and is ready for submission.\n"
2090 if ($clean == 0 && $quiet == 0) {
2091 print "$vname has style problems, please review. If any of these errors\n";
2092 print "are false positives report them to the maintainer, see\n";
2093 print "CHECKPATCH in MAINTAINERS.\n";
This page took 0.107175 seconds and 6 git commands to generate.