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);
42 my $configuration_file = ".checkpatch.conf";
43 my $max_line_length = 80;
44 my $ignore_perl_version = 0;
45 my $minimum_perl_version = 5.10.0;
51 Usage
: $P [OPTION
]... [FILE
]...
56 --no-tree run without a kernel tree
57 --no-signoff
do not check
for 'Signed-off-by' line
58 --patch treat FILE as patchfile
(default)
59 --emacs emacs compile window format
60 --terse one line per report
61 -f
, --file treat FILE as regular source file
62 --subjective
, --strict enable more subjective tests
63 --types TYPE
(,TYPE2
...) show only these comma separated message types
64 --ignore TYPE
(,TYPE2
...) ignore various comma separated message types
65 --max
-line
-length=n set the maximum line
length, if exceeded
, warn
66 --show
-types show the message
"types" in the output
67 --root
=PATH PATH to the kernel tree root
68 --no-summary suppress the per
-file summary
69 --mailback only produce a report
in case of warnings
/errors
70 --summary
-file include the filename
in summary
71 --debug KEY
=[0|1] turn on
/off debugging of KEY
, where KEY is one of
72 'values', 'possible', 'type', and 'attr' (default
74 --test
-only
=WORD report only warnings
/errors containing WORD
76 --fix EXPERIMENTAL
- may create horrible results
77 If correctable single
-line errors exist
, create
78 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
79 with potential errors corrected to the preferred
81 --fix
-inplace EXPERIMENTAL
- may create horrible results
82 Is the same as
--fix
, but overwrites the input
83 file
. It
's your fault if there's
no backup
or git
84 --ignore
-perl
-version override checking of perl version
. expect
86 -h
, --help
, --version display this help
and exit
88 When FILE is
- read standard input
.
94 my $conf = which_conf
($configuration_file);
97 open(my $conffile, '<', "$conf")
98 or warn "$P: Can't find a readable $configuration_file file $!\n";
100 while (<$conffile>) {
103 $line =~ s/\s*\n?$//g;
107 next if ($line =~ m/^\s*#/);
108 next if ($line =~ m/^\s*$/);
110 my @words = split(" ", $line);
111 foreach my $word (@words) {
112 last if ($word =~ m/^#/);
113 push (@conf_args, $word);
117 unshift(@ARGV, @conf_args) if @conf_args;
121 'q|quiet+' => \
$quiet,
123 'signoff!' => \
$chk_signoff,
124 'patch!' => \
$chk_patch,
128 'subjective!' => \
$check,
129 'strict!' => \
$check,
130 'ignore=s' => \
@ignore,
132 'show-types!' => \
$show_types,
133 'max-line-length=i' => \
$max_line_length,
135 'summary!' => \
$summary,
136 'mailback!' => \
$mailback,
137 'summary-file!' => \
$summary_file,
139 'fix-inplace!' => \
$fix_inplace,
140 'ignore-perl-version!' => \
$ignore_perl_version,
141 'debug=s' => \
%debug,
142 'test-only=s' => \
$tst_only,
149 $fix = 1 if ($fix_inplace);
150 $check_orig = $check;
154 if ($^V
&& $^V
lt $minimum_perl_version) {
155 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
156 if (!$ignore_perl_version) {
162 print "$P: no input files\n";
166 sub hash_save_array_words
{
167 my ($hashRef, $arrayRef) = @_;
169 my @array = split(/,/, join(',', @
$arrayRef));
170 foreach my $word (@array) {
171 $word =~ s/\s*\n?$//g;
174 $word =~ tr/[a-z]/[A-Z]/;
176 next if ($word =~ m/^\s*#/);
177 next if ($word =~ m/^\s*$/);
183 sub hash_show_words
{
184 my ($hashRef, $prefix) = @_;
186 if ($quiet == 0 && keys %$hashRef) {
187 print "NOTE: $prefix message types:";
188 foreach my $word (sort keys %$hashRef) {
195 hash_save_array_words
(\
%ignore_type, \
@ignore);
196 hash_save_array_words
(\
%use_type, \
@use);
199 my $dbg_possible = 0;
202 for my $key (keys %debug) {
204 eval "\${dbg_$key} = '$debug{$key}';";
208 my $rpt_cleaners = 0;
217 if (!top_of_kernel_tree
($root)) {
218 die "$P: $root: --root does not point at a valid tree\n";
221 if (top_of_kernel_tree
('.')) {
223 } elsif ($0 =~ m@
(.*)/scripts/[^/]*$@
&&
224 top_of_kernel_tree
($1)) {
229 if (!defined $root) {
230 print "Must be run from the top-level dir. of a kernel tree\n";
235 my $emitted_corrupt = 0;
238 [A
-Za
-z_
][A
-Za
-z\d_
]*
239 (?
:\s
*\#\#\s
*[A
-Za
-z_
][A
-Za
-z\d_
]*)*
241 our $Storage = qr{extern|static|asmlinkage};
253 our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
254 our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
255 our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
256 our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
257 our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
259 # Notes to $Attribute:
260 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
280 ____cacheline_aligned
|
281 ____cacheline_aligned_in_smp
|
282 ____cacheline_internodealigned_in_smp
|
286 our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
287 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
288 our $Lval = qr{$Ident(?:$Member)*};
290 our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
291 our $Binary = qr{(?i)0b[01]+$Int_type?};
292 our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
293 our $Int = qr{[0-9]+$Int_type?};
294 our $Octal = qr{0[0-7]+$Int_type?};
295 our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
296 our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
297 our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
298 our $Float = qr{$Float_hex|$Float_dec|$Float_int};
299 our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
300 our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
301 our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
302 our $Arithmetic = qr{\+|-|\*|\/|%};
306 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||$Arithmetic
309 our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
312 our $NonptrTypeMisordered;
313 our $NonptrTypeWithAttr;
317 our $DeclareMisordered;
319 our $NON_ASCII_UTF8 = qr{
320 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
321 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
322 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
323 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
324 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
325 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
326 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
330 [\x09\x0A\x0D\x20-\x7E] # ASCII
334 our $typeTypedefs = qr{(?x
:
335 (?
:__
)?
(?
:u
|s
|be
|le)(?
:8|16|32|64)|
339 our $logFunctions = qr{(?x
:
340 printk
(?
:_ratelimited
|_once
|)|
341 (?
:[a
-z0
-9]+_
){1,2}(?
:printk
|emerg
|alert
|crit
|err
|warning
|warn|notice
|info
|debug
|dbg
|vdbg
|devel
|cont
|WARN
)(?
:_ratelimited
|_once
|)|
342 WARN
(?
:_RATELIMIT
|_ONCE
|)|
345 seq_vprintf
|seq_printf
|seq_puts
348 our $signature_tags = qr{(?xi
:
359 our @typeListMisordered = (
360 qr{char\s+(?:un)?signed},
361 qr{int\s+(?:(?:un)?signed\s+)?short\s},
362 qr{int\s+short(?:\s+(?:un)?signed)},
363 qr{short\s+int(?:\s+(?:un)?signed)},
364 qr{(?:un)?signed\s+int\s+short},
365 qr{short\s+(?:un)?signed},
366 qr{long\s+int\s+(?:un)?signed},
367 qr{int\s+long\s+(?:un)?signed},
368 qr{long\s+(?:un)?signed\s+int},
369 qr{int\s+(?:un)?signed\s+long},
370 qr{int\s+(?:un)?signed},
371 qr{int\s+long\s+long\s+(?:un)?signed},
372 qr{long\s+long\s+int\s+(?:un)?signed},
373 qr{long\s+long\s+(?:un)?signed\s+int},
374 qr{long\s+long\s+(?:un)?signed},
375 qr{long\s+(?:un)?signed},
380 qr{(?:(?:un)?signed\s+)?char},
381 qr{(?:(?:un)?signed\s+)?short\s+int},
382 qr{(?:(?:un)?signed\s+)?short},
383 qr{(?:(?:un)?signed\s+)?int},
384 qr{(?:(?:un)?signed\s+)?long\s+int},
385 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
386 qr{(?:(?:un)?signed\s+)?long\s+long},
387 qr{(?:(?:un)?signed\s+)?long},
396 qr{${Ident}_handler
},
397 qr{${Ident}_handler_fn
},
400 our @typeListWithAttr = (
402 qr{struct\s+$InitAttribute\s+$Ident},
403 qr{union\s+$InitAttribute\s+$Ident},
406 our @modifierList = (
410 our @mode_permission_funcs = (
412 ["module_param_(?:array|named|string)", 4],
413 ["module_param_array_named", 5],
414 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
415 ["proc_create(?:_data|)", 2],
416 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
419 #Create a search pattern for all these functions to speed up a loop below
420 our $mode_perms_search = "";
421 foreach my $entry (@mode_permission_funcs) {
422 $mode_perms_search .= '|' if ($mode_perms_search ne "");
423 $mode_perms_search .= $entry->[0];
426 our $allowed_asm_includes = qr{(?x
:
430 # memory.h: ARM has a custom one
433 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
434 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
435 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
436 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
437 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
439 (?
:$Modifier\s
+|const\s
+)*
441 (?
:typeof
|__typeof__
)\s
*\
([^\
)]*\
)|
445 (?
:\s
+$Modifier|\s
+const
)*
447 $NonptrTypeMisordered = qr{
448 (?
:$Modifier\s
+|const\s
+)*
452 (?
:\s
+$Modifier|\s
+const
)*
454 $NonptrTypeWithAttr = qr{
455 (?
:$Modifier\s
+|const\s
+)*
457 (?
:typeof
|__typeof__
)\s
*\
([^\
)]*\
)|
461 (?
:\s
+$Modifier|\s
+const
)*
465 (?
:(?
:\s
|\
*|\
[\
])+\s
*const
|(?
:\s
|\
*\s
*(?
:const\s
*)?
|\
[\
])+|(?
:\s
*\
[\s
*\
])+)?
466 (?
:\s
+$Inline|\s
+$Modifier)*
468 $TypeMisordered = qr{
469 $NonptrTypeMisordered
470 (?
:(?
:\s
|\
*|\
[\
])+\s
*const
|(?
:\s
|\
*\s
*(?
:const\s
*)?
|\
[\
])+|(?
:\s
*\
[\s
*\
])+)?
471 (?
:\s
+$Inline|\s
+$Modifier)*
473 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
474 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
478 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s
*};
480 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
481 # requires at least perl version v5.10.0
482 # Any use must be runtime checked with $^V
484 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
485 our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s
*};
486 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
488 our $declaration_macros = qr{(?x
:
489 (?
:$Storage\s
+)?
(?
:[A
-Z_
][A
-Z0
-9]*_
){0,2}(?
:DEFINE
|DECLARE
)(?
:_
[A
-Z0
-9]+){1,2}\s
*\
(|
490 (?
:$Storage\s
+)?LIST_HEAD\s
*\
(|
491 (?
:$Storage\s
+)?
${Type
}\s
+uninitialized_var\s
*\
(
496 return "" if (!defined($string));
498 while ($string =~ /^\s*\(.*\)\s*$/) {
499 $string =~ s@
^\s
*\
(\s
*@@
;
500 $string =~ s@\s
*\
)\s
*$@@
;
503 $string =~ s@\s
+@
@g;
508 sub seed_camelcase_file
{
511 return if (!(-f
$file));
515 open(my $include_file, '<', "$file")
516 or warn "$P: Can't read '$file' $!\n";
517 my $text = <$include_file>;
518 close($include_file);
520 my @lines = split('\n', $text);
522 foreach my $line (@lines) {
523 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
524 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
526 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
528 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
534 my $camelcase_seeded = 0;
535 sub seed_camelcase_includes
{
536 return if ($camelcase_seeded);
539 my $camelcase_cache = "";
540 my @include_files = ();
542 $camelcase_seeded = 1;
545 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
546 chomp $git_last_include_commit;
547 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
549 my $last_mod_date = 0;
550 $files = `find $root/include -name "*.h"`;
551 @include_files = split('\n', $files);
552 foreach my $file (@include_files) {
553 my $date = POSIX
::strftime
("%Y%m%d%H%M",
554 localtime((stat $file)[9]));
555 $last_mod_date = $date if ($last_mod_date < $date);
557 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
560 if ($camelcase_cache ne "" && -f
$camelcase_cache) {
561 open(my $camelcase_file, '<', "$camelcase_cache")
562 or warn "$P: Can't read '$camelcase_cache' $!\n";
563 while (<$camelcase_file>) {
567 close($camelcase_file);
573 $files = `git ls-files "include/*.h"`;
574 @include_files = split('\n', $files);
577 foreach my $file (@include_files) {
578 seed_camelcase_file
($file);
581 if ($camelcase_cache ne "") {
582 unlink glob ".checkpatch-camelcase.*";
583 open(my $camelcase_file, '>', "$camelcase_cache")
584 or warn "$P: Can't write '$camelcase_cache' $!\n";
585 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
586 print $camelcase_file ("$_\n");
588 close($camelcase_file);
592 sub git_commit_info
{
593 my ($commit, $id, $desc) = @_;
595 return ($id, $desc) if ((which
("git") eq "") || !(-e
".git"));
597 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
598 $output =~ s/^\s*//gm;
599 my @lines = split("\n", $output);
601 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
602 # Maybe one day convert this block of bash into something that returns
603 # all matching commit ids, but it's very slow...
605 # echo "checking commits $1..."
606 # git rev-list --remotes | grep -i "^$1" |
607 # while read line ; do
608 # git log --format='%H %s' -1 $line |
609 # echo "commit $(cut -c 1-12,41-)"
611 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
613 $id = substr($lines[0], 0, 12);
614 $desc = substr($lines[0], 41);
620 $chk_signoff = 0 if ($file);
625 my @fixed_inserted = ();
626 my @fixed_deleted = ();
630 for my $filename (@ARGV) {
633 open($FILE, '-|', "diff -u /dev/null $filename") ||
634 die "$P: $filename: diff failed - $!\n";
635 } elsif ($filename eq '-') {
636 open($FILE, '<&STDIN');
638 open($FILE, '<', "$filename") ||
639 die "$P: $filename: open failed - $!\n";
641 if ($filename eq '-') {
642 $vname = 'Your patch';
651 if (!process
($filename)) {
657 @fixed_inserted = ();
664 sub top_of_kernel_tree
{
668 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
669 "README", "Documentation", "arch", "include", "drivers",
670 "fs", "init", "ipc", "kernel", "lib", "scripts",
673 foreach my $check (@tree_check) {
674 if (! -e
$root . '/' . $check) {
682 my ($formatted_email) = @_;
688 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
691 $comment = $3 if defined $3;
692 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
694 $comment = $2 if defined $2;
695 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
697 $comment = $2 if defined $2;
698 $formatted_email =~ s/$address.*$//;
699 $name = $formatted_email;
701 $name =~ s/^\"|\"$//g;
702 # If there's a name left after stripping spaces and
703 # leading quotes, and the address doesn't have both
704 # leading and trailing angle brackets, the address
706 # "joe smith joe@smith.com" bad
707 # "joe smith <joe@smith.com" bad
708 if ($name ne "" && $address !~ /^<[^>]+>$/) {
716 $name =~ s/^\"|\"$//g;
717 $address = trim
($address);
718 $address =~ s/^\<|\>$//g;
720 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
721 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
725 return ($name, $address, $comment);
729 my ($name, $address) = @_;
734 $name =~ s/^\"|\"$//g;
735 $address = trim
($address);
737 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
738 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
743 $formatted_email = "$address";
745 $formatted_email = "$name <$address>";
748 return $formatted_email;
754 foreach my $path (split(/:/, $ENV{PATH
})) {
755 if (-e
"$path/$bin") {
766 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
767 if (-e
"$path/$conf") {
768 return "$path/$conf";
780 for my $c (split(//, $str)) {
784 for (; ($n % 8) != 0; $n++) {
796 (my $res = shift) =~ tr/\t/ /c;
803 # Drop the diff line leader and expand tabs
805 $line = expand_tabs
($line);
807 # Pick the indent from the front of the line.
808 my ($white) = ($line =~ /^(\s*)/);
810 return (length($line), length($white));
813 my $sanitise_quote = '';
815 sub sanitise_line_reset
{
816 my ($in_comment) = @_;
819 $sanitise_quote = '*/';
821 $sanitise_quote = '';
834 # Always copy over the diff marker.
835 $res = substr($line, 0, 1);
837 for ($off = 1; $off < length($line); $off++) {
838 $c = substr($line, $off, 1);
840 # Comments we are wacking completly including the begin
841 # and end, all to $;.
842 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
843 $sanitise_quote = '*/';
845 substr($res, $off, 2, "$;$;");
849 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
850 $sanitise_quote = '';
851 substr($res, $off, 2, "$;$;");
855 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
856 $sanitise_quote = '//';
858 substr($res, $off, 2, $sanitise_quote);
863 # A \ in a string means ignore the next character.
864 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
866 substr($res, $off, 2, 'XX');
871 if ($c eq "'" || $c eq '"') {
872 if ($sanitise_quote eq '') {
873 $sanitise_quote = $c;
875 substr($res, $off, 1, $c);
877 } elsif ($sanitise_quote eq $c) {
878 $sanitise_quote = '';
882 #print "c<$c> SQ<$sanitise_quote>\n";
883 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
884 substr($res, $off, 1, $;);
885 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
886 substr($res, $off, 1, $;);
887 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
888 substr($res, $off, 1, 'X');
890 substr($res, $off, 1, $c);
894 if ($sanitise_quote eq '//') {
895 $sanitise_quote = '';
898 # The pathname on a #include may be surrounded by '<' and '>'.
899 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
900 my $clean = 'X' x
length($1);
901 $res =~ s@\
<.*\
>@
<$clean>@
;
903 # The whole of a #error is a string.
904 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
905 my $clean = 'X' x
length($1);
906 $res =~ s@
(\#\s
*(?
:error
|warning
)\s
+).*@
$1$clean@
;
912 sub get_quoted_string
{
913 my ($line, $rawline) = @_;
915 return "" if ($line !~ m/(\"[X]+\")/g);
916 return substr($rawline, $-[0], $+[0] - $-[0]);
919 sub ctx_statement_block
{
920 my ($linenr, $remain, $off) = @_;
921 my $line = $linenr - 1;
938 @stack = (['', 0]) if ($#stack == -1);
940 #warn "CSB: blk<$blk> remain<$remain>\n";
941 # If we are about to drop off the end, pull in more
944 for (; $remain > 0; $line++) {
945 last if (!defined $lines[$line]);
946 next if ($lines[$line] =~ /^-/);
949 $blk .= $lines[$line] . "\n";
954 # Bail if there is no further context.
955 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
959 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
965 $c = substr($blk, $off, 1);
966 $remainder = substr($blk, $off);
968 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
970 # Handle nested #if/#else.
971 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
972 push(@stack, [ $type, $level ]);
973 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
974 ($type, $level) = @
{$stack[$#stack - 1]};
975 } elsif ($remainder =~ /^#\s*endif\b/) {
976 ($type, $level) = @
{pop(@stack)};
979 # Statement ends at the ';' or a close '}' at the
981 if ($level == 0 && $c eq ';') {
985 # An else is really a conditional as long as its not else if
986 if ($level == 0 && $coff_set == 0 &&
987 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
988 $remainder =~ /^(else)(?:\s|{)/ &&
989 $remainder !~ /^else\s+if\b/) {
990 $coff = $off + length($1) - 1;
992 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
993 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
996 if (($type eq '' || $type eq '(') && $c eq '(') {
1000 if ($type eq '(' && $c eq ')') {
1002 $type = ($level != 0)?
'(' : '';
1004 if ($level == 0 && $coff < $soff) {
1007 #warn "CSB: mark coff<$coff>\n";
1010 if (($type eq '' || $type eq '{') && $c eq '{') {
1014 if ($type eq '{' && $c eq '}') {
1016 $type = ($level != 0)?
'{' : '';
1019 if (substr($blk, $off + 1, 1) eq ';') {
1025 # Preprocessor commands end at the newline unless escaped.
1026 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1034 # We are truly at the end, so shuffle to the next line.
1041 my $statement = substr($blk, $soff, $off - $soff + 1);
1042 my $condition = substr($blk, $soff, $coff - $soff + 1);
1044 #warn "STATEMENT<$statement>\n";
1045 #warn "CONDITION<$condition>\n";
1047 #print "coff<$coff> soff<$off> loff<$loff>\n";
1049 return ($statement, $condition,
1050 $line, $remain + 1, $off - $loff + 1, $level);
1053 sub statement_lines
{
1056 # Strip the diff line prefixes and rip blank lines at start and end.
1057 $stmt =~ s/(^|\n)./$1/g;
1061 my @stmt_lines = ($stmt =~ /\n/g);
1063 return $#stmt_lines + 2;
1066 sub statement_rawlines
{
1069 my @stmt_lines = ($stmt =~ /\n/g);
1071 return $#stmt_lines + 2;
1074 sub statement_block_size
{
1077 $stmt =~ s/(^|\n)./$1/g;
1083 my @stmt_lines = ($stmt =~ /\n/g);
1084 my @stmt_statements = ($stmt =~ /;/g);
1086 my $stmt_lines = $#stmt_lines + 2;
1087 my $stmt_statements = $#stmt_statements + 1;
1089 if ($stmt_lines > $stmt_statements) {
1092 return $stmt_statements;
1096 sub ctx_statement_full
{
1097 my ($linenr, $remain, $off) = @_;
1098 my ($statement, $condition, $level);
1102 # Grab the first conditional/block pair.
1103 ($statement, $condition, $linenr, $remain, $off, $level) =
1104 ctx_statement_block
($linenr, $remain, $off);
1105 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1106 push(@chunks, [ $condition, $statement ]);
1107 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1108 return ($level, $linenr, @chunks);
1111 # Pull in the following conditional/block pairs and see if they
1112 # could continue the statement.
1114 ($statement, $condition, $linenr, $remain, $off, $level) =
1115 ctx_statement_block
($linenr, $remain, $off);
1116 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1117 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1119 push(@chunks, [ $condition, $statement ]);
1122 return ($level, $linenr, @chunks);
1126 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1128 my $start = $linenr - 1;
1135 my @stack = ($level);
1136 for ($line = $start; $remain > 0; $line++) {
1137 next if ($rawlines[$line] =~ /^-/);
1140 $blk .= $rawlines[$line];
1142 # Handle nested #if/#else.
1143 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1144 push(@stack, $level);
1145 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1146 $level = $stack[$#stack - 1];
1147 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1148 $level = pop(@stack);
1151 foreach my $c (split(//, $lines[$line])) {
1152 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1158 if ($c eq $close && $level > 0) {
1160 last if ($level == 0);
1161 } elsif ($c eq $open) {
1166 if (!$outer || $level <= 1) {
1167 push(@res, $rawlines[$line]);
1170 last if ($level == 0);
1173 return ($level, @res);
1175 sub ctx_block_outer
{
1176 my ($linenr, $remain) = @_;
1178 my ($level, @r) = ctx_block_get
($linenr, $remain, 1, '{', '}', 0);
1182 my ($linenr, $remain) = @_;
1184 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
1188 my ($linenr, $remain, $off) = @_;
1190 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
1193 sub ctx_block_level
{
1194 my ($linenr, $remain) = @_;
1196 return ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
1198 sub ctx_statement_level
{
1199 my ($linenr, $remain, $off) = @_;
1201 return ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
1204 sub ctx_locate_comment
{
1205 my ($first_line, $end_line) = @_;
1207 # Catch a comment on the end of the line itself.
1208 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@
.*(/\*.*\*/)\s
*(?
:\\\s
*)?
$@
);
1209 return $current_comment if (defined $current_comment);
1211 # Look through the context and try and figure out if there is a
1214 $current_comment = '';
1215 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1216 my $line = $rawlines[$linenr - 1];
1218 if ($linenr == $first_line and $line =~ m@
^.\s
*\
*@
) {
1221 if ($line =~ m@
/\
*@
) {
1224 if (!$in_comment && $current_comment ne '') {
1225 $current_comment = '';
1227 $current_comment .= $line . "\n" if ($in_comment);
1228 if ($line =~ m@\
*/@
) {
1233 chomp($current_comment);
1234 return($current_comment);
1236 sub ctx_has_comment
{
1237 my ($first_line, $end_line) = @_;
1238 my $cmt = ctx_locate_comment
($first_line, $end_line);
1240 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1241 ##print "CMMT: $cmt\n";
1243 return ($cmt ne '');
1247 my ($linenr, $cnt) = @_;
1249 my $offset = $linenr - 1;
1254 $line = $rawlines[$offset++];
1255 next if (defined($line) && $line =~ /^-/);
1267 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1270 $coded = sprintf("^%c", unpack('C', $2) + 64);
1279 my $av_preprocessor = 0;
1284 sub annotate_reset
{
1285 $av_preprocessor = 0;
1287 @av_paren_type = ('E');
1288 $av_pend_colon = 'O';
1291 sub annotate_values
{
1292 my ($stream, $type) = @_;
1295 my $var = '_' x
length($stream);
1298 print "$stream\n" if ($dbg_values > 1);
1300 while (length($cur)) {
1301 @av_paren_type = ('E') if ($#av_paren_type < 0);
1302 print " <" . join('', @av_paren_type) .
1303 "> <$type> <$av_pending>" if ($dbg_values > 1);
1304 if ($cur =~ /^(\s+)/o) {
1305 print "WS($1)\n" if ($dbg_values > 1);
1306 if ($1 =~ /\n/ && $av_preprocessor) {
1307 $type = pop(@av_paren_type);
1308 $av_preprocessor = 0;
1311 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1312 print "CAST($1)\n" if ($dbg_values > 1);
1313 push(@av_paren_type, $type);
1316 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1317 print "DECLARE($1)\n" if ($dbg_values > 1);
1320 } elsif ($cur =~ /^($Modifier)\s*/) {
1321 print "MODIFIER($1)\n" if ($dbg_values > 1);
1324 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1325 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1326 $av_preprocessor = 1;
1327 push(@av_paren_type, $type);
1333 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1334 print "UNDEF($1)\n" if ($dbg_values > 1);
1335 $av_preprocessor = 1;
1336 push(@av_paren_type, $type);
1338 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1339 print "PRE_START($1)\n" if ($dbg_values > 1);
1340 $av_preprocessor = 1;
1342 push(@av_paren_type, $type);
1343 push(@av_paren_type, $type);
1346 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1347 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1348 $av_preprocessor = 1;
1350 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1354 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1355 print "PRE_END($1)\n" if ($dbg_values > 1);
1357 $av_preprocessor = 1;
1359 # Assume all arms of the conditional end as this
1360 # one does, and continue as if the #endif was not here.
1361 pop(@av_paren_type);
1362 push(@av_paren_type, $type);
1365 } elsif ($cur =~ /^(\\\n)/o) {
1366 print "PRECONT($1)\n" if ($dbg_values > 1);
1368 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1369 print "ATTR($1)\n" if ($dbg_values > 1);
1370 $av_pending = $type;
1373 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1374 print "SIZEOF($1)\n" if ($dbg_values > 1);
1380 } elsif ($cur =~ /^(if|while|for)\b/o) {
1381 print "COND($1)\n" if ($dbg_values > 1);
1385 } elsif ($cur =~/^(case)/o) {
1386 print "CASE($1)\n" if ($dbg_values > 1);
1387 $av_pend_colon = 'C';
1390 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1391 print "KEYWORD($1)\n" if ($dbg_values > 1);
1394 } elsif ($cur =~ /^(\()/o) {
1395 print "PAREN('$1')\n" if ($dbg_values > 1);
1396 push(@av_paren_type, $av_pending);
1400 } elsif ($cur =~ /^(\))/o) {
1401 my $new_type = pop(@av_paren_type);
1402 if ($new_type ne '_') {
1404 print "PAREN('$1') -> $type\n"
1405 if ($dbg_values > 1);
1407 print "PAREN('$1')\n" if ($dbg_values > 1);
1410 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1411 print "FUNC($1)\n" if ($dbg_values > 1);
1415 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1416 if (defined $2 && $type eq 'C' || $type eq 'T') {
1417 $av_pend_colon = 'B';
1418 } elsif ($type eq 'E') {
1419 $av_pend_colon = 'L';
1421 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1424 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1425 print "IDENT($1)\n" if ($dbg_values > 1);
1428 } elsif ($cur =~ /^($Assignment)/o) {
1429 print "ASSIGN($1)\n" if ($dbg_values > 1);
1432 } elsif ($cur =~/^(;|{|})/) {
1433 print "END($1)\n" if ($dbg_values > 1);
1435 $av_pend_colon = 'O';
1437 } elsif ($cur =~/^(,)/) {
1438 print "COMMA($1)\n" if ($dbg_values > 1);
1441 } elsif ($cur =~ /^(\?)/o) {
1442 print "QUESTION($1)\n" if ($dbg_values > 1);
1445 } elsif ($cur =~ /^(:)/o) {
1446 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1448 substr($var, length($res), 1, $av_pend_colon);
1449 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1454 $av_pend_colon = 'O';
1456 } elsif ($cur =~ /^(\[)/o) {
1457 print "CLOSE($1)\n" if ($dbg_values > 1);
1460 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1463 print "OPV($1)\n" if ($dbg_values > 1);
1470 substr($var, length($res), 1, $variant);
1473 } elsif ($cur =~ /^($Operators)/o) {
1474 print "OP($1)\n" if ($dbg_values > 1);
1475 if ($1 ne '++' && $1 ne '--') {
1479 } elsif ($cur =~ /(^.)/o) {
1480 print "C($1)\n" if ($dbg_values > 1);
1483 $cur = substr($cur, length($1));
1484 $res .= $type x
length($1);
1488 return ($res, $var);
1492 my ($possible, $line) = @_;
1493 my $notPermitted = qr{(?
:
1510 ^(?
:typedef
|struct
|enum
)\b
1512 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1513 if ($possible !~ $notPermitted) {
1514 # Check for modifiers.
1515 $possible =~ s/\s*$Storage\s*//g;
1516 $possible =~ s/\s*$Sparse\s*//g;
1517 if ($possible =~ /^\s*$/) {
1519 } elsif ($possible =~ /\s/) {
1520 $possible =~ s/\s*$Type\s*//g;
1521 for my $modifier (split(' ', $possible)) {
1522 if ($modifier !~ $notPermitted) {
1523 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1524 push(@modifierList, $modifier);
1529 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1530 push(@typeList, $possible);
1534 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1543 return defined $use_type{$type} if (scalar keys %use_type > 0);
1545 return !defined $ignore_type{$type};
1549 my ($level, $type, $msg) = @_;
1551 if (!show_type
($type) ||
1552 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1557 $line = "$prefix$level:$type: $msg\n";
1559 $line = "$prefix$level: $msg\n";
1561 $line = (split('\n', $line))[0] . "\n" if ($terse);
1563 push(our @report, $line);
1572 sub fixup_current_range
{
1573 my ($lineRef, $offset, $length) = @_;
1575 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1578 my $no = $o + $offset;
1579 my $nl = $l + $length;
1580 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1584 sub fix_inserted_deleted_lines
{
1585 my ($linesRef, $insertedRef, $deletedRef) = @_;
1587 my $range_last_linenr = 0;
1588 my $delta_offset = 0;
1593 my $next_insert = 0;
1594 my $next_delete = 0;
1598 my $inserted = @
{$insertedRef}[$next_insert++];
1599 my $deleted = @
{$deletedRef}[$next_delete++];
1601 foreach my $old_line (@
{$linesRef}) {
1603 my $line = $old_line; #don't modify the array
1604 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename
1606 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1607 $range_last_linenr = $new_linenr;
1608 fixup_current_range
(\
$line, $delta_offset, 0);
1611 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1612 $deleted = @
{$deletedRef}[$next_delete++];
1614 fixup_current_range
(\
$lines[$range_last_linenr], $delta_offset--, -1);
1617 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1618 push(@lines, ${$inserted}{'LINE'});
1619 $inserted = @
{$insertedRef}[$next_insert++];
1621 fixup_current_range
(\
$lines[$range_last_linenr], $delta_offset++, 1);
1625 push(@lines, $line);
1635 sub fix_insert_line
{
1636 my ($linenr, $line) = @_;
1642 push(@fixed_inserted, $inserted);
1645 sub fix_delete_line
{
1646 my ($linenr, $line) = @_;
1653 push(@fixed_deleted, $deleted);
1657 my ($type, $msg) = @_;
1659 if (report
("ERROR", $type, $msg)) {
1667 my ($type, $msg) = @_;
1669 if (report
("WARNING", $type, $msg)) {
1677 my ($type, $msg) = @_;
1679 if ($check && report
("CHECK", $type, $msg)) {
1687 sub check_absolute_file
{
1688 my ($absolute, $herecurr) = @_;
1689 my $file = $absolute;
1691 ##print "absolute<$absolute>\n";
1693 # See if any suffix of this path is a path within the tree.
1694 while ($file =~ s@
^[^/]*/@@
) {
1695 if (-f
"$root/$file") {
1696 ##print "file<$file>\n";
1704 # It is, so see if the prefix is acceptable.
1705 my $prefix = $absolute;
1706 substr($prefix, -length($file)) = '';
1708 ##print "prefix<$prefix>\n";
1709 if ($prefix ne ".../") {
1710 WARN
("USE_RELATIVE_PATH",
1711 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1718 $string =~ s/^\s+|\s+$//g;
1726 $string =~ s/^\s+//;
1734 $string =~ s/\s+$//;
1739 sub string_find_replace
{
1740 my ($string, $find, $replace) = @_;
1742 $string =~ s/$find/$replace/g;
1750 my $source_indent = 8;
1751 my $max_spaces_before_tab = $source_indent - 1;
1752 my $spaces_to_tab = " " x
$source_indent;
1754 #convert leading spaces to tabs
1755 1 while $leading =~ s@
^([\t]*)$spaces_to_tab@
$1\t@g;
1756 #Remove spaces before a tab
1757 1 while $leading =~ s@
^([\t]*)( {1,$max_spaces_before_tab})\t@
$1\t@g;
1762 sub pos_last_openparen
{
1767 my $opens = $line =~ tr/\(/\(/;
1768 my $closes = $line =~ tr/\)/\)/;
1770 my $last_openparen = 0;
1772 if (($opens == 0) || ($closes >= $opens)) {
1776 my $len = length($line);
1778 for ($pos = 0; $pos < $len; $pos++) {
1779 my $string = substr($line, $pos);
1780 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1781 $pos += length($1) - 1;
1782 } elsif (substr($line, $pos, 1) eq '(') {
1783 $last_openparen = $pos;
1784 } elsif (index($string, '(') == -1) {
1789 return length(expand_tabs
(substr($line, 0, $last_openparen))) + 1;
1793 my $filename = shift;
1799 my $stashrawline="";
1810 my $in_header_lines = $file ?
0 : 1;
1811 my $in_commit_log = 0; #Scanning lines before patch
1812 my $reported_maintainer_file = 0;
1813 my $non_utf8_charset = 0;
1815 my $last_blank_line = 0;
1823 # Trace the real file/line as we go.
1829 my $comment_edge = 0;
1833 my $prev_values = 'E';
1836 my %suppress_ifbraces;
1837 my %suppress_whiletrailers;
1838 my %suppress_export;
1839 my $suppress_statement = 0;
1841 my %signatures = ();
1843 # Pre-scan the patch sanitizing the lines.
1844 # Pre-scan the patch looking for any __setup documentation.
1846 my @setup_docs = ();
1849 my $camelcase_file_seeded = 0;
1851 sanitise_line_reset
();
1853 foreach my $rawline (@rawlines) {
1857 push(@fixed, $rawline) if ($fix);
1859 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1861 if ($1 =~ m
@Documentation/kernel
-parameters
.txt
$@
) {
1866 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1875 # Guestimate if this is a continuing comment. Run
1876 # the context looking for a comment "edge". If this
1877 # edge is a close comment then we must be in a comment
1881 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1882 next if (defined $rawlines[$ln - 1] &&
1883 $rawlines[$ln - 1] =~ /^-/);
1885 #print "RAW<$rawlines[$ln - 1]>\n";
1886 last if (!defined $rawlines[$ln - 1]);
1887 if ($rawlines[$ln - 1] =~ m@
(/\*|\*/)@
&&
1888 $rawlines[$ln - 1] !~ m@
"[^"]*(?
:/\*|\*/)[^"]*"@
) {
1893 if (defined $edge && $edge eq '*/') {
1897 # Guestimate if this is a continuing comment. If this
1898 # is the start of a diff block and this line starts
1899 # ' *' then it is very likely a comment.
1900 if (!defined $edge &&
1901 $rawlines[$linenr] =~ m@
^.\s
*(?
:\
*\
*+| \
*)(?
:\s
|$)@
)
1906 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1907 sanitise_line_reset
($in_comment);
1909 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1910 # Standardise the strings and chars within the input to
1911 # simplify matching -- only bother with positive lines.
1912 $line = sanitise_line
($rawline);
1914 push(@lines, $line);
1917 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1922 #print "==>$rawline\n";
1923 #print "-->$line\n";
1925 if ($setup_docs && $line =~ /^\+/) {
1926 push(@setup_docs, $line);
1935 foreach my $line (@lines) {
1938 my $sline = $line; #copy of $line
1939 $sline =~ s/$;/ /g; #with comments as spaces
1941 my $rawline = $rawlines[$linenr - 1];
1943 #extract the line range in the file after the patch is applied
1944 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1946 $first_line = $linenr + 1;
1956 %suppress_ifbraces = ();
1957 %suppress_whiletrailers = ();
1958 %suppress_export = ();
1959 $suppress_statement = 0;
1962 # track the line number as we move through the hunk, note that
1963 # new versions of GNU diff omit the leading space on completely
1964 # blank context lines so we need to count that too.
1965 } elsif ($line =~ /^( |\+|$)/) {
1967 $realcnt-- if ($realcnt != 0);
1969 # Measure the line length and indent.
1970 ($length, $indent) = line_stats
($rawline);
1972 # Track the previous line.
1973 ($prevline, $stashline) = ($stashline, $line);
1974 ($previndent, $stashindent) = ($stashindent, $indent);
1975 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1977 #warn "line<$line>\n";
1979 } elsif ($realcnt == 1) {
1983 my $hunk_line = ($realcnt != 0);
1985 #make up the handle for any error we report on this line
1986 $prefix = "$filename:$realline: " if ($emacs && $file);
1987 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1989 $here = "#$linenr: " if (!$file);
1990 $here = "#$realline: " if ($file);
1993 # extract the filename as it passes
1994 if ($line =~ /^diff --git.*?(\S+)$/) {
1996 $realfile =~ s@
^([^/]*)/@@
if (!$file);
1999 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2001 $realfile =~ s@
^([^/]*)/@@
if (!$file);
2005 if (!$file && $tree && $p1_prefix ne '' &&
2006 -e
"$root/$p1_prefix") {
2007 WARN
("PATCH_PREFIX",
2008 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2011 if ($realfile =~ m@
^include
/asm/@
) {
2012 ERROR
("MODIFIED_INCLUDE_ASM",
2013 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2019 if ($realfile =~ m@
^(drivers
/net
/|net
/)@
) {
2022 $check = $check_orig;
2027 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2029 my $hereline = "$here\n$rawline\n";
2030 my $herecurr = "$here\n$rawline\n";
2031 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2033 $cnt_lines++ if ($realcnt != 0);
2035 # Check for incorrect file permissions
2036 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2037 my $permhere = $here . "FILE: $realfile\n";
2038 if ($realfile !~ m
@scripts/@
&&
2039 $realfile !~ /\.(py|pl|awk|sh)$/) {
2040 ERROR
("EXECUTE_PERMISSIONS",
2041 "do not set execute permissions for source files\n" . $permhere);
2045 # Check the patch for a signoff:
2046 if ($line =~ /^\s*signed-off-by:/i) {
2051 # Check signature styles
2052 if (!$in_header_lines &&
2053 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
2054 my $space_before = $1;
2056 my $space_after = $3;
2058 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2060 if ($sign_off !~ /$signature_tags/) {
2061 WARN
("BAD_SIGN_OFF",
2062 "Non-standard signature: $sign_off\n" . $herecurr);
2064 if (defined $space_before && $space_before ne "") {
2065 if (WARN
("BAD_SIGN_OFF",
2066 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2068 $fixed[$fixlinenr] =
2069 "$ucfirst_sign_off $email";
2072 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
2073 if (WARN
("BAD_SIGN_OFF",
2074 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2076 $fixed[$fixlinenr] =
2077 "$ucfirst_sign_off $email";
2081 if (!defined $space_after || $space_after ne " ") {
2082 if (WARN
("BAD_SIGN_OFF",
2083 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2085 $fixed[$fixlinenr] =
2086 "$ucfirst_sign_off $email";
2090 my ($email_name, $email_address, $comment) = parse_email
($email);
2091 my $suggested_email = format_email
(($email_name, $email_address));
2092 if ($suggested_email eq "") {
2093 ERROR
("BAD_SIGN_OFF",
2094 "Unrecognized email address: '$email'\n" . $herecurr);
2096 my $dequoted = $suggested_email;
2097 $dequoted =~ s/^"//;
2098 $dequoted =~ s/" </ </;
2099 # Don't force email to have quotes
2100 # Allow just an angle bracketed address
2101 if ("$dequoted$comment" ne $email &&
2102 "<$email_address>$comment" ne $email &&
2103 "$suggested_email$comment" ne $email) {
2104 WARN
("BAD_SIGN_OFF",
2105 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2109 # Check for duplicate signatures
2110 my $sig_nospace = $line;
2111 $sig_nospace =~ s/\s//g;
2112 $sig_nospace = lc($sig_nospace);
2113 if (defined $signatures{$sig_nospace}) {
2114 WARN
("BAD_SIGN_OFF",
2115 "Duplicate signature\n" . $herecurr);
2117 $signatures{$sig_nospace} = 1;
2121 # Check for old stable address
2122 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2123 ERROR
("STABLE_ADDRESS",
2124 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2127 # Check for unwanted Gerrit info
2128 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2129 ERROR
("GERRIT_CHANGE_ID",
2130 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2133 # Check for improperly formed commit descriptions
2134 if ($in_commit_log &&
2135 $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
2136 $line !~ /\b[Cc]ommit [0-9a-f]{12,16} \("/) {
2137 $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2139 my $orig_commit = lc($2);
2140 my $id = '01234567890ab';
2141 my $desc = 'commit description';
2142 ($id, $desc) = git_commit_info
($orig_commit, $id, $desc);
2143 ERROR
("GIT_COMMIT_ID",
2144 "Please use 12 to 16 chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr);
2147 # Check for added, moved or deleted files
2148 if (!$reported_maintainer_file && !$in_commit_log &&
2149 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2150 $line =~ /^rename (?:from|to) [\w\/\
.\
-]+\s
*$/ ||
2151 ($line =~ /\{\s*([\w\/\
.\
-]*)\s
*\
=\
>\s
*([\w\
/\.\-]*)\s*\}/ &&
2152 (defined($1) || defined($2))))) {
2153 $reported_maintainer_file = 1;
2154 WARN
("FILE_PATH_CHANGES",
2155 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2158 # Check for wrappage within a valid hunk of the file
2159 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2160 ERROR
("CORRUPTED_PATCH",
2161 "patch seems to be corrupt (line wrapped?)\n" .
2162 $herecurr) if (!$emitted_corrupt++);
2165 # Check for absolute kernel paths.
2167 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2170 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2171 check_absolute_file
($1, $herecurr)) {
2174 check_absolute_file
($file, $herecurr);
2179 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2180 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2181 $rawline !~ m/^$UTF8*$/) {
2182 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2184 my $blank = copy_spacing
($rawline);
2185 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2186 my $hereptr = "$hereline$ptr\n";
2189 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
2192 # Check if it's the start of a commit log
2193 # (not a header line and we haven't seen the patch filename)
2194 if ($in_header_lines && $realfile =~ /^$/ &&
2195 !($rawline =~ /^\s+\S/ ||
2196 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
2197 $in_header_lines = 0;
2201 # Check if there is UTF-8 in a commit log when a mail header has explicitly
2202 # declined it, i.e defined some charset where it is missing.
2203 if ($in_header_lines &&
2204 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2206 $non_utf8_charset = 1;
2209 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
2210 $rawline =~ /$NON_ASCII_UTF8/) {
2211 WARN
("UTF8_BEFORE_PATCH",
2212 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2215 # ignore non-hunk lines and lines being removed
2216 next if (!$hunk_line || $line =~ /^-/);
2218 #trailing whitespace
2219 if ($line =~ /^\+.*\015/) {
2220 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2221 if (ERROR
("DOS_LINE_ENDINGS",
2222 "DOS line endings\n" . $herevet) &&
2224 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
2226 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2227 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2228 if (ERROR
("TRAILING_WHITESPACE",
2229 "trailing whitespace\n" . $herevet) &&
2231 $fixed[$fixlinenr] =~ s/\s+$//;
2237 # Check for FSF mailing addresses.
2238 if ($rawline =~ /\bwrite to the Free/i ||
2239 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2240 $rawline =~ /\b51\s+Franklin\s+St/i) {
2241 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2242 my $msg_type = \
&ERROR
;
2243 $msg_type = \
&CHK
if ($file);
2244 &{$msg_type}("FSF_MAILING_ADDRESS",
2245 "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet)
2248 # check for Kconfig help text having a real description
2249 # Only applies when adding the entry originally, after that we do not have
2250 # sufficient context to determine whether it is indeed long enough.
2251 if ($realfile =~ /Kconfig/ &&
2252 $line =~ /^\+\s*config\s+/) {
2255 my $ln = $linenr + 1;
2259 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
2260 $f = $lines[$ln - 1];
2261 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2262 $is_end = $lines[$ln - 1] =~ /^\+/;
2264 next if ($f =~ /^-/);
2265 last if (!$file && $f =~ /^\@\@/);
2267 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2269 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2276 next if ($f =~ /^$/);
2277 if ($f =~ /^\s*config\s/) {
2283 WARN
("CONFIG_DESCRIPTION",
2284 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
2285 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
2288 # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2289 if ($realfile =~ /Kconfig/ &&
2290 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2291 WARN
("CONFIG_EXPERIMENTAL",
2292 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2295 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2296 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2299 'EXTRA_AFLAGS' => 'asflags-y',
2300 'EXTRA_CFLAGS' => 'ccflags-y',
2301 'EXTRA_CPPFLAGS' => 'cppflags-y',
2302 'EXTRA_LDFLAGS' => 'ldflags-y',
2305 WARN
("DEPRECATED_VARIABLE",
2306 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2309 # check for DT compatible documentation
2310 if (defined $root &&
2311 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2312 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2314 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2316 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2317 my $vp_file = $dt_path . "vendor-prefixes.txt";
2319 foreach my $compat (@compats) {
2320 my $compat2 = $compat;
2321 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2322 my $compat3 = $compat;
2323 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2324 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2326 WARN
("UNDOCUMENTED_DT_STRING",
2327 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2330 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2332 `grep -Eq "^$vendor\\b" $vp_file`;
2334 WARN
("UNDOCUMENTED_DT_STRING",
2335 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2340 # check we are in a valid source file if not then ignore this hunk
2341 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
2344 if ($line =~ /^\+/ && $prevrawline !~ /\/\
*\
*/ &&
2345 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
2346 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
2347 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
2348 $length > $max_line_length)
2351 "line over $max_line_length characters\n" . $herecurr);
2354 # Check for user-visible strings broken across lines, which breaks the ability
2355 # to grep for the string. Make exceptions when the previous string ends in a
2356 # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
2357 # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
2358 if ($line =~ /^\+\s*"/ &&
2359 $prevline =~ /"\s*$/ &&
2360 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
2361 WARN
("SPLIT_STRING",
2362 "quoted string split across lines\n" . $hereprev);
2365 # check for missing a space in a string concatination
2366 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
2367 WARN
('MISSING_SPACE',
2368 "break quoted strings at a space character\n" . $hereprev);
2371 # check for spaces before a quoted newline
2372 if ($rawline =~ /^.*\".*\s\\n/) {
2373 if (WARN
("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2374 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2376 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
2381 # check for adding lines without a newline.
2382 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2383 WARN
("MISSING_EOF_NEWLINE",
2384 "adding a line without newline at end of file\n" . $herecurr);
2387 # Blackfin: use hi/lo macros
2388 if ($realfile =~ m
@arch/blackfin/.*\
.S
$@
) {
2389 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2390 my $herevet = "$here\n" . cat_vet
($line) . "\n";
2392 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
2394 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2395 my $herevet = "$here\n" . cat_vet
($line) . "\n";
2397 "use the HI() macro, not (... >> 16)\n" . $herevet);
2401 # check we are in a valid source file C or perl if not then ignore this hunk
2402 next if ($realfile !~ /\.(h|c|pl)$/);
2404 # at the beginning of a line any tabs must come first and anything
2405 # more than 8 must use tabs.
2406 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2407 $rawline =~ /^\+\s* \s*/) {
2408 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2410 if (ERROR
("CODE_INDENT",
2411 "code indent should use tabs where possible\n" . $herevet) &&
2413 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2417 # check for space before tabs.
2418 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2419 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2420 if (WARN
("SPACE_BEFORE_TAB",
2421 "please, no space before tabs\n" . $herevet) &&
2423 while ($fixed[$fixlinenr] =~
2424 s/(^\+.*) {8,8}+\t/$1\t\t/) {}
2425 while ($fixed[$fixlinenr] =~
2426 s/(^\+.*) +\t/$1\t/) {}
2430 # check for && or || at the start of a line
2431 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2432 CHK
("LOGICAL_CONTINUATIONS",
2433 "Logical continuations should be on the previous line\n" . $hereprev);
2436 # check multi-line statement indentation matches previous line
2437 if ($^V
&& $^V
ge 5.10.0 &&
2438 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2439 $prevline =~ /^\+(\t*)(.*)$/;
2443 my $pos = pos_last_openparen
($rest);
2445 $line =~ /^(\+| )([ \t]*)/;
2448 my $goodtabindent = $oldindent .
2451 my $goodspaceindent = $oldindent . " " x
$pos;
2453 if ($newindent ne $goodtabindent &&
2454 $newindent ne $goodspaceindent) {
2456 if (CHK
("PARENTHESIS_ALIGNMENT",
2457 "Alignment should match open parenthesis\n" . $hereprev) &&
2458 $fix && $line =~ /^\+/) {
2459 $fixed[$fixlinenr] =~
2460 s/^\+[ \t]*/\+$goodtabindent/;
2466 if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|{)/) {
2468 "No space is necessary after a cast\n" . $herecurr) &&
2470 $fixed[$fixlinenr] =~
2471 s/(\(\s*$Type\s*\))[ \t]+/$1/;
2475 if ($realfile =~ m@
^(drivers
/net
/|net
/)@
&&
2476 $prevrawline =~ /^\+[ \t]*\/\
*[ \t]*$/ &&
2477 $rawline =~ /^\+[ \t]*\*/ &&
2479 WARN
("NETWORKING_BLOCK_COMMENT_STYLE",
2480 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2483 if ($realfile =~ m@
^(drivers
/net
/|net
/)@
&&
2484 $prevrawline =~ /^\+[ \t]*\/\
*/ && #starting /*
2485 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
2486 $rawline =~ /^\+/ && #line is new
2487 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2488 WARN
("NETWORKING_BLOCK_COMMENT_STYLE",
2489 "networking block comments start with * on subsequent lines\n" . $hereprev);
2492 if ($realfile =~ m@
^(drivers
/net
/|net
/)@
&&
2493 $rawline !~ m@
^\
+[ \t]*\
*/[ \t]*$@ && #trailing */
2494 $rawline !~ m@
^\
+.*/\*.*\*/[ \t]*$@
&& #inline /*...*/
2495 $rawline !~ m@
^\
+.*\
*{2,}/[ \t]*$@ && #trailing **/
2496 $rawline =~ m@
^\
+[ \t]*.+\
*\
/[ \t]*$@) { #non blank */
2497 WARN
("NETWORKING_BLOCK_COMMENT_STYLE",
2498 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2501 # check for missing blank lines after struct/union declarations
2502 # with exceptions for various attributes and macros
2503 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2505 !($line =~ /^\+\s*$/ ||
2506 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2507 $line =~ /^\+\s*MODULE_/i ||
2508 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2509 $line =~ /^\+[a-z_]*init/ ||
2510 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2511 $line =~ /^\+\s*DECLARE/ ||
2512 $line =~ /^\+\s*__setup/)) {
2513 if (CHK
("LINE_SPACING",
2514 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2516 fix_insert_line
($fixlinenr, "\+");
2520 # check for multiple consecutive blank lines
2521 if ($prevline =~ /^[\+ ]\s*$/ &&
2522 $line =~ /^\+\s*$/ &&
2523 $last_blank_line != ($linenr - 1)) {
2524 if (CHK
("LINE_SPACING",
2525 "Please don't use multiple blank lines\n" . $hereprev) &&
2527 fix_delete_line
($fixlinenr, $rawline);
2530 $last_blank_line = $linenr;
2533 # check for missing blank lines after declarations
2534 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2535 # actual declarations
2536 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
2537 # function pointer declarations
2538 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
2539 # foo bar; where foo is some local typedef or #define
2540 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2541 # known declaration macros
2542 $prevline =~ /^\+\s+$declaration_macros/) &&
2543 # for "else if" which can look like "$Ident $Ident"
2544 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2545 # other possible extensions of declaration lines
2546 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2547 # not starting a section or a macro "\" extended line
2548 $prevline =~ /(?:\{\s*|\\)$/) &&
2549 # looks like a declaration
2550 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
2551 # function pointer declarations
2552 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
2553 # foo bar; where foo is some local typedef or #define
2554 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2555 # known declaration macros
2556 $sline =~ /^\+\s+$declaration_macros/ ||
2557 # start of struct or union or enum
2558 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
2559 # start or end of block or continuation of declaration
2560 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2561 # bitfield continuation
2562 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2563 # other possible extensions of declaration lines
2564 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2565 # indentation of previous and current line are the same
2566 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2567 if (WARN
("LINE_SPACING",
2568 "Missing a blank line after declarations\n" . $hereprev) &&
2570 fix_insert_line
($fixlinenr, "\+");
2574 # check for spaces at the beginning of a line.
2576 # 1) within comments
2577 # 2) indented preprocessor commands
2579 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
2580 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2581 if (WARN
("LEADING_SPACE",
2582 "please, no spaces at the start of a line\n" . $herevet) &&
2584 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2588 # check we are in a valid C source file if not then ignore this hunk
2589 next if ($realfile !~ /\.(h|c)$/);
2591 # check indentation of any line with a bare else
2592 # if the previous line is a break or return and is indented 1 tab more...
2593 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2594 my $tabs = length($1) + 1;
2595 if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) {
2596 WARN
("UNNECESSARY_ELSE",
2597 "else is not generally useful after a break or return\n" . $hereprev);
2601 # check indentation of a line with a break;
2602 # if the previous line is a goto or return and is indented the same # of tabs
2603 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2605 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2606 WARN
("UNNECESSARY_BREAK",
2607 "break is not useful after a goto or return\n" . $hereprev);
2611 # discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2612 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2613 WARN
("CONFIG_EXPERIMENTAL",
2614 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2617 # check for RCS/CVS revision markers
2618 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2620 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2623 # Blackfin: don't use __builtin_bfin_[cs]sync
2624 if ($line =~ /__builtin_bfin_csync/) {
2625 my $herevet = "$here\n" . cat_vet
($line) . "\n";
2627 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
2629 if ($line =~ /__builtin_bfin_ssync/) {
2630 my $herevet = "$here\n" . cat_vet
($line) . "\n";
2632 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
2635 # check for old HOTPLUG __dev<foo> section markings
2636 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2637 WARN
("HOTPLUG_SECTION",
2638 "Using $1 is unnecessary\n" . $herecurr);
2641 # Check for potential 'bare' types
2642 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2644 #print "LINE<$line>\n";
2645 if ($linenr >= $suppress_statement &&
2646 $realcnt && $sline =~ /.\s*\S/) {
2647 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2648 ctx_statement_block
($linenr, $realcnt, 0);
2649 $stat =~ s/\n./\n /g;
2650 $cond =~ s/\n./\n /g;
2652 #print "linenr<$linenr> <$stat>\n";
2653 # If this statement has no statement boundaries within
2654 # it there is no point in retrying a statement scan
2655 # until we hit end of it.
2656 my $frag = $stat; $frag =~ s/;+\s*$//;
2657 if ($frag !~ /(?:{|;)/) {
2658 #print "skip<$line_nr_next>\n";
2659 $suppress_statement = $line_nr_next;
2662 # Find the real next line.
2663 $realline_next = $line_nr_next;
2664 if (defined $realline_next &&
2665 (!defined $lines[$realline_next - 1] ||
2666 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2673 # Ignore goto labels.
2674 if ($s =~ /$Ident:\*$/s) {
2676 # Ignore functions being called
2677 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2679 } elsif ($s =~ /^.\s*else\b/s) {
2681 # declarations always start with types
2682 } 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) {
2685 possible
($type, "A:" . $s);
2687 # definitions in global scope can only start with types
2688 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2689 possible
($1, "B:" . $s);
2692 # any (foo ... *) is a pointer cast, and foo is a type
2693 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2694 possible
($1, "C:" . $s);
2697 # Check for any sort of function declaration.
2698 # int foo(something bar, other baz);
2699 # void (*store_gdt)(x86_descr_ptr *);
2700 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
2701 my ($name_len) = length($1);
2704 substr($ctx, 0, $name_len + 1, '');
2705 $ctx =~ s/\)[^\)]*$//;
2707 for my $arg (split(/\s*,\s*/, $ctx)) {
2708 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
2710 possible
($1, "D:" . $s);
2718 # Checks which may be anchored in the context.
2721 # Check for switch () and associated case and default
2722 # statements should be at the same indent.
2723 if ($line=~/\bswitch\s*\(.*\)/) {
2726 my @ctx = ctx_block_outer
($linenr, $realcnt);
2728 for my $ctx (@ctx) {
2729 my ($clen, $cindent) = line_stats
($ctx);
2730 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2731 $indent != $cindent) {
2732 $err .= "$sep$ctx\n";
2739 ERROR
("SWITCH_CASE_INDENT_LEVEL",
2740 "switch and case should be at the same indent\n$hereline$err");
2744 # if/while/etc brace do not go on next line, unless defining a do while loop,
2745 # or if that brace on the next line is for something else
2746 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2747 my $pre_ctx = "$1$2";
2749 my ($level, @ctx) = ctx_statement_level
($linenr, $realcnt, 0);
2751 if ($line =~ /^\+\t{6,}/) {
2752 WARN
("DEEP_INDENTATION",
2753 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2756 my $ctx_cnt = $realcnt - $#ctx - 1;
2757 my $ctx = join("\n", @ctx);
2759 my $ctx_ln = $linenr;
2760 my $ctx_skip = $realcnt;
2762 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2763 defined $lines[$ctx_ln - 1] &&
2764 $lines[$ctx_ln - 1] =~ /^-/)) {
2765 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2766 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2770 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2771 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2773 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2775 "that open brace { should be on the previous line\n" .
2776 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2778 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2779 $ctx =~ /\)\s*\;\s*$/ &&
2780 defined $lines[$ctx_ln - 1])
2782 my ($nlength, $nindent) = line_stats
($lines[$ctx_ln - 1]);
2783 if ($nindent > $indent) {
2784 WARN
("TRAILING_SEMICOLON",
2785 "trailing semicolon indicates no statements, indent implies otherwise\n" .
2786 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2791 # Check relative indent for conditionals and blocks.
2792 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
2793 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2794 ctx_statement_block
($linenr, $realcnt, 0)
2795 if (!defined $stat);
2796 my ($s, $c) = ($stat, $cond);
2798 substr($s, 0, length($c), '');
2800 # Make sure we remove the line prefixes as we have
2801 # none on the first line, and are going to readd them
2805 # Find out how long the conditional actually is.
2806 my @newlines = ($c =~ /\n/gs);
2807 my $cond_lines = 1 + $#newlines;
2809 # We want to check the first line inside the block
2810 # starting at the end of the conditional, so remove:
2811 # 1) any blank line termination
2812 # 2) any opening brace { on end of the line
2814 my $continuation = 0;
2816 $s =~ s/^.*\bdo\b//;
2818 if ($s =~ s/^\s*\\//) {
2821 if ($s =~ s/^\s*?\n//) {
2826 # Also ignore a loop construct at the end of a
2827 # preprocessor statement.
2828 if (($prevline =~ /^.\s*#\s*define\s/ ||
2829 $prevline =~ /\\\s*$/) && $continuation == 0) {
2835 while ($cond_ptr != $cond_lines) {
2836 $cond_ptr = $cond_lines;
2838 # If we see an #else/#elif then the code
2840 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2845 # 1) blank lines, they should be at 0,
2846 # 2) preprocessor lines, and
2848 if ($continuation ||
2850 $s =~ /^\s*#\s*?/ ||
2851 $s =~ /^\s*$Ident\s*:/) {
2852 $continuation = ($s =~ /^.*?\\\n/) ?
1 : 0;
2853 if ($s =~ s/^.*?\n//) {
2859 my (undef, $sindent) = line_stats
("+" . $s);
2860 my $stat_real = raw_line
($linenr, $cond_lines);
2862 # Check if either of these lines are modified, else
2863 # this is not this patch's fault.
2864 if (!defined($stat_real) ||
2865 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2868 if (defined($stat_real) && $cond_lines > 1) {
2869 $stat_real = "[...]\n$stat_real";
2872 #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";
2874 if ($check && (($sindent % 8) != 0 ||
2875 ($sindent <= $indent && $s ne ''))) {
2876 WARN
("SUSPECT_CODE_INDENT",
2877 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
2881 # Track the 'values' across context and added lines.
2882 my $opline = $line; $opline =~ s/^./ /;
2883 my ($curr_values, $curr_vars) =
2884 annotate_values
($opline . "\n", $prev_values);
2885 $curr_values = $prev_values . $curr_values;
2887 my $outline = $opline; $outline =~ s/\t/ /g;
2888 print "$linenr > .$outline\n";
2889 print "$linenr > $curr_values\n";
2890 print "$linenr > $curr_vars\n";
2892 $prev_values = substr($curr_values, -1);
2894 #ignore lines not being added
2895 next if ($line =~ /^[^\+]/);
2897 # TEST: allow direct testing of the type matcher.
2899 if ($line =~ /^.\s*$Declare\s*$/) {
2901 "TEST: is type\n" . $herecurr);
2902 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2903 ERROR
("TEST_NOT_TYPE",
2904 "TEST: is not type ($1 is)\n". $herecurr);
2908 # TEST: allow direct testing of the attribute matcher.
2910 if ($line =~ /^.\s*$Modifier\s*$/) {
2912 "TEST: is attr\n" . $herecurr);
2913 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2914 ERROR
("TEST_NOT_ATTR",
2915 "TEST: is not attr ($1 is)\n". $herecurr);
2920 # check for initialisation to aggregates open brace on the next line
2921 if ($line =~ /^.\s*{/ &&
2922 $prevline =~ /(?:^|[^=])=\s*$/) {
2923 if (ERROR
("OPEN_BRACE",
2924 "that open brace { should be on the previous line\n" . $hereprev) &&
2925 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
2926 fix_delete_line
($fixlinenr - 1, $prevrawline);
2927 fix_delete_line
($fixlinenr, $rawline);
2928 my $fixedline = $prevrawline;
2929 $fixedline =~ s/\s*=\s*$/ = {/;
2930 fix_insert_line
($fixlinenr, $fixedline);
2932 $fixedline =~ s/^(.\s*){\s*/$1/;
2933 fix_insert_line
($fixlinenr, $fixedline);
2938 # Checks which are anchored on the added line.
2941 # check for malformed paths in #include statements (uses RAW line)
2942 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2944 if ($path =~ m{//}) {
2945 ERROR
("MALFORMED_INCLUDE",
2946 "malformed #include filename\n" . $herecurr);
2948 if ($path =~ "^uapi/" && $realfile =~ m@
\binclude
/uapi
/@
) {
2949 ERROR
("UAPI_INCLUDE",
2950 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2954 # no C99 // comments
2955 if ($line =~ m{//}) {
2956 if (ERROR
("C99_COMMENTS",
2957 "do not use C99 // comments\n" . $herecurr) &&
2959 my $line = $fixed[$fixlinenr];
2960 if ($line =~ /\/\
/(.*)$/) {
2961 my $comment = trim
($1);
2962 $fixed[$fixlinenr] =~ s@\
/\/(.*)$@
/\* $comment \*/@
;
2966 # Remove C99 comments.
2968 $opline =~ s@
//.*@@
;
2970 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2971 # the whole statement.
2972 #print "APW <$lines[$realline_next - 1]>\n";
2973 if (defined $realline_next &&
2974 exists $lines[$realline_next - 1] &&
2975 !defined $suppress_export{$realline_next} &&
2976 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2977 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2978 # Handle definitions which produce identifiers with
2981 # EXPORT_SYMBOL(something_foo);
2983 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
2984 $name =~ /^${Ident}_$2/) {
2985 #print "FOO C name<$name>\n";
2986 $suppress_export{$realline_next} = 1;
2988 } elsif ($stat !~ /(?
:
2990 ^.DEFINE_
$Ident\
(\Q
$name\E\
)|
2991 ^.DECLARE_
$Ident\
(\Q
$name\E\
)|
2992 ^.LIST_HEAD\
(\Q
$name\E\
)|
2993 ^.(?
:$Storage\s
+)?
$Type\s
*\
(\s
*\
*\s
*\Q
$name\E\s
*\
)\s
*\
(|
2994 \b\Q
$name\E
(?
:\s
+$Attribute)*\s
*(?
:;|=|\
[|\
()
2996 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2997 $suppress_export{$realline_next} = 2;
2999 $suppress_export{$realline_next} = 1;
3002 if (!defined $suppress_export{$linenr} &&
3003 $prevline =~ /^.\s*$/ &&
3004 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3005 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3006 #print "FOO B <$lines[$linenr - 1]>\n";
3007 $suppress_export{$linenr} = 2;
3009 if (defined $suppress_export{$linenr} &&
3010 $suppress_export{$linenr} == 2) {
3011 WARN
("EXPORT_SYMBOL",
3012 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
3015 # check for global initialisers.
3016 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3017 if (ERROR
("GLOBAL_INITIALISERS",
3018 "do not initialise globals to 0 or NULL\n" .
3021 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
3024 # check for static initialisers.
3025 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3026 if (ERROR
("INITIALISED_STATIC",
3027 "do not initialise statics to 0 or NULL\n" .
3030 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
3034 # check for misordered declarations of char/short/int/long with signed/unsigned
3035 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3037 WARN
("MISORDERED_TYPE",
3038 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3041 # check for static const char * arrays.
3042 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3043 WARN
("STATIC_CONST_CHAR_ARRAY",
3044 "static const char * array should probably be static const char * const\n" .
3048 # check for static char foo[] = "bar" declarations.
3049 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3050 WARN
("STATIC_CONST_CHAR_ARRAY",
3051 "static char array declaration should probably be static const char\n" .
3055 # check for non-global char *foo[] = {"bar", ...} declarations.
3056 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3057 WARN
("STATIC_CONST_CHAR_ARRAY",
3058 "char * array declaration might be better as static const\n" .
3062 # check for function declarations without arguments like "int foo()"
3063 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3064 if (ERROR
("FUNCTION_WITHOUT_ARGS",
3065 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3067 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3071 # check for uses of DEFINE_PCI_DEVICE_TABLE
3072 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3073 if (WARN
("DEFINE_PCI_DEVICE_TABLE",
3074 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3076 $fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
3080 # check for new typedefs, only function parameters and sparse annotations
3082 if ($line =~ /\btypedef\s/ &&
3083 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3084 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
3085 $line !~ /\b$typeTypedefs\b/ &&
3086 $line !~ /\b__bitwise(?:__|)\b/) {
3087 WARN
("NEW_TYPEDEFS",
3088 "do not add new typedefs\n" . $herecurr);
3091 # * goes on variable not on type
3093 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3095 my ($ident, $from, $to) = ($1, $2, $2);
3097 # Should start with a space.
3098 $to =~ s/^(\S)/ $1/;
3099 # Should not end with a space.
3101 # '*'s should not have spaces between.
3102 while ($to =~ s/\*\s+\*/\*\*/) {
3105 ## print "1: from<$from> to<$to> ident<$ident>\n";
3107 if (ERROR
("POINTER_LOCATION",
3108 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3110 my $sub_from = $ident;
3111 my $sub_to = $ident;
3112 $sub_to =~ s/\Q$from\E/$to/;
3113 $fixed[$fixlinenr] =~
3114 s@\Q
$sub_from\E@
$sub_to@
;
3118 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3120 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3122 # Should start with a space.
3123 $to =~ s/^(\S)/ $1/;
3124 # Should not end with a space.
3126 # '*'s should not have spaces between.
3127 while ($to =~ s/\*\s+\*/\*\*/) {
3129 # Modifiers should have spaces.
3130 $to =~ s/(\b$Modifier$)/$1 /;
3132 ## print "2: from<$from> to<$to> ident<$ident>\n";
3133 if ($from ne $to && $ident !~ /^$Modifier$/) {
3134 if (ERROR
("POINTER_LOCATION",
3135 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3138 my $sub_from = $match;
3139 my $sub_to = $match;
3140 $sub_to =~ s/\Q$from\E/$to/;
3141 $fixed[$fixlinenr] =~
3142 s@\Q
$sub_from\E@
$sub_to@
;
3147 # # no BUG() or BUG_ON()
3148 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
3149 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3150 # print "$herecurr";
3154 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3155 WARN
("LINUX_VERSION_CODE",
3156 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
3159 # check for uses of printk_ratelimit
3160 if ($line =~ /\bprintk_ratelimit\s*\(/) {
3161 WARN
("PRINTK_RATELIMITED",
3162 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
3165 # printk should use KERN_* levels. Note that follow on printk's on the
3166 # same line do not need a level, so we use the current block context
3167 # to try and find and validate the current printk. In summary the current
3168 # printk includes all preceding printk's which have no newline on the end.
3169 # we assume the first bad printk is the one to report.
3170 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
3172 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3173 #print "CHECK<$lines[$ln - 1]\n";
3174 # we have a preceding printk if it ends
3175 # with "\n" ignore it, else it is to blame
3176 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3177 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3184 WARN
("PRINTK_WITHOUT_KERN_LEVEL",
3185 "printk() should include KERN_ facility level\n" . $herecurr);
3189 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3191 my $level = lc($orig);
3192 $level = "warn" if ($level eq "warning");
3193 my $level2 = $level;
3194 $level2 = "dbg" if ($level eq "debug");
3195 WARN
("PREFER_PR_LEVEL",
3196 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
3199 if ($line =~ /\bpr_warning\s*\(/) {
3200 if (WARN
("PREFER_PR_LEVEL",
3201 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3203 $fixed[$fixlinenr] =~
3204 s/\bpr_warning\b/pr_warn/;
3208 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3210 my $level = lc($orig);
3211 $level = "warn" if ($level eq "warning");
3212 $level = "dbg" if ($level eq "debug");
3213 WARN
("PREFER_DEV_LEVEL",
3214 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3217 # function brace can't be on same line, except for #defines of do while,
3218 # or if closed on same line
3219 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3220 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
3221 if (ERROR
("OPEN_BRACE",
3222 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3224 fix_delete_line
($fixlinenr, $rawline);
3225 my $fixed_line = $rawline;
3226 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3229 fix_insert_line
($fixlinenr, ltrim
($line1));
3230 fix_insert_line
($fixlinenr, "\+{");
3231 if ($line2 !~ /^\s*$/) {
3232 fix_insert_line
($fixlinenr, "\+\t" . trim
($line2));
3237 # open braces for enum, union and struct go on the same line.
3238 if ($line =~ /^.\s*{/ &&
3239 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
3240 if (ERROR
("OPEN_BRACE",
3241 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3242 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3243 fix_delete_line
($fixlinenr - 1, $prevrawline);
3244 fix_delete_line
($fixlinenr, $rawline);
3245 my $fixedline = rtrim
($prevrawline) . " {";
3246 fix_insert_line
($fixlinenr, $fixedline);
3247 $fixedline = $rawline;
3248 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3249 if ($fixedline !~ /^\+\s*$/) {
3250 fix_insert_line
($fixlinenr, $fixedline);
3255 # missing space after union, struct or enum definition
3256 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3258 "missing space after $1 definition\n" . $herecurr) &&
3260 $fixed[$fixlinenr] =~
3261 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3265 # Function pointer declarations
3266 # check spacing between type, funcptr, and args
3267 # canonical declaration is "type (*funcptr)(args...)"
3268 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
3270 my $pre_pointer_space = $2;
3271 my $post_pointer_space = $3;
3273 my $post_funcname_space = $5;
3274 my $pre_args_space = $6;
3276 # the $Declare variable will capture all spaces after the type
3277 # so check it for a missing trailing missing space but pointer return types
3278 # don't need a space so don't warn for those.
3279 my $post_declare_space = "";
3280 if ($declare =~ /(\s+)$/) {
3281 $post_declare_space = $1;
3282 $declare = rtrim
($declare);
3284 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
3286 "missing space after return type\n" . $herecurr);
3287 $post_declare_space = " ";
3290 # unnecessary space "type (*funcptr)(args...)"
3291 # This test is not currently implemented because these declarations are
3293 # int foo(int bar, ...)
3294 # and this is form shouldn't/doesn't generate a checkpatch warning.
3296 # elsif ($declare =~ /\s{2,}$/) {
3298 # "Multiple spaces after return type\n" . $herecurr);
3301 # unnecessary space "type ( *funcptr)(args...)"
3302 if (defined $pre_pointer_space &&
3303 $pre_pointer_space =~ /^\s/) {
3305 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3308 # unnecessary space "type (* funcptr)(args...)"
3309 if (defined $post_pointer_space &&
3310 $post_pointer_space =~ /^\s/) {
3312 "Unnecessary space before function pointer name\n" . $herecurr);
3315 # unnecessary space "type (*funcptr )(args...)"
3316 if (defined $post_funcname_space &&
3317 $post_funcname_space =~ /^\s/) {
3319 "Unnecessary space after function pointer name\n" . $herecurr);
3322 # unnecessary space "type (*funcptr) (args...)"
3323 if (defined $pre_args_space &&
3324 $pre_args_space =~ /^\s/) {
3326 "Unnecessary space before function pointer arguments\n" . $herecurr);
3329 if (show_type
("SPACING") && $fix) {
3330 $fixed[$fixlinenr] =~
3331 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
3335 # check for spacing round square brackets; allowed:
3336 # 1. with a type on the left -- int [] a;
3337 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3338 # 3. inside a curly brace -- = { [0...10] = 5 }
3339 while ($line =~ /(.*?\s)\[/g) {
3340 my ($where, $prefix) = ($-[1], $1);
3341 if ($prefix !~ /$Type\s+$/ &&
3342 ($where != 0 || $prefix !~ /^.\s+$/) &&
3343 $prefix !~ /[{,]\s+$/) {
3344 if (ERROR
("BRACKET_SPACE",
3345 "space prohibited before open square bracket '['\n" . $herecurr) &&
3347 $fixed[$fixlinenr] =~
3348 s/^(\+.*?)\s+\[/$1\[/;
3353 # check for spaces between functions and their parentheses.
3354 while ($line =~ /($Ident)\s+\(/g) {
3356 my $ctx_before = substr($line, 0, $-[1]);
3357 my $ctx = "$ctx_before$name";
3359 # Ignore those directives where spaces _are_ permitted.
3361 if|for|while|switch
|return|case
|
3362 volatile
|__volatile__
|
3363 __attribute__
|format
|__extension__
|
3366 # cpp #define statements have non-optional spaces, ie
3367 # if there is a space between the name and the open
3368 # parenthesis it is simply not a parameter group.
3369 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3371 # cpp #elif statement condition may start with a (
3372 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3374 # If this whole things ends with a type its most
3375 # likely a typedef for a function.
3376 } elsif ($ctx =~ /$Type$/) {
3380 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3382 $fixed[$fixlinenr] =~
3383 s/\b$name\s+\(/$name\(/;
3388 # Check operator spacing.
3389 if (!($line=~/\#\s*include/)) {
3390 my $fixed_line = "";
3394 <<=|>>=|<=|>=|==|!=|
3395 \
+=|-=|\
*=|\
/=|%=|\
^=|\
|=|&=|
3396 =>|->|<<|>>|<|>|=|!|~|
3397 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%|
3400 my @elements = split(/($ops|;)/, $opline);
3402 ## print("element count: <" . $#elements . ">\n");
3403 ## foreach my $el (@elements) {
3404 ## print("el: <$el>\n");
3407 my @fix_elements = ();
3410 foreach my $el (@elements) {
3411 push(@fix_elements, substr($rawline, $off, length($el)));
3412 $off += length($el);
3417 my $blank = copy_spacing
($opline);
3418 my $last_after = -1;
3420 for (my $n = 0; $n < $#elements; $n += 2) {
3422 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3424 ## print("n: <$n> good: <$good>\n");
3426 $off += length($elements[$n]);
3428 # Pick up the preceding and succeeding characters.
3429 my $ca = substr($opline, 0, $off);
3431 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3432 $cc = substr($opline, $off + length($elements[$n + 1]));
3434 my $cb = "$ca$;$cc";
3437 $a = 'V' if ($elements[$n] ne '');
3438 $a = 'W' if ($elements[$n] =~ /\s$/);
3439 $a = 'C' if ($elements[$n] =~ /$;$/);
3440 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3441 $a = 'O' if ($elements[$n] eq '');
3442 $a = 'E' if ($ca =~ /^\s*$/);
3444 my $op = $elements[$n + 1];
3447 if (defined $elements[$n + 2]) {
3448 $c = 'V' if ($elements[$n + 2] ne '');
3449 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
3450 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
3451 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3452 $c = 'O' if ($elements[$n + 2] eq '');
3453 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
3458 my $ctx = "${a}x${c}";
3460 my $at = "(ctx:$ctx)";
3462 my $ptr = substr($blank, 0, $off) . "^";
3463 my $hereptr = "$hereline$ptr\n";
3465 # Pull out the value of this operator.
3466 my $op_type = substr($curr_values, $off + 1, 1);
3468 # Get the full operator variant.
3469 my $opv = $op . substr($curr_vars, $off, 1);
3471 # Ignore operators passed as parameters.
3472 if ($op_type ne 'V' &&
3473 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3476 # } elsif ($op =~ /^$;+$/) {
3478 # ; should have either the end of line or a space or \ after it
3479 } elsif ($op eq ';') {
3480 if ($ctx !~ /.x[WEBC]/ &&
3481 $cc !~ /^\\/ && $cc !~ /^;/) {
3482 if (ERROR
("SPACING",
3483 "space required after that '$op' $at\n" . $hereptr)) {
3484 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]) . " ";
3490 } elsif ($op eq '//') {
3492 # : when part of a bitfield
3493 } elsif ($opv eq ':B') {
3494 # skip the bitfield test for now
3498 } elsif ($op eq '->') {
3499 if ($ctx =~ /Wx.|.xW/) {
3500 if (ERROR
("SPACING",
3501 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3502 $good = rtrim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
3503 if (defined $fix_elements[$n + 2]) {
3504 $fix_elements[$n + 2] =~ s/^\s+//;
3510 # , must have a space on the right.
3511 } elsif ($op eq ',') {
3512 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
3513 if (ERROR
("SPACING",
3514 "space required after that '$op' $at\n" . $hereptr)) {
3515 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]) . " ";
3521 # '*' as part of a type definition -- reported already.
3522 } elsif ($opv eq '*_') {
3523 #warn "'*' is part of type\n";
3525 # unary operators should have a space before and
3526 # none after. May be left adjacent to another
3527 # unary operator, or a cast
3528 } elsif ($op eq '!' || $op eq '~' ||
3529 $opv eq '*U' || $opv eq '-U' ||
3530 $opv eq '&U' || $opv eq '&&U') {
3531 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
3532 if (ERROR
("SPACING",
3533 "space required before that '$op' $at\n" . $hereptr)) {
3534 if ($n != $last_after + 2) {
3535 $good = $fix_elements[$n] . " " . ltrim
($fix_elements[$n + 1]);
3540 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3541 # A unary '*' may be const
3543 } elsif ($ctx =~ /.xW/) {
3544 if (ERROR
("SPACING",
3545 "space prohibited after that '$op' $at\n" . $hereptr)) {
3546 $good = $fix_elements[$n] . rtrim
($fix_elements[$n + 1]);
3547 if (defined $fix_elements[$n + 2]) {
3548 $fix_elements[$n + 2] =~ s/^\s+//;
3554 # unary ++ and unary -- are allowed no space on one side.
3555 } elsif ($op eq '++' or $op eq '--') {
3556 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
3557 if (ERROR
("SPACING",
3558 "space required one side of that '$op' $at\n" . $hereptr)) {
3559 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]) . " ";
3563 if ($ctx =~ /Wx[BE]/ ||
3564 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
3565 if (ERROR
("SPACING",
3566 "space prohibited before that '$op' $at\n" . $hereptr)) {
3567 $good = rtrim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
3571 if ($ctx =~ /ExW/) {
3572 if (ERROR
("SPACING",
3573 "space prohibited after that '$op' $at\n" . $hereptr)) {
3574 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]);
3575 if (defined $fix_elements[$n + 2]) {
3576 $fix_elements[$n + 2] =~ s/^\s+//;
3582 # << and >> may either have or not have spaces both sides
3583 } elsif ($op eq '<<' or $op eq '>>' or
3584 $op eq '&' or $op eq '^' or $op eq '|' or
3585 $op eq '+' or $op eq '-' or
3586 $op eq '*' or $op eq '/' or
3589 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
3590 if (ERROR
("SPACING",
3591 "need consistent spacing around '$op' $at\n" . $hereptr)) {
3592 $good = rtrim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]) . " ";
3593 if (defined $fix_elements[$n + 2]) {
3594 $fix_elements[$n + 2] =~ s/^\s+//;
3600 # A colon needs no spaces before when it is
3601 # terminating a case value or a label.
3602 } elsif ($opv eq ':C' || $opv eq ':L') {
3603 if ($ctx =~ /Wx./) {
3604 if (ERROR
("SPACING",
3605 "space prohibited before that '$op' $at\n" . $hereptr)) {
3606 $good = rtrim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
3611 # All the others need spaces both sides.
3612 } elsif ($ctx !~ /[EWC]x[CWE]/) {
3615 # Ignore email addresses <foo@bar>
3617 $cc =~ /^\S+\@\S+>/) ||
3619 $ca =~ /<\S+\@\S+$/))
3624 # messages are ERROR, but ?: are CHK
3626 my $msg_type = \
&ERROR
;
3627 $msg_type = \
&CHK
if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3629 if (&{$msg_type}("SPACING",
3630 "spaces required around that '$op' $at\n" . $hereptr)) {
3631 $good = rtrim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]) . " ";
3632 if (defined $fix_elements[$n + 2]) {
3633 $fix_elements[$n + 2] =~ s/^\s+//;
3639 $off += length($elements[$n + 1]);
3641 ## print("n: <$n> GOOD: <$good>\n");
3643 $fixed_line = $fixed_line . $good;
3646 if (($#elements % 2) == 0) {
3647 $fixed_line = $fixed_line . $fix_elements[$#elements];
3650 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3651 $fixed[$fixlinenr] = $fixed_line;
3657 # check for whitespace before a non-naked semicolon
3658 if ($line =~ /^\+.*\S\s+;\s*$/) {
3660 "space prohibited before semicolon\n" . $herecurr) &&
3662 1 while $fixed[$fixlinenr] =~
3663 s/^(\+.*\S)\s+;/$1;/;
3667 # check for multiple assignments
3668 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3669 CHK
("MULTIPLE_ASSIGNMENTS",
3670 "multiple assignments should be avoided\n" . $herecurr);
3673 ## # check for multiple declarations, allowing for a function declaration
3675 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3676 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3678 ## # Remove any bracketed sections to ensure we do not
3679 ## # falsly report the parameters of functions.
3681 ## while ($ln =~ s/\([^\(\)]*\)//g) {
3683 ## if ($ln =~ /,/) {
3684 ## WARN("MULTIPLE_DECLARATION",
3685 ## "declaring multiple variables together should be avoided\n" . $herecurr);
3689 #need space before brace following if, while, etc
3690 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3692 if (ERROR
("SPACING",
3693 "space required before the open brace '{'\n" . $herecurr) &&
3695 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
3699 ## # check for blank lines before declarations
3700 ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3701 ## $prevrawline =~ /^.\s*$/) {
3703 ## "No blank lines before declarations\n" . $hereprev);
3707 # closing brace should have a space following it when it has anything
3709 if ($line =~ /}(?!(?:,|;|\)))\S/) {
3710 if (ERROR
("SPACING",
3711 "space required after that close brace '}'\n" . $herecurr) &&
3713 $fixed[$fixlinenr] =~
3714 s/}((?!(?:,|;|\)))\S)/} $1/;
3718 # check spacing on square brackets
3719 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
3720 if (ERROR
("SPACING",
3721 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3723 $fixed[$fixlinenr] =~
3727 if ($line =~ /\s\]/) {
3728 if (ERROR
("SPACING",
3729 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3731 $fixed[$fixlinenr] =~
3736 # check spacing on parentheses
3737 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3738 $line !~ /for\s*\(\s+;/) {
3739 if (ERROR
("SPACING",
3740 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3742 $fixed[$fixlinenr] =~
3746 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3747 $line !~ /for\s*\(.*;\s+\)/ &&
3748 $line !~ /:\s+\)/) {
3749 if (ERROR
("SPACING",
3750 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3752 print("fixlinenr: <$fixlinenr> fixed[fixlinenr]: <$fixed[$fixlinenr]>\n");
3753 $fixed[$fixlinenr] =~
3758 # check unnecessary parentheses around addressof/dereference single $Lvals
3759 # ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3761 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3762 CHK
("UNNECESSARY_PARENTHESES",
3763 "Unnecessary parentheses around $1\n" . $herecurr);
3766 #goto labels aren't indented, allow a single space however
3767 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
3768 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
3769 if (WARN
("INDENTED_LABEL",
3770 "labels should not be indented\n" . $herecurr) &&
3772 $fixed[$fixlinenr] =~
3777 # return is not a function
3778 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3780 if ($^V
&& $^V
ge 5.10.0 &&
3781 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3783 $value = deparenthesize
($value);
3784 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3785 ERROR
("RETURN_PARENTHESES",
3786 "return is not a function, parentheses are not required\n" . $herecurr);
3788 } elsif ($spacing !~ /\s+/) {
3790 "space required before the open parenthesis '('\n" . $herecurr);
3794 # unnecessary return in a void function
3795 # at end-of-function, with the previous line a single leading tab, then return;
3796 # and the line before that not a goto label target like "out:"
3797 if ($sline =~ /^[ \+]}\s*$/ &&
3798 $prevline =~ /^\+\treturn\s*;\s*$/ &&
3800 $lines[$linenr - 3] =~ /^[ +]/ &&
3801 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
3803 "void function return statements are not generally useful\n" . $hereprev);
3806 # if statements using unnecessary parentheses - ie: if ((foo == bar))
3807 if ($^V
&& $^V
ge 5.10.0 &&
3808 $line =~ /\bif\s*((?:\(\s*){2,})/) {
3809 my $openparens = $1;
3810 my $count = $openparens =~ tr@\
(@\
(@
;
3812 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3813 my $comp = $4; #Not $1 because of $LvalOrFunc
3814 $msg = " - maybe == should be = ?" if ($comp eq "==");
3815 WARN
("UNNECESSARY_PARENTHESES",
3816 "Unnecessary parentheses$msg\n" . $herecurr);
3820 # Return of what appears to be an errno should normally be -'ve
3821 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3823 if ($name ne 'EOF' && $name ne 'ERROR') {
3824 WARN
("USE_NEGATIVE_ERRNO",
3825 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
3829 # Need a space before open parenthesis after if, while etc
3830 if ($line =~ /\b(if|while|for|switch)\(/) {
3831 if (ERROR
("SPACING",
3832 "space required before the open parenthesis '('\n" . $herecurr) &&
3834 $fixed[$fixlinenr] =~
3835 s/\b(if|while|for|switch)\(/$1 \(/;
3839 # Check for illegal assignment in if conditional -- and check for trailing
3840 # statements after the conditional.
3841 if ($line =~ /do\s*(?!{)/) {
3842 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3843 ctx_statement_block
($linenr, $realcnt, 0)
3844 if (!defined $stat);
3845 my ($stat_next) = ctx_statement_block
($line_nr_next,
3846 $remain_next, $off_next);
3847 $stat_next =~ s/\n./\n /g;
3848 ##print "stat<$stat> stat_next<$stat_next>\n";
3850 if ($stat_next =~ /^\s*while\b/) {
3851 # If the statement carries leading newlines,
3852 # then count those as offsets.
3854 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3856 statement_rawlines
($whitespace) - 1;
3858 $suppress_whiletrailers{$line_nr_next +
3862 if (!defined $suppress_whiletrailers{$linenr} &&
3863 defined($stat) && defined($cond) &&
3864 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3865 my ($s, $c) = ($stat, $cond);
3867 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3868 ERROR
("ASSIGN_IN_IF",
3869 "do not use assignment in if condition\n" . $herecurr);
3872 # Find out what is on the end of the line after the
3874 substr($s, 0, length($c), '');
3876 $s =~ s/$;//g; # Remove any comments
3877 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3878 $c !~ /}\s*while\s*/)
3880 # Find out how long the conditional actually is.
3881 my @newlines = ($c =~ /\n/gs);
3882 my $cond_lines = 1 + $#newlines;
3885 $stat_real = raw_line
($linenr, $cond_lines)
3886 . "\n" if ($cond_lines);
3887 if (defined($stat_real) && $cond_lines > 1) {
3888 $stat_real = "[...]\n$stat_real";
3891 ERROR
("TRAILING_STATEMENTS",
3892 "trailing statements should be on next line\n" . $herecurr . $stat_real);
3896 # Check for bitwise tests written as boolean
3908 WARN
("HEXADECIMAL_BOOLEAN_TEST",
3909 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
3912 # if and else should not have general statements after it
3913 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3915 $s =~ s/$;//g; # Remove any comments
3916 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3917 ERROR
("TRAILING_STATEMENTS",
3918 "trailing statements should be on next line\n" . $herecurr);
3921 # if should not continue a brace
3922 if ($line =~ /}\s*if\b/) {
3923 ERROR
("TRAILING_STATEMENTS",
3924 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
3927 # case and default should not have general statements after them
3928 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3930 (?
:\s
*$;*)(?
:\s
*{)?
(?
:\s
*$;*)(?
:\s
*\\)?\s
*$|
3934 ERROR
("TRAILING_STATEMENTS",
3935 "trailing statements should be on next line\n" . $herecurr);
3938 # Check for }<nl>else {, these must be at the same
3939 # indent level to be relevant to each other.
3940 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
3941 $previndent == $indent) {
3942 if (ERROR
("ELSE_AFTER_BRACE",
3943 "else should follow close brace '}'\n" . $hereprev) &&
3944 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3945 fix_delete_line
($fixlinenr - 1, $prevrawline);
3946 fix_delete_line
($fixlinenr, $rawline);
3947 my $fixedline = $prevrawline;
3948 $fixedline =~ s/}\s*$//;
3949 if ($fixedline !~ /^\+\s*$/) {
3950 fix_insert_line
($fixlinenr, $fixedline);
3952 $fixedline = $rawline;
3953 $fixedline =~ s/^(.\s*)else/$1} else/;
3954 fix_insert_line
($fixlinenr, $fixedline);
3958 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
3959 $previndent == $indent) {
3960 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
3962 # Find out what is on the end of the line after the
3964 substr($s, 0, length($c), '');
3967 if ($s =~ /^\s*;/) {
3968 if (ERROR
("WHILE_AFTER_BRACE",
3969 "while should follow close brace '}'\n" . $hereprev) &&
3970 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3971 fix_delete_line
($fixlinenr - 1, $prevrawline);
3972 fix_delete_line
($fixlinenr, $rawline);
3973 my $fixedline = $prevrawline;
3974 my $trailing = $rawline;
3975 $trailing =~ s/^\+//;
3976 $trailing = trim
($trailing);
3977 $fixedline =~ s/}\s*$/} $trailing/;
3978 fix_insert_line
($fixlinenr, $fixedline);
3983 #Specific variable tests
3984 while ($line =~ m{($Constant|$Lval)}g) {
3987 #gcc binary extension
3988 if ($var =~ /^$Binary$/) {
3989 if (WARN
("GCC_BINARY_CONSTANT",
3990 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3992 my $hexval = sprintf("0x%x", oct($var));
3993 $fixed[$fixlinenr] =~
3994 s/\b$var\b/$hexval/;
3999 if ($var !~ /^$Constant$/ &&
4000 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
4001 #Ignore Page<foo> variants
4002 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
4003 #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4004 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
4005 while ($var =~ m{($Ident)}g) {
4007 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4009 seed_camelcase_includes
();
4010 if (!$file && !$camelcase_file_seeded) {
4011 seed_camelcase_file
($realfile);
4012 $camelcase_file_seeded = 1;
4015 if (!defined $camelcase{$word}) {
4016 $camelcase{$word} = 1;
4018 "Avoid CamelCase: <$word>\n" . $herecurr);
4024 #no spaces allowed after \ in define
4025 if ($line =~ /\#\s*define.*\\\s+$/) {
4026 if (WARN
("WHITESPACE_AFTER_LINE_CONTINUATION",
4027 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4029 $fixed[$fixlinenr] =~ s/\s+$//;
4033 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
4034 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4036 my $checkfile = "include/linux/$file";
4037 if (-f
"$root/$checkfile" &&
4038 $realfile ne $checkfile &&
4039 $1 !~ /$allowed_asm_includes/)
4041 if ($realfile =~ m{^arch/}) {
4042 CHK
("ARCH_INCLUDE_LINUX",
4043 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4045 WARN
("INCLUDE_LINUX",
4046 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4051 # multi-statement macros should be enclosed in a do while loop, grab the
4052 # first statement and ensure its the whole macro if its not enclosed
4053 # in a known good container
4054 if ($realfile !~ m@
/vmlinux
.lds
.h
$@
&&
4055 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4058 my ($off, $dstat, $dcond, $rest);
4060 ($dstat, $dcond, $ln, $cnt, $off) =
4061 ctx_statement_block
($linenr, $realcnt, 0);
4063 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4064 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4066 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4068 $dstat =~ s/\\\n.//g;
4069 $dstat =~ s/^\s*//s;
4070 $dstat =~ s/\s*$//s;
4072 # Flatten any parentheses and braces
4073 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4074 $dstat =~ s/\{[^\{\}]*\}/1/ ||
4075 $dstat =~ s/\[[^\[\]]*\]/1/)
4079 # Flatten any obvious string concatentation.
4080 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4081 $dstat =~ s/$Ident\s*("X*")/$1/)
4085 my $exceptions = qr{
4097 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4099 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4100 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
4101 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4102 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
4103 $dstat !~ /$exceptions/ &&
4104 $dstat !~ /^\.$Ident\s*=/ && # .foo =
4105 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
4106 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
4107 $dstat !~ /^for\s*$Constant$/ && # for (...)
4108 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4109 $dstat !~ /^do\s*{/ && # do {...
4110 $dstat !~ /^\({/ && # ({...
4111 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4114 my $herectx = $here . "\n";
4115 my $cnt = statement_rawlines
($ctx);
4117 for (my $n = 0; $n < $cnt; $n++) {
4118 $herectx .= raw_line
($linenr, $n) . "\n";
4121 if ($dstat =~ /;/) {
4122 ERROR
("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4123 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4125 ERROR
("COMPLEX_MACRO",
4126 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
4130 # check for line continuations outside of #defines, preprocessor #, and asm
4133 if ($prevline !~ /^..*\\$/ &&
4134 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4135 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
4136 $line =~ /^\+.*\\$/) {
4137 WARN
("LINE_CONTINUATIONS",
4138 "Avoid unnecessary line continuations\n" . $herecurr);
4142 # do {} while (0) macro tests:
4143 # single-statement macros do not need to be enclosed in do while (0) loop,
4144 # macro should not end with a semicolon
4145 if ($^V
&& $^V
ge 5.10.0 &&
4146 $realfile !~ m@
/vmlinux
.lds
.h
$@
&&
4147 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4150 my ($off, $dstat, $dcond, $rest);
4152 ($dstat, $dcond, $ln, $cnt, $off) =
4153 ctx_statement_block
($linenr, $realcnt, 0);
4156 $dstat =~ s/\\\n.//g;
4158 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4163 my $cnt = statement_rawlines
($ctx);
4164 my $herectx = $here . "\n";
4166 for (my $n = 0; $n < $cnt; $n++) {
4167 $herectx .= raw_line
($linenr, $n) . "\n";
4170 if (($stmts =~ tr/;/;/) == 1 &&
4171 $stmts !~ /^\s*(if|while|for|switch)\b/) {
4172 WARN
("SINGLE_STATEMENT_DO_WHILE_MACRO",
4173 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4175 if (defined $semis && $semis ne "") {
4176 WARN
("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4177 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4179 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4181 my $cnt = statement_rawlines
($ctx);
4182 my $herectx = $here . "\n";
4184 for (my $n = 0; $n < $cnt; $n++) {
4185 $herectx .= raw_line
($linenr, $n) . "\n";
4188 WARN
("TRAILING_SEMICOLON",
4189 "macros should not use a trailing semicolon\n" . "$herectx");
4193 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4194 # all assignments may have only one of the following with an assignment:
4197 # VMLINUX_SYMBOL(...)
4198 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4199 WARN
("MISSING_VMLINUX_SYMBOL",
4200 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4203 # check for redundant bracing round if etc
4204 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4205 my ($level, $endln, @chunks) =
4206 ctx_statement_full
($linenr, $realcnt, 1);
4207 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4208 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4209 if ($#chunks > 0 && $level == 0) {
4213 my $herectx = $here . "\n";
4214 my $ln = $linenr - 1;
4215 for my $chunk (@chunks) {
4216 my ($cond, $block) = @
{$chunk};
4218 # If the condition carries leading newlines, then count those as offsets.
4219 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4220 my $offset = statement_rawlines
($whitespace) - 1;
4222 $allowed[$allow] = 0;
4223 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4225 # We have looked at and allowed this specific line.
4226 $suppress_ifbraces{$ln + $offset} = 1;
4228 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4229 $ln += statement_rawlines
($block) - 1;
4231 substr($block, 0, length($cond), '');
4233 $seen++ if ($block =~ /^\s*{/);
4235 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4236 if (statement_lines
($cond) > 1) {
4237 #print "APW: ALLOWED: cond<$cond>\n";
4238 $allowed[$allow] = 1;
4240 if ($block =~/\b(?:if|for|while)\b/) {
4241 #print "APW: ALLOWED: block<$block>\n";
4242 $allowed[$allow] = 1;
4244 if (statement_block_size
($block) > 1) {
4245 #print "APW: ALLOWED: lines block<$block>\n";
4246 $allowed[$allow] = 1;
4251 my $sum_allowed = 0;
4252 foreach (@allowed) {
4255 if ($sum_allowed == 0) {
4257 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4258 } elsif ($sum_allowed != $allow &&
4261 "braces {} should be used on all arms of this statement\n" . $herectx);
4266 if (!defined $suppress_ifbraces{$linenr - 1} &&
4267 $line =~ /\b(if|while|for|else)\b/) {
4270 # Check the pre-context.
4271 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4272 #print "APW: ALLOWED: pre<$1>\n";
4276 my ($level, $endln, @chunks) =
4277 ctx_statement_full
($linenr, $realcnt, $-[0]);
4279 # Check the condition.
4280 my ($cond, $block) = @
{$chunks[0]};
4281 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4282 if (defined $cond) {
4283 substr($block, 0, length($cond), '');
4285 if (statement_lines
($cond) > 1) {
4286 #print "APW: ALLOWED: cond<$cond>\n";
4289 if ($block =~/\b(?:if|for|while)\b/) {
4290 #print "APW: ALLOWED: block<$block>\n";
4293 if (statement_block_size
($block) > 1) {
4294 #print "APW: ALLOWED: lines block<$block>\n";
4297 # Check the post-context.
4298 if (defined $chunks[1]) {
4299 my ($cond, $block) = @
{$chunks[1]};
4300 if (defined $cond) {
4301 substr($block, 0, length($cond), '');
4303 if ($block =~ /^\s*\{/) {
4304 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4308 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
4309 my $herectx = $here . "\n";
4310 my $cnt = statement_rawlines
($block);
4312 for (my $n = 0; $n < $cnt; $n++) {
4313 $herectx .= raw_line
($linenr, $n) . "\n";
4317 "braces {} are not necessary for single statement blocks\n" . $herectx);
4321 # check for unnecessary blank lines around braces
4322 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
4324 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
4326 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
4328 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
4331 # no volatiles please
4332 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4333 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4335 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
4339 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4340 CHK
("REDUNDANT_CODE",
4341 "if this code is redundant consider removing it\n" .
4345 # check for needless "if (<foo>) fn(<foo>)" uses
4346 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4347 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4348 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4350 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
4354 # check for unnecessary "Out of Memory" messages
4355 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4356 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4357 (defined $1 || defined $3) &&
4360 my $testline = $lines[$linenr - 3];
4362 my ($s, $c) = ctx_statement_block
($linenr - 3, $realcnt, 0);
4363 # print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4365 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4367 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4371 # check for bad placement of section $InitAttribute (e.g.: __initdata)
4372 if ($line =~ /(\b$InitAttribute\b)/) {
4374 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4377 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4378 ERROR
("MISPLACED_INIT",
4379 "$attr should be placed after $var\n" . $herecurr)) ||
4380 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4381 WARN
("MISPLACED_INIT",
4382 "$attr should be placed after $var\n" . $herecurr))) &&
4384 $fixed[$fixlinenr] =~ s/(\bstatic\s+(?:const\s+)?)(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*([=;])\s*/"$1" . trim(string_find_replace($2, "\\s*$attr\\s*", " ")) . " " . trim(string_find_replace($3, "\\s*$attr\\s*", "")) . " $attr" . ("$4" eq ";" ? ";" : " = ")/e;
4389 # check for $InitAttributeData (ie: __initdata) with const
4390 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4392 $attr =~ /($InitAttributePrefix)(.*)/;
4393 my $attr_prefix = $1;
4395 if (ERROR
("INIT_ATTRIBUTE",
4396 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4398 $fixed[$fixlinenr] =~
4399 s/$InitAttributeData/${attr_prefix}initconst/;
4403 # check for $InitAttributeConst (ie: __initconst) without const
4404 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4406 if (ERROR
("INIT_ATTRIBUTE",
4407 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4409 my $lead = $fixed[$fixlinenr] =~
4410 /(^\+\s*(?:static\s+))/;
4412 $lead = "$lead " if ($lead !~ /^\+$/);
4413 $lead = "${lead}const ";
4414 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
4418 # don't use __constant_<foo> functions outside of include/uapi/
4419 if ($realfile !~ m@
^include
/uapi/@
&&
4420 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4421 my $constant_func = $1;
4422 my $func = $constant_func;
4423 $func =~ s/^__constant_//;
4424 if (WARN
("CONSTANT_CONVERSION",
4425 "$constant_func should be $func\n" . $herecurr) &&
4427 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
4431 # prefer usleep_range over udelay
4432 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
4434 # ignore udelay's < 10, however
4435 if (! ($delay < 10) ) {
4437 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4439 if ($delay > 2000) {
4441 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
4445 # warn about unexpectedly long msleep's
4446 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4449 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4453 # check for comparisons of jiffies
4454 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4455 WARN
("JIFFIES_COMPARISON",
4456 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4459 # check for comparisons of get_jiffies_64()
4460 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4461 WARN
("JIFFIES_COMPARISON",
4462 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4465 # warn about #ifdefs in C files
4466 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
4467 # print "#ifdef in C files should be avoided\n";
4468 # print "$herecurr";
4472 # warn about spacing in #ifdefs
4473 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
4474 if (ERROR
("SPACING",
4475 "exactly one space required after that #$1\n" . $herecurr) &&
4477 $fixed[$fixlinenr] =~
4478 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4483 # check for spinlock_t definitions without a comment.
4484 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4485 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
4487 if (!ctx_has_comment
($first_line, $linenr)) {
4488 CHK
("UNCOMMENTED_DEFINITION",
4489 "$1 definition without comment\n" . $herecurr);
4492 # check for memory barriers without a comment.
4493 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4494 if (!ctx_has_comment
($first_line, $linenr)) {
4495 WARN
("MEMORY_BARRIER",
4496 "memory barrier without comment\n" . $herecurr);
4499 # check of hardware specific defines
4500 if ($line =~ m@
^.\s
*\#\s
*if.*\b(__i386__
|__powerpc64__
|__sun__
|__s390x__
)\b@
&& $realfile !~ m
@include/asm
-@
) {
4502 "architecture specific defines should be avoided\n" . $herecurr);
4505 # Check that the storage class is at the beginning of a declaration
4506 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4507 WARN
("STORAGE_CLASS",
4508 "storage class should be at the beginning of the declaration\n" . $herecurr)
4511 # check the location of the inline attribute, that it is between
4512 # storage class and type.
4513 if ($line =~ /\b$Type\s+$Inline\b/ ||
4514 $line =~ /\b$Inline\s+$Storage\b/) {
4515 ERROR
("INLINE_LOCATION",
4516 "inline keyword should sit between storage class and type\n" . $herecurr);
4519 # Check for __inline__ and __inline, prefer inline
4520 if ($realfile !~ m@
\binclude
/uapi/@
&&
4521 $line =~ /\b(__inline__|__inline)\b/) {
4523 "plain inline is preferred over $1\n" . $herecurr) &&
4525 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
4530 # Check for __attribute__ packed, prefer __packed
4531 if ($realfile !~ m@
\binclude
/uapi/@
&&
4532 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4533 WARN
("PREFER_PACKED",
4534 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
4537 # Check for __attribute__ aligned, prefer __aligned
4538 if ($realfile !~ m@
\binclude
/uapi/@
&&
4539 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4540 WARN
("PREFER_ALIGNED",
4541 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
4544 # Check for __attribute__ format(printf, prefer __printf
4545 if ($realfile !~ m@
\binclude
/uapi/@
&&
4546 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4547 if (WARN
("PREFER_PRINTF",
4548 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4550 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4555 # Check for __attribute__ format(scanf, prefer __scanf
4556 if ($realfile !~ m@
\binclude
/uapi/@
&&
4557 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4558 if (WARN
("PREFER_SCANF",
4559 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4561 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4565 # check for sizeof(&)
4566 if ($line =~ /\bsizeof\s*\(\s*\&/) {
4567 WARN
("SIZEOF_ADDRESS",
4568 "sizeof(& should be avoided\n" . $herecurr);
4571 # check for sizeof without parenthesis
4572 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4573 if (WARN
("SIZEOF_PARENTHESIS",
4574 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4576 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4580 # check for line continuations in quoted strings with odd counts of "
4581 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4582 WARN
("LINE_CONTINUATIONS",
4583 "Avoid line continuations in quoted strings\n" . $herecurr);
4586 # check for struct spinlock declarations
4587 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4588 WARN
("USE_SPINLOCK_T",
4589 "struct spinlock should be spinlock_t\n" . $herecurr);
4592 # check for seq_printf uses that could be seq_puts
4593 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4594 my $fmt = get_quoted_string
($line, $rawline);
4595 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
4596 if (WARN
("PREFER_SEQ_PUTS",
4597 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4599 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
4604 # Check for misused memsets
4605 if ($^V
&& $^V
ge 5.10.0 &&
4607 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4613 if ($ms_size =~ /^(0x|)0$/i) {
4615 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4616 } elsif ($ms_size =~ /^(0x|)1$/i) {
4618 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4622 # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4623 if ($^V
&& $^V
ge 5.10.0 &&
4624 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4625 if (WARN
("PREFER_ETHER_ADDR_COPY",
4626 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4628 $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
4632 # typecasts on min/max could be min_t/max_t
4633 if ($^V
&& $^V
ge 5.10.0 &&
4635 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4636 if (defined $2 || defined $7) {
4638 my $cast1 = deparenthesize
($2);
4640 my $cast2 = deparenthesize
($7);
4644 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4645 $cast = "$cast1 or $cast2";
4646 } elsif ($cast1 ne "") {
4652 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4656 # check usleep_range arguments
4657 if ($^V
&& $^V
ge 5.10.0 &&
4659 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4663 WARN
("USLEEP_RANGE",
4664 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4665 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4667 WARN
("USLEEP_RANGE",
4668 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4672 # check for naked sscanf
4673 if ($^V
&& $^V
ge 5.10.0 &&
4675 $line =~ /\bsscanf\b/ &&
4676 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4677 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4678 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4679 my $lc = $stat =~ tr@
\n@@
;
4680 $lc = $lc + $linenr;
4681 my $stat_real = raw_line
($linenr, 0);
4682 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4683 $stat_real = $stat_real . "\n" . raw_line
($count, 0);
4685 WARN
("NAKED_SSCANF",
4686 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4689 # check for simple sscanf that should be kstrto<foo>
4690 if ($^V
&& $^V
ge 5.10.0 &&
4692 $line =~ /\bsscanf\b/) {
4693 my $lc = $stat =~ tr@
\n@@
;
4694 $lc = $lc + $linenr;
4695 my $stat_real = raw_line
($linenr, 0);
4696 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4697 $stat_real = $stat_real . "\n" . raw_line
($count, 0);
4699 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4701 my $count = $format =~ tr@
%@
%@
;
4703 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4704 WARN
("SSCANF_TO_KSTRTO",
4705 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4710 # check for new externs in .h files.
4711 if ($realfile =~ /\.h$/ &&
4712 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4713 if (CHK
("AVOID_EXTERNS",
4714 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
4716 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
4720 # check for new externs in .c files.
4721 if ($realfile =~ /\.c$/ && defined $stat &&
4722 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
4724 my $function_name = $1;
4725 my $paren_space = $2;
4728 if (defined $cond) {
4729 substr($s, 0, length($cond), '');
4731 if ($s =~ /^\s*;/ &&
4732 $function_name ne 'uninitialized_var')
4734 WARN
("AVOID_EXTERNS",
4735 "externs should be avoided in .c files\n" . $herecurr);
4738 if ($paren_space =~ /\n/) {
4739 WARN
("FUNCTION_ARGUMENTS",
4740 "arguments for function declarations should follow identifier\n" . $herecurr);
4743 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4744 $stat =~ /^.\s*extern\s+/)
4746 WARN
("AVOID_EXTERNS",
4747 "externs should be avoided in .c files\n" . $herecurr);
4750 # checks for new __setup's
4751 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4754 if (!grep(/$name/, @setup_docs)) {
4755 CHK
("UNDOCUMENTED_SETUP",
4756 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
4760 # check for pointless casting of kmalloc return
4761 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
4762 WARN
("UNNECESSARY_CASTS",
4763 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
4767 # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4768 if ($^V
&& $^V
ge 5.10.0 &&
4769 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4770 CHK
("ALLOC_SIZEOF_STRUCT",
4771 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4774 # check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
4775 if ($^V
&& $^V
ge 5.10.0 &&
4776 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
4780 my $newfunc = "kmalloc_array";
4781 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
4784 if ($a1 =~ /^sizeof\s*\S/) {
4788 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
4789 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
4790 if (WARN
("ALLOC_WITH_MULTIPLY",
4791 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
4793 $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
4799 # check for krealloc arg reuse
4800 if ($^V
&& $^V
ge 5.10.0 &&
4801 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4802 WARN
("KREALLOC_ARG_REUSE",
4803 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4806 # check for alloc argument mismatch
4807 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
4808 WARN
("ALLOC_ARRAY_ARGS",
4809 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
4812 # check for multiple semicolons
4813 if ($line =~ /;\s*;\s*$/) {
4814 if (WARN
("ONE_SEMICOLON",
4815 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4817 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
4821 # check for case / default statements not preceded by break/fallthrough/switch
4822 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4824 my $has_statement = 0;
4826 my $prevline = $linenr;
4827 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
4829 my $rline = $rawlines[$prevline - 1];
4830 my $fline = $lines[$prevline - 1];
4831 last if ($fline =~ /^\@\@/);
4832 next if ($fline =~ /^\-/);
4833 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4834 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4835 next if ($fline =~ /^.[\s$;]*$/);
4838 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4840 if (!$has_break && $has_statement) {
4841 WARN
("MISSING_BREAK",
4842 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4846 # check for switch/default statements without a break;
4847 if ($^V
&& $^V
ge 5.10.0 &&
4849 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4851 my $herectx = $here . "\n";
4852 my $cnt = statement_rawlines
($stat);
4853 for (my $n = 0; $n < $cnt; $n++) {
4854 $herectx .= raw_line
($linenr, $n) . "\n";
4856 WARN
("DEFAULT_NO_BREAK",
4857 "switch default: should use break\n" . $herectx);
4860 # check for gcc specific __FUNCTION__
4861 if ($line =~ /\b__FUNCTION__\b/) {
4862 if (WARN
("USE_FUNC",
4863 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
4865 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
4869 # check for use of yield()
4870 if ($line =~ /\byield\s*\(\s*\)/) {
4872 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
4875 # check for comparisons against true and false
4876 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4884 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4886 my $type = lc($otype);
4887 if ($type =~ /^(?:true|false)$/) {
4888 if (("$test" eq "==" && "$type" eq "true") ||
4889 ("$test" eq "!=" && "$type" eq "false")) {
4893 CHK
("BOOL_COMPARISON",
4894 "Using comparison to $otype is error prone\n" . $herecurr);
4896 ## maybe suggesting a correct construct would better
4897 ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4902 # check for semaphores initialized locked
4903 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
4904 WARN
("CONSIDER_COMPLETION",
4905 "consider using a completion\n" . $herecurr);
4908 # recommend kstrto* over simple_strto* and strict_strto*
4909 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
4910 WARN
("CONSIDER_KSTRTO",
4911 "$1 is obsolete, use k$3 instead\n" . $herecurr);
4914 # check for __initcall(), use device_initcall() explicitly or more appropriate function please
4915 if ($line =~ /^.\s*__initcall\s*\(/) {
4916 WARN
("USE_DEVICE_INITCALL",
4917 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
4920 # check for various ops structs, ensure they are const.
4921 my $struct_ops = qr{acpi_dock_ops
|
4922 address_space_operations
|
4924 block_device_operations
|
4929 file_lock_operations
|
4939 lock_manager_operations
|
4945 pipe_buf_operations
|
4946 platform_hibernation_ops
|
4947 platform_suspend_ops
|
4952 soc_pcmcia_socket_ops
|
4958 if ($line !~ /\bconst\b/ &&
4959 $line =~ /\bstruct\s+($struct_ops)\b/) {
4960 WARN
("CONST_STRUCT",
4961 "struct $1 should normally be const\n" .
4965 # use of NR_CPUS is usually wrong
4966 # ignore definitions of NR_CPUS and usage to define arrays as likely right
4967 if ($line =~ /\bNR_CPUS\b/ &&
4968 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4969 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
4970 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4971 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4972 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
4975 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4978 # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
4979 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
4980 ERROR
("DEFINE_ARCH_HAS",
4981 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
4984 # check for %L{u,d,i} in strings
4986 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4987 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4988 $string =~ s/%%/__/g;
4989 if ($string =~ /(?<!%)%L[udi]/) {
4991 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4996 # whine mightly about in_atomic
4997 if ($line =~ /\bin_atomic\s*\(/) {
4998 if ($realfile =~ m@
^drivers
/@
) {
5000 "do not use in_atomic in drivers\n" . $herecurr);
5001 } elsif ($realfile !~ m@
^kernel
/@
) {
5003 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5007 # check for lockdep_set_novalidate_class
5008 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5009 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5010 if ($realfile !~ m@
^kernel
/lockdep@
&&
5011 $realfile !~ m@
^include
/linux/lockdep@
&&
5012 $realfile !~ m@
^drivers
/base/core@
) {
5014 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
5018 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
5019 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
5020 WARN
("EXPORTED_WORLD_WRITABLE",
5021 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
5024 # Mode permission misuses where it seems decimal should be octal
5025 # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5026 if ($^V
&& $^V
ge 5.10.0 &&
5027 $line =~ /$mode_perms_search/) {
5028 foreach my $entry (@mode_permission_funcs) {
5029 my $func = $entry->[0];
5030 my $arg_pos = $entry->[1];
5035 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5037 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5038 if ($line =~ /$test/) {
5040 $val = $6 if ($skip_args ne "");
5042 if ($val !~ /^0$/ &&
5043 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5044 length($val) ne 4)) {
5045 ERROR
("NON_OCTAL_PERMISSIONS",
5046 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5053 # If we have no input at all, then there is nothing to report on
5054 # so just keep quiet.
5055 if ($#rawlines == -1) {
5059 # In mailback mode only produce a report in the negative, for
5060 # things that appear to be patches.
5061 if ($mailback && ($clean == 1 || !$is_patch)) {
5065 # This is not a patch, and we are are in 'no-patch' mode so
5067 if (!$chk_patch && !$is_patch) {
5072 ERROR
("NOT_UNIFIED_DIFF",
5073 "Does not appear to be a unified-diff format patch\n");
5075 if ($is_patch && $chk_signoff && $signoff == 0) {
5076 ERROR
("MISSING_SIGN_OFF",
5077 "Missing Signed-off-by: line(s)\n");
5080 print report_dump
();
5081 if ($summary && !($clean == 1 && $quiet == 1)) {
5082 print "$filename " if ($summary_file);
5083 print "total: $cnt_error errors, $cnt_warn warnings, " .
5084 (($check)?
"$cnt_chk checks, " : "") .
5085 "$cnt_lines lines checked\n";
5086 print "\n" if ($quiet == 0);
5091 if ($^V
lt 5.10.0) {
5092 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5093 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5096 # If there were whitespace errors which cleanpatch can fix
5097 # then suggest that.
5098 if ($rpt_cleaners) {
5099 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5100 print " scripts/cleanfile\n\n";
5105 hash_show_words
(\
%use_type, "Used");
5106 hash_show_words
(\
%ignore_type, "Ignored");
5108 if ($clean == 0 && $fix &&
5109 ("@rawlines" ne "@fixed" ||
5110 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
5111 my $newfile = $filename;
5112 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
5116 @fixed = fix_inserted_deleted_lines
(\
@fixed, \
@fixed_inserted, \
@fixed_deleted);
5118 open($f, '>', $newfile)
5119 or die "$P: Can't open $newfile for write\n";
5120 foreach my $fixed_line (@fixed) {
5123 if ($linecount > 3) {
5124 $fixed_line =~ s/^\+//;
5125 print $f $fixed_line . "\n";
5128 print $f $fixed_line . "\n";
5135 Wrote EXPERIMENTAL
--fix correction
(s
) to
'$newfile'
5137 Do _NOT_ trust the results written to this file
.
5138 Do _NOT_ submit these changes without inspecting them
for correctness
.
5140 This EXPERIMENTAL file is simply a convenience to help rewrite patches
.
5141 No warranties
, expressed
or implied
...
5147 if ($clean == 1 && $quiet == 0) {
5148 print "$vname has no obvious style problems and is ready for submission.\n"
5150 if ($clean == 0 && $quiet == 0) {
5152 $vname has style problems
, please review
.
5154 If any of these errors are false positives
, please report
5155 them to the maintainer
, see CHECKPATCH
in MAINTAINERS
.