2 # (c) 2001, Dave Jones. (the file handling bit)
3 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4 # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5 # (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
6 # Licensed under the terms of the GNU GPL License version 2
15 use Getopt
::Long
qw(:config no_auto_abbrev);
37 my $configuration_file = ".checkpatch.conf";
38 my $max_line_length = 80;
44 Usage
: $P [OPTION
]... [FILE
]...
49 --no-tree run without a kernel tree
50 --no-signoff
do not check
for 'Signed-off-by' line
51 --patch treat FILE as patchfile
(default)
52 --emacs emacs compile window format
53 --terse one line per report
54 -f
, --file treat FILE as regular source file
55 --subjective
, --strict enable more subjective tests
56 --ignore TYPE
(,TYPE2
...) ignore various comma separated message types
57 --max
-line
-length=n set the maximum line
length, if exceeded
, warn
58 --show
-types show the message
"types" in the output
59 --root
=PATH PATH to the kernel tree root
60 --no-summary suppress the per
-file summary
61 --mailback only produce a report
in case of warnings
/errors
62 --summary
-file include the filename
in summary
63 --debug KEY
=[0|1] turn on
/off debugging of KEY
, where KEY is one of
64 'values', 'possible', 'type', and 'attr' (default
66 --test
-only
=WORD report only warnings
/errors containing WORD
68 --fix EXPERIMENTAL
- may create horrible results
69 If correctable single
-line errors exist
, create
70 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
71 with potential errors corrected to the preferred
73 -h
, --help
, --version display this help
and exit
75 When FILE is
- read standard input
.
81 my $conf = which_conf
($configuration_file);
84 open(my $conffile, '<', "$conf")
85 or warn "$P: Can't find a readable $configuration_file file $!\n";
90 $line =~ s/\s*\n?$//g;
94 next if ($line =~ m/^\s*#/);
95 next if ($line =~ m/^\s*$/);
97 my @words = split(" ", $line);
98 foreach my $word (@words) {
99 last if ($word =~ m/^#/);
100 push (@conf_args, $word);
104 unshift(@ARGV, @conf_args) if @conf_args;
108 'q|quiet+' => \
$quiet,
110 'signoff!' => \
$chk_signoff,
111 'patch!' => \
$chk_patch,
115 'subjective!' => \
$check,
116 'strict!' => \
$check,
117 'ignore=s' => \
@ignore,
118 'show-types!' => \
$show_types,
119 'max-line-length=i' => \
$max_line_length,
121 'summary!' => \
$summary,
122 'mailback!' => \
$mailback,
123 'summary-file!' => \
$summary_file,
125 'debug=s' => \
%debug,
126 'test-only=s' => \
$tst_only,
136 print "$P: no input files\n";
140 @ignore = split(/,/, join(',',@ignore));
141 foreach my $word (@ignore) {
142 $word =~ s/\s*\n?$//g;
145 $word =~ tr/[a-z]/[A-Z]/;
147 next if ($word =~ m/^\s*#/);
148 next if ($word =~ m/^\s*$/);
150 $ignore_type{$word}++;
154 my $dbg_possible = 0;
157 for my $key (keys %debug) {
159 eval "\${dbg_$key} = '$debug{$key}';";
163 my $rpt_cleaners = 0;
172 if (!top_of_kernel_tree
($root)) {
173 die "$P: $root: --root does not point at a valid tree\n";
176 if (top_of_kernel_tree
('.')) {
178 } elsif ($0 =~ m@
(.*)/scripts/[^/]*$@
&&
179 top_of_kernel_tree
($1)) {
184 if (!defined $root) {
185 print "Must be run from the top-level dir. of a kernel tree\n";
190 my $emitted_corrupt = 0;
193 [A
-Za
-z_
][A
-Za
-z\d_
]*
194 (?
:\s
*\#\#\s
*[A
-Za
-z_
][A
-Za
-z\d_
]*)*
196 our $Storage = qr{extern|static|asmlinkage};
209 # Notes to $Attribute:
210 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
229 __
(?
:mem
|cpu
|dev
|)(?
:initdata
|initconst
|init
\b)|
230 ____cacheline_aligned
|
231 ____cacheline_aligned_in_smp
|
232 ____cacheline_internodealigned_in_smp
|
236 our $Inline = qr{inline|__always_inline|noinline};
237 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
238 our $Lval = qr{$Ident(?:$Member)*};
240 our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
241 our $Binary = qr{(?i)0b[01]+$Int_type?};
242 our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
243 our $Int = qr{[0-9]+$Int_type?};
244 our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
245 our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
246 our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
247 our $Float = qr{$Float_hex|$Float_dec|$Float_int};
248 our $Constant = qr{$Float|$Binary|$Hex|$Int};
249 our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
250 our $Compare = qr{<=|>=|==|!=|<|>};
251 our $Arithmetic = qr{\+|-|\*|\/|%};
255 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||$Arithmetic
262 our $NON_ASCII_UTF8 = qr{
263 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
264 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
265 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
266 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
267 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
268 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
269 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
273 [\x09\x0A\x0D\x20-\x7E] # ASCII
277 our $typeTypedefs = qr{(?x
:
278 (?
:__
)?
(?
:u
|s
|be
|le)(?
:8|16|32|64)|
282 our $logFunctions = qr{(?x
:
283 printk
(?
:_ratelimited
|_once
|)|
284 (?
:[a
-z0
-9]+_
){1,2}(?
:printk
|emerg
|alert
|crit
|err
|warning
|warn|notice
|info
|debug
|dbg
|vdbg
|devel
|cont
|WARN
)(?
:_ratelimited
|_once
|)|
285 WARN
(?
:_RATELIMIT
|_ONCE
|)|
290 our $signature_tags = qr{(?xi
:
303 qr{(?:unsigned\s+)?char},
304 qr{(?:unsigned\s+)?short},
305 qr{(?:unsigned\s+)?int},
306 qr{(?:unsigned\s+)?long},
307 qr{(?:unsigned\s+)?long\s+int},
308 qr{(?:unsigned\s+)?long\s+long},
309 qr{(?:unsigned\s+)?long\s+long\s+int},
318 qr{${Ident}_handler
},
319 qr{${Ident}_handler_fn
},
321 our @modifierList = (
325 our $allowed_asm_includes = qr{(?x
:
329 # memory.h: ARM has a custom one
332 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
333 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
334 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
336 (?
:$Modifier\s
+|const\s
+)*
338 (?
:typeof
|__typeof__
)\s
*\
([^\
)]*\
)|
342 (?
:\s
+$Modifier|\s
+const
)*
346 (?
:(?
:\s
|\
*|\
[\
])+\s
*const
|(?
:\s
|\
*|\
[\
])+|(?
:\s
*\
[\s
*\
])+)?
347 (?
:\s
+$Inline|\s
+$Modifier)*
349 $Declare = qr{(?:$Storage\s+)?$Type};
353 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s
*};
355 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
356 # requires at least perl version v5.10.0
357 # Any use must be runtime checked with $^V
359 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
360 our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s
*};
361 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
365 return "" if (!defined($string));
366 $string =~ s@
^\s
*\
(\s
*@
@g;
367 $string =~ s@\s
*\
)\s
*$@
@g;
368 $string =~ s@\s
+@
@g;
372 sub seed_camelcase_file
{
375 return if (!(-f
$file));
379 open(my $include_file, '<', "$file")
380 or warn "$P: Can't read '$file' $!\n";
381 my $text = <$include_file>;
382 close($include_file);
384 my @lines = split('\n', $text);
386 foreach my $line (@lines) {
387 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
388 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
391 elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*\(/) {
397 my $camelcase_seeded = 0;
398 sub seed_camelcase_includes
{
399 return if ($camelcase_seeded);
402 my $camelcase_git_file = "";
405 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
406 chomp $git_last_include_commit;
407 $camelcase_git_file = ".checkpatch-camelcase.$git_last_include_commit";
408 if (-f
$camelcase_git_file) {
409 open(my $camelcase_file, '<', "$camelcase_git_file")
410 or warn "$P: Can't read '$camelcase_git_file' $!\n";
411 while (<$camelcase_file>) {
415 close($camelcase_file);
419 $files = `git ls-files include`;
421 $files = `find $root/include -name "*.h"`;
423 my @include_files = split('\n', $files);
424 foreach my $file (@include_files) {
425 seed_camelcase_file
($file);
427 $camelcase_seeded = 1;
429 if ($camelcase_git_file ne "") {
430 unlink glob ".checkpatch-camelcase.*";
431 open(my $camelcase_file, '>', "$camelcase_git_file")
432 or warn "$P: Can't write '$camelcase_git_file' $!\n";
433 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
434 print $camelcase_file ("$_\n");
436 close($camelcase_file);
440 $chk_signoff = 0 if ($file);
446 for my $filename (@ARGV) {
449 open($FILE, '-|', "diff -u /dev/null $filename") ||
450 die "$P: $filename: diff failed - $!\n";
451 } elsif ($filename eq '-') {
452 open($FILE, '<&STDIN');
454 open($FILE, '<', "$filename") ||
455 die "$P: $filename: open failed - $!\n";
457 if ($filename eq '-') {
458 $vname = 'Your patch';
467 if (!process
($filename)) {
477 sub top_of_kernel_tree
{
481 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
482 "README", "Documentation", "arch", "include", "drivers",
483 "fs", "init", "ipc", "kernel", "lib", "scripts",
486 foreach my $check (@tree_check) {
487 if (! -e
$root . '/' . $check) {
495 my ($formatted_email) = @_;
501 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
504 $comment = $3 if defined $3;
505 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
507 $comment = $2 if defined $2;
508 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
510 $comment = $2 if defined $2;
511 $formatted_email =~ s/$address.*$//;
512 $name = $formatted_email;
514 $name =~ s/^\"|\"$//g;
515 # If there's a name left after stripping spaces and
516 # leading quotes, and the address doesn't have both
517 # leading and trailing angle brackets, the address
519 # "joe smith joe@smith.com" bad
520 # "joe smith <joe@smith.com" bad
521 if ($name ne "" && $address !~ /^<[^>]+>$/) {
529 $name =~ s/^\"|\"$//g;
530 $address = trim
($address);
531 $address =~ s/^\<|\>$//g;
533 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
534 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
538 return ($name, $address, $comment);
542 my ($name, $address) = @_;
547 $name =~ s/^\"|\"$//g;
548 $address = trim
($address);
550 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
551 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
556 $formatted_email = "$address";
558 $formatted_email = "$name <$address>";
561 return $formatted_email;
567 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
568 if (-e
"$path/$conf") {
569 return "$path/$conf";
581 for my $c (split(//, $str)) {
585 for (; ($n % 8) != 0; $n++) {
597 (my $res = shift) =~ tr/\t/ /c;
604 # Drop the diff line leader and expand tabs
606 $line = expand_tabs
($line);
608 # Pick the indent from the front of the line.
609 my ($white) = ($line =~ /^(\s*)/);
611 return (length($line), length($white));
614 my $sanitise_quote = '';
616 sub sanitise_line_reset
{
617 my ($in_comment) = @_;
620 $sanitise_quote = '*/';
622 $sanitise_quote = '';
635 # Always copy over the diff marker.
636 $res = substr($line, 0, 1);
638 for ($off = 1; $off < length($line); $off++) {
639 $c = substr($line, $off, 1);
641 # Comments we are wacking completly including the begin
642 # and end, all to $;.
643 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
644 $sanitise_quote = '*/';
646 substr($res, $off, 2, "$;$;");
650 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
651 $sanitise_quote = '';
652 substr($res, $off, 2, "$;$;");
656 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
657 $sanitise_quote = '//';
659 substr($res, $off, 2, $sanitise_quote);
664 # A \ in a string means ignore the next character.
665 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
667 substr($res, $off, 2, 'XX');
672 if ($c eq "'" || $c eq '"') {
673 if ($sanitise_quote eq '') {
674 $sanitise_quote = $c;
676 substr($res, $off, 1, $c);
678 } elsif ($sanitise_quote eq $c) {
679 $sanitise_quote = '';
683 #print "c<$c> SQ<$sanitise_quote>\n";
684 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
685 substr($res, $off, 1, $;);
686 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
687 substr($res, $off, 1, $;);
688 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
689 substr($res, $off, 1, 'X');
691 substr($res, $off, 1, $c);
695 if ($sanitise_quote eq '//') {
696 $sanitise_quote = '';
699 # The pathname on a #include may be surrounded by '<' and '>'.
700 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
701 my $clean = 'X' x
length($1);
702 $res =~ s@\
<.*\
>@
<$clean>@
;
704 # The whole of a #error is a string.
705 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
706 my $clean = 'X' x
length($1);
707 $res =~ s@
(\#\s
*(?
:error
|warning
)\s
+).*@
$1$clean@
;
713 sub get_quoted_string
{
714 my ($line, $rawline) = @_;
716 return "" if ($line !~ m/(\"[X]+\")/g);
717 return substr($rawline, $-[0], $+[0] - $-[0]);
720 sub ctx_statement_block
{
721 my ($linenr, $remain, $off) = @_;
722 my $line = $linenr - 1;
739 @stack = (['', 0]) if ($#stack == -1);
741 #warn "CSB: blk<$blk> remain<$remain>\n";
742 # If we are about to drop off the end, pull in more
745 for (; $remain > 0; $line++) {
746 last if (!defined $lines[$line]);
747 next if ($lines[$line] =~ /^-/);
750 $blk .= $lines[$line] . "\n";
755 # Bail if there is no further context.
756 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
760 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
766 $c = substr($blk, $off, 1);
767 $remainder = substr($blk, $off);
769 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
771 # Handle nested #if/#else.
772 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
773 push(@stack, [ $type, $level ]);
774 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
775 ($type, $level) = @
{$stack[$#stack - 1]};
776 } elsif ($remainder =~ /^#\s*endif\b/) {
777 ($type, $level) = @
{pop(@stack)};
780 # Statement ends at the ';' or a close '}' at the
782 if ($level == 0 && $c eq ';') {
786 # An else is really a conditional as long as its not else if
787 if ($level == 0 && $coff_set == 0 &&
788 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
789 $remainder =~ /^(else)(?:\s|{)/ &&
790 $remainder !~ /^else\s+if\b/) {
791 $coff = $off + length($1) - 1;
793 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
794 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
797 if (($type eq '' || $type eq '(') && $c eq '(') {
801 if ($type eq '(' && $c eq ')') {
803 $type = ($level != 0)?
'(' : '';
805 if ($level == 0 && $coff < $soff) {
808 #warn "CSB: mark coff<$coff>\n";
811 if (($type eq '' || $type eq '{') && $c eq '{') {
815 if ($type eq '{' && $c eq '}') {
817 $type = ($level != 0)?
'{' : '';
820 if (substr($blk, $off + 1, 1) eq ';') {
826 # Preprocessor commands end at the newline unless escaped.
827 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
835 # We are truly at the end, so shuffle to the next line.
842 my $statement = substr($blk, $soff, $off - $soff + 1);
843 my $condition = substr($blk, $soff, $coff - $soff + 1);
845 #warn "STATEMENT<$statement>\n";
846 #warn "CONDITION<$condition>\n";
848 #print "coff<$coff> soff<$off> loff<$loff>\n";
850 return ($statement, $condition,
851 $line, $remain + 1, $off - $loff + 1, $level);
854 sub statement_lines
{
857 # Strip the diff line prefixes and rip blank lines at start and end.
858 $stmt =~ s/(^|\n)./$1/g;
862 my @stmt_lines = ($stmt =~ /\n/g);
864 return $#stmt_lines + 2;
867 sub statement_rawlines
{
870 my @stmt_lines = ($stmt =~ /\n/g);
872 return $#stmt_lines + 2;
875 sub statement_block_size
{
878 $stmt =~ s/(^|\n)./$1/g;
884 my @stmt_lines = ($stmt =~ /\n/g);
885 my @stmt_statements = ($stmt =~ /;/g);
887 my $stmt_lines = $#stmt_lines + 2;
888 my $stmt_statements = $#stmt_statements + 1;
890 if ($stmt_lines > $stmt_statements) {
893 return $stmt_statements;
897 sub ctx_statement_full
{
898 my ($linenr, $remain, $off) = @_;
899 my ($statement, $condition, $level);
903 # Grab the first conditional/block pair.
904 ($statement, $condition, $linenr, $remain, $off, $level) =
905 ctx_statement_block
($linenr, $remain, $off);
906 #print "F: c<$condition> s<$statement> remain<$remain>\n";
907 push(@chunks, [ $condition, $statement ]);
908 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
909 return ($level, $linenr, @chunks);
912 # Pull in the following conditional/block pairs and see if they
913 # could continue the statement.
915 ($statement, $condition, $linenr, $remain, $off, $level) =
916 ctx_statement_block
($linenr, $remain, $off);
917 #print "C: c<$condition> s<$statement> remain<$remain>\n";
918 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
920 push(@chunks, [ $condition, $statement ]);
923 return ($level, $linenr, @chunks);
927 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
929 my $start = $linenr - 1;
936 my @stack = ($level);
937 for ($line = $start; $remain > 0; $line++) {
938 next if ($rawlines[$line] =~ /^-/);
941 $blk .= $rawlines[$line];
943 # Handle nested #if/#else.
944 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
945 push(@stack, $level);
946 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
947 $level = $stack[$#stack - 1];
948 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
949 $level = pop(@stack);
952 foreach my $c (split(//, $lines[$line])) {
953 ##print "C<$c>L<$level><$open$close>O<$off>\n";
959 if ($c eq $close && $level > 0) {
961 last if ($level == 0);
962 } elsif ($c eq $open) {
967 if (!$outer || $level <= 1) {
968 push(@res, $rawlines[$line]);
971 last if ($level == 0);
974 return ($level, @res);
976 sub ctx_block_outer
{
977 my ($linenr, $remain) = @_;
979 my ($level, @r) = ctx_block_get
($linenr, $remain, 1, '{', '}', 0);
983 my ($linenr, $remain) = @_;
985 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
989 my ($linenr, $remain, $off) = @_;
991 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
994 sub ctx_block_level
{
995 my ($linenr, $remain) = @_;
997 return ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
999 sub ctx_statement_level
{
1000 my ($linenr, $remain, $off) = @_;
1002 return ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
1005 sub ctx_locate_comment
{
1006 my ($first_line, $end_line) = @_;
1008 # Catch a comment on the end of the line itself.
1009 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@
.*(/\*.*\*/)\s
*(?
:\\\s
*)?
$@
);
1010 return $current_comment if (defined $current_comment);
1012 # Look through the context and try and figure out if there is a
1015 $current_comment = '';
1016 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1017 my $line = $rawlines[$linenr - 1];
1019 if ($linenr == $first_line and $line =~ m@
^.\s
*\
*@
) {
1022 if ($line =~ m@
/\
*@
) {
1025 if (!$in_comment && $current_comment ne '') {
1026 $current_comment = '';
1028 $current_comment .= $line . "\n" if ($in_comment);
1029 if ($line =~ m@\
*/@
) {
1034 chomp($current_comment);
1035 return($current_comment);
1037 sub ctx_has_comment
{
1038 my ($first_line, $end_line) = @_;
1039 my $cmt = ctx_locate_comment
($first_line, $end_line);
1041 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1042 ##print "CMMT: $cmt\n";
1044 return ($cmt ne '');
1048 my ($linenr, $cnt) = @_;
1050 my $offset = $linenr - 1;
1055 $line = $rawlines[$offset++];
1056 next if (defined($line) && $line =~ /^-/);
1068 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1071 $coded = sprintf("^%c", unpack('C', $2) + 64);
1080 my $av_preprocessor = 0;
1085 sub annotate_reset
{
1086 $av_preprocessor = 0;
1088 @av_paren_type = ('E');
1089 $av_pend_colon = 'O';
1092 sub annotate_values
{
1093 my ($stream, $type) = @_;
1096 my $var = '_' x
length($stream);
1099 print "$stream\n" if ($dbg_values > 1);
1101 while (length($cur)) {
1102 @av_paren_type = ('E') if ($#av_paren_type < 0);
1103 print " <" . join('', @av_paren_type) .
1104 "> <$type> <$av_pending>" if ($dbg_values > 1);
1105 if ($cur =~ /^(\s+)/o) {
1106 print "WS($1)\n" if ($dbg_values > 1);
1107 if ($1 =~ /\n/ && $av_preprocessor) {
1108 $type = pop(@av_paren_type);
1109 $av_preprocessor = 0;
1112 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1113 print "CAST($1)\n" if ($dbg_values > 1);
1114 push(@av_paren_type, $type);
1117 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1118 print "DECLARE($1)\n" if ($dbg_values > 1);
1121 } elsif ($cur =~ /^($Modifier)\s*/) {
1122 print "MODIFIER($1)\n" if ($dbg_values > 1);
1125 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1126 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1127 $av_preprocessor = 1;
1128 push(@av_paren_type, $type);
1134 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1135 print "UNDEF($1)\n" if ($dbg_values > 1);
1136 $av_preprocessor = 1;
1137 push(@av_paren_type, $type);
1139 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1140 print "PRE_START($1)\n" if ($dbg_values > 1);
1141 $av_preprocessor = 1;
1143 push(@av_paren_type, $type);
1144 push(@av_paren_type, $type);
1147 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1148 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1149 $av_preprocessor = 1;
1151 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1155 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1156 print "PRE_END($1)\n" if ($dbg_values > 1);
1158 $av_preprocessor = 1;
1160 # Assume all arms of the conditional end as this
1161 # one does, and continue as if the #endif was not here.
1162 pop(@av_paren_type);
1163 push(@av_paren_type, $type);
1166 } elsif ($cur =~ /^(\\\n)/o) {
1167 print "PRECONT($1)\n" if ($dbg_values > 1);
1169 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1170 print "ATTR($1)\n" if ($dbg_values > 1);
1171 $av_pending = $type;
1174 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1175 print "SIZEOF($1)\n" if ($dbg_values > 1);
1181 } elsif ($cur =~ /^(if|while|for)\b/o) {
1182 print "COND($1)\n" if ($dbg_values > 1);
1186 } elsif ($cur =~/^(case)/o) {
1187 print "CASE($1)\n" if ($dbg_values > 1);
1188 $av_pend_colon = 'C';
1191 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1192 print "KEYWORD($1)\n" if ($dbg_values > 1);
1195 } elsif ($cur =~ /^(\()/o) {
1196 print "PAREN('$1')\n" if ($dbg_values > 1);
1197 push(@av_paren_type, $av_pending);
1201 } elsif ($cur =~ /^(\))/o) {
1202 my $new_type = pop(@av_paren_type);
1203 if ($new_type ne '_') {
1205 print "PAREN('$1') -> $type\n"
1206 if ($dbg_values > 1);
1208 print "PAREN('$1')\n" if ($dbg_values > 1);
1211 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1212 print "FUNC($1)\n" if ($dbg_values > 1);
1216 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1217 if (defined $2 && $type eq 'C' || $type eq 'T') {
1218 $av_pend_colon = 'B';
1219 } elsif ($type eq 'E') {
1220 $av_pend_colon = 'L';
1222 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1225 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1226 print "IDENT($1)\n" if ($dbg_values > 1);
1229 } elsif ($cur =~ /^($Assignment)/o) {
1230 print "ASSIGN($1)\n" if ($dbg_values > 1);
1233 } elsif ($cur =~/^(;|{|})/) {
1234 print "END($1)\n" if ($dbg_values > 1);
1236 $av_pend_colon = 'O';
1238 } elsif ($cur =~/^(,)/) {
1239 print "COMMA($1)\n" if ($dbg_values > 1);
1242 } elsif ($cur =~ /^(\?)/o) {
1243 print "QUESTION($1)\n" if ($dbg_values > 1);
1246 } elsif ($cur =~ /^(:)/o) {
1247 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1249 substr($var, length($res), 1, $av_pend_colon);
1250 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1255 $av_pend_colon = 'O';
1257 } elsif ($cur =~ /^(\[)/o) {
1258 print "CLOSE($1)\n" if ($dbg_values > 1);
1261 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1264 print "OPV($1)\n" if ($dbg_values > 1);
1271 substr($var, length($res), 1, $variant);
1274 } elsif ($cur =~ /^($Operators)/o) {
1275 print "OP($1)\n" if ($dbg_values > 1);
1276 if ($1 ne '++' && $1 ne '--') {
1280 } elsif ($cur =~ /(^.)/o) {
1281 print "C($1)\n" if ($dbg_values > 1);
1284 $cur = substr($cur, length($1));
1285 $res .= $type x
length($1);
1289 return ($res, $var);
1293 my ($possible, $line) = @_;
1294 my $notPermitted = qr{(?
:
1311 ^(?
:typedef
|struct
|enum
)\b
1313 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1314 if ($possible !~ $notPermitted) {
1315 # Check for modifiers.
1316 $possible =~ s/\s*$Storage\s*//g;
1317 $possible =~ s/\s*$Sparse\s*//g;
1318 if ($possible =~ /^\s*$/) {
1320 } elsif ($possible =~ /\s/) {
1321 $possible =~ s/\s*$Type\s*//g;
1322 for my $modifier (split(' ', $possible)) {
1323 if ($modifier !~ $notPermitted) {
1324 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1325 push(@modifierList, $modifier);
1330 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1331 push(@typeList, $possible);
1335 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1342 return !defined $ignore_type{$_[0]};
1346 if (!show_type
($_[1]) ||
1347 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1352 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1354 $line = "$prefix$_[0]: $_[2]\n";
1356 $line = (split('\n', $line))[0] . "\n" if ($terse);
1358 push(our @report, $line);
1367 if (report
("ERROR", $_[0], $_[1])) {
1375 if (report
("WARNING", $_[0], $_[1])) {
1383 if ($check && report
("CHECK", $_[0], $_[1])) {
1391 sub check_absolute_file
{
1392 my ($absolute, $herecurr) = @_;
1393 my $file = $absolute;
1395 ##print "absolute<$absolute>\n";
1397 # See if any suffix of this path is a path within the tree.
1398 while ($file =~ s@
^[^/]*/@@
) {
1399 if (-f
"$root/$file") {
1400 ##print "file<$file>\n";
1408 # It is, so see if the prefix is acceptable.
1409 my $prefix = $absolute;
1410 substr($prefix, -length($file)) = '';
1412 ##print "prefix<$prefix>\n";
1413 if ($prefix ne ".../") {
1414 WARN
("USE_RELATIVE_PATH",
1415 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1422 $string =~ s/(^\s+|\s+$)//g;
1430 my $source_indent = 8;
1431 my $max_spaces_before_tab = $source_indent - 1;
1432 my $spaces_to_tab = " " x
$source_indent;
1434 #convert leading spaces to tabs
1435 1 while $leading =~ s@
^([\t]*)$spaces_to_tab@
$1\t@g;
1436 #Remove spaces before a tab
1437 1 while $leading =~ s@
^([\t]*)( {1,$max_spaces_before_tab})\t@
$1\t@g;
1442 sub pos_last_openparen
{
1447 my $opens = $line =~ tr/\(/\(/;
1448 my $closes = $line =~ tr/\)/\)/;
1450 my $last_openparen = 0;
1452 if (($opens == 0) || ($closes >= $opens)) {
1456 my $len = length($line);
1458 for ($pos = 0; $pos < $len; $pos++) {
1459 my $string = substr($line, $pos);
1460 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1461 $pos += length($1) - 1;
1462 } elsif (substr($line, $pos, 1) eq '(') {
1463 $last_openparen = $pos;
1464 } elsif (index($string, '(') == -1) {
1469 return $last_openparen + 1;
1473 my $filename = shift;
1479 my $stashrawline="";
1490 my $in_header_lines = 1;
1491 my $in_commit_log = 0; #Scanning lines before patch
1493 my $non_utf8_charset = 0;
1501 # Trace the real file/line as we go.
1507 my $comment_edge = 0;
1511 my $prev_values = 'E';
1514 my %suppress_ifbraces;
1515 my %suppress_whiletrailers;
1516 my %suppress_export;
1517 my $suppress_statement = 0;
1520 # Pre-scan the patch sanitizing the lines.
1521 # Pre-scan the patch looking for any __setup documentation.
1523 my @setup_docs = ();
1526 sanitise_line_reset
();
1528 foreach my $rawline (@rawlines) {
1532 push(@fixed, $rawline) if ($fix);
1534 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1536 if ($1 =~ m
@Documentation/kernel
-parameters
.txt
$@
) {
1541 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1550 # Guestimate if this is a continuing comment. Run
1551 # the context looking for a comment "edge". If this
1552 # edge is a close comment then we must be in a comment
1556 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1557 next if (defined $rawlines[$ln - 1] &&
1558 $rawlines[$ln - 1] =~ /^-/);
1560 #print "RAW<$rawlines[$ln - 1]>\n";
1561 last if (!defined $rawlines[$ln - 1]);
1562 if ($rawlines[$ln - 1] =~ m@
(/\*|\*/)@
&&
1563 $rawlines[$ln - 1] !~ m@
"[^"]*(?
:/\*|\*/)[^"]*"@
) {
1568 if (defined $edge && $edge eq '*/') {
1572 # Guestimate if this is a continuing comment. If this
1573 # is the start of a diff block and this line starts
1574 # ' *' then it is very likely a comment.
1575 if (!defined $edge &&
1576 $rawlines[$linenr] =~ m@
^.\s
*(?
:\
*\
*+| \
*)(?
:\s
|$)@
)
1581 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1582 sanitise_line_reset
($in_comment);
1584 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1585 # Standardise the strings and chars within the input to
1586 # simplify matching -- only bother with positive lines.
1587 $line = sanitise_line
($rawline);
1589 push(@lines, $line);
1592 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1597 #print "==>$rawline\n";
1598 #print "-->$line\n";
1600 if ($setup_docs && $line =~ /^\+/) {
1601 push(@setup_docs, $line);
1609 foreach my $line (@lines) {
1612 my $rawline = $rawlines[$linenr - 1];
1614 #extract the line range in the file after the patch is applied
1615 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1617 $first_line = $linenr + 1;
1627 %suppress_ifbraces = ();
1628 %suppress_whiletrailers = ();
1629 %suppress_export = ();
1630 $suppress_statement = 0;
1633 # track the line number as we move through the hunk, note that
1634 # new versions of GNU diff omit the leading space on completely
1635 # blank context lines so we need to count that too.
1636 } elsif ($line =~ /^( |\+|$)/) {
1638 $realcnt-- if ($realcnt != 0);
1640 # Measure the line length and indent.
1641 ($length, $indent) = line_stats
($rawline);
1643 # Track the previous line.
1644 ($prevline, $stashline) = ($stashline, $line);
1645 ($previndent, $stashindent) = ($stashindent, $indent);
1646 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1648 #warn "line<$line>\n";
1650 } elsif ($realcnt == 1) {
1654 my $hunk_line = ($realcnt != 0);
1656 #make up the handle for any error we report on this line
1657 $prefix = "$filename:$realline: " if ($emacs && $file);
1658 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1660 $here = "#$linenr: " if (!$file);
1661 $here = "#$realline: " if ($file);
1663 # extract the filename as it passes
1664 if ($line =~ /^diff --git.*?(\S+)$/) {
1666 $realfile =~ s@
^([^/]*)/@@
;
1668 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1670 $realfile =~ s@
^([^/]*)/@@
;
1674 if (!$file && $tree && $p1_prefix ne '' &&
1675 -e
"$root/$p1_prefix") {
1676 WARN
("PATCH_PREFIX",
1677 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1680 if ($realfile =~ m@
^include
/asm/@
) {
1681 ERROR
("MODIFIED_INCLUDE_ASM",
1682 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1687 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1689 my $hereline = "$here\n$rawline\n";
1690 my $herecurr = "$here\n$rawline\n";
1691 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1693 $cnt_lines++ if ($realcnt != 0);
1695 # Check for incorrect file permissions
1696 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1697 my $permhere = $here . "FILE: $realfile\n";
1698 if ($realfile !~ m
@scripts/@
&&
1699 $realfile !~ /\.(py|pl|awk|sh)$/) {
1700 ERROR
("EXECUTE_PERMISSIONS",
1701 "do not set execute permissions for source files\n" . $permhere);
1705 # Check the patch for a signoff:
1706 if ($line =~ /^\s*signed-off-by:/i) {
1711 # Check signature styles
1712 if (!$in_header_lines &&
1713 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
1714 my $space_before = $1;
1716 my $space_after = $3;
1718 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1720 if ($sign_off !~ /$signature_tags/) {
1721 WARN
("BAD_SIGN_OFF",
1722 "Non-standard signature: $sign_off\n" . $herecurr);
1724 if (defined $space_before && $space_before ne "") {
1725 if (WARN
("BAD_SIGN_OFF",
1726 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1728 $fixed[$linenr - 1] =
1729 "$ucfirst_sign_off $email";
1732 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1733 if (WARN
("BAD_SIGN_OFF",
1734 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1736 $fixed[$linenr - 1] =
1737 "$ucfirst_sign_off $email";
1741 if (!defined $space_after || $space_after ne " ") {
1742 if (WARN
("BAD_SIGN_OFF",
1743 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1745 $fixed[$linenr - 1] =
1746 "$ucfirst_sign_off $email";
1750 my ($email_name, $email_address, $comment) = parse_email
($email);
1751 my $suggested_email = format_email
(($email_name, $email_address));
1752 if ($suggested_email eq "") {
1753 ERROR
("BAD_SIGN_OFF",
1754 "Unrecognized email address: '$email'\n" . $herecurr);
1756 my $dequoted = $suggested_email;
1757 $dequoted =~ s/^"//;
1758 $dequoted =~ s/" </ </;
1759 # Don't force email to have quotes
1760 # Allow just an angle bracketed address
1761 if ("$dequoted$comment" ne $email &&
1762 "<$email_address>$comment" ne $email &&
1763 "$suggested_email$comment" ne $email) {
1764 WARN
("BAD_SIGN_OFF",
1765 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
1770 # Check for wrappage within a valid hunk of the file
1771 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1772 ERROR
("CORRUPTED_PATCH",
1773 "patch seems to be corrupt (line wrapped?)\n" .
1774 $herecurr) if (!$emitted_corrupt++);
1777 # Check for absolute kernel paths.
1779 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1782 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1783 check_absolute_file
($1, $herecurr)) {
1786 check_absolute_file
($file, $herecurr);
1791 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1792 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1793 $rawline !~ m/^$UTF8*$/) {
1794 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1796 my $blank = copy_spacing
($rawline);
1797 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1798 my $hereptr = "$hereline$ptr\n";
1801 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1804 # Check if it's the start of a commit log
1805 # (not a header line and we haven't seen the patch filename)
1806 if ($in_header_lines && $realfile =~ /^$/ &&
1807 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
1808 $in_header_lines = 0;
1812 # Check if there is UTF-8 in a commit log when a mail header has explicitly
1813 # declined it, i.e defined some charset where it is missing.
1814 if ($in_header_lines &&
1815 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1817 $non_utf8_charset = 1;
1820 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
1821 $rawline =~ /$NON_ASCII_UTF8/) {
1822 WARN
("UTF8_BEFORE_PATCH",
1823 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1826 # ignore non-hunk lines and lines being removed
1827 next if (!$hunk_line || $line =~ /^-/);
1829 #trailing whitespace
1830 if ($line =~ /^\+.*\015/) {
1831 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1832 ERROR
("DOS_LINE_ENDINGS",
1833 "DOS line endings\n" . $herevet);
1835 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1836 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1837 if (ERROR
("TRAILING_WHITESPACE",
1838 "trailing whitespace\n" . $herevet) &&
1840 $fixed[$linenr - 1] =~ s/^(\+.*?)\s+$/$1/;
1846 # check for Kconfig help text having a real description
1847 # Only applies when adding the entry originally, after that we do not have
1848 # sufficient context to determine whether it is indeed long enough.
1849 if ($realfile =~ /Kconfig/ &&
1850 $line =~ /.\s*config\s+/) {
1853 my $ln = $linenr + 1;
1857 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
1858 $f = $lines[$ln - 1];
1859 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1860 $is_end = $lines[$ln - 1] =~ /^\+/;
1862 next if ($f =~ /^-/);
1864 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1866 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1873 next if ($f =~ /^$/);
1874 if ($f =~ /^\s*config\s/) {
1880 WARN
("CONFIG_DESCRIPTION",
1881 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1882 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
1885 # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
1886 if ($realfile =~ /Kconfig/ &&
1887 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
1888 WARN
("CONFIG_EXPERIMENTAL",
1889 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1892 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1893 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1896 'EXTRA_AFLAGS' => 'asflags-y',
1897 'EXTRA_CFLAGS' => 'ccflags-y',
1898 'EXTRA_CPPFLAGS' => 'cppflags-y',
1899 'EXTRA_LDFLAGS' => 'ldflags-y',
1902 WARN
("DEPRECATED_VARIABLE",
1903 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1906 # check we are in a valid source file if not then ignore this hunk
1907 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1910 if ($line =~ /^\+/ && $prevrawline !~ /\/\
*\
*/ &&
1911 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1912 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
1913 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1914 $length > $max_line_length)
1917 "line over $max_line_length characters\n" . $herecurr);
1920 # Check for user-visible strings broken across lines, which breaks the ability
1921 # to grep for the string. Limited to strings used as parameters (those
1922 # following an open parenthesis), which almost completely eliminates false
1923 # positives, as well as warning only once per parameter rather than once per
1924 # line of the string. Make an exception when the previous string ends in a
1925 # newline (multiple lines in one string constant) or \n\t (common in inline
1926 # assembly to indent the instruction on the following line).
1927 if ($line =~ /^\+\s*"/ &&
1928 $prevline =~ /"\s*$/ &&
1929 $prevline =~ /\(/ &&
1930 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1931 WARN
("SPLIT_STRING",
1932 "quoted string split across lines\n" . $hereprev);
1935 # check for spaces before a quoted newline
1936 if ($rawline =~ /^.*\".*\s\\n/) {
1937 if (WARN
("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1938 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
1940 $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
1945 # check for adding lines without a newline.
1946 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1947 WARN
("MISSING_EOF_NEWLINE",
1948 "adding a line without newline at end of file\n" . $herecurr);
1951 # Blackfin: use hi/lo macros
1952 if ($realfile =~ m
@arch/blackfin/.*\
.S
$@
) {
1953 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1954 my $herevet = "$here\n" . cat_vet
($line) . "\n";
1956 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1958 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1959 my $herevet = "$here\n" . cat_vet
($line) . "\n";
1961 "use the HI() macro, not (... >> 16)\n" . $herevet);
1965 # check we are in a valid source file C or perl if not then ignore this hunk
1966 next if ($realfile !~ /\.(h|c|pl)$/);
1968 # at the beginning of a line any tabs must come first and anything
1969 # more than 8 must use tabs.
1970 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1971 $rawline =~ /^\+\s* \s*/) {
1972 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1974 if (ERROR
("CODE_INDENT",
1975 "code indent should use tabs where possible\n" . $herevet) &&
1977 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
1981 # check for space before tabs.
1982 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1983 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1984 if (WARN
("SPACE_BEFORE_TAB",
1985 "please, no space before tabs\n" . $herevet) &&
1987 $fixed[$linenr - 1] =~
1988 s/(^\+.*) +\t/$1\t/;
1992 # check for && or || at the start of a line
1993 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
1994 CHK
("LOGICAL_CONTINUATIONS",
1995 "Logical continuations should be on the previous line\n" . $hereprev);
1998 # check multi-line statement indentation matches previous line
1999 if ($^V
&& $^V
ge 5.10.0 &&
2000 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2001 $prevline =~ /^\+(\t*)(.*)$/;
2005 my $pos = pos_last_openparen
($rest);
2007 $line =~ /^(\+| )([ \t]*)/;
2010 my $goodtabindent = $oldindent .
2013 my $goodspaceindent = $oldindent . " " x
$pos;
2015 if ($newindent ne $goodtabindent &&
2016 $newindent ne $goodspaceindent) {
2018 if (CHK
("PARENTHESIS_ALIGNMENT",
2019 "Alignment should match open parenthesis\n" . $hereprev) &&
2020 $fix && $line =~ /^\+/) {
2021 $fixed[$linenr - 1] =~
2022 s/^\+[ \t]*/\+$goodtabindent/;
2028 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
2030 "No space is necessary after a cast\n" . $hereprev) &&
2032 $fixed[$linenr - 1] =~
2033 s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
2037 if ($realfile =~ m@
^(drivers
/net
/|net
/)@
&&
2038 $prevrawline =~ /^\+[ \t]*\/\
*[ \t]*$/ &&
2039 $rawline =~ /^\+[ \t]*\*/) {
2040 WARN
("NETWORKING_BLOCK_COMMENT_STYLE",
2041 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2044 if ($realfile =~ m@
^(drivers
/net
/|net
/)@
&&
2045 $prevrawline =~ /^\+[ \t]*\/\
*/ && #starting /*
2046 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
2047 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2048 WARN
("NETWORKING_BLOCK_COMMENT_STYLE",
2049 "networking block comments start with * on subsequent lines\n" . $hereprev);
2052 if ($realfile =~ m@
^(drivers
/net
/|net
/)@
&&
2053 $rawline !~ m@
^\
+[ \t]*\
*/[ \t]*$@ && #trailing */
2054 $rawline !~ m@
^\
+.*/\*.*\*/[ \t]*$@
&& #inline /*...*/
2055 $rawline !~ m@
^\
+.*\
*{2,}/[ \t]*$@ && #trailing **/
2056 $rawline =~ m@
^\
+[ \t]*.+\
*\
/[ \t]*$@) { #non blank */
2057 WARN
("NETWORKING_BLOCK_COMMENT_STYLE",
2058 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2061 # check for spaces at the beginning of a line.
2063 # 1) within comments
2064 # 2) indented preprocessor commands
2066 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
2067 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2068 if (WARN
("LEADING_SPACE",
2069 "please, no spaces at the start of a line\n" . $herevet) &&
2071 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2075 # check we are in a valid C source file if not then ignore this hunk
2076 next if ($realfile !~ /\.(h|c)$/);
2078 # discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2079 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2080 WARN
("CONFIG_EXPERIMENTAL",
2081 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2084 # check for RCS/CVS revision markers
2085 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2087 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2090 # Blackfin: don't use __builtin_bfin_[cs]sync
2091 if ($line =~ /__builtin_bfin_csync/) {
2092 my $herevet = "$here\n" . cat_vet
($line) . "\n";
2094 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
2096 if ($line =~ /__builtin_bfin_ssync/) {
2097 my $herevet = "$here\n" . cat_vet
($line) . "\n";
2099 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
2102 # check for old HOTPLUG __dev<foo> section markings
2103 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2104 WARN
("HOTPLUG_SECTION",
2105 "Using $1 is unnecessary\n" . $herecurr);
2108 # Check for potential 'bare' types
2109 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2111 #print "LINE<$line>\n";
2112 if ($linenr >= $suppress_statement &&
2113 $realcnt && $line =~ /.\s*\S/) {
2114 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2115 ctx_statement_block
($linenr, $realcnt, 0);
2116 $stat =~ s/\n./\n /g;
2117 $cond =~ s/\n./\n /g;
2119 #print "linenr<$linenr> <$stat>\n";
2120 # If this statement has no statement boundaries within
2121 # it there is no point in retrying a statement scan
2122 # until we hit end of it.
2123 my $frag = $stat; $frag =~ s/;+\s*$//;
2124 if ($frag !~ /(?:{|;)/) {
2125 #print "skip<$line_nr_next>\n";
2126 $suppress_statement = $line_nr_next;
2129 # Find the real next line.
2130 $realline_next = $line_nr_next;
2131 if (defined $realline_next &&
2132 (!defined $lines[$realline_next - 1] ||
2133 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2140 # Ignore goto labels.
2141 if ($s =~ /$Ident:\*$/s) {
2143 # Ignore functions being called
2144 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2146 } elsif ($s =~ /^.\s*else\b/s) {
2148 # declarations always start with types
2149 } 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) {
2152 possible
($type, "A:" . $s);
2154 # definitions in global scope can only start with types
2155 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2156 possible
($1, "B:" . $s);
2159 # any (foo ... *) is a pointer cast, and foo is a type
2160 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2161 possible
($1, "C:" . $s);
2164 # Check for any sort of function declaration.
2165 # int foo(something bar, other baz);
2166 # void (*store_gdt)(x86_descr_ptr *);
2167 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
2168 my ($name_len) = length($1);
2171 substr($ctx, 0, $name_len + 1, '');
2172 $ctx =~ s/\)[^\)]*$//;
2174 for my $arg (split(/\s*,\s*/, $ctx)) {
2175 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
2177 possible
($1, "D:" . $s);
2185 # Checks which may be anchored in the context.
2188 # Check for switch () and associated case and default
2189 # statements should be at the same indent.
2190 if ($line=~/\bswitch\s*\(.*\)/) {
2193 my @ctx = ctx_block_outer
($linenr, $realcnt);
2195 for my $ctx (@ctx) {
2196 my ($clen, $cindent) = line_stats
($ctx);
2197 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2198 $indent != $cindent) {
2199 $err .= "$sep$ctx\n";
2206 ERROR
("SWITCH_CASE_INDENT_LEVEL",
2207 "switch and case should be at the same indent\n$hereline$err");
2211 # if/while/etc brace do not go on next line, unless defining a do while loop,
2212 # or if that brace on the next line is for something else
2213 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2214 my $pre_ctx = "$1$2";
2216 my ($level, @ctx) = ctx_statement_level
($linenr, $realcnt, 0);
2218 if ($line =~ /^\+\t{6,}/) {
2219 WARN
("DEEP_INDENTATION",
2220 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2223 my $ctx_cnt = $realcnt - $#ctx - 1;
2224 my $ctx = join("\n", @ctx);
2226 my $ctx_ln = $linenr;
2227 my $ctx_skip = $realcnt;
2229 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2230 defined $lines[$ctx_ln - 1] &&
2231 $lines[$ctx_ln - 1] =~ /^-/)) {
2232 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2233 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2237 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2238 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2240 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2242 "that open brace { should be on the previous line\n" .
2243 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2245 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2246 $ctx =~ /\)\s*\;\s*$/ &&
2247 defined $lines[$ctx_ln - 1])
2249 my ($nlength, $nindent) = line_stats
($lines[$ctx_ln - 1]);
2250 if ($nindent > $indent) {
2251 WARN
("TRAILING_SEMICOLON",
2252 "trailing semicolon indicates no statements, indent implies otherwise\n" .
2253 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2258 # Check relative indent for conditionals and blocks.
2259 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
2260 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2261 ctx_statement_block
($linenr, $realcnt, 0)
2262 if (!defined $stat);
2263 my ($s, $c) = ($stat, $cond);
2265 substr($s, 0, length($c), '');
2267 # Make sure we remove the line prefixes as we have
2268 # none on the first line, and are going to readd them
2272 # Find out how long the conditional actually is.
2273 my @newlines = ($c =~ /\n/gs);
2274 my $cond_lines = 1 + $#newlines;
2276 # We want to check the first line inside the block
2277 # starting at the end of the conditional, so remove:
2278 # 1) any blank line termination
2279 # 2) any opening brace { on end of the line
2281 my $continuation = 0;
2283 $s =~ s/^.*\bdo\b//;
2285 if ($s =~ s/^\s*\\//) {
2288 if ($s =~ s/^\s*?\n//) {
2293 # Also ignore a loop construct at the end of a
2294 # preprocessor statement.
2295 if (($prevline =~ /^.\s*#\s*define\s/ ||
2296 $prevline =~ /\\\s*$/) && $continuation == 0) {
2302 while ($cond_ptr != $cond_lines) {
2303 $cond_ptr = $cond_lines;
2305 # If we see an #else/#elif then the code
2307 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2312 # 1) blank lines, they should be at 0,
2313 # 2) preprocessor lines, and
2315 if ($continuation ||
2317 $s =~ /^\s*#\s*?/ ||
2318 $s =~ /^\s*$Ident\s*:/) {
2319 $continuation = ($s =~ /^.*?\\\n/) ?
1 : 0;
2320 if ($s =~ s/^.*?\n//) {
2326 my (undef, $sindent) = line_stats
("+" . $s);
2327 my $stat_real = raw_line
($linenr, $cond_lines);
2329 # Check if either of these lines are modified, else
2330 # this is not this patch's fault.
2331 if (!defined($stat_real) ||
2332 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2335 if (defined($stat_real) && $cond_lines > 1) {
2336 $stat_real = "[...]\n$stat_real";
2339 #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";
2341 if ($check && (($sindent % 8) != 0 ||
2342 ($sindent <= $indent && $s ne ''))) {
2343 WARN
("SUSPECT_CODE_INDENT",
2344 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
2348 # Track the 'values' across context and added lines.
2349 my $opline = $line; $opline =~ s/^./ /;
2350 my ($curr_values, $curr_vars) =
2351 annotate_values
($opline . "\n", $prev_values);
2352 $curr_values = $prev_values . $curr_values;
2354 my $outline = $opline; $outline =~ s/\t/ /g;
2355 print "$linenr > .$outline\n";
2356 print "$linenr > $curr_values\n";
2357 print "$linenr > $curr_vars\n";
2359 $prev_values = substr($curr_values, -1);
2361 #ignore lines not being added
2362 next if ($line =~ /^[^\+]/);
2364 # TEST: allow direct testing of the type matcher.
2366 if ($line =~ /^.\s*$Declare\s*$/) {
2368 "TEST: is type\n" . $herecurr);
2369 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2370 ERROR
("TEST_NOT_TYPE",
2371 "TEST: is not type ($1 is)\n". $herecurr);
2375 # TEST: allow direct testing of the attribute matcher.
2377 if ($line =~ /^.\s*$Modifier\s*$/) {
2379 "TEST: is attr\n" . $herecurr);
2380 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2381 ERROR
("TEST_NOT_ATTR",
2382 "TEST: is not attr ($1 is)\n". $herecurr);
2387 # check for initialisation to aggregates open brace on the next line
2388 if ($line =~ /^.\s*{/ &&
2389 $prevline =~ /(?:^|[^=])=\s*$/) {
2391 "that open brace { should be on the previous line\n" . $hereprev);
2395 # Checks which are anchored on the added line.
2398 # check for malformed paths in #include statements (uses RAW line)
2399 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2401 if ($path =~ m{//}) {
2402 ERROR
("MALFORMED_INCLUDE",
2403 "malformed #include filename\n" . $herecurr);
2405 if ($path =~ "^uapi/" && $realfile =~ m@
\binclude
/uapi
/@
) {
2406 ERROR
("UAPI_INCLUDE",
2407 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2411 # no C99 // comments
2412 if ($line =~ m{//}) {
2413 if (ERROR
("C99_COMMENTS",
2414 "do not use C99 // comments\n" . $herecurr) &&
2416 my $line = $fixed[$linenr - 1];
2417 if ($line =~ /\/\
/(.*)$/) {
2418 my $comment = trim
($1);
2419 $fixed[$linenr - 1] =~ s@\
/\/(.*)$@
/\* $comment \*/@
;
2423 # Remove C99 comments.
2425 $opline =~ s@
//.*@@
;
2427 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2428 # the whole statement.
2429 #print "APW <$lines[$realline_next - 1]>\n";
2430 if (defined $realline_next &&
2431 exists $lines[$realline_next - 1] &&
2432 !defined $suppress_export{$realline_next} &&
2433 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2434 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2435 # Handle definitions which produce identifiers with
2438 # EXPORT_SYMBOL(something_foo);
2440 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
2441 $name =~ /^${Ident}_$2/) {
2442 #print "FOO C name<$name>\n";
2443 $suppress_export{$realline_next} = 1;
2445 } elsif ($stat !~ /(?
:
2447 ^.DEFINE_
$Ident\
(\Q
$name\E\
)|
2448 ^.DECLARE_
$Ident\
(\Q
$name\E\
)|
2449 ^.LIST_HEAD\
(\Q
$name\E\
)|
2450 ^.(?
:$Storage\s
+)?
$Type\s
*\
(\s
*\
*\s
*\Q
$name\E\s
*\
)\s
*\
(|
2451 \b\Q
$name\E
(?
:\s
+$Attribute)*\s
*(?
:;|=|\
[|\
()
2453 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2454 $suppress_export{$realline_next} = 2;
2456 $suppress_export{$realline_next} = 1;
2459 if (!defined $suppress_export{$linenr} &&
2460 $prevline =~ /^.\s*$/ &&
2461 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2462 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2463 #print "FOO B <$lines[$linenr - 1]>\n";
2464 $suppress_export{$linenr} = 2;
2466 if (defined $suppress_export{$linenr} &&
2467 $suppress_export{$linenr} == 2) {
2468 WARN
("EXPORT_SYMBOL",
2469 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
2472 # check for global initialisers.
2473 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
2474 ERROR
("GLOBAL_INITIALISERS",
2475 "do not initialise globals to 0 or NULL\n" .
2478 # check for static initialisers.
2479 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2480 ERROR
("INITIALISED_STATIC",
2481 "do not initialise statics to 0 or NULL\n" .
2485 # check for static const char * arrays.
2486 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2487 WARN
("STATIC_CONST_CHAR_ARRAY",
2488 "static const char * array should probably be static const char * const\n" .
2492 # check for static char foo[] = "bar" declarations.
2493 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2494 WARN
("STATIC_CONST_CHAR_ARRAY",
2495 "static char array declaration should probably be static const char\n" .
2499 # check for declarations of struct pci_device_id
2500 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2501 WARN
("DEFINE_PCI_DEVICE_TABLE",
2502 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
2505 # check for new typedefs, only function parameters and sparse annotations
2507 if ($line =~ /\btypedef\s/ &&
2508 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2509 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
2510 $line !~ /\b$typeTypedefs\b/ &&
2511 $line !~ /\b__bitwise(?:__|)\b/) {
2512 WARN
("NEW_TYPEDEFS",
2513 "do not add new typedefs\n" . $herecurr);
2516 # * goes on variable not on type
2518 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2520 my ($ident, $from, $to) = ($1, $2, $2);
2522 # Should start with a space.
2523 $to =~ s/^(\S)/ $1/;
2524 # Should not end with a space.
2526 # '*'s should not have spaces between.
2527 while ($to =~ s/\*\s+\*/\*\*/) {
2530 ## print "1: from<$from> to<$to> ident<$ident>\n";
2532 if (ERROR
("POINTER_LOCATION",
2533 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
2535 my $sub_from = $ident;
2536 my $sub_to = $ident;
2537 $sub_to =~ s/\Q$from\E/$to/;
2538 $fixed[$linenr - 1] =~
2539 s@\Q
$sub_from\E@
$sub_to@
;
2543 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2545 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
2547 # Should start with a space.
2548 $to =~ s/^(\S)/ $1/;
2549 # Should not end with a space.
2551 # '*'s should not have spaces between.
2552 while ($to =~ s/\*\s+\*/\*\*/) {
2554 # Modifiers should have spaces.
2555 $to =~ s/(\b$Modifier$)/$1 /;
2557 ## print "2: from<$from> to<$to> ident<$ident>\n";
2558 if ($from ne $to && $ident !~ /^$Modifier$/) {
2559 if (ERROR
("POINTER_LOCATION",
2560 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
2563 my $sub_from = $match;
2564 my $sub_to = $match;
2565 $sub_to =~ s/\Q$from\E/$to/;
2566 $fixed[$linenr - 1] =~
2567 s@\Q
$sub_from\E@
$sub_to@
;
2572 # # no BUG() or BUG_ON()
2573 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
2574 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2575 # print "$herecurr";
2579 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2580 WARN
("LINUX_VERSION_CODE",
2581 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
2584 # check for uses of printk_ratelimit
2585 if ($line =~ /\bprintk_ratelimit\s*\(/) {
2586 WARN
("PRINTK_RATELIMITED",
2587 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
2590 # printk should use KERN_* levels. Note that follow on printk's on the
2591 # same line do not need a level, so we use the current block context
2592 # to try and find and validate the current printk. In summary the current
2593 # printk includes all preceding printk's which have no newline on the end.
2594 # we assume the first bad printk is the one to report.
2595 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
2597 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2598 #print "CHECK<$lines[$ln - 1]\n";
2599 # we have a preceding printk if it ends
2600 # with "\n" ignore it, else it is to blame
2601 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2602 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2609 WARN
("PRINTK_WITHOUT_KERN_LEVEL",
2610 "printk() should include KERN_ facility level\n" . $herecurr);
2614 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2616 my $level = lc($orig);
2617 $level = "warn" if ($level eq "warning");
2618 my $level2 = $level;
2619 $level2 = "dbg" if ($level eq "debug");
2620 WARN
("PREFER_PR_LEVEL",
2621 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
2624 if ($line =~ /\bpr_warning\s*\(/) {
2625 WARN
("PREFER_PR_LEVEL",
2626 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr);
2629 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2631 my $level = lc($orig);
2632 $level = "warn" if ($level eq "warning");
2633 $level = "dbg" if ($level eq "debug");
2634 WARN
("PREFER_DEV_LEVEL",
2635 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2638 # function brace can't be on same line, except for #defines of do while,
2639 # or if closed on same line
2640 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2641 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2643 "open brace '{' following function declarations go on the next line\n" . $herecurr);
2646 # open braces for enum, union and struct go on the same line.
2647 if ($line =~ /^.\s*{/ &&
2648 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2650 "open brace '{' following $1 go on the same line\n" . $hereprev);
2653 # missing space after union, struct or enum definition
2654 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2656 "missing space after $1 definition\n" . $herecurr) &&
2658 $fixed[$linenr - 1] =~
2659 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
2663 # check for spacing round square brackets; allowed:
2664 # 1. with a type on the left -- int [] a;
2665 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2666 # 3. inside a curly brace -- = { [0...10] = 5 }
2667 while ($line =~ /(.*?\s)\[/g) {
2668 my ($where, $prefix) = ($-[1], $1);
2669 if ($prefix !~ /$Type\s+$/ &&
2670 ($where != 0 || $prefix !~ /^.\s+$/) &&
2671 $prefix !~ /[{,]\s+$/) {
2672 if (ERROR
("BRACKET_SPACE",
2673 "space prohibited before open square bracket '['\n" . $herecurr) &&
2675 $fixed[$linenr - 1] =~
2676 s/^(\+.*?)\s+\[/$1\[/;
2681 # check for spaces between functions and their parentheses.
2682 while ($line =~ /($Ident)\s+\(/g) {
2684 my $ctx_before = substr($line, 0, $-[1]);
2685 my $ctx = "$ctx_before$name";
2687 # Ignore those directives where spaces _are_ permitted.
2689 if|for|while|switch
|return|case
|
2690 volatile
|__volatile__
|
2691 __attribute__
|format
|__extension__
|
2694 # cpp #define statements have non-optional spaces, ie
2695 # if there is a space between the name and the open
2696 # parenthesis it is simply not a parameter group.
2697 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2699 # cpp #elif statement condition may start with a (
2700 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2702 # If this whole things ends with a type its most
2703 # likely a typedef for a function.
2704 } elsif ($ctx =~ /$Type$/) {
2708 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
2710 $fixed[$linenr - 1] =~
2711 s/\b$name\s+\(/$name\(/;
2716 # Check operator spacing.
2717 if (!($line=~/\#\s*include/)) {
2718 my $fixed_line = "";
2722 <<=|>>=|<=|>=|==|!=|
2723 \
+=|-=|\
*=|\
/=|%=|\
^=|\
|=|&=|
2724 =>|->|<<|>>|<|>|=|!|~|
2725 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%|
2728 my @elements = split(/($ops|;)/, $opline);
2730 ## print("element count: <" . $#elements . ">\n");
2731 ## foreach my $el (@elements) {
2732 ## print("el: <$el>\n");
2735 my @fix_elements = ();
2738 foreach my $el (@elements) {
2739 push(@fix_elements, substr($rawline, $off, length($el)));
2740 $off += length($el);
2745 my $blank = copy_spacing
($opline);
2747 for (my $n = 0; $n < $#elements; $n += 2) {
2749 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
2751 ## print("n: <$n> good: <$good>\n");
2753 $off += length($elements[$n]);
2755 # Pick up the preceding and succeeding characters.
2756 my $ca = substr($opline, 0, $off);
2758 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2759 $cc = substr($opline, $off + length($elements[$n + 1]));
2761 my $cb = "$ca$;$cc";
2764 $a = 'V' if ($elements[$n] ne '');
2765 $a = 'W' if ($elements[$n] =~ /\s$/);
2766 $a = 'C' if ($elements[$n] =~ /$;$/);
2767 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2768 $a = 'O' if ($elements[$n] eq '');
2769 $a = 'E' if ($ca =~ /^\s*$/);
2771 my $op = $elements[$n + 1];
2774 if (defined $elements[$n + 2]) {
2775 $c = 'V' if ($elements[$n + 2] ne '');
2776 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2777 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2778 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2779 $c = 'O' if ($elements[$n + 2] eq '');
2780 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2785 my $ctx = "${a}x${c}";
2787 my $at = "(ctx:$ctx)";
2789 my $ptr = substr($blank, 0, $off) . "^";
2790 my $hereptr = "$hereline$ptr\n";
2792 # Pull out the value of this operator.
2793 my $op_type = substr($curr_values, $off + 1, 1);
2795 # Get the full operator variant.
2796 my $opv = $op . substr($curr_vars, $off, 1);
2798 # Ignore operators passed as parameters.
2799 if ($op_type ne 'V' &&
2800 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2803 # } elsif ($op =~ /^$;+$/) {
2805 # ; should have either the end of line or a space or \ after it
2806 } elsif ($op eq ';') {
2807 if ($ctx !~ /.x[WEBC]/ &&
2808 $cc !~ /^\\/ && $cc !~ /^;/) {
2809 if (ERROR
("SPACING",
2810 "space required after that '$op' $at\n" . $hereptr)) {
2811 $good = trim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]) . " ";
2817 } elsif ($op eq '//') {
2821 # : when part of a bitfield
2822 } elsif ($op eq '->' || $opv eq ':B') {
2823 if ($ctx =~ /Wx.|.xW/) {
2824 if (ERROR
("SPACING",
2825 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
2826 $good = trim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
2828 if (defined $fix_elements[$n + 2]) {
2829 $fix_elements[$n + 2] =~ s/^\s+//;
2834 # , must have a space on the right.
2835 } elsif ($op eq ',') {
2836 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2837 if (ERROR
("SPACING",
2838 "space required after that '$op' $at\n" . $hereptr)) {
2839 $good = trim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]) . " ";
2844 # '*' as part of a type definition -- reported already.
2845 } elsif ($opv eq '*_') {
2846 #warn "'*' is part of type\n";
2848 # unary operators should have a space before and
2849 # none after. May be left adjacent to another
2850 # unary operator, or a cast
2851 } elsif ($op eq '!' || $op eq '~' ||
2852 $opv eq '*U' || $opv eq '-U' ||
2853 $opv eq '&U' || $opv eq '&&U') {
2854 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2855 if (ERROR
("SPACING",
2856 "space required before that '$op' $at\n" . $hereptr)) {
2857 $good = trim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]);
2861 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2862 # A unary '*' may be const
2864 } elsif ($ctx =~ /.xW/) {
2865 if (ERROR
("SPACING",
2866 "space prohibited after that '$op' $at\n" . $hereptr)) {
2867 $fixed_line =~ s/\s+$//;
2868 $good = trim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
2870 if (defined $fix_elements[$n + 2]) {
2871 $fix_elements[$n + 2] =~ s/^\s+//;
2876 # unary ++ and unary -- are allowed no space on one side.
2877 } elsif ($op eq '++' or $op eq '--') {
2878 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2879 if (ERROR
("SPACING",
2880 "space required one side of that '$op' $at\n" . $hereptr)) {
2881 $fixed_line =~ s/\s+$//;
2882 $good = trim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]) . " ";
2886 if ($ctx =~ /Wx[BE]/ ||
2887 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2888 if (ERROR
("SPACING",
2889 "space prohibited before that '$op' $at\n" . $hereptr)) {
2890 $fixed_line =~ s/\s+$//;
2891 $good = trim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
2895 if ($ctx =~ /ExW/) {
2896 if (ERROR
("SPACING",
2897 "space prohibited after that '$op' $at\n" . $hereptr)) {
2898 $fixed_line =~ s/\s+$//;
2899 $good = trim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
2901 if (defined $fix_elements[$n + 2]) {
2902 $fix_elements[$n + 2] =~ s/^\s+//;
2907 # << and >> may either have or not have spaces both sides
2908 } elsif ($op eq '<<' or $op eq '>>' or
2909 $op eq '&' or $op eq '^' or $op eq '|' or
2910 $op eq '+' or $op eq '-' or
2911 $op eq '*' or $op eq '/' or
2914 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2915 if (ERROR
("SPACING",
2916 "need consistent spacing around '$op' $at\n" . $hereptr)) {
2917 $fixed_line =~ s/\s+$//;
2918 $good = trim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]) . " ";
2923 # A colon needs no spaces before when it is
2924 # terminating a case value or a label.
2925 } elsif ($opv eq ':C' || $opv eq ':L') {
2926 if ($ctx =~ /Wx./) {
2927 if (ERROR
("SPACING",
2928 "space prohibited before that '$op' $at\n" . $hereptr)) {
2929 $good = trim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
2934 # All the others need spaces both sides.
2935 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2938 # Ignore email addresses <foo@bar>
2940 $cc =~ /^\S+\@\S+>/) ||
2942 $ca =~ /<\S+\@\S+$/))
2948 if (($opv eq ':O' && $ca =~ /\?$/) ||
2949 ($op eq '?' && $cc =~ /^:/)) {
2954 if (ERROR
("SPACING",
2955 "spaces required around that '$op' $at\n" . $hereptr)) {
2956 $good = trim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]) . " ";
2957 $good = $fix_elements[$n] . " " . trim
($fix_elements[$n + 1]) . " ";
2962 $off += length($elements[$n + 1]);
2964 ## print("n: <$n> GOOD: <$good>\n");
2966 $fixed_line = $fixed_line . $good;
2969 if (($#elements % 2) == 0) {
2970 $fixed_line = $fixed_line . $fix_elements[$#elements];
2973 if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
2974 $fixed[$linenr - 1] = $fixed_line;
2980 # check for whitespace before a non-naked semicolon
2981 if ($line =~ /^\+.*\S\s+;/) {
2983 "space prohibited before semicolon\n" . $herecurr) &&
2985 1 while $fixed[$linenr - 1] =~
2986 s/^(\+.*\S)\s+;/$1;/;
2990 # check for multiple assignments
2991 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2992 CHK
("MULTIPLE_ASSIGNMENTS",
2993 "multiple assignments should be avoided\n" . $herecurr);
2996 ## # check for multiple declarations, allowing for a function declaration
2998 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2999 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3001 ## # Remove any bracketed sections to ensure we do not
3002 ## # falsly report the parameters of functions.
3004 ## while ($ln =~ s/\([^\(\)]*\)//g) {
3006 ## if ($ln =~ /,/) {
3007 ## WARN("MULTIPLE_DECLARATION",
3008 ## "declaring multiple variables together should be avoided\n" . $herecurr);
3012 #need space before brace following if, while, etc
3013 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3015 if (ERROR
("SPACING",
3016 "space required before the open brace '{'\n" . $herecurr) &&
3018 $fixed[$linenr - 1] =~
3019 s/^(\+.*(?:do|\))){/$1 {/;
3023 ## # check for blank lines before declarations
3024 ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3025 ## $prevrawline =~ /^.\s*$/) {
3027 ## "No blank lines before declarations\n" . $hereprev);
3031 # closing brace should have a space following it when it has anything
3033 if ($line =~ /}(?!(?:,|;|\)))\S/) {
3035 "space required after that close brace '}'\n" . $herecurr);
3038 # check spacing on square brackets
3039 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
3040 if (ERROR
("SPACING",
3041 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3043 $fixed[$linenr - 1] =~
3047 if ($line =~ /\s\]/) {
3048 if (ERROR
("SPACING",
3049 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3051 $fixed[$linenr - 1] =~
3056 # check spacing on parentheses
3057 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3058 $line !~ /for\s*\(\s+;/) {
3059 if (ERROR
("SPACING",
3060 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3062 $fixed[$linenr - 1] =~
3066 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3067 $line !~ /for\s*\(.*;\s+\)/ &&
3068 $line !~ /:\s+\)/) {
3069 if (ERROR
("SPACING",
3070 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3072 $fixed[$linenr - 1] =~
3077 #goto labels aren't indented, allow a single space however
3078 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
3079 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
3080 if (WARN
("INDENTED_LABEL",
3081 "labels should not be indented\n" . $herecurr) &&
3083 $fixed[$linenr - 1] =~
3088 # Return is not a function.
3089 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
3093 # Flatten any parentheses
3094 $value =~ s/\(/ \(/g;
3095 $value =~ s/\)/\) /g;
3096 while ($value =~ s/\[[^\[\]]*\]/1/ ||
3097 $value !~ /(?
:$Ident|-?
$Constant)\s
*
3099 (?
:$Ident|-?
$Constant)/x
&&
3100 $value =~ s/\([^\(\)]*\)/1/) {
3102 #print "value<$value>\n";
3103 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
3104 ERROR
("RETURN_PARENTHESES",
3105 "return is not a function, parentheses are not required\n" . $herecurr);
3107 } elsif ($spacing !~ /\s+/) {
3109 "space required before the open parenthesis '('\n" . $herecurr);
3112 # Return of what appears to be an errno should normally be -'ve
3113 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3115 if ($name ne 'EOF' && $name ne 'ERROR') {
3116 WARN
("USE_NEGATIVE_ERRNO",
3117 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
3121 # Need a space before open parenthesis after if, while etc
3122 if ($line =~ /\b(if|while|for|switch)\(/) {
3123 if (ERROR
("SPACING",
3124 "space required before the open parenthesis '('\n" . $herecurr) &&
3126 $fixed[$linenr - 1] =~
3127 s/\b(if|while|for|switch)\(/$1 \(/;
3131 # Check for illegal assignment in if conditional -- and check for trailing
3132 # statements after the conditional.
3133 if ($line =~ /do\s*(?!{)/) {
3134 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3135 ctx_statement_block
($linenr, $realcnt, 0)
3136 if (!defined $stat);
3137 my ($stat_next) = ctx_statement_block
($line_nr_next,
3138 $remain_next, $off_next);
3139 $stat_next =~ s/\n./\n /g;
3140 ##print "stat<$stat> stat_next<$stat_next>\n";
3142 if ($stat_next =~ /^\s*while\b/) {
3143 # If the statement carries leading newlines,
3144 # then count those as offsets.
3146 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3148 statement_rawlines
($whitespace) - 1;
3150 $suppress_whiletrailers{$line_nr_next +
3154 if (!defined $suppress_whiletrailers{$linenr} &&
3155 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3156 my ($s, $c) = ($stat, $cond);
3158 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3159 ERROR
("ASSIGN_IN_IF",
3160 "do not use assignment in if condition\n" . $herecurr);
3163 # Find out what is on the end of the line after the
3165 substr($s, 0, length($c), '');
3167 $s =~ s/$;//g; # Remove any comments
3168 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3169 $c !~ /}\s*while\s*/)
3171 # Find out how long the conditional actually is.
3172 my @newlines = ($c =~ /\n/gs);
3173 my $cond_lines = 1 + $#newlines;
3176 $stat_real = raw_line
($linenr, $cond_lines)
3177 . "\n" if ($cond_lines);
3178 if (defined($stat_real) && $cond_lines > 1) {
3179 $stat_real = "[...]\n$stat_real";
3182 ERROR
("TRAILING_STATEMENTS",
3183 "trailing statements should be on next line\n" . $herecurr . $stat_real);
3187 # Check for bitwise tests written as boolean
3199 WARN
("HEXADECIMAL_BOOLEAN_TEST",
3200 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
3203 # if and else should not have general statements after it
3204 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3206 $s =~ s/$;//g; # Remove any comments
3207 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3208 ERROR
("TRAILING_STATEMENTS",
3209 "trailing statements should be on next line\n" . $herecurr);
3212 # if should not continue a brace
3213 if ($line =~ /}\s*if\b/) {
3214 ERROR
("TRAILING_STATEMENTS",
3215 "trailing statements should be on next line\n" .
3218 # case and default should not have general statements after them
3219 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3221 (?
:\s
*$;*)(?
:\s
*{)?
(?
:\s
*$;*)(?
:\s
*\\)?\s
*$|
3225 ERROR
("TRAILING_STATEMENTS",
3226 "trailing statements should be on next line\n" . $herecurr);
3229 # Check for }<nl>else {, these must be at the same
3230 # indent level to be relevant to each other.
3231 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3232 $previndent == $indent) {
3233 ERROR
("ELSE_AFTER_BRACE",
3234 "else should follow close brace '}'\n" . $hereprev);
3237 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3238 $previndent == $indent) {
3239 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
3241 # Find out what is on the end of the line after the
3243 substr($s, 0, length($c), '');
3246 if ($s =~ /^\s*;/) {
3247 ERROR
("WHILE_AFTER_BRACE",
3248 "while should follow close brace '}'\n" . $hereprev);
3252 #Specific variable tests
3253 while ($line =~ m{($Constant|$Lval)}g) {
3256 #gcc binary extension
3257 if ($var =~ /^$Binary$/) {
3258 WARN
("GCC_BINARY_CONSTANT",
3259 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr);
3263 if ($var !~ /^$Constant$/ &&
3264 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
3265 #Ignore Page<foo> variants
3266 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
3267 #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
3268 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
3269 seed_camelcase_includes
() if ($check);
3270 if (!defined $camelcase{$var}) {
3271 $camelcase{$var} = 1;
3273 "Avoid CamelCase: <$var>\n" . $herecurr);
3278 #no spaces allowed after \ in define
3279 if ($line=~/\#\s*define.*\\\s$/) {
3280 WARN
("WHITESPACE_AFTER_LINE_CONTINUATION",
3281 "Whitepspace after \\ makes next lines useless\n" . $herecurr);
3284 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
3285 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
3287 my $checkfile = "include/linux/$file";
3288 if (-f
"$root/$checkfile" &&
3289 $realfile ne $checkfile &&
3290 $1 !~ /$allowed_asm_includes/)
3292 if ($realfile =~ m{^arch/}) {
3293 CHK
("ARCH_INCLUDE_LINUX",
3294 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3296 WARN
("INCLUDE_LINUX",
3297 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3302 # multi-statement macros should be enclosed in a do while loop, grab the
3303 # first statement and ensure its the whole macro if its not enclosed
3304 # in a known good container
3305 if ($realfile !~ m@
/vmlinux
.lds
.h
$@
&&
3306 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
3309 my ($off, $dstat, $dcond, $rest);
3311 ($dstat, $dcond, $ln, $cnt, $off) =
3312 ctx_statement_block
($linenr, $realcnt, 0);
3314 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
3315 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
3317 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
3319 $dstat =~ s/\\\n.//g;
3320 $dstat =~ s/^\s*//s;
3321 $dstat =~ s/\s*$//s;
3323 # Flatten any parentheses and braces
3324 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3325 $dstat =~ s/\{[^\{\}]*\}/1/ ||
3326 $dstat =~ s/\[[^\[\]]*\]/1/)
3330 # Flatten any obvious string concatentation.
3331 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3332 $dstat =~ s/$Ident\s*("X*")/$1/)
3336 my $exceptions = qr{
3348 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
3350 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3351 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
3352 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
3353 $dstat !~ /^'X'$/ && # character constants
3354 $dstat !~ /$exceptions/ &&
3355 $dstat !~ /^\.$Ident\s*=/ && # .foo =
3356 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
3357 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
3358 $dstat !~ /^for\s*$Constant$/ && # for (...)
3359 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3360 $dstat !~ /^do\s*{/ && # do {...
3361 $dstat !~ /^\({/) # ({...
3364 my $herectx = $here . "\n";
3365 my $cnt = statement_rawlines
($ctx);
3367 for (my $n = 0; $n < $cnt; $n++) {
3368 $herectx .= raw_line
($linenr, $n) . "\n";
3371 if ($dstat =~ /;/) {
3372 ERROR
("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3373 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3375 ERROR
("COMPLEX_MACRO",
3376 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
3380 # check for line continuations outside of #defines, preprocessor #, and asm
3383 if ($prevline !~ /^..*\\$/ &&
3384 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3385 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
3386 $line =~ /^\+.*\\$/) {
3387 WARN
("LINE_CONTINUATIONS",
3388 "Avoid unnecessary line continuations\n" . $herecurr);
3392 # do {} while (0) macro tests:
3393 # single-statement macros do not need to be enclosed in do while (0) loop,
3394 # macro should not end with a semicolon
3395 if ($^V
&& $^V
ge 5.10.0 &&
3396 $realfile !~ m@
/vmlinux
.lds
.h
$@
&&
3397 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3400 my ($off, $dstat, $dcond, $rest);
3402 ($dstat, $dcond, $ln, $cnt, $off) =
3403 ctx_statement_block
($linenr, $realcnt, 0);
3406 $dstat =~ s/\\\n.//g;
3408 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3413 my $cnt = statement_rawlines
($ctx);
3414 my $herectx = $here . "\n";
3416 for (my $n = 0; $n < $cnt; $n++) {
3417 $herectx .= raw_line
($linenr, $n) . "\n";
3420 if (($stmts =~ tr/;/;/) == 1 &&
3421 $stmts !~ /^\s*(if|while|for|switch)\b/) {
3422 WARN
("SINGLE_STATEMENT_DO_WHILE_MACRO",
3423 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3425 if (defined $semis && $semis ne "") {
3426 WARN
("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3427 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3432 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3433 # all assignments may have only one of the following with an assignment:
3436 # VMLINUX_SYMBOL(...)
3437 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
3438 WARN
("MISSING_VMLINUX_SYMBOL",
3439 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
3442 # check for redundant bracing round if etc
3443 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3444 my ($level, $endln, @chunks) =
3445 ctx_statement_full
($linenr, $realcnt, 1);
3446 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
3447 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3448 if ($#chunks > 0 && $level == 0) {
3452 my $herectx = $here . "\n";
3453 my $ln = $linenr - 1;
3454 for my $chunk (@chunks) {
3455 my ($cond, $block) = @
{$chunk};
3457 # If the condition carries leading newlines, then count those as offsets.
3458 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3459 my $offset = statement_rawlines
($whitespace) - 1;
3461 $allowed[$allow] = 0;
3462 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3464 # We have looked at and allowed this specific line.
3465 $suppress_ifbraces{$ln + $offset} = 1;
3467 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
3468 $ln += statement_rawlines
($block) - 1;
3470 substr($block, 0, length($cond), '');
3472 $seen++ if ($block =~ /^\s*{/);
3474 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
3475 if (statement_lines
($cond) > 1) {
3476 #print "APW: ALLOWED: cond<$cond>\n";
3477 $allowed[$allow] = 1;
3479 if ($block =~/\b(?:if|for|while)\b/) {
3480 #print "APW: ALLOWED: block<$block>\n";
3481 $allowed[$allow] = 1;
3483 if (statement_block_size
($block) > 1) {
3484 #print "APW: ALLOWED: lines block<$block>\n";
3485 $allowed[$allow] = 1;
3490 my $sum_allowed = 0;
3491 foreach (@allowed) {
3494 if ($sum_allowed == 0) {
3496 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3497 } elsif ($sum_allowed != $allow &&
3500 "braces {} should be used on all arms of this statement\n" . $herectx);
3505 if (!defined $suppress_ifbraces{$linenr - 1} &&
3506 $line =~ /\b(if|while|for|else)\b/) {
3509 # Check the pre-context.
3510 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3511 #print "APW: ALLOWED: pre<$1>\n";
3515 my ($level, $endln, @chunks) =
3516 ctx_statement_full
($linenr, $realcnt, $-[0]);
3518 # Check the condition.
3519 my ($cond, $block) = @
{$chunks[0]};
3520 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3521 if (defined $cond) {
3522 substr($block, 0, length($cond), '');
3524 if (statement_lines
($cond) > 1) {
3525 #print "APW: ALLOWED: cond<$cond>\n";
3528 if ($block =~/\b(?:if|for|while)\b/) {
3529 #print "APW: ALLOWED: block<$block>\n";
3532 if (statement_block_size
($block) > 1) {
3533 #print "APW: ALLOWED: lines block<$block>\n";
3536 # Check the post-context.
3537 if (defined $chunks[1]) {
3538 my ($cond, $block) = @
{$chunks[1]};
3539 if (defined $cond) {
3540 substr($block, 0, length($cond), '');
3542 if ($block =~ /^\s*\{/) {
3543 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3547 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
3548 my $herectx = $here . "\n";
3549 my $cnt = statement_rawlines
($block);
3551 for (my $n = 0; $n < $cnt; $n++) {
3552 $herectx .= raw_line
($linenr, $n) . "\n";
3556 "braces {} are not necessary for single statement blocks\n" . $herectx);
3560 # check for unnecessary blank lines around braces
3561 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
3563 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3565 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
3567 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3570 # no volatiles please
3571 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3572 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3574 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
3578 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3579 CHK
("REDUNDANT_CODE",
3580 "if this code is redundant consider removing it\n" .
3584 # check for needless "if (<foo>) fn(<foo>)" uses
3585 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3586 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3587 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3589 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
3593 # prefer usleep_range over udelay
3594 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
3595 # ignore udelay's < 10, however
3598 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
3602 # warn about unexpectedly long msleep's
3603 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3606 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
3610 # check for comparisons of jiffies
3611 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3612 WARN
("JIFFIES_COMPARISON",
3613 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3616 # check for comparisons of get_jiffies_64()
3617 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3618 WARN
("JIFFIES_COMPARISON",
3619 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3622 # warn about #ifdefs in C files
3623 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
3624 # print "#ifdef in C files should be avoided\n";
3625 # print "$herecurr";
3629 # warn about spacing in #ifdefs
3630 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3631 if (ERROR
("SPACING",
3632 "exactly one space required after that #$1\n" . $herecurr) &&
3634 $fixed[$linenr - 1] =~
3635 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
3640 # check for spinlock_t definitions without a comment.
3641 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3642 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
3644 if (!ctx_has_comment
($first_line, $linenr)) {
3645 CHK
("UNCOMMENTED_DEFINITION",
3646 "$1 definition without comment\n" . $herecurr);
3649 # check for memory barriers without a comment.
3650 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3651 if (!ctx_has_comment
($first_line, $linenr)) {
3652 CHK
("MEMORY_BARRIER",
3653 "memory barrier without comment\n" . $herecurr);
3656 # check of hardware specific defines
3657 if ($line =~ m@
^.\s
*\#\s
*if.*\b(__i386__
|__powerpc64__
|__sun__
|__s390x__
)\b@
&& $realfile !~ m
@include/asm
-@
) {
3659 "architecture specific defines should be avoided\n" . $herecurr);
3662 # Check that the storage class is at the beginning of a declaration
3663 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3664 WARN
("STORAGE_CLASS",
3665 "storage class should be at the beginning of the declaration\n" . $herecurr)
3668 # check the location of the inline attribute, that it is between
3669 # storage class and type.
3670 if ($line =~ /\b$Type\s+$Inline\b/ ||
3671 $line =~ /\b$Inline\s+$Storage\b/) {
3672 ERROR
("INLINE_LOCATION",
3673 "inline keyword should sit between storage class and type\n" . $herecurr);
3676 # Check for __inline__ and __inline, prefer inline
3677 if ($line =~ /\b(__inline__|__inline)\b/) {
3679 "plain inline is preferred over $1\n" . $herecurr);
3682 # Check for __attribute__ packed, prefer __packed
3683 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3684 WARN
("PREFER_PACKED",
3685 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
3688 # Check for __attribute__ aligned, prefer __aligned
3689 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3690 WARN
("PREFER_ALIGNED",
3691 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
3694 # Check for __attribute__ format(printf, prefer __printf
3695 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3696 WARN
("PREFER_PRINTF",
3697 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3700 # Check for __attribute__ format(scanf, prefer __scanf
3701 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3702 WARN
("PREFER_SCANF",
3703 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
3706 # check for sizeof(&)
3707 if ($line =~ /\bsizeof\s*\(\s*\&/) {
3708 WARN
("SIZEOF_ADDRESS",
3709 "sizeof(& should be avoided\n" . $herecurr);
3712 # check for sizeof without parenthesis
3713 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
3714 WARN
("SIZEOF_PARENTHESIS",
3715 "sizeof $1 should be sizeof($1)\n" . $herecurr);
3718 # check for line continuations in quoted strings with odd counts of "
3719 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3720 WARN
("LINE_CONTINUATIONS",
3721 "Avoid line continuations in quoted strings\n" . $herecurr);
3724 # check for struct spinlock declarations
3725 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3726 WARN
("USE_SPINLOCK_T",
3727 "struct spinlock should be spinlock_t\n" . $herecurr);
3730 # check for seq_printf uses that could be seq_puts
3731 if ($line =~ /\bseq_printf\s*\(/) {
3732 my $fmt = get_quoted_string
($line, $rawline);
3733 if ($fmt !~ /[^\\]\%/) {
3734 WARN
("PREFER_SEQ_PUTS",
3735 "Prefer seq_puts to seq_printf\n" . $herecurr);
3739 # Check for misused memsets
3740 if ($^V
&& $^V
ge 5.10.0 &&
3742 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3748 if ($ms_size =~ /^(0x|)0$/i) {
3750 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
3751 } elsif ($ms_size =~ /^(0x|)1$/i) {
3753 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3757 # typecasts on min/max could be min_t/max_t
3758 if ($^V
&& $^V
ge 5.10.0 &&
3760 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3761 if (defined $2 || defined $7) {
3763 my $cast1 = deparenthesize
($2);
3765 my $cast2 = deparenthesize
($7);
3769 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
3770 $cast = "$cast1 or $cast2";
3771 } elsif ($cast1 ne "") {
3777 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
3781 # check usleep_range arguments
3782 if ($^V
&& $^V
ge 5.10.0 &&
3784 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
3788 WARN
("USLEEP_RANGE",
3789 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3790 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
3792 WARN
("USLEEP_RANGE",
3793 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3797 # check for new externs in .c files.
3798 if ($realfile =~ /\.c$/ && defined $stat &&
3799 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
3801 my $function_name = $1;
3802 my $paren_space = $2;
3805 if (defined $cond) {
3806 substr($s, 0, length($cond), '');
3808 if ($s =~ /^\s*;/ &&
3809 $function_name ne 'uninitialized_var')
3811 WARN
("AVOID_EXTERNS",
3812 "externs should be avoided in .c files\n" . $herecurr);
3815 if ($paren_space =~ /\n/) {
3816 WARN
("FUNCTION_ARGUMENTS",
3817 "arguments for function declarations should follow identifier\n" . $herecurr);
3820 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3821 $stat =~ /^.\s*extern\s+/)
3823 WARN
("AVOID_EXTERNS",
3824 "externs should be avoided in .c files\n" . $herecurr);
3827 # checks for new __setup's
3828 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3831 if (!grep(/$name/, @setup_docs)) {
3832 CHK
("UNDOCUMENTED_SETUP",
3833 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
3837 # check for pointless casting of kmalloc return
3838 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
3839 WARN
("UNNECESSARY_CASTS",
3840 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
3844 # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
3845 if ($^V
&& $^V
ge 5.10.0 &&
3846 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
3847 CHK
("ALLOC_SIZEOF_STRUCT",
3848 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
3851 # check for krealloc arg reuse
3852 if ($^V
&& $^V
ge 5.10.0 &&
3853 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
3854 WARN
("KREALLOC_ARG_REUSE",
3855 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
3858 # check for alloc argument mismatch
3859 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
3860 WARN
("ALLOC_ARRAY_ARGS",
3861 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
3864 # check for multiple semicolons
3865 if ($line =~ /;\s*;\s*$/) {
3866 WARN
("ONE_SEMICOLON",
3867 "Statements terminations use 1 semicolon\n" . $herecurr);
3870 # check for switch/default statements without a break;
3871 if ($^V
&& $^V
ge 5.10.0 &&
3873 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3875 my $herectx = $here . "\n";
3876 my $cnt = statement_rawlines
($stat);
3877 for (my $n = 0; $n < $cnt; $n++) {
3878 $herectx .= raw_line
($linenr, $n) . "\n";
3880 WARN
("DEFAULT_NO_BREAK",
3881 "switch default: should use break\n" . $herectx);
3884 # check for gcc specific __FUNCTION__
3885 if ($line =~ /__FUNCTION__/) {
3887 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
3890 # check for use of yield()
3891 if ($line =~ /\byield\s*\(\s*\)/) {
3893 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3896 # check for comparisons against true and false
3897 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
3905 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
3907 my $type = lc($otype);
3908 if ($type =~ /^(?:true|false)$/) {
3909 if (("$test" eq "==" && "$type" eq "true") ||
3910 ("$test" eq "!=" && "$type" eq "false")) {
3914 CHK
("BOOL_COMPARISON",
3915 "Using comparison to $otype is error prone\n" . $herecurr);
3917 ## maybe suggesting a correct construct would better
3918 ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
3923 # check for semaphores initialized locked
3924 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
3925 WARN
("CONSIDER_COMPLETION",
3926 "consider using a completion\n" . $herecurr);
3929 # recommend kstrto* over simple_strto* and strict_strto*
3930 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
3931 WARN
("CONSIDER_KSTRTO",
3932 "$1 is obsolete, use k$3 instead\n" . $herecurr);
3935 # check for __initcall(), use device_initcall() explicitly please
3936 if ($line =~ /^.\s*__initcall\s*\(/) {
3937 WARN
("USE_DEVICE_INITCALL",
3938 "please use device_initcall() instead of __initcall()\n" . $herecurr);
3941 # check for various ops structs, ensure they are const.
3942 my $struct_ops = qr{acpi_dock_ops
|
3943 address_space_operations
|
3945 block_device_operations
|
3950 file_lock_operations
|
3960 lock_manager_operations
|
3966 pipe_buf_operations
|
3967 platform_hibernation_ops
|
3968 platform_suspend_ops
|
3973 soc_pcmcia_socket_ops
|
3979 if ($line !~ /\bconst\b/ &&
3980 $line =~ /\bstruct\s+($struct_ops)\b/) {
3981 WARN
("CONST_STRUCT",
3982 "struct $1 should normally be const\n" .
3986 # use of NR_CPUS is usually wrong
3987 # ignore definitions of NR_CPUS and usage to define arrays as likely right
3988 if ($line =~ /\bNR_CPUS\b/ &&
3989 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3990 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
3991 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3992 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3993 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
3996 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3999 # check for %L{u,d,i} in strings
4001 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4002 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4003 $string =~ s/%%/__/g;
4004 if ($string =~ /(?<!%)%L[udi]/) {
4006 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4011 # whine mightly about in_atomic
4012 if ($line =~ /\bin_atomic\s*\(/) {
4013 if ($realfile =~ m@
^drivers
/@
) {
4015 "do not use in_atomic in drivers\n" . $herecurr);
4016 } elsif ($realfile !~ m@
^kernel
/@
) {
4018 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
4022 # check for lockdep_set_novalidate_class
4023 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
4024 $line =~ /__lockdep_no_validate__\s*\)/ ) {
4025 if ($realfile !~ m@
^kernel
/lockdep@
&&
4026 $realfile !~ m@
^include
/linux/lockdep@
&&
4027 $realfile !~ m@
^drivers
/base/core@
) {
4029 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
4033 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
4034 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
4035 WARN
("EXPORTED_WORLD_WRITABLE",
4036 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
4040 # If we have no input at all, then there is nothing to report on
4041 # so just keep quiet.
4042 if ($#rawlines == -1) {
4046 # In mailback mode only produce a report in the negative, for
4047 # things that appear to be patches.
4048 if ($mailback && ($clean == 1 || !$is_patch)) {
4052 # This is not a patch, and we are are in 'no-patch' mode so
4054 if (!$chk_patch && !$is_patch) {
4059 ERROR
("NOT_UNIFIED_DIFF",
4060 "Does not appear to be a unified-diff format patch\n");
4062 if ($is_patch && $chk_signoff && $signoff == 0) {
4063 ERROR
("MISSING_SIGN_OFF",
4064 "Missing Signed-off-by: line(s)\n");
4067 print report_dump
();
4068 if ($summary && !($clean == 1 && $quiet == 1)) {
4069 print "$filename " if ($summary_file);
4070 print "total: $cnt_error errors, $cnt_warn warnings, " .
4071 (($check)?
"$cnt_chk checks, " : "") .
4072 "$cnt_lines lines checked\n";
4073 print "\n" if ($quiet == 0);
4078 if ($^V
lt 5.10.0) {
4079 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4080 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4083 # If there were whitespace errors which cleanpatch can fix
4084 # then suggest that.
4085 if ($rpt_cleaners) {
4086 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4087 print " scripts/cleanfile\n\n";
4092 if ($quiet == 0 && keys %ignore_type) {
4093 print "NOTE: Ignored message types:";
4094 foreach my $ignore (sort keys %ignore_type) {
4100 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
4101 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
4105 open($f, '>', $newfile)
4106 or die "$P: Can't open $newfile for write\n";
4107 foreach my $fixed_line (@fixed) {
4110 if ($linecount > 3) {
4111 $fixed_line =~ s/^\+//;
4112 print $f $fixed_line. "\n";
4115 print $f $fixed_line . "\n";
4122 Wrote EXPERIMENTAL
--fix correction
(s
) to
'$newfile'
4124 Do _NOT_ trust the results written to this file
.
4125 Do _NOT_ submit these changes without inspecting them
for correctness
.
4127 This EXPERIMENTAL file is simply a convenience to help rewrite patches
.
4128 No warranties
, expressed
or implied
...
4134 if ($clean == 1 && $quiet == 0) {
4135 print "$vname has no obvious style problems and is ready for submission.\n"
4137 if ($clean == 0 && $quiet == 0) {
4139 $vname has style problems
, please review
.
4141 If any of these errors are false positives
, please report
4142 them to the maintainer
, see CHECKPATCH
in MAINTAINERS
.