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
8 # modified for coding style check for SLP (Samsung Linux Platform)
9 # Software Engineering Lab. DMC Research Center, Samsung Electronics
11 # modified for coding style check for TIZEN
12 # Software Platform Team. S/W Center, Samsung Electronics
19 my $V = '0.32-tizen0709';
21 use Getopt::Long qw(:config no_auto_abbrev);
44 Usage: $P [OPTION]... [FILE]...
49 --no-tree run without a kernel tree
50 --no-signoff do not check for 'Signed-off-by' line
51 --patch treat FILE as patchfile
52 --emacs emacs compile window format
53 --terse one line per report (default)
54 --detail detail report
55 -f, --file treat FILE as regular source file (default)
56 --subjective, --strict enable more subjective tests
57 --root=PATH PATH to the kernel tree root
58 --no-summary suppress the per-file summary (default)
59 --mailback only produce a report in case of warnings/errors
60 --summary-file include the filename in summary
61 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
62 'values', 'possible', 'type', and 'attr' (default
64 --test-only=WORD report only warnings/errors containing WORD
66 -h, --help, --version display this help and exit
68 When FILE is - read standard input.
75 'q|quiet+' => \$quiet,
77 'signoff!' => \$chk_signoff,
78 'patch!' => \$chk_patch,
81 'detail!' => \$detail,
83 'subjective!' => \$check,
86 'summary!' => \$summary,
87 'mailback!' => \$mailback,
88 'summary-file!' => \$summary_file,
91 'test-only=s' => \$tst_only,
101 print "$P: no input files\n";
106 my $dbg_possible = 0;
109 for my $key (keys %debug) {
111 eval "\${dbg_$key} = '$debug{$key}';";
115 my $rpt_cleaners = 0;
128 if (!top_of_kernel_tree($root)) {
129 die "$P: $root: --root does not point at a valid tree\n";
132 if (top_of_kernel_tree('.')) {
134 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
135 top_of_kernel_tree($1)) {
140 if (!defined $root) {
141 print "Must be run from the top-level dir. of a kernel tree\n";
146 my $emitted_corrupt = 0;
149 [A-Za-z_][A-Za-z\d_]*
150 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
152 our $Storage = qr{extern|static|asmlinkage};
164 # Notes to $Attribute:
165 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
184 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
185 ____cacheline_aligned|
186 ____cacheline_aligned_in_smp|
187 ____cacheline_internodealigned_in_smp|
191 our $Inline = qr{inline|__always_inline|noinline};
192 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
193 our $Lval = qr{$Ident(?:$Member)*};
195 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
196 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
197 our $Compare = qr{<=|>=|==|!=|<|>};
201 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
209 [\x09\x0A\x0D\x20-\x7E] # ASCII
210 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
211 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
212 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
213 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
214 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
215 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
216 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
219 our $typeTypedefs = qr{(?x:
220 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
224 our $logFunctions = qr{(?x:
226 pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)|
227 (dev|netdev|netif)_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)|
234 qr{(?:unsigned\s+)?char},
235 qr{(?:unsigned\s+)?short},
236 qr{(?:unsigned\s+)?int},
237 qr{(?:unsigned\s+)?long},
238 qr{(?:unsigned\s+)?long\s+int},
239 qr{(?:unsigned\s+)?long\s+long},
240 qr{(?:unsigned\s+)?long\s+long\s+int},
249 qr{${Ident}_handler},
250 qr{${Ident}_handler_fn},
252 our @modifierList = (
256 our $allowed_asm_includes = qr{(?x:
260 # memory.h: ARM has a custom one
263 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
264 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
265 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
267 (?:$Modifier\s+|const\s+)*
269 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
273 (?:\s+$Modifier|\s+const)*
277 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
278 (?:\s+$Inline|\s+$Modifier)*
280 $Declare = qr{(?:$Storage\s+)?$Type};
284 $chk_signoff = 0 if ($file);
286 my @dep_includes = ();
287 my @dep_functions = ();
288 my $removal = "Documentation/feature-removal-schedule.txt";
289 if ($tree && -f "$root/$removal") {
290 open(my $REMOVE, '<', "$root/$removal") ||
291 die "$P: $removal: open failed - $!\n";
293 if (/^Check:\s+(.*\S)/) {
294 for my $entry (split(/[, ]+/, $1)) {
295 if ($entry =~ m@include/(.*)@) {
296 push(@dep_includes, $1);
298 } elsif ($entry !~ m@/@) {
299 push(@dep_functions, $entry);
310 for my $filename (@ARGV) {
313 open($FILE, '-|', "diff -u /dev/null $filename") ||
314 die "$P: $filename: diff failed - $!\n";
315 } elsif ($filename eq '-') {
316 open($FILE, '<&STDIN');
318 open($FILE, '<', "$filename") ||
319 die "$P: $filename: open failed - $!\n";
321 if ($filename eq '-') {
322 $vname = 'Your patch';
331 if (!process($filename)) {
340 sub top_of_kernel_tree {
344 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
345 "README", "Documentation", "arch", "include", "drivers",
346 "fs", "init", "ipc", "kernel", "lib", "scripts",
349 foreach my $check (@tree_check) {
350 if (! -e $root . '/' . $check) {
362 for my $c (split(//, $str)) {
366 for (; ($n % 8) != 0; $n++) {
378 (my $res = shift) =~ tr/\t/ /c;
385 # Drop the diff line leader and expand tabs
387 $line = expand_tabs($line);
389 # Pick the indent from the front of the line.
390 my ($white) = ($line =~ /^(\s*)/);
392 return (length($line), length($white));
395 my $sanitise_quote = '';
397 sub sanitise_line_reset {
398 my ($in_comment) = @_;
401 $sanitise_quote = '*/';
403 $sanitise_quote = '';
416 # Always copy over the diff marker.
417 $res = substr($line, 0, 1);
419 for ($off = 1; $off < length($line); $off++) {
420 $c = substr($line, $off, 1);
422 # Comments we are wacking completly including the begin
423 # and end, all to $;.
424 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
425 $sanitise_quote = '*/';
427 substr($res, $off, 2, "$;$;");
431 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
432 $sanitise_quote = '';
433 substr($res, $off, 2, "$;$;");
437 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
438 $sanitise_quote = '//';
440 substr($res, $off, 2, $sanitise_quote);
445 # A \ in a string means ignore the next character.
446 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
448 substr($res, $off, 2, 'XX');
453 if ($c eq "'" || $c eq '"') {
454 if ($sanitise_quote eq '') {
455 $sanitise_quote = $c;
457 substr($res, $off, 1, $c);
459 } elsif ($sanitise_quote eq $c) {
460 $sanitise_quote = '';
464 #print "c<$c> SQ<$sanitise_quote>\n";
465 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
466 substr($res, $off, 1, $;);
467 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
468 substr($res, $off, 1, $;);
469 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
470 substr($res, $off, 1, 'X');
472 substr($res, $off, 1, $c);
476 if ($sanitise_quote eq '//') {
477 $sanitise_quote = '';
480 # The pathname on a #include may be surrounded by '<' and '>'.
481 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
482 my $clean = 'X' x length($1);
483 $res =~ s@\<.*\>@<$clean>@;
485 # The whole of a #error is a string.
486 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
487 my $clean = 'X' x length($1);
488 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
494 sub ctx_statement_block {
495 my ($linenr, $remain, $off) = @_;
496 my $line = $linenr - 1;
513 @stack = (['', 0]) if ($#stack == -1);
515 #warn "CSB: blk<$blk> remain<$remain>\n";
516 # If we are about to drop off the end, pull in more
519 for (; $remain > 0; $line++) {
520 last if (!defined $lines[$line]);
521 next if ($lines[$line] =~ /^-/);
524 $blk .= $lines[$line] . "\n";
529 # Bail if there is no further context.
530 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
536 $c = substr($blk, $off, 1);
537 $remainder = substr($blk, $off);
539 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
541 # Handle nested #if/#else.
542 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
543 push(@stack, [ $type, $level ]);
544 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
545 ($type, $level) = @{$stack[$#stack - 1]};
546 } elsif ($remainder =~ /^#\s*endif\b/) {
547 ($type, $level) = @{pop(@stack)};
550 # Statement ends at the ';' or a close '}' at the
552 if ($level == 0 && $c eq ';') {
556 # An else is really a conditional as long as its not else if
557 if ($level == 0 && $coff_set == 0 &&
558 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
559 $remainder =~ /^(else)(?:\s|{)/ &&
560 $remainder !~ /^else\s+if\b/) {
561 $coff = $off + length($1) - 1;
563 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
564 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
567 if (($type eq '' || $type eq '(') && $c eq '(') {
571 if ($type eq '(' && $c eq ')') {
573 $type = ($level != 0)? '(' : '';
575 if ($level == 0 && $coff < $soff) {
578 #warn "CSB: mark coff<$coff>\n";
581 if (($type eq '' || $type eq '{') && $c eq '{') {
585 if ($type eq '{' && $c eq '}') {
587 $type = ($level != 0)? '{' : '';
590 if (substr($blk, $off + 1, 1) eq ';') {
598 # We are truly at the end, so shuffle to the next line.
605 my $statement = substr($blk, $soff, $off - $soff + 1);
606 my $condition = substr($blk, $soff, $coff - $soff + 1);
608 #warn "STATEMENT<$statement>\n";
609 #warn "CONDITION<$condition>\n";
611 #print "coff<$coff> soff<$off> loff<$loff>\n";
613 return ($statement, $condition,
614 $line, $remain + 1, $off - $loff + 1, $level);
617 sub statement_lines {
620 # Strip the diff line prefixes and rip blank lines at start and end.
621 $stmt =~ s/(^|\n)./$1/g;
625 my @stmt_lines = ($stmt =~ /\n/g);
627 return $#stmt_lines + 2;
630 sub statement_rawlines {
633 my @stmt_lines = ($stmt =~ /\n/g);
635 return $#stmt_lines + 2;
638 sub statement_block_size {
641 $stmt =~ s/(^|\n)./$1/g;
647 my @stmt_lines = ($stmt =~ /\n/g);
648 my @stmt_statements = ($stmt =~ /;/g);
650 my $stmt_lines = $#stmt_lines + 2;
651 my $stmt_statements = $#stmt_statements + 1;
653 if ($stmt_lines > $stmt_statements) {
656 return $stmt_statements;
660 sub ctx_statement_full {
661 my ($linenr, $remain, $off) = @_;
662 my ($statement, $condition, $level);
666 # Grab the first conditional/block pair.
667 ($statement, $condition, $linenr, $remain, $off, $level) =
668 ctx_statement_block($linenr, $remain, $off);
669 #print "F: c<$condition> s<$statement> remain<$remain>\n";
670 push(@chunks, [ $condition, $statement ]);
671 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
672 return ($level, $linenr, @chunks);
675 # Pull in the following conditional/block pairs and see if they
676 # could continue the statement.
678 ($statement, $condition, $linenr, $remain, $off, $level) =
679 ctx_statement_block($linenr, $remain, $off);
680 #print "C: c<$condition> s<$statement> remain<$remain>\n";
681 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
683 push(@chunks, [ $condition, $statement ]);
686 return ($level, $linenr, @chunks);
690 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
692 my $start = $linenr - 1;
699 my @stack = ($level);
700 for ($line = $start; $remain > 0; $line++) {
701 next if ($rawlines[$line] =~ /^-/);
704 $blk .= $rawlines[$line];
706 # Handle nested #if/#else.
707 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
708 push(@stack, $level);
709 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
710 $level = $stack[$#stack - 1];
711 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
712 $level = pop(@stack);
715 foreach my $c (split(//, $lines[$line])) {
716 ##print "C<$c>L<$level><$open$close>O<$off>\n";
722 if ($c eq $close && $level > 0) {
724 last if ($level == 0);
725 } elsif ($c eq $open) {
730 if (!$outer || $level <= 1) {
731 push(@res, $rawlines[$line]);
734 last if ($level == 0);
737 return ($level, @res);
739 sub ctx_block_outer {
740 my ($linenr, $remain) = @_;
742 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
746 my ($linenr, $remain) = @_;
748 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
752 my ($linenr, $remain, $off) = @_;
754 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
757 sub ctx_block_level {
758 my ($linenr, $remain) = @_;
760 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
762 sub ctx_statement_level {
763 my ($linenr, $remain, $off) = @_;
765 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
768 sub ctx_locate_comment {
769 my ($first_line, $end_line) = @_;
771 # Catch a comment on the end of the line itself.
772 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
773 return $current_comment if (defined $current_comment);
775 # Look through the context and try and figure out if there is a
778 $current_comment = '';
779 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
780 my $line = $rawlines[$linenr - 1];
782 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
785 if ($line =~ m@/\*@) {
788 if (!$in_comment && $current_comment ne '') {
789 $current_comment = '';
791 $current_comment .= $line . "\n" if ($in_comment);
792 if ($line =~ m@\*/@) {
797 chomp($current_comment);
798 return($current_comment);
800 sub ctx_has_comment {
801 my ($first_line, $end_line) = @_;
802 my $cmt = ctx_locate_comment($first_line, $end_line);
804 ##print "LINE: $rawlines[$end_line - 1 ]\n";
805 ##print "CMMT: $cmt\n";
811 my ($linenr, $cnt) = @_;
813 my $offset = $linenr - 1;
818 $line = $rawlines[$offset++];
819 next if (defined($line) && $line =~ /^-/);
831 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
834 $coded = sprintf("^%c", unpack('C', $2) + 64);
843 my $av_preprocessor = 0;
849 $av_preprocessor = 0;
851 @av_paren_type = ('E');
852 $av_pend_colon = 'O';
855 sub annotate_values {
856 my ($stream, $type) = @_;
859 my $var = '_' x length($stream);
862 print "$stream\n" if ($dbg_values > 1);
864 while (length($cur)) {
865 @av_paren_type = ('E') if ($#av_paren_type < 0);
866 print " <" . join('', @av_paren_type) .
867 "> <$type> <$av_pending>" if ($dbg_values > 1);
868 if ($cur =~ /^(\s+)/o) {
869 print "WS($1)\n" if ($dbg_values > 1);
870 if ($1 =~ /\n/ && $av_preprocessor) {
871 $type = pop(@av_paren_type);
872 $av_preprocessor = 0;
875 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/) {
876 print "CAST($1)\n" if ($dbg_values > 1);
877 push(@av_paren_type, $type);
880 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
881 print "DECLARE($1)\n" if ($dbg_values > 1);
884 } elsif ($cur =~ /^($Modifier)\s*/) {
885 print "MODIFIER($1)\n" if ($dbg_values > 1);
888 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
889 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
890 $av_preprocessor = 1;
891 push(@av_paren_type, $type);
897 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
898 print "UNDEF($1)\n" if ($dbg_values > 1);
899 $av_preprocessor = 1;
900 push(@av_paren_type, $type);
902 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
903 print "PRE_START($1)\n" if ($dbg_values > 1);
904 $av_preprocessor = 1;
906 push(@av_paren_type, $type);
907 push(@av_paren_type, $type);
910 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
911 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
912 $av_preprocessor = 1;
914 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
918 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
919 print "PRE_END($1)\n" if ($dbg_values > 1);
921 $av_preprocessor = 1;
923 # Assume all arms of the conditional end as this
924 # one does, and continue as if the #endif was not here.
926 push(@av_paren_type, $type);
929 } elsif ($cur =~ /^(\\\n)/o) {
930 print "PRECONT($1)\n" if ($dbg_values > 1);
932 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
933 print "ATTR($1)\n" if ($dbg_values > 1);
937 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
938 print "SIZEOF($1)\n" if ($dbg_values > 1);
944 } elsif ($cur =~ /^(if|while|for)\b/o) {
945 print "COND($1)\n" if ($dbg_values > 1);
949 } elsif ($cur =~/^(case)/o) {
950 print "CASE($1)\n" if ($dbg_values > 1);
951 $av_pend_colon = 'C';
954 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
955 print "KEYWORD($1)\n" if ($dbg_values > 1);
958 } elsif ($cur =~ /^(\()/o) {
959 print "PAREN('$1')\n" if ($dbg_values > 1);
960 push(@av_paren_type, $av_pending);
964 } elsif ($cur =~ /^(\))/o) {
965 my $new_type = pop(@av_paren_type);
966 if ($new_type ne '_') {
968 print "PAREN('$1') -> $type\n"
969 if ($dbg_values > 1);
971 print "PAREN('$1')\n" if ($dbg_values > 1);
974 } elsif ($cur =~ /^($Ident)\s*\(/o) {
975 print "FUNC($1)\n" if ($dbg_values > 1);
979 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
980 if (defined $2 && $type eq 'C' || $type eq 'T') {
981 $av_pend_colon = 'B';
982 } elsif ($type eq 'E') {
983 $av_pend_colon = 'L';
985 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
988 } elsif ($cur =~ /^($Ident|$Constant)/o) {
989 print "IDENT($1)\n" if ($dbg_values > 1);
992 } elsif ($cur =~ /^($Assignment)/o) {
993 print "ASSIGN($1)\n" if ($dbg_values > 1);
996 } elsif ($cur =~/^(;|{|})/) {
997 print "END($1)\n" if ($dbg_values > 1);
999 $av_pend_colon = 'O';
1001 } elsif ($cur =~/^(,)/) {
1002 print "COMMA($1)\n" if ($dbg_values > 1);
1005 } elsif ($cur =~ /^(\?)/o) {
1006 print "QUESTION($1)\n" if ($dbg_values > 1);
1009 } elsif ($cur =~ /^(:)/o) {
1010 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1012 substr($var, length($res), 1, $av_pend_colon);
1013 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1018 $av_pend_colon = 'O';
1020 } elsif ($cur =~ /^(\[)/o) {
1021 print "CLOSE($1)\n" if ($dbg_values > 1);
1024 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1027 print "OPV($1)\n" if ($dbg_values > 1);
1034 substr($var, length($res), 1, $variant);
1037 } elsif ($cur =~ /^($Operators)/o) {
1038 print "OP($1)\n" if ($dbg_values > 1);
1039 if ($1 ne '++' && $1 ne '--') {
1043 } elsif ($cur =~ /(^.)/o) {
1044 print "C($1)\n" if ($dbg_values > 1);
1047 $cur = substr($cur, length($1));
1048 $res .= $type x length($1);
1052 return ($res, $var);
1056 my ($possible, $line) = @_;
1057 my $notPermitted = qr{(?:
1072 ^(?:typedef|struct|enum)\b
1074 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1075 if ($possible !~ $notPermitted) {
1076 # Check for modifiers.
1077 $possible =~ s/\s*$Storage\s*//g;
1078 $possible =~ s/\s*$Sparse\s*//g;
1079 if ($possible =~ /^\s*$/) {
1081 } elsif ($possible =~ /\s/) {
1082 $possible =~ s/\s*$Type\s*//g;
1083 for my $modifier (split(' ', $possible)) {
1084 if ($modifier !~ $notPermitted) {
1085 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1086 push(@modifierList, $modifier);
1091 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1092 push(@typeList, $possible);
1096 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1103 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1106 my $line = $prefix . $_[0];
1108 $line = (split('\n', $line))[0] . "\n" if ($terse);
1110 push(our @report, $line);
1118 if (report("ERROR: $_[0]\n")) {
1124 if (report("WARNING: $_[0]\n")) {
1130 if ($check && report("CHECK: $_[0]\n")) {
1136 sub check_absolute_file {
1137 my ($absolute, $herecurr) = @_;
1138 my $file = $absolute;
1140 ##print "absolute<$absolute>\n";
1142 # See if any suffix of this path is a path within the tree.
1143 while ($file =~ s@^[^/]*/@@) {
1144 if (-f "$root/$file") {
1145 ##print "file<$file>\n";
1153 # It is, so see if the prefix is acceptable.
1154 my $prefix = $absolute;
1155 substr($prefix, -length($file)) = '';
1157 ##print "prefix<$prefix>\n";
1158 # if ($prefix ne ".../") {
1159 # WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1164 my $filename = shift;
1170 my $stashrawline="";
1187 # Trace the real file/line as we go.
1193 my $comment_edge = 0;
1197 my $prev_values = 'E';
1200 my %suppress_ifbraces;
1201 my %suppress_whiletrailers;
1202 my %suppress_export;
1204 # Pre-scan the patch sanitizing the lines.
1205 # Pre-scan the patch looking for any __setup documentation.
1207 my @setup_docs = ();
1210 sanitise_line_reset();
1212 foreach my $rawline (@rawlines) {
1216 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1218 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1223 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1232 # Guestimate if this is a continuing comment. Run
1233 # the context looking for a comment "edge". If this
1234 # edge is a close comment then we must be in a comment
1238 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1239 next if (defined $rawlines[$ln - 1] &&
1240 $rawlines[$ln - 1] =~ /^-/);
1242 #print "RAW<$rawlines[$ln - 1]>\n";
1243 last if (!defined $rawlines[$ln - 1]);
1244 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1245 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1250 if (defined $edge && $edge eq '*/') {
1254 # Guestimate if this is a continuing comment. If this
1255 # is the start of a diff block and this line starts
1256 # ' *' then it is very likely a comment.
1257 if (!defined $edge &&
1258 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1263 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1264 sanitise_line_reset($in_comment);
1266 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1267 # Standardise the strings and chars within the input to
1268 # simplify matching -- only bother with positive lines.
1269 $line = sanitise_line($rawline);
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 != 0);
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 # WARN("[LNE_R_TWS]DOS line endings\n" . $herevet);
1435 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1436 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1437 WARN("[LNE_R_TWS]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(?:---)?$/) {
1447 # my $cnt = $realcnt;
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("[SMT_M02_L80] 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("[LNE_R_EOF] 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 ($rawline =~ /^\+\s* \t\s*\S/ ||
1513 $rawline =~ /^\+\s* \s*/) {
1514 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1515 WARN("[IDT_R_TAB] code indent should use tabs where possible\n" . $herevet);
1519 # check for space before tabs.
1520 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1521 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1522 WARN("[IDT_R_TAB] please, no space before tabs\n" . $herevet);
1525 # check for spaces at the beginning of a line.
1527 # 1) within comments
1528 # 2) indented preprocessor commands
1530 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
1531 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1532 WARN("[IDT_R_TAB] please, no spaces at the start of a line\n" . $herevet);
1535 # check we are in a valid C source file if not then ignore this hunk
1536 next if ($realfile !~ /\.(h|c)$/);
1538 # check for RCS/CVS revision markers
1539 # if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1540 # WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1543 # Blackfin: don't use __builtin_bfin_[cs]sync
1544 # if ($line =~ /__builtin_bfin_csync/) {
1545 # my $herevet = "$here\n" . cat_vet($line) . "\n";
1546 # ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1548 # if ($line =~ /__builtin_bfin_ssync/) {
1549 # my $herevet = "$here\n" . cat_vet($line) . "\n";
1550 # ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1553 # Check for potential 'bare' types
1554 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1556 if ($realcnt && $line =~ /.\s*\S/) {
1557 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1558 ctx_statement_block($linenr, $realcnt, 0);
1559 $stat =~ s/\n./\n /g;
1560 $cond =~ s/\n./\n /g;
1562 # Find the real next line.
1563 $realline_next = $line_nr_next;
1564 if (defined $realline_next &&
1565 (!defined $lines[$realline_next - 1] ||
1566 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1573 # Ignore goto labels.
1574 if ($s =~ /$Ident:\*$/s) {
1576 # Ignore functions being called
1577 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1579 } elsif ($s =~ /^.\s*else\b/s) {
1581 # declarations always start with types
1582 } 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) {
1585 possible($type, "A:" . $s);
1587 # definitions in global scope can only start with types
1588 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1589 possible($1, "B:" . $s);
1592 # any (foo ... *) is a pointer cast, and foo is a type
1593 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1594 possible($1, "C:" . $s);
1597 # Check for any sort of function declaration.
1598 # int foo(something bar, other baz);
1599 # void (*store_gdt)(x86_descr_ptr *);
1600 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1601 my ($name_len) = length($1);
1604 substr($ctx, 0, $name_len + 1, '');
1605 $ctx =~ s/\)[^\)]*$//;
1607 for my $arg (split(/\s*,\s*/, $ctx)) {
1608 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1610 possible($1, "D:" . $s);
1618 # Checks which may be anchored in the context.
1621 # Check for switch () and associated case and default
1622 # statements should be at the same indent.
1623 if ($line=~/\bswitch\s*\(.*\)/) {
1626 my @ctx = ctx_block_outer($linenr, $realcnt);
1628 for my $ctx (@ctx) {
1629 my ($clen, $cindent) = line_stats($ctx);
1630 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1631 $indent != $cindent) {
1632 $err .= "$sep$ctx\n";
1639 WARN("[IDT_R_SCH] switch and case should be at the same indent\n$hereline$err");
1643 # if/while/etc brace do not go on next line, unless defining a do while loop,
1644 # or if that brace on the next line is for something else
1645 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1646 my $pre_ctx = "$1$2";
1648 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1649 my $ctx_cnt = $realcnt - $#ctx - 1;
1650 my $ctx = join("\n", @ctx);
1652 my $ctx_ln = $linenr;
1653 my $ctx_skip = $realcnt;
1655 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1656 defined $lines[$ctx_ln - 1] &&
1657 $lines[$ctx_ln - 1] =~ /^-/)) {
1658 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1659 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1663 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1664 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1666 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1667 ERROR("[BRC_M_SMT] that open brace { should be on the previous line\n" .
1668 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1670 # if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1671 # $ctx =~ /\)\s*\;\s*$/ &&
1672 # defined $lines[$ctx_ln - 1])
1674 # my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1675 # if ($nindent > $indent) {
1676 # WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1677 # "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1682 # Check relative indent for conditionals and blocks.
1683 # if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1684 # my ($s, $c) = ($stat, $cond);
1686 # substr($s, 0, length($c), '');
1688 # # Make sure we remove the line prefixes as we have
1689 # # none on the first line, and are going to readd them
1690 # # where necessary.
1691 # $s =~ s/\n./\n/gs;
1693 # # Find out how long the conditional actually is.
1694 # my @newlines = ($c =~ /\n/gs);
1695 # my $cond_lines = 1 + $#newlines;
1697 # # We want to check the first line inside the block
1698 # # starting at the end of the conditional, so remove:
1699 # # 1) any blank line termination
1700 # # 2) any opening brace { on end of the line
1701 # # 3) any do (...) {
1702 # my $continuation = 0;
1704 # $s =~ s/^.*\bdo\b//;
1706 # if ($s =~ s/^\s*\\//) {
1707 # $continuation = 1;
1709 # if ($s =~ s/^\s*?\n//) {
1714 # # Also ignore a loop construct at the end of a
1715 # # preprocessor statement.
1716 # if (($prevline =~ /^.\s*#\s*define\s/ ||
1717 # $prevline =~ /\\\s*$/) && $continuation == 0) {
1721 # my $cond_ptr = -1;
1722 # $continuation = 0;
1723 # while ($cond_ptr != $cond_lines) {
1724 # $cond_ptr = $cond_lines;
1726 # # If we see an #else/#elif then the code
1728 # if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1733 # # 1) blank lines, they should be at 0,
1734 # # 2) preprocessor lines, and
1736 # if ($continuation ||
1737 # $s =~ /^\s*?\n/ ||
1738 # $s =~ /^\s*#\s*?/ ||
1739 # $s =~ /^\s*$Ident\s*:/) {
1740 # $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1741 # if ($s =~ s/^.*?\n//) {
1747 # my (undef, $sindent) = line_stats("+" . $s);
1748 # my $stat_real = raw_line($linenr, $cond_lines);
1750 # # Check if either of these lines are modified, else
1751 # # this is not this patch's fault.
1752 # if (!defined($stat_real) ||
1753 # $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1756 # if (defined($stat_real) && $cond_lines > 1) {
1757 # $stat_real = "[...]\n$stat_real";
1760 # #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";
1762 # if ($check && (($sindent % 8) != 0 ||
1763 # ($sindent <= $indent && $s ne ''))) {
1764 # WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1768 # Track the 'values' across context and added lines.
1769 my $opline = $line; $opline =~ s/^./ /;
1770 my ($curr_values, $curr_vars) =
1771 annotate_values($opline . "\n", $prev_values);
1772 $curr_values = $prev_values . $curr_values;
1774 my $outline = $opline; $outline =~ s/\t/ /g;
1775 print "$linenr > .$outline\n";
1776 print "$linenr > $curr_values\n";
1777 print "$linenr > $curr_vars\n";
1779 $prev_values = substr($curr_values, -1);
1781 #ignore lines not being added
1782 if ($line=~/^[^\+]/) {next;}
1784 # TEST: allow direct testing of the type matcher.
1786 # if ($line =~ /^.\s*$Declare\s*$/) {
1787 # ERROR("TEST: is type\n" . $herecurr);
1788 # } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1789 # ERROR("TEST: is not type ($1 is)\n". $herecurr);
1793 # TEST: allow direct testing of the attribute matcher.
1795 # if ($line =~ /^.\s*$Modifier\s*$/) {
1796 # ERROR("TEST: is attr\n" . $herecurr);
1797 # } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1798 # ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1803 # check for initialisation to aggregates open brace on the next line
1804 if ($line =~ /^.\s*{/ &&
1805 $prevline =~ /(?:^|[^=])=\s*$/) {
1806 ERROR("[BRC_M_SMT] that open brace { should be on the previous line\n" . $hereprev);
1810 # Checks which are anchored on the added line.
1813 # check for malformed paths in #include statements (uses RAW line)
1814 # if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1816 # if ($path =~ m{//}) {
1817 # ERROR("malformed #include filename\n" .
1822 # no C99 // comments
1823 if ($line =~ m{//}) {
1824 WARN("[CMT_M_C89] do not use C99 // comments\n" . $herecurr);
1826 # Remove C99 comments.
1828 $opline =~ s@//.*@@;
1830 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1831 # the whole statement.
1832 #print "APW <$lines[$realline_next - 1]>\n";
1833 if (defined $realline_next &&
1834 exists $lines[$realline_next - 1] &&
1835 !defined $suppress_export{$realline_next} &&
1836 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1837 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1838 # Handle definitions which produce identifiers with
1841 # EXPORT_SYMBOL(something_foo);
1843 if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
1844 $name =~ /^${Ident}_$2/) {
1845 #print "FOO C name<$name>\n";
1846 $suppress_export{$realline_next} = 1;
1848 } elsif ($stat !~ /(?:
1850 ^.DEFINE_$Ident\(\Q$name\E\)|
1851 ^.DECLARE_$Ident\(\Q$name\E\)|
1852 ^.LIST_HEAD\(\Q$name\E\)|
1853 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1854 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
1856 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1857 $suppress_export{$realline_next} = 2;
1859 $suppress_export{$realline_next} = 1;
1862 if (!defined $suppress_export{$linenr} &&
1863 $prevline =~ /^.\s*$/ &&
1864 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1865 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1866 #print "FOO B <$lines[$linenr - 1]>\n";
1867 $suppress_export{$linenr} = 2;
1869 # if (defined $suppress_export{$linenr} &&
1870 # $suppress_export{$linenr} == 2) {
1871 # WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1874 # check for global initialisers.
1875 # if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1876 # ERROR("do not initialise globals to 0 or NULL\n" .
1879 # check for static initialisers.
1880 # if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1881 # ERROR("do not initialise statics to 0 or NULL\n" .
1885 # check for static const char * arrays.
1886 # if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
1887 # WARN("static const char * array should probably be static const char * const\n" .
1891 # check for static char foo[] = "bar" declarations.
1892 # if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
1893 # WARN("static char array declaration should probably be static const char\n" .
1897 # check for declarations of struct pci_device_id
1898 # if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
1899 # WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
1902 # check for new typedefs, only function parameters and sparse annotations
1904 # if ($line =~ /\btypedef\s/ &&
1905 # $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1906 # $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
1907 # $line !~ /\b$typeTypedefs\b/ &&
1908 # $line !~ /\b__bitwise(?:__|)\b/) {
1909 # WARN("do not add new typedefs\n" . $herecurr);
1912 # * goes on variable not on type
1914 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1915 my ($from, $to) = ($1, $1);
1917 # Should start with a space.
1918 $to =~ s/^(\S)/ $1/;
1919 # Should not end with a space.
1921 # '*'s should not have spaces between.
1922 while ($to =~ s/\*\s+\*/\*\*/) {
1925 #print "from<$from> to<$to>\n";
1927 # ERROR("[SPC_M_OPR] \"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1929 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1930 my ($from, $to, $ident) = ($1, $1, $2);
1932 # Should start with a space.
1933 $to =~ s/^(\S)/ $1/;
1934 # Should not end with a space.
1936 # '*'s should not have spaces between.
1937 while ($to =~ s/\*\s+\*/\*\*/) {
1939 # Modifiers should have spaces.
1940 $to =~ s/(\b$Modifier$)/$1 /;
1942 #print "from<$from> to<$to> ident<$ident>\n";
1943 if ($from ne $to && $ident !~ /^$Modifier$/) {
1944 # ERROR("[SPC_M_OPR] \"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1948 # # no BUG() or BUG_ON()
1949 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1950 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1951 # print "$herecurr";
1955 # if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1956 # WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1959 # printk should use KERN_* levels. Note that follow on printk's on the
1960 # same line do not need a level, so we use the current block context
1961 # to try and find and validate the current printk. In summary the current
1962 # printk includes all preceeding printk's which have no newline on the end.
1963 # we assume the first bad printk is the one to report.
1964 # if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1966 # for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1967 # #print "CHECK<$lines[$ln - 1]\n";
1968 # # we have a preceeding printk if it ends
1969 # # with "\n" ignore it, else it is to blame
1970 # if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1971 # if ($rawlines[$ln - 1] !~ m{\\n"}) {
1978 # WARN("printk() should include KERN_ facility level\n" . $herecurr);
1982 # function brace can't be on same line, except for #defines of do while,
1983 # or if closed on same line
1984 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1985 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1986 ERROR("[BRC_M_FTN] open brace '{' following function declarations go on the next line\n" . $herecurr);
1989 # open braces for enum, union and struct go on the same line.
1990 if ($line =~ /^.\s*{/ &&
1991 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1992 ERROR("[BRC_M_EUS] open brace '{' following $1 go on the same line\n" . $hereprev);
1995 # missing space after union, struct or enum definition
1996 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1997 ERROR("[SPC_M_KWD] missing space after $1 definition\n" . $herecurr);
2000 # check for spacing round square brackets; allowed:
2001 # 1. with a type on the left -- int [] a;
2002 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2003 # 3. inside a curly brace -- = { [0...10] = 5 }
2004 while ($line =~ /(.*?\s)\[/g) {
2005 my ($where, $prefix) = ($-[1], $1);
2006 if ($prefix !~ /$Type\s+$/ &&
2007 ($where != 0 || $prefix !~ /^.\s+$/) &&
2008 $prefix !~ /{\s+$/) {
2009 ERROR("[SPC_M_SEP] space prohibited before open square bracket '['\n" . $herecurr);
2013 # check for spaces between functions and their parentheses.
2014 while ($line =~ /($Ident)\s+\(/g) {
2016 my $ctx_before = substr($line, 0, $-[1]);
2017 my $ctx = "$ctx_before$name";
2019 # Ignore those directives where spaces _are_ permitted.
2021 if|for|while|switch|return|case|
2022 volatile|__volatile__|
2023 __attribute__|format|__extension__|
2027 # cpp #define statements have non-optional spaces, ie
2028 # if there is a space between the name and the open
2029 # parenthesis it is simply not a parameter group.
2030 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2032 # cpp #elif statement condition may start with a (
2033 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2035 # If this whole things ends with a type its most
2036 # likely a typedef for a function.
2037 } elsif ($ctx =~ /$Type$/) {
2040 ERROR("[SPC_M_SEP] space prohibited between function name and open parenthesis '('\n" . $herecurr);
2043 # Check operator spacing.
2044 if (!($line=~/\#\s*include/)) {
2046 <<=|>>=|<=|>=|==|!=|
2047 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2048 =>|->|<<|>>|<|>|=|!|~|
2049 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2052 my @elements = split(/($ops|;)/, $opline);
2055 my $blank = copy_spacing($opline);
2057 for (my $n = 0; $n < $#elements; $n += 2) {
2058 $off += length($elements[$n]);
2060 # Pick up the preceeding and succeeding characters.
2061 my $ca = substr($opline, 0, $off);
2063 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2064 $cc = substr($opline, $off + length($elements[$n + 1]));
2066 my $cb = "$ca$;$cc";
2069 $a = 'V' if ($elements[$n] ne '');
2070 $a = 'W' if ($elements[$n] =~ /\s$/);
2071 $a = 'C' if ($elements[$n] =~ /$;$/);
2072 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2073 $a = 'O' if ($elements[$n] eq '');
2074 $a = 'E' if ($ca =~ /^\s*$/);
2076 my $op = $elements[$n + 1];
2079 if (defined $elements[$n + 2]) {
2080 $c = 'V' if ($elements[$n + 2] ne '');
2081 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2082 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2083 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2084 $c = 'O' if ($elements[$n + 2] eq '');
2085 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2090 my $ctx = "${a}x${c}";
2092 my $at = "(ctx:$ctx)";
2094 my $ptr = substr($blank, 0, $off) . "^";
2095 my $hereptr = "$hereline$ptr\n";
2097 # Pull out the value of this operator.
2098 my $op_type = substr($curr_values, $off + 1, 1);
2100 # Get the full operator variant.
2101 my $opv = $op . substr($curr_vars, $off, 1);
2103 # Ignore operators passed as parameters.
2104 if ($op_type ne 'V' &&
2105 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2108 # } elsif ($op =~ /^$;+$/) {
2110 # ; should have either the end of line or a space or \ after it
2111 } elsif ($op eq ';') {
2112 if ($ctx !~ /.x[WEBC]/ &&
2113 $cc !~ /^\\/ && $cc !~ /^;/) {
2114 ERROR("[SPC_M_OPR] space required after that '$op' $at\n" . $hereptr);
2118 } elsif ($op eq '//') {
2122 # : when part of a bitfield
2123 } elsif ($op eq '->' || $opv eq ':B') {
2124 if ($ctx =~ /Wx.|.xW/) {
2125 ERROR("[SPC_M_OPR] spaces prohibited around that '$op' $at\n" . $hereptr);
2128 # , must have a space on the right.
2129 } elsif ($op eq ',') {
2130 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2131 ERROR("[SPC_M_OPR] space required after that '$op' $at\n" . $hereptr);
2134 # '*' as part of a type definition -- reported already.
2135 } elsif ($opv eq '*_') {
2136 #warn "'*' is part of type\n";
2138 # unary operators should have a space before and
2139 # none after. May be left adjacent to another
2140 # unary operator, or a cast
2141 } elsif ($op eq '!' || $op eq '~' ||
2142 $opv eq '*U' || $opv eq '-U' ||
2143 $opv eq '&U' || $opv eq '&&U') {
2144 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2145 ERROR("[SPC_M_OPR] space required before that '$op' $at\n" . $hereptr);
2147 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2148 # A unary '*' may be const
2150 } elsif ($ctx =~ /.xW/) {
2152 if ($ctx =~ /WxW/) {
2153 # WARN("[SPC_M_OPR] space prohibited after that '$op' $at\n" . $hereptr);
2156 ERROR("[SPC_M_OPR] space prohibited after that '$op' $at\n" . $hereptr);}
2159 # unary ++ and unary -- are allowed no space on one side.
2160 } elsif ($op eq '++' or $op eq '--') {
2161 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2162 ERROR("[SPC_M_OPR] space required one side of that '$op' $at\n" . $hereptr);
2164 if ($ctx =~ /Wx[BE]/ ||
2165 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2166 ERROR("[SPC_M_OPR] space prohibited before that '$op' $at\n" . $hereptr);
2168 if ($ctx =~ /ExW/) {
2169 ERROR("[SPC_M_OPR] space prohibited after that '$op' $at\n" . $hereptr);
2173 # << and >> may either have or not have spaces both sides
2174 } elsif ($op eq '<<' or $op eq '>>' or
2175 $op eq '&' or $op eq '^' or $op eq '|' or
2176 $op eq '+' or $op eq '-' or
2177 $op eq '*' or $op eq '/' or
2180 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2182 if ($ctx =~ /WxV/) {
2183 # WARN("[SPC_M_OPR] need consistent spacing around '$op' $at\n" .$hereptr);
2185 elsif($ctx =~ /WxO/){
2186 # WARN("[SPC_M_OPR] need consistent spacing around '$op' $at\n" .$hereptr);
2188 elsif($ctx =~ /WxB/){
2189 # WARN("[SPC_M_OPR] need consistent spacing around '$op' $at\n" .$hereptr);
2192 ERROR("[SPC_M_OPR] need consistent spacing around '$op' $at\n" . $hereptr);
2196 # A colon needs no spaces before when it is
2197 # terminating a case value or a label.
2198 } elsif ($opv eq ':C' || $opv eq ':L') {
2199 if ($ctx =~ /Wx./) {
2200 ERROR("[SPC_M_OPR] space prohibited before that '$op' $at\n" . $hereptr);
2203 # All the others need spaces both sides.
2204 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2207 # Ignore email addresses <foo@bar>
2209 $cc =~ /^\S+\@\S+>/) ||
2211 $ca =~ /<\S+\@\S+$/))
2217 if (($opv eq ':O' && $ca =~ /\?$/) ||
2218 ($op eq '?' && $cc =~ /^:/)) {
2223 ERROR("[SPC_M_OPR] spaces required around that '$op' $at\n" . $hereptr);
2226 $off += length($elements[$n + 1]);
2230 # check for multiple assignments
2231 # if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2232 # CHK("multiple assignments should be avoided\n" . $herecurr);
2235 ## # check for multiple declarations, allowing for a function declaration
2237 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2238 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2240 ## # Remove any bracketed sections to ensure we do not
2241 ## # falsly report the parameters of functions.
2243 ## while ($ln =~ s/\([^\(\)]*\)//g) {
2245 ## if ($ln =~ /,/) {
2246 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2250 #need space before brace following if, while, etc
2251 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2253 ERROR("[SPC_M_KWD] space required before the open brace '{'\n" . $herecurr);
2256 # closing brace should have a space following it when it has anything
2258 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2259 ERROR("[SPC_M_SEP] space required after that close brace '}'\n" . $herecurr);
2262 # check spacing on square brackets
2263 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2264 ERROR("[SPC_M_SEP] space prohibited after that open square bracket '['\n" . $herecurr);
2266 if ($line =~ /\s\]/) {
2267 ERROR("[SPC_M_SEP] space prohibited before that close square bracket ']'\n" . $herecurr);
2270 # check spacing on parentheses
2271 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2272 $line !~ /for\s*\(\s+;/) {
2273 ERROR("[SPC_M_SEP] space prohibited after that open parenthesis '('\n" . $herecurr);
2275 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2276 $line !~ /for\s*\(.*;\s+\)/ &&
2277 $line !~ /:\s+\)/) {
2278 ERROR("[SPC_M_SEP] space prohibited before that close parenthesis ')'\n" . $herecurr);
2281 #goto labels aren't indented, allow a single space however
2282 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2283 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2284 WARN("[IDT_R_LBL] labels should not be indented\n" . $herecurr);
2287 # Return is not a function.
2288 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2292 # Flatten any parentheses
2293 $value =~ s/\(/ \(/g;
2294 $value =~ s/\)/\) /g;
2295 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2296 $value !~ /(?:$Ident|-?$Constant)\s*
2298 (?:$Ident|-?$Constant)/x &&
2299 $value =~ s/\([^\(\)]*\)/1/) {
2301 #print "value<$value>\n";
2302 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2303 # ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2305 } elsif ($spacing !~ /\s+/) {
2306 ERROR("[SPC_M_KWD] space required before the open parenthesis '('\n" . $herecurr);
2309 # Return of what appears to be an errno should normally be -'ve
2310 # if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2312 # if ($name ne 'EOF' && $name ne 'ERROR') {
2313 # WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2317 # Need a space before open parenthesis after if, while etc
2318 if ($line=~/\b(if|while|for|switch)\(/) {
2319 ERROR("[SPC_M_KWD] space required before the open parenthesis '('\n" . $herecurr);
2322 # Check for illegal assignment in if conditional -- and check for trailing
2323 # statements after the conditional.
2324 if ($line =~ /do\s*(?!{)/) {
2325 my ($stat_next) = ctx_statement_block($line_nr_next,
2326 $remain_next, $off_next);
2327 $stat_next =~ s/\n./\n /g;
2328 ##print "stat<$stat> stat_next<$stat_next>\n";
2330 if ($stat_next =~ /^\s*while\b/) {
2331 # If the statement carries leading newlines,
2332 # then count those as offsets.
2334 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2336 statement_rawlines($whitespace) - 1;
2338 $suppress_whiletrailers{$line_nr_next +
2342 if (!defined $suppress_whiletrailers{$linenr} &&
2343 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2344 my ($s, $c) = ($stat, $cond);
2346 # if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2347 # ERROR("do not use assignment in if condition\n" . $herecurr);
2350 # Find out what is on the end of the line after the
2352 # substr($s, 0, length($c), '');
2354 # $s =~ s/$;//g; # Remove any comments
2355 # if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2356 # $c !~ /}\s*while\s*/)
2358 # # Find out how long the conditional actually is.
2359 # my @newlines = ($c =~ /\n/gs);
2360 # my $cond_lines = 1 + $#newlines;
2361 # my $stat_real = '';
2363 # $stat_real = raw_line($linenr, $cond_lines)
2364 # . "\n" if ($cond_lines);
2365 # if (defined($stat_real) && $cond_lines > 1) {
2366 # $stat_real = "[...]\n$stat_real";
2369 # ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2373 # Check for bitwise tests written as boolean
2376 # (?:\[|\(|\&\&|\|\|)
2382 # (?:\&\&|\|\||\)|\])
2385 # WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2388 # if and else should not have general statements after it
2389 # if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2391 # $s =~ s/$;//g; # Remove any comments
2392 # if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2393 # ERROR("trailing statements should be on next line\n" . $herecurr);
2396 # if should not continue a brace
2397 # if ($line =~ /}\s*if\b/) {
2398 # ERROR("trailing statements should be on next line\n" .
2401 # case and default should not have general statements after them
2402 # if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2404 # (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2408 # ERROR("trailing statements should be on next line\n" . $herecurr);
2411 # Check for }<nl>else {, these must be at the same
2412 # indent level to be relevant to each other.
2413 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2414 $previndent == $indent) {
2415 ERROR("[BRC_M_SMT] else should follow close brace '}'\n" . $hereprev);
2418 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2419 $previndent == $indent) {
2420 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2422 # Find out what is on the end of the line after the
2424 substr($s, 0, length($c), '');
2427 if ($s =~ /^\s*;/) {
2428 ERROR("[BRC_M_SMT] while should follow close brace '}'\n" . $hereprev);
2432 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2433 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2434 # print "No studly caps, use _\n";
2435 # print "$herecurr";
2439 #no spaces allowed after \ in define
2440 # if ($line=~/\#\s*define.*\\\s$/) {
2441 # WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
2444 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2445 # if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2446 # my $file = "$1.h";
2447 # my $checkfile = "include/linux/$file";
2448 # if (-f "$root/$checkfile" &&
2449 # $realfile ne $checkfile &&
2450 # $1 !~ /$allowed_asm_includes/)
2452 # if ($realfile =~ m{^arch/}) {
2453 # CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2455 # WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2460 # multi-statement macros should be enclosed in a do while loop, grab the
2461 # first statement and ensure its the whole macro if its not enclosed
2462 # in a known good container
2463 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2464 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2467 my ($off, $dstat, $dcond, $rest);
2470 my $args = defined($1);
2472 # Find the end of the macro and limit our statement
2474 while ($cnt > 0 && defined $lines[$ln - 1] &&
2475 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2477 $ctx .= $rawlines[$ln - 1] . "\n";
2478 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2481 $ctx .= $rawlines[$ln - 1];
2483 ($dstat, $dcond, $ln, $cnt, $off) =
2484 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2485 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2486 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2488 # Extract the remainder of the define (if any) and
2489 # rip off surrounding spaces, and trailing \'s.
2491 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2492 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2493 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2494 $rest .= substr($lines[$ln - 1], $off) . "\n";
2500 $rest =~ s/\\\n.//g;
2504 # Clean up the original statement.
2506 substr($dstat, 0, length($dcond), '');
2508 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2511 $dstat =~ s/\\\n.//g;
2512 $dstat =~ s/^\s*//s;
2513 $dstat =~ s/\s*$//s;
2515 # Flatten any parentheses and braces
2516 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2517 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2518 $dstat =~ s/\[[^\{\}]*\]/1/)
2522 my $exceptions = qr{
2534 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2535 if ($rest ne '' && $rest ne ',') {
2536 if ($rest !~ /while\s*\(/ &&
2537 $dstat !~ /$exceptions/)
2539 # ERROR("[DEC_MDW_R] Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2542 } elsif ($ctx !~ /;/) {
2544 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2545 $dstat !~ /$exceptions/ &&
2546 $dstat !~ /^\.$Ident\s*=/ &&
2547 $dstat =~ /$Operators/)
2549 # ERROR("[DEC_MPA_R] Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2554 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2555 # all assignments may have only one of the following with an assignment:
2558 # VMLINUX_SYMBOL(...)
2559 # if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2560 # WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2563 # check for redundant bracing round if etc
2564 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2565 my ($level, $endln, @chunks) =
2566 ctx_statement_full($linenr, $realcnt, 1);
2567 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2568 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2569 if ($#chunks > 0 && $level == 0) {
2572 my $herectx = $here . "\n";
2573 my $ln = $linenr - 1;
2574 for my $chunk (@chunks) {
2575 my ($cond, $block) = @{$chunk};
2577 # If the condition carries leading newlines, then count those as offsets.
2578 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2579 my $offset = statement_rawlines($whitespace) - 1;
2581 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2583 # We have looked at and allowed this specific line.
2584 $suppress_ifbraces{$ln + $offset} = 1;
2586 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2587 $ln += statement_rawlines($block) - 1;
2589 substr($block, 0, length($cond), '');
2591 $seen++ if ($block =~ /^\s*{/);
2593 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2594 if (statement_lines($cond) > 1) {
2595 #print "APW: ALLOWED: cond<$cond>\n";
2598 if ($block =~/\b(?:if|for|while)\b/) {
2599 #print "APW: ALLOWED: block<$block>\n";
2602 if (statement_block_size($block) > 1) {
2603 #print "APW: ALLOWED: lines block<$block>\n";
2607 if ($seen && !$allowed) {
2608 WARN("[BRC_R_SST] braces {} are not necessary for any arm of this statement\n" . $herectx);
2612 if (!defined $suppress_ifbraces{$linenr - 1} &&
2613 $line =~ /\b(if|while|for|else)\b/) {
2616 # Check the pre-context.
2617 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2618 #print "APW: ALLOWED: pre<$1>\n";
2622 my ($level, $endln, @chunks) =
2623 ctx_statement_full($linenr, $realcnt, $-[0]);
2625 # Check the condition.
2626 my ($cond, $block) = @{$chunks[0]};
2627 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2628 if (defined $cond) {
2629 substr($block, 0, length($cond), '');
2631 if (statement_lines($cond) > 1) {
2632 #print "APW: ALLOWED: cond<$cond>\n";
2635 if ($block =~/\b(?:if|for|while)\b/) {
2636 #print "APW: ALLOWED: block<$block>\n";
2639 if (statement_block_size($block) > 1) {
2640 #print "APW: ALLOWED: lines block<$block>\n";
2643 # Check the post-context.
2644 if (defined $chunks[1]) {
2645 my ($cond, $block) = @{$chunks[1]};
2646 if (defined $cond) {
2647 substr($block, 0, length($cond), '');
2649 if ($block =~ /^\s*\{/) {
2650 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2654 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2655 my $herectx = $here . "\n";;
2656 my $cnt = statement_rawlines($block);
2658 for (my $n = 0; $n < $cnt; $n++) {
2659 $herectx .= raw_line($linenr, $n) . "\n";;
2662 WARN("[BRC_R_SST] braces {} are not necessary for single statement blocks\n" . $herectx);
2666 # don't include deprecated include files (uses RAW line)
2667 # for my $inc (@dep_includes) {
2668 # if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2669 # ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2673 # don't use deprecated functions
2674 # for my $func (@dep_functions) {
2675 # if ($line =~ /\b$func\b/) {
2676 # ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2680 # no volatiles please
2681 # my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2682 # if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2683 # WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2686 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2687 # if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2688 # ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2692 # if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2693 # CHK("if this code is redundant consider removing it\n" .
2697 # check for needless kfree() checks
2698 # if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2700 # if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2701 # WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2704 # check for needless usb_free_urb() checks
2705 # if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2707 # if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2708 # WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2712 # prefer usleep_range over udelay
2713 # if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2714 # # ignore udelay's < 10, however
2715 # if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2716 # CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2720 # warn about unexpectedly long msleep's
2721 # if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2723 # WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2727 # warn about #ifdefs in C files
2728 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2729 # print "#ifdef in C files should be avoided\n";
2730 # print "$herecurr";
2734 # warn about spacing in #ifdefs
2735 # if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2736 # ERROR("exactly one space required after that #$1\n" . $herecurr);
2739 # check for spinlock_t definitions without a comment.
2740 # if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2741 # $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
2743 # if (!ctx_has_comment($first_line, $linenr)) {
2744 # CHK("$1 definition without comment\n" . $herecurr);
2747 # check for memory barriers without a comment.
2748 # if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2749 # if (!ctx_has_comment($first_line, $linenr)) {
2750 # CHK("memory barrier without comment\n" . $herecurr);
2753 # check of hardware specific defines
2754 # if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2755 # CHK("architecture specific defines should be avoided\n" . $herecurr);
2758 # Check that the storage class is at the beginning of a declaration
2759 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2760 WARN("[DEC_R_INL] storage class should be at the beginning of the declaration\n" . $herecurr)
2763 # check the location of the inline attribute, that it is between
2764 # storage class and type.
2765 if ($line =~ /\b$Type\s+$Inline\b/ ||
2766 $line =~ /\b$Inline\s+$Storage\b/) {
2767 WARN("[DEC_R_INL] inline keyword should sit between storage class and type\n" . $herecurr);
2770 # Check for __inline__ and __inline, prefer inline
2771 # if ($line =~ /\b(__inline__|__inline)\b/) {
2772 # WARN("plain inline is preferred over $1\n" . $herecurr);
2775 # check for sizeof(&)
2776 # if ($line =~ /\bsizeof\s*\(\s*\&/) {
2777 # WARN("sizeof(& should be avoided\n" . $herecurr);
2780 # check for new externs in .c files.
2781 # if ($realfile =~ /\.c$/ && defined $stat &&
2782 # $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2784 # my $function_name = $1;
2785 # my $paren_space = $2;
2788 # if (defined $cond) {
2789 # substr($s, 0, length($cond), '');
2791 # if ($s =~ /^\s*;/ &&
2792 # $function_name ne 'uninitialized_var')
2794 # WARN("externs should be avoided in .c files\n" . $herecurr);
2797 # if ($paren_space =~ /\n/) {
2798 # WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2801 # } elsif ($realfile =~ /\.c$/ && defined $stat &&
2802 # $stat =~ /^.\s*extern\s+/)
2804 # WARN("externs should be avoided in .c files\n" . $herecurr);
2807 # checks for new __setup's
2808 # if ($rawline =~ /\b__setup\("([^"]*)"/) {
2811 # if (!grep(/$name/, @setup_docs)) {
2812 # CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2816 # check for pointless casting of kmalloc return
2817 # if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2818 # WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2821 # check for gcc specific __FUNCTION__
2822 # if ($line =~ /__FUNCTION__/) {
2823 # WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2826 # check for semaphores initialized locked
2827 # if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
2828 # WARN("consider using a completion\n" . $herecurr);
2831 # recommend strict_strto* over simple_strto*
2832 # if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2833 # WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2835 # check for __initcall(), use device_initcall() explicitly please
2836 # if ($line =~ /^.\s*__initcall\s*\(/) {
2837 # WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2839 # check for various ops structs, ensure they are const.
2840 # my $struct_ops = qr{acpi_dock_ops|
2841 # address_space_operations|
2843 # block_device_operations|
2844 # dentry_operations|
2848 # file_lock_operations|
2852 # intel_dvo_dev_ops|
2858 # lock_manager_operations|
2864 # pipe_buf_operations|
2865 # platform_hibernation_ops|
2866 # platform_suspend_ops|
2870 # snd_ac97_build_ops|
2871 # soc_pcmcia_socket_ops|
2875 # usb_mon_operations|
2877 # if ($line !~ /\bconst\b/ &&
2878 # $line =~ /\bstruct\s+($struct_ops)\b/) {
2879 # WARN("struct $1 should normally be const\n" .
2883 # use of NR_CPUS is usually wrong
2884 # ignore definitions of NR_CPUS and usage to define arrays as likely right
2885 # if ($line =~ /\bNR_CPUS\b/ &&
2886 # $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2887 # $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2888 # $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2889 # $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2890 # $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2892 # WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2895 # check for %L{u,d,i} in strings
2897 # while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2898 # $string = substr($rawline, $-[1], $+[1] - $-[1]);
2899 # $string =~ s/%%/__/g;
2900 # if ($string =~ /(?<!%)%L[udi]/) {
2901 # WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2906 # whine mightly about in_atomic
2907 # if ($line =~ /\bin_atomic\s*\(/) {
2908 # if ($realfile =~ m@^drivers/@) {
2909 # ERROR("do not use in_atomic in drivers\n" . $herecurr);
2910 # } elsif ($realfile !~ m@^kernel/@) {
2911 # WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2915 # check for lockdep_set_novalidate_class
2916 # if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
2917 # $line =~ /__lockdep_no_validate__\s*\)/ ) {
2918 # if ($realfile !~ m@^kernel/lockdep@ &&
2919 # $realfile !~ m@^include/linux/lockdep@ &&
2920 # $realfile !~ m@^drivers/base/core@) {
2921 # ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
2926 # If we have no input at all, then there is nothing to report on
2927 # so just keep quiet.
2928 if ($#rawlines == -1) {
2932 # In mailback mode only produce a report in the negative, for
2933 # things that appear to be patches.
2934 if ($mailback && ($clean == 1 || !$is_patch)) {
2938 # This is not a patch, and we are are in 'no-patch' mode so
2940 if (!$chk_patch && !$is_patch) {
2945 # ERROR("Does not appear to be a unified-diff format patch\n");
2947 # if ($is_patch && $chk_signoff && $signoff == 0) {
2948 # ERROR("Missing Signed-off-by: line(s)\n");
2951 print report_dump();
2952 if ($summary && !($clean == 1 && $quiet == 1)) {
2953 print "$filename " if ($summary_file);
2954 print "total: $cnt_error errors, $cnt_warn warnings, " .
2955 (($check)? "$cnt_chk checks, " : "") .
2956 "$cnt_lines lines checked\n";
2957 print "\n" if ($quiet == 0);
2960 print "$filename " if ($summary_file);
2961 print "total: $cnt_error errors, $cnt_warn warnings, " .
2962 (($check)? "$cnt_chk checks, " : "") .
2963 "$cnt_lines lines checked\n";
2964 print "\n" if ($quiet == 0);
2968 # If there were whitespace errors which cleanpatch can fix
2969 # then suggest that.
2970 if ($rpt_cleaners) {
2971 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2972 print " scripts/cleanfile\n\n";
2976 if ($clean == 1 && $quiet == 0) {
2977 print "$vname has no obvious style problems and is ready for submission.\n"
2979 if ($clean == 0 && $quiet == 0) {
2980 print "$vname has style problems, please review. If any of these errors\n";
2981 print "are false positives report them to the maintainer, see\n";
2982 print "CHECKPATCH in MAINTAINERS.\n";