2 # (c) 2001, Dave Jones. (the file handling bit)
3 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4 # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5 # (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
6 # Licensed under the terms of the GNU GPL License version 2
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};
245 our $InitAttribute = qr{__(?:mem|cpu|dev|net_|)(?:initdata|initconst|init\b)};
247 # Notes to $Attribute:
248 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
268 ____cacheline_aligned
|
269 ____cacheline_aligned_in_smp
|
270 ____cacheline_internodealigned_in_smp
|
274 our $Inline = qr{inline|__always_inline|noinline};
275 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
276 our $Lval = qr{$Ident(?:$Member)*};
278 our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
279 our $Binary = qr{(?i)0b[01]+$Int_type?};
280 our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
281 our $Int = qr{[0-9]+$Int_type?};
282 our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
283 our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
284 our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
285 our $Float = qr{$Float_hex|$Float_dec|$Float_int};
286 our $Constant = qr{$Float|$Binary|$Hex|$Int};
287 our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
288 our $Compare = qr{<=|>=|==|!=|<|>};
289 our $Arithmetic = qr{\+|-|\*|\/|%};
293 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||$Arithmetic
297 our $NonptrTypeWithAttr;
301 our $NON_ASCII_UTF8 = qr{
302 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
303 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
304 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
305 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
306 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
307 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
308 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
312 [\x09\x0A\x0D\x20-\x7E] # ASCII
316 our $typeTypedefs = qr{(?x
:
317 (?
:__
)?
(?
:u
|s
|be
|le)(?
:8|16|32|64)|
321 our $logFunctions = qr{(?x
:
322 printk
(?
:_ratelimited
|_once
|)|
323 (?
:[a-z0-9
]+_
){1,2}(?
:printk
|emerg
|alert
|crit
|err
|warning
|warn|notice
|info
|debug
|dbg
|vdbg
|devel
|cont
|WARN
)(?
:_ratelimited
|_once
|)|
324 WARN
(?
:_RATELIMIT
|_ONCE
|)|
327 seq_vprintf
|seq_printf
|seq_puts
330 our $signature_tags = qr{(?xi
:
343 qr{(?:unsigned\s+)?char},
344 qr{(?:unsigned\s+)?short},
345 qr{(?:unsigned\s+)?int},
346 qr{(?:unsigned\s+)?long},
347 qr{(?:unsigned\s+)?long\s+int},
348 qr{(?:unsigned\s+)?long\s+long},
349 qr{(?:unsigned\s+)?long\s+long\s+int},
358 qr{${Ident}_handler
},
359 qr{${Ident}_handler_fn
},
361 our @typeListWithAttr = (
363 qr{struct\s+$InitAttribute\s+$Ident},
364 qr{union\s+$InitAttribute\s+$Ident},
367 our @modifierList = (
371 our $allowed_asm_includes = qr{(?x
:
375 # memory.h: ARM has a custom one
378 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
379 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
380 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
381 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
383 (?
:$Modifier\s+|const\s
+)*
385 (?
:typeof
|__typeof__
)\s
*\
([^\
)]*\
)|
389 (?
:\s
+$Modifier|\s
+const
)*
391 $NonptrTypeWithAttr = qr{
392 (?
:$Modifier\s+|const\s
+)*
394 (?
:typeof
|__typeof__
)\s
*\
([^\
)]*\
)|
398 (?
:\s
+$Modifier|\s
+const
)*
402 (?
:(?
:\s
|\
*|\
[\
])+\s
*const
|(?
:\s
|\
*|\
[\
])+|(?
:\s
*\
[\s
*\
])+)?
403 (?
:\s
+$Inline|\s
+$Modifier)*
405 $Declare = qr{(?:$Storage\s+)?$Type};
409 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s
*};
411 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
412 # requires at least perl version v5.10.0
413 # Any use must be runtime checked with $^V
415 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
416 our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s
*};
417 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
421 return "" if (!defined($string));
422 $string =~ s
@^\s*\(\s
*@@g;
423 $string =~ s
@\s*\)\s
*$@@g;
424 $string =~ s
@\s+@ @g;
428 sub seed_camelcase_file
{
431 return if (!(-f
$file));
435 open(my $include_file, '<', "$file")
436 or warn "$P: Can't read '$file' $!\n";
437 my $text = <$include_file>;
438 close($include_file);
440 my @lines = split('\n', $text);
442 foreach my $line (@lines) {
443 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
444 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
447 elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
453 my $camelcase_seeded = 0;
454 sub seed_camelcase_includes
{
455 return if ($camelcase_seeded);
458 my $camelcase_cache = "";
459 my @include_files = ();
461 $camelcase_seeded = 1;
464 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
465 chomp $git_last_include_commit;
466 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
468 my $last_mod_date = 0;
469 $files = `find $root/include -name "*.h"`;
470 @include_files = split('\n', $files);
471 foreach my $file (@include_files) {
472 my $date = POSIX
::strftime
("%Y%m%d%H%M",
473 localtime((stat $file)[9]));
474 $last_mod_date = $date if ($last_mod_date < $date);
476 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
479 if ($camelcase_cache ne "" && -f
$camelcase_cache) {
480 open(my $camelcase_file, '<', "$camelcase_cache")
481 or warn "$P: Can't read '$camelcase_cache' $!\n";
482 while (<$camelcase_file>) {
486 close($camelcase_file);
492 $files = `git ls-files "include/*.h"`;
493 @include_files = split('\n', $files);
496 foreach my $file (@include_files) {
497 seed_camelcase_file
($file);
500 if ($camelcase_cache ne "") {
501 unlink glob ".checkpatch-camelcase.*";
502 open(my $camelcase_file, '>', "$camelcase_cache")
503 or warn "$P: Can't write '$camelcase_cache' $!\n";
504 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
505 print $camelcase_file ("$_\n");
507 close($camelcase_file);
511 $chk_signoff = 0 if ($file);
517 for my $filename (@ARGV) {
520 open($FILE, '-|', "diff -u /dev/null $filename") ||
521 die "$P: $filename: diff failed - $!\n";
522 } elsif ($filename eq '-') {
523 open($FILE, '<&STDIN');
525 open($FILE, '<', "$filename") ||
526 die "$P: $filename: open failed - $!\n";
528 if ($filename eq '-') {
529 $vname = 'Your patch';
538 if (!process
($filename)) {
548 sub top_of_kernel_tree
{
552 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
553 "README", "Documentation", "arch", "include", "drivers",
554 "fs", "init", "ipc", "kernel", "lib", "scripts",
557 foreach my $check (@tree_check) {
558 if (! -e
$root . '/' . $check) {
566 my ($formatted_email) = @_;
572 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
575 $comment = $3 if defined $3;
576 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
578 $comment = $2 if defined $2;
579 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
581 $comment = $2 if defined $2;
582 $formatted_email =~ s/$address.*$//;
583 $name = $formatted_email;
585 $name =~ s/^\"|\"$//g;
586 # If there's a name left after stripping spaces and
587 # leading quotes, and the address doesn't have both
588 # leading and trailing angle brackets, the address
590 # "joe smith joe@smith.com" bad
591 # "joe smith <joe@smith.com" bad
592 if ($name ne "" && $address !~ /^<[^>]+>$/) {
600 $name =~ s/^\"|\"$//g;
601 $address = trim
($address);
602 $address =~ s/^\<|\>$//g;
604 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
605 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
609 return ($name, $address, $comment);
613 my ($name, $address) = @_;
618 $name =~ s/^\"|\"$//g;
619 $address = trim($address);
621 if ($name =~ /[^\w \-]/i) { ##has "must quote
" chars
622 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
627 $formatted_email = "$address";
629 $formatted_email = "$name <$address>";
632 return $formatted_email;
638 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
639 if (-e
"$path/$conf") {
640 return "$path/$conf";
652 for my $c (split(//, $str)) {
656 for (; ($n % 8) != 0; $n++) {
668 (my $res = shift) =~ tr/\t/ /c;
675 # Drop the diff line leader and expand tabs
677 $line = expand_tabs
($line);
679 # Pick the indent from the front of the line.
680 my ($white) = ($line =~ /^(\s*)/);
682 return (length($line), length($white));
685 my $sanitise_quote = '';
687 sub sanitise_line_reset
{
688 my ($in_comment) = @_;
691 $sanitise_quote = '*/';
693 $sanitise_quote = '';
706 # Always copy over the diff marker.
707 $res = substr($line, 0, 1);
709 for ($off = 1; $off < length($line); $off++) {
710 $c = substr($line, $off, 1);
712 # Comments we are wacking completly including the begin
713 # and end, all to $;.
714 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
715 $sanitise_quote = '*/';
717 substr($res, $off, 2, "$;$;");
721 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
722 $sanitise_quote = '';
723 substr($res, $off, 2, "$;$;");
727 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
728 $sanitise_quote = '//';
730 substr($res, $off, 2, $sanitise_quote);
735 # A \ in a string means ignore the next character.
736 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
738 substr($res, $off, 2, 'XX');
743 if ($c eq "'" || $c eq '"') {
744 if ($sanitise_quote eq '') {
745 $sanitise_quote = $c;
747 substr($res, $off, 1, $c);
749 } elsif ($sanitise_quote eq $c) {
750 $sanitise_quote = '';
754 #print "c<$c> SQ<$sanitise_quote>\n";
755 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
756 substr($res, $off, 1, $;);
757 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
758 substr($res, $off, 1, $;);
759 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
760 substr($res, $off, 1, 'X');
762 substr($res, $off, 1, $c);
766 if ($sanitise_quote eq '//') {
767 $sanitise_quote = '';
770 # The pathname on a #include may be surrounded by '<' and '>'.
771 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
772 my $clean = 'X' x
length($1);
773 $res =~ s
@\<.*\
>@<$clean>@;
775 # The whole of a #error is a string.
776 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
777 my $clean = 'X' x
length($1);
778 $res =~ s
@(\#\s
*(?
:error
|warning
)\s
+).*@$1$clean@;
784 sub get_quoted_string
{
785 my ($line, $rawline) = @_;
787 return "" if ($line !~ m/(\"[X]+\")/g);
788 return substr($rawline, $-[0], $+[0] - $-[0]);
791 sub ctx_statement_block
{
792 my ($linenr, $remain, $off) = @_;
793 my $line = $linenr - 1;
810 @stack = (['', 0]) if ($#stack == -1);
812 #warn "CSB: blk<$blk> remain<$remain>\n";
813 # If we are about to drop off the end, pull in more
816 for (; $remain > 0; $line++) {
817 last if (!defined $lines[$line]);
818 next if ($lines[$line] =~ /^-/);
821 $blk .= $lines[$line] . "\n";
826 # Bail if there is no further context.
827 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
831 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
837 $c = substr($blk, $off, 1);
838 $remainder = substr($blk, $off);
840 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
842 # Handle nested #if/#else.
843 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
844 push(@stack, [ $type, $level ]);
845 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
846 ($type, $level) = @{$stack[$#stack - 1]};
847 } elsif ($remainder =~ /^#\s*endif\b/) {
848 ($type, $level) = @{pop(@stack)};
851 # Statement ends at the ';' or a close '}' at the
853 if ($level == 0 && $c eq ';') {
857 # An else is really a conditional as long as its not else if
858 if ($level == 0 && $coff_set == 0 &&
859 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
860 $remainder =~ /^(else)(?:\s|{)/ &&
861 $remainder !~ /^else\s+if\b/) {
862 $coff = $off + length($1) - 1;
864 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
865 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
868 if (($type eq '' || $type eq '(') && $c eq '(') {
872 if ($type eq '(' && $c eq ')') {
874 $type = ($level != 0)?
'(' : '';
876 if ($level == 0 && $coff < $soff) {
879 #warn "CSB: mark coff<$coff>\n";
882 if (($type eq '' || $type eq '{') && $c eq '{') {
886 if ($type eq '{' && $c eq '}') {
888 $type = ($level != 0)?
'{' : '';
891 if (substr($blk, $off + 1, 1) eq ';') {
897 # Preprocessor commands end at the newline unless escaped.
898 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
906 # We are truly at the end, so shuffle to the next line.
913 my $statement = substr($blk, $soff, $off - $soff + 1);
914 my $condition = substr($blk, $soff, $coff - $soff + 1);
916 #warn "STATEMENT<$statement>\n";
917 #warn "CONDITION<$condition>\n";
919 #print "coff<$coff> soff<$off> loff<$loff>\n";
921 return ($statement, $condition,
922 $line, $remain + 1, $off - $loff + 1, $level);
925 sub statement_lines
{
928 # Strip the diff line prefixes and rip blank lines at start and end.
929 $stmt =~ s/(^|\n)./$1/g;
933 my @stmt_lines = ($stmt =~ /\n/g);
935 return $#stmt_lines + 2;
938 sub statement_rawlines
{
941 my @stmt_lines = ($stmt =~ /\n/g);
943 return $#stmt_lines + 2;
946 sub statement_block_size
{
949 $stmt =~ s/(^|\n)./$1/g;
955 my @stmt_lines = ($stmt =~ /\n/g);
956 my @stmt_statements = ($stmt =~ /;/g);
958 my $stmt_lines = $#stmt_lines + 2;
959 my $stmt_statements = $#stmt_statements + 1;
961 if ($stmt_lines > $stmt_statements) {
964 return $stmt_statements;
968 sub ctx_statement_full
{
969 my ($linenr, $remain, $off) = @_;
970 my ($statement, $condition, $level);
974 # Grab the first conditional/block pair.
975 ($statement, $condition, $linenr, $remain, $off, $level) =
976 ctx_statement_block
($linenr, $remain, $off);
977 #print "F: c<$condition> s<$statement> remain<$remain>\n";
978 push(@chunks, [ $condition, $statement ]);
979 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
980 return ($level, $linenr, @chunks);
983 # Pull in the following conditional/block pairs and see if they
984 # could continue the statement.
986 ($statement, $condition, $linenr, $remain, $off, $level) =
987 ctx_statement_block
($linenr, $remain, $off);
988 #print "C: c<$condition> s<$statement> remain<$remain>\n";
989 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
991 push(@chunks, [ $condition, $statement ]);
994 return ($level, $linenr, @chunks);
998 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1000 my $start = $linenr - 1;
1007 my @stack = ($level);
1008 for ($line = $start; $remain > 0; $line++) {
1009 next if ($rawlines[$line] =~ /^-/);
1012 $blk .= $rawlines[$line];
1014 # Handle nested #if/#else.
1015 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1016 push(@stack, $level);
1017 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1018 $level = $stack[$#stack - 1];
1019 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1020 $level = pop(@stack);
1023 foreach my $c (split(//, $lines[$line])) {
1024 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1030 if ($c eq $close && $level > 0) {
1032 last if ($level == 0);
1033 } elsif ($c eq $open) {
1038 if (!$outer || $level <= 1) {
1039 push(@res, $rawlines[$line]);
1042 last if ($level == 0);
1045 return ($level, @res);
1047 sub ctx_block_outer
{
1048 my ($linenr, $remain) = @_;
1050 my ($level, @r) = ctx_block_get
($linenr, $remain, 1, '{', '}', 0);
1054 my ($linenr, $remain) = @_;
1056 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
1060 my ($linenr, $remain, $off) = @_;
1062 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
1065 sub ctx_block_level
{
1066 my ($linenr, $remain) = @_;
1068 return ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
1070 sub ctx_statement_level
{
1071 my ($linenr, $remain, $off) = @_;
1073 return ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
1076 sub ctx_locate_comment
{
1077 my ($first_line, $end_line) = @_;
1079 # Catch a comment on the end of the line itself.
1080 my ($current_comment) = ($rawlines[$end_line - 1] =~ m
@.*(/\*.*\*/)\s
*(?
:\\\s
*)?
$@);
1081 return $current_comment if (defined $current_comment);
1083 # Look through the context and try and figure out if there is a
1086 $current_comment = '';
1087 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1088 my $line = $rawlines[$linenr - 1];
1090 if ($linenr == $first_line and $line =~ m
@^.\s
*\
*@) {
1093 if ($line =~ m
@/\*@) {
1096 if (!$in_comment && $current_comment ne '') {
1097 $current_comment = '';
1099 $current_comment .= $line . "\n" if ($in_comment);
1100 if ($line =~ m
@\*/@) {
1105 chomp($current_comment);
1106 return($current_comment);
1108 sub ctx_has_comment
{
1109 my ($first_line, $end_line) = @_;
1110 my $cmt = ctx_locate_comment
($first_line, $end_line);
1112 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1113 ##print "CMMT: $cmt\n";
1115 return ($cmt ne '');
1119 my ($linenr, $cnt) = @_;
1121 my $offset = $linenr - 1;
1126 $line = $rawlines[$offset++];
1127 next if (defined($line) && $line =~ /^-/);
1139 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1142 $coded = sprintf("^%c", unpack('C', $2) + 64);
1151 my $av_preprocessor = 0;
1156 sub annotate_reset
{
1157 $av_preprocessor = 0;
1159 @av_paren_type = ('E');
1160 $av_pend_colon = 'O';
1163 sub annotate_values
{
1164 my ($stream, $type) = @_;
1167 my $var = '_' x
length($stream);
1170 print "$stream\n" if ($dbg_values > 1);
1172 while (length($cur)) {
1173 @av_paren_type = ('E') if ($#av_paren_type < 0);
1174 print " <" . join('', @av_paren_type) .
1175 "> <$type> <$av_pending>" if ($dbg_values > 1);
1176 if ($cur =~ /^(\s+)/o) {
1177 print "WS($1)\n" if ($dbg_values > 1);
1178 if ($1 =~ /\n/ && $av_preprocessor) {
1179 $type = pop(@av_paren_type);
1180 $av_preprocessor = 0;
1183 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1184 print "CAST($1)\n" if ($dbg_values > 1);
1185 push(@av_paren_type, $type);
1188 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1189 print "DECLARE($1)\n" if ($dbg_values > 1);
1192 } elsif ($cur =~ /^($Modifier)\s*/) {
1193 print "MODIFIER($1)\n" if ($dbg_values > 1);
1196 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1197 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1198 $av_preprocessor = 1;
1199 push(@av_paren_type, $type);
1205 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1206 print "UNDEF($1)\n" if ($dbg_values > 1);
1207 $av_preprocessor = 1;
1208 push(@av_paren_type, $type);
1210 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1211 print "PRE_START($1)\n" if ($dbg_values > 1);
1212 $av_preprocessor = 1;
1214 push(@av_paren_type, $type);
1215 push(@av_paren_type, $type);
1218 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1219 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1220 $av_preprocessor = 1;
1222 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1226 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1227 print "PRE_END($1)\n" if ($dbg_values > 1);
1229 $av_preprocessor = 1;
1231 # Assume all arms of the conditional end as this
1232 # one does, and continue as if the #endif was not here.
1233 pop(@av_paren_type);
1234 push(@av_paren_type, $type);
1237 } elsif ($cur =~ /^(\\\n)/o) {
1238 print "PRECONT($1)\n" if ($dbg_values > 1);
1240 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1241 print "ATTR($1)\n" if ($dbg_values > 1);
1242 $av_pending = $type;
1245 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1246 print "SIZEOF($1)\n" if ($dbg_values > 1);
1252 } elsif ($cur =~ /^(if|while|for)\b/o) {
1253 print "COND($1)\n" if ($dbg_values > 1);
1257 } elsif ($cur =~/^(case)/o) {
1258 print "CASE($1)\n" if ($dbg_values > 1);
1259 $av_pend_colon = 'C';
1262 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1263 print "KEYWORD($1)\n" if ($dbg_values > 1);
1266 } elsif ($cur =~ /^(\()/o) {
1267 print "PAREN('$1')\n" if ($dbg_values > 1);
1268 push(@av_paren_type, $av_pending);
1272 } elsif ($cur =~ /^(\))/o) {
1273 my $new_type = pop(@av_paren_type);
1274 if ($new_type ne '_') {
1276 print "PAREN('$1') -> $type\n"
1277 if ($dbg_values > 1);
1279 print "PAREN('$1')\n" if ($dbg_values > 1);
1282 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1283 print "FUNC($1)\n" if ($dbg_values > 1);
1287 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1288 if (defined $2 && $type eq 'C' || $type eq 'T') {
1289 $av_pend_colon = 'B';
1290 } elsif ($type eq 'E') {
1291 $av_pend_colon = 'L';
1293 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1296 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1297 print "IDENT($1)\n" if ($dbg_values > 1);
1300 } elsif ($cur =~ /^($Assignment)/o) {
1301 print "ASSIGN($1)\n" if ($dbg_values > 1);
1304 } elsif ($cur =~/^(;|{|})/) {
1305 print "END($1)\n" if ($dbg_values > 1);
1307 $av_pend_colon = 'O';
1309 } elsif ($cur =~/^(,)/) {
1310 print "COMMA($1)\n" if ($dbg_values > 1);
1313 } elsif ($cur =~ /^(\?)/o) {
1314 print "QUESTION($1)\n" if ($dbg_values > 1);
1317 } elsif ($cur =~ /^(:)/o) {
1318 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1320 substr($var, length($res), 1, $av_pend_colon);
1321 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1326 $av_pend_colon = 'O';
1328 } elsif ($cur =~ /^(\[)/o) {
1329 print "CLOSE($1)\n" if ($dbg_values > 1);
1332 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1335 print "OPV($1)\n" if ($dbg_values > 1);
1342 substr($var, length($res), 1, $variant);
1345 } elsif ($cur =~ /^($Operators)/o) {
1346 print "OP($1)\n" if ($dbg_values > 1);
1347 if ($1 ne '++' && $1 ne '--') {
1351 } elsif ($cur =~ /(^.)/o) {
1352 print "C($1)\n" if ($dbg_values > 1);
1355 $cur = substr($cur, length($1));
1356 $res .= $type x
length($1);
1360 return ($res, $var);
1364 my ($possible, $line) = @_;
1365 my $notPermitted = qr{(?
:
1382 ^(?
:typedef
|struct
|enum
)\b
1384 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1385 if ($possible !~ $notPermitted) {
1386 # Check for modifiers.
1387 $possible =~ s/\s*$Storage\s*//g;
1388 $possible =~ s/\s*$Sparse\s*//g;
1389 if ($possible =~ /^\s*$/) {
1391 } elsif ($possible =~ /\s/) {
1392 $possible =~ s/\s*$Type\s*//g;
1393 for my $modifier (split(' ', $possible)) {
1394 if ($modifier !~ $notPermitted) {
1395 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1396 push(@modifierList, $modifier);
1401 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1402 push(@typeList, $possible);
1406 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1413 return defined $use_type{$_[0]} if (scalar keys %use_type > 0);
1415 return !defined $ignore_type{$_[0]};
1419 if (!show_type
($_[1]) ||
1420 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1425 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1427 $line = "$prefix$_[0]: $_[2]\n";
1429 $line = (split('\n', $line))[0] . "\n" if ($terse);
1431 push(our @report, $line);
1440 if (report
("ERROR", $_[0], $_[1])) {
1448 if (report
("WARNING", $_[0], $_[1])) {
1456 if ($check && report
("CHECK", $_[0], $_[1])) {
1464 sub check_absolute_file
{
1465 my ($absolute, $herecurr) = @_;
1466 my $file = $absolute;
1468 ##print "absolute<$absolute>\n";
1470 # See if any suffix of this path is a path within the tree.
1471 while ($file =~ s
@^[^/]*/@@) {
1472 if (-f
"$root/$file") {
1473 ##print "file<$file>\n";
1481 # It is, so see if the prefix is acceptable.
1482 my $prefix = $absolute;
1483 substr($prefix, -length($file)) = '';
1485 ##print "prefix<$prefix>\n";
1486 if ($prefix ne ".../") {
1487 WARN
("USE_RELATIVE_PATH",
1488 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1495 $string =~ s/^\s+|\s+$//g;
1503 $string =~ s/^\s+//;
1511 $string =~ s/\s+$//;
1519 my $source_indent = 8;
1520 my $max_spaces_before_tab = $source_indent - 1;
1521 my $spaces_to_tab = " " x
$source_indent;
1523 #convert leading spaces to tabs
1524 1 while $leading =~ s
@^([\t]*)$spaces_to_tab@$1\t@g;
1525 #Remove spaces before a tab
1526 1 while $leading =~ s
@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1531 sub pos_last_openparen
{
1536 my $opens = $line =~ tr/\(/\(/;
1537 my $closes = $line =~ tr/\)/\)/;
1539 my $last_openparen = 0;
1541 if (($opens == 0) || ($closes >= $opens)) {
1545 my $len = length($line);
1547 for ($pos = 0; $pos < $len; $pos++) {
1548 my $string = substr($line, $pos);
1549 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1550 $pos += length($1) - 1;
1551 } elsif (substr($line, $pos, 1) eq '(') {
1552 $last_openparen = $pos;
1553 } elsif (index($string, '(') == -1) {
1558 return $last_openparen + 1;
1562 my $filename = shift;
1568 my $stashrawline="";
1579 my $in_header_lines = 1;
1580 my $in_commit_log = 0; #Scanning lines before patch
1582 my $non_utf8_charset = 0;
1590 # Trace the real file/line as we go.
1596 my $comment_edge = 0;
1600 my $prev_values = 'E';
1603 my %suppress_ifbraces;
1604 my %suppress_whiletrailers;
1605 my %suppress_export;
1606 my $suppress_statement = 0;
1608 my %signatures = ();
1610 # Pre-scan the patch sanitizing the lines.
1611 # Pre-scan the patch looking for any __setup documentation.
1613 my @setup_docs = ();
1616 my $camelcase_file_seeded = 0;
1618 sanitise_line_reset
();
1620 foreach my $rawline (@rawlines) {
1624 push(@fixed, $rawline) if ($fix);
1626 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1628 if ($1 =~ m
@Documentation/kernel-parameters
.txt
$@) {
1633 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1642 # Guestimate if this is a continuing comment. Run
1643 # the context looking for a comment "edge". If this
1644 # edge is a close comment then we must be in a comment
1648 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1649 next if (defined $rawlines[$ln - 1] &&
1650 $rawlines[$ln - 1] =~ /^-/);
1652 #print "RAW<$rawlines[$ln - 1]>\n";
1653 last if (!defined $rawlines[$ln - 1]);
1654 if ($rawlines[$ln - 1] =~ m
@(/\*|\*/)@ &&
1655 $rawlines[$ln - 1] !~ m
@"[^"]*(?
:/\*|\*/)[^"]*"@) {
1660 if (defined $edge && $edge eq '*/') {
1664 # Guestimate if this is a continuing comment. If this
1665 # is the start of a diff block and this line starts
1666 # ' *' then it is very likely a comment.
1667 if (!defined $edge &&
1668 $rawlines[$linenr] =~ m
@^.\s
*(?
:\
*\
*+| \
*)(?
:\s
|$)@)
1673 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1674 sanitise_line_reset
($in_comment);
1676 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1677 # Standardise the strings and chars within the input to
1678 # simplify matching -- only bother with positive lines.
1679 $line = sanitise_line
($rawline);
1681 push(@lines, $line);
1684 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1689 #print "==>$rawline\n";
1690 #print "-->$line\n";
1692 if ($setup_docs && $line =~ /^\+/) {
1693 push(@setup_docs, $line);
1701 foreach my $line (@lines) {
1703 my $sline = $line; #copy of $line
1704 $sline =~ s/$;/ /g; #with comments as spaces
1706 my $rawline = $rawlines[$linenr - 1];
1708 #extract the line range in the file after the patch is applied
1709 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1711 $first_line = $linenr + 1;
1721 %suppress_ifbraces = ();
1722 %suppress_whiletrailers = ();
1723 %suppress_export = ();
1724 $suppress_statement = 0;
1727 # track the line number as we move through the hunk, note that
1728 # new versions of GNU diff omit the leading space on completely
1729 # blank context lines so we need to count that too.
1730 } elsif ($line =~ /^( |\+|$)/) {
1732 $realcnt-- if ($realcnt != 0);
1734 # Measure the line length and indent.
1735 ($length, $indent) = line_stats
($rawline);
1737 # Track the previous line.
1738 ($prevline, $stashline) = ($stashline, $line);
1739 ($previndent, $stashindent) = ($stashindent, $indent);
1740 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1742 #warn "line<$line>\n";
1744 } elsif ($realcnt == 1) {
1748 my $hunk_line = ($realcnt != 0);
1750 #make up the handle for any error we report on this line
1751 $prefix = "$filename:$realline: " if ($emacs && $file);
1752 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1754 $here = "#$linenr: " if (!$file);
1755 $here = "#$realline: " if ($file);
1757 # extract the filename as it passes
1758 if ($line =~ /^diff --git.*?(\S+)$/) {
1760 $realfile =~ s
@^([^/]*)/@@;
1762 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1764 $realfile =~ s
@^([^/]*)/@@;
1768 if (!$file && $tree && $p1_prefix ne '' &&
1769 -e
"$root/$p1_prefix") {
1770 WARN
("PATCH_PREFIX",
1771 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1774 if ($realfile =~ m
@^include/asm/@) {
1775 ERROR
("MODIFIED_INCLUDE_ASM",
1776 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1781 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1783 my $hereline = "$here\n$rawline\n";
1784 my $herecurr = "$here\n$rawline\n";
1785 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1787 $cnt_lines++ if ($realcnt != 0);
1789 # Check for incorrect file permissions
1790 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1791 my $permhere = $here . "FILE: $realfile\n";
1792 if ($realfile !~ m
@scripts/@ &&
1793 $realfile !~ /\.(py|pl|awk|sh)$/) {
1794 ERROR
("EXECUTE_PERMISSIONS",
1795 "do not set execute permissions for source files\n" . $permhere);
1799 # Check the patch for a signoff:
1800 if ($line =~ /^\s*signed-off-by:/i) {
1805 # Check signature styles
1806 if (!$in_header_lines &&
1807 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
1808 my $space_before = $1;
1810 my $space_after = $3;
1812 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1814 if ($sign_off !~ /$signature_tags/) {
1815 WARN
("BAD_SIGN_OFF",
1816 "Non-standard signature: $sign_off\n" . $herecurr);
1818 if (defined $space_before && $space_before ne "") {
1819 if (WARN
("BAD_SIGN_OFF",
1820 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1822 $fixed[$linenr - 1] =
1823 "$ucfirst_sign_off $email";
1826 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1827 if (WARN
("BAD_SIGN_OFF",
1828 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1830 $fixed[$linenr - 1] =
1831 "$ucfirst_sign_off $email";
1835 if (!defined $space_after || $space_after ne " ") {
1836 if (WARN
("BAD_SIGN_OFF",
1837 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1839 $fixed[$linenr - 1] =
1840 "$ucfirst_sign_off $email";
1844 my ($email_name, $email_address, $comment) = parse_email
($email);
1845 my $suggested_email = format_email
(($email_name, $email_address));
1846 if ($suggested_email eq "") {
1847 ERROR
("BAD_SIGN_OFF",
1848 "Unrecognized email address: '$email'\n" . $herecurr);
1850 my $dequoted = $suggested_email;
1851 $dequoted =~ s/^"//;
1852 $dequoted =~ s/" </ </;
1853 # Don't force email to have quotes
1854 # Allow just an angle bracketed address
1855 if ("$dequoted$comment" ne $email &&
1856 "<$email_address>$comment" ne $email &&
1857 "$suggested_email$comment" ne $email) {
1858 WARN
("BAD_SIGN_OFF",
1859 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
1863 # Check for duplicate signatures
1864 my $sig_nospace = $line;
1865 $sig_nospace =~ s/\s//g;
1866 $sig_nospace = lc($sig_nospace);
1867 if (defined $signatures{$sig_nospace}) {
1868 WARN
("BAD_SIGN_OFF",
1869 "Duplicate signature\n" . $herecurr);
1871 $signatures{$sig_nospace} = 1;
1875 # Check for wrappage within a valid hunk of the file
1876 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1877 ERROR
("CORRUPTED_PATCH",
1878 "patch seems to be corrupt (line wrapped?)\n" .
1879 $herecurr) if (!$emitted_corrupt++);
1882 # Check for absolute kernel paths.
1884 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1887 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1888 check_absolute_file
($1, $herecurr)) {
1891 check_absolute_file
($file, $herecurr);
1896 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1897 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1898 $rawline !~ m/^$UTF8*$/) {
1899 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1901 my $blank = copy_spacing
($rawline);
1902 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1903 my $hereptr = "$hereline$ptr\n";
1906 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1909 # Check if it's the start of a commit log
1910 # (not a header line and we haven't seen the patch filename)
1911 if ($in_header_lines && $realfile =~ /^$/ &&
1912 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
1913 $in_header_lines = 0;
1917 # Check if there is UTF-8 in a commit log when a mail header has explicitly
1918 # declined it, i.e defined some charset where it is missing.
1919 if ($in_header_lines &&
1920 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1922 $non_utf8_charset = 1;
1925 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
1926 $rawline =~ /$NON_ASCII_UTF8/) {
1927 WARN
("UTF8_BEFORE_PATCH",
1928 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1931 # ignore non-hunk lines and lines being removed
1932 next if (!$hunk_line || $line =~ /^-/);
1934 #trailing whitespace
1935 if ($line =~ /^\+.*\015/) {
1936 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1937 if (ERROR
("DOS_LINE_ENDINGS",
1938 "DOS line endings\n" . $herevet) &&
1940 $fixed[$linenr - 1] =~ s/[\s\015]+$//;
1942 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1943 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1944 if (ERROR
("TRAILING_WHITESPACE",
1945 "trailing whitespace\n" . $herevet) &&
1947 $fixed[$linenr - 1] =~ s/\s+$//;
1953 # check for Kconfig help text having a real description
1954 # Only applies when adding the entry originally, after that we do not have
1955 # sufficient context to determine whether it is indeed long enough.
1956 if ($realfile =~ /Kconfig/ &&
1957 $line =~ /.\s*config\s+/) {
1960 my $ln = $linenr + 1;
1964 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
1965 $f = $lines[$ln - 1];
1966 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1967 $is_end = $lines[$ln - 1] =~ /^\+/;
1969 next if ($f =~ /^-/);
1971 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1973 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1980 next if ($f =~ /^$/);
1981 if ($f =~ /^\s*config\s/) {
1987 WARN
("CONFIG_DESCRIPTION",
1988 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1989 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
1992 # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
1993 if ($realfile =~ /Kconfig/ &&
1994 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
1995 WARN
("CONFIG_EXPERIMENTAL",
1996 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1999 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2000 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2003 'EXTRA_AFLAGS' => 'asflags-y',
2004 'EXTRA_CFLAGS' => 'ccflags-y',
2005 'EXTRA_CPPFLAGS' => 'cppflags-y',
2006 'EXTRA_LDFLAGS' => 'ldflags-y',
2009 WARN
("DEPRECATED_VARIABLE",
2010 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2013 # check we are in a valid source file if not then ignore this hunk
2014 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
2017 if ($line =~ /^\+/ && $prevrawline !~ /\/\
*\
*/ &&
2018 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
2019 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
2020 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
2021 $length > $max_line_length)
2024 "line over $max_line_length characters\n" . $herecurr);
2027 # Check for user-visible strings broken across lines, which breaks the ability
2028 # to grep for the string. Limited to strings used as parameters (those
2029 # following an open parenthesis), which almost completely eliminates false
2030 # positives, as well as warning only once per parameter rather than once per
2031 # line of the string. Make an exception when the previous string ends in a
2032 # newline (multiple lines in one string constant) or \n\t (common in inline
2033 # assembly to indent the instruction on the following line).
2034 if ($line =~ /^\+\s*"/ &&
2035 $prevline =~ /"\s*$/ &&
2036 $prevline =~ /\(/ &&
2037 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
2038 WARN
("SPLIT_STRING",
2039 "quoted string split across lines\n" . $hereprev);
2042 # check for spaces before a quoted newline
2043 if ($rawline =~ /^.*\".*\s\\n/) {
2044 if (WARN
("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2045 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2047 $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
2052 # check for adding lines without a newline.
2053 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2054 WARN
("MISSING_EOF_NEWLINE",
2055 "adding a line without newline at end of file\n" . $herecurr);
2058 # Blackfin: use hi/lo macros
2059 if ($realfile =~ m
@arch/blackfin/.*\
.S
$@) {
2060 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2061 my $herevet = "$here\n" . cat_vet
($line) . "\n";
2063 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
2065 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2066 my $herevet = "$here\n" . cat_vet
($line) . "\n";
2068 "use the HI() macro, not (... >> 16)\n" . $herevet);
2072 # check we are in a valid source file C or perl if not then ignore this hunk
2073 next if ($realfile !~ /\.(h|c|pl)$/);
2075 # at the beginning of a line any tabs must come first and anything
2076 # more than 8 must use tabs.
2077 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2078 $rawline =~ /^\+\s* \s*/) {
2079 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2081 if (ERROR
("CODE_INDENT",
2082 "code indent should use tabs where possible\n" . $herevet) &&
2084 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2088 # check for space before tabs.
2089 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2090 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2091 if (WARN
("SPACE_BEFORE_TAB",
2092 "please, no space before tabs\n" . $herevet) &&
2094 $fixed[$linenr - 1] =~
2095 s/(^\+.*) +\t/$1\t/;
2099 # check for && or || at the start of a line
2100 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2101 CHK
("LOGICAL_CONTINUATIONS",
2102 "Logical continuations should be on the previous line\n" . $hereprev);
2105 # check multi-line statement indentation matches previous line
2106 if ($^V && $^V ge 5.10.0 &&
2107 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2108 $prevline =~ /^\+(\t*)(.*)$/;
2112 my $pos = pos_last_openparen
($rest);
2114 $line =~ /^(\+| )([ \t]*)/;
2117 my $goodtabindent = $oldindent .
2120 my $goodspaceindent = $oldindent . " " x
$pos;
2122 if ($newindent ne $goodtabindent &&
2123 $newindent ne $goodspaceindent) {
2125 if (CHK
("PARENTHESIS_ALIGNMENT",
2126 "Alignment should match open parenthesis\n" . $hereprev) &&
2127 $fix && $line =~ /^\+/) {
2128 $fixed[$linenr - 1] =~
2129 s/^\+[ \t]*/\+$goodtabindent/;
2135 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
2137 "No space is necessary after a cast\n" . $hereprev) &&
2139 $fixed[$linenr - 1] =~
2140 s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
2144 if ($realfile =~ m
@^(drivers
/net
/|net
/)@ &&
2145 $prevrawline =~ /^\+[ \t]*\/\
*[ \t]*$/ &&
2146 $rawline =~ /^\+[ \t]*\*/) {
2147 WARN
("NETWORKING_BLOCK_COMMENT_STYLE",
2148 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2151 if ($realfile =~ m
@^(drivers
/net
/|net
/)@ &&
2152 $prevrawline =~ /^\+[ \t]*\/\
*/ && #starting /*
2153 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
2154 $rawline =~ /^\+/ && #line is new
2155 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2156 WARN
("NETWORKING_BLOCK_COMMENT_STYLE",
2157 "networking block comments start with * on subsequent lines\n" . $hereprev);
2160 if ($realfile =~ m
@^(drivers
/net
/|net
/)@ &&
2161 $rawline !~ m
@^\+[ \t]*\
*/[ \t]*$@ && #trailing */
2162 $rawline !~ m
@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2163 $rawline !~ m
@^\+.*\
*{2,}/[ \t]*$@ && #trailing **/
2164 $rawline =~ m
@^\+[ \t]*.+\
*\
/[ \t]*$@) { #non blank */
2165 WARN
("NETWORKING_BLOCK_COMMENT_STYLE",
2166 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2169 # check for spaces at the beginning of a line.
2171 # 1) within comments
2172 # 2) indented preprocessor commands
2174 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
2175 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2176 if (WARN
("LEADING_SPACE",
2177 "please, no spaces at the start of a line\n" . $herevet) &&
2179 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2183 # check we are in a valid C source file if not then ignore this hunk
2184 next if ($realfile !~ /\.(h|c)$/);
2186 # discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2187 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2188 WARN
("CONFIG_EXPERIMENTAL",
2189 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2192 # check for RCS/CVS revision markers
2193 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2195 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2198 # Blackfin: don't use __builtin_bfin_[cs]sync
2199 if ($line =~ /__builtin_bfin_csync/) {
2200 my $herevet = "$here\n" . cat_vet
($line) . "\n";
2202 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
2204 if ($line =~ /__builtin_bfin_ssync/) {
2205 my $herevet = "$here\n" . cat_vet
($line) . "\n";
2207 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
2210 # check for old HOTPLUG __dev<foo> section markings
2211 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2212 WARN
("HOTPLUG_SECTION",
2213 "Using $1 is unnecessary\n" . $herecurr);
2216 # Check for potential 'bare' types
2217 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2219 #print "LINE<$line>\n";
2220 if ($linenr >= $suppress_statement &&
2221 $realcnt && $sline =~ /.\s*\S/) {
2222 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2223 ctx_statement_block
($linenr, $realcnt, 0);
2224 $stat =~ s/\n./\n /g;
2225 $cond =~ s/\n./\n /g;
2227 #print "linenr<$linenr> <$stat>\n";
2228 # If this statement has no statement boundaries within
2229 # it there is no point in retrying a statement scan
2230 # until we hit end of it.
2231 my $frag = $stat; $frag =~ s/;+\s*$//;
2232 if ($frag !~ /(?:{|;)/) {
2233 #print "skip<$line_nr_next>\n";
2234 $suppress_statement = $line_nr_next;
2237 # Find the real next line.
2238 $realline_next = $line_nr_next;
2239 if (defined $realline_next &&
2240 (!defined $lines[$realline_next - 1] ||
2241 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2248 # Ignore goto labels.
2249 if ($s =~ /$Ident:\*$/s) {
2251 # Ignore functions being called
2252 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2254 } elsif ($s =~ /^.\s*else\b/s) {
2256 # declarations always start with types
2257 } 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) {
2260 possible
($type, "A:" . $s);
2262 # definitions in global scope can only start with types
2263 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2264 possible
($1, "B:" . $s);
2267 # any (foo ... *) is a pointer cast, and foo is a type
2268 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2269 possible
($1, "C:" . $s);
2272 # Check for any sort of function declaration.
2273 # int foo(something bar, other baz);
2274 # void (*store_gdt)(x86_descr_ptr *);
2275 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
2276 my ($name_len) = length($1);
2279 substr($ctx, 0, $name_len + 1, '');
2280 $ctx =~ s/\)[^\)]*$//;
2282 for my $arg (split(/\s*,\s*/, $ctx)) {
2283 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
2285 possible
($1, "D:" . $s);
2293 # Checks which may be anchored in the context.
2296 # Check for switch () and associated case and default
2297 # statements should be at the same indent.
2298 if ($line=~/\bswitch\s*\(.*\)/) {
2301 my @ctx = ctx_block_outer
($linenr, $realcnt);
2303 for my $ctx (@ctx) {
2304 my ($clen, $cindent) = line_stats
($ctx);
2305 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2306 $indent != $cindent) {
2307 $err .= "$sep$ctx\n";
2314 ERROR
("SWITCH_CASE_INDENT_LEVEL",
2315 "switch and case should be at the same indent\n$hereline$err");
2319 # if/while/etc brace do not go on next line, unless defining a do while loop,
2320 # or if that brace on the next line is for something else
2321 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2322 my $pre_ctx = "$1$2";
2324 my ($level, @ctx) = ctx_statement_level
($linenr, $realcnt, 0);
2326 if ($line =~ /^\+\t{6,}/) {
2327 WARN
("DEEP_INDENTATION",
2328 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2331 my $ctx_cnt = $realcnt - $#ctx - 1;
2332 my $ctx = join("\n", @ctx);
2334 my $ctx_ln = $linenr;
2335 my $ctx_skip = $realcnt;
2337 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2338 defined $lines[$ctx_ln - 1] &&
2339 $lines[$ctx_ln - 1] =~ /^-/)) {
2340 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2341 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2345 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2346 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2348 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2350 "that open brace { should be on the previous line\n" .
2351 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2353 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2354 $ctx =~ /\)\s*\;\s*$/ &&
2355 defined $lines[$ctx_ln - 1])
2357 my ($nlength, $nindent) = line_stats
($lines[$ctx_ln - 1]);
2358 if ($nindent > $indent) {
2359 WARN
("TRAILING_SEMICOLON",
2360 "trailing semicolon indicates no statements, indent implies otherwise\n" .
2361 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2366 # Check relative indent for conditionals and blocks.
2367 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
2368 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2369 ctx_statement_block
($linenr, $realcnt, 0)
2370 if (!defined $stat);
2371 my ($s, $c) = ($stat, $cond);
2373 substr($s, 0, length($c), '');
2375 # Make sure we remove the line prefixes as we have
2376 # none on the first line, and are going to readd them
2380 # Find out how long the conditional actually is.
2381 my @newlines = ($c =~ /\n/gs);
2382 my $cond_lines = 1 + $#newlines;
2384 # We want to check the first line inside the block
2385 # starting at the end of the conditional, so remove:
2386 # 1) any blank line termination
2387 # 2) any opening brace { on end of the line
2389 my $continuation = 0;
2391 $s =~ s/^.*\bdo\b//;
2393 if ($s =~ s/^\s*\\//) {
2396 if ($s =~ s/^\s*?\n//) {
2401 # Also ignore a loop construct at the end of a
2402 # preprocessor statement.
2403 if (($prevline =~ /^.\s*#\s*define\s/ ||
2404 $prevline =~ /\\\s*$/) && $continuation == 0) {
2410 while ($cond_ptr != $cond_lines) {
2411 $cond_ptr = $cond_lines;
2413 # If we see an #else/#elif then the code
2415 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2420 # 1) blank lines, they should be at 0,
2421 # 2) preprocessor lines, and
2423 if ($continuation ||
2425 $s =~ /^\s*#\s*?/ ||
2426 $s =~ /^\s*$Ident\s*:/) {
2427 $continuation = ($s =~ /^.*?\\\n/) ?
1 : 0;
2428 if ($s =~ s/^.*?\n//) {
2434 my (undef, $sindent) = line_stats
("+" . $s);
2435 my $stat_real = raw_line
($linenr, $cond_lines);
2437 # Check if either of these lines are modified, else
2438 # this is not this patch's fault.
2439 if (!defined($stat_real) ||
2440 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2443 if (defined($stat_real) && $cond_lines > 1) {
2444 $stat_real = "[...]\n$stat_real";
2447 #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";
2449 if ($check && (($sindent % 8) != 0 ||
2450 ($sindent <= $indent && $s ne ''))) {
2451 WARN
("SUSPECT_CODE_INDENT",
2452 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
2456 # Track the 'values' across context and added lines.
2457 my $opline = $line; $opline =~ s/^./ /;
2458 my ($curr_values, $curr_vars) =
2459 annotate_values
($opline . "\n", $prev_values);
2460 $curr_values = $prev_values . $curr_values;
2462 my $outline = $opline; $outline =~ s/\t/ /g;
2463 print "$linenr > .$outline\n";
2464 print "$linenr > $curr_values\n";
2465 print "$linenr > $curr_vars\n";
2467 $prev_values = substr($curr_values, -1);
2469 #ignore lines not being added
2470 next if ($line =~ /^[^\+]/);
2472 # TEST: allow direct testing of the type matcher.
2474 if ($line =~ /^.\s*$Declare\s*$/) {
2476 "TEST: is type\n" . $herecurr);
2477 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2478 ERROR
("TEST_NOT_TYPE",
2479 "TEST: is not type ($1 is)\n". $herecurr);
2483 # TEST: allow direct testing of the attribute matcher.
2485 if ($line =~ /^.\s*$Modifier\s*$/) {
2487 "TEST: is attr\n" . $herecurr);
2488 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2489 ERROR
("TEST_NOT_ATTR",
2490 "TEST: is not attr ($1 is)\n". $herecurr);
2495 # check for initialisation to aggregates open brace on the next line
2496 if ($line =~ /^.\s*{/ &&
2497 $prevline =~ /(?:^|[^=])=\s*$/) {
2499 "that open brace { should be on the previous line\n" . $hereprev);
2503 # Checks which are anchored on the added line.
2506 # check for malformed paths in #include statements (uses RAW line)
2507 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2509 if ($path =~ m{//}) {
2510 ERROR
("MALFORMED_INCLUDE",
2511 "malformed #include filename\n" . $herecurr);
2513 if ($path =~ "^uapi/" && $realfile =~ m
@\binclude/uapi/@) {
2514 ERROR
("UAPI_INCLUDE",
2515 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2519 # no C99 // comments
2520 if ($line =~ m{//}) {
2521 if (ERROR
("C99_COMMENTS",
2522 "do not use C99 // comments\n" . $herecurr) &&
2524 my $line = $fixed[$linenr - 1];
2525 if ($line =~ /\/\
/(.*)$/) {
2526 my $comment = trim
($1);
2527 $fixed[$linenr - 1] =~ s
@\/\/(.*)$@/\* $comment \
*/@;
2531 # Remove C99 comments.
2533 $opline =~ s
@//.*@@;
2535 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2536 # the whole statement.
2537 #print "APW <$lines[$realline_next - 1]>\n";
2538 if (defined $realline_next &&
2539 exists $lines[$realline_next - 1] &&
2540 !defined $suppress_export{$realline_next} &&
2541 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2542 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2543 # Handle definitions which produce identifiers with
2546 # EXPORT_SYMBOL(something_foo);
2548 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
2549 $name =~ /^${Ident}_$2/) {
2550 #print "FOO C name<$name>\n";
2551 $suppress_export{$realline_next} = 1;
2553 } elsif ($stat !~ /(?
:
2555 ^.DEFINE_
$Ident\(\Q
$name\E\)|
2556 ^.DECLARE_
$Ident\(\Q
$name\E\)|
2557 ^.LIST_HEAD\
(\Q
$name\E\)|
2558 ^.(?
:$Storage\s+)?
$Type\s*\(\s
*\
*\s
*\Q
$name\E\s*\)\s
*\
(|
2559 \b\Q
$name\E(?
:\s
+$Attribute)*\s
*(?
:;|=|\
[|\
()
2561 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2562 $suppress_export{$realline_next} = 2;
2564 $suppress_export{$realline_next} = 1;
2567 if (!defined $suppress_export{$linenr} &&
2568 $prevline =~ /^.\s*$/ &&
2569 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2570 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2571 #print "FOO B <$lines[$linenr - 1]>\n";
2572 $suppress_export{$linenr} = 2;
2574 if (defined $suppress_export{$linenr} &&
2575 $suppress_export{$linenr} == 2) {
2576 WARN
("EXPORT_SYMBOL",
2577 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
2580 # check for global initialisers.
2581 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2582 if (ERROR
("GLOBAL_INITIALISERS",
2583 "do not initialise globals to 0 or NULL\n" .
2586 $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2589 # check for static initialisers.
2590 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2591 if (ERROR
("INITIALISED_STATIC",
2592 "do not initialise statics to 0 or NULL\n" .
2595 $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2599 # check for static const char * arrays.
2600 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2601 WARN
("STATIC_CONST_CHAR_ARRAY",
2602 "static const char * array should probably be static const char * const\n" .
2606 # check for static char foo[] = "bar" declarations.
2607 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2608 WARN
("STATIC_CONST_CHAR_ARRAY",
2609 "static char array declaration should probably be static const char\n" .
2613 # check for declarations of struct pci_device_id
2614 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2615 WARN
("DEFINE_PCI_DEVICE_TABLE",
2616 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
2619 # check for new typedefs, only function parameters and sparse annotations
2621 if ($line =~ /\btypedef\s/ &&
2622 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2623 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
2624 $line !~ /\b$typeTypedefs\b/ &&
2625 $line !~ /\b__bitwise(?:__|)\b/) {
2626 WARN
("NEW_TYPEDEFS",
2627 "do not add new typedefs\n" . $herecurr);
2630 # * goes on variable not on type
2632 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2634 my ($ident, $from, $to) = ($1, $2, $2);
2636 # Should start with a space.
2637 $to =~ s/^(\S)/ $1/;
2638 # Should not end with a space.
2640 # '*'s should not have spaces between.
2641 while ($to =~ s/\*\s+\*/\*\*/) {
2644 ## print "1: from<$from> to<$to> ident<$ident>\n";
2646 if (ERROR
("POINTER_LOCATION",
2647 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
2649 my $sub_from = $ident;
2650 my $sub_to = $ident;
2651 $sub_to =~ s/\Q$from\E/$to/;
2652 $fixed[$linenr - 1] =~
2653 s
@\Q$sub_from\E@$sub_to@;
2657 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2659 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
2661 # Should start with a space.
2662 $to =~ s/^(\S)/ $1/;
2663 # Should not end with a space.
2665 # '*'s should not have spaces between.
2666 while ($to =~ s/\*\s+\*/\*\*/) {
2668 # Modifiers should have spaces.
2669 $to =~ s/(\b$Modifier$)/$1 /;
2671 ## print "2: from<$from> to<$to> ident<$ident>\n";
2672 if ($from ne $to && $ident !~ /^$Modifier$/) {
2673 if (ERROR
("POINTER_LOCATION",
2674 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
2677 my $sub_from = $match;
2678 my $sub_to = $match;
2679 $sub_to =~ s/\Q$from\E/$to/;
2680 $fixed[$linenr - 1] =~
2681 s
@\Q$sub_from\E@$sub_to@;
2686 # # no BUG() or BUG_ON()
2687 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
2688 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2689 # print "$herecurr";
2693 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2694 WARN
("LINUX_VERSION_CODE",
2695 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
2698 # check for uses of printk_ratelimit
2699 if ($line =~ /\bprintk_ratelimit\s*\(/) {
2700 WARN
("PRINTK_RATELIMITED",
2701 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
2704 # printk should use KERN_* levels. Note that follow on printk's on the
2705 # same line do not need a level, so we use the current block context
2706 # to try and find and validate the current printk. In summary the current
2707 # printk includes all preceding printk's which have no newline on the end.
2708 # we assume the first bad printk is the one to report.
2709 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
2711 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2712 #print "CHECK<$lines[$ln - 1]\n";
2713 # we have a preceding printk if it ends
2714 # with "\n" ignore it, else it is to blame
2715 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2716 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2723 WARN
("PRINTK_WITHOUT_KERN_LEVEL",
2724 "printk() should include KERN_ facility level\n" . $herecurr);
2728 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2730 my $level = lc($orig);
2731 $level = "warn" if ($level eq "warning");
2732 my $level2 = $level;
2733 $level2 = "dbg" if ($level eq "debug");
2734 WARN
("PREFER_PR_LEVEL",
2735 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
2738 if ($line =~ /\bpr_warning\s*\(/) {
2739 if (WARN
("PREFER_PR_LEVEL",
2740 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
2742 $fixed[$linenr - 1] =~
2743 s/\bpr_warning\b/pr_warn/;
2747 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2749 my $level = lc($orig);
2750 $level = "warn" if ($level eq "warning");
2751 $level = "dbg" if ($level eq "debug");
2752 WARN
("PREFER_DEV_LEVEL",
2753 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2756 # function brace can't be on same line, except for #defines of do while,
2757 # or if closed on same line
2758 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2759 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2761 "open brace '{' following function declarations go on the next line\n" . $herecurr);
2764 # open braces for enum, union and struct go on the same line.
2765 if ($line =~ /^.\s*{/ &&
2766 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2768 "open brace '{' following $1 go on the same line\n" . $hereprev);
2771 # missing space after union, struct or enum definition
2772 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2774 "missing space after $1 definition\n" . $herecurr) &&
2776 $fixed[$linenr - 1] =~
2777 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
2781 # check for spacing round square brackets; allowed:
2782 # 1. with a type on the left -- int [] a;
2783 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2784 # 3. inside a curly brace -- = { [0...10] = 5 }
2785 while ($line =~ /(.*?\s)\[/g) {
2786 my ($where, $prefix) = ($-[1], $1);
2787 if ($prefix !~ /$Type\s+$/ &&
2788 ($where != 0 || $prefix !~ /^.\s+$/) &&
2789 $prefix !~ /[{,]\s+$/) {
2790 if (ERROR
("BRACKET_SPACE",
2791 "space prohibited before open square bracket '['\n" . $herecurr) &&
2793 $fixed[$linenr - 1] =~
2794 s/^(\+.*?)\s+\[/$1\[/;
2799 # check for spaces between functions and their parentheses.
2800 while ($line =~ /($Ident)\s+\(/g) {
2802 my $ctx_before = substr($line, 0, $-[1]);
2803 my $ctx = "$ctx_before$name";
2805 # Ignore those directives where spaces _are_ permitted.
2807 if|for|while|switch
|return|case
|
2808 volatile
|__volatile__
|
2809 __attribute__
|format
|__extension__
|
2812 # cpp #define statements have non-optional spaces, ie
2813 # if there is a space between the name and the open
2814 # parenthesis it is simply not a parameter group.
2815 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2817 # cpp #elif statement condition may start with a (
2818 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2820 # If this whole things ends with a type its most
2821 # likely a typedef for a function.
2822 } elsif ($ctx =~ /$Type$/) {
2826 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
2828 $fixed[$linenr - 1] =~
2829 s/\b$name\s+\(/$name\(/;
2834 # Check operator spacing.
2835 if (!($line=~/\#\s*include/)) {
2836 my $fixed_line = "";
2840 <<=|>>=|<=|>=|==|!=|
2841 \
+=|-=|\
*=|\
/=|%=|\
^=|\
|=|&=|
2842 =>|->|<<|>>|<|>|=|!|~|
2843 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%|
2846 my @elements = split(/($ops|;)/, $opline);
2848 ## print("element count: <" . $#elements . ">\n");
2849 ## foreach my $el (@elements) {
2850 ## print("el: <$el>\n");
2853 my @fix_elements = ();
2856 foreach my $el (@elements) {
2857 push(@fix_elements, substr($rawline, $off, length($el)));
2858 $off += length($el);
2863 my $blank = copy_spacing
($opline);
2864 my $last_after = -1;
2866 for (my $n = 0; $n < $#elements; $n += 2) {
2868 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
2870 ## print("n: <$n> good: <$good>\n");
2872 $off += length($elements[$n]);
2874 # Pick up the preceding and succeeding characters.
2875 my $ca = substr($opline, 0, $off);
2877 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2878 $cc = substr($opline, $off + length($elements[$n + 1]));
2880 my $cb = "$ca$;$cc";
2883 $a = 'V' if ($elements[$n] ne '');
2884 $a = 'W' if ($elements[$n] =~ /\s$/);
2885 $a = 'C' if ($elements[$n] =~ /$;$/);
2886 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2887 $a = 'O' if ($elements[$n] eq '');
2888 $a = 'E' if ($ca =~ /^\s*$/);
2890 my $op = $elements[$n + 1];
2893 if (defined $elements[$n + 2]) {
2894 $c = 'V' if ($elements[$n + 2] ne '');
2895 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2896 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2897 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2898 $c = 'O' if ($elements[$n + 2] eq '');
2899 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2904 my $ctx = "${a}x${c}";
2906 my $at = "(ctx:$ctx)";
2908 my $ptr = substr($blank, 0, $off) . "^";
2909 my $hereptr = "$hereline$ptr\n";
2911 # Pull out the value of this operator.
2912 my $op_type = substr($curr_values, $off + 1, 1);
2914 # Get the full operator variant.
2915 my $opv = $op . substr($curr_vars, $off, 1);
2917 # Ignore operators passed as parameters.
2918 if ($op_type ne 'V' &&
2919 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2922 # } elsif ($op =~ /^$;+$/) {
2924 # ; should have either the end of line or a space or \ after it
2925 } elsif ($op eq ';') {
2926 if ($ctx !~ /.x[WEBC]/ &&
2927 $cc !~ /^\\/ && $cc !~ /^;/) {
2928 if (ERROR
("SPACING",
2929 "space required after that '$op' $at\n" . $hereptr)) {
2930 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]) . " ";
2936 } elsif ($op eq '//') {
2940 # : when part of a bitfield
2941 } elsif ($op eq '->' || $opv eq ':B') {
2942 if ($ctx =~ /Wx.|.xW/) {
2943 if (ERROR
("SPACING",
2944 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
2945 $good = rtrim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
2946 if (defined $fix_elements[$n + 2]) {
2947 $fix_elements[$n + 2] =~ s/^\s+//;
2953 # , must have a space on the right.
2954 } elsif ($op eq ',') {
2955 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2956 if (ERROR
("SPACING",
2957 "space required after that '$op' $at\n" . $hereptr)) {
2958 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]) . " ";
2964 # '*' as part of a type definition -- reported already.
2965 } elsif ($opv eq '*_') {
2966 #warn "'*' is part of type\n";
2968 # unary operators should have a space before and
2969 # none after. May be left adjacent to another
2970 # unary operator, or a cast
2971 } elsif ($op eq '!' || $op eq '~' ||
2972 $opv eq '*U' || $opv eq '-U' ||
2973 $opv eq '&U' || $opv eq '&&U') {
2974 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2975 if (ERROR
("SPACING",
2976 "space required before that '$op' $at\n" . $hereptr)) {
2977 if ($n != $last_after + 2) {
2978 $good = $fix_elements[$n] . " " . ltrim
($fix_elements[$n + 1]);
2983 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2984 # A unary '*' may be const
2986 } elsif ($ctx =~ /.xW/) {
2987 if (ERROR
("SPACING",
2988 "space prohibited after that '$op' $at\n" . $hereptr)) {
2989 $good = $fix_elements[$n] . rtrim
($fix_elements[$n + 1]);
2990 if (defined $fix_elements[$n + 2]) {
2991 $fix_elements[$n + 2] =~ s/^\s+//;
2997 # unary ++ and unary -- are allowed no space on one side.
2998 } elsif ($op eq '++' or $op eq '--') {
2999 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
3000 if (ERROR
("SPACING",
3001 "space required one side of that '$op' $at\n" . $hereptr)) {
3002 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]) . " ";
3006 if ($ctx =~ /Wx[BE]/ ||
3007 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
3008 if (ERROR
("SPACING",
3009 "space prohibited before that '$op' $at\n" . $hereptr)) {
3010 $good = rtrim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
3014 if ($ctx =~ /ExW/) {
3015 if (ERROR
("SPACING",
3016 "space prohibited after that '$op' $at\n" . $hereptr)) {
3017 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]);
3018 if (defined $fix_elements[$n + 2]) {
3019 $fix_elements[$n + 2] =~ s/^\s+//;
3025 # << and >> may either have or not have spaces both sides
3026 } elsif ($op eq '<<' or $op eq '>>' or
3027 $op eq '&' or $op eq '^' or $op eq '|' or
3028 $op eq '+' or $op eq '-' or
3029 $op eq '*' or $op eq '/' or
3032 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
3033 if (ERROR
("SPACING",
3034 "need consistent spacing around '$op' $at\n" . $hereptr)) {
3035 $good = rtrim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]) . " ";
3036 if (defined $fix_elements[$n + 2]) {
3037 $fix_elements[$n + 2] =~ s/^\s+//;
3043 # A colon needs no spaces before when it is
3044 # terminating a case value or a label.
3045 } elsif ($opv eq ':C' || $opv eq ':L') {
3046 if ($ctx =~ /Wx./) {
3047 if (ERROR
("SPACING",
3048 "space prohibited before that '$op' $at\n" . $hereptr)) {
3049 $good = rtrim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
3054 # All the others need spaces both sides.
3055 } elsif ($ctx !~ /[EWC]x[CWE]/) {
3058 # Ignore email addresses <foo@bar>
3060 $cc =~ /^\S+\@\S+>/) ||
3062 $ca =~ /<\S+\@\S+$/))
3067 # messages are ERROR, but ?: are CHK
3069 my $msg_type = \
&ERROR
;
3070 $msg_type = \
&CHK
if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3072 if (&{$msg_type}("SPACING",
3073 "spaces required around that '$op' $at\n" . $hereptr)) {
3074 $good = rtrim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]) . " ";
3075 if (defined $fix_elements[$n + 2]) {
3076 $fix_elements[$n + 2] =~ s/^\s+//;
3082 $off += length($elements[$n + 1]);
3084 ## print("n: <$n> GOOD: <$good>\n");
3086 $fixed_line = $fixed_line . $good;
3089 if (($#elements % 2) == 0) {
3090 $fixed_line = $fixed_line . $fix_elements[$#elements];
3093 if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
3094 $fixed[$linenr - 1] = $fixed_line;
3100 # check for whitespace before a non-naked semicolon
3101 if ($line =~ /^\+.*\S\s+;/) {
3103 "space prohibited before semicolon\n" . $herecurr) &&
3105 1 while $fixed[$linenr - 1] =~
3106 s/^(\+.*\S)\s+;/$1;/;
3110 # check for multiple assignments
3111 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3112 CHK
("MULTIPLE_ASSIGNMENTS",
3113 "multiple assignments should be avoided\n" . $herecurr);
3116 ## # check for multiple declarations, allowing for a function declaration
3118 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3119 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3121 ## # Remove any bracketed sections to ensure we do not
3122 ## # falsly report the parameters of functions.
3124 ## while ($ln =~ s/\([^\(\)]*\)//g) {
3126 ## if ($ln =~ /,/) {
3127 ## WARN("MULTIPLE_DECLARATION",
3128 ## "declaring multiple variables together should be avoided\n" . $herecurr);
3132 #need space before brace following if, while, etc
3133 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3135 if (ERROR
("SPACING",
3136 "space required before the open brace '{'\n" . $herecurr) &&
3138 $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
3142 ## # check for blank lines before declarations
3143 ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3144 ## $prevrawline =~ /^.\s*$/) {
3146 ## "No blank lines before declarations\n" . $hereprev);
3150 # closing brace should have a space following it when it has anything
3152 if ($line =~ /}(?!(?:,|;|\)))\S/) {
3153 if (ERROR
("SPACING",
3154 "space required after that close brace '}'\n" . $herecurr) &&
3156 $fixed[$linenr - 1] =~
3157 s/}((?!(?:,|;|\)))\S)/} $1/;
3161 # check spacing on square brackets
3162 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
3163 if (ERROR
("SPACING",
3164 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3166 $fixed[$linenr - 1] =~
3170 if ($line =~ /\s\]/) {
3171 if (ERROR
("SPACING",
3172 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3174 $fixed[$linenr - 1] =~
3179 # check spacing on parentheses
3180 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3181 $line !~ /for\s*\(\s+;/) {
3182 if (ERROR
("SPACING",
3183 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3185 $fixed[$linenr - 1] =~
3189 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3190 $line !~ /for\s*\(.*;\s+\)/ &&
3191 $line !~ /:\s+\)/) {
3192 if (ERROR
("SPACING",
3193 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3195 $fixed[$linenr - 1] =~
3200 #goto labels aren't indented, allow a single space however
3201 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
3202 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
3203 if (WARN
("INDENTED_LABEL",
3204 "labels should not be indented\n" . $herecurr) &&
3206 $fixed[$linenr - 1] =~
3211 # Return is not a function.
3212 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
3216 # Flatten any parentheses
3217 $value =~ s/\(/ \(/g;
3218 $value =~ s/\)/\) /g;
3219 while ($value =~ s/\[[^\[\]]*\]/1/ ||
3220 $value !~ /(?
:$Ident|-?
$Constant)\s
*
3222 (?
:$Ident|-?
$Constant)/x
&&
3223 $value =~ s/\([^\(\)]*\)/1/) {
3225 #print "value<$value>\n";
3226 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
3227 ERROR
("RETURN_PARENTHESES",
3228 "return is not a function, parentheses are not required\n" . $herecurr);
3230 } elsif ($spacing !~ /\s+/) {
3232 "space required before the open parenthesis '('\n" . $herecurr);
3235 # Return of what appears to be an errno should normally be -'ve
3236 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3238 if ($name ne 'EOF' && $name ne 'ERROR') {
3239 WARN
("USE_NEGATIVE_ERRNO",
3240 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
3244 # Need a space before open parenthesis after if, while etc
3245 if ($line =~ /\b(if|while|for|switch)\(/) {
3246 if (ERROR
("SPACING",
3247 "space required before the open parenthesis '('\n" . $herecurr) &&
3249 $fixed[$linenr - 1] =~
3250 s/\b(if|while|for|switch)\(/$1 \(/;
3254 # Check for illegal assignment in if conditional -- and check for trailing
3255 # statements after the conditional.
3256 if ($line =~ /do\s*(?!{)/) {
3257 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3258 ctx_statement_block
($linenr, $realcnt, 0)
3259 if (!defined $stat);
3260 my ($stat_next) = ctx_statement_block
($line_nr_next,
3261 $remain_next, $off_next);
3262 $stat_next =~ s/\n./\n /g;
3263 ##print "stat<$stat> stat_next<$stat_next>\n";
3265 if ($stat_next =~ /^\s*while\b/) {
3266 # If the statement carries leading newlines,
3267 # then count those as offsets.
3269 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3271 statement_rawlines
($whitespace) - 1;
3273 $suppress_whiletrailers{$line_nr_next +
3277 if (!defined $suppress_whiletrailers{$linenr} &&
3278 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3279 my ($s, $c) = ($stat, $cond);
3281 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3282 ERROR
("ASSIGN_IN_IF",
3283 "do not use assignment in if condition\n" . $herecurr);
3286 # Find out what is on the end of the line after the
3288 substr($s, 0, length($c), '');
3290 $s =~ s/$;//g; # Remove any comments
3291 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3292 $c !~ /}\s*while\s*/)
3294 # Find out how long the conditional actually is.
3295 my @newlines = ($c =~ /\n/gs);
3296 my $cond_lines = 1 + $#newlines;
3299 $stat_real = raw_line
($linenr, $cond_lines)
3300 . "\n" if ($cond_lines);
3301 if (defined($stat_real) && $cond_lines > 1) {
3302 $stat_real = "[...]\n$stat_real";
3305 ERROR
("TRAILING_STATEMENTS",
3306 "trailing statements should be on next line\n" . $herecurr . $stat_real);
3310 # Check for bitwise tests written as boolean
3322 WARN
("HEXADECIMAL_BOOLEAN_TEST",
3323 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
3326 # if and else should not have general statements after it
3327 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3329 $s =~ s/$;//g; # Remove any comments
3330 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3331 ERROR
("TRAILING_STATEMENTS",
3332 "trailing statements should be on next line\n" . $herecurr);
3335 # if should not continue a brace
3336 if ($line =~ /}\s*if\b/) {
3337 ERROR
("TRAILING_STATEMENTS",
3338 "trailing statements should be on next line\n" .
3341 # case and default should not have general statements after them
3342 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3344 (?
:\s
*$;*)(?
:\s
*{)?
(?
:\s
*$;*)(?
:\s
*\\)?\s
*$|
3348 ERROR
("TRAILING_STATEMENTS",
3349 "trailing statements should be on next line\n" . $herecurr);
3352 # Check for }<nl>else {, these must be at the same
3353 # indent level to be relevant to each other.
3354 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3355 $previndent == $indent) {
3356 ERROR
("ELSE_AFTER_BRACE",
3357 "else should follow close brace '}'\n" . $hereprev);
3360 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3361 $previndent == $indent) {
3362 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
3364 # Find out what is on the end of the line after the
3366 substr($s, 0, length($c), '');
3369 if ($s =~ /^\s*;/) {
3370 ERROR
("WHILE_AFTER_BRACE",
3371 "while should follow close brace '}'\n" . $hereprev);
3375 #Specific variable tests
3376 while ($line =~ m{($Constant|$Lval)}g) {
3379 #gcc binary extension
3380 if ($var =~ /^$Binary$/) {
3381 if (WARN
("GCC_BINARY_CONSTANT",
3382 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3384 my $hexval = sprintf("0x%x", oct($var));
3385 $fixed[$linenr - 1] =~
3386 s/\b$var\b/$hexval/;
3391 if ($var !~ /^$Constant$/ &&
3392 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
3393 #Ignore Page<foo> variants
3394 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
3395 #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
3396 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
3397 while ($var =~ m{($Ident)}g) {
3399 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
3401 seed_camelcase_includes
();
3402 if (!$file && !$camelcase_file_seeded) {
3403 seed_camelcase_file
($realfile);
3404 $camelcase_file_seeded = 1;
3407 if (!defined $camelcase{$word}) {
3408 $camelcase{$word} = 1;
3410 "Avoid CamelCase: <$word>\n" . $herecurr);
3416 #no spaces allowed after \ in define
3417 if ($line =~ /\#\s*define.*\\\s+$/) {
3418 if (WARN
("WHITESPACE_AFTER_LINE_CONTINUATION",
3419 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3421 $fixed[$linenr - 1] =~ s/\s+$//;
3425 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
3426 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
3428 my $checkfile = "include/linux/$file";
3429 if (-f
"$root/$checkfile" &&
3430 $realfile ne $checkfile &&
3431 $1 !~ /$allowed_asm_includes/)
3433 if ($realfile =~ m{^arch/}) {
3434 CHK
("ARCH_INCLUDE_LINUX",
3435 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3437 WARN
("INCLUDE_LINUX",
3438 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3443 # multi-statement macros should be enclosed in a do while loop, grab the
3444 # first statement and ensure its the whole macro if its not enclosed
3445 # in a known good container
3446 if ($realfile !~ m
@/vmlinux.lds
.h
$@ &&
3447 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
3450 my ($off, $dstat, $dcond, $rest);
3452 ($dstat, $dcond, $ln, $cnt, $off) =
3453 ctx_statement_block
($linenr, $realcnt, 0);
3455 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
3456 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
3458 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
3460 $dstat =~ s/\\\n.//g;
3461 $dstat =~ s/^\s*//s;
3462 $dstat =~ s/\s*$//s;
3464 # Flatten any parentheses and braces
3465 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3466 $dstat =~ s/\{[^\{\}]*\}/1/ ||
3467 $dstat =~ s/\[[^\[\]]*\]/1/)
3471 # Flatten any obvious string concatentation.
3472 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3473 $dstat =~ s/$Ident\s*("X*")/$1/)
3477 my $exceptions = qr{
3489 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
3491 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3492 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
3493 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
3494 $dstat !~ /^'X'$/ && # character constants
3495 $dstat !~ /$exceptions/ &&
3496 $dstat !~ /^\.$Ident\s*=/ && # .foo =
3497 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
3498 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
3499 $dstat !~ /^for\s*$Constant$/ && # for (...)
3500 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3501 $dstat !~ /^do\s*{/ && # do {...
3502 $dstat !~ /^\({/ && # ({...
3503 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
3506 my $herectx = $here . "\n";
3507 my $cnt = statement_rawlines
($ctx);
3509 for (my $n = 0; $n < $cnt; $n++) {
3510 $herectx .= raw_line
($linenr, $n) . "\n";
3513 if ($dstat =~ /;/) {
3514 ERROR
("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3515 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3517 ERROR
("COMPLEX_MACRO",
3518 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
3522 # check for line continuations outside of #defines, preprocessor #, and asm
3525 if ($prevline !~ /^..*\\$/ &&
3526 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3527 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
3528 $line =~ /^\+.*\\$/) {
3529 WARN
("LINE_CONTINUATIONS",
3530 "Avoid unnecessary line continuations\n" . $herecurr);
3534 # do {} while (0) macro tests:
3535 # single-statement macros do not need to be enclosed in do while (0) loop,
3536 # macro should not end with a semicolon
3537 if ($^V && $^V ge 5.10.0 &&
3538 $realfile !~ m
@/vmlinux.lds
.h
$@ &&
3539 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3542 my ($off, $dstat, $dcond, $rest);
3544 ($dstat, $dcond, $ln, $cnt, $off) =
3545 ctx_statement_block
($linenr, $realcnt, 0);
3548 $dstat =~ s/\\\n.//g;
3550 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3555 my $cnt = statement_rawlines
($ctx);
3556 my $herectx = $here . "\n";
3558 for (my $n = 0; $n < $cnt; $n++) {
3559 $herectx .= raw_line
($linenr, $n) . "\n";
3562 if (($stmts =~ tr/;/;/) == 1 &&
3563 $stmts !~ /^\s*(if|while|for|switch)\b/) {
3564 WARN
("SINGLE_STATEMENT_DO_WHILE_MACRO",
3565 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3567 if (defined $semis && $semis ne "") {
3568 WARN
("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3569 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3574 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3575 # all assignments may have only one of the following with an assignment:
3578 # VMLINUX_SYMBOL(...)
3579 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
3580 WARN
("MISSING_VMLINUX_SYMBOL",
3581 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
3584 # check for redundant bracing round if etc
3585 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3586 my ($level, $endln, @chunks) =
3587 ctx_statement_full
($linenr, $realcnt, 1);
3588 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
3589 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3590 if ($#chunks > 0 && $level == 0) {
3594 my $herectx = $here . "\n";
3595 my $ln = $linenr - 1;
3596 for my $chunk (@chunks) {
3597 my ($cond, $block) = @{$chunk};
3599 # If the condition carries leading newlines, then count those as offsets.
3600 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3601 my $offset = statement_rawlines
($whitespace) - 1;
3603 $allowed[$allow] = 0;
3604 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3606 # We have looked at and allowed this specific line.
3607 $suppress_ifbraces{$ln + $offset} = 1;
3609 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
3610 $ln += statement_rawlines
($block) - 1;
3612 substr($block, 0, length($cond), '');
3614 $seen++ if ($block =~ /^\s*{/);
3616 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
3617 if (statement_lines
($cond) > 1) {
3618 #print "APW: ALLOWED: cond<$cond>\n";
3619 $allowed[$allow] = 1;
3621 if ($block =~/\b(?:if|for|while)\b/) {
3622 #print "APW: ALLOWED: block<$block>\n";
3623 $allowed[$allow] = 1;
3625 if (statement_block_size
($block) > 1) {
3626 #print "APW: ALLOWED: lines block<$block>\n";
3627 $allowed[$allow] = 1;
3632 my $sum_allowed = 0;
3633 foreach (@allowed) {
3636 if ($sum_allowed == 0) {
3638 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3639 } elsif ($sum_allowed != $allow &&
3642 "braces {} should be used on all arms of this statement\n" . $herectx);
3647 if (!defined $suppress_ifbraces{$linenr - 1} &&
3648 $line =~ /\b(if|while|for|else)\b/) {
3651 # Check the pre-context.
3652 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3653 #print "APW: ALLOWED: pre<$1>\n";
3657 my ($level, $endln, @chunks) =
3658 ctx_statement_full
($linenr, $realcnt, $-[0]);
3660 # Check the condition.
3661 my ($cond, $block) = @{$chunks[0]};
3662 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3663 if (defined $cond) {
3664 substr($block, 0, length($cond), '');
3666 if (statement_lines
($cond) > 1) {
3667 #print "APW: ALLOWED: cond<$cond>\n";
3670 if ($block =~/\b(?:if|for|while)\b/) {
3671 #print "APW: ALLOWED: block<$block>\n";
3674 if (statement_block_size
($block) > 1) {
3675 #print "APW: ALLOWED: lines block<$block>\n";
3678 # Check the post-context.
3679 if (defined $chunks[1]) {
3680 my ($cond, $block) = @{$chunks[1]};
3681 if (defined $cond) {
3682 substr($block, 0, length($cond), '');
3684 if ($block =~ /^\s*\{/) {
3685 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3689 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
3690 my $herectx = $here . "\n";
3691 my $cnt = statement_rawlines
($block);
3693 for (my $n = 0; $n < $cnt; $n++) {
3694 $herectx .= raw_line
($linenr, $n) . "\n";
3698 "braces {} are not necessary for single statement blocks\n" . $herectx);
3702 # check for unnecessary blank lines around braces
3703 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
3705 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3707 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
3709 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3712 # no volatiles please
3713 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3714 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3716 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
3720 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3721 CHK
("REDUNDANT_CODE",
3722 "if this code is redundant consider removing it\n" .
3726 # check for needless "if (<foo>) fn(<foo>)" uses
3727 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3728 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3729 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3731 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
3735 sub string_find_replace
{
3736 my ($string, $find, $replace) = @_;
3738 $string =~ s/$find/$replace/g;
3743 # check for bad placement of section $InitAttribute (e.g.: __initdata)
3744 if ($line =~ /(\b$InitAttribute\b)/) {
3746 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
3749 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
3750 ERROR
("MISPLACED_INIT",
3751 "$attr should be placed after $var\n" . $herecurr)) ||
3752 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
3753 WARN
("MISPLACED_INIT",
3754 "$attr should be placed after $var\n" . $herecurr))) &&
3756 $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;
3761 # prefer usleep_range over udelay
3762 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
3763 # ignore udelay's < 10, however
3766 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
3770 # warn about unexpectedly long msleep's
3771 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3774 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
3778 # check for comparisons of jiffies
3779 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3780 WARN
("JIFFIES_COMPARISON",
3781 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3784 # check for comparisons of get_jiffies_64()
3785 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3786 WARN
("JIFFIES_COMPARISON",
3787 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3790 # warn about #ifdefs in C files
3791 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
3792 # print "#ifdef in C files should be avoided\n";
3793 # print "$herecurr";
3797 # warn about spacing in #ifdefs
3798 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3799 if (ERROR
("SPACING",
3800 "exactly one space required after that #$1\n" . $herecurr) &&
3802 $fixed[$linenr - 1] =~
3803 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
3808 # check for spinlock_t definitions without a comment.
3809 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3810 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
3812 if (!ctx_has_comment
($first_line, $linenr)) {
3813 CHK
("UNCOMMENTED_DEFINITION",
3814 "$1 definition without comment\n" . $herecurr);
3817 # check for memory barriers without a comment.
3818 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3819 if (!ctx_has_comment
($first_line, $linenr)) {
3820 CHK
("MEMORY_BARRIER",
3821 "memory barrier without comment\n" . $herecurr);
3824 # check of hardware specific defines
3825 if ($line =~ m
@^.\s
*\#\s
*if.*\b(__i386__
|__powerpc64__
|__sun__
|__s390x__
)\b@ && $realfile !~ m
@include/asm-@) {
3827 "architecture specific defines should be avoided\n" . $herecurr);
3830 # Check that the storage class is at the beginning of a declaration
3831 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3832 WARN
("STORAGE_CLASS",
3833 "storage class should be at the beginning of the declaration\n" . $herecurr)
3836 # check the location of the inline attribute, that it is between
3837 # storage class and type.
3838 if ($line =~ /\b$Type\s+$Inline\b/ ||
3839 $line =~ /\b$Inline\s+$Storage\b/) {
3840 ERROR
("INLINE_LOCATION",
3841 "inline keyword should sit between storage class and type\n" . $herecurr);
3844 # Check for __inline__ and __inline, prefer inline
3845 if ($line =~ /\b(__inline__|__inline)\b/) {
3847 "plain inline is preferred over $1\n" . $herecurr) &&
3849 $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
3854 # Check for __attribute__ packed, prefer __packed
3855 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3856 WARN
("PREFER_PACKED",
3857 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
3860 # Check for __attribute__ aligned, prefer __aligned
3861 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3862 WARN
("PREFER_ALIGNED",
3863 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
3866 # Check for __attribute__ format(printf, prefer __printf
3867 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3868 if (WARN
("PREFER_PRINTF",
3869 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
3871 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
3876 # Check for __attribute__ format(scanf, prefer __scanf
3877 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3878 if (WARN
("PREFER_SCANF",
3879 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
3881 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
3885 # check for sizeof(&)
3886 if ($line =~ /\bsizeof\s*\(\s*\&/) {
3887 WARN
("SIZEOF_ADDRESS",
3888 "sizeof(& should be avoided\n" . $herecurr);
3891 # check for sizeof without parenthesis
3892 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
3893 if (WARN
("SIZEOF_PARENTHESIS",
3894 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
3896 $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
3900 # check for line continuations in quoted strings with odd counts of "
3901 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3902 WARN
("LINE_CONTINUATIONS",
3903 "Avoid line continuations in quoted strings\n" . $herecurr);
3906 # check for struct spinlock declarations
3907 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3908 WARN
("USE_SPINLOCK_T",
3909 "struct spinlock should be spinlock_t\n" . $herecurr);
3912 # check for seq_printf uses that could be seq_puts
3913 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
3914 my $fmt = get_quoted_string
($line, $rawline);
3915 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
3916 if (WARN
("PREFER_SEQ_PUTS",
3917 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
3919 $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
3924 # Check for misused memsets
3925 if ($^V && $^V ge 5.10.0 &&
3927 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3933 if ($ms_size =~ /^(0x|)0$/i) {
3935 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
3936 } elsif ($ms_size =~ /^(0x|)1$/i) {
3938 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3942 # typecasts on min/max could be min_t/max_t
3943 if ($^V && $^V ge 5.10.0 &&
3945 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3946 if (defined $2 || defined $7) {
3948 my $cast1 = deparenthesize
($2);
3950 my $cast2 = deparenthesize
($7);
3954 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
3955 $cast = "$cast1 or $cast2";
3956 } elsif ($cast1 ne "") {
3962 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
3966 # check usleep_range arguments
3967 if ($^V && $^V ge 5.10.0 &&
3969 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
3973 WARN
("USLEEP_RANGE",
3974 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3975 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
3977 WARN
("USLEEP_RANGE",
3978 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3982 # check for new externs in .h files.
3983 if ($realfile =~ /\.h$/ &&
3984 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
3985 if (CHK
("AVOID_EXTERNS",
3986 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
3988 $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
3992 # check for new externs in .c files.
3993 if ($realfile =~ /\.c$/ && defined $stat &&
3994 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
3996 my $function_name = $1;
3997 my $paren_space = $2;
4000 if (defined $cond) {
4001 substr($s, 0, length($cond), '');
4003 if ($s =~ /^\s*;/ &&
4004 $function_name ne 'uninitialized_var')
4006 WARN
("AVOID_EXTERNS",
4007 "externs should be avoided in .c files\n" . $herecurr);
4010 if ($paren_space =~ /\n/) {
4011 WARN
("FUNCTION_ARGUMENTS",
4012 "arguments for function declarations should follow identifier\n" . $herecurr);
4015 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4016 $stat =~ /^.\s*extern\s+/)
4018 WARN
("AVOID_EXTERNS",
4019 "externs should be avoided in .c files\n" . $herecurr);
4022 # checks for new __setup's
4023 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4026 if (!grep(/$name/, @setup_docs)) {
4027 CHK
("UNDOCUMENTED_SETUP",
4028 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
4032 # check for pointless casting of kmalloc return
4033 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
4034 WARN
("UNNECESSARY_CASTS",
4035 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
4039 # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4040 if ($^V && $^V ge 5.10.0 &&
4041 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4042 CHK
("ALLOC_SIZEOF_STRUCT",
4043 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4046 # check for krealloc arg reuse
4047 if ($^V && $^V ge 5.10.0 &&
4048 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4049 WARN
("KREALLOC_ARG_REUSE",
4050 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4053 # check for alloc argument mismatch
4054 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
4055 WARN
("ALLOC_ARRAY_ARGS",
4056 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
4059 # check for multiple semicolons
4060 if ($line =~ /;\s*;\s*$/) {
4061 if (WARN
("ONE_SEMICOLON",
4062 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4064 $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
4068 # check for switch/default statements without a break;
4069 if ($^V && $^V ge 5.10.0 &&
4071 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4073 my $herectx = $here . "\n";
4074 my $cnt = statement_rawlines
($stat);
4075 for (my $n = 0; $n < $cnt; $n++) {
4076 $herectx .= raw_line
($linenr, $n) . "\n";
4078 WARN
("DEFAULT_NO_BREAK",
4079 "switch default: should use break\n" . $herectx);
4082 # check for gcc specific __FUNCTION__
4083 if ($line =~ /\b__FUNCTION__\b/) {
4084 if (WARN
("USE_FUNC",
4085 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
4087 $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
4091 # check for use of yield()
4092 if ($line =~ /\byield\s*\(\s*\)/) {
4094 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
4097 # check for comparisons against true and false
4098 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4106 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4108 my $type = lc($otype);
4109 if ($type =~ /^(?:true|false)$/) {
4110 if (("$test" eq "==" && "$type" eq "true") ||
4111 ("$test" eq "!=" && "$type" eq "false")) {
4115 CHK
("BOOL_COMPARISON",
4116 "Using comparison to $otype is error prone\n" . $herecurr);
4118 ## maybe suggesting a correct construct would better
4119 ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4124 # check for semaphores initialized locked
4125 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
4126 WARN
("CONSIDER_COMPLETION",
4127 "consider using a completion\n" . $herecurr);
4130 # recommend kstrto* over simple_strto* and strict_strto*
4131 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
4132 WARN
("CONSIDER_KSTRTO",
4133 "$1 is obsolete, use k$3 instead\n" . $herecurr);
4136 # check for __initcall(), use device_initcall() explicitly please
4137 if ($line =~ /^.\s*__initcall\s*\(/) {
4138 WARN
("USE_DEVICE_INITCALL",
4139 "please use device_initcall() instead of __initcall()\n" . $herecurr);
4142 # check for various ops structs, ensure they are const.
4143 my $struct_ops = qr{acpi_dock_ops
|
4144 address_space_operations
|
4146 block_device_operations
|
4151 file_lock_operations
|
4161 lock_manager_operations
|
4167 pipe_buf_operations
|
4168 platform_hibernation_ops
|
4169 platform_suspend_ops
|
4174 soc_pcmcia_socket_ops
|
4180 if ($line !~ /\bconst\b/ &&
4181 $line =~ /\bstruct\s+($struct_ops)\b/) {
4182 WARN
("CONST_STRUCT",
4183 "struct $1 should normally be const\n" .
4187 # use of NR_CPUS is usually wrong
4188 # ignore definitions of NR_CPUS and usage to define arrays as likely right
4189 if ($line =~ /\bNR_CPUS\b/ &&
4190 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4191 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
4192 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4193 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4194 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
4197 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4200 # check for %L{u,d,i} in strings
4202 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4203 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4204 $string =~ s/%%/__/g;
4205 if ($string =~ /(?<!%)%L[udi]/) {
4207 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4212 # whine mightly about in_atomic
4213 if ($line =~ /\bin_atomic\s*\(/) {
4214 if ($realfile =~ m
@^drivers/@) {
4216 "do not use in_atomic in drivers\n" . $herecurr);
4217 } elsif ($realfile !~ m
@^kernel/@) {
4219 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
4223 # check for lockdep_set_novalidate_class
4224 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
4225 $line =~ /__lockdep_no_validate__\s*\)/ ) {
4226 if ($realfile !~ m
@^kernel/lockdep@ &&
4227 $realfile !~ m
@^include/linux/lockdep@ &&
4228 $realfile !~ m
@^drivers/base/core@) {
4230 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
4234 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
4235 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
4236 WARN
("EXPORTED_WORLD_WRITABLE",
4237 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
4241 # If we have no input at all, then there is nothing to report on
4242 # so just keep quiet.
4243 if ($#rawlines == -1) {
4247 # In mailback mode only produce a report in the negative, for
4248 # things that appear to be patches.
4249 if ($mailback && ($clean == 1 || !$is_patch)) {
4253 # This is not a patch, and we are are in 'no-patch' mode so
4255 if (!$chk_patch && !$is_patch) {
4260 ERROR
("NOT_UNIFIED_DIFF",
4261 "Does not appear to be a unified-diff format patch\n");
4263 if ($is_patch && $chk_signoff && $signoff == 0) {
4264 ERROR
("MISSING_SIGN_OFF",
4265 "Missing Signed-off-by: line(s)\n");
4268 print report_dump
();
4269 if ($summary && !($clean == 1 && $quiet == 1)) {
4270 print "$filename " if ($summary_file);
4271 print "total: $cnt_error errors, $cnt_warn warnings, " .
4272 (($check)?
"$cnt_chk checks, " : "") .
4273 "$cnt_lines lines checked\n";
4274 print "\n" if ($quiet == 0);
4279 if ($^V lt 5.10.0) {
4280 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4281 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4284 # If there were whitespace errors which cleanpatch can fix
4285 # then suggest that.
4286 if ($rpt_cleaners) {
4287 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4288 print " scripts/cleanfile\n\n";
4293 hash_show_words
(\
%use_type, "Used");
4294 hash_show_words
(\
%ignore_type, "Ignored");
4296 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
4297 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
4301 open($f, '>', $newfile)
4302 or die "$P: Can't open $newfile for write\n";
4303 foreach my $fixed_line (@fixed) {
4306 if ($linecount > 3) {
4307 $fixed_line =~ s/^\+//;
4308 print $f $fixed_line. "\n";
4311 print $f $fixed_line . "\n";
4318 Wrote EXPERIMENTAL
--fix correction
(s
) to
'$newfile'
4320 Do _NOT_ trust the results written to this file
.
4321 Do _NOT_ submit these changes without inspecting them
for correctness
.
4323 This EXPERIMENTAL file
is simply a convenience to help rewrite patches
.
4324 No warranties
, expressed
or implied
...
4330 if ($clean == 1 && $quiet == 0) {
4331 print "$vname has no obvious style problems and is ready for submission.\n"
4333 if ($clean == 0 && $quiet == 0) {
4335 $vname has style problems
, please review
.
4337 If any of these errors are false positives
, please report
4338 them to the maintainer
, see CHECKPATCH
in MAINTAINERS
.