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 # (c) 2013 Vasilis Tsiligiannis <acinonyx@openwrt.gr> (adapt for OpenWrt tree)
7 # Licensed under the terms of the GNU GPL License version 2
15 my $V = '0.32-openwrt';
17 use Getopt::Long qw(:config no_auto_abbrev);
37 my $configuration_file = ".checkpatch.conf";
43 Usage: $P [OPTION]... [FILE]...
48 --no-tree run without a kernel tree
49 --no-signoff do not check for 'Signed-off-by' line
50 --patch treat FILE as patchfile (default)
51 --emacs emacs compile window format
52 --terse one line per report
53 -f, --file treat FILE as regular source file
54 --subjective, --strict enable more subjective tests
55 --ignore TYPE(,TYPE2...) ignore various comma separated message types
56 --show-types show the message "types" in the output
57 --root=PATH PATH to the kernel tree root
58 --no-summary suppress the per-file summary
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.
74 my $conf = which_conf($configuration_file);
77 open(my $conffile, '<', "$conf")
78 or warn "$P: Can't find a readable $configuration_file file $!\n";
83 $line =~ s/\s*\n?$//g;
87 next if ($line =~ m/^\s*#/);
88 next if ($line =~ m/^\s*$/);
90 my @words = split(" ", $line);
91 foreach my $word (@words) {
92 last if ($word =~ m/^#/);
93 push (@conf_args, $word);
97 unshift(@ARGV, @conf_args) if @conf_args;
101 'q|quiet+' => \$quiet,
103 'signoff!' => \$chk_signoff,
104 'patch!' => \$chk_patch,
108 'subjective!' => \$check,
109 'strict!' => \$check,
110 'ignore=s' => \@ignore,
111 'show-types!' => \$show_types,
113 'summary!' => \$summary,
114 'mailback!' => \$mailback,
115 'summary-file!' => \$summary_file,
117 'debug=s' => \%debug,
118 'test-only=s' => \$tst_only,
128 print "$P: no input files\n";
132 @ignore = split(/,/, join(',',@ignore));
133 foreach my $word (@ignore) {
134 $word =~ s/\s*\n?$//g;
137 $word =~ tr/[a-z]/[A-Z]/;
139 next if ($word =~ m/^\s*#/);
140 next if ($word =~ m/^\s*$/);
142 $ignore_type{$word}++;
146 my $dbg_possible = 0;
149 for my $key (keys %debug) {
151 eval "\${dbg_$key} = '$debug{$key}';";
155 my $rpt_cleaners = 0;
164 if (!top_of_openwrt_tree($root)) {
165 die "$P: $root: --root does not point at a valid tree\n";
168 if (top_of_openwrt_tree('.')) {
170 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
171 top_of_openwrt_tree($1)) {
176 if (!defined $root) {
177 print "Must be run from the top-level dir. of a OpenWrt tree\n";
182 my $emitted_corrupt = 0;
185 [A-Za-z_][A-Za-z\d_]*
186 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
188 our $Storage = qr{extern|static|asmlinkage};
201 # Notes to $Attribute:
202 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
221 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
222 ____cacheline_aligned|
223 ____cacheline_aligned_in_smp|
224 ____cacheline_internodealigned_in_smp|
228 our $Inline = qr{inline|__always_inline|noinline};
229 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
230 our $Lval = qr{$Ident(?:$Member)*};
232 our $Constant = qr{(?i:(?:[0-9]+|0x[0-9a-f]+)[ul]*)};
233 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
234 our $Compare = qr{<=|>=|==|!=|<|>};
238 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
245 our $NON_ASCII_UTF8 = qr{
246 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
247 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
248 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
249 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
250 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
251 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
252 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
256 [\x09\x0A\x0D\x20-\x7E] # ASCII
260 our $typeTypedefs = qr{(?x:
261 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
265 our $logFunctions = qr{(?x:
266 printk(?:_ratelimited|_once|)|
267 [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
268 WARN(?:_RATELIMIT|_ONCE|)|
273 our $signature_tags = qr{(?xi:
285 qr{(?:unsigned\s+)?char},
286 qr{(?:unsigned\s+)?short},
287 qr{(?:unsigned\s+)?int},
288 qr{(?:unsigned\s+)?long},
289 qr{(?:unsigned\s+)?long\s+int},
290 qr{(?:unsigned\s+)?long\s+long},
291 qr{(?:unsigned\s+)?long\s+long\s+int},
300 qr{${Ident}_handler},
301 qr{${Ident}_handler_fn},
303 our @modifierList = (
307 our $allowed_asm_includes = qr{(?x:
311 # memory.h: ARM has a custom one
314 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
315 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
316 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
318 (?:$Modifier\s+|const\s+)*
320 (?:typeof|__typeof__)\s*\([^\)]*\)|
324 (?:\s+$Modifier|\s+const)*
328 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
329 (?:\s+$Inline|\s+$Modifier)*
331 $Declare = qr{(?:$Storage\s+)?$Type};
336 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
338 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
339 # requires at least perl version v5.10.0
340 # Any use must be runtime checked with $^V
342 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
343 our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
344 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
348 return "" if (!defined($string));
349 $string =~ s@^\s*\(\s*@@g;
350 $string =~ s@\s*\)\s*$@@g;
351 $string =~ s@\s+@ @g;
355 $chk_signoff = 0 if ($file);
360 for my $filename (@ARGV) {
363 open($FILE, '-|', "diff -u /dev/null $filename") ||
364 die "$P: $filename: diff failed - $!\n";
365 } elsif ($filename eq '-') {
366 open($FILE, '<&STDIN');
368 open($FILE, '<', "$filename") ||
369 die "$P: $filename: open failed - $!\n";
371 if ($filename eq '-') {
372 $vname = 'Your patch';
381 if (!process($filename)) {
390 sub top_of_openwrt_tree {
394 "BSDmakefile", "Config.in", "LICENSE", "Makefile", "README",
395 "feeds.conf.default", "include", "package", "rules.mk",
396 "scripts", "target", "toolchain", "tools"
399 foreach my $check (@tree_check) {
400 if (! -e $root . '/' . $check) {
408 my ($formatted_email) = @_;
414 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
417 $comment = $3 if defined $3;
418 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
420 $comment = $2 if defined $2;
421 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
423 $comment = $2 if defined $2;
424 $formatted_email =~ s/$address.*$//;
425 $name = $formatted_email;
426 $name =~ s/^\s+|\s+$//g;
427 $name =~ s/^\"|\"$//g;
428 # If there's a name left after stripping spaces and
429 # leading quotes, and the address doesn't have both
430 # leading and trailing angle brackets, the address
432 # "joe smith joe@smith.com" bad
433 # "joe smith <joe@smith.com" bad
434 if ($name ne "" && $address !~ /^<[^>]+>$/) {
441 $name =~ s/^\s+|\s+$//g;
442 $name =~ s/^\"|\"$//g;
443 $address =~ s/^\s+|\s+$//g;
444 $address =~ s/^\<|\>$//g;
446 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
447 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
451 return ($name, $address, $comment);
455 my ($name, $address) = @_;
459 $name =~ s/^\s+|\s+$//g;
460 $name =~ s/^\"|\"$//g;
461 $address =~ s/^\s+|\s+$//g;
463 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
464 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
469 $formatted_email = "$address";
471 $formatted_email = "$name <$address>";
474 return $formatted_email;
480 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
481 if (-e "$path/$conf") {
482 return "$path/$conf";
494 for my $c (split(//, $str)) {
498 for (; ($n % 8) != 0; $n++) {
510 (my $res = shift) =~ tr/\t/ /c;
517 # Drop the diff line leader and expand tabs
519 $line = expand_tabs($line);
521 # Pick the indent from the front of the line.
522 my ($white) = ($line =~ /^(\s*)/);
524 return (length($line), length($white));
527 my $sanitise_quote = '';
529 sub sanitise_line_reset {
530 my ($in_comment) = @_;
533 $sanitise_quote = '*/';
535 $sanitise_quote = '';
548 # Always copy over the diff marker.
549 $res = substr($line, 0, 1);
551 for ($off = 1; $off < length($line); $off++) {
552 $c = substr($line, $off, 1);
554 # Comments we are wacking completly including the begin
555 # and end, all to $;.
556 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
557 $sanitise_quote = '*/';
559 substr($res, $off, 2, "$;$;");
563 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
564 $sanitise_quote = '';
565 substr($res, $off, 2, "$;$;");
569 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
570 $sanitise_quote = '//';
572 substr($res, $off, 2, $sanitise_quote);
577 # A \ in a string means ignore the next character.
578 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
580 substr($res, $off, 2, 'XX');
585 if ($c eq "'" || $c eq '"') {
586 if ($sanitise_quote eq '') {
587 $sanitise_quote = $c;
589 substr($res, $off, 1, $c);
591 } elsif ($sanitise_quote eq $c) {
592 $sanitise_quote = '';
596 #print "c<$c> SQ<$sanitise_quote>\n";
597 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
598 substr($res, $off, 1, $;);
599 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
600 substr($res, $off, 1, $;);
601 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
602 substr($res, $off, 1, 'X');
604 substr($res, $off, 1, $c);
608 if ($sanitise_quote eq '//') {
609 $sanitise_quote = '';
612 # The pathname on a #include may be surrounded by '<' and '>'.
613 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
614 my $clean = 'X' x length($1);
615 $res =~ s@\<.*\>@<$clean>@;
617 # The whole of a #error is a string.
618 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
619 my $clean = 'X' x length($1);
620 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
626 sub ctx_statement_block {
627 my ($linenr, $remain, $off) = @_;
628 my $line = $linenr - 1;
645 @stack = (['', 0]) if ($#stack == -1);
647 #warn "CSB: blk<$blk> remain<$remain>\n";
648 # If we are about to drop off the end, pull in more
651 for (; $remain > 0; $line++) {
652 last if (!defined $lines[$line]);
653 next if ($lines[$line] =~ /^-/);
656 $blk .= $lines[$line] . "\n";
661 # Bail if there is no further context.
662 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
666 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
672 $c = substr($blk, $off, 1);
673 $remainder = substr($blk, $off);
675 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
677 # Handle nested #if/#else.
678 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
679 push(@stack, [ $type, $level ]);
680 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
681 ($type, $level) = @{$stack[$#stack - 1]};
682 } elsif ($remainder =~ /^#\s*endif\b/) {
683 ($type, $level) = @{pop(@stack)};
686 # Statement ends at the ';' or a close '}' at the
688 if ($level == 0 && $c eq ';') {
692 # An else is really a conditional as long as its not else if
693 if ($level == 0 && $coff_set == 0 &&
694 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
695 $remainder =~ /^(else)(?:\s|{)/ &&
696 $remainder !~ /^else\s+if\b/) {
697 $coff = $off + length($1) - 1;
699 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
700 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
703 if (($type eq '' || $type eq '(') && $c eq '(') {
707 if ($type eq '(' && $c eq ')') {
709 $type = ($level != 0)? '(' : '';
711 if ($level == 0 && $coff < $soff) {
714 #warn "CSB: mark coff<$coff>\n";
717 if (($type eq '' || $type eq '{') && $c eq '{') {
721 if ($type eq '{' && $c eq '}') {
723 $type = ($level != 0)? '{' : '';
726 if (substr($blk, $off + 1, 1) eq ';') {
732 # Preprocessor commands end at the newline unless escaped.
733 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
741 # We are truly at the end, so shuffle to the next line.
748 my $statement = substr($blk, $soff, $off - $soff + 1);
749 my $condition = substr($blk, $soff, $coff - $soff + 1);
751 #warn "STATEMENT<$statement>\n";
752 #warn "CONDITION<$condition>\n";
754 #print "coff<$coff> soff<$off> loff<$loff>\n";
756 return ($statement, $condition,
757 $line, $remain + 1, $off - $loff + 1, $level);
760 sub statement_lines {
763 # Strip the diff line prefixes and rip blank lines at start and end.
764 $stmt =~ s/(^|\n)./$1/g;
768 my @stmt_lines = ($stmt =~ /\n/g);
770 return $#stmt_lines + 2;
773 sub statement_rawlines {
776 my @stmt_lines = ($stmt =~ /\n/g);
778 return $#stmt_lines + 2;
781 sub statement_block_size {
784 $stmt =~ s/(^|\n)./$1/g;
790 my @stmt_lines = ($stmt =~ /\n/g);
791 my @stmt_statements = ($stmt =~ /;/g);
793 my $stmt_lines = $#stmt_lines + 2;
794 my $stmt_statements = $#stmt_statements + 1;
796 if ($stmt_lines > $stmt_statements) {
799 return $stmt_statements;
803 sub ctx_statement_full {
804 my ($linenr, $remain, $off) = @_;
805 my ($statement, $condition, $level);
809 # Grab the first conditional/block pair.
810 ($statement, $condition, $linenr, $remain, $off, $level) =
811 ctx_statement_block($linenr, $remain, $off);
812 #print "F: c<$condition> s<$statement> remain<$remain>\n";
813 push(@chunks, [ $condition, $statement ]);
814 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
815 return ($level, $linenr, @chunks);
818 # Pull in the following conditional/block pairs and see if they
819 # could continue the statement.
821 ($statement, $condition, $linenr, $remain, $off, $level) =
822 ctx_statement_block($linenr, $remain, $off);
823 #print "C: c<$condition> s<$statement> remain<$remain>\n";
824 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
826 push(@chunks, [ $condition, $statement ]);
829 return ($level, $linenr, @chunks);
833 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
835 my $start = $linenr - 1;
842 my @stack = ($level);
843 for ($line = $start; $remain > 0; $line++) {
844 next if ($rawlines[$line] =~ /^-/);
847 $blk .= $rawlines[$line];
849 # Handle nested #if/#else.
850 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
851 push(@stack, $level);
852 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
853 $level = $stack[$#stack - 1];
854 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
855 $level = pop(@stack);
858 foreach my $c (split(//, $lines[$line])) {
859 ##print "C<$c>L<$level><$open$close>O<$off>\n";
865 if ($c eq $close && $level > 0) {
867 last if ($level == 0);
868 } elsif ($c eq $open) {
873 if (!$outer || $level <= 1) {
874 push(@res, $rawlines[$line]);
877 last if ($level == 0);
880 return ($level, @res);
882 sub ctx_block_outer {
883 my ($linenr, $remain) = @_;
885 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
889 my ($linenr, $remain) = @_;
891 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
895 my ($linenr, $remain, $off) = @_;
897 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
900 sub ctx_block_level {
901 my ($linenr, $remain) = @_;
903 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
905 sub ctx_statement_level {
906 my ($linenr, $remain, $off) = @_;
908 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
911 sub ctx_locate_comment {
912 my ($first_line, $end_line) = @_;
914 # Catch a comment on the end of the line itself.
915 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
916 return $current_comment if (defined $current_comment);
918 # Look through the context and try and figure out if there is a
921 $current_comment = '';
922 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
923 my $line = $rawlines[$linenr - 1];
925 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
928 if ($line =~ m@/\*@) {
931 if (!$in_comment && $current_comment ne '') {
932 $current_comment = '';
934 $current_comment .= $line . "\n" if ($in_comment);
935 if ($line =~ m@\*/@) {
940 chomp($current_comment);
941 return($current_comment);
943 sub ctx_has_comment {
944 my ($first_line, $end_line) = @_;
945 my $cmt = ctx_locate_comment($first_line, $end_line);
947 ##print "LINE: $rawlines[$end_line - 1 ]\n";
948 ##print "CMMT: $cmt\n";
954 my ($linenr, $cnt) = @_;
956 my $offset = $linenr - 1;
961 $line = $rawlines[$offset++];
962 next if (defined($line) && $line =~ /^-/);
974 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
977 $coded = sprintf("^%c", unpack('C', $2) + 64);
986 my $av_preprocessor = 0;
992 $av_preprocessor = 0;
994 @av_paren_type = ('E');
995 $av_pend_colon = 'O';
998 sub annotate_values {
999 my ($stream, $type) = @_;
1002 my $var = '_' x length($stream);
1005 print "$stream\n" if ($dbg_values > 1);
1007 while (length($cur)) {
1008 @av_paren_type = ('E') if ($#av_paren_type < 0);
1009 print " <" . join('', @av_paren_type) .
1010 "> <$type> <$av_pending>" if ($dbg_values > 1);
1011 if ($cur =~ /^(\s+)/o) {
1012 print "WS($1)\n" if ($dbg_values > 1);
1013 if ($1 =~ /\n/ && $av_preprocessor) {
1014 $type = pop(@av_paren_type);
1015 $av_preprocessor = 0;
1018 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1019 print "CAST($1)\n" if ($dbg_values > 1);
1020 push(@av_paren_type, $type);
1023 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1024 print "DECLARE($1)\n" if ($dbg_values > 1);
1027 } elsif ($cur =~ /^($Modifier)\s*/) {
1028 print "MODIFIER($1)\n" if ($dbg_values > 1);
1031 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1032 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1033 $av_preprocessor = 1;
1034 push(@av_paren_type, $type);
1040 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1041 print "UNDEF($1)\n" if ($dbg_values > 1);
1042 $av_preprocessor = 1;
1043 push(@av_paren_type, $type);
1045 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1046 print "PRE_START($1)\n" if ($dbg_values > 1);
1047 $av_preprocessor = 1;
1049 push(@av_paren_type, $type);
1050 push(@av_paren_type, $type);
1053 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1054 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1055 $av_preprocessor = 1;
1057 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1061 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1062 print "PRE_END($1)\n" if ($dbg_values > 1);
1064 $av_preprocessor = 1;
1066 # Assume all arms of the conditional end as this
1067 # one does, and continue as if the #endif was not here.
1068 pop(@av_paren_type);
1069 push(@av_paren_type, $type);
1072 } elsif ($cur =~ /^(\\\n)/o) {
1073 print "PRECONT($1)\n" if ($dbg_values > 1);
1075 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1076 print "ATTR($1)\n" if ($dbg_values > 1);
1077 $av_pending = $type;
1080 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1081 print "SIZEOF($1)\n" if ($dbg_values > 1);
1087 } elsif ($cur =~ /^(if|while|for)\b/o) {
1088 print "COND($1)\n" if ($dbg_values > 1);
1092 } elsif ($cur =~/^(case)/o) {
1093 print "CASE($1)\n" if ($dbg_values > 1);
1094 $av_pend_colon = 'C';
1097 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1098 print "KEYWORD($1)\n" if ($dbg_values > 1);
1101 } elsif ($cur =~ /^(\()/o) {
1102 print "PAREN('$1')\n" if ($dbg_values > 1);
1103 push(@av_paren_type, $av_pending);
1107 } elsif ($cur =~ /^(\))/o) {
1108 my $new_type = pop(@av_paren_type);
1109 if ($new_type ne '_') {
1111 print "PAREN('$1') -> $type\n"
1112 if ($dbg_values > 1);
1114 print "PAREN('$1')\n" if ($dbg_values > 1);
1117 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1118 print "FUNC($1)\n" if ($dbg_values > 1);
1122 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1123 if (defined $2 && $type eq 'C' || $type eq 'T') {
1124 $av_pend_colon = 'B';
1125 } elsif ($type eq 'E') {
1126 $av_pend_colon = 'L';
1128 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1131 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1132 print "IDENT($1)\n" if ($dbg_values > 1);
1135 } elsif ($cur =~ /^($Assignment)/o) {
1136 print "ASSIGN($1)\n" if ($dbg_values > 1);
1139 } elsif ($cur =~/^(;|{|})/) {
1140 print "END($1)\n" if ($dbg_values > 1);
1142 $av_pend_colon = 'O';
1144 } elsif ($cur =~/^(,)/) {
1145 print "COMMA($1)\n" if ($dbg_values > 1);
1148 } elsif ($cur =~ /^(\?)/o) {
1149 print "QUESTION($1)\n" if ($dbg_values > 1);
1152 } elsif ($cur =~ /^(:)/o) {
1153 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1155 substr($var, length($res), 1, $av_pend_colon);
1156 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1161 $av_pend_colon = 'O';
1163 } elsif ($cur =~ /^(\[)/o) {
1164 print "CLOSE($1)\n" if ($dbg_values > 1);
1167 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1170 print "OPV($1)\n" if ($dbg_values > 1);
1177 substr($var, length($res), 1, $variant);
1180 } elsif ($cur =~ /^($Operators)/o) {
1181 print "OP($1)\n" if ($dbg_values > 1);
1182 if ($1 ne '++' && $1 ne '--') {
1186 } elsif ($cur =~ /(^.)/o) {
1187 print "C($1)\n" if ($dbg_values > 1);
1190 $cur = substr($cur, length($1));
1191 $res .= $type x length($1);
1195 return ($res, $var);
1199 my ($possible, $line) = @_;
1200 my $notPermitted = qr{(?:
1217 ^(?:typedef|struct|enum)\b
1219 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1220 if ($possible !~ $notPermitted) {
1221 # Check for modifiers.
1222 $possible =~ s/\s*$Storage\s*//g;
1223 $possible =~ s/\s*$Sparse\s*//g;
1224 if ($possible =~ /^\s*$/) {
1226 } elsif ($possible =~ /\s/) {
1227 $possible =~ s/\s*$Type\s*//g;
1228 for my $modifier (split(' ', $possible)) {
1229 if ($modifier !~ $notPermitted) {
1230 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1231 push(@modifierList, $modifier);
1236 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1237 push(@typeList, $possible);
1241 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1248 return !defined $ignore_type{$_[0]};
1252 if (!show_type($_[1]) ||
1253 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1258 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1260 $line = "$prefix$_[0]: $_[2]\n";
1262 $line = (split('\n', $line))[0] . "\n" if ($terse);
1264 push(our @report, $line);
1273 if (report("ERROR", $_[0], $_[1])) {
1279 if (report("WARNING", $_[0], $_[1])) {
1285 if ($check && report("CHECK", $_[0], $_[1])) {
1291 sub check_absolute_file {
1292 my ($absolute, $herecurr) = @_;
1293 my $file = $absolute;
1295 ##print "absolute<$absolute>\n";
1297 # See if any suffix of this path is a path within the tree.
1298 while ($file =~ s@^[^/]*/@@) {
1299 if (-f "$root/$file") {
1300 ##print "file<$file>\n";
1308 # It is, so see if the prefix is acceptable.
1309 my $prefix = $absolute;
1310 substr($prefix, -length($file)) = '';
1312 ##print "prefix<$prefix>\n";
1313 if ($prefix ne ".../") {
1314 WARN("USE_RELATIVE_PATH",
1315 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1319 sub pos_last_openparen {
1324 my $opens = $line =~ tr/\(/\(/;
1325 my $closes = $line =~ tr/\)/\)/;
1327 my $last_openparen = 0;
1329 if (($opens == 0) || ($closes >= $opens)) {
1333 my $len = length($line);
1335 for ($pos = 0; $pos < $len; $pos++) {
1336 my $string = substr($line, $pos);
1337 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1338 $pos += length($1) - 1;
1339 } elsif (substr($line, $pos, 1) eq '(') {
1340 $last_openparen = $pos;
1341 } elsif (index($string, '(') == -1) {
1346 return $last_openparen + 1;
1350 my $filename = shift;
1356 my $stashrawline="";
1367 my $in_header_lines = 1;
1368 my $in_commit_log = 0; #Scanning lines before patch
1376 # Trace the real file/line as we go.
1382 my $comment_edge = 0;
1386 my $prev_values = 'E';
1389 my %suppress_ifbraces;
1390 my %suppress_whiletrailers;
1391 my %suppress_export;
1392 my $suppress_statement = 0;
1394 # Pre-scan the patch sanitizing the lines.
1395 sanitise_line_reset();
1397 foreach my $rawline (@rawlines) {
1401 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1410 # Guestimate if this is a continuing comment. Run
1411 # the context looking for a comment "edge". If this
1412 # edge is a close comment then we must be in a comment
1416 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1417 next if (defined $rawlines[$ln - 1] &&
1418 $rawlines[$ln - 1] =~ /^-/);
1420 #print "RAW<$rawlines[$ln - 1]>\n";
1421 last if (!defined $rawlines[$ln - 1]);
1422 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1423 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1428 if (defined $edge && $edge eq '*/') {
1432 # Guestimate if this is a continuing comment. If this
1433 # is the start of a diff block and this line starts
1434 # ' *' then it is very likely a comment.
1435 if (!defined $edge &&
1436 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1441 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1442 sanitise_line_reset($in_comment);
1444 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1445 # Standardise the strings and chars within the input to
1446 # simplify matching -- only bother with positive lines.
1447 $line = sanitise_line($rawline);
1449 push(@lines, $line);
1452 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1457 #print "==>$rawline\n";
1458 #print "-->$line\n";
1465 foreach my $line (@lines) {
1468 my $rawline = $rawlines[$linenr - 1];
1470 #extract the line range in the file after the patch is applied
1471 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1473 $first_line = $linenr + 1;
1483 %suppress_ifbraces = ();
1484 %suppress_whiletrailers = ();
1485 %suppress_export = ();
1486 $suppress_statement = 0;
1489 # track the line number as we move through the hunk, note that
1490 # new versions of GNU diff omit the leading space on completely
1491 # blank context lines so we need to count that too.
1492 } elsif ($line =~ /^( |\+|$)/) {
1494 $realcnt-- if ($realcnt != 0);
1496 # Measure the line length and indent.
1497 ($length, $indent) = line_stats($rawline);
1499 # Track the previous line.
1500 ($prevline, $stashline) = ($stashline, $line);
1501 ($previndent, $stashindent) = ($stashindent, $indent);
1502 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1504 #warn "line<$line>\n";
1506 } elsif ($realcnt == 1) {
1510 my $hunk_line = ($realcnt != 0);
1512 #make up the handle for any error we report on this line
1513 $prefix = "$filename:$realline: " if ($emacs && $file);
1514 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1516 $here = "#$linenr: " if (!$file);
1517 $here = "#$realline: " if ($file);
1519 # extract the filename as it passes
1520 if ($line =~ /^diff --git.*?(\S+)$/) {
1522 $realfile =~ s@^([^/]*)/@@;
1524 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1526 $realfile =~ s@^([^/]*)/@@;
1530 if (!$file && $tree && $p1_prefix ne '' &&
1531 -e "$root/$p1_prefix") {
1532 WARN("PATCH_PREFIX",
1533 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1536 if ($realfile =~ m@^include/asm/@) {
1537 ERROR("MODIFIED_INCLUDE_ASM",
1538 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1543 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1545 my $hereline = "$here\n$rawline\n";
1546 my $herecurr = "$here\n$rawline\n";
1547 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1549 $cnt_lines++ if ($realcnt != 0);
1551 # Check for incorrect file permissions
1552 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1553 my $permhere = $here . "FILE: $realfile\n";
1554 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1555 ERROR("EXECUTE_PERMISSIONS",
1556 "do not set execute permissions for source files\n" . $permhere);
1560 # Check the patch for a signoff:
1561 if ($line =~ /^\s*signed-off-by:/i) {
1566 # Check signature styles
1567 if (!$in_header_lines &&
1568 $line =~ /^(\s*)($signature_tags)(\s*)(.*)/) {
1569 my $space_before = $1;
1571 my $space_after = $3;
1573 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1575 if (defined $space_before && $space_before ne "") {
1576 WARN("BAD_SIGN_OFF",
1577 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
1579 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1580 WARN("BAD_SIGN_OFF",
1581 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
1583 if (!defined $space_after || $space_after ne " ") {
1584 WARN("BAD_SIGN_OFF",
1585 "Use a single space after $ucfirst_sign_off\n" . $herecurr);
1588 my ($email_name, $email_address, $comment) = parse_email($email);
1589 my $suggested_email = format_email(($email_name, $email_address));
1590 if ($suggested_email eq "") {
1591 ERROR("BAD_SIGN_OFF",
1592 "Unrecognized email address: '$email'\n" . $herecurr);
1594 my $dequoted = $suggested_email;
1595 $dequoted =~ s/^"//;
1596 $dequoted =~ s/" </ </;
1597 # Don't force email to have quotes
1598 # Allow just an angle bracketed address
1599 if ("$dequoted$comment" ne $email &&
1600 "<$email_address>$comment" ne $email &&
1601 "$suggested_email$comment" ne $email) {
1602 WARN("BAD_SIGN_OFF",
1603 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
1608 # Check for wrappage within a valid hunk of the file
1609 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1610 ERROR("CORRUPTED_PATCH",
1611 "patch seems to be corrupt (line wrapped?)\n" .
1612 $herecurr) if (!$emitted_corrupt++);
1615 # Check for absolute kernel paths.
1617 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1620 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1621 check_absolute_file($1, $herecurr)) {
1624 check_absolute_file($file, $herecurr);
1629 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1630 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1631 $rawline !~ m/^$UTF8*$/) {
1632 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1634 my $blank = copy_spacing($rawline);
1635 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1636 my $hereptr = "$hereline$ptr\n";
1639 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1642 # Check if it's the start of a commit log
1643 # (not a header line and we haven't seen the patch filename)
1644 if ($in_header_lines && $realfile =~ /^$/ &&
1645 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
1646 $in_header_lines = 0;
1650 # Still not yet in a patch, check for any UTF-8
1651 if ($in_commit_log && $realfile =~ /^$/ &&
1652 $rawline =~ /$NON_ASCII_UTF8/) {
1653 CHK("UTF8_BEFORE_PATCH",
1654 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1657 # ignore non-hunk lines and lines being removed
1658 next if (!$hunk_line || $line =~ /^-/);
1660 #trailing whitespace
1661 if ($line =~ /^\+.*\015/) {
1662 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1663 ERROR("DOS_LINE_ENDINGS",
1664 "DOS line endings\n" . $herevet);
1666 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1667 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1668 ERROR("TRAILING_WHITESPACE",
1669 "trailing whitespace\n" . $herevet);
1673 # check for Kconfig help text having a real description
1674 # Only applies when adding the entry originally, after that we do not have
1675 # sufficient context to determine whether it is indeed long enough.
1676 if ($realfile =~ /Kconfig/ &&
1677 $line =~ /.\s*config\s+/) {
1680 my $ln = $linenr + 1;
1684 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
1685 $f = $lines[$ln - 1];
1686 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1687 $is_end = $lines[$ln - 1] =~ /^\+/;
1689 next if ($f =~ /^-/);
1691 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1693 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1700 next if ($f =~ /^$/);
1701 if ($f =~ /^\s*config\s/) {
1707 WARN("CONFIG_DESCRIPTION",
1708 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1709 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
1712 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1713 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1716 'EXTRA_AFLAGS' => 'asflags-y',
1717 'EXTRA_CFLAGS' => 'ccflags-y',
1718 'EXTRA_CPPFLAGS' => 'cppflags-y',
1719 'EXTRA_LDFLAGS' => 'ldflags-y',
1722 WARN("DEPRECATED_VARIABLE",
1723 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1726 # check we are in a valid source file if not then ignore this hunk
1727 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1730 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1731 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1732 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
1733 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1737 "line over 80 characters\n" . $herecurr);
1740 # Check for user-visible strings broken across lines, which breaks the ability
1741 # to grep for the string. Limited to strings used as parameters (those
1742 # following an open parenthesis), which almost completely eliminates false
1743 # positives, as well as warning only once per parameter rather than once per
1744 # line of the string. Make an exception when the previous string ends in a
1745 # newline (multiple lines in one string constant) or \n\t (common in inline
1746 # assembly to indent the instruction on the following line).
1747 if ($line =~ /^\+\s*"/ &&
1748 $prevline =~ /"\s*$/ &&
1749 $prevline =~ /\(/ &&
1750 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1751 WARN("SPLIT_STRING",
1752 "quoted string split across lines\n" . $hereprev);
1755 # check for spaces before a quoted newline
1756 if ($rawline =~ /^.*\".*\s\\n/) {
1757 WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1758 "unnecessary whitespace before a quoted newline\n" . $herecurr);
1761 # check for adding lines without a newline.
1762 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1763 WARN("MISSING_EOF_NEWLINE",
1764 "adding a line without newline at end of file\n" . $herecurr);
1767 # Blackfin: use hi/lo macros
1768 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1769 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1770 my $herevet = "$here\n" . cat_vet($line) . "\n";
1772 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1774 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1775 my $herevet = "$here\n" . cat_vet($line) . "\n";
1777 "use the HI() macro, not (... >> 16)\n" . $herevet);
1781 # check we are in a valid source file C or perl if not then ignore this hunk
1782 next if ($realfile !~ /\.(h|c|pl)$/);
1784 # at the beginning of a line any tabs must come first and anything
1785 # more than 8 must use tabs.
1786 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1787 $rawline =~ /^\+\s* \s*/) {
1788 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1789 ERROR("CODE_INDENT",
1790 "code indent should use tabs where possible\n" . $herevet);
1794 # check for space before tabs.
1795 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1796 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1797 WARN("SPACE_BEFORE_TAB",
1798 "please, no space before tabs\n" . $herevet);
1801 # check for && or || at the start of a line
1802 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
1803 CHK("LOGICAL_CONTINUATIONS",
1804 "Logical continuations should be on the previous line\n" . $hereprev);
1807 # check multi-line statement indentation matches previous line
1808 if ($^V && $^V ge 5.10.0 &&
1809 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
1810 $prevline =~ /^\+(\t*)(.*)$/;
1814 my $pos = pos_last_openparen($rest);
1816 $line =~ /^\+([ \t]*)/;
1819 my $goodtabindent = $oldindent .
1822 my $goodspaceindent = $oldindent . " " x $pos;
1824 if ($newindent ne $goodtabindent &&
1825 $newindent ne $goodspaceindent) {
1826 CHK("PARENTHESIS_ALIGNMENT",
1827 "Alignment should match open parenthesis\n" . $hereprev);
1832 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) {
1834 "No space is necessary after a cast\n" . $hereprev);
1837 if ($rawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
1838 $prevrawline =~ /^\+[ \t]*$/) {
1839 CHK("BLOCK_COMMENT_STYLE",
1840 "Don't begin block comments with only a /* line, use /* comment...\n" . $hereprev);
1843 # check for spaces at the beginning of a line.
1845 # 1) within comments
1846 # 2) indented preprocessor commands
1848 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
1849 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1850 WARN("LEADING_SPACE",
1851 "please, no spaces at the start of a line\n" . $herevet);
1854 # check we are in a valid C source file if not then ignore this hunk
1855 next if ($realfile !~ /\.(h|c)$/);
1857 # check for RCS/CVS revision markers
1858 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1860 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1863 # Blackfin: don't use __builtin_bfin_[cs]sync
1864 if ($line =~ /__builtin_bfin_csync/) {
1865 my $herevet = "$here\n" . cat_vet($line) . "\n";
1867 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1869 if ($line =~ /__builtin_bfin_ssync/) {
1870 my $herevet = "$here\n" . cat_vet($line) . "\n";
1872 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1875 # Check for potential 'bare' types
1876 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1878 #print "LINE<$line>\n";
1879 if ($linenr >= $suppress_statement &&
1880 $realcnt && $line =~ /.\s*\S/) {
1881 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1882 ctx_statement_block($linenr, $realcnt, 0);
1883 $stat =~ s/\n./\n /g;
1884 $cond =~ s/\n./\n /g;
1886 #print "linenr<$linenr> <$stat>\n";
1887 # If this statement has no statement boundaries within
1888 # it there is no point in retrying a statement scan
1889 # until we hit end of it.
1890 my $frag = $stat; $frag =~ s/;+\s*$//;
1891 if ($frag !~ /(?:{|;)/) {
1892 #print "skip<$line_nr_next>\n";
1893 $suppress_statement = $line_nr_next;
1896 # Find the real next line.
1897 $realline_next = $line_nr_next;
1898 if (defined $realline_next &&
1899 (!defined $lines[$realline_next - 1] ||
1900 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1907 # Ignore goto labels.
1908 if ($s =~ /$Ident:\*$/s) {
1910 # Ignore functions being called
1911 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1913 } elsif ($s =~ /^.\s*else\b/s) {
1915 # declarations always start with types
1916 } 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) {
1919 possible($type, "A:" . $s);
1921 # definitions in global scope can only start with types
1922 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1923 possible($1, "B:" . $s);
1926 # any (foo ... *) is a pointer cast, and foo is a type
1927 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1928 possible($1, "C:" . $s);
1931 # Check for any sort of function declaration.
1932 # int foo(something bar, other baz);
1933 # void (*store_gdt)(x86_descr_ptr *);
1934 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1935 my ($name_len) = length($1);
1938 substr($ctx, 0, $name_len + 1, '');
1939 $ctx =~ s/\)[^\)]*$//;
1941 for my $arg (split(/\s*,\s*/, $ctx)) {
1942 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1944 possible($1, "D:" . $s);
1952 # Checks which may be anchored in the context.
1955 # Check for switch () and associated case and default
1956 # statements should be at the same indent.
1957 if ($line=~/\bswitch\s*\(.*\)/) {
1960 my @ctx = ctx_block_outer($linenr, $realcnt);
1962 for my $ctx (@ctx) {
1963 my ($clen, $cindent) = line_stats($ctx);
1964 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1965 $indent != $cindent) {
1966 $err .= "$sep$ctx\n";
1973 ERROR("SWITCH_CASE_INDENT_LEVEL",
1974 "switch and case should be at the same indent\n$hereline$err");
1978 # if/while/etc brace do not go on next line, unless defining a do while loop,
1979 # or if that brace on the next line is for something else
1980 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1981 my $pre_ctx = "$1$2";
1983 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1985 if ($line =~ /^\+\t{6,}/) {
1986 WARN("DEEP_INDENTATION",
1987 "Too many leading tabs - consider code refactoring\n" . $herecurr);
1990 my $ctx_cnt = $realcnt - $#ctx - 1;
1991 my $ctx = join("\n", @ctx);
1993 my $ctx_ln = $linenr;
1994 my $ctx_skip = $realcnt;
1996 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1997 defined $lines[$ctx_ln - 1] &&
1998 $lines[$ctx_ln - 1] =~ /^-/)) {
1999 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2000 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2004 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2005 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2007 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2009 "that open brace { should be on the previous line\n" .
2010 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2012 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2013 $ctx =~ /\)\s*\;\s*$/ &&
2014 defined $lines[$ctx_ln - 1])
2016 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2017 if ($nindent > $indent) {
2018 WARN("TRAILING_SEMICOLON",
2019 "trailing semicolon indicates no statements, indent implies otherwise\n" .
2020 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2025 # Check relative indent for conditionals and blocks.
2026 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
2027 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2028 ctx_statement_block($linenr, $realcnt, 0)
2029 if (!defined $stat);
2030 my ($s, $c) = ($stat, $cond);
2032 substr($s, 0, length($c), '');
2034 # Make sure we remove the line prefixes as we have
2035 # none on the first line, and are going to readd them
2039 # Find out how long the conditional actually is.
2040 my @newlines = ($c =~ /\n/gs);
2041 my $cond_lines = 1 + $#newlines;
2043 # We want to check the first line inside the block
2044 # starting at the end of the conditional, so remove:
2045 # 1) any blank line termination
2046 # 2) any opening brace { on end of the line
2048 my $continuation = 0;
2050 $s =~ s/^.*\bdo\b//;
2052 if ($s =~ s/^\s*\\//) {
2055 if ($s =~ s/^\s*?\n//) {
2060 # Also ignore a loop construct at the end of a
2061 # preprocessor statement.
2062 if (($prevline =~ /^.\s*#\s*define\s/ ||
2063 $prevline =~ /\\\s*$/) && $continuation == 0) {
2069 while ($cond_ptr != $cond_lines) {
2070 $cond_ptr = $cond_lines;
2072 # If we see an #else/#elif then the code
2074 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2079 # 1) blank lines, they should be at 0,
2080 # 2) preprocessor lines, and
2082 if ($continuation ||
2084 $s =~ /^\s*#\s*?/ ||
2085 $s =~ /^\s*$Ident\s*:/) {
2086 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
2087 if ($s =~ s/^.*?\n//) {
2093 my (undef, $sindent) = line_stats("+" . $s);
2094 my $stat_real = raw_line($linenr, $cond_lines);
2096 # Check if either of these lines are modified, else
2097 # this is not this patch's fault.
2098 if (!defined($stat_real) ||
2099 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2102 if (defined($stat_real) && $cond_lines > 1) {
2103 $stat_real = "[...]\n$stat_real";
2106 #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";
2108 if ($check && (($sindent % 8) != 0 ||
2109 ($sindent <= $indent && $s ne ''))) {
2110 WARN("SUSPECT_CODE_INDENT",
2111 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
2115 # Track the 'values' across context and added lines.
2116 my $opline = $line; $opline =~ s/^./ /;
2117 my ($curr_values, $curr_vars) =
2118 annotate_values($opline . "\n", $prev_values);
2119 $curr_values = $prev_values . $curr_values;
2121 my $outline = $opline; $outline =~ s/\t/ /g;
2122 print "$linenr > .$outline\n";
2123 print "$linenr > $curr_values\n";
2124 print "$linenr > $curr_vars\n";
2126 $prev_values = substr($curr_values, -1);
2128 #ignore lines not being added
2129 if ($line=~/^[^\+]/) {next;}
2131 # TEST: allow direct testing of the type matcher.
2133 if ($line =~ /^.\s*$Declare\s*$/) {
2135 "TEST: is type\n" . $herecurr);
2136 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2137 ERROR("TEST_NOT_TYPE",
2138 "TEST: is not type ($1 is)\n". $herecurr);
2142 # TEST: allow direct testing of the attribute matcher.
2144 if ($line =~ /^.\s*$Modifier\s*$/) {
2146 "TEST: is attr\n" . $herecurr);
2147 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2148 ERROR("TEST_NOT_ATTR",
2149 "TEST: is not attr ($1 is)\n". $herecurr);
2154 # check for initialisation to aggregates open brace on the next line
2155 if ($line =~ /^.\s*{/ &&
2156 $prevline =~ /(?:^|[^=])=\s*$/) {
2158 "that open brace { should be on the previous line\n" . $hereprev);
2162 # Checks which are anchored on the added line.
2165 # check for malformed paths in #include statements (uses RAW line)
2166 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2168 if ($path =~ m{//}) {
2169 ERROR("MALFORMED_INCLUDE",
2170 "malformed #include filename\n" .
2175 # no C99 // comments
2176 if ($line =~ m{//}) {
2177 ERROR("C99_COMMENTS",
2178 "do not use C99 // comments\n" . $herecurr);
2180 # Remove C99 comments.
2182 $opline =~ s@//.*@@;
2184 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2185 # the whole statement.
2186 #print "APW <$lines[$realline_next - 1]>\n";
2187 if (defined $realline_next &&
2188 exists $lines[$realline_next - 1] &&
2189 !defined $suppress_export{$realline_next} &&
2190 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2191 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2192 # Handle definitions which produce identifiers with
2195 # EXPORT_SYMBOL(something_foo);
2197 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
2198 $name =~ /^${Ident}_$2/) {
2199 #print "FOO C name<$name>\n";
2200 $suppress_export{$realline_next} = 1;
2202 } elsif ($stat !~ /(?:
2204 ^.DEFINE_$Ident\(\Q$name\E\)|
2205 ^.DECLARE_$Ident\(\Q$name\E\)|
2206 ^.LIST_HEAD\(\Q$name\E\)|
2207 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2208 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
2210 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2211 $suppress_export{$realline_next} = 2;
2213 $suppress_export{$realline_next} = 1;
2216 if (!defined $suppress_export{$linenr} &&
2217 $prevline =~ /^.\s*$/ &&
2218 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2219 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2220 #print "FOO B <$lines[$linenr - 1]>\n";
2221 $suppress_export{$linenr} = 2;
2223 if (defined $suppress_export{$linenr} &&
2224 $suppress_export{$linenr} == 2) {
2225 WARN("EXPORT_SYMBOL",
2226 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
2229 # check for global initialisers.
2230 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
2231 ERROR("GLOBAL_INITIALISERS",
2232 "do not initialise globals to 0 or NULL\n" .
2235 # check for static initialisers.
2236 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2237 ERROR("INITIALISED_STATIC",
2238 "do not initialise statics to 0 or NULL\n" .
2242 # check for static const char * arrays.
2243 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2244 WARN("STATIC_CONST_CHAR_ARRAY",
2245 "static const char * array should probably be static const char * const\n" .
2249 # check for static char foo[] = "bar" declarations.
2250 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2251 WARN("STATIC_CONST_CHAR_ARRAY",
2252 "static char array declaration should probably be static const char\n" .
2256 # check for declarations of struct pci_device_id
2257 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2258 WARN("DEFINE_PCI_DEVICE_TABLE",
2259 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
2262 # check for new typedefs, only function parameters and sparse annotations
2264 if ($line =~ /\btypedef\s/ &&
2265 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2266 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
2267 $line !~ /\b$typeTypedefs\b/ &&
2268 $line !~ /\b__bitwise(?:__|)\b/) {
2269 WARN("NEW_TYPEDEFS",
2270 "do not add new typedefs\n" . $herecurr);
2273 # * goes on variable not on type
2275 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2277 my ($from, $to) = ($2, $2);
2279 # Should start with a space.
2280 $to =~ s/^(\S)/ $1/;
2281 # Should not end with a space.
2283 # '*'s should not have spaces between.
2284 while ($to =~ s/\*\s+\*/\*\*/) {
2287 #print "from<$from> to<$to>\n";
2289 ERROR("POINTER_LOCATION",
2290 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
2293 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2295 my ($from, $to, $ident) = ($2, $2, $3);
2297 # Should start with a space.
2298 $to =~ s/^(\S)/ $1/;
2299 # Should not end with a space.
2301 # '*'s should not have spaces between.
2302 while ($to =~ s/\*\s+\*/\*\*/) {
2304 # Modifiers should have spaces.
2305 $to =~ s/(\b$Modifier$)/$1 /;
2307 #print "from<$from> to<$to> ident<$ident>\n";
2308 if ($from ne $to && $ident !~ /^$Modifier$/) {
2309 ERROR("POINTER_LOCATION",
2310 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
2314 # # no BUG() or BUG_ON()
2315 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
2316 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2317 # print "$herecurr";
2321 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2322 WARN("LINUX_VERSION_CODE",
2323 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
2326 # check for uses of printk_ratelimit
2327 if ($line =~ /\bprintk_ratelimit\s*\(/) {
2328 WARN("PRINTK_RATELIMITED",
2329 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
2332 # printk should use KERN_* levels. Note that follow on printk's on the
2333 # same line do not need a level, so we use the current block context
2334 # to try and find and validate the current printk. In summary the current
2335 # printk includes all preceding printk's which have no newline on the end.
2336 # we assume the first bad printk is the one to report.
2337 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
2339 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2340 #print "CHECK<$lines[$ln - 1]\n";
2341 # we have a preceding printk if it ends
2342 # with "\n" ignore it, else it is to blame
2343 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2344 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2351 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2352 "printk() should include KERN_ facility level\n" . $herecurr);
2356 # function brace can't be on same line, except for #defines of do while,
2357 # or if closed on same line
2358 if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and
2359 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
2361 "open brace '{' following function declarations go on the next line\n" . $herecurr);
2364 # open braces for enum, union and struct go on the same line.
2365 if ($line =~ /^.\s*{/ &&
2366 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2368 "open brace '{' following $1 go on the same line\n" . $hereprev);
2371 # missing space after union, struct or enum definition
2372 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2374 "missing space after $1 definition\n" . $herecurr);
2377 # check for spacing round square brackets; allowed:
2378 # 1. with a type on the left -- int [] a;
2379 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2380 # 3. inside a curly brace -- = { [0...10] = 5 }
2381 while ($line =~ /(.*?\s)\[/g) {
2382 my ($where, $prefix) = ($-[1], $1);
2383 if ($prefix !~ /$Type\s+$/ &&
2384 ($where != 0 || $prefix !~ /^.\s+$/) &&
2385 $prefix !~ /[{,]\s+$/) {
2386 ERROR("BRACKET_SPACE",
2387 "space prohibited before open square bracket '['\n" . $herecurr);
2391 # check for spaces between functions and their parentheses.
2392 while ($line =~ /($Ident)\s+\(/g) {
2394 my $ctx_before = substr($line, 0, $-[1]);
2395 my $ctx = "$ctx_before$name";
2397 # Ignore those directives where spaces _are_ permitted.
2399 if|for|while|switch|return|case|
2400 volatile|__volatile__|
2401 __attribute__|format|__extension__|
2405 # cpp #define statements have non-optional spaces, ie
2406 # if there is a space between the name and the open
2407 # parenthesis it is simply not a parameter group.
2408 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2410 # cpp #elif statement condition may start with a (
2411 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2413 # If this whole things ends with a type its most
2414 # likely a typedef for a function.
2415 } elsif ($ctx =~ /$Type$/) {
2419 "space prohibited between function name and open parenthesis '('\n" . $herecurr);
2422 # Check operator spacing.
2423 if (!($line=~/\#\s*include/)) {
2425 <<=|>>=|<=|>=|==|!=|
2426 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2427 =>|->|<<|>>|<|>|=|!|~|
2428 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2431 my @elements = split(/($ops|;)/, $opline);
2434 my $blank = copy_spacing($opline);
2436 for (my $n = 0; $n < $#elements; $n += 2) {
2437 $off += length($elements[$n]);
2439 # Pick up the preceding and succeeding characters.
2440 my $ca = substr($opline, 0, $off);
2442 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2443 $cc = substr($opline, $off + length($elements[$n + 1]));
2445 my $cb = "$ca$;$cc";
2448 $a = 'V' if ($elements[$n] ne '');
2449 $a = 'W' if ($elements[$n] =~ /\s$/);
2450 $a = 'C' if ($elements[$n] =~ /$;$/);
2451 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2452 $a = 'O' if ($elements[$n] eq '');
2453 $a = 'E' if ($ca =~ /^\s*$/);
2455 my $op = $elements[$n + 1];
2458 if (defined $elements[$n + 2]) {
2459 $c = 'V' if ($elements[$n + 2] ne '');
2460 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2461 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2462 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2463 $c = 'O' if ($elements[$n + 2] eq '');
2464 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2469 my $ctx = "${a}x${c}";
2471 my $at = "(ctx:$ctx)";
2473 my $ptr = substr($blank, 0, $off) . "^";
2474 my $hereptr = "$hereline$ptr\n";
2476 # Pull out the value of this operator.
2477 my $op_type = substr($curr_values, $off + 1, 1);
2479 # Get the full operator variant.
2480 my $opv = $op . substr($curr_vars, $off, 1);
2482 # Ignore operators passed as parameters.
2483 if ($op_type ne 'V' &&
2484 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2487 # } elsif ($op =~ /^$;+$/) {
2489 # ; should have either the end of line or a space or \ after it
2490 } elsif ($op eq ';') {
2491 if ($ctx !~ /.x[WEBC]/ &&
2492 $cc !~ /^\\/ && $cc !~ /^;/) {
2494 "space required after that '$op' $at\n" . $hereptr);
2498 } elsif ($op eq '//') {
2502 # : when part of a bitfield
2503 } elsif ($op eq '->' || $opv eq ':B') {
2504 if ($ctx =~ /Wx.|.xW/) {
2506 "spaces prohibited around that '$op' $at\n" . $hereptr);
2509 # , must have a space on the right.
2510 } elsif ($op eq ',') {
2511 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2513 "space required after that '$op' $at\n" . $hereptr);
2516 # '*' as part of a type definition -- reported already.
2517 } elsif ($opv eq '*_') {
2518 #warn "'*' is part of type\n";
2520 # unary operators should have a space before and
2521 # none after. May be left adjacent to another
2522 # unary operator, or a cast
2523 } elsif ($op eq '!' || $op eq '~' ||
2524 $opv eq '*U' || $opv eq '-U' ||
2525 $opv eq '&U' || $opv eq '&&U') {
2526 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2528 "space required before that '$op' $at\n" . $hereptr);
2530 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2531 # A unary '*' may be const
2533 } elsif ($ctx =~ /.xW/) {
2535 "space prohibited after that '$op' $at\n" . $hereptr);
2538 # unary ++ and unary -- are allowed no space on one side.
2539 } elsif ($op eq '++' or $op eq '--') {
2540 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2542 "space required one side of that '$op' $at\n" . $hereptr);
2544 if ($ctx =~ /Wx[BE]/ ||
2545 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2547 "space prohibited before that '$op' $at\n" . $hereptr);
2549 if ($ctx =~ /ExW/) {
2551 "space prohibited after that '$op' $at\n" . $hereptr);
2555 # << and >> may either have or not have spaces both sides
2556 } elsif ($op eq '<<' or $op eq '>>' or
2557 $op eq '&' or $op eq '^' or $op eq '|' or
2558 $op eq '+' or $op eq '-' or
2559 $op eq '*' or $op eq '/' or
2562 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2564 "need consistent spacing around '$op' $at\n" .
2568 # A colon needs no spaces before when it is
2569 # terminating a case value or a label.
2570 } elsif ($opv eq ':C' || $opv eq ':L') {
2571 if ($ctx =~ /Wx./) {
2573 "space prohibited before that '$op' $at\n" . $hereptr);
2576 # All the others need spaces both sides.
2577 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2580 # Ignore email addresses <foo@bar>
2582 $cc =~ /^\S+\@\S+>/) ||
2584 $ca =~ /<\S+\@\S+$/))
2590 if (($opv eq ':O' && $ca =~ /\?$/) ||
2591 ($op eq '?' && $cc =~ /^:/)) {
2597 "spaces required around that '$op' $at\n" . $hereptr);
2600 $off += length($elements[$n + 1]);
2604 # check for multiple assignments
2605 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2606 CHK("MULTIPLE_ASSIGNMENTS",
2607 "multiple assignments should be avoided\n" . $herecurr);
2610 ## # check for multiple declarations, allowing for a function declaration
2612 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2613 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2615 ## # Remove any bracketed sections to ensure we do not
2616 ## # falsly report the parameters of functions.
2618 ## while ($ln =~ s/\([^\(\)]*\)//g) {
2620 ## if ($ln =~ /,/) {
2621 ## WARN("MULTIPLE_DECLARATION",
2622 ## "declaring multiple variables together should be avoided\n" . $herecurr);
2626 #need space before brace following if, while, etc
2627 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
2630 "space required before the open brace '{'\n" . $herecurr);
2633 # closing brace should have a space following it when it has anything
2635 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2637 "space required after that close brace '}'\n" . $herecurr);
2640 # check spacing on square brackets
2641 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2643 "space prohibited after that open square bracket '['\n" . $herecurr);
2645 if ($line =~ /\s\]/) {
2647 "space prohibited before that close square bracket ']'\n" . $herecurr);
2650 # check spacing on parentheses
2651 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2652 $line !~ /for\s*\(\s+;/) {
2654 "space prohibited after that open parenthesis '('\n" . $herecurr);
2656 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2657 $line !~ /for\s*\(.*;\s+\)/ &&
2658 $line !~ /:\s+\)/) {
2660 "space prohibited before that close parenthesis ')'\n" . $herecurr);
2663 #goto labels aren't indented, allow a single space however
2664 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2665 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2666 WARN("INDENTED_LABEL",
2667 "labels should not be indented\n" . $herecurr);
2670 # Return is not a function.
2671 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2675 # Flatten any parentheses
2676 $value =~ s/\(/ \(/g;
2677 $value =~ s/\)/\) /g;
2678 while ($value =~ s/\[[^\[\]]*\]/1/ ||
2679 $value !~ /(?:$Ident|-?$Constant)\s*
2681 (?:$Ident|-?$Constant)/x &&
2682 $value =~ s/\([^\(\)]*\)/1/) {
2684 #print "value<$value>\n";
2685 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2686 ERROR("RETURN_PARENTHESES",
2687 "return is not a function, parentheses are not required\n" . $herecurr);
2689 } elsif ($spacing !~ /\s+/) {
2691 "space required before the open parenthesis '('\n" . $herecurr);
2694 # Return of what appears to be an errno should normally be -'ve
2695 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2697 if ($name ne 'EOF' && $name ne 'ERROR') {
2698 WARN("USE_NEGATIVE_ERRNO",
2699 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2703 # Need a space before open parenthesis after if, while etc
2704 if ($line=~/\b(if|while|for|switch)\(/) {
2705 ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
2708 # Check for illegal assignment in if conditional -- and check for trailing
2709 # statements after the conditional.
2710 if ($line =~ /do\s*(?!{)/) {
2711 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2712 ctx_statement_block($linenr, $realcnt, 0)
2713 if (!defined $stat);
2714 my ($stat_next) = ctx_statement_block($line_nr_next,
2715 $remain_next, $off_next);
2716 $stat_next =~ s/\n./\n /g;
2717 ##print "stat<$stat> stat_next<$stat_next>\n";
2719 if ($stat_next =~ /^\s*while\b/) {
2720 # If the statement carries leading newlines,
2721 # then count those as offsets.
2723 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2725 statement_rawlines($whitespace) - 1;
2727 $suppress_whiletrailers{$line_nr_next +
2731 if (!defined $suppress_whiletrailers{$linenr} &&
2732 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2733 my ($s, $c) = ($stat, $cond);
2735 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2736 ERROR("ASSIGN_IN_IF",
2737 "do not use assignment in if condition\n" . $herecurr);
2740 # Find out what is on the end of the line after the
2742 substr($s, 0, length($c), '');
2744 $s =~ s/$;//g; # Remove any comments
2745 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2746 $c !~ /}\s*while\s*/)
2748 # Find out how long the conditional actually is.
2749 my @newlines = ($c =~ /\n/gs);
2750 my $cond_lines = 1 + $#newlines;
2753 $stat_real = raw_line($linenr, $cond_lines)
2754 . "\n" if ($cond_lines);
2755 if (defined($stat_real) && $cond_lines > 1) {
2756 $stat_real = "[...]\n$stat_real";
2759 ERROR("TRAILING_STATEMENTS",
2760 "trailing statements should be on next line\n" . $herecurr . $stat_real);
2764 # Check for bitwise tests written as boolean
2776 WARN("HEXADECIMAL_BOOLEAN_TEST",
2777 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2780 # if and else should not have general statements after it
2781 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2783 $s =~ s/$;//g; # Remove any comments
2784 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2785 ERROR("TRAILING_STATEMENTS",
2786 "trailing statements should be on next line\n" . $herecurr);
2789 # if should not continue a brace
2790 if ($line =~ /}\s*if\b/) {
2791 ERROR("TRAILING_STATEMENTS",
2792 "trailing statements should be on next line\n" .
2795 # case and default should not have general statements after them
2796 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2798 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2802 ERROR("TRAILING_STATEMENTS",
2803 "trailing statements should be on next line\n" . $herecurr);
2806 # Check for }<nl>else {, these must be at the same
2807 # indent level to be relevant to each other.
2808 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2809 $previndent == $indent) {
2810 ERROR("ELSE_AFTER_BRACE",
2811 "else should follow close brace '}'\n" . $hereprev);
2814 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2815 $previndent == $indent) {
2816 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2818 # Find out what is on the end of the line after the
2820 substr($s, 0, length($c), '');
2823 if ($s =~ /^\s*;/) {
2824 ERROR("WHILE_AFTER_BRACE",
2825 "while should follow close brace '}'\n" . $hereprev);
2829 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2830 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2831 # print "No studly caps, use _\n";
2832 # print "$herecurr";
2836 #no spaces allowed after \ in define
2837 if ($line=~/\#\s*define.*\\\s$/) {
2838 WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
2839 "Whitepspace after \\ makes next lines useless\n" . $herecurr);
2842 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2843 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2845 my $checkfile = "include/linux/$file";
2846 if (-f "$root/$checkfile" &&
2847 $realfile ne $checkfile &&
2848 $1 !~ /$allowed_asm_includes/)
2850 if ($realfile =~ m{^arch/}) {
2851 CHK("ARCH_INCLUDE_LINUX",
2852 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2854 WARN("INCLUDE_LINUX",
2855 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2860 # multi-statement macros should be enclosed in a do while loop, grab the
2861 # first statement and ensure its the whole macro if its not enclosed
2862 # in a known good container
2863 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2864 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2867 my ($off, $dstat, $dcond, $rest);
2869 ($dstat, $dcond, $ln, $cnt, $off) =
2870 ctx_statement_block($linenr, $realcnt, 0);
2872 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2873 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2875 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
2877 $dstat =~ s/\\\n.//g;
2878 $dstat =~ s/^\s*//s;
2879 $dstat =~ s/\s*$//s;
2881 # Flatten any parentheses and braces
2882 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2883 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2884 $dstat =~ s/\[[^\[\]]*\]/1/)
2888 # Flatten any obvious string concatentation.
2889 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
2890 $dstat =~ s/$Ident\s*("X*")/$1/)
2894 my $exceptions = qr{
2906 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2908 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
2909 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
2910 $dstat !~ /^[!~-]?(?:$Ident|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo
2911 $dstat !~ /^'X'$/ && # character constants
2912 $dstat !~ /$exceptions/ &&
2913 $dstat !~ /^\.$Ident\s*=/ && # .foo =
2914 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
2915 $dstat !~ /^for\s*$Constant$/ && # for (...)
2916 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
2917 $dstat !~ /^do\s*{/ && # do {...
2918 $dstat !~ /^\(\{/) # ({...
2921 my $herectx = $here . "\n";
2922 my $cnt = statement_rawlines($ctx);
2924 for (my $n = 0; $n < $cnt; $n++) {
2925 $herectx .= raw_line($linenr, $n) . "\n";
2928 if ($dstat =~ /;/) {
2929 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
2930 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
2932 ERROR("COMPLEX_MACRO",
2933 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
2938 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2939 # all assignments may have only one of the following with an assignment:
2942 # VMLINUX_SYMBOL(...)
2943 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2944 WARN("MISSING_VMLINUX_SYMBOL",
2945 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2948 # check for redundant bracing round if etc
2949 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2950 my ($level, $endln, @chunks) =
2951 ctx_statement_full($linenr, $realcnt, 1);
2952 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2953 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2954 if ($#chunks > 0 && $level == 0) {
2958 my $herectx = $here . "\n";
2959 my $ln = $linenr - 1;
2960 for my $chunk (@chunks) {
2961 my ($cond, $block) = @{$chunk};
2963 # If the condition carries leading newlines, then count those as offsets.
2964 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2965 my $offset = statement_rawlines($whitespace) - 1;
2967 $allowed[$allow] = 0;
2968 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2970 # We have looked at and allowed this specific line.
2971 $suppress_ifbraces{$ln + $offset} = 1;
2973 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2974 $ln += statement_rawlines($block) - 1;
2976 substr($block, 0, length($cond), '');
2978 $seen++ if ($block =~ /^\s*{/);
2980 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
2981 if (statement_lines($cond) > 1) {
2982 #print "APW: ALLOWED: cond<$cond>\n";
2983 $allowed[$allow] = 1;
2985 if ($block =~/\b(?:if|for|while)\b/) {
2986 #print "APW: ALLOWED: block<$block>\n";
2987 $allowed[$allow] = 1;
2989 if (statement_block_size($block) > 1) {
2990 #print "APW: ALLOWED: lines block<$block>\n";
2991 $allowed[$allow] = 1;
2996 my $sum_allowed = 0;
2997 foreach (@allowed) {
3000 if ($sum_allowed == 0) {
3002 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3003 } elsif ($sum_allowed != $allow &&
3006 "braces {} should be used on all arms of this statement\n" . $herectx);
3011 if (!defined $suppress_ifbraces{$linenr - 1} &&
3012 $line =~ /\b(if|while|for|else)\b/) {
3015 # Check the pre-context.
3016 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3017 #print "APW: ALLOWED: pre<$1>\n";
3021 my ($level, $endln, @chunks) =
3022 ctx_statement_full($linenr, $realcnt, $-[0]);
3024 # Check the condition.
3025 my ($cond, $block) = @{$chunks[0]};
3026 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3027 if (defined $cond) {
3028 substr($block, 0, length($cond), '');
3030 if (statement_lines($cond) > 1) {
3031 #print "APW: ALLOWED: cond<$cond>\n";
3034 if ($block =~/\b(?:if|for|while)\b/) {
3035 #print "APW: ALLOWED: block<$block>\n";
3038 if (statement_block_size($block) > 1) {
3039 #print "APW: ALLOWED: lines block<$block>\n";
3042 # Check the post-context.
3043 if (defined $chunks[1]) {
3044 my ($cond, $block) = @{$chunks[1]};
3045 if (defined $cond) {
3046 substr($block, 0, length($cond), '');
3048 if ($block =~ /^\s*\{/) {
3049 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3053 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
3054 my $herectx = $here . "\n";
3055 my $cnt = statement_rawlines($block);
3057 for (my $n = 0; $n < $cnt; $n++) {
3058 $herectx .= raw_line($linenr, $n) . "\n";
3062 "braces {} are not necessary for single statement blocks\n" . $herectx);
3066 # no volatiles please
3067 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3068 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3070 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
3074 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3075 CHK("REDUNDANT_CODE",
3076 "if this code is redundant consider removing it\n" .
3080 # check for needless kfree() checks
3081 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3083 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
3084 WARN("NEEDLESS_KFREE",
3085 "kfree(NULL) is safe this check is probably not required\n" . $hereprev);
3088 # check for needless usb_free_urb() checks
3089 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3091 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
3092 WARN("NEEDLESS_USB_FREE_URB",
3093 "usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
3097 # prefer usleep_range over udelay
3098 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
3099 # ignore udelay's < 10, however
3100 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
3102 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
3106 # warn about unexpectedly long msleep's
3107 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3110 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
3114 # warn about #ifdefs in C files
3115 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
3116 # print "#ifdef in C files should be avoided\n";
3117 # print "$herecurr";
3121 # warn about spacing in #ifdefs
3122 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3124 "exactly one space required after that #$1\n" . $herecurr);
3127 # check for spinlock_t definitions without a comment.
3128 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3129 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
3131 if (!ctx_has_comment($first_line, $linenr)) {
3132 CHK("UNCOMMENTED_DEFINITION",
3133 "$1 definition without comment\n" . $herecurr);
3136 # check for memory barriers without a comment.
3137 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3138 if (!ctx_has_comment($first_line, $linenr)) {
3139 CHK("MEMORY_BARRIER",
3140 "memory barrier without comment\n" . $herecurr);
3143 # check of hardware specific defines
3144 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
3146 "architecture specific defines should be avoided\n" . $herecurr);
3149 # Check that the storage class is at the beginning of a declaration
3150 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3151 WARN("STORAGE_CLASS",
3152 "storage class should be at the beginning of the declaration\n" . $herecurr)
3155 # check the location of the inline attribute, that it is between
3156 # storage class and type.
3157 if ($line =~ /\b$Type\s+$Inline\b/ ||
3158 $line =~ /\b$Inline\s+$Storage\b/) {
3159 ERROR("INLINE_LOCATION",
3160 "inline keyword should sit between storage class and type\n" . $herecurr);
3163 # Check for __inline__ and __inline, prefer inline
3164 if ($line =~ /\b(__inline__|__inline)\b/) {
3166 "plain inline is preferred over $1\n" . $herecurr);
3169 # Check for __attribute__ packed, prefer __packed
3170 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3171 WARN("PREFER_PACKED",
3172 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
3175 # Check for __attribute__ aligned, prefer __aligned
3176 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3177 WARN("PREFER_ALIGNED",
3178 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
3181 # Check for __attribute__ format(printf, prefer __printf
3182 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3183 WARN("PREFER_PRINTF",
3184 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3187 # Check for __attribute__ format(scanf, prefer __scanf
3188 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3189 WARN("PREFER_SCANF",
3190 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
3193 # check for sizeof(&)
3194 if ($line =~ /\bsizeof\s*\(\s*\&/) {
3195 WARN("SIZEOF_ADDRESS",
3196 "sizeof(& should be avoided\n" . $herecurr);
3199 # check for line continuations in quoted strings with odd counts of "
3200 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3201 WARN("LINE_CONTINUATIONS",
3202 "Avoid line continuations in quoted strings\n" . $herecurr);
3205 # Check for misused memsets
3206 if ($^V && $^V ge 5.10.0 &&
3208 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3214 if ($ms_size =~ /^(0x|)0$/i) {
3216 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
3217 } elsif ($ms_size =~ /^(0x|)1$/i) {
3219 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3223 # typecasts on min/max could be min_t/max_t
3224 if ($^V && $^V ge 5.10.0 &&
3226 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3227 if (defined $2 || defined $7) {
3229 my $cast1 = deparenthesize($2);
3231 my $cast2 = deparenthesize($7);
3235 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
3236 $cast = "$cast1 or $cast2";
3237 } elsif ($cast1 ne "") {
3243 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
3247 # check for new externs in .c files.
3248 if ($realfile =~ /\.c$/ && defined $stat &&
3249 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
3251 my $function_name = $1;
3252 my $paren_space = $2;
3255 if (defined $cond) {
3256 substr($s, 0, length($cond), '');
3258 if ($s =~ /^\s*;/ &&
3259 $function_name ne 'uninitialized_var')
3261 WARN("AVOID_EXTERNS",
3262 "externs should be avoided in .c files\n" . $herecurr);
3265 if ($paren_space =~ /\n/) {
3266 WARN("FUNCTION_ARGUMENTS",
3267 "arguments for function declarations should follow identifier\n" . $herecurr);
3270 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3271 $stat =~ /^.\s*extern\s+/)
3273 WARN("AVOID_EXTERNS",
3274 "externs should be avoided in .c files\n" . $herecurr);
3277 # check for pointless casting of kmalloc return
3278 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
3279 WARN("UNNECESSARY_CASTS",
3280 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
3283 # check for multiple semicolons
3284 if ($line =~ /;\s*;\s*$/) {
3285 WARN("ONE_SEMICOLON",
3286 "Statements terminations use 1 semicolon\n" . $herecurr);
3289 # check for gcc specific __FUNCTION__
3290 if ($line =~ /__FUNCTION__/) {
3292 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
3295 # check for use of yield()
3296 if ($line =~ /\byield\s*\(\s*\)/) {
3298 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3301 # check for semaphores initialized locked
3302 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
3303 WARN("CONSIDER_COMPLETION",
3304 "consider using a completion\n" . $herecurr);
3307 # recommend kstrto* over simple_strto* and strict_strto*
3308 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
3309 WARN("CONSIDER_KSTRTO",
3310 "$1 is obsolete, use k$3 instead\n" . $herecurr);
3313 # check for __initcall(), use device_initcall() explicitly please
3314 if ($line =~ /^.\s*__initcall\s*\(/) {
3315 WARN("USE_DEVICE_INITCALL",
3316 "please use device_initcall() instead of __initcall()\n" . $herecurr);
3319 # check for various ops structs, ensure they are const.
3320 my $struct_ops = qr{acpi_dock_ops|
3321 address_space_operations|
3323 block_device_operations|
3328 file_lock_operations|
3338 lock_manager_operations|
3344 pipe_buf_operations|
3345 platform_hibernation_ops|
3346 platform_suspend_ops|
3351 soc_pcmcia_socket_ops|
3357 if ($line !~ /\bconst\b/ &&
3358 $line =~ /\bstruct\s+($struct_ops)\b/) {
3359 WARN("CONST_STRUCT",
3360 "struct $1 should normally be const\n" .
3364 # use of NR_CPUS is usually wrong
3365 # ignore definitions of NR_CPUS and usage to define arrays as likely right
3366 if ($line =~ /\bNR_CPUS\b/ &&
3367 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3368 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
3369 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3370 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3371 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
3374 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3377 # check for %L{u,d,i} in strings
3379 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3380 $string = substr($rawline, $-[1], $+[1] - $-[1]);
3381 $string =~ s/%%/__/g;
3382 if ($string =~ /(?<!%)%L[udi]/) {
3384 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
3389 # whine mightly about in_atomic
3390 if ($line =~ /\bin_atomic\s*\(/) {
3391 if ($realfile =~ m@^drivers/@) {
3393 "do not use in_atomic in drivers\n" . $herecurr);
3394 } elsif ($realfile !~ m@^kernel/@) {
3396 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
3400 # check for lockdep_set_novalidate_class
3401 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3402 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3403 if ($realfile !~ m@^kernel/lockdep@ &&
3404 $realfile !~ m@^include/linux/lockdep@ &&
3405 $realfile !~ m@^drivers/base/core@) {
3407 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
3411 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3412 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
3413 WARN("EXPORTED_WORLD_WRITABLE",
3414 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
3418 # If we have no input at all, then there is nothing to report on
3419 # so just keep quiet.
3420 if ($#rawlines == -1) {
3424 # In mailback mode only produce a report in the negative, for
3425 # things that appear to be patches.
3426 if ($mailback && ($clean == 1 || !$is_patch)) {
3430 # This is not a patch, and we are are in 'no-patch' mode so
3432 if (!$chk_patch && !$is_patch) {
3437 ERROR("NOT_UNIFIED_DIFF",
3438 "Does not appear to be a unified-diff format patch\n");
3440 if ($is_patch && $chk_signoff && $signoff == 0) {
3441 ERROR("MISSING_SIGN_OFF",
3442 "Missing Signed-off-by: line(s)\n");
3445 print report_dump();
3446 if ($summary && !($clean == 1 && $quiet == 1)) {
3447 print "$filename " if ($summary_file);
3448 print "total: $cnt_error errors, $cnt_warn warnings, " .
3449 (($check)? "$cnt_chk checks, " : "") .
3450 "$cnt_lines lines checked\n";
3451 print "\n" if ($quiet == 0);
3456 if ($^V lt 5.10.0) {
3457 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
3458 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
3461 # If there were whitespace errors which cleanpatch can fix
3462 # then suggest that.
3463 if ($rpt_cleaners) {
3464 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3465 print " scripts/cleanfile\n\n";
3470 if ($quiet == 0 && keys %ignore_type) {
3471 print "NOTE: Ignored message types:";
3472 foreach my $ignore (sort keys %ignore_type) {
3478 if ($clean == 1 && $quiet == 0) {
3479 print "$vname has no obvious style problems and is ready for submission.\n"
3481 if ($clean == 0 && $quiet == 0) {
3483 $vname has style problems, please review.
3485 If any of these errors are false positives, please report
3486 them to the maintainer, see CHECKPATCH in MAINTAINERS.