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
13 use Term
::ANSIColor
qw(:constants);
16 my $D = dirname
(abs_path
($P));
20 use Getopt
::Long
qw(:config no_auto_abbrev);
50 my $configuration_file = ".checkpatch.conf";
51 my $max_line_length = 80;
52 my $ignore_perl_version = 0;
53 my $minimum_perl_version = 5.10.0;
54 my $min_conf_desc_length = 4;
55 my $spelling_file = "$D/spelling.txt";
57 my $codespellfile = "/usr/share/codespell/dictionary.txt";
58 my $typedefsfile = "";
60 my $allow_c99_comments = 1;
66 Usage
: $P [OPTION
]... [FILE
]...
71 --no-tree run without a kernel tree
72 --no-signoff
do not check
for 'Signed-off-by' line
73 --patch treat FILE as patchfile
(default)
74 --emacs emacs compile window format
75 --terse one line per report
76 --showfile emit diffed file position
, not input file position
77 -g
, --git treat FILE as a single commit
or git revision range
78 single git commit with
:
82 multiple git commits with
:
86 git merges are ignored
87 -f
, --file treat FILE as regular source file
88 --subjective
, --strict enable more subjective tests
89 --list-types list the possible message types
90 --types TYPE
(,TYPE2
...) show only these comma separated message types
91 --ignore TYPE
(,TYPE2
...) ignore various comma separated message types
92 --show-types show the specific message type
in the output
93 --max-line-length
=n set the maximum line
length, if exceeded
, warn
94 --min-conf-desc-length
=n set the min description
length, if shorter
, warn
95 --root
=PATH PATH to the kernel tree root
96 --no-summary suppress the per-file summary
97 --mailback only produce a report
in case of warnings
/errors
98 --summary-file include the filename
in summary
99 --debug KEY
=[0|1] turn on
/off debugging of KEY
, where KEY
is one of
100 'values', 'possible', 'type', and 'attr' (default
102 --test-only
=WORD report only warnings
/errors containing WORD
104 --fix EXPERIMENTAL
- may create horrible results
105 If correctable single-line errors exist
, create
106 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
107 with potential errors corrected to the preferred
109 --fix-inplace EXPERIMENTAL
- may create horrible results
110 Is the same as
--fix
, but overwrites the input
111 file
. It
's your fault if there's
no backup
or git
112 --ignore-perl-version override checking of perl version
. expect
114 --codespell Use the codespell dictionary
for spelling
/typos
115 (default:/usr/share
/codespell/dictionary
.txt
)
116 --codespellfile Use this codespell dictionary
117 --typedefsfile Read additional types from this file
118 --color
[=WHEN
] Use colors
'always', 'never', or only
when output
119 is a terminal
('auto'). Default
is 'auto'.
120 -h
, --help
, --version display this help
and exit
122 When FILE
is - read standard input
.
130 return grep { !$seen{$_}++ } @_;
140 open(my $script, '<', abs_path
($P)) or
141 die "$P: Can't read '$P' $!\n";
143 my $text = <$script>;
147 # Also catch when type or level is passed through a variable
148 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
151 @types = sort(uniq
(@types));
152 print("#\tMessage type\n\n");
153 foreach my $type (@types) {
154 print(++$count . "\t" . $type . "\n");
160 my $conf = which_conf
($configuration_file);
163 open(my $conffile, '<', "$conf")
164 or warn "$P: Can't find a readable $configuration_file file $!\n";
166 while (<$conffile>) {
169 $line =~ s/\s*\n?$//g;
173 next if ($line =~ m/^\s*#/);
174 next if ($line =~ m/^\s*$/);
176 my @words = split(" ", $line);
177 foreach my $word (@words) {
178 last if ($word =~ m/^#/);
179 push (@conf_args, $word);
183 unshift(@ARGV, @conf_args) if @conf_args;
186 # Perl's Getopt::Long allows options to take optional arguments after a space.
187 # Prevent --color by itself from consuming other arguments
189 if ($_ eq "--color" || $_ eq "-color") {
190 $_ = "--color=$color";
195 'q|quiet+' => \
$quiet,
197 'signoff!' => \
$chk_signoff,
198 'patch!' => \
$chk_patch,
201 'showfile!' => \
$showfile,
204 'subjective!' => \
$check,
205 'strict!' => \
$check,
206 'ignore=s' => \
@ignore,
208 'show-types!' => \
$show_types,
209 'list-types!' => \
$list_types,
210 'max-line-length=i' => \
$max_line_length,
211 'min-conf-desc-length=i' => \
$min_conf_desc_length,
213 'summary!' => \
$summary,
214 'mailback!' => \
$mailback,
215 'summary-file!' => \
$summary_file,
217 'fix-inplace!' => \
$fix_inplace,
218 'ignore-perl-version!' => \
$ignore_perl_version,
219 'debug=s' => \
%debug,
220 'test-only=s' => \
$tst_only,
221 'codespell!' => \
$codespell,
222 'codespellfile=s' => \
$codespellfile,
223 'typedefsfile=s' => \
$typedefsfile,
224 'color=s' => \
$color,
225 'no-color' => \
$color, #keep old behaviors of -nocolor
226 'nocolor' => \
$color, #keep old behaviors of -nocolor
233 list_types
(0) if ($list_types);
235 $fix = 1 if ($fix_inplace);
236 $check_orig = $check;
240 if ($^V && $^V lt $minimum_perl_version) {
241 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
242 if (!$ignore_perl_version) {
247 #if no filenames are given, push '-' to read patch from stdin
252 if ($color =~ /^[01]$/) {
254 } elsif ($color =~ /^always$/i) {
256 } elsif ($color =~ /^never$/i) {
258 } elsif ($color =~ /^auto$/i) {
259 $color = (-t STDOUT
);
261 die "Invalid color mode: $color\n";
264 sub hash_save_array_words
{
265 my ($hashRef, $arrayRef) = @_;
267 my @array = split(/,/, join(',', @$arrayRef));
268 foreach my $word (@array) {
269 $word =~ s/\s*\n?$//g;
272 $word =~ tr/[a-z]/[A-Z]/;
274 next if ($word =~ m/^\s*#/);
275 next if ($word =~ m/^\s*$/);
281 sub hash_show_words
{
282 my ($hashRef, $prefix) = @_;
284 if (keys %$hashRef) {
285 print "\nNOTE: $prefix message types:";
286 foreach my $word (sort keys %$hashRef) {
293 hash_save_array_words
(\
%ignore_type, \
@ignore);
294 hash_save_array_words
(\
%use_type, \
@use);
297 my $dbg_possible = 0;
300 for my $key (keys %debug) {
302 eval "\${dbg_$key} = '$debug{$key}';";
306 my $rpt_cleaners = 0;
315 if (!top_of_kernel_tree
($root)) {
316 die "$P: $root: --root does not point at a valid tree\n";
319 if (top_of_kernel_tree
('.')) {
321 } elsif ($0 =~ m
@(.*)/scripts/[^/]*$@ &&
322 top_of_kernel_tree
($1)) {
327 if (!defined $root) {
328 print "Must be run from the top-level dir. of a kernel tree\n";
333 my $emitted_corrupt = 0;
336 [A-Za-z_
][A-Za-z\d_
]*
337 (?
:\s
*\#\#\s
*[A-Za-z_
][A-Za-z\d_
]*)*
339 our $Storage = qr{extern|static|asmlinkage};
352 our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
353 our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
354 our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
355 our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
356 our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
358 # Notes to $Attribute:
359 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
380 ____cacheline_aligned
|
381 ____cacheline_aligned_in_smp
|
382 ____cacheline_internodealigned_in_smp
|
386 our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
387 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
388 our $Lval = qr{$Ident(?:$Member)*};
390 our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
391 our $Binary = qr{(?i)0b[01]+$Int_type?};
392 our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
393 our $Int = qr{[0-9]+$Int_type?};
394 our $Octal = qr{0[0-7]+$Int_type?};
395 our $String = qr{"[X\t]*"};
396 our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
397 our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
398 our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
399 our $Float = qr{$Float_hex|$Float_dec|$Float_int};
400 our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
401 our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
402 our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
403 our $Arithmetic = qr{\+|-|\*|\/|%};
407 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||$Arithmetic
410 our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
414 our $NonptrTypeMisordered;
415 our $NonptrTypeWithAttr;
419 our $DeclareMisordered;
421 our $NON_ASCII_UTF8 = qr{
422 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
423 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
424 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
425 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
426 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
427 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
428 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
432 [\x09\x0A\x0D\x20-\x7E] # ASCII
436 our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
437 our $typeOtherOSTypedefs = qr{(?x
:
438 u_
(?
:char
|short
|int|long
) | # bsd
439 u
(?
:nchar
|short
|int|long
) # sysv
441 our $typeKernelTypedefs = qr{(?x
:
442 (?
:__
)?
(?
:u
|s
|be
|le)(?
:8|16|32|64)|
445 our $typeTypedefs = qr{(?x
:
447 $typeOtherOSTypedefs\b|
448 $typeKernelTypedefs\b
451 our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
453 our $logFunctions = qr{(?x
:
454 printk
(?
:_ratelimited
|_once
|_deferred_once
|_deferred
|)|
455 (?
:[a-z0-9
]+_
){1,2}(?
:printk
|emerg
|alert
|crit
|err
|warning
|warn|notice
|info
|debug
|dbg
|vdbg
|devel
|cont
|WARN
)(?
:_ratelimited
|_once
|)|
457 WARN
(?
:_RATELIMIT
|_ONCE
|)|
460 seq_vprintf
|seq_printf
|seq_puts
463 our $signature_tags = qr{(?xi
:
474 our @typeListMisordered = (
475 qr{char\s+(?:un)?signed},
476 qr{int\s+(?:(?:un)?signed\s+)?short\s},
477 qr{int\s+short(?:\s+(?:un)?signed)},
478 qr{short\s+int(?:\s+(?:un)?signed)},
479 qr{(?:un)?signed\s+int\s+short},
480 qr{short\s+(?:un)?signed},
481 qr{long\s+int\s+(?:un)?signed},
482 qr{int\s+long\s+(?:un)?signed},
483 qr{long\s+(?:un)?signed\s+int},
484 qr{int\s+(?:un)?signed\s+long},
485 qr{int\s+(?:un)?signed},
486 qr{int\s+long\s+long\s+(?:un)?signed},
487 qr{long\s+long\s+int\s+(?:un)?signed},
488 qr{long\s+long\s+(?:un)?signed\s+int},
489 qr{long\s+long\s+(?:un)?signed},
490 qr{long\s+(?:un)?signed},
495 qr{(?:(?:un)?signed\s+)?char},
496 qr{(?:(?:un)?signed\s+)?short\s+int},
497 qr{(?:(?:un)?signed\s+)?short},
498 qr{(?:(?:un)?signed\s+)?int},
499 qr{(?:(?:un)?signed\s+)?long\s+int},
500 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
501 qr{(?:(?:un)?signed\s+)?long\s+long},
502 qr{(?:(?:un)?signed\s+)?long},
511 qr{${Ident}_handler
},
512 qr{${Ident}_handler_fn
},
516 our $C90_int_types = qr{(?x
:
517 long\s
+long\s
+int\s
+(?
:un
)?signed
|
518 long\s
+long\s
+(?
:un
)?signed\s
+int|
519 long\s
+long\s
+(?
:un
)?signed
|
520 (?
:(?
:un
)?signed\s
+)?long\s
+long\s
+int|
521 (?
:(?
:un
)?signed\s
+)?long\s
+long
|
522 int\s
+long\s
+long\s
+(?
:un
)?signed
|
523 int\s
+(?
:(?
:un
)?signed\s
+)?long\s
+long
|
525 long\s
+int\s
+(?
:un
)?signed
|
526 long\s
+(?
:un
)?signed\s
+int|
527 long\s
+(?
:un
)?signed
|
528 (?
:(?
:un
)?signed\s
+)?long\s
+int|
529 (?
:(?
:un
)?signed\s
+)?long
|
530 int\s
+long\s
+(?
:un
)?signed
|
531 int\s
+(?
:(?
:un
)?signed\s
+)?long
|
534 (?
:(?
:un
)?signed\s
+)?
int
537 our @typeListFile = ();
538 our @typeListWithAttr = (
540 qr{struct\s+$InitAttribute\s+$Ident},
541 qr{union\s+$InitAttribute\s+$Ident},
544 our @modifierList = (
547 our @modifierListFile = ();
549 our @mode_permission_funcs = (
551 ["module_param_(?:array|named|string)", 4],
552 ["module_param_array_named", 5],
553 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
554 ["proc_create(?:_data|)", 2],
555 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
556 ["IIO_DEV_ATTR_[A-Z_]+", 1],
557 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
558 ["SENSOR_TEMPLATE(?:_2|)", 3],
562 #Create a search pattern for all these functions to speed up a loop below
563 our $mode_perms_search = "";
564 foreach my $entry (@mode_permission_funcs) {
565 $mode_perms_search .= '|' if ($mode_perms_search ne "");
566 $mode_perms_search .= $entry->[0];
569 our $mode_perms_world_writable = qr{
577 our %mode_permission_string_types = (
596 #Create a search pattern for all these strings to speed up a loop below
597 our $mode_perms_string_search = "";
598 foreach my $entry (keys %mode_permission_string_types) {
599 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
600 $mode_perms_string_search .= $entry;
603 our $allowed_asm_includes = qr{(?x
:
609 # memory.h: ARM has a custom one
611 # Load common spelling mistakes and build regular expression list.
615 if (open(my $spelling, '<', $spelling_file)) {
616 while (<$spelling>) {
619 $line =~ s/\s*\n?$//g;
622 next if ($line =~ m/^\s*#/);
623 next if ($line =~ m/^\s*$/);
625 my ($suspect, $fix) = split(/\|\|/, $line);
627 $spelling_fix{$suspect} = $fix;
631 warn "No typos will be found - file '$spelling_file': $!\n";
635 if (open(my $spelling, '<', $codespellfile)) {
636 while (<$spelling>) {
639 $line =~ s/\s*\n?$//g;
642 next if ($line =~ m/^\s*#/);
643 next if ($line =~ m/^\s*$/);
644 next if ($line =~ m/, disabled/i);
648 my ($suspect, $fix) = split(/->/, $line);
650 $spelling_fix{$suspect} = $fix;
654 warn "No codespell typos will be found - file '$codespellfile': $!\n";
658 $misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
661 my ($wordsRef, $file) = @_;
663 if (open(my $words, '<', $file)) {
667 $line =~ s/\s*\n?$//g;
670 next if ($line =~ m/^\s*#/);
671 next if ($line =~ m/^\s*$/);
673 print("$file: '$line' invalid - ignored\n");
677 $$wordsRef .= '|' if ($$wordsRef ne "");
687 my $typeOtherTypedefs = "";
688 if (length($typedefsfile)) {
689 read_words
(\
$typeOtherTypedefs, $typedefsfile)
690 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
692 $typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
695 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
696 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
697 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
698 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
699 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
705 (?
:$Modifier\s+|const\s
+)*
707 (?
:typeof
|__typeof__
)\s
*\
([^\
)]*\
)|
711 (?
:\s
+$Modifier|\s
+const
)*
713 $NonptrTypeMisordered = qr{
714 (?
:$Modifier\s+|const\s
+)*
718 (?
:\s
+$Modifier|\s
+const
)*
720 $NonptrTypeWithAttr = qr{
721 (?
:$Modifier\s+|const\s
+)*
723 (?
:typeof
|__typeof__
)\s
*\
([^\
)]*\
)|
727 (?
:\s
+$Modifier|\s
+const
)*
731 (?
:(?
:\s
|\
*|\
[\
])+\s
*const
|(?
:\s
|\
*\s
*(?
:const\s
*)?
|\
[\
])+|(?
:\s
*\
[\s
*\
])+)?
732 (?
:\s
+$Inline|\s
+$Modifier)*
734 $TypeMisordered = qr{
735 $NonptrTypeMisordered
736 (?
:(?
:\s
|\
*|\
[\
])+\s
*const
|(?
:\s
|\
*\s
*(?
:const\s
*)?
|\
[\
])+|(?
:\s
*\
[\s
*\
])+)?
737 (?
:\s
+$Inline|\s
+$Modifier)*
739 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
740 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
744 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s
*};
746 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
747 # requires at least perl version v5.10.0
748 # Any use must be runtime checked with $^V
750 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
751 our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s
*};
752 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
754 our $declaration_macros = qr{(?x
:
755 (?
:$Storage\s+)?
(?
:[A-Z_
][A-Z0-9
]*_
){0,2}(?
:DEFINE
|DECLARE
)(?
:_
[A-Z0-9
]+){1,6}\s
*\
(|
756 (?
:$Storage\s+)?
[HLP
]?LIST_HEAD\s
*\
(|
757 (?
:$Storage\s+)?
${Type
}\s
+uninitialized_var\s
*\
(
762 return "" if (!defined($string));
764 while ($string =~ /^\s*\(.*\)\s*$/) {
765 $string =~ s
@^\s*\(\s
*@@;
766 $string =~ s
@\s*\)\s
*$@@;
769 $string =~ s
@\s+@ @g;
774 sub seed_camelcase_file
{
777 return if (!(-f
$file));
781 open(my $include_file, '<', "$file")
782 or warn "$P: Can't read '$file' $!\n";
783 my $text = <$include_file>;
784 close($include_file);
786 my @lines = split('\n', $text);
788 foreach my $line (@lines) {
789 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
790 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
792 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
794 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
800 sub is_maintained_obsolete
{
803 return 0 if (!$tree || !(-e
"$root/scripts/get_maintainer.pl"));
805 my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
807 return $status =~ /obsolete/i;
810 my $camelcase_seeded = 0;
811 sub seed_camelcase_includes
{
812 return if ($camelcase_seeded);
815 my $camelcase_cache = "";
816 my @include_files = ();
818 $camelcase_seeded = 1;
821 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
822 chomp $git_last_include_commit;
823 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
825 my $last_mod_date = 0;
826 $files = `find $root/include -name "*.h"`;
827 @include_files = split('\n', $files);
828 foreach my $file (@include_files) {
829 my $date = POSIX
::strftime
("%Y%m%d%H%M",
830 localtime((stat $file)[9]));
831 $last_mod_date = $date if ($last_mod_date < $date);
833 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
836 if ($camelcase_cache ne "" && -f
$camelcase_cache) {
837 open(my $camelcase_file, '<', "$camelcase_cache")
838 or warn "$P: Can't read '$camelcase_cache' $!\n";
839 while (<$camelcase_file>) {
843 close($camelcase_file);
849 $files = `git ls-files "include/*.h"`;
850 @include_files = split('\n', $files);
853 foreach my $file (@include_files) {
854 seed_camelcase_file
($file);
857 if ($camelcase_cache ne "") {
858 unlink glob ".checkpatch-camelcase.*";
859 open(my $camelcase_file, '>', "$camelcase_cache")
860 or warn "$P: Can't write '$camelcase_cache' $!\n";
861 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
862 print $camelcase_file ("$_\n");
864 close($camelcase_file);
868 sub git_commit_info
{
869 my ($commit, $id, $desc) = @_;
871 return ($id, $desc) if ((which
("git") eq "") || !(-e
".git"));
873 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
874 $output =~ s/^\s*//gm;
875 my @lines = split("\n", $output);
877 return ($id, $desc) if ($#lines < 0);
879 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
880 # Maybe one day convert this block of bash into something that returns
881 # all matching commit ids, but it's very slow...
883 # echo "checking commits $1..."
884 # git rev-list --remotes | grep -i "^$1" |
885 # while read line ; do
886 # git log --format='%H %s' -1 $line |
887 # echo "commit $(cut -c 1-12,41-)"
889 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
892 $id = substr($lines[0], 0, 12);
893 $desc = substr($lines[0], 41);
899 $chk_signoff = 0 if ($file);
904 my @fixed_inserted = ();
905 my @fixed_deleted = ();
908 # If input is git commits, extract all commits from the commit expressions.
909 # For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
910 die "$P: No git repository found\n" if ($git && !-e
".git");
914 foreach my $commit_expr (@ARGV) {
916 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
917 $git_range = "-$2 $1";
918 } elsif ($commit_expr =~ m/\.\./) {
919 $git_range = "$commit_expr";
921 $git_range = "-1 $commit_expr";
923 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
924 foreach my $line (split(/\n/, $lines)) {
925 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
926 next if (!defined($1) || !defined($2));
929 unshift(@commits, $sha1);
930 $git_commits{$sha1} = $subject;
933 die "$P: no git commits after extraction!\n" if (@commits == 0);
938 for my $filename (@ARGV) {
941 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
942 die "$P: $filename: git format-patch failed - $!\n";
944 open($FILE, '-|', "diff -u /dev/null $filename") ||
945 die "$P: $filename: diff failed - $!\n";
946 } elsif ($filename eq '-') {
947 open($FILE, '<&STDIN');
949 open($FILE, '<', "$filename") ||
950 die "$P: $filename: open failed - $!\n";
952 if ($filename eq '-') {
953 $vname = 'Your patch';
955 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
965 if ($#ARGV > 0 && $quiet == 0) {
966 print '-' x length($vname) . "\n";
968 print '-' x length($vname) . "\n";
971 if (!process($filename)) {
977 @fixed_inserted = ();
980 @modifierListFile = ();
986 hash_show_words(\%use_type, "Used");
987 hash_show_words(\%ignore_type, "Ignored");
992 NOTE: perl $^V is not modern enough to detect all possible issues.
993 An upgrade to at least perl v5.10.0 is suggested.
999 NOTE: If any of the errors are false positives, please report
1000 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1007 sub top_of_kernel_tree {
1011 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1012 "README", "Documentation", "arch", "include", "drivers",
1013 "fs", "init", "ipc", "kernel", "lib", "scripts",
1016 foreach my $check (@tree_check) {
1017 if (! -e $root . '/' . $check) {
1025 my ($formatted_email) = @_;
1031 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1034 $comment = $3 if defined $3;
1035 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1037 $comment = $2 if defined $2;
1038 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1040 $comment = $2 if defined $2;
1041 $formatted_email =~ s/$address.*$//;
1042 $name = $formatted_email;
1043 $name = trim($name);
1044 $name =~ s/^\"|\"$//g;
1045 # If there's a name left after stripping spaces and
1046 # leading quotes, and the address doesn't have both
1047 # leading and trailing angle brackets, the address
1049 # "joe smith joe@smith.com" bad
1050 # "joe smith <joe@smith.com" bad
1051 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1058 $name = trim($name);
1059 $name =~ s/^\"|\"$//g;
1060 $address = trim($address);
1061 $address =~ s/^\<|\>$//g;
1063 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1064 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1065 $name = "\"$name\"";
1068 return ($name, $address, $comment);
1072 my ($name, $address) = @_;
1074 my $formatted_email;
1076 $name = trim($name);
1077 $name =~ s/^\"|\"$//g;
1078 $address = trim($address);
1080 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1081 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1082 $name = "\"$name\"";
1085 if ("$name" eq "") {
1086 $formatted_email = "$address";
1088 $formatted_email = "$name <$address>";
1091 return $formatted_email;
1097 foreach my $path (split(/:/, $ENV{PATH})) {
1098 if (-e "$path/$bin") {
1099 return "$path/$bin";
1109 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1110 if (-e "$path/$conf") {
1111 return "$path/$conf";
1123 for my $c (split(//, $str)) {
1127 for (; ($n % 8) != 0; $n++) {
1139 (my $res = shift) =~ tr/\t/ /c;
1146 # Drop the diff line leader and expand tabs
1148 $line = expand_tabs($line);
1150 # Pick the indent from the front of the line.
1151 my ($white) = ($line =~ /^(\s*)/);
1153 return (length($line), length($white));
1156 my $sanitise_quote = '';
1158 sub sanitise_line_reset {
1159 my ($in_comment) = @_;
1162 $sanitise_quote = '*/';
1164 $sanitise_quote = '';
1177 # Always copy over the diff marker.
1178 $res = substr($line, 0, 1);
1180 for ($off = 1; $off < length($line); $off++) {
1181 $c = substr($line, $off, 1);
1183 # Comments we are wacking completly including the begin
1184 # and end, all to $;.
1185 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1186 $sanitise_quote = '*/';
1188 substr($res, $off, 2, "$;$;");
1192 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1193 $sanitise_quote = '';
1194 substr($res, $off, 2, "$;$;");
1198 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1199 $sanitise_quote = '//';
1201 substr($res, $off, 2, $sanitise_quote);
1206 # A \ in a string means ignore the next character.
1207 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1209 substr($res, $off, 2, 'XX');
1214 if ($c eq "'" || $c eq '"') {
1215 if ($sanitise_quote eq '') {
1216 $sanitise_quote = $c;
1218 substr($res, $off, 1, $c);
1220 } elsif ($sanitise_quote eq $c) {
1221 $sanitise_quote = '';
1225 #print "c<$c> SQ<$sanitise_quote>\n";
1226 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1227 substr($res, $off, 1, $;);
1228 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1229 substr($res, $off, 1, $;);
1230 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1231 substr($res, $off, 1, 'X');
1233 substr($res, $off, 1, $c);
1237 if ($sanitise_quote eq '//') {
1238 $sanitise_quote = '';
1241 # The pathname on a #include may be surrounded by '<' and '>'.
1242 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1243 my $clean = 'X' x length($1);
1244 $res =~ s@\<.*\>@<$clean>@;
1246 # The whole of a #error is a string.
1247 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1248 my $clean = 'X' x length($1);
1249 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1255 sub get_quoted_string {
1256 my ($line, $rawline) = @_;
1258 return "" if ($line !~ m/($String)/g);
1259 return substr($rawline, $-[0], $+[0] - $-[0]);
1262 sub ctx_statement_block {
1263 my ($linenr, $remain, $off) = @_;
1264 my $line = $linenr - 1;
1267 my $coff = $off - 1;
1281 @stack = (['', 0]) if ($#stack == -1);
1283 #warn "CSB: blk<$blk> remain<$remain>\n";
1284 # If we are about to drop off the end, pull in more
1287 for (; $remain > 0; $line++) {
1288 last if (!defined $lines[$line]);
1289 next if ($lines[$line] =~ /^-/);
1292 $blk .= $lines[$line] . "\n";
1293 $len = length($blk);
1297 # Bail if there is no further context.
1298 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
1302 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1308 $c = substr($blk, $off, 1);
1309 $remainder = substr($blk, $off);
1311 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1313 # Handle nested #if/#else.
1314 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1315 push(@stack, [ $type, $level ]);
1316 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1317 ($type, $level) = @{$stack[$#stack - 1]};
1318 } elsif ($remainder =~ /^#\s*endif\b/) {
1319 ($type, $level) = @{pop(@stack)};
1322 # Statement ends at the ';' or a close '}' at the
1324 if ($level == 0 && $c eq ';') {
1328 # An else is really a conditional as long as its not else if
1329 if ($level == 0 && $coff_set == 0 &&
1330 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1331 $remainder =~ /^(else)(?:\s|{)/ &&
1332 $remainder !~ /^else\s+if\b/) {
1333 $coff = $off + length($1) - 1;
1335 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1336 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1339 if (($type eq '' || $type eq '(') && $c eq '(') {
1343 if ($type eq '(' && $c eq ')') {
1345 $type = ($level != 0)? '(' : '';
1347 if ($level == 0 && $coff < $soff) {
1350 #warn "CSB: mark coff<$coff>\n";
1353 if (($type eq '' || $type eq '{') && $c eq '{') {
1357 if ($type eq '{' && $c eq '}') {
1359 $type = ($level != 0)? '{' : '';
1362 if (substr($blk, $off + 1, 1) eq ';') {
1368 # Preprocessor commands end at the newline unless escaped.
1369 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1377 # We are truly at the end, so shuffle to the next line.
1384 my $statement = substr($blk, $soff, $off - $soff + 1);
1385 my $condition = substr($blk, $soff, $coff - $soff + 1);
1387 #warn "STATEMENT<$statement>\n";
1388 #warn "CONDITION<$condition>\n";
1390 #print "coff<$coff> soff<$off> loff<$loff>\n";
1392 return ($statement, $condition,
1393 $line, $remain + 1, $off - $loff + 1, $level);
1396 sub statement_lines {
1399 # Strip the diff line prefixes and rip blank lines at start and end.
1400 $stmt =~ s/(^|\n)./$1/g;
1404 my @stmt_lines = ($stmt =~ /\n/g);
1406 return $#stmt_lines + 2;
1409 sub statement_rawlines {
1412 my @stmt_lines = ($stmt =~ /\n/g);
1414 return $#stmt_lines + 2;
1417 sub statement_block_size {
1420 $stmt =~ s/(^|\n)./$1/g;
1426 my @stmt_lines = ($stmt =~ /\n/g);
1427 my @stmt_statements = ($stmt =~ /;/g);
1429 my $stmt_lines = $#stmt_lines + 2;
1430 my $stmt_statements = $#stmt_statements + 1;
1432 if ($stmt_lines > $stmt_statements) {
1435 return $stmt_statements;
1439 sub ctx_statement_full {
1440 my ($linenr, $remain, $off) = @_;
1441 my ($statement, $condition, $level);
1445 # Grab the first conditional/block pair.
1446 ($statement, $condition, $linenr, $remain, $off, $level) =
1447 ctx_statement_block($linenr, $remain, $off);
1448 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1449 push(@chunks, [ $condition, $statement ]);
1450 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1451 return ($level, $linenr, @chunks);
1454 # Pull in the following conditional/block pairs and see if they
1455 # could continue the statement.
1457 ($statement, $condition, $linenr, $remain, $off, $level) =
1458 ctx_statement_block($linenr, $remain, $off);
1459 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1460 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1462 push(@chunks, [ $condition, $statement ]);
1465 return ($level, $linenr, @chunks);
1469 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1471 my $start = $linenr - 1;
1478 my @stack = ($level);
1479 for ($line = $start; $remain > 0; $line++) {
1480 next if ($rawlines[$line] =~ /^-/);
1483 $blk .= $rawlines[$line];
1485 # Handle nested #if/#else.
1486 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1487 push(@stack, $level);
1488 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1489 $level = $stack[$#stack - 1];
1490 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1491 $level = pop(@stack);
1494 foreach my $c (split(//, $lines[$line])) {
1495 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1501 if ($c eq $close && $level > 0) {
1503 last if ($level == 0);
1504 } elsif ($c eq $open) {
1509 if (!$outer || $level <= 1) {
1510 push(@res, $rawlines[$line]);
1513 last if ($level == 0);
1516 return ($level, @res);
1518 sub ctx_block_outer {
1519 my ($linenr, $remain) = @_;
1521 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1525 my ($linenr, $remain) = @_;
1527 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1531 my ($linenr, $remain, $off) = @_;
1533 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1536 sub ctx_block_level {
1537 my ($linenr, $remain) = @_;
1539 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1541 sub ctx_statement_level {
1542 my ($linenr, $remain, $off) = @_;
1544 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1547 sub ctx_locate_comment {
1548 my ($first_line, $end_line) = @_;
1550 # Catch a comment on the end of the line itself.
1551 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1552 return $current_comment if (defined $current_comment);
1554 # Look through the context and try and figure out if there is a
1557 $current_comment = '';
1558 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1559 my $line = $rawlines[$linenr - 1];
1561 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1564 if ($line =~ m@/\*@) {
1567 if (!$in_comment && $current_comment ne '') {
1568 $current_comment = '';
1570 $current_comment .= $line . "\n" if ($in_comment);
1571 if ($line =~ m@\*/@) {
1576 chomp($current_comment);
1577 return($current_comment);
1579 sub ctx_has_comment {
1580 my ($first_line, $end_line) = @_;
1581 my $cmt = ctx_locate_comment($first_line, $end_line);
1583 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1584 ##print "CMMT: $cmt\n";
1586 return ($cmt ne '');
1590 my ($linenr, $cnt) = @_;
1592 my $offset = $linenr - 1;
1597 $line = $rawlines[$offset++];
1598 next if (defined($line) && $line =~ /^-/);
1610 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1613 $coded = sprintf("^%c", unpack('C', $2) + 64);
1622 my $av_preprocessor = 0;
1627 sub annotate_reset {
1628 $av_preprocessor = 0;
1630 @av_paren_type = ('E');
1631 $av_pend_colon = 'O';
1634 sub annotate_values {
1635 my ($stream, $type) = @_;
1638 my $var = '_' x length($stream);
1641 print "$stream\n" if ($dbg_values > 1);
1643 while (length($cur)) {
1644 @av_paren_type = ('E') if ($#av_paren_type < 0);
1645 print " <" . join('', @av_paren_type) .
1646 "> <$type> <$av_pending>" if ($dbg_values > 1);
1647 if ($cur =~ /^(\s+)/o) {
1648 print "WS($1)\n" if ($dbg_values > 1);
1649 if ($1 =~ /\n/ && $av_preprocessor) {
1650 $type = pop(@av_paren_type);
1651 $av_preprocessor = 0;
1654 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1655 print "CAST($1)\n" if ($dbg_values > 1);
1656 push(@av_paren_type, $type);
1659 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1660 print "DECLARE($1)\n" if ($dbg_values > 1);
1663 } elsif ($cur =~ /^($Modifier)\s*/) {
1664 print "MODIFIER($1)\n" if ($dbg_values > 1);
1667 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1668 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1669 $av_preprocessor = 1;
1670 push(@av_paren_type, $type);
1676 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1677 print "UNDEF($1)\n" if ($dbg_values > 1);
1678 $av_preprocessor = 1;
1679 push(@av_paren_type, $type);
1681 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1682 print "PRE_START($1)\n" if ($dbg_values > 1);
1683 $av_preprocessor = 1;
1685 push(@av_paren_type, $type);
1686 push(@av_paren_type, $type);
1689 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1690 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1691 $av_preprocessor = 1;
1693 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1697 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1698 print "PRE_END($1)\n" if ($dbg_values > 1);
1700 $av_preprocessor = 1;
1702 # Assume all arms of the conditional end as this
1703 # one does, and continue as if the #endif was not here.
1704 pop(@av_paren_type);
1705 push(@av_paren_type, $type);
1708 } elsif ($cur =~ /^(\\\n)/o) {
1709 print "PRECONT($1)\n" if ($dbg_values > 1);
1711 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1712 print "ATTR($1)\n" if ($dbg_values > 1);
1713 $av_pending = $type;
1716 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1717 print "SIZEOF($1)\n" if ($dbg_values > 1);
1723 } elsif ($cur =~ /^(if|while|for)\b/o) {
1724 print "COND($1)\n" if ($dbg_values > 1);
1728 } elsif ($cur =~/^(case)/o) {
1729 print "CASE($1)\n" if ($dbg_values > 1);
1730 $av_pend_colon = 'C';
1733 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1734 print "KEYWORD($1)\n" if ($dbg_values > 1);
1737 } elsif ($cur =~ /^(\()/o) {
1738 print "PAREN('$1')\n" if ($dbg_values > 1);
1739 push(@av_paren_type, $av_pending);
1743 } elsif ($cur =~ /^(\))/o) {
1744 my $new_type = pop(@av_paren_type);
1745 if ($new_type ne '_') {
1747 print "PAREN('$1') -> $type\n"
1748 if ($dbg_values > 1);
1750 print "PAREN('$1')\n" if ($dbg_values > 1);
1753 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1754 print "FUNC($1)\n" if ($dbg_values > 1);
1758 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1759 if (defined $2 && $type eq 'C' || $type eq 'T') {
1760 $av_pend_colon = 'B';
1761 } elsif ($type eq 'E') {
1762 $av_pend_colon = 'L';
1764 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1767 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1768 print "IDENT($1)\n" if ($dbg_values > 1);
1771 } elsif ($cur =~ /^($Assignment)/o) {
1772 print "ASSIGN($1)\n" if ($dbg_values > 1);
1775 } elsif ($cur =~/^(;|{|})/) {
1776 print "END($1)\n" if ($dbg_values > 1);
1778 $av_pend_colon = 'O';
1780 } elsif ($cur =~/^(,)/) {
1781 print "COMMA($1)\n" if ($dbg_values > 1);
1784 } elsif ($cur =~ /^(\?)/o) {
1785 print "QUESTION($1)\n" if ($dbg_values > 1);
1788 } elsif ($cur =~ /^(:)/o) {
1789 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1791 substr($var, length($res), 1, $av_pend_colon);
1792 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1797 $av_pend_colon = 'O';
1799 } elsif ($cur =~ /^(\[)/o) {
1800 print "CLOSE($1)\n" if ($dbg_values > 1);
1803 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1806 print "OPV($1)\n" if ($dbg_values > 1);
1813 substr($var, length($res), 1, $variant);
1816 } elsif ($cur =~ /^($Operators)/o) {
1817 print "OP($1)\n" if ($dbg_values > 1);
1818 if ($1 ne '++' && $1 ne '--') {
1822 } elsif ($cur =~ /(^.)/o) {
1823 print "C($1)\n" if ($dbg_values > 1);
1826 $cur = substr($cur, length($1));
1827 $res .= $type x length($1);
1831 return ($res, $var);
1835 my ($possible, $line) = @_;
1836 my $notPermitted = qr{(?:
1853 ^(?:typedef|struct|enum)\b
1855 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1856 if ($possible !~ $notPermitted) {
1857 # Check for modifiers.
1858 $possible =~ s/\s*$Storage\s*//g;
1859 $possible =~ s/\s*$Sparse\s*//g;
1860 if ($possible =~ /^\s*$/) {
1862 } elsif ($possible =~ /\s/) {
1863 $possible =~ s/\s*$Type\s*//g;
1864 for my $modifier (split(' ', $possible)) {
1865 if ($modifier !~ $notPermitted) {
1866 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1867 push(@modifierListFile, $modifier);
1872 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1873 push(@typeListFile, $possible);
1877 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1886 $type =~ tr/[a-z]/[A-Z]/;
1888 return defined $use_type{$type} if (scalar keys %use_type > 0);
1890 return !defined $ignore_type{$type};
1894 my ($level, $type, $msg) = @_;
1896 if (!show_type($type) ||
1897 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1902 if ($level eq 'ERROR') {
1904 } elsif ($level eq 'WARNING') {
1910 $output .= $prefix . $level . ':';
1912 $output .= BLUE if ($color);
1913 $output .= "$type:";
1915 $output .= RESET if ($color);
1916 $output .= ' ' . $msg . "\n";
1919 my @lines = split("\n", $output, -1);
1920 splice(@lines, 1, 1);
1921 $output = join("\n", @lines);
1923 $output = (split('\n', $output))[0] . "\n" if ($terse);
1925 push(our @report, $output);
1934 sub fixup_current_range {
1935 my ($lineRef, $offset, $length) = @_;
1937 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1940 my $no = $o + $offset;
1941 my $nl = $l + $length;
1942 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1946 sub fix_inserted_deleted_lines {
1947 my ($linesRef, $insertedRef, $deletedRef) = @_;
1949 my $range_last_linenr = 0;
1950 my $delta_offset = 0;
1955 my $next_insert = 0;
1956 my $next_delete = 0;
1960 my $inserted = @{$insertedRef}[$next_insert++];
1961 my $deleted = @{$deletedRef}[$next_delete++];
1963 foreach my $old_line (@{$linesRef}) {
1965 my $line = $old_line; #don't modify the array
1966 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
1968 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1969 $range_last_linenr = $new_linenr;
1970 fixup_current_range(\$line, $delta_offset, 0);
1973 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1974 $deleted = @{$deletedRef}[$next_delete++];
1976 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1979 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1980 push(@lines, ${$inserted}{'LINE'});
1981 $inserted = @{$insertedRef}[$next_insert++];
1983 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1987 push(@lines, $line);
1997 sub fix_insert_line {
1998 my ($linenr, $line) = @_;
2004 push(@fixed_inserted, $inserted);
2007 sub fix_delete_line {
2008 my ($linenr, $line) = @_;
2015 push(@fixed_deleted, $deleted);
2019 my ($type, $msg) = @_;
2021 if (report("ERROR", $type, $msg)) {
2029 my ($type, $msg) = @_;
2031 if (report("WARNING", $type, $msg)) {
2039 my ($type, $msg) = @_;
2041 if ($check && report("CHECK", $type, $msg)) {
2049 sub check_absolute_file {
2050 my ($absolute, $herecurr) = @_;
2051 my $file = $absolute;
2053 ##print "absolute<$absolute>\n";
2055 # See if any suffix of this path is a path within the tree.
2056 while ($file =~ s@^[^/]*/@@) {
2057 if (-f "$root/$file") {
2058 ##print "file<$file>\n";
2066 # It is, so see if the prefix is acceptable.
2067 my $prefix = $absolute;
2068 substr($prefix, -length($file)) = '';
2070 ##print "prefix<$prefix>\n";
2071 if ($prefix ne ".../") {
2072 WARN("USE_RELATIVE_PATH",
2073 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
2080 $string =~ s/^\s+|\s+$//g;
2088 $string =~ s/^\s+//;
2096 $string =~ s/\s+$//;
2101 sub string_find_replace {
2102 my ($string, $find, $replace) = @_;
2104 $string =~ s/$find/$replace/g;
2112 my $source_indent = 8;
2113 my $max_spaces_before_tab = $source_indent - 1;
2114 my $spaces_to_tab = " " x $source_indent;
2116 #convert leading spaces to tabs
2117 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2118 #Remove spaces before a tab
2119 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2124 sub pos_last_openparen {
2129 my $opens = $line =~ tr/\(/\(/;
2130 my $closes = $line =~ tr/\)/\)/;
2132 my $last_openparen = 0;
2134 if (($opens == 0) || ($closes >= $opens)) {
2138 my $len = length($line);
2140 for ($pos = 0; $pos < $len; $pos++) {
2141 my $string = substr($line, $pos);
2142 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2143 $pos += length($1) - 1;
2144 } elsif (substr($line, $pos, 1) eq '(') {
2145 $last_openparen = $pos;
2146 } elsif (index($string, '(') == -1) {
2151 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2157 for my $tasty (@rawlines) {
2158 $milktoast = $tasty;
2159 if (($tasty =~ /^\+DEFPY/ ||
2160 $tasty =~ /^\+DEFUN/ ||
2161 $tasty =~ /^\+ALIAS/) .. ($tasty =~ /^\+\{/)) {
2164 push(@breakfast, $milktoast);
2166 @rawlines = @breakfast;
2170 my $filename = shift;
2176 my $stashrawline="";
2186 my $in_header_lines = $file ? 0 : 1;
2187 my $in_commit_log = 0; #Scanning lines before patch
2188 my $has_commit_log = 0; #Encountered lines before patch
2189 my $commit_log_possible_stack_dump = 0;
2190 my $commit_log_long_line = 0;
2191 my $commit_log_has_diff = 0;
2192 my $reported_maintainer_file = 0;
2193 my $non_utf8_charset = 0;
2195 my $last_blank_line = 0;
2196 my $last_coalesced_string_linenr = -1;
2204 # Trace the real file/line as we go.
2209 my $context_function; #undef'd unless there's a known function
2211 my $comment_edge = 0;
2215 my $prev_values = 'E';
2218 my %suppress_ifbraces;
2219 my %suppress_whiletrailers;
2220 my %suppress_export;
2221 my $suppress_statement = 0;
2223 my %signatures = ();
2225 # Pre-scan the patch sanitizing the lines.
2226 # Pre-scan the patch looking for any __setup documentation.
2228 my @setup_docs = ();
2231 my $camelcase_file_seeded = 0;
2233 sanitise_line_reset();
2237 foreach my $rawline (@rawlines) {
2241 push(@fixed, $rawline) if ($fix);
2243 if ($rawline=~/^\+\+\+\s+(\S+)/) {
2245 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
2250 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2259 # Guestimate if this is a continuing comment. Run
2260 # the context looking for a comment "edge". If this
2261 # edge is a close comment then we must be in a comment
2265 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2266 next if (defined $rawlines[$ln - 1] &&
2267 $rawlines[$ln - 1] =~ /^-/);
2269 #print "RAW<$rawlines[$ln - 1]>\n";
2270 last if (!defined $rawlines[$ln - 1]);
2271 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2272 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2277 if (defined $edge && $edge eq '*/') {
2281 # Guestimate if this is a continuing comment. If this
2282 # is the start of a diff block and this line starts
2283 # ' *' then it is very likely a comment.
2284 if (!defined $edge &&
2285 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2290 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2291 sanitise_line_reset($in_comment);
2293 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2294 # Standardise the strings and chars within the input to
2295 # simplify matching -- only bother with positive lines.
2296 $line = sanitise_line($rawline);
2298 push(@lines, $line);
2301 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2306 #print "==>$rawline\n";
2307 #print "-->$line\n";
2309 if ($setup_docs && $line =~ /^\+/) {
2310 push(@setup_docs, $line);
2319 foreach my $line (@lines) {
2322 my $sline = $line; #copy of $line
2323 $sline =~ s/$;/ /g; #with comments as spaces
2325 my $rawline = $rawlines[$linenr - 1];
2327 #extract the line range in the file after the patch is applied
2328 if (!$in_commit_log &&
2329 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2332 $first_line = $linenr + 1;
2342 %suppress_ifbraces = ();
2343 %suppress_whiletrailers = ();
2344 %suppress_export = ();
2345 $suppress_statement = 0;
2346 if ($context =~ /\b(\w+)\s*\(/) {
2347 $context_function = $1;
2349 undef $context_function;
2353 # track the line number as we move through the hunk, note that
2354 # new versions of GNU diff omit the leading space on completely
2355 # blank context lines so we need to count that too.
2356 } elsif ($line =~ /^( |\+|$)/) {
2358 $realcnt-- if ($realcnt != 0);
2360 # Measure the line length and indent.
2361 ($length, $indent) = line_stats($rawline);
2363 # Track the previous line.
2364 ($prevline, $stashline) = ($stashline, $line);
2365 ($previndent, $stashindent) = ($stashindent, $indent);
2366 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2368 #warn "line<$line>\n";
2370 } elsif ($realcnt == 1) {
2374 my $hunk_line = ($realcnt != 0);
2376 $here = "#$linenr: " if (!$file);
2377 $here = "#$realline: " if ($file);
2380 # extract the filename as it passes
2381 if ($line =~ /^diff --git.*?(\S+)$/) {
2383 $realfile =~ s@^([^/]*)/@@ if (!$file);
2386 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2388 $realfile =~ s@^([^/]*)/@@ if (!$file);
2392 if (!$file && $tree && $p1_prefix ne '' &&
2393 -e "$root/$p1_prefix") {
2394 WARN("PATCH_PREFIX",
2395 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2398 if ($realfile =~ m@^include/asm/@) {
2399 ERROR("MODIFIED_INCLUDE_ASM",
2400 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2405 #make up the handle for any error we report on this line
2407 $prefix = "$realfile:$realline: "
2410 $prefix = "$filename:$realline: ";
2412 $prefix = "$filename:$linenr: ";
2417 if (is_maintained_obsolete($realfile)) {
2419 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2421 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
2424 $check = $check_orig;
2429 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2431 my $hereline = "$here\n$rawline\n";
2432 my $herecurr = "$here\n$rawline\n";
2433 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2435 $cnt_lines++ if ($realcnt != 0);
2437 # Check if the commit log has what seems like a diff which can confuse patch
2438 if ($in_commit_log && !$commit_log_has_diff &&
2439 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2440 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2441 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2442 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2443 ERROR("DIFF_IN_COMMIT_MSG",
2444 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2445 $commit_log_has_diff = 1;
2448 # Check for incorrect file permissions
2449 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2450 my $permhere = $here . "FILE: $realfile\n";
2451 if ($realfile !~ m@scripts/@ &&
2452 $realfile !~ /\.(py|pl|awk|sh)$/) {
2453 ERROR("EXECUTE_PERMISSIONS",
2454 "do not set execute permissions for source files\n" . $permhere);
2458 # Check the patch for a signoff:
2459 if ($line =~ /^\s*signed-off-by:/i) {
2464 # Check if MAINTAINERS is being updated. If so, there's probably no need to
2465 # emit the "does MAINTAINERS need updating?" message on file add/move/delete
2466 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2467 $reported_maintainer_file = 1;
2470 # Check signature styles
2471 if (!$in_header_lines &&
2472 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
2473 my $space_before = $1;
2475 my $space_after = $3;
2477 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2479 if ($sign_off !~ /$signature_tags/) {
2480 WARN("BAD_SIGN_OFF",
2481 "Non-standard signature: $sign_off\n" . $herecurr);
2483 if (defined $space_before && $space_before ne "") {
2484 if (WARN("BAD_SIGN_OFF",
2485 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2487 $fixed[$fixlinenr] =
2488 "$ucfirst_sign_off $email";
2491 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
2492 if (WARN("BAD_SIGN_OFF",
2493 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2495 $fixed[$fixlinenr] =
2496 "$ucfirst_sign_off $email";
2500 if (!defined $space_after || $space_after ne " ") {
2501 if (WARN("BAD_SIGN_OFF",
2502 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2504 $fixed[$fixlinenr] =
2505 "$ucfirst_sign_off $email";
2509 my ($email_name, $email_address, $comment) = parse_email($email);
2510 my $suggested_email = format_email(($email_name, $email_address));
2511 if ($suggested_email eq "") {
2512 ERROR("BAD_SIGN_OFF",
2513 "Unrecognized email address: '$email'\n" . $herecurr);
2515 my $dequoted = $suggested_email;
2516 $dequoted =~ s/^"//;
2517 $dequoted =~ s/" </ </;
2518 # Don't force email to have quotes
2519 # Allow just an angle bracketed address
2520 if ("$dequoted$comment" ne $email &&
2521 "<$email_address>$comment" ne $email &&
2522 "$suggested_email$comment" ne $email) {
2523 WARN("BAD_SIGN_OFF",
2524 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2528 # Check for duplicate signatures
2529 my $sig_nospace = $line;
2530 $sig_nospace =~ s/\s//g;
2531 $sig_nospace = lc($sig_nospace);
2532 if (defined $signatures{$sig_nospace}) {
2533 WARN("BAD_SIGN_OFF",
2534 "Duplicate signature\n" . $herecurr);
2536 $signatures{$sig_nospace} = 1;
2540 # Check email subject for common tools that don't need to be mentioned
2541 if ($in_header_lines &&
2542 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2543 WARN("EMAIL_SUBJECT",
2544 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2547 # Check for old stable address
2548 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2549 ERROR("STABLE_ADDRESS",
2550 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2553 # Check for unwanted Gerrit info
2554 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2555 ERROR("GERRIT_CHANGE_ID",
2556 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2559 # Check if the commit log is in a possible stack dump
2560 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2561 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2562 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2564 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2565 # stack dump address
2566 $commit_log_possible_stack_dump = 1;
2569 # Check for line lengths > 75 in commit log, warn once
2570 if ($in_commit_log && !$commit_log_long_line &&
2571 length($line) > 75 &&
2572 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2573 # file delta changes
2574 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2576 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2577 # A Fixes: or Link: line
2578 $commit_log_possible_stack_dump)) {
2579 WARN("COMMIT_LOG_LONG_LINE",
2580 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2581 $commit_log_long_line = 1;
2584 # Reset possible stack dump if a blank line is found
2585 if ($in_commit_log && $commit_log_possible_stack_dump &&
2587 $commit_log_possible_stack_dump = 0;
2590 # Check for git id commit length and improperly formed commit descriptions
2591 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2592 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
2593 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2594 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2595 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2596 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2597 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2598 my $init_char = "c";
2599 my $orig_commit = "";
2606 my $id = '0123456789ab';
2607 my $orig_desc = "commit description";
2608 my $description = "";
2610 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2612 $orig_commit = lc($2);
2613 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2614 $orig_commit = lc($1);
2617 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2618 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2619 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2620 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2621 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2624 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2625 defined $rawlines[$linenr] &&
2626 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2629 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2630 defined $rawlines[$linenr] &&
2631 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2632 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2634 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2635 $orig_desc .= " " . $1;
2639 ($id, $description) = git_commit_info($orig_commit,
2643 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2644 ERROR("GIT_COMMIT_ID",
2645 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2649 # Check for added, moved or deleted files
2650 if (!$reported_maintainer_file && !$in_commit_log &&
2651 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2652 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2653 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2654 (defined($1) || defined($2))))) {
2656 $reported_maintainer_file = 1;
2657 WARN("FILE_PATH_CHANGES",
2658 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2661 # Check for wrappage within a valid hunk of the file
2662 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2663 ERROR("CORRUPTED_PATCH",
2664 "patch seems to be corrupt (line wrapped?)\n" .
2665 $herecurr) if (!$emitted_corrupt++);
2668 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2669 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2670 $rawline !~ m/^$UTF8*$/) {
2671 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2673 my $blank = copy_spacing($rawline);
2674 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2675 my $hereptr = "$hereline$ptr\n";
2678 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
2681 # Check if it's the start of a commit log
2682 # (not a header line and we haven't seen the patch filename)
2683 if ($in_header_lines && $realfile =~ /^$/ &&
2684 !($rawline =~ /^\s+(?:\S|$)/ ||
2685 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
2686 $in_header_lines = 0;
2688 $has_commit_log = 1;
2691 # Check if there is UTF-8 in a commit log when a mail header has explicitly
2692 # declined it, i.e defined some charset where it is missing.
2693 if ($in_header_lines &&
2694 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2696 $non_utf8_charset = 1;
2699 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
2700 $rawline =~ /$NON_ASCII_UTF8/) {
2701 WARN("UTF8_BEFORE_PATCH",
2702 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2705 # Check for absolute kernel paths in commit message
2706 if ($tree && $in_commit_log) {
2707 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2710 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2711 check_absolute_file($1, $herecurr)) {
2714 check_absolute_file($file, $herecurr);
2719 # Check for various typo / spelling mistakes
2720 if (defined($misspellings) &&
2721 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2722 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
2724 my $typo_fix = $spelling_fix{lc($typo)};
2725 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2726 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2727 my $msg_level = \&WARN;
2728 $msg_level = \&CHK if ($file);
2729 if (&{$msg_level}("TYPO_SPELLING",
2730 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2732 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2737 # ignore non-hunk lines and lines being removed
2738 next if (!$hunk_line || $line =~ /^-/);
2740 #trailing whitespace
2741 if ($line =~ /^\+.*\015/) {
2742 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2743 if (ERROR("DOS_LINE_ENDINGS",
2744 "DOS line endings\n" . $herevet) &&
2746 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
2748 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2749 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2750 if (ERROR("TRAILING_WHITESPACE",
2751 "trailing whitespace\n" . $herevet) &&
2753 $fixed[$fixlinenr] =~ s/\s+$//;
2759 # check for Kconfig help text having a real description
2760 # Only applies when adding the entry originally, after that we do not have
2761 # sufficient context to determine whether it is indeed long enough.
2762 if ($realfile =~ /Kconfig/ &&
2763 $line =~ /^\+\s*config\s+/) {
2766 my $ln = $linenr + 1;
2770 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
2771 $f = $lines[$ln - 1];
2772 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2773 $is_end = $lines[$ln - 1] =~ /^\+/;
2775 next if ($f =~ /^-/);
2776 last if (!$file && $f =~ /^\@\@/);
2778 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2780 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2787 next if ($f =~ /^$/);
2788 if ($f =~ /^\s*config\s/) {
2794 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2795 WARN("CONFIG_DESCRIPTION",
2796 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2798 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
2801 # check for MAINTAINERS entries that don't have the right form
2802 if ($realfile =~ /^MAINTAINERS$/ &&
2803 $rawline =~ /^\+[A-Z]:/ &&
2804 $rawline !~ /^\+[A-Z]:\t\S/) {
2805 if (WARN("MAINTAINERS_STYLE",
2806 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2808 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2812 # discourage the use of boolean for type definition attributes of Kconfig options
2813 if ($realfile =~ /Kconfig/ &&
2814 $line =~ /^\+\s*\bboolean\b/) {
2815 WARN("CONFIG_TYPE_BOOLEAN",
2816 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2819 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2820 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2823 'EXTRA_AFLAGS' => 'asflags-y',
2824 'EXTRA_CFLAGS' => 'ccflags-y',
2825 'EXTRA_CPPFLAGS' => 'cppflags-y',
2826 'EXTRA_LDFLAGS' => 'ldflags-y',
2829 WARN("DEPRECATED_VARIABLE",
2830 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead
.\n" . $herecurr) if ($replacement->{$flag});
2833 # check for DT compatible documentation
2834 if (defined $root &&
2835 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2836 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2838 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2840 my $dt_path = $root . "/Documentation/devicetree
/bindings/";
2841 my $vp_file = $dt_path . "vendor-prefixes
.txt
";
2843 foreach my $compat (@compats) {
2844 my $compat2 = $compat;
2845 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2846 my $compat3 = $compat;
2847 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2848 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2850 WARN("UNDOCUMENTED_DT_STRING
",
2851 "DT compatible string
\"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2854 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2856 `grep -Eq "^$vendor\\b" $vp_file`;
2858 WARN("UNDOCUMENTED_DT_STRING",
2859 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2864 # check we are in a valid source file if not then ignore this hunk
2865 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
2867 # line length limit (with some exclusions)
2869 # There are a few types of lines that may extend beyond $max_line_length:
2870 # logging functions like pr_info that end in a string
2871 # lines with a single string
2872 # #defines that are a single string
2874 # There are 3 different line length message types:
2875 # LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
2876 # LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2877 # LONG_LINE all other lines longer than $max_line_length
2879 # if LONG_LINE is ignored, the other 2 types are also ignored
2882 if ($line =~ /^\+/ && $length > $max_line_length) {
2883 my $msg_type = "LONG_LINE";
2885 # Check the allowed long line types first
2887 # logging functions that end in a string that starts
2888 # before $max_line_length
2889 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2890 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2893 # lines with only strings (w/ possible termination)
2894 # #defines with only strings
2895 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2896 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2899 # More special cases
2900 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
2901 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
2904 # Otherwise set the alternate message types
2906 # a comment starts before $max_line_length
2907 } elsif ($line =~ /($;[\s$;]*)$/ &&
2908 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2909 $msg_type = "LONG_LINE_COMMENT"
2911 # a quoted string starts before $max_line_length
2912 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
2913 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2914 $msg_type = "LONG_LINE_STRING"
2917 if ($msg_type ne "" &&
2918 (show_type("LONG_LINE") || show_type($msg_type))) {
2920 "line over $max_line_length characters\n" . $herecurr);
2924 # check for adding lines without a newline.
2925 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2926 WARN("MISSING_EOF_NEWLINE",
2927 "adding a line without newline at end of file\n" . $herecurr);
2930 # Blackfin: use hi/lo macros
2931 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2932 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2933 my $herevet = "$here\n" . cat_vet($line) . "\n";
2935 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
2937 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2938 my $herevet = "$here\n" . cat_vet($line) . "\n";
2940 "use the HI() macro, not (... >> 16)\n" . $herevet);
2944 # check we are in a valid source file C or perl if not then ignore this hunk
2945 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
2947 # at the beginning of a line any tabs must come first and anything
2948 # more than 8 must use tabs.
2949 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2950 $rawline =~ /^\+\s* \s*/) {
2951 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2953 if (ERROR("CODE_INDENT",
2954 "code indent should use tabs where possible\n" . $herevet) &&
2956 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2960 # check for space before tabs.
2961 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2962 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2963 if (WARN("SPACE_BEFORE_TAB",
2964 "please, no space before tabs\n" . $herevet) &&
2966 while ($fixed[$fixlinenr] =~
2967 s/(^\+.*) {8,8}\t/$1\t\t/) {}
2968 while ($fixed[$fixlinenr] =~
2969 s/(^\+.*) +\t/$1\t/) {}
2973 # check for && or || at the start of a line
2974 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2975 CHK("LOGICAL_CONTINUATIONS",
2976 "Logical continuations should be on the previous line\n" . $hereprev);
2979 # check indentation starts on a tab stop
2980 if ($^V && $^V ge 5.10.0 &&
2981 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
2982 my $indent = length($1);
2985 "Statements should start on a tabstop\n" . $herecurr) &&
2987 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
2992 # check multi-line statement indentation matches previous line
2993 if ($^V && $^V ge 5.10.0 &&
2994 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2995 $prevline =~ /^\+(\t*)(.*)$/;
2999 my $pos = pos_last_openparen($rest);
3001 $line =~ /^(\+| )([ \t]*)/;
3004 my $goodtabindent = $oldindent .
3007 my $goodspaceindent = $oldindent . " " x $pos;
3009 if ($newindent ne $goodtabindent &&
3010 $newindent ne $goodspaceindent) {
3012 if (CHK("PARENTHESIS_ALIGNMENT",
3013 "Alignment should match open parenthesis\n" . $hereprev) &&
3014 $fix && $line =~ /^\+/) {
3015 $fixed[$fixlinenr] =~
3016 s/^\+[ \t]*/\+$goodtabindent/;
3022 # check for space after cast like "(int) foo" or "(struct foo) bar"
3023 # avoid checking a few false positives:
3024 # "sizeof(<type>)" or "__alignof__(<type>)"
3025 # function pointer declarations like "(*foo)(int) = bar;"
3026 # structure definitions like "(struct foo) { 0 };"
3027 # multiline macros that define functions
3028 # known attributes or the __attribute__ keyword
3029 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3030 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
3032 "No space is necessary after a cast\n" . $herecurr) &&
3034 $fixed[$fixlinenr] =~
3035 s/(\(\s*$Type\s*\))[ \t]+/$1/;
3039 # Block comment styles
3040 # Networking with an initial /*
3041 if ($realfile =~ m@^(drivers/net/|net/)@ &&
3042 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
3043 $rawline =~ /^\+[ \t]*\*/ &&
3045 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3046 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3049 # Block comments use * on subsequent lines
3050 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3051 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
3052 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
3053 $rawline =~ /^\+/ && #line is new
3054 $rawline !~ /^\+[ \t]*\*/) { #no leading *
3055 WARN("BLOCK_COMMENT_STYLE",
3056 "Block comments use * on subsequent lines\n" . $hereprev);
3059 # Block comments use */ on trailing lines
3060 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
3061 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3062 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3063 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
3064 WARN("BLOCK_COMMENT_STYLE",
3065 "Block comments use a trailing */ on a separate line\n" . $herecurr);
3068 # Block comment * alignment
3069 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3070 $line =~ /^\+[ \t]*$;/ && #leading comment
3071 $rawline =~ /^\+[ \t]*\*/ && #leading *
3072 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
3073 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
3074 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3076 $prevrawline =~ m@^\+([ \t]*/?)\*@;
3078 $oldindent = expand_tabs($1);
3080 $prevrawline =~ m@^\+(.*/?)\*@;
3081 $oldindent = expand_tabs($1);
3083 $rawline =~ m@^\+([ \t]*)\*@;
3085 $newindent = expand_tabs($newindent);
3086 if (length($oldindent) ne length($newindent)) {
3087 WARN("BLOCK_COMMENT_STYLE",
3088 "Block comments should align the * on each line\n" . $hereprev);
3092 # check for missing blank lines after struct/union declarations
3093 # with exceptions for various attributes and macros
3094 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3096 !($line =~ /^\+\s*$/ ||
3097 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3098 $line =~ /^\+\s*MODULE_/i ||
3099 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3100 $line =~ /^\+[a-z_]*init/ ||
3101 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3102 $line =~ /^\+\s*DECLARE/ ||
3103 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
3104 $line =~ /^\+\s*__setup/)) {
3105 if (CHK("LINE_SPACING",
3106 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3108 fix_insert_line($fixlinenr, "\+");
3112 # check for multiple consecutive blank lines
3113 if ($prevline =~ /^[\+ ]\s*$/ &&
3114 $line =~ /^\+\s*$/ &&
3115 $last_blank_line != ($linenr - 1)) {
3116 if (CHK("LINE_SPACING",
3117 "Please don't use multiple blank lines\n" . $hereprev) &&
3119 fix_delete_line($fixlinenr, $rawline);
3122 $last_blank_line = $linenr;
3125 # check for missing blank lines after declarations
3126 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3127 # actual declarations
3128 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3129 # function pointer declarations
3130 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3131 # foo bar; where foo is some local typedef or #define
3132 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3133 # known declaration macros
3134 $prevline =~ /^\+\s+$declaration_macros/) &&
3135 # for "else if" which can look like "$Ident $Ident"
3136 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3137 # other possible extensions of declaration lines
3138 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3139 # not starting a section or a macro "\" extended line
3140 $prevline =~ /(?:\{\s*|\\)$/) &&
3141 # looks like a declaration
3142 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3143 # function pointer declarations
3144 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3145 # foo bar; where foo is some local typedef or #define
3146 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3147 # known declaration macros
3148 $sline =~ /^\+\s+$declaration_macros/ ||
3149 # start of struct or union or enum
3150 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
3151 # start or end of block or continuation of declaration
3152 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3153 # bitfield continuation
3154 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3155 # other possible extensions of declaration lines
3156 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3157 # indentation of previous and current line are the same
3158 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3159 if (WARN("LINE_SPACING",
3160 "Missing a blank line after declarations\n" . $hereprev) &&
3162 fix_insert_line($fixlinenr, "\+");
3166 # check for spaces at the beginning of a line.
3168 # 1) within comments
3169 # 2) indented preprocessor commands
3171 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
3172 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3173 if (WARN("LEADING_SPACE",
3174 "please, no spaces at the start of a line\n" . $herevet) &&
3176 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
3180 # check we are in a valid C source file if not then ignore this hunk
3181 next if ($realfile !~ /\.(h|c)$/);
3183 # check for unusual line ending [ or (
3184 if ($line =~ /^\+.*([\[\(])\s*$/) {
3185 CHK("OPEN_ENDED_LINE",
3186 "Lines should not end with a '$1'\n" . $herecurr);
3189 # check if this appears to be the start function declaration, save the name
3190 if ($sline =~ /^\+\{\s*$/ &&
3191 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3192 $context_function = $1;
3195 # check if this appears to be the end of function declaration
3196 if ($sline =~ /^\+\}\s*$/) {
3197 undef $context_function;
3200 # check indentation of any line with a bare else
3201 # (but not if it is a multiple line "if (foo) return bar; else return baz;")
3202 # if the previous line is a break or return and is indented 1 tab more...
3203 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3204 my $tabs = length($1) + 1;
3205 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3206 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3207 defined $lines[$linenr] &&
3208 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3209 WARN("UNNECESSARY_ELSE",
3210 "else is not generally useful after a break or return\n" . $hereprev);
3214 # check indentation of a line with a break;
3215 # if the previous line is a goto or return and is indented the same # of tabs
3216 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3218 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3219 WARN("UNNECESSARY_BREAK",
3220 "break is not useful after a goto or return\n" . $hereprev);
3224 # check for RCS/CVS revision markers
3225 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3227 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3230 # Blackfin: don't use __builtin_bfin_[cs]sync
3231 if ($line =~ /__builtin_bfin_csync/) {
3232 my $herevet = "$here\n" . cat_vet($line) . "\n";
3234 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
3236 if ($line =~ /__builtin_bfin_ssync/) {
3237 my $herevet = "$here\n" . cat_vet($line) . "\n";
3239 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
3242 # check for old HOTPLUG __dev<foo> section markings
3243 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3244 WARN("HOTPLUG_SECTION",
3245 "Using $1 is unnecessary\n" . $herecurr);
3248 # Check for potential 'bare' types
3249 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3251 #print "LINE<$line>\n";
3252 if ($linenr > $suppress_statement &&
3253 $realcnt && $sline =~ /.\s*\S/) {
3254 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3255 ctx_statement_block($linenr, $realcnt, 0);
3256 $stat =~ s/\n./\n /g;
3257 $cond =~ s/\n./\n /g;
3259 #print "linenr<$linenr> <$stat>\n";
3260 # If this statement has no statement boundaries within
3261 # it there is no point in retrying a statement scan
3262 # until we hit end of it.
3263 my $frag = $stat; $frag =~ s/;+\s*$//;
3264 if ($frag !~ /(?:{|;)/) {
3265 #print "skip<$line_nr_next>\n";
3266 $suppress_statement = $line_nr_next;
3269 # Find the real next line.
3270 $realline_next = $line_nr_next;
3271 if (defined $realline_next &&
3272 (!defined $lines[$realline_next - 1] ||
3273 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3280 # Ignore goto labels.
3281 if ($s =~ /$Ident:\*$/s) {
3283 # Ignore functions being called
3284 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3286 } elsif ($s =~ /^.\s*else\b/s) {
3288 # declarations always start with types
3289 } 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) {
3292 possible($type, "A:" . $s);
3294 # definitions in global scope can only start with types
3295 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3296 possible($1, "B:" . $s);
3299 # any (foo ... *) is a pointer cast, and foo is a type
3300 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3301 possible($1, "C:" . $s);
3304 # Check for any sort of function declaration.
3305 # int foo(something bar, other baz);
3306 # void (*store_gdt)(x86_descr_ptr *);
3307 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
3308 my ($name_len) = length($1);
3311 substr($ctx, 0, $name_len + 1, '');
3312 $ctx =~ s/\)[^\)]*$//;
3314 for my $arg (split(/\s*,\s*/, $ctx)) {
3315 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
3317 possible($1, "D:" . $s);
3325 # Checks which may be anchored in the context.
3328 # Check for switch () and associated case and default
3329 # statements should be at the same indent.
3330 if ($line=~/\bswitch\s*\(.*\)/) {
3333 my @ctx = ctx_block_outer($linenr, $realcnt);
3335 for my $ctx (@ctx) {
3336 my ($clen, $cindent) = line_stats($ctx);
3337 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3338 $indent != $cindent) {
3339 $err .= "$sep$ctx\n";
3346 ERROR("SWITCH_CASE_INDENT_LEVEL",
3347 "switch and case should be at the same indent\n$hereline$err");
3351 # if/while/etc brace do not go on next line, unless defining a do while loop,
3352 # or if that brace on the next line is for something else
3353 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3354 my $pre_ctx = "$1$2";
3356 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
3358 if ($line =~ /^\+\t{6,}/) {
3359 WARN("DEEP_INDENTATION",
3360 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3363 my $ctx_cnt = $realcnt - $#ctx - 1;
3364 my $ctx = join("\n", @ctx);
3366 my $ctx_ln = $linenr;
3367 my $ctx_skip = $realcnt;
3369 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3370 defined $lines[$ctx_ln - 1] &&
3371 $lines[$ctx_ln - 1] =~ /^-/)) {
3372 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3373 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3377 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3378 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3380 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3382 "that open brace { should be on the previous line\n" .
3383 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3385 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3386 $ctx =~ /\)\s*\;\s*$/ &&
3387 defined $lines[$ctx_ln - 1])
3389 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3390 if ($nindent > $indent) {
3391 WARN("TRAILING_SEMICOLON",
3392 "trailing semicolon indicates no statements, indent implies otherwise\n" .
3393 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3398 # Check relative indent for conditionals and blocks.
3399 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
3400 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3401 ctx_statement_block($linenr, $realcnt, 0)
3402 if (!defined $stat);
3403 my ($s, $c) = ($stat, $cond);
3405 substr($s, 0, length($c), '');
3407 # remove inline comments
3411 # Find out how long the conditional actually is.
3412 my @newlines = ($c =~ /\n/gs);
3413 my $cond_lines = 1 + $#newlines;
3415 # Make sure we remove the line prefixes as we have
3416 # none on the first line, and are going to readd them
3419 while ($s =~ /\n\s+\\\n/) {
3420 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3423 # We want to check the first line inside the block
3424 # starting at the end of the conditional, so remove:
3425 # 1) any blank line termination
3426 # 2) any opening brace { on end of the line
3428 my $continuation = 0;
3430 $s =~ s/^.*\bdo\b//;
3432 if ($s =~ s/^\s*\\//) {
3435 if ($s =~ s/^\s*?\n//) {
3440 # Also ignore a loop construct at the end of a
3441 # preprocessor statement.
3442 if (($prevline =~ /^.\s*#\s*define\s/ ||
3443 $prevline =~ /\\\s*$/) && $continuation == 0) {
3449 while ($cond_ptr != $cond_lines) {
3450 $cond_ptr = $cond_lines;
3452 # If we see an #else/#elif then the code
3454 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3459 # 1) blank lines, they should be at 0,
3460 # 2) preprocessor lines, and
3462 if ($continuation ||
3464 $s =~ /^\s*#\s*?/ ||
3465 $s =~ /^\s*$Ident\s*:/) {
3466 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
3467 if ($s =~ s/^.*?\n//) {
3473 my (undef, $sindent) = line_stats("+" . $s);
3474 my $stat_real = raw_line($linenr, $cond_lines);
3476 # Check if either of these lines are modified, else
3477 # this is not this patch's fault.
3478 if (!defined($stat_real) ||
3479 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3482 if (defined($stat_real) && $cond_lines > 1) {
3483 $stat_real = "[...]\n$stat_real";
3486 #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";
3488 if ($check && $s ne '' &&
3489 (($sindent % 8) != 0 ||
3490 ($sindent < $indent) ||
3491 ($sindent == $indent &&
3492 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
3493 ($sindent > $indent + 8))) {
3494 WARN("SUSPECT_CODE_INDENT",
3495 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
3499 # Track the 'values' across context and added lines.
3500 my $opline = $line; $opline =~ s/^./ /;
3501 my ($curr_values, $curr_vars) =
3502 annotate_values($opline . "\n", $prev_values);
3503 $curr_values = $prev_values . $curr_values;
3505 my $outline = $opline; $outline =~ s/\t/ /g;
3506 print "$linenr > .$outline\n";
3507 print "$linenr > $curr_values\n";
3508 print "$linenr > $curr_vars\n";
3510 $prev_values = substr($curr_values, -1);
3512 #ignore lines not being added
3513 next if ($line =~ /^[^\+]/);
3515 # check for dereferences that span multiple lines
3516 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3517 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3518 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3520 $line =~ /^.\s*($Lval)/;
3523 WARN("MULTILINE_DEREFERENCE",
3524 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3527 # check for declarations of signed or unsigned without int
3528 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3531 $var = "" if (!defined $var);
3532 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3536 $pointer = "" if (!defined $pointer);
3538 if (WARN("UNSPECIFIED_INT",
3539 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3541 my $decl = trim($sign) . " int ";
3542 my $comp_pointer = $pointer;
3543 $comp_pointer =~ s/\s//g;
3544 $decl .= $comp_pointer;
3545 $decl = rtrim($decl) if ($var eq "");
3546 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3551 # TEST: allow direct testing of the type matcher.
3553 if ($line =~ /^.\s*$Declare\s*$/) {
3555 "TEST: is type\n" . $herecurr);
3556 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3557 ERROR("TEST_NOT_TYPE",
3558 "TEST: is not type ($1 is)\n". $herecurr);
3562 # TEST: allow direct testing of the attribute matcher.
3564 if ($line =~ /^.\s*$Modifier\s*$/) {
3566 "TEST: is attr\n" . $herecurr);
3567 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3568 ERROR("TEST_NOT_ATTR",
3569 "TEST: is not attr ($1 is)\n". $herecurr);
3574 # check for initialisation to aggregates open brace on the next line
3575 if ($line =~ /^.\s*{/ &&
3576 $prevline =~ /(?:^|[^=])=\s*$/) {
3577 if (ERROR("OPEN_BRACE",
3578 "that open brace { should be on the previous line\n" . $hereprev) &&
3579 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3580 fix_delete_line($fixlinenr - 1, $prevrawline);
3581 fix_delete_line($fixlinenr, $rawline);
3582 my $fixedline = $prevrawline;
3583 $fixedline =~ s/\s*=\s*$/ = {/;
3584 fix_insert_line($fixlinenr, $fixedline);
3586 $fixedline =~ s/^(.\s*)\{\s*/$1/;
3587 fix_insert_line($fixlinenr, $fixedline);
3592 # Checks which are anchored on the added line.
3595 # check for malformed paths in #include statements (uses RAW line)
3596 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3598 if ($path =~ m{//}) {
3599 ERROR("MALFORMED_INCLUDE",
3600 "malformed #include filename\n" . $herecurr);
3602 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3603 ERROR("UAPI_INCLUDE",
3604 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3608 # no C99 // comments
3609 if ($line =~ m{//}) {
3610 if (!$allow_c99_comments) {
3611 if(ERROR("C99_COMMENTS",
3612 "do not use C99 // comments\n" . $herecurr) &&
3614 my $line = $fixed[$fixlinenr];
3615 if ($line =~ /\/\/(.*)$/) {
3616 my $comment = trim($1);
3617 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
3621 WARN("C99_COMMENTS",
3622 "C99 // comments do not match recommendation\n" . $herecurr);
3625 # Remove C99 comments.
3627 $opline =~ s@//.*@@;
3629 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3630 # the whole statement.
3631 #print "APW <$lines[$realline_next - 1]>\n";
3632 if (defined $realline_next &&
3633 exists $lines[$realline_next - 1] &&
3634 !defined $suppress_export{$realline_next} &&
3635 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3636 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3637 # Handle definitions which produce identifiers with
3640 # EXPORT_SYMBOL(something_foo);
3642 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
3643 $name =~ /^${Ident}_$2/) {
3644 #print "FOO C name<$name>\n";
3645 $suppress_export{$realline_next} = 1;
3647 } elsif ($stat !~ /(?:
3649 ^.DEFINE_$Ident\(\Q$name\E\)|
3650 ^.DECLARE_$Ident\(\Q$name\E\)|
3651 ^.LIST_HEAD\(\Q$name\E\)|
3652 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3653 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
3655 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3656 $suppress_export{$realline_next} = 2;
3658 $suppress_export{$realline_next} = 1;
3661 if (!defined $suppress_export{$linenr} &&
3662 $prevline =~ /^.\s*$/ &&
3663 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3664 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3665 #print "FOO B <$lines[$linenr - 1]>\n";
3666 $suppress_export{$linenr} = 2;
3668 if (defined $suppress_export{$linenr} &&
3669 $suppress_export{$linenr} == 2) {
3670 WARN("EXPORT_SYMBOL",
3671 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
3674 # check for global initialisers.
3675 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3676 if (ERROR("GLOBAL_INITIALISERS",
3677 "do not initialise globals to $1\n" . $herecurr) &&
3679 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3682 # check for static initialisers.
3683 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3684 if (ERROR("INITIALISED_STATIC",
3685 "do not initialise statics to $1\n" .
3688 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3692 # check for misordered declarations of char/short/int/long with signed/unsigned
3693 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3695 WARN("MISORDERED_TYPE",
3696 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3699 # check for static const char * arrays.
3700 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3701 WARN("STATIC_CONST_CHAR_ARRAY",
3702 "static const char * array should probably be static const char * const\n" .
3706 # check for static char foo[] = "bar" declarations.
3707 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3708 WARN("STATIC_CONST_CHAR_ARRAY",
3709 "static char array declaration should probably be static const char\n" .
3713 # check for const <foo> const where <foo> is not a pointer or array type
3714 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3716 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3718 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3719 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3721 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3725 # check for non-global char *foo[] = {"bar", ...} declarations.
3726 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3727 WARN("STATIC_CONST_CHAR_ARRAY",
3728 "char * array declaration might be better as static const\n" .
3732 # check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3733 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3735 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3737 if (WARN("ARRAY_SIZE",
3738 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3740 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3745 # check for function declarations without arguments like "int foo()"
3746 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3747 if (ERROR("FUNCTION_WITHOUT_ARGS",
3748 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3750 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3754 # check for new typedefs, only function parameters and sparse annotations
3756 if ($line =~ /\btypedef\s/ &&
3757 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3758 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
3759 $line !~ /\b$typeTypedefs\b/ &&
3760 $line !~ /\b__bitwise\b/) {
3761 WARN("NEW_TYPEDEFS",
3762 "do not add new typedefs\n" . $herecurr);
3765 # * goes on variable not on type
3767 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3769 my ($ident, $from, $to) = ($1, $2, $2);
3771 # Should start with a space.
3772 $to =~ s/^(\S)/ $1/;
3773 # Should not end with a space.
3775 # '*'s should not have spaces between.
3776 while ($to =~ s/\*\s+\*/\*\*/) {
3779 ## print "1: from<$from> to<$to> ident<$ident>\n";
3781 if (ERROR("POINTER_LOCATION",
3782 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3784 my $sub_from = $ident;
3785 my $sub_to = $ident;
3786 $sub_to =~ s/\Q$from\E/$to/;
3787 $fixed[$fixlinenr] =~
3788 s@\Q$sub_from\E@$sub_to@;
3792 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3794 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3796 # Should start with a space.
3797 $to =~ s/^(\S)/ $1/;
3798 # Should not end with a space.
3800 # '*'s should not have spaces between.
3801 while ($to =~ s/\*\s+\*/\*\*/) {
3803 # Modifiers should have spaces.
3804 $to =~ s/(\b$Modifier$)/$1 /;
3806 ## print "2: from<$from> to<$to> ident<$ident>\n";
3807 if ($from ne $to && $ident !~ /^$Modifier$/) {
3808 if (ERROR("POINTER_LOCATION",
3809 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3812 my $sub_from = $match;
3813 my $sub_to = $match;
3814 $sub_to =~ s/\Q$from\E/$to/;
3815 $fixed[$fixlinenr] =~
3816 s@\Q$sub_from\E@$sub_to@;
3821 # avoid BUG() or BUG_ON()
3822 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
3823 my $msg_level = \&WARN;
3824 $msg_level = \&CHK if ($file);
3825 &{$msg_level}("AVOID_BUG",
3826 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
3829 # avoid LINUX_VERSION_CODE
3830 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3831 WARN("LINUX_VERSION_CODE",
3832 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
3835 # check for uses of printk_ratelimit
3836 if ($line =~ /\bprintk_ratelimit\s*\(/) {
3837 WARN("PRINTK_RATELIMITED",
3838 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
3841 # printk should use KERN_* levels
3842 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
3843 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3844 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
3847 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3849 my $level = lc($orig);
3850 $level = "warn" if ($level eq "warning");
3851 my $level2 = $level;
3852 $level2 = "dbg" if ($level eq "debug");
3853 WARN("PREFER_PR_LEVEL",
3854 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
3857 if ($line =~ /\bpr_warning\s*\(/) {
3858 if (WARN("PREFER_PR_LEVEL",
3859 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3861 $fixed[$fixlinenr] =~
3862 s/\bpr_warning\b/pr_warn/;
3866 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3868 my $level = lc($orig);
3869 $level = "warn" if ($level eq "warning");
3870 $level = "dbg" if ($level eq "debug");
3871 WARN("PREFER_DEV_LEVEL",
3872 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3875 # ENOSYS means "bad syscall nr" and nothing else. This will have a small
3876 # number of false positives, but assembly files are not checked, so at
3877 # least the arch entry code will not trigger this warning.
3878 if ($line =~ /\bENOSYS\b/) {
3880 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3883 # function brace can't be on same line, except for #defines of do while,
3884 # or if closed on same line
3885 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3886 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
3887 if (ERROR("OPEN_BRACE",
3888 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3890 fix_delete_line($fixlinenr, $rawline);
3891 my $fixed_line = $rawline;
3892 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3895 fix_insert_line($fixlinenr, ltrim($line1));
3896 fix_insert_line($fixlinenr, "\+{");
3897 if ($line2 !~ /^\s*$/) {
3898 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3903 # open braces for enum, union and struct go on the same line.
3904 if ($line =~ /^.\s*{/ &&
3905 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
3906 if (ERROR("OPEN_BRACE",
3907 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3908 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3909 fix_delete_line($fixlinenr - 1, $prevrawline);
3910 fix_delete_line($fixlinenr, $rawline);
3911 my $fixedline = rtrim($prevrawline) . " {";
3912 fix_insert_line($fixlinenr, $fixedline);
3913 $fixedline = $rawline;
3914 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
3915 if ($fixedline !~ /^\+\s*$/) {
3916 fix_insert_line($fixlinenr, $fixedline);
3921 # missing space after union, struct or enum definition
3922 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3924 "missing space after $1 definition\n" . $herecurr) &&
3926 $fixed[$fixlinenr] =~
3927 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3931 # Function pointer declarations
3932 # check spacing between type, funcptr, and args
3933 # canonical declaration is "type (*funcptr)(args...)"
3934 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
3936 my $pre_pointer_space = $2;
3937 my $post_pointer_space = $3;
3939 my $post_funcname_space = $5;
3940 my $pre_args_space = $6;
3942 # the $Declare variable will capture all spaces after the type
3943 # so check it for a missing trailing missing space but pointer return types
3944 # don't need a space so don't warn for those.
3945 my $post_declare_space = "";
3946 if ($declare =~ /(\s+)$/) {
3947 $post_declare_space = $1;
3948 $declare = rtrim($declare);
3950 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
3952 "missing space after return type\n" . $herecurr);
3953 $post_declare_space = " ";
3956 # unnecessary space "type (*funcptr)(args...)"
3957 # This test is not currently implemented because these declarations are
3959 # int foo(int bar, ...)
3960 # and this is form shouldn't/doesn't generate a checkpatch warning.
3962 # elsif ($declare =~ /\s{2,}$/) {
3964 # "Multiple spaces after return type\n" . $herecurr);
3967 # unnecessary space "type ( *funcptr)(args...)"
3968 if (defined $pre_pointer_space &&
3969 $pre_pointer_space =~ /^\s/) {
3971 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3974 # unnecessary space "type (* funcptr)(args...)"
3975 if (defined $post_pointer_space &&
3976 $post_pointer_space =~ /^\s/) {
3978 "Unnecessary space before function pointer name\n" . $herecurr);
3981 # unnecessary space "type (*funcptr )(args...)"
3982 if (defined $post_funcname_space &&
3983 $post_funcname_space =~ /^\s/) {
3985 "Unnecessary space after function pointer name\n" . $herecurr);
3988 # unnecessary space "type (*funcptr) (args...)"
3989 if (defined $pre_args_space &&
3990 $pre_args_space =~ /^\s/) {
3992 "Unnecessary space before function pointer arguments\n" . $herecurr);
3995 if (show_type("SPACING") && $fix) {
3996 $fixed[$fixlinenr] =~
3997 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
4001 # check for spacing round square brackets; allowed:
4002 # 1. with a type on the left -- int [] a;
4003 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4004 # 3. inside a curly brace -- = { [0...10] = 5 }
4005 while ($line =~ /(.*?\s)\[/g) {
4006 my ($where, $prefix) = ($-[1], $1);
4007 if ($prefix !~ /$Type\s+$/ &&
4008 ($where != 0 || $prefix !~ /^.\s+$/) &&
4009 $prefix !~ /[{,]\s+$/) {
4010 if (ERROR("BRACKET_SPACE",
4011 "space prohibited before open square bracket '['\n" . $herecurr) &&
4013 $fixed[$fixlinenr] =~
4014 s/^(\+.*?)\s+\[/$1\[/;
4019 # check for spaces between functions and their parentheses.
4020 while ($line =~ /($Ident)\s+\(/g) {
4022 my $ctx_before = substr($line, 0, $-[1]);
4023 my $ctx = "$ctx_before$name";
4025 # Ignore those directives where spaces _are_ permitted.
4027 if|for|while|switch|return|case|
4028 volatile|__volatile__|
4029 __attribute__|format|__extension__|
4032 # cpp #define statements have non-optional spaces, ie
4033 # if there is a space between the name and the open
4034 # parenthesis it is simply not a parameter group.
4035 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4037 # cpp #elif statement condition may start with a (
4038 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4040 # If this whole things ends with a type its most
4041 # likely a typedef for a function.
4042 } elsif ($ctx =~ /$Type$/) {
4044 # All-uppercase function names are usually macros,
4046 } elsif ($name eq uc $name) {
4050 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4052 $fixed[$fixlinenr] =~
4053 s/\b$name\s+\(/$name\(/;
4058 # Check operator spacing.
4059 if (!($line=~/\#\s*include/)) {
4060 my $fixed_line = "";
4064 <<=|>>=|<=|>=|==|!=|
4065 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4066 =>|->|<<|>>|<|>|=|!|~|
4067 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
4070 my @elements = split(/($ops|;)/, $opline);
4072 ## print("element count: <" . $#elements . ">\n");
4073 ## foreach my $el (@elements) {
4074 ## print("el: <$el>\n");
4077 my @fix_elements = ();
4080 foreach my $el (@elements) {
4081 push(@fix_elements, substr($rawline, $off, length($el)));
4082 $off += length($el);
4087 my $blank = copy_spacing($opline);
4088 my $last_after = -1;
4090 for (my $n = 0; $n < $#elements; $n += 2) {
4092 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4094 ## print("n: <$n> good: <$good>\n");
4096 $off += length($elements[$n]);
4098 # Pick up the preceding and succeeding characters.
4099 my $ca = substr($opline, 0, $off);
4101 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4102 $cc = substr($opline, $off + length($elements[$n + 1]));
4104 my $cb = "$ca$;$cc";
4107 $a = 'V' if ($elements[$n] ne '');
4108 $a = 'W' if ($elements[$n] =~ /\s$/);
4109 $a = 'C' if ($elements[$n] =~ /$;$/);
4110 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4111 $a = 'O' if ($elements[$n] eq '');
4112 $a = 'E' if ($ca =~ /^\s*$/);
4114 my $op = $elements[$n + 1];
4117 if (defined $elements[$n + 2]) {
4118 $c = 'V' if ($elements[$n + 2] ne '');
4119 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
4120 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
4121 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4122 $c = 'O' if ($elements[$n + 2] eq '');
4123 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
4128 my $ctx = "${a}x${c}";
4130 my $at = "(ctx:$ctx)";
4132 my $ptr = substr($blank, 0, $off) . "^";
4133 my $hereptr = "$hereline$ptr\n";
4135 # Pull out the value of this operator.
4136 my $op_type = substr($curr_values, $off + 1, 1);
4138 # Get the full operator variant.
4139 my $opv = $op . substr($curr_vars, $off, 1);
4141 # Ignore operators passed as parameters.
4142 if ($op_type ne 'V' &&
4143 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
4146 # } elsif ($op =~ /^$;+$/) {
4148 # ; should have either the end of line or a space or \ after it
4149 } elsif ($op eq ';') {
4150 if ($ctx !~ /.x[WEBC]/ &&
4151 $cc !~ /^\\/ && $cc !~ /^;/) {
4152 if (ERROR("SPACING",
4153 "space required after that '$op' $at\n" . $hereptr)) {
4154 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4160 } elsif ($op eq '//') {
4162 # : when part of a bitfield
4163 } elsif ($opv eq ':B') {
4164 # skip the bitfield test for now
4168 } elsif ($op eq '->') {
4169 if ($ctx =~ /Wx.|.xW/) {
4170 if (ERROR("SPACING",
4171 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4172 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4173 if (defined $fix_elements[$n + 2]) {
4174 $fix_elements[$n + 2] =~ s/^\s+//;
4180 # , must not have a space before and must have a space on the right.
4181 } elsif ($op eq ',') {
4182 my $rtrim_before = 0;
4183 my $space_after = 0;
4184 if ($ctx =~ /Wx./) {
4185 if (ERROR("SPACING",
4186 "space prohibited before that '$op' $at\n" . $hereptr)) {
4191 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
4192 if (ERROR("SPACING",
4193 "space required after that '$op' $at\n" . $hereptr)) {
4199 if ($rtrim_before || $space_after) {
4200 if ($rtrim_before) {
4201 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4203 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4210 # '*' as part of a type definition -- reported already.
4211 } elsif ($opv eq '*_') {
4212 #warn "'*' is part of type\n";
4214 # unary operators should have a space before and
4215 # none after. May be left adjacent to another
4216 # unary operator, or a cast
4217 } elsif ($op eq '!' || $op eq '~' ||
4218 $opv eq '*U' || $opv eq '-U' ||
4219 $opv eq '&U' || $opv eq '&&U') {
4220 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
4221 if (ERROR("SPACING",
4222 "space required before that '$op' $at\n" . $hereptr)) {
4223 if ($n != $last_after + 2) {
4224 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4229 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4230 # A unary '*' may be const
4232 } elsif ($ctx =~ /.xW/) {
4233 if (ERROR("SPACING",
4234 "space prohibited after that '$op' $at\n" . $hereptr)) {
4235 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
4236 if (defined $fix_elements[$n + 2]) {
4237 $fix_elements[$n + 2] =~ s/^\s+//;
4243 # unary ++ and unary -- are allowed no space on one side.
4244 } elsif ($op eq '++' or $op eq '--') {
4245 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
4246 if (ERROR("SPACING",
4247 "space required one side of that '$op' $at\n" . $hereptr)) {
4248 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4252 if ($ctx =~ /Wx[BE]/ ||
4253 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
4254 if (ERROR("SPACING",
4255 "space prohibited before that '$op' $at\n" . $hereptr)) {
4256 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4260 if ($ctx =~ /ExW/) {
4261 if (ERROR("SPACING",
4262 "space prohibited after that '$op' $at\n" . $hereptr)) {
4263 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4264 if (defined $fix_elements[$n + 2]) {
4265 $fix_elements[$n + 2] =~ s/^\s+//;
4271 # << and >> may either have or not have spaces both sides
4272 } elsif ($op eq '<<' or $op eq '>>' or
4273 $op eq '&' or $op eq '^' or $op eq '|' or
4274 $op eq '+' or $op eq '-' or
4275 $op eq '*' or $op eq '/' or
4279 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4281 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4282 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4283 $fix_elements[$n + 2] =~ s/^\s+//;
4286 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4288 "space preferred before that '$op' $at\n" . $hereptr)) {
4289 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4293 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
4294 if (ERROR("SPACING",
4295 "need consistent spacing around '$op' $at\n" . $hereptr)) {
4296 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4297 if (defined $fix_elements[$n + 2]) {
4298 $fix_elements[$n + 2] =~ s/^\s+//;
4304 # A colon needs no spaces before when it is
4305 # terminating a case value or a label.
4306 } elsif ($opv eq ':C' || $opv eq ':L') {
4307 if ($ctx =~ /Wx./) {
4308 if (ERROR("SPACING",
4309 "space prohibited before that '$op' $at\n" . $hereptr)) {
4310 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4315 # All the others need spaces both sides.
4316 } elsif ($ctx !~ /[EWC]x[CWE]/) {
4319 # Ignore email addresses <foo@bar>
4321 $cc =~ /^\S+\@\S+>/) ||
4323 $ca =~ /<\S+\@\S+$/))
4328 # for asm volatile statements
4329 # ignore a colon with another
4330 # colon immediately before or after
4332 ($ca =~ /:$/ || $cc =~ /^:/)) {
4336 # messages are ERROR, but ?: are CHK
4338 my $msg_level = \&ERROR;
4339 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4341 if (&{$msg_level}("SPACING",
4342 "spaces required around that '$op' $at\n" . $hereptr)) {
4343 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4344 if (defined $fix_elements[$n + 2]) {
4345 $fix_elements[$n + 2] =~ s/^\s+//;
4351 $off += length($elements[$n + 1]);
4353 ## print("n: <$n> GOOD: <$good>\n");
4355 $fixed_line = $fixed_line . $good;
4358 if (($#elements % 2) == 0) {
4359 $fixed_line = $fixed_line . $fix_elements[$#elements];
4362 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4363 $fixed[$fixlinenr] = $fixed_line;
4369 # check for whitespace before a non-naked semicolon
4370 if ($line =~ /^\+.*\S\s+;\s*$/) {
4372 "space prohibited before semicolon\n" . $herecurr) &&
4374 1 while $fixed[$fixlinenr] =~
4375 s/^(\+.*\S)\s+;/$1;/;
4379 # check for multiple assignments
4380 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4381 CHK("MULTIPLE_ASSIGNMENTS",
4382 "multiple assignments should be avoided\n" . $herecurr);
4385 ## # check for multiple declarations, allowing for a function declaration
4387 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4388 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4390 ## # Remove any bracketed sections to ensure we do not
4391 ## # falsly report the parameters of functions.
4393 ## while ($ln =~ s/\([^\(\)]*\)//g) {
4395 ## if ($ln =~ /,/) {
4396 ## WARN("MULTIPLE_DECLARATION",
4397 ## "declaring multiple variables together should be avoided\n" . $herecurr);
4401 #need space before brace following if, while, etc
4402 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
4404 if (ERROR("SPACING",
4405 "space required before the open brace '{'\n" . $herecurr) &&
4407 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/;
4411 ## # check for blank lines before declarations
4412 ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4413 ## $prevrawline =~ /^.\s*$/) {
4415 ## "No blank lines before declarations\n" . $hereprev);
4419 # closing brace should have a space following it when it has anything
4421 if ($line =~ /}(?!(?:,|;|\)))\S/) {
4422 if (ERROR("SPACING",
4423 "space required after that close brace '}'\n" . $herecurr) &&
4425 $fixed[$fixlinenr] =~
4426 s/}((?!(?:,|;|\)))\S)/} $1/;
4430 # check spacing on square brackets
4431 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
4432 if (ERROR("SPACING",
4433 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4435 $fixed[$fixlinenr] =~
4439 if ($line =~ /\s\]/) {
4440 if (ERROR("SPACING",
4441 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4443 $fixed[$fixlinenr] =~
4448 # check spacing on parentheses
4449 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4450 $line !~ /for\s*\(\s+;/) {
4451 if (ERROR("SPACING",
4452 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4454 $fixed[$fixlinenr] =~
4458 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4459 $line !~ /for\s*\(.*;\s+\)/ &&
4460 $line !~ /:\s+\)/) {
4461 if (ERROR("SPACING",
4462 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4464 $fixed[$fixlinenr] =~
4469 # check unnecessary parentheses around addressof/dereference single $Lvals
4470 # ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4472 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4474 if (CHK("UNNECESSARY_PARENTHESES",
4475 "Unnecessary parentheses around $var\n" . $herecurr) &&
4477 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4481 # check for unnecessary parentheses around function pointer uses
4482 # ie: (foo->bar)(); should be foo->bar();
4483 # but not "if (foo->bar) (" to avoid some false positives
4484 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4486 if (CHK("UNNECESSARY_PARENTHESES",
4487 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4489 my $var2 = deparenthesize($var);
4491 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4495 # check for unnecessary parentheses around comparisons in if uses
4496 if ($^V && $^V ge 5.10.0 && defined($stat) &&
4497 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4499 my $test = substr($2, 1, -1);
4501 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4503 # avoid parentheses around potential macro args
4504 next if ($match =~ /^\s*\w+\s*$/);
4505 if (!defined($herectx)) {
4506 $herectx = $here . "\n";
4507 my $cnt = statement_rawlines($if_stat);
4508 for (my $n = 0; $n < $cnt; $n++) {
4509 my $rl = raw_line($linenr, $n);
4510 $herectx .= $rl . "\n";
4511 last if $rl =~ /^[ \+].*\{/;
4514 CHK("UNNECESSARY_PARENTHESES",
4515 "Unnecessary parentheses around '$match'\n" . $herectx);
4519 #goto labels aren't indented, allow a single space however
4520 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
4521 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
4522 if (WARN("INDENTED_LABEL",
4523 "labels should not be indented\n" . $herecurr) &&
4525 $fixed[$fixlinenr] =~
4530 # return is not a function
4531 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4533 if ($^V && $^V ge 5.10.0 &&
4534 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4536 $value = deparenthesize($value);
4537 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4538 ERROR("RETURN_PARENTHESES",
4539 "return is not a function, parentheses are not required\n" . $herecurr);
4541 } elsif ($spacing !~ /\s+/) {
4543 "space required before the open parenthesis '('\n" . $herecurr);
4547 # unnecessary return in a void function
4548 # at end-of-function, with the previous line a single leading tab, then return;
4549 # and the line before that not a goto label target like "out:"
4550 if ($sline =~ /^[ \+]}\s*$/ &&
4551 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4553 $lines[$linenr - 3] =~ /^[ +]/ &&
4554 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
4556 "void function return statements are not generally useful\n" . $hereprev);
4559 # if statements using unnecessary parentheses - ie: if ((foo == bar))
4560 if ($^V && $^V ge 5.10.0 &&
4561 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4562 my $openparens = $1;
4563 my $count = $openparens =~ tr@\(@\(@;
4565 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4566 my $comp = $4; #Not $1 because of $LvalOrFunc
4567 $msg = " - maybe == should be = ?" if ($comp eq "==");
4568 WARN("UNNECESSARY_PARENTHESES",
4569 "Unnecessary parentheses$msg\n" . $herecurr);
4573 # comparisons with a constant or upper case identifier on the left
4574 # avoid cases like "foo + BAR < baz"
4575 # only fix matches surrounded by parentheses to avoid incorrect
4576 # conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4577 if ($^V && $^V ge 5.10.0 &&
4578 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4583 my $newcomp = $comp;
4584 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4585 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4586 WARN("CONSTANT_COMPARISON",
4587 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4591 } elsif ($comp eq "<=") {
4593 } elsif ($comp eq ">") {
4595 } elsif ($comp eq ">=") {
4598 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4602 # Return of what appears to be an errno should normally be negative
4603 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
4605 if ($name ne 'EOF' && $name ne 'ERROR') {
4606 WARN("USE_NEGATIVE_ERRNO",
4607 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
4611 # Need a space before open parenthesis after if, while etc
4612 if ($line =~ /\b(if|while|for|switch)\(/) {
4613 if (ERROR("SPACING",
4614 "space required before the open parenthesis '('\n" . $herecurr) &&
4616 $fixed[$fixlinenr] =~
4617 s/\b(if|while|for|switch)\(/$1 \(/;
4621 # Check for illegal assignment in if conditional -- and check for trailing
4622 # statements after the conditional.
4623 if ($line =~ /do\s*(?!{)/) {
4624 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4625 ctx_statement_block($linenr, $realcnt, 0)
4626 if (!defined $stat);
4627 my ($stat_next) = ctx_statement_block($line_nr_next,
4628 $remain_next, $off_next);
4629 $stat_next =~ s/\n./\n /g;
4630 ##print "stat<$stat> stat_next<$stat_next>\n";
4632 if ($stat_next =~ /^\s*while\b/) {
4633 # If the statement carries leading newlines,
4634 # then count those as offsets.
4636 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4638 statement_rawlines($whitespace) - 1;
4640 $suppress_whiletrailers{$line_nr_next +
4644 if (!defined $suppress_whiletrailers{$linenr} &&
4645 defined($stat) && defined($cond) &&
4646 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4647 my ($s, $c) = ($stat, $cond);
4649 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4650 ERROR("ASSIGN_IN_IF",
4651 "do not use assignment in if condition\n" . $herecurr);
4654 # Find out what is on the end of the line after the
4656 substr($s, 0, length($c), '');
4658 $s =~ s/$;//g; # Remove any comments
4659 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4660 $c !~ /}\s*while\s*/)
4662 # Find out how long the conditional actually is.
4663 my @newlines = ($c =~ /\n/gs);
4664 my $cond_lines = 1 + $#newlines;
4667 $stat_real = raw_line($linenr, $cond_lines)
4668 . "\n" if ($cond_lines);
4669 if (defined($stat_real) && $cond_lines > 1) {
4670 $stat_real = "[...]\n$stat_real";
4673 ERROR("TRAILING_STATEMENTS",
4674 "trailing statements should be on next line\n" . $herecurr . $stat_real);
4678 # Check for bitwise tests written as boolean
4690 WARN("HEXADECIMAL_BOOLEAN_TEST",
4691 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
4694 # if and else should not have general statements after it
4695 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4697 $s =~ s/$;//g; # Remove any comments
4698 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4699 ERROR("TRAILING_STATEMENTS",
4700 "trailing statements should be on next line\n" . $herecurr);
4703 # if should not continue a brace
4704 if ($line =~ /}\s*if\b/) {
4705 ERROR("TRAILING_STATEMENTS",
4706 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
4709 # case and default should not have general statements after them
4710 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4712 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4716 ERROR("TRAILING_STATEMENTS",
4717 "trailing statements should be on next line\n" . $herecurr);
4720 # Check for }<nl>else {, these must be at the same
4721 # indent level to be relevant to each other.
4722 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4723 $previndent == $indent) {
4724 if (ERROR("ELSE_AFTER_BRACE",
4725 "else should follow close brace '}'\n" . $hereprev) &&
4726 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4727 fix_delete_line($fixlinenr - 1, $prevrawline);
4728 fix_delete_line($fixlinenr, $rawline);
4729 my $fixedline = $prevrawline;
4730 $fixedline =~ s/}\s*$//;
4731 if ($fixedline !~ /^\+\s*$/) {
4732 fix_insert_line($fixlinenr, $fixedline);
4734 $fixedline = $rawline;
4735 $fixedline =~ s/^(.\s*)else/$1} else/;
4736 fix_insert_line($fixlinenr, $fixedline);
4740 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4741 $previndent == $indent) {
4742 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4744 # Find out what is on the end of the line after the
4746 substr($s, 0, length($c), '');
4749 if ($s =~ /^\s*;/) {
4750 if (ERROR("WHILE_AFTER_BRACE",
4751 "while should follow close brace '}'\n" . $hereprev) &&
4752 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4753 fix_delete_line($fixlinenr - 1, $prevrawline);
4754 fix_delete_line($fixlinenr, $rawline);
4755 my $fixedline = $prevrawline;
4756 my $trailing = $rawline;
4757 $trailing =~ s/^\+//;
4758 $trailing = trim($trailing);
4759 $fixedline =~ s/}\s*$/} $trailing/;
4760 fix_insert_line($fixlinenr, $fixedline);
4765 #Specific variable tests
4766 while ($line =~ m{($Constant|$Lval)}g) {
4769 #gcc binary extension
4770 if ($var =~ /^$Binary$/) {
4771 if (WARN("GCC_BINARY_CONSTANT",
4772 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4774 my $hexval = sprintf("0x%x", oct($var));
4775 $fixed[$fixlinenr] =~
4776 s/\b$var\b/$hexval/;
4781 if ($var !~ /^$Constant$/ &&
4782 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
4783 #Ignore Page<foo> variants
4784 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
4785 #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4786 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4787 #Ignore some three character SI units explicitly, like MiB and KHz
4788 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
4789 while ($var =~ m{($Ident)}g) {
4791 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4793 seed_camelcase_includes();
4794 if (!$file && !$camelcase_file_seeded) {
4795 seed_camelcase_file($realfile);
4796 $camelcase_file_seeded = 1;
4799 if (!defined $camelcase{$word}) {
4800 $camelcase{$word} = 1;
4802 "Avoid CamelCase: <$word>\n" . $herecurr);
4808 #no spaces allowed after \ in define
4809 if ($line =~ /\#\s*define.*\\\s+$/) {
4810 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4811 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4813 $fixed[$fixlinenr] =~ s/\s+$//;
4817 # warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4818 # itself <asm/foo.h> (uses RAW line)
4819 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4821 my $checkfile = "include/linux/$file";
4822 if (-f "$root/$checkfile" &&
4823 $realfile ne $checkfile &&
4824 $1 !~ /$allowed_asm_includes/)
4826 my $asminclude = `grep -Ec
"#include\\s+<asm/$file>" $root/$checkfile`;
4827 if ($asminclude > 0) {
4828 if ($realfile =~ m{^arch/}) {
4829 CHK
("ARCH_INCLUDE_LINUX",
4830 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4832 WARN
("INCLUDE_LINUX",
4833 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4839 # multi-statement macros should be enclosed in a do while loop, grab the
4840 # first statement and ensure its the whole macro if its not enclosed
4841 # in a known good container
4842 if ($realfile !~ m
@/vmlinux.lds
.h
$@ &&
4843 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4846 my ($off, $dstat, $dcond, $rest);
4848 my $has_flow_statement = 0;
4849 my $has_arg_concat = 0;
4850 ($dstat, $dcond, $ln, $cnt, $off) =
4851 ctx_statement_block
($linenr, $realcnt, 0);
4853 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4854 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4856 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4857 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
4859 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4860 my $define_args = $1;
4861 my $define_stmt = $dstat;
4864 if (defined $define_args && $define_args ne "") {
4865 $define_args = substr($define_args, 1, length($define_args) - 2);
4866 $define_args =~ s/\s*//g;
4867 @def_args = split(",", $define_args);
4871 $dstat =~ s/\\\n.//g;
4872 $dstat =~ s/^\s*//s;
4873 $dstat =~ s/\s*$//s;
4875 # Flatten any parentheses and braces
4876 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4877 $dstat =~ s/\{[^\{\}]*\}/1/ ||
4878 $dstat =~ s/.\[[^\[\]]*\]/1/)
4882 # Flatten any obvious string concatentation.
4883 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4884 $dstat =~ s/$Ident\s*($String)/$1/)
4888 # Make asm volatile uses seem like a generic function
4889 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4891 my $exceptions = qr{
4904 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4907 my $herectx = $here . "\n";
4908 my $stmt_cnt = statement_rawlines
($ctx);
4910 for (my $n = 0; $n < $stmt_cnt; $n++) {
4911 $herectx .= raw_line
($linenr, $n) . "\n";
4915 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4916 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
4917 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4918 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
4919 $dstat !~ /$exceptions/ &&
4920 $dstat !~ /^\.$Ident\s*=/ && # .foo =
4921 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
4922 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
4923 $dstat !~ /^for\s*$Constant$/ && # for (...)
4924 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4925 $dstat !~ /^do\s*{/ && # do {...
4926 $dstat !~ /^\(\{/ && # ({...
4927 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4929 if ($dstat =~ /^\s*if\b/) {
4930 ERROR
("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4931 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
4932 } elsif ($dstat =~ /;/) {
4933 ERROR
("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4934 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4936 ERROR
("COMPLEX_MACRO",
4937 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4942 # Make $define_stmt single line, comment-free, etc
4943 my @stmt_array = split('\n', $define_stmt);
4946 foreach my $l (@stmt_array) {
4951 } elsif ($l =~ /^[\+ ]/) {
4952 $define_stmt .= substr($l, 1);
4955 $define_stmt =~ s/$;//g;
4956 $define_stmt =~ s/\s+/ /g;
4957 $define_stmt = trim
($define_stmt);
4959 # check if any macro arguments are reused (ignore '...' and 'type')
4960 foreach my $arg (@def_args) {
4961 next if ($arg =~ /\.\.\./);
4962 next if ($arg =~ /^type$/i);
4963 my $tmp_stmt = $define_stmt;
4964 $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
4965 $tmp_stmt =~ s/\#+\s*$arg\b//g;
4966 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
4967 my $use_cnt = $tmp_stmt =~ s/\b$arg\b//g;
4969 CHK
("MACRO_ARG_REUSE",
4970 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
4972 # check if any macro arguments may have other precedence issues
4973 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
4974 ((defined($1) && $1 ne ',') ||
4975 (defined($2) && $2 ne ','))) {
4976 CHK
("MACRO_ARG_PRECEDENCE",
4977 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
4981 # check for macros with flow control, but without ## concatenation
4982 # ## concatenation is commonly a macro that defines a function so ignore those
4983 if ($has_flow_statement && !$has_arg_concat) {
4984 my $herectx = $here . "\n";
4985 my $cnt = statement_rawlines
($ctx);
4987 for (my $n = 0; $n < $cnt; $n++) {
4988 $herectx .= raw_line
($linenr, $n) . "\n";
4990 WARN
("MACRO_WITH_FLOW_CONTROL",
4991 "Macros with flow control statements should be avoided\n" . "$herectx");
4994 # check for line continuations outside of #defines, preprocessor #, and asm
4997 if ($prevline !~ /^..*\\$/ &&
4998 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4999 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
5000 $line =~ /^\+.*\\$/) {
5001 WARN
("LINE_CONTINUATIONS",
5002 "Avoid unnecessary line continuations\n" . $herecurr);
5006 # do {} while (0) macro tests:
5007 # single-statement macros do not need to be enclosed in do while (0) loop,
5008 # macro should not end with a semicolon
5009 if ($^V && $^V ge 5.10.0 &&
5010 $realfile !~ m
@/vmlinux.lds
.h
$@ &&
5011 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5014 my ($off, $dstat, $dcond, $rest);
5016 ($dstat, $dcond, $ln, $cnt, $off) =
5017 ctx_statement_block
($linenr, $realcnt, 0);
5020 $dstat =~ s/\\\n.//g;
5023 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5028 my $cnt = statement_rawlines
($ctx);
5029 my $herectx = $here . "\n";
5031 for (my $n = 0; $n < $cnt; $n++) {
5032 $herectx .= raw_line
($linenr, $n) . "\n";
5035 if (($stmts =~ tr/;/;/) == 1 &&
5036 $stmts !~ /^\s*(if|while|for|switch)\b/) {
5037 WARN
("SINGLE_STATEMENT_DO_WHILE_MACRO",
5038 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5040 if (defined $semis && $semis ne "") {
5041 WARN
("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5042 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5044 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5046 my $cnt = statement_rawlines
($ctx);
5047 my $herectx = $here . "\n";
5049 for (my $n = 0; $n < $cnt; $n++) {
5050 $herectx .= raw_line
($linenr, $n) . "\n";
5053 WARN
("TRAILING_SEMICOLON",
5054 "macros should not use a trailing semicolon\n" . "$herectx");
5058 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
5059 # all assignments may have only one of the following with an assignment:
5062 # VMLINUX_SYMBOL(...)
5063 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
5064 WARN
("MISSING_VMLINUX_SYMBOL",
5065 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
5068 # check for redundant bracing round if etc
5069 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5070 my ($level, $endln, @chunks) =
5071 ctx_statement_full
($linenr, $realcnt, 1);
5072 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5073 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5074 if ($#chunks > 0 && $level == 0) {
5078 my $herectx = $here . "\n";
5079 my $ln = $linenr - 1;
5080 for my $chunk (@chunks) {
5081 my ($cond, $block) = @{$chunk};
5083 # If the condition carries leading newlines, then count those as offsets.
5084 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5085 my $offset = statement_rawlines
($whitespace) - 1;
5087 $allowed[$allow] = 0;
5088 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5090 # We have looked at and allowed this specific line.
5091 $suppress_ifbraces{$ln + $offset} = 1;
5093 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5094 $ln += statement_rawlines
($block) - 1;
5096 substr($block, 0, length($cond), '');
5098 $seen++ if ($block =~ /^\s*{/);
5100 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5101 if (statement_lines
($cond) > 1) {
5102 #print "APW: ALLOWED: cond<$cond>\n";
5103 $allowed[$allow] = 1;
5105 if ($block =~/\b(?:if|for|while)\b/) {
5106 #print "APW: ALLOWED: block<$block>\n";
5107 $allowed[$allow] = 1;
5109 if (statement_block_size
($block) > 1) {
5110 #print "APW: ALLOWED: lines block<$block>\n";
5111 $allowed[$allow] = 1;
5116 my $sum_allowed = 0;
5117 foreach (@allowed) {
5120 if ($sum_allowed == 0) {
5122 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5123 } elsif ($sum_allowed != $allow &&
5126 "braces {} should be used on all arms of this statement\n" . $herectx);
5131 if (!defined $suppress_ifbraces{$linenr - 1} &&
5132 $line =~ /\b(if|while|for|else)\b/) {
5135 # Check the pre-context.
5136 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5137 #print "APW: ALLOWED: pre<$1>\n";
5141 my ($level, $endln, @chunks) =
5142 ctx_statement_full
($linenr, $realcnt, $-[0]);
5144 # Check the condition.
5145 my ($cond, $block) = @{$chunks[0]};
5146 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5147 if (defined $cond) {
5148 substr($block, 0, length($cond), '');
5150 if (statement_lines
($cond) > 1) {
5151 #print "APW: ALLOWED: cond<$cond>\n";
5154 if ($block =~/\b(?:if|for|while)\b/) {
5155 #print "APW: ALLOWED: block<$block>\n";
5158 if (statement_block_size
($block) > 1) {
5159 #print "APW: ALLOWED: lines block<$block>\n";
5162 # Check the post-context.
5163 if (defined $chunks[1]) {
5164 my ($cond, $block) = @{$chunks[1]};
5165 if (defined $cond) {
5166 substr($block, 0, length($cond), '');
5168 if ($block =~ /^\s*\{/) {
5169 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5173 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5174 my $herectx = $here . "\n";
5175 my $cnt = statement_rawlines
($block);
5177 for (my $n = 0; $n < $cnt; $n++) {
5178 $herectx .= raw_line
($linenr, $n) . "\n";
5182 "braces {} are not necessary for single statement blocks\n" . $herectx);
5186 # check for single line unbalanced braces
5187 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5188 $sline =~ /^.\s*else\s*\{\s*$/) {
5189 CHK
("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5192 # check for unnecessary blank lines around braces
5193 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5195 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5196 $fix && $prevrawline =~ /^\+/) {
5197 fix_delete_line
($fixlinenr - 1, $prevrawline);
5200 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5202 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5204 fix_delete_line
($fixlinenr, $rawline);
5208 # no volatiles please
5209 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5210 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5212 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
5215 # Check for user-visible strings broken across lines, which breaks the ability
5216 # to grep for the string. Make exceptions when the previous string ends in a
5217 # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5218 # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
5219 if ($line =~ /^\+\s*$String/ &&
5220 $prevline =~ /"\s*$/ &&
5221 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5222 if (WARN
("SPLIT_STRING",
5223 "quoted string split across lines\n" . $hereprev) &&
5225 $prevrawline =~ /^\+.*"\s*$/ &&
5226 $last_coalesced_string_linenr != $linenr - 1) {
5227 my $extracted_string = get_quoted_string
($line, $rawline);
5228 my $comma_close = "";
5229 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5233 fix_delete_line
($fixlinenr - 1, $prevrawline);
5234 fix_delete_line
($fixlinenr, $rawline);
5235 my $fixedline = $prevrawline;
5236 $fixedline =~ s/"\s*$//;
5237 $fixedline .= substr($extracted_string, 1) . trim
($comma_close);
5238 fix_insert_line
($fixlinenr - 1, $fixedline);
5239 $fixedline = $rawline;
5240 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5241 if ($fixedline !~ /\+\s*$/) {
5242 fix_insert_line
($fixlinenr, $fixedline);
5244 $last_coalesced_string_linenr = $linenr;
5248 # check for missing a space in a string concatenation
5249 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5250 WARN
('MISSING_SPACE',
5251 "break quoted strings at a space character\n" . $hereprev);
5254 # check for an embedded function name in a string when the function is known
5255 # This does not work very well for -f --file checking as it depends on patch
5256 # context providing the function name or a single line form for in-file
5257 # function declarations
5258 if ($line =~ /^\+.*$String/ &&
5259 defined($context_function) &&
5260 get_quoted_string
($line, $rawline) =~ /\b$context_function\b/ &&
5261 length(get_quoted_string
($line, $rawline)) != (length($context_function) + 2)) {
5262 WARN
("EMBEDDED_FUNCTION_NAME",
5263 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
5266 # check for spaces before a quoted newline
5267 if ($rawline =~ /^.*\".*\s\\n/) {
5268 if (WARN
("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5269 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5271 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5276 # concatenated string without spaces between elements
5277 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5278 CHK
("CONCATENATED_STRING",
5279 "Concatenated strings should use spaces between elements\n" . $herecurr);
5282 # uncoalesced string fragments
5283 if ($line =~ /$String\s*"/) {
5284 WARN
("STRING_FRAGMENTS",
5285 "Consecutive strings are generally better as a single string\n" . $herecurr);
5288 # check for non-standard and hex prefixed decimal printf formats
5289 my $show_L = 1; #don't show the same defect twice
5291 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5292 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
5293 $string =~ s/%%/__/g;
5295 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
5297 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5301 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5303 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5306 # check for 0x<decimal>
5307 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5308 ERROR
("PRINTF_0XDECIMAL",
5309 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5313 # check for line continuations in quoted strings with odd counts of "
5314 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
5315 WARN
("LINE_CONTINUATIONS",
5316 "Avoid line continuations in quoted strings\n" . $herecurr);
5320 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
5321 CHK
("REDUNDANT_CODE",
5322 "if this code is redundant consider removing it\n" .
5326 # check for needless "if (<foo>) fn(<foo>)" uses
5327 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5328 my $tested = quotemeta($1);
5329 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5330 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5332 if (WARN
('NEEDLESS_IF',
5333 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5336 my $leading_tabs = "";
5337 my $new_leading_tabs = "";
5338 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5343 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5344 $new_leading_tabs = $1;
5345 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5352 fix_delete_line
($fixlinenr - 1, $prevrawline);
5353 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5359 # check for unnecessary "Out of Memory" messages
5360 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5361 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5362 (defined $1 || defined $3) &&
5365 my $testline = $lines[$linenr - 3];
5367 my ($s, $c) = ctx_statement_block
($linenr - 3, $realcnt, 0);
5368 # print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5370 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|kmemdup|(?:dev_)?alloc_skb)/) {
5372 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5376 # check for logging functions with KERN_<LEVEL>
5377 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5378 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5380 if (WARN
("UNNECESSARY_KERN_LEVEL",
5381 "Possible unnecessary $level\n" . $herecurr) &&
5383 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5387 # check for logging continuations
5388 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5389 WARN
("LOGGING_CONTINUATION",
5390 "Avoid logging continuation uses where feasible\n" . $herecurr);
5393 # check for mask then right shift without a parentheses
5394 if ($^V && $^V ge 5.10.0 &&
5395 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5396 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5397 WARN
("MASK_THEN_SHIFT",
5398 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5401 # check for pointer comparisons to NULL
5402 if ($^V && $^V ge 5.10.0) {
5403 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5406 $equal = "" if ($4 eq "!=");
5407 if (CHK
("COMPARISON_TO_NULL",
5408 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5410 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5415 # check for bad placement of section $InitAttribute (e.g.: __initdata)
5416 if ($line =~ /(\b$InitAttribute\b)/) {
5418 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5421 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5422 ERROR
("MISPLACED_INIT",
5423 "$attr should be placed after $var\n" . $herecurr)) ||
5424 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5425 WARN
("MISPLACED_INIT",
5426 "$attr should be placed after $var\n" . $herecurr))) &&
5428 $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;
5433 # check for $InitAttributeData (ie: __initdata) with const
5434 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5436 $attr =~ /($InitAttributePrefix)(.*)/;
5437 my $attr_prefix = $1;
5439 if (ERROR
("INIT_ATTRIBUTE",
5440 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5442 $fixed[$fixlinenr] =~
5443 s/$InitAttributeData/${attr_prefix}initconst/;
5447 # check for $InitAttributeConst (ie: __initconst) without const
5448 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5450 if (ERROR
("INIT_ATTRIBUTE",
5451 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5453 my $lead = $fixed[$fixlinenr] =~
5454 /(^\+\s*(?:static\s+))/;
5456 $lead = "$lead " if ($lead !~ /^\+$/);
5457 $lead = "${lead}const ";
5458 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5462 # check for __read_mostly with const non-pointer (should just be const)
5463 if ($line =~ /\b__read_mostly\b/ &&
5464 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5465 if (ERROR
("CONST_READ_MOSTLY",
5466 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5468 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5472 # don't use __constant_<foo> functions outside of include/uapi/
5473 if ($realfile !~ m
@^include/uapi/@ &&
5474 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5475 my $constant_func = $1;
5476 my $func = $constant_func;
5477 $func =~ s/^__constant_//;
5478 if (WARN
("CONSTANT_CONVERSION",
5479 "$constant_func should be $func\n" . $herecurr) &&
5481 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5485 # prefer usleep_range over udelay
5486 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
5488 # ignore udelay's < 10, however
5489 if (! ($delay < 10) ) {
5491 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5493 if ($delay > 2000) {
5495 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
5499 # warn about unexpectedly long msleep's
5500 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5503 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5507 # check for comparisons of jiffies
5508 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5509 WARN
("JIFFIES_COMPARISON",
5510 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5513 # check for comparisons of get_jiffies_64()
5514 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5515 WARN
("JIFFIES_COMPARISON",
5516 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5519 # warn about #ifdefs in C files
5520 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
5521 # print "#ifdef in C files should be avoided\n";
5522 # print "$herecurr";
5526 # warn about spacing in #ifdefs
5527 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
5528 if (ERROR
("SPACING",
5529 "exactly one space required after that #$1\n" . $herecurr) &&
5531 $fixed[$fixlinenr] =~
5532 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5537 # check for spinlock_t definitions without a comment.
5538 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5539 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
5541 if (!ctx_has_comment
($first_line, $linenr)) {
5542 CHK
("UNCOMMENTED_DEFINITION",
5543 "$1 definition without comment\n" . $herecurr);
5546 # check for memory barriers without a comment.
5552 read_barrier_depends
5554 my $barrier_stems = qr{
5562 my $all_barriers = qr{
5564 smp_
(?
:$barrier_stems)|
5565 virt_
(?
:$barrier_stems)
5568 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
5569 if (!ctx_has_comment
($first_line, $linenr)) {
5570 WARN
("MEMORY_BARRIER",
5571 "memory barrier without comment\n" . $herecurr);
5575 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5577 if ($realfile !~ m
@^include/asm-generic
/@ &&
5578 $realfile !~ m
@/barrier\.h
$@ &&
5579 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5580 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5581 WARN
("MEMORY_BARRIER",
5582 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5585 # check for waitqueue_active without a comment.
5586 if ($line =~ /\bwaitqueue_active\s*\(/) {
5587 if (!ctx_has_comment
($first_line, $linenr)) {
5588 WARN
("WAITQUEUE_ACTIVE",
5589 "waitqueue_active without comment\n" . $herecurr);
5593 # check of hardware specific defines
5594 if ($line =~ m
@^.\s
*\#\s
*if.*\b(__i386__
|__powerpc64__
|__sun__
|__s390x__
)\b@ && $realfile !~ m
@include/asm-@) {
5596 "architecture specific defines should be avoided\n" . $herecurr);
5599 # check that the storage class is not after a type
5600 if ($line =~ /\b($Type)\s+($Storage)\b/) {
5601 WARN
("STORAGE_CLASS",
5602 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5604 # Check that the storage class is at the beginning of a declaration
5605 if ($line =~ /\b$Storage\b/ &&
5606 $line !~ /^.\s*$Storage/ &&
5607 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5608 $1 !~ /[\,\)]\s*$/) {
5609 WARN
("STORAGE_CLASS",
5610 "storage class should be at the beginning of the declaration\n" . $herecurr);
5613 # check the location of the inline attribute, that it is between
5614 # storage class and type.
5615 if ($line =~ /\b$Type\s+$Inline\b/ ||
5616 $line =~ /\b$Inline\s+$Storage\b/) {
5617 ERROR
("INLINE_LOCATION",
5618 "inline keyword should sit between storage class and type\n" . $herecurr);
5621 # Check for __inline__ and __inline, prefer inline
5622 if ($realfile !~ m
@\binclude/uapi/@ &&
5623 $line =~ /\b(__inline__|__inline)\b/) {
5625 "plain inline is preferred over $1\n" . $herecurr) &&
5627 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5633 # Kernel macros unnused for FRR
5635 # Check for __attribute__ packed, prefer __packed
5636 # if ($realfile !~ m@\binclude/uapi/@ &&
5637 # $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5638 # WARN("PREFER_PACKED",
5639 # "__packed is preferred over __attribute__((packed))\n" . $herecurr);
5642 # Check for __attribute__ aligned, prefer __aligned
5643 # if ($realfile !~ m@\binclude/uapi/@ &&
5644 # $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5645 # WARN("PREFER_ALIGNED",
5646 # "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
5649 # Check for __attribute__ format(printf, prefer __printf
5650 # if ($realfile !~ m@\binclude/uapi/@ &&
5651 # $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5652 # if (WARN("PREFER_PRINTF",
5653 # "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5655 # $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5660 # Check for __attribute__ format(scanf, prefer __scanf
5661 # if ($realfile !~ m@\binclude/uapi/@ &&
5662 # $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5663 # if (WARN("PREFER_SCANF",
5664 # "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5666 # $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5670 # Check for __attribute__ weak, or __weak declarations (may have link issues)
5671 # if ($^V && $^V ge 5.10.0 &&
5672 # $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5673 # ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5674 # $line =~ /\b__weak\b/)) {
5675 # ERROR("WEAK_DECLARATION",
5676 # "Using weak declarations can have unintended link defects\n" . $herecurr);
5679 # check for c99 types like uint8_t used outside of uapi/ and tools/
5680 if ($realfile !~ m
@\binclude/uapi/@ &&
5681 $realfile !~ m
@\btools/@ &&
5682 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5684 if ($type =~ /\b($typeC99Typedefs)\b/) {
5686 my $kernel_type = 'u';
5687 $kernel_type = 's' if ($type =~ /^_*[si]/);
5690 if (CHK
("PREFER_KERNEL_TYPES",
5691 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5693 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5698 # check for cast of C90 native int or longer types constants
5699 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5702 if (WARN
("TYPECAST_INT_CONSTANT",
5703 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5706 my $newconst = $const;
5707 $newconst =~ s/${Int_type}$//;
5708 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5709 if ($cast =~ /\blong\s+long\b/) {
5711 } elsif ($cast =~ /\blong\b/) {
5714 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5718 # check for sizeof(&)
5719 if ($line =~ /\bsizeof\s*\(\s*\&/) {
5720 WARN
("SIZEOF_ADDRESS",
5721 "sizeof(& should be avoided\n" . $herecurr);
5724 # check for sizeof without parenthesis
5725 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5726 if (WARN
("SIZEOF_PARENTHESIS",
5727 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5729 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5733 # check for struct spinlock declarations
5734 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5735 WARN
("USE_SPINLOCK_T",
5736 "struct spinlock should be spinlock_t\n" . $herecurr);
5739 # check for seq_printf uses that could be seq_puts
5740 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5741 my $fmt = get_quoted_string
($line, $rawline);
5744 if (WARN
("PREFER_SEQ_PUTS",
5745 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5747 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5752 # check for vsprintf extension %p<foo> misuses
5753 if ($^V && $^V ge 5.10.0 &&
5755 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5756 $1 !~ /^_*volatile_*$/) {
5757 my $bad_extension = "";
5758 my $lc = $stat =~ tr
@\n@@;
5759 $lc = $lc + $linenr;
5760 for (my $count = $linenr; $count <= $lc; $count++) {
5761 my $fmt = get_quoted_string
($lines[$count - 1], raw_line
($count, 0));
5763 if ($fmt =~ /(\%[\*\d\.]*p(?![\WFfSsBKRraEhMmIiUDdgVCbGNOx]).)/) {
5764 $bad_extension = $1;
5768 if ($bad_extension ne "") {
5769 my $stat_real = raw_line
($linenr, 0);
5770 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5771 $stat_real = $stat_real . "\n" . raw_line
($count, 0);
5773 WARN
("VSPRINTF_POINTER_EXTENSION",
5774 "Invalid vsprintf pointer extension '$bad_extension'\n" . "$here\n$stat_real\n");
5778 # Check for misused memsets
5779 if ($^V && $^V ge 5.10.0 &&
5781 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
5787 if ($ms_size =~ /^(0x|)0$/i) {
5789 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5790 } elsif ($ms_size =~ /^(0x|)1$/i) {
5792 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5796 # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5797 # if ($^V && $^V ge 5.10.0 &&
5799 # $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5800 # if (WARN("PREFER_ETHER_ADDR_COPY",
5801 # "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5803 # $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5807 # Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5808 # if ($^V && $^V ge 5.10.0 &&
5810 # $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5811 # WARN("PREFER_ETHER_ADDR_EQUAL",
5812 # "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5815 # check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5816 # check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
5817 # if ($^V && $^V ge 5.10.0 &&
5819 # $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5823 # if ($ms_val =~ /^(?:0x|)0+$/i) {
5824 # if (WARN("PREFER_ETH_ZERO_ADDR",
5825 # "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5827 # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5829 # } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5830 # if (WARN("PREFER_ETH_BROADCAST_ADDR",
5831 # "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5833 # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5838 # typecasts on min/max could be min_t/max_t
5839 if ($^V && $^V ge 5.10.0 &&
5841 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
5842 if (defined $2 || defined $7) {
5844 my $cast1 = deparenthesize
($2);
5846 my $cast2 = deparenthesize
($7);
5850 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
5851 $cast = "$cast1 or $cast2";
5852 } elsif ($cast1 ne "") {
5858 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
5862 # check usleep_range arguments
5863 if ($^V && $^V ge 5.10.0 &&
5865 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5869 WARN
("USLEEP_RANGE",
5870 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5871 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5873 WARN
("USLEEP_RANGE",
5874 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5878 # check for naked sscanf
5879 if ($^V && $^V ge 5.10.0 &&
5881 $line =~ /\bsscanf\b/ &&
5882 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5883 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5884 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5885 my $lc = $stat =~ tr
@\n@@;
5886 $lc = $lc + $linenr;
5887 my $stat_real = raw_line
($linenr, 0);
5888 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5889 $stat_real = $stat_real . "\n" . raw_line
($count, 0);
5891 WARN
("NAKED_SSCANF",
5892 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5895 # check for simple sscanf that should be kstrto<foo>
5896 if ($^V && $^V ge 5.10.0 &&
5898 $line =~ /\bsscanf\b/) {
5899 my $lc = $stat =~ tr
@\n@@;
5900 $lc = $lc + $linenr;
5901 my $stat_real = raw_line
($linenr, 0);
5902 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5903 $stat_real = $stat_real . "\n" . raw_line
($count, 0);
5905 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5907 my $count = $format =~ tr
@%@%@;
5909 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5910 WARN
("SSCANF_TO_KSTRTO",
5911 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5916 # check for new externs in .h files.
5917 if ($realfile =~ /\.h$/ &&
5918 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
5919 if (CHK
("AVOID_EXTERNS",
5920 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
5922 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
5926 # check for new externs in .c files.
5927 if ($realfile =~ /\.c$/ && defined $stat &&
5928 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
5930 my $function_name = $1;
5931 my $paren_space = $2;
5934 if (defined $cond) {
5935 substr($s, 0, length($cond), '');
5937 if ($s =~ /^\s*;/ &&
5938 $function_name ne 'uninitialized_var')
5940 WARN
("AVOID_EXTERNS",
5941 "externs should be avoided in .c files\n" . $herecurr);
5944 if ($paren_space =~ /\n/) {
5945 WARN
("FUNCTION_ARGUMENTS",
5946 "arguments for function declarations should follow identifier\n" . $herecurr);
5949 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5950 $stat =~ /^.\s*extern\s+/)
5952 WARN
("AVOID_EXTERNS",
5953 "externs should be avoided in .c files\n" . $herecurr);
5956 # check for function declarations that have arguments without identifier names
5957 if (defined $stat &&
5958 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
5960 my $args = trim
($1);
5961 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
5963 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
5964 WARN
("FUNCTION_ARGUMENTS",
5965 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
5970 # check for function definitions
5971 if ($^V && $^V ge 5.10.0 &&
5973 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
5974 $context_function = $1;
5976 # check for multiline function definition with misplaced open brace
5978 my $cnt = statement_rawlines
($stat);
5979 my $herectx = $here . "\n";
5980 for (my $n = 0; $n < $cnt; $n++) {
5981 my $rl = raw_line
($linenr, $n);
5982 $herectx .= $rl . "\n";
5983 $ok = 1 if ($rl =~ /^[ \+]\{/);
5984 $ok = 1 if ($rl =~ /\{/ && $n == 0);
5985 last if $rl =~ /^[ \+].*\{/;
5989 "open brace '{' following function definitions go on the next line\n" . $herectx);
5993 # checks for new __setup's
5994 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5997 if (!grep(/$name/, @setup_docs)) {
5998 CHK
("UNDOCUMENTED_SETUP",
5999 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
6003 # check for pointless casting of kmalloc return
6004 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
6005 WARN
("UNNECESSARY_CASTS",
6006 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
6010 # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
6011 if ($^V && $^V ge 5.10.0 &&
6012 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6013 CHK
("ALLOC_SIZEOF_STRUCT",
6014 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6017 # check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
6018 if ($^V && $^V ge 5.10.0 &&
6020 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
6024 my $newfunc = "kmalloc_array";
6025 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
6028 if ($a1 =~ /^sizeof\s*\S/) {
6032 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6033 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
6035 my $herectx = $here . "\n";
6036 my $cnt = statement_rawlines
($stat);
6037 for (my $n = 0; $n < $cnt; $n++) {
6038 $herectx .= raw_line
($linenr, $n) . "\n";
6040 if (WARN
("ALLOC_WITH_MULTIPLY",
6041 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6044 $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;
6049 # check for krealloc arg reuse
6050 if ($^V && $^V ge 5.10.0 &&
6051 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
6052 WARN
("KREALLOC_ARG_REUSE",
6053 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6056 # check for alloc argument mismatch
6057 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6058 WARN
("ALLOC_ARRAY_ARGS",
6059 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6062 # check for multiple semicolons
6063 if ($line =~ /;\s*;\s*$/) {
6064 if (WARN
("ONE_SEMICOLON",
6065 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6067 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6071 # check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6072 if ($realfile !~ m
@^include/uapi/@ &&
6073 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
6075 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6076 if (CHK
("BIT_MACRO",
6077 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6079 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6083 # check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6084 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6086 if (WARN
("PREFER_IS_ENABLED",
6087 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6089 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6093 # check for case / default statements not preceded by break/fallthrough/switch
6094 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6096 my $has_statement = 0;
6098 my $prevline = $linenr;
6099 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
6101 my $rline = $rawlines[$prevline - 1];
6102 my $fline = $lines[$prevline - 1];
6103 last if ($fline =~ /^\@\@/);
6104 next if ($fline =~ /^\-/);
6105 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6106 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6107 next if ($fline =~ /^.[\s$;]*$/);
6110 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
6112 if (!$has_break && $has_statement) {
6113 WARN
("MISSING_BREAK",
6114 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
6118 # check for switch/default statements without a break;
6119 if ($^V && $^V ge 5.10.0 &&
6121 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6123 my $herectx = $here . "\n";
6124 my $cnt = statement_rawlines
($stat);
6125 for (my $n = 0; $n < $cnt; $n++) {
6126 $herectx .= raw_line
($linenr, $n) . "\n";
6128 WARN
("DEFAULT_NO_BREAK",
6129 "switch default: should use break\n" . $herectx);
6132 # check for gcc specific __FUNCTION__
6133 if ($line =~ /\b__FUNCTION__\b/) {
6134 if (WARN
("USE_FUNC",
6135 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6137 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6141 # check for uses of __DATE__, __TIME__, __TIMESTAMP__
6142 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6144 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6147 # check for use of yield()
6148 if ($line =~ /\byield\s*\(\s*\)/) {
6150 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6153 # check for comparisons against true and false
6154 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6162 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6164 my $type = lc($otype);
6165 if ($type =~ /^(?:true|false)$/) {
6166 if (("$test" eq "==" && "$type" eq "true") ||
6167 ("$test" eq "!=" && "$type" eq "false")) {
6171 CHK
("BOOL_COMPARISON",
6172 "Using comparison to $otype is error prone\n" . $herecurr);
6174 ## maybe suggesting a correct construct would better
6175 ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6180 # check for semaphores initialized locked
6181 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6182 WARN
("CONSIDER_COMPLETION",
6183 "consider using a completion\n" . $herecurr);
6186 # recommend kstrto* over simple_strto* and strict_strto*
6187 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6188 WARN
("CONSIDER_KSTRTO",
6189 "$1 is obsolete, use k$3 instead\n" . $herecurr);
6192 # check for __initcall(), use device_initcall() explicitly or more appropriate function please
6193 if ($line =~ /^.\s*__initcall\s*\(/) {
6194 WARN
("USE_DEVICE_INITCALL",
6195 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6198 # use of NR_CPUS is usually wrong
6199 # ignore definitions of NR_CPUS and usage to define arrays as likely right
6200 if ($line =~ /\bNR_CPUS\b/ &&
6201 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6202 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6203 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6204 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6205 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6208 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6211 # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6212 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6213 ERROR
("DEFINE_ARCH_HAS",
6214 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6217 # likely/unlikely comparisons similar to "(likely(foo) > 0)"
6218 if ($^V && $^V ge 5.10.0 &&
6219 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6220 WARN
("LIKELY_MISUSE",
6221 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6224 # whine mightly about in_atomic
6225 if ($line =~ /\bin_atomic\s*\(/) {
6226 if ($realfile =~ m
@^drivers/@) {
6228 "do not use in_atomic in drivers\n" . $herecurr);
6229 } elsif ($realfile !~ m
@^kernel/@) {
6231 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6235 # whine about ACCESS_ONCE
6236 if ($^V && $^V ge 5.10.0 &&
6237 $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6241 $par =~ s/^\(\s*(.*)\s*\)$/$1/;
6243 if (WARN
("PREFER_WRITE_ONCE",
6244 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6246 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6249 if (WARN
("PREFER_READ_ONCE",
6250 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6252 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6257 # check for mutex_trylock_recursive usage
6258 if ($line =~ /mutex_trylock_recursive/) {
6260 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6263 # check for lockdep_set_novalidate_class
6264 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6265 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6266 if ($realfile !~ m
@^kernel/lockdep@ &&
6267 $realfile !~ m
@^include/linux/lockdep@ &&
6268 $realfile !~ m
@^drivers/base/core@) {
6270 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
6274 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6275 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6276 WARN
("EXPORTED_WORLD_WRITABLE",
6277 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
6280 # Mode permission misuses where it seems decimal should be octal
6281 # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6282 if ($^V && $^V ge 5.10.0 &&
6284 $line =~ /$mode_perms_search/) {
6285 foreach my $entry (@mode_permission_funcs) {
6286 my $func = $entry->[0];
6287 my $arg_pos = $entry->[1];
6289 my $lc = $stat =~ tr
@\n@@;
6290 $lc = $lc + $linenr;
6291 my $stat_real = raw_line
($linenr, 0);
6292 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6293 $stat_real = $stat_real . "\n" . raw_line
($count, 0);
6299 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6301 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6302 if ($stat =~ /$test/) {
6304 $val = $6 if ($skip_args ne "");
6305 if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6306 ($val =~ /^$Octal$/ && length($val) ne 4)) {
6307 ERROR
("NON_OCTAL_PERMISSIONS",
6308 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6310 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6311 ERROR
("EXPORTED_WORLD_WRITABLE",
6312 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
6318 # check for uses of S_<PERMS> that could be octal for readability
6319 if ($line =~ /\b$mode_perms_string_search\b/) {
6325 while ($line =~ /\b(($mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
6326 $curpos = pos($line);
6329 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
6331 $to |= $mode_permission_string_types{$match};
6332 $val .= '\s*\|\s*' if ($val ne "");
6336 $oval =~ s/^\s*\|\s*//;
6337 $oval =~ s/\s*\|\s*$//;
6338 my $octal = sprintf("%04o", $to);
6339 if (WARN
("SYMBOLIC_PERMS",
6340 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6342 $fixed[$fixlinenr] =~ s/$val/$octal/;
6346 # validate content of MODULE_LICENSE against list from include/linux/module.h
6347 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6348 my $extracted_string = get_quoted_string
($line, $rawline);
6349 my $valid_licenses = qr{
6352 GPL\
and\ additional\ rights
|
6358 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6359 WARN
("MODULE_LICENSE",
6360 "unknown module license " . $extracted_string . "\n" . $herecurr);
6364 # check for usage of nonstandard fixed-width integral types
6365 if ($line =~ /u_int8_t/ ||
6366 $line =~ /u_int32_t/ ||
6367 $line =~ /u_int16_t/ ||
6368 $line =~ /u_int64_t/ ||
6369 $line =~ /[^a-z_]u_char[^a-z_]/ ||
6370 $line =~ /[^a-z_]u_short[^a-z_]/ ||
6371 $line =~ /[^a-z_]u_int[^a-z_]/ ||
6372 $line =~ /[^a-z_]u_long[^a-z_]/) {
6373 ERROR
("NONSTANDARD_INTEGRAL_TYPES",
6374 "Please, no nonstandard integer types in new code.\n" . $herecurr)
6378 # If we have no input at all, then there is nothing to report on
6379 # so just keep quiet.
6380 if ($#rawlines == -1) {
6384 # In mailback mode only produce a report in the negative, for
6385 # things that appear to be patches.
6386 if ($mailback && ($clean == 1 || !$is_patch)) {
6390 # This is not a patch, and we are are in 'no-patch' mode so
6392 if (!$chk_patch && !$is_patch) {
6396 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
6397 ERROR
("NOT_UNIFIED_DIFF",
6398 "Does not appear to be a unified-diff format patch\n");
6400 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
6401 ERROR
("MISSING_SIGN_OFF",
6402 "Missing Signed-off-by: line(s)\n");
6405 print report_dump
();
6406 if ($summary && !($clean == 1 && $quiet == 1)) {
6407 print "$filename " if ($summary_file);
6408 print "total: $cnt_error errors, $cnt_warn warnings, " .
6409 (($check)?
"$cnt_chk checks, " : "") .
6410 "$cnt_lines lines checked\n";
6414 # If there were any defects found and not already fixing them
6415 if (!$clean and !$fix) {
6418 NOTE
: For some of the reported defects
, checkpatch may be able to
6419 mechanically convert to the typical style using
--fix
or --fix-inplace
.
6422 # If there were whitespace errors which cleanpatch can fix
6423 # then suggest that.
6424 if ($rpt_cleaners) {
6428 NOTE
: Whitespace errors detected
.
6429 You may wish to
use scripts
/cleanpatch or scripts/cleanfile
6434 if ($clean == 0 && $fix &&
6435 ("@rawlines" ne "@fixed" ||
6436 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
6437 my $newfile = $filename;
6438 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
6442 @fixed = fix_inserted_deleted_lines
(\
@fixed, \
@fixed_inserted, \
@fixed_deleted);
6444 open($f, '>', $newfile)
6445 or die "$P: Can't open $newfile for write\n";
6446 foreach my $fixed_line (@fixed) {
6449 if ($linecount > 3) {
6450 $fixed_line =~ s/^\+//;
6451 print $f $fixed_line . "\n";
6454 print $f $fixed_line . "\n";
6462 Wrote EXPERIMENTAL
--fix correction
(s
) to
'$newfile'
6464 Do _NOT_ trust the results written to this file
.
6465 Do _NOT_ submit these changes without inspecting them
for correctness
.
6467 This EXPERIMENTAL file
is simply a convenience to help rewrite patches
.
6468 No warranties
, expressed
or implied
...
6476 print "$vname has no obvious style problems and is ready for submission.\n";
6478 print "$vname has style problems, please review.\n";