2 # (c) 2001, Dave Jones. <davej@redhat.com> (the file handling bit)
3 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4 # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5 # (c) 2008, Andy Whitcroft <apw@canonical.com>
6 # Licensed under the terms of the GNU GPL License version 2
15 use Getopt::Long qw(:config no_auto_abbrev);
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-only=s' => \$tst_only,
53 print "usage: $P [options] patchfile\n";
54 print "version: $V\n";
55 print "options: -q => quiet\n";
56 print " --no-tree => run without a kernel tree\n";
57 print " --terse => one line per report\n";
58 print " --emacs => emacs compile window format\n";
59 print " --file => check a source file\n";
60 print " --strict => enable more subjective tests\n";
61 print " --root => path to the kernel tree root\n";
62 print " --no-summary => suppress the per-file summary\n";
63 print " --summary-file => include the filename in summary\n";
71 for my $key (keys %debug) {
72 eval "\${dbg_$key} = '$debug{$key}';"
82 if (!top_of_kernel_tree($root)) {
83 die "$P: $root: --root does not point at a valid tree\n";
86 if (top_of_kernel_tree('.')) {
88 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
89 top_of_kernel_tree($1)) {
95 print "Must be run from the top-level dir. of a kernel tree\n";
100 my $emitted_corrupt = 0;
102 our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
103 our $Storage = qr{extern|static|asmlinkage};
117 __(?:mem|cpu|dev|)(?:initdata|init)|
118 ____cacheline_aligned|
119 ____cacheline_aligned_in_smp|
120 ____cacheline_internodealigned_in_smp|
124 our $Inline = qr{inline|__always_inline|noinline};
125 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
126 our $Lval = qr{$Ident(?:$Member)*};
128 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
129 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
130 our $Compare = qr{<=|>=|==|!=|<|>};
134 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
142 [\x09\x0A\x0D\x20-\x7E] # ASCII
143 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
144 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
145 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
146 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
147 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
148 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
149 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
152 our $typeTypedefs = qr{(?x:
153 (?:__)?(?:u|s|be|le)(?:\d|\d\d)|
159 qr{(?:unsigned\s+)?char},
160 qr{(?:unsigned\s+)?short},
161 qr{(?:unsigned\s+)?int},
162 qr{(?:unsigned\s+)?long},
163 qr{(?:unsigned\s+)?long\s+int},
164 qr{(?:unsigned\s+)?long\s+long},
165 qr{(?:unsigned\s+)?long\s+long\s+int},
174 qr{${Ident}_handler},
175 qr{${Ident}_handler_fn},
177 our @modifierList = (
182 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
183 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
184 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
186 (?:$Modifier\s+|const\s+)*
188 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
192 (?:\s+$Modifier|\s+const)*
196 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
197 (?:\s+$Inline|\s+$Modifier)*
199 $Declare = qr{(?:$Storage\s+)?$Type};
203 $chk_signoff = 0 if ($file);
205 my @dep_includes = ();
206 my @dep_functions = ();
207 my $removal = "Documentation/feature-removal-schedule.txt";
208 if ($tree && -f "$root/$removal") {
209 open(REMOVE, "<$root/$removal") ||
210 die "$P: $removal: open failed - $!\n";
212 if (/^Check:\s+(.*\S)/) {
213 for my $entry (split(/[, ]+/, $1)) {
214 if ($entry =~ m@include/(.*)@) {
215 push(@dep_includes, $1);
217 } elsif ($entry !~ m@/@) {
218 push(@dep_functions, $entry);
228 for my $filename (@ARGV) {
230 open(FILE, "diff -u /dev/null $filename|") ||
231 die "$P: $filename: diff failed - $!\n";
233 open(FILE, "<$filename") ||
234 die "$P: $filename: open failed - $!\n";
236 if ($filename eq '-') {
237 $vname = 'Your patch';
246 if (!process($filename)) {
255 sub top_of_kernel_tree {
259 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
260 "README", "Documentation", "arch", "include", "drivers",
261 "fs", "init", "ipc", "kernel", "lib", "scripts",
264 foreach my $check (@tree_check) {
265 if (! -e $root . '/' . $check) {
277 for my $c (split(//, $str)) {
281 for (; ($n % 8) != 0; $n++) {
293 (my $res = shift) =~ tr/\t/ /c;
300 # Drop the diff line leader and expand tabs
302 $line = expand_tabs($line);
304 # Pick the indent from the front of the line.
305 my ($white) = ($line =~ /^(\s*)/);
307 return (length($line), length($white));
310 my $sanitise_quote = '';
312 sub sanitise_line_reset {
313 my ($in_comment) = @_;
316 $sanitise_quote = '*/';
318 $sanitise_quote = '';
331 # Always copy over the diff marker.
332 $res = substr($line, 0, 1);
334 for ($off = 1; $off < length($line); $off++) {
335 $c = substr($line, $off, 1);
337 # Comments we are wacking completly including the begin
338 # and end, all to $;.
339 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
340 $sanitise_quote = '*/';
342 substr($res, $off, 2, "$;$;");
346 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
347 $sanitise_quote = '';
348 substr($res, $off, 2, "$;$;");
353 # A \ in a string means ignore the next character.
354 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
356 substr($res, $off, 2, 'XX');
361 if ($c eq "'" || $c eq '"') {
362 if ($sanitise_quote eq '') {
363 $sanitise_quote = $c;
365 substr($res, $off, 1, $c);
367 } elsif ($sanitise_quote eq $c) {
368 $sanitise_quote = '';
372 #print "c<$c> SQ<$sanitise_quote>\n";
373 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
374 substr($res, $off, 1, $;);
375 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
376 substr($res, $off, 1, 'X');
378 substr($res, $off, 1, $c);
382 # The pathname on a #include may be surrounded by '<' and '>'.
383 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
384 my $clean = 'X' x length($1);
385 $res =~ s@\<.*\>@<$clean>@;
387 # The whole of a #error is a string.
388 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
389 my $clean = 'X' x length($1);
390 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
396 sub ctx_statement_block {
397 my ($linenr, $remain, $off) = @_;
398 my $line = $linenr - 1;
408 my @stack = ([$type, $level]);
415 #warn "CSB: blk<$blk> remain<$remain>\n";
416 # If we are about to drop off the end, pull in more
419 for (; $remain > 0; $line++) {
420 last if (!defined $lines[$line]);
421 next if ($lines[$line] =~ /^-/);
424 $blk .= $lines[$line] . "\n";
429 # Bail if there is no further context.
430 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
436 $c = substr($blk, $off, 1);
437 $remainder = substr($blk, $off);
439 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
441 # Handle nested #if/#else.
442 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
443 push(@stack, [ $type, $level ]);
444 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
445 ($type, $level) = @{$stack[$#stack - 1]};
446 } elsif ($remainder =~ /^#\s*endif\b/) {
447 ($type, $level) = @{pop(@stack)};
450 # Statement ends at the ';' or a close '}' at the
452 if ($level == 0 && $c eq ';') {
456 # An else is really a conditional as long as its not else if
457 if ($level == 0 && $coff_set == 0 &&
458 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
459 $remainder =~ /^(else)(?:\s|{)/ &&
460 $remainder !~ /^else\s+if\b/) {
461 $coff = $off + length($1) - 1;
463 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
464 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
467 if (($type eq '' || $type eq '(') && $c eq '(') {
471 if ($type eq '(' && $c eq ')') {
473 $type = ($level != 0)? '(' : '';
475 if ($level == 0 && $coff < $soff) {
478 #warn "CSB: mark coff<$coff>\n";
481 if (($type eq '' || $type eq '{') && $c eq '{') {
485 if ($type eq '{' && $c eq '}') {
487 $type = ($level != 0)? '{' : '';
495 # We are truly at the end, so shuffle to the next line.
502 my $statement = substr($blk, $soff, $off - $soff + 1);
503 my $condition = substr($blk, $soff, $coff - $soff + 1);
505 #warn "STATEMENT<$statement>\n";
506 #warn "CONDITION<$condition>\n";
508 #print "coff<$coff> soff<$off> loff<$loff>\n";
510 return ($statement, $condition,
511 $line, $remain + 1, $off - $loff + 1, $level);
514 sub statement_lines {
517 # Strip the diff line prefixes and rip blank lines at start and end.
518 $stmt =~ s/(^|\n)./$1/g;
522 my @stmt_lines = ($stmt =~ /\n/g);
524 return $#stmt_lines + 2;
527 sub statement_rawlines {
530 my @stmt_lines = ($stmt =~ /\n/g);
532 return $#stmt_lines + 2;
535 sub statement_block_size {
538 $stmt =~ s/(^|\n)./$1/g;
544 my @stmt_lines = ($stmt =~ /\n/g);
545 my @stmt_statements = ($stmt =~ /;/g);
547 my $stmt_lines = $#stmt_lines + 2;
548 my $stmt_statements = $#stmt_statements + 1;
550 if ($stmt_lines > $stmt_statements) {
553 return $stmt_statements;
557 sub ctx_statement_full {
558 my ($linenr, $remain, $off) = @_;
559 my ($statement, $condition, $level);
563 # Grab the first conditional/block pair.
564 ($statement, $condition, $linenr, $remain, $off, $level) =
565 ctx_statement_block($linenr, $remain, $off);
566 #print "F: c<$condition> s<$statement> remain<$remain>\n";
567 push(@chunks, [ $condition, $statement ]);
568 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
569 return ($level, $linenr, @chunks);
572 # Pull in the following conditional/block pairs and see if they
573 # could continue the statement.
575 ($statement, $condition, $linenr, $remain, $off, $level) =
576 ctx_statement_block($linenr, $remain, $off);
577 #print "C: c<$condition> s<$statement> remain<$remain>\n";
578 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
580 push(@chunks, [ $condition, $statement ]);
583 return ($level, $linenr, @chunks);
587 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
589 my $start = $linenr - 1;
596 my @stack = ($level);
597 for ($line = $start; $remain > 0; $line++) {
598 next if ($rawlines[$line] =~ /^-/);
601 $blk .= $rawlines[$line];
603 # Handle nested #if/#else.
604 if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
605 push(@stack, $level);
606 } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
607 $level = $stack[$#stack - 1];
608 } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
609 $level = pop(@stack);
612 foreach my $c (split(//, $rawlines[$line])) {
613 ##print "C<$c>L<$level><$open$close>O<$off>\n";
619 if ($c eq $close && $level > 0) {
621 last if ($level == 0);
622 } elsif ($c eq $open) {
627 if (!$outer || $level <= 1) {
628 push(@res, $rawlines[$line]);
631 last if ($level == 0);
634 return ($level, @res);
636 sub ctx_block_outer {
637 my ($linenr, $remain) = @_;
639 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
643 my ($linenr, $remain) = @_;
645 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
649 my ($linenr, $remain, $off) = @_;
651 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
654 sub ctx_block_level {
655 my ($linenr, $remain) = @_;
657 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
659 sub ctx_statement_level {
660 my ($linenr, $remain, $off) = @_;
662 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
665 sub ctx_locate_comment {
666 my ($first_line, $end_line) = @_;
668 # Catch a comment on the end of the line itself.
669 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
670 return $current_comment if (defined $current_comment);
672 # Look through the context and try and figure out if there is a
675 $current_comment = '';
676 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
677 my $line = $rawlines[$linenr - 1];
679 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
682 if ($line =~ m@/\*@) {
685 if (!$in_comment && $current_comment ne '') {
686 $current_comment = '';
688 $current_comment .= $line . "\n" if ($in_comment);
689 if ($line =~ m@\*/@) {
694 chomp($current_comment);
695 return($current_comment);
697 sub ctx_has_comment {
698 my ($first_line, $end_line) = @_;
699 my $cmt = ctx_locate_comment($first_line, $end_line);
701 ##print "LINE: $rawlines[$end_line - 1 ]\n";
702 ##print "CMMT: $cmt\n";
708 my ($linenr, $cnt) = @_;
710 my $offset = $linenr - 1;
715 $line = $rawlines[$offset++];
716 next if (defined($line) && $line =~ /^-/);
728 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
731 $coded = sprintf("^%c", unpack('C', $2) + 64);
740 my $av_preprocessor = 0;
746 $av_preprocessor = 0;
748 @av_paren_type = ('E');
749 $av_pend_colon = 'O';
752 sub annotate_values {
753 my ($stream, $type) = @_;
756 my $var = '_' x length($stream);
759 print "$stream\n" if ($dbg_values > 1);
761 while (length($cur)) {
762 @av_paren_type = ('E') if ($#av_paren_type < 0);
763 print " <" . join('', @av_paren_type) .
764 "> <$type> <$av_pending>" if ($dbg_values > 1);
765 if ($cur =~ /^(\s+)/o) {
766 print "WS($1)\n" if ($dbg_values > 1);
767 if ($1 =~ /\n/ && $av_preprocessor) {
768 $type = pop(@av_paren_type);
769 $av_preprocessor = 0;
772 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
773 print "DECLARE($1)\n" if ($dbg_values > 1);
776 } elsif ($cur =~ /^($Modifier)\s*/) {
777 print "MODIFIER($1)\n" if ($dbg_values > 1);
780 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
781 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
782 $av_preprocessor = 1;
783 push(@av_paren_type, $type);
789 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
790 print "UNDEF($1)\n" if ($dbg_values > 1);
791 $av_preprocessor = 1;
792 push(@av_paren_type, $type);
794 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
795 print "PRE_START($1)\n" if ($dbg_values > 1);
796 $av_preprocessor = 1;
798 push(@av_paren_type, $type);
799 push(@av_paren_type, $type);
802 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
803 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
804 $av_preprocessor = 1;
806 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
810 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
811 print "PRE_END($1)\n" if ($dbg_values > 1);
813 $av_preprocessor = 1;
815 # Assume all arms of the conditional end as this
816 # one does, and continue as if the #endif was not here.
818 push(@av_paren_type, $type);
821 } elsif ($cur =~ /^(\\\n)/o) {
822 print "PRECONT($1)\n" if ($dbg_values > 1);
824 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
825 print "ATTR($1)\n" if ($dbg_values > 1);
829 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
830 print "SIZEOF($1)\n" if ($dbg_values > 1);
836 } elsif ($cur =~ /^(if|while|for)\b/o) {
837 print "COND($1)\n" if ($dbg_values > 1);
841 } elsif ($cur =~/^(case)/o) {
842 print "CASE($1)\n" if ($dbg_values > 1);
843 $av_pend_colon = 'C';
846 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
847 print "KEYWORD($1)\n" if ($dbg_values > 1);
850 } elsif ($cur =~ /^(\()/o) {
851 print "PAREN('$1')\n" if ($dbg_values > 1);
852 push(@av_paren_type, $av_pending);
856 } elsif ($cur =~ /^(\))/o) {
857 my $new_type = pop(@av_paren_type);
858 if ($new_type ne '_') {
860 print "PAREN('$1') -> $type\n"
861 if ($dbg_values > 1);
863 print "PAREN('$1')\n" if ($dbg_values > 1);
866 } elsif ($cur =~ /^($Ident)\s*\(/o) {
867 print "FUNC($1)\n" if ($dbg_values > 1);
871 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
872 if (defined $2 && $type eq 'C' || $type eq 'T') {
873 $av_pend_colon = 'B';
874 } elsif ($type eq 'E') {
875 $av_pend_colon = 'L';
877 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
880 } elsif ($cur =~ /^($Ident|$Constant)/o) {
881 print "IDENT($1)\n" if ($dbg_values > 1);
884 } elsif ($cur =~ /^($Assignment)/o) {
885 print "ASSIGN($1)\n" if ($dbg_values > 1);
888 } elsif ($cur =~/^(;|{|})/) {
889 print "END($1)\n" if ($dbg_values > 1);
891 $av_pend_colon = 'O';
893 } elsif ($cur =~/^(,)/) {
894 print "COMMA($1)\n" if ($dbg_values > 1);
897 } elsif ($cur =~ /^(\?)/o) {
898 print "QUESTION($1)\n" if ($dbg_values > 1);
901 } elsif ($cur =~ /^(:)/o) {
902 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
904 substr($var, length($res), 1, $av_pend_colon);
905 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
910 $av_pend_colon = 'O';
912 } elsif ($cur =~ /^(\[)/o) {
913 print "CLOSE($1)\n" if ($dbg_values > 1);
916 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
919 print "OPV($1)\n" if ($dbg_values > 1);
926 substr($var, length($res), 1, $variant);
929 } elsif ($cur =~ /^($Operators)/o) {
930 print "OP($1)\n" if ($dbg_values > 1);
931 if ($1 ne '++' && $1 ne '--') {
935 } elsif ($cur =~ /(^.)/o) {
936 print "C($1)\n" if ($dbg_values > 1);
939 $cur = substr($cur, length($1));
940 $res .= $type x length($1);
948 my ($possible, $line) = @_;
950 print "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
951 if ($possible !~ /(?:
964 ^(?:typedef|struct|enum)\b
966 # Check for modifiers.
967 $possible =~ s/\s*$Storage\s*//g;
968 $possible =~ s/\s*$Sparse\s*//g;
969 if ($possible =~ /^\s*$/) {
971 } elsif ($possible =~ /\s/) {
972 $possible =~ s/\s*$Type\s*//g;
973 for my $modifier (split(' ', $possible)) {
974 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
975 push(@modifierList, $modifier);
979 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
980 push(@typeList, $possible);
984 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
991 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
994 my $line = $prefix . $_[0];
996 $line = (split('\n', $line))[0] . "\n" if ($terse);
998 push(our @report, $line);
1006 if (report("ERROR: $_[0]\n")) {
1012 if (report("WARNING: $_[0]\n")) {
1018 if ($check && report("CHECK: $_[0]\n")) {
1024 sub check_absolute_file {
1025 my ($absolute, $herecurr) = @_;
1026 my $file = $absolute;
1028 ##print "absolute<$absolute>\n";
1030 # See if any suffix of this path is a path within the tree.
1031 while ($file =~ s@^[^/]*/@@) {
1032 if (-f "$root/$file") {
1033 ##print "file<$file>\n";
1041 # It is, so see if the prefix is acceptable.
1042 my $prefix = $absolute;
1043 substr($prefix, -length($file)) = '';
1045 ##print "prefix<$prefix>\n";
1046 if ($prefix ne ".../") {
1047 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1052 my $filename = shift;
1058 my $stashrawline="";
1075 # Trace the real file/line as we go.
1081 my $comment_edge = 0;
1085 my $prev_values = 'E';
1088 my %suppress_ifbraces;
1089 my %suppress_whiletrailers;
1091 # Pre-scan the patch sanitizing the lines.
1092 # Pre-scan the patch looking for any __setup documentation.
1094 my @setup_docs = ();
1097 sanitise_line_reset();
1099 foreach my $rawline (@rawlines) {
1103 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1105 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1110 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1119 # Guestimate if this is a continuing comment. Run
1120 # the context looking for a comment "edge". If this
1121 # edge is a close comment then we must be in a comment
1125 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1126 next if (defined $rawlines[$ln - 1] &&
1127 $rawlines[$ln - 1] =~ /^-/);
1129 #print "RAW<$rawlines[$ln - 1]>\n";
1130 last if (!defined $rawlines[$ln - 1]);
1131 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1132 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1137 if (defined $edge && $edge eq '*/') {
1141 # Guestimate if this is a continuing comment. If this
1142 # is the start of a diff block and this line starts
1143 # ' *' then it is very likely a comment.
1144 if (!defined $edge &&
1145 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1150 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1151 sanitise_line_reset($in_comment);
1153 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1154 # Standardise the strings and chars within the input to
1155 # simplify matching -- only bother with positive lines.
1156 $line = sanitise_line($rawline);
1158 push(@lines, $line);
1161 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1166 #print "==>$rawline\n";
1167 #print "-->$line\n";
1169 if ($setup_docs && $line =~ /^\+/) {
1170 push(@setup_docs, $line);
1178 foreach my $line (@lines) {
1181 my $rawline = $rawlines[$linenr - 1];
1182 my $hunk_line = ($realcnt != 0);
1184 #extract the line range in the file after the patch is applied
1185 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1187 $first_line = $linenr + 1;
1197 %suppress_ifbraces = ();
1198 %suppress_whiletrailers = ();
1201 # track the line number as we move through the hunk, note that
1202 # new versions of GNU diff omit the leading space on completely
1203 # blank context lines so we need to count that too.
1204 } elsif ($line =~ /^( |\+|$)/) {
1206 $realcnt-- if ($realcnt != 0);
1208 # Measure the line length and indent.
1209 ($length, $indent) = line_stats($rawline);
1211 # Track the previous line.
1212 ($prevline, $stashline) = ($stashline, $line);
1213 ($previndent, $stashindent) = ($stashindent, $indent);
1214 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1216 #warn "line<$line>\n";
1218 } elsif ($realcnt == 1) {
1222 #make up the handle for any error we report on this line
1223 $prefix = "$filename:$realline: " if ($emacs && $file);
1224 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1226 $here = "#$linenr: " if (!$file);
1227 $here = "#$realline: " if ($file);
1229 # extract the filename as it passes
1230 if ($line=~/^\+\+\+\s+(\S+)/) {
1232 $realfile =~ s@^([^/]*)/@@;
1235 if ($tree && $p1_prefix ne '' && -e "$root/$p1_prefix") {
1236 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1239 if ($realfile =~ m@^include/asm/@) {
1240 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1245 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1247 my $hereline = "$here\n$rawline\n";
1248 my $herecurr = "$here\n$rawline\n";
1249 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1251 $cnt_lines++ if ($realcnt != 0);
1253 #check the patch for a signoff:
1254 if ($line =~ /^\s*signed-off-by:/i) {
1255 # This is a signoff, if ugly, so do not double report.
1257 if (!($line =~ /^\s*Signed-off-by:/)) {
1258 WARN("Signed-off-by: is the preferred form\n" .
1261 if ($line =~ /^\s*signed-off-by:\S/i) {
1262 WARN("space required after Signed-off-by:\n" .
1267 # Check for wrappage within a valid hunk of the file
1268 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1269 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1270 $herecurr) if (!$emitted_corrupt++);
1273 # Check for absolute kernel paths.
1275 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1278 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1279 check_absolute_file($1, $herecurr)) {
1282 check_absolute_file($file, $herecurr);
1287 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1288 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1289 $rawline !~ m/^$UTF8*$/) {
1290 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1292 my $blank = copy_spacing($rawline);
1293 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1294 my $hereptr = "$hereline$ptr\n";
1296 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1299 # ignore non-hunk lines and lines being removed
1300 next if (!$hunk_line || $line =~ /^-/);
1302 #trailing whitespace
1303 if ($line =~ /^\+.*\015/) {
1304 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1305 ERROR("DOS line endings\n" . $herevet);
1307 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1308 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1309 ERROR("trailing whitespace\n" . $herevet);
1312 # check we are in a valid source file if not then ignore this hunk
1313 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1316 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1317 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1318 $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1321 WARN("line over 80 characters\n" . $herecurr);
1324 # check for adding lines without a newline.
1325 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1326 WARN("adding a line without newline at end of file\n" . $herecurr);
1329 # check we are in a valid source file C or perl if not then ignore this hunk
1330 next if ($realfile !~ /\.(h|c|pl)$/);
1332 # at the beginning of a line any tabs must come first and anything
1333 # more than 8 must use tabs.
1334 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1335 $rawline =~ /^\+\s* \s*/) {
1336 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1337 ERROR("code indent should use tabs where possible\n" . $herevet);
1340 # check we are in a valid C source file if not then ignore this hunk
1341 next if ($realfile !~ /\.(h|c)$/);
1343 # check for RCS/CVS revision markers
1344 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1345 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1348 # Check for potential 'bare' types
1349 my ($stat, $cond, $line_nr_next, $remain_next, $off_next);
1350 if ($realcnt && $line =~ /.\s*\S/) {
1351 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1352 ctx_statement_block($linenr, $realcnt, 0);
1353 $stat =~ s/\n./\n /g;
1354 $cond =~ s/\n./\n /g;
1359 # Ignore goto labels.
1360 if ($s =~ /$Ident:\*$/s) {
1362 # Ignore functions being called
1363 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1365 # declarations always start with types
1366 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) {
1369 possible($type, "A:" . $s);
1371 # definitions in global scope can only start with types
1372 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1373 possible($1, "B:" . $s);
1376 # any (foo ... *) is a pointer cast, and foo is a type
1377 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1378 possible($1, "C:" . $s);
1381 # Check for any sort of function declaration.
1382 # int foo(something bar, other baz);
1383 # void (*store_gdt)(x86_descr_ptr *);
1384 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1385 my ($name_len) = length($1);
1388 substr($ctx, 0, $name_len + 1, '');
1389 $ctx =~ s/\)[^\)]*$//;
1391 for my $arg (split(/\s*,\s*/, $ctx)) {
1392 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1394 possible($1, "D:" . $s);
1402 # Checks which may be anchored in the context.
1405 # Check for switch () and associated case and default
1406 # statements should be at the same indent.
1407 if ($line=~/\bswitch\s*\(.*\)/) {
1410 my @ctx = ctx_block_outer($linenr, $realcnt);
1412 for my $ctx (@ctx) {
1413 my ($clen, $cindent) = line_stats($ctx);
1414 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1415 $indent != $cindent) {
1416 $err .= "$sep$ctx\n";
1423 ERROR("switch and case should be at the same indent\n$hereline$err");
1427 # if/while/etc brace do not go on next line, unless defining a do while loop,
1428 # or if that brace on the next line is for something else
1429 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1430 my $pre_ctx = "$1$2";
1432 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1433 my $ctx_cnt = $realcnt - $#ctx - 1;
1434 my $ctx = join("\n", @ctx);
1436 my $ctx_ln = $linenr;
1437 my $ctx_skip = $realcnt;
1439 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1440 defined $lines[$ctx_ln - 1] &&
1441 $lines[$ctx_ln - 1] =~ /^-/)) {
1442 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1443 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1447 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1448 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1450 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1451 ERROR("that open brace { should be on the previous line\n" .
1452 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
1454 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1455 $ctx =~ /\)\s*\;\s*$/ &&
1456 defined $lines[$ctx_ln - 1])
1458 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1459 if ($nindent > $indent) {
1460 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1461 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
1466 # Check relative indent for conditionals and blocks.
1467 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1468 my ($s, $c) = ($stat, $cond);
1470 substr($s, 0, length($c), '');
1472 # Make sure we remove the line prefixes as we have
1473 # none on the first line, and are going to readd them
1477 # Find out how long the conditional actually is.
1478 my @newlines = ($c =~ /\n/gs);
1479 my $cond_lines = 1 + $#newlines;
1481 # We want to check the first line inside the block
1482 # starting at the end of the conditional, so remove:
1483 # 1) any blank line termination
1484 # 2) any opening brace { on end of the line
1486 my $continuation = 0;
1488 $s =~ s/^.*\bdo\b//;
1490 if ($s =~ s/^\s*\\//) {
1493 if ($s =~ s/^\s*?\n//) {
1498 # Also ignore a loop construct at the end of a
1499 # preprocessor statement.
1500 if (($prevline =~ /^.\s*#\s*define\s/ ||
1501 $prevline =~ /\\\s*$/) && $continuation == 0) {
1507 while ($cond_ptr != $cond_lines) {
1508 $cond_ptr = $cond_lines;
1510 # If we see an #else/#elif then the code
1512 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1517 # 1) blank lines, they should be at 0,
1518 # 2) preprocessor lines, and
1520 if ($continuation ||
1522 $s =~ /^\s*#\s*?/ ||
1523 $s =~ /^\s*$Ident\s*:/) {
1524 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1530 my (undef, $sindent) = line_stats("+" . $s);
1531 my $stat_real = raw_line($linenr, $cond_lines);
1533 # Check if either of these lines are modified, else
1534 # this is not this patch's fault.
1535 if (!defined($stat_real) ||
1536 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1539 if (defined($stat_real) && $cond_lines > 1) {
1540 $stat_real = "[...]\n$stat_real";
1543 #print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
1545 if ($check && (($sindent % 8) != 0 ||
1546 ($sindent <= $indent && $s ne ''))) {
1547 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1551 # Track the 'values' across context and added lines.
1552 my $opline = $line; $opline =~ s/^./ /;
1553 my ($curr_values, $curr_vars) =
1554 annotate_values($opline . "\n", $prev_values);
1555 $curr_values = $prev_values . $curr_values;
1557 my $outline = $opline; $outline =~ s/\t/ /g;
1558 print "$linenr > .$outline\n";
1559 print "$linenr > $curr_values\n";
1560 print "$linenr > $curr_vars\n";
1562 $prev_values = substr($curr_values, -1);
1564 #ignore lines not being added
1565 if ($line=~/^[^\+]/) {next;}
1567 # TEST: allow direct testing of the type matcher.
1569 if ($line =~ /^.\s*$Declare\s*$/) {
1570 ERROR("TEST: is type\n" . $herecurr);
1571 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1572 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1576 # TEST: allow direct testing of the attribute matcher.
1578 if ($line =~ /^.\s*$Attribute\s*$/) {
1579 ERROR("TEST: is attr\n" . $herecurr);
1580 } elsif ($dbg_attr > 1 && $line =~ /^.+($Attribute)/) {
1581 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1586 # check for initialisation to aggregates open brace on the next line
1587 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1588 $line =~ /^.\s*{/) {
1589 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1593 # Checks which are anchored on the added line.
1596 # check for malformed paths in #include statements (uses RAW line)
1597 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1599 if ($path =~ m{//}) {
1600 ERROR("malformed #include filename\n" .
1605 # no C99 // comments
1606 if ($line =~ m{//}) {
1607 ERROR("do not use C99 // comments\n" . $herecurr);
1609 # Remove C99 comments.
1611 $opline =~ s@//.*@@;
1613 #EXPORT_SYMBOL should immediately follow its function closing }.
1614 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1615 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1617 if ($prevline !~ /(?:
1619 ^.DEFINE_$Ident\(\Q$name\E\)|
1620 ^.DECLARE_$Ident\(\Q$name\E\)|
1621 ^.LIST_HEAD\(\Q$name\E\)|
1622 ^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1623 \b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)
1625 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1629 # check for external initialisers.
1630 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1631 ERROR("do not initialise externals to 0 or NULL\n" .
1634 # check for static initialisers.
1635 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1636 ERROR("do not initialise statics to 0 or NULL\n" .
1640 # check for new typedefs, only function parameters and sparse annotations
1642 if ($line =~ /\btypedef\s/ &&
1643 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1644 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
1645 $line !~ /\b$typeTypedefs\b/ &&
1646 $line !~ /\b__bitwise(?:__|)\b/) {
1647 WARN("do not add new typedefs\n" . $herecurr);
1650 # * goes on variable not on type
1652 if ($line =~ m{\($NonptrType(\s*\*[\s\*]*(?:$Modifier\s*)*)\)}) {
1653 my ($from, $to) = ($1, $1);
1655 # Should start with a space.
1656 $to =~ s/^(\S)/ $1/;
1657 # Should not end with a space.
1659 # '*'s should not have spaces between.
1660 while ($to =~ s/(.)\s\*/$1\*/) {
1663 #print "from<$from> to<$to>\n";
1665 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1667 } elsif ($line =~ m{\b$NonptrType(\s*\*[\s\*]*(?:$Modifier\s*)?)($Ident)}) {
1668 my ($from, $to, $ident) = ($1, $1, $2);
1670 # Should start with a space.
1671 $to =~ s/^(\S)/ $1/;
1672 # Should not end with a space.
1674 # '*'s should not have spaces between.
1675 while ($to =~ s/(.)\s\*/$1\*/) {
1677 # Modifiers should have spaces.
1678 $to =~ s/(\b$Modifier$)/$1 /;
1680 #print "from<$from> to<$to>\n";
1682 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1686 # # no BUG() or BUG_ON()
1687 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1688 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1689 # print "$herecurr";
1693 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1694 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1697 # printk should use KERN_* levels. Note that follow on printk's on the
1698 # same line do not need a level, so we use the current block context
1699 # to try and find and validate the current printk. In summary the current
1700 # printk includes all preceeding printk's which have no newline on the end.
1701 # we assume the first bad printk is the one to report.
1702 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1704 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1705 #print "CHECK<$lines[$ln - 1]\n";
1706 # we have a preceeding printk if it ends
1707 # with "\n" ignore it, else it is to blame
1708 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1709 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1716 WARN("printk() should include KERN_ facility level\n" . $herecurr);
1720 # function brace can't be on same line, except for #defines of do while,
1721 # or if closed on same line
1722 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1723 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1724 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1727 # open braces for enum, union and struct go on the same line.
1728 if ($line =~ /^.\s*{/ &&
1729 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1730 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1733 # check for spacing round square brackets; allowed:
1734 # 1. with a type on the left -- int [] a;
1735 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1736 # 3. inside a curly brace -- = { [0...10] = 5 }
1737 while ($line =~ /(.*?\s)\[/g) {
1738 my ($where, $prefix) = ($-[1], $1);
1739 if ($prefix !~ /$Type\s+$/ &&
1740 ($where != 0 || $prefix !~ /^.\s+$/) &&
1741 $prefix !~ /{\s+$/) {
1742 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1746 # check for spaces between functions and their parentheses.
1747 while ($line =~ /($Ident)\s+\(/g) {
1749 my $ctx_before = substr($line, 0, $-[1]);
1750 my $ctx = "$ctx_before$name";
1752 # Ignore those directives where spaces _are_ permitted.
1754 if|for|while|switch|return|case|
1755 volatile|__volatile__|
1756 __attribute__|format|__extension__|
1760 # cpp #define statements have non-optional spaces, ie
1761 # if there is a space between the name and the open
1762 # parenthesis it is simply not a parameter group.
1763 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1765 # cpp #elif statement condition may start with a (
1766 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1768 # If this whole things ends with a type its most
1769 # likely a typedef for a function.
1770 } elsif ($ctx =~ /$Type$/) {
1773 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1776 # Check operator spacing.
1777 if (!($line=~/\#\s*include/)) {
1779 <<=|>>=|<=|>=|==|!=|
1780 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1781 =>|->|<<|>>|<|>|=|!|~|
1782 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1785 my @elements = split(/($ops|;)/, $opline);
1788 my $blank = copy_spacing($opline);
1790 for (my $n = 0; $n < $#elements; $n += 2) {
1791 $off += length($elements[$n]);
1793 # Pick up the preceeding and succeeding characters.
1794 my $ca = substr($opline, 0, $off);
1796 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1797 $cc = substr($opline, $off + length($elements[$n + 1]));
1799 my $cb = "$ca$;$cc";
1802 $a = 'V' if ($elements[$n] ne '');
1803 $a = 'W' if ($elements[$n] =~ /\s$/);
1804 $a = 'C' if ($elements[$n] =~ /$;$/);
1805 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1806 $a = 'O' if ($elements[$n] eq '');
1807 $a = 'E' if ($ca =~ /^\s*$/);
1809 my $op = $elements[$n + 1];
1812 if (defined $elements[$n + 2]) {
1813 $c = 'V' if ($elements[$n + 2] ne '');
1814 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1815 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1816 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1817 $c = 'O' if ($elements[$n + 2] eq '');
1818 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
1823 my $ctx = "${a}x${c}";
1825 my $at = "(ctx:$ctx)";
1827 my $ptr = substr($blank, 0, $off) . "^";
1828 my $hereptr = "$hereline$ptr\n";
1830 # Pull out the value of this operator.
1831 my $op_type = substr($curr_values, $off + 1, 1);
1833 # Get the full operator variant.
1834 my $opv = $op . substr($curr_vars, $off, 1);
1836 # Ignore operators passed as parameters.
1837 if ($op_type ne 'V' &&
1838 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1841 # } elsif ($op =~ /^$;+$/) {
1843 # ; should have either the end of line or a space or \ after it
1844 } elsif ($op eq ';') {
1845 if ($ctx !~ /.x[WEBC]/ &&
1846 $cc !~ /^\\/ && $cc !~ /^;/) {
1847 ERROR("space required after that '$op' $at\n" . $hereptr);
1851 } elsif ($op eq '//') {
1855 # : when part of a bitfield
1856 } elsif ($op eq '->' || $opv eq ':B') {
1857 if ($ctx =~ /Wx.|.xW/) {
1858 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
1861 # , must have a space on the right.
1862 } elsif ($op eq ',') {
1863 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1864 ERROR("space required after that '$op' $at\n" . $hereptr);
1867 # '*' as part of a type definition -- reported already.
1868 } elsif ($opv eq '*_') {
1869 #warn "'*' is part of type\n";
1871 # unary operators should have a space before and
1872 # none after. May be left adjacent to another
1873 # unary operator, or a cast
1874 } elsif ($op eq '!' || $op eq '~' ||
1875 $opv eq '*U' || $opv eq '-U' ||
1876 $opv eq '&U' || $opv eq '&&U') {
1877 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1878 ERROR("space required before that '$op' $at\n" . $hereptr);
1880 if ($op eq '*' && $cc =~/\s*const\b/) {
1881 # A unary '*' may be const
1883 } elsif ($ctx =~ /.xW/) {
1884 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1887 # unary ++ and unary -- are allowed no space on one side.
1888 } elsif ($op eq '++' or $op eq '--') {
1889 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1890 ERROR("space required one side of that '$op' $at\n" . $hereptr);
1892 if ($ctx =~ /Wx[BE]/ ||
1893 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1894 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1896 if ($ctx =~ /ExW/) {
1897 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1901 # << and >> may either have or not have spaces both sides
1902 } elsif ($op eq '<<' or $op eq '>>' or
1903 $op eq '&' or $op eq '^' or $op eq '|' or
1904 $op eq '+' or $op eq '-' or
1905 $op eq '*' or $op eq '/' or
1908 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1909 ERROR("need consistent spacing around '$op' $at\n" .
1913 # A colon needs no spaces before when it is
1914 # terminating a case value or a label.
1915 } elsif ($opv eq ':C' || $opv eq ':L') {
1916 if ($ctx =~ /Wx./) {
1917 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1920 # All the others need spaces both sides.
1921 } elsif ($ctx !~ /[EWC]x[CWE]/) {
1924 # Ignore email addresses <foo@bar>
1926 $cc =~ /^\S+\@\S+>/) ||
1928 $ca =~ /<\S+\@\S+$/))
1934 if (($opv eq ':O' && $ca =~ /\?$/) ||
1935 ($op eq '?' && $cc =~ /^:/)) {
1940 ERROR("spaces required around that '$op' $at\n" . $hereptr);
1943 $off += length($elements[$n + 1]);
1947 # check for multiple assignments
1948 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
1949 CHK("multiple assignments should be avoided\n" . $herecurr);
1952 ## # check for multiple declarations, allowing for a function declaration
1954 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1955 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1957 ## # Remove any bracketed sections to ensure we do not
1958 ## # falsly report the parameters of functions.
1960 ## while ($ln =~ s/\([^\(\)]*\)//g) {
1962 ## if ($ln =~ /,/) {
1963 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1967 #need space before brace following if, while, etc
1968 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1970 ERROR("space required before the open brace '{'\n" . $herecurr);
1973 # closing brace should have a space following it when it has anything
1975 if ($line =~ /}(?!(?:,|;|\)))\S/) {
1976 ERROR("space required after that close brace '}'\n" . $herecurr);
1979 # check spacing on square brackets
1980 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1981 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
1983 if ($line =~ /\s\]/) {
1984 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
1987 # check spacing on parentheses
1988 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1989 $line !~ /for\s*\(\s+;/) {
1990 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
1992 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1993 $line !~ /for\s*\(.*;\s+\)/ &&
1994 $line !~ /:\s+\)/) {
1995 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
1998 #goto labels aren't indented, allow a single space however
1999 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2000 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2001 WARN("labels should not be indented\n" . $herecurr);
2004 # Return is not a function.
2005 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2009 # Flatten any parentheses
2010 $value =~ s/\)\(/\) \(/g;
2011 while ($value !~ /(?:$Ident|-?$Constant)\s*$Compare\s*(?:$Ident|-?$Constant)/ && $value =~ s/\([^\(\)]*\)/1/) {
2014 if ($value =~ /^(?:$Ident|-?$Constant)$/) {
2015 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2017 } elsif ($spacing !~ /\s+/) {
2018 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2022 # Need a space before open parenthesis after if, while etc
2023 if ($line=~/\b(if|while|for|switch)\(/) {
2024 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2027 # Check for illegal assignment in if conditional -- and check for trailing
2028 # statements after the conditional.
2029 if ($line =~ /do\s*(?!{)/) {
2030 my ($stat_next) = ctx_statement_block($line_nr_next,
2031 $remain_next, $off_next);
2032 $stat_next =~ s/\n./\n /g;
2033 ##print "stat<$stat> stat_next<$stat_next>\n";
2035 if ($stat_next =~ /^\s*while\b/) {
2036 # If the statement carries leading newlines,
2037 # then count those as offsets.
2039 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2041 statement_rawlines($whitespace) - 1;
2043 $suppress_whiletrailers{$line_nr_next +
2047 if (!defined $suppress_whiletrailers{$linenr} &&
2048 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2049 my ($s, $c) = ($stat, $cond);
2051 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2052 ERROR("do not use assignment in if condition\n" . $herecurr);
2055 # Find out what is on the end of the line after the
2057 substr($s, 0, length($c), '');
2059 $s =~ s/$;//g; # Remove any comments
2060 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2061 $c !~ /}\s*while\s*/)
2063 # Find out how long the conditional actually is.
2064 my @newlines = ($c =~ /\n/gs);
2065 my $cond_lines = 1 + $#newlines;
2067 my $stat_real = raw_line($linenr, $cond_lines);
2068 if (defined($stat_real) && $cond_lines > 1) {
2069 $stat_real = "[...]\n$stat_real";
2072 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2076 # Check for bitwise tests written as boolean
2088 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2091 # if and else should not have general statements after it
2092 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2094 $s =~ s/$;//g; # Remove any comments
2095 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2096 ERROR("trailing statements should be on next line\n" . $herecurr);
2099 # case and default should not have general statements after them
2100 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2102 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2106 ERROR("trailing statements should be on next line\n" . $herecurr);
2109 # Check for }<nl>else {, these must be at the same
2110 # indent level to be relevant to each other.
2111 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2112 $previndent == $indent) {
2113 ERROR("else should follow close brace '}'\n" . $hereprev);
2116 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2117 $previndent == $indent) {
2118 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2120 # Find out what is on the end of the line after the
2122 substr($s, 0, length($c), '');
2125 if ($s =~ /^\s*;/) {
2126 ERROR("while should follow close brace '}'\n" . $hereprev);
2130 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2131 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2132 # print "No studly caps, use _\n";
2133 # print "$herecurr";
2137 #no spaces allowed after \ in define
2138 if ($line=~/\#\s*define.*\\\s$/) {
2139 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
2142 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2143 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2145 my $checkfile = "include/linux/$file";
2146 if (-f "$root/$checkfile" &&
2147 $realfile ne $checkfile &&
2150 if ($realfile =~ m{^arch/}) {
2151 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2153 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2158 # multi-statement macros should be enclosed in a do while loop, grab the
2159 # first statement and ensure its the whole macro if its not enclosed
2160 # in a known good container
2161 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2162 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2165 my ($off, $dstat, $dcond, $rest);
2168 my $args = defined($1);
2170 # Find the end of the macro and limit our statement
2172 while ($cnt > 0 && defined $lines[$ln - 1] &&
2173 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2175 $ctx .= $rawlines[$ln - 1] . "\n";
2176 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2179 $ctx .= $rawlines[$ln - 1];
2181 ($dstat, $dcond, $ln, $cnt, $off) =
2182 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2183 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2184 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2186 # Extract the remainder of the define (if any) and
2187 # rip off surrounding spaces, and trailing \'s.
2189 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2190 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2191 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2192 $rest .= substr($lines[$ln - 1], $off) . "\n";
2198 $rest =~ s/\\\n.//g;
2202 # Clean up the original statement.
2204 substr($dstat, 0, length($dcond), '');
2206 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2209 $dstat =~ s/\\\n.//g;
2210 $dstat =~ s/^\s*//s;
2211 $dstat =~ s/\s*$//s;
2213 # Flatten any parentheses and braces
2214 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2215 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2216 $dstat =~ s/\[[^\{\}]*\]/1/)
2220 my $exceptions = qr{
2229 #print "REST<$rest> dstat<$dstat>\n";
2231 if ($rest !~ /while\s*\(/ &&
2232 $dstat !~ /$exceptions/)
2234 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2237 } elsif ($ctx !~ /;/) {
2239 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2240 $dstat !~ /$exceptions/ &&
2241 $dstat !~ /^\.$Ident\s*=/ &&
2242 $dstat =~ /$Operators/)
2244 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2249 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2250 # all assignments may have only one of the following with an assignment:
2253 # VMLINUX_SYMBOL(...)
2254 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2255 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2258 # check for redundant bracing round if etc
2259 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2260 my ($level, $endln, @chunks) =
2261 ctx_statement_full($linenr, $realcnt, 1);
2262 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2263 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2264 if ($#chunks > 0 && $level == 0) {
2267 my $herectx = $here . "\n";
2268 my $ln = $linenr - 1;
2269 for my $chunk (@chunks) {
2270 my ($cond, $block) = @{$chunk};
2272 # If the condition carries leading newlines, then count those as offsets.
2273 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2274 my $offset = statement_rawlines($whitespace) - 1;
2276 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2278 # We have looked at and allowed this specific line.
2279 $suppress_ifbraces{$ln + $offset} = 1;
2281 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2282 $ln += statement_rawlines($block) - 1;
2284 substr($block, 0, length($cond), '');
2286 $seen++ if ($block =~ /^\s*{/);
2288 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2289 if (statement_lines($cond) > 1) {
2290 #print "APW: ALLOWED: cond<$cond>\n";
2293 if ($block =~/\b(?:if|for|while)\b/) {
2294 #print "APW: ALLOWED: block<$block>\n";
2297 if (statement_block_size($block) > 1) {
2298 #print "APW: ALLOWED: lines block<$block>\n";
2302 if ($seen && !$allowed) {
2303 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
2307 if (!defined $suppress_ifbraces{$linenr - 1} &&
2308 $line =~ /\b(if|while|for|else)\b/) {
2311 # Check the pre-context.
2312 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2313 #print "APW: ALLOWED: pre<$1>\n";
2317 my ($level, $endln, @chunks) =
2318 ctx_statement_full($linenr, $realcnt, $-[0]);
2320 # Check the condition.
2321 my ($cond, $block) = @{$chunks[0]};
2322 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2323 if (defined $cond) {
2324 substr($block, 0, length($cond), '');
2326 if (statement_lines($cond) > 1) {
2327 #print "APW: ALLOWED: cond<$cond>\n";
2330 if ($block =~/\b(?:if|for|while)\b/) {
2331 #print "APW: ALLOWED: block<$block>\n";
2334 if (statement_block_size($block) > 1) {
2335 #print "APW: ALLOWED: lines block<$block>\n";
2338 # Check the post-context.
2339 if (defined $chunks[1]) {
2340 my ($cond, $block) = @{$chunks[1]};
2341 if (defined $cond) {
2342 substr($block, 0, length($cond), '');
2344 if ($block =~ /^\s*\{/) {
2345 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2349 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2350 my $herectx = $here . "\n";;
2351 my $cnt = statement_rawlines($block);
2353 for (my $n = 0; $n < $cnt; $n++) {
2354 $herectx .= raw_line($linenr, $n) . "\n";;
2357 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2361 # don't include deprecated include files (uses RAW line)
2362 for my $inc (@dep_includes) {
2363 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2364 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2368 # don't use deprecated functions
2369 for my $func (@dep_functions) {
2370 if ($line =~ /\b$func\b/) {
2371 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2375 # no volatiles please
2376 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2377 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2378 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2381 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2382 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2383 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2387 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2388 CHK("if this code is redundant consider removing it\n" .
2392 # check for needless kfree() checks
2393 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2395 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2396 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2399 # check for needless usb_free_urb() checks
2400 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2402 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2403 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2407 # warn about #ifdefs in C files
2408 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2409 # print "#ifdef in C files should be avoided\n";
2410 # print "$herecurr";
2414 # warn about spacing in #ifdefs
2415 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2416 ERROR("exactly one space required after that #$1\n" . $herecurr);
2419 # check for spinlock_t definitions without a comment.
2420 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2421 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
2423 if (!ctx_has_comment($first_line, $linenr)) {
2424 CHK("$1 definition without comment\n" . $herecurr);
2427 # check for memory barriers without a comment.
2428 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2429 if (!ctx_has_comment($first_line, $linenr)) {
2430 CHK("memory barrier without comment\n" . $herecurr);
2433 # check of hardware specific defines
2434 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2435 CHK("architecture specific defines should be avoided\n" . $herecurr);
2438 # check the location of the inline attribute, that it is between
2439 # storage class and type.
2440 if ($line =~ /\b$Type\s+$Inline\b/ ||
2441 $line =~ /\b$Inline\s+$Storage\b/) {
2442 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2445 # Check for __inline__ and __inline, prefer inline
2446 if ($line =~ /\b(__inline__|__inline)\b/) {
2447 WARN("plain inline is preferred over $1\n" . $herecurr);
2450 # check for new externs in .c files.
2451 if ($realfile =~ /\.c$/ && defined $stat &&
2452 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2454 my $function_name = $1;
2455 my $paren_space = $2;
2458 if (defined $cond) {
2459 substr($s, 0, length($cond), '');
2461 if ($s =~ /^\s*;/ &&
2462 $function_name ne 'uninitialized_var')
2464 WARN("externs should be avoided in .c files\n" . $herecurr);
2467 if ($paren_space =~ /\n/) {
2468 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2471 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2472 $stat =~ /^.\s*extern\s+/)
2474 WARN("externs should be avoided in .c files\n" . $herecurr);
2477 # checks for new __setup's
2478 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2481 if (!grep(/$name/, @setup_docs)) {
2482 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2486 # check for pointless casting of kmalloc return
2487 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2488 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2491 # check for gcc specific __FUNCTION__
2492 if ($line =~ /__FUNCTION__/) {
2493 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2496 # check for semaphores used as mutexes
2497 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2498 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2500 # check for semaphores used as mutexes
2501 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2502 WARN("consider using a completion\n" . $herecurr);
2504 # recommend strict_strto* over simple_strto*
2505 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2506 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2508 # check for __initcall(), use device_initcall() explicitly please
2509 if ($line =~ /^.\s*__initcall\s*\(/) {
2510 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2513 # use of NR_CPUS is usually wrong
2514 # ignore definitions of NR_CPUS and usage to define arrays as likely right
2515 if ($line =~ /\bNR_CPUS\b/ &&
2516 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2517 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2518 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2519 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2520 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2522 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2525 # check for %L{u,d,i} in strings
2527 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2528 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2529 $string =~ s/%%/__/g;
2530 if ($string =~ /(?<!%)%L[udi]/) {
2531 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2536 # whine mightly about in_atomic
2537 if ($line =~ /\bin_atomic\s*\(/) {
2538 if ($realfile =~ m@^drivers/@) {
2539 ERROR("do not use in_atomic in drivers\n" . $herecurr);
2541 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2546 # If we have no input at all, then there is nothing to report on
2547 # so just keep quiet.
2548 if ($#rawlines == -1) {
2552 # In mailback mode only produce a report in the negative, for
2553 # things that appear to be patches.
2554 if ($mailback && ($clean == 1 || !$is_patch)) {
2558 # This is not a patch, and we are are in 'no-patch' mode so
2560 if (!$chk_patch && !$is_patch) {
2565 ERROR("Does not appear to be a unified-diff format patch\n");
2567 if ($is_patch && $chk_signoff && $signoff == 0) {
2568 ERROR("Missing Signed-off-by: line(s)\n");
2571 print report_dump();
2572 if ($summary && !($clean == 1 && $quiet == 1)) {
2573 print "$filename " if ($summary_file);
2574 print "total: $cnt_error errors, $cnt_warn warnings, " .
2575 (($check)? "$cnt_chk checks, " : "") .
2576 "$cnt_lines lines checked\n";
2577 print "\n" if ($quiet == 0);
2580 if ($clean == 1 && $quiet == 0) {
2581 print "$vname has no obvious style problems and is ready for submission.\n"
2583 if ($clean == 0 && $quiet == 0) {
2584 print "$vname has style problems, please review. If any of these errors\n";
2585 print "are false positives report them to the maintainer, see\n";
2586 print "CHECKPATCH in MAINTAINERS.\n";