2 # (c) 2001, Dave Jones. (the file handling bit)
6 # Licensed under the terms of the GNU GPL License version 2
16 use Getopt::Long qw(:config no_auto_abbrev);
40 my $configuration_file = ".checkpatch.conf";
41 my $max_line_length = 80;
42 my $ignore_perl_version = 0;
43 my $minimum_perl_version = 5.10.0;
49 Usage: $P [OPTION]... [FILE]...
54 --no-tree run without a kernel tree
55 --no-signoff do not check for 'Signed-off-by' line
56 --patch treat FILE as patchfile (default)
57 --emacs emacs compile window format
58 --terse one line per report
59 -f, --file treat FILE as regular source file
60 --subjective, --strict enable more subjective tests
61 --types TYPE(,TYPE2...) show only these comma separated message types
62 --ignore TYPE(,TYPE2...) ignore various comma separated message types
63 --max-line-length=n set the maximum line length, if exceeded, warn
64 --show-types show the message "types" in the output
65 --root=PATH PATH to the kernel tree root
66 --no-summary suppress the per-file summary
67 --mailback only produce a report in case of warnings/errors
68 --summary-file include the filename in summary
69 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
70 'values', 'possible', 'type', and 'attr' (default
72 --test-only=WORD report only warnings/errors containing WORD
74 --fix EXPERIMENTAL - may create horrible results
75 If correctable single-line errors exist, create
76 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
77 with potential errors corrected to the preferred
79 --ignore-perl-version override checking of perl version. expect
81 -h, --help, --version display this help and exit
83 When FILE is - read standard input.
89 my $conf = which_conf($configuration_file);
92 open(my $conffile, '<', "$conf")
93 or warn "$P: Can't find a readable $configuration_file file $!\n";
98 $line =~ s/\s*\n?$//g;
102 next if ($line =~ m/^\s*#/);
103 next if ($line =~ m/^\s*$/);
105 my @words = split(" ", $line);
106 foreach my $word (@words) {
107 last if ($word =~ m/^#/);
108 push (@conf_args, $word);
112 unshift(@ARGV, @conf_args) if @conf_args;
116 'q|quiet+' => \$quiet,
118 'signoff!' => \$chk_signoff,
119 'patch!' => \$chk_patch,
123 'subjective!' => \$check,
124 'strict!' => \$check,
125 'ignore=s' => \@ignore,
127 'show-types!' => \$show_types,
128 'max-line-length=i' => \$max_line_length,
130 'summary!' => \$summary,
131 'mailback!' => \$mailback,
132 'summary-file!' => \$summary_file,
134 'ignore-perl-version!' => \$ignore_perl_version,
135 'debug=s' => \%debug,
136 'test-only=s' => \$tst_only,
145 if ($^V && $^V lt $minimum_perl_version) {
146 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
147 if (!$ignore_perl_version) {
153 print "$P: no input files\n";
157 sub hash_save_array_words {
158 my ($hashRef, $arrayRef) = @_;
160 my @array = split(/,/, join(',', @$arrayRef));
161 foreach my $word (@array) {
162 $word =~ s/\s*\n?$//g;
165 $word =~ tr/[a-z]/[A-Z]/;
167 next if ($word =~ m/^\s*#/);
168 next if ($word =~ m/^\s*$/);
174 sub hash_show_words {
175 my ($hashRef, $prefix) = @_;
177 if ($quiet == 0 && keys %$hashRef) {
178 print "NOTE: $prefix message types:";
179 foreach my $word (sort keys %$hashRef) {
186 hash_save_array_words(\%ignore_type, \@ignore);
187 hash_save_array_words(\%use_type, \@use);
190 my $dbg_possible = 0;
193 for my $key (keys %debug) {
195 eval "\${dbg_$key} = '$debug{$key}';";
199 my $rpt_cleaners = 0;
208 if (!top_of_kernel_tree($root)) {
209 die "$P: $root: --root does not point at a valid tree\n";
212 if (top_of_kernel_tree('.')) {
214 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
215 top_of_kernel_tree($1)) {
220 if (!defined $root) {
221 print "Must be run from the top-level dir. of a kernel tree\n";
226 my $emitted_corrupt = 0;
229 [A-Za-z_][A-Za-z\d_]*
230 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
232 our $Storage = qr{extern|static|asmlinkage};
244 our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
245 our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
246 our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
247 our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
248 our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
250 # Notes to $Attribute:
251 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
271 ____cacheline_aligned|
272 ____cacheline_aligned_in_smp|
273 ____cacheline_internodealigned_in_smp|
277 our $Inline = qr{inline|__always_inline|noinline};
278 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
279 our $Lval = qr{$Ident(?:$Member)*};
281 our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
282 our $Binary = qr{(?i)0b[01]+$Int_type?};
283 our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
284 our $Int = qr{[0-9]+$Int_type?};
285 our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
286 our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
287 our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
288 our $Float = qr{$Float_hex|$Float_dec|$Float_int};
289 our $Constant = qr{$Float|$Binary|$Hex|$Int};
290 our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
291 our $Compare = qr{<=|>=|==|!=|<|>};
292 our $Arithmetic = qr{\+|-|\*|\/|%};
296 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
300 our $NonptrTypeWithAttr;
304 our $NON_ASCII_UTF8 = qr{
305 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
306 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
307 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
308 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
309 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
310 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
311 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
315 [\x09\x0A\x0D\x20-\x7E] # ASCII
319 our $typeTypedefs = qr{(?x:
320 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
324 our $logFunctions = qr{(?x:
325 printk(?:_ratelimited|_once|)|
326 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
327 WARN(?:_RATELIMIT|_ONCE|)|
330 seq_vprintf|seq_printf|seq_puts
333 our $signature_tags = qr{(?xi:
346 qr{(?:unsigned\s+)?char},
347 qr{(?:unsigned\s+)?short},
348 qr{(?:unsigned\s+)?int},
349 qr{(?:unsigned\s+)?long},
350 qr{(?:unsigned\s+)?long\s+int},
351 qr{(?:unsigned\s+)?long\s+long},
352 qr{(?:unsigned\s+)?long\s+long\s+int},
361 qr{${Ident}_handler},
362 qr{${Ident}_handler_fn},
364 our @typeListWithAttr = (
366 qr{struct\s+$InitAttribute\s+$Ident},
367 qr{union\s+$InitAttribute\s+$Ident},
370 our @modifierList = (
374 our $allowed_asm_includes = qr{(?x:
378 # memory.h: ARM has a custom one
381 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
382 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
383 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
384 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
386 (?:$Modifier\s+|const\s+)*
388 (?:typeof|__typeof__)\s*\([^\)]*\)|
392 (?:\s+$Modifier|\s+const)*
394 $NonptrTypeWithAttr = qr{
395 (?:$Modifier\s+|const\s+)*
397 (?:typeof|__typeof__)\s*\([^\)]*\)|
401 (?:\s+$Modifier|\s+const)*
405 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
406 (?:\s+$Inline|\s+$Modifier)*
408 $Declare = qr{(?:$Storage\s+)?$Type};
412 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
414 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
415 # requires at least perl version v5.10.0
416 # Any use must be runtime checked with $^V
418 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
419 our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
420 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
424 return "" if (!defined($string));
425 $string =~ s@^\s*\(\s*@@g;
426 $string =~ s@\s*\)\s*$@@g;
427 $string =~ s@\s+@ @g;
431 sub seed_camelcase_file {
434 return if (!(-f $file));
438 open(my $include_file, '<', "$file")
439 or warn "$P: Can't read '$file' $!\n";
440 my $text = <$include_file>;
441 close($include_file);
443 my @lines = split('\n', $text);
445 foreach my $line (@lines) {
446 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
447 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
449 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
451 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
457 my $camelcase_seeded = 0;
458 sub seed_camelcase_includes {
459 return if ($camelcase_seeded);
462 my $camelcase_cache = "";
463 my @include_files = ();
465 $camelcase_seeded = 1;
468 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
469 chomp $git_last_include_commit;
470 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
472 my $last_mod_date = 0;
473 $files = `find $root/include -name "*.h"`;
474 @include_files = split('\n', $files);
475 foreach my $file (@include_files) {
476 my $date = POSIX::strftime("%Y%m%d%H%M",
477 localtime((stat $file)[9]));
478 $last_mod_date = $date if ($last_mod_date < $date);
480 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
483 if ($camelcase_cache ne "" && -f $camelcase_cache) {
484 open(my $camelcase_file, '<', "$camelcase_cache")
485 or warn "$P: Can't read '$camelcase_cache' $!\n";
486 while (<$camelcase_file>) {
490 close($camelcase_file);
496 $files = `git ls-files "include/*.h"`;
497 @include_files = split('\n', $files);
500 foreach my $file (@include_files) {
501 seed_camelcase_file($file);
504 if ($camelcase_cache ne "") {
505 unlink glob ".checkpatch-camelcase.*";
506 open(my $camelcase_file, '>', "$camelcase_cache")
507 or warn "$P: Can't write '$camelcase_cache' $!\n";
508 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
509 print $camelcase_file ("$_\n");
511 close($camelcase_file);
515 $chk_signoff = 0 if ($file);
521 for my $filename (@ARGV) {
524 open($FILE, '-|', "diff -u /dev/null $filename") ||
525 die "$P: $filename: diff failed - $!\n";
526 } elsif ($filename eq '-') {
527 open($FILE, '<&STDIN');
529 open($FILE, '<', "$filename") ||
530 die "$P: $filename: open failed - $!\n";
532 if ($filename eq '-') {
533 $vname = 'Your patch';
542 if (!process($filename)) {
552 sub top_of_kernel_tree {
556 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
557 "README", "Documentation", "arch", "include", "drivers",
558 "fs", "init", "ipc", "kernel", "lib", "scripts",
561 foreach my $check (@tree_check) {
562 if (! -e $root . '/' . $check) {
570 my ($formatted_email) = @_;
576 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
579 $comment = $3 if defined $3;
580 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
582 $comment = $2 if defined $2;
583 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
585 $comment = $2 if defined $2;
586 $formatted_email =~ s/$address.*$//;
587 $name = $formatted_email;
589 $name =~ s/^\"|\"$//g;
590 # If there's a name left after stripping spaces and
591 # leading quotes, and the address doesn't have both
592 # leading and trailing angle brackets, the address
596 if ($name ne "" && $address !~ /^<[^>]+>$/) {
604 $name =~ s/^\"|\"$//g;
605 $address = trim($address);
606 $address =~ s/^\<|\>$//g;
608 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
609 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
613 return ($name, $address, $comment);
617 my ($name, $address) = @_;
622 $name =~ s/^\"|\"$//g;
623 $address = trim($address);
625 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
626 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
631 $formatted_email = "$address";
633 $formatted_email = "$name <$address>";
636 return $formatted_email;
642 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
643 if (-e "$path/$conf") {
644 return "$path/$conf";
656 for my $c (split(//, $str)) {
660 for (; ($n % 8) != 0; $n++) {
672 (my $res = shift) =~ tr/\t/ /c;
679 # Drop the diff line leader and expand tabs
681 $line = expand_tabs($line);
683 # Pick the indent from the front of the line.
684 my ($white) = ($line =~ /^(\s*)/);
686 return (length($line), length($white));
689 my $sanitise_quote = '';
691 sub sanitise_line_reset {
692 my ($in_comment) = @_;
695 $sanitise_quote = '*/';
697 $sanitise_quote = '';
710 # Always copy over the diff marker.
711 $res = substr($line, 0, 1);
713 for ($off = 1; $off < length($line); $off++) {
714 $c = substr($line, $off, 1);
716 # Comments we are wacking completly including the begin
717 # and end, all to $;.
718 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
719 $sanitise_quote = '*/';
721 substr($res, $off, 2, "$;$;");
725 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
726 $sanitise_quote = '';
727 substr($res, $off, 2, "$;$;");
731 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
732 $sanitise_quote = '//';
734 substr($res, $off, 2, $sanitise_quote);
739 # A \ in a string means ignore the next character.
740 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
742 substr($res, $off, 2, 'XX');
747 if ($c eq "'" || $c eq '"') {
748 if ($sanitise_quote eq '') {
749 $sanitise_quote = $c;
751 substr($res, $off, 1, $c);
753 } elsif ($sanitise_quote eq $c) {
754 $sanitise_quote = '';
758 #print "c<$c> SQ<$sanitise_quote>\n";
759 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
760 substr($res, $off, 1, $;);
761 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
762 substr($res, $off, 1, $;);
763 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
764 substr($res, $off, 1, 'X');
766 substr($res, $off, 1, $c);
770 if ($sanitise_quote eq '//') {
771 $sanitise_quote = '';
774 # The pathname on a #include may be surrounded by '<' and '>'.
775 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
776 my $clean = 'X' x length($1);
777 $res =~ s@\<.*\>@<$clean>@;
779 # The whole of a #error is a string.
780 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
781 my $clean = 'X' x length($1);
782 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
788 sub get_quoted_string {
789 my ($line, $rawline) = @_;
791 return "" if ($line !~ m/(\"[X]+\")/g);
792 return substr($rawline, $-[0], $+[0] - $-[0]);
795 sub ctx_statement_block {
796 my ($linenr, $remain, $off) = @_;
797 my $line = $linenr - 1;
814 @stack = (['', 0]) if ($#stack == -1);
816 #warn "CSB: blk<$blk> remain<$remain>\n";
817 # If we are about to drop off the end, pull in more
820 for (; $remain > 0; $line++) {
821 last if (!defined $lines[$line]);
822 next if ($lines[$line] =~ /^-/);
825 $blk .= $lines[$line] . "\n";
830 # Bail if there is no further context.
831 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
835 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
841 $c = substr($blk, $off, 1);
842 $remainder = substr($blk, $off);
844 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
846 # Handle nested #if/#else.
847 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
848 push(@stack, [ $type, $level ]);
849 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
850 ($type, $level) = @{$stack[$#stack - 1]};
851 } elsif ($remainder =~ /^#\s*endif\b/) {
852 ($type, $level) = @{pop(@stack)};
855 # Statement ends at the ';' or a close '}' at the
857 if ($level == 0 && $c eq ';') {
861 # An else is really a conditional as long as its not else if
862 if ($level == 0 && $coff_set == 0 &&
863 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
864 $remainder =~ /^(else)(?:\s|{)/ &&
865 $remainder !~ /^else\s+if\b/) {
866 $coff = $off + length($1) - 1;
868 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
869 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
872 if (($type eq '' || $type eq '(') && $c eq '(') {
876 if ($type eq '(' && $c eq ')') {
878 $type = ($level != 0)? '(' : '';
880 if ($level == 0 && $coff < $soff) {
883 #warn "CSB: mark coff<$coff>\n";
886 if (($type eq '' || $type eq '{') && $c eq '{') {
890 if ($type eq '{' && $c eq '}') {
892 $type = ($level != 0)? '{' : '';
895 if (substr($blk, $off + 1, 1) eq ';') {
901 # Preprocessor commands end at the newline unless escaped.
902 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
910 # We are truly at the end, so shuffle to the next line.
917 my $statement = substr($blk, $soff, $off - $soff + 1);
918 my $condition = substr($blk, $soff, $coff - $soff + 1);
920 #warn "STATEMENT<$statement>\n";
921 #warn "CONDITION<$condition>\n";
923 #print "coff<$coff> soff<$off> loff<$loff>\n";
925 return ($statement, $condition,
926 $line, $remain + 1, $off - $loff + 1, $level);
929 sub statement_lines {
932 # Strip the diff line prefixes and rip blank lines at start and end.
933 $stmt =~ s/(^|\n)./$1/g;
937 my @stmt_lines = ($stmt =~ /\n/g);
939 return $#stmt_lines + 2;
942 sub statement_rawlines {
945 my @stmt_lines = ($stmt =~ /\n/g);
947 return $#stmt_lines + 2;
950 sub statement_block_size {
953 $stmt =~ s/(^|\n)./$1/g;
959 my @stmt_lines = ($stmt =~ /\n/g);
960 my @stmt_statements = ($stmt =~ /;/g);
962 my $stmt_lines = $#stmt_lines + 2;
963 my $stmt_statements = $#stmt_statements + 1;
965 if ($stmt_lines > $stmt_statements) {
968 return $stmt_statements;
972 sub ctx_statement_full {
973 my ($linenr, $remain, $off) = @_;
974 my ($statement, $condition, $level);
978 # Grab the first conditional/block pair.
979 ($statement, $condition, $linenr, $remain, $off, $level) =
980 ctx_statement_block($linenr, $remain, $off);
981 #print "F: c<$condition> s<$statement> remain<$remain>\n";
982 push(@chunks, [ $condition, $statement ]);
983 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
984 return ($level, $linenr, @chunks);
987 # Pull in the following conditional/block pairs and see if they
988 # could continue the statement.
990 ($statement, $condition, $linenr, $remain, $off, $level) =
991 ctx_statement_block($linenr, $remain, $off);
992 #print "C: c<$condition> s<$statement> remain<$remain>\n";
993 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
995 push(@chunks, [ $condition, $statement ]);
998 return ($level, $linenr, @chunks);
1002 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1004 my $start = $linenr - 1;
1011 my @stack = ($level);
1012 for ($line = $start; $remain > 0; $line++) {
1013 next if ($rawlines[$line] =~ /^-/);
1016 $blk .= $rawlines[$line];
1018 # Handle nested #if/#else.
1019 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1020 push(@stack, $level);
1021 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1022 $level = $stack[$#stack - 1];
1023 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1024 $level = pop(@stack);
1027 foreach my $c (split(//, $lines[$line])) {
1028 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1034 if ($c eq $close && $level > 0) {
1036 last if ($level == 0);
1037 } elsif ($c eq $open) {
1042 if (!$outer || $level <= 1) {
1043 push(@res, $rawlines[$line]);
1046 last if ($level == 0);
1049 return ($level, @res);
1051 sub ctx_block_outer {
1052 my ($linenr, $remain) = @_;
1054 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1058 my ($linenr, $remain) = @_;
1060 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1064 my ($linenr, $remain, $off) = @_;
1066 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1069 sub ctx_block_level {
1070 my ($linenr, $remain) = @_;
1072 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1074 sub ctx_statement_level {
1075 my ($linenr, $remain, $off) = @_;
1077 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1080 sub ctx_locate_comment {
1081 my ($first_line, $end_line) = @_;
1083 # Catch a comment on the end of the line itself.
1084 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1085 return $current_comment if (defined $current_comment);
1087 # Look through the context and try and figure out if there is a
1090 $current_comment = '';
1091 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1092 my $line = $rawlines[$linenr - 1];
1094 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1097 if ($line =~ m@/\*@) {
1100 if (!$in_comment && $current_comment ne '') {
1101 $current_comment = '';
1103 $current_comment .= $line . "\n" if ($in_comment);
1104 if ($line =~ m@\*/@) {
1109 chomp($current_comment);
1110 return($current_comment);
1112 sub ctx_has_comment {
1113 my ($first_line, $end_line) = @_;
1114 my $cmt = ctx_locate_comment($first_line, $end_line);
1116 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1117 ##print "CMMT: $cmt\n";
1119 return ($cmt ne '');
1123 my ($linenr, $cnt) = @_;
1125 my $offset = $linenr - 1;
1130 $line = $rawlines[$offset++];
1131 next if (defined($line) && $line =~ /^-/);
1143 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1146 $coded = sprintf("^%c", unpack('C', $2) + 64);
1155 my $av_preprocessor = 0;
1160 sub annotate_reset {
1161 $av_preprocessor = 0;
1163 @av_paren_type = ('E');
1164 $av_pend_colon = 'O';
1167 sub annotate_values {
1168 my ($stream, $type) = @_;
1171 my $var = '_' x length($stream);
1174 print "$stream\n" if ($dbg_values > 1);
1176 while (length($cur)) {
1177 @av_paren_type = ('E') if ($#av_paren_type < 0);
1178 print " <" . join('', @av_paren_type) .
1179 "> <$type> <$av_pending>" if ($dbg_values > 1);
1180 if ($cur =~ /^(\s+)/o) {
1181 print "WS($1)\n" if ($dbg_values > 1);
1182 if ($1 =~ /\n/ && $av_preprocessor) {
1183 $type = pop(@av_paren_type);
1184 $av_preprocessor = 0;
1187 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1188 print "CAST($1)\n" if ($dbg_values > 1);
1189 push(@av_paren_type, $type);
1192 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1193 print "DECLARE($1)\n" if ($dbg_values > 1);
1196 } elsif ($cur =~ /^($Modifier)\s*/) {
1197 print "MODIFIER($1)\n" if ($dbg_values > 1);
1200 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1201 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1202 $av_preprocessor = 1;
1203 push(@av_paren_type, $type);
1209 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1210 print "UNDEF($1)\n" if ($dbg_values > 1);
1211 $av_preprocessor = 1;
1212 push(@av_paren_type, $type);
1214 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1215 print "PRE_START($1)\n" if ($dbg_values > 1);
1216 $av_preprocessor = 1;
1218 push(@av_paren_type, $type);
1219 push(@av_paren_type, $type);
1222 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1223 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1224 $av_preprocessor = 1;
1226 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1230 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1231 print "PRE_END($1)\n" if ($dbg_values > 1);
1233 $av_preprocessor = 1;
1235 # Assume all arms of the conditional end as this
1236 # one does, and continue as if the #endif was not here.
1237 pop(@av_paren_type);
1238 push(@av_paren_type, $type);
1241 } elsif ($cur =~ /^(\\\n)/o) {
1242 print "PRECONT($1)\n" if ($dbg_values > 1);
1244 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1245 print "ATTR($1)\n" if ($dbg_values > 1);
1246 $av_pending = $type;
1249 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1250 print "SIZEOF($1)\n" if ($dbg_values > 1);
1256 } elsif ($cur =~ /^(if|while|for)\b/o) {
1257 print "COND($1)\n" if ($dbg_values > 1);
1261 } elsif ($cur =~/^(case)/o) {
1262 print "CASE($1)\n" if ($dbg_values > 1);
1263 $av_pend_colon = 'C';
1266 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1267 print "KEYWORD($1)\n" if ($dbg_values > 1);
1270 } elsif ($cur =~ /^(\()/o) {
1271 print "PAREN('$1')\n" if ($dbg_values > 1);
1272 push(@av_paren_type, $av_pending);
1276 } elsif ($cur =~ /^(\))/o) {
1277 my $new_type = pop(@av_paren_type);
1278 if ($new_type ne '_') {
1280 print "PAREN('$1') -> $type\n"
1281 if ($dbg_values > 1);
1283 print "PAREN('$1')\n" if ($dbg_values > 1);
1286 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1287 print "FUNC($1)\n" if ($dbg_values > 1);
1291 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1292 if (defined $2 && $type eq 'C' || $type eq 'T') {
1293 $av_pend_colon = 'B';
1294 } elsif ($type eq 'E') {
1295 $av_pend_colon = 'L';
1297 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1300 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1301 print "IDENT($1)\n" if ($dbg_values > 1);
1304 } elsif ($cur =~ /^($Assignment)/o) {
1305 print "ASSIGN($1)\n" if ($dbg_values > 1);
1308 } elsif ($cur =~/^(;|{|})/) {
1309 print "END($1)\n" if ($dbg_values > 1);
1311 $av_pend_colon = 'O';
1313 } elsif ($cur =~/^(,)/) {
1314 print "COMMA($1)\n" if ($dbg_values > 1);
1317 } elsif ($cur =~ /^(\?)/o) {
1318 print "QUESTION($1)\n" if ($dbg_values > 1);
1321 } elsif ($cur =~ /^(:)/o) {
1322 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1324 substr($var, length($res), 1, $av_pend_colon);
1325 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1330 $av_pend_colon = 'O';
1332 } elsif ($cur =~ /^(\[)/o) {
1333 print "CLOSE($1)\n" if ($dbg_values > 1);
1336 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1339 print "OPV($1)\n" if ($dbg_values > 1);
1346 substr($var, length($res), 1, $variant);
1349 } elsif ($cur =~ /^($Operators)/o) {
1350 print "OP($1)\n" if ($dbg_values > 1);
1351 if ($1 ne '++' && $1 ne '--') {
1355 } elsif ($cur =~ /(^.)/o) {
1356 print "C($1)\n" if ($dbg_values > 1);
1359 $cur = substr($cur, length($1));
1360 $res .= $type x length($1);
1364 return ($res, $var);
1368 my ($possible, $line) = @_;
1369 my $notPermitted = qr{(?:
1386 ^(?:typedef|struct|enum)\b
1388 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1389 if ($possible !~ $notPermitted) {
1390 # Check for modifiers.
1391 $possible =~ s/\s*$Storage\s*//g;
1392 $possible =~ s/\s*$Sparse\s*//g;
1393 if ($possible =~ /^\s*$/) {
1395 } elsif ($possible =~ /\s/) {
1396 $possible =~ s/\s*$Type\s*//g;
1397 for my $modifier (split(' ', $possible)) {
1398 if ($modifier !~ $notPermitted) {
1399 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1400 push(@modifierList, $modifier);
1405 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1406 push(@typeList, $possible);
1410 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1417 return defined $use_type{$_[0]} if (scalar keys %use_type > 0);
1419 return !defined $ignore_type{$_[0]};
1423 if (!show_type($_[1]) ||
1424 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1429 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1431 $line = "$prefix$_[0]: $_[2]\n";
1433 $line = (split('\n', $line))[0] . "\n" if ($terse);
1435 push(our @report, $line);
1444 if (report("ERROR", $_[0], $_[1])) {
1452 if (report("WARNING", $_[0], $_[1])) {
1460 if ($check && report("CHECK", $_[0], $_[1])) {
1468 sub check_absolute_file {
1469 my ($absolute, $herecurr) = @_;
1470 my $file = $absolute;
1472 ##print "absolute<$absolute>\n";
1474 # See if any suffix of this path is a path within the tree.
1475 while ($file =~ s@^[^/]*/@@) {
1476 if (-f "$root/$file") {
1477 ##print "file<$file>\n";
1485 # It is, so see if the prefix is acceptable.
1486 my $prefix = $absolute;
1487 substr($prefix, -length($file)) = '';
1489 ##print "prefix<$prefix>\n";
1490 if ($prefix ne ".../") {
1491 WARN("USE_RELATIVE_PATH",
1492 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1499 $string =~ s/^\s+|\s+$//g;
1507 $string =~ s/^\s+//;
1515 $string =~ s/\s+$//;
1520 sub string_find_replace {
1521 my ($string, $find, $replace) = @_;
1523 $string =~ s/$find/$replace/g;
1531 my $source_indent = 8;
1532 my $max_spaces_before_tab = $source_indent - 1;
1533 my $spaces_to_tab = " " x $source_indent;
1535 #convert leading spaces to tabs
1536 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1537 #Remove spaces before a tab
1538 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1543 sub pos_last_openparen {
1548 my $opens = $line =~ tr/\(/\(/;
1549 my $closes = $line =~ tr/\)/\)/;
1551 my $last_openparen = 0;
1553 if (($opens == 0) || ($closes >= $opens)) {
1557 my $len = length($line);
1559 for ($pos = 0; $pos < $len; $pos++) {
1560 my $string = substr($line, $pos);
1561 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1562 $pos += length($1) - 1;
1563 } elsif (substr($line, $pos, 1) eq '(') {
1564 $last_openparen = $pos;
1565 } elsif (index($string, '(') == -1) {
1570 return $last_openparen + 1;
1574 my $filename = shift;
1580 my $stashrawline="";
1591 my $in_header_lines = 1;
1592 my $in_commit_log = 0; #Scanning lines before patch
1594 my $non_utf8_charset = 0;
1602 # Trace the real file/line as we go.
1608 my $comment_edge = 0;
1612 my $prev_values = 'E';
1615 my %suppress_ifbraces;
1616 my %suppress_whiletrailers;
1617 my %suppress_export;
1618 my $suppress_statement = 0;
1620 my %signatures = ();
1622 # Pre-scan the patch sanitizing the lines.
1623 # Pre-scan the patch looking for any __setup documentation.
1625 my @setup_docs = ();
1628 my $camelcase_file_seeded = 0;
1630 sanitise_line_reset();
1632 foreach my $rawline (@rawlines) {
1636 push(@fixed, $rawline) if ($fix);
1638 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1640 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1645 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1654 # Guestimate if this is a continuing comment. Run
1655 # the context looking for a comment "edge". If this
1656 # edge is a close comment then we must be in a comment
1660 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1661 next if (defined $rawlines[$ln - 1] &&
1662 $rawlines[$ln - 1] =~ /^-/);
1664 #print "RAW<$rawlines[$ln - 1]>\n";
1665 last if (!defined $rawlines[$ln - 1]);
1666 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1667 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1672 if (defined $edge && $edge eq '*/') {
1676 # Guestimate if this is a continuing comment. If this
1677 # is the start of a diff block and this line starts
1678 # ' *' then it is very likely a comment.
1679 if (!defined $edge &&
1680 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1685 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1686 sanitise_line_reset($in_comment);
1688 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1689 # Standardise the strings and chars within the input to
1690 # simplify matching -- only bother with positive lines.
1691 $line = sanitise_line($rawline);
1693 push(@lines, $line);
1696 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1701 #print "==>$rawline\n";
1702 #print "-->$line\n";
1704 if ($setup_docs && $line =~ /^\+/) {
1705 push(@setup_docs, $line);
1713 foreach my $line (@lines) {
1715 my $sline = $line; #copy of $line
1716 $sline =~ s/$;/ /g; #with comments as spaces
1718 my $rawline = $rawlines[$linenr - 1];
1720 #extract the line range in the file after the patch is applied
1721 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1723 $first_line = $linenr + 1;
1733 %suppress_ifbraces = ();
1734 %suppress_whiletrailers = ();
1735 %suppress_export = ();
1736 $suppress_statement = 0;
1739 # track the line number as we move through the hunk, note that
1740 # new versions of GNU diff omit the leading space on completely
1741 # blank context lines so we need to count that too.
1742 } elsif ($line =~ /^( |\+|$)/) {
1744 $realcnt-- if ($realcnt != 0);
1746 # Measure the line length and indent.
1747 ($length, $indent) = line_stats($rawline);
1749 # Track the previous line.
1750 ($prevline, $stashline) = ($stashline, $line);
1751 ($previndent, $stashindent) = ($stashindent, $indent);
1752 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1754 #warn "line<$line>\n";
1756 } elsif ($realcnt == 1) {
1760 my $hunk_line = ($realcnt != 0);
1762 #make up the handle for any error we report on this line
1763 $prefix = "$filename:$realline: " if ($emacs && $file);
1764 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1766 $here = "#$linenr: " if (!$file);
1767 $here = "#$realline: " if ($file);
1769 # extract the filename as it passes
1770 if ($line =~ /^diff --git.*?(\S+)$/) {
1772 $realfile =~ s@^([^/]*)/@@ if (!$file);
1774 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1776 $realfile =~ s@^([^/]*)/@@ if (!$file);
1780 if (!$file && $tree && $p1_prefix ne '' &&
1781 -e "$root/$p1_prefix") {
1782 WARN("PATCH_PREFIX",
1783 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1786 if ($realfile =~ m@^include/asm/@) {
1787 ERROR("MODIFIED_INCLUDE_ASM",
1788 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1793 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1795 my $hereline = "$here\n$rawline\n";
1796 my $herecurr = "$here\n$rawline\n";
1797 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1799 $cnt_lines++ if ($realcnt != 0);
1801 # Check for incorrect file permissions
1802 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1803 my $permhere = $here . "FILE: $realfile\n";
1804 if ($realfile !~ m@scripts/@ &&
1805 $realfile !~ /\.(py|pl|awk|sh)$/) {
1806 ERROR("EXECUTE_PERMISSIONS",
1807 "do not set execute permissions for source files\n" . $permhere);
1811 # Check the patch for a signoff:
1812 if ($line =~ /^\s*signed-off-by:/i) {
1817 # Check signature styles
1818 if (!$in_header_lines &&
1819 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
1820 my $space_before = $1;
1822 my $space_after = $3;
1824 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1826 if ($sign_off !~ /$signature_tags/) {
1827 WARN("BAD_SIGN_OFF",
1828 "Non-standard signature: $sign_off\n" . $herecurr);
1830 if (defined $space_before && $space_before ne "") {
1831 if (WARN("BAD_SIGN_OFF",
1832 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1834 $fixed[$linenr - 1] =
1835 "$ucfirst_sign_off $email";
1838 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1839 if (WARN("BAD_SIGN_OFF",
1840 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1842 $fixed[$linenr - 1] =
1843 "$ucfirst_sign_off $email";
1847 if (!defined $space_after || $space_after ne " ") {
1848 if (WARN("BAD_SIGN_OFF",
1849 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1851 $fixed[$linenr - 1] =
1852 "$ucfirst_sign_off $email";
1856 my ($email_name, $email_address, $comment) = parse_email($email);
1857 my $suggested_email = format_email(($email_name, $email_address));
1858 if ($suggested_email eq "") {
1859 ERROR("BAD_SIGN_OFF",
1860 "Unrecognized email address: '$email'\n" . $herecurr);
1862 my $dequoted = $suggested_email;
1863 $dequoted =~ s/^"//;
1864 $dequoted =~ s/" </ </;
1865 # Don't force email to have quotes
1866 # Allow just an angle bracketed address
1867 if ("$dequoted$comment" ne $email &&
1868 "<$email_address>$comment" ne $email &&
1869 "$suggested_email$comment" ne $email) {
1870 WARN("BAD_SIGN_OFF",
1871 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
1875 # Check for duplicate signatures
1876 my $sig_nospace = $line;
1877 $sig_nospace =~ s/\s//g;
1878 $sig_nospace = lc($sig_nospace);
1879 if (defined $signatures{$sig_nospace}) {
1880 WARN("BAD_SIGN_OFF",
1881 "Duplicate signature\n" . $herecurr);
1883 $signatures{$sig_nospace} = 1;
1887 # Check for wrappage within a valid hunk of the file
1888 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1889 ERROR("CORRUPTED_PATCH",
1890 "patch seems to be corrupt (line wrapped?)\n" .
1891 $herecurr) if (!$emitted_corrupt++);
1894 # Check for absolute kernel paths.
1896 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1899 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1900 check_absolute_file($1, $herecurr)) {
1903 check_absolute_file($file, $herecurr);
1908 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1909 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1910 $rawline !~ m/^$UTF8*$/) {
1911 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1913 my $blank = copy_spacing($rawline);
1914 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1915 my $hereptr = "$hereline$ptr\n";
1918 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1921 # Check if it's the start of a commit log
1922 # (not a header line and we haven't seen the patch filename)
1923 if ($in_header_lines && $realfile =~ /^$/ &&
1924 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
1925 $in_header_lines = 0;
1929 # Check if there is UTF-8 in a commit log when a mail header has explicitly
1930 # declined it, i.e defined some charset where it is missing.
1931 if ($in_header_lines &&
1932 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1934 $non_utf8_charset = 1;
1937 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
1938 $rawline =~ /$NON_ASCII_UTF8/) {
1939 WARN("UTF8_BEFORE_PATCH",
1940 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1943 # ignore non-hunk lines and lines being removed
1944 next if (!$hunk_line || $line =~ /^-/);
1946 #trailing whitespace
1947 if ($line =~ /^\+.*\015/) {
1948 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1949 if (ERROR("DOS_LINE_ENDINGS",
1950 "DOS line endings\n" . $herevet) &&
1952 $fixed[$linenr - 1] =~ s/[\s\015]+$//;
1954 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1955 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1956 if (ERROR("TRAILING_WHITESPACE",
1957 "trailing whitespace\n" . $herevet) &&
1959 $fixed[$linenr - 1] =~ s/\s+$//;
1965 # Check for FSF mailing addresses.
1966 if ($rawline =~ /You should have received a copy/ ||
1967 $rawline =~ /write to the Free Software/ ||
1968 $rawline =~ /59 Temple Place/ ||
1969 $rawline =~ /51 Franklin Street/) {
1970 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1971 my $msg_type = \&ERROR;
1972 $msg_type = \&CHK if ($file);
1973 &{$msg_type}("FSF_MAILING_ADDRESS",
1974 "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet)
1977 # check for Kconfig help text having a real description
1978 # Only applies when adding the entry originally, after that we do not have
1979 # sufficient context to determine whether it is indeed long enough.
1980 if ($realfile =~ /Kconfig/ &&
1981 $line =~ /.\s*config\s+/) {
1984 my $ln = $linenr + 1;
1988 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
1989 $f = $lines[$ln - 1];
1990 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1991 $is_end = $lines[$ln - 1] =~ /^\+/;
1993 next if ($f =~ /^-/);
1995 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1997 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
2004 next if ($f =~ /^$/);
2005 if ($f =~ /^\s*config\s/) {
2011 WARN("CONFIG_DESCRIPTION",
2012 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
2013 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
2016 # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2017 if ($realfile =~ /Kconfig/ &&
2018 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2019 WARN("CONFIG_EXPERIMENTAL",
2020 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2023 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2024 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2027 'EXTRA_AFLAGS' => 'asflags-y',
2028 'EXTRA_CFLAGS' => 'ccflags-y',
2029 'EXTRA_CPPFLAGS' => 'cppflags-y',
2030 'EXTRA_LDFLAGS' => 'ldflags-y',
2033 WARN("DEPRECATED_VARIABLE",
2034 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2037 # check we are in a valid source file if not then ignore this hunk
2038 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
2041 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2042 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
2043 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
2044 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
2045 $length > $max_line_length)
2048 "line over $max_line_length characters\n" . $herecurr);
2051 # Check for user-visible strings broken across lines, which breaks the ability
2052 # to grep for the string. Limited to strings used as parameters (those
2053 # following an open parenthesis), which almost completely eliminates false
2054 # positives, as well as warning only once per parameter rather than once per
2055 # line of the string. Make an exception when the previous string ends in a
2056 # newline (multiple lines in one string constant) or \n\t (common in inline
2057 # assembly to indent the instruction on the following line).
2058 if ($line =~ /^\+\s*"/ &&
2059 $prevline =~ /"\s*$/ &&
2060 $prevline =~ /\(/ &&
2061 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
2062 WARN("SPLIT_STRING",
2063 "quoted string split across lines\n" . $hereprev);
2066 # check for spaces before a quoted newline
2067 if ($rawline =~ /^.*\".*\s\\n/) {
2068 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2069 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2071 $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
2076 # check for adding lines without a newline.
2077 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2078 WARN("MISSING_EOF_NEWLINE",
2079 "adding a line without newline at end of file\n" . $herecurr);
2082 # Blackfin: use hi/lo macros
2083 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2084 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2085 my $herevet = "$here\n" . cat_vet($line) . "\n";
2087 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
2089 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2090 my $herevet = "$here\n" . cat_vet($line) . "\n";
2092 "use the HI() macro, not (... >> 16)\n" . $herevet);
2096 # check we are in a valid source file C or perl if not then ignore this hunk
2097 next if ($realfile !~ /\.(h|c|pl)$/);
2099 # at the beginning of a line any tabs must come first and anything
2100 # more than 8 must use tabs.
2101 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2102 $rawline =~ /^\+\s* \s*/) {
2103 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2105 if (ERROR("CODE_INDENT",
2106 "code indent should use tabs where possible\n" . $herevet) &&
2108 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2112 # check for space before tabs.
2113 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2114 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2115 if (WARN("SPACE_BEFORE_TAB",
2116 "please, no space before tabs\n" . $herevet) &&
2118 $fixed[$linenr - 1] =~
2119 s/(^\+.*) +\t/$1\t/;
2123 # check for && or || at the start of a line
2124 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2125 CHK("LOGICAL_CONTINUATIONS",
2126 "Logical continuations should be on the previous line\n" . $hereprev);
2129 # check multi-line statement indentation matches previous line
2130 if ($^V && $^V ge 5.10.0 &&
2131 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2132 $prevline =~ /^\+(\t*)(.*)$/;
2136 my $pos = pos_last_openparen($rest);
2138 $line =~ /^(\+| )([ \t]*)/;
2141 my $goodtabindent = $oldindent .
2144 my $goodspaceindent = $oldindent . " " x $pos;
2146 if ($newindent ne $goodtabindent &&
2147 $newindent ne $goodspaceindent) {
2149 if (CHK("PARENTHESIS_ALIGNMENT",
2150 "Alignment should match open parenthesis\n" . $hereprev) &&
2151 $fix && $line =~ /^\+/) {
2152 $fixed[$linenr - 1] =~
2153 s/^\+[ \t]*/\+$goodtabindent/;
2159 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
2161 "No space is necessary after a cast\n" . $hereprev) &&
2163 $fixed[$linenr - 1] =~
2164 s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
2168 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2169 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2170 $rawline =~ /^\+[ \t]*\*/) {
2171 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2172 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2175 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2176 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2177 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
2178 $rawline =~ /^\+/ && #line is new
2179 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2180 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2181 "networking block comments start with * on subsequent lines\n" . $hereprev);
2184 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2185 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2186 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2187 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2188 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
2189 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2190 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2193 # check for spaces at the beginning of a line.
2195 # 1) within comments
2196 # 2) indented preprocessor commands
2198 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
2199 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2200 if (WARN("LEADING_SPACE",
2201 "please, no spaces at the start of a line\n" . $herevet) &&
2203 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2207 # check we are in a valid C source file if not then ignore this hunk
2208 next if ($realfile !~ /\.(h|c)$/);
2210 # discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2211 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2212 WARN("CONFIG_EXPERIMENTAL",
2213 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2216 # check for RCS/CVS revision markers
2217 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2219 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2222 # Blackfin: don't use __builtin_bfin_[cs]sync
2223 if ($line =~ /__builtin_bfin_csync/) {
2224 my $herevet = "$here\n" . cat_vet($line) . "\n";
2226 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
2228 if ($line =~ /__builtin_bfin_ssync/) {
2229 my $herevet = "$here\n" . cat_vet($line) . "\n";
2231 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
2234 # check for old HOTPLUG __dev<foo> section markings
2235 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2236 WARN("HOTPLUG_SECTION",
2237 "Using $1 is unnecessary\n" . $herecurr);
2240 # Check for potential 'bare' types
2241 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2243 #print "LINE<$line>\n";
2244 if ($linenr >= $suppress_statement &&
2245 $realcnt && $sline =~ /.\s*\S/) {
2246 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2247 ctx_statement_block($linenr, $realcnt, 0);
2248 $stat =~ s/\n./\n /g;
2249 $cond =~ s/\n./\n /g;
2251 #print "linenr<$linenr> <$stat>\n";
2252 # If this statement has no statement boundaries within
2253 # it there is no point in retrying a statement scan
2254 # until we hit end of it.
2255 my $frag = $stat; $frag =~ s/;+\s*$//;
2256 if ($frag !~ /(?:{|;)/) {
2257 #print "skip<$line_nr_next>\n";
2258 $suppress_statement = $line_nr_next;
2261 # Find the real next line.
2262 $realline_next = $line_nr_next;
2263 if (defined $realline_next &&
2264 (!defined $lines[$realline_next - 1] ||
2265 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2272 # Ignore goto labels.
2273 if ($s =~ /$Ident:\*$/s) {
2275 # Ignore functions being called
2276 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2278 } elsif ($s =~ /^.\s*else\b/s) {
2280 # declarations always start with types
2281 } 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) {
2284 possible($type, "A:" . $s);
2286 # definitions in global scope can only start with types
2287 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2288 possible($1, "B:" . $s);
2291 # any (foo ... *) is a pointer cast, and foo is a type
2292 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2293 possible($1, "C:" . $s);
2296 # Check for any sort of function declaration.
2297 # int foo(something bar, other baz);
2298 # void (*store_gdt)(x86_descr_ptr *);
2299 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
2300 my ($name_len) = length($1);
2303 substr($ctx, 0, $name_len + 1, '');
2304 $ctx =~ s/\)[^\)]*$//;
2306 for my $arg (split(/\s*,\s*/, $ctx)) {
2307 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
2309 possible($1, "D:" . $s);
2317 # Checks which may be anchored in the context.
2320 # Check for switch () and associated case and default
2321 # statements should be at the same indent.
2322 if ($line=~/\bswitch\s*\(.*\)/) {
2325 my @ctx = ctx_block_outer($linenr, $realcnt);
2327 for my $ctx (@ctx) {
2328 my ($clen, $cindent) = line_stats($ctx);
2329 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2330 $indent != $cindent) {
2331 $err .= "$sep$ctx\n";
2338 ERROR("SWITCH_CASE_INDENT_LEVEL",
2339 "switch and case should be at the same indent\n$hereline$err");
2343 # if/while/etc brace do not go on next line, unless defining a do while loop,
2344 # or if that brace on the next line is for something else
2345 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2346 my $pre_ctx = "$1$2";
2348 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
2350 if ($line =~ /^\+\t{6,}/) {
2351 WARN("DEEP_INDENTATION",
2352 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2355 my $ctx_cnt = $realcnt - $#ctx - 1;
2356 my $ctx = join("\n", @ctx);
2358 my $ctx_ln = $linenr;
2359 my $ctx_skip = $realcnt;
2361 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2362 defined $lines[$ctx_ln - 1] &&
2363 $lines[$ctx_ln - 1] =~ /^-/)) {
2364 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2365 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2369 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2370 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2372 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2374 "that open brace { should be on the previous line\n" .
2375 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2377 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2378 $ctx =~ /\)\s*\;\s*$/ &&
2379 defined $lines[$ctx_ln - 1])
2381 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2382 if ($nindent > $indent) {
2383 WARN("TRAILING_SEMICOLON",
2384 "trailing semicolon indicates no statements, indent implies otherwise\n" .
2385 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2390 # Check relative indent for conditionals and blocks.
2391 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
2392 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2393 ctx_statement_block($linenr, $realcnt, 0)
2394 if (!defined $stat);
2395 my ($s, $c) = ($stat, $cond);
2397 substr($s, 0, length($c), '');
2399 # Make sure we remove the line prefixes as we have
2400 # none on the first line, and are going to readd them
2404 # Find out how long the conditional actually is.
2405 my @newlines = ($c =~ /\n/gs);
2406 my $cond_lines = 1 + $#newlines;
2408 # We want to check the first line inside the block
2409 # starting at the end of the conditional, so remove:
2410 # 1) any blank line termination
2411 # 2) any opening brace { on end of the line
2413 my $continuation = 0;
2415 $s =~ s/^.*\bdo\b//;
2417 if ($s =~ s/^\s*\\//) {
2420 if ($s =~ s/^\s*?\n//) {
2425 # Also ignore a loop construct at the end of a
2426 # preprocessor statement.
2427 if (($prevline =~ /^.\s*#\s*define\s/ ||
2428 $prevline =~ /\\\s*$/) && $continuation == 0) {
2434 while ($cond_ptr != $cond_lines) {
2435 $cond_ptr = $cond_lines;
2437 # If we see an #else/#elif then the code
2439 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2444 # 1) blank lines, they should be at 0,
2445 # 2) preprocessor lines, and
2447 if ($continuation ||
2449 $s =~ /^\s*#\s*?/ ||
2450 $s =~ /^\s*$Ident\s*:/) {
2451 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
2452 if ($s =~ s/^.*?\n//) {
2458 my (undef, $sindent) = line_stats("+" . $s);
2459 my $stat_real = raw_line($linenr, $cond_lines);
2461 # Check if either of these lines are modified, else
2462 # this is not this patch's fault.
2463 if (!defined($stat_real) ||
2464 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2467 if (defined($stat_real) && $cond_lines > 1) {
2468 $stat_real = "[...]\n$stat_real";
2471 #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";
2473 if ($check && (($sindent % 8) != 0 ||
2474 ($sindent <= $indent && $s ne ''))) {
2475 WARN("SUSPECT_CODE_INDENT",
2476 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
2480 # Track the 'values' across context and added lines.
2481 my $opline = $line; $opline =~ s/^./ /;
2482 my ($curr_values, $curr_vars) =
2483 annotate_values($opline . "\n", $prev_values);
2484 $curr_values = $prev_values . $curr_values;
2486 my $outline = $opline; $outline =~ s/\t/ /g;
2487 print "$linenr > .$outline\n";
2488 print "$linenr > $curr_values\n";
2489 print "$linenr > $curr_vars\n";
2491 $prev_values = substr($curr_values, -1);
2493 #ignore lines not being added
2494 next if ($line =~ /^[^\+]/);
2496 # TEST: allow direct testing of the type matcher.
2498 if ($line =~ /^.\s*$Declare\s*$/) {
2500 "TEST: is type\n" . $herecurr);
2501 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2502 ERROR("TEST_NOT_TYPE",
2503 "TEST: is not type ($1 is)\n". $herecurr);
2507 # TEST: allow direct testing of the attribute matcher.
2509 if ($line =~ /^.\s*$Modifier\s*$/) {
2511 "TEST: is attr\n" . $herecurr);
2512 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2513 ERROR("TEST_NOT_ATTR",
2514 "TEST: is not attr ($1 is)\n". $herecurr);
2519 # check for initialisation to aggregates open brace on the next line
2520 if ($line =~ /^.\s*{/ &&
2521 $prevline =~ /(?:^|[^=])=\s*$/) {
2523 "that open brace { should be on the previous line\n" . $hereprev);
2527 # Checks which are anchored on the added line.
2530 # check for malformed paths in #include statements (uses RAW line)
2531 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2533 if ($path =~ m{//}) {
2534 ERROR("MALFORMED_INCLUDE",
2535 "malformed #include filename\n" . $herecurr);
2537 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2538 ERROR("UAPI_INCLUDE",
2539 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2543 # no C99 // comments
2544 if ($line =~ m{//}) {
2545 if (ERROR("C99_COMMENTS",
2546 "do not use C99 // comments\n" . $herecurr) &&
2548 my $line = $fixed[$linenr - 1];
2549 if ($line =~ /\/\/(.*)$/) {
2550 my $comment = trim($1);
2551 $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
2555 # Remove C99 comments.
2557 $opline =~ s@//.*@@;
2559 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2560 # the whole statement.
2561 #print "APW <$lines[$realline_next - 1]>\n";
2562 if (defined $realline_next &&
2563 exists $lines[$realline_next - 1] &&
2564 !defined $suppress_export{$realline_next} &&
2565 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2566 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2567 # Handle definitions which produce identifiers with
2570 # EXPORT_SYMBOL(something_foo);
2572 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
2573 $name =~ /^${Ident}_$2/) {
2574 #print "FOO C name<$name>\n";
2575 $suppress_export{$realline_next} = 1;
2577 } elsif ($stat !~ /(?:
2579 ^.DEFINE_$Ident\(\Q$name\E\)|
2580 ^.DECLARE_$Ident\(\Q$name\E\)|
2581 ^.LIST_HEAD\(\Q$name\E\)|
2582 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2583 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
2585 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2586 $suppress_export{$realline_next} = 2;
2588 $suppress_export{$realline_next} = 1;
2591 if (!defined $suppress_export{$linenr} &&
2592 $prevline =~ /^.\s*$/ &&
2593 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2594 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2595 #print "FOO B <$lines[$linenr - 1]>\n";
2596 $suppress_export{$linenr} = 2;
2598 if (defined $suppress_export{$linenr} &&
2599 $suppress_export{$linenr} == 2) {
2600 WARN("EXPORT_SYMBOL",
2601 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
2604 # check for global initialisers.
2605 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2606 if (ERROR("GLOBAL_INITIALISERS",
2607 "do not initialise globals to 0 or NULL\n" .
2610 $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2613 # check for static initialisers.
2614 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2615 if (ERROR("INITIALISED_STATIC",
2616 "do not initialise statics to 0 or NULL\n" .
2619 $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2623 # check for static const char * arrays.
2624 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2625 WARN("STATIC_CONST_CHAR_ARRAY",
2626 "static const char * array should probably be static const char * const\n" .
2630 # check for static char foo[] = "bar" declarations.
2631 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2632 WARN("STATIC_CONST_CHAR_ARRAY",
2633 "static char array declaration should probably be static const char\n" .
2637 # check for uses of DEFINE_PCI_DEVICE_TABLE
2638 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
2639 if (WARN("DEFINE_PCI_DEVICE_TABLE",
2640 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
2642 $fixed[$linenr - 1] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
2646 # check for new typedefs, only function parameters and sparse annotations
2648 if ($line =~ /\btypedef\s/ &&
2649 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2650 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
2651 $line !~ /\b$typeTypedefs\b/ &&
2652 $line !~ /\b__bitwise(?:__|)\b/) {
2653 WARN("NEW_TYPEDEFS",
2654 "do not add new typedefs\n" . $herecurr);
2657 # * goes on variable not on type
2659 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2661 my ($ident, $from, $to) = ($1, $2, $2);
2663 # Should start with a space.
2664 $to =~ s/^(\S)/ $1/;
2665 # Should not end with a space.
2667 # '*'s should not have spaces between.
2668 while ($to =~ s/\*\s+\*/\*\*/) {
2671 ## print "1: from<$from> to<$to> ident<$ident>\n";
2673 if (ERROR("POINTER_LOCATION",
2674 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
2676 my $sub_from = $ident;
2677 my $sub_to = $ident;
2678 $sub_to =~ s/\Q$from\E/$to/;
2679 $fixed[$linenr - 1] =~
2680 s@\Q$sub_from\E@$sub_to@;
2684 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2686 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
2688 # Should start with a space.
2689 $to =~ s/^(\S)/ $1/;
2690 # Should not end with a space.
2692 # '*'s should not have spaces between.
2693 while ($to =~ s/\*\s+\*/\*\*/) {
2695 # Modifiers should have spaces.
2696 $to =~ s/(\b$Modifier$)/$1 /;
2698 ## print "2: from<$from> to<$to> ident<$ident>\n";
2699 if ($from ne $to && $ident !~ /^$Modifier$/) {
2700 if (ERROR("POINTER_LOCATION",
2701 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
2704 my $sub_from = $match;
2705 my $sub_to = $match;
2706 $sub_to =~ s/\Q$from\E/$to/;
2707 $fixed[$linenr - 1] =~
2708 s@\Q$sub_from\E@$sub_to@;
2713 # # no BUG() or BUG_ON()
2714 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
2715 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2716 # print "$herecurr";
2720 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2721 WARN("LINUX_VERSION_CODE",
2722 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
2725 # check for uses of printk_ratelimit
2726 if ($line =~ /\bprintk_ratelimit\s*\(/) {
2727 WARN("PRINTK_RATELIMITED",
2728 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
2731 # printk should use KERN_* levels. Note that follow on printk's on the
2732 # same line do not need a level, so we use the current block context
2733 # to try and find and validate the current printk. In summary the current
2734 # printk includes all preceding printk's which have no newline on the end.
2735 # we assume the first bad printk is the one to report.
2736 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
2738 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2739 #print "CHECK<$lines[$ln - 1]\n";
2740 # we have a preceding printk if it ends
2741 # with "\n" ignore it, else it is to blame
2742 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2743 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2750 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2751 "printk() should include KERN_ facility level\n" . $herecurr);
2755 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2757 my $level = lc($orig);
2758 $level = "warn" if ($level eq "warning");
2759 my $level2 = $level;
2760 $level2 = "dbg" if ($level eq "debug");
2761 WARN("PREFER_PR_LEVEL",
2762 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
2765 if ($line =~ /\bpr_warning\s*\(/) {
2766 if (WARN("PREFER_PR_LEVEL",
2767 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
2769 $fixed[$linenr - 1] =~
2770 s/\bpr_warning\b/pr_warn/;
2774 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2776 my $level = lc($orig);
2777 $level = "warn" if ($level eq "warning");
2778 $level = "dbg" if ($level eq "debug");
2779 WARN("PREFER_DEV_LEVEL",
2780 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2783 # function brace can't be on same line, except for #defines of do while,
2784 # or if closed on same line
2785 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2786 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2788 "open brace '{' following function declarations go on the next line\n" . $herecurr);
2791 # open braces for enum, union and struct go on the same line.
2792 if ($line =~ /^.\s*{/ &&
2793 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2795 "open brace '{' following $1 go on the same line\n" . $hereprev);
2798 # missing space after union, struct or enum definition
2799 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2801 "missing space after $1 definition\n" . $herecurr) &&
2803 $fixed[$linenr - 1] =~
2804 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
2808 # check for spacing round square brackets; allowed:
2809 # 1. with a type on the left -- int [] a;
2810 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2811 # 3. inside a curly brace -- = { [0...10] = 5 }
2812 while ($line =~ /(.*?\s)\[/g) {
2813 my ($where, $prefix) = ($-[1], $1);
2814 if ($prefix !~ /$Type\s+$/ &&
2815 ($where != 0 || $prefix !~ /^.\s+$/) &&
2816 $prefix !~ /[{,]\s+$/) {
2817 if (ERROR("BRACKET_SPACE",
2818 "space prohibited before open square bracket '['\n" . $herecurr) &&
2820 $fixed[$linenr - 1] =~
2821 s/^(\+.*?)\s+\[/$1\[/;
2826 # check for spaces between functions and their parentheses.
2827 while ($line =~ /($Ident)\s+\(/g) {
2829 my $ctx_before = substr($line, 0, $-[1]);
2830 my $ctx = "$ctx_before$name";
2832 # Ignore those directives where spaces _are_ permitted.
2834 if|for|while|switch|return|case|
2835 volatile|__volatile__|
2836 __attribute__|format|__extension__|
2839 # cpp #define statements have non-optional spaces, ie
2840 # if there is a space between the name and the open
2841 # parenthesis it is simply not a parameter group.
2842 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2844 # cpp #elif statement condition may start with a (
2845 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2847 # If this whole things ends with a type its most
2848 # likely a typedef for a function.
2849 } elsif ($ctx =~ /$Type$/) {
2853 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
2855 $fixed[$linenr - 1] =~
2856 s/\b$name\s+\(/$name\(/;
2861 # Check operator spacing.
2862 if (!($line=~/\#\s*include/)) {
2863 my $fixed_line = "";
2867 <<=|>>=|<=|>=|==|!=|
2868 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2869 =>|->|<<|>>|<|>|=|!|~|
2870 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2873 my @elements = split(/($ops|;)/, $opline);
2875 ## print("element count: <" . $#elements . ">\n");
2876 ## foreach my $el (@elements) {
2877 ## print("el: <$el>\n");
2880 my @fix_elements = ();
2883 foreach my $el (@elements) {
2884 push(@fix_elements, substr($rawline, $off, length($el)));
2885 $off += length($el);
2890 my $blank = copy_spacing($opline);
2891 my $last_after = -1;
2893 for (my $n = 0; $n < $#elements; $n += 2) {
2895 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
2897 ## print("n: <$n> good: <$good>\n");
2899 $off += length($elements[$n]);
2901 # Pick up the preceding and succeeding characters.
2902 my $ca = substr($opline, 0, $off);
2904 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2905 $cc = substr($opline, $off + length($elements[$n + 1]));
2907 my $cb = "$ca$;$cc";
2910 $a = 'V' if ($elements[$n] ne '');
2911 $a = 'W' if ($elements[$n] =~ /\s$/);
2912 $a = 'C' if ($elements[$n] =~ /$;$/);
2913 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2914 $a = 'O' if ($elements[$n] eq '');
2915 $a = 'E' if ($ca =~ /^\s*$/);
2917 my $op = $elements[$n + 1];
2920 if (defined $elements[$n + 2]) {
2921 $c = 'V' if ($elements[$n + 2] ne '');
2922 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2923 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2924 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2925 $c = 'O' if ($elements[$n + 2] eq '');
2926 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2931 my $ctx = "${a}x${c}";
2933 my $at = "(ctx:$ctx)";
2935 my $ptr = substr($blank, 0, $off) . "^";
2936 my $hereptr = "$hereline$ptr\n";
2938 # Pull out the value of this operator.
2939 my $op_type = substr($curr_values, $off + 1, 1);
2941 # Get the full operator variant.
2942 my $opv = $op . substr($curr_vars, $off, 1);
2944 # Ignore operators passed as parameters.
2945 if ($op_type ne 'V' &&
2946 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2949 # } elsif ($op =~ /^$;+$/) {
2951 # ; should have either the end of line or a space or \ after it
2952 } elsif ($op eq ';') {
2953 if ($ctx !~ /.x[WEBC]/ &&
2954 $cc !~ /^\\/ && $cc !~ /^;/) {
2955 if (ERROR("SPACING",
2956 "space required after that '$op' $at\n" . $hereptr)) {
2957 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
2963 } elsif ($op eq '//') {
2967 # : when part of a bitfield
2968 } elsif ($op eq '->' || $opv eq ':B') {
2969 if ($ctx =~ /Wx.|.xW/) {
2970 if (ERROR("SPACING",
2971 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
2972 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2973 if (defined $fix_elements[$n + 2]) {
2974 $fix_elements[$n + 2] =~ s/^\s+//;
2980 # , must have a space on the right.
2981 } elsif ($op eq ',') {
2982 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2983 if (ERROR("SPACING",
2984 "space required after that '$op' $at\n" . $hereptr)) {
2985 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
2991 # '*' as part of a type definition -- reported already.
2992 } elsif ($opv eq '*_') {
2993 #warn "'*' is part of type\n";
2995 # unary operators should have a space before and
2996 # none after. May be left adjacent to another
2997 # unary operator, or a cast
2998 } elsif ($op eq '!' || $op eq '~' ||
2999 $opv eq '*U' || $opv eq '-U' ||
3000 $opv eq '&U' || $opv eq '&&U') {
3001 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
3002 if (ERROR("SPACING",
3003 "space required before that '$op' $at\n" . $hereptr)) {
3004 if ($n != $last_after + 2) {
3005 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3010 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3011 # A unary '*' may be const
3013 } elsif ($ctx =~ /.xW/) {
3014 if (ERROR("SPACING",
3015 "space prohibited after that '$op' $at\n" . $hereptr)) {
3016 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
3017 if (defined $fix_elements[$n + 2]) {
3018 $fix_elements[$n + 2] =~ s/^\s+//;
3024 # unary ++ and unary -- are allowed no space on one side.
3025 } elsif ($op eq '++' or $op eq '--') {
3026 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
3027 if (ERROR("SPACING",
3028 "space required one side of that '$op' $at\n" . $hereptr)) {
3029 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
3033 if ($ctx =~ /Wx[BE]/ ||
3034 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
3035 if (ERROR("SPACING",
3036 "space prohibited before that '$op' $at\n" . $hereptr)) {
3037 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3041 if ($ctx =~ /ExW/) {
3042 if (ERROR("SPACING",
3043 "space prohibited after that '$op' $at\n" . $hereptr)) {
3044 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3045 if (defined $fix_elements[$n + 2]) {
3046 $fix_elements[$n + 2] =~ s/^\s+//;
3052 # << and >> may either have or not have spaces both sides
3053 } elsif ($op eq '<<' or $op eq '>>' or
3054 $op eq '&' or $op eq '^' or $op eq '|' or
3055 $op eq '+' or $op eq '-' or
3056 $op eq '*' or $op eq '/' or
3059 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
3060 if (ERROR("SPACING",
3061 "need consistent spacing around '$op' $at\n" . $hereptr)) {
3062 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3063 if (defined $fix_elements[$n + 2]) {
3064 $fix_elements[$n + 2] =~ s/^\s+//;
3070 # A colon needs no spaces before when it is
3071 # terminating a case value or a label.
3072 } elsif ($opv eq ':C' || $opv eq ':L') {
3073 if ($ctx =~ /Wx./) {
3074 if (ERROR("SPACING",
3075 "space prohibited before that '$op' $at\n" . $hereptr)) {
3076 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3081 # All the others need spaces both sides.
3082 } elsif ($ctx !~ /[EWC]x[CWE]/) {
3085 # Ignore email addresses <foo@bar>
3087 $cc =~ /^\S+\@\S+>/) ||
3089 $ca =~ /<\S+\@\S+$/))
3094 # messages are ERROR, but ?: are CHK
3096 my $msg_type = \&ERROR;
3097 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3099 if (&{$msg_type}("SPACING",
3100 "spaces required around that '$op' $at\n" . $hereptr)) {
3101 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3102 if (defined $fix_elements[$n + 2]) {
3103 $fix_elements[$n + 2] =~ s/^\s+//;
3109 $off += length($elements[$n + 1]);
3111 ## print("n: <$n> GOOD: <$good>\n");
3113 $fixed_line = $fixed_line . $good;
3116 if (($#elements % 2) == 0) {
3117 $fixed_line = $fixed_line . $fix_elements[$#elements];
3120 if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
3121 $fixed[$linenr - 1] = $fixed_line;
3127 # check for whitespace before a non-naked semicolon
3128 if ($line =~ /^\+.*\S\s+;/) {
3130 "space prohibited before semicolon\n" . $herecurr) &&
3132 1 while $fixed[$linenr - 1] =~
3133 s/^(\+.*\S)\s+;/$1;/;
3137 # check for multiple assignments
3138 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3139 CHK("MULTIPLE_ASSIGNMENTS",
3140 "multiple assignments should be avoided\n" . $herecurr);
3143 ## # check for multiple declarations, allowing for a function declaration
3145 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3146 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3148 ## # Remove any bracketed sections to ensure we do not
3149 ## # falsly report the parameters of functions.
3151 ## while ($ln =~ s/\([^\(\)]*\)//g) {
3153 ## if ($ln =~ /,/) {
3154 ## WARN("MULTIPLE_DECLARATION",
3155 ## "declaring multiple variables together should be avoided\n" . $herecurr);
3159 #need space before brace following if, while, etc
3160 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3162 if (ERROR("SPACING",
3163 "space required before the open brace '{'\n" . $herecurr) &&
3165 $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
3169 ## # check for blank lines before declarations
3170 ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3171 ## $prevrawline =~ /^.\s*$/) {
3173 ## "No blank lines before declarations\n" . $hereprev);
3177 # closing brace should have a space following it when it has anything
3179 if ($line =~ /}(?!(?:,|;|\)))\S/) {
3180 if (ERROR("SPACING",
3181 "space required after that close brace '}'\n" . $herecurr) &&
3183 $fixed[$linenr - 1] =~
3184 s/}((?!(?:,|;|\)))\S)/} $1/;
3188 # check spacing on square brackets
3189 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
3190 if (ERROR("SPACING",
3191 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3193 $fixed[$linenr - 1] =~
3197 if ($line =~ /\s\]/) {
3198 if (ERROR("SPACING",
3199 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3201 $fixed[$linenr - 1] =~
3206 # check spacing on parentheses
3207 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3208 $line !~ /for\s*\(\s+;/) {
3209 if (ERROR("SPACING",
3210 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3212 $fixed[$linenr - 1] =~
3216 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3217 $line !~ /for\s*\(.*;\s+\)/ &&
3218 $line !~ /:\s+\)/) {
3219 if (ERROR("SPACING",
3220 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3222 $fixed[$linenr - 1] =~
3227 #goto labels aren't indented, allow a single space however
3228 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
3229 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
3230 if (WARN("INDENTED_LABEL",
3231 "labels should not be indented\n" . $herecurr) &&
3233 $fixed[$linenr - 1] =~
3238 # Return is not a function.
3239 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3241 if ($^V && $^V ge 5.10.0 &&
3242 $stat =~ /^.\s*return\s*$balanced_parens\s*;\s*$/) {
3243 ERROR("RETURN_PARENTHESES",
3244 "return is not a function, parentheses are not required\n" . $herecurr);
3246 } elsif ($spacing !~ /\s+/) {
3248 "space required before the open parenthesis '('\n" . $herecurr);
3252 # Return of what appears to be an errno should normally be -'ve
3253 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3255 if ($name ne 'EOF' && $name ne 'ERROR') {
3256 WARN("USE_NEGATIVE_ERRNO",
3257 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
3261 # Need a space before open parenthesis after if, while etc
3262 if ($line =~ /\b(if|while|for|switch)\(/) {
3263 if (ERROR("SPACING",
3264 "space required before the open parenthesis '('\n" . $herecurr) &&
3266 $fixed[$linenr - 1] =~
3267 s/\b(if|while|for|switch)\(/$1 \(/;
3271 # Check for illegal assignment in if conditional -- and check for trailing
3272 # statements after the conditional.
3273 if ($line =~ /do\s*(?!{)/) {
3274 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3275 ctx_statement_block($linenr, $realcnt, 0)
3276 if (!defined $stat);
3277 my ($stat_next) = ctx_statement_block($line_nr_next,
3278 $remain_next, $off_next);
3279 $stat_next =~ s/\n./\n /g;
3280 ##print "stat<$stat> stat_next<$stat_next>\n";
3282 if ($stat_next =~ /^\s*while\b/) {
3283 # If the statement carries leading newlines,
3284 # then count those as offsets.
3286 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3288 statement_rawlines($whitespace) - 1;
3290 $suppress_whiletrailers{$line_nr_next +
3294 if (!defined $suppress_whiletrailers{$linenr} &&
3295 defined($stat) && defined($cond) &&
3296 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3297 my ($s, $c) = ($stat, $cond);
3299 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3300 ERROR("ASSIGN_IN_IF",
3301 "do not use assignment in if condition\n" . $herecurr);
3304 # Find out what is on the end of the line after the
3306 substr($s, 0, length($c), '');
3308 $s =~ s/$;//g; # Remove any comments
3309 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3310 $c !~ /}\s*while\s*/)
3312 # Find out how long the conditional actually is.
3313 my @newlines = ($c =~ /\n/gs);
3314 my $cond_lines = 1 + $#newlines;
3317 $stat_real = raw_line($linenr, $cond_lines)
3318 . "\n" if ($cond_lines);
3319 if (defined($stat_real) && $cond_lines > 1) {
3320 $stat_real = "[...]\n$stat_real";
3323 ERROR("TRAILING_STATEMENTS",
3324 "trailing statements should be on next line\n" . $herecurr . $stat_real);
3328 # Check for bitwise tests written as boolean
3340 WARN("HEXADECIMAL_BOOLEAN_TEST",
3341 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
3344 # if and else should not have general statements after it
3345 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3347 $s =~ s/$;//g; # Remove any comments
3348 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3349 ERROR("TRAILING_STATEMENTS",
3350 "trailing statements should be on next line\n" . $herecurr);
3353 # if should not continue a brace
3354 if ($line =~ /}\s*if\b/) {
3355 ERROR("TRAILING_STATEMENTS",
3356 "trailing statements should be on next line\n" .
3359 # case and default should not have general statements after them
3360 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3362 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3366 ERROR("TRAILING_STATEMENTS",
3367 "trailing statements should be on next line\n" . $herecurr);
3370 # Check for }<nl>else {, these must be at the same
3371 # indent level to be relevant to each other.
3372 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3373 $previndent == $indent) {
3374 ERROR("ELSE_AFTER_BRACE",
3375 "else should follow close brace '}'\n" . $hereprev);
3378 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3379 $previndent == $indent) {
3380 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3382 # Find out what is on the end of the line after the
3384 substr($s, 0, length($c), '');
3387 if ($s =~ /^\s*;/) {
3388 ERROR("WHILE_AFTER_BRACE",
3389 "while should follow close brace '}'\n" . $hereprev);
3393 #Specific variable tests
3394 while ($line =~ m{($Constant|$Lval)}g) {
3397 #gcc binary extension
3398 if ($var =~ /^$Binary$/) {
3399 if (WARN("GCC_BINARY_CONSTANT",
3400 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3402 my $hexval = sprintf("0x%x", oct($var));
3403 $fixed[$linenr - 1] =~
3404 s/\b$var\b/$hexval/;
3409 if ($var !~ /^$Constant$/ &&
3410 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
3411 #Ignore Page<foo> variants
3412 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
3413 #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
3414 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
3415 while ($var =~ m{($Ident)}g) {
3417 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
3419 seed_camelcase_includes();
3420 if (!$file && !$camelcase_file_seeded) {
3421 seed_camelcase_file($realfile);
3422 $camelcase_file_seeded = 1;
3425 if (!defined $camelcase{$word}) {
3426 $camelcase{$word} = 1;
3428 "Avoid CamelCase: <$word>\n" . $herecurr);
3434 #no spaces allowed after \ in define
3435 if ($line =~ /\#\s*define.*\\\s+$/) {
3436 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3437 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3439 $fixed[$linenr - 1] =~ s/\s+$//;
3443 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
3444 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
3446 my $checkfile = "include/linux/$file";
3447 if (-f "$root/$checkfile" &&
3448 $realfile ne $checkfile &&
3449 $1 !~ /$allowed_asm_includes/)
3451 if ($realfile =~ m{^arch/}) {
3452 CHK("ARCH_INCLUDE_LINUX",
3453 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3455 WARN("INCLUDE_LINUX",
3456 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3461 # multi-statement macros should be enclosed in a do while loop, grab the
3462 # first statement and ensure its the whole macro if its not enclosed
3463 # in a known good container
3464 if ($realfile !~ m@/vmlinux.lds.h$@ &&
3465 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
3468 my ($off, $dstat, $dcond, $rest);
3470 ($dstat, $dcond, $ln, $cnt, $off) =
3471 ctx_statement_block($linenr, $realcnt, 0);
3473 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
3474 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
3476 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
3478 $dstat =~ s/\\\n.//g;
3479 $dstat =~ s/^\s*//s;
3480 $dstat =~ s/\s*$//s;
3482 # Flatten any parentheses and braces
3483 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3484 $dstat =~ s/\{[^\{\}]*\}/1/ ||
3485 $dstat =~ s/\[[^\[\]]*\]/1/)
3489 # Flatten any obvious string concatentation.
3490 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3491 $dstat =~ s/$Ident\s*("X*")/$1/)
3495 my $exceptions = qr{
3507 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
3509 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3510 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
3511 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
3512 $dstat !~ /^'X'$/ && # character constants
3513 $dstat !~ /$exceptions/ &&
3514 $dstat !~ /^\.$Ident\s*=/ && # .foo =
3515 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
3516 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
3517 $dstat !~ /^for\s*$Constant$/ && # for (...)
3518 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3519 $dstat !~ /^do\s*{/ && # do {...
3520 $dstat !~ /^\({/ && # ({...
3521 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
3524 my $herectx = $here . "\n";
3525 my $cnt = statement_rawlines($ctx);
3527 for (my $n = 0; $n < $cnt; $n++) {
3528 $herectx .= raw_line($linenr, $n) . "\n";
3531 if ($dstat =~ /;/) {
3532 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3533 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3535 ERROR("COMPLEX_MACRO",
3536 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
3540 # check for line continuations outside of #defines, preprocessor #, and asm
3543 if ($prevline !~ /^..*\\$/ &&
3544 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3545 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
3546 $line =~ /^\+.*\\$/) {
3547 WARN("LINE_CONTINUATIONS",
3548 "Avoid unnecessary line continuations\n" . $herecurr);
3552 # do {} while (0) macro tests:
3553 # single-statement macros do not need to be enclosed in do while (0) loop,
3554 # macro should not end with a semicolon
3555 if ($^V && $^V ge 5.10.0 &&
3556 $realfile !~ m@/vmlinux.lds.h$@ &&
3557 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3560 my ($off, $dstat, $dcond, $rest);
3562 ($dstat, $dcond, $ln, $cnt, $off) =
3563 ctx_statement_block($linenr, $realcnt, 0);
3566 $dstat =~ s/\\\n.//g;
3568 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3573 my $cnt = statement_rawlines($ctx);
3574 my $herectx = $here . "\n";
3576 for (my $n = 0; $n < $cnt; $n++) {
3577 $herectx .= raw_line($linenr, $n) . "\n";
3580 if (($stmts =~ tr/;/;/) == 1 &&
3581 $stmts !~ /^\s*(if|while|for|switch)\b/) {
3582 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3583 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3585 if (defined $semis && $semis ne "") {
3586 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3587 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3592 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3593 # all assignments may have only one of the following with an assignment:
3596 # VMLINUX_SYMBOL(...)
3597 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
3598 WARN("MISSING_VMLINUX_SYMBOL",
3599 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
3602 # check for redundant bracing round if etc
3603 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3604 my ($level, $endln, @chunks) =
3605 ctx_statement_full($linenr, $realcnt, 1);
3606 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
3607 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3608 if ($#chunks > 0 && $level == 0) {
3612 my $herectx = $here . "\n";
3613 my $ln = $linenr - 1;
3614 for my $chunk (@chunks) {
3615 my ($cond, $block) = @{$chunk};
3617 # If the condition carries leading newlines, then count those as offsets.
3618 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3619 my $offset = statement_rawlines($whitespace) - 1;
3621 $allowed[$allow] = 0;
3622 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3624 # We have looked at and allowed this specific line.
3625 $suppress_ifbraces{$ln + $offset} = 1;
3627 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
3628 $ln += statement_rawlines($block) - 1;
3630 substr($block, 0, length($cond), '');
3632 $seen++ if ($block =~ /^\s*{/);
3634 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
3635 if (statement_lines($cond) > 1) {
3636 #print "APW: ALLOWED: cond<$cond>\n";
3637 $allowed[$allow] = 1;
3639 if ($block =~/\b(?:if|for|while)\b/) {
3640 #print "APW: ALLOWED: block<$block>\n";
3641 $allowed[$allow] = 1;
3643 if (statement_block_size($block) > 1) {
3644 #print "APW: ALLOWED: lines block<$block>\n";
3645 $allowed[$allow] = 1;
3650 my $sum_allowed = 0;
3651 foreach (@allowed) {
3654 if ($sum_allowed == 0) {
3656 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3657 } elsif ($sum_allowed != $allow &&
3660 "braces {} should be used on all arms of this statement\n" . $herectx);
3665 if (!defined $suppress_ifbraces{$linenr - 1} &&
3666 $line =~ /\b(if|while|for|else)\b/) {
3669 # Check the pre-context.
3670 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3671 #print "APW: ALLOWED: pre<$1>\n";
3675 my ($level, $endln, @chunks) =
3676 ctx_statement_full($linenr, $realcnt, $-[0]);
3678 # Check the condition.
3679 my ($cond, $block) = @{$chunks[0]};
3680 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3681 if (defined $cond) {
3682 substr($block, 0, length($cond), '');
3684 if (statement_lines($cond) > 1) {
3685 #print "APW: ALLOWED: cond<$cond>\n";
3688 if ($block =~/\b(?:if|for|while)\b/) {
3689 #print "APW: ALLOWED: block<$block>\n";
3692 if (statement_block_size($block) > 1) {
3693 #print "APW: ALLOWED: lines block<$block>\n";
3696 # Check the post-context.
3697 if (defined $chunks[1]) {
3698 my ($cond, $block) = @{$chunks[1]};
3699 if (defined $cond) {
3700 substr($block, 0, length($cond), '');
3702 if ($block =~ /^\s*\{/) {
3703 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3707 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
3708 my $herectx = $here . "\n";
3709 my $cnt = statement_rawlines($block);
3711 for (my $n = 0; $n < $cnt; $n++) {
3712 $herectx .= raw_line($linenr, $n) . "\n";
3716 "braces {} are not necessary for single statement blocks\n" . $herectx);
3720 # check for unnecessary blank lines around braces
3721 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
3723 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3725 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
3727 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3730 # no volatiles please
3731 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3732 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3734 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
3738 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3739 CHK("REDUNDANT_CODE",
3740 "if this code is redundant consider removing it\n" .
3744 # check for needless "if (<foo>) fn(<foo>)" uses
3745 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3746 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3747 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3749 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
3753 # check for bad placement of section $InitAttribute (e.g.: __initdata)
3754 if ($line =~ /(\b$InitAttribute\b)/) {
3756 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
3759 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
3760 ERROR("MISPLACED_INIT",
3761 "$attr should be placed after $var\n" . $herecurr)) ||
3762 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
3763 WARN("MISPLACED_INIT",
3764 "$attr should be placed after $var\n" . $herecurr))) &&
3766 $fixed[$linenr - 1] =~ s/(\bstatic\s+(?:const\s+)?)(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*([=;])\s*/"$1" . trim(string_find_replace($2, "\\s*$attr\\s*", " ")) . " " . trim(string_find_replace($3, "\\s*$attr\\s*", "")) . " $attr" . ("$4" eq ";" ? ";" : " = ")/e;
3771 # check for $InitAttributeData (ie: __initdata) with const
3772 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
3774 $attr =~ /($InitAttributePrefix)(.*)/;
3775 my $attr_prefix = $1;
3777 if (ERROR("INIT_ATTRIBUTE",
3778 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
3780 $fixed[$linenr - 1] =~
3781 s/$InitAttributeData/${attr_prefix}initconst/;
3785 # check for $InitAttributeConst (ie: __initconst) without const
3786 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
3788 if (ERROR("INIT_ATTRIBUTE",
3789 "Use of $attr requires a separate use of const\n" . $herecurr) &&
3791 my $lead = $fixed[$linenr - 1] =~
3792 /(^\+\s*(?:static\s+))/;
3794 $lead = "$lead " if ($lead !~ /^\+$/);
3795 $lead = "${lead}const ";
3796 $fixed[$linenr - 1] =~ s/(^\+\s*(?:static\s+))/$lead/;
3800 # prefer usleep_range over udelay
3801 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
3802 # ignore udelay's < 10, however
3805 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
3809 # warn about unexpectedly long msleep's
3810 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3813 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
3817 # check for comparisons of jiffies
3818 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3819 WARN("JIFFIES_COMPARISON",
3820 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3823 # check for comparisons of get_jiffies_64()
3824 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3825 WARN("JIFFIES_COMPARISON",
3826 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3829 # warn about #ifdefs in C files
3830 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
3831 # print "#ifdef in C files should be avoided\n";
3832 # print "$herecurr";
3836 # warn about spacing in #ifdefs
3837 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3838 if (ERROR("SPACING",
3839 "exactly one space required after that #$1\n" . $herecurr) &&
3841 $fixed[$linenr - 1] =~
3842 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
3847 # check for spinlock_t definitions without a comment.
3848 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3849 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
3851 if (!ctx_has_comment($first_line, $linenr)) {
3852 CHK("UNCOMMENTED_DEFINITION",
3853 "$1 definition without comment\n" . $herecurr);
3856 # check for memory barriers without a comment.
3857 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3858 if (!ctx_has_comment($first_line, $linenr)) {
3859 WARN("MEMORY_BARRIER",
3860 "memory barrier without comment\n" . $herecurr);
3863 # check of hardware specific defines
3864 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
3866 "architecture specific defines should be avoided\n" . $herecurr);
3869 # Check that the storage class is at the beginning of a declaration
3870 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3871 WARN("STORAGE_CLASS",
3872 "storage class should be at the beginning of the declaration\n" . $herecurr)
3875 # check the location of the inline attribute, that it is between
3876 # storage class and type.
3877 if ($line =~ /\b$Type\s+$Inline\b/ ||
3878 $line =~ /\b$Inline\s+$Storage\b/) {
3879 ERROR("INLINE_LOCATION",
3880 "inline keyword should sit between storage class and type\n" . $herecurr);
3883 # Check for __inline__ and __inline, prefer inline
3884 if ($realfile !~ m@\binclude/uapi/@ &&
3885 $line =~ /\b(__inline__|__inline)\b/) {
3887 "plain inline is preferred over $1\n" . $herecurr) &&
3889 $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
3894 # Check for __attribute__ packed, prefer __packed
3895 if ($realfile !~ m@\binclude/uapi/@ &&
3896 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3897 WARN("PREFER_PACKED",
3898 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
3901 # Check for __attribute__ aligned, prefer __aligned
3902 if ($realfile !~ m@\binclude/uapi/@ &&
3903 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3904 WARN("PREFER_ALIGNED",
3905 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
3908 # Check for __attribute__ format(printf, prefer __printf
3909 if ($realfile !~ m@\binclude/uapi/@ &&
3910 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3911 if (WARN("PREFER_PRINTF",
3912 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
3914 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
3919 # Check for __attribute__ format(scanf, prefer __scanf
3920 if ($realfile !~ m@\binclude/uapi/@ &&
3921 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3922 if (WARN("PREFER_SCANF",
3923 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
3925 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
3929 # check for sizeof(&)
3930 if ($line =~ /\bsizeof\s*\(\s*\&/) {
3931 WARN("SIZEOF_ADDRESS",
3932 "sizeof(& should be avoided\n" . $herecurr);
3935 # check for sizeof without parenthesis
3936 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
3937 if (WARN("SIZEOF_PARENTHESIS",
3938 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
3940 $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
3944 # check for line continuations in quoted strings with odd counts of "
3945 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3946 WARN("LINE_CONTINUATIONS",
3947 "Avoid line continuations in quoted strings\n" . $herecurr);
3950 # check for struct spinlock declarations
3951 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3952 WARN("USE_SPINLOCK_T",
3953 "struct spinlock should be spinlock_t\n" . $herecurr);
3956 # check for seq_printf uses that could be seq_puts
3957 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
3958 my $fmt = get_quoted_string($line, $rawline);
3959 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
3960 if (WARN("PREFER_SEQ_PUTS",
3961 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
3963 $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
3968 # Check for misused memsets
3969 if ($^V && $^V ge 5.10.0 &&
3971 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3977 if ($ms_size =~ /^(0x|)0$/i) {
3979 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
3980 } elsif ($ms_size =~ /^(0x|)1$/i) {
3982 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3986 # typecasts on min/max could be min_t/max_t
3987 if ($^V && $^V ge 5.10.0 &&
3989 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3990 if (defined $2 || defined $7) {
3992 my $cast1 = deparenthesize($2);
3994 my $cast2 = deparenthesize($7);
3998 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
3999 $cast = "$cast1 or $cast2";
4000 } elsif ($cast1 ne "") {
4006 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4010 # check usleep_range arguments
4011 if ($^V && $^V ge 5.10.0 &&
4013 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4017 WARN("USLEEP_RANGE",
4018 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4019 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4021 WARN("USLEEP_RANGE",
4022 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4026 # check for naked sscanf
4027 if ($^V && $^V ge 5.10.0 &&
4029 $stat =~ /\bsscanf\b/ &&
4030 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4031 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4032 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4033 my $lc = $stat =~ tr@\n@@;
4034 $lc = $lc + $linenr;
4035 my $stat_real = raw_line($linenr, 0);
4036 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4037 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4039 WARN("NAKED_SSCANF",
4040 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4043 # check for new externs in .h files.
4044 if ($realfile =~ /\.h$/ &&
4045 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4046 if (CHK("AVOID_EXTERNS",
4047 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
4049 $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
4053 # check for new externs in .c files.
4054 if ($realfile =~ /\.c$/ && defined $stat &&
4055 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
4057 my $function_name = $1;
4058 my $paren_space = $2;
4061 if (defined $cond) {
4062 substr($s, 0, length($cond), '');
4064 if ($s =~ /^\s*;/ &&
4065 $function_name ne 'uninitialized_var')
4067 WARN("AVOID_EXTERNS",
4068 "externs should be avoided in .c files\n" . $herecurr);
4071 if ($paren_space =~ /\n/) {
4072 WARN("FUNCTION_ARGUMENTS",
4073 "arguments for function declarations should follow identifier\n" . $herecurr);
4076 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4077 $stat =~ /^.\s*extern\s+/)
4079 WARN("AVOID_EXTERNS",
4080 "externs should be avoided in .c files\n" . $herecurr);
4083 # checks for new __setup's
4084 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4087 if (!grep(/$name/, @setup_docs)) {
4088 CHK("UNDOCUMENTED_SETUP",
4089 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
4093 # check for pointless casting of kmalloc return
4094 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
4095 WARN("UNNECESSARY_CASTS",
4096 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
4100 # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4101 if ($^V && $^V ge 5.10.0 &&
4102 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4103 CHK("ALLOC_SIZEOF_STRUCT",
4104 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4107 # check for krealloc arg reuse
4108 if ($^V && $^V ge 5.10.0 &&
4109 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4110 WARN("KREALLOC_ARG_REUSE",
4111 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4114 # check for alloc argument mismatch
4115 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
4116 WARN("ALLOC_ARRAY_ARGS",
4117 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
4120 # check for multiple semicolons
4121 if ($line =~ /;\s*;\s*$/) {
4122 if (WARN("ONE_SEMICOLON",
4123 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4125 $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
4129 # check for switch/default statements without a break;
4130 if ($^V && $^V ge 5.10.0 &&
4132 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4134 my $herectx = $here . "\n";
4135 my $cnt = statement_rawlines($stat);
4136 for (my $n = 0; $n < $cnt; $n++) {
4137 $herectx .= raw_line($linenr, $n) . "\n";
4139 WARN("DEFAULT_NO_BREAK",
4140 "switch default: should use break\n" . $herectx);
4143 # check for gcc specific __FUNCTION__
4144 if ($line =~ /\b__FUNCTION__\b/) {
4145 if (WARN("USE_FUNC",
4146 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
4148 $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
4152 # check for use of yield()
4153 if ($line =~ /\byield\s*\(\s*\)/) {
4155 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
4158 # check for comparisons against true and false
4159 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4167 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4169 my $type = lc($otype);
4170 if ($type =~ /^(?:true|false)$/) {
4171 if (("$test" eq "==" && "$type" eq "true") ||
4172 ("$test" eq "!=" && "$type" eq "false")) {
4176 CHK("BOOL_COMPARISON",
4177 "Using comparison to $otype is error prone\n" . $herecurr);
4179 ## maybe suggesting a correct construct would better
4180 ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4185 # check for semaphores initialized locked
4186 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
4187 WARN("CONSIDER_COMPLETION",
4188 "consider using a completion\n" . $herecurr);
4191 # recommend kstrto* over simple_strto* and strict_strto*
4192 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
4193 WARN("CONSIDER_KSTRTO",
4194 "$1 is obsolete, use k$3 instead\n" . $herecurr);
4197 # check for __initcall(), use device_initcall() explicitly please
4198 if ($line =~ /^.\s*__initcall\s*\(/) {
4199 WARN("USE_DEVICE_INITCALL",
4200 "please use device_initcall() instead of __initcall()\n" . $herecurr);
4203 # check for various ops structs, ensure they are const.
4204 my $struct_ops = qr{acpi_dock_ops|
4205 address_space_operations|
4207 block_device_operations|
4212 file_lock_operations|
4222 lock_manager_operations|
4228 pipe_buf_operations|
4229 platform_hibernation_ops|
4230 platform_suspend_ops|
4235 soc_pcmcia_socket_ops|
4241 if ($line !~ /\bconst\b/ &&
4242 $line =~ /\bstruct\s+($struct_ops)\b/) {
4243 WARN("CONST_STRUCT",
4244 "struct $1 should normally be const\n" .
4248 # use of NR_CPUS is usually wrong
4249 # ignore definitions of NR_CPUS and usage to define arrays as likely right
4250 if ($line =~ /\bNR_CPUS\b/ &&
4251 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4252 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
4253 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4254 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4255 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
4258 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4261 # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
4262 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
4263 ERROR("DEFINE_ARCH_HAS",
4264 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
4267 # check for %L{u,d,i} in strings
4269 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4270 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4271 $string =~ s/%%/__/g;
4272 if ($string =~ /(?<!%)%L[udi]/) {
4274 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4279 # whine mightly about in_atomic
4280 if ($line =~ /\bin_atomic\s*\(/) {
4281 if ($realfile =~ m@^drivers/@) {
4283 "do not use in_atomic in drivers\n" . $herecurr);
4284 } elsif ($realfile !~ m@^kernel/@) {
4286 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
4290 # check for lockdep_set_novalidate_class
4291 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
4292 $line =~ /__lockdep_no_validate__\s*\)/ ) {
4293 if ($realfile !~ m@^kernel/lockdep@ &&
4294 $realfile !~ m@^include/linux/lockdep@ &&
4295 $realfile !~ m@^drivers/base/core@) {
4297 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
4301 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
4302 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
4303 WARN("EXPORTED_WORLD_WRITABLE",
4304 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
4308 # If we have no input at all, then there is nothing to report on
4309 # so just keep quiet.
4310 if ($#rawlines == -1) {
4314 # In mailback mode only produce a report in the negative, for
4315 # things that appear to be patches.
4316 if ($mailback && ($clean == 1 || !$is_patch)) {
4320 # This is not a patch, and we are are in 'no-patch' mode so
4322 if (!$chk_patch && !$is_patch) {
4327 ERROR("NOT_UNIFIED_DIFF",
4328 "Does not appear to be a unified-diff format patch\n");
4330 if ($is_patch && $chk_signoff && $signoff == 0) {
4331 ERROR("MISSING_SIGN_OFF",
4332 "Missing Signed-off-by: line(s)\n");
4335 print report_dump();
4336 if ($summary && !($clean == 1 && $quiet == 1)) {
4337 print "$filename " if ($summary_file);
4338 print "total: $cnt_error errors, $cnt_warn warnings, " .
4339 (($check)? "$cnt_chk checks, " : "") .
4340 "$cnt_lines lines checked\n";
4341 print "\n" if ($quiet == 0);
4346 if ($^V lt 5.10.0) {
4347 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4348 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4351 # If there were whitespace errors which cleanpatch can fix
4352 # then suggest that.
4353 if ($rpt_cleaners) {
4354 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4355 print " scripts/cleanfile\n\n";
4360 hash_show_words(\%use_type, "Used");
4361 hash_show_words(\%ignore_type, "Ignored");
4363 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
4364 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
4368 open($f, '>', $newfile)
4369 or die "$P: Can't open $newfile for write\n";
4370 foreach my $fixed_line (@fixed) {
4373 if ($linecount > 3) {
4374 $fixed_line =~ s/^\+//;
4375 print $f $fixed_line. "\n";
4378 print $f $fixed_line . "\n";
4385 Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
4387 Do _NOT_ trust the results written to this file.
4388 Do _NOT_ submit these changes without inspecting them for correctness.
4390 This EXPERIMENTAL file is simply a convenience to help rewrite patches.
4391 No warranties, expressed or implied...
4397 if ($clean == 1 && $quiet == 0) {
4398 print "$vname has no obvious style problems and is ready for submission.\n"
4400 if ($clean == 0 && $quiet == 0) {
4402 $vname has style problems, please review.
4404 If any of these errors are false positives, please report
4405 them to the maintainer, see CHECKPATCH in MAINTAINERS.