2 # (c) 2001, Dave Jones. (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-2010 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);
38 Usage: $P [OPTION]... [FILE]...
43 --tabs tabs should be used for indent instead of spaces
44 --no-tree run without a kernel tree
45 --no-signoff do not check for 'Signed-off-by' line
46 --patch treat FILE as patchfile (default)
47 --emacs emacs compile window format
48 --terse one line per report
49 -f, --file treat FILE as regular source file
50 --subjective, --strict enable more subjective tests
51 --root=PATH PATH to the kernel tree root
52 --no-summary suppress the per-file summary
53 --mailback only produce a report in case of warnings/errors
54 --summary-file include the filename in summary
55 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
56 'values', 'possible', 'type', and 'attr' (default
58 --test-only=WORD report only warnings/errors containing WORD
60 -h, --help, --version display this help and exit
62 When FILE is - read standard input.
69 'q|quiet+' => \$quiet,
72 'signoff!' => \$chk_signoff,
73 'patch!' => \$chk_patch,
77 'subjective!' => \$check,
80 'summary!' => \$summary,
81 'mailback!' => \$mailback,
82 'summary-file!' => \$summary_file,
85 'test-only=s' => \$tst_only,
95 print "$P: no input files\n";
100 my $dbg_possible = 0;
103 for my $key (keys %debug) {
105 eval "\${dbg_$key} = '$debug{$key}';";
109 my $rpt_cleaners = 0;
118 if (!top_of_kernel_tree($root)) {
119 die "$P: $root: --root does not point at a valid tree\n";
122 if (top_of_kernel_tree('.')) {
124 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
125 top_of_kernel_tree($1)) {
130 if (!defined $root) {
131 print "Must be run from the top-level dir. of a kernel tree\n";
136 my $emitted_corrupt = 0;
139 [A-Za-z_][A-Za-z\d_]*
140 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
142 our $Storage = qr{extern|static|asmlinkage};
154 # Notes to $Attribute:
155 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
174 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
175 ____cacheline_aligned|
176 ____cacheline_aligned_in_smp|
177 ____cacheline_internodealigned_in_smp|
181 our $Inline = qr{inline|__always_inline|noinline};
182 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
183 our $Lval = qr{$Ident(?:$Member)*};
185 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
186 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
187 our $Compare = qr{<=|>=|==|!=|<|>};
191 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
199 [\x09\x0A\x0D\x20-\x7E] # ASCII
200 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
201 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
202 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
203 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
204 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
205 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
206 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
209 our $typeTypedefs = qr{(?x:
210 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
214 our $logFunctions = qr{(?x:
216 [a-z]+_(emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)|
224 qr{(?:unsigned\s+)?char},
225 qr{(?:unsigned\s+)?short},
226 qr{(?:unsigned\s+)?int},
227 qr{(?:unsigned\s+)?long},
228 qr{(?:unsigned\s+)?long\s+int},
229 qr{(?:unsigned\s+)?long\s+long},
230 qr{(?:unsigned\s+)?long\s+long\s+int},
239 qr{${Ident}_handler},
240 qr{${Ident}_handler_fn},
242 our @modifierList = (
246 our $allowed_asm_includes = qr{(?x:
250 # memory.h: ARM has a custom one
253 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
254 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
255 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
257 (?:$Modifier\s+|const\s+)*
259 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
263 (?:\s+$Modifier|\s+const)*
267 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
268 (?:\s+$Inline|\s+$Modifier)*
270 $Declare = qr{(?:$Storage\s+)?$Type};
274 $chk_signoff = 0 if ($file);
276 my @dep_includes = ();
277 my @dep_functions = ();
278 my $removal = "Documentation/feature-removal-schedule.txt";
279 if ($tree && -f "$root/$removal") {
280 open(my $REMOVE, '<', "$root/$removal") ||
281 die "$P: $removal: open failed - $!\n";
283 if (/^Check:\s+(.*\S)/) {
284 for my $entry (split(/[, ]+/, $1)) {
285 if ($entry =~ m@include/(.*)@) {
286 push(@dep_includes, $1);
288 } elsif ($entry !~ m@/@) {
289 push(@dep_functions, $entry);
300 for my $filename (@ARGV) {
303 open($FILE, '-|', "diff -u /dev/null $filename") ||
304 die "$P: $filename: diff failed - $!\n";
305 } elsif ($filename eq '-') {
306 open($FILE, '<&STDIN');
308 open($FILE, '<', "$filename") ||
309 die "$P: $filename: open failed - $!\n";
311 if ($filename eq '-') {
312 $vname = 'Your patch';
321 if (!process($filename)) {
330 sub top_of_kernel_tree {
334 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
335 "README", "Documentation", "arch", "include", "drivers",
336 "fs", "init", "ipc", "kernel", "lib", "scripts",
339 foreach my $check (@tree_check) {
340 if (! -e $root . '/' . $check) {
352 for my $c (split(//, $str)) {
356 for (; ($n % 8) != 0; $n++) {
368 (my $res = shift) =~ tr/\t/ /c;
375 # Drop the diff line leader and expand tabs
377 $line = expand_tabs($line);
379 # Pick the indent from the front of the line.
380 my ($white) = ($line =~ /^(\s*)/);
382 return (length($line), length($white));
385 my $sanitise_quote = '';
387 sub sanitise_line_reset {
388 my ($in_comment) = @_;
391 $sanitise_quote = '*/';
393 $sanitise_quote = '';
406 # Always copy over the diff marker.
407 $res = substr($line, 0, 1);
409 for ($off = 1; $off < length($line); $off++) {
410 $c = substr($line, $off, 1);
412 # Comments we are wacking completly including the begin
413 # and end, all to $;.
414 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
415 $sanitise_quote = '*/';
417 substr($res, $off, 2, "$;$;");
421 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
422 $sanitise_quote = '';
423 substr($res, $off, 2, "$;$;");
427 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
428 $sanitise_quote = '//';
430 substr($res, $off, 2, $sanitise_quote);
435 # A \ in a string means ignore the next character.
436 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
438 substr($res, $off, 2, 'XX');
443 if ($c eq "'" || $c eq '"') {
444 if ($sanitise_quote eq '') {
445 $sanitise_quote = $c;
447 substr($res, $off, 1, $c);
449 } elsif ($sanitise_quote eq $c) {
450 $sanitise_quote = '';
454 #print "c<$c> SQ<$sanitise_quote>\n";
455 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
456 substr($res, $off, 1, $;);
457 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
458 substr($res, $off, 1, $;);
459 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
460 substr($res, $off, 1, 'X');
462 substr($res, $off, 1, $c);
466 if ($sanitise_quote eq '//') {
467 $sanitise_quote = '';
470 # The pathname on a #include may be surrounded by '<' and '>'.
471 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
472 my $clean = 'X' x length($1);
473 $res =~ s@\<.*\>@<$clean>@;
475 # The whole of a #error is a string.
476 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
477 my $clean = 'X' x length($1);
478 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
484 sub ctx_statement_block {
485 my ($linenr, $remain, $off) = @_;
486 my $line = $linenr - 1;
503 @stack = (['', 0]) if ($#stack == -1);
505 #warn "CSB: blk<$blk> remain<$remain>\n";
506 # If we are about to drop off the end, pull in more
509 for (; $remain > 0; $line++) {
510 last if (!defined $lines[$line]);
511 next if ($lines[$line] =~ /^-/);
514 $blk .= $lines[$line] . "\n";
519 # Bail if there is no further context.
520 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
526 $c = substr($blk, $off, 1);
527 $remainder = substr($blk, $off);
529 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
531 # Handle nested #if/#else.
532 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
533 push(@stack, [ $type, $level ]);
534 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
535 ($type, $level) = @{$stack[$#stack - 1]};
536 } elsif ($remainder =~ /^#\s*endif\b/) {
537 ($type, $level) = @{pop(@stack)};
540 # Statement ends at the ';' or a close '}' at the
542 if ($level == 0 && $c eq ';') {
546 # An else is really a conditional as long as its not else if
547 if ($level == 0 && $coff_set == 0 &&
548 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
549 $remainder =~ /^(else)(?:\s|{)/ &&
550 $remainder !~ /^else\s+if\b/) {
551 $coff = $off + length($1) - 1;
553 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
554 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
557 if (($type eq '' || $type eq '(') && $c eq '(') {
561 if ($type eq '(' && $c eq ')') {
563 $type = ($level != 0)? '(' : '';
565 if ($level == 0 && $coff < $soff) {
568 #warn "CSB: mark coff<$coff>\n";
571 if (($type eq '' || $type eq '{') && $c eq '{') {
575 if ($type eq '{' && $c eq '}') {
577 $type = ($level != 0)? '{' : '';
580 if (substr($blk, $off + 1, 1) eq ';') {
588 # We are truly at the end, so shuffle to the next line.
595 my $statement = substr($blk, $soff, $off - $soff + 1);
596 my $condition = substr($blk, $soff, $coff - $soff + 1);
598 #warn "STATEMENT<$statement>\n";
599 #warn "CONDITION<$condition>\n";
601 #print "coff<$coff> soff<$off> loff<$loff>\n";
603 return ($statement, $condition,
604 $line, $remain + 1, $off - $loff + 1, $level);
607 sub statement_lines {
610 # Strip the diff line prefixes and rip blank lines at start and end.
611 $stmt =~ s/(^|\n)./$1/g;
615 my @stmt_lines = ($stmt =~ /\n/g);
617 return $#stmt_lines + 2;
620 sub statement_rawlines {
623 my @stmt_lines = ($stmt =~ /\n/g);
625 return $#stmt_lines + 2;
628 sub statement_block_size {
631 $stmt =~ s/(^|\n)./$1/g;
637 my @stmt_lines = ($stmt =~ /\n/g);
638 my @stmt_statements = ($stmt =~ /;/g);
640 my $stmt_lines = $#stmt_lines + 2;
641 my $stmt_statements = $#stmt_statements + 1;
643 if ($stmt_lines > $stmt_statements) {
646 return $stmt_statements;
650 sub ctx_statement_full {
651 my ($linenr, $remain, $off) = @_;
652 my ($statement, $condition, $level);
656 # Grab the first conditional/block pair.
657 ($statement, $condition, $linenr, $remain, $off, $level) =
658 ctx_statement_block($linenr, $remain, $off);
659 #print "F: c<$condition> s<$statement> remain<$remain>\n";
660 push(@chunks, [ $condition, $statement ]);
661 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
662 return ($level, $linenr, @chunks);
665 # Pull in the following conditional/block pairs and see if they
666 # could continue the statement.
668 ($statement, $condition, $linenr, $remain, $off, $level) =
669 ctx_statement_block($linenr, $remain, $off);
670 #print "C: c<$condition> s<$statement> remain<$remain>\n";
671 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
673 push(@chunks, [ $condition, $statement ]);
676 return ($level, $linenr, @chunks);
680 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
682 my $start = $linenr - 1;
689 my @stack = ($level);
690 for ($line = $start; $remain > 0; $line++) {
691 next if ($rawlines[$line] =~ /^-/);
694 $blk .= $rawlines[$line];
696 # Handle nested #if/#else.
697 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
698 push(@stack, $level);
699 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
700 $level = $stack[$#stack - 1];
701 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
702 $level = pop(@stack);
705 foreach my $c (split(//, $lines[$line])) {
706 ##print "C<$c>L<$level><$open$close>O<$off>\n";
712 if ($c eq $close && $level > 0) {
714 last if ($level == 0);
715 } elsif ($c eq $open) {
720 if (!$outer || $level <= 1) {
721 push(@res, $rawlines[$line]);
724 last if ($level == 0);
727 return ($level, @res);
729 sub ctx_block_outer {
730 my ($linenr, $remain) = @_;
732 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
736 my ($linenr, $remain) = @_;
738 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
742 my ($linenr, $remain, $off) = @_;
744 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
747 sub ctx_block_level {
748 my ($linenr, $remain) = @_;
750 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
752 sub ctx_statement_level {
753 my ($linenr, $remain, $off) = @_;
755 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
758 sub ctx_locate_comment {
759 my ($first_line, $end_line) = @_;
761 # Catch a comment on the end of the line itself.
762 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
763 return $current_comment if (defined $current_comment);
765 # Look through the context and try and figure out if there is a
768 $current_comment = '';
769 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
770 my $line = $rawlines[$linenr - 1];
772 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
775 if ($line =~ m@/\*@) {
778 if (!$in_comment && $current_comment ne '') {
779 $current_comment = '';
781 $current_comment .= $line . "\n" if ($in_comment);
782 if ($line =~ m@\*/@) {
787 chomp($current_comment);
788 return($current_comment);
790 sub ctx_has_comment {
791 my ($first_line, $end_line) = @_;
792 my $cmt = ctx_locate_comment($first_line, $end_line);
794 ##print "LINE: $rawlines[$end_line - 1 ]\n";
795 ##print "CMMT: $cmt\n";
801 my ($linenr, $cnt) = @_;
803 my $offset = $linenr - 1;
808 $line = $rawlines[$offset++];
809 next if (defined($line) && $line =~ /^-/);
821 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
824 $coded = sprintf("^%c", unpack('C', $2) + 64);
833 my $av_preprocessor = 0;
839 $av_preprocessor = 0;
841 @av_paren_type = ('E');
842 $av_pend_colon = 'O';
845 sub annotate_values {
846 my ($stream, $type) = @_;
849 my $var = '_' x length($stream);
852 print "$stream\n" if ($dbg_values > 1);
854 while (length($cur)) {
855 @av_paren_type = ('E') if ($#av_paren_type < 0);
856 print " <" . join('', @av_paren_type) .
857 "> <$type> <$av_pending>" if ($dbg_values > 1);
858 if ($cur =~ /^(\s+)/o) {
859 print "WS($1)\n" if ($dbg_values > 1);
860 if ($1 =~ /\n/ && $av_preprocessor) {
861 $type = pop(@av_paren_type);
862 $av_preprocessor = 0;
865 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
866 print "CAST($1)\n" if ($dbg_values > 1);
867 push(@av_paren_type, $type);
870 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
871 print "DECLARE($1)\n" if ($dbg_values > 1);
874 } elsif ($cur =~ /^($Modifier)\s*/) {
875 print "MODIFIER($1)\n" if ($dbg_values > 1);
878 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
879 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
880 $av_preprocessor = 1;
881 push(@av_paren_type, $type);
887 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
888 print "UNDEF($1)\n" if ($dbg_values > 1);
889 $av_preprocessor = 1;
890 push(@av_paren_type, $type);
892 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
893 print "PRE_START($1)\n" if ($dbg_values > 1);
894 $av_preprocessor = 1;
896 push(@av_paren_type, $type);
897 push(@av_paren_type, $type);
900 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
901 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
902 $av_preprocessor = 1;
904 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
908 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
909 print "PRE_END($1)\n" if ($dbg_values > 1);
911 $av_preprocessor = 1;
913 # Assume all arms of the conditional end as this
914 # one does, and continue as if the #endif was not here.
916 push(@av_paren_type, $type);
919 } elsif ($cur =~ /^(\\\n)/o) {
920 print "PRECONT($1)\n" if ($dbg_values > 1);
922 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
923 print "ATTR($1)\n" if ($dbg_values > 1);
927 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
928 print "SIZEOF($1)\n" if ($dbg_values > 1);
934 } elsif ($cur =~ /^(if|while|for)\b/o) {
935 print "COND($1)\n" if ($dbg_values > 1);
939 } elsif ($cur =~/^(case)/o) {
940 print "CASE($1)\n" if ($dbg_values > 1);
941 $av_pend_colon = 'C';
944 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
945 print "KEYWORD($1)\n" if ($dbg_values > 1);
948 } elsif ($cur =~ /^(\()/o) {
949 print "PAREN('$1')\n" if ($dbg_values > 1);
950 push(@av_paren_type, $av_pending);
954 } elsif ($cur =~ /^(\))/o) {
955 my $new_type = pop(@av_paren_type);
956 if ($new_type ne '_') {
958 print "PAREN('$1') -> $type\n"
959 if ($dbg_values > 1);
961 print "PAREN('$1')\n" if ($dbg_values > 1);
964 } elsif ($cur =~ /^($Ident)\s*\(/o) {
965 print "FUNC($1)\n" if ($dbg_values > 1);
969 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
970 if (defined $2 && $type eq 'C' || $type eq 'T') {
971 $av_pend_colon = 'B';
972 } elsif ($type eq 'E') {
973 $av_pend_colon = 'L';
975 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
978 } elsif ($cur =~ /^($Ident|$Constant)/o) {
979 print "IDENT($1)\n" if ($dbg_values > 1);
982 } elsif ($cur =~ /^($Assignment)/o) {
983 print "ASSIGN($1)\n" if ($dbg_values > 1);
986 } elsif ($cur =~/^(;|{|})/) {
987 print "END($1)\n" if ($dbg_values > 1);
989 $av_pend_colon = 'O';
991 } elsif ($cur =~/^(,)/) {
992 print "COMMA($1)\n" if ($dbg_values > 1);
995 } elsif ($cur =~ /^(\?)/o) {
996 print "QUESTION($1)\n" if ($dbg_values > 1);
999 } elsif ($cur =~ /^(:)/o) {
1000 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1002 substr($var, length($res), 1, $av_pend_colon);
1003 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1008 $av_pend_colon = 'O';
1010 } elsif ($cur =~ /^(\[)/o) {
1011 print "CLOSE($1)\n" if ($dbg_values > 1);
1014 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1017 print "OPV($1)\n" if ($dbg_values > 1);
1024 substr($var, length($res), 1, $variant);
1027 } elsif ($cur =~ /^($Operators)/o) {
1028 print "OP($1)\n" if ($dbg_values > 1);
1029 if ($1 ne '++' && $1 ne '--') {
1033 } elsif ($cur =~ /(^.)/o) {
1034 print "C($1)\n" if ($dbg_values > 1);
1037 $cur = substr($cur, length($1));
1038 $res .= $type x length($1);
1042 return ($res, $var);
1046 my ($possible, $line) = @_;
1047 my $notPermitted = qr{(?:
1062 ^(?:typedef|struct|enum)\b
1064 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1065 if ($possible !~ $notPermitted) {
1066 # Check for modifiers.
1067 $possible =~ s/\s*$Storage\s*//g;
1068 $possible =~ s/\s*$Sparse\s*//g;
1069 if ($possible =~ /^\s*$/) {
1071 } elsif ($possible =~ /\s/) {
1072 $possible =~ s/\s*$Type\s*//g;
1073 for my $modifier (split(' ', $possible)) {
1074 if ($modifier !~ $notPermitted) {
1075 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1076 push(@modifierList, $modifier);
1081 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1082 push(@typeList, $possible);
1086 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1093 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1096 my $line = $prefix . $_[0];
1098 $line = (split('\n', $line))[0] . "\n" if ($terse);
1100 push(our @report, $line);
1108 if (report("error: $_[0]\n")) {
1113 if (report("warning: $_[0]\n")) {
1118 if ($check && report("CHECK: $_[0]\n")) {
1123 sub check_absolute_file {
1124 my ($absolute, $herecurr) = @_;
1125 my $file = $absolute;
1127 ##print "absolute<$absolute>\n";
1129 # See if any suffix of this path is a path within the tree.
1130 while ($file =~ s@^[^/]*/@@) {
1131 if (-f "$root/$file") {
1132 ##print "file<$file>\n";
1140 # It is, so see if the prefix is acceptable.
1141 my $prefix = $absolute;
1142 substr($prefix, -length($file)) = '';
1144 ##print "prefix<$prefix>\n";
1145 if ($prefix ne ".../") {
1146 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1151 my $filename = shift;
1157 my $stashrawline="";
1173 # Trace the real file/line as we go.
1179 my $comment_edge = 0;
1183 my $prev_values = 'E';
1186 my %suppress_ifbraces;
1187 my %suppress_whiletrailers;
1188 my %suppress_export;
1190 # Pre-scan the patch sanitizing the lines.
1191 # Pre-scan the patch looking for any __setup documentation.
1193 my @setup_docs = ();
1196 sanitise_line_reset();
1198 foreach my $rawline (@rawlines) {
1202 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1204 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1209 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1218 # Guestimate if this is a continuing comment. Run
1219 # the context looking for a comment "edge". If this
1220 # edge is a close comment then we must be in a comment
1224 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1225 next if (defined $rawlines[$ln - 1] &&
1226 $rawlines[$ln - 1] =~ /^-/);
1228 #print "RAW<$rawlines[$ln - 1]>\n";
1229 last if (!defined $rawlines[$ln - 1]);
1230 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1231 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1236 if (defined $edge && $edge eq '*/') {
1240 # Guestimate if this is a continuing comment. If this
1241 # is the start of a diff block and this line starts
1242 # ' *' then it is very likely a comment.
1243 if (!defined $edge &&
1244 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1249 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1250 sanitise_line_reset($in_comment);
1252 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1253 # Standardise the strings and chars within the input to
1254 # simplify matching -- only bother with positive lines.
1255 $line = sanitise_line($rawline);
1258 # extract the filename as it passes
1259 if ($line =~ /^diff --git.*?(\S+)$/) {
1261 $realfile =~ s@^([^/]*)/@@;
1263 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1265 $realfile =~ s@^([^/]*)/@@;
1268 next if ($realfile =~ /\.patch/);
1269 next if ($realfile =~ /wire(test|check)\.c/);
1271 push(@lines, $line);
1274 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1279 #print "==>$rawline\n";
1280 #print "-->$line\n";
1282 if ($setup_docs && $line =~ /^\+/) {
1283 push(@setup_docs, $line);
1291 foreach my $line (@lines) {
1294 my $rawline = $rawlines[$linenr - 1];
1296 #extract the line range in the file after the patch is applied
1297 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1299 $first_line = $linenr + 1;
1309 %suppress_ifbraces = ();
1310 %suppress_whiletrailers = ();
1311 %suppress_export = ();
1314 # track the line number as we move through the hunk, note that
1315 # new versions of GNU diff omit the leading space on completely
1316 # blank context lines so we need to count that too.
1317 } elsif ($line =~ /^( |\+|$)/) {
1319 $realcnt-- if ($realcnt != 0);
1321 # Measure the line length and indent.
1322 ($length, $indent) = line_stats($rawline);
1324 # Track the previous line.
1325 ($prevline, $stashline) = ($stashline, $line);
1326 ($previndent, $stashindent) = ($stashindent, $indent);
1327 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1329 #warn "line<$line>\n";
1331 } elsif ($realcnt == 1) {
1335 my $hunk_line = ($realcnt != 0);
1337 #make up the handle for any error we report on this line
1338 $prefix = "$filename:$realline: " if ($emacs && $file);
1339 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1341 $here = "#$linenr: " if (!$file);
1342 $here = "#$realline: " if ($file);
1344 # extract the filename as it passes
1345 if ($line =~ /^diff --git.*?(\S+)$/) {
1347 $realfile =~ s@^([^/]*)/@@;
1349 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1351 $realfile =~ s@^([^/]*)/@@;
1354 if (!$file && $tree && $p1_prefix ne '' &&
1355 -e "$root/$p1_prefix") {
1356 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1359 if ($realfile =~ m@^include/asm/@) {
1360 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1365 $here .= "FILE: $realfile:$realline:" if ($realcnt != 1);
1367 my $hereline = "$here\n$rawline\n";
1368 my $herecurr = "$here\n$rawline\n";
1369 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1371 $cnt_lines++ if ($realcnt != 0);
1373 # Check for incorrect file permissions
1374 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1375 my $permhere = $here . "FILE: $realfile\n";
1376 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1377 ERROR("do not set execute permissions for source files\n" . $permhere);
1381 #check the patch for a signoff:
1382 if ($line =~ /^\s*signed-off-by:/i) {
1383 # This is a signoff, if ugly, so do not double report.
1385 if (!($line =~ /^\s*Signed-off-by:/)) {
1386 WARN("Signed-off-by: is the preferred form\n" .
1389 if ($line =~ /^\s*signed-off-by:\S/i) {
1390 WARN("space required after Signed-off-by:\n" .
1395 # Check for wrappage within a valid hunk of the file
1396 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1397 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1398 $herecurr) if (!$emitted_corrupt++);
1401 # Check for absolute kernel paths.
1403 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1406 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1407 check_absolute_file($1, $herecurr)) {
1410 check_absolute_file($file, $herecurr);
1415 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1416 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1417 $rawline !~ m/^$UTF8*$/) {
1418 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1420 my $blank = copy_spacing($rawline);
1421 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1422 my $hereptr = "$hereline$ptr\n";
1424 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1427 # ignore non-hunk lines and lines being removed
1428 next if (!$hunk_line || $line =~ /^-/);
1430 #trailing whitespace
1431 if ($line =~ /^\+.*\015/) {
1432 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1433 ERROR("DOS line endings\n" . $herevet);
1435 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1436 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1437 ERROR("trailing whitespace\n" . $herevet);
1441 # check for Kconfig help text having a real description
1442 # Only applies when adding the entry originally, after that we do not have
1443 # sufficient context to determine whether it is indeed long enough.
1444 if ($realfile =~ /Kconfig/ &&
1445 $line =~ /\+\s*(?:---)?help(?:---)?$/) {
1448 my $ln = $linenr + 1;
1451 while ($cnt > 0 && defined $lines[$ln - 1]) {
1452 $f = $lines[$ln - 1];
1453 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1454 $is_end = $lines[$ln - 1] =~ /^\+/;
1457 next if ($f =~ /^-/);
1461 next if ($f =~ /^$/);
1462 if ($f =~ /^\s*config\s/) {
1468 WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
1469 #print "is_end<$is_end> length<$length>\n";
1472 # check we are in a valid source file if not then ignore this hunk
1473 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1476 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1477 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1478 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
1479 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1482 WARN("line over 80 characters\n" . $herecurr);
1485 # check for spaces before a quoted newline
1486 if ($rawline =~ /^.*\".*\s\\n/) {
1487 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1490 # check for adding lines without a newline.
1491 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1492 WARN("adding a line without newline at end of file\n" . $herecurr);
1495 # Blackfin: use hi/lo macros
1496 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1497 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1498 my $herevet = "$here\n" . cat_vet($line) . "\n";
1499 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1501 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1502 my $herevet = "$here\n" . cat_vet($line) . "\n";
1503 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1507 # check we are in a valid source file C or perl if not then ignore this hunk
1508 next if ($realfile !~ /\.(h|c|pl)$/);
1510 # at the beginning of a line any tabs must come first and anything
1511 # more than 8 must use tabs.
1512 if ($tabs && ($rawline =~ /^\+\s* \t\s*\S/ ||
1513 $rawline =~ /^\+\s* \s*/)) {
1514 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1515 ERROR("code indent should use tabs where possible\n" . $herevet);
1518 } elsif (! $tabs && $rawline =~ /^\+.*\t/) {
1519 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1520 ERROR("code indent should use spaces where possible\n" . $herevet);
1524 # check for space before tabs.
1525 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1526 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1527 WARN("please, no space before tabs\n" . $herevet);
1530 # check for spaces at the beginning of a line.
1532 # 1) within comments
1533 # 2) indented preprocessor commands
1535 if ($tabs && $rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
1536 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1537 WARN("please, no spaces at the start of a line\n" . $herevet);
1540 # check we are in a valid C source file if not then ignore this hunk
1541 next if ($realfile !~ /\.(h|c)$/);
1543 # check for RCS/CVS revision markers
1544 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1545 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1548 # Blackfin: don't use __builtin_bfin_[cs]sync
1549 if ($line =~ /__builtin_bfin_csync/) {
1550 my $herevet = "$here\n" . cat_vet($line) . "\n";
1551 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1553 if ($line =~ /__builtin_bfin_ssync/) {
1554 my $herevet = "$here\n" . cat_vet($line) . "\n";
1555 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1558 # Check for potential 'bare' types
1559 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1561 if ($realcnt && $line =~ /.\s*\S/) {
1562 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1563 ctx_statement_block($linenr, $realcnt, 0);
1564 $stat =~ s/\n./\n /g;
1565 $cond =~ s/\n./\n /g;
1567 # Find the real next line.
1568 $realline_next = $line_nr_next;
1569 if (defined $realline_next &&
1570 (!defined $lines[$realline_next - 1] ||
1571 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1578 # Ignore goto labels.
1579 if ($s =~ /$Ident:\*$/s) {
1581 # Ignore functions being called
1582 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1584 } elsif ($s =~ /^.\s*else\b/s) {
1586 # declarations always start with types
1587 } 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) {
1590 possible($type, "A:" . $s);
1592 # definitions in global scope can only start with types
1593 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1594 possible($1, "B:" . $s);
1597 # any (foo ... *) is a pointer cast, and foo is a type
1598 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1599 possible($1, "C:" . $s);
1602 # Check for any sort of function declaration.
1603 # int foo(something bar, other baz);
1604 # void (*store_gdt)(x86_descr_ptr *);
1605 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1606 my ($name_len) = length($1);
1609 substr($ctx, 0, $name_len + 1, '');
1610 $ctx =~ s/\)[^\)]*$//;
1612 for my $arg (split(/\s*,\s*/, $ctx)) {
1613 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1615 possible($1, "D:" . $s);
1623 # Checks which may be anchored in the context.
1626 # Check for switch () and associated case and default
1627 # statements should be at the same indent.
1628 if ($line=~/\bswitch\s*\(.*\)/) {
1631 my @ctx = ctx_block_outer($linenr, $realcnt);
1633 for my $ctx (@ctx) {
1634 my ($clen, $cindent) = line_stats($ctx);
1635 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1636 $indent != $cindent) {
1637 $err .= "$sep$ctx\n";
1644 ERROR("switch and case should be at the same indent\n$hereline$err");
1648 # if/while/etc brace do not go on next line, unless defining a do while loop,
1649 # or if that brace on the next line is for something else
1650 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1651 my $pre_ctx = "$1$2";
1653 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1654 my $ctx_cnt = $realcnt - $#ctx - 1;
1655 my $ctx = join("\n", @ctx);
1657 my $ctx_ln = $linenr;
1658 my $ctx_skip = $realcnt;
1660 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1661 defined $lines[$ctx_ln - 1] &&
1662 $lines[$ctx_ln - 1] =~ /^-/)) {
1663 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1664 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1668 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1669 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1671 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1672 ERROR("that open brace { should be on the previous line\n" .
1673 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1675 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1676 $ctx =~ /\)\s*\;\s*$/ &&
1677 defined $lines[$ctx_ln - 1])
1679 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1680 if ($nindent > $indent) {
1681 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1682 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1687 # Check relative indent for conditionals and blocks.
1688 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1689 my ($s, $c) = ($stat, $cond);
1691 substr($s, 0, length($c), '');
1693 # Make sure we remove the line prefixes as we have
1694 # none on the first line, and are going to readd them
1698 # Find out how long the conditional actually is.
1699 my @newlines = ($c =~ /\n/gs);
1700 my $cond_lines = 1 + $#newlines;
1702 # We want to check the first line inside the block
1703 # starting at the end of the conditional, so remove:
1704 # 1) any blank line termination
1705 # 2) any opening brace { on end of the line
1707 my $continuation = 0;
1709 $s =~ s/^.*\bdo\b//;
1711 if ($s =~ s/^\s*\\//) {
1714 if ($s =~ s/^\s*?\n//) {
1719 # Also ignore a loop construct at the end of a
1720 # preprocessor statement.
1721 if (($prevline =~ /^.\s*#\s*define\s/ ||
1722 $prevline =~ /\\\s*$/) && $continuation == 0) {
1728 while ($cond_ptr != $cond_lines) {
1729 $cond_ptr = $cond_lines;
1731 # If we see an #else/#elif then the code
1733 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1738 # 1) blank lines, they should be at 0,
1739 # 2) preprocessor lines, and
1741 if ($continuation ||
1743 $s =~ /^\s*#\s*?/ ||
1744 $s =~ /^\s*$Ident\s*:/) {
1745 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1746 if ($s =~ s/^.*?\n//) {
1752 my (undef, $sindent) = line_stats("+" . $s);
1753 my $stat_real = raw_line($linenr, $cond_lines);
1755 # Check if either of these lines are modified, else
1756 # this is not this patch's fault.
1757 if (!defined($stat_real) ||
1758 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1761 if (defined($stat_real) && $cond_lines > 1) {
1762 $stat_real = "[...]\n$stat_real";
1765 #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";
1767 if ($check && (($sindent % 8) != 0 ||
1768 ($sindent <= $indent && $s ne ''))) {
1769 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1773 # Track the 'values' across context and added lines.
1774 my $opline = $line; $opline =~ s/^./ /;
1775 my ($curr_values, $curr_vars) =
1776 annotate_values($opline . "\n", $prev_values);
1777 $curr_values = $prev_values . $curr_values;
1779 my $outline = $opline; $outline =~ s/\t/ /g;
1780 print "$linenr > .$outline\n";
1781 print "$linenr > $curr_values\n";
1782 print "$linenr > $curr_vars\n";
1784 $prev_values = substr($curr_values, -1);
1786 #ignore lines not being added
1787 if ($line=~/^[^\+]/) {next;}
1789 # TEST: allow direct testing of the type matcher.
1791 if ($line =~ /^.\s*$Declare\s*$/) {
1792 ERROR("TEST: is type\n" . $herecurr);
1793 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1794 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1798 # TEST: allow direct testing of the attribute matcher.
1800 if ($line =~ /^.\s*$Modifier\s*$/) {
1801 ERROR("TEST: is attr\n" . $herecurr);
1802 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1803 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1808 # check for initialisation to aggregates open brace on the next line
1809 if ($line =~ /^.\s*{/ &&
1810 $prevline =~ /(?:^|[^=])=\s*$/) {
1811 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1815 # Checks which are anchored on the added line.
1818 # check for malformed paths in #include statements (uses RAW line)
1819 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1821 if ($path =~ m{//}) {
1822 ERROR("malformed #include filename\n" .
1827 # no C99 // comments
1828 if ($line =~ m{//}) {
1829 ERROR("do not use C99 // comments\n" . $herecurr);
1831 # Remove C99 comments.
1833 $opline =~ s@//.*@@;
1835 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1836 # the whole statement.
1837 #print "APW <$lines[$realline_next - 1]>\n";
1838 if (defined $realline_next &&
1839 exists $lines[$realline_next - 1] &&
1840 !defined $suppress_export{$realline_next} &&
1841 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1842 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1843 # Handle definitions which produce identifiers with
1846 # EXPORT_SYMBOL(something_foo);
1848 if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
1849 $name =~ /^${Ident}_$2/) {
1850 #print "FOO C name<$name>\n";
1851 $suppress_export{$realline_next} = 1;
1853 } elsif ($stat !~ /(?:
1855 ^.DEFINE_$Ident\(\Q$name\E\)|
1856 ^.DECLARE_$Ident\(\Q$name\E\)|
1857 ^.LIST_HEAD\(\Q$name\E\)|
1858 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1859 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
1861 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1862 $suppress_export{$realline_next} = 2;
1864 $suppress_export{$realline_next} = 1;
1867 if (!defined $suppress_export{$linenr} &&
1868 $prevline =~ /^.\s*$/ &&
1869 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1870 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1871 #print "FOO B <$lines[$linenr - 1]>\n";
1872 $suppress_export{$linenr} = 2;
1874 if (defined $suppress_export{$linenr} &&
1875 $suppress_export{$linenr} == 2) {
1876 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1879 # check for global initialisers.
1880 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1881 ERROR("do not initialise globals to 0 or NULL\n" .
1884 # check for static initialisers.
1885 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1886 ERROR("do not initialise statics to 0 or NULL\n" .
1890 # check for static const char * arrays.
1891 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
1892 WARN("static const char * array should probably be static const char * const\n" .
1896 # check for static char foo[] = "bar" declarations.
1897 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
1898 WARN("static char array declaration should probably be static const char\n" .
1902 # check for declarations of struct pci_device_id
1903 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
1904 WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
1907 # check for new typedefs, only function parameters and sparse annotations
1909 if ($line =~ /\btypedef\s/ &&
1910 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1911 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
1912 $line !~ /\b$typeTypedefs\b/ &&
1913 $line !~ /\b__bitwise(?:__|)\b/) {
1914 WARN("do not add new typedefs\n" . $herecurr);
1917 # * goes on variable not on type
1919 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1920 my ($from, $to) = ($1, $1);
1922 # Should start with a space.
1923 $to =~ s/^(\S)/ $1/;
1924 # Should not end with a space.
1926 # '*'s should not have spaces between.
1927 while ($to =~ s/\*\s+\*/\*\*/) {
1930 #print "from<$from> to<$to>\n";
1932 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1934 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1935 my ($from, $to, $ident) = ($1, $1, $2);
1937 # Should start with a space.
1938 $to =~ s/^(\S)/ $1/;
1939 # Should not end with a space.
1941 # '*'s should not have spaces between.
1942 while ($to =~ s/\*\s+\*/\*\*/) {
1944 # Modifiers should have spaces.
1945 $to =~ s/(\b$Modifier$)/$1 /;
1947 #print "from<$from> to<$to> ident<$ident>\n";
1948 if ($from ne $to && $ident !~ /^$Modifier$/) {
1949 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1953 # # no BUG() or BUG_ON()
1954 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1955 # WARN("Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n");
1958 if ($line =~ /\bLINUX_VERSION_CODE\b/ &&
1959 $realfile !~ /compat/) {
1960 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1963 # printk should use KERN_* levels. Note that follow on printk's on the
1964 # same line do not need a level, so we use the current block context
1965 # to try and find and validate the current printk. In summary the current
1966 # printk includes all preceding printk's which have no newline on the end.
1967 # we assume the first bad printk is the one to report.
1968 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1970 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1971 #print "CHECK<$lines[$ln - 1]\n";
1972 # we have a preceding printk if it ends
1973 # with "\n" ignore it, else it is to blame
1974 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1975 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1982 WARN("printk() should include KERN_ facility level\n" . $herecurr);
1986 # function brace can't be on same line, except for #defines of do while,
1987 # or if closed on same line
1988 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1989 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1990 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1993 # open braces for enum, union and struct go on the same line.
1994 if ($line =~ /^.\s*{/ &&
1995 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1996 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1999 # missing space after union, struct or enum definition
2000 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2001 WARN("missing space after $1 definition\n" . $herecurr);
2004 # check for spacing round square brackets; allowed:
2005 # 1. with a type on the left -- int [] a;
2006 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2007 # 3. inside a curly brace -- = { [0...10] = 5 }
2008 while ($line =~ /(.*?\s)\[/g) {
2009 my ($where, $prefix) = ($-[1], $1);
2010 if ($prefix !~ /$Type\s+$/ &&
2011 ($where != 0 || $prefix !~ /^.\s+$/) &&
2012 $prefix !~ /{\s+$/) {
2013 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
2017 # check for spaces between functions and their parentheses.
2018 while ($line =~ /($Ident)\s+\(/g) {
2020 my $ctx_before = substr($line, 0, $-[1]);
2021 my $ctx = "$ctx_before$name";
2023 # Ignore those directives where spaces _are_ permitted.
2025 if|for|while|switch|return|case|
2026 volatile|__volatile__|
2027 __attribute__|format|__extension__|
2031 # cpp #define statements have non-optional spaces, ie
2032 # if there is a space between the name and the open
2033 # parenthesis it is simply not a parameter group.
2034 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2036 # cpp #elif statement condition may start with a (
2037 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2039 # If this whole things ends with a type its most
2040 # likely a typedef for a function.
2041 } elsif ($ctx =~ /$Type$/) {
2044 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
2047 # Check operator spacing.
2048 if (!($line=~/\#\s*include/)) {
2050 <<=|>>=|<=|>=|==|!=|
2051 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2052 =>|->|<<|>>|<|>|=|!|~|
2053 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2056 my @elements = split(/($ops|;)/, $opline);
2059 my $blank = copy_spacing($opline);
2061 for (my $n = 0; $n < $#elements; $n += 2) {
2062 $off += length($elements[$n]);
2064 # Pick up the preceding and succeeding characters.
2065 my $ca = substr($opline, 0, $off);
2067 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2068 $cc = substr($opline, $off + length($elements[$n + 1]));
2070 my $cb = "$ca$;$cc";
2073 $a = 'V' if ($elements[$n] ne '');
2074 $a = 'W' if ($elements[$n] =~ /\s$/);
2075 $a = 'C' if ($elements[$n] =~ /$;$/);
2076 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2077 $a = 'O' if ($elements[$n] eq '');
2078 $a = 'E' if ($ca =~ /^\s*$/);
2080 my $op = $elements[$n + 1];
2083 if (defined $elements[$n + 2]) {
2084 $c = 'V' if ($elements[$n + 2] ne '');
2085 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2086 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2087 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2088 $c = 'O' if ($elements[$n + 2] eq '');
2089 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2094 my $ctx = "${a}x${c}";
2096 my $at = "(ctx:$ctx)";
2098 my $ptr = substr($blank, 0, $off) . "^";
2099 my $hereptr = "$hereline$ptr\n";
2101 # Pull out the value of this operator.
2102 my $op_type = substr($curr_values, $off + 1, 1);
2104 # Get the full operator variant.
2105 my $opv = $op . substr($curr_vars, $off, 1);
2107 # Ignore operators passed as parameters.
2108 if ($op_type ne 'V' &&
2109 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2112 # } elsif ($op =~ /^$;+$/) {
2114 # ; should have either the end of line or a space or \ after it
2115 } elsif ($op eq ';') {
2116 if ($ctx !~ /.x[WEBC]/ &&
2117 $cc !~ /^\\/ && $cc !~ /^;/ &&
2119 ERROR("space required after that '$op' $at\n" . $hereptr);
2123 } elsif ($op eq '//') {
2127 # : when part of a bitfield
2128 } elsif ($op eq '->' || $opv eq ':B') {
2129 if ($ctx =~ /Wx.|.xW/) {
2130 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
2133 # , must have a space on the right.
2134 } elsif ($op eq ',') {
2135 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
2137 $line !~ /KERNEL_VERSION/ &&
2138 $line !~ /OBD_OCD_VERSION/) {
2139 WARN("space required after that '$op' $at\n" . $hereptr);
2142 # '*' as part of a type definition -- reported already.
2143 } elsif ($opv eq '*_') {
2144 #warn "'*' is part of type\n";
2146 # unary operators should have a space before and
2147 # none after. May be left adjacent to another
2148 # unary operator, or a cast
2149 } elsif ($op eq '!' || $op eq '~' ||
2150 $opv eq '*U' || $opv eq '-U' ||
2151 $opv eq '&U' || $opv eq '&&U') {
2152 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2153 ERROR("space required before that '$op' $at\n" . $hereptr);
2155 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2156 # A unary '*' may be const
2158 } elsif ($ctx =~ /.xW/) {
2159 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2162 # unary ++ and unary -- are allowed no space on one side.
2163 } elsif ($op eq '++' or $op eq '--') {
2164 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2165 ERROR("space required one side of that '$op' $at\n" . $hereptr);
2167 if ($ctx =~ /Wx[BE]/ ||
2168 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2169 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2171 if ($ctx =~ /ExW/) {
2172 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2176 # << and >> may either have or not have spaces both sides
2177 } elsif ($op eq '<<' or $op eq '>>' or
2178 $op eq '&' or $op eq '^' or $op eq '|' or
2179 $op eq '+' or $op eq '-' or
2180 $op eq '*' or $op eq '/' or
2183 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2184 ERROR("need consistent spacing around '$op' $at\n" .
2188 # A colon needs no spaces before when it is
2189 # terminating a case value or a label.
2190 } elsif ($opv eq ':C' || $opv eq ':L') {
2191 if ($ctx =~ /Wx./) {
2192 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2195 # All the others need spaces both sides.
2196 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2199 # Ignore email addresses <foo@bar>
2201 $cc =~ /^\S+\@\S+>/) ||
2203 $ca =~ /<\S+\@\S+$/))
2209 if (($opv eq ':O' && $ca =~ /\?$/) ||
2210 ($op eq '?' && $cc =~ /^:/)) {
2214 if ($length >= 79) {
2219 ERROR("spaces required around that '$op' $at\n" . $hereptr);
2222 $off += length($elements[$n + 1]);
2226 # check for multiple assignments
2227 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2228 CHK("multiple assignments should be avoided\n" . $herecurr);
2231 ## # check for multiple declarations, allowing for a function declaration
2233 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2234 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2236 ## # Remove any bracketed sections to ensure we do not
2237 ## # falsly report the parameters of functions.
2239 ## while ($ln =~ s/\([^\(\)]*\)//g) {
2241 ## if ($ln =~ /,/) {
2242 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2246 #need space before brace following if, while, etc
2247 if ((($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2248 $line =~ /do{/) && $length <= 79) {
2249 ERROR("space required before the open brace '{'\n" . $herecurr);
2252 # closing brace should have a space following it when it has anything
2254 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2255 ERROR("space required after that close brace '}'\n" . $herecurr);
2258 # check spacing on square brackets
2259 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2260 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2262 if ($line =~ /\s\]/) {
2263 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2266 # check spacing on parentheses
2267 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2268 $line !~ /for\s*\(\s+;/) {
2269 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2271 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2272 $line !~ /for\s*\(.*;\s+\)/ &&
2273 $line !~ /:\s+\)/) {
2274 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2277 #goto labels aren't indented, allow a single space however
2278 # if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2279 # !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2280 # WARN("labels should not be indented\n" . $herecurr);
2283 # Return is not a function.
2284 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2288 # Flatten any parentheses
2289 $value =~ s/\(/ \(/g;
2290 $value =~ s/\)/\) /g;
2291 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2292 $value !~ /(?:$Ident|-?$Constant)\s*
2294 (?:$Ident|-?$Constant)/x &&
2295 $value =~ s/\([^\(\)]*\)/1/) {
2297 #print "value<$value>\n";
2298 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2299 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2301 } elsif ($spacing !~ /\s+/) {
2302 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2305 # Return of what appears to be an errno should normally be -'ve
2306 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2308 if ($name ne 'EOF' && $name ne 'ERROR') {
2309 WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2313 # Need a space before open parenthesis after if, while etc
2314 if ($line=~/\b(if|while|for|switch)\(/) {
2315 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2318 # Check for illegal assignment in if conditional -- and check for trailing
2319 # statements after the conditional.
2320 if ($line =~ /do\s*(?!{)/) {
2321 my ($stat_next) = ctx_statement_block($line_nr_next,
2322 $remain_next, $off_next);
2323 $stat_next =~ s/\n./\n /g;
2324 ##print "stat<$stat> stat_next<$stat_next>\n";
2326 if ($stat_next =~ /^\s*while\b/) {
2327 # If the statement carries leading newlines,
2328 # then count those as offsets.
2330 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2332 statement_rawlines($whitespace) - 1;
2334 $suppress_whiletrailers{$line_nr_next +
2338 if (!defined $suppress_whiletrailers{$linenr} &&
2339 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2340 my ($s, $c) = ($stat, $cond);
2342 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2343 ERROR("do not use assignment in if condition\n" . $herecurr);
2346 # Find out what is on the end of the line after the
2348 substr($s, 0, length($c), '');
2350 $s =~ s/$;//g; # Remove any comments
2351 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2352 $c !~ /}\s*while\s*/)
2354 # Find out how long the conditional actually is.
2355 my @newlines = ($c =~ /\n/gs);
2356 my $cond_lines = 1 + $#newlines;
2359 $stat_real = raw_line($linenr, $cond_lines)
2360 . "\n" if ($cond_lines);
2361 if (defined($stat_real) && $cond_lines > 1) {
2362 $stat_real = "[...]\n$stat_real";
2365 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2369 # Check for bitwise tests written as boolean
2381 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2384 # if and else should not have general statements after it
2385 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2387 $s =~ s/$;//g; # Remove any comments
2388 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2389 ERROR("trailing statements should be on next line\n" . $herecurr);
2392 # if should not continue a brace
2393 if ($line =~ /}\s*if\b/) {
2394 ERROR("trailing statements should be on next line\n" .
2397 # case and default should not have general statements after them
2398 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2400 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2404 ERROR("trailing statements should be on next line\n" . $herecurr);
2407 # Check for }<nl>else {, these must be at the same
2408 # indent level to be relevant to each other.
2409 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2410 $previndent == $indent) {
2411 ERROR("else should follow close brace '}'\n" . $hereprev);
2414 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2415 $previndent == $indent) {
2416 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2418 # Find out what is on the end of the line after the
2420 substr($s, 0, length($c), '');
2423 if ($s =~ /^\s*;/) {
2424 ERROR("while should follow close brace '}'\n" . $hereprev);
2428 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2429 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2430 # WARN("No studly caps, use _\n");
2433 #no spaces allowed after \ in define
2434 if ($line=~/\#\s*define.*\\\s$/) {
2435 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
2438 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2439 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2441 my $checkfile = "include/linux/$file";
2442 if (-f "$root/$checkfile" &&
2443 $realfile ne $checkfile &&
2444 $1 !~ /$allowed_asm_includes/)
2446 if ($realfile =~ m{^arch/}) {
2447 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2449 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2454 # multi-statement macros should be enclosed in a do while loop, grab the
2455 # first statement and ensure its the whole macro if its not enclosed
2456 # in a known good container
2457 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2458 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2461 my ($off, $dstat, $dcond, $rest);
2464 my $args = defined($1);
2466 # Find the end of the macro and limit our statement
2468 while ($cnt > 0 && defined $lines[$ln - 1] &&
2469 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2471 $ctx .= $rawlines[$ln - 1] . "\n";
2472 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2475 $ctx .= $rawlines[$ln - 1];
2477 ($dstat, $dcond, $ln, $cnt, $off) =
2478 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2479 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2480 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2482 # Extract the remainder of the define (if any) and
2483 # rip off surrounding spaces, and trailing \'s.
2485 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2486 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2487 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2488 $rest .= substr($lines[$ln - 1], $off) . "\n";
2494 $rest =~ s/\\\n.//g;
2498 # Clean up the original statement.
2500 substr($dstat, 0, length($dcond), '');
2502 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2505 $dstat =~ s/\\\n.//g;
2506 $dstat =~ s/^\s*//s;
2507 $dstat =~ s/\s*$//s;
2509 # Flatten any parentheses and braces
2510 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2511 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2512 $dstat =~ s/\[[^\{\}]*\]/1/)
2516 my $exceptions = qr{
2528 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2529 if ($rest ne '' && $rest ne ',') {
2530 if ($rest !~ /while\s*\(/ &&
2531 $dstat !~ /$exceptions/)
2533 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2536 } elsif ($ctx !~ /;/) {
2538 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2539 $dstat !~ /$exceptions/ &&
2540 $dstat !~ /^\.$Ident\s*=/ &&
2541 $dstat =~ /$Operators/)
2543 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2548 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2549 # all assignments may have only one of the following with an assignment:
2552 # VMLINUX_SYMBOL(...)
2553 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2554 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2557 # check for redundant bracing round if etc
2558 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2559 my ($level, $endln, @chunks) =
2560 ctx_statement_full($linenr, $realcnt, 1);
2561 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2562 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2563 if ($#chunks > 0 && $level == 0) {
2566 my $herectx = $here . "\n";
2567 my $ln = $linenr - 1;
2568 for my $chunk (@chunks) {
2569 my ($cond, $block) = @{$chunk};
2571 # If the condition carries leading newlines, then count those as offsets.
2572 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2573 my $offset = statement_rawlines($whitespace) - 1;
2575 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2577 # We have looked at and allowed this specific line.
2578 $suppress_ifbraces{$ln + $offset} = 1;
2580 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2581 $ln += statement_rawlines($block) - 1;
2583 substr($block, 0, length($cond), '');
2585 $seen++ if ($block =~ /^\s*{/);
2587 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2588 if (statement_lines($cond) > 1) {
2589 #print "APW: ALLOWED: cond<$cond>\n";
2592 if ($block =~/\b(?:if|for|while)\b/) {
2593 #print "APW: ALLOWED: block<$block>\n";
2596 if (statement_block_size($block) > 1) {
2597 #print "APW: ALLOWED: lines block<$block>\n";
2601 if ($seen && !$allowed) {
2602 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
2606 if (!defined $suppress_ifbraces{$linenr - 1} &&
2607 $line =~ /\b(if|while|for|else)\b/) {
2610 # Check the pre-context.
2611 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2612 #print "APW: ALLOWED: pre<$1>\n";
2616 my ($level, $endln, @chunks) =
2617 ctx_statement_full($linenr, $realcnt, $-[0]);
2619 # Check the condition.
2620 my ($cond, $block) = @{$chunks[0]};
2621 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2622 if (defined $cond) {
2623 substr($block, 0, length($cond), '');
2625 if (statement_lines($cond) > 1) {
2626 #print "APW: ALLOWED: cond<$cond>\n";
2629 if ($block =~/\b(?:if|for|while)\b/) {
2630 #print "APW: ALLOWED: block<$block>\n";
2633 if (statement_block_size($block) > 1) {
2634 #print "APW: ALLOWED: lines block<$block>\n";
2637 # Check the post-context.
2638 if (defined $chunks[1]) {
2639 my ($cond, $block) = @{$chunks[1]};
2640 if (defined $cond) {
2641 substr($block, 0, length($cond), '');
2643 if ($block =~ /^\s*\{/) {
2644 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2648 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2649 my $herectx = $here . "\n";;
2650 my $cnt = statement_rawlines($block);
2652 for (my $n = 0; $n < $cnt; $n++) {
2653 $herectx .= raw_line($linenr, $n) . "\n";;
2656 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2660 # don't include deprecated include files (uses RAW line)
2661 for my $inc (@dep_includes) {
2662 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2663 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2667 # don't use deprecated functions
2668 for my $func (@dep_functions) {
2669 if ($line =~ /\b$func\b/) {
2670 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2674 # no volatiles please
2675 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2676 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2677 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2681 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2682 CHK("if this code is redundant consider removing it\n" .
2686 # check for needless kfree() checks
2687 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2689 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2690 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2693 # check for needless usb_free_urb() checks
2694 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2696 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2697 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2701 # prefer usleep_range over udelay
2702 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2703 # ignore udelay's < 10, however
2704 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2705 CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2709 # warn about unexpectedly long msleep's
2710 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2712 WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2716 # warn about #ifdefs in C files
2717 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2718 # WARN("#ifdef in C files should be avoided\n");
2721 # warn about spacing in #ifdefs
2722 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2723 ERROR("exactly one space required after that #$1\n" . $herecurr);
2726 # check for spinlock_t definitions without a comment.
2727 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2728 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
2730 if (!ctx_has_comment($first_line, $linenr)) {
2731 CHK("$1 definition without comment\n" . $herecurr);
2734 # check for memory barriers without a comment.
2735 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2736 if (!ctx_has_comment($first_line, $linenr)) {
2737 CHK("memory barrier without comment\n" . $herecurr);
2740 # check of hardware specific defines
2741 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2742 CHK("architecture specific defines should be avoided\n" . $herecurr);
2745 # Check that the storage class is at the beginning of a declaration
2746 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2747 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2750 # check the location of the inline attribute, that it is between
2751 # storage class and type.
2752 if ($line =~ /\b$Type\s+$Inline\b/ ||
2753 $line =~ /\b$Inline\s+$Storage\b/) {
2754 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2757 # Check for __inline__ and __inline, prefer inline
2758 if ($line =~ /\b(__inline__|__inline)\b/) {
2759 WARN("plain inline is preferred over $1\n" . $herecurr);
2762 # Check for __attribute__ packed, prefer __packed
2763 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
2764 WARN("__packed is preferred over __attribute__((packed))\n" . $herecurr);
2767 # check for sizeof(&)
2768 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2769 WARN("sizeof(& should be avoided\n" . $herecurr);
2772 # check for line continuations in quoted strings with odd counts of "
2773 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
2774 WARN("Avoid line continuations in quoted strings\n" . $herecurr);
2777 # check for new externs in .c files.
2778 if ($realfile =~ /\.c$/ && defined $stat &&
2779 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2781 my $function_name = $1;
2782 my $paren_space = $2;
2785 if (defined $cond) {
2786 substr($s, 0, length($cond), '');
2788 if ($s =~ /^\s*;/ &&
2789 $function_name ne 'uninitialized_var')
2791 WARN("externs should be avoided in .c files\n" . $herecurr);
2794 if ($paren_space =~ /\n/) {
2795 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2798 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2799 $stat =~ /^.\s*extern\s+/)
2801 WARN("externs should be avoided in .c files\n" . $herecurr);
2804 # checks for new __setup's
2805 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2808 if (!grep(/$name/, @setup_docs)) {
2809 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2813 # check for pointless casting of kmalloc return
2814 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
2815 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2818 # check for multiple semicolons
2819 if ($line =~ /;\s*;\s*$/) {
2820 WARN("Statements terminations use 1 semicolon\n" . $herecurr);
2823 # check for gcc specific __FUNCTION__
2824 if ($line =~ /__FUNCTION__/) {
2825 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2828 # check for semaphores initialized locked
2829 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
2830 WARN("consider using a completion\n" . $herecurr);
2833 # recommend kstrto* over simple_strto*
2834 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2835 WARN("consider using kstrto* in preference to simple_$1\n" . $herecurr);
2837 # check for __initcall(), use device_initcall() explicitly please
2838 if ($line =~ /^.\s*__initcall\s*\(/) {
2839 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2841 # check for various ops structs, ensure they are const.
2842 my $struct_ops = qr{acpi_dock_ops|
2843 address_space_operations|
2845 block_device_operations|
2850 file_lock_operations|
2860 lock_manager_operations|
2866 pipe_buf_operations|
2867 platform_hibernation_ops|
2868 platform_suspend_ops|
2873 soc_pcmcia_socket_ops|
2879 if ($line !~ /\bconst\b/ &&
2880 $line =~ /\bstruct\s+($struct_ops)\b/) {
2881 WARN("struct $1 should normally be const\n" .
2885 # use of NR_CPUS is usually wrong
2886 # ignore definitions of NR_CPUS and usage to define arrays as likely right
2887 if ($line =~ /\bNR_CPUS\b/ &&
2888 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2889 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2890 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2891 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2892 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2894 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2897 # check for %L{u,d,i} in strings
2899 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2900 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2901 $string =~ s/%%/__/g;
2902 if ($string =~ /(?<!%)%L[udi]/) {
2903 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2908 # whine mightly about in_atomic
2909 if ($line =~ /\bin_atomic\s*\(/) {
2910 if ($realfile =~ m@^drivers/@) {
2911 ERROR("do not use in_atomic in drivers\n" . $herecurr);
2912 } elsif ($realfile !~ m@^kernel/@) {
2913 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2917 # check for lockdep_set_novalidate_class
2918 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
2919 $line =~ /__lockdep_no_validate__\s*\)/ ) {
2920 if ($realfile !~ m@^kernel/lockdep@ &&
2921 $realfile !~ m@^include/linux/lockdep@ &&
2922 $realfile !~ m@^drivers/base/core@) {
2923 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
2927 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
2928 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
2929 WARN("Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
2932 # Check for memset with swapped arguments
2933 if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
2934 ERROR("memset size is 3rd argument, not the second.\n" . $herecurr);
2938 # If we have no input at all, then there is nothing to report on
2939 # so just keep quiet.
2940 if ($#rawlines == -1) {
2944 # In mailback mode only produce a report in the negative, for
2945 # things that appear to be patches.
2946 if ($mailback && ($cnt_error == 0 || !$is_patch)) {
2950 # This is not a patch, and we are are in 'no-patch' mode so
2952 if (!$chk_patch && !$is_patch) {
2957 ERROR("Does not appear to be a unified-diff format patch\n");
2959 if ($is_patch && $chk_signoff && $signoff == 0) {
2960 ERROR("Missing Signed-off-by: line(s)\n");
2963 print report_dump();
2964 if ($summary && ($cnt_error + $cnt_warn > 0 || $quiet == 0)) {
2965 print "$filename " if ($summary_file);
2966 print "total: $cnt_error errors, $cnt_warn warnings, " .
2967 (($check)? "$cnt_chk checks, " : "") .
2968 "$cnt_lines lines checked\n";
2969 print "\n" if ($quiet == 0);
2973 # If there were whitespace errors which cleanpatch can fix
2974 # then suggest that.
2975 if ($rpt_cleaners) {
2976 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2977 print " scripts/cleanfile\n\n";
2982 if ($cnt_error == 0 && $quiet == 0) {
2983 print "$vname has no obvious style problems and is ready for submission.\n"
2984 } elsif (($cnt_error > 0 || $cnt_warn > 0) && $quiet == 0) {
2985 print "$vname has style problems, please review. If any of these errors\n";
2986 print "are false positives report them to the maintainer, see\n";
2987 print "CHECKPATCH in MAINTAINERS.\n";