2 # (c) 2001, Dave Jones. (the file handling bit)
3 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4 # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5 # (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
6 # (c) 2013 Vasilis Tsiligiannis <acinonyx@openwrt.gr> (adapt for OpenWrt tree)
7 # Licensed under the terms of the GNU GPL License version 2
14 my $V = '0.32-openwrt';
16 use Getopt
::Long
qw(:config no_auto_abbrev);
36 my $configuration_file = ".checkpatch.conf";
42 Usage
: $P [OPTION
]... [FILE
]...
47 --no-tree run without a kernel tree
48 --no-signoff
do not check
for 'Signed-off-by' line
49 --patch treat FILE as patchfile
(default)
50 --emacs emacs compile window format
51 --terse one line per report
52 -f
, --file treat FILE as regular source file
53 --subjective
, --strict enable more subjective tests
54 --ignore TYPE
(,TYPE2
...) ignore various comma separated message types
55 --show
-types show the message
"types" in the output
56 --root
=PATH PATH to the kernel tree root
57 --no-summary suppress the per
-file summary
58 --mailback only produce a report
in case of warnings
/errors
59 --summary
-file include the filename
in summary
60 --debug KEY
=[0|1] turn on
/off debugging of KEY
, where KEY is one of
61 'values', 'possible', 'type', and 'attr' (default
63 --test
-only
=WORD report only warnings
/errors containing WORD
65 -h
, --help
, --version display this help
and exit
67 When FILE is
- read standard input
.
73 my $conf = which_conf
($configuration_file);
76 open(my $conffile, '<', "$conf")
77 or warn "$P: Can't find a readable $configuration_file file $!\n";
82 $line =~ s/\s*\n?$//g;
86 next if ($line =~ m/^\s*#/);
87 next if ($line =~ m/^\s*$/);
89 my @words = split(" ", $line);
90 foreach my $word (@words) {
91 last if ($word =~ m/^#/);
92 push (@conf_args, $word);
96 unshift(@ARGV, @conf_args) if @conf_args;
100 'q|quiet+' => \
$quiet,
102 'signoff!' => \
$chk_signoff,
103 'patch!' => \
$chk_patch,
107 'subjective!' => \
$check,
108 'strict!' => \
$check,
109 'ignore=s' => \
@ignore,
110 'show-types!' => \
$show_types,
112 'summary!' => \
$summary,
113 'mailback!' => \
$mailback,
114 'summary-file!' => \
$summary_file,
116 'debug=s' => \
%debug,
117 'test-only=s' => \
$tst_only,
127 print "$P: no input files\n";
131 @ignore = split(/,/, join(',',@ignore));
132 foreach my $word (@ignore) {
133 $word =~ s/\s*\n?$//g;
136 $word =~ tr/[a-z]/[A-Z]/;
138 next if ($word =~ m/^\s*#/);
139 next if ($word =~ m/^\s*$/);
141 $ignore_type{$word}++;
145 my $dbg_possible = 0;
148 for my $key (keys %debug) {
150 eval "\${dbg_$key} = '$debug{$key}';";
154 my $rpt_cleaners = 0;
163 if (!top_of_openwrt_tree
($root)) {
164 die "$P: $root: --root does not point at a valid tree\n";
167 if (top_of_openwrt_tree
('.')) {
169 } elsif ($0 =~ m@
(.*)/scripts/[^/]*$@
&&
170 top_of_openwrt_tree
($1)) {
175 if (!defined $root) {
176 print "Must be run from the top-level dir. of a OpenWrt tree\n";
181 my $emitted_corrupt = 0;
184 [A
-Za
-z_
][A
-Za
-z\d_
]*
185 (?
:\s
*\#\#\s
*[A
-Za
-z_
][A
-Za
-z\d_
]*)*
187 our $Storage = qr{extern|static|asmlinkage};
200 # Notes to $Attribute:
201 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
220 __
(?
:mem
|cpu
|dev
|)(?
:initdata
|initconst
|init
\b)|
221 ____cacheline_aligned
|
222 ____cacheline_aligned_in_smp
|
223 ____cacheline_internodealigned_in_smp
|
227 our $Inline = qr{inline|__always_inline|noinline};
228 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
229 our $Lval = qr{$Ident(?:$Member)*};
231 our $Constant = qr{(?i:(?:[0-9]+|0x[0-9a-f]+)[ul]*)};
232 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
233 our $Compare = qr{<=|>=|==|!=|<|>};
237 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%
244 our $NON_ASCII_UTF8 = qr{
245 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
246 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
247 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
248 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
249 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
250 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
251 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
255 [\x09\x0A\x0D\x20-\x7E] # ASCII
259 our $typeTypedefs = qr{(?x
:
260 (?
:__
)?
(?
:u
|s
|be
|le)(?
:8|16|32|64)|
264 our $logFunctions = qr{(?x
:
265 printk
(?
:_ratelimited
|_once
|)|
266 [a
-z0
-9]+_
(?
:printk
|emerg
|alert
|crit
|err
|warning
|warn|notice
|info
|debug
|dbg
|vdbg
|devel
|cont
|WARN
)(?
:_ratelimited
|_once
|)|
267 WARN
(?
:_RATELIMIT
|_ONCE
|)|
272 our $signature_tags = qr{(?xi
:
284 qr{(?:unsigned\s+)?char},
285 qr{(?:unsigned\s+)?short},
286 qr{(?:unsigned\s+)?int},
287 qr{(?:unsigned\s+)?long},
288 qr{(?:unsigned\s+)?long\s+int},
289 qr{(?:unsigned\s+)?long\s+long},
290 qr{(?:unsigned\s+)?long\s+long\s+int},
299 qr{${Ident}_handler
},
300 qr{${Ident}_handler_fn
},
302 our @modifierList = (
306 our $allowed_asm_includes = qr{(?x
:
310 # memory.h: ARM has a custom one
313 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
314 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
315 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
317 (?
:$Modifier\s
+|const\s
+)*
319 (?
:typeof
|__typeof__
)\s
*\
([^\
)]*\
)|
323 (?
:\s
+$Modifier|\s
+const
)*
327 (?
:(?
:\s
|\
*|\
[\
])+\s
*const
|(?
:\s
|\
*|\
[\
])+|(?
:\s
*\
[\s
*\
])+)?
328 (?
:\s
+$Inline|\s
+$Modifier)*
330 $Declare = qr{(?:$Storage\s+)?$Type};
335 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s
*};
337 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
338 # requires at least perl version v5.10.0
339 # Any use must be runtime checked with $^V
341 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
342 our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s
*};
343 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
347 return "" if (!defined($string));
348 $string =~ s@
^\s
*\
(\s
*@
@g;
349 $string =~ s@\s
*\
)\s
*$@
@g;
350 $string =~ s@\s
+@
@g;
354 $chk_signoff = 0 if ($file);
359 for my $filename (@ARGV) {
362 open($FILE, '-|', "diff -u /dev/null $filename") ||
363 die "$P: $filename: diff failed - $!\n";
364 } elsif ($filename eq '-') {
365 open($FILE, '<&STDIN');
367 open($FILE, '<', "$filename") ||
368 die "$P: $filename: open failed - $!\n";
370 if ($filename eq '-') {
371 $vname = 'Your patch';
380 if (!process
($filename)) {
389 sub top_of_openwrt_tree
{
393 "BSDmakefile", "Config.in", "LICENSE", "Makefile", "README",
394 "docs", "feeds.conf.default", "include", "package", "rules.mk",
395 "scripts", "target", "toolchain", "tools"
398 foreach my $check (@tree_check) {
399 if (! -e
$root . '/' . $check) {
407 my ($formatted_email) = @_;
413 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
416 $comment = $3 if defined $3;
417 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
419 $comment = $2 if defined $2;
420 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
422 $comment = $2 if defined $2;
423 $formatted_email =~ s/$address.*$//;
424 $name = $formatted_email;
425 $name =~ s/^\s+|\s+$//g;
426 $name =~ s/^\"|\"$//g;
427 # If there's a name left after stripping spaces and
428 # leading quotes, and the address doesn't have both
429 # leading and trailing angle brackets, the address
431 # "joe smith joe@smith.com" bad
432 # "joe smith <joe@smith.com" bad
433 if ($name ne "" && $address !~ /^<[^>]+>$/) {
440 $name =~ s/^\s+|\s+$//g;
441 $name =~ s/^\"|\"$//g;
442 $address =~ s/^\s+|\s+$//g;
443 $address =~ s/^\<|\>$//g;
445 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
446 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
450 return ($name, $address, $comment);
454 my ($name, $address) = @_;
458 $name =~ s/^\s+|\s+$//g;
459 $name =~ s/^\"|\"$//g;
460 $address =~ s/^\s+|\s+$//g;
462 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
463 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
468 $formatted_email = "$address";
470 $formatted_email = "$name <$address>";
473 return $formatted_email;
479 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
480 if (-e
"$path/$conf") {
481 return "$path/$conf";
493 for my $c (split(//, $str)) {
497 for (; ($n % 8) != 0; $n++) {
509 (my $res = shift) =~ tr/\t/ /c;
516 # Drop the diff line leader and expand tabs
518 $line = expand_tabs
($line);
520 # Pick the indent from the front of the line.
521 my ($white) = ($line =~ /^(\s*)/);
523 return (length($line), length($white));
526 my $sanitise_quote = '';
528 sub sanitise_line_reset
{
529 my ($in_comment) = @_;
532 $sanitise_quote = '*/';
534 $sanitise_quote = '';
547 # Always copy over the diff marker.
548 $res = substr($line, 0, 1);
550 for ($off = 1; $off < length($line); $off++) {
551 $c = substr($line, $off, 1);
553 # Comments we are wacking completly including the begin
554 # and end, all to $;.
555 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
556 $sanitise_quote = '*/';
558 substr($res, $off, 2, "$;$;");
562 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
563 $sanitise_quote = '';
564 substr($res, $off, 2, "$;$;");
568 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
569 $sanitise_quote = '//';
571 substr($res, $off, 2, $sanitise_quote);
576 # A \ in a string means ignore the next character.
577 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
579 substr($res, $off, 2, 'XX');
584 if ($c eq "'" || $c eq '"') {
585 if ($sanitise_quote eq '') {
586 $sanitise_quote = $c;
588 substr($res, $off, 1, $c);
590 } elsif ($sanitise_quote eq $c) {
591 $sanitise_quote = '';
595 #print "c<$c> SQ<$sanitise_quote>\n";
596 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
597 substr($res, $off, 1, $;);
598 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
599 substr($res, $off, 1, $;);
600 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
601 substr($res, $off, 1, 'X');
603 substr($res, $off, 1, $c);
607 if ($sanitise_quote eq '//') {
608 $sanitise_quote = '';
611 # The pathname on a #include may be surrounded by '<' and '>'.
612 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
613 my $clean = 'X' x
length($1);
614 $res =~ s@\
<.*\
>@
<$clean>@
;
616 # The whole of a #error is a string.
617 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
618 my $clean = 'X' x
length($1);
619 $res =~ s@
(\#\s
*(?
:error
|warning
)\s
+).*@
$1$clean@
;
625 sub ctx_statement_block
{
626 my ($linenr, $remain, $off) = @_;
627 my $line = $linenr - 1;
644 @stack = (['', 0]) if ($#stack == -1);
646 #warn "CSB: blk<$blk> remain<$remain>\n";
647 # If we are about to drop off the end, pull in more
650 for (; $remain > 0; $line++) {
651 last if (!defined $lines[$line]);
652 next if ($lines[$line] =~ /^-/);
655 $blk .= $lines[$line] . "\n";
660 # Bail if there is no further context.
661 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
665 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
671 $c = substr($blk, $off, 1);
672 $remainder = substr($blk, $off);
674 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
676 # Handle nested #if/#else.
677 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
678 push(@stack, [ $type, $level ]);
679 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
680 ($type, $level) = @
{$stack[$#stack - 1]};
681 } elsif ($remainder =~ /^#\s*endif\b/) {
682 ($type, $level) = @
{pop(@stack)};
685 # Statement ends at the ';' or a close '}' at the
687 if ($level == 0 && $c eq ';') {
691 # An else is really a conditional as long as its not else if
692 if ($level == 0 && $coff_set == 0 &&
693 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
694 $remainder =~ /^(else)(?:\s|{)/ &&
695 $remainder !~ /^else\s+if\b/) {
696 $coff = $off + length($1) - 1;
698 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
699 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
702 if (($type eq '' || $type eq '(') && $c eq '(') {
706 if ($type eq '(' && $c eq ')') {
708 $type = ($level != 0)?
'(' : '';
710 if ($level == 0 && $coff < $soff) {
713 #warn "CSB: mark coff<$coff>\n";
716 if (($type eq '' || $type eq '{') && $c eq '{') {
720 if ($type eq '{' && $c eq '}') {
722 $type = ($level != 0)?
'{' : '';
725 if (substr($blk, $off + 1, 1) eq ';') {
731 # Preprocessor commands end at the newline unless escaped.
732 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
740 # We are truly at the end, so shuffle to the next line.
747 my $statement = substr($blk, $soff, $off - $soff + 1);
748 my $condition = substr($blk, $soff, $coff - $soff + 1);
750 #warn "STATEMENT<$statement>\n";
751 #warn "CONDITION<$condition>\n";
753 #print "coff<$coff> soff<$off> loff<$loff>\n";
755 return ($statement, $condition,
756 $line, $remain + 1, $off - $loff + 1, $level);
759 sub statement_lines
{
762 # Strip the diff line prefixes and rip blank lines at start and end.
763 $stmt =~ s/(^|\n)./$1/g;
767 my @stmt_lines = ($stmt =~ /\n/g);
769 return $#stmt_lines + 2;
772 sub statement_rawlines
{
775 my @stmt_lines = ($stmt =~ /\n/g);
777 return $#stmt_lines + 2;
780 sub statement_block_size
{
783 $stmt =~ s/(^|\n)./$1/g;
789 my @stmt_lines = ($stmt =~ /\n/g);
790 my @stmt_statements = ($stmt =~ /;/g);
792 my $stmt_lines = $#stmt_lines + 2;
793 my $stmt_statements = $#stmt_statements + 1;
795 if ($stmt_lines > $stmt_statements) {
798 return $stmt_statements;
802 sub ctx_statement_full
{
803 my ($linenr, $remain, $off) = @_;
804 my ($statement, $condition, $level);
808 # Grab the first conditional/block pair.
809 ($statement, $condition, $linenr, $remain, $off, $level) =
810 ctx_statement_block
($linenr, $remain, $off);
811 #print "F: c<$condition> s<$statement> remain<$remain>\n";
812 push(@chunks, [ $condition, $statement ]);
813 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
814 return ($level, $linenr, @chunks);
817 # Pull in the following conditional/block pairs and see if they
818 # could continue the statement.
820 ($statement, $condition, $linenr, $remain, $off, $level) =
821 ctx_statement_block
($linenr, $remain, $off);
822 #print "C: c<$condition> s<$statement> remain<$remain>\n";
823 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
825 push(@chunks, [ $condition, $statement ]);
828 return ($level, $linenr, @chunks);
832 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
834 my $start = $linenr - 1;
841 my @stack = ($level);
842 for ($line = $start; $remain > 0; $line++) {
843 next if ($rawlines[$line] =~ /^-/);
846 $blk .= $rawlines[$line];
848 # Handle nested #if/#else.
849 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
850 push(@stack, $level);
851 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
852 $level = $stack[$#stack - 1];
853 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
854 $level = pop(@stack);
857 foreach my $c (split(//, $lines[$line])) {
858 ##print "C<$c>L<$level><$open$close>O<$off>\n";
864 if ($c eq $close && $level > 0) {
866 last if ($level == 0);
867 } elsif ($c eq $open) {
872 if (!$outer || $level <= 1) {
873 push(@res, $rawlines[$line]);
876 last if ($level == 0);
879 return ($level, @res);
881 sub ctx_block_outer
{
882 my ($linenr, $remain) = @_;
884 my ($level, @r) = ctx_block_get
($linenr, $remain, 1, '{', '}', 0);
888 my ($linenr, $remain) = @_;
890 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
894 my ($linenr, $remain, $off) = @_;
896 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
899 sub ctx_block_level
{
900 my ($linenr, $remain) = @_;
902 return ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
904 sub ctx_statement_level
{
905 my ($linenr, $remain, $off) = @_;
907 return ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
910 sub ctx_locate_comment
{
911 my ($first_line, $end_line) = @_;
913 # Catch a comment on the end of the line itself.
914 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@
.*(/\*.*\*/)\s
*(?
:\\\s
*)?
$@
);
915 return $current_comment if (defined $current_comment);
917 # Look through the context and try and figure out if there is a
920 $current_comment = '';
921 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
922 my $line = $rawlines[$linenr - 1];
924 if ($linenr == $first_line and $line =~ m@
^.\s
*\
*@
) {
927 if ($line =~ m@
/\
*@
) {
930 if (!$in_comment && $current_comment ne '') {
931 $current_comment = '';
933 $current_comment .= $line . "\n" if ($in_comment);
934 if ($line =~ m@\
*/@
) {
939 chomp($current_comment);
940 return($current_comment);
942 sub ctx_has_comment
{
943 my ($first_line, $end_line) = @_;
944 my $cmt = ctx_locate_comment
($first_line, $end_line);
946 ##print "LINE: $rawlines[$end_line - 1 ]\n";
947 ##print "CMMT: $cmt\n";
953 my ($linenr, $cnt) = @_;
955 my $offset = $linenr - 1;
960 $line = $rawlines[$offset++];
961 next if (defined($line) && $line =~ /^-/);
973 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
976 $coded = sprintf("^%c", unpack('C', $2) + 64);
985 my $av_preprocessor = 0;
991 $av_preprocessor = 0;
993 @av_paren_type = ('E');
994 $av_pend_colon = 'O';
997 sub annotate_values
{
998 my ($stream, $type) = @_;
1001 my $var = '_' x
length($stream);
1004 print "$stream\n" if ($dbg_values > 1);
1006 while (length($cur)) {
1007 @av_paren_type = ('E') if ($#av_paren_type < 0);
1008 print " <" . join('', @av_paren_type) .
1009 "> <$type> <$av_pending>" if ($dbg_values > 1);
1010 if ($cur =~ /^(\s+)/o) {
1011 print "WS($1)\n" if ($dbg_values > 1);
1012 if ($1 =~ /\n/ && $av_preprocessor) {
1013 $type = pop(@av_paren_type);
1014 $av_preprocessor = 0;
1017 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1018 print "CAST($1)\n" if ($dbg_values > 1);
1019 push(@av_paren_type, $type);
1022 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1023 print "DECLARE($1)\n" if ($dbg_values > 1);
1026 } elsif ($cur =~ /^($Modifier)\s*/) {
1027 print "MODIFIER($1)\n" if ($dbg_values > 1);
1030 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1031 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1032 $av_preprocessor = 1;
1033 push(@av_paren_type, $type);
1039 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1040 print "UNDEF($1)\n" if ($dbg_values > 1);
1041 $av_preprocessor = 1;
1042 push(@av_paren_type, $type);
1044 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1045 print "PRE_START($1)\n" if ($dbg_values > 1);
1046 $av_preprocessor = 1;
1048 push(@av_paren_type, $type);
1049 push(@av_paren_type, $type);
1052 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1053 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1054 $av_preprocessor = 1;
1056 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1060 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1061 print "PRE_END($1)\n" if ($dbg_values > 1);
1063 $av_preprocessor = 1;
1065 # Assume all arms of the conditional end as this
1066 # one does, and continue as if the #endif was not here.
1067 pop(@av_paren_type);
1068 push(@av_paren_type, $type);
1071 } elsif ($cur =~ /^(\\\n)/o) {
1072 print "PRECONT($1)\n" if ($dbg_values > 1);
1074 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1075 print "ATTR($1)\n" if ($dbg_values > 1);
1076 $av_pending = $type;
1079 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1080 print "SIZEOF($1)\n" if ($dbg_values > 1);
1086 } elsif ($cur =~ /^(if|while|for)\b/o) {
1087 print "COND($1)\n" if ($dbg_values > 1);
1091 } elsif ($cur =~/^(case)/o) {
1092 print "CASE($1)\n" if ($dbg_values > 1);
1093 $av_pend_colon = 'C';
1096 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1097 print "KEYWORD($1)\n" if ($dbg_values > 1);
1100 } elsif ($cur =~ /^(\()/o) {
1101 print "PAREN('$1')\n" if ($dbg_values > 1);
1102 push(@av_paren_type, $av_pending);
1106 } elsif ($cur =~ /^(\))/o) {
1107 my $new_type = pop(@av_paren_type);
1108 if ($new_type ne '_') {
1110 print "PAREN('$1') -> $type\n"
1111 if ($dbg_values > 1);
1113 print "PAREN('$1')\n" if ($dbg_values > 1);
1116 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1117 print "FUNC($1)\n" if ($dbg_values > 1);
1121 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1122 if (defined $2 && $type eq 'C' || $type eq 'T') {
1123 $av_pend_colon = 'B';
1124 } elsif ($type eq 'E') {
1125 $av_pend_colon = 'L';
1127 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1130 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1131 print "IDENT($1)\n" if ($dbg_values > 1);
1134 } elsif ($cur =~ /^($Assignment)/o) {
1135 print "ASSIGN($1)\n" if ($dbg_values > 1);
1138 } elsif ($cur =~/^(;|{|})/) {
1139 print "END($1)\n" if ($dbg_values > 1);
1141 $av_pend_colon = 'O';
1143 } elsif ($cur =~/^(,)/) {
1144 print "COMMA($1)\n" if ($dbg_values > 1);
1147 } elsif ($cur =~ /^(\?)/o) {
1148 print "QUESTION($1)\n" if ($dbg_values > 1);
1151 } elsif ($cur =~ /^(:)/o) {
1152 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1154 substr($var, length($res), 1, $av_pend_colon);
1155 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1160 $av_pend_colon = 'O';
1162 } elsif ($cur =~ /^(\[)/o) {
1163 print "CLOSE($1)\n" if ($dbg_values > 1);
1166 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1169 print "OPV($1)\n" if ($dbg_values > 1);
1176 substr($var, length($res), 1, $variant);
1179 } elsif ($cur =~ /^($Operators)/o) {
1180 print "OP($1)\n" if ($dbg_values > 1);
1181 if ($1 ne '++' && $1 ne '--') {
1185 } elsif ($cur =~ /(^.)/o) {
1186 print "C($1)\n" if ($dbg_values > 1);
1189 $cur = substr($cur, length($1));
1190 $res .= $type x
length($1);
1194 return ($res, $var);
1198 my ($possible, $line) = @_;
1199 my $notPermitted = qr{(?
:
1216 ^(?
:typedef
|struct
|enum
)\b
1218 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1219 if ($possible !~ $notPermitted) {
1220 # Check for modifiers.
1221 $possible =~ s/\s*$Storage\s*//g;
1222 $possible =~ s/\s*$Sparse\s*//g;
1223 if ($possible =~ /^\s*$/) {
1225 } elsif ($possible =~ /\s/) {
1226 $possible =~ s/\s*$Type\s*//g;
1227 for my $modifier (split(' ', $possible)) {
1228 if ($modifier !~ $notPermitted) {
1229 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1230 push(@modifierList, $modifier);
1235 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1236 push(@typeList, $possible);
1240 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1247 return !defined $ignore_type{$_[0]};
1251 if (!show_type
($_[1]) ||
1252 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1257 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1259 $line = "$prefix$_[0]: $_[2]\n";
1261 $line = (split('\n', $line))[0] . "\n" if ($terse);
1263 push(our @report, $line);
1272 if (report
("ERROR", $_[0], $_[1])) {
1278 if (report
("WARNING", $_[0], $_[1])) {
1284 if ($check && report
("CHECK", $_[0], $_[1])) {
1290 sub check_absolute_file
{
1291 my ($absolute, $herecurr) = @_;
1292 my $file = $absolute;
1294 ##print "absolute<$absolute>\n";
1296 # See if any suffix of this path is a path within the tree.
1297 while ($file =~ s@
^[^/]*/@@
) {
1298 if (-f
"$root/$file") {
1299 ##print "file<$file>\n";
1307 # It is, so see if the prefix is acceptable.
1308 my $prefix = $absolute;
1309 substr($prefix, -length($file)) = '';
1311 ##print "prefix<$prefix>\n";
1312 if ($prefix ne ".../") {
1313 WARN
("USE_RELATIVE_PATH",
1314 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1318 sub pos_last_openparen
{
1323 my $opens = $line =~ tr/\(/\(/;
1324 my $closes = $line =~ tr/\)/\)/;
1326 my $last_openparen = 0;
1328 if (($opens == 0) || ($closes >= $opens)) {
1332 my $len = length($line);
1334 for ($pos = 0; $pos < $len; $pos++) {
1335 my $string = substr($line, $pos);
1336 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1337 $pos += length($1) - 1;
1338 } elsif (substr($line, $pos, 1) eq '(') {
1339 $last_openparen = $pos;
1340 } elsif (index($string, '(') == -1) {
1345 return $last_openparen + 1;
1349 my $filename = shift;
1355 my $stashrawline="";
1366 my $in_header_lines = 1;
1367 my $in_commit_log = 0; #Scanning lines before patch
1375 # Trace the real file/line as we go.
1381 my $comment_edge = 0;
1385 my $prev_values = 'E';
1388 my %suppress_ifbraces;
1389 my %suppress_whiletrailers;
1390 my %suppress_export;
1391 my $suppress_statement = 0;
1393 # Pre-scan the patch sanitizing the lines.
1394 # Pre-scan the patch looking for any __setup documentation.
1396 my @setup_docs = ();
1399 sanitise_line_reset
();
1401 foreach my $rawline (@rawlines) {
1405 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1407 if ($1 =~ m
@Documentation/kernel
-parameters
.txt
$@
) {
1412 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1421 # Guestimate if this is a continuing comment. Run
1422 # the context looking for a comment "edge". If this
1423 # edge is a close comment then we must be in a comment
1427 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1428 next if (defined $rawlines[$ln - 1] &&
1429 $rawlines[$ln - 1] =~ /^-/);
1431 #print "RAW<$rawlines[$ln - 1]>\n";
1432 last if (!defined $rawlines[$ln - 1]);
1433 if ($rawlines[$ln - 1] =~ m@
(/\*|\*/)@
&&
1434 $rawlines[$ln - 1] !~ m@
"[^"]*(?
:/\*|\*/)[^"]*"@
) {
1439 if (defined $edge && $edge eq '*/') {
1443 # Guestimate if this is a continuing comment. If this
1444 # is the start of a diff block and this line starts
1445 # ' *' then it is very likely a comment.
1446 if (!defined $edge &&
1447 $rawlines[$linenr] =~ m@
^.\s
*(?
:\
*\
*+| \
*)(?
:\s
|$)@
)
1452 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1453 sanitise_line_reset
($in_comment);
1455 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1456 # Standardise the strings and chars within the input to
1457 # simplify matching -- only bother with positive lines.
1458 $line = sanitise_line
($rawline);
1460 push(@lines, $line);
1463 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1468 #print "==>$rawline\n";
1469 #print "-->$line\n";
1471 if ($setup_docs && $line =~ /^\+/) {
1472 push(@setup_docs, $line);
1480 foreach my $line (@lines) {
1483 my $rawline = $rawlines[$linenr - 1];
1485 #extract the line range in the file after the patch is applied
1486 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1488 $first_line = $linenr + 1;
1498 %suppress_ifbraces = ();
1499 %suppress_whiletrailers = ();
1500 %suppress_export = ();
1501 $suppress_statement = 0;
1504 # track the line number as we move through the hunk, note that
1505 # new versions of GNU diff omit the leading space on completely
1506 # blank context lines so we need to count that too.
1507 } elsif ($line =~ /^( |\+|$)/) {
1509 $realcnt-- if ($realcnt != 0);
1511 # Measure the line length and indent.
1512 ($length, $indent) = line_stats
($rawline);
1514 # Track the previous line.
1515 ($prevline, $stashline) = ($stashline, $line);
1516 ($previndent, $stashindent) = ($stashindent, $indent);
1517 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1519 #warn "line<$line>\n";
1521 } elsif ($realcnt == 1) {
1525 my $hunk_line = ($realcnt != 0);
1527 #make up the handle for any error we report on this line
1528 $prefix = "$filename:$realline: " if ($emacs && $file);
1529 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1531 $here = "#$linenr: " if (!$file);
1532 $here = "#$realline: " if ($file);
1534 # extract the filename as it passes
1535 if ($line =~ /^diff --git.*?(\S+)$/) {
1537 $realfile =~ s@
^([^/]*)/@@
;
1539 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1541 $realfile =~ s@
^([^/]*)/@@
;
1545 if (!$file && $tree && $p1_prefix ne '' &&
1546 -e
"$root/$p1_prefix") {
1547 WARN
("PATCH_PREFIX",
1548 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1551 if ($realfile =~ m@
^include
/asm/@
) {
1552 ERROR
("MODIFIED_INCLUDE_ASM",
1553 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1558 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1560 my $hereline = "$here\n$rawline\n";
1561 my $herecurr = "$here\n$rawline\n";
1562 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1564 $cnt_lines++ if ($realcnt != 0);
1566 # Check for incorrect file permissions
1567 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1568 my $permhere = $here . "FILE: $realfile\n";
1569 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1570 ERROR
("EXECUTE_PERMISSIONS",
1571 "do not set execute permissions for source files\n" . $permhere);
1575 # Check the patch for a signoff:
1576 if ($line =~ /^\s*signed-off-by:/i) {
1581 # Check signature styles
1582 if (!$in_header_lines &&
1583 $line =~ /^(\s*)($signature_tags)(\s*)(.*)/) {
1584 my $space_before = $1;
1586 my $space_after = $3;
1588 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1590 if (defined $space_before && $space_before ne "") {
1591 WARN
("BAD_SIGN_OFF",
1592 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
1594 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1595 WARN
("BAD_SIGN_OFF",
1596 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
1598 if (!defined $space_after || $space_after ne " ") {
1599 WARN
("BAD_SIGN_OFF",
1600 "Use a single space after $ucfirst_sign_off\n" . $herecurr);
1603 my ($email_name, $email_address, $comment) = parse_email
($email);
1604 my $suggested_email = format_email
(($email_name, $email_address));
1605 if ($suggested_email eq "") {
1606 ERROR
("BAD_SIGN_OFF",
1607 "Unrecognized email address: '$email'\n" . $herecurr);
1609 my $dequoted = $suggested_email;
1610 $dequoted =~ s/^"//;
1611 $dequoted =~ s/" </ </;
1612 # Don't force email to have quotes
1613 # Allow just an angle bracketed address
1614 if ("$dequoted$comment" ne $email &&
1615 "<$email_address>$comment" ne $email &&
1616 "$suggested_email$comment" ne $email) {
1617 WARN
("BAD_SIGN_OFF",
1618 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
1623 # Check for wrappage within a valid hunk of the file
1624 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1625 ERROR
("CORRUPTED_PATCH",
1626 "patch seems to be corrupt (line wrapped?)\n" .
1627 $herecurr) if (!$emitted_corrupt++);
1630 # Check for absolute kernel paths.
1632 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1635 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1636 check_absolute_file
($1, $herecurr)) {
1639 check_absolute_file
($file, $herecurr);
1644 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1645 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1646 $rawline !~ m/^$UTF8*$/) {
1647 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1649 my $blank = copy_spacing
($rawline);
1650 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1651 my $hereptr = "$hereline$ptr\n";
1654 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1657 # Check if it's the start of a commit log
1658 # (not a header line and we haven't seen the patch filename)
1659 if ($in_header_lines && $realfile =~ /^$/ &&
1660 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
1661 $in_header_lines = 0;
1665 # Still not yet in a patch, check for any UTF-8
1666 if ($in_commit_log && $realfile =~ /^$/ &&
1667 $rawline =~ /$NON_ASCII_UTF8/) {
1668 CHK
("UTF8_BEFORE_PATCH",
1669 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1672 # ignore non-hunk lines and lines being removed
1673 next if (!$hunk_line || $line =~ /^-/);
1675 #trailing whitespace
1676 if ($line =~ /^\+.*\015/) {
1677 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1678 ERROR
("DOS_LINE_ENDINGS",
1679 "DOS line endings\n" . $herevet);
1681 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1682 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1683 ERROR
("TRAILING_WHITESPACE",
1684 "trailing whitespace\n" . $herevet);
1688 # check for Kconfig help text having a real description
1689 # Only applies when adding the entry originally, after that we do not have
1690 # sufficient context to determine whether it is indeed long enough.
1691 if ($realfile =~ /Kconfig/ &&
1692 $line =~ /.\s*config\s+/) {
1695 my $ln = $linenr + 1;
1699 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
1700 $f = $lines[$ln - 1];
1701 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1702 $is_end = $lines[$ln - 1] =~ /^\+/;
1704 next if ($f =~ /^-/);
1706 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1708 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1715 next if ($f =~ /^$/);
1716 if ($f =~ /^\s*config\s/) {
1722 WARN
("CONFIG_DESCRIPTION",
1723 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1724 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
1727 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1728 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1731 'EXTRA_AFLAGS' => 'asflags-y',
1732 'EXTRA_CFLAGS' => 'ccflags-y',
1733 'EXTRA_CPPFLAGS' => 'cppflags-y',
1734 'EXTRA_LDFLAGS' => 'ldflags-y',
1737 WARN
("DEPRECATED_VARIABLE",
1738 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1741 # check we are in a valid source file if not then ignore this hunk
1742 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1745 if ($line =~ /^\+/ && $prevrawline !~ /\/\
*\
*/ &&
1746 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1747 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
1748 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1752 "line over 80 characters\n" . $herecurr);
1755 # Check for user-visible strings broken across lines, which breaks the ability
1756 # to grep for the string. Limited to strings used as parameters (those
1757 # following an open parenthesis), which almost completely eliminates false
1758 # positives, as well as warning only once per parameter rather than once per
1759 # line of the string. Make an exception when the previous string ends in a
1760 # newline (multiple lines in one string constant) or \n\t (common in inline
1761 # assembly to indent the instruction on the following line).
1762 if ($line =~ /^\+\s*"/ &&
1763 $prevline =~ /"\s*$/ &&
1764 $prevline =~ /\(/ &&
1765 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1766 WARN
("SPLIT_STRING",
1767 "quoted string split across lines\n" . $hereprev);
1770 # check for spaces before a quoted newline
1771 if ($rawline =~ /^.*\".*\s\\n/) {
1772 WARN
("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1773 "unnecessary whitespace before a quoted newline\n" . $herecurr);
1776 # check for adding lines without a newline.
1777 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1778 WARN
("MISSING_EOF_NEWLINE",
1779 "adding a line without newline at end of file\n" . $herecurr);
1782 # Blackfin: use hi/lo macros
1783 if ($realfile =~ m
@arch/blackfin/.*\
.S
$@
) {
1784 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1785 my $herevet = "$here\n" . cat_vet
($line) . "\n";
1787 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1789 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1790 my $herevet = "$here\n" . cat_vet
($line) . "\n";
1792 "use the HI() macro, not (... >> 16)\n" . $herevet);
1796 # check we are in a valid source file C or perl if not then ignore this hunk
1797 next if ($realfile !~ /\.(h|c|pl)$/);
1799 # at the beginning of a line any tabs must come first and anything
1800 # more than 8 must use tabs.
1801 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1802 $rawline =~ /^\+\s* \s*/) {
1803 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1804 ERROR
("CODE_INDENT",
1805 "code indent should use tabs where possible\n" . $herevet);
1809 # check for space before tabs.
1810 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1811 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1812 WARN
("SPACE_BEFORE_TAB",
1813 "please, no space before tabs\n" . $herevet);
1816 # check for && or || at the start of a line
1817 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
1818 CHK
("LOGICAL_CONTINUATIONS",
1819 "Logical continuations should be on the previous line\n" . $hereprev);
1822 # check multi-line statement indentation matches previous line
1823 if ($^V
&& $^V
ge 5.10.0 &&
1824 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
1825 $prevline =~ /^\+(\t*)(.*)$/;
1829 my $pos = pos_last_openparen
($rest);
1831 $line =~ /^\+([ \t]*)/;
1834 my $goodtabindent = $oldindent .
1837 my $goodspaceindent = $oldindent . " " x
$pos;
1839 if ($newindent ne $goodtabindent &&
1840 $newindent ne $goodspaceindent) {
1841 CHK
("PARENTHESIS_ALIGNMENT",
1842 "Alignment should match open parenthesis\n" . $hereprev);
1847 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) {
1849 "No space is necessary after a cast\n" . $hereprev);
1852 if ($rawline =~ /^\+[ \t]*\/\
*[ \t]*$/ &&
1853 $prevrawline =~ /^\+[ \t]*$/) {
1854 CHK
("BLOCK_COMMENT_STYLE",
1855 "Don't begin block comments with only a /* line, use /* comment...\n" . $hereprev);
1858 # check for spaces at the beginning of a line.
1860 # 1) within comments
1861 # 2) indented preprocessor commands
1863 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
1864 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1865 WARN
("LEADING_SPACE",
1866 "please, no spaces at the start of a line\n" . $herevet);
1869 # check we are in a valid C source file if not then ignore this hunk
1870 next if ($realfile !~ /\.(h|c)$/);
1872 # check for RCS/CVS revision markers
1873 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1875 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1878 # Blackfin: don't use __builtin_bfin_[cs]sync
1879 if ($line =~ /__builtin_bfin_csync/) {
1880 my $herevet = "$here\n" . cat_vet
($line) . "\n";
1882 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1884 if ($line =~ /__builtin_bfin_ssync/) {
1885 my $herevet = "$here\n" . cat_vet
($line) . "\n";
1887 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1890 # Check for potential 'bare' types
1891 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1893 #print "LINE<$line>\n";
1894 if ($linenr >= $suppress_statement &&
1895 $realcnt && $line =~ /.\s*\S/) {
1896 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1897 ctx_statement_block
($linenr, $realcnt, 0);
1898 $stat =~ s/\n./\n /g;
1899 $cond =~ s/\n./\n /g;
1901 #print "linenr<$linenr> <$stat>\n";
1902 # If this statement has no statement boundaries within
1903 # it there is no point in retrying a statement scan
1904 # until we hit end of it.
1905 my $frag = $stat; $frag =~ s/;+\s*$//;
1906 if ($frag !~ /(?:{|;)/) {
1907 #print "skip<$line_nr_next>\n";
1908 $suppress_statement = $line_nr_next;
1911 # Find the real next line.
1912 $realline_next = $line_nr_next;
1913 if (defined $realline_next &&
1914 (!defined $lines[$realline_next - 1] ||
1915 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1922 # Ignore goto labels.
1923 if ($s =~ /$Ident:\*$/s) {
1925 # Ignore functions being called
1926 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1928 } elsif ($s =~ /^.\s*else\b/s) {
1930 # declarations always start with types
1931 } 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) {
1934 possible
($type, "A:" . $s);
1936 # definitions in global scope can only start with types
1937 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1938 possible
($1, "B:" . $s);
1941 # any (foo ... *) is a pointer cast, and foo is a type
1942 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1943 possible
($1, "C:" . $s);
1946 # Check for any sort of function declaration.
1947 # int foo(something bar, other baz);
1948 # void (*store_gdt)(x86_descr_ptr *);
1949 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1950 my ($name_len) = length($1);
1953 substr($ctx, 0, $name_len + 1, '');
1954 $ctx =~ s/\)[^\)]*$//;
1956 for my $arg (split(/\s*,\s*/, $ctx)) {
1957 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1959 possible
($1, "D:" . $s);
1967 # Checks which may be anchored in the context.
1970 # Check for switch () and associated case and default
1971 # statements should be at the same indent.
1972 if ($line=~/\bswitch\s*\(.*\)/) {
1975 my @ctx = ctx_block_outer
($linenr, $realcnt);
1977 for my $ctx (@ctx) {
1978 my ($clen, $cindent) = line_stats
($ctx);
1979 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1980 $indent != $cindent) {
1981 $err .= "$sep$ctx\n";
1988 ERROR
("SWITCH_CASE_INDENT_LEVEL",
1989 "switch and case should be at the same indent\n$hereline$err");
1993 # if/while/etc brace do not go on next line, unless defining a do while loop,
1994 # or if that brace on the next line is for something else
1995 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1996 my $pre_ctx = "$1$2";
1998 my ($level, @ctx) = ctx_statement_level
($linenr, $realcnt, 0);
2000 if ($line =~ /^\+\t{6,}/) {
2001 WARN
("DEEP_INDENTATION",
2002 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2005 my $ctx_cnt = $realcnt - $#ctx - 1;
2006 my $ctx = join("\n", @ctx);
2008 my $ctx_ln = $linenr;
2009 my $ctx_skip = $realcnt;
2011 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2012 defined $lines[$ctx_ln - 1] &&
2013 $lines[$ctx_ln - 1] =~ /^-/)) {
2014 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2015 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2019 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2020 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2022 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2024 "that open brace { should be on the previous line\n" .
2025 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2027 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2028 $ctx =~ /\)\s*\;\s*$/ &&
2029 defined $lines[$ctx_ln - 1])
2031 my ($nlength, $nindent) = line_stats
($lines[$ctx_ln - 1]);
2032 if ($nindent > $indent) {
2033 WARN
("TRAILING_SEMICOLON",
2034 "trailing semicolon indicates no statements, indent implies otherwise\n" .
2035 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2040 # Check relative indent for conditionals and blocks.
2041 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
2042 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2043 ctx_statement_block
($linenr, $realcnt, 0)
2044 if (!defined $stat);
2045 my ($s, $c) = ($stat, $cond);
2047 substr($s, 0, length($c), '');
2049 # Make sure we remove the line prefixes as we have
2050 # none on the first line, and are going to readd them
2054 # Find out how long the conditional actually is.
2055 my @newlines = ($c =~ /\n/gs);
2056 my $cond_lines = 1 + $#newlines;
2058 # We want to check the first line inside the block
2059 # starting at the end of the conditional, so remove:
2060 # 1) any blank line termination
2061 # 2) any opening brace { on end of the line
2063 my $continuation = 0;
2065 $s =~ s/^.*\bdo\b//;
2067 if ($s =~ s/^\s*\\//) {
2070 if ($s =~ s/^\s*?\n//) {
2075 # Also ignore a loop construct at the end of a
2076 # preprocessor statement.
2077 if (($prevline =~ /^.\s*#\s*define\s/ ||
2078 $prevline =~ /\\\s*$/) && $continuation == 0) {
2084 while ($cond_ptr != $cond_lines) {
2085 $cond_ptr = $cond_lines;
2087 # If we see an #else/#elif then the code
2089 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2094 # 1) blank lines, they should be at 0,
2095 # 2) preprocessor lines, and
2097 if ($continuation ||
2099 $s =~ /^\s*#\s*?/ ||
2100 $s =~ /^\s*$Ident\s*:/) {
2101 $continuation = ($s =~ /^.*?\\\n/) ?
1 : 0;
2102 if ($s =~ s/^.*?\n//) {
2108 my (undef, $sindent) = line_stats
("+" . $s);
2109 my $stat_real = raw_line
($linenr, $cond_lines);
2111 # Check if either of these lines are modified, else
2112 # this is not this patch's fault.
2113 if (!defined($stat_real) ||
2114 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2117 if (defined($stat_real) && $cond_lines > 1) {
2118 $stat_real = "[...]\n$stat_real";
2121 #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";
2123 if ($check && (($sindent % 8) != 0 ||
2124 ($sindent <= $indent && $s ne ''))) {
2125 WARN
("SUSPECT_CODE_INDENT",
2126 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
2130 # Track the 'values' across context and added lines.
2131 my $opline = $line; $opline =~ s/^./ /;
2132 my ($curr_values, $curr_vars) =
2133 annotate_values
($opline . "\n", $prev_values);
2134 $curr_values = $prev_values . $curr_values;
2136 my $outline = $opline; $outline =~ s/\t/ /g;
2137 print "$linenr > .$outline\n";
2138 print "$linenr > $curr_values\n";
2139 print "$linenr > $curr_vars\n";
2141 $prev_values = substr($curr_values, -1);
2143 #ignore lines not being added
2144 if ($line=~/^[^\+]/) {next;}
2146 # TEST: allow direct testing of the type matcher.
2148 if ($line =~ /^.\s*$Declare\s*$/) {
2150 "TEST: is type\n" . $herecurr);
2151 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2152 ERROR
("TEST_NOT_TYPE",
2153 "TEST: is not type ($1 is)\n". $herecurr);
2157 # TEST: allow direct testing of the attribute matcher.
2159 if ($line =~ /^.\s*$Modifier\s*$/) {
2161 "TEST: is attr\n" . $herecurr);
2162 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2163 ERROR
("TEST_NOT_ATTR",
2164 "TEST: is not attr ($1 is)\n". $herecurr);
2169 # check for initialisation to aggregates open brace on the next line
2170 if ($line =~ /^.\s*{/ &&
2171 $prevline =~ /(?:^|[^=])=\s*$/) {
2173 "that open brace { should be on the previous line\n" . $hereprev);
2177 # Checks which are anchored on the added line.
2180 # check for malformed paths in #include statements (uses RAW line)
2181 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2183 if ($path =~ m{//}) {
2184 ERROR
("MALFORMED_INCLUDE",
2185 "malformed #include filename\n" .
2190 # no C99 // comments
2191 if ($line =~ m{//}) {
2192 ERROR
("C99_COMMENTS",
2193 "do not use C99 // comments\n" . $herecurr);
2195 # Remove C99 comments.
2197 $opline =~ s@
//.*@@
;
2199 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2200 # the whole statement.
2201 #print "APW <$lines[$realline_next - 1]>\n";
2202 if (defined $realline_next &&
2203 exists $lines[$realline_next - 1] &&
2204 !defined $suppress_export{$realline_next} &&
2205 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2206 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2207 # Handle definitions which produce identifiers with
2210 # EXPORT_SYMBOL(something_foo);
2212 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
2213 $name =~ /^${Ident}_$2/) {
2214 #print "FOO C name<$name>\n";
2215 $suppress_export{$realline_next} = 1;
2217 } elsif ($stat !~ /(?
:
2219 ^.DEFINE_
$Ident\
(\Q
$name\E\
)|
2220 ^.DECLARE_
$Ident\
(\Q
$name\E\
)|
2221 ^.LIST_HEAD\
(\Q
$name\E\
)|
2222 ^.(?
:$Storage\s
+)?
$Type\s
*\
(\s
*\
*\s
*\Q
$name\E\s
*\
)\s
*\
(|
2223 \b\Q
$name\E
(?
:\s
+$Attribute)*\s
*(?
:;|=|\
[|\
()
2225 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2226 $suppress_export{$realline_next} = 2;
2228 $suppress_export{$realline_next} = 1;
2231 if (!defined $suppress_export{$linenr} &&
2232 $prevline =~ /^.\s*$/ &&
2233 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2234 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2235 #print "FOO B <$lines[$linenr - 1]>\n";
2236 $suppress_export{$linenr} = 2;
2238 if (defined $suppress_export{$linenr} &&
2239 $suppress_export{$linenr} == 2) {
2240 WARN
("EXPORT_SYMBOL",
2241 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
2244 # check for global initialisers.
2245 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
2246 ERROR
("GLOBAL_INITIALISERS",
2247 "do not initialise globals to 0 or NULL\n" .
2250 # check for static initialisers.
2251 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2252 ERROR
("INITIALISED_STATIC",
2253 "do not initialise statics to 0 or NULL\n" .
2257 # check for static const char * arrays.
2258 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2259 WARN
("STATIC_CONST_CHAR_ARRAY",
2260 "static const char * array should probably be static const char * const\n" .
2264 # check for static char foo[] = "bar" declarations.
2265 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2266 WARN
("STATIC_CONST_CHAR_ARRAY",
2267 "static char array declaration should probably be static const char\n" .
2271 # check for declarations of struct pci_device_id
2272 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2273 WARN
("DEFINE_PCI_DEVICE_TABLE",
2274 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
2277 # check for new typedefs, only function parameters and sparse annotations
2279 if ($line =~ /\btypedef\s/ &&
2280 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2281 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
2282 $line !~ /\b$typeTypedefs\b/ &&
2283 $line !~ /\b__bitwise(?:__|)\b/) {
2284 WARN
("NEW_TYPEDEFS",
2285 "do not add new typedefs\n" . $herecurr);
2288 # * goes on variable not on type
2290 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2292 my ($from, $to) = ($2, $2);
2294 # Should start with a space.
2295 $to =~ s/^(\S)/ $1/;
2296 # Should not end with a space.
2298 # '*'s should not have spaces between.
2299 while ($to =~ s/\*\s+\*/\*\*/) {
2302 #print "from<$from> to<$to>\n";
2304 ERROR
("POINTER_LOCATION",
2305 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
2308 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2310 my ($from, $to, $ident) = ($2, $2, $3);
2312 # Should start with a space.
2313 $to =~ s/^(\S)/ $1/;
2314 # Should not end with a space.
2316 # '*'s should not have spaces between.
2317 while ($to =~ s/\*\s+\*/\*\*/) {
2319 # Modifiers should have spaces.
2320 $to =~ s/(\b$Modifier$)/$1 /;
2322 #print "from<$from> to<$to> ident<$ident>\n";
2323 if ($from ne $to && $ident !~ /^$Modifier$/) {
2324 ERROR
("POINTER_LOCATION",
2325 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
2329 # # no BUG() or BUG_ON()
2330 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
2331 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2332 # print "$herecurr";
2336 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2337 WARN
("LINUX_VERSION_CODE",
2338 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
2341 # check for uses of printk_ratelimit
2342 if ($line =~ /\bprintk_ratelimit\s*\(/) {
2343 WARN
("PRINTK_RATELIMITED",
2344 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
2347 # printk should use KERN_* levels. Note that follow on printk's on the
2348 # same line do not need a level, so we use the current block context
2349 # to try and find and validate the current printk. In summary the current
2350 # printk includes all preceding printk's which have no newline on the end.
2351 # we assume the first bad printk is the one to report.
2352 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
2354 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2355 #print "CHECK<$lines[$ln - 1]\n";
2356 # we have a preceding printk if it ends
2357 # with "\n" ignore it, else it is to blame
2358 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2359 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2366 WARN
("PRINTK_WITHOUT_KERN_LEVEL",
2367 "printk() should include KERN_ facility level\n" . $herecurr);
2371 # function brace can't be on same line, except for #defines of do while,
2372 # or if closed on same line
2373 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2374 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2376 "open brace '{' following function declarations go on the next line\n" . $herecurr);
2379 # open braces for enum, union and struct go on the same line.
2380 if ($line =~ /^.\s*{/ &&
2381 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2383 "open brace '{' following $1 go on the same line\n" . $hereprev);
2386 # missing space after union, struct or enum definition
2387 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2389 "missing space after $1 definition\n" . $herecurr);
2392 # check for spacing round square brackets; allowed:
2393 # 1. with a type on the left -- int [] a;
2394 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2395 # 3. inside a curly brace -- = { [0...10] = 5 }
2396 while ($line =~ /(.*?\s)\[/g) {
2397 my ($where, $prefix) = ($-[1], $1);
2398 if ($prefix !~ /$Type\s+$/ &&
2399 ($where != 0 || $prefix !~ /^.\s+$/) &&
2400 $prefix !~ /[{,]\s+$/) {
2401 ERROR
("BRACKET_SPACE",
2402 "space prohibited before open square bracket '['\n" . $herecurr);
2406 # check for spaces between functions and their parentheses.
2407 while ($line =~ /($Ident)\s+\(/g) {
2409 my $ctx_before = substr($line, 0, $-[1]);
2410 my $ctx = "$ctx_before$name";
2412 # Ignore those directives where spaces _are_ permitted.
2414 if|for|while|switch
|return|case
|
2415 volatile
|__volatile__
|
2416 __attribute__
|format
|__extension__
|
2420 # cpp #define statements have non-optional spaces, ie
2421 # if there is a space between the name and the open
2422 # parenthesis it is simply not a parameter group.
2423 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2425 # cpp #elif statement condition may start with a (
2426 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2428 # If this whole things ends with a type its most
2429 # likely a typedef for a function.
2430 } elsif ($ctx =~ /$Type$/) {
2434 "space prohibited between function name and open parenthesis '('\n" . $herecurr);
2437 # Check operator spacing.
2438 if (!($line=~/\#\s*include/)) {
2440 <<=|>>=|<=|>=|==|!=|
2441 \
+=|-=|\
*=|\
/=|%=|\
^=|\
|=|&=|
2442 =>|->|<<|>>|<|>|=|!|~|
2443 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%|
2446 my @elements = split(/($ops|;)/, $opline);
2449 my $blank = copy_spacing
($opline);
2451 for (my $n = 0; $n < $#elements; $n += 2) {
2452 $off += length($elements[$n]);
2454 # Pick up the preceding and succeeding characters.
2455 my $ca = substr($opline, 0, $off);
2457 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2458 $cc = substr($opline, $off + length($elements[$n + 1]));
2460 my $cb = "$ca$;$cc";
2463 $a = 'V' if ($elements[$n] ne '');
2464 $a = 'W' if ($elements[$n] =~ /\s$/);
2465 $a = 'C' if ($elements[$n] =~ /$;$/);
2466 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2467 $a = 'O' if ($elements[$n] eq '');
2468 $a = 'E' if ($ca =~ /^\s*$/);
2470 my $op = $elements[$n + 1];
2473 if (defined $elements[$n + 2]) {
2474 $c = 'V' if ($elements[$n + 2] ne '');
2475 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2476 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2477 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2478 $c = 'O' if ($elements[$n + 2] eq '');
2479 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2484 my $ctx = "${a}x${c}";
2486 my $at = "(ctx:$ctx)";
2488 my $ptr = substr($blank, 0, $off) . "^";
2489 my $hereptr = "$hereline$ptr\n";
2491 # Pull out the value of this operator.
2492 my $op_type = substr($curr_values, $off + 1, 1);
2494 # Get the full operator variant.
2495 my $opv = $op . substr($curr_vars, $off, 1);
2497 # Ignore operators passed as parameters.
2498 if ($op_type ne 'V' &&
2499 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2502 # } elsif ($op =~ /^$;+$/) {
2504 # ; should have either the end of line or a space or \ after it
2505 } elsif ($op eq ';') {
2506 if ($ctx !~ /.x[WEBC]/ &&
2507 $cc !~ /^\\/ && $cc !~ /^;/) {
2509 "space required after that '$op' $at\n" . $hereptr);
2513 } elsif ($op eq '//') {
2517 # : when part of a bitfield
2518 } elsif ($op eq '->' || $opv eq ':B') {
2519 if ($ctx =~ /Wx.|.xW/) {
2521 "spaces prohibited around that '$op' $at\n" . $hereptr);
2524 # , must have a space on the right.
2525 } elsif ($op eq ',') {
2526 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2528 "space required after that '$op' $at\n" . $hereptr);
2531 # '*' as part of a type definition -- reported already.
2532 } elsif ($opv eq '*_') {
2533 #warn "'*' is part of type\n";
2535 # unary operators should have a space before and
2536 # none after. May be left adjacent to another
2537 # unary operator, or a cast
2538 } elsif ($op eq '!' || $op eq '~' ||
2539 $opv eq '*U' || $opv eq '-U' ||
2540 $opv eq '&U' || $opv eq '&&U') {
2541 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2543 "space required before that '$op' $at\n" . $hereptr);
2545 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2546 # A unary '*' may be const
2548 } elsif ($ctx =~ /.xW/) {
2550 "space prohibited after that '$op' $at\n" . $hereptr);
2553 # unary ++ and unary -- are allowed no space on one side.
2554 } elsif ($op eq '++' or $op eq '--') {
2555 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2557 "space required one side of that '$op' $at\n" . $hereptr);
2559 if ($ctx =~ /Wx[BE]/ ||
2560 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2562 "space prohibited before that '$op' $at\n" . $hereptr);
2564 if ($ctx =~ /ExW/) {
2566 "space prohibited after that '$op' $at\n" . $hereptr);
2570 # << and >> may either have or not have spaces both sides
2571 } elsif ($op eq '<<' or $op eq '>>' or
2572 $op eq '&' or $op eq '^' or $op eq '|' or
2573 $op eq '+' or $op eq '-' or
2574 $op eq '*' or $op eq '/' or
2577 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2579 "need consistent spacing around '$op' $at\n" .
2583 # A colon needs no spaces before when it is
2584 # terminating a case value or a label.
2585 } elsif ($opv eq ':C' || $opv eq ':L') {
2586 if ($ctx =~ /Wx./) {
2588 "space prohibited before that '$op' $at\n" . $hereptr);
2591 # All the others need spaces both sides.
2592 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2595 # Ignore email addresses <foo@bar>
2597 $cc =~ /^\S+\@\S+>/) ||
2599 $ca =~ /<\S+\@\S+$/))
2605 if (($opv eq ':O' && $ca =~ /\?$/) ||
2606 ($op eq '?' && $cc =~ /^:/)) {
2612 "spaces required around that '$op' $at\n" . $hereptr);
2615 $off += length($elements[$n + 1]);
2619 # check for multiple assignments
2620 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2621 CHK
("MULTIPLE_ASSIGNMENTS",
2622 "multiple assignments should be avoided\n" . $herecurr);
2625 ## # check for multiple declarations, allowing for a function declaration
2627 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2628 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2630 ## # Remove any bracketed sections to ensure we do not
2631 ## # falsly report the parameters of functions.
2633 ## while ($ln =~ s/\([^\(\)]*\)//g) {
2635 ## if ($ln =~ /,/) {
2636 ## WARN("MULTIPLE_DECLARATION",
2637 ## "declaring multiple variables together should be avoided\n" . $herecurr);
2641 #need space before brace following if, while, etc
2642 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2645 "space required before the open brace '{'\n" . $herecurr);
2648 # closing brace should have a space following it when it has anything
2650 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2652 "space required after that close brace '}'\n" . $herecurr);
2655 # check spacing on square brackets
2656 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2658 "space prohibited after that open square bracket '['\n" . $herecurr);
2660 if ($line =~ /\s\]/) {
2662 "space prohibited before that close square bracket ']'\n" . $herecurr);
2665 # check spacing on parentheses
2666 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2667 $line !~ /for\s*\(\s+;/) {
2669 "space prohibited after that open parenthesis '('\n" . $herecurr);
2671 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2672 $line !~ /for\s*\(.*;\s+\)/ &&
2673 $line !~ /:\s+\)/) {
2675 "space prohibited before that close parenthesis ')'\n" . $herecurr);
2678 #goto labels aren't indented, allow a single space however
2679 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2680 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2681 WARN
("INDENTED_LABEL",
2682 "labels should not be indented\n" . $herecurr);
2685 # Return is not a function.
2686 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2690 # Flatten any parentheses
2691 $value =~ s/\(/ \(/g;
2692 $value =~ s/\)/\) /g;
2693 while ($value =~ s/\[[^\[\]]*\]/1/ ||
2694 $value !~ /(?
:$Ident|-?
$Constant)\s
*
2696 (?
:$Ident|-?
$Constant)/x
&&
2697 $value =~ s/\([^\(\)]*\)/1/) {
2699 #print "value<$value>\n";
2700 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2701 ERROR
("RETURN_PARENTHESES",
2702 "return is not a function, parentheses are not required\n" . $herecurr);
2704 } elsif ($spacing !~ /\s+/) {
2706 "space required before the open parenthesis '('\n" . $herecurr);
2709 # Return of what appears to be an errno should normally be -'ve
2710 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2712 if ($name ne 'EOF' && $name ne 'ERROR') {
2713 WARN
("USE_NEGATIVE_ERRNO",
2714 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2718 # Need a space before open parenthesis after if, while etc
2719 if ($line=~/\b(if|while|for|switch)\(/) {
2720 ERROR
("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
2723 # Check for illegal assignment in if conditional -- and check for trailing
2724 # statements after the conditional.
2725 if ($line =~ /do\s*(?!{)/) {
2726 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2727 ctx_statement_block
($linenr, $realcnt, 0)
2728 if (!defined $stat);
2729 my ($stat_next) = ctx_statement_block
($line_nr_next,
2730 $remain_next, $off_next);
2731 $stat_next =~ s/\n./\n /g;
2732 ##print "stat<$stat> stat_next<$stat_next>\n";
2734 if ($stat_next =~ /^\s*while\b/) {
2735 # If the statement carries leading newlines,
2736 # then count those as offsets.
2738 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2740 statement_rawlines
($whitespace) - 1;
2742 $suppress_whiletrailers{$line_nr_next +
2746 if (!defined $suppress_whiletrailers{$linenr} &&
2747 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2748 my ($s, $c) = ($stat, $cond);
2750 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2751 ERROR
("ASSIGN_IN_IF",
2752 "do not use assignment in if condition\n" . $herecurr);
2755 # Find out what is on the end of the line after the
2757 substr($s, 0, length($c), '');
2759 $s =~ s/$;//g; # Remove any comments
2760 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2761 $c !~ /}\s*while\s*/)
2763 # Find out how long the conditional actually is.
2764 my @newlines = ($c =~ /\n/gs);
2765 my $cond_lines = 1 + $#newlines;
2768 $stat_real = raw_line
($linenr, $cond_lines)
2769 . "\n" if ($cond_lines);
2770 if (defined($stat_real) && $cond_lines > 1) {
2771 $stat_real = "[...]\n$stat_real";
2774 ERROR
("TRAILING_STATEMENTS",
2775 "trailing statements should be on next line\n" . $herecurr . $stat_real);
2779 # Check for bitwise tests written as boolean
2791 WARN
("HEXADECIMAL_BOOLEAN_TEST",
2792 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2795 # if and else should not have general statements after it
2796 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2798 $s =~ s/$;//g; # Remove any comments
2799 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2800 ERROR
("TRAILING_STATEMENTS",
2801 "trailing statements should be on next line\n" . $herecurr);
2804 # if should not continue a brace
2805 if ($line =~ /}\s*if\b/) {
2806 ERROR
("TRAILING_STATEMENTS",
2807 "trailing statements should be on next line\n" .
2810 # case and default should not have general statements after them
2811 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2813 (?
:\s
*$;*)(?
:\s
*{)?
(?
:\s
*$;*)(?
:\s
*\\)?\s
*$|
2817 ERROR
("TRAILING_STATEMENTS",
2818 "trailing statements should be on next line\n" . $herecurr);
2821 # Check for }<nl>else {, these must be at the same
2822 # indent level to be relevant to each other.
2823 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2824 $previndent == $indent) {
2825 ERROR
("ELSE_AFTER_BRACE",
2826 "else should follow close brace '}'\n" . $hereprev);
2829 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2830 $previndent == $indent) {
2831 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
2833 # Find out what is on the end of the line after the
2835 substr($s, 0, length($c), '');
2838 if ($s =~ /^\s*;/) {
2839 ERROR
("WHILE_AFTER_BRACE",
2840 "while should follow close brace '}'\n" . $hereprev);
2844 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2845 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2846 # print "No studly caps, use _\n";
2847 # print "$herecurr";
2851 #no spaces allowed after \ in define
2852 if ($line=~/\#\s*define.*\\\s$/) {
2853 WARN
("WHITESPACE_AFTER_LINE_CONTINUATION",
2854 "Whitepspace after \\ makes next lines useless\n" . $herecurr);
2857 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2858 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2860 my $checkfile = "include/linux/$file";
2861 if (-f
"$root/$checkfile" &&
2862 $realfile ne $checkfile &&
2863 $1 !~ /$allowed_asm_includes/)
2865 if ($realfile =~ m{^arch/}) {
2866 CHK
("ARCH_INCLUDE_LINUX",
2867 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2869 WARN
("INCLUDE_LINUX",
2870 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2875 # multi-statement macros should be enclosed in a do while loop, grab the
2876 # first statement and ensure its the whole macro if its not enclosed
2877 # in a known good container
2878 if ($realfile !~ m@
/vmlinux
.lds
.h
$@
&&
2879 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2882 my ($off, $dstat, $dcond, $rest);
2884 ($dstat, $dcond, $ln, $cnt, $off) =
2885 ctx_statement_block
($linenr, $realcnt, 0);
2887 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2888 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2890 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
2892 $dstat =~ s/\\\n.//g;
2893 $dstat =~ s/^\s*//s;
2894 $dstat =~ s/\s*$//s;
2896 # Flatten any parentheses and braces
2897 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2898 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2899 $dstat =~ s/\[[^\[\]]*\]/1/)
2903 # Flatten any obvious string concatentation.
2904 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
2905 $dstat =~ s/$Ident\s*("X*")/$1/)
2909 my $exceptions = qr{
2921 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2923 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
2924 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
2925 $dstat !~ /^[!~-]?(?:$Ident|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo
2926 $dstat !~ /^'X'$/ && # character constants
2927 $dstat !~ /$exceptions/ &&
2928 $dstat !~ /^\.$Ident\s*=/ && # .foo =
2929 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
2930 $dstat !~ /^for\s*$Constant$/ && # for (...)
2931 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
2932 $dstat !~ /^do\s*{/ && # do {...
2933 $dstat !~ /^\({/) # ({...
2936 my $herectx = $here . "\n";
2937 my $cnt = statement_rawlines
($ctx);
2939 for (my $n = 0; $n < $cnt; $n++) {
2940 $herectx .= raw_line
($linenr, $n) . "\n";
2943 if ($dstat =~ /;/) {
2944 ERROR
("MULTISTATEMENT_MACRO_USE_DO_WHILE",
2945 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
2947 ERROR
("COMPLEX_MACRO",
2948 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
2953 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2954 # all assignments may have only one of the following with an assignment:
2957 # VMLINUX_SYMBOL(...)
2958 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2959 WARN
("MISSING_VMLINUX_SYMBOL",
2960 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2963 # check for redundant bracing round if etc
2964 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2965 my ($level, $endln, @chunks) =
2966 ctx_statement_full
($linenr, $realcnt, 1);
2967 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2968 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2969 if ($#chunks > 0 && $level == 0) {
2973 my $herectx = $here . "\n";
2974 my $ln = $linenr - 1;
2975 for my $chunk (@chunks) {
2976 my ($cond, $block) = @
{$chunk};
2978 # If the condition carries leading newlines, then count those as offsets.
2979 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2980 my $offset = statement_rawlines
($whitespace) - 1;
2982 $allowed[$allow] = 0;
2983 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2985 # We have looked at and allowed this specific line.
2986 $suppress_ifbraces{$ln + $offset} = 1;
2988 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2989 $ln += statement_rawlines
($block) - 1;
2991 substr($block, 0, length($cond), '');
2993 $seen++ if ($block =~ /^\s*{/);
2995 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
2996 if (statement_lines
($cond) > 1) {
2997 #print "APW: ALLOWED: cond<$cond>\n";
2998 $allowed[$allow] = 1;
3000 if ($block =~/\b(?:if|for|while)\b/) {
3001 #print "APW: ALLOWED: block<$block>\n";
3002 $allowed[$allow] = 1;
3004 if (statement_block_size
($block) > 1) {
3005 #print "APW: ALLOWED: lines block<$block>\n";
3006 $allowed[$allow] = 1;
3011 my $sum_allowed = 0;
3012 foreach (@allowed) {
3015 if ($sum_allowed == 0) {
3017 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3018 } elsif ($sum_allowed != $allow &&
3021 "braces {} should be used on all arms of this statement\n" . $herectx);
3026 if (!defined $suppress_ifbraces{$linenr - 1} &&
3027 $line =~ /\b(if|while|for|else)\b/) {
3030 # Check the pre-context.
3031 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3032 #print "APW: ALLOWED: pre<$1>\n";
3036 my ($level, $endln, @chunks) =
3037 ctx_statement_full
($linenr, $realcnt, $-[0]);
3039 # Check the condition.
3040 my ($cond, $block) = @
{$chunks[0]};
3041 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3042 if (defined $cond) {
3043 substr($block, 0, length($cond), '');
3045 if (statement_lines
($cond) > 1) {
3046 #print "APW: ALLOWED: cond<$cond>\n";
3049 if ($block =~/\b(?:if|for|while)\b/) {
3050 #print "APW: ALLOWED: block<$block>\n";
3053 if (statement_block_size
($block) > 1) {
3054 #print "APW: ALLOWED: lines block<$block>\n";
3057 # Check the post-context.
3058 if (defined $chunks[1]) {
3059 my ($cond, $block) = @
{$chunks[1]};
3060 if (defined $cond) {
3061 substr($block, 0, length($cond), '');
3063 if ($block =~ /^\s*\{/) {
3064 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3068 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
3069 my $herectx = $here . "\n";
3070 my $cnt = statement_rawlines
($block);
3072 for (my $n = 0; $n < $cnt; $n++) {
3073 $herectx .= raw_line
($linenr, $n) . "\n";
3077 "braces {} are not necessary for single statement blocks\n" . $herectx);
3081 # no volatiles please
3082 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3083 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3085 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
3089 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3090 CHK
("REDUNDANT_CODE",
3091 "if this code is redundant consider removing it\n" .
3095 # check for needless kfree() checks
3096 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3098 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
3099 WARN
("NEEDLESS_KFREE",
3100 "kfree(NULL) is safe this check is probably not required\n" . $hereprev);
3103 # check for needless usb_free_urb() checks
3104 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3106 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
3107 WARN
("NEEDLESS_USB_FREE_URB",
3108 "usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
3112 # prefer usleep_range over udelay
3113 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
3114 # ignore udelay's < 10, however
3115 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
3117 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
3121 # warn about unexpectedly long msleep's
3122 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3125 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
3129 # warn about #ifdefs in C files
3130 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
3131 # print "#ifdef in C files should be avoided\n";
3132 # print "$herecurr";
3136 # warn about spacing in #ifdefs
3137 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3139 "exactly one space required after that #$1\n" . $herecurr);
3142 # check for spinlock_t definitions without a comment.
3143 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3144 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
3146 if (!ctx_has_comment
($first_line, $linenr)) {
3147 CHK
("UNCOMMENTED_DEFINITION",
3148 "$1 definition without comment\n" . $herecurr);
3151 # check for memory barriers without a comment.
3152 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3153 if (!ctx_has_comment
($first_line, $linenr)) {
3154 CHK
("MEMORY_BARRIER",
3155 "memory barrier without comment\n" . $herecurr);
3158 # check of hardware specific defines
3159 if ($line =~ m@
^.\s
*\#\s
*if.*\b(__i386__
|__powerpc64__
|__sun__
|__s390x__
)\b@
&& $realfile !~ m
@include/asm
-@
) {
3161 "architecture specific defines should be avoided\n" . $herecurr);
3164 # Check that the storage class is at the beginning of a declaration
3165 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3166 WARN
("STORAGE_CLASS",
3167 "storage class should be at the beginning of the declaration\n" . $herecurr)
3170 # check the location of the inline attribute, that it is between
3171 # storage class and type.
3172 if ($line =~ /\b$Type\s+$Inline\b/ ||
3173 $line =~ /\b$Inline\s+$Storage\b/) {
3174 ERROR
("INLINE_LOCATION",
3175 "inline keyword should sit between storage class and type\n" . $herecurr);
3178 # Check for __inline__ and __inline, prefer inline
3179 if ($line =~ /\b(__inline__|__inline)\b/) {
3181 "plain inline is preferred over $1\n" . $herecurr);
3184 # Check for __attribute__ packed, prefer __packed
3185 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3186 WARN
("PREFER_PACKED",
3187 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
3190 # Check for __attribute__ aligned, prefer __aligned
3191 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3192 WARN
("PREFER_ALIGNED",
3193 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
3196 # Check for __attribute__ format(printf, prefer __printf
3197 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3198 WARN
("PREFER_PRINTF",
3199 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3202 # Check for __attribute__ format(scanf, prefer __scanf
3203 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3204 WARN
("PREFER_SCANF",
3205 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
3208 # check for sizeof(&)
3209 if ($line =~ /\bsizeof\s*\(\s*\&/) {
3210 WARN
("SIZEOF_ADDRESS",
3211 "sizeof(& should be avoided\n" . $herecurr);
3214 # check for line continuations in quoted strings with odd counts of "
3215 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3216 WARN
("LINE_CONTINUATIONS",
3217 "Avoid line continuations in quoted strings\n" . $herecurr);
3220 # Check for misused memsets
3221 if ($^V
&& $^V
ge 5.10.0 &&
3223 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3229 if ($ms_size =~ /^(0x|)0$/i) {
3231 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
3232 } elsif ($ms_size =~ /^(0x|)1$/i) {
3234 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3238 # typecasts on min/max could be min_t/max_t
3239 if ($^V
&& $^V
ge 5.10.0 &&
3241 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3242 if (defined $2 || defined $7) {
3244 my $cast1 = deparenthesize
($2);
3246 my $cast2 = deparenthesize
($7);
3250 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
3251 $cast = "$cast1 or $cast2";
3252 } elsif ($cast1 ne "") {
3258 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
3262 # check for new externs in .c files.
3263 if ($realfile =~ /\.c$/ && defined $stat &&
3264 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
3266 my $function_name = $1;
3267 my $paren_space = $2;
3270 if (defined $cond) {
3271 substr($s, 0, length($cond), '');
3273 if ($s =~ /^\s*;/ &&
3274 $function_name ne 'uninitialized_var')
3276 WARN
("AVOID_EXTERNS",
3277 "externs should be avoided in .c files\n" . $herecurr);
3280 if ($paren_space =~ /\n/) {
3281 WARN
("FUNCTION_ARGUMENTS",
3282 "arguments for function declarations should follow identifier\n" . $herecurr);
3285 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3286 $stat =~ /^.\s*extern\s+/)
3288 WARN
("AVOID_EXTERNS",
3289 "externs should be avoided in .c files\n" . $herecurr);
3292 # checks for new __setup's
3293 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3296 if (!grep(/$name/, @setup_docs)) {
3297 CHK
("UNDOCUMENTED_SETUP",
3298 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
3302 # check for pointless casting of kmalloc return
3303 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
3304 WARN
("UNNECESSARY_CASTS",
3305 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
3308 # check for multiple semicolons
3309 if ($line =~ /;\s*;\s*$/) {
3310 WARN
("ONE_SEMICOLON",
3311 "Statements terminations use 1 semicolon\n" . $herecurr);
3314 # check for gcc specific __FUNCTION__
3315 if ($line =~ /__FUNCTION__/) {
3317 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
3320 # check for use of yield()
3321 if ($line =~ /\byield\s*\(\s*\)/) {
3323 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3326 # check for semaphores initialized locked
3327 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
3328 WARN
("CONSIDER_COMPLETION",
3329 "consider using a completion\n" . $herecurr);
3332 # recommend kstrto* over simple_strto* and strict_strto*
3333 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
3334 WARN
("CONSIDER_KSTRTO",
3335 "$1 is obsolete, use k$3 instead\n" . $herecurr);
3338 # check for __initcall(), use device_initcall() explicitly please
3339 if ($line =~ /^.\s*__initcall\s*\(/) {
3340 WARN
("USE_DEVICE_INITCALL",
3341 "please use device_initcall() instead of __initcall()\n" . $herecurr);
3344 # check for various ops structs, ensure they are const.
3345 my $struct_ops = qr{acpi_dock_ops
|
3346 address_space_operations
|
3348 block_device_operations
|
3353 file_lock_operations
|
3363 lock_manager_operations
|
3369 pipe_buf_operations
|
3370 platform_hibernation_ops
|
3371 platform_suspend_ops
|
3376 soc_pcmcia_socket_ops
|
3382 if ($line !~ /\bconst\b/ &&
3383 $line =~ /\bstruct\s+($struct_ops)\b/) {
3384 WARN
("CONST_STRUCT",
3385 "struct $1 should normally be const\n" .
3389 # use of NR_CPUS is usually wrong
3390 # ignore definitions of NR_CPUS and usage to define arrays as likely right
3391 if ($line =~ /\bNR_CPUS\b/ &&
3392 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3393 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
3394 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3395 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3396 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
3399 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3402 # check for %L{u,d,i} in strings
3404 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3405 $string = substr($rawline, $-[1], $+[1] - $-[1]);
3406 $string =~ s/%%/__/g;
3407 if ($string =~ /(?<!%)%L[udi]/) {
3409 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
3414 # whine mightly about in_atomic
3415 if ($line =~ /\bin_atomic\s*\(/) {
3416 if ($realfile =~ m@
^drivers
/@
) {
3418 "do not use in_atomic in drivers\n" . $herecurr);
3419 } elsif ($realfile !~ m@
^kernel
/@
) {
3421 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
3425 # check for lockdep_set_novalidate_class
3426 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3427 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3428 if ($realfile !~ m@
^kernel
/lockdep@
&&
3429 $realfile !~ m@
^include
/linux/lockdep@
&&
3430 $realfile !~ m@
^drivers
/base/core@
) {
3432 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
3436 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3437 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
3438 WARN
("EXPORTED_WORLD_WRITABLE",
3439 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
3443 # If we have no input at all, then there is nothing to report on
3444 # so just keep quiet.
3445 if ($#rawlines == -1) {
3449 # In mailback mode only produce a report in the negative, for
3450 # things that appear to be patches.
3451 if ($mailback && ($clean == 1 || !$is_patch)) {
3455 # This is not a patch, and we are are in 'no-patch' mode so
3457 if (!$chk_patch && !$is_patch) {
3462 ERROR
("NOT_UNIFIED_DIFF",
3463 "Does not appear to be a unified-diff format patch\n");
3465 if ($is_patch && $chk_signoff && $signoff == 0) {
3466 ERROR
("MISSING_SIGN_OFF",
3467 "Missing Signed-off-by: line(s)\n");
3470 print report_dump
();
3471 if ($summary && !($clean == 1 && $quiet == 1)) {
3472 print "$filename " if ($summary_file);
3473 print "total: $cnt_error errors, $cnt_warn warnings, " .
3474 (($check)?
"$cnt_chk checks, " : "") .
3475 "$cnt_lines lines checked\n";
3476 print "\n" if ($quiet == 0);
3481 if ($^V
lt 5.10.0) {
3482 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
3483 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
3486 # If there were whitespace errors which cleanpatch can fix
3487 # then suggest that.
3488 if ($rpt_cleaners) {
3489 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3490 print " scripts/cleanfile\n\n";
3495 if ($quiet == 0 && keys %ignore_type) {
3496 print "NOTE: Ignored message types:";
3497 foreach my $ignore (sort keys %ignore_type) {
3503 if ($clean == 1 && $quiet == 0) {
3504 print "$vname has no obvious style problems and is ready for submission.\n"
3506 if ($clean == 0 && $quiet == 0) {
3508 $vname has style problems
, please review
.
3510 If any of these errors are false positives
, please report
3511 them to the maintainer
, see CHECKPATCH
in MAINTAINERS
.