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
14 my $D = dirname
(abs_path
($P));
18 use Getopt
::Long
qw(:config no_auto_abbrev);
44 my $configuration_file = ".checkpatch.conf";
45 my $max_line_length = 80;
46 my $ignore_perl_version = 0;
47 my $minimum_perl_version = 5.10.0;
48 my $min_conf_desc_length = 4;
49 my $spelling_file = "$D/spelling.txt";
55 Usage
: $P [OPTION
]... [FILE
]...
60 --no-tree run without a kernel tree
61 --no-signoff
do not check
for 'Signed-off-by' line
62 --patch treat FILE as patchfile
(default)
63 --emacs emacs compile window format
64 --terse one line per report
65 -f
, --file treat FILE as regular source file
66 --subjective
, --strict enable more subjective tests
67 --types TYPE
(,TYPE2
...) show only these comma separated message types
68 --ignore TYPE
(,TYPE2
...) ignore various comma separated message types
69 --max-line-length
=n set the maximum line
length, if exceeded
, warn
70 --min-conf-desc-length
=n set the min description
length, if shorter
, warn
71 --show-types show the message
"types" in the output
72 --root
=PATH PATH to the kernel tree root
73 --no-summary suppress the per-file summary
74 --mailback only produce a report
in case of warnings
/errors
75 --summary-file include the filename
in summary
76 --debug KEY
=[0|1] turn on
/off debugging of KEY
, where KEY
is one of
77 'values', 'possible', 'type', and 'attr' (default
79 --test-only
=WORD report only warnings
/errors containing WORD
81 --fix EXPERIMENTAL
- may create horrible results
82 If correctable single-line errors exist
, create
83 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
84 with potential errors corrected to the preferred
86 --fix-inplace EXPERIMENTAL
- may create horrible results
87 Is the same as
--fix
, but overwrites the input
88 file
. It
's your fault if there's
no backup
or git
89 --ignore-perl-version override checking of perl version
. expect
91 -h
, --help
, --version display this help
and exit
93 When FILE
is - read standard input
.
99 my $conf = which_conf
($configuration_file);
102 open(my $conffile, '<', "$conf")
103 or warn "$P: Can't find a readable $configuration_file file $!\n";
105 while (<$conffile>) {
108 $line =~ s/\s*\n?$//g;
112 next if ($line =~ m/^\s*#/);
113 next if ($line =~ m/^\s*$/);
115 my @words = split(" ", $line);
116 foreach my $word (@words) {
117 last if ($word =~ m/^#/);
118 push (@conf_args, $word);
122 unshift(@ARGV, @conf_args) if @conf_args;
126 'q|quiet+' => \
$quiet,
128 'signoff!' => \
$chk_signoff,
129 'patch!' => \
$chk_patch,
133 'subjective!' => \
$check,
134 'strict!' => \
$check,
135 'ignore=s' => \
@ignore,
137 'show-types!' => \
$show_types,
138 'max-line-length=i' => \
$max_line_length,
139 'min-conf-desc-length=i' => \
$min_conf_desc_length,
141 'summary!' => \
$summary,
142 'mailback!' => \
$mailback,
143 'summary-file!' => \
$summary_file,
145 'fix-inplace!' => \
$fix_inplace,
146 'ignore-perl-version!' => \
$ignore_perl_version,
147 'debug=s' => \
%debug,
148 'test-only=s' => \
$tst_only,
155 $fix = 1 if ($fix_inplace);
156 $check_orig = $check;
160 if ($^V && $^V lt $minimum_perl_version) {
161 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
162 if (!$ignore_perl_version) {
168 print "$P: no input files\n";
172 sub hash_save_array_words
{
173 my ($hashRef, $arrayRef) = @_;
175 my @array = split(/,/, join(',', @$arrayRef));
176 foreach my $word (@array) {
177 $word =~ s/\s*\n?$//g;
180 $word =~ tr/[a-z]/[A-Z]/;
182 next if ($word =~ m/^\s*#/);
183 next if ($word =~ m/^\s*$/);
189 sub hash_show_words
{
190 my ($hashRef, $prefix) = @_;
192 if ($quiet == 0 && keys %$hashRef) {
193 print "NOTE: $prefix message types:";
194 foreach my $word (sort keys %$hashRef) {
201 hash_save_array_words
(\
%ignore_type, \
@ignore);
202 hash_save_array_words
(\
%use_type, \
@use);
205 my $dbg_possible = 0;
208 for my $key (keys %debug) {
210 eval "\${dbg_$key} = '$debug{$key}';";
214 my $rpt_cleaners = 0;
223 if (!top_of_kernel_tree
($root)) {
224 die "$P: $root: --root does not point at a valid tree\n";
227 if (top_of_kernel_tree
('.')) {
229 } elsif ($0 =~ m
@(.*)/scripts/[^/]*$@ &&
230 top_of_kernel_tree
($1)) {
235 if (!defined $root) {
236 print "Must be run from the top-level dir. of a kernel tree\n";
241 my $emitted_corrupt = 0;
244 [A-Za-z_
][A-Za-z\d_
]*
245 (?
:\s
*\#\#\s
*[A-Za-z_
][A-Za-z\d_
]*)*
247 our $Storage = qr{extern|static|asmlinkage};
259 our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
260 our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
261 our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
262 our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
263 our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
265 # Notes to $Attribute:
266 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
287 ____cacheline_aligned
|
288 ____cacheline_aligned_in_smp
|
289 ____cacheline_internodealigned_in_smp
|
293 our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
294 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
295 our $Lval = qr{$Ident(?:$Member)*};
297 our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
298 our $Binary = qr{(?i)0b[01]+$Int_type?};
299 our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
300 our $Int = qr{[0-9]+$Int_type?};
301 our $Octal = qr{0[0-7]+$Int_type?};
302 our $String = qr{"[X\t]*"};
303 our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
304 our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
305 our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
306 our $Float = qr{$Float_hex|$Float_dec|$Float_int};
307 our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
308 our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
309 our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
310 our $Arithmetic = qr{\+|-|\*|\/|%};
314 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||$Arithmetic
317 our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
320 our $NonptrTypeMisordered;
321 our $NonptrTypeWithAttr;
325 our $DeclareMisordered;
327 our $NON_ASCII_UTF8 = qr{
328 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
329 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
330 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
331 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
332 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
333 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
334 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
338 [\x09\x0A\x0D\x20-\x7E] # ASCII
342 our $typeOtherOSTypedefs = qr{(?x
:
343 u_
(?
:char
|short
|int|long
) | # bsd
344 u
(?
:nchar
|short
|int|long
) # sysv
347 our $typeTypedefs = qr{(?x
:
348 (?
:__
)?
(?
:u
|s
|be
|le)(?
:8|16|32|64)|
352 our $logFunctions = qr{(?x
:
353 printk
(?
:_ratelimited
|_once
|)|
354 (?
:[a-z0-9
]+_
){1,2}(?
:printk
|emerg
|alert
|crit
|err
|warning
|warn|notice
|info
|debug
|dbg
|vdbg
|devel
|cont
|WARN
)(?
:_ratelimited
|_once
|)|
355 WARN
(?
:_RATELIMIT
|_ONCE
|)|
358 seq_vprintf
|seq_printf
|seq_puts
361 our $signature_tags = qr{(?xi
:
372 our @typeListMisordered = (
373 qr{char\s+(?:un)?signed},
374 qr{int\s+(?:(?:un)?signed\s+)?short\s},
375 qr{int\s+short(?:\s+(?:un)?signed)},
376 qr{short\s+int(?:\s+(?:un)?signed)},
377 qr{(?:un)?signed\s+int\s+short},
378 qr{short\s+(?:un)?signed},
379 qr{long\s+int\s+(?:un)?signed},
380 qr{int\s+long\s+(?:un)?signed},
381 qr{long\s+(?:un)?signed\s+int},
382 qr{int\s+(?:un)?signed\s+long},
383 qr{int\s+(?:un)?signed},
384 qr{int\s+long\s+long\s+(?:un)?signed},
385 qr{long\s+long\s+int\s+(?:un)?signed},
386 qr{long\s+long\s+(?:un)?signed\s+int},
387 qr{long\s+long\s+(?:un)?signed},
388 qr{long\s+(?:un)?signed},
393 qr{(?:(?:un)?signed\s+)?char},
394 qr{(?:(?:un)?signed\s+)?short\s+int},
395 qr{(?:(?:un)?signed\s+)?short},
396 qr{(?:(?:un)?signed\s+)?int},
397 qr{(?:(?:un)?signed\s+)?long\s+int},
398 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
399 qr{(?:(?:un)?signed\s+)?long\s+long},
400 qr{(?:(?:un)?signed\s+)?long},
409 qr{${Ident}_handler
},
410 qr{${Ident}_handler_fn
},
413 our @typeListWithAttr = (
415 qr{struct\s+$InitAttribute\s+$Ident},
416 qr{union\s+$InitAttribute\s+$Ident},
419 our @modifierList = (
423 our @mode_permission_funcs = (
425 ["module_param_(?:array|named|string)", 4],
426 ["module_param_array_named", 5],
427 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
428 ["proc_create(?:_data|)", 2],
429 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
432 #Create a search pattern for all these functions to speed up a loop below
433 our $mode_perms_search = "";
434 foreach my $entry (@mode_permission_funcs) {
435 $mode_perms_search .= '|' if ($mode_perms_search ne "");
436 $mode_perms_search .= $entry->[0];
439 our $allowed_asm_includes = qr{(?x
:
445 # memory.h: ARM has a custom one
447 # Load common spelling mistakes and build regular expression list.
451 if (open(my $spelling, '<', $spelling_file)) {
453 while (<$spelling>) {
456 $line =~ s/\s*\n?$//g;
459 next if ($line =~ m/^\s*#/);
460 next if ($line =~ m/^\s*$/);
462 my ($suspect, $fix) = split(/\|\|/, $line);
464 push(@spelling_list, $suspect);
465 $spelling_fix{$suspect} = $fix;
468 $misspellings = join("|", @spelling_list);
470 warn "No typos will be found - file '$spelling_file': $!\n";
474 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
475 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
476 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
477 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
478 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
480 (?
:$Modifier\s+|const\s
+)*
482 (?
:typeof
|__typeof__
)\s
*\
([^\
)]*\
)|
483 (?
:$typeOtherOSTypedefs\b)|
487 (?
:\s
+$Modifier|\s
+const
)*
489 $NonptrTypeMisordered = qr{
490 (?
:$Modifier\s+|const\s
+)*
494 (?
:\s
+$Modifier|\s
+const
)*
496 $NonptrTypeWithAttr = qr{
497 (?
:$Modifier\s+|const\s
+)*
499 (?
:typeof
|__typeof__
)\s
*\
([^\
)]*\
)|
501 (?
:$typeOtherOSTypedefs\b)|
504 (?
:\s
+$Modifier|\s
+const
)*
508 (?
:(?
:\s
|\
*|\
[\
])+\s
*const
|(?
:\s
|\
*\s
*(?
:const\s
*)?
|\
[\
])+|(?
:\s
*\
[\s
*\
])+)?
509 (?
:\s
+$Inline|\s
+$Modifier)*
511 $TypeMisordered = qr{
512 $NonptrTypeMisordered
513 (?
:(?
:\s
|\
*|\
[\
])+\s
*const
|(?
:\s
|\
*\s
*(?
:const\s
*)?
|\
[\
])+|(?
:\s
*\
[\s
*\
])+)?
514 (?
:\s
+$Inline|\s
+$Modifier)*
516 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
517 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
521 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s
*};
523 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
524 # requires at least perl version v5.10.0
525 # Any use must be runtime checked with $^V
527 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
528 our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s
*};
529 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
531 our $declaration_macros = qr{(?x
:
532 (?
:$Storage\s+)?
(?
:[A-Z_
][A-Z0-9
]*_
){0,2}(?
:DEFINE
|DECLARE
)(?
:_
[A-Z0-9
]+){1,2}\s
*\
(|
533 (?
:$Storage\s+)?LIST_HEAD\s
*\
(|
534 (?
:$Storage\s+)?
${Type
}\s
+uninitialized_var\s
*\
(
539 return "" if (!defined($string));
541 while ($string =~ /^\s*\(.*\)\s*$/) {
542 $string =~ s
@^\s*\(\s
*@@;
543 $string =~ s
@\s*\)\s
*$@@;
546 $string =~ s
@\s+@ @g;
551 sub seed_camelcase_file
{
554 return if (!(-f
$file));
558 open(my $include_file, '<', "$file")
559 or warn "$P: Can't read '$file' $!\n";
560 my $text = <$include_file>;
561 close($include_file);
563 my @lines = split('\n', $text);
565 foreach my $line (@lines) {
566 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
567 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
569 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
571 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
577 my $camelcase_seeded = 0;
578 sub seed_camelcase_includes
{
579 return if ($camelcase_seeded);
582 my $camelcase_cache = "";
583 my @include_files = ();
585 $camelcase_seeded = 1;
588 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
589 chomp $git_last_include_commit;
590 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
592 my $last_mod_date = 0;
593 $files = `find $root/include -name "*.h"`;
594 @include_files = split('\n', $files);
595 foreach my $file (@include_files) {
596 my $date = POSIX
::strftime
("%Y%m%d%H%M",
597 localtime((stat $file)[9]));
598 $last_mod_date = $date if ($last_mod_date < $date);
600 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
603 if ($camelcase_cache ne "" && -f
$camelcase_cache) {
604 open(my $camelcase_file, '<', "$camelcase_cache")
605 or warn "$P: Can't read '$camelcase_cache' $!\n";
606 while (<$camelcase_file>) {
610 close($camelcase_file);
616 $files = `git ls-files "include/*.h"`;
617 @include_files = split('\n', $files);
620 foreach my $file (@include_files) {
621 seed_camelcase_file
($file);
624 if ($camelcase_cache ne "") {
625 unlink glob ".checkpatch-camelcase.*";
626 open(my $camelcase_file, '>', "$camelcase_cache")
627 or warn "$P: Can't write '$camelcase_cache' $!\n";
628 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
629 print $camelcase_file ("$_\n");
631 close($camelcase_file);
635 sub git_commit_info
{
636 my ($commit, $id, $desc) = @_;
638 return ($id, $desc) if ((which
("git") eq "") || !(-e
".git"));
640 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
641 $output =~ s/^\s*//gm;
642 my @lines = split("\n", $output);
644 return ($id, $desc) if ($#lines < 0);
646 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
647 # Maybe one day convert this block of bash into something that returns
648 # all matching commit ids, but it's very slow...
650 # echo "checking commits $1..."
651 # git rev-list --remotes | grep -i "^$1" |
652 # while read line ; do
653 # git log --format='%H %s' -1 $line |
654 # echo "commit $(cut -c 1-12,41-)"
656 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
658 $id = substr($lines[0], 0, 12);
659 $desc = substr($lines[0], 41);
665 $chk_signoff = 0 if ($file);
670 my @fixed_inserted = ();
671 my @fixed_deleted = ();
675 for my $filename (@ARGV) {
678 open($FILE, '-|', "diff -u /dev/null $filename") ||
679 die "$P: $filename: diff failed - $!\n";
680 } elsif ($filename eq '-') {
681 open($FILE, '<&STDIN');
683 open($FILE, '<', "$filename") ||
684 die "$P: $filename: open failed - $!\n";
686 if ($filename eq '-') {
687 $vname = 'Your patch';
696 if (!process
($filename)) {
702 @fixed_inserted = ();
709 sub top_of_kernel_tree
{
713 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
714 "README", "Documentation", "arch", "include", "drivers",
715 "fs", "init", "ipc", "kernel", "lib", "scripts",
718 foreach my $check (@tree_check) {
719 if (! -e
$root . '/' . $check) {
727 my ($formatted_email) = @_;
733 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
736 $comment = $3 if defined $3;
737 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
739 $comment = $2 if defined $2;
740 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
742 $comment = $2 if defined $2;
743 $formatted_email =~ s/$address.*$//;
744 $name = $formatted_email;
746 $name =~ s/^\"|\"$//g;
747 # If there's a name left after stripping spaces and
748 # leading quotes, and the address doesn't have both
749 # leading and trailing angle brackets, the address
751 # "joe smith joe@smith.com" bad
752 # "joe smith <joe@smith.com" bad
753 if ($name ne "" && $address !~ /^<[^>]+>$/) {
761 $name =~ s/^\"|\"$//g;
762 $address = trim
($address);
763 $address =~ s/^\<|\>$//g;
765 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
766 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
770 return ($name, $address, $comment);
774 my ($name, $address) = @_;
779 $name =~ s/^\"|\"$//g;
780 $address = trim($address);
782 if ($name =~ /[^\w \-]/i) { ##has "must quote
" chars
783 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
788 $formatted_email = "$address";
790 $formatted_email = "$name <$address>";
793 return $formatted_email;
799 foreach my $path (split(/:/, $ENV{PATH
})) {
800 if (-e
"$path/$bin") {
811 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
812 if (-e
"$path/$conf") {
813 return "$path/$conf";
825 for my $c (split(//, $str)) {
829 for (; ($n % 8) != 0; $n++) {
841 (my $res = shift) =~ tr/\t/ /c;
848 # Drop the diff line leader and expand tabs
850 $line = expand_tabs
($line);
852 # Pick the indent from the front of the line.
853 my ($white) = ($line =~ /^(\s*)/);
855 return (length($line), length($white));
858 my $sanitise_quote = '';
860 sub sanitise_line_reset
{
861 my ($in_comment) = @_;
864 $sanitise_quote = '*/';
866 $sanitise_quote = '';
879 # Always copy over the diff marker.
880 $res = substr($line, 0, 1);
882 for ($off = 1; $off < length($line); $off++) {
883 $c = substr($line, $off, 1);
885 # Comments we are wacking completly including the begin
886 # and end, all to $;.
887 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
888 $sanitise_quote = '*/';
890 substr($res, $off, 2, "$;$;");
894 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
895 $sanitise_quote = '';
896 substr($res, $off, 2, "$;$;");
900 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
901 $sanitise_quote = '//';
903 substr($res, $off, 2, $sanitise_quote);
908 # A \ in a string means ignore the next character.
909 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
911 substr($res, $off, 2, 'XX');
916 if ($c eq "'" || $c eq '"') {
917 if ($sanitise_quote eq '') {
918 $sanitise_quote = $c;
920 substr($res, $off, 1, $c);
922 } elsif ($sanitise_quote eq $c) {
923 $sanitise_quote = '';
927 #print "c<$c> SQ<$sanitise_quote>\n";
928 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
929 substr($res, $off, 1, $;);
930 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
931 substr($res, $off, 1, $;);
932 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
933 substr($res, $off, 1, 'X');
935 substr($res, $off, 1, $c);
939 if ($sanitise_quote eq '//') {
940 $sanitise_quote = '';
943 # The pathname on a #include may be surrounded by '<' and '>'.
944 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
945 my $clean = 'X' x
length($1);
946 $res =~ s
@\<.*\
>@<$clean>@;
948 # The whole of a #error is a string.
949 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
950 my $clean = 'X' x
length($1);
951 $res =~ s
@(\#\s
*(?
:error
|warning
)\s
+).*@$1$clean@;
957 sub get_quoted_string
{
958 my ($line, $rawline) = @_;
960 return "" if ($line !~ m/(\"[X\t]+\")/g);
961 return substr($rawline, $-[0], $+[0] - $-[0]);
964 sub ctx_statement_block
{
965 my ($linenr, $remain, $off) = @_;
966 my $line = $linenr - 1;
983 @stack = (['', 0]) if ($#stack == -1);
985 #warn "CSB: blk<$blk> remain<$remain>\n";
986 # If we are about to drop off the end, pull in more
989 for (; $remain > 0; $line++) {
990 last if (!defined $lines[$line]);
991 next if ($lines[$line] =~ /^-/);
994 $blk .= $lines[$line] . "\n";
999 # Bail if there is no further context.
1000 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
1004 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1010 $c = substr($blk, $off, 1);
1011 $remainder = substr($blk, $off);
1013 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1015 # Handle nested #if/#else.
1016 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1017 push(@stack, [ $type, $level ]);
1018 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1019 ($type, $level) = @{$stack[$#stack - 1]};
1020 } elsif ($remainder =~ /^#\s*endif\b/) {
1021 ($type, $level) = @{pop(@stack)};
1024 # Statement ends at the ';' or a close '}' at the
1026 if ($level == 0 && $c eq ';') {
1030 # An else is really a conditional as long as its not else if
1031 if ($level == 0 && $coff_set == 0 &&
1032 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1033 $remainder =~ /^(else)(?:\s|{)/ &&
1034 $remainder !~ /^else\s+if\b/) {
1035 $coff = $off + length($1) - 1;
1037 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1038 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1041 if (($type eq '' || $type eq '(') && $c eq '(') {
1045 if ($type eq '(' && $c eq ')') {
1047 $type = ($level != 0)?
'(' : '';
1049 if ($level == 0 && $coff < $soff) {
1052 #warn "CSB: mark coff<$coff>\n";
1055 if (($type eq '' || $type eq '{') && $c eq '{') {
1059 if ($type eq '{' && $c eq '}') {
1061 $type = ($level != 0)?
'{' : '';
1064 if (substr($blk, $off + 1, 1) eq ';') {
1070 # Preprocessor commands end at the newline unless escaped.
1071 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1079 # We are truly at the end, so shuffle to the next line.
1086 my $statement = substr($blk, $soff, $off - $soff + 1);
1087 my $condition = substr($blk, $soff, $coff - $soff + 1);
1089 #warn "STATEMENT<$statement>\n";
1090 #warn "CONDITION<$condition>\n";
1092 #print "coff<$coff> soff<$off> loff<$loff>\n";
1094 return ($statement, $condition,
1095 $line, $remain + 1, $off - $loff + 1, $level);
1098 sub statement_lines
{
1101 # Strip the diff line prefixes and rip blank lines at start and end.
1102 $stmt =~ s/(^|\n)./$1/g;
1106 my @stmt_lines = ($stmt =~ /\n/g);
1108 return $#stmt_lines + 2;
1111 sub statement_rawlines
{
1114 my @stmt_lines = ($stmt =~ /\n/g);
1116 return $#stmt_lines + 2;
1119 sub statement_block_size
{
1122 $stmt =~ s/(^|\n)./$1/g;
1128 my @stmt_lines = ($stmt =~ /\n/g);
1129 my @stmt_statements = ($stmt =~ /;/g);
1131 my $stmt_lines = $#stmt_lines + 2;
1132 my $stmt_statements = $#stmt_statements + 1;
1134 if ($stmt_lines > $stmt_statements) {
1137 return $stmt_statements;
1141 sub ctx_statement_full
{
1142 my ($linenr, $remain, $off) = @_;
1143 my ($statement, $condition, $level);
1147 # Grab the first conditional/block pair.
1148 ($statement, $condition, $linenr, $remain, $off, $level) =
1149 ctx_statement_block
($linenr, $remain, $off);
1150 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1151 push(@chunks, [ $condition, $statement ]);
1152 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1153 return ($level, $linenr, @chunks);
1156 # Pull in the following conditional/block pairs and see if they
1157 # could continue the statement.
1159 ($statement, $condition, $linenr, $remain, $off, $level) =
1160 ctx_statement_block
($linenr, $remain, $off);
1161 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1162 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1164 push(@chunks, [ $condition, $statement ]);
1167 return ($level, $linenr, @chunks);
1171 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1173 my $start = $linenr - 1;
1180 my @stack = ($level);
1181 for ($line = $start; $remain > 0; $line++) {
1182 next if ($rawlines[$line] =~ /^-/);
1185 $blk .= $rawlines[$line];
1187 # Handle nested #if/#else.
1188 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1189 push(@stack, $level);
1190 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1191 $level = $stack[$#stack - 1];
1192 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1193 $level = pop(@stack);
1196 foreach my $c (split(//, $lines[$line])) {
1197 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1203 if ($c eq $close && $level > 0) {
1205 last if ($level == 0);
1206 } elsif ($c eq $open) {
1211 if (!$outer || $level <= 1) {
1212 push(@res, $rawlines[$line]);
1215 last if ($level == 0);
1218 return ($level, @res);
1220 sub ctx_block_outer
{
1221 my ($linenr, $remain) = @_;
1223 my ($level, @r) = ctx_block_get
($linenr, $remain, 1, '{', '}', 0);
1227 my ($linenr, $remain) = @_;
1229 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
1233 my ($linenr, $remain, $off) = @_;
1235 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
1238 sub ctx_block_level
{
1239 my ($linenr, $remain) = @_;
1241 return ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
1243 sub ctx_statement_level
{
1244 my ($linenr, $remain, $off) = @_;
1246 return ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
1249 sub ctx_locate_comment
{
1250 my ($first_line, $end_line) = @_;
1252 # Catch a comment on the end of the line itself.
1253 my ($current_comment) = ($rawlines[$end_line - 1] =~ m
@.*(/\*.*\*/)\s
*(?
:\\\s
*)?
$@);
1254 return $current_comment if (defined $current_comment);
1256 # Look through the context and try and figure out if there is a
1259 $current_comment = '';
1260 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1261 my $line = $rawlines[$linenr - 1];
1263 if ($linenr == $first_line and $line =~ m
@^.\s
*\
*@) {
1266 if ($line =~ m
@/\*@) {
1269 if (!$in_comment && $current_comment ne '') {
1270 $current_comment = '';
1272 $current_comment .= $line . "\n" if ($in_comment);
1273 if ($line =~ m
@\*/@) {
1278 chomp($current_comment);
1279 return($current_comment);
1281 sub ctx_has_comment
{
1282 my ($first_line, $end_line) = @_;
1283 my $cmt = ctx_locate_comment
($first_line, $end_line);
1285 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1286 ##print "CMMT: $cmt\n";
1288 return ($cmt ne '');
1292 my ($linenr, $cnt) = @_;
1294 my $offset = $linenr - 1;
1299 $line = $rawlines[$offset++];
1300 next if (defined($line) && $line =~ /^-/);
1312 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1315 $coded = sprintf("^%c", unpack('C', $2) + 64);
1324 my $av_preprocessor = 0;
1329 sub annotate_reset
{
1330 $av_preprocessor = 0;
1332 @av_paren_type = ('E');
1333 $av_pend_colon = 'O';
1336 sub annotate_values
{
1337 my ($stream, $type) = @_;
1340 my $var = '_' x
length($stream);
1343 print "$stream\n" if ($dbg_values > 1);
1345 while (length($cur)) {
1346 @av_paren_type = ('E') if ($#av_paren_type < 0);
1347 print " <" . join('', @av_paren_type) .
1348 "> <$type> <$av_pending>" if ($dbg_values > 1);
1349 if ($cur =~ /^(\s+)/o) {
1350 print "WS($1)\n" if ($dbg_values > 1);
1351 if ($1 =~ /\n/ && $av_preprocessor) {
1352 $type = pop(@av_paren_type);
1353 $av_preprocessor = 0;
1356 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1357 print "CAST($1)\n" if ($dbg_values > 1);
1358 push(@av_paren_type, $type);
1361 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1362 print "DECLARE($1)\n" if ($dbg_values > 1);
1365 } elsif ($cur =~ /^($Modifier)\s*/) {
1366 print "MODIFIER($1)\n" if ($dbg_values > 1);
1369 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1370 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1371 $av_preprocessor = 1;
1372 push(@av_paren_type, $type);
1378 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1379 print "UNDEF($1)\n" if ($dbg_values > 1);
1380 $av_preprocessor = 1;
1381 push(@av_paren_type, $type);
1383 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1384 print "PRE_START($1)\n" if ($dbg_values > 1);
1385 $av_preprocessor = 1;
1387 push(@av_paren_type, $type);
1388 push(@av_paren_type, $type);
1391 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1392 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1393 $av_preprocessor = 1;
1395 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1399 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1400 print "PRE_END($1)\n" if ($dbg_values > 1);
1402 $av_preprocessor = 1;
1404 # Assume all arms of the conditional end as this
1405 # one does, and continue as if the #endif was not here.
1406 pop(@av_paren_type);
1407 push(@av_paren_type, $type);
1410 } elsif ($cur =~ /^(\\\n)/o) {
1411 print "PRECONT($1)\n" if ($dbg_values > 1);
1413 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1414 print "ATTR($1)\n" if ($dbg_values > 1);
1415 $av_pending = $type;
1418 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1419 print "SIZEOF($1)\n" if ($dbg_values > 1);
1425 } elsif ($cur =~ /^(if|while|for)\b/o) {
1426 print "COND($1)\n" if ($dbg_values > 1);
1430 } elsif ($cur =~/^(case)/o) {
1431 print "CASE($1)\n" if ($dbg_values > 1);
1432 $av_pend_colon = 'C';
1435 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1436 print "KEYWORD($1)\n" if ($dbg_values > 1);
1439 } elsif ($cur =~ /^(\()/o) {
1440 print "PAREN('$1')\n" if ($dbg_values > 1);
1441 push(@av_paren_type, $av_pending);
1445 } elsif ($cur =~ /^(\))/o) {
1446 my $new_type = pop(@av_paren_type);
1447 if ($new_type ne '_') {
1449 print "PAREN('$1') -> $type\n"
1450 if ($dbg_values > 1);
1452 print "PAREN('$1')\n" if ($dbg_values > 1);
1455 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1456 print "FUNC($1)\n" if ($dbg_values > 1);
1460 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1461 if (defined $2 && $type eq 'C' || $type eq 'T') {
1462 $av_pend_colon = 'B';
1463 } elsif ($type eq 'E') {
1464 $av_pend_colon = 'L';
1466 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1469 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1470 print "IDENT($1)\n" if ($dbg_values > 1);
1473 } elsif ($cur =~ /^($Assignment)/o) {
1474 print "ASSIGN($1)\n" if ($dbg_values > 1);
1477 } elsif ($cur =~/^(;|{|})/) {
1478 print "END($1)\n" if ($dbg_values > 1);
1480 $av_pend_colon = 'O';
1482 } elsif ($cur =~/^(,)/) {
1483 print "COMMA($1)\n" if ($dbg_values > 1);
1486 } elsif ($cur =~ /^(\?)/o) {
1487 print "QUESTION($1)\n" if ($dbg_values > 1);
1490 } elsif ($cur =~ /^(:)/o) {
1491 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1493 substr($var, length($res), 1, $av_pend_colon);
1494 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1499 $av_pend_colon = 'O';
1501 } elsif ($cur =~ /^(\[)/o) {
1502 print "CLOSE($1)\n" if ($dbg_values > 1);
1505 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1508 print "OPV($1)\n" if ($dbg_values > 1);
1515 substr($var, length($res), 1, $variant);
1518 } elsif ($cur =~ /^($Operators)/o) {
1519 print "OP($1)\n" if ($dbg_values > 1);
1520 if ($1 ne '++' && $1 ne '--') {
1524 } elsif ($cur =~ /(^.)/o) {
1525 print "C($1)\n" if ($dbg_values > 1);
1528 $cur = substr($cur, length($1));
1529 $res .= $type x
length($1);
1533 return ($res, $var);
1537 my ($possible, $line) = @_;
1538 my $notPermitted = qr{(?
:
1555 ^(?
:typedef
|struct
|enum
)\b
1557 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1558 if ($possible !~ $notPermitted) {
1559 # Check for modifiers.
1560 $possible =~ s/\s*$Storage\s*//g;
1561 $possible =~ s/\s*$Sparse\s*//g;
1562 if ($possible =~ /^\s*$/) {
1564 } elsif ($possible =~ /\s/) {
1565 $possible =~ s/\s*$Type\s*//g;
1566 for my $modifier (split(' ', $possible)) {
1567 if ($modifier !~ $notPermitted) {
1568 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1569 push(@modifierList, $modifier);
1574 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1575 push(@typeList, $possible);
1579 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1588 return defined $use_type{$type} if (scalar keys %use_type > 0);
1590 return !defined $ignore_type{$type};
1594 my ($level, $type, $msg) = @_;
1596 if (!show_type
($type) ||
1597 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1602 $line = "$prefix$level:$type: $msg\n";
1604 $line = "$prefix$level: $msg\n";
1606 $line = (split('\n', $line))[0] . "\n" if ($terse);
1608 push(our @report, $line);
1617 sub fixup_current_range
{
1618 my ($lineRef, $offset, $length) = @_;
1620 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1623 my $no = $o + $offset;
1624 my $nl = $l + $length;
1625 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1629 sub fix_inserted_deleted_lines
{
1630 my ($linesRef, $insertedRef, $deletedRef) = @_;
1632 my $range_last_linenr = 0;
1633 my $delta_offset = 0;
1638 my $next_insert = 0;
1639 my $next_delete = 0;
1643 my $inserted = @{$insertedRef}[$next_insert++];
1644 my $deleted = @{$deletedRef}[$next_delete++];
1646 foreach my $old_line (@{$linesRef}) {
1648 my $line = $old_line; #don't modify the array
1649 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename
1651 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1652 $range_last_linenr = $new_linenr;
1653 fixup_current_range
(\
$line, $delta_offset, 0);
1656 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1657 $deleted = @{$deletedRef}[$next_delete++];
1659 fixup_current_range
(\
$lines[$range_last_linenr], $delta_offset--, -1);
1662 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1663 push(@lines, ${$inserted}{'LINE'});
1664 $inserted = @{$insertedRef}[$next_insert++];
1666 fixup_current_range
(\
$lines[$range_last_linenr], $delta_offset++, 1);
1670 push(@lines, $line);
1680 sub fix_insert_line
{
1681 my ($linenr, $line) = @_;
1687 push(@fixed_inserted, $inserted);
1690 sub fix_delete_line
{
1691 my ($linenr, $line) = @_;
1698 push(@fixed_deleted, $deleted);
1702 my ($type, $msg) = @_;
1704 if (report
("ERROR", $type, $msg)) {
1712 my ($type, $msg) = @_;
1714 if (report
("WARNING", $type, $msg)) {
1722 my ($type, $msg) = @_;
1724 if ($check && report
("CHECK", $type, $msg)) {
1732 sub check_absolute_file
{
1733 my ($absolute, $herecurr) = @_;
1734 my $file = $absolute;
1736 ##print "absolute<$absolute>\n";
1738 # See if any suffix of this path is a path within the tree.
1739 while ($file =~ s
@^[^/]*/@@) {
1740 if (-f
"$root/$file") {
1741 ##print "file<$file>\n";
1749 # It is, so see if the prefix is acceptable.
1750 my $prefix = $absolute;
1751 substr($prefix, -length($file)) = '';
1753 ##print "prefix<$prefix>\n";
1754 if ($prefix ne ".../") {
1755 WARN
("USE_RELATIVE_PATH",
1756 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1763 $string =~ s/^\s+|\s+$//g;
1771 $string =~ s/^\s+//;
1779 $string =~ s/\s+$//;
1784 sub string_find_replace
{
1785 my ($string, $find, $replace) = @_;
1787 $string =~ s/$find/$replace/g;
1795 my $source_indent = 8;
1796 my $max_spaces_before_tab = $source_indent - 1;
1797 my $spaces_to_tab = " " x
$source_indent;
1799 #convert leading spaces to tabs
1800 1 while $leading =~ s
@^([\t]*)$spaces_to_tab@$1\t@g;
1801 #Remove spaces before a tab
1802 1 while $leading =~ s
@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1807 sub pos_last_openparen
{
1812 my $opens = $line =~ tr/\(/\(/;
1813 my $closes = $line =~ tr/\)/\)/;
1815 my $last_openparen = 0;
1817 if (($opens == 0) || ($closes >= $opens)) {
1821 my $len = length($line);
1823 for ($pos = 0; $pos < $len; $pos++) {
1824 my $string = substr($line, $pos);
1825 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1826 $pos += length($1) - 1;
1827 } elsif (substr($line, $pos, 1) eq '(') {
1828 $last_openparen = $pos;
1829 } elsif (index($string, '(') == -1) {
1834 return length(expand_tabs
(substr($line, 0, $last_openparen))) + 1;
1838 my $filename = shift;
1844 my $stashrawline="";
1855 my $in_header_lines = $file ?
0 : 1;
1856 my $in_commit_log = 0; #Scanning lines before patch
1857 my $reported_maintainer_file = 0;
1858 my $non_utf8_charset = 0;
1860 my $last_blank_line = 0;
1861 my $last_coalesced_string_linenr = -1;
1869 # Trace the real file/line as we go.
1875 my $comment_edge = 0;
1879 my $prev_values = 'E';
1882 my %suppress_ifbraces;
1883 my %suppress_whiletrailers;
1884 my %suppress_export;
1885 my $suppress_statement = 0;
1887 my %signatures = ();
1889 # Pre-scan the patch sanitizing the lines.
1890 # Pre-scan the patch looking for any __setup documentation.
1892 my @setup_docs = ();
1895 my $camelcase_file_seeded = 0;
1897 sanitise_line_reset
();
1899 foreach my $rawline (@rawlines) {
1903 push(@fixed, $rawline) if ($fix);
1905 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1907 if ($1 =~ m
@Documentation/kernel-parameters
.txt
$@) {
1912 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1921 # Guestimate if this is a continuing comment. Run
1922 # the context looking for a comment "edge". If this
1923 # edge is a close comment then we must be in a comment
1927 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1928 next if (defined $rawlines[$ln - 1] &&
1929 $rawlines[$ln - 1] =~ /^-/);
1931 #print "RAW<$rawlines[$ln - 1]>\n";
1932 last if (!defined $rawlines[$ln - 1]);
1933 if ($rawlines[$ln - 1] =~ m
@(/\*|\*/)@ &&
1934 $rawlines[$ln - 1] !~ m
@"[^"]*(?
:/\*|\*/)[^"]*"@) {
1939 if (defined $edge && $edge eq '*/') {
1943 # Guestimate if this is a continuing comment. If this
1944 # is the start of a diff block and this line starts
1945 # ' *' then it is very likely a comment.
1946 if (!defined $edge &&
1947 $rawlines[$linenr] =~ m
@^.\s
*(?
:\
*\
*+| \
*)(?
:\s
|$)@)
1952 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1953 sanitise_line_reset
($in_comment);
1955 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1956 # Standardise the strings and chars within the input to
1957 # simplify matching -- only bother with positive lines.
1958 $line = sanitise_line
($rawline);
1960 push(@lines, $line);
1963 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1968 #print "==>$rawline\n";
1969 #print "-->$line\n";
1971 if ($setup_docs && $line =~ /^\+/) {
1972 push(@setup_docs, $line);
1981 foreach my $line (@lines) {
1984 my $sline = $line; #copy of $line
1985 $sline =~ s/$;/ /g; #with comments as spaces
1987 my $rawline = $rawlines[$linenr - 1];
1989 #extract the line range in the file after the patch is applied
1990 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1992 $first_line = $linenr + 1;
2002 %suppress_ifbraces = ();
2003 %suppress_whiletrailers = ();
2004 %suppress_export = ();
2005 $suppress_statement = 0;
2008 # track the line number as we move through the hunk, note that
2009 # new versions of GNU diff omit the leading space on completely
2010 # blank context lines so we need to count that too.
2011 } elsif ($line =~ /^( |\+|$)/) {
2013 $realcnt-- if ($realcnt != 0);
2015 # Measure the line length and indent.
2016 ($length, $indent) = line_stats
($rawline);
2018 # Track the previous line.
2019 ($prevline, $stashline) = ($stashline, $line);
2020 ($previndent, $stashindent) = ($stashindent, $indent);
2021 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2023 #warn "line<$line>\n";
2025 } elsif ($realcnt == 1) {
2029 my $hunk_line = ($realcnt != 0);
2031 #make up the handle for any error we report on this line
2032 $prefix = "$filename:$realline: " if ($emacs && $file);
2033 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2035 $here = "#$linenr: " if (!$file);
2036 $here = "#$realline: " if ($file);
2039 # extract the filename as it passes
2040 if ($line =~ /^diff --git.*?(\S+)$/) {
2042 $realfile =~ s
@^([^/]*)/@@ if (!$file);
2045 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2047 $realfile =~ s
@^([^/]*)/@@ if (!$file);
2051 if (!$file && $tree && $p1_prefix ne '' &&
2052 -e
"$root/$p1_prefix") {
2053 WARN
("PATCH_PREFIX",
2054 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2057 if ($realfile =~ m
@^include/asm/@) {
2058 ERROR
("MODIFIED_INCLUDE_ASM",
2059 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2065 if ($realfile =~ m
@^(drivers
/net
/|net
/)@) {
2068 $check = $check_orig;
2073 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2075 my $hereline = "$here\n$rawline\n";
2076 my $herecurr = "$here\n$rawline\n";
2077 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2079 $cnt_lines++ if ($realcnt != 0);
2081 # Check for incorrect file permissions
2082 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2083 my $permhere = $here . "FILE: $realfile\n";
2084 if ($realfile !~ m
@scripts/@ &&
2085 $realfile !~ /\.(py|pl|awk|sh)$/) {
2086 ERROR
("EXECUTE_PERMISSIONS",
2087 "do not set execute permissions for source files\n" . $permhere);
2091 # Check the patch for a signoff:
2092 if ($line =~ /^\s*signed-off-by:/i) {
2097 # Check if MAINTAINERS is being updated. If so, there's probably no need to
2098 # emit the "does MAINTAINERS need updating?" message on file add/move/delete
2099 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2100 $reported_maintainer_file = 1;
2103 # Check signature styles
2104 if (!$in_header_lines &&
2105 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
2106 my $space_before = $1;
2108 my $space_after = $3;
2110 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2112 if ($sign_off !~ /$signature_tags/) {
2113 WARN
("BAD_SIGN_OFF",
2114 "Non-standard signature: $sign_off\n" . $herecurr);
2116 if (defined $space_before && $space_before ne "") {
2117 if (WARN
("BAD_SIGN_OFF",
2118 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2120 $fixed[$fixlinenr] =
2121 "$ucfirst_sign_off $email";
2124 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
2125 if (WARN
("BAD_SIGN_OFF",
2126 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2128 $fixed[$fixlinenr] =
2129 "$ucfirst_sign_off $email";
2133 if (!defined $space_after || $space_after ne " ") {
2134 if (WARN
("BAD_SIGN_OFF",
2135 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2137 $fixed[$fixlinenr] =
2138 "$ucfirst_sign_off $email";
2142 my ($email_name, $email_address, $comment) = parse_email
($email);
2143 my $suggested_email = format_email
(($email_name, $email_address));
2144 if ($suggested_email eq "") {
2145 ERROR
("BAD_SIGN_OFF",
2146 "Unrecognized email address: '$email'\n" . $herecurr);
2148 my $dequoted = $suggested_email;
2149 $dequoted =~ s/^"//;
2150 $dequoted =~ s/" </ </;
2151 # Don't force email to have quotes
2152 # Allow just an angle bracketed address
2153 if ("$dequoted$comment" ne $email &&
2154 "<$email_address>$comment" ne $email &&
2155 "$suggested_email$comment" ne $email) {
2156 WARN
("BAD_SIGN_OFF",
2157 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2161 # Check for duplicate signatures
2162 my $sig_nospace = $line;
2163 $sig_nospace =~ s/\s//g;
2164 $sig_nospace = lc($sig_nospace);
2165 if (defined $signatures{$sig_nospace}) {
2166 WARN
("BAD_SIGN_OFF",
2167 "Duplicate signature\n" . $herecurr);
2169 $signatures{$sig_nospace} = 1;
2173 # Check for old stable address
2174 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2175 ERROR
("STABLE_ADDRESS",
2176 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2179 # Check for unwanted Gerrit info
2180 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2181 ERROR
("GERRIT_CHANGE_ID",
2182 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2185 # Check for git id commit length and improperly formed commit descriptions
2186 if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) {
2188 my $orig_commit = lc($2);
2194 my $id = '0123456789ab';
2195 my $orig_desc = "commit description";
2196 my $description = "";
2198 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2199 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2200 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2201 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2202 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2204 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2205 defined $rawlines[$linenr] &&
2206 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2208 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2209 defined $rawlines[$linenr] &&
2210 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2211 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2213 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2214 $orig_desc .= " " . $1;
2217 ($id, $description) = git_commit_info
($orig_commit,
2220 if ($short || $long || $space || $case || ($orig_desc ne $description)) {
2221 ERROR
("GIT_COMMIT_ID",
2222 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2226 # Check for added, moved or deleted files
2227 if (!$reported_maintainer_file && !$in_commit_log &&
2228 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2229 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2230 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2231 (defined($1) || defined($2))))) {
2232 $reported_maintainer_file = 1;
2233 WARN("FILE_PATH_CHANGES",
2234 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2237 # Check for wrappage within a valid hunk of the file
2238 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2239 ERROR("CORRUPTED_PATCH",
2240 "patch seems to be corrupt (line wrapped?)\n" .
2241 $herecurr) if (!$emitted_corrupt++);
2244 # Check for absolute kernel paths.
2246 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2249 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2250 check_absolute_file($1, $herecurr)) {
2253 check_absolute_file($file, $herecurr);
2258 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2259 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2260 $rawline !~ m/^$UTF8*$/) {
2261 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2263 my $blank = copy_spacing($rawline);
2264 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2265 my $hereptr = "$hereline$ptr\n";
2268 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
2271 # Check if it's the start of a commit
log
2272 # (not a header line and we haven't seen the patch filename)
2273 if ($in_header_lines && $realfile =~ /^$/ &&
2274 !($rawline =~ /^\s+\S/ ||
2275 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
2276 $in_header_lines = 0;
2280 # Check if there is UTF-8 in a commit log when a mail header has explicitly
2281 # declined it, i.e defined some charset where it is missing.
2282 if ($in_header_lines &&
2283 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2285 $non_utf8_charset = 1;
2288 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
2289 $rawline =~ /$NON_ASCII_UTF8/) {
2290 WARN
("UTF8_BEFORE_PATCH",
2291 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2294 # Check for various typo / spelling mistakes
2295 if (defined($misspellings) && ($in_commit_log || $line =~ /^\+/)) {
2296 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
2298 my $typo_fix = $spelling_fix{lc($typo)};
2299 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2300 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2301 my $msg_type = \
&WARN
;
2302 $msg_type = \
&CHK
if ($file);
2303 if (&{$msg_type}("TYPO_SPELLING",
2304 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2306 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2311 # ignore non-hunk lines and lines being removed
2312 next if (!$hunk_line || $line =~ /^-/);
2314 #trailing whitespace
2315 if ($line =~ /^\+.*\015/) {
2316 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2317 if (ERROR
("DOS_LINE_ENDINGS",
2318 "DOS line endings\n" . $herevet) &&
2320 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
2322 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2323 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2324 if (ERROR
("TRAILING_WHITESPACE",
2325 "trailing whitespace\n" . $herevet) &&
2327 $fixed[$fixlinenr] =~ s/\s+$//;
2333 # Check for FSF mailing addresses.
2334 if ($rawline =~ /\bwrite to the Free/i ||
2335 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2336 $rawline =~ /\b51\s+Franklin\s+St/i) {
2337 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2338 my $msg_type = \
&ERROR
;
2339 $msg_type = \
&CHK
if ($file);
2340 &{$msg_type}("FSF_MAILING_ADDRESS",
2341 "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)
2344 # check for Kconfig help text having a real description
2345 # Only applies when adding the entry originally, after that we do not have
2346 # sufficient context to determine whether it is indeed long enough.
2347 if ($realfile =~ /Kconfig/ &&
2348 $line =~ /^\+\s*config\s+/) {
2351 my $ln = $linenr + 1;
2355 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
2356 $f = $lines[$ln - 1];
2357 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2358 $is_end = $lines[$ln - 1] =~ /^\+/;
2360 next if ($f =~ /^-/);
2361 last if (!$file && $f =~ /^\@\@/);
2363 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2365 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2372 next if ($f =~ /^$/);
2373 if ($f =~ /^\s*config\s/) {
2379 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2380 WARN
("CONFIG_DESCRIPTION",
2381 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2383 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
2386 # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2387 if ($realfile =~ /Kconfig/ &&
2388 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2389 WARN
("CONFIG_EXPERIMENTAL",
2390 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2393 # discourage the use of boolean for type definition attributes of Kconfig options
2394 if ($realfile =~ /Kconfig/ &&
2395 $line =~ /^\+\s*\bboolean\b/) {
2396 WARN
("CONFIG_TYPE_BOOLEAN",
2397 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2400 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2401 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2404 'EXTRA_AFLAGS' => 'asflags-y',
2405 'EXTRA_CFLAGS' => 'ccflags-y',
2406 'EXTRA_CPPFLAGS' => 'cppflags-y',
2407 'EXTRA_LDFLAGS' => 'ldflags-y',
2410 WARN
("DEPRECATED_VARIABLE",
2411 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2414 # check for DT compatible documentation
2415 if (defined $root &&
2416 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2417 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2419 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2421 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2422 my $vp_file = $dt_path . "vendor-prefixes.txt";
2424 foreach my $compat (@compats) {
2425 my $compat2 = $compat;
2426 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2427 my $compat3 = $compat;
2428 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2429 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2431 WARN("UNDOCUMENTED_DT_STRING",
2432 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2435 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2437 `grep -Eq
"^$vendor\\b" $vp_file`;
2439 WARN
("UNDOCUMENTED_DT_STRING",
2440 "DT compatible string vendor \"$vendor\" appears un-documented
-- check
$vp_file\n" . $herecurr);
2445 # check we are in a valid source file if not then ignore this hunk
2446 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
2449 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2450 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
2451 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?
"[X\t]*"\s
*(?
:|,|\
)\s
*;)\s
*$/ ||
2452 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
2453 $length > $max_line_length)
2456 "line over $max_line_length characters\n" . $herecurr);
2459 # check for adding lines without a newline.
2460 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2461 WARN
("MISSING_EOF_NEWLINE",
2462 "adding a line without newline at end of file\n" . $herecurr);
2465 # Blackfin: use hi/lo macros
2466 if ($realfile =~ m
@arch/blackfin/.*\
.S
$@) {
2467 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2468 my $herevet = "$here\n" . cat_vet
($line) . "\n";
2470 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
2472 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2473 my $herevet = "$here\n" . cat_vet
($line) . "\n";
2475 "use the HI() macro, not (... >> 16)\n" . $herevet);
2479 # check we are in a valid source file C or perl if not then ignore this hunk
2480 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
2482 # at the beginning of a line any tabs must come first and anything
2483 # more than 8 must use tabs.
2484 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2485 $rawline =~ /^\+\s* \s*/) {
2486 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2488 if (ERROR
("CODE_INDENT",
2489 "code indent should use tabs where possible\n" . $herevet) &&
2491 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2495 # check for space before tabs.
2496 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2497 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2498 if (WARN
("SPACE_BEFORE_TAB",
2499 "please, no space before tabs\n" . $herevet) &&
2501 while ($fixed[$fixlinenr] =~
2502 s/(^\+.*) {8,8}\t/$1\t\t/) {}
2503 while ($fixed[$fixlinenr] =~
2504 s/(^\+.*) +\t/$1\t/) {}
2508 # check for && or || at the start of a line
2509 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2510 CHK
("LOGICAL_CONTINUATIONS",
2511 "Logical continuations should be on the previous line\n" . $hereprev);
2514 # check multi-line statement indentation matches previous line
2515 if ($^V && $^V ge 5.10.0 &&
2516 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2517 $prevline =~ /^\+(\t*)(.*)$/;
2521 my $pos = pos_last_openparen
($rest);
2523 $line =~ /^(\+| )([ \t]*)/;
2526 my $goodtabindent = $oldindent .
2529 my $goodspaceindent = $oldindent . " " x
$pos;
2531 if ($newindent ne $goodtabindent &&
2532 $newindent ne $goodspaceindent) {
2534 if (CHK
("PARENTHESIS_ALIGNMENT",
2535 "Alignment should match open parenthesis\n" . $hereprev) &&
2536 $fix && $line =~ /^\+/) {
2537 $fixed[$fixlinenr] =~
2538 s/^\+[ \t]*/\+$goodtabindent/;
2544 if ($line =~ /^\+.*(\w+\s*)?\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|[,;\({\[\<\>])/ &&
2545 (!defined($1) || $1 !~ /sizeof\s*/)) {
2547 "No space is necessary after a cast\n" . $herecurr) &&
2549 $fixed[$fixlinenr] =~
2550 s/(\(\s*$Type\s*\))[ \t]+/$1/;
2554 if ($realfile =~ m
@^(drivers
/net
/|net
/)@ &&
2555 $prevrawline =~ /^\+[ \t]*\/\
*[ \t]*$/ &&
2556 $rawline =~ /^\+[ \t]*\*/ &&
2558 WARN
("NETWORKING_BLOCK_COMMENT_STYLE",
2559 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2562 if ($realfile =~ m
@^(drivers
/net
/|net
/)@ &&
2563 $prevrawline =~ /^\+[ \t]*\/\
*/ && #starting /*
2564 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
2565 $rawline =~ /^\+/ && #line is new
2566 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2567 WARN
("NETWORKING_BLOCK_COMMENT_STYLE",
2568 "networking block comments start with * on subsequent lines\n" . $hereprev);
2571 if ($realfile =~ m
@^(drivers
/net
/|net
/)@ &&
2572 $rawline !~ m
@^\+[ \t]*\
*/[ \t]*$@ && #trailing */
2573 $rawline !~ m
@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2574 $rawline !~ m
@^\+.*\
*{2,}/[ \t]*$@ && #trailing **/
2575 $rawline =~ m
@^\+[ \t]*.+\
*\
/[ \t]*$@) { #non blank */
2576 WARN
("NETWORKING_BLOCK_COMMENT_STYLE",
2577 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2580 # check for missing blank lines after struct/union declarations
2581 # with exceptions for various attributes and macros
2582 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2584 !($line =~ /^\+\s*$/ ||
2585 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2586 $line =~ /^\+\s*MODULE_/i ||
2587 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2588 $line =~ /^\+[a-z_]*init/ ||
2589 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2590 $line =~ /^\+\s*DECLARE/ ||
2591 $line =~ /^\+\s*__setup/)) {
2592 if (CHK
("LINE_SPACING",
2593 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2595 fix_insert_line
($fixlinenr, "\+");
2599 # check for multiple consecutive blank lines
2600 if ($prevline =~ /^[\+ ]\s*$/ &&
2601 $line =~ /^\+\s*$/ &&
2602 $last_blank_line != ($linenr - 1)) {
2603 if (CHK
("LINE_SPACING",
2604 "Please don't use multiple blank lines\n" . $hereprev) &&
2606 fix_delete_line
($fixlinenr, $rawline);
2609 $last_blank_line = $linenr;
2612 # check for missing blank lines after declarations
2613 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2614 # actual declarations
2615 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
2616 # function pointer declarations
2617 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
2618 # foo bar; where foo is some local typedef or #define
2619 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2620 # known declaration macros
2621 $prevline =~ /^\+\s+$declaration_macros/) &&
2622 # for "else if" which can look like "$Ident $Ident"
2623 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2624 # other possible extensions of declaration lines
2625 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2626 # not starting a section or a macro "\" extended line
2627 $prevline =~ /(?:\{\s*|\\)$/) &&
2628 # looks like a declaration
2629 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
2630 # function pointer declarations
2631 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
2632 # foo bar; where foo is some local typedef or #define
2633 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2634 # known declaration macros
2635 $sline =~ /^\+\s+$declaration_macros/ ||
2636 # start of struct or union or enum
2637 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
2638 # start or end of block or continuation of declaration
2639 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2640 # bitfield continuation
2641 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2642 # other possible extensions of declaration lines
2643 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2644 # indentation of previous and current line are the same
2645 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2646 if (WARN
("LINE_SPACING",
2647 "Missing a blank line after declarations\n" . $hereprev) &&
2649 fix_insert_line
($fixlinenr, "\+");
2653 # check for spaces at the beginning of a line.
2655 # 1) within comments
2656 # 2) indented preprocessor commands
2658 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
2659 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
2660 if (WARN
("LEADING_SPACE",
2661 "please, no spaces at the start of a line\n" . $herevet) &&
2663 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2667 # check we are in a valid C source file if not then ignore this hunk
2668 next if ($realfile !~ /\.(h|c)$/);
2670 # check indentation of any line with a bare else
2671 # (but not if it is a multiple line "if (foo) return bar; else return baz;")
2672 # if the previous line is a break or return and is indented 1 tab more...
2673 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2674 my $tabs = length($1) + 1;
2675 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2676 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2677 defined $lines[$linenr] &&
2678 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
2679 WARN
("UNNECESSARY_ELSE",
2680 "else is not generally useful after a break or return\n" . $hereprev);
2684 # check indentation of a line with a break;
2685 # if the previous line is a goto or return and is indented the same # of tabs
2686 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2688 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2689 WARN
("UNNECESSARY_BREAK",
2690 "break is not useful after a goto or return\n" . $hereprev);
2694 # discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2695 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2696 WARN
("CONFIG_EXPERIMENTAL",
2697 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2700 # check for RCS/CVS revision markers
2701 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2703 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2706 # Blackfin: don't use __builtin_bfin_[cs]sync
2707 if ($line =~ /__builtin_bfin_csync/) {
2708 my $herevet = "$here\n" . cat_vet
($line) . "\n";
2710 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
2712 if ($line =~ /__builtin_bfin_ssync/) {
2713 my $herevet = "$here\n" . cat_vet
($line) . "\n";
2715 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
2718 # check for old HOTPLUG __dev<foo> section markings
2719 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2720 WARN
("HOTPLUG_SECTION",
2721 "Using $1 is unnecessary\n" . $herecurr);
2724 # Check for potential 'bare' types
2725 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2727 #print "LINE<$line>\n";
2728 if ($linenr >= $suppress_statement &&
2729 $realcnt && $sline =~ /.\s*\S/) {
2730 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2731 ctx_statement_block
($linenr, $realcnt, 0);
2732 $stat =~ s/\n./\n /g;
2733 $cond =~ s/\n./\n /g;
2735 #print "linenr<$linenr> <$stat>\n";
2736 # If this statement has no statement boundaries within
2737 # it there is no point in retrying a statement scan
2738 # until we hit end of it.
2739 my $frag = $stat; $frag =~ s/;+\s*$//;
2740 if ($frag !~ /(?:{|;)/) {
2741 #print "skip<$line_nr_next>\n";
2742 $suppress_statement = $line_nr_next;
2745 # Find the real next line.
2746 $realline_next = $line_nr_next;
2747 if (defined $realline_next &&
2748 (!defined $lines[$realline_next - 1] ||
2749 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2756 # Ignore goto labels.
2757 if ($s =~ /$Ident:\*$/s) {
2759 # Ignore functions being called
2760 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2762 } elsif ($s =~ /^.\s*else\b/s) {
2764 # declarations always start with types
2765 } 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) {
2768 possible
($type, "A:" . $s);
2770 # definitions in global scope can only start with types
2771 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2772 possible
($1, "B:" . $s);
2775 # any (foo ... *) is a pointer cast, and foo is a type
2776 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2777 possible
($1, "C:" . $s);
2780 # Check for any sort of function declaration.
2781 # int foo(something bar, other baz);
2782 # void (*store_gdt)(x86_descr_ptr *);
2783 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
2784 my ($name_len) = length($1);
2787 substr($ctx, 0, $name_len + 1, '');
2788 $ctx =~ s/\)[^\)]*$//;
2790 for my $arg (split(/\s*,\s*/, $ctx)) {
2791 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
2793 possible
($1, "D:" . $s);
2801 # Checks which may be anchored in the context.
2804 # Check for switch () and associated case and default
2805 # statements should be at the same indent.
2806 if ($line=~/\bswitch\s*\(.*\)/) {
2809 my @ctx = ctx_block_outer
($linenr, $realcnt);
2811 for my $ctx (@ctx) {
2812 my ($clen, $cindent) = line_stats
($ctx);
2813 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2814 $indent != $cindent) {
2815 $err .= "$sep$ctx\n";
2822 ERROR
("SWITCH_CASE_INDENT_LEVEL",
2823 "switch and case should be at the same indent\n$hereline$err");
2827 # if/while/etc brace do not go on next line, unless defining a do while loop,
2828 # or if that brace on the next line is for something else
2829 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2830 my $pre_ctx = "$1$2";
2832 my ($level, @ctx) = ctx_statement_level
($linenr, $realcnt, 0);
2834 if ($line =~ /^\+\t{6,}/) {
2835 WARN
("DEEP_INDENTATION",
2836 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2839 my $ctx_cnt = $realcnt - $#ctx - 1;
2840 my $ctx = join("\n", @ctx);
2842 my $ctx_ln = $linenr;
2843 my $ctx_skip = $realcnt;
2845 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2846 defined $lines[$ctx_ln - 1] &&
2847 $lines[$ctx_ln - 1] =~ /^-/)) {
2848 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2849 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2853 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2854 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2856 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2858 "that open brace { should be on the previous line\n" .
2859 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2861 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2862 $ctx =~ /\)\s*\;\s*$/ &&
2863 defined $lines[$ctx_ln - 1])
2865 my ($nlength, $nindent) = line_stats
($lines[$ctx_ln - 1]);
2866 if ($nindent > $indent) {
2867 WARN
("TRAILING_SEMICOLON",
2868 "trailing semicolon indicates no statements, indent implies otherwise\n" .
2869 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2874 # Check relative indent for conditionals and blocks.
2875 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
2876 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2877 ctx_statement_block
($linenr, $realcnt, 0)
2878 if (!defined $stat);
2879 my ($s, $c) = ($stat, $cond);
2881 substr($s, 0, length($c), '');
2883 # Make sure we remove the line prefixes as we have
2884 # none on the first line, and are going to readd them
2888 # Find out how long the conditional actually is.
2889 my @newlines = ($c =~ /\n/gs);
2890 my $cond_lines = 1 + $#newlines;
2892 # We want to check the first line inside the block
2893 # starting at the end of the conditional, so remove:
2894 # 1) any blank line termination
2895 # 2) any opening brace { on end of the line
2897 my $continuation = 0;
2899 $s =~ s/^.*\bdo\b//;
2901 if ($s =~ s/^\s*\\//) {
2904 if ($s =~ s/^\s*?\n//) {
2909 # Also ignore a loop construct at the end of a
2910 # preprocessor statement.
2911 if (($prevline =~ /^.\s*#\s*define\s/ ||
2912 $prevline =~ /\\\s*$/) && $continuation == 0) {
2918 while ($cond_ptr != $cond_lines) {
2919 $cond_ptr = $cond_lines;
2921 # If we see an #else/#elif then the code
2923 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2928 # 1) blank lines, they should be at 0,
2929 # 2) preprocessor lines, and
2931 if ($continuation ||
2933 $s =~ /^\s*#\s*?/ ||
2934 $s =~ /^\s*$Ident\s*:/) {
2935 $continuation = ($s =~ /^.*?\\\n/) ?
1 : 0;
2936 if ($s =~ s/^.*?\n//) {
2942 my (undef, $sindent) = line_stats
("+" . $s);
2943 my $stat_real = raw_line
($linenr, $cond_lines);
2945 # Check if either of these lines are modified, else
2946 # this is not this patch's fault.
2947 if (!defined($stat_real) ||
2948 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2951 if (defined($stat_real) && $cond_lines > 1) {
2952 $stat_real = "[...]\n$stat_real";
2955 #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";
2957 if ($check && (($sindent % 8) != 0 ||
2958 ($sindent <= $indent && $s ne ''))) {
2959 WARN
("SUSPECT_CODE_INDENT",
2960 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
2964 # Track the 'values' across context and added lines.
2965 my $opline = $line; $opline =~ s/^./ /;
2966 my ($curr_values, $curr_vars) =
2967 annotate_values
($opline . "\n", $prev_values);
2968 $curr_values = $prev_values . $curr_values;
2970 my $outline = $opline; $outline =~ s/\t/ /g;
2971 print "$linenr > .$outline\n";
2972 print "$linenr > $curr_values\n";
2973 print "$linenr > $curr_vars\n";
2975 $prev_values = substr($curr_values, -1);
2977 #ignore lines not being added
2978 next if ($line =~ /^[^\+]/);
2980 # TEST: allow direct testing of the type matcher.
2982 if ($line =~ /^.\s*$Declare\s*$/) {
2984 "TEST: is type\n" . $herecurr);
2985 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2986 ERROR
("TEST_NOT_TYPE",
2987 "TEST: is not type ($1 is)\n". $herecurr);
2991 # TEST: allow direct testing of the attribute matcher.
2993 if ($line =~ /^.\s*$Modifier\s*$/) {
2995 "TEST: is attr\n" . $herecurr);
2996 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2997 ERROR
("TEST_NOT_ATTR",
2998 "TEST: is not attr ($1 is)\n". $herecurr);
3003 # check for initialisation to aggregates open brace on the next line
3004 if ($line =~ /^.\s*{/ &&
3005 $prevline =~ /(?:^|[^=])=\s*$/) {
3006 if (ERROR
("OPEN_BRACE",
3007 "that open brace { should be on the previous line\n" . $hereprev) &&
3008 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3009 fix_delete_line
($fixlinenr - 1, $prevrawline);
3010 fix_delete_line
($fixlinenr, $rawline);
3011 my $fixedline = $prevrawline;
3012 $fixedline =~ s/\s*=\s*$/ = {/;
3013 fix_insert_line
($fixlinenr, $fixedline);
3015 $fixedline =~ s/^(.\s*){\s*/$1/;
3016 fix_insert_line
($fixlinenr, $fixedline);
3021 # Checks which are anchored on the added line.
3024 # check for malformed paths in #include statements (uses RAW line)
3025 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3027 if ($path =~ m{//}) {
3028 ERROR
("MALFORMED_INCLUDE",
3029 "malformed #include filename\n" . $herecurr);
3031 if ($path =~ "^uapi/" && $realfile =~ m
@\binclude/uapi/@) {
3032 ERROR
("UAPI_INCLUDE",
3033 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3037 # no C99 // comments
3038 if ($line =~ m{//}) {
3039 if (ERROR
("C99_COMMENTS",
3040 "do not use C99 // comments\n" . $herecurr) &&
3042 my $line = $fixed[$fixlinenr];
3043 if ($line =~ /\/\
/(.*)$/) {
3044 my $comment = trim
($1);
3045 $fixed[$fixlinenr] =~ s
@\/\/(.*)$@/\* $comment \
*/@;
3049 # Remove C99 comments.
3051 $opline =~ s
@//.*@@;
3053 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3054 # the whole statement.
3055 #print "APW <$lines[$realline_next - 1]>\n";
3056 if (defined $realline_next &&
3057 exists $lines[$realline_next - 1] &&
3058 !defined $suppress_export{$realline_next} &&
3059 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3060 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3061 # Handle definitions which produce identifiers with
3064 # EXPORT_SYMBOL(something_foo);
3066 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
3067 $name =~ /^${Ident}_$2/) {
3068 #print "FOO C name<$name>\n";
3069 $suppress_export{$realline_next} = 1;
3071 } elsif ($stat !~ /(?
:
3073 ^.DEFINE_
$Ident\(\Q
$name\E\)|
3074 ^.DECLARE_
$Ident\(\Q
$name\E\)|
3075 ^.LIST_HEAD\
(\Q
$name\E\)|
3076 ^.(?
:$Storage\s+)?
$Type\s*\(\s
*\
*\s
*\Q
$name\E\s*\)\s
*\
(|
3077 \b\Q
$name\E(?
:\s
+$Attribute)*\s
*(?
:;|=|\
[|\
()
3079 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3080 $suppress_export{$realline_next} = 2;
3082 $suppress_export{$realline_next} = 1;
3085 if (!defined $suppress_export{$linenr} &&
3086 $prevline =~ /^.\s*$/ &&
3087 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3088 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3089 #print "FOO B <$lines[$linenr - 1]>\n";
3090 $suppress_export{$linenr} = 2;
3092 if (defined $suppress_export{$linenr} &&
3093 $suppress_export{$linenr} == 2) {
3094 WARN
("EXPORT_SYMBOL",
3095 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
3098 # check for global initialisers.
3099 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3100 if (ERROR
("GLOBAL_INITIALISERS",
3101 "do not initialise globals to 0 or NULL\n" .
3104 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
3107 # check for static initialisers.
3108 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3109 if (ERROR
("INITIALISED_STATIC",
3110 "do not initialise statics to 0 or NULL\n" .
3113 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
3117 # check for misordered declarations of char/short/int/long with signed/unsigned
3118 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3120 WARN
("MISORDERED_TYPE",
3121 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3124 # check for static const char * arrays.
3125 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3126 WARN
("STATIC_CONST_CHAR_ARRAY",
3127 "static const char * array should probably be static const char * const\n" .
3131 # check for static char foo[] = "bar" declarations.
3132 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3133 WARN
("STATIC_CONST_CHAR_ARRAY",
3134 "static char array declaration should probably be static const char\n" .
3138 # check for non-global char *foo[] = {"bar", ...} declarations.
3139 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3140 WARN
("STATIC_CONST_CHAR_ARRAY",
3141 "char * array declaration might be better as static const\n" .
3145 # check for function declarations without arguments like "int foo()"
3146 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3147 if (ERROR
("FUNCTION_WITHOUT_ARGS",
3148 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3150 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3154 # check for uses of DEFINE_PCI_DEVICE_TABLE
3155 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3156 if (WARN
("DEFINE_PCI_DEVICE_TABLE",
3157 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3159 $fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
3163 # check for new typedefs, only function parameters and sparse annotations
3165 if ($line =~ /\btypedef\s/ &&
3166 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3167 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
3168 $line !~ /\b$typeTypedefs\b/ &&
3169 $line !~ /\b$typeOtherOSTypedefs\b/ &&
3170 $line !~ /\b__bitwise(?:__|)\b/) {
3171 WARN
("NEW_TYPEDEFS",
3172 "do not add new typedefs\n" . $herecurr);
3175 # * goes on variable not on type
3177 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3179 my ($ident, $from, $to) = ($1, $2, $2);
3181 # Should start with a space.
3182 $to =~ s/^(\S)/ $1/;
3183 # Should not end with a space.
3185 # '*'s should not have spaces between.
3186 while ($to =~ s/\*\s+\*/\*\*/) {
3189 ## print "1: from<$from> to<$to> ident<$ident>\n";
3191 if (ERROR
("POINTER_LOCATION",
3192 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3194 my $sub_from = $ident;
3195 my $sub_to = $ident;
3196 $sub_to =~ s/\Q$from\E/$to/;
3197 $fixed[$fixlinenr] =~
3198 s
@\Q$sub_from\E@$sub_to@;
3202 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3204 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3206 # Should start with a space.
3207 $to =~ s/^(\S)/ $1/;
3208 # Should not end with a space.
3210 # '*'s should not have spaces between.
3211 while ($to =~ s/\*\s+\*/\*\*/) {
3213 # Modifiers should have spaces.
3214 $to =~ s/(\b$Modifier$)/$1 /;
3216 ## print "2: from<$from> to<$to> ident<$ident>\n";
3217 if ($from ne $to && $ident !~ /^$Modifier$/) {
3218 if (ERROR
("POINTER_LOCATION",
3219 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3222 my $sub_from = $match;
3223 my $sub_to = $match;
3224 $sub_to =~ s/\Q$from\E/$to/;
3225 $fixed[$fixlinenr] =~
3226 s
@\Q$sub_from\E@$sub_to@;
3231 # # no BUG() or BUG_ON()
3232 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
3233 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3234 # print "$herecurr";
3238 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3239 WARN
("LINUX_VERSION_CODE",
3240 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
3243 # check for uses of printk_ratelimit
3244 if ($line =~ /\bprintk_ratelimit\s*\(/) {
3245 WARN
("PRINTK_RATELIMITED",
3246 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
3249 # printk should use KERN_* levels. Note that follow on printk's on the
3250 # same line do not need a level, so we use the current block context
3251 # to try and find and validate the current printk. In summary the current
3252 # printk includes all preceding printk's which have no newline on the end.
3253 # we assume the first bad printk is the one to report.
3254 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
3256 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3257 #print "CHECK<$lines[$ln - 1]\n";
3258 # we have a preceding printk if it ends
3259 # with "\n" ignore it, else it is to blame
3260 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3261 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3268 WARN
("PRINTK_WITHOUT_KERN_LEVEL",
3269 "printk() should include KERN_ facility level\n" . $herecurr);
3273 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3275 my $level = lc($orig);
3276 $level = "warn" if ($level eq "warning");
3277 my $level2 = $level;
3278 $level2 = "dbg" if ($level eq "debug");
3279 WARN
("PREFER_PR_LEVEL",
3280 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
3283 if ($line =~ /\bpr_warning\s*\(/) {
3284 if (WARN
("PREFER_PR_LEVEL",
3285 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3287 $fixed[$fixlinenr] =~
3288 s/\bpr_warning\b/pr_warn/;
3292 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3294 my $level = lc($orig);
3295 $level = "warn" if ($level eq "warning");
3296 $level = "dbg" if ($level eq "debug");
3297 WARN
("PREFER_DEV_LEVEL",
3298 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3301 # function brace can't be on same line, except for #defines of do while,
3302 # or if closed on same line
3303 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3304 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
3305 if (ERROR
("OPEN_BRACE",
3306 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3308 fix_delete_line
($fixlinenr, $rawline);
3309 my $fixed_line = $rawline;
3310 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3313 fix_insert_line
($fixlinenr, ltrim
($line1));
3314 fix_insert_line
($fixlinenr, "\+{");
3315 if ($line2 !~ /^\s*$/) {
3316 fix_insert_line
($fixlinenr, "\+\t" . trim
($line2));
3321 # open braces for enum, union and struct go on the same line.
3322 if ($line =~ /^.\s*{/ &&
3323 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
3324 if (ERROR
("OPEN_BRACE",
3325 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3326 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3327 fix_delete_line
($fixlinenr - 1, $prevrawline);
3328 fix_delete_line
($fixlinenr, $rawline);
3329 my $fixedline = rtrim
($prevrawline) . " {";
3330 fix_insert_line
($fixlinenr, $fixedline);
3331 $fixedline = $rawline;
3332 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3333 if ($fixedline !~ /^\+\s*$/) {
3334 fix_insert_line
($fixlinenr, $fixedline);
3339 # missing space after union, struct or enum definition
3340 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3342 "missing space after $1 definition\n" . $herecurr) &&
3344 $fixed[$fixlinenr] =~
3345 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3349 # Function pointer declarations
3350 # check spacing between type, funcptr, and args
3351 # canonical declaration is "type (*funcptr)(args...)"
3352 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
3354 my $pre_pointer_space = $2;
3355 my $post_pointer_space = $3;
3357 my $post_funcname_space = $5;
3358 my $pre_args_space = $6;
3360 # the $Declare variable will capture all spaces after the type
3361 # so check it for a missing trailing missing space but pointer return types
3362 # don't need a space so don't warn for those.
3363 my $post_declare_space = "";
3364 if ($declare =~ /(\s+)$/) {
3365 $post_declare_space = $1;
3366 $declare = rtrim
($declare);
3368 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
3370 "missing space after return type\n" . $herecurr);
3371 $post_declare_space = " ";
3374 # unnecessary space "type (*funcptr)(args...)"
3375 # This test is not currently implemented because these declarations are
3377 # int foo(int bar, ...)
3378 # and this is form shouldn't/doesn't generate a checkpatch warning.
3380 # elsif ($declare =~ /\s{2,}$/) {
3382 # "Multiple spaces after return type\n" . $herecurr);
3385 # unnecessary space "type ( *funcptr)(args...)"
3386 if (defined $pre_pointer_space &&
3387 $pre_pointer_space =~ /^\s/) {
3389 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3392 # unnecessary space "type (* funcptr)(args...)"
3393 if (defined $post_pointer_space &&
3394 $post_pointer_space =~ /^\s/) {
3396 "Unnecessary space before function pointer name\n" . $herecurr);
3399 # unnecessary space "type (*funcptr )(args...)"
3400 if (defined $post_funcname_space &&
3401 $post_funcname_space =~ /^\s/) {
3403 "Unnecessary space after function pointer name\n" . $herecurr);
3406 # unnecessary space "type (*funcptr) (args...)"
3407 if (defined $pre_args_space &&
3408 $pre_args_space =~ /^\s/) {
3410 "Unnecessary space before function pointer arguments\n" . $herecurr);
3413 if (show_type
("SPACING") && $fix) {
3414 $fixed[$fixlinenr] =~
3415 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
3419 # check for spacing round square brackets; allowed:
3420 # 1. with a type on the left -- int [] a;
3421 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3422 # 3. inside a curly brace -- = { [0...10] = 5 }
3423 while ($line =~ /(.*?\s)\[/g) {
3424 my ($where, $prefix) = ($-[1], $1);
3425 if ($prefix !~ /$Type\s+$/ &&
3426 ($where != 0 || $prefix !~ /^.\s+$/) &&
3427 $prefix !~ /[{,]\s+$/) {
3428 if (ERROR
("BRACKET_SPACE",
3429 "space prohibited before open square bracket '['\n" . $herecurr) &&
3431 $fixed[$fixlinenr] =~
3432 s/^(\+.*?)\s+\[/$1\[/;
3437 # check for spaces between functions and their parentheses.
3438 while ($line =~ /($Ident)\s+\(/g) {
3440 my $ctx_before = substr($line, 0, $-[1]);
3441 my $ctx = "$ctx_before$name";
3443 # Ignore those directives where spaces _are_ permitted.
3445 if|for|while|switch
|return|case
|
3446 volatile
|__volatile__
|
3447 __attribute__
|format
|__extension__
|
3450 # cpp #define statements have non-optional spaces, ie
3451 # if there is a space between the name and the open
3452 # parenthesis it is simply not a parameter group.
3453 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3455 # cpp #elif statement condition may start with a (
3456 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3458 # If this whole things ends with a type its most
3459 # likely a typedef for a function.
3460 } elsif ($ctx =~ /$Type$/) {
3464 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3466 $fixed[$fixlinenr] =~
3467 s/\b$name\s+\(/$name\(/;
3472 # Check operator spacing.
3473 if (!($line=~/\#\s*include/)) {
3474 my $fixed_line = "";
3478 <<=|>>=|<=|>=|==|!=|
3479 \
+=|-=|\
*=|\
/=|%=|\
^=|\
|=|&=|
3480 =>|->|<<|>>|<|>|=|!|~|
3481 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%|
3484 my @elements = split(/($ops|;)/, $opline);
3486 ## print("element count: <" . $#elements . ">\n");
3487 ## foreach my $el (@elements) {
3488 ## print("el: <$el>\n");
3491 my @fix_elements = ();
3494 foreach my $el (@elements) {
3495 push(@fix_elements, substr($rawline, $off, length($el)));
3496 $off += length($el);
3501 my $blank = copy_spacing
($opline);
3502 my $last_after = -1;
3504 for (my $n = 0; $n < $#elements; $n += 2) {
3506 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3508 ## print("n: <$n> good: <$good>\n");
3510 $off += length($elements[$n]);
3512 # Pick up the preceding and succeeding characters.
3513 my $ca = substr($opline, 0, $off);
3515 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3516 $cc = substr($opline, $off + length($elements[$n + 1]));
3518 my $cb = "$ca$;$cc";
3521 $a = 'V' if ($elements[$n] ne '');
3522 $a = 'W' if ($elements[$n] =~ /\s$/);
3523 $a = 'C' if ($elements[$n] =~ /$;$/);
3524 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3525 $a = 'O' if ($elements[$n] eq '');
3526 $a = 'E' if ($ca =~ /^\s*$/);
3528 my $op = $elements[$n + 1];
3531 if (defined $elements[$n + 2]) {
3532 $c = 'V' if ($elements[$n + 2] ne '');
3533 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
3534 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
3535 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3536 $c = 'O' if ($elements[$n + 2] eq '');
3537 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
3542 my $ctx = "${a}x${c}";
3544 my $at = "(ctx:$ctx)";
3546 my $ptr = substr($blank, 0, $off) . "^";
3547 my $hereptr = "$hereline$ptr\n";
3549 # Pull out the value of this operator.
3550 my $op_type = substr($curr_values, $off + 1, 1);
3552 # Get the full operator variant.
3553 my $opv = $op . substr($curr_vars, $off, 1);
3555 # Ignore operators passed as parameters.
3556 if ($op_type ne 'V' &&
3557 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3560 # } elsif ($op =~ /^$;+$/) {
3562 # ; should have either the end of line or a space or \ after it
3563 } elsif ($op eq ';') {
3564 if ($ctx !~ /.x[WEBC]/ &&
3565 $cc !~ /^\\/ && $cc !~ /^;/) {
3566 if (ERROR
("SPACING",
3567 "space required after that '$op' $at\n" . $hereptr)) {
3568 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]) . " ";
3574 } elsif ($op eq '//') {
3576 # : when part of a bitfield
3577 } elsif ($opv eq ':B') {
3578 # skip the bitfield test for now
3582 } elsif ($op eq '->') {
3583 if ($ctx =~ /Wx.|.xW/) {
3584 if (ERROR
("SPACING",
3585 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3586 $good = rtrim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
3587 if (defined $fix_elements[$n + 2]) {
3588 $fix_elements[$n + 2] =~ s/^\s+//;
3594 # , must not have a space before and must have a space on the right.
3595 } elsif ($op eq ',') {
3596 my $rtrim_before = 0;
3597 my $space_after = 0;
3598 if ($ctx =~ /Wx./) {
3599 if (ERROR
("SPACING",
3600 "space prohibited before that '$op' $at\n" . $hereptr)) {
3605 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
3606 if (ERROR
("SPACING",
3607 "space required after that '$op' $at\n" . $hereptr)) {
3613 if ($rtrim_before || $space_after) {
3614 if ($rtrim_before) {
3615 $good = rtrim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
3617 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]);
3624 # '*' as part of a type definition -- reported already.
3625 } elsif ($opv eq '*_') {
3626 #warn "'*' is part of type\n";
3628 # unary operators should have a space before and
3629 # none after. May be left adjacent to another
3630 # unary operator, or a cast
3631 } elsif ($op eq '!' || $op eq '~' ||
3632 $opv eq '*U' || $opv eq '-U' ||
3633 $opv eq '&U' || $opv eq '&&U') {
3634 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
3635 if (ERROR
("SPACING",
3636 "space required before that '$op' $at\n" . $hereptr)) {
3637 if ($n != $last_after + 2) {
3638 $good = $fix_elements[$n] . " " . ltrim
($fix_elements[$n + 1]);
3643 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3644 # A unary '*' may be const
3646 } elsif ($ctx =~ /.xW/) {
3647 if (ERROR
("SPACING",
3648 "space prohibited after that '$op' $at\n" . $hereptr)) {
3649 $good = $fix_elements[$n] . rtrim
($fix_elements[$n + 1]);
3650 if (defined $fix_elements[$n + 2]) {
3651 $fix_elements[$n + 2] =~ s/^\s+//;
3657 # unary ++ and unary -- are allowed no space on one side.
3658 } elsif ($op eq '++' or $op eq '--') {
3659 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
3660 if (ERROR
("SPACING",
3661 "space required one side of that '$op' $at\n" . $hereptr)) {
3662 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]) . " ";
3666 if ($ctx =~ /Wx[BE]/ ||
3667 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
3668 if (ERROR
("SPACING",
3669 "space prohibited before that '$op' $at\n" . $hereptr)) {
3670 $good = rtrim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
3674 if ($ctx =~ /ExW/) {
3675 if (ERROR
("SPACING",
3676 "space prohibited after that '$op' $at\n" . $hereptr)) {
3677 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]);
3678 if (defined $fix_elements[$n + 2]) {
3679 $fix_elements[$n + 2] =~ s/^\s+//;
3685 # << and >> may either have or not have spaces both sides
3686 } elsif ($op eq '<<' or $op eq '>>' or
3687 $op eq '&' or $op eq '^' or $op eq '|' or
3688 $op eq '+' or $op eq '-' or
3689 $op eq '*' or $op eq '/' or
3692 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
3693 if (ERROR
("SPACING",
3694 "need consistent spacing around '$op' $at\n" . $hereptr)) {
3695 $good = rtrim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]) . " ";
3696 if (defined $fix_elements[$n + 2]) {
3697 $fix_elements[$n + 2] =~ s/^\s+//;
3703 # A colon needs no spaces before when it is
3704 # terminating a case value or a label.
3705 } elsif ($opv eq ':C' || $opv eq ':L') {
3706 if ($ctx =~ /Wx./) {
3707 if (ERROR
("SPACING",
3708 "space prohibited before that '$op' $at\n" . $hereptr)) {
3709 $good = rtrim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
3714 # All the others need spaces both sides.
3715 } elsif ($ctx !~ /[EWC]x[CWE]/) {
3718 # Ignore email addresses <foo@bar>
3720 $cc =~ /^\S+\@\S+>/) ||
3722 $ca =~ /<\S+\@\S+$/))
3727 # messages are ERROR, but ?: are CHK
3729 my $msg_type = \
&ERROR
;
3730 $msg_type = \
&CHK
if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3732 if (&{$msg_type}("SPACING",
3733 "spaces required around that '$op' $at\n" . $hereptr)) {
3734 $good = rtrim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]) . " ";
3735 if (defined $fix_elements[$n + 2]) {
3736 $fix_elements[$n + 2] =~ s/^\s+//;
3742 $off += length($elements[$n + 1]);
3744 ## print("n: <$n> GOOD: <$good>\n");
3746 $fixed_line = $fixed_line . $good;
3749 if (($#elements % 2) == 0) {
3750 $fixed_line = $fixed_line . $fix_elements[$#elements];
3753 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3754 $fixed[$fixlinenr] = $fixed_line;
3760 # check for whitespace before a non-naked semicolon
3761 if ($line =~ /^\+.*\S\s+;\s*$/) {
3763 "space prohibited before semicolon\n" . $herecurr) &&
3765 1 while $fixed[$fixlinenr] =~
3766 s/^(\+.*\S)\s+;/$1;/;
3770 # check for multiple assignments
3771 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3772 CHK
("MULTIPLE_ASSIGNMENTS",
3773 "multiple assignments should be avoided\n" . $herecurr);
3776 ## # check for multiple declarations, allowing for a function declaration
3778 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3779 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3781 ## # Remove any bracketed sections to ensure we do not
3782 ## # falsly report the parameters of functions.
3784 ## while ($ln =~ s/\([^\(\)]*\)//g) {
3786 ## if ($ln =~ /,/) {
3787 ## WARN("MULTIPLE_DECLARATION",
3788 ## "declaring multiple variables together should be avoided\n" . $herecurr);
3792 #need space before brace following if, while, etc
3793 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3795 if (ERROR
("SPACING",
3796 "space required before the open brace '{'\n" . $herecurr) &&
3798 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
3802 ## # check for blank lines before declarations
3803 ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3804 ## $prevrawline =~ /^.\s*$/) {
3806 ## "No blank lines before declarations\n" . $hereprev);
3810 # closing brace should have a space following it when it has anything
3812 if ($line =~ /}(?!(?:,|;|\)))\S/) {
3813 if (ERROR
("SPACING",
3814 "space required after that close brace '}'\n" . $herecurr) &&
3816 $fixed[$fixlinenr] =~
3817 s/}((?!(?:,|;|\)))\S)/} $1/;
3821 # check spacing on square brackets
3822 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
3823 if (ERROR
("SPACING",
3824 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3826 $fixed[$fixlinenr] =~
3830 if ($line =~ /\s\]/) {
3831 if (ERROR
("SPACING",
3832 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3834 $fixed[$fixlinenr] =~
3839 # check spacing on parentheses
3840 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3841 $line !~ /for\s*\(\s+;/) {
3842 if (ERROR
("SPACING",
3843 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3845 $fixed[$fixlinenr] =~
3849 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3850 $line !~ /for\s*\(.*;\s+\)/ &&
3851 $line !~ /:\s+\)/) {
3852 if (ERROR
("SPACING",
3853 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3855 $fixed[$fixlinenr] =~
3860 # check unnecessary parentheses around addressof/dereference single $Lvals
3861 # ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3863 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3865 if (CHK
("UNNECESSARY_PARENTHESES",
3866 "Unnecessary parentheses around $var\n" . $herecurr) &&
3868 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
3872 # check for unnecessary parentheses around function pointer uses
3873 # ie: (foo->bar)(); should be foo->bar();
3874 # but not "if (foo->bar) (" to avoid some false positives
3875 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
3877 if (CHK
("UNNECESSARY_PARENTHESES",
3878 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
3880 my $var2 = deparenthesize
($var);
3882 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
3886 #goto labels aren't indented, allow a single space however
3887 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
3888 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
3889 if (WARN
("INDENTED_LABEL",
3890 "labels should not be indented\n" . $herecurr) &&
3892 $fixed[$fixlinenr] =~
3897 # return is not a function
3898 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3900 if ($^V && $^V ge 5.10.0 &&
3901 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3903 $value = deparenthesize
($value);
3904 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3905 ERROR
("RETURN_PARENTHESES",
3906 "return is not a function, parentheses are not required\n" . $herecurr);
3908 } elsif ($spacing !~ /\s+/) {
3910 "space required before the open parenthesis '('\n" . $herecurr);
3914 # unnecessary return in a void function
3915 # at end-of-function, with the previous line a single leading tab, then return;
3916 # and the line before that not a goto label target like "out:"
3917 if ($sline =~ /^[ \+]}\s*$/ &&
3918 $prevline =~ /^\+\treturn\s*;\s*$/ &&
3920 $lines[$linenr - 3] =~ /^[ +]/ &&
3921 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
3923 "void function return statements are not generally useful\n" . $hereprev);
3926 # if statements using unnecessary parentheses - ie: if ((foo == bar))
3927 if ($^V && $^V ge 5.10.0 &&
3928 $line =~ /\bif\s*((?:\(\s*){2,})/) {
3929 my $openparens = $1;
3930 my $count = $openparens =~ tr
@\(@\(@;
3932 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3933 my $comp = $4; #Not $1 because of $LvalOrFunc
3934 $msg = " - maybe == should be = ?" if ($comp eq "==");
3935 WARN
("UNNECESSARY_PARENTHESES",
3936 "Unnecessary parentheses$msg\n" . $herecurr);
3940 # Return of what appears to be an errno should normally be -'ve
3941 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3943 if ($name ne 'EOF' && $name ne 'ERROR') {
3944 WARN
("USE_NEGATIVE_ERRNO",
3945 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
3949 # Need a space before open parenthesis after if, while etc
3950 if ($line =~ /\b(if|while|for|switch)\(/) {
3951 if (ERROR
("SPACING",
3952 "space required before the open parenthesis '('\n" . $herecurr) &&
3954 $fixed[$fixlinenr] =~
3955 s/\b(if|while|for|switch)\(/$1 \(/;
3959 # Check for illegal assignment in if conditional -- and check for trailing
3960 # statements after the conditional.
3961 if ($line =~ /do\s*(?!{)/) {
3962 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3963 ctx_statement_block
($linenr, $realcnt, 0)
3964 if (!defined $stat);
3965 my ($stat_next) = ctx_statement_block
($line_nr_next,
3966 $remain_next, $off_next);
3967 $stat_next =~ s/\n./\n /g;
3968 ##print "stat<$stat> stat_next<$stat_next>\n";
3970 if ($stat_next =~ /^\s*while\b/) {
3971 # If the statement carries leading newlines,
3972 # then count those as offsets.
3974 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3976 statement_rawlines
($whitespace) - 1;
3978 $suppress_whiletrailers{$line_nr_next +
3982 if (!defined $suppress_whiletrailers{$linenr} &&
3983 defined($stat) && defined($cond) &&
3984 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3985 my ($s, $c) = ($stat, $cond);
3987 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3988 ERROR
("ASSIGN_IN_IF",
3989 "do not use assignment in if condition\n" . $herecurr);
3992 # Find out what is on the end of the line after the
3994 substr($s, 0, length($c), '');
3996 $s =~ s/$;//g; # Remove any comments
3997 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3998 $c !~ /}\s*while\s*/)
4000 # Find out how long the conditional actually is.
4001 my @newlines = ($c =~ /\n/gs);
4002 my $cond_lines = 1 + $#newlines;
4005 $stat_real = raw_line
($linenr, $cond_lines)
4006 . "\n" if ($cond_lines);
4007 if (defined($stat_real) && $cond_lines > 1) {
4008 $stat_real = "[...]\n$stat_real";
4011 ERROR
("TRAILING_STATEMENTS",
4012 "trailing statements should be on next line\n" . $herecurr . $stat_real);
4016 # Check for bitwise tests written as boolean
4028 WARN
("HEXADECIMAL_BOOLEAN_TEST",
4029 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
4032 # if and else should not have general statements after it
4033 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4035 $s =~ s/$;//g; # Remove any comments
4036 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4037 ERROR
("TRAILING_STATEMENTS",
4038 "trailing statements should be on next line\n" . $herecurr);
4041 # if should not continue a brace
4042 if ($line =~ /}\s*if\b/) {
4043 ERROR
("TRAILING_STATEMENTS",
4044 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
4047 # case and default should not have general statements after them
4048 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4050 (?
:\s
*$;*)(?
:\s
*{)?
(?
:\s
*$;*)(?
:\s
*\\)?\s
*$|
4054 ERROR
("TRAILING_STATEMENTS",
4055 "trailing statements should be on next line\n" . $herecurr);
4058 # Check for }<nl>else {, these must be at the same
4059 # indent level to be relevant to each other.
4060 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4061 $previndent == $indent) {
4062 if (ERROR
("ELSE_AFTER_BRACE",
4063 "else should follow close brace '}'\n" . $hereprev) &&
4064 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4065 fix_delete_line
($fixlinenr - 1, $prevrawline);
4066 fix_delete_line
($fixlinenr, $rawline);
4067 my $fixedline = $prevrawline;
4068 $fixedline =~ s/}\s*$//;
4069 if ($fixedline !~ /^\+\s*$/) {
4070 fix_insert_line
($fixlinenr, $fixedline);
4072 $fixedline = $rawline;
4073 $fixedline =~ s/^(.\s*)else/$1} else/;
4074 fix_insert_line
($fixlinenr, $fixedline);
4078 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4079 $previndent == $indent) {
4080 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
4082 # Find out what is on the end of the line after the
4084 substr($s, 0, length($c), '');
4087 if ($s =~ /^\s*;/) {
4088 if (ERROR
("WHILE_AFTER_BRACE",
4089 "while should follow close brace '}'\n" . $hereprev) &&
4090 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4091 fix_delete_line
($fixlinenr - 1, $prevrawline);
4092 fix_delete_line
($fixlinenr, $rawline);
4093 my $fixedline = $prevrawline;
4094 my $trailing = $rawline;
4095 $trailing =~ s/^\+//;
4096 $trailing = trim
($trailing);
4097 $fixedline =~ s/}\s*$/} $trailing/;
4098 fix_insert_line
($fixlinenr, $fixedline);
4103 #Specific variable tests
4104 while ($line =~ m{($Constant|$Lval)}g) {
4107 #gcc binary extension
4108 if ($var =~ /^$Binary$/) {
4109 if (WARN
("GCC_BINARY_CONSTANT",
4110 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4112 my $hexval = sprintf("0x%x", oct($var));
4113 $fixed[$fixlinenr] =~
4114 s/\b$var\b/$hexval/;
4119 if ($var !~ /^$Constant$/ &&
4120 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
4121 #Ignore Page<foo> variants
4122 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
4123 #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4124 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4125 #Ignore some three character SI units explicitly, like MiB and KHz
4126 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
4127 while ($var =~ m{($Ident)}g) {
4129 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4131 seed_camelcase_includes
();
4132 if (!$file && !$camelcase_file_seeded) {
4133 seed_camelcase_file
($realfile);
4134 $camelcase_file_seeded = 1;
4137 if (!defined $camelcase{$word}) {
4138 $camelcase{$word} = 1;
4140 "Avoid CamelCase: <$word>\n" . $herecurr);
4146 #no spaces allowed after \ in define
4147 if ($line =~ /\#\s*define.*\\\s+$/) {
4148 if (WARN
("WHITESPACE_AFTER_LINE_CONTINUATION",
4149 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4151 $fixed[$fixlinenr] =~ s/\s+$//;
4155 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
4156 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4158 my $checkfile = "include/linux/$file";
4159 if (-f
"$root/$checkfile" &&
4160 $realfile ne $checkfile &&
4161 $1 !~ /$allowed_asm_includes/)
4163 if ($realfile =~ m{^arch/}) {
4164 CHK
("ARCH_INCLUDE_LINUX",
4165 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4167 WARN
("INCLUDE_LINUX",
4168 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4173 # multi-statement macros should be enclosed in a do while loop, grab the
4174 # first statement and ensure its the whole macro if its not enclosed
4175 # in a known good container
4176 if ($realfile !~ m
@/vmlinux.lds
.h
$@ &&
4177 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4180 my ($off, $dstat, $dcond, $rest);
4182 my $has_flow_statement = 0;
4183 my $has_arg_concat = 0;
4184 ($dstat, $dcond, $ln, $cnt, $off) =
4185 ctx_statement_block
($linenr, $realcnt, 0);
4187 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4188 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4190 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4191 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4193 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4195 $dstat =~ s/\\\n.//g;
4196 $dstat =~ s/^\s*//s;
4197 $dstat =~ s/\s*$//s;
4199 # Flatten any parentheses and braces
4200 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4201 $dstat =~ s/\{[^\{\}]*\}/1/ ||
4202 $dstat =~ s/\[[^\[\]]*\]/1/)
4206 # Flatten any obvious string concatentation.
4207 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4208 $dstat =~ s/$Ident\s*("X*")/$1/)
4212 my $exceptions = qr{
4224 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4226 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4227 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
4228 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4229 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
4230 $dstat !~ /$exceptions/ &&
4231 $dstat !~ /^\.$Ident\s*=/ && # .foo =
4232 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
4233 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
4234 $dstat !~ /^for\s*$Constant$/ && # for (...)
4235 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4236 $dstat !~ /^do\s*{/ && # do {...
4237 $dstat !~ /^\({/ && # ({...
4238 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4241 my $herectx = $here . "\n";
4242 my $cnt = statement_rawlines
($ctx);
4244 for (my $n = 0; $n < $cnt; $n++) {
4245 $herectx .= raw_line
($linenr, $n) . "\n";
4248 if ($dstat =~ /;/) {
4249 ERROR
("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4250 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4252 ERROR
("COMPLEX_MACRO",
4253 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4257 # check for macros with flow control, but without ## concatenation
4258 # ## concatenation is commonly a macro that defines a function so ignore those
4259 if ($has_flow_statement && !$has_arg_concat) {
4260 my $herectx = $here . "\n";
4261 my $cnt = statement_rawlines
($ctx);
4263 for (my $n = 0; $n < $cnt; $n++) {
4264 $herectx .= raw_line
($linenr, $n) . "\n";
4266 WARN
("MACRO_WITH_FLOW_CONTROL",
4267 "Macros with flow control statements should be avoided\n" . "$herectx");
4270 # check for line continuations outside of #defines, preprocessor #, and asm
4273 if ($prevline !~ /^..*\\$/ &&
4274 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4275 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
4276 $line =~ /^\+.*\\$/) {
4277 WARN
("LINE_CONTINUATIONS",
4278 "Avoid unnecessary line continuations\n" . $herecurr);
4282 # do {} while (0) macro tests:
4283 # single-statement macros do not need to be enclosed in do while (0) loop,
4284 # macro should not end with a semicolon
4285 if ($^V && $^V ge 5.10.0 &&
4286 $realfile !~ m
@/vmlinux.lds
.h
$@ &&
4287 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4290 my ($off, $dstat, $dcond, $rest);
4292 ($dstat, $dcond, $ln, $cnt, $off) =
4293 ctx_statement_block
($linenr, $realcnt, 0);
4296 $dstat =~ s/\\\n.//g;
4299 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4304 my $cnt = statement_rawlines
($ctx);
4305 my $herectx = $here . "\n";
4307 for (my $n = 0; $n < $cnt; $n++) {
4308 $herectx .= raw_line
($linenr, $n) . "\n";
4311 if (($stmts =~ tr/;/;/) == 1 &&
4312 $stmts !~ /^\s*(if|while|for|switch)\b/) {
4313 WARN
("SINGLE_STATEMENT_DO_WHILE_MACRO",
4314 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4316 if (defined $semis && $semis ne "") {
4317 WARN
("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4318 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4320 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4322 my $cnt = statement_rawlines
($ctx);
4323 my $herectx = $here . "\n";
4325 for (my $n = 0; $n < $cnt; $n++) {
4326 $herectx .= raw_line
($linenr, $n) . "\n";
4329 WARN
("TRAILING_SEMICOLON",
4330 "macros should not use a trailing semicolon\n" . "$herectx");
4334 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4335 # all assignments may have only one of the following with an assignment:
4338 # VMLINUX_SYMBOL(...)
4339 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4340 WARN
("MISSING_VMLINUX_SYMBOL",
4341 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4344 # check for redundant bracing round if etc
4345 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4346 my ($level, $endln, @chunks) =
4347 ctx_statement_full
($linenr, $realcnt, 1);
4348 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4349 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4350 if ($#chunks > 0 && $level == 0) {
4354 my $herectx = $here . "\n";
4355 my $ln = $linenr - 1;
4356 for my $chunk (@chunks) {
4357 my ($cond, $block) = @{$chunk};
4359 # If the condition carries leading newlines, then count those as offsets.
4360 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4361 my $offset = statement_rawlines
($whitespace) - 1;
4363 $allowed[$allow] = 0;
4364 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4366 # We have looked at and allowed this specific line.
4367 $suppress_ifbraces{$ln + $offset} = 1;
4369 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4370 $ln += statement_rawlines
($block) - 1;
4372 substr($block, 0, length($cond), '');
4374 $seen++ if ($block =~ /^\s*{/);
4376 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4377 if (statement_lines
($cond) > 1) {
4378 #print "APW: ALLOWED: cond<$cond>\n";
4379 $allowed[$allow] = 1;
4381 if ($block =~/\b(?:if|for|while)\b/) {
4382 #print "APW: ALLOWED: block<$block>\n";
4383 $allowed[$allow] = 1;
4385 if (statement_block_size
($block) > 1) {
4386 #print "APW: ALLOWED: lines block<$block>\n";
4387 $allowed[$allow] = 1;
4392 my $sum_allowed = 0;
4393 foreach (@allowed) {
4396 if ($sum_allowed == 0) {
4398 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4399 } elsif ($sum_allowed != $allow &&
4402 "braces {} should be used on all arms of this statement\n" . $herectx);
4407 if (!defined $suppress_ifbraces{$linenr - 1} &&
4408 $line =~ /\b(if|while|for|else)\b/) {
4411 # Check the pre-context.
4412 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4413 #print "APW: ALLOWED: pre<$1>\n";
4417 my ($level, $endln, @chunks) =
4418 ctx_statement_full
($linenr, $realcnt, $-[0]);
4420 # Check the condition.
4421 my ($cond, $block) = @{$chunks[0]};
4422 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4423 if (defined $cond) {
4424 substr($block, 0, length($cond), '');
4426 if (statement_lines
($cond) > 1) {
4427 #print "APW: ALLOWED: cond<$cond>\n";
4430 if ($block =~/\b(?:if|for|while)\b/) {
4431 #print "APW: ALLOWED: block<$block>\n";
4434 if (statement_block_size
($block) > 1) {
4435 #print "APW: ALLOWED: lines block<$block>\n";
4438 # Check the post-context.
4439 if (defined $chunks[1]) {
4440 my ($cond, $block) = @{$chunks[1]};
4441 if (defined $cond) {
4442 substr($block, 0, length($cond), '');
4444 if ($block =~ /^\s*\{/) {
4445 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4449 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
4450 my $herectx = $here . "\n";
4451 my $cnt = statement_rawlines
($block);
4453 for (my $n = 0; $n < $cnt; $n++) {
4454 $herectx .= raw_line
($linenr, $n) . "\n";
4458 "braces {} are not necessary for single statement blocks\n" . $herectx);
4462 # check for unnecessary blank lines around braces
4463 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
4465 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
4467 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
4469 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
4472 # no volatiles please
4473 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4474 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4476 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
4479 # Check for user-visible strings broken across lines, which breaks the ability
4480 # to grep for the string. Make exceptions when the previous string ends in a
4481 # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
4482 # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
4483 if ($line =~ /^\+\s*"[X\t]*"/ &&
4484 $prevline =~ /"\s*$/ &&
4485 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
4486 if (WARN
("SPLIT_STRING",
4487 "quoted string split across lines\n" . $hereprev) &&
4489 $prevrawline =~ /^\+.*"\s*$/ &&
4490 $last_coalesced_string_linenr != $linenr - 1) {
4491 my $extracted_string = get_quoted_string
($line, $rawline);
4492 my $comma_close = "";
4493 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
4497 fix_delete_line
($fixlinenr - 1, $prevrawline);
4498 fix_delete_line
($fixlinenr, $rawline);
4499 my $fixedline = $prevrawline;
4500 $fixedline =~ s/"\s*$//;
4501 $fixedline .= substr($extracted_string, 1) . trim
($comma_close);
4502 fix_insert_line
($fixlinenr - 1, $fixedline);
4503 $fixedline = $rawline;
4504 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
4505 if ($fixedline !~ /\+\s*$/) {
4506 fix_insert_line
($fixlinenr, $fixedline);
4508 $last_coalesced_string_linenr = $linenr;
4512 # check for missing a space in a string concatenation
4513 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
4514 WARN
('MISSING_SPACE',
4515 "break quoted strings at a space character\n" . $hereprev);
4518 # check for spaces before a quoted newline
4519 if ($rawline =~ /^.*\".*\s\\n/) {
4520 if (WARN
("QUOTED_WHITESPACE_BEFORE_NEWLINE",
4521 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
4523 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
4528 # concatenated string without spaces between elements
4529 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4530 CHK
("CONCATENATED_STRING",
4531 "Concatenated strings should use spaces between elements\n" . $herecurr);
4534 # uncoalesced string fragments
4535 if ($line =~ /"X*"\s*"/) {
4536 WARN
("STRING_FRAGMENTS",
4537 "Consecutive strings are generally better as a single string\n" . $herecurr);
4540 # check for %L{u,d,i} in strings
4542 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4543 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4544 $string =~ s/%%/__/g;
4545 if ($string =~ /(?<!%)%L[udi]/) {
4547 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4552 # check for line continuations in quoted strings with odd counts of "
4553 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4554 WARN
("LINE_CONTINUATIONS",
4555 "Avoid line continuations in quoted strings\n" . $herecurr);
4559 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4560 CHK
("REDUNDANT_CODE",
4561 "if this code is redundant consider removing it\n" .
4565 # check for needless "if (<foo>) fn(<foo>)" uses
4566 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4567 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4568 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4570 "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
4574 # check for unnecessary "Out of Memory" messages
4575 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4576 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4577 (defined $1 || defined $3) &&
4580 my $testline = $lines[$linenr - 3];
4582 my ($s, $c) = ctx_statement_block
($linenr - 3, $realcnt, 0);
4583 # print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4585 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4587 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4591 # check for logging functions with KERN_<LEVEL>
4592 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
4593 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4595 if (WARN
("UNNECESSARY_KERN_LEVEL",
4596 "Possible unnecessary $level\n" . $herecurr) &&
4598 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4602 # check for mask then right shift without a parentheses
4603 if ($^V && $^V ge 5.10.0 &&
4604 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4605 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4606 WARN
("MASK_THEN_SHIFT",
4607 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4610 # check for pointer comparisons to NULL
4611 if ($^V && $^V ge 5.10.0) {
4612 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4615 $equal = "" if ($4 eq "!=");
4616 if (CHK
("COMPARISON_TO_NULL",
4617 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4619 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4624 # check for bad placement of section $InitAttribute (e.g.: __initdata)
4625 if ($line =~ /(\b$InitAttribute\b)/) {
4627 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4630 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4631 ERROR
("MISPLACED_INIT",
4632 "$attr should be placed after $var\n" . $herecurr)) ||
4633 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4634 WARN
("MISPLACED_INIT",
4635 "$attr should be placed after $var\n" . $herecurr))) &&
4637 $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;
4642 # check for $InitAttributeData (ie: __initdata) with const
4643 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4645 $attr =~ /($InitAttributePrefix)(.*)/;
4646 my $attr_prefix = $1;
4648 if (ERROR
("INIT_ATTRIBUTE",
4649 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4651 $fixed[$fixlinenr] =~
4652 s/$InitAttributeData/${attr_prefix}initconst/;
4656 # check for $InitAttributeConst (ie: __initconst) without const
4657 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4659 if (ERROR
("INIT_ATTRIBUTE",
4660 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4662 my $lead = $fixed[$fixlinenr] =~
4663 /(^\+\s*(?:static\s+))/;
4665 $lead = "$lead " if ($lead !~ /^\+$/);
4666 $lead = "${lead}const ";
4667 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
4671 # don't use __constant_<foo> functions outside of include/uapi/
4672 if ($realfile !~ m
@^include/uapi/@ &&
4673 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4674 my $constant_func = $1;
4675 my $func = $constant_func;
4676 $func =~ s/^__constant_//;
4677 if (WARN
("CONSTANT_CONVERSION",
4678 "$constant_func should be $func\n" . $herecurr) &&
4680 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
4684 # prefer usleep_range over udelay
4685 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
4687 # ignore udelay's < 10, however
4688 if (! ($delay < 10) ) {
4690 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4692 if ($delay > 2000) {
4694 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
4698 # warn about unexpectedly long msleep's
4699 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4702 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4706 # check for comparisons of jiffies
4707 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4708 WARN
("JIFFIES_COMPARISON",
4709 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4712 # check for comparisons of get_jiffies_64()
4713 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4714 WARN
("JIFFIES_COMPARISON",
4715 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4718 # warn about #ifdefs in C files
4719 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
4720 # print "#ifdef in C files should be avoided\n";
4721 # print "$herecurr";
4725 # warn about spacing in #ifdefs
4726 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
4727 if (ERROR
("SPACING",
4728 "exactly one space required after that #$1\n" . $herecurr) &&
4730 $fixed[$fixlinenr] =~
4731 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4736 # check for spinlock_t definitions without a comment.
4737 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4738 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
4740 if (!ctx_has_comment
($first_line, $linenr)) {
4741 CHK
("UNCOMMENTED_DEFINITION",
4742 "$1 definition without comment\n" . $herecurr);
4745 # check for memory barriers without a comment.
4746 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4747 if (!ctx_has_comment
($first_line, $linenr)) {
4748 WARN
("MEMORY_BARRIER",
4749 "memory barrier without comment\n" . $herecurr);
4752 # check of hardware specific defines
4753 if ($line =~ m
@^.\s
*\#\s
*if.*\b(__i386__
|__powerpc64__
|__sun__
|__s390x__
)\b@ && $realfile !~ m
@include/asm-@) {
4755 "architecture specific defines should be avoided\n" . $herecurr);
4758 # Check that the storage class is at the beginning of a declaration
4759 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4760 WARN
("STORAGE_CLASS",
4761 "storage class should be at the beginning of the declaration\n" . $herecurr)
4764 # check the location of the inline attribute, that it is between
4765 # storage class and type.
4766 if ($line =~ /\b$Type\s+$Inline\b/ ||
4767 $line =~ /\b$Inline\s+$Storage\b/) {
4768 ERROR
("INLINE_LOCATION",
4769 "inline keyword should sit between storage class and type\n" . $herecurr);
4772 # Check for __inline__ and __inline, prefer inline
4773 if ($realfile !~ m
@\binclude/uapi/@ &&
4774 $line =~ /\b(__inline__|__inline)\b/) {
4776 "plain inline is preferred over $1\n" . $herecurr) &&
4778 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
4783 # Check for __attribute__ packed, prefer __packed
4784 if ($realfile !~ m
@\binclude/uapi/@ &&
4785 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4786 WARN
("PREFER_PACKED",
4787 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
4790 # Check for __attribute__ aligned, prefer __aligned
4791 if ($realfile !~ m
@\binclude/uapi/@ &&
4792 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4793 WARN
("PREFER_ALIGNED",
4794 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
4797 # Check for __attribute__ format(printf, prefer __printf
4798 if ($realfile !~ m
@\binclude/uapi/@ &&
4799 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4800 if (WARN
("PREFER_PRINTF",
4801 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4803 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4808 # Check for __attribute__ format(scanf, prefer __scanf
4809 if ($realfile !~ m
@\binclude/uapi/@ &&
4810 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4811 if (WARN
("PREFER_SCANF",
4812 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4814 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4818 # Check for __attribute__ weak, or __weak declarations (may have link issues)
4819 if ($^V && $^V ge 5.10.0 &&
4820 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
4821 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
4822 $line =~ /\b__weak\b/)) {
4823 ERROR
("WEAK_DECLARATION",
4824 "Using weak declarations can have unintended link defects\n" . $herecurr);
4827 # check for sizeof(&)
4828 if ($line =~ /\bsizeof\s*\(\s*\&/) {
4829 WARN
("SIZEOF_ADDRESS",
4830 "sizeof(& should be avoided\n" . $herecurr);
4833 # check for sizeof without parenthesis
4834 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4835 if (WARN
("SIZEOF_PARENTHESIS",
4836 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4838 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4842 # check for struct spinlock declarations
4843 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4844 WARN
("USE_SPINLOCK_T",
4845 "struct spinlock should be spinlock_t\n" . $herecurr);
4848 # check for seq_printf uses that could be seq_puts
4849 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4850 my $fmt = get_quoted_string
($line, $rawline);
4851 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
4852 if (WARN
("PREFER_SEQ_PUTS",
4853 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4855 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
4860 # Check for misused memsets
4861 if ($^V && $^V ge 5.10.0 &&
4863 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4869 if ($ms_size =~ /^(0x|)0$/i) {
4871 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4872 } elsif ($ms_size =~ /^(0x|)1$/i) {
4874 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4878 # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4879 if ($^V && $^V ge 5.10.0 &&
4880 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4881 if (WARN
("PREFER_ETHER_ADDR_COPY",
4882 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4884 $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
4888 # typecasts on min/max could be min_t/max_t
4889 if ($^V && $^V ge 5.10.0 &&
4891 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4892 if (defined $2 || defined $7) {
4894 my $cast1 = deparenthesize
($2);
4896 my $cast2 = deparenthesize
($7);
4900 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4901 $cast = "$cast1 or $cast2";
4902 } elsif ($cast1 ne "") {
4908 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4912 # check usleep_range arguments
4913 if ($^V && $^V ge 5.10.0 &&
4915 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4919 WARN
("USLEEP_RANGE",
4920 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4921 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4923 WARN
("USLEEP_RANGE",
4924 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4928 # check for naked sscanf
4929 if ($^V && $^V ge 5.10.0 &&
4931 $line =~ /\bsscanf\b/ &&
4932 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4933 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4934 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4935 my $lc = $stat =~ tr
@\n@@;
4936 $lc = $lc + $linenr;
4937 my $stat_real = raw_line
($linenr, 0);
4938 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4939 $stat_real = $stat_real . "\n" . raw_line
($count, 0);
4941 WARN
("NAKED_SSCANF",
4942 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4945 # check for simple sscanf that should be kstrto<foo>
4946 if ($^V && $^V ge 5.10.0 &&
4948 $line =~ /\bsscanf\b/) {
4949 my $lc = $stat =~ tr
@\n@@;
4950 $lc = $lc + $linenr;
4951 my $stat_real = raw_line
($linenr, 0);
4952 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4953 $stat_real = $stat_real . "\n" . raw_line
($count, 0);
4955 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4957 my $count = $format =~ tr
@%@%@;
4959 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4960 WARN
("SSCANF_TO_KSTRTO",
4961 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4966 # check for new externs in .h files.
4967 if ($realfile =~ /\.h$/ &&
4968 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4969 if (CHK
("AVOID_EXTERNS",
4970 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
4972 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
4976 # check for new externs in .c files.
4977 if ($realfile =~ /\.c$/ && defined $stat &&
4978 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
4980 my $function_name = $1;
4981 my $paren_space = $2;
4984 if (defined $cond) {
4985 substr($s, 0, length($cond), '');
4987 if ($s =~ /^\s*;/ &&
4988 $function_name ne 'uninitialized_var')
4990 WARN
("AVOID_EXTERNS",
4991 "externs should be avoided in .c files\n" . $herecurr);
4994 if ($paren_space =~ /\n/) {
4995 WARN
("FUNCTION_ARGUMENTS",
4996 "arguments for function declarations should follow identifier\n" . $herecurr);
4999 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5000 $stat =~ /^.\s*extern\s+/)
5002 WARN
("AVOID_EXTERNS",
5003 "externs should be avoided in .c files\n" . $herecurr);
5006 # checks for new __setup's
5007 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5010 if (!grep(/$name/, @setup_docs)) {
5011 CHK
("UNDOCUMENTED_SETUP",
5012 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
5016 # check for pointless casting of kmalloc return
5017 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
5018 WARN
("UNNECESSARY_CASTS",
5019 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
5023 # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5024 if ($^V && $^V ge 5.10.0 &&
5025 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5026 CHK
("ALLOC_SIZEOF_STRUCT",
5027 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5030 # check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5031 if ($^V && $^V ge 5.10.0 &&
5032 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
5036 my $newfunc = "kmalloc_array";
5037 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
5040 if ($a1 =~ /^sizeof\s*\S/) {
5044 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5045 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
5046 if (WARN
("ALLOC_WITH_MULTIPLY",
5047 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5049 $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;
5055 # check for krealloc arg reuse
5056 if ($^V && $^V ge 5.10.0 &&
5057 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5058 WARN
("KREALLOC_ARG_REUSE",
5059 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5062 # check for alloc argument mismatch
5063 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5064 WARN
("ALLOC_ARRAY_ARGS",
5065 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5068 # check for multiple semicolons
5069 if ($line =~ /;\s*;\s*$/) {
5070 if (WARN
("ONE_SEMICOLON",
5071 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5073 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
5077 # check for #defines like: 1 << <digit> that could be BIT(digit)
5078 if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
5080 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5081 if (CHK
("BIT_MACRO",
5082 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5084 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5088 # check for case / default statements not preceded by break/fallthrough/switch
5089 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5091 my $has_statement = 0;
5093 my $prevline = $linenr;
5094 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
5096 my $rline = $rawlines[$prevline - 1];
5097 my $fline = $lines[$prevline - 1];
5098 last if ($fline =~ /^\@\@/);
5099 next if ($fline =~ /^\-/);
5100 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5101 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5102 next if ($fline =~ /^.[\s$;]*$/);
5105 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5107 if (!$has_break && $has_statement) {
5108 WARN
("MISSING_BREAK",
5109 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5113 # check for switch/default statements without a break;
5114 if ($^V && $^V ge 5.10.0 &&
5116 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5118 my $herectx = $here . "\n";
5119 my $cnt = statement_rawlines
($stat);
5120 for (my $n = 0; $n < $cnt; $n++) {
5121 $herectx .= raw_line
($linenr, $n) . "\n";
5123 WARN
("DEFAULT_NO_BREAK",
5124 "switch default: should use break\n" . $herectx);
5127 # check for gcc specific __FUNCTION__
5128 if ($line =~ /\b__FUNCTION__\b/) {
5129 if (WARN
("USE_FUNC",
5130 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5132 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
5136 # check for uses of __DATE__, __TIME__, __TIMESTAMP__
5137 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
5139 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
5142 # check for use of yield()
5143 if ($line =~ /\byield\s*\(\s*\)/) {
5145 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
5148 # check for comparisons against true and false
5149 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5157 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5159 my $type = lc($otype);
5160 if ($type =~ /^(?:true|false)$/) {
5161 if (("$test" eq "==" && "$type" eq "true") ||
5162 ("$test" eq "!=" && "$type" eq "false")) {
5166 CHK
("BOOL_COMPARISON",
5167 "Using comparison to $otype is error prone\n" . $herecurr);
5169 ## maybe suggesting a correct construct would better
5170 ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5175 # check for semaphores initialized locked
5176 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
5177 WARN
("CONSIDER_COMPLETION",
5178 "consider using a completion\n" . $herecurr);
5181 # recommend kstrto* over simple_strto* and strict_strto*
5182 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
5183 WARN
("CONSIDER_KSTRTO",
5184 "$1 is obsolete, use k$3 instead\n" . $herecurr);
5187 # check for __initcall(), use device_initcall() explicitly or more appropriate function please
5188 if ($line =~ /^.\s*__initcall\s*\(/) {
5189 WARN
("USE_DEVICE_INITCALL",
5190 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
5193 # check for various ops structs, ensure they are const.
5194 my $struct_ops = qr{acpi_dock_ops
|
5195 address_space_operations
|
5197 block_device_operations
|
5202 file_lock_operations
|
5212 lock_manager_operations
|
5218 pipe_buf_operations
|
5219 platform_hibernation_ops
|
5220 platform_suspend_ops
|
5225 soc_pcmcia_socket_ops
|
5231 if ($line !~ /\bconst\b/ &&
5232 $line =~ /\bstruct\s+($struct_ops)\b/) {
5233 WARN
("CONST_STRUCT",
5234 "struct $1 should normally be const\n" .
5238 # use of NR_CPUS is usually wrong
5239 # ignore definitions of NR_CPUS and usage to define arrays as likely right
5240 if ($line =~ /\bNR_CPUS\b/ &&
5241 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5242 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
5243 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5244 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5245 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
5248 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
5251 # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
5252 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
5253 ERROR
("DEFINE_ARCH_HAS",
5254 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
5257 # likely/unlikely comparisons similar to "(likely(foo) > 0)"
5258 if ($^V && $^V ge 5.10.0 &&
5259 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5260 WARN
("LIKELY_MISUSE",
5261 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5264 # whine mightly about in_atomic
5265 if ($line =~ /\bin_atomic\s*\(/) {
5266 if ($realfile =~ m
@^drivers/@) {
5268 "do not use in_atomic in drivers\n" . $herecurr);
5269 } elsif ($realfile !~ m
@^kernel/@) {
5271 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5275 # check for lockdep_set_novalidate_class
5276 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5277 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5278 if ($realfile !~ m
@^kernel/lockdep@ &&
5279 $realfile !~ m
@^include/linux/lockdep@ &&
5280 $realfile !~ m
@^drivers/base/core@) {
5282 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
5286 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
5287 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
5288 WARN
("EXPORTED_WORLD_WRITABLE",
5289 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
5292 # Mode permission misuses where it seems decimal should be octal
5293 # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5294 if ($^V && $^V ge 5.10.0 &&
5295 $line =~ /$mode_perms_search/) {
5296 foreach my $entry (@mode_permission_funcs) {
5297 my $func = $entry->[0];
5298 my $arg_pos = $entry->[1];
5303 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5305 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5306 if ($line =~ /$test/) {
5308 $val = $6 if ($skip_args ne "");
5310 if ($val !~ /^0$/ &&
5311 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5312 length($val) ne 4)) {
5313 ERROR
("NON_OCTAL_PERMISSIONS",
5314 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5315 } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5316 ERROR
("EXPORTED_WORLD_WRITABLE",
5317 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
5324 # If we have no input at all, then there is nothing to report on
5325 # so just keep quiet.
5326 if ($#rawlines == -1) {
5330 # In mailback mode only produce a report in the negative, for
5331 # things that appear to be patches.
5332 if ($mailback && ($clean == 1 || !$is_patch)) {
5336 # This is not a patch, and we are are in 'no-patch' mode so
5338 if (!$chk_patch && !$is_patch) {
5343 ERROR
("NOT_UNIFIED_DIFF",
5344 "Does not appear to be a unified-diff format patch\n");
5346 if ($is_patch && $chk_signoff && $signoff == 0) {
5347 ERROR
("MISSING_SIGN_OFF",
5348 "Missing Signed-off-by: line(s)\n");
5351 print report_dump
();
5352 if ($summary && !($clean == 1 && $quiet == 1)) {
5353 print "$filename " if ($summary_file);
5354 print "total: $cnt_error errors, $cnt_warn warnings, " .
5355 (($check)?
"$cnt_chk checks, " : "") .
5356 "$cnt_lines lines checked\n";
5357 print "\n" if ($quiet == 0);
5362 if ($^V lt 5.10.0) {
5363 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5364 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5367 # If there were whitespace errors which cleanpatch can fix
5368 # then suggest that.
5369 if ($rpt_cleaners) {
5370 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5371 print " scripts/cleanfile\n\n";
5376 hash_show_words
(\
%use_type, "Used");
5377 hash_show_words
(\
%ignore_type, "Ignored");
5379 if ($clean == 0 && $fix &&
5380 ("@rawlines" ne "@fixed" ||
5381 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
5382 my $newfile = $filename;
5383 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
5387 @fixed = fix_inserted_deleted_lines
(\
@fixed, \
@fixed_inserted, \
@fixed_deleted);
5389 open($f, '>', $newfile)
5390 or die "$P: Can't open $newfile for write\n";
5391 foreach my $fixed_line (@fixed) {
5394 if ($linecount > 3) {
5395 $fixed_line =~ s/^\+//;
5396 print $f $fixed_line . "\n";
5399 print $f $fixed_line . "\n";
5406 Wrote EXPERIMENTAL
--fix correction
(s
) to
'$newfile'
5408 Do _NOT_ trust the results written to this file
.
5409 Do _NOT_ submit these changes without inspecting them
for correctness
.
5411 This EXPERIMENTAL file
is simply a convenience to help rewrite patches
.
5412 No warranties
, expressed
or implied
...
5418 if ($clean == 1 && $quiet == 0) {
5419 print "$vname has no obvious style problems and is ready for submission.\n"
5421 if ($clean == 0 && $quiet == 0) {
5423 $vname has style problems
, please review
.
5425 If any of these errors are false positives
, please report
5426 them to the maintainer
, see CHECKPATCH
in MAINTAINERS
.