2 # (c) 2001, Dave Jones. (the file handling bit)
3 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4 # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5 # (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
6 # Licensed under the terms of the GNU GPL License version 2
15 use Getopt::Long qw(:config no_auto_abbrev);
35 my $configuration_file = ".checkpatch.conf";
36 my $max_line_length = 80;
42 Usage: $P [OPTION]... [FILE]...
47 --no-tree run without a kernel tree
48 --no-signoff do not check for 'Signed-off-by' line
49 --patch treat FILE as patchfile (default)
50 --emacs emacs compile window format
51 --terse one line per report
52 -f, --file treat FILE as regular source file
53 --subjective, --strict enable more subjective tests
54 --ignore TYPE(,TYPE2...) ignore various comma separated message types
55 --max-line-length=n set the maximum line length, if exceeded, warn
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,
112 'max-line-length=i' => \$max_line_length,
114 'summary!' => \$summary,
115 'mailback!' => \$mailback,
116 'summary-file!' => \$summary_file,
118 'debug=s' => \%debug,
119 'test-only=s' => \$tst_only,
129 print "$P: no input files\n";
133 @ignore = split(/,/, join(',',@ignore));
134 foreach my $word (@ignore) {
135 $word =~ s/\s*\n?$//g;
138 $word =~ tr/[a-z]/[A-Z]/;
140 next if ($word =~ m/^\s*#/);
141 next if ($word =~ m/^\s*$/);
143 $ignore_type{$word}++;
147 my $dbg_possible = 0;
150 for my $key (keys %debug) {
152 eval "\${dbg_$key} = '$debug{$key}';";
156 my $rpt_cleaners = 0;
165 if (!top_of_kernel_tree($root)) {
166 die "$P: $root: --root does not point at a valid tree\n";
169 if (top_of_kernel_tree('.')) {
171 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
172 top_of_kernel_tree($1)) {
177 if (!defined $root) {
178 print "Must be run from the top-level dir. of a kernel tree\n";
183 my $emitted_corrupt = 0;
186 [A-Za-z_][A-Za-z\d_]*
187 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
189 our $Storage = qr{extern|static|asmlinkage};
202 # Notes to $Attribute:
203 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
222 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
223 ____cacheline_aligned|
224 ____cacheline_aligned_in_smp|
225 ____cacheline_internodealigned_in_smp|
229 our $Inline = qr{inline|__always_inline|noinline};
230 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
231 our $Lval = qr{$Ident(?:$Member)*};
233 our $Constant = qr{(?i:(?:[0-9]+|0x[0-9a-f]+)[ul]*)};
234 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
235 our $Compare = qr{<=|>=|==|!=|<|>};
239 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
246 our $NON_ASCII_UTF8 = qr{
247 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
248 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
249 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
250 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
251 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
252 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
253 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
257 [\x09\x0A\x0D\x20-\x7E] # ASCII
261 our $typeTypedefs = qr{(?x:
262 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
266 our $logFunctions = qr{(?x:
267 printk(?:_ratelimited|_once|)|
268 [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
269 WARN(?:_RATELIMIT|_ONCE|)|
274 our $signature_tags = qr{(?xi:
286 qr{(?:unsigned\s+)?char},
287 qr{(?:unsigned\s+)?short},
288 qr{(?:unsigned\s+)?int},
289 qr{(?:unsigned\s+)?long},
290 qr{(?:unsigned\s+)?long\s+int},
291 qr{(?:unsigned\s+)?long\s+long},
292 qr{(?:unsigned\s+)?long\s+long\s+int},
301 qr{${Ident}_handler},
302 qr{${Ident}_handler_fn},
304 our @modifierList = (
308 our $allowed_asm_includes = qr{(?x:
312 # memory.h: ARM has a custom one
315 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
316 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
317 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
319 (?:$Modifier\s+|const\s+)*
321 (?:typeof|__typeof__)\s*\([^\)]*\)|
325 (?:\s+$Modifier|\s+const)*
329 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
330 (?:\s+$Inline|\s+$Modifier)*
332 $Declare = qr{(?:$Storage\s+)?$Type};
337 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
339 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
340 # requires at least perl version v5.10.0
341 # Any use must be runtime checked with $^V
343 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
344 our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
345 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
349 return "" if (!defined($string));
350 $string =~ s@^\s*\(\s*@@g;
351 $string =~ s@\s*\)\s*$@@g;
352 $string =~ s@\s+@ @g;
356 $chk_signoff = 0 if ($file);
361 for my $filename (@ARGV) {
364 open($FILE, '-|', "diff -u /dev/null $filename") ||
365 die "$P: $filename: diff failed - $!\n";
366 } elsif ($filename eq '-') {
367 open($FILE, '<&STDIN');
369 open($FILE, '<', "$filename") ||
370 die "$P: $filename: open failed - $!\n";
372 if ($filename eq '-') {
373 $vname = 'Your patch';
382 if (!process($filename)) {
391 sub top_of_kernel_tree {
395 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
396 "README", "Documentation", "arch", "include", "drivers",
397 "fs", "init", "ipc", "kernel", "lib", "scripts",
400 foreach my $check (@tree_check) {
401 if (! -e $root . '/' . $check) {
409 my ($formatted_email) = @_;
415 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
418 $comment = $3 if defined $3;
419 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
421 $comment = $2 if defined $2;
422 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
424 $comment = $2 if defined $2;
425 $formatted_email =~ s/$address.*$//;
426 $name = $formatted_email;
427 $name =~ s/^\s+|\s+$//g;
428 $name =~ s/^\"|\"$//g;
429 # If there's a name left after stripping spaces and
430 # leading quotes, and the address doesn't have both
431 # leading and trailing angle brackets, the address
433 # "joe smith joe@smith.com" bad
434 # "joe smith <joe@smith.com" bad
435 if ($name ne "" && $address !~ /^<[^>]+>$/) {
442 $name =~ s/^\s+|\s+$//g;
443 $name =~ s/^\"|\"$//g;
444 $address =~ s/^\s+|\s+$//g;
445 $address =~ s/^\<|\>$//g;
447 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
448 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
452 return ($name, $address, $comment);
456 my ($name, $address) = @_;
460 $name =~ s/^\s+|\s+$//g;
461 $name =~ s/^\"|\"$//g;
462 $address =~ s/^\s+|\s+$//g;
464 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
465 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
470 $formatted_email = "$address";
472 $formatted_email = "$name <$address>";
475 return $formatted_email;
481 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
482 if (-e "$path/$conf") {
483 return "$path/$conf";
495 for my $c (split(//, $str)) {
499 for (; ($n % 8) != 0; $n++) {
511 (my $res = shift) =~ tr/\t/ /c;
518 # Drop the diff line leader and expand tabs
520 $line = expand_tabs($line);
522 # Pick the indent from the front of the line.
523 my ($white) = ($line =~ /^(\s*)/);
525 return (length($line), length($white));
528 my $sanitise_quote = '';
530 sub sanitise_line_reset {
531 my ($in_comment) = @_;
534 $sanitise_quote = '*/';
536 $sanitise_quote = '';
549 # Always copy over the diff marker.
550 $res = substr($line, 0, 1);
552 for ($off = 1; $off < length($line); $off++) {
553 $c = substr($line, $off, 1);
555 # Comments we are wacking completly including the begin
556 # and end, all to $;.
557 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
558 $sanitise_quote = '*/';
560 substr($res, $off, 2, "$;$;");
564 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
565 $sanitise_quote = '';
566 substr($res, $off, 2, "$;$;");
570 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
571 $sanitise_quote = '//';
573 substr($res, $off, 2, $sanitise_quote);
578 # A \ in a string means ignore the next character.
579 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
581 substr($res, $off, 2, 'XX');
586 if ($c eq "'" || $c eq '"') {
587 if ($sanitise_quote eq '') {
588 $sanitise_quote = $c;
590 substr($res, $off, 1, $c);
592 } elsif ($sanitise_quote eq $c) {
593 $sanitise_quote = '';
597 #print "c<$c> SQ<$sanitise_quote>\n";
598 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
599 substr($res, $off, 1, $;);
600 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
601 substr($res, $off, 1, $;);
602 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
603 substr($res, $off, 1, 'X');
605 substr($res, $off, 1, $c);
609 if ($sanitise_quote eq '//') {
610 $sanitise_quote = '';
613 # The pathname on a #include may be surrounded by '<' and '>'.
614 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
615 my $clean = 'X' x length($1);
616 $res =~ s@\<.*\>@<$clean>@;
618 # The whole of a #error is a string.
619 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
620 my $clean = 'X' x length($1);
621 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
627 sub ctx_statement_block {
628 my ($linenr, $remain, $off) = @_;
629 my $line = $linenr - 1;
646 @stack = (['', 0]) if ($#stack == -1);
648 #warn "CSB: blk<$blk> remain<$remain>\n";
649 # If we are about to drop off the end, pull in more
652 for (; $remain > 0; $line++) {
653 last if (!defined $lines[$line]);
654 next if ($lines[$line] =~ /^-/);
657 $blk .= $lines[$line] . "\n";
662 # Bail if there is no further context.
663 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
667 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
673 $c = substr($blk, $off, 1);
674 $remainder = substr($blk, $off);
676 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
678 # Handle nested #if/#else.
679 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
680 push(@stack, [ $type, $level ]);
681 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
682 ($type, $level) = @{$stack[$#stack - 1]};
683 } elsif ($remainder =~ /^#\s*endif\b/) {
684 ($type, $level) = @{pop(@stack)};
687 # Statement ends at the ';' or a close '}' at the
689 if ($level == 0 && $c eq ';') {
693 # An else is really a conditional as long as its not else if
694 if ($level == 0 && $coff_set == 0 &&
695 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
696 $remainder =~ /^(else)(?:\s|{)/ &&
697 $remainder !~ /^else\s+if\b/) {
698 $coff = $off + length($1) - 1;
700 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
701 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
704 if (($type eq '' || $type eq '(') && $c eq '(') {
708 if ($type eq '(' && $c eq ')') {
710 $type = ($level != 0)? '(' : '';
712 if ($level == 0 && $coff < $soff) {
715 #warn "CSB: mark coff<$coff>\n";
718 if (($type eq '' || $type eq '{') && $c eq '{') {
722 if ($type eq '{' && $c eq '}') {
724 $type = ($level != 0)? '{' : '';
727 if (substr($blk, $off + 1, 1) eq ';') {
733 # Preprocessor commands end at the newline unless escaped.
734 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
742 # We are truly at the end, so shuffle to the next line.
749 my $statement = substr($blk, $soff, $off - $soff + 1);
750 my $condition = substr($blk, $soff, $coff - $soff + 1);
752 #warn "STATEMENT<$statement>\n";
753 #warn "CONDITION<$condition>\n";
755 #print "coff<$coff> soff<$off> loff<$loff>\n";
757 return ($statement, $condition,
758 $line, $remain + 1, $off - $loff + 1, $level);
761 sub statement_lines {
764 # Strip the diff line prefixes and rip blank lines at start and end.
765 $stmt =~ s/(^|\n)./$1/g;
769 my @stmt_lines = ($stmt =~ /\n/g);
771 return $#stmt_lines + 2;
774 sub statement_rawlines {
777 my @stmt_lines = ($stmt =~ /\n/g);
779 return $#stmt_lines + 2;
782 sub statement_block_size {
785 $stmt =~ s/(^|\n)./$1/g;
791 my @stmt_lines = ($stmt =~ /\n/g);
792 my @stmt_statements = ($stmt =~ /;/g);
794 my $stmt_lines = $#stmt_lines + 2;
795 my $stmt_statements = $#stmt_statements + 1;
797 if ($stmt_lines > $stmt_statements) {
800 return $stmt_statements;
804 sub ctx_statement_full {
805 my ($linenr, $remain, $off) = @_;
806 my ($statement, $condition, $level);
810 # Grab the first conditional/block pair.
811 ($statement, $condition, $linenr, $remain, $off, $level) =
812 ctx_statement_block($linenr, $remain, $off);
813 #print "F: c<$condition> s<$statement> remain<$remain>\n";
814 push(@chunks, [ $condition, $statement ]);
815 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
816 return ($level, $linenr, @chunks);
819 # Pull in the following conditional/block pairs and see if they
820 # could continue the statement.
822 ($statement, $condition, $linenr, $remain, $off, $level) =
823 ctx_statement_block($linenr, $remain, $off);
824 #print "C: c<$condition> s<$statement> remain<$remain>\n";
825 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
827 push(@chunks, [ $condition, $statement ]);
830 return ($level, $linenr, @chunks);
834 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
836 my $start = $linenr - 1;
843 my @stack = ($level);
844 for ($line = $start; $remain > 0; $line++) {
845 next if ($rawlines[$line] =~ /^-/);
848 $blk .= $rawlines[$line];
850 # Handle nested #if/#else.
851 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
852 push(@stack, $level);
853 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
854 $level = $stack[$#stack - 1];
855 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
856 $level = pop(@stack);
859 foreach my $c (split(//, $lines[$line])) {
860 ##print "C<$c>L<$level><$open$close>O<$off>\n";
866 if ($c eq $close && $level > 0) {
868 last if ($level == 0);
869 } elsif ($c eq $open) {
874 if (!$outer || $level <= 1) {
875 push(@res, $rawlines[$line]);
878 last if ($level == 0);
881 return ($level, @res);
883 sub ctx_block_outer {
884 my ($linenr, $remain) = @_;
886 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
890 my ($linenr, $remain) = @_;
892 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
896 my ($linenr, $remain, $off) = @_;
898 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
901 sub ctx_block_level {
902 my ($linenr, $remain) = @_;
904 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
906 sub ctx_statement_level {
907 my ($linenr, $remain, $off) = @_;
909 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
912 sub ctx_locate_comment {
913 my ($first_line, $end_line) = @_;
915 # Catch a comment on the end of the line itself.
916 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
917 return $current_comment if (defined $current_comment);
919 # Look through the context and try and figure out if there is a
922 $current_comment = '';
923 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
924 my $line = $rawlines[$linenr - 1];
926 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
929 if ($line =~ m@/\*@) {
932 if (!$in_comment && $current_comment ne '') {
933 $current_comment = '';
935 $current_comment .= $line . "\n" if ($in_comment);
936 if ($line =~ m@\*/@) {
941 chomp($current_comment);
942 return($current_comment);
944 sub ctx_has_comment {
945 my ($first_line, $end_line) = @_;
946 my $cmt = ctx_locate_comment($first_line, $end_line);
948 ##print "LINE: $rawlines[$end_line - 1 ]\n";
949 ##print "CMMT: $cmt\n";
955 my ($linenr, $cnt) = @_;
957 my $offset = $linenr - 1;
962 $line = $rawlines[$offset++];
963 next if (defined($line) && $line =~ /^-/);
975 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
978 $coded = sprintf("^%c", unpack('C', $2) + 64);
987 my $av_preprocessor = 0;
993 $av_preprocessor = 0;
995 @av_paren_type = ('E');
996 $av_pend_colon = 'O';
999 sub annotate_values {
1000 my ($stream, $type) = @_;
1003 my $var = '_' x length($stream);
1006 print "$stream\n" if ($dbg_values > 1);
1008 while (length($cur)) {
1009 @av_paren_type = ('E') if ($#av_paren_type < 0);
1010 print " <" . join('', @av_paren_type) .
1011 "> <$type> <$av_pending>" if ($dbg_values > 1);
1012 if ($cur =~ /^(\s+)/o) {
1013 print "WS($1)\n" if ($dbg_values > 1);
1014 if ($1 =~ /\n/ && $av_preprocessor) {
1015 $type = pop(@av_paren_type);
1016 $av_preprocessor = 0;
1019 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1020 print "CAST($1)\n" if ($dbg_values > 1);
1021 push(@av_paren_type, $type);
1024 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1025 print "DECLARE($1)\n" if ($dbg_values > 1);
1028 } elsif ($cur =~ /^($Modifier)\s*/) {
1029 print "MODIFIER($1)\n" if ($dbg_values > 1);
1032 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1033 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1034 $av_preprocessor = 1;
1035 push(@av_paren_type, $type);
1041 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1042 print "UNDEF($1)\n" if ($dbg_values > 1);
1043 $av_preprocessor = 1;
1044 push(@av_paren_type, $type);
1046 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1047 print "PRE_START($1)\n" if ($dbg_values > 1);
1048 $av_preprocessor = 1;
1050 push(@av_paren_type, $type);
1051 push(@av_paren_type, $type);
1054 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1055 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1056 $av_preprocessor = 1;
1058 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1062 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1063 print "PRE_END($1)\n" if ($dbg_values > 1);
1065 $av_preprocessor = 1;
1067 # Assume all arms of the conditional end as this
1068 # one does, and continue as if the #endif was not here.
1069 pop(@av_paren_type);
1070 push(@av_paren_type, $type);
1073 } elsif ($cur =~ /^(\\\n)/o) {
1074 print "PRECONT($1)\n" if ($dbg_values > 1);
1076 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1077 print "ATTR($1)\n" if ($dbg_values > 1);
1078 $av_pending = $type;
1081 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1082 print "SIZEOF($1)\n" if ($dbg_values > 1);
1088 } elsif ($cur =~ /^(if|while|for)\b/o) {
1089 print "COND($1)\n" if ($dbg_values > 1);
1093 } elsif ($cur =~/^(case)/o) {
1094 print "CASE($1)\n" if ($dbg_values > 1);
1095 $av_pend_colon = 'C';
1098 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1099 print "KEYWORD($1)\n" if ($dbg_values > 1);
1102 } elsif ($cur =~ /^(\()/o) {
1103 print "PAREN('$1')\n" if ($dbg_values > 1);
1104 push(@av_paren_type, $av_pending);
1108 } elsif ($cur =~ /^(\))/o) {
1109 my $new_type = pop(@av_paren_type);
1110 if ($new_type ne '_') {
1112 print "PAREN('$1') -> $type\n"
1113 if ($dbg_values > 1);
1115 print "PAREN('$1')\n" if ($dbg_values > 1);
1118 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1119 print "FUNC($1)\n" if ($dbg_values > 1);
1123 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1124 if (defined $2 && $type eq 'C' || $type eq 'T') {
1125 $av_pend_colon = 'B';
1126 } elsif ($type eq 'E') {
1127 $av_pend_colon = 'L';
1129 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1132 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1133 print "IDENT($1)\n" if ($dbg_values > 1);
1136 } elsif ($cur =~ /^($Assignment)/o) {
1137 print "ASSIGN($1)\n" if ($dbg_values > 1);
1140 } elsif ($cur =~/^(;|{|})/) {
1141 print "END($1)\n" if ($dbg_values > 1);
1143 $av_pend_colon = 'O';
1145 } elsif ($cur =~/^(,)/) {
1146 print "COMMA($1)\n" if ($dbg_values > 1);
1149 } elsif ($cur =~ /^(\?)/o) {
1150 print "QUESTION($1)\n" if ($dbg_values > 1);
1153 } elsif ($cur =~ /^(:)/o) {
1154 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1156 substr($var, length($res), 1, $av_pend_colon);
1157 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1162 $av_pend_colon = 'O';
1164 } elsif ($cur =~ /^(\[)/o) {
1165 print "CLOSE($1)\n" if ($dbg_values > 1);
1168 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1171 print "OPV($1)\n" if ($dbg_values > 1);
1178 substr($var, length($res), 1, $variant);
1181 } elsif ($cur =~ /^($Operators)/o) {
1182 print "OP($1)\n" if ($dbg_values > 1);
1183 if ($1 ne '++' && $1 ne '--') {
1187 } elsif ($cur =~ /(^.)/o) {
1188 print "C($1)\n" if ($dbg_values > 1);
1191 $cur = substr($cur, length($1));
1192 $res .= $type x length($1);
1196 return ($res, $var);
1200 my ($possible, $line) = @_;
1201 my $notPermitted = qr{(?:
1218 ^(?:typedef|struct|enum)\b
1220 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1221 if ($possible !~ $notPermitted) {
1222 # Check for modifiers.
1223 $possible =~ s/\s*$Storage\s*//g;
1224 $possible =~ s/\s*$Sparse\s*//g;
1225 if ($possible =~ /^\s*$/) {
1227 } elsif ($possible =~ /\s/) {
1228 $possible =~ s/\s*$Type\s*//g;
1229 for my $modifier (split(' ', $possible)) {
1230 if ($modifier !~ $notPermitted) {
1231 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1232 push(@modifierList, $modifier);
1237 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1238 push(@typeList, $possible);
1242 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1249 return !defined $ignore_type{$_[0]};
1253 if (!show_type($_[1]) ||
1254 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1259 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1261 $line = "$prefix$_[0]: $_[2]\n";
1263 $line = (split('\n', $line))[0] . "\n" if ($terse);
1265 push(our @report, $line);
1274 if (report("ERROR", $_[0], $_[1])) {
1280 if (report("WARNING", $_[0], $_[1])) {
1286 if ($check && report("CHECK", $_[0], $_[1])) {
1292 sub check_absolute_file {
1293 my ($absolute, $herecurr) = @_;
1294 my $file = $absolute;
1296 ##print "absolute<$absolute>\n";
1298 # See if any suffix of this path is a path within the tree.
1299 while ($file =~ s@^[^/]*/@@) {
1300 if (-f "$root/$file") {
1301 ##print "file<$file>\n";
1309 # It is, so see if the prefix is acceptable.
1310 my $prefix = $absolute;
1311 substr($prefix, -length($file)) = '';
1313 ##print "prefix<$prefix>\n";
1314 if ($prefix ne ".../") {
1315 WARN("USE_RELATIVE_PATH",
1316 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1320 sub pos_last_openparen {
1325 my $opens = $line =~ tr/\(/\(/;
1326 my $closes = $line =~ tr/\)/\)/;
1328 my $last_openparen = 0;
1330 if (($opens == 0) || ($closes >= $opens)) {
1334 my $len = length($line);
1336 for ($pos = 0; $pos < $len; $pos++) {
1337 my $string = substr($line, $pos);
1338 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1339 $pos += length($1) - 1;
1340 } elsif (substr($line, $pos, 1) eq '(') {
1341 $last_openparen = $pos;
1342 } elsif (index($string, '(') == -1) {
1347 return $last_openparen + 1;
1351 my $filename = shift;
1357 my $stashrawline="";
1368 my $in_header_lines = 1;
1369 my $in_commit_log = 0; #Scanning lines before patch
1371 my $non_utf8_charset = 0;
1379 # Trace the real file/line as we go.
1385 my $comment_edge = 0;
1389 my $prev_values = 'E';
1392 my %suppress_ifbraces;
1393 my %suppress_whiletrailers;
1394 my %suppress_export;
1395 my $suppress_statement = 0;
1397 # Pre-scan the patch sanitizing the lines.
1398 # Pre-scan the patch looking for any __setup documentation.
1400 my @setup_docs = ();
1403 sanitise_line_reset();
1405 foreach my $rawline (@rawlines) {
1409 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1411 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1416 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1425 # Guestimate if this is a continuing comment. Run
1426 # the context looking for a comment "edge". If this
1427 # edge is a close comment then we must be in a comment
1431 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1432 next if (defined $rawlines[$ln - 1] &&
1433 $rawlines[$ln - 1] =~ /^-/);
1435 #print "RAW<$rawlines[$ln - 1]>\n";
1436 last if (!defined $rawlines[$ln - 1]);
1437 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1438 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1443 if (defined $edge && $edge eq '*/') {
1447 # Guestimate if this is a continuing comment. If this
1448 # is the start of a diff block and this line starts
1449 # ' *' then it is very likely a comment.
1450 if (!defined $edge &&
1451 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1456 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1457 sanitise_line_reset($in_comment);
1459 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1460 # Standardise the strings and chars within the input to
1461 # simplify matching -- only bother with positive lines.
1462 $line = sanitise_line($rawline);
1464 push(@lines, $line);
1467 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1472 #print "==>$rawline\n";
1473 #print "-->$line\n";
1475 if ($setup_docs && $line =~ /^\+/) {
1476 push(@setup_docs, $line);
1484 foreach my $line (@lines) {
1487 my $rawline = $rawlines[$linenr - 1];
1489 #extract the line range in the file after the patch is applied
1490 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1492 $first_line = $linenr + 1;
1502 %suppress_ifbraces = ();
1503 %suppress_whiletrailers = ();
1504 %suppress_export = ();
1505 $suppress_statement = 0;
1508 # track the line number as we move through the hunk, note that
1509 # new versions of GNU diff omit the leading space on completely
1510 # blank context lines so we need to count that too.
1511 } elsif ($line =~ /^( |\+|$)/) {
1513 $realcnt-- if ($realcnt != 0);
1515 # Measure the line length and indent.
1516 ($length, $indent) = line_stats($rawline);
1518 # Track the previous line.
1519 ($prevline, $stashline) = ($stashline, $line);
1520 ($previndent, $stashindent) = ($stashindent, $indent);
1521 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1523 #warn "line<$line>\n";
1525 } elsif ($realcnt == 1) {
1529 my $hunk_line = ($realcnt != 0);
1531 #make up the handle for any error we report on this line
1532 $prefix = "$filename:$realline: " if ($emacs && $file);
1533 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1535 $here = "#$linenr: " if (!$file);
1536 $here = "#$realline: " if ($file);
1538 # extract the filename as it passes
1539 if ($line =~ /^diff --git.*?(\S+)$/) {
1541 $realfile =~ s@^([^/]*)/@@;
1543 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1545 $realfile =~ s@^([^/]*)/@@;
1549 if (!$file && $tree && $p1_prefix ne '' &&
1550 -e "$root/$p1_prefix") {
1551 WARN("PATCH_PREFIX",
1552 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1555 if ($realfile =~ m@^include/asm/@) {
1556 ERROR("MODIFIED_INCLUDE_ASM",
1557 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1562 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1564 my $hereline = "$here\n$rawline\n";
1565 my $herecurr = "$here\n$rawline\n";
1566 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1568 $cnt_lines++ if ($realcnt != 0);
1570 # Check for incorrect file permissions
1571 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1572 my $permhere = $here . "FILE: $realfile\n";
1573 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1574 ERROR("EXECUTE_PERMISSIONS",
1575 "do not set execute permissions for source files\n" . $permhere);
1579 # Check the patch for a signoff:
1580 if ($line =~ /^\s*signed-off-by:/i) {
1585 # Check signature styles
1586 if (!$in_header_lines &&
1587 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
1588 my $space_before = $1;
1590 my $space_after = $3;
1592 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1594 if ($sign_off !~ /$signature_tags/) {
1595 WARN("BAD_SIGN_OFF",
1596 "Non-standard signature: $sign_off\n" . $herecurr);
1598 if (defined $space_before && $space_before ne "") {
1599 WARN("BAD_SIGN_OFF",
1600 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
1602 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1603 WARN("BAD_SIGN_OFF",
1604 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
1606 if (!defined $space_after || $space_after ne " ") {
1607 WARN("BAD_SIGN_OFF",
1608 "Use a single space after $ucfirst_sign_off\n" . $herecurr);
1611 my ($email_name, $email_address, $comment) = parse_email($email);
1612 my $suggested_email = format_email(($email_name, $email_address));
1613 if ($suggested_email eq "") {
1614 ERROR("BAD_SIGN_OFF",
1615 "Unrecognized email address: '$email'\n" . $herecurr);
1617 my $dequoted = $suggested_email;
1618 $dequoted =~ s/^"//;
1619 $dequoted =~ s/" </ </;
1620 # Don't force email to have quotes
1621 # Allow just an angle bracketed address
1622 if ("$dequoted$comment" ne $email &&
1623 "<$email_address>$comment" ne $email &&
1624 "$suggested_email$comment" ne $email) {
1625 WARN("BAD_SIGN_OFF",
1626 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
1631 # Check for wrappage within a valid hunk of the file
1632 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1633 ERROR("CORRUPTED_PATCH",
1634 "patch seems to be corrupt (line wrapped?)\n" .
1635 $herecurr) if (!$emitted_corrupt++);
1638 # Check for absolute kernel paths.
1640 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1643 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1644 check_absolute_file($1, $herecurr)) {
1647 check_absolute_file($file, $herecurr);
1652 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1653 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1654 $rawline !~ m/^$UTF8*$/) {
1655 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1657 my $blank = copy_spacing($rawline);
1658 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1659 my $hereptr = "$hereline$ptr\n";
1662 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1665 # Check if it's the start of a commit log
1666 # (not a header line and we haven't seen the patch filename)
1667 if ($in_header_lines && $realfile =~ /^$/ &&
1668 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
1669 $in_header_lines = 0;
1673 # Check if there is UTF-8 in a commit log when a mail header has explicitly
1674 # declined it, i.e defined some charset where it is missing.
1675 if ($in_header_lines &&
1676 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1678 $non_utf8_charset = 1;
1681 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
1682 $rawline =~ /$NON_ASCII_UTF8/) {
1683 WARN("UTF8_BEFORE_PATCH",
1684 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1687 # ignore non-hunk lines and lines being removed
1688 next if (!$hunk_line || $line =~ /^-/);
1690 #trailing whitespace
1691 if ($line =~ /^\+.*\015/) {
1692 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1693 ERROR("DOS_LINE_ENDINGS",
1694 "DOS line endings\n" . $herevet);
1696 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1697 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1698 ERROR("TRAILING_WHITESPACE",
1699 "trailing whitespace\n" . $herevet);
1703 # check for Kconfig help text having a real description
1704 # Only applies when adding the entry originally, after that we do not have
1705 # sufficient context to determine whether it is indeed long enough.
1706 if ($realfile =~ /Kconfig/ &&
1707 $line =~ /.\s*config\s+/) {
1710 my $ln = $linenr + 1;
1714 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
1715 $f = $lines[$ln - 1];
1716 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1717 $is_end = $lines[$ln - 1] =~ /^\+/;
1719 next if ($f =~ /^-/);
1721 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1723 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1730 next if ($f =~ /^$/);
1731 if ($f =~ /^\s*config\s/) {
1737 WARN("CONFIG_DESCRIPTION",
1738 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1739 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
1742 # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
1743 if ($realfile =~ /Kconfig/ &&
1744 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
1745 WARN("CONFIG_EXPERIMENTAL",
1746 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1749 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1750 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1753 'EXTRA_AFLAGS' => 'asflags-y',
1754 'EXTRA_CFLAGS' => 'ccflags-y',
1755 'EXTRA_CPPFLAGS' => 'cppflags-y',
1756 'EXTRA_LDFLAGS' => 'ldflags-y',
1759 WARN("DEPRECATED_VARIABLE",
1760 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1763 # check we are in a valid source file if not then ignore this hunk
1764 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1767 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1768 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1769 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
1770 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1771 $length > $max_line_length)
1774 "line over $max_line_length characters\n" . $herecurr);
1777 # Check for user-visible strings broken across lines, which breaks the ability
1778 # to grep for the string. Limited to strings used as parameters (those
1779 # following an open parenthesis), which almost completely eliminates false
1780 # positives, as well as warning only once per parameter rather than once per
1781 # line of the string. Make an exception when the previous string ends in a
1782 # newline (multiple lines in one string constant) or \n\t (common in inline
1783 # assembly to indent the instruction on the following line).
1784 if ($line =~ /^\+\s*"/ &&
1785 $prevline =~ /"\s*$/ &&
1786 $prevline =~ /\(/ &&
1787 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1788 WARN("SPLIT_STRING",
1789 "quoted string split across lines\n" . $hereprev);
1792 # check for spaces before a quoted newline
1793 if ($rawline =~ /^.*\".*\s\\n/) {
1794 WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1795 "unnecessary whitespace before a quoted newline\n" . $herecurr);
1798 # check for adding lines without a newline.
1799 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1800 WARN("MISSING_EOF_NEWLINE",
1801 "adding a line without newline at end of file\n" . $herecurr);
1804 # Blackfin: use hi/lo macros
1805 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1806 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1807 my $herevet = "$here\n" . cat_vet($line) . "\n";
1809 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1811 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1812 my $herevet = "$here\n" . cat_vet($line) . "\n";
1814 "use the HI() macro, not (... >> 16)\n" . $herevet);
1818 # check we are in a valid source file C or perl if not then ignore this hunk
1819 next if ($realfile !~ /\.(h|c|pl)$/);
1821 # at the beginning of a line any tabs must come first and anything
1822 # more than 8 must use tabs.
1823 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1824 $rawline =~ /^\+\s* \s*/) {
1825 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1826 ERROR("CODE_INDENT",
1827 "code indent should use tabs where possible\n" . $herevet);
1831 # check for space before tabs.
1832 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1833 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1834 WARN("SPACE_BEFORE_TAB",
1835 "please, no space before tabs\n" . $herevet);
1838 # check for && or || at the start of a line
1839 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
1840 CHK("LOGICAL_CONTINUATIONS",
1841 "Logical continuations should be on the previous line\n" . $hereprev);
1844 # check multi-line statement indentation matches previous line
1845 if ($^V && $^V ge 5.10.0 &&
1846 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
1847 $prevline =~ /^\+(\t*)(.*)$/;
1851 my $pos = pos_last_openparen($rest);
1853 $line =~ /^(\+| )([ \t]*)/;
1856 my $goodtabindent = $oldindent .
1859 my $goodspaceindent = $oldindent . " " x $pos;
1861 if ($newindent ne $goodtabindent &&
1862 $newindent ne $goodspaceindent) {
1863 CHK("PARENTHESIS_ALIGNMENT",
1864 "Alignment should match open parenthesis\n" . $hereprev);
1869 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) {
1871 "No space is necessary after a cast\n" . $hereprev);
1874 if ($realfile =~ m@^(drivers/net/|net/)@ &&
1875 $rawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
1876 $prevrawline =~ /^\+[ \t]*$/) {
1877 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1878 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
1881 if ($realfile =~ m@^(drivers/net/|net/)@ &&
1882 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
1883 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
1884 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
1885 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
1886 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1887 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
1890 # check for spaces at the beginning of a line.
1892 # 1) within comments
1893 # 2) indented preprocessor commands
1895 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
1896 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1897 WARN("LEADING_SPACE",
1898 "please, no spaces at the start of a line\n" . $herevet);
1901 # check we are in a valid C source file if not then ignore this hunk
1902 next if ($realfile !~ /\.(h|c)$/);
1904 # discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
1905 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
1906 WARN("CONFIG_EXPERIMENTAL",
1907 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1910 # check for RCS/CVS revision markers
1911 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1913 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1916 # Blackfin: don't use __builtin_bfin_[cs]sync
1917 if ($line =~ /__builtin_bfin_csync/) {
1918 my $herevet = "$here\n" . cat_vet($line) . "\n";
1920 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1922 if ($line =~ /__builtin_bfin_ssync/) {
1923 my $herevet = "$here\n" . cat_vet($line) . "\n";
1925 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1928 # Check for potential 'bare' types
1929 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1931 #print "LINE<$line>\n";
1932 if ($linenr >= $suppress_statement &&
1933 $realcnt && $line =~ /.\s*\S/) {
1934 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1935 ctx_statement_block($linenr, $realcnt, 0);
1936 $stat =~ s/\n./\n /g;
1937 $cond =~ s/\n./\n /g;
1939 #print "linenr<$linenr> <$stat>\n";
1940 # If this statement has no statement boundaries within
1941 # it there is no point in retrying a statement scan
1942 # until we hit end of it.
1943 my $frag = $stat; $frag =~ s/;+\s*$//;
1944 if ($frag !~ /(?:{|;)/) {
1945 #print "skip<$line_nr_next>\n";
1946 $suppress_statement = $line_nr_next;
1949 # Find the real next line.
1950 $realline_next = $line_nr_next;
1951 if (defined $realline_next &&
1952 (!defined $lines[$realline_next - 1] ||
1953 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1960 # Ignore goto labels.
1961 if ($s =~ /$Ident:\*$/s) {
1963 # Ignore functions being called
1964 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1966 } elsif ($s =~ /^.\s*else\b/s) {
1968 # declarations always start with types
1969 } 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) {
1972 possible($type, "A:" . $s);
1974 # definitions in global scope can only start with types
1975 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1976 possible($1, "B:" . $s);
1979 # any (foo ... *) is a pointer cast, and foo is a type
1980 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1981 possible($1, "C:" . $s);
1984 # Check for any sort of function declaration.
1985 # int foo(something bar, other baz);
1986 # void (*store_gdt)(x86_descr_ptr *);
1987 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1988 my ($name_len) = length($1);
1991 substr($ctx, 0, $name_len + 1, '');
1992 $ctx =~ s/\)[^\)]*$//;
1994 for my $arg (split(/\s*,\s*/, $ctx)) {
1995 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1997 possible($1, "D:" . $s);
2005 # Checks which may be anchored in the context.
2008 # Check for switch () and associated case and default
2009 # statements should be at the same indent.
2010 if ($line=~/\bswitch\s*\(.*\)/) {
2013 my @ctx = ctx_block_outer($linenr, $realcnt);
2015 for my $ctx (@ctx) {
2016 my ($clen, $cindent) = line_stats($ctx);
2017 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2018 $indent != $cindent) {
2019 $err .= "$sep$ctx\n";
2026 ERROR("SWITCH_CASE_INDENT_LEVEL",
2027 "switch and case should be at the same indent\n$hereline$err");
2031 # if/while/etc brace do not go on next line, unless defining a do while loop,
2032 # or if that brace on the next line is for something else
2033 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2034 my $pre_ctx = "$1$2";
2036 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
2038 if ($line =~ /^\+\t{6,}/) {
2039 WARN("DEEP_INDENTATION",
2040 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2043 my $ctx_cnt = $realcnt - $#ctx - 1;
2044 my $ctx = join("\n", @ctx);
2046 my $ctx_ln = $linenr;
2047 my $ctx_skip = $realcnt;
2049 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2050 defined $lines[$ctx_ln - 1] &&
2051 $lines[$ctx_ln - 1] =~ /^-/)) {
2052 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2053 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2057 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2058 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2060 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2062 "that open brace { should be on the previous line\n" .
2063 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2065 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2066 $ctx =~ /\)\s*\;\s*$/ &&
2067 defined $lines[$ctx_ln - 1])
2069 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2070 if ($nindent > $indent) {
2071 WARN("TRAILING_SEMICOLON",
2072 "trailing semicolon indicates no statements, indent implies otherwise\n" .
2073 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2078 # Check relative indent for conditionals and blocks.
2079 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
2080 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2081 ctx_statement_block($linenr, $realcnt, 0)
2082 if (!defined $stat);
2083 my ($s, $c) = ($stat, $cond);
2085 substr($s, 0, length($c), '');
2087 # Make sure we remove the line prefixes as we have
2088 # none on the first line, and are going to readd them
2092 # Find out how long the conditional actually is.
2093 my @newlines = ($c =~ /\n/gs);
2094 my $cond_lines = 1 + $#newlines;
2096 # We want to check the first line inside the block
2097 # starting at the end of the conditional, so remove:
2098 # 1) any blank line termination
2099 # 2) any opening brace { on end of the line
2101 my $continuation = 0;
2103 $s =~ s/^.*\bdo\b//;
2105 if ($s =~ s/^\s*\\//) {
2108 if ($s =~ s/^\s*?\n//) {
2113 # Also ignore a loop construct at the end of a
2114 # preprocessor statement.
2115 if (($prevline =~ /^.\s*#\s*define\s/ ||
2116 $prevline =~ /\\\s*$/) && $continuation == 0) {
2122 while ($cond_ptr != $cond_lines) {
2123 $cond_ptr = $cond_lines;
2125 # If we see an #else/#elif then the code
2127 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2132 # 1) blank lines, they should be at 0,
2133 # 2) preprocessor lines, and
2135 if ($continuation ||
2137 $s =~ /^\s*#\s*?/ ||
2138 $s =~ /^\s*$Ident\s*:/) {
2139 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
2140 if ($s =~ s/^.*?\n//) {
2146 my (undef, $sindent) = line_stats("+" . $s);
2147 my $stat_real = raw_line($linenr, $cond_lines);
2149 # Check if either of these lines are modified, else
2150 # this is not this patch's fault.
2151 if (!defined($stat_real) ||
2152 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2155 if (defined($stat_real) && $cond_lines > 1) {
2156 $stat_real = "[...]\n$stat_real";
2159 #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";
2161 if ($check && (($sindent % 8) != 0 ||
2162 ($sindent <= $indent && $s ne ''))) {
2163 WARN("SUSPECT_CODE_INDENT",
2164 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
2168 # Track the 'values' across context and added lines.
2169 my $opline = $line; $opline =~ s/^./ /;
2170 my ($curr_values, $curr_vars) =
2171 annotate_values($opline . "\n", $prev_values);
2172 $curr_values = $prev_values . $curr_values;
2174 my $outline = $opline; $outline =~ s/\t/ /g;
2175 print "$linenr > .$outline\n";
2176 print "$linenr > $curr_values\n";
2177 print "$linenr > $curr_vars\n";
2179 $prev_values = substr($curr_values, -1);
2181 #ignore lines not being added
2182 if ($line=~/^[^\+]/) {next;}
2184 # TEST: allow direct testing of the type matcher.
2186 if ($line =~ /^.\s*$Declare\s*$/) {
2188 "TEST: is type\n" . $herecurr);
2189 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2190 ERROR("TEST_NOT_TYPE",
2191 "TEST: is not type ($1 is)\n". $herecurr);
2195 # TEST: allow direct testing of the attribute matcher.
2197 if ($line =~ /^.\s*$Modifier\s*$/) {
2199 "TEST: is attr\n" . $herecurr);
2200 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2201 ERROR("TEST_NOT_ATTR",
2202 "TEST: is not attr ($1 is)\n". $herecurr);
2207 # check for initialisation to aggregates open brace on the next line
2208 if ($line =~ /^.\s*{/ &&
2209 $prevline =~ /(?:^|[^=])=\s*$/) {
2211 "that open brace { should be on the previous line\n" . $hereprev);
2215 # Checks which are anchored on the added line.
2218 # check for malformed paths in #include statements (uses RAW line)
2219 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2221 if ($path =~ m{//}) {
2222 ERROR("MALFORMED_INCLUDE",
2223 "malformed #include filename\n" .
2228 # no C99 // comments
2229 if ($line =~ m{//}) {
2230 ERROR("C99_COMMENTS",
2231 "do not use C99 // comments\n" . $herecurr);
2233 # Remove C99 comments.
2235 $opline =~ s@//.*@@;
2237 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2238 # the whole statement.
2239 #print "APW <$lines[$realline_next - 1]>\n";
2240 if (defined $realline_next &&
2241 exists $lines[$realline_next - 1] &&
2242 !defined $suppress_export{$realline_next} &&
2243 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2244 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2245 # Handle definitions which produce identifiers with
2248 # EXPORT_SYMBOL(something_foo);
2250 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
2251 $name =~ /^${Ident}_$2/) {
2252 #print "FOO C name<$name>\n";
2253 $suppress_export{$realline_next} = 1;
2255 } elsif ($stat !~ /(?:
2257 ^.DEFINE_$Ident\(\Q$name\E\)|
2258 ^.DECLARE_$Ident\(\Q$name\E\)|
2259 ^.LIST_HEAD\(\Q$name\E\)|
2260 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2261 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
2263 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2264 $suppress_export{$realline_next} = 2;
2266 $suppress_export{$realline_next} = 1;
2269 if (!defined $suppress_export{$linenr} &&
2270 $prevline =~ /^.\s*$/ &&
2271 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2272 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2273 #print "FOO B <$lines[$linenr - 1]>\n";
2274 $suppress_export{$linenr} = 2;
2276 if (defined $suppress_export{$linenr} &&
2277 $suppress_export{$linenr} == 2) {
2278 WARN("EXPORT_SYMBOL",
2279 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
2282 # check for global initialisers.
2283 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
2284 ERROR("GLOBAL_INITIALISERS",
2285 "do not initialise globals to 0 or NULL\n" .
2288 # check for static initialisers.
2289 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2290 ERROR("INITIALISED_STATIC",
2291 "do not initialise statics to 0 or NULL\n" .
2295 # check for static const char * arrays.
2296 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2297 WARN("STATIC_CONST_CHAR_ARRAY",
2298 "static const char * array should probably be static const char * const\n" .
2302 # check for static char foo[] = "bar" declarations.
2303 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2304 WARN("STATIC_CONST_CHAR_ARRAY",
2305 "static char array declaration should probably be static const char\n" .
2309 # check for declarations of struct pci_device_id
2310 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2311 WARN("DEFINE_PCI_DEVICE_TABLE",
2312 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
2315 # check for new typedefs, only function parameters and sparse annotations
2317 if ($line =~ /\btypedef\s/ &&
2318 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2319 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
2320 $line !~ /\b$typeTypedefs\b/ &&
2321 $line !~ /\b__bitwise(?:__|)\b/) {
2322 WARN("NEW_TYPEDEFS",
2323 "do not add new typedefs\n" . $herecurr);
2326 # * goes on variable not on type
2328 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2330 my ($from, $to) = ($2, $2);
2332 # Should start with a space.
2333 $to =~ s/^(\S)/ $1/;
2334 # Should not end with a space.
2336 # '*'s should not have spaces between.
2337 while ($to =~ s/\*\s+\*/\*\*/) {
2340 #print "from<$from> to<$to>\n";
2342 ERROR("POINTER_LOCATION",
2343 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
2346 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2348 my ($from, $to, $ident) = ($2, $2, $3);
2350 # Should start with a space.
2351 $to =~ s/^(\S)/ $1/;
2352 # Should not end with a space.
2354 # '*'s should not have spaces between.
2355 while ($to =~ s/\*\s+\*/\*\*/) {
2357 # Modifiers should have spaces.
2358 $to =~ s/(\b$Modifier$)/$1 /;
2360 #print "from<$from> to<$to> ident<$ident>\n";
2361 if ($from ne $to && $ident !~ /^$Modifier$/) {
2362 ERROR("POINTER_LOCATION",
2363 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
2367 # # no BUG() or BUG_ON()
2368 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
2369 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2370 # print "$herecurr";
2374 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2375 WARN("LINUX_VERSION_CODE",
2376 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
2379 # check for uses of printk_ratelimit
2380 if ($line =~ /\bprintk_ratelimit\s*\(/) {
2381 WARN("PRINTK_RATELIMITED",
2382 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
2385 # printk should use KERN_* levels. Note that follow on printk's on the
2386 # same line do not need a level, so we use the current block context
2387 # to try and find and validate the current printk. In summary the current
2388 # printk includes all preceding printk's which have no newline on the end.
2389 # we assume the first bad printk is the one to report.
2390 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
2392 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2393 #print "CHECK<$lines[$ln - 1]\n";
2394 # we have a preceding printk if it ends
2395 # with "\n" ignore it, else it is to blame
2396 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2397 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2404 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2405 "printk() should include KERN_ facility level\n" . $herecurr);
2409 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2411 my $level = lc($orig);
2412 $level = "warn" if ($level eq "warning");
2413 my $level2 = $level;
2414 $level2 = "dbg" if ($level eq "debug");
2415 WARN("PREFER_PR_LEVEL",
2416 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
2419 if ($line =~ /\bpr_warning\s*\(/) {
2420 WARN("PREFER_PR_LEVEL",
2421 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr);
2424 # function brace can't be on same line, except for #defines of do while,
2425 # or if closed on same line
2426 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2427 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2429 "open brace '{' following function declarations go on the next line\n" . $herecurr);
2432 # open braces for enum, union and struct go on the same line.
2433 if ($line =~ /^.\s*{/ &&
2434 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2436 "open brace '{' following $1 go on the same line\n" . $hereprev);
2439 # missing space after union, struct or enum definition
2440 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2442 "missing space after $1 definition\n" . $herecurr);
2445 # check for spacing round square brackets; allowed:
2446 # 1. with a type on the left -- int [] a;
2447 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2448 # 3. inside a curly brace -- = { [0...10] = 5 }
2449 while ($line =~ /(.*?\s)\[/g) {
2450 my ($where, $prefix) = ($-[1], $1);
2451 if ($prefix !~ /$Type\s+$/ &&
2452 ($where != 0 || $prefix !~ /^.\s+$/) &&
2453 $prefix !~ /[{,]\s+$/) {
2454 ERROR("BRACKET_SPACE",
2455 "space prohibited before open square bracket '['\n" . $herecurr);
2459 # check for spaces between functions and their parentheses.
2460 while ($line =~ /($Ident)\s+\(/g) {
2462 my $ctx_before = substr($line, 0, $-[1]);
2463 my $ctx = "$ctx_before$name";
2465 # Ignore those directives where spaces _are_ permitted.
2467 if|for|while|switch|return|case|
2468 volatile|__volatile__|
2469 __attribute__|format|__extension__|
2473 # cpp #define statements have non-optional spaces, ie
2474 # if there is a space between the name and the open
2475 # parenthesis it is simply not a parameter group.
2476 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2478 # cpp #elif statement condition may start with a (
2479 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2481 # If this whole things ends with a type its most
2482 # likely a typedef for a function.
2483 } elsif ($ctx =~ /$Type$/) {
2487 "space prohibited between function name and open parenthesis '('\n" . $herecurr);
2491 # check for whitespace before a non-naked semicolon
2492 if ($line =~ /^\+.*\S\s+;/) {
2494 "space prohibited before semicolon\n" . $herecurr);
2497 # Check operator spacing.
2498 if (!($line=~/\#\s*include/)) {
2500 <<=|>>=|<=|>=|==|!=|
2501 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2502 =>|->|<<|>>|<|>|=|!|~|
2503 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2506 my @elements = split(/($ops|;)/, $opline);
2509 my $blank = copy_spacing($opline);
2511 for (my $n = 0; $n < $#elements; $n += 2) {
2512 $off += length($elements[$n]);
2514 # Pick up the preceding and succeeding characters.
2515 my $ca = substr($opline, 0, $off);
2517 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2518 $cc = substr($opline, $off + length($elements[$n + 1]));
2520 my $cb = "$ca$;$cc";
2523 $a = 'V' if ($elements[$n] ne '');
2524 $a = 'W' if ($elements[$n] =~ /\s$/);
2525 $a = 'C' if ($elements[$n] =~ /$;$/);
2526 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2527 $a = 'O' if ($elements[$n] eq '');
2528 $a = 'E' if ($ca =~ /^\s*$/);
2530 my $op = $elements[$n + 1];
2533 if (defined $elements[$n + 2]) {
2534 $c = 'V' if ($elements[$n + 2] ne '');
2535 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2536 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2537 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2538 $c = 'O' if ($elements[$n + 2] eq '');
2539 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2544 my $ctx = "${a}x${c}";
2546 my $at = "(ctx:$ctx)";
2548 my $ptr = substr($blank, 0, $off) . "^";
2549 my $hereptr = "$hereline$ptr\n";
2551 # Pull out the value of this operator.
2552 my $op_type = substr($curr_values, $off + 1, 1);
2554 # Get the full operator variant.
2555 my $opv = $op . substr($curr_vars, $off, 1);
2557 # Ignore operators passed as parameters.
2558 if ($op_type ne 'V' &&
2559 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2562 # } elsif ($op =~ /^$;+$/) {
2564 # ; should have either the end of line or a space or \ after it
2565 } elsif ($op eq ';') {
2566 if ($ctx !~ /.x[WEBC]/ &&
2567 $cc !~ /^\\/ && $cc !~ /^;/) {
2569 "space required after that '$op' $at\n" . $hereptr);
2573 } elsif ($op eq '//') {
2577 # : when part of a bitfield
2578 } elsif ($op eq '->' || $opv eq ':B') {
2579 if ($ctx =~ /Wx.|.xW/) {
2581 "spaces prohibited around that '$op' $at\n" . $hereptr);
2584 # , must have a space on the right.
2585 } elsif ($op eq ',') {
2586 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2588 "space required after that '$op' $at\n" . $hereptr);
2591 # '*' as part of a type definition -- reported already.
2592 } elsif ($opv eq '*_') {
2593 #warn "'*' is part of type\n";
2595 # unary operators should have a space before and
2596 # none after. May be left adjacent to another
2597 # unary operator, or a cast
2598 } elsif ($op eq '!' || $op eq '~' ||
2599 $opv eq '*U' || $opv eq '-U' ||
2600 $opv eq '&U' || $opv eq '&&U') {
2601 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2603 "space required before that '$op' $at\n" . $hereptr);
2605 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2606 # A unary '*' may be const
2608 } elsif ($ctx =~ /.xW/) {
2610 "space prohibited after that '$op' $at\n" . $hereptr);
2613 # unary ++ and unary -- are allowed no space on one side.
2614 } elsif ($op eq '++' or $op eq '--') {
2615 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2617 "space required one side of that '$op' $at\n" . $hereptr);
2619 if ($ctx =~ /Wx[BE]/ ||
2620 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2622 "space prohibited before that '$op' $at\n" . $hereptr);
2624 if ($ctx =~ /ExW/) {
2626 "space prohibited after that '$op' $at\n" . $hereptr);
2630 # << and >> may either have or not have spaces both sides
2631 } elsif ($op eq '<<' or $op eq '>>' or
2632 $op eq '&' or $op eq '^' or $op eq '|' or
2633 $op eq '+' or $op eq '-' or
2634 $op eq '*' or $op eq '/' or
2637 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2639 "need consistent spacing around '$op' $at\n" .
2643 # A colon needs no spaces before when it is
2644 # terminating a case value or a label.
2645 } elsif ($opv eq ':C' || $opv eq ':L') {
2646 if ($ctx =~ /Wx./) {
2648 "space prohibited before that '$op' $at\n" . $hereptr);
2651 # All the others need spaces both sides.
2652 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2655 # Ignore email addresses <foo@bar>
2657 $cc =~ /^\S+\@\S+>/) ||
2659 $ca =~ /<\S+\@\S+$/))
2665 if (($opv eq ':O' && $ca =~ /\?$/) ||
2666 ($op eq '?' && $cc =~ /^:/)) {
2672 "spaces required around that '$op' $at\n" . $hereptr);
2675 $off += length($elements[$n + 1]);
2679 # check for multiple assignments
2680 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2681 CHK("MULTIPLE_ASSIGNMENTS",
2682 "multiple assignments should be avoided\n" . $herecurr);
2685 ## # check for multiple declarations, allowing for a function declaration
2687 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2688 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2690 ## # Remove any bracketed sections to ensure we do not
2691 ## # falsly report the parameters of functions.
2693 ## while ($ln =~ s/\([^\(\)]*\)//g) {
2695 ## if ($ln =~ /,/) {
2696 ## WARN("MULTIPLE_DECLARATION",
2697 ## "declaring multiple variables together should be avoided\n" . $herecurr);
2701 #need space before brace following if, while, etc
2702 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2705 "space required before the open brace '{'\n" . $herecurr);
2708 # closing brace should have a space following it when it has anything
2710 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2712 "space required after that close brace '}'\n" . $herecurr);
2715 # check spacing on square brackets
2716 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2718 "space prohibited after that open square bracket '['\n" . $herecurr);
2720 if ($line =~ /\s\]/) {
2722 "space prohibited before that close square bracket ']'\n" . $herecurr);
2725 # check spacing on parentheses
2726 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2727 $line !~ /for\s*\(\s+;/) {
2729 "space prohibited after that open parenthesis '('\n" . $herecurr);
2731 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2732 $line !~ /for\s*\(.*;\s+\)/ &&
2733 $line !~ /:\s+\)/) {
2735 "space prohibited before that close parenthesis ')'\n" . $herecurr);
2738 #goto labels aren't indented, allow a single space however
2739 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2740 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2741 WARN("INDENTED_LABEL",
2742 "labels should not be indented\n" . $herecurr);
2745 # Return is not a function.
2746 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2750 # Flatten any parentheses
2751 $value =~ s/\(/ \(/g;
2752 $value =~ s/\)/\) /g;
2753 while ($value =~ s/\[[^\[\]]*\]/1/ ||
2754 $value !~ /(?:$Ident|-?$Constant)\s*
2756 (?:$Ident|-?$Constant)/x &&
2757 $value =~ s/\([^\(\)]*\)/1/) {
2759 #print "value<$value>\n";
2760 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2761 ERROR("RETURN_PARENTHESES",
2762 "return is not a function, parentheses are not required\n" . $herecurr);
2764 } elsif ($spacing !~ /\s+/) {
2766 "space required before the open parenthesis '('\n" . $herecurr);
2769 # Return of what appears to be an errno should normally be -'ve
2770 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2772 if ($name ne 'EOF' && $name ne 'ERROR') {
2773 WARN("USE_NEGATIVE_ERRNO",
2774 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2778 # Need a space before open parenthesis after if, while etc
2779 if ($line=~/\b(if|while|for|switch)\(/) {
2780 ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
2783 # Check for illegal assignment in if conditional -- and check for trailing
2784 # statements after the conditional.
2785 if ($line =~ /do\s*(?!{)/) {
2786 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2787 ctx_statement_block($linenr, $realcnt, 0)
2788 if (!defined $stat);
2789 my ($stat_next) = ctx_statement_block($line_nr_next,
2790 $remain_next, $off_next);
2791 $stat_next =~ s/\n./\n /g;
2792 ##print "stat<$stat> stat_next<$stat_next>\n";
2794 if ($stat_next =~ /^\s*while\b/) {
2795 # If the statement carries leading newlines,
2796 # then count those as offsets.
2798 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2800 statement_rawlines($whitespace) - 1;
2802 $suppress_whiletrailers{$line_nr_next +
2806 if (!defined $suppress_whiletrailers{$linenr} &&
2807 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2808 my ($s, $c) = ($stat, $cond);
2810 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2811 ERROR("ASSIGN_IN_IF",
2812 "do not use assignment in if condition\n" . $herecurr);
2815 # Find out what is on the end of the line after the
2817 substr($s, 0, length($c), '');
2819 $s =~ s/$;//g; # Remove any comments
2820 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2821 $c !~ /}\s*while\s*/)
2823 # Find out how long the conditional actually is.
2824 my @newlines = ($c =~ /\n/gs);
2825 my $cond_lines = 1 + $#newlines;
2828 $stat_real = raw_line($linenr, $cond_lines)
2829 . "\n" if ($cond_lines);
2830 if (defined($stat_real) && $cond_lines > 1) {
2831 $stat_real = "[...]\n$stat_real";
2834 ERROR("TRAILING_STATEMENTS",
2835 "trailing statements should be on next line\n" . $herecurr . $stat_real);
2839 # Check for bitwise tests written as boolean
2851 WARN("HEXADECIMAL_BOOLEAN_TEST",
2852 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2855 # if and else should not have general statements after it
2856 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2858 $s =~ s/$;//g; # Remove any comments
2859 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2860 ERROR("TRAILING_STATEMENTS",
2861 "trailing statements should be on next line\n" . $herecurr);
2864 # if should not continue a brace
2865 if ($line =~ /}\s*if\b/) {
2866 ERROR("TRAILING_STATEMENTS",
2867 "trailing statements should be on next line\n" .
2870 # case and default should not have general statements after them
2871 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2873 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2877 ERROR("TRAILING_STATEMENTS",
2878 "trailing statements should be on next line\n" . $herecurr);
2881 # Check for }<nl>else {, these must be at the same
2882 # indent level to be relevant to each other.
2883 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2884 $previndent == $indent) {
2885 ERROR("ELSE_AFTER_BRACE",
2886 "else should follow close brace '}'\n" . $hereprev);
2889 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2890 $previndent == $indent) {
2891 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2893 # Find out what is on the end of the line after the
2895 substr($s, 0, length($c), '');
2898 if ($s =~ /^\s*;/) {
2899 ERROR("WHILE_AFTER_BRACE",
2900 "while should follow close brace '}'\n" . $hereprev);
2904 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2905 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2906 # print "No studly caps, use _\n";
2907 # print "$herecurr";
2911 #no spaces allowed after \ in define
2912 if ($line=~/\#\s*define.*\\\s$/) {
2913 WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
2914 "Whitepspace after \\ makes next lines useless\n" . $herecurr);
2917 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2918 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2920 my $checkfile = "include/linux/$file";
2921 if (-f "$root/$checkfile" &&
2922 $realfile ne $checkfile &&
2923 $1 !~ /$allowed_asm_includes/)
2925 if ($realfile =~ m{^arch/}) {
2926 CHK("ARCH_INCLUDE_LINUX",
2927 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2929 WARN("INCLUDE_LINUX",
2930 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2935 # multi-statement macros should be enclosed in a do while loop, grab the
2936 # first statement and ensure its the whole macro if its not enclosed
2937 # in a known good container
2938 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2939 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2942 my ($off, $dstat, $dcond, $rest);
2944 ($dstat, $dcond, $ln, $cnt, $off) =
2945 ctx_statement_block($linenr, $realcnt, 0);
2947 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2948 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2950 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
2952 $dstat =~ s/\\\n.//g;
2953 $dstat =~ s/^\s*//s;
2954 $dstat =~ s/\s*$//s;
2956 # Flatten any parentheses and braces
2957 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2958 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2959 $dstat =~ s/\[[^\[\]]*\]/1/)
2963 # Flatten any obvious string concatentation.
2964 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
2965 $dstat =~ s/$Ident\s*("X*")/$1/)
2969 my $exceptions = qr{
2981 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2983 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
2984 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
2985 $dstat !~ /^[!~-]?(?:$Ident|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo
2986 $dstat !~ /^'X'$/ && # character constants
2987 $dstat !~ /$exceptions/ &&
2988 $dstat !~ /^\.$Ident\s*=/ && # .foo =
2989 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
2990 $dstat !~ /^for\s*$Constant$/ && # for (...)
2991 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
2992 $dstat !~ /^do\s*{/ && # do {...
2993 $dstat !~ /^\({/) # ({...
2996 my $herectx = $here . "\n";
2997 my $cnt = statement_rawlines($ctx);
2999 for (my $n = 0; $n < $cnt; $n++) {
3000 $herectx .= raw_line($linenr, $n) . "\n";
3003 if ($dstat =~ /;/) {
3004 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3005 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3007 ERROR("COMPLEX_MACRO",
3008 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
3012 # check for line continuations outside of #defines, preprocessor #, and asm
3015 if ($prevline !~ /^..*\\$/ &&
3016 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3017 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
3018 $line =~ /^\+.*\\$/) {
3019 WARN("LINE_CONTINUATIONS",
3020 "Avoid unnecessary line continuations\n" . $herecurr);
3024 # do {} while (0) macro tests:
3025 # single-statement macros do not need to be enclosed in do while (0) loop,
3026 # macro should not end with a semicolon
3027 if ($^V && $^V ge 5.10.0 &&
3028 $realfile !~ m@/vmlinux.lds.h$@ &&
3029 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3032 my ($off, $dstat, $dcond, $rest);
3034 ($dstat, $dcond, $ln, $cnt, $off) =
3035 ctx_statement_block($linenr, $realcnt, 0);
3038 $dstat =~ s/\\\n.//g;
3040 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3045 my $cnt = statement_rawlines($ctx);
3046 my $herectx = $here . "\n";
3048 for (my $n = 0; $n < $cnt; $n++) {
3049 $herectx .= raw_line($linenr, $n) . "\n";
3052 if (($stmts =~ tr/;/;/) == 1 &&
3053 $stmts !~ /^\s*(if|while|for|switch)\b/) {
3054 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3055 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3057 if (defined $semis && $semis ne "") {
3058 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3059 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3064 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3065 # all assignments may have only one of the following with an assignment:
3068 # VMLINUX_SYMBOL(...)
3069 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
3070 WARN("MISSING_VMLINUX_SYMBOL",
3071 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
3074 # check for redundant bracing round if etc
3075 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3076 my ($level, $endln, @chunks) =
3077 ctx_statement_full($linenr, $realcnt, 1);
3078 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
3079 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3080 if ($#chunks > 0 && $level == 0) {
3084 my $herectx = $here . "\n";
3085 my $ln = $linenr - 1;
3086 for my $chunk (@chunks) {
3087 my ($cond, $block) = @{$chunk};
3089 # If the condition carries leading newlines, then count those as offsets.
3090 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3091 my $offset = statement_rawlines($whitespace) - 1;
3093 $allowed[$allow] = 0;
3094 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3096 # We have looked at and allowed this specific line.
3097 $suppress_ifbraces{$ln + $offset} = 1;
3099 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
3100 $ln += statement_rawlines($block) - 1;
3102 substr($block, 0, length($cond), '');
3104 $seen++ if ($block =~ /^\s*{/);
3106 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
3107 if (statement_lines($cond) > 1) {
3108 #print "APW: ALLOWED: cond<$cond>\n";
3109 $allowed[$allow] = 1;
3111 if ($block =~/\b(?:if|for|while)\b/) {
3112 #print "APW: ALLOWED: block<$block>\n";
3113 $allowed[$allow] = 1;
3115 if (statement_block_size($block) > 1) {
3116 #print "APW: ALLOWED: lines block<$block>\n";
3117 $allowed[$allow] = 1;
3122 my $sum_allowed = 0;
3123 foreach (@allowed) {
3126 if ($sum_allowed == 0) {
3128 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3129 } elsif ($sum_allowed != $allow &&
3132 "braces {} should be used on all arms of this statement\n" . $herectx);
3137 if (!defined $suppress_ifbraces{$linenr - 1} &&
3138 $line =~ /\b(if|while|for|else)\b/) {
3141 # Check the pre-context.
3142 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3143 #print "APW: ALLOWED: pre<$1>\n";
3147 my ($level, $endln, @chunks) =
3148 ctx_statement_full($linenr, $realcnt, $-[0]);
3150 # Check the condition.
3151 my ($cond, $block) = @{$chunks[0]};
3152 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3153 if (defined $cond) {
3154 substr($block, 0, length($cond), '');
3156 if (statement_lines($cond) > 1) {
3157 #print "APW: ALLOWED: cond<$cond>\n";
3160 if ($block =~/\b(?:if|for|while)\b/) {
3161 #print "APW: ALLOWED: block<$block>\n";
3164 if (statement_block_size($block) > 1) {
3165 #print "APW: ALLOWED: lines block<$block>\n";
3168 # Check the post-context.
3169 if (defined $chunks[1]) {
3170 my ($cond, $block) = @{$chunks[1]};
3171 if (defined $cond) {
3172 substr($block, 0, length($cond), '');
3174 if ($block =~ /^\s*\{/) {
3175 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3179 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
3180 my $herectx = $here . "\n";
3181 my $cnt = statement_rawlines($block);
3183 for (my $n = 0; $n < $cnt; $n++) {
3184 $herectx .= raw_line($linenr, $n) . "\n";
3188 "braces {} are not necessary for single statement blocks\n" . $herectx);
3192 # check for unnecessary blank lines around braces
3193 if (($line =~ /^..*}\s*$/ && $prevline =~ /^.\s*$/)) {
3195 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3197 if (($line =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
3199 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3202 # no volatiles please
3203 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3204 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3206 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
3210 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3211 CHK("REDUNDANT_CODE",
3212 "if this code is redundant consider removing it\n" .
3216 # check for needless "if (<foo>) fn(<foo>)" uses
3217 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3218 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3219 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3221 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
3225 # prefer usleep_range over udelay
3226 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
3227 # ignore udelay's < 10, however
3228 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
3230 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
3234 # warn about unexpectedly long msleep's
3235 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3238 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
3242 # warn about #ifdefs in C files
3243 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
3244 # print "#ifdef in C files should be avoided\n";
3245 # print "$herecurr";
3249 # warn about spacing in #ifdefs
3250 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3252 "exactly one space required after that #$1\n" . $herecurr);
3255 # check for spinlock_t definitions without a comment.
3256 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3257 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
3259 if (!ctx_has_comment($first_line, $linenr)) {
3260 CHK("UNCOMMENTED_DEFINITION",
3261 "$1 definition without comment\n" . $herecurr);
3264 # check for memory barriers without a comment.
3265 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3266 if (!ctx_has_comment($first_line, $linenr)) {
3267 CHK("MEMORY_BARRIER",
3268 "memory barrier without comment\n" . $herecurr);
3271 # check of hardware specific defines
3272 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
3274 "architecture specific defines should be avoided\n" . $herecurr);
3277 # Check that the storage class is at the beginning of a declaration
3278 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3279 WARN("STORAGE_CLASS",
3280 "storage class should be at the beginning of the declaration\n" . $herecurr)
3283 # check the location of the inline attribute, that it is between
3284 # storage class and type.
3285 if ($line =~ /\b$Type\s+$Inline\b/ ||
3286 $line =~ /\b$Inline\s+$Storage\b/) {
3287 ERROR("INLINE_LOCATION",
3288 "inline keyword should sit between storage class and type\n" . $herecurr);
3291 # Check for __inline__ and __inline, prefer inline
3292 if ($line =~ /\b(__inline__|__inline)\b/) {
3294 "plain inline is preferred over $1\n" . $herecurr);
3297 # Check for __attribute__ packed, prefer __packed
3298 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3299 WARN("PREFER_PACKED",
3300 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
3303 # Check for __attribute__ aligned, prefer __aligned
3304 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3305 WARN("PREFER_ALIGNED",
3306 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
3309 # Check for __attribute__ format(printf, prefer __printf
3310 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3311 WARN("PREFER_PRINTF",
3312 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3315 # Check for __attribute__ format(scanf, prefer __scanf
3316 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3317 WARN("PREFER_SCANF",
3318 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
3321 # check for sizeof(&)
3322 if ($line =~ /\bsizeof\s*\(\s*\&/) {
3323 WARN("SIZEOF_ADDRESS",
3324 "sizeof(& should be avoided\n" . $herecurr);
3327 # check for sizeof without parenthesis
3328 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
3329 WARN("SIZEOF_PARENTHESIS",
3330 "sizeof $1 should be sizeof($1)\n" . $herecurr);
3333 # check for line continuations in quoted strings with odd counts of "
3334 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3335 WARN("LINE_CONTINUATIONS",
3336 "Avoid line continuations in quoted strings\n" . $herecurr);
3339 # check for struct spinlock declarations
3340 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3341 WARN("USE_SPINLOCK_T",
3342 "struct spinlock should be spinlock_t\n" . $herecurr);
3345 # Check for misused memsets
3346 if ($^V && $^V ge 5.10.0 &&
3348 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3354 if ($ms_size =~ /^(0x|)0$/i) {
3356 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
3357 } elsif ($ms_size =~ /^(0x|)1$/i) {
3359 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3363 # typecasts on min/max could be min_t/max_t
3364 if ($^V && $^V ge 5.10.0 &&
3366 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3367 if (defined $2 || defined $7) {
3369 my $cast1 = deparenthesize($2);
3371 my $cast2 = deparenthesize($7);
3375 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
3376 $cast = "$cast1 or $cast2";
3377 } elsif ($cast1 ne "") {
3383 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
3387 # check usleep_range arguments
3388 if ($^V && $^V ge 5.10.0 &&
3390 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
3394 WARN("USLEEP_RANGE",
3395 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3396 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
3398 WARN("USLEEP_RANGE",
3399 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3403 # check for new externs in .c files.
3404 if ($realfile =~ /\.c$/ && defined $stat &&
3405 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
3407 my $function_name = $1;
3408 my $paren_space = $2;
3411 if (defined $cond) {
3412 substr($s, 0, length($cond), '');
3414 if ($s =~ /^\s*;/ &&
3415 $function_name ne 'uninitialized_var')
3417 WARN("AVOID_EXTERNS",
3418 "externs should be avoided in .c files\n" . $herecurr);
3421 if ($paren_space =~ /\n/) {
3422 WARN("FUNCTION_ARGUMENTS",
3423 "arguments for function declarations should follow identifier\n" . $herecurr);
3426 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3427 $stat =~ /^.\s*extern\s+/)
3429 WARN("AVOID_EXTERNS",
3430 "externs should be avoided in .c files\n" . $herecurr);
3433 # checks for new __setup's
3434 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3437 if (!grep(/$name/, @setup_docs)) {
3438 CHK("UNDOCUMENTED_SETUP",
3439 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
3443 # check for pointless casting of kmalloc return
3444 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
3445 WARN("UNNECESSARY_CASTS",
3446 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
3449 # check for multiple semicolons
3450 if ($line =~ /;\s*;\s*$/) {
3451 WARN("ONE_SEMICOLON",
3452 "Statements terminations use 1 semicolon\n" . $herecurr);
3455 # check for switch/default statements without a break;
3456 if ($^V && $^V ge 5.10.0 &&
3458 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3460 my $herectx = $here . "\n";
3461 my $cnt = statement_rawlines($stat);
3462 for (my $n = 0; $n < $cnt; $n++) {
3463 $herectx .= raw_line($linenr, $n) . "\n";
3465 WARN("DEFAULT_NO_BREAK",
3466 "switch default: should use break\n" . $herectx);
3469 # check for gcc specific __FUNCTION__
3470 if ($line =~ /__FUNCTION__/) {
3472 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
3475 # check for use of yield()
3476 if ($line =~ /\byield\s*\(\s*\)/) {
3478 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3481 # check for semaphores initialized locked
3482 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
3483 WARN("CONSIDER_COMPLETION",
3484 "consider using a completion\n" . $herecurr);
3487 # recommend kstrto* over simple_strto* and strict_strto*
3488 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
3489 WARN("CONSIDER_KSTRTO",
3490 "$1 is obsolete, use k$3 instead\n" . $herecurr);
3493 # check for __initcall(), use device_initcall() explicitly please
3494 if ($line =~ /^.\s*__initcall\s*\(/) {
3495 WARN("USE_DEVICE_INITCALL",
3496 "please use device_initcall() instead of __initcall()\n" . $herecurr);
3499 # check for various ops structs, ensure they are const.
3500 my $struct_ops = qr{acpi_dock_ops|
3501 address_space_operations|
3503 block_device_operations|
3508 file_lock_operations|
3518 lock_manager_operations|
3524 pipe_buf_operations|
3525 platform_hibernation_ops|
3526 platform_suspend_ops|
3531 soc_pcmcia_socket_ops|
3537 if ($line !~ /\bconst\b/ &&
3538 $line =~ /\bstruct\s+($struct_ops)\b/) {
3539 WARN("CONST_STRUCT",
3540 "struct $1 should normally be const\n" .
3544 # use of NR_CPUS is usually wrong
3545 # ignore definitions of NR_CPUS and usage to define arrays as likely right
3546 if ($line =~ /\bNR_CPUS\b/ &&
3547 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3548 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
3549 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3550 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3551 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
3554 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3557 # check for %L{u,d,i} in strings
3559 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3560 $string = substr($rawline, $-[1], $+[1] - $-[1]);
3561 $string =~ s/%%/__/g;
3562 if ($string =~ /(?<!%)%L[udi]/) {
3564 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
3569 # whine mightly about in_atomic
3570 if ($line =~ /\bin_atomic\s*\(/) {
3571 if ($realfile =~ m@^drivers/@) {
3573 "do not use in_atomic in drivers\n" . $herecurr);
3574 } elsif ($realfile !~ m@^kernel/@) {
3576 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
3580 # check for lockdep_set_novalidate_class
3581 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3582 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3583 if ($realfile !~ m@^kernel/lockdep@ &&
3584 $realfile !~ m@^include/linux/lockdep@ &&
3585 $realfile !~ m@^drivers/base/core@) {
3587 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
3591 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3592 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
3593 WARN("EXPORTED_WORLD_WRITABLE",
3594 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
3598 # If we have no input at all, then there is nothing to report on
3599 # so just keep quiet.
3600 if ($#rawlines == -1) {
3604 # In mailback mode only produce a report in the negative, for
3605 # things that appear to be patches.
3606 if ($mailback && ($clean == 1 || !$is_patch)) {
3610 # This is not a patch, and we are are in 'no-patch' mode so
3612 if (!$chk_patch && !$is_patch) {
3617 ERROR("NOT_UNIFIED_DIFF",
3618 "Does not appear to be a unified-diff format patch\n");
3620 if ($is_patch && $chk_signoff && $signoff == 0) {
3621 ERROR("MISSING_SIGN_OFF",
3622 "Missing Signed-off-by: line(s)\n");
3625 print report_dump();
3626 if ($summary && !($clean == 1 && $quiet == 1)) {
3627 print "$filename " if ($summary_file);
3628 print "total: $cnt_error errors, $cnt_warn warnings, " .
3629 (($check)? "$cnt_chk checks, " : "") .
3630 "$cnt_lines lines checked\n";
3631 print "\n" if ($quiet == 0);
3636 if ($^V lt 5.10.0) {
3637 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
3638 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
3641 # If there were whitespace errors which cleanpatch can fix
3642 # then suggest that.
3643 if ($rpt_cleaners) {
3644 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3645 print " scripts/cleanfile\n\n";
3650 if ($quiet == 0 && keys %ignore_type) {
3651 print "NOTE: Ignored message types:";
3652 foreach my $ignore (sort keys %ignore_type) {
3658 if ($clean == 1 && $quiet == 0) {
3659 print "$vname has no obvious style problems and is ready for submission.\n"
3661 if ($clean == 0 && $quiet == 0) {
3663 $vname has style problems, please review.
3665 If any of these errors are false positives, please report
3666 them to the maintainer, see CHECKPATCH in MAINTAINERS.