2 # (c) 2001, Dave Jones. (the file handling bit)
3 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4 # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5 # (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
6 # Licensed under the terms of the GNU GPL License version 2
16 use Getopt
::Long
qw(:config no_auto_abbrev);
38 my $configuration_file = ".checkpatch.conf";
39 my $max_line_length = 80;
40 my $ignore_perl_version = 0;
41 my $minimum_perl_version = 5.10.0;
47 Usage
: $P [OPTION
]... [FILE
]...
52 --no-tree run without a kernel tree
53 --no-signoff
do not check
for 'Signed-off-by' line
54 --patch treat FILE as patchfile
(default)
55 --emacs emacs compile window format
56 --terse one line per report
57 -f
, --file treat FILE as regular source file
58 --subjective
, --strict enable more subjective tests
59 --ignore TYPE
(,TYPE2
...) ignore various comma separated message types
60 --max
-line
-length=n set the maximum line
length, if exceeded
, warn
61 --show
-types show the message
"types" in the output
62 --root
=PATH PATH to the kernel tree root
63 --no-summary suppress the per
-file summary
64 --mailback only produce a report
in case of warnings
/errors
65 --summary
-file include the filename
in summary
66 --debug KEY
=[0|1] turn on
/off debugging of KEY
, where KEY is one of
67 'values', 'possible', 'type', and 'attr' (default
69 --test
-only
=WORD report only warnings
/errors containing WORD
71 --fix EXPERIMENTAL
- may create horrible results
72 If correctable single
-line errors exist
, create
73 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
74 with potential errors corrected to the preferred
76 --ignore
-perl
-version override checking of perl version
. expect
78 -h
, --help
, --version display this help
and exit
80 When FILE is
- read standard input
.
86 my $conf = which_conf
($configuration_file);
89 open(my $conffile, '<', "$conf")
90 or warn "$P: Can't find a readable $configuration_file file $!\n";
95 $line =~ s/\s*\n?$//g;
99 next if ($line =~ m/^\s*#/);
100 next if ($line =~ m/^\s*$/);
102 my @words = split(" ", $line);
103 foreach my $word (@words) {
104 last if ($word =~ m/^#/);
105 push (@conf_args, $word);
109 unshift(@ARGV, @conf_args) if @conf_args;
113 'q|quiet+' => \
$quiet,
115 'signoff!' => \
$chk_signoff,
116 'patch!' => \
$chk_patch,
120 'subjective!' => \
$check,
121 'strict!' => \
$check,
122 'ignore=s' => \
@ignore,
123 'show-types!' => \
$show_types,
124 'max-line-length=i' => \
$max_line_length,
126 'summary!' => \
$summary,
127 'mailback!' => \
$mailback,
128 'summary-file!' => \
$summary_file,
130 'ignore-perl-version!' => \
$ignore_perl_version,
131 'debug=s' => \
%debug,
132 'test-only=s' => \
$tst_only,
141 if ($^V
&& $^V
lt $minimum_perl_version) {
142 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
143 if (!$ignore_perl_version) {
149 print "$P: no input files\n";
153 @ignore = split(/,/, join(',',@ignore));
154 foreach my $word (@ignore) {
155 $word =~ s/\s*\n?$//g;
158 $word =~ tr/[a-z]/[A-Z]/;
160 next if ($word =~ m/^\s*#/);
161 next if ($word =~ m/^\s*$/);
163 $ignore_type{$word}++;
167 my $dbg_possible = 0;
170 for my $key (keys %debug) {
172 eval "\${dbg_$key} = '$debug{$key}';";
176 my $rpt_cleaners = 0;
185 if (!top_of_kernel_tree
($root)) {
186 die "$P: $root: --root does not point at a valid tree\n";
189 if (top_of_kernel_tree
('.')) {
191 } elsif ($0 =~ m@
(.*)/scripts/[^/]*$@
&&
192 top_of_kernel_tree
($1)) {
197 if (!defined $root) {
198 print "Must be run from the top-level dir. of a kernel tree\n";
203 my $emitted_corrupt = 0;
206 [A
-Za
-z_
][A
-Za
-z\d_
]*
207 (?
:\s
*\#\#\s
*[A
-Za
-z_
][A
-Za
-z\d_
]*)*
209 our $Storage = qr{extern|static|asmlinkage};
222 # Notes to $Attribute:
223 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
242 __
(?
:mem
|cpu
|dev
|)(?
:initdata
|initconst
|init
\b)|
243 ____cacheline_aligned
|
244 ____cacheline_aligned_in_smp
|
245 ____cacheline_internodealigned_in_smp
|
249 our $Inline = qr{inline|__always_inline|noinline};
250 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
251 our $Lval = qr{$Ident(?:$Member)*};
253 our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
254 our $Binary = qr{(?i)0b[01]+$Int_type?};
255 our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
256 our $Int = qr{[0-9]+$Int_type?};
257 our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
258 our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
259 our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
260 our $Float = qr{$Float_hex|$Float_dec|$Float_int};
261 our $Constant = qr{$Float|$Binary|$Hex|$Int};
262 our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
263 our $Compare = qr{<=|>=|==|!=|<|>};
264 our $Arithmetic = qr{\+|-|\*|\/|%};
268 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||$Arithmetic
275 our $NON_ASCII_UTF8 = qr{
276 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
277 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
278 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
279 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
280 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
281 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
282 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
286 [\x09\x0A\x0D\x20-\x7E] # ASCII
290 our $typeTypedefs = qr{(?x
:
291 (?
:__
)?
(?
:u
|s
|be
|le)(?
:8|16|32|64)|
295 our $logFunctions = qr{(?x
:
296 printk
(?
:_ratelimited
|_once
|)|
297 (?
:[a
-z0
-9]+_
){1,2}(?
:printk
|emerg
|alert
|crit
|err
|warning
|warn|notice
|info
|debug
|dbg
|vdbg
|devel
|cont
|WARN
)(?
:_ratelimited
|_once
|)|
298 WARN
(?
:_RATELIMIT
|_ONCE
|)|
303 our $signature_tags = qr{(?xi
:
316 qr{(?:unsigned\s+)?char},
317 qr{(?:unsigned\s+)?short},
318 qr{(?:unsigned\s+)?int},
319 qr{(?:unsigned\s+)?long},
320 qr{(?:unsigned\s+)?long\s+int},
321 qr{(?:unsigned\s+)?long\s+long},
322 qr{(?:unsigned\s+)?long\s+long\s+int},
331 qr{${Ident}_handler
},
332 qr{${Ident}_handler_fn
},
334 our @modifierList = (
338 our $allowed_asm_includes = qr{(?x
:
342 # memory.h: ARM has a custom one
345 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
346 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
347 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
349 (?
:$Modifier\s
+|const\s
+)*
351 (?
:typeof
|__typeof__
)\s
*\
([^\
)]*\
)|
355 (?
:\s
+$Modifier|\s
+const
)*
359 (?
:(?
:\s
|\
*|\
[\
])+\s
*const
|(?
:\s
|\
*|\
[\
])+|(?
:\s
*\
[\s
*\
])+)?
360 (?
:\s
+$Inline|\s
+$Modifier)*
362 $Declare = qr{(?:$Storage\s+)?$Type};
366 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s
*};
368 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
369 # requires at least perl version v5.10.0
370 # Any use must be runtime checked with $^V
372 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
373 our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s
*};
374 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
378 return "" if (!defined($string));
379 $string =~ s@
^\s
*\
(\s
*@
@g;
380 $string =~ s@\s
*\
)\s
*$@
@g;
381 $string =~ s@\s
+@
@g;
385 sub seed_camelcase_file
{
388 return if (!(-f
$file));
392 open(my $include_file, '<', "$file")
393 or warn "$P: Can't read '$file' $!\n";
394 my $text = <$include_file>;
395 close($include_file);
397 my @lines = split('\n', $text);
399 foreach my $line (@lines) {
400 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
401 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
404 elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*\(/) {
410 my $camelcase_seeded = 0;
411 sub seed_camelcase_includes
{
412 return if ($camelcase_seeded);
415 my $camelcase_cache = "";
416 my @include_files = ();
418 $camelcase_seeded = 1;
421 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
422 chomp $git_last_include_commit;
423 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
425 my $last_mod_date = 0;
426 $files = `find $root/include -name "*.h"`;
427 @include_files = split('\n', $files);
428 foreach my $file (@include_files) {
429 my $date = POSIX
::strftime
("%Y%m%d%H%M",
430 localtime((stat $file)[9]));
431 $last_mod_date = $date if ($last_mod_date < $date);
433 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
436 if ($camelcase_cache ne "" && -f
$camelcase_cache) {
437 open(my $camelcase_file, '<', "$camelcase_cache")
438 or warn "$P: Can't read '$camelcase_cache' $!\n";
439 while (<$camelcase_file>) {
443 close($camelcase_file);
449 $files = `git ls-files "include/*.h"`;
450 @include_files = split('\n', $files);
453 foreach my $file (@include_files) {
454 seed_camelcase_file
($file);
457 if ($camelcase_cache ne "") {
458 unlink glob ".checkpatch-camelcase.*";
459 open(my $camelcase_file, '>', "$camelcase_cache")
460 or warn "$P: Can't write '$camelcase_cache' $!\n";
461 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
462 print $camelcase_file ("$_\n");
464 close($camelcase_file);
468 $chk_signoff = 0 if ($file);
474 for my $filename (@ARGV) {
477 open($FILE, '-|', "diff -u /dev/null $filename") ||
478 die "$P: $filename: diff failed - $!\n";
479 } elsif ($filename eq '-') {
480 open($FILE, '<&STDIN');
482 open($FILE, '<', "$filename") ||
483 die "$P: $filename: open failed - $!\n";
485 if ($filename eq '-') {
486 $vname = 'Your patch';
495 if (!process
($filename)) {
505 sub top_of_kernel_tree
{
509 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
510 "README", "Documentation", "arch", "include", "drivers",
511 "fs", "init", "ipc", "kernel", "lib", "scripts",
514 foreach my $check (@tree_check) {
515 if (! -e
$root . '/' . $check) {
523 my ($formatted_email) = @_;
529 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
532 $comment = $3 if defined $3;
533 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
535 $comment = $2 if defined $2;
536 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
538 $comment = $2 if defined $2;
539 $formatted_email =~ s/$address.*$//;
540 $name = $formatted_email;
542 $name =~ s/^\"|\"$//g;
543 # If there's a name left after stripping spaces and
544 # leading quotes, and the address doesn't have both
545 # leading and trailing angle brackets, the address
547 # "joe smith joe@smith.com" bad
548 # "joe smith <joe@smith.com" bad
549 if ($name ne "" && $address !~ /^<[^>]+>$/) {
557 $name =~ s/^\"|\"$//g;
558 $address = trim
($address);
559 $address =~ s/^\<|\>$//g;
561 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
562 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
566 return ($name, $address, $comment);
570 my ($name, $address) = @_;
575 $name =~ s/^\"|\"$//g;
576 $address = trim
($address);
578 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
579 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
584 $formatted_email = "$address";
586 $formatted_email = "$name <$address>";
589 return $formatted_email;
595 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
596 if (-e
"$path/$conf") {
597 return "$path/$conf";
609 for my $c (split(//, $str)) {
613 for (; ($n % 8) != 0; $n++) {
625 (my $res = shift) =~ tr/\t/ /c;
632 # Drop the diff line leader and expand tabs
634 $line = expand_tabs
($line);
636 # Pick the indent from the front of the line.
637 my ($white) = ($line =~ /^(\s*)/);
639 return (length($line), length($white));
642 my $sanitise_quote = '';
644 sub sanitise_line_reset
{
645 my ($in_comment) = @_;
648 $sanitise_quote = '*/';
650 $sanitise_quote = '';
663 # Always copy over the diff marker.
664 $res = substr($line, 0, 1);
666 for ($off = 1; $off < length($line); $off++) {
667 $c = substr($line, $off, 1);
669 # Comments we are wacking completly including the begin
670 # and end, all to $;.
671 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
672 $sanitise_quote = '*/';
674 substr($res, $off, 2, "$;$;");
678 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
679 $sanitise_quote = '';
680 substr($res, $off, 2, "$;$;");
684 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
685 $sanitise_quote = '//';
687 substr($res, $off, 2, $sanitise_quote);
692 # A \ in a string means ignore the next character.
693 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
695 substr($res, $off, 2, 'XX');
700 if ($c eq "'" || $c eq '"') {
701 if ($sanitise_quote eq '') {
702 $sanitise_quote = $c;
704 substr($res, $off, 1, $c);
706 } elsif ($sanitise_quote eq $c) {
707 $sanitise_quote = '';
711 #print "c<$c> SQ<$sanitise_quote>\n";
712 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
713 substr($res, $off, 1, $;);
714 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
715 substr($res, $off, 1, $;);
716 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
717 substr($res, $off, 1, 'X');
719 substr($res, $off, 1, $c);
723 if ($sanitise_quote eq '//') {
724 $sanitise_quote = '';
727 # The pathname on a #include may be surrounded by '<' and '>'.
728 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
729 my $clean = 'X' x
length($1);
730 $res =~ s@\
<.*\
>@
<$clean>@
;
732 # The whole of a #error is a string.
733 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
734 my $clean = 'X' x
length($1);
735 $res =~ s@
(\#\s
*(?
:error
|warning
)\s
+).*@
$1$clean@
;
741 sub get_quoted_string
{
742 my ($line, $rawline) = @_;
744 return "" if ($line !~ m/(\"[X]+\")/g);
745 return substr($rawline, $-[0], $+[0] - $-[0]);
748 sub ctx_statement_block
{
749 my ($linenr, $remain, $off) = @_;
750 my $line = $linenr - 1;
767 @stack = (['', 0]) if ($#stack == -1);
769 #warn "CSB: blk<$blk> remain<$remain>\n";
770 # If we are about to drop off the end, pull in more
773 for (; $remain > 0; $line++) {
774 last if (!defined $lines[$line]);
775 next if ($lines[$line] =~ /^-/);
778 $blk .= $lines[$line] . "\n";
783 # Bail if there is no further context.
784 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
788 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
794 $c = substr($blk, $off, 1);
795 $remainder = substr($blk, $off);
797 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
799 # Handle nested #if/#else.
800 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
801 push(@stack, [ $type, $level ]);
802 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
803 ($type, $level) = @
{$stack[$#stack - 1]};
804 } elsif ($remainder =~ /^#\s*endif\b/) {
805 ($type, $level) = @
{pop(@stack)};
808 # Statement ends at the ';' or a close '}' at the
810 if ($level == 0 && $c eq ';') {
814 # An else is really a conditional as long as its not else if
815 if ($level == 0 && $coff_set == 0 &&
816 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
817 $remainder =~ /^(else)(?:\s|{)/ &&
818 $remainder !~ /^else\s+if\b/) {
819 $coff = $off + length($1) - 1;
821 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
822 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
825 if (($type eq '' || $type eq '(') && $c eq '(') {
829 if ($type eq '(' && $c eq ')') {
831 $type = ($level != 0)?
'(' : '';
833 if ($level == 0 && $coff < $soff) {
836 #warn "CSB: mark coff<$coff>\n";
839 if (($type eq '' || $type eq '{') && $c eq '{') {
843 if ($type eq '{' && $c eq '}') {
845 $type = ($level != 0)?
'{' : '';
848 if (substr($blk, $off + 1, 1) eq ';') {
854 # Preprocessor commands end at the newline unless escaped.
855 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
863 # We are truly at the end, so shuffle to the next line.
870 my $statement = substr($blk, $soff, $off - $soff + 1);
871 my $condition = substr($blk, $soff, $coff - $soff + 1);
873 #warn "STATEMENT<$statement>\n";
874 #warn "CONDITION<$condition>\n";
876 #print "coff<$coff> soff<$off> loff<$loff>\n";
878 return ($statement, $condition,
879 $line, $remain + 1, $off - $loff + 1, $level);
882 sub statement_lines
{
885 # Strip the diff line prefixes and rip blank lines at start and end.
886 $stmt =~ s/(^|\n)./$1/g;
890 my @stmt_lines = ($stmt =~ /\n/g);
892 return $#stmt_lines + 2;
895 sub statement_rawlines
{
898 my @stmt_lines = ($stmt =~ /\n/g);
900 return $#stmt_lines + 2;
903 sub statement_block_size
{
906 $stmt =~ s/(^|\n)./$1/g;
912 my @stmt_lines = ($stmt =~ /\n/g);
913 my @stmt_statements = ($stmt =~ /;/g);
915 my $stmt_lines = $#stmt_lines + 2;
916 my $stmt_statements = $#stmt_statements + 1;
918 if ($stmt_lines > $stmt_statements) {
921 return $stmt_statements;
925 sub ctx_statement_full
{
926 my ($linenr, $remain, $off) = @_;
927 my ($statement, $condition, $level);
931 # Grab the first conditional/block pair.
932 ($statement, $condition, $linenr, $remain, $off, $level) =
933 ctx_statement_block
($linenr, $remain, $off);
934 #print "F: c<$condition> s<$statement> remain<$remain>\n";
935 push(@chunks, [ $condition, $statement ]);
936 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
937 return ($level, $linenr, @chunks);
940 # Pull in the following conditional/block pairs and see if they
941 # could continue the statement.
943 ($statement, $condition, $linenr, $remain, $off, $level) =
944 ctx_statement_block
($linenr, $remain, $off);
945 #print "C: c<$condition> s<$statement> remain<$remain>\n";
946 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
948 push(@chunks, [ $condition, $statement ]);
951 return ($level, $linenr, @chunks);
955 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
957 my $start = $linenr - 1;
964 my @stack = ($level);
965 for ($line = $start; $remain > 0; $line++) {
966 next if ($rawlines[$line] =~ /^-/);
969 $blk .= $rawlines[$line];
971 # Handle nested #if/#else.
972 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
973 push(@stack, $level);
974 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
975 $level = $stack[$#stack - 1];
976 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
977 $level = pop(@stack);
980 foreach my $c (split(//, $lines[$line])) {
981 ##print "C<$c>L<$level><$open$close>O<$off>\n";
987 if ($c eq $close && $level > 0) {
989 last if ($level == 0);
990 } elsif ($c eq $open) {
995 if (!$outer || $level <= 1) {
996 push(@res, $rawlines[$line]);
999 last if ($level == 0);
1002 return ($level, @res);
1004 sub ctx_block_outer
{
1005 my ($linenr, $remain) = @_;
1007 my ($level, @r) = ctx_block_get
($linenr, $remain, 1, '{', '}', 0);
1011 my ($linenr, $remain) = @_;
1013 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
1017 my ($linenr, $remain, $off) = @_;
1019 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
1022 sub ctx_block_level
{
1023 my ($linenr, $remain) = @_;
1025 return ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
1027 sub ctx_statement_level
{
1028 my ($linenr, $remain, $off) = @_;
1030 return ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
1033 sub ctx_locate_comment
{
1034 my ($first_line, $end_line) = @_;
1036 # Catch a comment on the end of the line itself.
1037 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@
.*(/\*.*\*/)\s
*(?
:\\\s
*)?
$@
);
1038 return $current_comment if (defined $current_comment);
1040 # Look through the context and try and figure out if there is a
1043 $current_comment = '';
1044 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1045 my $line = $rawlines[$linenr - 1];
1047 if ($linenr == $first_line and $line =~ m@
^.\s
*\
*@
) {
1050 if ($line =~ m@
/\
*@
) {
1053 if (!$in_comment && $current_comment ne '') {
1054 $current_comment = '';
1056 $current_comment .= $line . "\n" if ($in_comment);
1057 if ($line =~ m@\
*/@
) {
1062 chomp($current_comment);
1063 return($current_comment);
1065 sub ctx_has_comment
{
1066 my ($first_line, $end_line) = @_;
1067 my $cmt = ctx_locate_comment
($first_line, $end_line);
1069 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1070 ##print "CMMT: $cmt\n";
1072 return ($cmt ne '');
1076 my ($linenr, $cnt) = @_;
1078 my $offset = $linenr - 1;
1083 $line = $rawlines[$offset++];
1084 next if (defined($line) && $line =~ /^-/);
1096 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1099 $coded = sprintf("^%c", unpack('C', $2) + 64);
1108 my $av_preprocessor = 0;
1113 sub annotate_reset
{
1114 $av_preprocessor = 0;
1116 @av_paren_type = ('E');
1117 $av_pend_colon = 'O';
1120 sub annotate_values
{
1121 my ($stream, $type) = @_;
1124 my $var = '_' x
length($stream);
1127 print "$stream\n" if ($dbg_values > 1);
1129 while (length($cur)) {
1130 @av_paren_type = ('E') if ($#av_paren_type < 0);
1131 print " <" . join('', @av_paren_type) .
1132 "> <$type> <$av_pending>" if ($dbg_values > 1);
1133 if ($cur =~ /^(\s+)/o) {
1134 print "WS($1)\n" if ($dbg_values > 1);
1135 if ($1 =~ /\n/ && $av_preprocessor) {
1136 $type = pop(@av_paren_type);
1137 $av_preprocessor = 0;
1140 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1141 print "CAST($1)\n" if ($dbg_values > 1);
1142 push(@av_paren_type, $type);
1145 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1146 print "DECLARE($1)\n" if ($dbg_values > 1);
1149 } elsif ($cur =~ /^($Modifier)\s*/) {
1150 print "MODIFIER($1)\n" if ($dbg_values > 1);
1153 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1154 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1155 $av_preprocessor = 1;
1156 push(@av_paren_type, $type);
1162 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1163 print "UNDEF($1)\n" if ($dbg_values > 1);
1164 $av_preprocessor = 1;
1165 push(@av_paren_type, $type);
1167 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1168 print "PRE_START($1)\n" if ($dbg_values > 1);
1169 $av_preprocessor = 1;
1171 push(@av_paren_type, $type);
1172 push(@av_paren_type, $type);
1175 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1176 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1177 $av_preprocessor = 1;
1179 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1183 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1184 print "PRE_END($1)\n" if ($dbg_values > 1);
1186 $av_preprocessor = 1;
1188 # Assume all arms of the conditional end as this
1189 # one does, and continue as if the #endif was not here.
1190 pop(@av_paren_type);
1191 push(@av_paren_type, $type);
1194 } elsif ($cur =~ /^(\\\n)/o) {
1195 print "PRECONT($1)\n" if ($dbg_values > 1);
1197 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1198 print "ATTR($1)\n" if ($dbg_values > 1);
1199 $av_pending = $type;
1202 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1203 print "SIZEOF($1)\n" if ($dbg_values > 1);
1209 } elsif ($cur =~ /^(if|while|for)\b/o) {
1210 print "COND($1)\n" if ($dbg_values > 1);
1214 } elsif ($cur =~/^(case)/o) {
1215 print "CASE($1)\n" if ($dbg_values > 1);
1216 $av_pend_colon = 'C';
1219 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1220 print "KEYWORD($1)\n" if ($dbg_values > 1);
1223 } elsif ($cur =~ /^(\()/o) {
1224 print "PAREN('$1')\n" if ($dbg_values > 1);
1225 push(@av_paren_type, $av_pending);
1229 } elsif ($cur =~ /^(\))/o) {
1230 my $new_type = pop(@av_paren_type);
1231 if ($new_type ne '_') {
1233 print "PAREN('$1') -> $type\n"
1234 if ($dbg_values > 1);
1236 print "PAREN('$1')\n" if ($dbg_values > 1);
1239 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1240 print "FUNC($1)\n" if ($dbg_values > 1);
1244 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1245 if (defined $2 && $type eq 'C' || $type eq 'T') {
1246 $av_pend_colon = 'B';
1247 } elsif ($type eq 'E') {
1248 $av_pend_colon = 'L';
1250 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1253 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1254 print "IDENT($1)\n" if ($dbg_values > 1);
1257 } elsif ($cur =~ /^($Assignment)/o) {
1258 print "ASSIGN($1)\n" if ($dbg_values > 1);
1261 } elsif ($cur =~/^(;|{|})/) {
1262 print "END($1)\n" if ($dbg_values > 1);
1264 $av_pend_colon = 'O';
1266 } elsif ($cur =~/^(,)/) {
1267 print "COMMA($1)\n" if ($dbg_values > 1);
1270 } elsif ($cur =~ /^(\?)/o) {
1271 print "QUESTION($1)\n" if ($dbg_values > 1);
1274 } elsif ($cur =~ /^(:)/o) {
1275 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1277 substr($var, length($res), 1, $av_pend_colon);
1278 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1283 $av_pend_colon = 'O';
1285 } elsif ($cur =~ /^(\[)/o) {
1286 print "CLOSE($1)\n" if ($dbg_values > 1);
1289 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1292 print "OPV($1)\n" if ($dbg_values > 1);
1299 substr($var, length($res), 1, $variant);
1302 } elsif ($cur =~ /^($Operators)/o) {
1303 print "OP($1)\n" if ($dbg_values > 1);
1304 if ($1 ne '++' && $1 ne '--') {
1308 } elsif ($cur =~ /(^.)/o) {
1309 print "C($1)\n" if ($dbg_values > 1);
1312 $cur = substr($cur, length($1));
1313 $res .= $type x
length($1);
1317 return ($res, $var);
1321 my ($possible, $line) = @_;
1322 my $notPermitted = qr{(?
:
1339 ^(?
:typedef
|struct
|enum
)\b
1341 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1342 if ($possible !~ $notPermitted) {
1343 # Check for modifiers.
1344 $possible =~ s/\s*$Storage\s*//g;
1345 $possible =~ s/\s*$Sparse\s*//g;
1346 if ($possible =~ /^\s*$/) {
1348 } elsif ($possible =~ /\s/) {
1349 $possible =~ s/\s*$Type\s*//g;
1350 for my $modifier (split(' ', $possible)) {
1351 if ($modifier !~ $notPermitted) {
1352 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1353 push(@modifierList, $modifier);
1358 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1359 push(@typeList, $possible);
1363 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1370 return !defined $ignore_type{$_[0]};
1374 if (!show_type
($_[1]) ||
1375 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1380 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1382 $line = "$prefix$_[0]: $_[2]\n";
1384 $line = (split('\n', $line))[0] . "\n" if ($terse);
1386 push(our @report, $line);
1395 if (report
("ERROR", $_[0], $_[1])) {
1403 if (report
("WARNING", $_[0], $_[1])) {
1411 if ($check && report
("CHECK", $_[0], $_[1])) {
1419 sub check_absolute_file
{
1420 my ($absolute, $herecurr) = @_;
1421 my $file = $absolute;
1423 ##print "absolute<$absolute>\n";
1425 # See if any suffix of this path is a path within the tree.
1426 while ($file =~ s@
^[^/]*/@@
) {
1427 if (-f
"$root/$file") {
1428 ##print "file<$file>\n";
1436 # It is, so see if the prefix is acceptable.
1437 my $prefix = $absolute;
1438 substr($prefix, -length($file)) = '';
1440 ##print "prefix<$prefix>\n";
1441 if ($prefix ne ".../") {
1442 WARN
("USE_RELATIVE_PATH",
1443 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1450 $string =~ s/(^\s+|\s+$)//g;
1458 my $source_indent = 8;
1459 my $max_spaces_before_tab = $source_indent - 1;
1460 my $spaces_to_tab = " " x
$source_indent;
1462 #convert leading spaces to tabs
1463 1 while $leading =~ s@
^([\t]*)$spaces_to_tab@
$1\t@g;
1464 #Remove spaces before a tab
1465 1 while $leading =~ s@
^([\t]*)( {1,$max_spaces_before_tab})\t@
$1\t@g;
1470 sub pos_last_openparen
{
1475 my $opens = $line =~ tr/\(/\(/;
1476 my $closes = $line =~ tr/\)/\)/;
1478 my $last_openparen = 0;
1480 if (($opens == 0) || ($closes >= $opens)) {
1484 my $len = length($line);
1486 for ($pos = 0; $pos < $len; $pos++) {
1487 my $string = substr($line, $pos);
1488 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1489 $pos += length($1) - 1;
1490 } elsif (substr($line, $pos, 1) eq '(') {
1491 $last_openparen = $pos;
1492 } elsif (index($string, '(') == -1) {
1497 return $last_openparen + 1;
1501 my $filename = shift;
1507 my $stashrawline="";
1518 my $in_header_lines = 1;
1519 my $in_commit_log = 0; #Scanning lines before patch
1521 my $non_utf8_charset = 0;
1529 # Trace the real file/line as we go.
1535 my $comment_edge = 0;
1539 my $prev_values = 'E';
1542 my %suppress_ifbraces;
1543 my %suppress_whiletrailers;
1544 my %suppress_export;
1545 my $suppress_statement = 0;
1547 my %signatures = ();
1549 # Pre-scan the patch sanitizing the lines.
1550 # Pre-scan the patch looking for any __setup documentation.
1552 my @setup_docs = ();
1555 sanitise_line_reset
();
1557 foreach my $rawline (@rawlines) {
1561 push(@fixed, $rawline) if ($fix);
1563 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1565 if ($1 =~ m
@Documentation/kernel
-parameters
.txt
$@
) {
1570 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1579 # Guestimate if this is a continuing comment. Run
1580 # the context looking for a comment "edge". If this
1581 # edge is a close comment then we must be in a comment
1585 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1586 next if (defined $rawlines[$ln - 1] &&
1587 $rawlines[$ln - 1] =~ /^-/);
1589 #print "RAW<$rawlines[$ln - 1]>\n";
1590 last if (!defined $rawlines[$ln - 1]);
1591 if ($rawlines[$ln - 1] =~ m@
(/\*|\*/)@
&&
1592 $rawlines[$ln - 1] !~ m@
"[^"]*(?
:/\*|\*/)[^"]*"@
) {
1597 if (defined $edge && $edge eq '*/') {
1601 # Guestimate if this is a continuing comment. If this
1602 # is the start of a diff block and this line starts
1603 # ' *' then it is very likely a comment.
1604 if (!defined $edge &&
1605 $rawlines[$linenr] =~ m@
^.\s
*(?
:\
*\
*+| \
*)(?
:\s
|$)@
)
1610 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1611 sanitise_line_reset
($in_comment);
1613 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1614 # Standardise the strings and chars within the input to
1615 # simplify matching -- only bother with positive lines.
1616 $line = sanitise_line
($rawline);
1618 push(@lines, $line);
1621 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1626 #print "==>$rawline\n";
1627 #print "-->$line\n";
1629 if ($setup_docs && $line =~ /^\+/) {
1630 push(@setup_docs, $line);
1638 foreach my $line (@lines) {
1641 my $rawline = $rawlines[$linenr - 1];
1643 #extract the line range in the file after the patch is applied
1644 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1646 $first_line = $linenr + 1;
1656 %suppress_ifbraces = ();
1657 %suppress_whiletrailers = ();
1658 %suppress_export = ();
1659 $suppress_statement = 0;
1662 # track the line number as we move through the hunk, note that
1663 # new versions of GNU diff omit the leading space on completely
1664 # blank context lines so we need to count that too.
1665 } elsif ($line =~ /^( |\+|$)/) {
1667 $realcnt-- if ($realcnt != 0);
1669 # Measure the line length and indent.
1670 ($length, $indent) = line_stats
($rawline);
1672 # Track the previous line.
1673 ($prevline, $stashline) = ($stashline, $line);
1674 ($previndent, $stashindent) = ($stashindent, $indent);
1675 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1677 #warn "line<$line>\n";
1679 } elsif ($realcnt == 1) {
1683 my $hunk_line = ($realcnt != 0);
1685 #make up the handle for any error we report on this line
1686 $prefix = "$filename:$realline: " if ($emacs && $file);
1687 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1689 $here = "#$linenr: " if (!$file);
1690 $here = "#$realline: " if ($file);
1692 # extract the filename as it passes
1693 if ($line =~ /^diff --git.*?(\S+)$/) {
1695 $realfile =~ s@
^([^/]*)/@@
;
1697 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1699 $realfile =~ s@
^([^/]*)/@@
;
1703 if (!$file && $tree && $p1_prefix ne '' &&
1704 -e
"$root/$p1_prefix") {
1705 WARN
("PATCH_PREFIX",
1706 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1709 if ($realfile =~ m@
^include
/asm/@
) {
1710 ERROR
("MODIFIED_INCLUDE_ASM",
1711 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1716 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1718 my $hereline = "$here\n$rawline\n";
1719 my $herecurr = "$here\n$rawline\n";
1720 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1722 $cnt_lines++ if ($realcnt != 0);
1724 # Check for incorrect file permissions
1725 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1726 my $permhere = $here . "FILE: $realfile\n";
1727 if ($realfile !~ m
@scripts/@
&&
1728 $realfile !~ /\.(py|pl|awk|sh)$/) {
1729 ERROR
("EXECUTE_PERMISSIONS",
1730 "do not set execute permissions for source files\n" . $permhere);
1734 # Check the patch for a signoff:
1735 if ($line =~ /^\s*signed-off-by:/i) {
1740 # Check signature styles
1741 if (!$in_header_lines &&
1742 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
1743 my $space_before = $1;
1745 my $space_after = $3;
1747 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1749 if ($sign_off !~ /$signature_tags/) {
1750 WARN
("BAD_SIGN_OFF",
1751 "Non-standard signature: $sign_off\n" . $herecurr);
1753 if (defined $space_before && $space_before ne "") {
1754 if (WARN
("BAD_SIGN_OFF",
1755 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1757 $fixed[$linenr - 1] =
1758 "$ucfirst_sign_off $email";
1761 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1762 if (WARN
("BAD_SIGN_OFF",
1763 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1765 $fixed[$linenr - 1] =
1766 "$ucfirst_sign_off $email";
1770 if (!defined $space_after || $space_after ne " ") {
1771 if (WARN
("BAD_SIGN_OFF",
1772 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1774 $fixed[$linenr - 1] =
1775 "$ucfirst_sign_off $email";
1779 my ($email_name, $email_address, $comment) = parse_email
($email);
1780 my $suggested_email = format_email
(($email_name, $email_address));
1781 if ($suggested_email eq "") {
1782 ERROR
("BAD_SIGN_OFF",
1783 "Unrecognized email address: '$email'\n" . $herecurr);
1785 my $dequoted = $suggested_email;
1786 $dequoted =~ s/^"//;
1787 $dequoted =~ s/" </ </;
1788 # Don't force email to have quotes
1789 # Allow just an angle bracketed address
1790 if ("$dequoted$comment" ne $email &&
1791 "<$email_address>$comment" ne $email &&
1792 "$suggested_email$comment" ne $email) {
1793 WARN
("BAD_SIGN_OFF",
1794 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
1798 # Check for duplicate signatures
1799 my $sig_nospace = $line;
1800 $sig_nospace =~ s/\s//g;
1801 $sig_nospace = lc($sig_nospace);
1802 if (defined $signatures{$sig_nospace}) {
1803 WARN
("BAD_SIGN_OFF",
1804 "Duplicate signature\n" . $herecurr);
1806 $signatures{$sig_nospace} = 1;
1810 # Check for wrappage within a valid hunk of the file
1811 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1812 ERROR
("CORRUPTED_PATCH",
1813 "patch seems to be corrupt (line wrapped?)\n" .
1814 $herecurr) if (!$emitted_corrupt++);
1817 # Check for absolute kernel paths.
1819 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1822 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1823 check_absolute_file
($1, $herecurr)) {
1826 check_absolute_file
($file, $herecurr);
1831 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1832 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1833 $rawline !~ m/^$UTF8*$/) {
1834 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1836 my $blank = copy_spacing
($rawline);
1837 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1838 my $hereptr = "$hereline$ptr\n";
1841 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1844 # Check if it's the start of a commit log
1845 # (not a header line and we haven't seen the patch filename)
1846 if ($in_header_lines && $realfile =~ /^$/ &&
1847 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
1848 $in_header_lines = 0;
1852 # Check if there is UTF-8 in a commit log when a mail header has explicitly
1853 # declined it, i.e defined some charset where it is missing.
1854 if ($in_header_lines &&
1855 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1857 $non_utf8_charset = 1;
1860 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
1861 $rawline =~ /$NON_ASCII_UTF8/) {
1862 WARN
("UTF8_BEFORE_PATCH",
1863 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1866 # ignore non-hunk lines and lines being removed
1867 next if (!$hunk_line || $line =~ /^-/);
1869 #trailing whitespace
1870 if ($line =~ /^\+.*\015/) {
1871 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1872 if (ERROR
("DOS_LINE_ENDINGS",
1873 "DOS line endings\n" . $herevet) &&
1875 $fixed[$linenr - 1] =~ s/[\s\015]+$//;
1877 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1878 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1879 if (ERROR
("TRAILING_WHITESPACE",
1880 "trailing whitespace\n" . $herevet) &&
1882 $fixed[$linenr - 1] =~ s/\s+$//;
1888 # check for Kconfig help text having a real description
1889 # Only applies when adding the entry originally, after that we do not have
1890 # sufficient context to determine whether it is indeed long enough.
1891 if ($realfile =~ /Kconfig/ &&
1892 $line =~ /.\s*config\s+/) {
1895 my $ln = $linenr + 1;
1899 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
1900 $f = $lines[$ln - 1];
1901 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1902 $is_end = $lines[$ln - 1] =~ /^\+/;
1904 next if ($f =~ /^-/);
1906 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1908 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1915 next if ($f =~ /^$/);
1916 if ($f =~ /^\s*config\s/) {
1922 WARN
("CONFIG_DESCRIPTION",
1923 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1924 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
1927 # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
1928 if ($realfile =~ /Kconfig/ &&
1929 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
1930 WARN
("CONFIG_EXPERIMENTAL",
1931 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1934 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1935 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1938 'EXTRA_AFLAGS' => 'asflags-y',
1939 'EXTRA_CFLAGS' => 'ccflags-y',
1940 'EXTRA_CPPFLAGS' => 'cppflags-y',
1941 'EXTRA_LDFLAGS' => 'ldflags-y',
1944 WARN
("DEPRECATED_VARIABLE",
1945 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1948 # check we are in a valid source file if not then ignore this hunk
1949 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1952 if ($line =~ /^\+/ && $prevrawline !~ /\/\
*\
*/ &&
1953 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1954 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
1955 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1956 $length > $max_line_length)
1959 "line over $max_line_length characters\n" . $herecurr);
1962 # Check for user-visible strings broken across lines, which breaks the ability
1963 # to grep for the string. Limited to strings used as parameters (those
1964 # following an open parenthesis), which almost completely eliminates false
1965 # positives, as well as warning only once per parameter rather than once per
1966 # line of the string. Make an exception when the previous string ends in a
1967 # newline (multiple lines in one string constant) or \n\t (common in inline
1968 # assembly to indent the instruction on the following line).
1969 if ($line =~ /^\+\s*"/ &&
1970 $prevline =~ /"\s*$/ &&
1971 $prevline =~ /\(/ &&
1972 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1973 WARN
("SPLIT_STRING",
1974 "quoted string split across lines\n" . $hereprev);
1977 # check for spaces before a quoted newline
1978 if ($rawline =~ /^.*\".*\s\\n/) {
1979 if (WARN
("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1980 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
1982 $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
1987 # check for adding lines without a newline.
1988 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1989 WARN
("MISSING_EOF_NEWLINE",
1990 "adding a line without newline at end of file\n" . $herecurr);
1993 # Blackfin: use hi/lo macros
1994 if ($realfile =~ m
@arch/blackfin/.*\
.S
$@
) {
1995 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1996 my $herevet = "$here\n" . cat_vet
($line) . "\n";
1998 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
2000 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2001 my $herevet = "$here\n" . cat_vet
($line) . "\n";
2003 "use the HI() macro, not (... >> 16)\n" . $herevet);
2007 # check we are in a valid source file C or perl if not then ignore this hunk
2008 next if ($realfile !~ /\.(h|c|pl)$/);
2010 # at the beginning of a line any tabs must come first and anything
2011 # more than 8 must use tabs.
2012 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2013 $rawline =~ /^\+\s* \s*/) {
2014 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2016 if (ERROR
("CODE_INDENT",
2017 "code indent should use tabs where possible\n" . $herevet) &&
2019 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2023 # check for space before tabs.
2024 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2025 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2026 if (WARN
("SPACE_BEFORE_TAB",
2027 "please, no space before tabs\n" . $herevet) &&
2029 $fixed[$linenr - 1] =~
2030 s/(^\+.*) +\t/$1\t/;
2034 # check for && or || at the start of a line
2035 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2036 CHK
("LOGICAL_CONTINUATIONS",
2037 "Logical continuations should be on the previous line\n" . $hereprev);
2040 # check multi-line statement indentation matches previous line
2041 if ($^V
&& $^V
ge 5.10.0 &&
2042 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2043 $prevline =~ /^\+(\t*)(.*)$/;
2047 my $pos = pos_last_openparen
($rest);
2049 $line =~ /^(\+| )([ \t]*)/;
2052 my $goodtabindent = $oldindent .
2055 my $goodspaceindent = $oldindent . " " x
$pos;
2057 if ($newindent ne $goodtabindent &&
2058 $newindent ne $goodspaceindent) {
2060 if (CHK
("PARENTHESIS_ALIGNMENT",
2061 "Alignment should match open parenthesis\n" . $hereprev) &&
2062 $fix && $line =~ /^\+/) {
2063 $fixed[$linenr - 1] =~
2064 s/^\+[ \t]*/\+$goodtabindent/;
2070 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
2072 "No space is necessary after a cast\n" . $hereprev) &&
2074 $fixed[$linenr - 1] =~
2075 s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
2079 if ($realfile =~ m@
^(drivers
/net
/|net
/)@
&&
2080 $prevrawline =~ /^\+[ \t]*\/\
*[ \t]*$/ &&
2081 $rawline =~ /^\+[ \t]*\*/) {
2082 WARN
("NETWORKING_BLOCK_COMMENT_STYLE",
2083 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2086 if ($realfile =~ m@
^(drivers
/net
/|net
/)@
&&
2087 $prevrawline =~ /^\+[ \t]*\/\
*/ && #starting /*
2088 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
2089 $rawline =~ /^\+/ && #line is new
2090 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2091 WARN
("NETWORKING_BLOCK_COMMENT_STYLE",
2092 "networking block comments start with * on subsequent lines\n" . $hereprev);
2095 if ($realfile =~ m@
^(drivers
/net
/|net
/)@
&&
2096 $rawline !~ m@
^\
+[ \t]*\
*/[ \t]*$@ && #trailing */
2097 $rawline !~ m@
^\
+.*/\*.*\*/[ \t]*$@
&& #inline /*...*/
2098 $rawline !~ m@
^\
+.*\
*{2,}/[ \t]*$@ && #trailing **/
2099 $rawline =~ m@
^\
+[ \t]*.+\
*\
/[ \t]*$@) { #non blank */
2100 WARN
("NETWORKING_BLOCK_COMMENT_STYLE",
2101 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2104 # check for spaces at the beginning of a line.
2106 # 1) within comments
2107 # 2) indented preprocessor commands
2109 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
2110 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2111 if (WARN
("LEADING_SPACE",
2112 "please, no spaces at the start of a line\n" . $herevet) &&
2114 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2118 # check we are in a valid C source file if not then ignore this hunk
2119 next if ($realfile !~ /\.(h|c)$/);
2121 # discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2122 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2123 WARN
("CONFIG_EXPERIMENTAL",
2124 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2127 # check for RCS/CVS revision markers
2128 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2130 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2133 # Blackfin: don't use __builtin_bfin_[cs]sync
2134 if ($line =~ /__builtin_bfin_csync/) {
2135 my $herevet = "$here\n" . cat_vet
($line) . "\n";
2137 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
2139 if ($line =~ /__builtin_bfin_ssync/) {
2140 my $herevet = "$here\n" . cat_vet
($line) . "\n";
2142 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
2145 # check for old HOTPLUG __dev<foo> section markings
2146 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2147 WARN
("HOTPLUG_SECTION",
2148 "Using $1 is unnecessary\n" . $herecurr);
2151 # Check for potential 'bare' types
2152 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2154 #print "LINE<$line>\n";
2155 if ($linenr >= $suppress_statement &&
2156 $realcnt && $line =~ /.\s*\S/) {
2157 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2158 ctx_statement_block
($linenr, $realcnt, 0);
2159 $stat =~ s/\n./\n /g;
2160 $cond =~ s/\n./\n /g;
2162 #print "linenr<$linenr> <$stat>\n";
2163 # If this statement has no statement boundaries within
2164 # it there is no point in retrying a statement scan
2165 # until we hit end of it.
2166 my $frag = $stat; $frag =~ s/;+\s*$//;
2167 if ($frag !~ /(?:{|;)/) {
2168 #print "skip<$line_nr_next>\n";
2169 $suppress_statement = $line_nr_next;
2172 # Find the real next line.
2173 $realline_next = $line_nr_next;
2174 if (defined $realline_next &&
2175 (!defined $lines[$realline_next - 1] ||
2176 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2183 # Ignore goto labels.
2184 if ($s =~ /$Ident:\*$/s) {
2186 # Ignore functions being called
2187 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2189 } elsif ($s =~ /^.\s*else\b/s) {
2191 # declarations always start with types
2192 } 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) {
2195 possible
($type, "A:" . $s);
2197 # definitions in global scope can only start with types
2198 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2199 possible
($1, "B:" . $s);
2202 # any (foo ... *) is a pointer cast, and foo is a type
2203 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2204 possible
($1, "C:" . $s);
2207 # Check for any sort of function declaration.
2208 # int foo(something bar, other baz);
2209 # void (*store_gdt)(x86_descr_ptr *);
2210 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
2211 my ($name_len) = length($1);
2214 substr($ctx, 0, $name_len + 1, '');
2215 $ctx =~ s/\)[^\)]*$//;
2217 for my $arg (split(/\s*,\s*/, $ctx)) {
2218 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
2220 possible
($1, "D:" . $s);
2228 # Checks which may be anchored in the context.
2231 # Check for switch () and associated case and default
2232 # statements should be at the same indent.
2233 if ($line=~/\bswitch\s*\(.*\)/) {
2236 my @ctx = ctx_block_outer
($linenr, $realcnt);
2238 for my $ctx (@ctx) {
2239 my ($clen, $cindent) = line_stats
($ctx);
2240 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2241 $indent != $cindent) {
2242 $err .= "$sep$ctx\n";
2249 ERROR
("SWITCH_CASE_INDENT_LEVEL",
2250 "switch and case should be at the same indent\n$hereline$err");
2254 # if/while/etc brace do not go on next line, unless defining a do while loop,
2255 # or if that brace on the next line is for something else
2256 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2257 my $pre_ctx = "$1$2";
2259 my ($level, @ctx) = ctx_statement_level
($linenr, $realcnt, 0);
2261 if ($line =~ /^\+\t{6,}/) {
2262 WARN
("DEEP_INDENTATION",
2263 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2266 my $ctx_cnt = $realcnt - $#ctx - 1;
2267 my $ctx = join("\n", @ctx);
2269 my $ctx_ln = $linenr;
2270 my $ctx_skip = $realcnt;
2272 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2273 defined $lines[$ctx_ln - 1] &&
2274 $lines[$ctx_ln - 1] =~ /^-/)) {
2275 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2276 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2280 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2281 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2283 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2285 "that open brace { should be on the previous line\n" .
2286 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2288 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2289 $ctx =~ /\)\s*\;\s*$/ &&
2290 defined $lines[$ctx_ln - 1])
2292 my ($nlength, $nindent) = line_stats
($lines[$ctx_ln - 1]);
2293 if ($nindent > $indent) {
2294 WARN
("TRAILING_SEMICOLON",
2295 "trailing semicolon indicates no statements, indent implies otherwise\n" .
2296 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2301 # Check relative indent for conditionals and blocks.
2302 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
2303 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2304 ctx_statement_block
($linenr, $realcnt, 0)
2305 if (!defined $stat);
2306 my ($s, $c) = ($stat, $cond);
2308 substr($s, 0, length($c), '');
2310 # Make sure we remove the line prefixes as we have
2311 # none on the first line, and are going to readd them
2315 # Find out how long the conditional actually is.
2316 my @newlines = ($c =~ /\n/gs);
2317 my $cond_lines = 1 + $#newlines;
2319 # We want to check the first line inside the block
2320 # starting at the end of the conditional, so remove:
2321 # 1) any blank line termination
2322 # 2) any opening brace { on end of the line
2324 my $continuation = 0;
2326 $s =~ s/^.*\bdo\b//;
2328 if ($s =~ s/^\s*\\//) {
2331 if ($s =~ s/^\s*?\n//) {
2336 # Also ignore a loop construct at the end of a
2337 # preprocessor statement.
2338 if (($prevline =~ /^.\s*#\s*define\s/ ||
2339 $prevline =~ /\\\s*$/) && $continuation == 0) {
2345 while ($cond_ptr != $cond_lines) {
2346 $cond_ptr = $cond_lines;
2348 # If we see an #else/#elif then the code
2350 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2355 # 1) blank lines, they should be at 0,
2356 # 2) preprocessor lines, and
2358 if ($continuation ||
2360 $s =~ /^\s*#\s*?/ ||
2361 $s =~ /^\s*$Ident\s*:/) {
2362 $continuation = ($s =~ /^.*?\\\n/) ?
1 : 0;
2363 if ($s =~ s/^.*?\n//) {
2369 my (undef, $sindent) = line_stats
("+" . $s);
2370 my $stat_real = raw_line
($linenr, $cond_lines);
2372 # Check if either of these lines are modified, else
2373 # this is not this patch's fault.
2374 if (!defined($stat_real) ||
2375 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2378 if (defined($stat_real) && $cond_lines > 1) {
2379 $stat_real = "[...]\n$stat_real";
2382 #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";
2384 if ($check && (($sindent % 8) != 0 ||
2385 ($sindent <= $indent && $s ne ''))) {
2386 WARN
("SUSPECT_CODE_INDENT",
2387 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
2391 # Track the 'values' across context and added lines.
2392 my $opline = $line; $opline =~ s/^./ /;
2393 my ($curr_values, $curr_vars) =
2394 annotate_values
($opline . "\n", $prev_values);
2395 $curr_values = $prev_values . $curr_values;
2397 my $outline = $opline; $outline =~ s/\t/ /g;
2398 print "$linenr > .$outline\n";
2399 print "$linenr > $curr_values\n";
2400 print "$linenr > $curr_vars\n";
2402 $prev_values = substr($curr_values, -1);
2404 #ignore lines not being added
2405 next if ($line =~ /^[^\+]/);
2407 # TEST: allow direct testing of the type matcher.
2409 if ($line =~ /^.\s*$Declare\s*$/) {
2411 "TEST: is type\n" . $herecurr);
2412 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2413 ERROR
("TEST_NOT_TYPE",
2414 "TEST: is not type ($1 is)\n". $herecurr);
2418 # TEST: allow direct testing of the attribute matcher.
2420 if ($line =~ /^.\s*$Modifier\s*$/) {
2422 "TEST: is attr\n" . $herecurr);
2423 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2424 ERROR
("TEST_NOT_ATTR",
2425 "TEST: is not attr ($1 is)\n". $herecurr);
2430 # check for initialisation to aggregates open brace on the next line
2431 if ($line =~ /^.\s*{/ &&
2432 $prevline =~ /(?:^|[^=])=\s*$/) {
2434 "that open brace { should be on the previous line\n" . $hereprev);
2438 # Checks which are anchored on the added line.
2441 # check for malformed paths in #include statements (uses RAW line)
2442 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2444 if ($path =~ m{//}) {
2445 ERROR
("MALFORMED_INCLUDE",
2446 "malformed #include filename\n" . $herecurr);
2448 if ($path =~ "^uapi/" && $realfile =~ m@
\binclude
/uapi
/@
) {
2449 ERROR
("UAPI_INCLUDE",
2450 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2454 # no C99 // comments
2455 if ($line =~ m{//}) {
2456 if (ERROR
("C99_COMMENTS",
2457 "do not use C99 // comments\n" . $herecurr) &&
2459 my $line = $fixed[$linenr - 1];
2460 if ($line =~ /\/\
/(.*)$/) {
2461 my $comment = trim
($1);
2462 $fixed[$linenr - 1] =~ s@\
/\/(.*)$@
/\* $comment \*/@
;
2466 # Remove C99 comments.
2468 $opline =~ s@
//.*@@
;
2470 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2471 # the whole statement.
2472 #print "APW <$lines[$realline_next - 1]>\n";
2473 if (defined $realline_next &&
2474 exists $lines[$realline_next - 1] &&
2475 !defined $suppress_export{$realline_next} &&
2476 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2477 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2478 # Handle definitions which produce identifiers with
2481 # EXPORT_SYMBOL(something_foo);
2483 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
2484 $name =~ /^${Ident}_$2/) {
2485 #print "FOO C name<$name>\n";
2486 $suppress_export{$realline_next} = 1;
2488 } elsif ($stat !~ /(?
:
2490 ^.DEFINE_
$Ident\
(\Q
$name\E\
)|
2491 ^.DECLARE_
$Ident\
(\Q
$name\E\
)|
2492 ^.LIST_HEAD\
(\Q
$name\E\
)|
2493 ^.(?
:$Storage\s
+)?
$Type\s
*\
(\s
*\
*\s
*\Q
$name\E\s
*\
)\s
*\
(|
2494 \b\Q
$name\E
(?
:\s
+$Attribute)*\s
*(?
:;|=|\
[|\
()
2496 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2497 $suppress_export{$realline_next} = 2;
2499 $suppress_export{$realline_next} = 1;
2502 if (!defined $suppress_export{$linenr} &&
2503 $prevline =~ /^.\s*$/ &&
2504 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2505 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2506 #print "FOO B <$lines[$linenr - 1]>\n";
2507 $suppress_export{$linenr} = 2;
2509 if (defined $suppress_export{$linenr} &&
2510 $suppress_export{$linenr} == 2) {
2511 WARN
("EXPORT_SYMBOL",
2512 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
2515 # check for global initialisers.
2516 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2517 if (ERROR
("GLOBAL_INITIALISERS",
2518 "do not initialise globals to 0 or NULL\n" .
2521 $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2524 # check for static initialisers.
2525 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2526 if (ERROR
("INITIALISED_STATIC",
2527 "do not initialise statics to 0 or NULL\n" .
2530 $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2534 # check for static const char * arrays.
2535 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2536 WARN
("STATIC_CONST_CHAR_ARRAY",
2537 "static const char * array should probably be static const char * const\n" .
2541 # check for static char foo[] = "bar" declarations.
2542 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2543 WARN
("STATIC_CONST_CHAR_ARRAY",
2544 "static char array declaration should probably be static const char\n" .
2548 # check for declarations of struct pci_device_id
2549 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2550 WARN
("DEFINE_PCI_DEVICE_TABLE",
2551 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
2554 # check for new typedefs, only function parameters and sparse annotations
2556 if ($line =~ /\btypedef\s/ &&
2557 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2558 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
2559 $line !~ /\b$typeTypedefs\b/ &&
2560 $line !~ /\b__bitwise(?:__|)\b/) {
2561 WARN
("NEW_TYPEDEFS",
2562 "do not add new typedefs\n" . $herecurr);
2565 # * goes on variable not on type
2567 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2569 my ($ident, $from, $to) = ($1, $2, $2);
2571 # Should start with a space.
2572 $to =~ s/^(\S)/ $1/;
2573 # Should not end with a space.
2575 # '*'s should not have spaces between.
2576 while ($to =~ s/\*\s+\*/\*\*/) {
2579 ## print "1: from<$from> to<$to> ident<$ident>\n";
2581 if (ERROR
("POINTER_LOCATION",
2582 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
2584 my $sub_from = $ident;
2585 my $sub_to = $ident;
2586 $sub_to =~ s/\Q$from\E/$to/;
2587 $fixed[$linenr - 1] =~
2588 s@\Q
$sub_from\E@
$sub_to@
;
2592 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2594 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
2596 # Should start with a space.
2597 $to =~ s/^(\S)/ $1/;
2598 # Should not end with a space.
2600 # '*'s should not have spaces between.
2601 while ($to =~ s/\*\s+\*/\*\*/) {
2603 # Modifiers should have spaces.
2604 $to =~ s/(\b$Modifier$)/$1 /;
2606 ## print "2: from<$from> to<$to> ident<$ident>\n";
2607 if ($from ne $to && $ident !~ /^$Modifier$/) {
2608 if (ERROR
("POINTER_LOCATION",
2609 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
2612 my $sub_from = $match;
2613 my $sub_to = $match;
2614 $sub_to =~ s/\Q$from\E/$to/;
2615 $fixed[$linenr - 1] =~
2616 s@\Q
$sub_from\E@
$sub_to@
;
2621 # # no BUG() or BUG_ON()
2622 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
2623 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2624 # print "$herecurr";
2628 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2629 WARN
("LINUX_VERSION_CODE",
2630 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
2633 # check for uses of printk_ratelimit
2634 if ($line =~ /\bprintk_ratelimit\s*\(/) {
2635 WARN
("PRINTK_RATELIMITED",
2636 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
2639 # printk should use KERN_* levels. Note that follow on printk's on the
2640 # same line do not need a level, so we use the current block context
2641 # to try and find and validate the current printk. In summary the current
2642 # printk includes all preceding printk's which have no newline on the end.
2643 # we assume the first bad printk is the one to report.
2644 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
2646 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2647 #print "CHECK<$lines[$ln - 1]\n";
2648 # we have a preceding printk if it ends
2649 # with "\n" ignore it, else it is to blame
2650 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2651 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2658 WARN
("PRINTK_WITHOUT_KERN_LEVEL",
2659 "printk() should include KERN_ facility level\n" . $herecurr);
2663 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2665 my $level = lc($orig);
2666 $level = "warn" if ($level eq "warning");
2667 my $level2 = $level;
2668 $level2 = "dbg" if ($level eq "debug");
2669 WARN
("PREFER_PR_LEVEL",
2670 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
2673 if ($line =~ /\bpr_warning\s*\(/) {
2674 if (WARN
("PREFER_PR_LEVEL",
2675 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
2677 $fixed[$linenr - 1] =~
2678 s/\bpr_warning\b/pr_warn/;
2682 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2684 my $level = lc($orig);
2685 $level = "warn" if ($level eq "warning");
2686 $level = "dbg" if ($level eq "debug");
2687 WARN
("PREFER_DEV_LEVEL",
2688 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2691 # function brace can't be on same line, except for #defines of do while,
2692 # or if closed on same line
2693 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2694 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2696 "open brace '{' following function declarations go on the next line\n" . $herecurr);
2699 # open braces for enum, union and struct go on the same line.
2700 if ($line =~ /^.\s*{/ &&
2701 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2703 "open brace '{' following $1 go on the same line\n" . $hereprev);
2706 # missing space after union, struct or enum definition
2707 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2709 "missing space after $1 definition\n" . $herecurr) &&
2711 $fixed[$linenr - 1] =~
2712 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
2716 # check for spacing round square brackets; allowed:
2717 # 1. with a type on the left -- int [] a;
2718 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2719 # 3. inside a curly brace -- = { [0...10] = 5 }
2720 while ($line =~ /(.*?\s)\[/g) {
2721 my ($where, $prefix) = ($-[1], $1);
2722 if ($prefix !~ /$Type\s+$/ &&
2723 ($where != 0 || $prefix !~ /^.\s+$/) &&
2724 $prefix !~ /[{,]\s+$/) {
2725 if (ERROR
("BRACKET_SPACE",
2726 "space prohibited before open square bracket '['\n" . $herecurr) &&
2728 $fixed[$linenr - 1] =~
2729 s/^(\+.*?)\s+\[/$1\[/;
2734 # check for spaces between functions and their parentheses.
2735 while ($line =~ /($Ident)\s+\(/g) {
2737 my $ctx_before = substr($line, 0, $-[1]);
2738 my $ctx = "$ctx_before$name";
2740 # Ignore those directives where spaces _are_ permitted.
2742 if|for|while|switch
|return|case
|
2743 volatile
|__volatile__
|
2744 __attribute__
|format
|__extension__
|
2747 # cpp #define statements have non-optional spaces, ie
2748 # if there is a space between the name and the open
2749 # parenthesis it is simply not a parameter group.
2750 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2752 # cpp #elif statement condition may start with a (
2753 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2755 # If this whole things ends with a type its most
2756 # likely a typedef for a function.
2757 } elsif ($ctx =~ /$Type$/) {
2761 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
2763 $fixed[$linenr - 1] =~
2764 s/\b$name\s+\(/$name\(/;
2769 # Check operator spacing.
2770 if (!($line=~/\#\s*include/)) {
2771 my $fixed_line = "";
2775 <<=|>>=|<=|>=|==|!=|
2776 \
+=|-=|\
*=|\
/=|%=|\
^=|\
|=|&=|
2777 =>|->|<<|>>|<|>|=|!|~|
2778 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%|
2781 my @elements = split(/($ops|;)/, $opline);
2783 ## print("element count: <" . $#elements . ">\n");
2784 ## foreach my $el (@elements) {
2785 ## print("el: <$el>\n");
2788 my @fix_elements = ();
2791 foreach my $el (@elements) {
2792 push(@fix_elements, substr($rawline, $off, length($el)));
2793 $off += length($el);
2798 my $blank = copy_spacing
($opline);
2800 for (my $n = 0; $n < $#elements; $n += 2) {
2802 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
2804 ## print("n: <$n> good: <$good>\n");
2806 $off += length($elements[$n]);
2808 # Pick up the preceding and succeeding characters.
2809 my $ca = substr($opline, 0, $off);
2811 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2812 $cc = substr($opline, $off + length($elements[$n + 1]));
2814 my $cb = "$ca$;$cc";
2817 $a = 'V' if ($elements[$n] ne '');
2818 $a = 'W' if ($elements[$n] =~ /\s$/);
2819 $a = 'C' if ($elements[$n] =~ /$;$/);
2820 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2821 $a = 'O' if ($elements[$n] eq '');
2822 $a = 'E' if ($ca =~ /^\s*$/);
2824 my $op = $elements[$n + 1];
2827 if (defined $elements[$n + 2]) {
2828 $c = 'V' if ($elements[$n + 2] ne '');
2829 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2830 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2831 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2832 $c = 'O' if ($elements[$n + 2] eq '');
2833 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2838 my $ctx = "${a}x${c}";
2840 my $at = "(ctx:$ctx)";
2842 my $ptr = substr($blank, 0, $off) . "^";
2843 my $hereptr = "$hereline$ptr\n";
2845 # Pull out the value of this operator.
2846 my $op_type = substr($curr_values, $off + 1, 1);
2848 # Get the full operator variant.
2849 my $opv = $op . substr($curr_vars, $off, 1);
2851 # Ignore operators passed as parameters.
2852 if ($op_type ne 'V' &&
2853 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2856 # } elsif ($op =~ /^$;+$/) {
2858 # ; should have either the end of line or a space or \ after it
2859 } elsif ($op eq ';') {
2860 if ($ctx !~ /.x[WEBC]/ &&
2861 $cc !~ /^\\/ && $cc !~ /^;/) {
2862 if (ERROR
("SPACING",
2863 "space required after that '$op' $at\n" . $hereptr)) {
2864 $good = trim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]) . " ";
2870 } elsif ($op eq '//') {
2874 # : when part of a bitfield
2875 } elsif ($op eq '->' || $opv eq ':B') {
2876 if ($ctx =~ /Wx.|.xW/) {
2877 if (ERROR
("SPACING",
2878 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
2879 $good = trim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
2881 if (defined $fix_elements[$n + 2]) {
2882 $fix_elements[$n + 2] =~ s/^\s+//;
2887 # , must have a space on the right.
2888 } elsif ($op eq ',') {
2889 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2890 if (ERROR
("SPACING",
2891 "space required after that '$op' $at\n" . $hereptr)) {
2892 $good = trim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]) . " ";
2897 # '*' as part of a type definition -- reported already.
2898 } elsif ($opv eq '*_') {
2899 #warn "'*' is part of type\n";
2901 # unary operators should have a space before and
2902 # none after. May be left adjacent to another
2903 # unary operator, or a cast
2904 } elsif ($op eq '!' || $op eq '~' ||
2905 $opv eq '*U' || $opv eq '-U' ||
2906 $opv eq '&U' || $opv eq '&&U') {
2907 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2908 if (ERROR
("SPACING",
2909 "space required before that '$op' $at\n" . $hereptr)) {
2910 $good = trim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]);
2914 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2915 # A unary '*' may be const
2917 } elsif ($ctx =~ /.xW/) {
2918 if (ERROR
("SPACING",
2919 "space prohibited after that '$op' $at\n" . $hereptr)) {
2920 $fixed_line =~ s/\s+$//;
2921 $good = trim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
2923 if (defined $fix_elements[$n + 2]) {
2924 $fix_elements[$n + 2] =~ s/^\s+//;
2929 # unary ++ and unary -- are allowed no space on one side.
2930 } elsif ($op eq '++' or $op eq '--') {
2931 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2932 if (ERROR
("SPACING",
2933 "space required one side of that '$op' $at\n" . $hereptr)) {
2934 $fixed_line =~ s/\s+$//;
2935 $good = trim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]) . " ";
2939 if ($ctx =~ /Wx[BE]/ ||
2940 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2941 if (ERROR
("SPACING",
2942 "space prohibited before that '$op' $at\n" . $hereptr)) {
2943 $fixed_line =~ s/\s+$//;
2944 $good = trim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
2948 if ($ctx =~ /ExW/) {
2949 if (ERROR
("SPACING",
2950 "space prohibited after that '$op' $at\n" . $hereptr)) {
2951 $fixed_line =~ s/\s+$//;
2952 $good = trim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
2954 if (defined $fix_elements[$n + 2]) {
2955 $fix_elements[$n + 2] =~ s/^\s+//;
2960 # << and >> may either have or not have spaces both sides
2961 } elsif ($op eq '<<' or $op eq '>>' or
2962 $op eq '&' or $op eq '^' or $op eq '|' or
2963 $op eq '+' or $op eq '-' or
2964 $op eq '*' or $op eq '/' or
2967 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2968 if (ERROR
("SPACING",
2969 "need consistent spacing around '$op' $at\n" . $hereptr)) {
2970 $fixed_line =~ s/\s+$//;
2971 $good = trim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]) . " ";
2976 # A colon needs no spaces before when it is
2977 # terminating a case value or a label.
2978 } elsif ($opv eq ':C' || $opv eq ':L') {
2979 if ($ctx =~ /Wx./) {
2980 if (ERROR
("SPACING",
2981 "space prohibited before that '$op' $at\n" . $hereptr)) {
2982 $good = trim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
2987 # All the others need spaces both sides.
2988 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2991 # Ignore email addresses <foo@bar>
2993 $cc =~ /^\S+\@\S+>/) ||
2995 $ca =~ /<\S+\@\S+$/))
3001 if (($opv eq ':O' && $ca =~ /\?$/) ||
3002 ($op eq '?' && $cc =~ /^:/)) {
3007 if (ERROR
("SPACING",
3008 "spaces required around that '$op' $at\n" . $hereptr)) {
3009 $good = trim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]) . " ";
3010 $good = $fix_elements[$n] . " " . trim
($fix_elements[$n + 1]) . " ";
3015 $off += length($elements[$n + 1]);
3017 ## print("n: <$n> GOOD: <$good>\n");
3019 $fixed_line = $fixed_line . $good;
3022 if (($#elements % 2) == 0) {
3023 $fixed_line = $fixed_line . $fix_elements[$#elements];
3026 if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
3027 $fixed[$linenr - 1] = $fixed_line;
3033 # check for whitespace before a non-naked semicolon
3034 if ($line =~ /^\+.*\S\s+;/) {
3036 "space prohibited before semicolon\n" . $herecurr) &&
3038 1 while $fixed[$linenr - 1] =~
3039 s/^(\+.*\S)\s+;/$1;/;
3043 # check for multiple assignments
3044 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3045 CHK
("MULTIPLE_ASSIGNMENTS",
3046 "multiple assignments should be avoided\n" . $herecurr);
3049 ## # check for multiple declarations, allowing for a function declaration
3051 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3052 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3054 ## # Remove any bracketed sections to ensure we do not
3055 ## # falsly report the parameters of functions.
3057 ## while ($ln =~ s/\([^\(\)]*\)//g) {
3059 ## if ($ln =~ /,/) {
3060 ## WARN("MULTIPLE_DECLARATION",
3061 ## "declaring multiple variables together should be avoided\n" . $herecurr);
3065 #need space before brace following if, while, etc
3066 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3068 if (ERROR
("SPACING",
3069 "space required before the open brace '{'\n" . $herecurr) &&
3071 $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
3075 ## # check for blank lines before declarations
3076 ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3077 ## $prevrawline =~ /^.\s*$/) {
3079 ## "No blank lines before declarations\n" . $hereprev);
3083 # closing brace should have a space following it when it has anything
3085 if ($line =~ /}(?!(?:,|;|\)))\S/) {
3086 if (ERROR
("SPACING",
3087 "space required after that close brace '}'\n" . $herecurr) &&
3089 $fixed[$linenr - 1] =~
3090 s/}((?!(?:,|;|\)))\S)/} $1/;
3094 # check spacing on square brackets
3095 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
3096 if (ERROR
("SPACING",
3097 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3099 $fixed[$linenr - 1] =~
3103 if ($line =~ /\s\]/) {
3104 if (ERROR
("SPACING",
3105 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3107 $fixed[$linenr - 1] =~
3112 # check spacing on parentheses
3113 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3114 $line !~ /for\s*\(\s+;/) {
3115 if (ERROR
("SPACING",
3116 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3118 $fixed[$linenr - 1] =~
3122 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3123 $line !~ /for\s*\(.*;\s+\)/ &&
3124 $line !~ /:\s+\)/) {
3125 if (ERROR
("SPACING",
3126 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3128 $fixed[$linenr - 1] =~
3133 #goto labels aren't indented, allow a single space however
3134 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
3135 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
3136 if (WARN
("INDENTED_LABEL",
3137 "labels should not be indented\n" . $herecurr) &&
3139 $fixed[$linenr - 1] =~
3144 # Return is not a function.
3145 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
3149 # Flatten any parentheses
3150 $value =~ s/\(/ \(/g;
3151 $value =~ s/\)/\) /g;
3152 while ($value =~ s/\[[^\[\]]*\]/1/ ||
3153 $value !~ /(?
:$Ident|-?
$Constant)\s
*
3155 (?
:$Ident|-?
$Constant)/x
&&
3156 $value =~ s/\([^\(\)]*\)/1/) {
3158 #print "value<$value>\n";
3159 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
3160 ERROR
("RETURN_PARENTHESES",
3161 "return is not a function, parentheses are not required\n" . $herecurr);
3163 } elsif ($spacing !~ /\s+/) {
3165 "space required before the open parenthesis '('\n" . $herecurr);
3168 # Return of what appears to be an errno should normally be -'ve
3169 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3171 if ($name ne 'EOF' && $name ne 'ERROR') {
3172 WARN
("USE_NEGATIVE_ERRNO",
3173 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
3177 # Need a space before open parenthesis after if, while etc
3178 if ($line =~ /\b(if|while|for|switch)\(/) {
3179 if (ERROR
("SPACING",
3180 "space required before the open parenthesis '('\n" . $herecurr) &&
3182 $fixed[$linenr - 1] =~
3183 s/\b(if|while|for|switch)\(/$1 \(/;
3187 # Check for illegal assignment in if conditional -- and check for trailing
3188 # statements after the conditional.
3189 if ($line =~ /do\s*(?!{)/) {
3190 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3191 ctx_statement_block
($linenr, $realcnt, 0)
3192 if (!defined $stat);
3193 my ($stat_next) = ctx_statement_block
($line_nr_next,
3194 $remain_next, $off_next);
3195 $stat_next =~ s/\n./\n /g;
3196 ##print "stat<$stat> stat_next<$stat_next>\n";
3198 if ($stat_next =~ /^\s*while\b/) {
3199 # If the statement carries leading newlines,
3200 # then count those as offsets.
3202 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3204 statement_rawlines
($whitespace) - 1;
3206 $suppress_whiletrailers{$line_nr_next +
3210 if (!defined $suppress_whiletrailers{$linenr} &&
3211 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3212 my ($s, $c) = ($stat, $cond);
3214 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3215 ERROR
("ASSIGN_IN_IF",
3216 "do not use assignment in if condition\n" . $herecurr);
3219 # Find out what is on the end of the line after the
3221 substr($s, 0, length($c), '');
3223 $s =~ s/$;//g; # Remove any comments
3224 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3225 $c !~ /}\s*while\s*/)
3227 # Find out how long the conditional actually is.
3228 my @newlines = ($c =~ /\n/gs);
3229 my $cond_lines = 1 + $#newlines;
3232 $stat_real = raw_line
($linenr, $cond_lines)
3233 . "\n" if ($cond_lines);
3234 if (defined($stat_real) && $cond_lines > 1) {
3235 $stat_real = "[...]\n$stat_real";
3238 ERROR
("TRAILING_STATEMENTS",
3239 "trailing statements should be on next line\n" . $herecurr . $stat_real);
3243 # Check for bitwise tests written as boolean
3255 WARN
("HEXADECIMAL_BOOLEAN_TEST",
3256 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
3259 # if and else should not have general statements after it
3260 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3262 $s =~ s/$;//g; # Remove any comments
3263 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3264 ERROR
("TRAILING_STATEMENTS",
3265 "trailing statements should be on next line\n" . $herecurr);
3268 # if should not continue a brace
3269 if ($line =~ /}\s*if\b/) {
3270 ERROR
("TRAILING_STATEMENTS",
3271 "trailing statements should be on next line\n" .
3274 # case and default should not have general statements after them
3275 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3277 (?
:\s
*$;*)(?
:\s
*{)?
(?
:\s
*$;*)(?
:\s
*\\)?\s
*$|
3281 ERROR
("TRAILING_STATEMENTS",
3282 "trailing statements should be on next line\n" . $herecurr);
3285 # Check for }<nl>else {, these must be at the same
3286 # indent level to be relevant to each other.
3287 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3288 $previndent == $indent) {
3289 ERROR
("ELSE_AFTER_BRACE",
3290 "else should follow close brace '}'\n" . $hereprev);
3293 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3294 $previndent == $indent) {
3295 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
3297 # Find out what is on the end of the line after the
3299 substr($s, 0, length($c), '');
3302 if ($s =~ /^\s*;/) {
3303 ERROR
("WHILE_AFTER_BRACE",
3304 "while should follow close brace '}'\n" . $hereprev);
3308 #Specific variable tests
3309 while ($line =~ m{($Constant|$Lval)}g) {
3312 #gcc binary extension
3313 if ($var =~ /^$Binary$/) {
3314 if (WARN
("GCC_BINARY_CONSTANT",
3315 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3317 my $hexval = sprintf("0x%x", oct($var));
3318 $fixed[$linenr - 1] =~
3319 s/\b$var\b/$hexval/;
3324 if ($var !~ /^$Constant$/ &&
3325 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
3326 #Ignore Page<foo> variants
3327 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
3328 #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
3329 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
3330 while ($var =~ m{($Ident)}g) {
3332 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
3333 seed_camelcase_includes
() if ($check);
3334 if (!defined $camelcase{$word}) {
3335 $camelcase{$word} = 1;
3337 "Avoid CamelCase: <$word>\n" . $herecurr);
3343 #no spaces allowed after \ in define
3344 if ($line =~ /\#\s*define.*\\\s+$/) {
3345 if (WARN
("WHITESPACE_AFTER_LINE_CONTINUATION",
3346 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3348 $fixed[$linenr - 1] =~ s/\s+$//;
3352 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
3353 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
3355 my $checkfile = "include/linux/$file";
3356 if (-f
"$root/$checkfile" &&
3357 $realfile ne $checkfile &&
3358 $1 !~ /$allowed_asm_includes/)
3360 if ($realfile =~ m{^arch/}) {
3361 CHK
("ARCH_INCLUDE_LINUX",
3362 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3364 WARN
("INCLUDE_LINUX",
3365 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3370 # multi-statement macros should be enclosed in a do while loop, grab the
3371 # first statement and ensure its the whole macro if its not enclosed
3372 # in a known good container
3373 if ($realfile !~ m@
/vmlinux
.lds
.h
$@
&&
3374 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
3377 my ($off, $dstat, $dcond, $rest);
3379 ($dstat, $dcond, $ln, $cnt, $off) =
3380 ctx_statement_block
($linenr, $realcnt, 0);
3382 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
3383 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
3385 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
3387 $dstat =~ s/\\\n.//g;
3388 $dstat =~ s/^\s*//s;
3389 $dstat =~ s/\s*$//s;
3391 # Flatten any parentheses and braces
3392 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3393 $dstat =~ s/\{[^\{\}]*\}/1/ ||
3394 $dstat =~ s/\[[^\[\]]*\]/1/)
3398 # Flatten any obvious string concatentation.
3399 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3400 $dstat =~ s/$Ident\s*("X*")/$1/)
3404 my $exceptions = qr{
3416 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
3418 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3419 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
3420 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
3421 $dstat !~ /^'X'$/ && # character constants
3422 $dstat !~ /$exceptions/ &&
3423 $dstat !~ /^\.$Ident\s*=/ && # .foo =
3424 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
3425 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
3426 $dstat !~ /^for\s*$Constant$/ && # for (...)
3427 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3428 $dstat !~ /^do\s*{/ && # do {...
3429 $dstat !~ /^\({/) # ({...
3432 my $herectx = $here . "\n";
3433 my $cnt = statement_rawlines
($ctx);
3435 for (my $n = 0; $n < $cnt; $n++) {
3436 $herectx .= raw_line
($linenr, $n) . "\n";
3439 if ($dstat =~ /;/) {
3440 ERROR
("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3441 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3443 ERROR
("COMPLEX_MACRO",
3444 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
3448 # check for line continuations outside of #defines, preprocessor #, and asm
3451 if ($prevline !~ /^..*\\$/ &&
3452 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3453 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
3454 $line =~ /^\+.*\\$/) {
3455 WARN
("LINE_CONTINUATIONS",
3456 "Avoid unnecessary line continuations\n" . $herecurr);
3460 # do {} while (0) macro tests:
3461 # single-statement macros do not need to be enclosed in do while (0) loop,
3462 # macro should not end with a semicolon
3463 if ($^V
&& $^V
ge 5.10.0 &&
3464 $realfile !~ m@
/vmlinux
.lds
.h
$@
&&
3465 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3468 my ($off, $dstat, $dcond, $rest);
3470 ($dstat, $dcond, $ln, $cnt, $off) =
3471 ctx_statement_block
($linenr, $realcnt, 0);
3474 $dstat =~ s/\\\n.//g;
3476 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3481 my $cnt = statement_rawlines
($ctx);
3482 my $herectx = $here . "\n";
3484 for (my $n = 0; $n < $cnt; $n++) {
3485 $herectx .= raw_line
($linenr, $n) . "\n";
3488 if (($stmts =~ tr/;/;/) == 1 &&
3489 $stmts !~ /^\s*(if|while|for|switch)\b/) {
3490 WARN
("SINGLE_STATEMENT_DO_WHILE_MACRO",
3491 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3493 if (defined $semis && $semis ne "") {
3494 WARN
("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3495 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3500 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3501 # all assignments may have only one of the following with an assignment:
3504 # VMLINUX_SYMBOL(...)
3505 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
3506 WARN
("MISSING_VMLINUX_SYMBOL",
3507 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
3510 # check for redundant bracing round if etc
3511 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3512 my ($level, $endln, @chunks) =
3513 ctx_statement_full
($linenr, $realcnt, 1);
3514 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
3515 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3516 if ($#chunks > 0 && $level == 0) {
3520 my $herectx = $here . "\n";
3521 my $ln = $linenr - 1;
3522 for my $chunk (@chunks) {
3523 my ($cond, $block) = @
{$chunk};
3525 # If the condition carries leading newlines, then count those as offsets.
3526 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3527 my $offset = statement_rawlines
($whitespace) - 1;
3529 $allowed[$allow] = 0;
3530 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3532 # We have looked at and allowed this specific line.
3533 $suppress_ifbraces{$ln + $offset} = 1;
3535 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
3536 $ln += statement_rawlines
($block) - 1;
3538 substr($block, 0, length($cond), '');
3540 $seen++ if ($block =~ /^\s*{/);
3542 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
3543 if (statement_lines
($cond) > 1) {
3544 #print "APW: ALLOWED: cond<$cond>\n";
3545 $allowed[$allow] = 1;
3547 if ($block =~/\b(?:if|for|while)\b/) {
3548 #print "APW: ALLOWED: block<$block>\n";
3549 $allowed[$allow] = 1;
3551 if (statement_block_size
($block) > 1) {
3552 #print "APW: ALLOWED: lines block<$block>\n";
3553 $allowed[$allow] = 1;
3558 my $sum_allowed = 0;
3559 foreach (@allowed) {
3562 if ($sum_allowed == 0) {
3564 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3565 } elsif ($sum_allowed != $allow &&
3568 "braces {} should be used on all arms of this statement\n" . $herectx);
3573 if (!defined $suppress_ifbraces{$linenr - 1} &&
3574 $line =~ /\b(if|while|for|else)\b/) {
3577 # Check the pre-context.
3578 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3579 #print "APW: ALLOWED: pre<$1>\n";
3583 my ($level, $endln, @chunks) =
3584 ctx_statement_full
($linenr, $realcnt, $-[0]);
3586 # Check the condition.
3587 my ($cond, $block) = @
{$chunks[0]};
3588 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3589 if (defined $cond) {
3590 substr($block, 0, length($cond), '');
3592 if (statement_lines
($cond) > 1) {
3593 #print "APW: ALLOWED: cond<$cond>\n";
3596 if ($block =~/\b(?:if|for|while)\b/) {
3597 #print "APW: ALLOWED: block<$block>\n";
3600 if (statement_block_size
($block) > 1) {
3601 #print "APW: ALLOWED: lines block<$block>\n";
3604 # Check the post-context.
3605 if (defined $chunks[1]) {
3606 my ($cond, $block) = @
{$chunks[1]};
3607 if (defined $cond) {
3608 substr($block, 0, length($cond), '');
3610 if ($block =~ /^\s*\{/) {
3611 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3615 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
3616 my $herectx = $here . "\n";
3617 my $cnt = statement_rawlines
($block);
3619 for (my $n = 0; $n < $cnt; $n++) {
3620 $herectx .= raw_line
($linenr, $n) . "\n";
3624 "braces {} are not necessary for single statement blocks\n" . $herectx);
3628 # check for unnecessary blank lines around braces
3629 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
3631 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3633 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
3635 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3638 # no volatiles please
3639 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3640 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3642 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
3646 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3647 CHK
("REDUNDANT_CODE",
3648 "if this code is redundant consider removing it\n" .
3652 # check for needless "if (<foo>) fn(<foo>)" uses
3653 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3654 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3655 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3657 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
3661 # prefer usleep_range over udelay
3662 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
3663 # ignore udelay's < 10, however
3666 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
3670 # warn about unexpectedly long msleep's
3671 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3674 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
3678 # check for comparisons of jiffies
3679 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3680 WARN
("JIFFIES_COMPARISON",
3681 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3684 # check for comparisons of get_jiffies_64()
3685 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3686 WARN
("JIFFIES_COMPARISON",
3687 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3690 # warn about #ifdefs in C files
3691 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
3692 # print "#ifdef in C files should be avoided\n";
3693 # print "$herecurr";
3697 # warn about spacing in #ifdefs
3698 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3699 if (ERROR
("SPACING",
3700 "exactly one space required after that #$1\n" . $herecurr) &&
3702 $fixed[$linenr - 1] =~
3703 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
3708 # check for spinlock_t definitions without a comment.
3709 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3710 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
3712 if (!ctx_has_comment
($first_line, $linenr)) {
3713 CHK
("UNCOMMENTED_DEFINITION",
3714 "$1 definition without comment\n" . $herecurr);
3717 # check for memory barriers without a comment.
3718 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3719 if (!ctx_has_comment
($first_line, $linenr)) {
3720 CHK
("MEMORY_BARRIER",
3721 "memory barrier without comment\n" . $herecurr);
3724 # check of hardware specific defines
3725 if ($line =~ m@
^.\s
*\#\s
*if.*\b(__i386__
|__powerpc64__
|__sun__
|__s390x__
)\b@
&& $realfile !~ m
@include/asm
-@
) {
3727 "architecture specific defines should be avoided\n" . $herecurr);
3730 # Check that the storage class is at the beginning of a declaration
3731 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3732 WARN
("STORAGE_CLASS",
3733 "storage class should be at the beginning of the declaration\n" . $herecurr)
3736 # check the location of the inline attribute, that it is between
3737 # storage class and type.
3738 if ($line =~ /\b$Type\s+$Inline\b/ ||
3739 $line =~ /\b$Inline\s+$Storage\b/) {
3740 ERROR
("INLINE_LOCATION",
3741 "inline keyword should sit between storage class and type\n" . $herecurr);
3744 # Check for __inline__ and __inline, prefer inline
3745 if ($line =~ /\b(__inline__|__inline)\b/) {
3747 "plain inline is preferred over $1\n" . $herecurr) &&
3749 $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
3754 # Check for __attribute__ packed, prefer __packed
3755 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3756 WARN
("PREFER_PACKED",
3757 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
3760 # Check for __attribute__ aligned, prefer __aligned
3761 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3762 WARN
("PREFER_ALIGNED",
3763 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
3766 # Check for __attribute__ format(printf, prefer __printf
3767 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3768 if (WARN
("PREFER_PRINTF",
3769 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
3771 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
3776 # Check for __attribute__ format(scanf, prefer __scanf
3777 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3778 if (WARN
("PREFER_SCANF",
3779 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
3781 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
3785 # check for sizeof(&)
3786 if ($line =~ /\bsizeof\s*\(\s*\&/) {
3787 WARN
("SIZEOF_ADDRESS",
3788 "sizeof(& should be avoided\n" . $herecurr);
3791 # check for sizeof without parenthesis
3792 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
3793 if (WARN
("SIZEOF_PARENTHESIS",
3794 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
3796 $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
3800 # check for line continuations in quoted strings with odd counts of "
3801 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3802 WARN
("LINE_CONTINUATIONS",
3803 "Avoid line continuations in quoted strings\n" . $herecurr);
3806 # check for struct spinlock declarations
3807 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3808 WARN
("USE_SPINLOCK_T",
3809 "struct spinlock should be spinlock_t\n" . $herecurr);
3812 # check for seq_printf uses that could be seq_puts
3813 if ($line =~ /\bseq_printf\s*\(/) {
3814 my $fmt = get_quoted_string
($line, $rawline);
3815 if ($fmt !~ /[^\\]\%/) {
3816 if (WARN
("PREFER_SEQ_PUTS",
3817 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
3819 $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
3824 # Check for misused memsets
3825 if ($^V
&& $^V
ge 5.10.0 &&
3827 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3833 if ($ms_size =~ /^(0x|)0$/i) {
3835 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
3836 } elsif ($ms_size =~ /^(0x|)1$/i) {
3838 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3842 # typecasts on min/max could be min_t/max_t
3843 if ($^V
&& $^V
ge 5.10.0 &&
3845 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3846 if (defined $2 || defined $7) {
3848 my $cast1 = deparenthesize
($2);
3850 my $cast2 = deparenthesize
($7);
3854 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
3855 $cast = "$cast1 or $cast2";
3856 } elsif ($cast1 ne "") {
3862 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
3866 # check usleep_range arguments
3867 if ($^V
&& $^V
ge 5.10.0 &&
3869 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
3873 WARN
("USLEEP_RANGE",
3874 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3875 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
3877 WARN
("USLEEP_RANGE",
3878 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3882 # check for new externs in .h files.
3883 if ($realfile =~ /\.h$/ &&
3884 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
3885 if (WARN
("AVOID_EXTERNS",
3886 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
3888 $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
3892 # check for new externs in .c files.
3893 if ($realfile =~ /\.c$/ && defined $stat &&
3894 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
3896 my $function_name = $1;
3897 my $paren_space = $2;
3900 if (defined $cond) {
3901 substr($s, 0, length($cond), '');
3903 if ($s =~ /^\s*;/ &&
3904 $function_name ne 'uninitialized_var')
3906 WARN
("AVOID_EXTERNS",
3907 "externs should be avoided in .c files\n" . $herecurr);
3910 if ($paren_space =~ /\n/) {
3911 WARN
("FUNCTION_ARGUMENTS",
3912 "arguments for function declarations should follow identifier\n" . $herecurr);
3915 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3916 $stat =~ /^.\s*extern\s+/)
3918 WARN
("AVOID_EXTERNS",
3919 "externs should be avoided in .c files\n" . $herecurr);
3922 # checks for new __setup's
3923 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3926 if (!grep(/$name/, @setup_docs)) {
3927 CHK
("UNDOCUMENTED_SETUP",
3928 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
3932 # check for pointless casting of kmalloc return
3933 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
3934 WARN
("UNNECESSARY_CASTS",
3935 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
3939 # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
3940 if ($^V
&& $^V
ge 5.10.0 &&
3941 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
3942 CHK
("ALLOC_SIZEOF_STRUCT",
3943 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
3946 # check for krealloc arg reuse
3947 if ($^V
&& $^V
ge 5.10.0 &&
3948 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
3949 WARN
("KREALLOC_ARG_REUSE",
3950 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
3953 # check for alloc argument mismatch
3954 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
3955 WARN
("ALLOC_ARRAY_ARGS",
3956 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
3959 # check for multiple semicolons
3960 if ($line =~ /;\s*;\s*$/) {
3961 if (WARN
("ONE_SEMICOLON",
3962 "Statements terminations use 1 semicolon\n" . $herecurr) &&
3964 $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
3968 # check for switch/default statements without a break;
3969 if ($^V
&& $^V
ge 5.10.0 &&
3971 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3973 my $herectx = $here . "\n";
3974 my $cnt = statement_rawlines
($stat);
3975 for (my $n = 0; $n < $cnt; $n++) {
3976 $herectx .= raw_line
($linenr, $n) . "\n";
3978 WARN
("DEFAULT_NO_BREAK",
3979 "switch default: should use break\n" . $herectx);
3982 # check for gcc specific __FUNCTION__
3983 if ($line =~ /\b__FUNCTION__\b/) {
3984 if (WARN
("USE_FUNC",
3985 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
3987 $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
3991 # check for use of yield()
3992 if ($line =~ /\byield\s*\(\s*\)/) {
3994 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3997 # check for comparisons against true and false
3998 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4006 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4008 my $type = lc($otype);
4009 if ($type =~ /^(?:true|false)$/) {
4010 if (("$test" eq "==" && "$type" eq "true") ||
4011 ("$test" eq "!=" && "$type" eq "false")) {
4015 CHK
("BOOL_COMPARISON",
4016 "Using comparison to $otype is error prone\n" . $herecurr);
4018 ## maybe suggesting a correct construct would better
4019 ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4024 # check for semaphores initialized locked
4025 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
4026 WARN
("CONSIDER_COMPLETION",
4027 "consider using a completion\n" . $herecurr);
4030 # recommend kstrto* over simple_strto* and strict_strto*
4031 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
4032 WARN
("CONSIDER_KSTRTO",
4033 "$1 is obsolete, use k$3 instead\n" . $herecurr);
4036 # check for __initcall(), use device_initcall() explicitly please
4037 if ($line =~ /^.\s*__initcall\s*\(/) {
4038 WARN
("USE_DEVICE_INITCALL",
4039 "please use device_initcall() instead of __initcall()\n" . $herecurr);
4042 # check for various ops structs, ensure they are const.
4043 my $struct_ops = qr{acpi_dock_ops
|
4044 address_space_operations
|
4046 block_device_operations
|
4051 file_lock_operations
|
4061 lock_manager_operations
|
4067 pipe_buf_operations
|
4068 platform_hibernation_ops
|
4069 platform_suspend_ops
|
4074 soc_pcmcia_socket_ops
|
4080 if ($line !~ /\bconst\b/ &&
4081 $line =~ /\bstruct\s+($struct_ops)\b/) {
4082 WARN
("CONST_STRUCT",
4083 "struct $1 should normally be const\n" .
4087 # use of NR_CPUS is usually wrong
4088 # ignore definitions of NR_CPUS and usage to define arrays as likely right
4089 if ($line =~ /\bNR_CPUS\b/ &&
4090 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4091 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
4092 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4093 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4094 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
4097 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4100 # check for %L{u,d,i} in strings
4102 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4103 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4104 $string =~ s/%%/__/g;
4105 if ($string =~ /(?<!%)%L[udi]/) {
4107 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4112 # whine mightly about in_atomic
4113 if ($line =~ /\bin_atomic\s*\(/) {
4114 if ($realfile =~ m@
^drivers
/@
) {
4116 "do not use in_atomic in drivers\n" . $herecurr);
4117 } elsif ($realfile !~ m@
^kernel
/@
) {
4119 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
4123 # check for lockdep_set_novalidate_class
4124 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
4125 $line =~ /__lockdep_no_validate__\s*\)/ ) {
4126 if ($realfile !~ m@
^kernel
/lockdep@
&&
4127 $realfile !~ m@
^include
/linux/lockdep@
&&
4128 $realfile !~ m@
^drivers
/base/core@
) {
4130 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
4134 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
4135 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
4136 WARN
("EXPORTED_WORLD_WRITABLE",
4137 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
4141 # If we have no input at all, then there is nothing to report on
4142 # so just keep quiet.
4143 if ($#rawlines == -1) {
4147 # In mailback mode only produce a report in the negative, for
4148 # things that appear to be patches.
4149 if ($mailback && ($clean == 1 || !$is_patch)) {
4153 # This is not a patch, and we are are in 'no-patch' mode so
4155 if (!$chk_patch && !$is_patch) {
4160 ERROR
("NOT_UNIFIED_DIFF",
4161 "Does not appear to be a unified-diff format patch\n");
4163 if ($is_patch && $chk_signoff && $signoff == 0) {
4164 ERROR
("MISSING_SIGN_OFF",
4165 "Missing Signed-off-by: line(s)\n");
4168 print report_dump
();
4169 if ($summary && !($clean == 1 && $quiet == 1)) {
4170 print "$filename " if ($summary_file);
4171 print "total: $cnt_error errors, $cnt_warn warnings, " .
4172 (($check)?
"$cnt_chk checks, " : "") .
4173 "$cnt_lines lines checked\n";
4174 print "\n" if ($quiet == 0);
4179 if ($^V
lt 5.10.0) {
4180 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4181 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4184 # If there were whitespace errors which cleanpatch can fix
4185 # then suggest that.
4186 if ($rpt_cleaners) {
4187 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4188 print " scripts/cleanfile\n\n";
4193 if ($quiet == 0 && keys %ignore_type) {
4194 print "NOTE: Ignored message types:";
4195 foreach my $ignore (sort keys %ignore_type) {
4201 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
4202 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
4206 open($f, '>', $newfile)
4207 or die "$P: Can't open $newfile for write\n";
4208 foreach my $fixed_line (@fixed) {
4211 if ($linecount > 3) {
4212 $fixed_line =~ s/^\+//;
4213 print $f $fixed_line. "\n";
4216 print $f $fixed_line . "\n";
4223 Wrote EXPERIMENTAL
--fix correction
(s
) to
'$newfile'
4225 Do _NOT_ trust the results written to this file
.
4226 Do _NOT_ submit these changes without inspecting them
for correctness
.
4228 This EXPERIMENTAL file is simply a convenience to help rewrite patches
.
4229 No warranties
, expressed
or implied
...
4235 if ($clean == 1 && $quiet == 0) {
4236 print "$vname has no obvious style problems and is ready for submission.\n"
4238 if ($clean == 0 && $quiet == 0) {
4240 $vname has style problems
, please review
.
4242 If any of these errors are false positives
, please report
4243 them to the maintainer
, see CHECKPATCH
in MAINTAINERS
.