2 # SPDX-License-Identifier: GPL-2.0
4 # (c) 2001, Dave Jones. (the file handling bit)
5 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
6 # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
7 # (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
8 # (c) 2010-2018 Joe Perches <joe@perches.com>
15 use Term
::ANSIColor
qw(:constants);
16 use Encode
qw(decode encode);
19 my $D = dirname
(abs_path
($P));
23 use Getopt
::Long
qw(:config no_auto_abbrev);
25 # ATTENTION: easily track modification to this script for OpenOCD.
26 # When possible, don't modify the existing code, don't change its indentation,
27 # but remove it enclosing it within:
33 # Mark every addition within comments
34 # # OpenOCD specific: Begin[: additional comment]
35 # # OpenOCD specific: End
40 my %verbose_messages = ();
41 my %verbose_emitted = ();
62 my $gitroot = $ENV{'GIT_DIR'};
63 $gitroot = ".git" if !defined($gitroot);
71 my $configuration_file = ".checkpatch.conf";
72 my $max_line_length = 100;
73 my $ignore_perl_version = 0;
74 my $minimum_perl_version = 5.10.0;
75 my $min_conf_desc_length = 4;
76 my $spelling_file = "$D/spelling.txt";
78 my $codespellfile = "/usr/share/codespell/dictionary.txt";
79 my $user_codespellfile = "";
80 my $conststructsfile = "$D/const_structs.checkpatch";
81 my $docsfile = "$D/../Documentation/dev-tools/checkpatch.rst";
84 my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
85 # git output parsing needs US English output, so first set backtick child process LANGUAGE
86 my $git_command ='export LANGUAGE=en_US.UTF-8; git';
88 my ${CONFIG_
} = "CONFIG_";
94 Usage
: $P [OPTION
]... [FILE
]...
99 -v
, --verbose verbose mode
100 --no-tree run without an OpenOCD tree
101 --no-signoff
do not check
for 'Signed-off-by' line
102 --patch treat FILE as patchfile
(default)
103 --emacs emacs compile window format
104 --terse one line per report
105 --showfile emit diffed file position
, not input file position
106 -g
, --git treat FILE as a single commit
or git revision range
107 single git commit with
:
111 multiple git commits with
:
115 git merges are ignored
116 -f
, --file treat FILE as regular source file
117 --subjective
, --strict enable more subjective tests
118 --list
-types list the possible message types
119 --types TYPE
(,TYPE2
...) show only these comma separated message types
120 --ignore TYPE
(,TYPE2
...) ignore various comma separated message types
121 --show
-types show the specific message type
in the output
122 --max
-line
-length=n set the maximum line
length, (default $max_line_length)
123 if exceeded
, warn on patches
124 requires
--strict
for use with
--file
125 --min
-conf
-desc
-length=n set the min description
length, if shorter
, warn
126 --tab
-size
=n set the number of spaces
for tab
(default $tabsize)
127 --root
=PATH PATH to the OpenOCD tree root
128 --no-summary suppress the per
-file summary
129 --mailback only produce a report
in case of warnings
/errors
130 --summary
-file include the filename
in summary
131 --debug KEY
=[0|1] turn on
/off debugging of KEY
, where KEY is one of
132 'values', 'possible', 'type', and 'attr' (default
134 --test
-only
=WORD report only warnings
/errors containing WORD
136 --fix EXPERIMENTAL
- may create horrible results
137 If correctable single
-line errors exist
, create
138 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
139 with potential errors corrected to the preferred
141 --fix
-inplace EXPERIMENTAL
- may create horrible results
142 Is the same as
--fix
, but overwrites the input
143 file
. It
's your fault if there's
no backup
or git
144 --ignore
-perl
-version override checking of perl version
. expect
146 --codespell Use the codespell dictionary
for spelling
/typos
147 (default:$codespellfile)
148 --codespellfile Use this codespell dictionary
149 --typedefsfile Read additional types from this file
150 --color
[=WHEN
] Use colors
'always', 'never', or only
when output
151 is a terminal
('auto'). Default is
'auto'.
152 --kconfig
-prefix
=WORD
use WORD as a prefix
for Kconfig symbols
(default
154 -h
, --help
, --version display this help
and exit
156 When FILE is
- read standard input
.
164 return grep { !$seen{$_}++ } @_;
174 open(my $script, '<', abs_path
($P)) or
175 die "$P: Can't read '$P' $!\n";
177 my $text = <$script>;
181 # Also catch when type or level is passed through a variable
182 while ($text =~ /(?:(\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
184 if (exists($types{$2})) {
185 $types{$2} .= ",$1" if ($types{$2} ne $1);
190 $types{$2} = "UNDETERMINED";
194 print("#\tMessage type\n\n");
196 print(" ( Color coding: ");
197 print(RED
. "ERROR" . RESET
);
199 print(YELLOW
. "WARNING" . RESET
);
201 print(GREEN
. "CHECK" . RESET
);
203 print("Multiple levels / Undetermined");
207 foreach my $type (sort keys %types) {
208 my $orig_type = $type;
210 my $level = $types{$type};
211 if ($level eq "ERROR") {
212 $type = RED
. $type . RESET
;
213 } elsif ($level eq "WARN") {
214 $type = YELLOW
. $type . RESET
;
215 } elsif ($level eq "CHK") {
216 $type = GREEN
. $type . RESET
;
219 print(++$count . "\t" . $type . "\n");
220 if ($verbose && exists($verbose_messages{$orig_type})) {
221 my $message = $verbose_messages{$orig_type};
222 $message =~ s/\n/\n\t/g;
223 print("\t" . $message . "\n\n");
230 my $conf = which_conf
($configuration_file);
233 open(my $conffile, '<', "$conf")
234 or warn "$P: Can't find a readable $configuration_file file $!\n";
236 while (<$conffile>) {
239 $line =~ s/\s*\n?$//g;
243 next if ($line =~ m/^\s*#/);
244 next if ($line =~ m/^\s*$/);
246 my @words = split(" ", $line);
247 foreach my $word (@words) {
248 last if ($word =~ m/^#/);
249 push (@conf_args, $word);
253 unshift(@ARGV, @conf_args) if @conf_args;
257 open(my $docs, '<', "$docsfile")
258 or warn "$P: Can't read the documentation file $docsfile $!\n";
269 if ($line =~ /^\s*\*\*(.+)\*\*$/) {
271 $verbose_messages{$type} = trim
($desc);
277 if ($line =~ /^(?:\s{4,}|$)/) {
282 $verbose_messages{$type} = trim
($desc);
291 $verbose_messages{$type} = trim
($desc);
296 # Perl's Getopt::Long allows options to take optional arguments after a space.
297 # Prevent --color by itself from consuming other arguments
299 if ($_ eq "--color" || $_ eq "-color") {
300 $_ = "--color=$color";
305 'q|quiet+' => \
$quiet,
306 'v|verbose!' => \
$verbose,
308 'signoff!' => \
$chk_signoff,
309 'patch!' => \
$chk_patch,
312 'showfile!' => \
$showfile,
315 'subjective!' => \
$check,
316 'strict!' => \
$check,
317 'ignore=s' => \
@ignore,
319 'show-types!' => \
$show_types,
320 'list-types!' => \
$list_types,
321 'max-line-length=i' => \
$max_line_length,
322 'min-conf-desc-length=i' => \
$min_conf_desc_length,
323 'tab-size=i' => \
$tabsize,
325 'summary!' => \
$summary,
326 'mailback!' => \
$mailback,
327 'summary-file!' => \
$summary_file,
329 'fix-inplace!' => \
$fix_inplace,
330 'ignore-perl-version!' => \
$ignore_perl_version,
331 'debug=s' => \
%debug,
332 'test-only=s' => \
$tst_only,
333 'codespell!' => \
$codespell,
334 'codespellfile=s' => \
$user_codespellfile,
335 'typedefsfile=s' => \
$typedefsfile,
336 'color=s' => \
$color,
337 'no-color' => \
$color, #keep old behaviors of -nocolor
338 'nocolor' => \
$color, #keep old behaviors of -nocolor
339 'kconfig-prefix=s' => \
${CONFIG_
},
344 if ($user_codespellfile) {
345 # Use the user provided codespell file unconditionally
346 $codespellfile = $user_codespellfile;
347 } elsif (!(-f
$codespellfile)) {
348 # If /usr/share/codespell/dictionary.txt is not present, try to find it
349 # under codespell's install directory: <codespell_root>/data/dictionary.txt
350 if (($codespell || $help) && which
("python3") ne "") {
351 my $python_codespell_dict = << "EOF";
355 codespell_dir
= op
.dirname
(codespell_lib
.__file__
)
356 codespell_file
= op
.join(codespell_dir
, 'data', 'dictionary.txt')
357 print(codespell_file
, end
='')
360 my $codespell_dict = `python3 -c "$python_codespell_dict" 2> /dev/null`;
361 $codespellfile = $codespell_dict if (-f
$codespell_dict);
365 # $help is 1 if either -h, --help or --version is passed as option - exitcode: 0
366 # $help is 2 if invalid option is passed - exitcode: 1
367 help
($help - 1) if ($help);
369 die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
370 die "$P: --verbose cannot be used with --terse\n" if ($verbose && $terse);
372 if ($color =~ /^[01]$/) {
374 } elsif ($color =~ /^always$/i) {
376 } elsif ($color =~ /^never$/i) {
378 } elsif ($color =~ /^auto$/i) {
379 $color = (-t STDOUT
);
381 die "$P: Invalid color mode: $color\n";
384 load_docs
() if ($verbose);
385 list_types
(0) if ($list_types);
387 $fix = 1 if ($fix_inplace);
388 $check_orig = $check;
392 my $perl_version_ok = 1;
393 if ($^V
&& $^V
lt $minimum_perl_version) {
394 $perl_version_ok = 0;
395 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
396 exit(1) if (!$ignore_perl_version);
399 #if no filenames are given, push '-' to read patch from stdin
404 # skip TAB size 1 to avoid additional checks on $tabsize - 1
405 die "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
407 sub hash_save_array_words
{
408 my ($hashRef, $arrayRef) = @_;
410 my @array = split(/,/, join(',', @
$arrayRef));
411 foreach my $word (@array) {
412 $word =~ s/\s*\n?$//g;
415 $word =~ tr/[a-z]/[A-Z]/;
417 next if ($word =~ m/^\s*#/);
418 next if ($word =~ m/^\s*$/);
424 sub hash_show_words
{
425 my ($hashRef, $prefix) = @_;
427 if (keys %$hashRef) {
428 print "\nNOTE: $prefix message types:";
429 foreach my $word (sort keys %$hashRef) {
436 hash_save_array_words
(\
%ignore_type, \
@ignore);
437 hash_save_array_words
(\
%use_type, \
@use);
440 my $dbg_possible = 0;
443 for my $key (keys %debug) {
445 eval "\${dbg_$key} = '$debug{$key}';";
449 my $rpt_cleaners = 0;
458 if (!top_of_kernel_tree
($root)) {
459 die "$P: $root: --root does not point at a valid tree\n";
462 if (top_of_kernel_tree
('.')) {
464 # OpenOCD specific: Begin: replace s"/scripts/"/tools/scripts/"
465 } elsif ($0 =~ m@
(.*)/tools/scripts
/[^/]*$@
&&
466 top_of_kernel_tree
($1)) {
469 # OpenOCD specific: End
472 if (!defined $root) {
473 print "Must be run from the top-level dir. of an OpenOCD tree\n";
478 my $emitted_corrupt = 0;
481 [A
-Za
-z_
][A
-Za
-z\d_
]*
482 (?
:\s
*\#\#\s
*[A
-Za
-z_
][A
-Za
-z\d_
]*)*
484 our $Storage = qr{extern|static|asmlinkage};
498 our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
499 our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
500 our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
501 our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
502 our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
504 # Notes to $Attribute:
505 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
528 ____cacheline_aligned
|
529 ____cacheline_aligned_in_smp
|
530 ____cacheline_internodealigned_in_smp
|
532 __alloc_size\s
*\
(\s
*\d
+\s
*(?
:,\s
*\d
+\s
*)?\
)
535 our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
536 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
537 our $Lval = qr{$Ident(?:$Member)*};
539 our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
540 our $Binary = qr{(?i)0b[01]+$Int_type?};
541 our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
542 our $Int = qr{[0-9]+$Int_type?};
543 our $Octal = qr{0[0-7]+$Int_type?};
544 our $String = qr{(?:\b[Lu])?"[X\t]*"};
545 our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
546 our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
547 our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
548 our $Float = qr{$Float_hex|$Float_dec|$Float_int};
549 our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
550 our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
551 our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
552 our $Arithmetic = qr{\+|-|\*|\/|%};
556 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||$Arithmetic
559 our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
563 our $NonptrTypeMisordered;
564 our $NonptrTypeWithAttr;
568 our $DeclareMisordered;
570 our $NON_ASCII_UTF8 = qr{
571 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
572 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
573 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
574 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
575 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
576 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
577 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
581 [\x09\x0A\x0D\x20-\x7E] # ASCII
585 our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
586 our $typeOtherOSTypedefs = qr{(?x
:
587 u_
(?
:char
|short
|int|long
) | # bsd
588 u
(?
:nchar
|short
|int|long
) # sysv
590 our $typeKernelTypedefs = qr{(?x
:
591 (?
:__
)?
(?
:u
|s
|be
|le)(?
:8|16|32|64)|
594 our $typeTypedefs = qr{(?x
:
596 $typeOtherOSTypedefs\b|
597 $typeKernelTypedefs\b
600 our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
603 our $logFunctions = qr{(?x
:
604 printk
(?
:_ratelimited
|_once
|_deferred_once
|_deferred
|)|
605 (?
:[a
-z0
-9]+_
){1,2}(?
:printk
|emerg
|alert
|crit
|err
|warning
|warn|notice
|info
|debug
|dbg
|vdbg
|devel
|cont
|WARN
)(?
:_ratelimited
|_once
|)|
607 WARN
(?
:_RATELIMIT
|_ONCE
|)|
610 seq_vprintf
|seq_printf
|seq_puts
613 # OpenOCD specific: Begin: list log functions
614 our $logFunctions = qr{(?x
:
615 LOG_
(?
:TARGET_
|)(?
:DEBUG_IO
|DEBUG
|INFO
|WARNING
|ERROR
|USER
|USER_N
|OUTPUT
)
617 # OpenOCD specific: End
619 our $allocFunctions = qr{(?x
:
621 (?
:kv
|k
|v
)[czm
]alloc
(?
:_array
)?
(?
:_node
)?
|
624 (?
:\w
+)?alloc_skb
(?
:_ip_align
)?
|
625 # dev_alloc_skb/netdev_alloc_skb, et al
629 our $signature_tags = qr{(?xi
:
641 our $tracing_logging_tags = qr{(?xi
:
665 sub edit_distance_min
{
667 my $len = scalar @arr;
668 if ((scalar @arr) < 1) {
669 # if underflow, return
673 for my $i (0 .. ($len-1)) {
674 if ($arr[$i] < $min) {
681 sub get_edit_distance
{
682 my ($str1, $str2) = @_;
687 my $len1 = length($str1);
688 my $len2 = length($str2);
689 # two dimensional array storing minimum edit distance
691 for my $i (0 .. $len1) {
692 for my $j (0 .. $len2) {
694 $distance[$i][$j] = $j;
696 $distance[$i][$j] = $i;
697 } elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) {
698 $distance[$i][$j] = $distance[$i - 1][$j - 1];
700 my $dist1 = $distance[$i][$j - 1]; #insert distance
701 my $dist2 = $distance[$i - 1][$j]; # remove
702 my $dist3 = $distance[$i - 1][$j - 1]; #replace
703 $distance[$i][$j] = 1 + edit_distance_min
($dist1, $dist2, $dist3);
707 return $distance[$len1][$len2];
710 sub find_standard_signature
{
712 my @standard_signature_tags = (
713 'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:',
714 'Reviewed-by:', 'Reported-by:', 'Suggested-by:'
716 foreach my $signature (@standard_signature_tags) {
717 return $signature if (get_edit_distance
($sign_off, $signature) <= 2);
723 our @typeListMisordered = (
724 qr{char\s+(?:un)?signed},
725 qr{int\s+(?:(?:un)?signed\s+)?short\s},
726 qr{int\s+short(?:\s+(?:un)?signed)},
727 qr{short\s+int(?:\s+(?:un)?signed)},
728 qr{(?:un)?signed\s+int\s+short},
729 qr{short\s+(?:un)?signed},
730 qr{long\s+int\s+(?:un)?signed},
731 qr{int\s+long\s+(?:un)?signed},
732 qr{long\s+(?:un)?signed\s+int},
733 qr{int\s+(?:un)?signed\s+long},
734 qr{int\s+(?:un)?signed},
735 qr{int\s+long\s+long\s+(?:un)?signed},
736 qr{long\s+long\s+int\s+(?:un)?signed},
737 qr{long\s+long\s+(?:un)?signed\s+int},
738 qr{long\s+long\s+(?:un)?signed},
739 qr{long\s+(?:un)?signed},
744 qr{(?:(?:un)?signed\s+)?char},
745 qr{(?:(?:un)?signed\s+)?short\s+int},
746 qr{(?:(?:un)?signed\s+)?short},
747 qr{(?:(?:un)?signed\s+)?int},
748 qr{(?:(?:un)?signed\s+)?long\s+int},
749 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
750 qr{(?:(?:un)?signed\s+)?long\s+long},
751 qr{(?:(?:un)?signed\s+)?long},
760 qr{${Ident}_handler
},
761 qr{${Ident}_handler_fn
},
765 our $C90_int_types = qr{(?x
:
766 long\s
+long\s
+int\s
+(?
:un
)?signed
|
767 long\s
+long\s
+(?
:un
)?signed\s
+int|
768 long\s
+long\s
+(?
:un
)?signed
|
769 (?
:(?
:un
)?signed\s
+)?long\s
+long\s
+int|
770 (?
:(?
:un
)?signed\s
+)?long\s
+long
|
771 int\s
+long\s
+long\s
+(?
:un
)?signed
|
772 int\s
+(?
:(?
:un
)?signed\s
+)?long\s
+long
|
774 long\s
+int\s
+(?
:un
)?signed
|
775 long\s
+(?
:un
)?signed\s
+int|
776 long\s
+(?
:un
)?signed
|
777 (?
:(?
:un
)?signed\s
+)?long\s
+int|
778 (?
:(?
:un
)?signed\s
+)?long
|
779 int\s
+long\s
+(?
:un
)?signed
|
780 int\s
+(?
:(?
:un
)?signed\s
+)?long
|
783 (?
:(?
:un
)?signed\s
+)?
int
786 our @typeListFile = ();
787 our @typeListWithAttr = (
789 qr{struct\s+$InitAttribute\s+$Ident},
790 qr{union\s+$InitAttribute\s+$Ident},
793 our @modifierList = (
796 our @modifierListFile = ();
798 our @mode_permission_funcs = (
800 ["module_param_(?:array|named|string)", 4],
801 ["module_param_array_named", 5],
802 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
803 ["proc_create(?:_data|)", 2],
804 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
805 ["IIO_DEV_ATTR_[A-Z_]+", 1],
806 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
807 ["SENSOR_TEMPLATE(?:_2|)", 3],
811 my $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
813 #Create a search pattern for all these functions to speed up a loop below
814 our $mode_perms_search = "";
815 foreach my $entry (@mode_permission_funcs) {
816 $mode_perms_search .= '|' if ($mode_perms_search ne "");
817 $mode_perms_search .= $entry->[0];
819 $mode_perms_search = "(?:${mode_perms_search})";
821 our %deprecated_apis = (
822 "synchronize_rcu_bh" => "synchronize_rcu",
823 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
824 "call_rcu_bh" => "call_rcu",
825 "rcu_barrier_bh" => "rcu_barrier",
826 "synchronize_sched" => "synchronize_rcu",
827 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
828 "call_rcu_sched" => "call_rcu",
829 "rcu_barrier_sched" => "rcu_barrier",
830 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
831 "cond_synchronize_sched" => "cond_synchronize_rcu",
834 #Create a search pattern for all these strings to speed up a loop below
835 our $deprecated_apis_search = "";
836 foreach my $entry (keys %deprecated_apis) {
837 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
838 $deprecated_apis_search .= $entry;
840 $deprecated_apis_search = "(?:${deprecated_apis_search})";
842 our $mode_perms_world_writable = qr{
850 our %mode_permission_string_types = (
869 #Create a search pattern for all these strings to speed up a loop below
870 our $mode_perms_string_search = "";
871 foreach my $entry (keys %mode_permission_string_types) {
872 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
873 $mode_perms_string_search .= $entry;
875 our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
876 our $multi_mode_perms_string_search = qr{
877 ${single_mode_perms_string_search
}
878 (?
:\s
*\
|\s
*${single_mode_perms_string_search
})*
884 return trim
($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
891 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
892 $curpos = pos($string);
895 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
897 $to |= $mode_permission_string_types{$match};
898 $val .= '\s*\|\s*' if ($val ne "");
902 $oval =~ s/^\s*\|\s*//;
903 $oval =~ s/\s*\|\s*$//;
904 return sprintf("%04o", $to);
907 our $allowed_asm_includes = qr{(?x
:
913 # memory.h: ARM has a custom one
915 # Load common spelling mistakes and build regular expression list.
919 if (open(my $spelling, '<', $spelling_file)) {
920 while (<$spelling>) {
923 $line =~ s/\s*\n?$//g;
926 next if ($line =~ m/^\s*#/);
927 next if ($line =~ m/^\s*$/);
929 my ($suspect, $fix) = split(/\|\|/, $line);
931 $spelling_fix{$suspect} = $fix;
935 warn "No typos will be found - file '$spelling_file': $!\n";
939 if (open(my $spelling, '<', $codespellfile)) {
940 while (<$spelling>) {
943 $line =~ s/\s*\n?$//g;
946 next if ($line =~ m/^\s*#/);
947 next if ($line =~ m/^\s*$/);
948 next if ($line =~ m/, disabled/i);
952 my ($suspect, $fix) = split(/->/, $line);
954 $spelling_fix{$suspect} = $fix;
958 warn "No codespell typos will be found - file '$codespellfile': $!\n";
962 $misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
965 my ($wordsRef, $file) = @_;
967 if (open(my $words, '<', $file)) {
971 $line =~ s/\s*\n?$//g;
974 next if ($line =~ m/^\s*#/);
975 next if ($line =~ m/^\s*$/);
977 print("$file: '$line' invalid - ignored\n");
981 $$wordsRef .= '|' if (defined $$wordsRef);
992 if (show_type
("CONST_STRUCT")) {
993 read_words
(\
$const_structs, $conststructsfile)
994 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
997 if (defined($typedefsfile)) {
998 my $typeOtherTypedefs;
999 read_words
(\
$typeOtherTypedefs, $typedefsfile)
1000 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
1001 $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
1005 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
1006 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
1007 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
1008 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
1009 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
1011 (?
:$typeTypedefs\b)|
1015 (?
:$Modifier\s
+|const\s
+)*
1017 (?
:typeof
|__typeof__
)\s
*\
([^\
)]*\
)|
1018 (?
:$typeTypedefs\b)|
1021 (?
:\s
+$Modifier|\s
+const
)*
1023 $NonptrTypeMisordered = qr{
1024 (?
:$Modifier\s
+|const\s
+)*
1028 (?
:\s
+$Modifier|\s
+const
)*
1030 $NonptrTypeWithAttr = qr{
1031 (?
:$Modifier\s
+|const\s
+)*
1033 (?
:typeof
|__typeof__
)\s
*\
([^\
)]*\
)|
1034 (?
:$typeTypedefs\b)|
1035 (?
:${allWithAttr
}\b)
1037 (?
:\s
+$Modifier|\s
+const
)*
1041 (?
:(?
:\s
|\
*|\
[\
])+\s
*const
|(?
:\s
|\
*\s
*(?
:const\s
*)?
|\
[\
])+|(?
:\s
*\
[\s
*\
])+){0,4}
1042 (?
:\s
+$Inline|\s
+$Modifier)*
1044 $TypeMisordered = qr{
1045 $NonptrTypeMisordered
1046 (?
:(?
:\s
|\
*|\
[\
])+\s
*const
|(?
:\s
|\
*\s
*(?
:const\s
*)?
|\
[\
])+|(?
:\s
*\
[\s
*\
])+){0,4}
1047 (?
:\s
+$Inline|\s
+$Modifier)*
1049 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
1050 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
1054 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s
*};
1056 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
1057 # requires at least perl version v5.10.0
1058 # Any use must be runtime checked with $^V
1060 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
1061 our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s
*};
1062 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
1064 our $declaration_macros = qr{(?x
:
1065 (?
:$Storage\s
+)?
(?
:[A
-Z_
][A
-Z0
-9]*_
){0,2}(?
:DEFINE
|DECLARE
)(?
:_
[A
-Z0
-9]+){1,6}\s
*\
(|
1066 (?
:$Storage\s
+)?
[HLP
]?LIST_HEAD\s
*\
(|
1067 (?
:SKCIPHER_REQUEST
|SHASH_DESC
|AHASH_REQUEST
)_ON_STACK\s
*\
(|
1068 (?
:$Storage\s
+)?
(?
:XA_STATE
|XA_STATE_ORDER
)\s
*\
(
1071 our %allow_repeated_words = (
1078 sub deparenthesize
{
1080 return "" if (!defined($string));
1082 while ($string =~ /^\s*\(.*\)\s*$/) {
1083 $string =~ s@
^\s
*\
(\s
*@@
;
1084 $string =~ s@\s
*\
)\s
*$@@
;
1087 $string =~ s@\s
+@
@g;
1092 sub seed_camelcase_file
{
1095 return if (!(-f
$file));
1099 open(my $include_file, '<', "$file")
1100 or warn "$P: Can't read '$file' $!\n";
1101 my $text = <$include_file>;
1102 close($include_file);
1104 my @lines = split('\n', $text);
1106 foreach my $line (@lines) {
1107 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
1108 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
1110 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
1112 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
1118 our %maintained_status = ();
1120 sub is_maintained_obsolete
{
1121 my ($filename) = @_;
1123 return 0 if (!$tree || !(-e
"$root/scripts/get_maintainer.pl"));
1125 if (!exists($maintained_status{$filename})) {
1126 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
1129 return $maintained_status{$filename} =~ /obsolete/i;
1132 sub is_SPDX_License_valid
{
1135 # OpenOCD specific: Begin: replace s"scripts"tools/scripts"
1136 return 1 if (!$tree || which
("python3") eq "" || !(-x
"$root/tools/scripts/spdxcheck.py") || !(-e
"$gitroot"));
1138 my $root_path = abs_path
($root);
1139 my $status = `cd "$root_path"; echo "$license" | tools/scripts/spdxcheck.py -`;
1140 # OpenOCD specific: End
1141 return 0 if ($status ne "");
1145 my $camelcase_seeded = 0;
1146 sub seed_camelcase_includes
{
1147 return if ($camelcase_seeded);
1150 my $camelcase_cache = "";
1151 my @include_files = ();
1153 $camelcase_seeded = 1;
1155 if (-e
"$gitroot") {
1156 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
1157 chomp $git_last_include_commit;
1158 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
1160 my $last_mod_date = 0;
1161 $files = `find $root/include -name "*.h"`;
1162 @include_files = split('\n', $files);
1163 foreach my $file (@include_files) {
1164 my $date = POSIX
::strftime
("%Y%m%d%H%M",
1165 localtime((stat $file)[9]));
1166 $last_mod_date = $date if ($last_mod_date < $date);
1168 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
1171 if ($camelcase_cache ne "" && -f
$camelcase_cache) {
1172 open(my $camelcase_file, '<', "$camelcase_cache")
1173 or warn "$P: Can't read '$camelcase_cache' $!\n";
1174 while (<$camelcase_file>) {
1178 close($camelcase_file);
1183 if (-e
"$gitroot") {
1184 $files = `${git_command} ls-files "include/*.h"`;
1185 @include_files = split('\n', $files);
1188 foreach my $file (@include_files) {
1189 seed_camelcase_file
($file);
1192 if ($camelcase_cache ne "") {
1193 unlink glob ".checkpatch-camelcase.*";
1194 open(my $camelcase_file, '>', "$camelcase_cache")
1195 or warn "$P: Can't write '$camelcase_cache' $!\n";
1196 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
1197 print $camelcase_file ("$_\n");
1199 close($camelcase_file);
1203 sub git_is_single_file
{
1204 my ($filename) = @_;
1206 return 0 if ((which
("git") eq "") || !(-e
"$gitroot"));
1208 my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
1209 my $count = $output =~ tr/\n//;
1210 return $count eq 1 && $output =~ m{^${filename}$};
1213 sub git_commit_info
{
1214 my ($commit, $id, $desc) = @_;
1216 return ($id, $desc) if ((which
("git") eq "") || !(-e
"$gitroot"));
1218 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
1219 $output =~ s/^\s*//gm;
1220 my @lines = split("\n", $output);
1222 return ($id, $desc) if ($#lines < 0);
1224 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
1225 # Maybe one day convert this block of bash into something that returns
1226 # all matching commit ids, but it's very slow...
1228 # echo "checking commits $1..."
1229 # git rev-list --remotes | grep -i "^$1" |
1230 # while read line ; do
1231 # git log --format='%H %s' -1 $line |
1232 # echo "commit $(cut -c 1-12,41-)"
1234 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./ ||
1235 $lines[0] =~ /^fatal: bad object $commit/) {
1238 $id = substr($lines[0], 0, 12);
1239 $desc = substr($lines[0], 41);
1242 return ($id, $desc);
1245 $chk_signoff = 0 if ($file);
1250 my @fixed_inserted = ();
1251 my @fixed_deleted = ();
1254 # If input is git commits, extract all commits from the commit expressions.
1255 # For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
1256 die "$P: No git repository found\n" if ($git && !-e
"$gitroot");
1260 foreach my $commit_expr (@ARGV) {
1262 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1263 $git_range = "-$2 $1";
1264 } elsif ($commit_expr =~ m/\.\./) {
1265 $git_range = "$commit_expr";
1267 $git_range = "-1 $commit_expr";
1269 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
1270 foreach my $line (split(/\n/, $lines)) {
1271 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1272 next if (!defined($1) || !defined($2));
1275 unshift(@commits, $sha1);
1276 $git_commits{$sha1} = $subject;
1279 die "$P: no git commits after extraction!\n" if (@commits == 0);
1284 $allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
1285 for my $filename (@ARGV) {
1287 my $is_git_file = git_is_single_file
($filename);
1288 my $oldfile = $file;
1289 $file = 1 if ($is_git_file);
1291 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1292 die "$P: $filename: git format-patch failed - $!\n";
1294 open($FILE, '-|', "diff -u /dev/null $filename") ||
1295 die "$P: $filename: diff failed - $!\n";
1296 } elsif ($filename eq '-') {
1297 open($FILE, '<&STDIN');
1299 open($FILE, '<', "$filename") ||
1300 die "$P: $filename: open failed - $!\n";
1302 if ($filename eq '-') {
1303 $vname = 'Your patch';
1305 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1311 push(@rawlines, $_);
1312 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
1316 if ($#ARGV > 0 && $quiet == 0) {
1317 print '-' x
length($vname) . "\n";
1319 print '-' x
length($vname) . "\n";
1322 if (!process
($filename)) {
1328 @fixed_inserted = ();
1329 @fixed_deleted = ();
1331 @modifierListFile = ();
1334 $file = $oldfile if ($is_git_file);
1338 hash_show_words
(\
%use_type, "Used");
1339 hash_show_words
(\
%ignore_type, "Ignored");
1341 if (!$perl_version_ok) {
1344 NOTE
: perl
$^V is
not modern enough to detect all possible issues
.
1345 An upgrade to at least perl
$minimum_perl_version is suggested
.
1352 NOTE
: If any of the errors are false positives
, please report
1353 them to the maintainer
, see CHECKPATCH
in MAINTAINERS
.
1356 # OpenOCD specific: Begin
1359 NOTE
: If any of the errors are false positives
, please report
1360 them to the openocd
-devel mailing list
or prepare a patch
1361 and send it to Gerrit
for review
.
1363 # OpenOCD specific: End
1369 sub top_of_kernel_tree
{
1374 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1375 "README", "Documentation", "arch", "include", "drivers",
1376 "fs", "init", "ipc", "kernel", "lib", "scripts",
1379 # OpenOCD specific: Begin
1381 "AUTHORS", "BUGS", "COPYING", "HACKING", "Makefile.am",
1382 "README", "contrib", "doc", "src", "tcl", "testing", "tools",
1384 # OpenOCD specific: End
1386 foreach my $check (@tree_check) {
1387 if (! -e
$root . '/' . $check) {
1395 my ($formatted_email) = @_;
1399 my $name_comment = "";
1403 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1406 $comment = $3 if defined $3;
1407 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1409 $comment = $2 if defined $2;
1410 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1412 $comment = $2 if defined $2;
1413 $formatted_email =~ s/\Q$address\E.*$//;
1414 $name = $formatted_email;
1415 $name = trim
($name);
1416 $name =~ s/^\"|\"$//g;
1417 # If there's a name left after stripping spaces and
1418 # leading quotes, and the address doesn't have both
1419 # leading and trailing angle brackets, the address
1421 # "joe smith joe@smith.com" bad
1422 # "joe smith <joe@smith.com" bad
1423 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1428 # OpenOCD specific: Begin: handle jenkins as valid email
1429 } elsif ($formatted_email eq "jenkins") {
1430 $address = "jenkins";
1431 # OpenOCD specific: End
1434 # Extract comments from names excluding quoted parts
1435 # "John D. (Doe)" - Do not extract
1436 if ($name =~ s/\"(.+)\"//) {
1439 while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1440 $name_comment .= trim
($1);
1442 $name =~ s/^[ \"]+|[ \"]+$//g;
1443 $name = trim
("$quoted $name");
1445 $address = trim
($address);
1446 $address =~ s/^\<|\>$//g;
1447 $comment = trim
($comment);
1449 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1450 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1451 $name = "\"$name\"";
1454 return ($name, $name_comment, $address, $comment);
1458 my ($name, $name_comment, $address, $comment) = @_;
1460 my $formatted_email;
1462 $name =~ s/^[ \"]+|[ \"]+$//g;
1463 $address = trim
($address);
1464 $address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
1466 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1467 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1468 $name = "\"$name\"";
1471 $name_comment = trim
($name_comment);
1472 $name_comment = " $name_comment" if ($name_comment ne "");
1473 $comment = trim
($comment);
1474 $comment = " $comment" if ($comment ne "");
1476 if ("$name" eq "") {
1477 $formatted_email = "$address";
1479 $formatted_email = "$name$name_comment <$address>";
1481 $formatted_email .= "$comment";
1482 return $formatted_email;
1485 sub reformat_email
{
1488 my ($email_name, $name_comment, $email_address, $comment) = parse_email
($email);
1489 return format_email
($email_name, $name_comment, $email_address, $comment);
1492 sub same_email_addresses
{
1493 my ($email1, $email2) = @_;
1495 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email
($email1);
1496 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email
($email2);
1498 return $email1_name eq $email2_name &&
1499 $email1_address eq $email2_address &&
1500 $name1_comment eq $name2_comment &&
1501 $comment1 eq $comment2;
1507 foreach my $path (split(/:/, $ENV{PATH
})) {
1508 if (-e
"$path/$bin") {
1509 return "$path/$bin";
1519 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1520 if (-e
"$path/$conf") {
1521 return "$path/$conf";
1533 for my $c (split(//, $str)) {
1537 for (; ($n % $tabsize) != 0; $n++) {
1549 (my $res = shift) =~ tr/\t/ /c;
1556 # Drop the diff line leader and expand tabs
1558 $line = expand_tabs
($line);
1560 # Pick the indent from the front of the line.
1561 my ($white) = ($line =~ /^(\s*)/);
1563 return (length($line), length($white));
1566 my $sanitise_quote = '';
1568 sub sanitise_line_reset
{
1569 my ($in_comment) = @_;
1572 $sanitise_quote = '*/';
1574 $sanitise_quote = '';
1587 # Always copy over the diff marker.
1588 $res = substr($line, 0, 1);
1590 for ($off = 1; $off < length($line); $off++) {
1591 $c = substr($line, $off, 1);
1593 # Comments we are whacking completely including the begin
1594 # and end, all to $;.
1595 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1596 $sanitise_quote = '*/';
1598 substr($res, $off, 2, "$;$;");
1602 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1603 $sanitise_quote = '';
1604 substr($res, $off, 2, "$;$;");
1608 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1609 $sanitise_quote = '//';
1611 substr($res, $off, 2, $sanitise_quote);
1616 # A \ in a string means ignore the next character.
1617 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1619 substr($res, $off, 2, 'XX');
1624 if ($c eq "'" || $c eq '"') {
1625 if ($sanitise_quote eq '') {
1626 $sanitise_quote = $c;
1628 substr($res, $off, 1, $c);
1630 } elsif ($sanitise_quote eq $c) {
1631 $sanitise_quote = '';
1635 #print "c<$c> SQ<$sanitise_quote>\n";
1636 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1637 substr($res, $off, 1, $;);
1638 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1639 substr($res, $off, 1, $;);
1640 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1641 substr($res, $off, 1, 'X');
1643 substr($res, $off, 1, $c);
1647 if ($sanitise_quote eq '//') {
1648 $sanitise_quote = '';
1651 # The pathname on a #include may be surrounded by '<' and '>'.
1652 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1653 my $clean = 'X' x
length($1);
1654 $res =~ s@\
<.*\
>@
<$clean>@
;
1656 # The whole of a #error is a string.
1657 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1658 my $clean = 'X' x
length($1);
1659 $res =~ s@
(\#\s
*(?
:error
|warning
)\s
+).*@
$1$clean@
;
1662 if ($allow_c99_comments && $res =~ m@
(//.*$)@
) {
1664 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1670 sub get_quoted_string
{
1671 my ($line, $rawline) = @_;
1673 return "" if (!defined($line) || !defined($rawline));
1674 return "" if ($line !~ m/($String)/g);
1675 return substr($rawline, $-[0], $+[0] - $-[0]);
1678 sub ctx_statement_block
{
1679 my ($linenr, $remain, $off) = @_;
1680 my $line = $linenr - 1;
1683 my $coff = $off - 1;
1697 @stack = (['', 0]) if ($#stack == -1);
1699 #warn "CSB: blk<$blk> remain<$remain>\n";
1700 # If we are about to drop off the end, pull in more
1703 for (; $remain > 0; $line++) {
1704 last if (!defined $lines[$line]);
1705 next if ($lines[$line] =~ /^-/);
1708 $blk .= $lines[$line] . "\n";
1709 $len = length($blk);
1713 # Bail if there is no further context.
1714 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
1718 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1724 $c = substr($blk, $off, 1);
1725 $remainder = substr($blk, $off);
1727 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1729 # Handle nested #if/#else.
1730 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1731 push(@stack, [ $type, $level ]);
1732 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1733 ($type, $level) = @
{$stack[$#stack - 1]};
1734 } elsif ($remainder =~ /^#\s*endif\b/) {
1735 ($type, $level) = @
{pop(@stack)};
1738 # Statement ends at the ';' or a close '}' at the
1740 if ($level == 0 && $c eq ';') {
1744 # An else is really a conditional as long as its not else if
1745 if ($level == 0 && $coff_set == 0 &&
1746 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1747 $remainder =~ /^(else)(?:\s|{)/ &&
1748 $remainder !~ /^else\s+if\b/) {
1749 $coff = $off + length($1) - 1;
1751 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1752 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1755 if (($type eq '' || $type eq '(') && $c eq '(') {
1759 if ($type eq '(' && $c eq ')') {
1761 $type = ($level != 0)?
'(' : '';
1763 if ($level == 0 && $coff < $soff) {
1766 #warn "CSB: mark coff<$coff>\n";
1769 if (($type eq '' || $type eq '{') && $c eq '{') {
1773 if ($type eq '{' && $c eq '}') {
1775 $type = ($level != 0)?
'{' : '';
1778 if (substr($blk, $off + 1, 1) eq ';') {
1784 # Preprocessor commands end at the newline unless escaped.
1785 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1793 # We are truly at the end, so shuffle to the next line.
1800 my $statement = substr($blk, $soff, $off - $soff + 1);
1801 my $condition = substr($blk, $soff, $coff - $soff + 1);
1803 #warn "STATEMENT<$statement>\n";
1804 #warn "CONDITION<$condition>\n";
1806 #print "coff<$coff> soff<$off> loff<$loff>\n";
1808 return ($statement, $condition,
1809 $line, $remain + 1, $off - $loff + 1, $level);
1812 sub statement_lines
{
1815 # Strip the diff line prefixes and rip blank lines at start and end.
1816 $stmt =~ s/(^|\n)./$1/g;
1820 my @stmt_lines = ($stmt =~ /\n/g);
1822 return $#stmt_lines + 2;
1825 sub statement_rawlines
{
1828 my @stmt_lines = ($stmt =~ /\n/g);
1830 return $#stmt_lines + 2;
1833 sub statement_block_size
{
1836 $stmt =~ s/(^|\n)./$1/g;
1842 my @stmt_lines = ($stmt =~ /\n/g);
1843 my @stmt_statements = ($stmt =~ /;/g);
1845 my $stmt_lines = $#stmt_lines + 2;
1846 my $stmt_statements = $#stmt_statements + 1;
1848 if ($stmt_lines > $stmt_statements) {
1851 return $stmt_statements;
1855 sub ctx_statement_full
{
1856 my ($linenr, $remain, $off) = @_;
1857 my ($statement, $condition, $level);
1861 # Grab the first conditional/block pair.
1862 ($statement, $condition, $linenr, $remain, $off, $level) =
1863 ctx_statement_block
($linenr, $remain, $off);
1864 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1865 push(@chunks, [ $condition, $statement ]);
1866 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1867 return ($level, $linenr, @chunks);
1870 # Pull in the following conditional/block pairs and see if they
1871 # could continue the statement.
1873 ($statement, $condition, $linenr, $remain, $off, $level) =
1874 ctx_statement_block
($linenr, $remain, $off);
1875 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1876 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1878 push(@chunks, [ $condition, $statement ]);
1881 return ($level, $linenr, @chunks);
1885 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1887 my $start = $linenr - 1;
1894 my @stack = ($level);
1895 for ($line = $start; $remain > 0; $line++) {
1896 next if ($rawlines[$line] =~ /^-/);
1899 $blk .= $rawlines[$line];
1901 # Handle nested #if/#else.
1902 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1903 push(@stack, $level);
1904 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1905 $level = $stack[$#stack - 1];
1906 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1907 $level = pop(@stack);
1910 foreach my $c (split(//, $lines[$line])) {
1911 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1917 if ($c eq $close && $level > 0) {
1919 last if ($level == 0);
1920 } elsif ($c eq $open) {
1925 if (!$outer || $level <= 1) {
1926 push(@res, $rawlines[$line]);
1929 last if ($level == 0);
1932 return ($level, @res);
1934 sub ctx_block_outer
{
1935 my ($linenr, $remain) = @_;
1937 my ($level, @r) = ctx_block_get
($linenr, $remain, 1, '{', '}', 0);
1941 my ($linenr, $remain) = @_;
1943 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
1947 my ($linenr, $remain, $off) = @_;
1949 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
1952 sub ctx_block_level
{
1953 my ($linenr, $remain) = @_;
1955 return ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
1957 sub ctx_statement_level
{
1958 my ($linenr, $remain, $off) = @_;
1960 return ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
1963 sub ctx_locate_comment
{
1964 my ($first_line, $end_line) = @_;
1966 # If c99 comment on the current line, or the line before or after
1967 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@
^\
+.*(//.*$)@
);
1968 return $current_comment if (defined $current_comment);
1969 ($current_comment) = ($rawlines[$end_line - 2] =~ m@
^[\
+ ].*(//.*$)@
);
1970 return $current_comment if (defined $current_comment);
1971 ($current_comment) = ($rawlines[$end_line] =~ m@
^[\
+ ].*(//.*$)@
);
1972 return $current_comment if (defined $current_comment);
1974 # Catch a comment on the end of the line itself.
1975 ($current_comment) = ($rawlines[$end_line - 1] =~ m@
.*(/\*.*\*/)\s
*(?
:\\\s
*)?
$@
);
1976 return $current_comment if (defined $current_comment);
1978 # Look through the context and try and figure out if there is a
1981 $current_comment = '';
1982 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1983 my $line = $rawlines[$linenr - 1];
1985 if ($linenr == $first_line and $line =~ m@
^.\s
*\
*@
) {
1988 if ($line =~ m@
/\
*@
) {
1991 if (!$in_comment && $current_comment ne '') {
1992 $current_comment = '';
1994 $current_comment .= $line . "\n" if ($in_comment);
1995 if ($line =~ m@\
*/@
) {
2000 chomp($current_comment);
2001 return($current_comment);
2003 sub ctx_has_comment
{
2004 my ($first_line, $end_line) = @_;
2005 my $cmt = ctx_locate_comment
($first_line, $end_line);
2007 ##print "LINE: $rawlines[$end_line - 1 ]\n";
2008 ##print "CMMT: $cmt\n";
2010 return ($cmt ne '');
2014 my ($linenr, $cnt) = @_;
2016 my $offset = $linenr - 1;
2021 $line = $rawlines[$offset++];
2022 next if (defined($line) && $line =~ /^-/);
2030 my ($linenr, $lc) = @_;
2032 my $stat_real = raw_line
($linenr, 0);
2033 for (my $count = $linenr + 1; $count <= $lc; $count++) {
2034 $stat_real = $stat_real . "\n" . raw_line
($count, 0);
2041 my ($linenr, $cnt, $here) = @_;
2043 my $herectx = $here . "\n";
2044 for (my $n = 0; $n < $cnt; $n++) {
2045 $herectx .= raw_line
($linenr, $n) . "\n";
2056 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
2059 $coded = sprintf("^%c", unpack('C', $2) + 64);
2068 my $av_preprocessor = 0;
2073 sub annotate_reset
{
2074 $av_preprocessor = 0;
2076 @av_paren_type = ('E');
2077 $av_pend_colon = 'O';
2080 sub annotate_values
{
2081 my ($stream, $type) = @_;
2084 my $var = '_' x
length($stream);
2087 print "$stream\n" if ($dbg_values > 1);
2089 while (length($cur)) {
2090 @av_paren_type = ('E') if ($#av_paren_type < 0);
2091 print " <" . join('', @av_paren_type) .
2092 "> <$type> <$av_pending>" if ($dbg_values > 1);
2093 if ($cur =~ /^(\s+)/o) {
2094 print "WS($1)\n" if ($dbg_values > 1);
2095 if ($1 =~ /\n/ && $av_preprocessor) {
2096 $type = pop(@av_paren_type);
2097 $av_preprocessor = 0;
2100 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
2101 print "CAST($1)\n" if ($dbg_values > 1);
2102 push(@av_paren_type, $type);
2105 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
2106 print "DECLARE($1)\n" if ($dbg_values > 1);
2109 } elsif ($cur =~ /^($Modifier)\s*/) {
2110 print "MODIFIER($1)\n" if ($dbg_values > 1);
2113 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
2114 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
2115 $av_preprocessor = 1;
2116 push(@av_paren_type, $type);
2122 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
2123 print "UNDEF($1)\n" if ($dbg_values > 1);
2124 $av_preprocessor = 1;
2125 push(@av_paren_type, $type);
2127 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
2128 print "PRE_START($1)\n" if ($dbg_values > 1);
2129 $av_preprocessor = 1;
2131 push(@av_paren_type, $type);
2132 push(@av_paren_type, $type);
2135 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
2136 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
2137 $av_preprocessor = 1;
2139 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
2143 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
2144 print "PRE_END($1)\n" if ($dbg_values > 1);
2146 $av_preprocessor = 1;
2148 # Assume all arms of the conditional end as this
2149 # one does, and continue as if the #endif was not here.
2150 pop(@av_paren_type);
2151 push(@av_paren_type, $type);
2154 } elsif ($cur =~ /^(\\\n)/o) {
2155 print "PRECONT($1)\n" if ($dbg_values > 1);
2157 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
2158 print "ATTR($1)\n" if ($dbg_values > 1);
2159 $av_pending = $type;
2162 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
2163 print "SIZEOF($1)\n" if ($dbg_values > 1);
2169 } elsif ($cur =~ /^(if|while|for)\b/o) {
2170 print "COND($1)\n" if ($dbg_values > 1);
2174 } elsif ($cur =~/^(case)/o) {
2175 print "CASE($1)\n" if ($dbg_values > 1);
2176 $av_pend_colon = 'C';
2179 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
2180 print "KEYWORD($1)\n" if ($dbg_values > 1);
2183 } elsif ($cur =~ /^(\()/o) {
2184 print "PAREN('$1')\n" if ($dbg_values > 1);
2185 push(@av_paren_type, $av_pending);
2189 } elsif ($cur =~ /^(\))/o) {
2190 my $new_type = pop(@av_paren_type);
2191 if ($new_type ne '_') {
2193 print "PAREN('$1') -> $type\n"
2194 if ($dbg_values > 1);
2196 print "PAREN('$1')\n" if ($dbg_values > 1);
2199 } elsif ($cur =~ /^($Ident)\s*\(/o) {
2200 print "FUNC($1)\n" if ($dbg_values > 1);
2204 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
2205 if (defined $2 && $type eq 'C' || $type eq 'T') {
2206 $av_pend_colon = 'B';
2207 } elsif ($type eq 'E') {
2208 $av_pend_colon = 'L';
2210 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
2213 } elsif ($cur =~ /^($Ident|$Constant)/o) {
2214 print "IDENT($1)\n" if ($dbg_values > 1);
2217 } elsif ($cur =~ /^($Assignment)/o) {
2218 print "ASSIGN($1)\n" if ($dbg_values > 1);
2221 } elsif ($cur =~/^(;|{|})/) {
2222 print "END($1)\n" if ($dbg_values > 1);
2224 $av_pend_colon = 'O';
2226 } elsif ($cur =~/^(,)/) {
2227 print "COMMA($1)\n" if ($dbg_values > 1);
2230 } elsif ($cur =~ /^(\?)/o) {
2231 print "QUESTION($1)\n" if ($dbg_values > 1);
2234 } elsif ($cur =~ /^(:)/o) {
2235 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
2237 substr($var, length($res), 1, $av_pend_colon);
2238 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
2243 $av_pend_colon = 'O';
2245 } elsif ($cur =~ /^(\[)/o) {
2246 print "CLOSE($1)\n" if ($dbg_values > 1);
2249 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
2252 print "OPV($1)\n" if ($dbg_values > 1);
2259 substr($var, length($res), 1, $variant);
2262 } elsif ($cur =~ /^($Operators)/o) {
2263 print "OP($1)\n" if ($dbg_values > 1);
2264 if ($1 ne '++' && $1 ne '--') {
2268 } elsif ($cur =~ /(^.)/o) {
2269 print "C($1)\n" if ($dbg_values > 1);
2272 $cur = substr($cur, length($1));
2273 $res .= $type x
length($1);
2277 return ($res, $var);
2281 my ($possible, $line) = @_;
2282 my $notPermitted = qr{(?
:
2299 ^(?
:typedef
|struct
|enum
)\b
2301 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2302 if ($possible !~ $notPermitted) {
2303 # Check for modifiers.
2304 $possible =~ s/\s*$Storage\s*//g;
2305 $possible =~ s/\s*$Sparse\s*//g;
2306 if ($possible =~ /^\s*$/) {
2308 } elsif ($possible =~ /\s/) {
2309 $possible =~ s/\s*$Type\s*//g;
2310 for my $modifier (split(' ', $possible)) {
2311 if ($modifier !~ $notPermitted) {
2312 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
2313 push(@modifierListFile, $modifier);
2318 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
2319 push(@typeListFile, $possible);
2323 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
2332 $type =~ tr/[a-z]/[A-Z]/;
2334 return defined $use_type{$type} if (scalar keys %use_type > 0);
2336 return !defined $ignore_type{$type};
2340 my ($level, $type, $msg) = @_;
2342 if (!show_type
($type) ||
2343 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2348 if ($level eq 'ERROR') {
2350 } elsif ($level eq 'WARNING') {
2356 $output .= $prefix . $level . ':';
2358 $output .= BLUE
if ($color);
2359 $output .= "$type:";
2361 $output .= RESET
if ($color);
2362 $output .= ' ' . $msg . "\n";
2365 my @lines = split("\n", $output, -1);
2366 splice(@lines, 1, 1);
2367 $output = join("\n", @lines);
2371 $output = (split('\n', $output))[0] . "\n";
2374 if ($verbose && exists($verbose_messages{$type}) &&
2375 !exists($verbose_emitted{$type})) {
2376 $output .= $verbose_messages{$type} . "\n\n";
2377 $verbose_emitted{$type} = 1;
2380 push(our @report, $output);
2389 sub fixup_current_range
{
2390 my ($lineRef, $offset, $length) = @_;
2392 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2395 my $no = $o + $offset;
2396 my $nl = $l + $length;
2397 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2401 sub fix_inserted_deleted_lines
{
2402 my ($linesRef, $insertedRef, $deletedRef) = @_;
2404 my $range_last_linenr = 0;
2405 my $delta_offset = 0;
2410 my $next_insert = 0;
2411 my $next_delete = 0;
2415 my $inserted = @
{$insertedRef}[$next_insert++];
2416 my $deleted = @
{$deletedRef}[$next_delete++];
2418 foreach my $old_line (@
{$linesRef}) {
2420 my $line = $old_line; #don't modify the array
2421 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
2423 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2424 $range_last_linenr = $new_linenr;
2425 fixup_current_range
(\
$line, $delta_offset, 0);
2428 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2429 $deleted = @
{$deletedRef}[$next_delete++];
2431 fixup_current_range
(\
$lines[$range_last_linenr], $delta_offset--, -1);
2434 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2435 push(@lines, ${$inserted}{'LINE'});
2436 $inserted = @
{$insertedRef}[$next_insert++];
2438 fixup_current_range
(\
$lines[$range_last_linenr], $delta_offset++, 1);
2442 push(@lines, $line);
2452 sub fix_insert_line
{
2453 my ($linenr, $line) = @_;
2459 push(@fixed_inserted, $inserted);
2462 sub fix_delete_line
{
2463 my ($linenr, $line) = @_;
2470 push(@fixed_deleted, $deleted);
2474 my ($type, $msg) = @_;
2476 if (report
("ERROR", $type, $msg)) {
2484 my ($type, $msg) = @_;
2486 if (report
("WARNING", $type, $msg)) {
2494 my ($type, $msg) = @_;
2496 if ($check && report
("CHECK", $type, $msg)) {
2504 sub check_absolute_file
{
2505 my ($absolute, $herecurr) = @_;
2506 my $file = $absolute;
2508 ##print "absolute<$absolute>\n";
2510 # See if any suffix of this path is a path within the tree.
2511 while ($file =~ s@
^[^/]*/@@
) {
2512 if (-f
"$root/$file") {
2513 ##print "file<$file>\n";
2521 # It is, so see if the prefix is acceptable.
2522 my $prefix = $absolute;
2523 substr($prefix, -length($file)) = '';
2525 ##print "prefix<$prefix>\n";
2526 if ($prefix ne ".../") {
2527 WARN
("USE_RELATIVE_PATH",
2528 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
2535 $string =~ s/^\s+|\s+$//g;
2543 $string =~ s/^\s+//;
2551 $string =~ s/\s+$//;
2556 sub string_find_replace
{
2557 my ($string, $find, $replace) = @_;
2559 $string =~ s/$find/$replace/g;
2567 my $source_indent = $tabsize;
2568 my $max_spaces_before_tab = $source_indent - 1;
2569 my $spaces_to_tab = " " x
$source_indent;
2571 #convert leading spaces to tabs
2572 1 while $leading =~ s@
^([\t]*)$spaces_to_tab@
$1\t@g;
2573 #Remove spaces before a tab
2574 1 while $leading =~ s@
^([\t]*)( {1,$max_spaces_before_tab})\t@
$1\t@g;
2579 sub pos_last_openparen
{
2584 my $opens = $line =~ tr/\(/\(/;
2585 my $closes = $line =~ tr/\)/\)/;
2587 my $last_openparen = 0;
2589 if (($opens == 0) || ($closes >= $opens)) {
2593 my $len = length($line);
2595 for ($pos = 0; $pos < $len; $pos++) {
2596 my $string = substr($line, $pos);
2597 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2598 $pos += length($1) - 1;
2599 } elsif (substr($line, $pos, 1) eq '(') {
2600 $last_openparen = $pos;
2601 } elsif (index($string, '(') == -1) {
2606 return length(expand_tabs
(substr($line, 0, $last_openparen))) + 1;
2609 sub get_raw_comment
{
2610 my ($line, $rawline) = @_;
2613 for my $i (0 .. (length($line) - 1)) {
2614 if (substr($line, $i, 1) eq "$;") {
2615 $comment .= substr($rawline, $i, 1);
2622 sub exclude_global_initialisers
{
2623 my ($realfile) = @_;
2625 # Do not check for BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c).
2626 return $realfile =~ m@
^tools
/testing
/selftests
/bpf
/progs/.*\
.c
$@
||
2627 $realfile =~ m@
^samples
/bpf/.*_kern\
.c
$@
||
2628 $realfile =~ m@
/bpf/.*\
.bpf\
.c
$@
;
2632 my $filename = shift;
2638 my $stashrawline="";
2648 my $authorsignoff = 0;
2649 my $author_sob = '';
2651 my $is_binding_patch = -1;
2652 my $in_header_lines = $file ?
0 : 1;
2653 my $in_commit_log = 0; #Scanning lines before patch
2654 my $has_patch_separator = 0; #Found a --- line
2655 my $has_commit_log = 0; #Encountered lines before patch
2656 my $commit_log_lines = 0; #Number of commit log lines
2657 my $commit_log_possible_stack_dump = 0;
2658 my $commit_log_long_line = 0;
2659 my $commit_log_has_diff = 0;
2660 my $reported_maintainer_file = 0;
2661 my $non_utf8_charset = 0;
2663 my $last_git_commit_id_linenr = -1;
2665 my $last_blank_line = 0;
2666 my $last_coalesced_string_linenr = -1;
2674 # Trace the real file/line as we go.
2679 my $context_function; #undef'd unless there's a known function
2681 my $comment_edge = 0;
2685 my $prev_values = 'E';
2688 my %suppress_ifbraces;
2689 my %suppress_whiletrailers;
2690 my %suppress_export;
2691 my $suppress_statement = 0;
2693 my %signatures = ();
2695 # Pre-scan the patch sanitizing the lines.
2696 # Pre-scan the patch looking for any __setup documentation.
2698 my @setup_docs = ();
2701 my $camelcase_file_seeded = 0;
2703 my $checklicenseline = 1;
2705 sanitise_line_reset
();
2707 foreach my $rawline (@rawlines) {
2711 push(@fixed, $rawline) if ($fix);
2713 if ($rawline=~/^\+\+\+\s+(\S+)/) {
2715 if ($1 =~ m
@Documentation/admin-guide/kernel
-parameters
.txt
$@
) {
2720 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2729 # Guestimate if this is a continuing comment. Run
2730 # the context looking for a comment "edge". If this
2731 # edge is a close comment then we must be in a comment
2735 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2736 next if (defined $rawlines[$ln - 1] &&
2737 $rawlines[$ln - 1] =~ /^-/);
2739 #print "RAW<$rawlines[$ln - 1]>\n";
2740 last if (!defined $rawlines[$ln - 1]);
2741 if ($rawlines[$ln - 1] =~ m@
(/\*|\*/)@
&&
2742 $rawlines[$ln - 1] !~ m@
"[^"]*(?
:/\*|\*/)[^"]*"@
) {
2747 if (defined $edge && $edge eq '*/') {
2751 # Guestimate if this is a continuing comment. If this
2752 # is the start of a diff block and this line starts
2753 # ' *' then it is very likely a comment.
2754 if (!defined $edge &&
2755 $rawlines[$linenr] =~ m@
^.\s
*(?
:\
*\
*+| \
*)(?
:\s
|$)@
)
2760 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2761 sanitise_line_reset
($in_comment);
2763 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2764 # Standardise the strings and chars within the input to
2765 # simplify matching -- only bother with positive lines.
2766 $line = sanitise_line
($rawline);
2768 push(@lines, $line);
2771 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2776 #print "==>$rawline\n";
2777 #print "-->$line\n";
2779 if ($setup_docs && $line =~ /^\+/) {
2780 push(@setup_docs, $line);
2789 foreach my $line (@lines) {
2792 my $sline = $line; #copy of $line
2793 $sline =~ s/$;/ /g; #with comments as spaces
2795 my $rawline = $rawlines[$linenr - 1];
2796 my $raw_comment = get_raw_comment
($line, $rawline);
2798 # check if it's a mode change, rename or start of a patch
2799 if (!$in_commit_log &&
2800 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2801 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2802 $line =~ /^diff --git a\/[\w\
/\.\_\-]+ b\/\S
+\s
*$/))) {
2806 #extract the line range in the file after the patch is applied
2807 if (!$in_commit_log &&
2808 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2811 $first_line = $linenr + 1;
2821 %suppress_ifbraces = ();
2822 %suppress_whiletrailers = ();
2823 %suppress_export = ();
2824 $suppress_statement = 0;
2825 if ($context =~ /\b(\w+)\s*\(/) {
2826 $context_function = $1;
2828 undef $context_function;
2832 # track the line number as we move through the hunk, note that
2833 # new versions of GNU diff omit the leading space on completely
2834 # blank context lines so we need to count that too.
2835 } elsif ($line =~ /^( |\+|$)/) {
2837 $realcnt-- if ($realcnt != 0);
2839 # Measure the line length and indent.
2840 ($length, $indent) = line_stats
($rawline);
2842 # Track the previous line.
2843 ($prevline, $stashline) = ($stashline, $line);
2844 ($previndent, $stashindent) = ($stashindent, $indent);
2845 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2847 #warn "line<$line>\n";
2849 } elsif ($realcnt == 1) {
2853 my $hunk_line = ($realcnt != 0);
2855 $here = "#$linenr: " if (!$file);
2856 $here = "#$realline: " if ($file);
2859 # extract the filename as it passes
2860 if ($line =~ /^diff --git.*?(\S+)$/) {
2862 $realfile =~ s@
^([^/]*)/@@
if (!$file);
2865 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2867 $realfile =~ s@
^([^/]*)/@@
if (!$file);
2871 if (!$file && $tree && $p1_prefix ne '' &&
2872 -e
"$root/$p1_prefix") {
2873 WARN
("PATCH_PREFIX",
2874 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2877 if ($realfile =~ m@
^include
/asm/@
) {
2878 ERROR
("MODIFIED_INCLUDE_ASM",
2879 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2884 #make up the handle for any error we report on this line
2886 $prefix = "$realfile:$realline: "
2889 $prefix = "$filename:$realline: ";
2891 $prefix = "$filename:$linenr: ";
2896 if (is_maintained_obsolete
($realfile)) {
2898 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2900 if ($realfile =~ m@
^(?
:drivers
/net
/|net
/|drivers
/staging/)@
) {
2903 $check = $check_orig;
2905 $checklicenseline = 1;
2907 if ($realfile !~ /^MAINTAINERS/) {
2908 my $last_binding_patch = $is_binding_patch;
2910 $is_binding_patch = () = $realfile =~ m@
^(?
:Documentation
/devicetree/|include
/dt-bindings/)@
;
2912 if (($last_binding_patch != -1) &&
2913 ($last_binding_patch ^ $is_binding_patch)) {
2914 WARN
("DT_SPLIT_BINDING_PATCH",
2915 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n");
2922 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2924 my $hereline = "$here\n$rawline\n";
2925 my $herecurr = "$here\n$rawline\n";
2926 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2928 $cnt_lines++ if ($realcnt != 0);
2930 # Verify the existence of a commit log if appropriate
2931 # 2 is used because a $signature is counted in $commit_log_lines
2932 if ($in_commit_log) {
2933 if ($line !~ /^\s*$/) {
2934 $commit_log_lines++; #could be a $signature
2936 } elsif ($has_commit_log && $commit_log_lines < 2) {
2937 WARN
("COMMIT_MESSAGE",
2938 "Missing commit description - Add an appropriate one\n");
2939 $commit_log_lines = 2; #warn only once
2942 # Check if the commit log has what seems like a diff which can confuse patch
2943 if ($in_commit_log && !$commit_log_has_diff &&
2944 (($line =~ m@
^\s
+diff
\b.*a
/([\w/]+)@
&&
2945 $line =~ m@
^\s
+diff
\b.*a
/[\w/]+\s
+b
/$1\b@
) ||
2946 $line =~ m@
^\s
*(?
:\
-\
-\
-\s
+a
/|\+\+\+\s+b/)@
||
2947 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2948 ERROR
("DIFF_IN_COMMIT_MSG",
2949 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2950 $commit_log_has_diff = 1;
2953 # Check for incorrect file permissions
2954 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2955 my $permhere = $here . "FILE: $realfile\n";
2956 if ($realfile !~ m
@scripts/@
&&
2957 $realfile !~ /\.(py|pl|awk|sh)$/) {
2958 ERROR
("EXECUTE_PERMISSIONS",
2959 "do not set execute permissions for source files\n" . $permhere);
2963 # Check the patch for a From:
2964 if (decode
("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2966 my $curline = $linenr;
2967 while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2970 $author = encode
("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2972 $author = reformat_email
($author);
2975 # Check the patch for a signoff:
2976 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
2979 if ($author ne '' && $authorsignoff != 1) {
2980 if (same_email_addresses
($1, $author)) {
2984 my ($email_name, $email_comment, $email_address, $comment1) = parse_email
($ctx);
2985 my ($author_name, $author_comment, $author_address, $comment2) = parse_email
($author);
2987 if (lc $email_address eq lc $author_address && $email_name eq $author_name) {
2990 } elsif (lc $email_address eq lc $author_address) {
2993 } elsif ($email_name eq $author_name) {
2997 my $address1 = $email_address;
2998 my $address2 = $author_address;
3000 if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
3003 if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
3006 if ($address1 eq $address2) {
3014 # Check for patch separator
3015 if ($line =~ /^---$/) {
3016 $has_patch_separator = 1;
3020 # Check if MAINTAINERS is being updated. If so, there's probably no need to
3021 # emit the "does MAINTAINERS need updating?" message on file add/move/delete
3022 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
3023 $reported_maintainer_file = 1;
3026 # Check signature styles
3027 if (!$in_header_lines &&
3028 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
3029 my $space_before = $1;
3031 my $space_after = $3;
3033 my $ucfirst_sign_off = ucfirst(lc($sign_off));
3035 if ($sign_off !~ /$signature_tags/) {
3036 my $suggested_signature = find_standard_signature
($sign_off);
3037 if ($suggested_signature eq "") {
3038 WARN
("BAD_SIGN_OFF",
3039 "Non-standard signature: $sign_off\n" . $herecurr);
3041 if (WARN
("BAD_SIGN_OFF",
3042 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
3044 $fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
3048 if (defined $space_before && $space_before ne "") {
3049 if (WARN
("BAD_SIGN_OFF",
3050 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
3052 $fixed[$fixlinenr] =
3053 "$ucfirst_sign_off $email";
3056 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
3057 if (WARN
("BAD_SIGN_OFF",
3058 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
3060 $fixed[$fixlinenr] =
3061 "$ucfirst_sign_off $email";
3065 if (!defined $space_after || $space_after ne " ") {
3066 if (WARN
("BAD_SIGN_OFF",
3067 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
3069 $fixed[$fixlinenr] =
3070 "$ucfirst_sign_off $email";
3074 my ($email_name, $name_comment, $email_address, $comment) = parse_email
($email);
3075 my $suggested_email = format_email
(($email_name, $name_comment, $email_address, $comment));
3076 if ($suggested_email eq "") {
3077 ERROR
("BAD_SIGN_OFF",
3078 "Unrecognized email address: '$email'\n" . $herecurr);
3080 my $dequoted = $suggested_email;
3081 $dequoted =~ s/^"//;
3082 $dequoted =~ s/" </ </;
3083 # Don't force email to have quotes
3084 # Allow just an angle bracketed address
3085 if (!same_email_addresses
($email, $suggested_email)) {
3086 if (WARN
("BAD_SIGN_OFF",
3087 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
3089 $fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
3093 # Address part shouldn't have comments
3094 my $stripped_address = $email_address;
3095 $stripped_address =~ s/\([^\(\)]*\)//g;
3096 if ($email_address ne $stripped_address) {
3097 if (WARN
("BAD_SIGN_OFF",
3098 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
3100 $fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
3104 # Only one name comment should be allowed
3105 my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
3106 if ($comment_count > 1) {
3107 WARN
("BAD_SIGN_OFF",
3108 "Use a single name comment in email: '$email'\n" . $herecurr);
3112 # stable@vger.kernel.org or stable@kernel.org shouldn't
3113 # have an email name. In addition comments should strictly
3115 if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
3116 if (($comment ne "" && $comment !~ /^#.+/) ||
3117 ($email_name ne "")) {
3118 my $cur_name = $email_name;
3119 my $new_comment = $comment;
3120 $cur_name =~ s/[a-zA-Z\s\-\"]+//g;
3122 # Remove brackets enclosing comment text
3123 # and # from start of comments to get comment text
3124 $new_comment =~ s/^\((.*)\)$/$1/;
3125 $new_comment =~ s/^\[(.*)\]$/$1/;
3126 $new_comment =~ s/^[\s\#]+|\s+$//g;
3128 $new_comment = trim
("$new_comment $cur_name") if ($cur_name ne $new_comment);
3129 $new_comment = " # $new_comment" if ($new_comment ne "");
3130 my $new_email = "$email_address$new_comment";
3132 if (WARN
("BAD_STABLE_ADDRESS_STYLE",
3133 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
3135 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3138 } elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
3139 my $new_comment = $comment;
3141 # Extract comment text from within brackets or
3142 # c89 style /*...*/ comments
3143 $new_comment =~ s/^\[(.*)\]$/$1/;
3144 $new_comment =~ s/^\/\*(.*)\*\/$/$1/;
3146 $new_comment = trim
($new_comment);
3147 $new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
3148 $new_comment = "($new_comment)" if ($new_comment ne "");
3149 my $new_email = format_email
($email_name, $name_comment, $email_address, $new_comment);
3151 if (WARN
("BAD_SIGN_OFF",
3152 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
3154 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3159 # Check for duplicate signatures
3160 my $sig_nospace = $line;
3161 $sig_nospace =~ s/\s//g;
3162 $sig_nospace = lc($sig_nospace);
3163 if (defined $signatures{$sig_nospace}) {
3164 WARN
("BAD_SIGN_OFF",
3165 "Duplicate signature\n" . $herecurr);
3167 $signatures{$sig_nospace} = 1;
3170 # Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
3171 if ($sign_off =~ /^co-developed-by:$/i) {
3172 if ($email eq $author) {
3173 WARN
("BAD_SIGN_OFF",
3174 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
3176 if (!defined $lines[$linenr]) {
3177 WARN
("BAD_SIGN_OFF",
3178 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
3179 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
3180 WARN
("BAD_SIGN_OFF",
3181 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
3182 } elsif ($1 ne $email) {
3183 WARN
("BAD_SIGN_OFF",
3184 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
3189 # Check email subject for common tools that don't need to be mentioned
3190 if ($in_header_lines &&
3191 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
3192 WARN
("EMAIL_SUBJECT",
3193 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
3196 # Check for Gerrit Change-Ids not in any patch context
3197 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
3198 if (ERROR
("GERRIT_CHANGE_ID",
3199 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
3201 fix_delete_line
($fixlinenr, $rawline);
3205 # Check if the commit log is in a possible stack dump
3206 if ($in_commit_log && !$commit_log_possible_stack_dump &&
3207 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
3208 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
3210 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
3211 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
3212 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
3213 # stack dump address styles
3214 $commit_log_possible_stack_dump = 1;
3217 # Check for line lengths > 75 in commit log, warn once
3218 if ($in_commit_log && !$commit_log_long_line &&
3219 length($line) > 75 &&
3220 !($line =~ /^\s*[a-zA-Z0-9_\/\
.]+\s
+\
|\s
+\d
+/ ||
3221 # file delta changes
3222 $line =~ /^\s*(?:[\w\.\-\+]*\/)++[\w\
.\
-\
+]+:/ ||
3224 $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i ||
3225 # A Fixes: or Link: line or signature tag line
3226 $commit_log_possible_stack_dump)) {
3227 WARN
("COMMIT_LOG_LONG_LINE",
3228 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
3229 $commit_log_long_line = 1;
3232 # Reset possible stack dump if a blank line is found
3233 if ($in_commit_log && $commit_log_possible_stack_dump &&
3235 $commit_log_possible_stack_dump = 0;
3238 # Check for lines starting with a #
3239 if ($in_commit_log && $line =~ /^#/) {
3240 if (WARN
("COMMIT_COMMENT_SYMBOL",
3241 "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
3243 $fixed[$fixlinenr] =~ s/^/ /;
3247 # Check for git id commit length and improperly formed commit descriptions
3248 # A correctly formed commit description is:
3249 # commit <SHA-1 hash length 12+ chars> ("Complete commit subject")
3250 # with the commit subject '("' prefix and '")' suffix
3251 # This is a fairly compilicated block as it tests for what appears to be
3252 # bare SHA-1 hash with minimum length of 5. It also avoids several types of
3253 # possible SHA-1 matches.
3254 # A commit match can span multiple lines so this block attempts to find a
3255 # complete typical commit on a maximum of 3 lines
3256 if ($perl_version_ok &&
3257 $in_commit_log && !$commit_log_possible_stack_dump &&
3258 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
3259 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
3260 (($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
3261 ($line =~ /\bcommit\s*$/i && defined($rawlines[$linenr]) && $rawlines[$linenr] =~ /^\s*[0-9a-f]{5,}\b/i)) ||
3262 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
3263 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
3264 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
3265 my $init_char = "c";
3266 my $orig_commit = "";
3271 my $id = '0123456789ab';
3272 my $orig_desc = "commit description";
3273 my $description = "";
3274 my $herectx = $herecurr;
3279 if ($line =~ /(?:\bcommit\s+[0-9a-f]{5,}|\bcommit\s*$)/i) {
3280 for (my $n = 0; $n < 2; $n++) {
3281 if ($input =~ /\bcommit\s+[0-9a-f]{5,}\s*($balanced_parens)/i) {
3284 # Always strip leading/trailing parens then double quotes if existing
3285 $orig_desc = substr($orig_desc, 1, -1);
3286 if ($orig_desc =~ /^".*"$/) {
3287 $orig_desc = substr($orig_desc, 1, -1);
3292 last if ($#lines < $linenr + $n);
3293 $input .= " " . trim
($rawlines[$linenr + $n]);
3294 $herectx .= "$rawlines[$linenr + $n]\n";
3296 $herectx = $herecurr if (!$has_parens);
3299 if ($input =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
3301 $orig_commit = lc($2);
3302 $short = 0 if ($input =~ /\bcommit\s+[0-9a-f]{12,40}/i);
3303 $long = 1 if ($input =~ /\bcommit\s+[0-9a-f]{41,}/i);
3304 $space = 0 if ($input =~ /\bcommit [0-9a-f]/i);
3305 $case = 0 if ($input =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
3306 } elsif ($input =~ /\b([0-9a-f]{12,40})\b/i) {
3307 $orig_commit = lc($1);
3310 ($id, $description) = git_commit_info
($orig_commit,
3314 ($short || $long || $space || $case || ($orig_desc ne $description) || !$has_quotes) &&
3315 $last_git_commit_id_linenr != $linenr - 1) {
3316 ERROR
("GIT_COMMIT_ID",
3317 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herectx);
3319 #don't report the next line if this line ends in commit and the sha1 hash is the next line
3320 $last_git_commit_id_linenr = $linenr if ($line =~ /\bcommit\s*$/i);
3323 # Check for added, moved or deleted files
3324 if (!$reported_maintainer_file && !$in_commit_log &&
3325 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
3326 $line =~ /^rename (?:from|to) [\w\/\
.\
-]+\s
*$/ ||
3327 ($line =~ /\{\s*([\w\/\
.\
-]*)\s
*\
=\
>\s
*([\w\
/\.\-]*)\s*\}/ &&
3328 (defined($1) || defined($2))))) {
3330 $reported_maintainer_file = 1;
3331 WARN
("FILE_PATH_CHANGES",
3332 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
3335 # Check for adding new DT bindings not in schema format
3336 if (!$in_commit_log &&
3337 ($line =~ /^new file mode\s*\d+\s*$/) &&
3338 ($realfile =~ m@
^Documentation
/devicetree/bindings
/.*\
.txt
$@
)) {
3339 WARN
("DT_SCHEMA_BINDING_PATCH",
3340 "DT bindings should be in DT schema format. See: Documentation/devicetree/bindings/writing-schema.rst\n");
3343 # Check for wrappage within a valid hunk of the file
3344 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
3345 ERROR
("CORRUPTED_PATCH",
3346 "patch seems to be corrupt (line wrapped?)\n" .
3347 $herecurr) if (!$emitted_corrupt++);
3350 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3351 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
3352 $rawline !~ m/^$UTF8*$/) {
3353 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3355 my $blank = copy_spacing
($rawline);
3356 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3357 my $hereptr = "$hereline$ptr\n";
3360 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
3363 # Check if it's the start of a commit log
3364 # (not a header line and we haven't seen the patch filename)
3365 if ($in_header_lines && $realfile =~ /^$/ &&
3366 !($rawline =~ /^\s+(?:\S|$)/ ||
3367 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
3368 $in_header_lines = 0;
3370 $has_commit_log = 1;
3373 # Check if there is UTF-8 in a commit log when a mail header has explicitly
3374 # declined it, i.e defined some charset where it is missing.
3375 if ($in_header_lines &&
3376 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3378 $non_utf8_charset = 1;
3381 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
3382 $rawline =~ /$NON_ASCII_UTF8/) {
3383 WARN
("UTF8_BEFORE_PATCH",
3384 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3387 # Check for absolute kernel paths in commit message
3388 if ($tree && $in_commit_log) {
3389 while ($line =~ m{(?:^|\s)(/\S*)}g) {
3392 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3393 check_absolute_file
($1, $herecurr)) {
3396 check_absolute_file
($file, $herecurr);
3401 # Check for various typo / spelling mistakes
3402 if (defined($misspellings) &&
3403 # OpenOCD specific: Begin: don't check spelling on spelling_file
3404 index($spelling_file, $realfile) + length($realfile) != length($spelling_file) &&
3405 # OpenOCD specific: End
3406 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
3407 while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
3409 my $blank = copy_spacing
($rawline);
3410 my $ptr = substr($blank, 0, $-[1]) . "^" x
length($typo);
3411 my $hereptr = "$hereline$ptr\n";
3412 my $typo_fix = $spelling_fix{lc($typo)};
3413 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3414 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
3415 my $msg_level = \
&WARN
;
3416 $msg_level = \
&CHK
if ($file);
3417 if (&{$msg_level}("TYPO_SPELLING",
3418 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
3420 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3425 # check for invalid commit id
3426 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3429 ($id, $description) = git_commit_info
($2, undef, undef);
3430 if (!defined($id)) {
3431 WARN
("UNKNOWN_COMMIT_ID",
3432 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3436 # check for repeated words separated by a single space
3437 # avoid false positive from list command eg, '-rw-r--r-- 1 root root'
3438 if (($rawline =~ /^\+/ || $in_commit_log) &&
3439 $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
3440 pos($rawline) = 1 if (!$in_commit_log);
3441 while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3445 my $start_pos = $-[1];
3446 my $end_pos = $+[2];
3447 if ($first =~ /(?:struct|union|enum)/) {
3448 pos($rawline) += length($first) + length($second) + 1;
3452 next if (lc($first) ne lc($second));
3453 next if ($first eq 'long');
3455 # check for character before and after the word matches
3456 my $start_char = '';
3458 $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ?
0 : 1));
3459 $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
3461 next if ($start_char =~ /^\S$/);
3462 next if (index(" \t.,;?!", $end_char) == -1);
3464 # avoid repeating hex occurrences like 'ff ff fe 09 ...'
3465 if ($first =~ /\b[0-9a-f]{2,}\b/i) {
3466 next if (!exists($allow_repeated_words{lc($first)}));
3469 if (WARN
("REPEATED_WORD",
3470 "Possible repeated word: '$first'\n" . $herecurr) &&
3472 $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3476 # if it's a repeated word on consecutive lines in a comment block
3477 if ($prevline =~ /$;+\s*$/ &&
3478 $prevrawline =~ /($word_pattern)\s*$/) {
3480 if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3481 if (WARN
("REPEATED_WORD",
3482 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3484 $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3490 # ignore non-hunk lines and lines being removed
3491 next if (!$hunk_line || $line =~ /^-/);
3493 #trailing whitespace
3494 if ($line =~ /^\+.*\015/) {
3495 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
3496 if (ERROR
("DOS_LINE_ENDINGS",
3497 "DOS line endings\n" . $herevet) &&
3499 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
3501 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3502 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
3503 if (ERROR
("TRAILING_WHITESPACE",
3504 "trailing whitespace\n" . $herevet) &&
3506 $fixed[$fixlinenr] =~ s/\s+$//;
3512 # Check for FSF mailing addresses.
3513 if ($rawline =~ /\bwrite to the Free/i ||
3514 $rawline =~ /\b675\s+Mass\s+Ave/i ||
3515 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3516 $rawline =~ /\b51\s+Franklin\s+St/i) {
3517 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
3518 my $msg_level = \
&ERROR
;
3519 $msg_level = \
&CHK
if ($file);
3520 &{$msg_level}("FSF_MAILING_ADDRESS",
3521 "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. OpenOCD already includes a copy of the GPL.\n" . $herevet)
3524 # check for Kconfig help text having a real description
3525 # Only applies when adding the entry originally, after that we do not have
3526 # sufficient context to determine whether it is indeed long enough.
3527 if ($realfile =~ /Kconfig/ &&
3528 # 'choice' is usually the last thing on the line (though
3529 # Kconfig supports named choices), so use a word boundary
3530 # (\b) rather than a whitespace character (\s)
3531 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
3535 my $help_length = 0;
3536 while (defined $lines[$ln]) {
3537 my $f = $lines[$ln++];
3539 next if ($f =~ /^-/);
3540 last if ($f !~ /^[\+ ]/); # !patch context
3542 if ($f =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
3546 if ($f =~ /^\+\s*help\s*$/) {
3551 $f =~ s/^.//; # strip patch context [+ ]
3552 $f =~ s/#.*//; # strip # directives
3553 $f =~ s/^\s+//; # strip leading blanks
3554 next if ($f =~ /^$/); # skip blank lines
3556 # At the end of this Kconfig block:
3557 # This only checks context lines in the patch
3558 # and so hopefully shouldn't trigger false
3559 # positives, even though some of these are
3560 # common words in help texts
3561 if ($f =~ /^(?
:config
|menuconfig
|choice
|endchoice
|
3562 if|endif
|menu
|endmenu
|source
)\b/x
) {
3565 $help_length++ if ($has_help);
3568 $help_length < $min_conf_desc_length) {
3569 my $stat_real = get_stat_real
($linenr, $ln - 1);
3570 WARN
("CONFIG_DESCRIPTION",
3571 "please write a help paragraph that fully describes the config symbol\n" . "$here\n$stat_real\n");
3575 # check MAINTAINERS entries
3576 if ($realfile =~ /^MAINTAINERS$/) {
3577 # check MAINTAINERS entries for the right form
3578 if ($rawline =~ /^\+[A-Z]:/ &&
3579 $rawline !~ /^\+[A-Z]:\t\S/) {
3580 if (WARN
("MAINTAINERS_STYLE",
3581 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3583 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3586 # check MAINTAINERS entries for the right ordering too
3587 my $preferred_order = 'MRLSWQBCPTFXNK';
3588 if ($rawline =~ /^\+[A-Z]:/ &&
3589 $prevrawline =~ /^[\+ ][A-Z]:/) {
3590 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3593 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3596 my $curindex = index($preferred_order, $cur);
3597 my $previndex = index($preferred_order, $prev);
3598 if ($curindex < 0) {
3599 WARN
("MAINTAINERS_STYLE",
3600 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3602 if ($previndex >= 0 && $curindex < $previndex) {
3603 WARN
("MAINTAINERS_STYLE",
3604 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3605 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3606 ($prev eq 'X' && $cur eq 'X')) &&
3607 ($prevval cmp $curval) > 0) {
3608 WARN
("MAINTAINERS_STYLE",
3609 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3615 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3616 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3619 'EXTRA_AFLAGS' => 'asflags-y',
3620 'EXTRA_CFLAGS' => 'ccflags-y',
3621 'EXTRA_CPPFLAGS' => 'cppflags-y',
3622 'EXTRA_LDFLAGS' => 'ldflags-y',
3625 WARN
("DEPRECATED_VARIABLE",
3626 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3629 # check for DT compatible documentation
3630 if (defined $root &&
3631 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3632 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3634 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3636 my $dt_path = $root . "/Documentation/devicetree/bindings/";
3637 my $vp_file = $dt_path . "vendor-prefixes.yaml";
3639 foreach my $compat (@compats) {
3640 my $compat2 = $compat;
3641 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3642 my $compat3 = $compat;
3643 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3644 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3646 WARN
("UNDOCUMENTED_DT_STRING",
3647 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3650 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3652 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
3654 WARN
("UNDOCUMENTED_DT_STRING",
3655 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3660 # check for using SPDX license tag at beginning of files
3661 if ($realline == $checklicenseline) {
3662 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3663 $checklicenseline = 2;
3664 } elsif ($rawline =~ /^\+/) {
3666 if ($realfile =~ /\.(h|s|S)$/) {
3668 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3670 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
3672 } elsif ($realfile =~ /\.rst$/) {
3674 # OpenOCD specific: Begin
3675 } elsif ($realfile =~ /\.(am|cfg|tcl)$/) {
3677 # OpenOCD specific: End
3680 # check SPDX comment style for .[chsS] files
3681 if ($realfile =~ /\.[chsS]$/ &&
3682 $rawline =~ /SPDX-License-Identifier:/ &&
3683 $rawline !~ m@
^\
+\s
*\Q
$comment\E\s
*@
) {
3684 WARN
("SPDX_LICENSE_TAG",
3685 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3688 if ($comment !~ /^$/ &&
3689 $rawline !~ m@
^\
+\Q
$comment\E SPDX
-License
-Identifier
: @
) {
3690 WARN
("SPDX_LICENSE_TAG",
3691 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
3692 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
3693 my $spdx_license = $1;
3694 if (!is_SPDX_License_valid
($spdx_license)) {
3695 WARN
("SPDX_LICENSE_TAG",
3696 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3698 if ($realfile =~ m@
^Documentation
/devicetree/bindings
/@
&&
3699 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3700 my $msg_level = \
&WARN
;
3701 $msg_level = \
&CHK
if ($file);
3702 if (&{$msg_level}("SPDX_LICENSE_TAG",
3704 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3706 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3713 # check for embedded filenames
3714 if ($rawline =~ /^\+.*\Q$realfile\E/) {
3715 WARN
("EMBEDDED_FILENAME",
3716 "It's generally not useful to have the filename in the file\n" . $herecurr);
3719 # check we are in a valid source file if not then ignore this hunk
3720 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
3722 # check for using SPDX-License-Identifier on the wrong line number
3723 if ($realline != $checklicenseline &&
3724 $rawline =~ /\bSPDX-License-Identifier:/ &&
3725 substr($line, @
-, @
+ - @
-) eq "$;" x
(@
+ - @
-)) {
3726 WARN
("SPDX_LICENSE_TAG",
3727 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3730 # line length limit (with some exclusions)
3732 # There are a few types of lines that may extend beyond $max_line_length:
3733 # logging functions like pr_info that end in a string
3734 # lines with a single string
3735 # #defines that are a single string
3736 # lines with an RFC3986 like URL
3738 # There are 3 different line length message types:
3739 # LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
3740 # LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3741 # LONG_LINE all other lines longer than $max_line_length
3743 # if LONG_LINE is ignored, the other 2 types are also ignored
3746 if ($line =~ /^\+/ && $length > $max_line_length) {
3747 my $msg_type = "LONG_LINE";
3749 # Check the allowed long line types first
3751 # logging functions that end in a string that starts
3752 # before $max_line_length
3753 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3754 length(expand_tabs
(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3757 # lines with only strings (w/ possible termination)
3758 # #defines with only strings
3759 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3760 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3763 # More special cases
3764 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3765 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3768 # URL ($rawline is used in case the URL is in a comment)
3769 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\
/\S+/i) {
3772 # Otherwise set the alternate message types
3774 # a comment starts before $max_line_length
3775 } elsif ($line =~ /($;[\s$;]*)$/ &&
3776 length(expand_tabs
(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3777 $msg_type = "LONG_LINE_COMMENT"
3779 # a quoted string starts before $max_line_length
3780 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3781 length(expand_tabs
(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3782 $msg_type = "LONG_LINE_STRING"
3785 if ($msg_type ne "" &&
3786 (show_type
("LONG_LINE") || show_type
($msg_type))) {
3787 my $msg_level = \
&WARN
;
3788 $msg_level = \
&CHK
if ($file);
3789 &{$msg_level}($msg_type,
3790 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
3794 # check for adding lines without a newline.
3795 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3796 if (WARN
("MISSING_EOF_NEWLINE",
3797 "adding a line without newline at end of file\n" . $herecurr) &&
3799 fix_delete_line
($fixlinenr+1, "No newline at end of file");
3803 # check for .L prefix local symbols in .S files
3804 if ($realfile =~ /\.S$/ &&
3805 $line =~ /^\+\s*(?:[A-Z]+_)?SYM_[A-Z]+_(?:START|END)(?:_[A-Z_]+)?\s*\(\s*\.L/) {
3806 WARN
("AVOID_L_PREFIX",
3807 "Avoid using '.L' prefixed local symbol names for denoting a range of code via 'SYM_*_START/END' annotations; see Documentation/asm-annotations.rst\n" . $herecurr);
3810 # check we are in a valid source file C or perl if not then ignore this hunk
3811 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
3813 # at the beginning of a line any tabs must come first and anything
3814 # more than $tabsize must use tabs.
3815 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3816 $rawline =~ /^\+\s* \s*/) {
3817 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
3819 if (ERROR
("CODE_INDENT",
3820 "code indent should use tabs where possible\n" . $herevet) &&
3822 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
3826 # check for space before tabs.
3827 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3828 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
3829 if (WARN
("SPACE_BEFORE_TAB",
3830 "please, no space before tabs\n" . $herevet) &&
3832 while ($fixed[$fixlinenr] =~
3833 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
3834 while ($fixed[$fixlinenr] =~
3835 s/(^\+.*) +\t/$1\t/) {}
3839 # check for assignments on the start of a line
3840 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3842 if (CHK
("ASSIGNMENT_CONTINUATIONS",
3843 "Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3844 $fix && $prevrawline =~ /^\+/) {
3845 # add assignment operator to the previous line, remove from current line
3846 $fixed[$fixlinenr - 1] .= " $operator";
3847 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3851 # check for && or || at the start of a line
3852 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3854 if (CHK
("LOGICAL_CONTINUATIONS",
3855 "Logical continuations should be on the previous line\n" . $hereprev) &&
3856 $fix && $prevrawline =~ /^\+/) {
3857 # insert logical operator at last non-comment, non-whitepsace char on previous line
3858 $prevline =~ /[\s$;]*$/;
3859 my $line_end = substr($prevrawline, $-[0]);
3860 $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
3861 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3865 # check indentation starts on a tab stop
3866 if ($perl_version_ok &&
3867 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3868 my $indent = length($1);
3869 if ($indent % $tabsize) {
3871 "Statements should start on a tabstop\n" . $herecurr) &&
3873 $fixed[$fixlinenr] =~ s@
(^\
+\t+) +@
$1 . "\t" x
($indent/$tabsize)@e;
3878 # check multi-line statement indentation matches previous line
3879 if ($perl_version_ok &&
3880 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3881 $prevline =~ /^\+(\t*)(.*)$/;
3885 my $pos = pos_last_openparen
($rest);
3887 $line =~ /^(\+| )([ \t]*)/;
3890 my $goodtabindent = $oldindent .
3891 "\t" x
($pos / $tabsize) .
3892 " " x
($pos % $tabsize);
3893 my $goodspaceindent = $oldindent . " " x
$pos;
3895 if ($newindent ne $goodtabindent &&
3896 $newindent ne $goodspaceindent) {
3898 if (CHK
("PARENTHESIS_ALIGNMENT",
3899 "Alignment should match open parenthesis\n" . $hereprev) &&
3900 $fix && $line =~ /^\+/) {
3901 $fixed[$fixlinenr] =~
3902 s/^\+[ \t]*/\+$goodtabindent/;
3908 # check for space after cast like "(int) foo" or "(struct foo) bar"
3909 # avoid checking a few false positives:
3910 # "sizeof(<type>)" or "__alignof__(<type>)"
3911 # function pointer declarations like "(*foo)(int) = bar;"
3912 # structure definitions like "(struct foo) { 0 };"
3913 # multiline macros that define functions
3914 # known attributes or the __attribute__ keyword
3915 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3916 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
3918 "No space is necessary after a cast\n" . $herecurr) &&
3920 $fixed[$fixlinenr] =~
3921 s/(\(\s*$Type\s*\))[ \t]+/$1/;
3925 # Block comment styles
3926 # Networking with an initial /*
3927 if ($realfile =~ m@
^(drivers
/net
/|net
/)@
&&
3928 $prevrawline =~ /^\+[ \t]*\/\
*[ \t]*$/ &&
3929 $rawline =~ /^\+[ \t]*\*/ &&
3930 $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
3931 WARN
("NETWORKING_BLOCK_COMMENT_STYLE",
3932 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3935 # Block comments use * on subsequent lines
3936 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3937 $prevrawline =~ /^\+.*?\/\
*/ && #starting /*
3938 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
3939 $rawline =~ /^\+/ && #line is new
3940 $rawline !~ /^\+[ \t]*\*/) { #no leading *
3941 WARN
("BLOCK_COMMENT_STYLE",
3942 "Block comments use * on subsequent lines\n" . $hereprev);
3945 # Block comments use */ on trailing lines
3946 if ($rawline !~ m@
^\
+[ \t]*\
*/[ \t]*$@ && #trailing */
3947 $rawline !~ m@
^\
+.*/\*.*\*/[ \t]*$@
&& #inline /*...*/
3948 $rawline !~ m@
^\
+.*\
*{2,}/[ \t]*$@ && #trailing **/
3949 $rawline =~ m@
^\
+[ \t]*.+\
*\
/[ \t]*$@) { #non blank */
3950 WARN
("BLOCK_COMMENT_STYLE",
3951 "Block comments use a trailing */ on a separate line\n" . $herecurr);
3954 # Block comment * alignment
3955 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3956 $line =~ /^\+[ \t]*$;/ && #leading comment
3957 $rawline =~ /^\+[ \t]*\*/ && #leading *
3958 (($prevrawline =~ /^\+.*?\/\
*/ && #leading /*
3959 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
3960 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3962 $prevrawline =~ m@
^\
+([ \t]*/?
)\
*@
;
3964 $oldindent = expand_tabs
($1);
3966 $prevrawline =~ m@
^\
+(.*/?
)\
*@
;
3967 $oldindent = expand_tabs
($1);
3969 $rawline =~ m@
^\
+([ \t]*)\
*@
;
3971 $newindent = expand_tabs
($newindent);
3972 if (length($oldindent) ne length($newindent)) {
3973 WARN
("BLOCK_COMMENT_STYLE",
3974 "Block comments should align the * on each line\n" . $hereprev);
3978 # check for missing blank lines after struct/union declarations
3979 # with exceptions for various attributes and macros
3980 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3982 !($line =~ /^\+\s*$/ ||
3983 $line =~ /^\+\s*(?:EXPORT_SYMBOL|early_param)/ ||
3984 $line =~ /^\+\s*MODULE_/i ||
3985 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3986 $line =~ /^\+[a-z_]*init/ ||
3987 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3988 $line =~ /^\+\s*DECLARE/ ||
3989 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
3990 $line =~ /^\+\s*__setup/)) {
3991 if (CHK
("LINE_SPACING",
3992 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3994 fix_insert_line
($fixlinenr, "\+");
3998 # check for multiple consecutive blank lines
3999 if ($prevline =~ /^[\+ ]\s*$/ &&
4000 $line =~ /^\+\s*$/ &&
4001 $last_blank_line != ($linenr - 1)) {
4002 if (CHK
("LINE_SPACING",
4003 "Please don't use multiple blank lines\n" . $hereprev) &&
4005 fix_delete_line
($fixlinenr, $rawline);
4008 $last_blank_line = $linenr;
4011 # check for missing blank lines after declarations
4012 # (declarations must have the same indentation and not be at the start of line)
4013 if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) {
4017 # remove $Attribute/$Sparse uses to simplify comparisons
4018 $sl =~ s/\b(?:$Attribute|$Sparse)\b//g;
4019 $pl =~ s/\b(?:$Attribute|$Sparse)\b//g;
4020 if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
4021 # function pointer declarations
4022 $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
4023 # foo bar; where foo is some local typedef or #define
4024 $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
4025 # known declaration macros
4026 $pl =~ /^\+\s+$declaration_macros/) &&
4027 # for "else if" which can look like "$Ident $Ident"
4028 !($pl =~ /^\+\s+$c90_Keywords\b/ ||
4029 # other possible extensions of declaration lines
4030 $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
4031 # not starting a section or a macro "\" extended line
4032 $pl =~ /(?:\{\s*|\\)$/) &&
4033 # looks like a declaration
4034 !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
4035 # function pointer declarations
4036 $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
4037 # foo bar; where foo is some local typedef or #define
4038 $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
4039 # known declaration macros
4040 $sl =~ /^\+\s+$declaration_macros/ ||
4041 # start of struct or union or enum
4042 $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
4043 # start or end of block or continuation of declaration
4044 $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
4045 # bitfield continuation
4046 $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
4047 # other possible extensions of declaration lines
4048 $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) {
4049 if (WARN
("LINE_SPACING",
4050 "Missing a blank line after declarations\n" . $hereprev) &&
4052 fix_insert_line
($fixlinenr, "\+");
4057 # check for spaces at the beginning of a line.
4059 # 1) within comments
4060 # 2) indented preprocessor commands
4062 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
4063 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
4064 if (WARN
("LEADING_SPACE",
4065 "please, no spaces at the start of a line\n" . $herevet) &&
4067 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
4071 # check we are in a valid C source file if not then ignore this hunk
4072 next if ($realfile !~ /\.(h|c)$/);
4074 # check for unusual line ending [ or (
4075 if ($line =~ /^\+.*([\[\(])\s*$/) {
4076 CHK
("OPEN_ENDED_LINE",
4077 "Lines should not end with a '$1'\n" . $herecurr);
4080 # check if this appears to be the start function declaration, save the name
4081 if ($sline =~ /^\+\{\s*$/ &&
4082 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
4083 $context_function = $1;
4086 # check if this appears to be the end of function declaration
4087 if ($sline =~ /^\+\}\s*$/) {
4088 undef $context_function;
4091 # check indentation of any line with a bare else
4092 # (but not if it is a multiple line "if (foo) return bar; else return baz;")
4093 # if the previous line is a break or return and is indented 1 tab more...
4094 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
4095 my $tabs = length($1) + 1;
4096 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
4097 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
4098 defined $lines[$linenr] &&
4099 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
4100 WARN
("UNNECESSARY_ELSE",
4101 "else is not generally useful after a break or return\n" . $hereprev);
4105 # check indentation of a line with a break;
4106 # if the previous line is a goto, return or break
4107 # and is indented the same # of tabs
4108 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
4110 if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
4111 if (WARN
("UNNECESSARY_BREAK",
4112 "break is not useful after a $1\n" . $hereprev) &&
4114 fix_delete_line
($fixlinenr, $rawline);
4119 # check for RCS/CVS revision markers
4120 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
4122 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
4125 # check for old HOTPLUG __dev<foo> section markings
4126 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
4127 WARN
("HOTPLUG_SECTION",
4128 "Using $1 is unnecessary\n" . $herecurr);
4131 # Check for potential 'bare' types
4132 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
4134 #print "LINE<$line>\n";
4135 if ($linenr > $suppress_statement &&
4136 $realcnt && $sline =~ /.\s*\S/) {
4137 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4138 ctx_statement_block
($linenr, $realcnt, 0);
4139 $stat =~ s/\n./\n /g;
4140 $cond =~ s/\n./\n /g;
4142 #print "linenr<$linenr> <$stat>\n";
4143 # If this statement has no statement boundaries within
4144 # it there is no point in retrying a statement scan
4145 # until we hit end of it.
4146 my $frag = $stat; $frag =~ s/;+\s*$//;
4147 if ($frag !~ /(?:{|;)/) {
4148 #print "skip<$line_nr_next>\n";
4149 $suppress_statement = $line_nr_next;
4152 # Find the real next line.
4153 $realline_next = $line_nr_next;
4154 if (defined $realline_next &&
4155 (!defined $lines[$realline_next - 1] ||
4156 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
4163 # Ignore goto labels.
4164 if ($s =~ /$Ident:\*$/s) {
4166 # Ignore functions being called
4167 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
4169 } elsif ($s =~ /^.\s*else\b/s) {
4171 # declarations always start with types
4172 } 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) {
4175 possible
($type, "A:" . $s);
4177 # definitions in global scope can only start with types
4178 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
4179 possible
($1, "B:" . $s);
4182 # any (foo ... *) is a pointer cast, and foo is a type
4183 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
4184 possible
($1, "C:" . $s);
4187 # Check for any sort of function declaration.
4188 # int foo(something bar, other baz);
4189 # void (*store_gdt)(x86_descr_ptr *);
4190 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
4191 my ($name_len) = length($1);
4194 substr($ctx, 0, $name_len + 1, '');
4195 $ctx =~ s/\)[^\)]*$//;
4197 for my $arg (split(/\s*,\s*/, $ctx)) {
4198 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
4200 possible
($1, "D:" . $s);
4208 # Checks which may be anchored in the context.
4211 # Check for switch () and associated case and default
4212 # statements should be at the same indent.
4213 if ($line=~/\bswitch\s*\(.*\)/) {
4216 my @ctx = ctx_block_outer
($linenr, $realcnt);
4218 for my $ctx (@ctx) {
4219 my ($clen, $cindent) = line_stats
($ctx);
4220 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
4221 $indent != $cindent) {
4222 $err .= "$sep$ctx\n";
4229 ERROR
("SWITCH_CASE_INDENT_LEVEL",
4230 "switch and case should be at the same indent\n$hereline$err");
4234 # if/while/etc brace do not go on next line, unless defining a do while loop,
4235 # or if that brace on the next line is for something else
4236 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
4237 my $pre_ctx = "$1$2";
4239 my ($level, @ctx) = ctx_statement_level
($linenr, $realcnt, 0);
4241 # OpenOCD specific: Begin: replace s/6/10/
4242 if ($line =~ /^\+\t{10,}/) {
4243 # OpenOCD specific: End
4244 WARN
("DEEP_INDENTATION",
4245 "Too many leading tabs - consider code refactoring\n" . $herecurr);
4248 my $ctx_cnt = $realcnt - $#ctx - 1;
4249 my $ctx = join("\n", @ctx);
4251 my $ctx_ln = $linenr;
4252 my $ctx_skip = $realcnt;
4254 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
4255 defined $lines[$ctx_ln - 1] &&
4256 $lines[$ctx_ln - 1] =~ /^-/)) {
4257 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
4258 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
4262 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
4263 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
4265 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
4267 "that open brace { should be on the previous line\n" .
4268 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
4270 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
4271 $ctx =~ /\)\s*\;\s*$/ &&
4272 defined $lines[$ctx_ln - 1])
4274 my ($nlength, $nindent) = line_stats
($lines[$ctx_ln - 1]);
4275 if ($nindent > $indent) {
4276 WARN
("TRAILING_SEMICOLON",
4277 "trailing semicolon indicates no statements, indent implies otherwise\n" .
4278 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
4283 # Check relative indent for conditionals and blocks.
4284 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
4285 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4286 ctx_statement_block
($linenr, $realcnt, 0)
4287 if (!defined $stat);
4288 my ($s, $c) = ($stat, $cond);
4290 substr($s, 0, length($c), '');
4292 # remove inline comments
4296 # Find out how long the conditional actually is.
4297 my @newlines = ($c =~ /\n/gs);
4298 my $cond_lines = 1 + $#newlines;
4300 # Make sure we remove the line prefixes as we have
4301 # none on the first line, and are going to readd them
4304 while ($s =~ /\n\s+\\\n/) {
4305 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
4308 # We want to check the first line inside the block
4309 # starting at the end of the conditional, so remove:
4310 # 1) any blank line termination
4311 # 2) any opening brace { on end of the line
4313 my $continuation = 0;
4315 $s =~ s/^.*\bdo\b//;
4317 if ($s =~ s/^\s*\\//) {
4320 if ($s =~ s/^\s*?\n//) {
4325 # Also ignore a loop construct at the end of a
4326 # preprocessor statement.
4327 if (($prevline =~ /^.\s*#\s*define\s/ ||
4328 $prevline =~ /\\\s*$/) && $continuation == 0) {
4334 while ($cond_ptr != $cond_lines) {
4335 $cond_ptr = $cond_lines;
4337 # If we see an #else/#elif then the code
4339 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4344 # 1) blank lines, they should be at 0,
4345 # 2) preprocessor lines, and
4347 if ($continuation ||
4349 $s =~ /^\s*#\s*?/ ||
4350 $s =~ /^\s*$Ident\s*:/) {
4351 $continuation = ($s =~ /^.*?\\\n/) ?
1 : 0;
4352 if ($s =~ s/^.*?\n//) {
4358 my (undef, $sindent) = line_stats
("+" . $s);
4359 my $stat_real = raw_line
($linenr, $cond_lines);
4361 # Check if either of these lines are modified, else
4362 # this is not this patch's fault.
4363 if (!defined($stat_real) ||
4364 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
4367 if (defined($stat_real) && $cond_lines > 1) {
4368 $stat_real = "[...]\n$stat_real";
4371 #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";
4373 if ($check && $s ne '' &&
4374 (($sindent % $tabsize) != 0 ||
4375 ($sindent < $indent) ||
4376 ($sindent == $indent &&
4377 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
4378 ($sindent > $indent + $tabsize))) {
4379 WARN
("SUSPECT_CODE_INDENT",
4380 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
4384 # Track the 'values' across context and added lines.
4385 my $opline = $line; $opline =~ s/^./ /;
4386 my ($curr_values, $curr_vars) =
4387 annotate_values
($opline . "\n", $prev_values);
4388 $curr_values = $prev_values . $curr_values;
4390 my $outline = $opline; $outline =~ s/\t/ /g;
4391 print "$linenr > .$outline\n";
4392 print "$linenr > $curr_values\n";
4393 print "$linenr > $curr_vars\n";
4395 $prev_values = substr($curr_values, -1);
4397 #ignore lines not being added
4398 next if ($line =~ /^[^\+]/);
4400 # check for self assignments used to avoid compiler warnings
4401 # e.g.: int foo = foo, *bar = NULL;
4402 # struct foo bar = *(&(bar));
4403 if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
4405 if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
4406 WARN
("SELF_ASSIGNMENT",
4407 "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
4411 # check for dereferences that span multiple lines
4412 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
4413 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
4414 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
4416 $line =~ /^.\s*($Lval)/;
4419 WARN
("MULTILINE_DEREFERENCE",
4420 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
4423 # check for declarations of signed or unsigned without int
4424 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
4427 $var = "" if (!defined $var);
4428 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
4432 $pointer = "" if (!defined $pointer);
4434 if (WARN
("UNSPECIFIED_INT",
4435 "Prefer '" . trim
($sign) . " int" . rtrim
($pointer) . "' to bare use of '$sign" . rtrim
($pointer) . "'\n" . $herecurr) &&
4437 my $decl = trim
($sign) . " int ";
4438 my $comp_pointer = $pointer;
4439 $comp_pointer =~ s/\s//g;
4440 $decl .= $comp_pointer;
4441 $decl = rtrim
($decl) if ($var eq "");
4442 $fixed[$fixlinenr] =~ s@
\b$sign\s
*\Q
$pointer\E\s
*$var\b@
$decl$var@
;
4447 # TEST: allow direct testing of the type matcher.
4449 if ($line =~ /^.\s*$Declare\s*$/) {
4451 "TEST: is type\n" . $herecurr);
4452 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
4453 ERROR
("TEST_NOT_TYPE",
4454 "TEST: is not type ($1 is)\n". $herecurr);
4458 # TEST: allow direct testing of the attribute matcher.
4460 if ($line =~ /^.\s*$Modifier\s*$/) {
4462 "TEST: is attr\n" . $herecurr);
4463 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
4464 ERROR
("TEST_NOT_ATTR",
4465 "TEST: is not attr ($1 is)\n". $herecurr);
4470 # check for initialisation to aggregates open brace on the next line
4471 if ($line =~ /^.\s*{/ &&
4472 $prevline =~ /(?:^|[^=])=\s*$/) {
4473 if (ERROR
("OPEN_BRACE",
4474 "that open brace { should be on the previous line\n" . $hereprev) &&
4475 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4476 fix_delete_line
($fixlinenr - 1, $prevrawline);
4477 fix_delete_line
($fixlinenr, $rawline);
4478 my $fixedline = $prevrawline;
4479 $fixedline =~ s/\s*=\s*$/ = {/;
4480 fix_insert_line
($fixlinenr, $fixedline);
4482 $fixedline =~ s/^(.\s*)\{\s*/$1/;
4483 fix_insert_line
($fixlinenr, $fixedline);
4488 # Checks which are anchored on the added line.
4491 # check for malformed paths in #include statements (uses RAW line)
4492 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
4494 if ($path =~ m{//}) {
4495 ERROR
("MALFORMED_INCLUDE",
4496 "malformed #include filename\n" . $herecurr);
4498 if ($path =~ "^uapi/" && $realfile =~ m@
\binclude
/uapi
/@
) {
4499 ERROR
("UAPI_INCLUDE",
4500 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
4504 # no C99 // comments
4505 if ($line =~ m{//}) {
4506 if (ERROR
("C99_COMMENTS",
4507 "do not use C99 // comments\n" . $herecurr) &&
4509 my $line = $fixed[$fixlinenr];
4510 if ($line =~ /\/\
/(.*)$/) {
4511 my $comment = trim
($1);
4512 $fixed[$fixlinenr] =~ s@\
/\/(.*)$@
/\* $comment \*/@
;
4516 # Remove C99 comments.
4518 $opline =~ s@
//.*@@
;
4520 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4521 # the whole statement.
4522 #print "APW <$lines[$realline_next - 1]>\n";
4523 if (defined $realline_next &&
4524 exists $lines[$realline_next - 1] &&
4525 !defined $suppress_export{$realline_next} &&
4526 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
4527 # Handle definitions which produce identifiers with
4530 # EXPORT_SYMBOL(something_foo);
4532 $name =~ s/^\s*($Ident).*/$1/;
4533 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
4534 $name =~ /^${Ident}_$2/) {
4535 #print "FOO C name<$name>\n";
4536 $suppress_export{$realline_next} = 1;
4538 } elsif ($stat !~ /(?
:
4540 ^.DEFINE_
$Ident\
(\Q
$name\E\
)|
4541 ^.DECLARE_
$Ident\
(\Q
$name\E\
)|
4542 ^.LIST_HEAD\
(\Q
$name\E\
)|
4543 ^.(?
:$Storage\s
+)?
$Type\s
*\
(\s
*\
*\s
*\Q
$name\E\s
*\
)\s
*\
(|
4544 \b\Q
$name\E
(?
:\s
+$Attribute)*\s
*(?
:;|=|\
[|\
()
4546 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4547 $suppress_export{$realline_next} = 2;
4549 $suppress_export{$realline_next} = 1;
4552 if (!defined $suppress_export{$linenr} &&
4553 $prevline =~ /^.\s*$/ &&
4554 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
4555 #print "FOO B <$lines[$linenr - 1]>\n";
4556 $suppress_export{$linenr} = 2;
4558 if (defined $suppress_export{$linenr} &&
4559 $suppress_export{$linenr} == 2) {
4560 WARN
("EXPORT_SYMBOL",
4561 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
4564 # check for global initialisers.
4565 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ &&
4566 !exclude_global_initialisers
($realfile)) {
4567 if (ERROR
("GLOBAL_INITIALISERS",
4568 "do not initialise globals to $1\n" . $herecurr) &&
4570 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
4573 # check for static initialisers.
4574 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
4575 if (ERROR
("INITIALISED_STATIC",
4576 "do not initialise statics to $1\n" .
4579 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
4583 # check for misordered declarations of char/short/int/long with signed/unsigned
4584 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4586 WARN
("MISORDERED_TYPE",
4587 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4590 # check for unnecessary <signed> int declarations of short/long/long long
4591 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4592 my $type = trim
($1);
4593 next if ($type !~ /\bint\b/);
4594 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4595 my $new_type = $type;
4596 $new_type =~ s/\b\s*int\s*\b/ /;
4597 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4598 $new_type =~ s/^const\s+//;
4599 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4600 $new_type = "const $new_type" if ($type =~ /^const\b/);
4601 $new_type =~ s/\s+/ /g;
4602 $new_type = trim
($new_type);
4603 if (WARN
("UNNECESSARY_INT",
4604 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4606 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4610 # check for static const char * arrays.
4611 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4612 WARN
("STATIC_CONST_CHAR_ARRAY",
4613 "static const char * array should probably be static const char * const\n" .
4617 # check for initialized const char arrays that should be static const
4618 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4619 if (WARN
("STATIC_CONST_CHAR_ARRAY",
4620 "const array should probably be static const\n" . $herecurr) &&
4622 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4626 # check for static char foo[] = "bar" declarations.
4627 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4628 WARN
("STATIC_CONST_CHAR_ARRAY",
4629 "static char array declaration should probably be static const char\n" .
4633 # check for const <foo> const where <foo> is not a pointer or array type
4634 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4636 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4638 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4639 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4641 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4645 # check for const static or static <non ptr type> const declarations
4646 # prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
4647 if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
4648 $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
4649 if (WARN
("STATIC_CONST",
4650 "Move const after static - use 'static const $1'\n" . $herecurr) &&
4652 $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
4653 $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
4657 # check for non-global char *foo[] = {"bar", ...} declarations.
4658 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4659 WARN
("STATIC_CONST_CHAR_ARRAY",
4660 "char * array declaration might be better as static const\n" .
4664 # check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4665 if ($line =~ m@
\bsizeof\s
*\
(\s
*($Lval)\s
*\
)@
) {
4667 if ($line =~ m@
\b(sizeof\s
*\
(\s
*\Q
$array\E\s
*\
)\s
*/\s
*sizeof\s
*\
(\s
*\Q
$array\E\s
*\
[\s
*0\s
*\
]\s
*\
))@
) {
4669 if (WARN
("ARRAY_SIZE",
4670 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4672 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4677 # check for function declarations without arguments like "int foo()"
4678 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
4679 if (ERROR
("FUNCTION_WITHOUT_ARGS",
4680 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4682 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4686 # check for new typedefs, only function parameters and sparse annotations
4688 if ($line =~ /\btypedef\s/ &&
4689 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4690 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
4691 $line !~ /\b$typeTypedefs\b/ &&
4692 $line !~ /\b__bitwise\b/) {
4693 WARN
("NEW_TYPEDEFS",
4694 "do not add new typedefs\n" . $herecurr);
4697 # * goes on variable not on type
4699 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4701 my ($ident, $from, $to) = ($1, $2, $2);
4703 # Should start with a space.
4704 $to =~ s/^(\S)/ $1/;
4705 # Should not end with a space.
4707 # '*'s should not have spaces between.
4708 while ($to =~ s/\*\s+\*/\*\*/) {
4711 ## print "1: from<$from> to<$to> ident<$ident>\n";
4713 if (ERROR
("POINTER_LOCATION",
4714 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4716 my $sub_from = $ident;
4717 my $sub_to = $ident;
4718 $sub_to =~ s/\Q$from\E/$to/;
4719 $fixed[$fixlinenr] =~
4720 s@\Q
$sub_from\E@
$sub_to@
;
4724 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4726 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4728 # Should start with a space.
4729 $to =~ s/^(\S)/ $1/;
4730 # Should not end with a space.
4732 # '*'s should not have spaces between.
4733 while ($to =~ s/\*\s+\*/\*\*/) {
4735 # Modifiers should have spaces.
4736 $to =~ s/(\b$Modifier$)/$1 /;
4738 ## print "2: from<$from> to<$to> ident<$ident>\n";
4739 if ($from ne $to && $ident !~ /^$Modifier$/) {
4740 if (ERROR
("POINTER_LOCATION",
4741 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4744 my $sub_from = $match;
4745 my $sub_to = $match;
4746 $sub_to =~ s/\Q$from\E/$to/;
4747 $fixed[$fixlinenr] =~
4748 s@\Q
$sub_from\E@
$sub_to@
;
4753 # avoid BUG() or BUG_ON()
4754 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
4755 my $msg_level = \
&WARN
;
4756 $msg_level = \
&CHK
if ($file);
4757 &{$msg_level}("AVOID_BUG",
4758 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
4761 # avoid LINUX_VERSION_CODE
4762 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4763 WARN
("LINUX_VERSION_CODE",
4764 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
4767 # check for uses of printk_ratelimit
4768 if ($line =~ /\bprintk_ratelimit\s*\(/) {
4769 WARN
("PRINTK_RATELIMITED",
4770 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
4773 # printk should use KERN_* levels
4774 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4775 WARN
("PRINTK_WITHOUT_KERN_LEVEL",
4776 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
4779 # prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL>
4780 if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) {
4784 $modifier = "" if (!defined($modifier));
4785 my $level = lc($orig);
4786 $level = "warn" if ($level eq "warning");
4787 my $level2 = $level;
4788 $level2 = "dbg" if ($level eq "debug");
4789 $level .= $modifier;
4790 $level2 .= $modifier;
4791 WARN
("PREFER_PR_LEVEL",
4792 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to $printk(KERN_$orig ...\n" . $herecurr);
4795 # prefer dev_<level> to dev_printk(KERN_<LEVEL>
4796 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4798 my $level = lc($orig);
4799 $level = "warn" if ($level eq "warning");
4800 $level = "dbg" if ($level eq "debug");
4801 WARN
("PREFER_DEV_LEVEL",
4802 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4805 # trace_printk should not be used in production code.
4806 if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
4807 WARN
("TRACE_PRINTK",
4808 "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
4811 # ENOSYS means "bad syscall nr" and nothing else. This will have a small
4812 # number of false positives, but assembly files are not checked, so at
4813 # least the arch entry code will not trigger this warning.
4814 if ($line =~ /\bENOSYS\b/) {
4816 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4819 # ENOTSUPP is not a standard error code and should be avoided in new patches.
4820 # Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4821 # Similarly to ENOSYS warning a small number of false positives is expected.
4822 if (!$file && $line =~ /\bENOTSUPP\b/) {
4823 if (WARN
("ENOTSUPP",
4824 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4826 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4830 # function brace can't be on same line, except for #defines of do while,
4831 # or if closed on same line
4832 if ($perl_version_ok &&
4833 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4834 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4836 if (ERROR
("OPEN_BRACE",
4837 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
4839 fix_delete_line
($fixlinenr, $rawline);
4840 my $fixed_line = $rawline;
4841 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
4844 fix_insert_line
($fixlinenr, ltrim
($line1));
4845 fix_insert_line
($fixlinenr, "\+{");
4846 if ($line2 !~ /^\s*$/) {
4847 fix_insert_line
($fixlinenr, "\+\t" . trim
($line2));
4852 # open braces for enum, union and struct go on the same line.
4853 if ($line =~ /^.\s*{/ &&
4854 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
4855 if (ERROR
("OPEN_BRACE",
4856 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4857 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4858 fix_delete_line
($fixlinenr - 1, $prevrawline);
4859 fix_delete_line
($fixlinenr, $rawline);
4860 my $fixedline = rtrim
($prevrawline) . " {";
4861 fix_insert_line
($fixlinenr, $fixedline);
4862 $fixedline = $rawline;
4863 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
4864 if ($fixedline !~ /^\+\s*$/) {
4865 fix_insert_line
($fixlinenr, $fixedline);
4870 # missing space after union, struct or enum definition
4871 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4873 "missing space after $1 definition\n" . $herecurr) &&
4875 $fixed[$fixlinenr] =~
4876 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4880 # Function pointer declarations
4881 # check spacing between type, funcptr, and args
4882 # canonical declaration is "type (*funcptr)(args...)"
4883 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
4885 my $pre_pointer_space = $2;
4886 my $post_pointer_space = $3;
4888 my $post_funcname_space = $5;
4889 my $pre_args_space = $6;
4891 # the $Declare variable will capture all spaces after the type
4892 # so check it for a missing trailing missing space but pointer return types
4893 # don't need a space so don't warn for those.
4894 my $post_declare_space = "";
4895 if ($declare =~ /(\s+)$/) {
4896 $post_declare_space = $1;
4897 $declare = rtrim
($declare);
4899 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
4901 "missing space after return type\n" . $herecurr);
4902 $post_declare_space = " ";
4905 # unnecessary space "type (*funcptr)(args...)"
4906 # This test is not currently implemented because these declarations are
4908 # int foo(int bar, ...)
4909 # and this is form shouldn't/doesn't generate a checkpatch warning.
4911 # elsif ($declare =~ /\s{2,}$/) {
4913 # "Multiple spaces after return type\n" . $herecurr);
4916 # unnecessary space "type ( *funcptr)(args...)"
4917 if (defined $pre_pointer_space &&
4918 $pre_pointer_space =~ /^\s/) {
4920 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4923 # unnecessary space "type (* funcptr)(args...)"
4924 if (defined $post_pointer_space &&
4925 $post_pointer_space =~ /^\s/) {
4927 "Unnecessary space before function pointer name\n" . $herecurr);
4930 # unnecessary space "type (*funcptr )(args...)"
4931 if (defined $post_funcname_space &&
4932 $post_funcname_space =~ /^\s/) {
4934 "Unnecessary space after function pointer name\n" . $herecurr);
4937 # unnecessary space "type (*funcptr) (args...)"
4938 if (defined $pre_args_space &&
4939 $pre_args_space =~ /^\s/) {
4941 "Unnecessary space before function pointer arguments\n" . $herecurr);
4944 if (show_type
("SPACING") && $fix) {
4945 $fixed[$fixlinenr] =~
4946 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
4950 # check for spacing round square brackets; allowed:
4951 # 1. with a type on the left -- int [] a;
4952 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4953 # 3. inside a curly brace -- = { [0...10] = 5 }
4954 while ($line =~ /(.*?\s)\[/g) {
4955 my ($where, $prefix) = ($-[1], $1);
4956 if ($prefix !~ /$Type\s+$/ &&
4957 ($where != 0 || $prefix !~ /^.\s+$/) &&
4958 $prefix !~ /[{,:]\s+$/) {
4959 if (ERROR
("BRACKET_SPACE",
4960 "space prohibited before open square bracket '['\n" . $herecurr) &&
4962 $fixed[$fixlinenr] =~
4963 s/^(\+.*?)\s+\[/$1\[/;
4968 # check for spaces between functions and their parentheses.
4969 while ($line =~ /($Ident)\s+\(/g) {
4971 my $ctx_before = substr($line, 0, $-[1]);
4972 my $ctx = "$ctx_before$name";
4974 # Ignore those directives where spaces _are_ permitted.
4976 if|for|while|switch
|return|case
|
4977 volatile
|__volatile__
|
4978 __attribute__
|format
|__extension__
|
4981 # cpp #define statements have non-optional spaces, ie
4982 # if there is a space between the name and the open
4983 # parenthesis it is simply not a parameter group.
4984 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4986 # cpp #elif statement condition may start with a (
4987 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4989 # If this whole things ends with a type its most
4990 # likely a typedef for a function.
4991 } elsif ($ctx =~ /$Type$/) {
4995 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4997 $fixed[$fixlinenr] =~
4998 s/\b$name\s+\(/$name\(/;
5003 # Check operator spacing.
5004 if (!($line=~/\#\s*include/)) {
5005 my $fixed_line = "";
5009 <<=|>>=|<=|>=|==|!=|
5010 \
+=|-=|\
*=|\
/=|%=|\
^=|\
|=|&=|
5011 =>|->|<<|>>|<|>|=|!|~|
5012 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%|
5015 my @elements = split(/($ops|;)/, $opline);
5017 ## print("element count: <" . $#elements . ">\n");
5018 ## foreach my $el (@elements) {
5019 ## print("el: <$el>\n");
5022 my @fix_elements = ();
5025 foreach my $el (@elements) {
5026 push(@fix_elements, substr($rawline, $off, length($el)));
5027 $off += length($el);
5032 my $blank = copy_spacing
($opline);
5033 my $last_after = -1;
5035 for (my $n = 0; $n < $#elements; $n += 2) {
5037 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
5039 ## print("n: <$n> good: <$good>\n");
5041 $off += length($elements[$n]);
5043 # Pick up the preceding and succeeding characters.
5044 my $ca = substr($opline, 0, $off);
5046 if (length($opline) >= ($off + length($elements[$n + 1]))) {
5047 $cc = substr($opline, $off + length($elements[$n + 1]));
5049 my $cb = "$ca$;$cc";
5052 $a = 'V' if ($elements[$n] ne '');
5053 $a = 'W' if ($elements[$n] =~ /\s$/);
5054 $a = 'C' if ($elements[$n] =~ /$;$/);
5055 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
5056 $a = 'O' if ($elements[$n] eq '');
5057 $a = 'E' if ($ca =~ /^\s*$/);
5059 my $op = $elements[$n + 1];
5062 if (defined $elements[$n + 2]) {
5063 $c = 'V' if ($elements[$n + 2] ne '');
5064 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
5065 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
5066 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
5067 $c = 'O' if ($elements[$n + 2] eq '');
5068 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
5073 my $ctx = "${a}x${c}";
5075 my $at = "(ctx:$ctx)";
5077 my $ptr = substr($blank, 0, $off) . "^";
5078 my $hereptr = "$hereline$ptr\n";
5080 # Pull out the value of this operator.
5081 my $op_type = substr($curr_values, $off + 1, 1);
5083 # Get the full operator variant.
5084 my $opv = $op . substr($curr_vars, $off, 1);
5086 # Ignore operators passed as parameters.
5087 if ($op_type ne 'V' &&
5088 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
5091 # } elsif ($op =~ /^$;+$/) {
5093 # ; should have either the end of line or a space or \ after it
5094 } elsif ($op eq ';') {
5095 if ($ctx !~ /.x[WEBC]/ &&
5096 $cc !~ /^\\/ && $cc !~ /^;/) {
5097 if (ERROR
("SPACING",
5098 "space required after that '$op' $at\n" . $hereptr)) {
5099 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]) . " ";
5105 } elsif ($op eq '//') {
5107 # : when part of a bitfield
5108 } elsif ($opv eq ':B') {
5109 # skip the bitfield test for now
5113 } elsif ($op eq '->') {
5114 if ($ctx =~ /Wx.|.xW/) {
5115 if (ERROR
("SPACING",
5116 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
5117 $good = rtrim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
5118 if (defined $fix_elements[$n + 2]) {
5119 $fix_elements[$n + 2] =~ s/^\s+//;
5125 # , must not have a space before and must have a space on the right.
5126 } elsif ($op eq ',') {
5127 my $rtrim_before = 0;
5128 my $space_after = 0;
5129 if ($ctx =~ /Wx./) {
5130 if (ERROR
("SPACING",
5131 "space prohibited before that '$op' $at\n" . $hereptr)) {
5136 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
5137 if (ERROR
("SPACING",
5138 "space required after that '$op' $at\n" . $hereptr)) {
5144 if ($rtrim_before || $space_after) {
5145 if ($rtrim_before) {
5146 $good = rtrim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
5148 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]);
5155 # '*' as part of a type definition -- reported already.
5156 } elsif ($opv eq '*_') {
5157 #warn "'*' is part of type\n";
5159 # unary operators should have a space before and
5160 # none after. May be left adjacent to another
5161 # unary operator, or a cast
5162 } elsif ($op eq '!' || $op eq '~' ||
5163 $opv eq '*U' || $opv eq '-U' ||
5164 $opv eq '&U' || $opv eq '&&U') {
5165 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
5166 if (ERROR
("SPACING",
5167 "space required before that '$op' $at\n" . $hereptr)) {
5168 if ($n != $last_after + 2) {
5169 $good = $fix_elements[$n] . " " . ltrim
($fix_elements[$n + 1]);
5174 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
5175 # A unary '*' may be const
5177 } elsif ($ctx =~ /.xW/) {
5178 if (ERROR
("SPACING",
5179 "space prohibited after that '$op' $at\n" . $hereptr)) {
5180 $good = $fix_elements[$n] . rtrim
($fix_elements[$n + 1]);
5181 if (defined $fix_elements[$n + 2]) {
5182 $fix_elements[$n + 2] =~ s/^\s+//;
5188 # unary ++ and unary -- are allowed no space on one side.
5189 } elsif ($op eq '++' or $op eq '--') {
5190 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
5191 if (ERROR
("SPACING",
5192 "space required one side of that '$op' $at\n" . $hereptr)) {
5193 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]) . " ";
5197 if ($ctx =~ /Wx[BE]/ ||
5198 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
5199 if (ERROR
("SPACING",
5200 "space prohibited before that '$op' $at\n" . $hereptr)) {
5201 $good = rtrim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
5205 if ($ctx =~ /ExW/) {
5206 if (ERROR
("SPACING",
5207 "space prohibited after that '$op' $at\n" . $hereptr)) {
5208 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]);
5209 if (defined $fix_elements[$n + 2]) {
5210 $fix_elements[$n + 2] =~ s/^\s+//;
5216 # << and >> may either have or not have spaces both sides
5217 } elsif ($op eq '<<' or $op eq '>>' or
5218 $op eq '&' or $op eq '^' or $op eq '|' or
5219 $op eq '+' or $op eq '-' or
5220 $op eq '*' or $op eq '/' or
5224 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
5226 "spaces preferred around that '$op' $at\n" . $hereptr)) {
5227 $good = rtrim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]) . " ";
5228 $fix_elements[$n + 2] =~ s/^\s+//;
5231 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
5233 "space preferred before that '$op' $at\n" . $hereptr)) {
5234 $good = rtrim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]);
5238 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
5239 if (ERROR
("SPACING",
5240 "need consistent spacing around '$op' $at\n" . $hereptr)) {
5241 $good = rtrim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]) . " ";
5242 if (defined $fix_elements[$n + 2]) {
5243 $fix_elements[$n + 2] =~ s/^\s+//;
5249 # A colon needs no spaces before when it is
5250 # terminating a case value or a label.
5251 } elsif ($opv eq ':C' || $opv eq ':L') {
5252 if ($ctx =~ /Wx./ and $realfile !~ m@
.*\
.lds\
.h
$@
) {
5253 if (ERROR
("SPACING",
5254 "space prohibited before that '$op' $at\n" . $hereptr)) {
5255 $good = rtrim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
5260 # All the others need spaces both sides.
5261 } elsif ($ctx !~ /[EWC]x[CWE]/) {
5264 # Ignore email addresses <foo@bar>
5266 $cc =~ /^\S+\@\S+>/) ||
5268 $ca =~ /<\S+\@\S+$/))
5273 # for asm volatile statements
5274 # ignore a colon with another
5275 # colon immediately before or after
5277 ($ca =~ /:$/ || $cc =~ /^:/)) {
5281 # messages are ERROR, but ?: are CHK
5283 my $msg_level = \
&ERROR
;
5284 $msg_level = \
&CHK
if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
5286 if (&{$msg_level}("SPACING",
5287 "spaces required around that '$op' $at\n" . $hereptr)) {
5288 $good = rtrim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]) . " ";
5289 if (defined $fix_elements[$n + 2]) {
5290 $fix_elements[$n + 2] =~ s/^\s+//;
5296 $off += length($elements[$n + 1]);
5298 ## print("n: <$n> GOOD: <$good>\n");
5300 $fixed_line = $fixed_line . $good;
5303 if (($#elements % 2) == 0) {
5304 $fixed_line = $fixed_line . $fix_elements[$#elements];
5307 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
5308 $fixed[$fixlinenr] = $fixed_line;
5314 # check for whitespace before a non-naked semicolon
5315 if ($line =~ /^\+.*\S\s+;\s*$/) {
5317 "space prohibited before semicolon\n" . $herecurr) &&
5319 1 while $fixed[$fixlinenr] =~
5320 s/^(\+.*\S)\s+;/$1;/;
5324 # check for multiple assignments
5325 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
5326 CHK
("MULTIPLE_ASSIGNMENTS",
5327 "multiple assignments should be avoided\n" . $herecurr);
5330 ## # check for multiple declarations, allowing for a function declaration
5332 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
5333 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
5335 ## # Remove any bracketed sections to ensure we do not
5336 ## # falsely report the parameters of functions.
5338 ## while ($ln =~ s/\([^\(\)]*\)//g) {
5340 ## if ($ln =~ /,/) {
5341 ## WARN("MULTIPLE_DECLARATION",
5342 ## "declaring multiple variables together should be avoided\n" . $herecurr);
5346 #need space before brace following if, while, etc
5347 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
5348 $line =~ /\b(?:else|do)\{/) {
5349 if (ERROR
("SPACING",
5350 "space required before the open brace '{'\n" . $herecurr) &&
5352 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
5356 ## # check for blank lines before declarations
5357 ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5358 ## $prevrawline =~ /^.\s*$/) {
5360 ## "No blank lines before declarations\n" . $hereprev);
5364 # closing brace should have a space following it when it has anything
5366 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
5367 if (ERROR
("SPACING",
5368 "space required after that close brace '}'\n" . $herecurr) &&
5370 $fixed[$fixlinenr] =~
5371 s/}((?!(?:,|;|\)))\S)/} $1/;
5375 # check spacing on square brackets
5376 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
5377 if (ERROR
("SPACING",
5378 "space prohibited after that open square bracket '['\n" . $herecurr) &&
5380 $fixed[$fixlinenr] =~
5384 if ($line =~ /\s\]/) {
5385 if (ERROR
("SPACING",
5386 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
5388 $fixed[$fixlinenr] =~
5393 # check spacing on parentheses
5394 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
5395 $line !~ /for\s*\(\s+;/) {
5396 if (ERROR
("SPACING",
5397 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
5399 $fixed[$fixlinenr] =~
5403 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
5404 $line !~ /for\s*\(.*;\s+\)/ &&
5405 $line !~ /:\s+\)/) {
5406 if (ERROR
("SPACING",
5407 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
5409 $fixed[$fixlinenr] =~
5414 # check unnecessary parentheses around addressof/dereference single $Lvals
5415 # ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5417 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
5419 if (CHK
("UNNECESSARY_PARENTHESES",
5420 "Unnecessary parentheses around $var\n" . $herecurr) &&
5422 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5426 # check for unnecessary parentheses around function pointer uses
5427 # ie: (foo->bar)(); should be foo->bar();
5428 # but not "if (foo->bar) (" to avoid some false positives
5429 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5431 if (CHK
("UNNECESSARY_PARENTHESES",
5432 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5434 my $var2 = deparenthesize
($var);
5436 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5440 # check for unnecessary parentheses around comparisons in if uses
5441 # when !drivers/staging or command-line uses --strict
5442 if (($realfile !~ m@
^(?
:drivers
/staging/)@
|| $check_orig) &&
5443 $perl_version_ok && defined($stat) &&
5444 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
5446 my $test = substr($2, 1, -1);
5448 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
5450 # avoid parentheses around potential macro args
5451 next if ($match =~ /^\s*\w+\s*$/);
5452 if (!defined($herectx)) {
5453 $herectx = $here . "\n";
5454 my $cnt = statement_rawlines
($if_stat);
5455 for (my $n = 0; $n < $cnt; $n++) {
5456 my $rl = raw_line
($linenr, $n);
5457 $herectx .= $rl . "\n";
5458 last if $rl =~ /^[ \+].*\{/;
5461 CHK
("UNNECESSARY_PARENTHESES",
5462 "Unnecessary parentheses around '$match'\n" . $herectx);
5466 # check that goto labels aren't indented (allow a single space indentation)
5467 # and ignore bitfield definitions like foo:1
5468 # Strictly, labels can have whitespace after the identifier and before the :
5469 # but this is not allowed here as many ?: uses would appear to be labels
5470 if ($sline =~ /^.\s+[A-Za-z_][A-Za-z\d_]*:(?!\s*\d+)/ &&
5471 $sline !~ /^. [A-Za-z\d_][A-Za-z\d_]*:/ &&
5472 $sline !~ /^.\s+default:/) {
5473 if (WARN
("INDENTED_LABEL",
5474 "labels should not be indented\n" . $herecurr) &&
5476 $fixed[$fixlinenr] =~
5481 # check if a statement with a comma should be two statements like:
5482 # foo = bar(), /* comma should be semicolon */
5484 if (defined($stat) &&
5485 $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
5486 my $cnt = statement_rawlines
($stat);
5487 my $herectx = get_stat_here
($linenr, $cnt, $here);
5488 WARN
("SUSPECT_COMMA_SEMICOLON",
5489 "Possible comma where semicolon could be used\n" . $herectx);
5492 # return is not a function
5493 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
5495 if ($perl_version_ok &&
5496 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
5498 $value = deparenthesize
($value);
5499 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5500 ERROR
("RETURN_PARENTHESES",
5501 "return is not a function, parentheses are not required\n" . $herecurr);
5503 } elsif ($spacing !~ /\s+/) {
5505 "space required before the open parenthesis '('\n" . $herecurr);
5509 # unnecessary return in a void function
5510 # at end-of-function, with the previous line a single leading tab, then return;
5511 # and the line before that not a goto label target like "out:"
5512 if ($sline =~ /^[ \+]}\s*$/ &&
5513 $prevline =~ /^\+\treturn\s*;\s*$/ &&
5515 $lines[$linenr - 3] =~ /^[ +]/ &&
5516 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
5518 "void function return statements are not generally useful\n" . $hereprev);
5521 # if statements using unnecessary parentheses - ie: if ((foo == bar))
5522 if ($perl_version_ok &&
5523 $line =~ /\bif\s*((?:\(\s*){2,})/) {
5524 my $openparens = $1;
5525 my $count = $openparens =~ tr@\
(@\
(@
;
5527 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5528 my $comp = $4; #Not $1 because of $LvalOrFunc
5529 $msg = " - maybe == should be = ?" if ($comp eq "==");
5530 WARN
("UNNECESSARY_PARENTHESES",
5531 "Unnecessary parentheses$msg\n" . $herecurr);
5535 # comparisons with a constant or upper case identifier on the left
5536 # avoid cases like "foo + BAR < baz"
5537 # only fix matches surrounded by parentheses to avoid incorrect
5538 # conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
5539 if ($perl_version_ok &&
5540 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5545 my $newcomp = $comp;
5546 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
5547 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5548 WARN
("CONSTANT_COMPARISON",
5549 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5553 } elsif ($comp eq "<=") {
5555 } elsif ($comp eq ">") {
5557 } elsif ($comp eq ">=") {
5560 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5564 # Return of what appears to be an errno should normally be negative
5565 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
5567 if ($name ne 'EOF' && $name ne 'ERROR' && $name !~ /^EPOLL/) {
5568 WARN
("USE_NEGATIVE_ERRNO",
5569 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
5573 # Need a space before open parenthesis after if, while etc
5574 if ($line =~ /\b(if|while|for|switch)\(/) {
5575 if (ERROR
("SPACING",
5576 "space required before the open parenthesis '('\n" . $herecurr) &&
5578 $fixed[$fixlinenr] =~
5579 s/\b(if|while|for|switch)\(/$1 \(/;
5583 # Check for illegal assignment in if conditional -- and check for trailing
5584 # statements after the conditional.
5585 if ($line =~ /do\s*(?!{)/) {
5586 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5587 ctx_statement_block
($linenr, $realcnt, 0)
5588 if (!defined $stat);
5589 my ($stat_next) = ctx_statement_block
($line_nr_next,
5590 $remain_next, $off_next);
5591 $stat_next =~ s/\n./\n /g;
5592 ##print "stat<$stat> stat_next<$stat_next>\n";
5594 if ($stat_next =~ /^\s*while\b/) {
5595 # If the statement carries leading newlines,
5596 # then count those as offsets.
5598 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5600 statement_rawlines
($whitespace) - 1;
5602 $suppress_whiletrailers{$line_nr_next +
5606 if (!defined $suppress_whiletrailers{$linenr} &&
5607 defined($stat) && defined($cond) &&
5608 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
5609 my ($s, $c) = ($stat, $cond);
5610 my $fixed_assign_in_if = 0;
5612 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
5613 if (ERROR
("ASSIGN_IN_IF",
5614 "do not use assignment in if condition\n" . $herecurr) &&
5615 $fix && $perl_version_ok) {
5616 if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
5624 fix_delete_line
($fixlinenr, $rawline);
5625 fix_insert_line
($fixlinenr, "$space$statement;");
5626 my $newline = "${space}if (";
5627 $newline .= '!' if defined($not);
5628 $newline .= '(' if (defined $not && defined($test) && defined($against));
5629 $newline .= "$assigned";
5630 $newline .= " $test $against" if (defined($test) && defined($against));
5631 $newline .= ')' if (defined $not && defined($test) && defined($against));
5633 $newline .= " {" if (defined($brace));
5634 fix_insert_line
($fixlinenr + 1, $newline);
5635 $fixed_assign_in_if = 1;
5640 # Find out what is on the end of the line after the
5642 substr($s, 0, length($c), '');
5644 $s =~ s/$;//g; # Remove any comments
5645 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5646 $c !~ /}\s*while\s*/)
5648 # Find out how long the conditional actually is.
5649 my @newlines = ($c =~ /\n/gs);
5650 my $cond_lines = 1 + $#newlines;
5653 $stat_real = raw_line
($linenr, $cond_lines)
5654 . "\n" if ($cond_lines);
5655 if (defined($stat_real) && $cond_lines > 1) {
5656 $stat_real = "[...]\n$stat_real";
5659 if (ERROR
("TRAILING_STATEMENTS",
5660 "trailing statements should be on next line\n" . $herecurr . $stat_real) &&
5661 !$fixed_assign_in_if &&
5663 $fix && $perl_version_ok &&
5664 $fixed[$fixlinenr] =~ /^\+(\s*)((?:if|while|for)\s*$balanced_parens)\s*(.*)$/) {
5667 my $rest = rtrim
($4);
5668 if ($rest =~ /;$/) {
5669 $fixed[$fixlinenr] = "\+$indent$test";
5670 fix_insert_line
($fixlinenr + 1, "$indent\t$rest");
5676 # Check for bitwise tests written as boolean
5688 WARN
("HEXADECIMAL_BOOLEAN_TEST",
5689 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
5692 # if and else should not have general statements after it
5693 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5695 $s =~ s/$;//g; # Remove any comments
5696 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5697 ERROR
("TRAILING_STATEMENTS",
5698 "trailing statements should be on next line\n" . $herecurr);
5701 # if should not continue a brace
5702 if ($line =~ /}\s*if\b/) {
5703 ERROR
("TRAILING_STATEMENTS",
5704 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
5707 # case and default should not have general statements after them
5708 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5710 (?
:\s
*$;*)(?
:\s
*{)?
(?
:\s
*$;*)(?
:\s
*\\)?\s
*$|
5714 ERROR
("TRAILING_STATEMENTS",
5715 "trailing statements should be on next line\n" . $herecurr);
5718 # Check for }<nl>else {, these must be at the same
5719 # indent level to be relevant to each other.
5720 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5721 $previndent == $indent) {
5722 if (ERROR
("ELSE_AFTER_BRACE",
5723 "else should follow close brace '}'\n" . $hereprev) &&
5724 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5725 fix_delete_line
($fixlinenr - 1, $prevrawline);
5726 fix_delete_line
($fixlinenr, $rawline);
5727 my $fixedline = $prevrawline;
5728 $fixedline =~ s/}\s*$//;
5729 if ($fixedline !~ /^\+\s*$/) {
5730 fix_insert_line
($fixlinenr, $fixedline);
5732 $fixedline = $rawline;
5733 $fixedline =~ s/^(.\s*)else/$1} else/;
5734 fix_insert_line
($fixlinenr, $fixedline);
5738 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5739 $previndent == $indent) {
5740 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
5742 # Find out what is on the end of the line after the
5744 substr($s, 0, length($c), '');
5747 if ($s =~ /^\s*;/) {
5748 if (ERROR
("WHILE_AFTER_BRACE",
5749 "while should follow close brace '}'\n" . $hereprev) &&
5750 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5751 fix_delete_line
($fixlinenr - 1, $prevrawline);
5752 fix_delete_line
($fixlinenr, $rawline);
5753 my $fixedline = $prevrawline;
5754 my $trailing = $rawline;
5755 $trailing =~ s/^\+//;
5756 $trailing = trim
($trailing);
5757 $fixedline =~ s/}\s*$/} $trailing/;
5758 fix_insert_line
($fixlinenr, $fixedline);
5763 #Specific variable tests
5764 while ($line =~ m{($Constant|$Lval)}g) {
5768 if ($var !~ /^$Constant$/ &&
5769 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
5770 #Ignore some autogenerated defines and enum values
5771 $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
5772 #Ignore Page<foo> variants
5773 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5774 #Ignore SI style variants like nS, mV and dB
5775 #(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5776 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
5777 #Ignore some three character SI units explicitly, like MiB and KHz
5778 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
5779 while ($var =~ m{\b($Ident)}g) {
5781 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5783 seed_camelcase_includes
();
5784 if (!$file && !$camelcase_file_seeded) {
5785 seed_camelcase_file
($realfile);
5786 $camelcase_file_seeded = 1;
5789 if (!defined $camelcase{$word}) {
5790 $camelcase{$word} = 1;
5792 "Avoid CamelCase: <$word>\n" . $herecurr);
5798 #no spaces allowed after \ in define
5799 if ($line =~ /\#\s*define.*\\\s+$/) {
5800 if (WARN
("WHITESPACE_AFTER_LINE_CONTINUATION",
5801 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5803 $fixed[$fixlinenr] =~ s/\s+$//;
5807 # warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5808 # itself <asm/foo.h> (uses RAW line)
5809 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5811 my $checkfile = "include/linux/$file";
5812 if (-f
"$root/$checkfile" &&
5813 $realfile ne $checkfile &&
5814 $1 !~ /$allowed_asm_includes/)
5816 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5817 if ($asminclude > 0) {
5818 if ($realfile =~ m{^arch/}) {
5819 CHK
("ARCH_INCLUDE_LINUX",
5820 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5822 WARN
("INCLUDE_LINUX",
5823 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5829 # multi-statement macros should be enclosed in a do while loop, grab the
5830 # first statement and ensure its the whole macro if its not enclosed
5831 # in a known good container
5832 if ($realfile !~ m@
/vmlinux
.lds
.h
$@
&&
5833 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5836 my ($off, $dstat, $dcond, $rest);
5838 my $has_flow_statement = 0;
5839 my $has_arg_concat = 0;
5840 ($dstat, $dcond, $ln, $cnt, $off) =
5841 ctx_statement_block
($linenr, $realcnt, 0);
5843 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5844 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5846 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
5847 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
5849 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5850 my $define_args = $1;
5851 my $define_stmt = $dstat;
5854 if (defined $define_args && $define_args ne "") {
5855 $define_args = substr($define_args, 1, length($define_args) - 2);
5856 $define_args =~ s/\s*//g;
5857 $define_args =~ s/\\\+?//g;
5858 @def_args = split(",", $define_args);
5862 $dstat =~ s/\\\n.//g;
5863 $dstat =~ s/^\s*//s;
5864 $dstat =~ s/\s*$//s;
5866 # Flatten any parentheses and braces
5867 while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
5868 $dstat =~ s/\{[^\{\}]*\}/1u/ ||
5869 $dstat =~ s/.\[[^\[\]]*\]/1u/)
5873 # Flatten any obvious string concatenation.
5874 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5875 $dstat =~ s/$Ident\s*($String)/$1/)
5879 # Make asm volatile uses seem like a generic function
5880 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5882 my $exceptions = qr{
5895 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5898 my $stmt_cnt = statement_rawlines
($ctx);
5899 my $herectx = get_stat_here
($linenr, $stmt_cnt, $here);
5902 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5903 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
5904 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5905 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
5906 $dstat !~ /$exceptions/ &&
5907 $dstat !~ /^\.$Ident\s*=/ && # .foo =
5908 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
5909 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
5910 $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...}
5911 $dstat !~ /^for\s*$Constant$/ && # for (...)
5912 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5913 $dstat !~ /^do\s*{/ && # do {...
5914 $dstat !~ /^\(\{/ && # ({...
5915 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5917 if ($dstat =~ /^\s*if\b/) {
5918 ERROR
("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5919 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5920 } elsif ($dstat =~ /;/) {
5921 ERROR
("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5922 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5924 ERROR
("COMPLEX_MACRO",
5925 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5930 # Make $define_stmt single line, comment-free, etc
5931 my @stmt_array = split('\n', $define_stmt);
5934 foreach my $l (@stmt_array) {
5939 } elsif ($l =~ /^[\+ ]/) {
5940 $define_stmt .= substr($l, 1);
5943 $define_stmt =~ s/$;//g;
5944 $define_stmt =~ s/\s+/ /g;
5945 $define_stmt = trim
($define_stmt);
5947 # check if any macro arguments are reused (ignore '...' and 'type')
5948 foreach my $arg (@def_args) {
5949 next if ($arg =~ /\.\.\./);
5950 next if ($arg =~ /^type$/i);
5951 my $tmp_stmt = $define_stmt;
5952 $tmp_stmt =~ s/\b(__must_be_array|offsetof|sizeof|sizeof_field|__stringify|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
5953 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5954 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
5955 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5957 CHK
("MACRO_ARG_REUSE",
5958 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5960 # check if any macro arguments may have other precedence issues
5961 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
5962 ((defined($1) && $1 ne ',') ||
5963 (defined($2) && $2 ne ','))) {
5964 CHK
("MACRO_ARG_PRECEDENCE",
5965 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
5969 # check for macros with flow control, but without ## concatenation
5970 # ## concatenation is commonly a macro that defines a function so ignore those
5971 if ($has_flow_statement && !$has_arg_concat) {
5972 my $cnt = statement_rawlines
($ctx);
5973 my $herectx = get_stat_here
($linenr, $cnt, $here);
5975 WARN
("MACRO_WITH_FLOW_CONTROL",
5976 "Macros with flow control statements should be avoided\n" . "$herectx");
5979 # check for line continuations outside of #defines, preprocessor #, and asm
5982 if ($prevline !~ /^..*\\$/ &&
5983 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5984 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
5985 $line =~ /^\+.*\\$/) {
5986 WARN
("LINE_CONTINUATIONS",
5987 "Avoid unnecessary line continuations\n" . $herecurr);
5991 # do {} while (0) macro tests:
5992 # single-statement macros do not need to be enclosed in do while (0) loop,
5993 # macro should not end with a semicolon
5994 if ($perl_version_ok &&
5995 $realfile !~ m@
/vmlinux
.lds
.h
$@
&&
5996 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5999 my ($off, $dstat, $dcond, $rest);
6001 ($dstat, $dcond, $ln, $cnt, $off) =
6002 ctx_statement_block
($linenr, $realcnt, 0);
6005 $dstat =~ s/\\\n.//g;
6008 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
6013 my $cnt = statement_rawlines
($ctx);
6014 my $herectx = get_stat_here
($linenr, $cnt, $here);
6016 if (($stmts =~ tr/;/;/) == 1 &&
6017 $stmts !~ /^\s*(if|while|for|switch)\b/) {
6018 WARN
("SINGLE_STATEMENT_DO_WHILE_MACRO",
6019 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
6021 if (defined $semis && $semis ne "") {
6022 WARN
("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
6023 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
6025 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
6027 my $cnt = statement_rawlines
($ctx);
6028 my $herectx = get_stat_here
($linenr, $cnt, $here);
6030 WARN
("TRAILING_SEMICOLON",
6031 "macros should not use a trailing semicolon\n" . "$herectx");
6035 # check for redundant bracing round if etc
6036 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
6037 my ($level, $endln, @chunks) =
6038 ctx_statement_full
($linenr, $realcnt, 1);
6039 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
6040 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
6041 if ($#chunks > 0 && $level == 0) {
6045 my $herectx = $here . "\n";
6046 my $ln = $linenr - 1;
6047 for my $chunk (@chunks) {
6048 my ($cond, $block) = @
{$chunk};
6050 # If the condition carries leading newlines, then count those as offsets.
6051 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
6052 my $offset = statement_rawlines
($whitespace) - 1;
6054 $allowed[$allow] = 0;
6055 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
6057 # We have looked at and allowed this specific line.
6058 $suppress_ifbraces{$ln + $offset} = 1;
6060 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
6061 $ln += statement_rawlines
($block) - 1;
6063 substr($block, 0, length($cond), '');
6065 $seen++ if ($block =~ /^\s*{/);
6067 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
6068 if (statement_lines
($cond) > 1) {
6069 #print "APW: ALLOWED: cond<$cond>\n";
6070 $allowed[$allow] = 1;
6072 if ($block =~/\b(?:if|for|while)\b/) {
6073 #print "APW: ALLOWED: block<$block>\n";
6074 $allowed[$allow] = 1;
6076 if (statement_block_size
($block) > 1) {
6077 #print "APW: ALLOWED: lines block<$block>\n";
6078 $allowed[$allow] = 1;
6083 my $sum_allowed = 0;
6084 foreach (@allowed) {
6087 if ($sum_allowed == 0) {
6089 "braces {} are not necessary for any arm of this statement\n" . $herectx);
6090 } elsif ($sum_allowed != $allow &&
6093 "braces {} should be used on all arms of this statement\n" . $herectx);
6098 if (!defined $suppress_ifbraces{$linenr - 1} &&
6099 $line =~ /\b(if|while|for|else)\b/) {
6102 # Check the pre-context.
6103 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
6104 #print "APW: ALLOWED: pre<$1>\n";
6108 my ($level, $endln, @chunks) =
6109 ctx_statement_full
($linenr, $realcnt, $-[0]);
6111 # Check the condition.
6112 my ($cond, $block) = @
{$chunks[0]};
6113 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
6114 if (defined $cond) {
6115 substr($block, 0, length($cond), '');
6117 if (statement_lines
($cond) > 1) {
6118 #print "APW: ALLOWED: cond<$cond>\n";
6121 if ($block =~/\b(?:if|for|while)\b/) {
6122 #print "APW: ALLOWED: block<$block>\n";
6125 if (statement_block_size
($block) > 1) {
6126 #print "APW: ALLOWED: lines block<$block>\n";
6129 # Check the post-context.
6130 if (defined $chunks[1]) {
6131 my ($cond, $block) = @
{$chunks[1]};
6132 if (defined $cond) {
6133 substr($block, 0, length($cond), '');
6135 if ($block =~ /^\s*\{/) {
6136 #print "APW: ALLOWED: chunk-1 block<$block>\n";
6140 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
6141 my $cnt = statement_rawlines
($block);
6142 my $herectx = get_stat_here
($linenr, $cnt, $here);
6145 "braces {} are not necessary for single statement blocks\n" . $herectx);
6149 # check for single line unbalanced braces
6150 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
6151 $sline =~ /^.\s*else\s*\{\s*$/) {
6152 CHK
("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
6155 # check for unnecessary blank lines around braces
6156 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
6158 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
6159 $fix && $prevrawline =~ /^\+/) {
6160 fix_delete_line
($fixlinenr - 1, $prevrawline);
6163 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
6165 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
6167 fix_delete_line
($fixlinenr, $rawline);
6171 # no volatiles please
6172 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
6173 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
6175 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
6178 # Check for user-visible strings broken across lines, which breaks the ability
6179 # to grep for the string. Make exceptions when the previous string ends in a
6180 # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
6181 # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
6182 if ($line =~ /^\+\s*$String/ &&
6183 $prevline =~ /"\s*$/ &&
6184 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
6185 if (WARN
("SPLIT_STRING",
6186 "quoted string split across lines\n" . $hereprev) &&
6188 $prevrawline =~ /^\+.*"\s*$/ &&
6189 $last_coalesced_string_linenr != $linenr - 1) {
6190 my $extracted_string = get_quoted_string
($line, $rawline);
6191 my $comma_close = "";
6192 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
6196 fix_delete_line
($fixlinenr - 1, $prevrawline);
6197 fix_delete_line
($fixlinenr, $rawline);
6198 my $fixedline = $prevrawline;
6199 $fixedline =~ s/"\s*$//;
6200 $fixedline .= substr($extracted_string, 1) . trim
($comma_close);
6201 fix_insert_line
($fixlinenr - 1, $fixedline);
6202 $fixedline = $rawline;
6203 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
6204 if ($fixedline !~ /\+\s*$/) {
6205 fix_insert_line
($fixlinenr, $fixedline);
6207 $last_coalesced_string_linenr = $linenr;
6211 # check for missing a space in a string concatenation
6212 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
6213 WARN
('MISSING_SPACE',
6214 "break quoted strings at a space character\n" . $hereprev);
6217 # check for an embedded function name in a string when the function is known
6218 # This does not work very well for -f --file checking as it depends on patch
6219 # context providing the function name or a single line form for in-file
6220 # function declarations
6221 if ($line =~ /^\+.*$String/ &&
6222 defined($context_function) &&
6223 get_quoted_string
($line, $rawline) =~ /\b$context_function\b/ &&
6224 length(get_quoted_string
($line, $rawline)) != (length($context_function) + 2)) {
6225 WARN
("EMBEDDED_FUNCTION_NAME",
6226 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
6229 # check for unnecessary function tracing like uses
6230 # This does not use $logFunctions because there are many instances like
6231 # 'dprintk(FOO, "%s()\n", __func__);' which do not match $logFunctions
6232 if ($rawline =~ /^\+.*\([^"]*"$tracing_logging_tags{0,3}%s(?:\s*\(\s*\)\s*)?$tracing_logging_tags{0,3}(?:\\n)?"\s*,\s*__func__\s*\)\s*;/) {
6233 if (WARN
("TRACING_LOGGING",
6234 "Unnecessary ftrace-like logging - prefer using ftrace\n" . $herecurr) &&
6236 fix_delete_line
($fixlinenr, $rawline);
6240 # check for spaces before a quoted newline
6241 if ($rawline =~ /^.*\".*\s\\n/) {
6242 if (WARN
("QUOTED_WHITESPACE_BEFORE_NEWLINE",
6243 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
6245 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
6250 # concatenated string without spaces between elements
6251 if ($line =~ /$String[A-Z_]/ ||
6252 ($line =~ /([A-Za-z0-9_]+)$String/ && $1 !~ /^[Lu]$/)) {
6253 if (CHK
("CONCATENATED_STRING",
6254 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
6256 while ($line =~ /($String)/g) {
6257 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6258 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
6259 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
6264 # uncoalesced string fragments
6265 if ($line =~ /$String\s*[Lu]?"/) {
6266 if (WARN
("STRING_FRAGMENTS",
6267 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
6269 while ($line =~ /($String)(?=\s*")/g) {
6270 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6271 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
6276 # check for non-standard and hex prefixed decimal printf formats
6277 my $show_L = 1; #don't show the same defect twice
6279 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
6280 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
6281 $string =~ s/%%/__/g;
6283 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
6285 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
6289 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
6291 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
6294 # check for 0x<decimal>
6295 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
6296 ERROR
("PRINTF_0XDECIMAL",
6297 "Prefixing 0x with decimal output is defective\n" . $herecurr);
6301 # check for line continuations in quoted strings with odd counts of "
6302 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
6303 WARN
("LINE_CONTINUATIONS",
6304 "Avoid line continuations in quoted strings\n" . $herecurr);
6308 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
6310 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
6314 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
6316 "Consider removing the #if 1 and its #endif\n" . $herecurr);
6319 # check for needless "if (<foo>) fn(<foo>)" uses
6320 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
6321 my $tested = quotemeta($1);
6322 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
6323 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
6325 if (WARN
('NEEDLESS_IF',
6326 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
6329 my $leading_tabs = "";
6330 my $new_leading_tabs = "";
6331 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
6336 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
6337 $new_leading_tabs = $1;
6338 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
6345 fix_delete_line
($fixlinenr - 1, $prevrawline);
6346 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
6352 # check for unnecessary "Out of Memory" messages
6353 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
6354 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
6355 (defined $1 || defined $3) &&
6358 my $testline = $lines[$linenr - 3];
6360 my ($s, $c) = ctx_statement_block
($linenr - 3, $realcnt, 0);
6361 # print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
6363 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6364 $s !~ /\b__GFP_NOWARN\b/ ) {
6366 "Possible unnecessary 'out of memory' message\n" . $hereprev);
6370 # check for logging functions with KERN_<LEVEL>
6371 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
6372 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6374 if (WARN
("UNNECESSARY_KERN_LEVEL",
6375 "Possible unnecessary $level\n" . $herecurr) &&
6377 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
6381 # check for logging continuations
6382 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
6383 WARN
("LOGGING_CONTINUATION",
6384 "Avoid logging continuation uses where feasible\n" . $herecurr);
6387 # check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
6388 if (defined $stat &&
6389 $line =~ /\b$logFunctions\s*\(/ &&
6390 index($stat, '"') >= 0) {
6391 my $lc = $stat =~ tr@
\n@@
;
6392 $lc = $lc + $linenr;
6393 my $stat_real = get_stat_real
($linenr, $lc);
6394 pos($stat_real) = index($stat_real, '"');
6395 while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
6398 my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@
\n@@
;
6399 if (WARN
("UNNECESSARY_MODIFIER",
6400 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
6401 $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
6404 $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
6409 # check for mask then right shift without a parentheses
6410 if ($perl_version_ok &&
6411 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6412 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6413 WARN
("MASK_THEN_SHIFT",
6414 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6417 # check for pointer comparisons to NULL
6418 if ($perl_version_ok) {
6419 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6422 $equal = "" if ($4 eq "!=");
6423 if (CHK
("COMPARISON_TO_NULL",
6424 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6426 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6431 # check for bad placement of section $InitAttribute (e.g.: __initdata)
6432 if ($line =~ /(\b$InitAttribute\b)/) {
6434 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
6437 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
6438 ERROR
("MISPLACED_INIT",
6439 "$attr should be placed after $var\n" . $herecurr)) ||
6440 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
6441 WARN
("MISPLACED_INIT",
6442 "$attr should be placed after $var\n" . $herecurr))) &&
6444 $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;
6449 # check for $InitAttributeData (ie: __initdata) with const
6450 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6452 $attr =~ /($InitAttributePrefix)(.*)/;
6453 my $attr_prefix = $1;
6455 if (ERROR
("INIT_ATTRIBUTE",
6456 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6458 $fixed[$fixlinenr] =~
6459 s/$InitAttributeData/${attr_prefix}initconst/;
6463 # check for $InitAttributeConst (ie: __initconst) without const
6464 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6466 if (ERROR
("INIT_ATTRIBUTE",
6467 "Use of $attr requires a separate use of const\n" . $herecurr) &&
6469 my $lead = $fixed[$fixlinenr] =~
6470 /(^\+\s*(?:static\s+))/;
6472 $lead = "$lead " if ($lead !~ /^\+$/);
6473 $lead = "${lead}const ";
6474 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
6478 # check for __read_mostly with const non-pointer (should just be const)
6479 if ($line =~ /\b__read_mostly\b/ &&
6480 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6481 if (ERROR
("CONST_READ_MOSTLY",
6482 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6484 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6488 # don't use __constant_<foo> functions outside of include/uapi/
6489 if ($realfile !~ m@
^include
/uapi/@
&&
6490 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6491 my $constant_func = $1;
6492 my $func = $constant_func;
6493 $func =~ s/^__constant_//;
6494 if (WARN
("CONSTANT_CONVERSION",
6495 "$constant_func should be $func\n" . $herecurr) &&
6497 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
6501 # prefer usleep_range over udelay
6502 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
6504 # ignore udelay's < 10, however
6505 if (! ($delay < 10) ) {
6507 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
6509 if ($delay > 2000) {
6511 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
6515 # warn about unexpectedly long msleep's
6516 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
6519 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
6523 # check for comparisons of jiffies
6524 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
6525 WARN
("JIFFIES_COMPARISON",
6526 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
6529 # check for comparisons of get_jiffies_64()
6530 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
6531 WARN
("JIFFIES_COMPARISON",
6532 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
6535 # warn about #ifdefs in C files
6536 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
6537 # print "#ifdef in C files should be avoided\n";
6538 # print "$herecurr";
6542 # warn about spacing in #ifdefs
6543 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
6544 if (ERROR
("SPACING",
6545 "exactly one space required after that #$1\n" . $herecurr) &&
6547 $fixed[$fixlinenr] =~
6548 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
6553 # check for spinlock_t definitions without a comment.
6554 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6555 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
6557 if (!ctx_has_comment
($first_line, $linenr)) {
6558 CHK
("UNCOMMENTED_DEFINITION",
6559 "$1 definition without comment\n" . $herecurr);
6562 # check for memory barriers without a comment.
6569 my $barrier_stems = qr{
6577 my $all_barriers = qr{
6579 smp_
(?
:$barrier_stems)|
6580 virt_
(?
:$barrier_stems)
6583 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
6584 if (!ctx_has_comment
($first_line, $linenr)) {
6585 WARN
("MEMORY_BARRIER",
6586 "memory barrier without comment\n" . $herecurr);
6590 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6592 if ($realfile !~ m@
^include
/asm-generic/@
&&
6593 $realfile !~ m@
/barrier\
.h
$@
&&
6594 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6595 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6596 WARN
("MEMORY_BARRIER",
6597 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6600 # check for waitqueue_active without a comment.
6601 if ($line =~ /\bwaitqueue_active\s*\(/) {
6602 if (!ctx_has_comment
($first_line, $linenr)) {
6603 WARN
("WAITQUEUE_ACTIVE",
6604 "waitqueue_active without comment\n" . $herecurr);
6608 # check for data_race without a comment.
6609 if ($line =~ /\bdata_race\s*\(/) {
6610 if (!ctx_has_comment
($first_line, $linenr)) {
6612 "data_race without comment\n" . $herecurr);
6616 # check of hardware specific defines
6617 if ($line =~ m@
^.\s
*\#\s
*if.*\b(__i386__
|__powerpc64__
|__sun__
|__s390x__
)\b@
&& $realfile !~ m
@include/asm
-@
) {
6619 "architecture specific defines should be avoided\n" . $herecurr);
6622 # check that the storage class is not after a type
6623 if ($line =~ /\b($Type)\s+($Storage)\b/) {
6624 WARN
("STORAGE_CLASS",
6625 "storage class '$2' should be located before type '$1'\n" . $herecurr);
6627 # Check that the storage class is at the beginning of a declaration
6628 if ($line =~ /\b$Storage\b/ &&
6629 $line !~ /^.\s*$Storage/ &&
6630 $line =~ /^.\s*(.+?)\$Storage\s/ &&
6631 $1 !~ /[\,\)]\s*$/) {
6632 WARN
("STORAGE_CLASS",
6633 "storage class should be at the beginning of the declaration\n" . $herecurr);
6636 # check the location of the inline attribute, that it is between
6637 # storage class and type.
6638 if ($line =~ /\b$Type\s+$Inline\b/ ||
6639 $line =~ /\b$Inline\s+$Storage\b/) {
6640 ERROR
("INLINE_LOCATION",
6641 "inline keyword should sit between storage class and type\n" . $herecurr);
6644 # Check for __inline__ and __inline, prefer inline
6645 if ($realfile !~ m@
\binclude
/uapi/@
&&
6646 $line =~ /\b(__inline__|__inline)\b/) {
6648 "plain inline is preferred over $1\n" . $herecurr) &&
6650 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
6655 # Check for compiler attributes
6656 if ($realfile !~ m@
\binclude
/uapi/@
&&
6657 $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
6659 $attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
6662 "alias" => "__alias",
6663 "aligned" => "__aligned",
6664 "always_inline" => "__always_inline",
6665 "assume_aligned" => "__assume_aligned",
6667 "const" => "__attribute_const__",
6669 "designated_init" => "__designated_init",
6670 "externally_visible" => "__visible",
6671 "format" => "printf|scanf",
6672 "gnu_inline" => "__gnu_inline",
6673 "malloc" => "__malloc",
6675 "no_caller_saved_registers" => "__no_caller_saved_registers",
6676 "noclone" => "__noclone",
6677 "noinline" => "noinline",
6678 "nonstring" => "__nonstring",
6679 "noreturn" => "__noreturn",
6680 "packed" => "__packed",
6682 "section" => "__section",
6687 while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
6690 $params = $2 if defined($2);
6691 my $curr_attr = $orig_attr;
6692 $curr_attr =~ s/^[\s_]+|[\s_]+$//g;
6693 if (exists($attr_list{$curr_attr})) {
6694 my $new = $attr_list{$curr_attr};
6695 if ($curr_attr eq "format" && $params) {
6696 $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
6699 $new = "$new$params";
6701 if (WARN
("PREFER_DEFINED_ATTRIBUTE_MACRO",
6702 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
6704 my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6705 $fixed[$fixlinenr] =~ s/$remove//;
6706 $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6707 $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6708 $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
6713 # Check for __attribute__ unused, prefer __always_unused or __maybe_unused
6714 if ($attr =~ /^_*unused/) {
6715 WARN
("PREFER_DEFINED_ATTRIBUTE_MACRO",
6716 "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
6720 # Check for __attribute__ weak, or __weak declarations (may have link issues)
6721 if ($perl_version_ok &&
6722 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6723 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6724 $line =~ /\b__weak\b/)) {
6725 ERROR
("WEAK_DECLARATION",
6726 "Using weak declarations can have unintended link defects\n" . $herecurr);
6729 # check for c99 types like uint8_t used outside of uapi/ and tools/
6730 if ($realfile !~ m@
\binclude
/uapi/@
&&
6731 $realfile !~ m@
\btools
/@
&&
6732 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6734 if ($type =~ /\b($typeC99Typedefs)\b/) {
6736 my $kernel_type = 'u';
6737 $kernel_type = 's' if ($type =~ /^_*[si]/);
6740 if (CHK
("PREFER_KERNEL_TYPES",
6741 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6743 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6748 # check for cast of C90 native int or longer types constants
6749 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6753 my $newconst = $const;
6754 $newconst =~ s/${Int_type}$//;
6755 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6756 if ($cast =~ /\blong\s+long\b/) {
6758 } elsif ($cast =~ /\blong\b/) {
6761 if (WARN
("TYPECAST_INT_CONSTANT",
6762 "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) &&
6764 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6768 # check for sizeof(&)
6769 if ($line =~ /\bsizeof\s*\(\s*\&/) {
6770 WARN
("SIZEOF_ADDRESS",
6771 "sizeof(& should be avoided\n" . $herecurr);
6774 # check for sizeof without parenthesis
6775 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
6776 if (WARN
("SIZEOF_PARENTHESIS",
6777 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6779 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
6783 # check for struct spinlock declarations
6784 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6785 WARN
("USE_SPINLOCK_T",
6786 "struct spinlock should be spinlock_t\n" . $herecurr);
6789 # check for seq_printf uses that could be seq_puts
6790 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6791 my $fmt = get_quoted_string
($line, $rawline);
6794 if (WARN
("PREFER_SEQ_PUTS",
6795 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6797 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6802 # check for vsprintf extension %p<foo> misuses
6803 if ($perl_version_ok &&
6805 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6806 $1 !~ /^_*volatile_*$/) {
6809 my $lc = $stat =~ tr@
\n@@
;
6810 $lc = $lc + $linenr;
6811 for (my $count = $linenr; $count <= $lc; $count++) {
6815 my $bad_specifier = "";
6816 my $fmt = get_quoted_string
($lines[$count - 1], raw_line
($count, 0));
6819 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
6823 if ($extension !~ /[4SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
6824 ($extension eq "f" &&
6825 defined $qualifier && $qualifier !~ /^w/) ||
6826 ($extension eq "4" &&
6827 defined $qualifier && $qualifier !~ /^cc/)) {
6828 $bad_specifier = $specifier;
6831 if ($extension eq "x" && !defined($stat_real)) {
6832 if (!defined($stat_real)) {
6833 $stat_real = get_stat_real
($linenr, $lc);
6835 WARN
("VSPRINTF_SPECIFIER_PX",
6836 "Using vsprintf specifier '\%px' potentially exposes the kernel memory layout, if you don't really need the address please consider using '\%p'.\n" . "$here\n$stat_real\n");
6839 if ($bad_specifier ne "") {
6840 my $stat_real = get_stat_real
($linenr, $lc);
6841 my $ext_type = "Invalid";
6843 if ($bad_specifier =~ /p[Ff]/) {
6844 $use = " - use %pS instead";
6845 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6848 WARN
("VSPRINTF_POINTER_EXTENSION",
6849 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6854 # Check for misused memsets
6855 if ($perl_version_ok &&
6857 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6863 if ($ms_size =~ /^(0x|)0$/i) {
6865 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6866 } elsif ($ms_size =~ /^(0x|)1$/i) {
6868 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6872 # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
6873 # if ($perl_version_ok &&
6875 # $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6876 # if (WARN("PREFER_ETHER_ADDR_COPY",
6877 # "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6879 # $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6883 # Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
6884 # if ($perl_version_ok &&
6886 # $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6887 # WARN("PREFER_ETHER_ADDR_EQUAL",
6888 # "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6891 # check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6892 # check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
6893 # if ($perl_version_ok &&
6895 # $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6899 # if ($ms_val =~ /^(?:0x|)0+$/i) {
6900 # if (WARN("PREFER_ETH_ZERO_ADDR",
6901 # "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6903 # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6905 # } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6906 # if (WARN("PREFER_ETH_BROADCAST_ADDR",
6907 # "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6909 # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6914 # strlcpy uses that should likely be strscpy
6915 if ($line =~ /\bstrlcpy\s*\(/) {
6917 "Prefer strscpy over strlcpy - see: https://lore.kernel.org/r/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw\@mail.gmail.com/\n" . $herecurr);
6920 # typecasts on min/max could be min_t/max_t
6921 if ($perl_version_ok &&
6923 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6924 if (defined $2 || defined $7) {
6926 my $cast1 = deparenthesize
($2);
6928 my $cast2 = deparenthesize
($7);
6932 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6933 $cast = "$cast1 or $cast2";
6934 } elsif ($cast1 ne "") {
6940 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6944 # check usleep_range arguments
6945 if ($perl_version_ok &&
6947 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6951 WARN
("USLEEP_RANGE",
6952 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
6953 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6955 WARN
("USLEEP_RANGE",
6956 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
6960 # check for naked sscanf
6961 if ($perl_version_ok &&
6963 $line =~ /\bsscanf\b/ &&
6964 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6965 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6966 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6967 my $lc = $stat =~ tr@
\n@@
;
6968 $lc = $lc + $linenr;
6969 my $stat_real = get_stat_real
($linenr, $lc);
6970 WARN
("NAKED_SSCANF",
6971 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6974 # check for simple sscanf that should be kstrto<foo>
6975 if ($perl_version_ok &&
6977 $line =~ /\bsscanf\b/) {
6978 my $lc = $stat =~ tr@
\n@@
;
6979 $lc = $lc + $linenr;
6980 my $stat_real = get_stat_real
($linenr, $lc);
6981 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6983 my $count = $format =~ tr@
%@
%@
;
6985 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6986 WARN
("SSCANF_TO_KSTRTO",
6987 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6992 # check for new externs in .h files.
6993 if ($realfile =~ /\.h$/ &&
6994 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6995 if (CHK
("AVOID_EXTERNS",
6996 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
6998 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
7002 # check for new externs in .c files.
7003 if ($realfile =~ /\.c$/ && defined $stat &&
7004 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
7006 my $function_name = $1;
7007 my $paren_space = $2;
7010 if (defined $cond) {
7011 substr($s, 0, length($cond), '');
7015 WARN
("AVOID_EXTERNS",
7016 "externs should be avoided in .c files\n" . $herecurr);
7019 if ($paren_space =~ /\n/) {
7020 WARN
("FUNCTION_ARGUMENTS",
7021 "arguments for function declarations should follow identifier\n" . $herecurr);
7024 } elsif ($realfile =~ /\.c$/ && defined $stat &&
7025 $stat =~ /^.\s*extern\s+/)
7027 WARN
("AVOID_EXTERNS",
7028 "externs should be avoided in .c files\n" . $herecurr);
7031 # check for function declarations that have arguments without identifier names
7032 if (defined $stat &&
7033 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
7035 my $args = trim
($1);
7036 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
7038 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
7039 WARN
("FUNCTION_ARGUMENTS",
7040 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
7045 # check for function definitions
7046 if ($perl_version_ok &&
7048 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
7049 $context_function = $1;
7051 # check for multiline function definition with misplaced open brace
7053 my $cnt = statement_rawlines
($stat);
7054 my $herectx = $here . "\n";
7055 for (my $n = 0; $n < $cnt; $n++) {
7056 my $rl = raw_line
($linenr, $n);
7057 $herectx .= $rl . "\n";
7058 $ok = 1 if ($rl =~ /^[ \+]\{/);
7059 $ok = 1 if ($rl =~ /\{/ && $n == 0);
7060 last if $rl =~ /^[ \+].*\{/;
7064 "open brace '{' following function definitions go on the next line\n" . $herectx);
7068 # checks for new __setup's
7069 if ($rawline =~ /\b__setup\("([^"]*)"/) {
7072 if (!grep(/$name/, @setup_docs)) {
7073 CHK
("UNDOCUMENTED_SETUP",
7074 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
7078 # check for pointless casting of alloc functions
7079 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
7080 WARN
("UNNECESSARY_CASTS",
7081 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
7085 # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
7086 if ($perl_version_ok &&
7087 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
7088 CHK
("ALLOC_SIZEOF_STRUCT",
7089 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
7092 # check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kvmalloc_array/kvcalloc/kcalloc
7093 if ($perl_version_ok &&
7095 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
7099 my $newfunc = "kmalloc_array";
7100 $newfunc = "kvmalloc_array" if ($oldfunc eq "kvmalloc");
7101 $newfunc = "kvcalloc" if ($oldfunc eq "kvzalloc");
7102 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
7105 if ($a1 =~ /^sizeof\s*\S/) {
7109 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
7110 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
7111 my $cnt = statement_rawlines
($stat);
7112 my $herectx = get_stat_here
($linenr, $cnt, $here);
7114 if (WARN
("ALLOC_WITH_MULTIPLY",
7115 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
7118 $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
7123 # check for krealloc arg reuse
7124 if ($perl_version_ok &&
7125 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
7127 WARN
("KREALLOC_ARG_REUSE",
7128 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
7131 # check for alloc argument mismatch
7132 if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
7133 WARN
("ALLOC_ARRAY_ARGS",
7134 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
7137 # check for multiple semicolons
7138 if ($line =~ /;\s*;\s*$/) {
7139 if (WARN
("ONE_SEMICOLON",
7140 "Statements terminations use 1 semicolon\n" . $herecurr) &&
7142 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
7146 # check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
7147 if ($realfile !~ m@
^include
/uapi/@
&&
7148 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
7150 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
7151 if (CHK
("BIT_MACRO",
7152 "Prefer using the BIT$ull macro\n" . $herecurr) &&
7154 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
7158 # check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
7159 if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
7160 WARN
("IS_ENABLED_CONFIG",
7161 "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
7164 # check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
7165 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(${CONFIG_}[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
7167 if (WARN
("PREFER_IS_ENABLED",
7168 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
7170 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
7174 # check for /* fallthrough */ like comment, prefer fallthrough;
7175 my @fallthroughs = (
7178 'lint -fallthrough[ \t]*',
7179 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
7180 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
7181 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7182 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7184 if ($raw_comment ne '') {
7185 foreach my $ft (@fallthroughs) {
7186 if ($raw_comment =~ /$ft/) {
7187 my $msg_level = \
&WARN
;
7188 $msg_level = \
&CHK
if ($file);
7189 &{$msg_level}("PREFER_FALLTHROUGH",
7190 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
7196 # check for switch/default statements without a break;
7197 if ($perl_version_ok &&
7199 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
7200 my $cnt = statement_rawlines
($stat);
7201 my $herectx = get_stat_here
($linenr, $cnt, $here);
7203 WARN
("DEFAULT_NO_BREAK",
7204 "switch default: should use break\n" . $herectx);
7207 # check for gcc specific __FUNCTION__
7208 if ($line =~ /\b__FUNCTION__\b/) {
7209 if (WARN
("USE_FUNC",
7210 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
7212 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
7216 # check for uses of __DATE__, __TIME__, __TIMESTAMP__
7217 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
7219 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
7222 # check for use of yield()
7223 if ($line =~ /\byield\s*\(\s*\)/) {
7225 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
7228 # check for comparisons against true and false
7229 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
7237 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
7239 my $type = lc($otype);
7240 if ($type =~ /^(?:true|false)$/) {
7241 if (("$test" eq "==" && "$type" eq "true") ||
7242 ("$test" eq "!=" && "$type" eq "false")) {
7246 CHK
("BOOL_COMPARISON",
7247 "Using comparison to $otype is error prone\n" . $herecurr);
7249 ## maybe suggesting a correct construct would better
7250 ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
7255 # check for semaphores initialized locked
7256 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
7257 WARN
("CONSIDER_COMPLETION",
7258 "consider using a completion\n" . $herecurr);
7261 # recommend kstrto* over simple_strto* and strict_strto*
7262 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
7263 WARN
("CONSIDER_KSTRTO",
7264 "$1 is obsolete, use k$3 instead\n" . $herecurr);
7267 # check for __initcall(), use device_initcall() explicitly or more appropriate function please
7268 if ($line =~ /^.\s*__initcall\s*\(/) {
7269 WARN
("USE_DEVICE_INITCALL",
7270 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
7273 # check for spin_is_locked(), suggest lockdep instead
7274 if ($line =~ /\bspin_is_locked\(/) {
7276 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
7279 # check for deprecated apis
7280 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
7281 my $deprecated_api = $1;
7282 my $new_api = $deprecated_apis{$deprecated_api};
7283 WARN
("DEPRECATED_API",
7284 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
7287 # check for various structs that are normally const (ops, kgdb, device_tree)
7288 # and avoid what seem like struct definitions 'struct foo {'
7289 if (defined($const_structs) &&
7290 $line !~ /\bconst\b/ &&
7291 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
7292 WARN
("CONST_STRUCT",
7293 "struct $1 should normally be const\n" . $herecurr);
7296 # use of NR_CPUS is usually wrong
7297 # ignore definitions of NR_CPUS and usage to define arrays as likely right
7298 # ignore designated initializers using NR_CPUS
7299 if ($line =~ /\bNR_CPUS\b/ &&
7300 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
7301 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
7302 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
7303 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
7304 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
7305 $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/)
7308 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
7311 # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
7312 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
7313 ERROR
("DEFINE_ARCH_HAS",
7314 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
7317 # likely/unlikely comparisons similar to "(likely(foo) > 0)"
7318 if ($perl_version_ok &&
7319 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
7320 WARN
("LIKELY_MISUSE",
7321 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
7324 # return sysfs_emit(foo, fmt, ...) fmt without newline
7325 if ($line =~ /\breturn\s+sysfs_emit\s*\(\s*$FuncArg\s*,\s*($String)/ &&
7326 substr($rawline, $-[6], $+[6] - $-[6]) !~ /\\n"$/) {
7327 my $offset = $+[6] - 1;
7328 if (WARN
("SYSFS_EMIT",
7329 "return sysfs_emit(...) formats should include a terminating newline\n" . $herecurr) &&
7331 substr($fixed[$fixlinenr], $offset, 0) = '\\n';
7335 # nested likely/unlikely calls
7336 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
7337 WARN
("LIKELY_MISUSE",
7338 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
7341 # whine mightly about in_atomic
7342 if ($line =~ /\bin_atomic\s*\(/) {
7343 if ($realfile =~ m@
^drivers
/@
) {
7345 "do not use in_atomic in drivers\n" . $herecurr);
7346 } elsif ($realfile !~ m@
^kernel
/@
) {
7348 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
7352 # check for lockdep_set_novalidate_class
7353 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
7354 $line =~ /__lockdep_no_validate__\s*\)/ ) {
7355 if ($realfile !~ m@
^kernel
/lockdep@
&&
7356 $realfile !~ m@
^include
/linux/lockdep@
&&
7357 $realfile !~ m@
^drivers
/base/core@
) {
7359 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
7363 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
7364 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
7365 WARN
("EXPORTED_WORLD_WRITABLE",
7366 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
7369 # check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
7370 # and whether or not function naming is typical and if
7371 # DEVICE_ATTR permissions uses are unusual too
7372 if ($perl_version_ok &&
7374 $stat =~ /\bDEVICE_ATTR\s*\(\s*(\w+)\s*,\s*\(?\s*(\s*(?:${multi_mode_perms_string_search}|0[0-7]{3,3})\s*)\s*\)?\s*,\s*(\w+)\s*,\s*(\w+)\s*\)/) {
7379 my $octal_perms = perms_to_octal
($perms);
7380 if ($show =~ /^${var}_show$/ &&
7381 $store =~ /^${var}_store$/ &&
7382 $octal_perms eq "0644") {
7383 if (WARN
("DEVICE_ATTR_RW",
7384 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
7386 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
7388 } elsif ($show =~ /^${var}_show$/ &&
7389 $store =~ /^NULL$/ &&
7390 $octal_perms eq "0444") {
7391 if (WARN
("DEVICE_ATTR_RO",
7392 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
7394 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
7396 } elsif ($show =~ /^NULL$/ &&
7397 $store =~ /^${var}_store$/ &&
7398 $octal_perms eq "0200") {
7399 if (WARN
("DEVICE_ATTR_WO",
7400 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
7402 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
7404 } elsif ($octal_perms eq "0644" ||
7405 $octal_perms eq "0444" ||
7406 $octal_perms eq "0200") {
7407 my $newshow = "$show";
7408 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
7409 my $newstore = $store;
7410 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
7412 if ($show ne $newshow) {
7413 $rename .= " '$show' to '$newshow'";
7415 if ($store ne $newstore) {
7416 $rename .= " '$store' to '$newstore'";
7418 WARN
("DEVICE_ATTR_FUNCTIONS",
7419 "Consider renaming function(s)$rename\n" . $herecurr);
7421 WARN
("DEVICE_ATTR_PERMS",
7422 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
7426 # Mode permission misuses where it seems decimal should be octal
7427 # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
7428 # o Ignore module_param*(...) uses with a decimal 0 permission as that has a
7429 # specific definition of not visible in sysfs.
7430 # o Ignore proc_create*(...) uses with a decimal 0 permission as that means
7431 # use the default permissions
7432 if ($perl_version_ok &&
7434 $line =~ /$mode_perms_search/) {
7435 foreach my $entry (@mode_permission_funcs) {
7436 my $func = $entry->[0];
7437 my $arg_pos = $entry->[1];
7439 my $lc = $stat =~ tr@
\n@@
;
7440 $lc = $lc + $linenr;
7441 my $stat_real = get_stat_real
($linenr, $lc);
7446 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
7448 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
7449 if ($stat =~ /$test/) {
7451 $val = $6 if ($skip_args ne "");
7452 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
7453 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
7454 ($val =~ /^$Octal$/ && length($val) ne 4))) {
7455 ERROR
("NON_OCTAL_PERMISSIONS",
7456 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
7458 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
7459 ERROR
("EXPORTED_WORLD_WRITABLE",
7460 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
7466 # check for uses of S_<PERMS> that could be octal for readability
7467 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
7469 my $octal = perms_to_octal
($oval);
7470 if (WARN
("SYMBOLIC_PERMS",
7471 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7473 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
7477 # validate content of MODULE_LICENSE against list from include/linux/module.h
7478 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
7479 my $extracted_string = get_quoted_string
($line, $rawline);
7480 my $valid_licenses = qr{
7483 GPL\
and\ additional\ rights
|
7489 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
7490 WARN
("MODULE_LICENSE",
7491 "unknown module license " . $extracted_string . "\n" . $herecurr);
7493 if (!$file && $extracted_string eq '"GPL v2"') {
7494 if (WARN
("MODULE_LICENSE",
7495 "Prefer \"GPL\" over \"GPL v2\" - see commit bf7fbeeae6db (\"module: Cure the MODULE_LICENSE \"GPL\" vs. \"GPL v2\" bogosity\")\n" . $herecurr) &&
7497 $fixed[$fixlinenr] =~ s/\bMODULE_LICENSE\s*\(\s*"GPL v2"\s*\)/MODULE_LICENSE("GPL")/;
7502 # check for sysctl duplicate constants
7503 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
7504 WARN
("DUPLICATED_SYSCTL_CONST",
7505 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
7509 # If we have no input at all, then there is nothing to report on
7510 # so just keep quiet.
7511 if ($#rawlines == -1) {
7515 # In mailback mode only produce a report in the negative, for
7516 # things that appear to be patches.
7517 if ($mailback && ($clean == 1 || !$is_patch)) {
7521 # This is not a patch, and we are in 'no-patch' mode so
7523 if (!$chk_patch && !$is_patch) {
7527 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
7528 ERROR
("NOT_UNIFIED_DIFF",
7529 "Does not appear to be a unified-diff format patch\n");
7531 if ($is_patch && $has_commit_log && $chk_signoff) {
7532 if ($signoff == 0) {
7533 ERROR
("MISSING_SIGN_OFF",
7534 "Missing Signed-off-by: line(s)\n");
7535 } elsif ($authorsignoff != 1) {
7536 # authorsignoff values:
7537 # 0 -> missing sign off
7538 # 1 -> sign off identical
7539 # 2 -> names and addresses match, comments mismatch
7540 # 3 -> addresses match, names different
7541 # 4 -> names match, addresses different
7542 # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
7544 my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
7546 if ($authorsignoff == 0) {
7547 ERROR
("NO_AUTHOR_SIGN_OFF",
7548 "Missing Signed-off-by: line by nominal patch author '$author'\n");
7549 } elsif ($authorsignoff == 2) {
7550 CHK
("FROM_SIGN_OFF_MISMATCH",
7551 "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
7552 } elsif ($authorsignoff == 3) {
7553 WARN
("FROM_SIGN_OFF_MISMATCH",
7554 "From:/Signed-off-by: email name mismatch: $sob_msg\n");
7555 } elsif ($authorsignoff == 4) {
7556 WARN
("FROM_SIGN_OFF_MISMATCH",
7557 "From:/Signed-off-by: email address mismatch: $sob_msg\n");
7558 } elsif ($authorsignoff == 5) {
7559 WARN
("FROM_SIGN_OFF_MISMATCH",
7560 "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
7565 print report_dump
();
7566 if ($summary && !($clean == 1 && $quiet == 1)) {
7567 print "$filename " if ($summary_file);
7568 print "total: $cnt_error errors, $cnt_warn warnings, " .
7569 (($check)?
"$cnt_chk checks, " : "") .
7570 "$cnt_lines lines checked\n";
7574 # If there were any defects found and not already fixing them
7575 if (!$clean and !$fix) {
7578 NOTE
: For some of the reported defects
, checkpatch may be able to
7579 mechanically convert to the typical style using
--fix
or --fix
-inplace
.
7582 # If there were whitespace errors which cleanpatch can fix
7583 # then suggest that.
7584 if ($rpt_cleaners) {
7588 NOTE
: Whitespace errors detected
.
7589 You may wish to
use scripts
/cleanpatch or scripts/cleanfile
7594 if ($clean == 0 && $fix &&
7595 ("@rawlines" ne "@fixed" ||
7596 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
7597 my $newfile = $filename;
7598 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
7602 @fixed = fix_inserted_deleted_lines
(\
@fixed, \
@fixed_inserted, \
@fixed_deleted);
7604 open($f, '>', $newfile)
7605 or die "$P: Can't open $newfile for write\n";
7606 foreach my $fixed_line (@fixed) {
7609 if ($linecount > 3) {
7610 $fixed_line =~ s/^\+//;
7611 print $f $fixed_line . "\n";
7614 print $f $fixed_line . "\n";
7622 Wrote EXPERIMENTAL
--fix correction
(s
) to
'$newfile'
7624 Do _NOT_ trust the results written to this file
.
7625 Do _NOT_ submit these changes without inspecting them
for correctness
.
7627 This EXPERIMENTAL file is simply a convenience to help rewrite patches
.
7628 No warranties
, expressed
or implied
...
7636 print "$vname has no obvious style problems and is ready for submission.\n";
7638 print "$vname has style problems, please review.\n";
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)