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{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
133 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%
141 [\x09\x0A\x0D\x20-\x7E] # ASCII
142 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
143 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
144 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
145 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
146 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
147 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
148 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
151 our $typeTypedefs = qr{(?x
:
152 (?
:__
)?
(?
:u
|s
|be
|le)(?
:\d
|\d\d
)|
158 qr{(?:unsigned\s+)?char},
159 qr{(?:unsigned\s+)?short},
160 qr{(?:unsigned\s+)?int},
161 qr{(?:unsigned\s+)?long},
162 qr{(?:unsigned\s+)?long\s+int},
163 qr{(?:unsigned\s+)?long\s+long},
164 qr{(?:unsigned\s+)?long\s+long\s+int},
173 qr{${Ident}_handler
},
174 qr{${Ident}_handler_fn
},
176 our @modifierList = (
181 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
182 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
183 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
185 (?
:$Modifier\s
+|const\s
+)*
187 (?
:typeof
|__typeof__
)\s
*\
(\s
*\
**\s
*$Ident\s
*\
)|
191 (?
:\s
+$Modifier|\s
+const
)*
195 (?
:[\s\
*]+\s
*const
|[\s\
*]+|(?
:\s
*\
[\s
*\
])+)?
196 (?
:\s
+$Inline|\s
+$Modifier)*
198 $Declare = qr{(?:$Storage\s+)?$Type};
202 $chk_signoff = 0 if ($file);
204 my @dep_includes = ();
205 my @dep_functions = ();
206 my $removal = "Documentation/feature-removal-schedule.txt";
207 if ($tree && -f
"$root/$removal") {
208 open(REMOVE
, "<$root/$removal") ||
209 die "$P: $removal: open failed - $!\n";
211 if (/^Check:\s+(.*\S)/) {
212 for my $entry (split(/[, ]+/, $1)) {
213 if ($entry =~ m
@include/(.*)@
) {
214 push(@dep_includes, $1);
216 } elsif ($entry !~ m@
/@
) {
217 push(@dep_functions, $entry);
227 for my $filename (@ARGV) {
229 open(FILE
, "diff -u /dev/null $filename|") ||
230 die "$P: $filename: diff failed - $!\n";
232 open(FILE
, "<$filename") ||
233 die "$P: $filename: open failed - $!\n";
235 if ($filename eq '-') {
236 $vname = 'Your patch';
245 if (!process
($filename)) {
254 sub top_of_kernel_tree
{
258 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
259 "README", "Documentation", "arch", "include", "drivers",
260 "fs", "init", "ipc", "kernel", "lib", "scripts",
263 foreach my $check (@tree_check) {
264 if (! -e
$root . '/' . $check) {
276 for my $c (split(//, $str)) {
280 for (; ($n % 8) != 0; $n++) {
292 (my $res = shift) =~ tr/\t/ /c;
299 # Drop the diff line leader and expand tabs
301 $line = expand_tabs
($line);
303 # Pick the indent from the front of the line.
304 my ($white) = ($line =~ /^(\s*)/);
306 return (length($line), length($white));
309 my $sanitise_quote = '';
311 sub sanitise_line_reset
{
312 my ($in_comment) = @_;
315 $sanitise_quote = '*/';
317 $sanitise_quote = '';
330 # Always copy over the diff marker.
331 $res = substr($line, 0, 1);
333 for ($off = 1; $off < length($line); $off++) {
334 $c = substr($line, $off, 1);
336 # Comments we are wacking completly including the begin
337 # and end, all to $;.
338 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
339 $sanitise_quote = '*/';
341 substr($res, $off, 2, "$;$;");
345 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
346 $sanitise_quote = '';
347 substr($res, $off, 2, "$;$;");
352 # A \ in a string means ignore the next character.
353 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
355 substr($res, $off, 2, 'XX');
360 if ($c eq "'" || $c eq '"') {
361 if ($sanitise_quote eq '') {
362 $sanitise_quote = $c;
364 substr($res, $off, 1, $c);
366 } elsif ($sanitise_quote eq $c) {
367 $sanitise_quote = '';
371 #print "c<$c> SQ<$sanitise_quote>\n";
372 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
373 substr($res, $off, 1, $;);
374 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
375 substr($res, $off, 1, 'X');
377 substr($res, $off, 1, $c);
381 # The pathname on a #include may be surrounded by '<' and '>'.
382 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
383 my $clean = 'X' x
length($1);
384 $res =~ s@\
<.*\
>@
<$clean>@
;
386 # The whole of a #error is a string.
387 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
388 my $clean = 'X' x
length($1);
389 $res =~ s@
(\#\s
*(?
:error
|warning
)\s
+).*@
$1$clean@
;
395 sub ctx_statement_block
{
396 my ($linenr, $remain, $off) = @_;
397 my $line = $linenr - 1;
413 #warn "CSB: blk<$blk> remain<$remain>\n";
414 # If we are about to drop off the end, pull in more
417 for (; $remain > 0; $line++) {
418 last if (!defined $lines[$line]);
419 next if ($lines[$line] =~ /^-/);
422 $blk .= $lines[$line] . "\n";
427 # Bail if there is no further context.
428 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
434 $c = substr($blk, $off, 1);
435 $remainder = substr($blk, $off);
437 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
438 # Statement ends at the ';' or a close '}' at the
440 if ($level == 0 && $c eq ';') {
444 # An else is really a conditional as long as its not else if
445 if ($level == 0 && $coff_set == 0 &&
446 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
447 $remainder =~ /^(else)(?:\s|{)/ &&
448 $remainder !~ /^else\s+if\b/) {
449 $coff = $off + length($1) - 1;
451 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
452 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
455 if (($type eq '' || $type eq '(') && $c eq '(') {
459 if ($type eq '(' && $c eq ')') {
461 $type = ($level != 0)?
'(' : '';
463 if ($level == 0 && $coff < $soff) {
466 #warn "CSB: mark coff<$coff>\n";
469 if (($type eq '' || $type eq '{') && $c eq '{') {
473 if ($type eq '{' && $c eq '}') {
475 $type = ($level != 0)?
'{' : '';
483 # We are truly at the end, so shuffle to the next line.
490 my $statement = substr($blk, $soff, $off - $soff + 1);
491 my $condition = substr($blk, $soff, $coff - $soff + 1);
493 #warn "STATEMENT<$statement>\n";
494 #warn "CONDITION<$condition>\n";
496 #print "coff<$coff> soff<$off> loff<$loff>\n";
498 return ($statement, $condition,
499 $line, $remain + 1, $off - $loff + 1, $level);
502 sub statement_lines
{
505 # Strip the diff line prefixes and rip blank lines at start and end.
506 $stmt =~ s/(^|\n)./$1/g;
510 my @stmt_lines = ($stmt =~ /\n/g);
512 return $#stmt_lines + 2;
515 sub statement_rawlines
{
518 my @stmt_lines = ($stmt =~ /\n/g);
520 return $#stmt_lines + 2;
523 sub statement_block_size
{
526 $stmt =~ s/(^|\n)./$1/g;
532 my @stmt_lines = ($stmt =~ /\n/g);
533 my @stmt_statements = ($stmt =~ /;/g);
535 my $stmt_lines = $#stmt_lines + 2;
536 my $stmt_statements = $#stmt_statements + 1;
538 if ($stmt_lines > $stmt_statements) {
541 return $stmt_statements;
545 sub ctx_statement_full
{
546 my ($linenr, $remain, $off) = @_;
547 my ($statement, $condition, $level);
551 # Grab the first conditional/block pair.
552 ($statement, $condition, $linenr, $remain, $off, $level) =
553 ctx_statement_block
($linenr, $remain, $off);
554 #print "F: c<$condition> s<$statement> remain<$remain>\n";
555 push(@chunks, [ $condition, $statement ]);
556 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
557 return ($level, $linenr, @chunks);
560 # Pull in the following conditional/block pairs and see if they
561 # could continue the statement.
563 ($statement, $condition, $linenr, $remain, $off, $level) =
564 ctx_statement_block
($linenr, $remain, $off);
565 #print "C: c<$condition> s<$statement> remain<$remain>\n";
566 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
568 push(@chunks, [ $condition, $statement ]);
571 return ($level, $linenr, @chunks);
575 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
577 my $start = $linenr - 1;
584 for ($line = $start; $remain > 0; $line++) {
585 next if ($rawlines[$line] =~ /^-/);
588 $blk .= $rawlines[$line];
589 foreach my $c (split(//, $rawlines[$line])) {
590 ##print "C<$c>L<$level><$open$close>O<$off>\n";
596 if ($c eq $close && $level > 0) {
598 last if ($level == 0);
599 } elsif ($c eq $open) {
604 if (!$outer || $level <= 1) {
605 push(@res, $rawlines[$line]);
608 last if ($level == 0);
611 return ($level, @res);
613 sub ctx_block_outer
{
614 my ($linenr, $remain) = @_;
616 my ($level, @r) = ctx_block_get
($linenr, $remain, 1, '{', '}', 0);
620 my ($linenr, $remain) = @_;
622 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
626 my ($linenr, $remain, $off) = @_;
628 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
631 sub ctx_block_level
{
632 my ($linenr, $remain) = @_;
634 return ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
636 sub ctx_statement_level
{
637 my ($linenr, $remain, $off) = @_;
639 return ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
642 sub ctx_locate_comment
{
643 my ($first_line, $end_line) = @_;
645 # Catch a comment on the end of the line itself.
646 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@
.*(/\*.*\*/)\s
*(?
:\\\s
*)?
$@
);
647 return $current_comment if (defined $current_comment);
649 # Look through the context and try and figure out if there is a
652 $current_comment = '';
653 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
654 my $line = $rawlines[$linenr - 1];
656 if ($linenr == $first_line and $line =~ m@
^.\s
*\
*@
) {
659 if ($line =~ m@
/\
*@
) {
662 if (!$in_comment && $current_comment ne '') {
663 $current_comment = '';
665 $current_comment .= $line . "\n" if ($in_comment);
666 if ($line =~ m@\
*/@
) {
671 chomp($current_comment);
672 return($current_comment);
674 sub ctx_has_comment
{
675 my ($first_line, $end_line) = @_;
676 my $cmt = ctx_locate_comment
($first_line, $end_line);
678 ##print "LINE: $rawlines[$end_line - 1 ]\n";
679 ##print "CMMT: $cmt\n";
685 my ($linenr, $cnt) = @_;
687 my $offset = $linenr - 1;
692 $line = $rawlines[$offset++];
693 next if (defined($line) && $line =~ /^-/);
705 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
708 $coded = sprintf("^%c", unpack('C', $2) + 64);
717 my $av_preprocessor = 0;
723 $av_preprocessor = 0;
725 @av_paren_type = ('E');
726 $av_pend_colon = 'O';
729 sub annotate_values
{
730 my ($stream, $type) = @_;
733 my $var = '_' x
length($stream);
736 print "$stream\n" if ($dbg_values > 1);
738 while (length($cur)) {
739 @av_paren_type = ('E') if ($#av_paren_type < 0);
740 print " <" . join('', @av_paren_type) .
741 "> <$type> <$av_pending>" if ($dbg_values > 1);
742 if ($cur =~ /^(\s+)/o) {
743 print "WS($1)\n" if ($dbg_values > 1);
744 if ($1 =~ /\n/ && $av_preprocessor) {
745 $type = pop(@av_paren_type);
746 $av_preprocessor = 0;
749 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
750 print "DECLARE($1)\n" if ($dbg_values > 1);
753 } elsif ($cur =~ /^($Modifier)\s*/) {
754 print "MODIFIER($1)\n" if ($dbg_values > 1);
757 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
758 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
759 $av_preprocessor = 1;
760 push(@av_paren_type, $type);
766 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
767 print "UNDEF($1)\n" if ($dbg_values > 1);
768 $av_preprocessor = 1;
769 push(@av_paren_type, $type);
771 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
772 print "PRE_START($1)\n" if ($dbg_values > 1);
773 $av_preprocessor = 1;
775 push(@av_paren_type, $type);
776 push(@av_paren_type, $type);
779 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
780 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
781 $av_preprocessor = 1;
783 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
787 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
788 print "PRE_END($1)\n" if ($dbg_values > 1);
790 $av_preprocessor = 1;
792 # Assume all arms of the conditional end as this
793 # one does, and continue as if the #endif was not here.
795 push(@av_paren_type, $type);
798 } elsif ($cur =~ /^(\\\n)/o) {
799 print "PRECONT($1)\n" if ($dbg_values > 1);
801 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
802 print "ATTR($1)\n" if ($dbg_values > 1);
806 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
807 print "SIZEOF($1)\n" if ($dbg_values > 1);
813 } elsif ($cur =~ /^(if|while|for)\b/o) {
814 print "COND($1)\n" if ($dbg_values > 1);
818 } elsif ($cur =~/^(case)/o) {
819 print "CASE($1)\n" if ($dbg_values > 1);
820 $av_pend_colon = 'C';
823 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
824 print "KEYWORD($1)\n" if ($dbg_values > 1);
827 } elsif ($cur =~ /^(\()/o) {
828 print "PAREN('$1')\n" if ($dbg_values > 1);
829 push(@av_paren_type, $av_pending);
833 } elsif ($cur =~ /^(\))/o) {
834 my $new_type = pop(@av_paren_type);
835 if ($new_type ne '_') {
837 print "PAREN('$1') -> $type\n"
838 if ($dbg_values > 1);
840 print "PAREN('$1')\n" if ($dbg_values > 1);
843 } elsif ($cur =~ /^($Ident)\s*\(/o) {
844 print "FUNC($1)\n" if ($dbg_values > 1);
848 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
849 if (defined $2 && $type eq 'C' || $type eq 'T') {
850 $av_pend_colon = 'B';
851 } elsif ($type eq 'E') {
852 $av_pend_colon = 'L';
854 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
857 } elsif ($cur =~ /^($Ident|$Constant)/o) {
858 print "IDENT($1)\n" if ($dbg_values > 1);
861 } elsif ($cur =~ /^($Assignment)/o) {
862 print "ASSIGN($1)\n" if ($dbg_values > 1);
865 } elsif ($cur =~/^(;|{|})/) {
866 print "END($1)\n" if ($dbg_values > 1);
868 $av_pend_colon = 'O';
870 } elsif ($cur =~/^(,)/) {
871 print "COMMA($1)\n" if ($dbg_values > 1);
874 } elsif ($cur =~ /^(\?)/o) {
875 print "QUESTION($1)\n" if ($dbg_values > 1);
878 } elsif ($cur =~ /^(:)/o) {
879 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
881 substr($var, length($res), 1, $av_pend_colon);
882 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
887 $av_pend_colon = 'O';
889 } elsif ($cur =~ /^(\[)/o) {
890 print "CLOSE($1)\n" if ($dbg_values > 1);
893 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
896 print "OPV($1)\n" if ($dbg_values > 1);
903 substr($var, length($res), 1, $variant);
906 } elsif ($cur =~ /^($Operators)/o) {
907 print "OP($1)\n" if ($dbg_values > 1);
908 if ($1 ne '++' && $1 ne '--') {
912 } elsif ($cur =~ /(^.)/o) {
913 print "C($1)\n" if ($dbg_values > 1);
916 $cur = substr($cur, length($1));
917 $res .= $type x
length($1);
925 my ($possible, $line) = @_;
927 print "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
928 if ($possible !~ /(?
:
941 ^(?
:typedef
|struct
|enum
)\b
943 # Check for modifiers.
944 $possible =~ s/\s*$Storage\s*//g;
945 $possible =~ s/\s*$Sparse\s*//g;
946 if ($possible =~ /^\s*$/) {
948 } elsif ($possible =~ /\s/) {
949 $possible =~ s/\s*$Type\s*//g;
950 for my $modifier (split(' ', $possible)) {
951 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
952 push(@modifierList, $modifier);
956 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
957 push(@typeList, $possible);
961 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
968 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
971 my $line = $prefix . $_[0];
973 $line = (split('\n', $line))[0] . "\n" if ($terse);
975 push(our @report, $line);
983 if (report
("ERROR: $_[0]\n")) {
989 if (report
("WARNING: $_[0]\n")) {
995 if ($check && report
("CHECK: $_[0]\n")) {
1001 sub check_absolute_file
{
1002 my ($absolute, $herecurr) = @_;
1003 my $file = $absolute;
1005 ##print "absolute<$absolute>\n";
1007 # See if any suffix of this path is a path within the tree.
1008 while ($file =~ s@
^[^/]*/@@
) {
1009 if (-f
"$root/$file") {
1010 ##print "file<$file>\n";
1018 # It is, so see if the prefix is acceptable.
1019 my $prefix = $absolute;
1020 substr($prefix, -length($file)) = '';
1022 ##print "prefix<$prefix>\n";
1023 if ($prefix ne ".../") {
1024 WARN
("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1029 my $filename = shift;
1035 my $stashrawline="";
1052 # Trace the real file/line as we go.
1058 my $comment_edge = 0;
1062 my $prev_values = 'E';
1065 my %suppress_ifbraces;
1066 my %suppress_whiletrailers;
1068 # Pre-scan the patch sanitizing the lines.
1069 # Pre-scan the patch looking for any __setup documentation.
1071 my @setup_docs = ();
1074 sanitise_line_reset
();
1076 foreach my $rawline (@rawlines) {
1080 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1082 if ($1 =~ m
@Documentation/kernel
-parameters
.txt
$@
) {
1087 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1096 # Guestimate if this is a continuing comment. Run
1097 # the context looking for a comment "edge". If this
1098 # edge is a close comment then we must be in a comment
1102 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1103 next if (defined $rawlines[$ln - 1] &&
1104 $rawlines[$ln - 1] =~ /^-/);
1106 #print "RAW<$rawlines[$ln - 1]>\n";
1107 last if (!defined $rawlines[$ln - 1]);
1108 if ($rawlines[$ln - 1] =~ m@
(/\*|\*/)@
&&
1109 $rawlines[$ln - 1] !~ m@
"[^"]*(?
:/\*|\*/)[^"]*"@
) {
1114 if (defined $edge && $edge eq '*/') {
1118 # Guestimate if this is a continuing comment. If this
1119 # is the start of a diff block and this line starts
1120 # ' *' then it is very likely a comment.
1121 if (!defined $edge &&
1122 $rawlines[$linenr] =~ m@
^.\s
*(?
:\
*\
*+| \
*)(?
:\s
|$)@
)
1127 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1128 sanitise_line_reset
($in_comment);
1130 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1131 # Standardise the strings and chars within the input to
1132 # simplify matching -- only bother with positive lines.
1133 $line = sanitise_line
($rawline);
1135 push(@lines, $line);
1138 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1143 #print "==>$rawline\n";
1144 #print "-->$line\n";
1146 if ($setup_docs && $line =~ /^\+/) {
1147 push(@setup_docs, $line);
1155 foreach my $line (@lines) {
1158 my $rawline = $rawlines[$linenr - 1];
1159 my $hunk_line = ($realcnt != 0);
1161 #extract the line range in the file after the patch is applied
1162 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1164 $first_line = $linenr + 1;
1174 %suppress_ifbraces = ();
1175 %suppress_whiletrailers = ();
1178 # track the line number as we move through the hunk, note that
1179 # new versions of GNU diff omit the leading space on completely
1180 # blank context lines so we need to count that too.
1181 } elsif ($line =~ /^( |\+|$)/) {
1183 $realcnt-- if ($realcnt != 0);
1185 # Measure the line length and indent.
1186 ($length, $indent) = line_stats
($rawline);
1188 # Track the previous line.
1189 ($prevline, $stashline) = ($stashline, $line);
1190 ($previndent, $stashindent) = ($stashindent, $indent);
1191 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1193 #warn "line<$line>\n";
1195 } elsif ($realcnt == 1) {
1199 #make up the handle for any error we report on this line
1200 $prefix = "$filename:$realline: " if ($emacs && $file);
1201 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1203 $here = "#$linenr: " if (!$file);
1204 $here = "#$realline: " if ($file);
1206 # extract the filename as it passes
1207 if ($line=~/^\+\+\+\s+(\S+)/) {
1209 $realfile =~ s@
^([^/]*)/@@
;
1212 if ($tree && $p1_prefix ne '' && -e
"$root/$p1_prefix") {
1213 WARN
("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1216 if ($realfile =~ m@
^include
/asm/@
) {
1217 ERROR
("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1222 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1224 my $hereline = "$here\n$rawline\n";
1225 my $herecurr = "$here\n$rawline\n";
1226 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1228 $cnt_lines++ if ($realcnt != 0);
1230 #check the patch for a signoff:
1231 if ($line =~ /^\s*signed-off-by:/i) {
1232 # This is a signoff, if ugly, so do not double report.
1234 if (!($line =~ /^\s*Signed-off-by:/)) {
1235 WARN
("Signed-off-by: is the preferred form\n" .
1238 if ($line =~ /^\s*signed-off-by:\S/i) {
1239 WARN
("space required after Signed-off-by:\n" .
1244 # Check for wrappage within a valid hunk of the file
1245 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1246 ERROR
("patch seems to be corrupt (line wrapped?)\n" .
1247 $herecurr) if (!$emitted_corrupt++);
1250 # Check for absolute kernel paths.
1252 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1255 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1256 check_absolute_file
($1, $herecurr)) {
1259 check_absolute_file
($file, $herecurr);
1264 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1265 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1266 $rawline !~ m/^$UTF8*$/) {
1267 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1269 my $blank = copy_spacing
($rawline);
1270 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1271 my $hereptr = "$hereline$ptr\n";
1273 ERROR
("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1276 # ignore non-hunk lines and lines being removed
1277 next if (!$hunk_line || $line =~ /^-/);
1279 #trailing whitespace
1280 if ($line =~ /^\+.*\015/) {
1281 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1282 ERROR
("DOS line endings\n" . $herevet);
1284 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1285 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1286 ERROR
("trailing whitespace\n" . $herevet);
1289 # check we are in a valid source file if not then ignore this hunk
1290 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1293 if ($line =~ /^\+/ && $prevrawline !~ /\/\
*\
*/ &&
1294 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1295 $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1298 WARN
("line over 80 characters\n" . $herecurr);
1301 # check for adding lines without a newline.
1302 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1303 WARN
("adding a line without newline at end of file\n" . $herecurr);
1306 # check we are in a valid source file C or perl if not then ignore this hunk
1307 next if ($realfile !~ /\.(h|c|pl)$/);
1309 # at the beginning of a line any tabs must come first and anything
1310 # more than 8 must use tabs.
1311 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1312 $rawline =~ /^\+\s* \s*/) {
1313 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1314 ERROR
("code indent should use tabs where possible\n" . $herevet);
1317 # check we are in a valid C source file if not then ignore this hunk
1318 next if ($realfile !~ /\.(h|c)$/);
1320 # check for RCS/CVS revision markers
1321 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1322 WARN
("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1325 # Check for potential 'bare' types
1326 my ($stat, $cond, $line_nr_next, $remain_next, $off_next);
1327 if ($realcnt && $line =~ /.\s*\S/) {
1328 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1329 ctx_statement_block
($linenr, $realcnt, 0);
1330 $stat =~ s/\n./\n /g;
1331 $cond =~ s/\n./\n /g;
1336 # Ignore goto labels.
1337 if ($s =~ /$Ident:\*$/s) {
1339 # Ignore functions being called
1340 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1342 # declarations always start with types
1343 } 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) {
1346 possible
($type, "A:" . $s);
1348 # definitions in global scope can only start with types
1349 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1350 possible
($1, "B:" . $s);
1353 # any (foo ... *) is a pointer cast, and foo is a type
1354 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1355 possible
($1, "C:" . $s);
1358 # Check for any sort of function declaration.
1359 # int foo(something bar, other baz);
1360 # void (*store_gdt)(x86_descr_ptr *);
1361 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1362 my ($name_len) = length($1);
1365 substr($ctx, 0, $name_len + 1, '');
1366 $ctx =~ s/\)[^\)]*$//;
1368 for my $arg (split(/\s*,\s*/, $ctx)) {
1369 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1371 possible
($1, "D:" . $s);
1379 # Checks which may be anchored in the context.
1382 # Check for switch () and associated case and default
1383 # statements should be at the same indent.
1384 if ($line=~/\bswitch\s*\(.*\)/) {
1387 my @ctx = ctx_block_outer
($linenr, $realcnt);
1389 for my $ctx (@ctx) {
1390 my ($clen, $cindent) = line_stats
($ctx);
1391 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1392 $indent != $cindent) {
1393 $err .= "$sep$ctx\n";
1400 ERROR
("switch and case should be at the same indent\n$hereline$err");
1404 # if/while/etc brace do not go on next line, unless defining a do while loop,
1405 # or if that brace on the next line is for something else
1406 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1407 my $pre_ctx = "$1$2";
1409 my ($level, @ctx) = ctx_statement_level
($linenr, $realcnt, 0);
1410 my $ctx_cnt = $realcnt - $#ctx - 1;
1411 my $ctx = join("\n", @ctx);
1413 my $ctx_ln = $linenr;
1414 my $ctx_skip = $realcnt;
1416 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1417 defined $lines[$ctx_ln - 1] &&
1418 $lines[$ctx_ln - 1] =~ /^-/)) {
1419 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1420 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1424 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1425 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1427 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1428 ERROR
("that open brace { should be on the previous line\n" .
1429 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
1431 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1432 $ctx =~ /\)\s*\;\s*$/ &&
1433 defined $lines[$ctx_ln - 1])
1435 my ($nlength, $nindent) = line_stats
($lines[$ctx_ln - 1]);
1436 if ($nindent > $indent) {
1437 WARN
("trailing semicolon indicates no statements, indent implies otherwise\n" .
1438 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
1443 # Check relative indent for conditionals and blocks.
1444 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1445 my ($s, $c) = ($stat, $cond);
1447 substr($s, 0, length($c), '');
1449 # Make sure we remove the line prefixes as we have
1450 # none on the first line, and are going to readd them
1454 # Find out how long the conditional actually is.
1455 my @newlines = ($c =~ /\n/gs);
1456 my $cond_lines = 1 + $#newlines;
1458 # We want to check the first line inside the block
1459 # starting at the end of the conditional, so remove:
1460 # 1) any blank line termination
1461 # 2) any opening brace { on end of the line
1463 my $continuation = 0;
1465 $s =~ s/^.*\bdo\b//;
1467 if ($s =~ s/^\s*\\//) {
1470 if ($s =~ s/^\s*?\n//) {
1475 # Also ignore a loop construct at the end of a
1476 # preprocessor statement.
1477 if (($prevline =~ /^.\s*#\s*define\s/ ||
1478 $prevline =~ /\\\s*$/) && $continuation == 0) {
1484 while ($cond_ptr != $cond_lines) {
1485 $cond_ptr = $cond_lines;
1487 # If we see an #else/#elif then the code
1489 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1494 # 1) blank lines, they should be at 0,
1495 # 2) preprocessor lines, and
1497 if ($continuation ||
1499 $s =~ /^\s*#\s*?/ ||
1500 $s =~ /^\s*$Ident\s*:/) {
1501 $continuation = ($s =~ /^.*?\\\n/) ?
1 : 0;
1507 my (undef, $sindent) = line_stats
("+" . $s);
1508 my $stat_real = raw_line
($linenr, $cond_lines);
1510 # Check if either of these lines are modified, else
1511 # this is not this patch's fault.
1512 if (!defined($stat_real) ||
1513 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1516 if (defined($stat_real) && $cond_lines > 1) {
1517 $stat_real = "[...]\n$stat_real";
1520 #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";
1522 if ($check && (($sindent % 8) != 0 ||
1523 ($sindent <= $indent && $s ne ''))) {
1524 WARN
("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1528 # Track the 'values' across context and added lines.
1529 my $opline = $line; $opline =~ s/^./ /;
1530 my ($curr_values, $curr_vars) =
1531 annotate_values
($opline . "\n", $prev_values);
1532 $curr_values = $prev_values . $curr_values;
1534 my $outline = $opline; $outline =~ s/\t/ /g;
1535 print "$linenr > .$outline\n";
1536 print "$linenr > $curr_values\n";
1537 print "$linenr > $curr_vars\n";
1539 $prev_values = substr($curr_values, -1);
1541 #ignore lines not being added
1542 if ($line=~/^[^\+]/) {next;}
1544 # TEST: allow direct testing of the type matcher.
1546 if ($line =~ /^.\s*$Declare\s*$/) {
1547 ERROR
("TEST: is type\n" . $herecurr);
1548 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1549 ERROR
("TEST: is not type ($1 is)\n". $herecurr);
1553 # TEST: allow direct testing of the attribute matcher.
1555 if ($line =~ /^.\s*$Attribute\s*$/) {
1556 ERROR
("TEST: is attr\n" . $herecurr);
1557 } elsif ($dbg_attr > 1 && $line =~ /^.+($Attribute)/) {
1558 ERROR
("TEST: is not attr ($1 is)\n". $herecurr);
1563 # check for initialisation to aggregates open brace on the next line
1564 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1565 $line =~ /^.\s*{/) {
1566 ERROR
("that open brace { should be on the previous line\n" . $hereprev);
1570 # Checks which are anchored on the added line.
1573 # check for malformed paths in #include statements (uses RAW line)
1574 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1576 if ($path =~ m{//}) {
1577 ERROR
("malformed #include filename\n" .
1582 # no C99 // comments
1583 if ($line =~ m{//}) {
1584 ERROR
("do not use C99 // comments\n" . $herecurr);
1586 # Remove C99 comments.
1588 $opline =~ s@
//.*@@
;
1590 #EXPORT_SYMBOL should immediately follow its function closing }.
1591 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1592 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1594 if ($prevline !~ /(?
:
1596 ^.DEFINE_
$Ident\
(\Q
$name\E\
)|
1597 ^.DECLARE_
$Ident\
(\Q
$name\E\
)|
1598 ^.LIST_HEAD\
(\Q
$name\E\
)|
1599 ^.$Type\s
*\
(\s
*\
*\s
*\Q
$name\E\s
*\
)\s
*\
(|
1600 \b\Q
$name\E
(?
:\s
+$Attribute)?\s
*(?
:;|=|\
[)
1602 WARN
("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1606 # check for external initialisers.
1607 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1608 ERROR
("do not initialise externals to 0 or NULL\n" .
1611 # check for static initialisers.
1612 if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
1613 ERROR
("do not initialise statics to 0 or NULL\n" .
1617 # check for new typedefs, only function parameters and sparse annotations
1619 if ($line =~ /\btypedef\s/ &&
1620 $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1621 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
1622 $line !~ /\b$typeTypedefs\b/ &&
1623 $line !~ /\b__bitwise(?:__|)\b/) {
1624 WARN
("do not add new typedefs\n" . $herecurr);
1627 # * goes on variable not on type
1629 if ($line =~ m{\($NonptrType(\s*\*[\s\*]*(?:$Modifier\s*)*)\)}) {
1630 my ($from, $to) = ($1, $1);
1632 # Should start with a space.
1633 $to =~ s/^(\S)/ $1/;
1634 # Should not end with a space.
1636 # '*'s should not have spaces between.
1637 while ($to =~ s/(.)\s\*/$1\*/) {
1640 #print "from<$from> to<$to>\n";
1642 ERROR
("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1644 } elsif ($line =~ m{\b$NonptrType(\s*\*[\s\*]*(?:$Modifier\s*)?)($Ident)}) {
1645 my ($from, $to, $ident) = ($1, $1, $2);
1647 # Should start with a space.
1648 $to =~ s/^(\S)/ $1/;
1649 # Should not end with a space.
1651 # '*'s should not have spaces between.
1652 while ($to =~ s/(.)\s\*/$1\*/) {
1654 # Modifiers should have spaces.
1655 $to =~ s/(\b$Modifier$)/$1 /;
1657 #print "from<$from> to<$to>\n";
1659 ERROR
("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1663 # # no BUG() or BUG_ON()
1664 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1665 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1666 # print "$herecurr";
1670 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1671 WARN
("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1674 # printk should use KERN_* levels. Note that follow on printk's on the
1675 # same line do not need a level, so we use the current block context
1676 # to try and find and validate the current printk. In summary the current
1677 # printk includes all preceeding printk's which have no newline on the end.
1678 # we assume the first bad printk is the one to report.
1679 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1681 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1682 #print "CHECK<$lines[$ln - 1]\n";
1683 # we have a preceeding printk if it ends
1684 # with "\n" ignore it, else it is to blame
1685 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1686 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1693 WARN
("printk() should include KERN_ facility level\n" . $herecurr);
1697 # function brace can't be on same line, except for #defines of do while,
1698 # or if closed on same line
1699 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1700 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1701 ERROR
("open brace '{' following function declarations go on the next line\n" . $herecurr);
1704 # open braces for enum, union and struct go on the same line.
1705 if ($line =~ /^.\s*{/ &&
1706 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1707 ERROR
("open brace '{' following $1 go on the same line\n" . $hereprev);
1710 # check for spacing round square brackets; allowed:
1711 # 1. with a type on the left -- int [] a;
1712 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1713 # 3. inside a curly brace -- = { [0...10] = 5 }
1714 while ($line =~ /(.*?\s)\[/g) {
1715 my ($where, $prefix) = ($-[1], $1);
1716 if ($prefix !~ /$Type\s+$/ &&
1717 ($where != 0 || $prefix !~ /^.\s+$/) &&
1718 $prefix !~ /{\s+$/) {
1719 ERROR
("space prohibited before open square bracket '['\n" . $herecurr);
1723 # check for spaces between functions and their parentheses.
1724 while ($line =~ /($Ident)\s+\(/g) {
1726 my $ctx_before = substr($line, 0, $-[1]);
1727 my $ctx = "$ctx_before$name";
1729 # Ignore those directives where spaces _are_ permitted.
1731 if|for|while|switch
|return|case
|
1732 volatile
|__volatile__
|
1733 __attribute__
|format
|__extension__
|
1737 # cpp #define statements have non-optional spaces, ie
1738 # if there is a space between the name and the open
1739 # parenthesis it is simply not a parameter group.
1740 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1742 # cpp #elif statement condition may start with a (
1743 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1745 # If this whole things ends with a type its most
1746 # likely a typedef for a function.
1747 } elsif ($ctx =~ /$Type$/) {
1750 WARN
("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1753 # Check operator spacing.
1754 if (!($line=~/\#\s*include/)) {
1756 <<=|>>=|<=|>=|==|!=|
1757 \
+=|-=|\
*=|\
/=|%=|\
^=|\
|=|&=|
1758 =>|->|<<|>>|<|>|=|!|~|
1759 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%|
1762 my @elements = split(/($ops|;)/, $opline);
1765 my $blank = copy_spacing
($opline);
1767 for (my $n = 0; $n < $#elements; $n += 2) {
1768 $off += length($elements[$n]);
1770 # Pick up the preceeding and succeeding characters.
1771 my $ca = substr($opline, 0, $off);
1773 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1774 $cc = substr($opline, $off + length($elements[$n + 1]));
1776 my $cb = "$ca$;$cc";
1779 $a = 'V' if ($elements[$n] ne '');
1780 $a = 'W' if ($elements[$n] =~ /\s$/);
1781 $a = 'C' if ($elements[$n] =~ /$;$/);
1782 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1783 $a = 'O' if ($elements[$n] eq '');
1784 $a = 'E' if ($ca =~ /^\s*$/);
1786 my $op = $elements[$n + 1];
1789 if (defined $elements[$n + 2]) {
1790 $c = 'V' if ($elements[$n + 2] ne '');
1791 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1792 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1793 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1794 $c = 'O' if ($elements[$n + 2] eq '');
1795 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
1800 my $ctx = "${a}x${c}";
1802 my $at = "(ctx:$ctx)";
1804 my $ptr = substr($blank, 0, $off) . "^";
1805 my $hereptr = "$hereline$ptr\n";
1807 # Pull out the value of this operator.
1808 my $op_type = substr($curr_values, $off + 1, 1);
1810 # Get the full operator variant.
1811 my $opv = $op . substr($curr_vars, $off, 1);
1813 # Ignore operators passed as parameters.
1814 if ($op_type ne 'V' &&
1815 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1818 # } elsif ($op =~ /^$;+$/) {
1820 # ; should have either the end of line or a space or \ after it
1821 } elsif ($op eq ';') {
1822 if ($ctx !~ /.x[WEBC]/ &&
1823 $cc !~ /^\\/ && $cc !~ /^;/) {
1824 ERROR
("space required after that '$op' $at\n" . $hereptr);
1828 } elsif ($op eq '//') {
1832 # : when part of a bitfield
1833 } elsif ($op eq '->' || $opv eq ':B') {
1834 if ($ctx =~ /Wx.|.xW/) {
1835 ERROR
("spaces prohibited around that '$op' $at\n" . $hereptr);
1838 # , must have a space on the right.
1839 } elsif ($op eq ',') {
1840 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1841 ERROR
("space required after that '$op' $at\n" . $hereptr);
1844 # '*' as part of a type definition -- reported already.
1845 } elsif ($opv eq '*_') {
1846 #warn "'*' is part of type\n";
1848 # unary operators should have a space before and
1849 # none after. May be left adjacent to another
1850 # unary operator, or a cast
1851 } elsif ($op eq '!' || $op eq '~' ||
1852 $opv eq '*U' || $opv eq '-U' ||
1853 $opv eq '&U' || $opv eq '&&U') {
1854 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1855 ERROR
("space required before that '$op' $at\n" . $hereptr);
1857 if ($op eq '*' && $cc =~/\s*const\b/) {
1858 # A unary '*' may be const
1860 } elsif ($ctx =~ /.xW/) {
1861 ERROR
("space prohibited after that '$op' $at\n" . $hereptr);
1864 # unary ++ and unary -- are allowed no space on one side.
1865 } elsif ($op eq '++' or $op eq '--') {
1866 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1867 ERROR
("space required one side of that '$op' $at\n" . $hereptr);
1869 if ($ctx =~ /Wx[BE]/ ||
1870 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1871 ERROR
("space prohibited before that '$op' $at\n" . $hereptr);
1873 if ($ctx =~ /ExW/) {
1874 ERROR
("space prohibited after that '$op' $at\n" . $hereptr);
1878 # << and >> may either have or not have spaces both sides
1879 } elsif ($op eq '<<' or $op eq '>>' or
1880 $op eq '&' or $op eq '^' or $op eq '|' or
1881 $op eq '+' or $op eq '-' or
1882 $op eq '*' or $op eq '/' or
1885 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1886 ERROR
("need consistent spacing around '$op' $at\n" .
1890 # A colon needs no spaces before when it is
1891 # terminating a case value or a label.
1892 } elsif ($opv eq ':C' || $opv eq ':L') {
1893 if ($ctx =~ /Wx./) {
1894 ERROR
("space prohibited before that '$op' $at\n" . $hereptr);
1897 # All the others need spaces both sides.
1898 } elsif ($ctx !~ /[EWC]x[CWE]/) {
1901 # Ignore email addresses <foo@bar>
1903 $cc =~ /^\S+\@\S+>/) ||
1905 $ca =~ /<\S+\@\S+$/))
1911 if (($opv eq ':O' && $ca =~ /\?$/) ||
1912 ($op eq '?' && $cc =~ /^:/)) {
1917 ERROR
("spaces required around that '$op' $at\n" . $hereptr);
1920 $off += length($elements[$n + 1]);
1924 # check for multiple assignments
1925 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
1926 CHK
("multiple assignments should be avoided\n" . $herecurr);
1929 ## # check for multiple declarations, allowing for a function declaration
1931 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1932 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1934 ## # Remove any bracketed sections to ensure we do not
1935 ## # falsly report the parameters of functions.
1937 ## while ($ln =~ s/\([^\(\)]*\)//g) {
1939 ## if ($ln =~ /,/) {
1940 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1944 #need space before brace following if, while, etc
1945 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1947 ERROR
("space required before the open brace '{'\n" . $herecurr);
1950 # closing brace should have a space following it when it has anything
1952 if ($line =~ /}(?!(?:,|;|\)))\S/) {
1953 ERROR
("space required after that close brace '}'\n" . $herecurr);
1956 # check spacing on square brackets
1957 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1958 ERROR
("space prohibited after that open square bracket '['\n" . $herecurr);
1960 if ($line =~ /\s\]/) {
1961 ERROR
("space prohibited before that close square bracket ']'\n" . $herecurr);
1964 # check spacing on parentheses
1965 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1966 $line !~ /for\s*\(\s+;/) {
1967 ERROR
("space prohibited after that open parenthesis '('\n" . $herecurr);
1969 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1970 $line !~ /for\s*\(.*;\s+\)/ &&
1971 $line !~ /:\s+\)/) {
1972 ERROR
("space prohibited before that close parenthesis ')'\n" . $herecurr);
1975 #goto labels aren't indented, allow a single space however
1976 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
1977 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1978 WARN
("labels should not be indented\n" . $herecurr);
1981 # Return is not a function.
1982 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
1986 # Flatten any parentheses and braces
1987 $value =~ s/\)\(/\) \(/g;
1988 while ($value =~ s/\([^\(\)]*\)/1/) {
1991 if ($value =~ /^(?:$Ident|-?$Constant)$/) {
1992 ERROR
("return is not a function, parentheses are not required\n" . $herecurr);
1994 } elsif ($spacing !~ /\s+/) {
1995 ERROR
("space required before the open parenthesis '('\n" . $herecurr);
1999 # Need a space before open parenthesis after if, while etc
2000 if ($line=~/\b(if|while|for|switch)\(/) {
2001 ERROR
("space required before the open parenthesis '('\n" . $herecurr);
2004 # Check for illegal assignment in if conditional -- and check for trailing
2005 # statements after the conditional.
2006 if ($line =~ /do\s*(?!{)/) {
2007 my ($stat_next) = ctx_statement_block
($line_nr_next,
2008 $remain_next, $off_next);
2009 $stat_next =~ s/\n./\n /g;
2010 ##print "stat<$stat> stat_next<$stat_next>\n";
2012 if ($stat_next =~ /^\s*while\b/) {
2013 # If the statement carries leading newlines,
2014 # then count those as offsets.
2016 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2018 statement_rawlines
($whitespace) - 1;
2020 $suppress_whiletrailers{$line_nr_next +
2024 if (!defined $suppress_whiletrailers{$linenr} &&
2025 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2026 my ($s, $c) = ($stat, $cond);
2028 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
2029 ERROR
("do not use assignment in if condition\n" . $herecurr);
2032 # Find out what is on the end of the line after the
2034 substr($s, 0, length($c), '');
2036 $s =~ s/$;//g; # Remove any comments
2037 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2038 $c !~ /}\s*while\s*/)
2040 # Find out how long the conditional actually is.
2041 my @newlines = ($c =~ /\n/gs);
2042 my $cond_lines = 1 + $#newlines;
2044 my $stat_real = raw_line
($linenr, $cond_lines);
2045 if (defined($stat_real) && $cond_lines > 1) {
2046 $stat_real = "[...]\n$stat_real";
2049 ERROR
("trailing statements should be on next line\n" . $herecurr . $stat_real);
2053 # Check for bitwise tests written as boolean
2065 WARN
("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2068 # if and else should not have general statements after it
2069 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2071 $s =~ s/$;//g; # Remove any comments
2072 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2073 ERROR
("trailing statements should be on next line\n" . $herecurr);
2076 # case and default should not have general statements after them
2077 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2079 (?
:\s
*$;*)(?
:\s
*{)?
(?
:\s
*$;*)(?
:\s
*\\)?\s
*$|
2083 ERROR
("trailing statements should be on next line\n" . $herecurr);
2086 # Check for }<nl>else {, these must be at the same
2087 # indent level to be relevant to each other.
2088 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2089 $previndent == $indent) {
2090 ERROR
("else should follow close brace '}'\n" . $hereprev);
2093 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2094 $previndent == $indent) {
2095 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
2097 # Find out what is on the end of the line after the
2099 substr($s, 0, length($c), '');
2102 if ($s =~ /^\s*;/) {
2103 ERROR
("while should follow close brace '}'\n" . $hereprev);
2107 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2108 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2109 # print "No studly caps, use _\n";
2110 # print "$herecurr";
2114 #no spaces allowed after \ in define
2115 if ($line=~/\#\s*define.*\\\s$/) {
2116 WARN
("Whitepspace after \\ makes next lines useless\n" . $herecurr);
2119 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2120 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2122 my $checkfile = "include/linux/$file";
2123 if (-f
"$root/$checkfile" &&
2124 $realfile ne $checkfile &&
2127 if ($realfile =~ m{^arch/}) {
2128 CHK
("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2130 WARN
("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2135 # multi-statement macros should be enclosed in a do while loop, grab the
2136 # first statement and ensure its the whole macro if its not enclosed
2137 # in a known good container
2138 if ($realfile !~ m@
/vmlinux
.lds
.h
$@
&&
2139 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2142 my ($off, $dstat, $dcond, $rest);
2145 my $args = defined($1);
2147 # Find the end of the macro and limit our statement
2149 while ($cnt > 0 && defined $lines[$ln - 1] &&
2150 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2152 $ctx .= $rawlines[$ln - 1] . "\n";
2153 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2156 $ctx .= $rawlines[$ln - 1];
2158 ($dstat, $dcond, $ln, $cnt, $off) =
2159 ctx_statement_block
($linenr, $ln - $linenr + 1, 0);
2160 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2161 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2163 # Extract the remainder of the define (if any) and
2164 # rip off surrounding spaces, and trailing \'s.
2166 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2167 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2168 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2169 $rest .= substr($lines[$ln - 1], $off) . "\n";
2175 $rest =~ s/\\\n.//g;
2179 # Clean up the original statement.
2181 substr($dstat, 0, length($dcond), '');
2183 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2186 $dstat =~ s/\\\n.//g;
2187 $dstat =~ s/^\s*//s;
2188 $dstat =~ s/\s*$//s;
2190 # Flatten any parentheses and braces
2191 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2192 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2193 $dstat =~ s/\[[^\{\}]*\]/1/)
2197 my $exceptions = qr{
2206 #print "REST<$rest> dstat<$dstat>\n";
2208 if ($rest !~ /while\s*\(/ &&
2209 $dstat !~ /$exceptions/)
2211 ERROR
("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2214 } elsif ($ctx !~ /;/) {
2216 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2217 $dstat !~ /$exceptions/ &&
2218 $dstat !~ /^\.$Ident\s*=/ &&
2219 $dstat =~ /$Operators/)
2221 ERROR
("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2226 # check for redundant bracing round if etc
2227 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2228 my ($level, $endln, @chunks) =
2229 ctx_statement_full
($linenr, $realcnt, 1);
2230 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2231 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2232 if ($#chunks > 0 && $level == 0) {
2235 my $herectx = $here . "\n";
2236 my $ln = $linenr - 1;
2237 for my $chunk (@chunks) {
2238 my ($cond, $block) = @
{$chunk};
2240 # If the condition carries leading newlines, then count those as offsets.
2241 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2242 my $offset = statement_rawlines
($whitespace) - 1;
2244 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2246 # We have looked at and allowed this specific line.
2247 $suppress_ifbraces{$ln + $offset} = 1;
2249 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2250 $ln += statement_rawlines
($block) - 1;
2252 substr($block, 0, length($cond), '');
2254 $seen++ if ($block =~ /^\s*{/);
2256 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2257 if (statement_lines
($cond) > 1) {
2258 #print "APW: ALLOWED: cond<$cond>\n";
2261 if ($block =~/\b(?:if|for|while)\b/) {
2262 #print "APW: ALLOWED: block<$block>\n";
2265 if (statement_block_size
($block) > 1) {
2266 #print "APW: ALLOWED: lines block<$block>\n";
2270 if ($seen && !$allowed) {
2271 WARN
("braces {} are not necessary for any arm of this statement\n" . $herectx);
2275 if (!defined $suppress_ifbraces{$linenr - 1} &&
2276 $line =~ /\b(if|while|for|else)\b/) {
2279 # Check the pre-context.
2280 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2281 #print "APW: ALLOWED: pre<$1>\n";
2285 my ($level, $endln, @chunks) =
2286 ctx_statement_full
($linenr, $realcnt, $-[0]);
2288 # Check the condition.
2289 my ($cond, $block) = @
{$chunks[0]};
2290 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2291 if (defined $cond) {
2292 substr($block, 0, length($cond), '');
2294 if (statement_lines
($cond) > 1) {
2295 #print "APW: ALLOWED: cond<$cond>\n";
2298 if ($block =~/\b(?:if|for|while)\b/) {
2299 #print "APW: ALLOWED: block<$block>\n";
2302 if (statement_block_size
($block) > 1) {
2303 #print "APW: ALLOWED: lines block<$block>\n";
2306 # Check the post-context.
2307 if (defined $chunks[1]) {
2308 my ($cond, $block) = @
{$chunks[1]};
2309 if (defined $cond) {
2310 substr($block, 0, length($cond), '');
2312 if ($block =~ /^\s*\{/) {
2313 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2317 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2318 my $herectx = $here . "\n";;
2319 my $cnt = statement_rawlines
($block);
2321 for (my $n = 0; $n < $cnt; $n++) {
2322 $herectx .= raw_line
($linenr, $n) . "\n";;
2325 WARN
("braces {} are not necessary for single statement blocks\n" . $herectx);
2329 # don't include deprecated include files (uses RAW line)
2330 for my $inc (@dep_includes) {
2331 if ($rawline =~ m@
^.\s
*\#\s
*include\s
*\
<$inc>@
) {
2332 ERROR
("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2336 # don't use deprecated functions
2337 for my $func (@dep_functions) {
2338 if ($line =~ /\b$func\b/) {
2339 ERROR
("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2343 # no volatiles please
2344 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2345 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2346 WARN
("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2349 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2350 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2351 ERROR
("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2355 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2356 CHK
("if this code is redundant consider removing it\n" .
2360 # check for needless kfree() checks
2361 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2363 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2364 WARN
("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2367 # check for needless usb_free_urb() checks
2368 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2370 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2371 WARN
("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2375 # warn about #ifdefs in C files
2376 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2377 # print "#ifdef in C files should be avoided\n";
2378 # print "$herecurr";
2382 # warn about spacing in #ifdefs
2383 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2384 ERROR
("exactly one space required after that #$1\n" . $herecurr);
2387 # check for spinlock_t definitions without a comment.
2388 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2389 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
2391 if (!ctx_has_comment
($first_line, $linenr)) {
2392 CHK
("$1 definition without comment\n" . $herecurr);
2395 # check for memory barriers without a comment.
2396 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2397 if (!ctx_has_comment
($first_line, $linenr)) {
2398 CHK
("memory barrier without comment\n" . $herecurr);
2401 # check of hardware specific defines
2402 if ($line =~ m@
^.\s
*\#\s
*if.*\b(__i386__
|__powerpc64__
|__sun__
|__s390x__
)\b@
&& $realfile !~ m
@include/asm
-@
) {
2403 CHK
("architecture specific defines should be avoided\n" . $herecurr);
2406 # check the location of the inline attribute, that it is between
2407 # storage class and type.
2408 if ($line =~ /\b$Type\s+$Inline\b/ ||
2409 $line =~ /\b$Inline\s+$Storage\b/) {
2410 ERROR
("inline keyword should sit between storage class and type\n" . $herecurr);
2413 # Check for __inline__ and __inline, prefer inline
2414 if ($line =~ /\b(__inline__|__inline)\b/) {
2415 WARN
("plain inline is preferred over $1\n" . $herecurr);
2418 # check for new externs in .c files.
2419 if ($realfile =~ /\.c$/ && defined $stat &&
2420 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2422 my $function_name = $1;
2423 my $paren_space = $2;
2426 if (defined $cond) {
2427 substr($s, 0, length($cond), '');
2429 if ($s =~ /^\s*;/ &&
2430 $function_name ne 'uninitialized_var')
2432 WARN
("externs should be avoided in .c files\n" . $herecurr);
2435 if ($paren_space =~ /\n/) {
2436 WARN
("arguments for function declarations should follow identifier\n" . $herecurr);
2439 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2440 $stat =~ /^.\s*extern\s+/)
2442 WARN
("externs should be avoided in .c files\n" . $herecurr);
2445 # checks for new __setup's
2446 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2449 if (!grep(/$name/, @setup_docs)) {
2450 CHK
("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2454 # check for pointless casting of kmalloc return
2455 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2456 WARN
("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2459 # check for gcc specific __FUNCTION__
2460 if ($line =~ /__FUNCTION__/) {
2461 WARN
("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2464 # check for semaphores used as mutexes
2465 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2466 WARN
("mutexes are preferred for single holder semaphores\n" . $herecurr);
2468 # check for semaphores used as mutexes
2469 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2470 WARN
("consider using a completion\n" . $herecurr);
2472 # recommend strict_strto* over simple_strto*
2473 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2474 WARN
("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2476 # check for __initcall(), use device_initcall() explicitly please
2477 if ($line =~ /^.\s*__initcall\s*\(/) {
2478 WARN
("please use device_initcall() instead of __initcall()\n" . $herecurr);
2481 # use of NR_CPUS is usually wrong
2482 # ignore definitions of NR_CPUS and usage to define arrays as likely right
2483 if ($line =~ /\bNR_CPUS\b/ &&
2484 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2485 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2486 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2487 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2488 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2490 WARN
("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2493 # check for %L{u,d,i} in strings
2495 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2496 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2497 $string =~ s/%%/__/g;
2498 if ($string =~ /(?<!%)%L[udi]/) {
2499 WARN
("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2504 # whine mightly about in_atomic
2505 if ($line =~ /\bin_atomic\s*\(/) {
2506 if ($realfile =~ m@
^drivers
/@
) {
2507 ERROR
("do not use in_atomic in drivers\n" . $herecurr);
2509 WARN
("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2514 # If we have no input at all, then there is nothing to report on
2515 # so just keep quiet.
2516 if ($#rawlines == -1) {
2520 # In mailback mode only produce a report in the negative, for
2521 # things that appear to be patches.
2522 if ($mailback && ($clean == 1 || !$is_patch)) {
2526 # This is not a patch, and we are are in 'no-patch' mode so
2528 if (!$chk_patch && !$is_patch) {
2533 ERROR
("Does not appear to be a unified-diff format patch\n");
2535 if ($is_patch && $chk_signoff && $signoff == 0) {
2536 ERROR
("Missing Signed-off-by: line(s)\n");
2539 print report_dump
();
2540 if ($summary && !($clean == 1 && $quiet == 1)) {
2541 print "$filename " if ($summary_file);
2542 print "total: $cnt_error errors, $cnt_warn warnings, " .
2543 (($check)?
"$cnt_chk checks, " : "") .
2544 "$cnt_lines lines checked\n";
2545 print "\n" if ($quiet == 0);
2548 if ($clean == 1 && $quiet == 0) {
2549 print "$vname has no obvious style problems and is ready for submission.\n"
2551 if ($clean == 0 && $quiet == 0) {
2552 print "$vname has style problems, please review. If any of these errors\n";
2553 print "are false positives report them to the maintainer, see\n";
2554 print "CHECKPATCH in MAINTAINERS.\n";