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 return 1 if (!$tree || which
("python3") eq "" || !(-x
"$root/scripts/spdxcheck.py") || !(-e
"$gitroot"));
1137 my $root_path = abs_path
($root);
1138 my $status = `cd "$root_path"; echo "$license" | scripts/spdxcheck.py -`;
1139 return 0 if ($status ne "");
1143 my $camelcase_seeded = 0;
1144 sub seed_camelcase_includes
{
1145 return if ($camelcase_seeded);
1148 my $camelcase_cache = "";
1149 my @include_files = ();
1151 $camelcase_seeded = 1;
1153 if (-e
"$gitroot") {
1154 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
1155 chomp $git_last_include_commit;
1156 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
1158 my $last_mod_date = 0;
1159 $files = `find $root/include -name "*.h"`;
1160 @include_files = split('\n', $files);
1161 foreach my $file (@include_files) {
1162 my $date = POSIX
::strftime
("%Y%m%d%H%M",
1163 localtime((stat $file)[9]));
1164 $last_mod_date = $date if ($last_mod_date < $date);
1166 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
1169 if ($camelcase_cache ne "" && -f
$camelcase_cache) {
1170 open(my $camelcase_file, '<', "$camelcase_cache")
1171 or warn "$P: Can't read '$camelcase_cache' $!\n";
1172 while (<$camelcase_file>) {
1176 close($camelcase_file);
1181 if (-e
"$gitroot") {
1182 $files = `${git_command} ls-files "include/*.h"`;
1183 @include_files = split('\n', $files);
1186 foreach my $file (@include_files) {
1187 seed_camelcase_file
($file);
1190 if ($camelcase_cache ne "") {
1191 unlink glob ".checkpatch-camelcase.*";
1192 open(my $camelcase_file, '>', "$camelcase_cache")
1193 or warn "$P: Can't write '$camelcase_cache' $!\n";
1194 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
1195 print $camelcase_file ("$_\n");
1197 close($camelcase_file);
1201 sub git_is_single_file
{
1202 my ($filename) = @_;
1204 return 0 if ((which
("git") eq "") || !(-e
"$gitroot"));
1206 my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
1207 my $count = $output =~ tr/\n//;
1208 return $count eq 1 && $output =~ m{^${filename}$};
1211 sub git_commit_info
{
1212 my ($commit, $id, $desc) = @_;
1214 return ($id, $desc) if ((which
("git") eq "") || !(-e
"$gitroot"));
1216 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
1217 $output =~ s/^\s*//gm;
1218 my @lines = split("\n", $output);
1220 return ($id, $desc) if ($#lines < 0);
1222 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
1223 # Maybe one day convert this block of bash into something that returns
1224 # all matching commit ids, but it's very slow...
1226 # echo "checking commits $1..."
1227 # git rev-list --remotes | grep -i "^$1" |
1228 # while read line ; do
1229 # git log --format='%H %s' -1 $line |
1230 # echo "commit $(cut -c 1-12,41-)"
1232 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./ ||
1233 $lines[0] =~ /^fatal: bad object $commit/) {
1236 $id = substr($lines[0], 0, 12);
1237 $desc = substr($lines[0], 41);
1240 return ($id, $desc);
1243 $chk_signoff = 0 if ($file);
1248 my @fixed_inserted = ();
1249 my @fixed_deleted = ();
1252 # If input is git commits, extract all commits from the commit expressions.
1253 # For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
1254 die "$P: No git repository found\n" if ($git && !-e
"$gitroot");
1258 foreach my $commit_expr (@ARGV) {
1260 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1261 $git_range = "-$2 $1";
1262 } elsif ($commit_expr =~ m/\.\./) {
1263 $git_range = "$commit_expr";
1265 $git_range = "-1 $commit_expr";
1267 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
1268 foreach my $line (split(/\n/, $lines)) {
1269 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1270 next if (!defined($1) || !defined($2));
1273 unshift(@commits, $sha1);
1274 $git_commits{$sha1} = $subject;
1277 die "$P: no git commits after extraction!\n" if (@commits == 0);
1282 $allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
1283 for my $filename (@ARGV) {
1285 my $is_git_file = git_is_single_file
($filename);
1286 my $oldfile = $file;
1287 $file = 1 if ($is_git_file);
1289 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1290 die "$P: $filename: git format-patch failed - $!\n";
1292 open($FILE, '-|', "diff -u /dev/null $filename") ||
1293 die "$P: $filename: diff failed - $!\n";
1294 } elsif ($filename eq '-') {
1295 open($FILE, '<&STDIN');
1297 open($FILE, '<', "$filename") ||
1298 die "$P: $filename: open failed - $!\n";
1300 if ($filename eq '-') {
1301 $vname = 'Your patch';
1303 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1309 push(@rawlines, $_);
1310 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
1314 if ($#ARGV > 0 && $quiet == 0) {
1315 print '-' x
length($vname) . "\n";
1317 print '-' x
length($vname) . "\n";
1320 if (!process
($filename)) {
1326 @fixed_inserted = ();
1327 @fixed_deleted = ();
1329 @modifierListFile = ();
1332 $file = $oldfile if ($is_git_file);
1336 hash_show_words
(\
%use_type, "Used");
1337 hash_show_words
(\
%ignore_type, "Ignored");
1339 if (!$perl_version_ok) {
1342 NOTE
: perl
$^V is
not modern enough to detect all possible issues
.
1343 An upgrade to at least perl
$minimum_perl_version is suggested
.
1350 NOTE
: If any of the errors are false positives
, please report
1351 them to the maintainer
, see CHECKPATCH
in MAINTAINERS
.
1354 # OpenOCD specific: Begin
1357 NOTE
: If any of the errors are false positives
, please report
1358 them to the openocd
-devel mailing list
or prepare a patch
1359 and send it to Gerrit
for review
.
1361 # OpenOCD specific: End
1367 sub top_of_kernel_tree
{
1372 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1373 "README", "Documentation", "arch", "include", "drivers",
1374 "fs", "init", "ipc", "kernel", "lib", "scripts",
1377 # OpenOCD specific: Begin
1379 "AUTHORS", "BUGS", "COPYING", "HACKING", "Makefile.am",
1380 "README", "contrib", "doc", "src", "tcl", "testing", "tools",
1382 # OpenOCD specific: End
1384 foreach my $check (@tree_check) {
1385 if (! -e
$root . '/' . $check) {
1393 my ($formatted_email) = @_;
1397 my $name_comment = "";
1401 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1404 $comment = $3 if defined $3;
1405 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1407 $comment = $2 if defined $2;
1408 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1410 $comment = $2 if defined $2;
1411 $formatted_email =~ s/\Q$address\E.*$//;
1412 $name = $formatted_email;
1413 $name = trim
($name);
1414 $name =~ s/^\"|\"$//g;
1415 # If there's a name left after stripping spaces and
1416 # leading quotes, and the address doesn't have both
1417 # leading and trailing angle brackets, the address
1419 # "joe smith joe@smith.com" bad
1420 # "joe smith <joe@smith.com" bad
1421 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1426 # OpenOCD specific: Begin: handle jenkins as valid email
1427 } elsif ($formatted_email eq "jenkins") {
1428 $address = "jenkins";
1429 # OpenOCD specific: End
1432 # Extract comments from names excluding quoted parts
1433 # "John D. (Doe)" - Do not extract
1434 if ($name =~ s/\"(.+)\"//) {
1437 while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1438 $name_comment .= trim
($1);
1440 $name =~ s/^[ \"]+|[ \"]+$//g;
1441 $name = trim
("$quoted $name");
1443 $address = trim
($address);
1444 $address =~ s/^\<|\>$//g;
1445 $comment = trim
($comment);
1447 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1448 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1449 $name = "\"$name\"";
1452 return ($name, $name_comment, $address, $comment);
1456 my ($name, $name_comment, $address, $comment) = @_;
1458 my $formatted_email;
1460 $name =~ s/^[ \"]+|[ \"]+$//g;
1461 $address = trim
($address);
1462 $address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
1464 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1465 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1466 $name = "\"$name\"";
1469 $name_comment = trim
($name_comment);
1470 $name_comment = " $name_comment" if ($name_comment ne "");
1471 $comment = trim
($comment);
1472 $comment = " $comment" if ($comment ne "");
1474 if ("$name" eq "") {
1475 $formatted_email = "$address";
1477 $formatted_email = "$name$name_comment <$address>";
1479 $formatted_email .= "$comment";
1480 return $formatted_email;
1483 sub reformat_email
{
1486 my ($email_name, $name_comment, $email_address, $comment) = parse_email
($email);
1487 return format_email
($email_name, $name_comment, $email_address, $comment);
1490 sub same_email_addresses
{
1491 my ($email1, $email2) = @_;
1493 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email
($email1);
1494 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email
($email2);
1496 return $email1_name eq $email2_name &&
1497 $email1_address eq $email2_address &&
1498 $name1_comment eq $name2_comment &&
1499 $comment1 eq $comment2;
1505 foreach my $path (split(/:/, $ENV{PATH
})) {
1506 if (-e
"$path/$bin") {
1507 return "$path/$bin";
1517 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1518 if (-e
"$path/$conf") {
1519 return "$path/$conf";
1531 for my $c (split(//, $str)) {
1535 for (; ($n % $tabsize) != 0; $n++) {
1547 (my $res = shift) =~ tr/\t/ /c;
1554 # Drop the diff line leader and expand tabs
1556 $line = expand_tabs
($line);
1558 # Pick the indent from the front of the line.
1559 my ($white) = ($line =~ /^(\s*)/);
1561 return (length($line), length($white));
1564 my $sanitise_quote = '';
1566 sub sanitise_line_reset
{
1567 my ($in_comment) = @_;
1570 $sanitise_quote = '*/';
1572 $sanitise_quote = '';
1585 # Always copy over the diff marker.
1586 $res = substr($line, 0, 1);
1588 for ($off = 1; $off < length($line); $off++) {
1589 $c = substr($line, $off, 1);
1591 # Comments we are whacking completely including the begin
1592 # and end, all to $;.
1593 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1594 $sanitise_quote = '*/';
1596 substr($res, $off, 2, "$;$;");
1600 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1601 $sanitise_quote = '';
1602 substr($res, $off, 2, "$;$;");
1606 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1607 $sanitise_quote = '//';
1609 substr($res, $off, 2, $sanitise_quote);
1614 # A \ in a string means ignore the next character.
1615 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1617 substr($res, $off, 2, 'XX');
1622 if ($c eq "'" || $c eq '"') {
1623 if ($sanitise_quote eq '') {
1624 $sanitise_quote = $c;
1626 substr($res, $off, 1, $c);
1628 } elsif ($sanitise_quote eq $c) {
1629 $sanitise_quote = '';
1633 #print "c<$c> SQ<$sanitise_quote>\n";
1634 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1635 substr($res, $off, 1, $;);
1636 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1637 substr($res, $off, 1, $;);
1638 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1639 substr($res, $off, 1, 'X');
1641 substr($res, $off, 1, $c);
1645 if ($sanitise_quote eq '//') {
1646 $sanitise_quote = '';
1649 # The pathname on a #include may be surrounded by '<' and '>'.
1650 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1651 my $clean = 'X' x
length($1);
1652 $res =~ s@\
<.*\
>@
<$clean>@
;
1654 # The whole of a #error is a string.
1655 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1656 my $clean = 'X' x
length($1);
1657 $res =~ s@
(\#\s
*(?
:error
|warning
)\s
+).*@
$1$clean@
;
1660 if ($allow_c99_comments && $res =~ m@
(//.*$)@
) {
1662 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1668 sub get_quoted_string
{
1669 my ($line, $rawline) = @_;
1671 return "" if (!defined($line) || !defined($rawline));
1672 return "" if ($line !~ m/($String)/g);
1673 return substr($rawline, $-[0], $+[0] - $-[0]);
1676 sub ctx_statement_block
{
1677 my ($linenr, $remain, $off) = @_;
1678 my $line = $linenr - 1;
1681 my $coff = $off - 1;
1695 @stack = (['', 0]) if ($#stack == -1);
1697 #warn "CSB: blk<$blk> remain<$remain>\n";
1698 # If we are about to drop off the end, pull in more
1701 for (; $remain > 0; $line++) {
1702 last if (!defined $lines[$line]);
1703 next if ($lines[$line] =~ /^-/);
1706 $blk .= $lines[$line] . "\n";
1707 $len = length($blk);
1711 # Bail if there is no further context.
1712 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
1716 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1722 $c = substr($blk, $off, 1);
1723 $remainder = substr($blk, $off);
1725 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1727 # Handle nested #if/#else.
1728 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1729 push(@stack, [ $type, $level ]);
1730 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1731 ($type, $level) = @
{$stack[$#stack - 1]};
1732 } elsif ($remainder =~ /^#\s*endif\b/) {
1733 ($type, $level) = @
{pop(@stack)};
1736 # Statement ends at the ';' or a close '}' at the
1738 if ($level == 0 && $c eq ';') {
1742 # An else is really a conditional as long as its not else if
1743 if ($level == 0 && $coff_set == 0 &&
1744 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1745 $remainder =~ /^(else)(?:\s|{)/ &&
1746 $remainder !~ /^else\s+if\b/) {
1747 $coff = $off + length($1) - 1;
1749 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1750 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1753 if (($type eq '' || $type eq '(') && $c eq '(') {
1757 if ($type eq '(' && $c eq ')') {
1759 $type = ($level != 0)?
'(' : '';
1761 if ($level == 0 && $coff < $soff) {
1764 #warn "CSB: mark coff<$coff>\n";
1767 if (($type eq '' || $type eq '{') && $c eq '{') {
1771 if ($type eq '{' && $c eq '}') {
1773 $type = ($level != 0)?
'{' : '';
1776 if (substr($blk, $off + 1, 1) eq ';') {
1782 # Preprocessor commands end at the newline unless escaped.
1783 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1791 # We are truly at the end, so shuffle to the next line.
1798 my $statement = substr($blk, $soff, $off - $soff + 1);
1799 my $condition = substr($blk, $soff, $coff - $soff + 1);
1801 #warn "STATEMENT<$statement>\n";
1802 #warn "CONDITION<$condition>\n";
1804 #print "coff<$coff> soff<$off> loff<$loff>\n";
1806 return ($statement, $condition,
1807 $line, $remain + 1, $off - $loff + 1, $level);
1810 sub statement_lines
{
1813 # Strip the diff line prefixes and rip blank lines at start and end.
1814 $stmt =~ s/(^|\n)./$1/g;
1818 my @stmt_lines = ($stmt =~ /\n/g);
1820 return $#stmt_lines + 2;
1823 sub statement_rawlines
{
1826 my @stmt_lines = ($stmt =~ /\n/g);
1828 return $#stmt_lines + 2;
1831 sub statement_block_size
{
1834 $stmt =~ s/(^|\n)./$1/g;
1840 my @stmt_lines = ($stmt =~ /\n/g);
1841 my @stmt_statements = ($stmt =~ /;/g);
1843 my $stmt_lines = $#stmt_lines + 2;
1844 my $stmt_statements = $#stmt_statements + 1;
1846 if ($stmt_lines > $stmt_statements) {
1849 return $stmt_statements;
1853 sub ctx_statement_full
{
1854 my ($linenr, $remain, $off) = @_;
1855 my ($statement, $condition, $level);
1859 # Grab the first conditional/block pair.
1860 ($statement, $condition, $linenr, $remain, $off, $level) =
1861 ctx_statement_block
($linenr, $remain, $off);
1862 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1863 push(@chunks, [ $condition, $statement ]);
1864 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1865 return ($level, $linenr, @chunks);
1868 # Pull in the following conditional/block pairs and see if they
1869 # could continue the statement.
1871 ($statement, $condition, $linenr, $remain, $off, $level) =
1872 ctx_statement_block
($linenr, $remain, $off);
1873 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1874 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1876 push(@chunks, [ $condition, $statement ]);
1879 return ($level, $linenr, @chunks);
1883 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1885 my $start = $linenr - 1;
1892 my @stack = ($level);
1893 for ($line = $start; $remain > 0; $line++) {
1894 next if ($rawlines[$line] =~ /^-/);
1897 $blk .= $rawlines[$line];
1899 # Handle nested #if/#else.
1900 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1901 push(@stack, $level);
1902 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1903 $level = $stack[$#stack - 1];
1904 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1905 $level = pop(@stack);
1908 foreach my $c (split(//, $lines[$line])) {
1909 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1915 if ($c eq $close && $level > 0) {
1917 last if ($level == 0);
1918 } elsif ($c eq $open) {
1923 if (!$outer || $level <= 1) {
1924 push(@res, $rawlines[$line]);
1927 last if ($level == 0);
1930 return ($level, @res);
1932 sub ctx_block_outer
{
1933 my ($linenr, $remain) = @_;
1935 my ($level, @r) = ctx_block_get
($linenr, $remain, 1, '{', '}', 0);
1939 my ($linenr, $remain) = @_;
1941 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
1945 my ($linenr, $remain, $off) = @_;
1947 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
1950 sub ctx_block_level
{
1951 my ($linenr, $remain) = @_;
1953 return ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
1955 sub ctx_statement_level
{
1956 my ($linenr, $remain, $off) = @_;
1958 return ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
1961 sub ctx_locate_comment
{
1962 my ($first_line, $end_line) = @_;
1964 # If c99 comment on the current line, or the line before or after
1965 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@
^\
+.*(//.*$)@
);
1966 return $current_comment if (defined $current_comment);
1967 ($current_comment) = ($rawlines[$end_line - 2] =~ m@
^[\
+ ].*(//.*$)@
);
1968 return $current_comment if (defined $current_comment);
1969 ($current_comment) = ($rawlines[$end_line] =~ m@
^[\
+ ].*(//.*$)@
);
1970 return $current_comment if (defined $current_comment);
1972 # Catch a comment on the end of the line itself.
1973 ($current_comment) = ($rawlines[$end_line - 1] =~ m@
.*(/\*.*\*/)\s
*(?
:\\\s
*)?
$@
);
1974 return $current_comment if (defined $current_comment);
1976 # Look through the context and try and figure out if there is a
1979 $current_comment = '';
1980 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1981 my $line = $rawlines[$linenr - 1];
1983 if ($linenr == $first_line and $line =~ m@
^.\s
*\
*@
) {
1986 if ($line =~ m@
/\
*@
) {
1989 if (!$in_comment && $current_comment ne '') {
1990 $current_comment = '';
1992 $current_comment .= $line . "\n" if ($in_comment);
1993 if ($line =~ m@\
*/@
) {
1998 chomp($current_comment);
1999 return($current_comment);
2001 sub ctx_has_comment
{
2002 my ($first_line, $end_line) = @_;
2003 my $cmt = ctx_locate_comment
($first_line, $end_line);
2005 ##print "LINE: $rawlines[$end_line - 1 ]\n";
2006 ##print "CMMT: $cmt\n";
2008 return ($cmt ne '');
2012 my ($linenr, $cnt) = @_;
2014 my $offset = $linenr - 1;
2019 $line = $rawlines[$offset++];
2020 next if (defined($line) && $line =~ /^-/);
2028 my ($linenr, $lc) = @_;
2030 my $stat_real = raw_line
($linenr, 0);
2031 for (my $count = $linenr + 1; $count <= $lc; $count++) {
2032 $stat_real = $stat_real . "\n" . raw_line
($count, 0);
2039 my ($linenr, $cnt, $here) = @_;
2041 my $herectx = $here . "\n";
2042 for (my $n = 0; $n < $cnt; $n++) {
2043 $herectx .= raw_line
($linenr, $n) . "\n";
2054 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
2057 $coded = sprintf("^%c", unpack('C', $2) + 64);
2066 my $av_preprocessor = 0;
2071 sub annotate_reset
{
2072 $av_preprocessor = 0;
2074 @av_paren_type = ('E');
2075 $av_pend_colon = 'O';
2078 sub annotate_values
{
2079 my ($stream, $type) = @_;
2082 my $var = '_' x
length($stream);
2085 print "$stream\n" if ($dbg_values > 1);
2087 while (length($cur)) {
2088 @av_paren_type = ('E') if ($#av_paren_type < 0);
2089 print " <" . join('', @av_paren_type) .
2090 "> <$type> <$av_pending>" if ($dbg_values > 1);
2091 if ($cur =~ /^(\s+)/o) {
2092 print "WS($1)\n" if ($dbg_values > 1);
2093 if ($1 =~ /\n/ && $av_preprocessor) {
2094 $type = pop(@av_paren_type);
2095 $av_preprocessor = 0;
2098 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
2099 print "CAST($1)\n" if ($dbg_values > 1);
2100 push(@av_paren_type, $type);
2103 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
2104 print "DECLARE($1)\n" if ($dbg_values > 1);
2107 } elsif ($cur =~ /^($Modifier)\s*/) {
2108 print "MODIFIER($1)\n" if ($dbg_values > 1);
2111 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
2112 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
2113 $av_preprocessor = 1;
2114 push(@av_paren_type, $type);
2120 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
2121 print "UNDEF($1)\n" if ($dbg_values > 1);
2122 $av_preprocessor = 1;
2123 push(@av_paren_type, $type);
2125 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
2126 print "PRE_START($1)\n" if ($dbg_values > 1);
2127 $av_preprocessor = 1;
2129 push(@av_paren_type, $type);
2130 push(@av_paren_type, $type);
2133 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
2134 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
2135 $av_preprocessor = 1;
2137 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
2141 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
2142 print "PRE_END($1)\n" if ($dbg_values > 1);
2144 $av_preprocessor = 1;
2146 # Assume all arms of the conditional end as this
2147 # one does, and continue as if the #endif was not here.
2148 pop(@av_paren_type);
2149 push(@av_paren_type, $type);
2152 } elsif ($cur =~ /^(\\\n)/o) {
2153 print "PRECONT($1)\n" if ($dbg_values > 1);
2155 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
2156 print "ATTR($1)\n" if ($dbg_values > 1);
2157 $av_pending = $type;
2160 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
2161 print "SIZEOF($1)\n" if ($dbg_values > 1);
2167 } elsif ($cur =~ /^(if|while|for)\b/o) {
2168 print "COND($1)\n" if ($dbg_values > 1);
2172 } elsif ($cur =~/^(case)/o) {
2173 print "CASE($1)\n" if ($dbg_values > 1);
2174 $av_pend_colon = 'C';
2177 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
2178 print "KEYWORD($1)\n" if ($dbg_values > 1);
2181 } elsif ($cur =~ /^(\()/o) {
2182 print "PAREN('$1')\n" if ($dbg_values > 1);
2183 push(@av_paren_type, $av_pending);
2187 } elsif ($cur =~ /^(\))/o) {
2188 my $new_type = pop(@av_paren_type);
2189 if ($new_type ne '_') {
2191 print "PAREN('$1') -> $type\n"
2192 if ($dbg_values > 1);
2194 print "PAREN('$1')\n" if ($dbg_values > 1);
2197 } elsif ($cur =~ /^($Ident)\s*\(/o) {
2198 print "FUNC($1)\n" if ($dbg_values > 1);
2202 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
2203 if (defined $2 && $type eq 'C' || $type eq 'T') {
2204 $av_pend_colon = 'B';
2205 } elsif ($type eq 'E') {
2206 $av_pend_colon = 'L';
2208 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
2211 } elsif ($cur =~ /^($Ident|$Constant)/o) {
2212 print "IDENT($1)\n" if ($dbg_values > 1);
2215 } elsif ($cur =~ /^($Assignment)/o) {
2216 print "ASSIGN($1)\n" if ($dbg_values > 1);
2219 } elsif ($cur =~/^(;|{|})/) {
2220 print "END($1)\n" if ($dbg_values > 1);
2222 $av_pend_colon = 'O';
2224 } elsif ($cur =~/^(,)/) {
2225 print "COMMA($1)\n" if ($dbg_values > 1);
2228 } elsif ($cur =~ /^(\?)/o) {
2229 print "QUESTION($1)\n" if ($dbg_values > 1);
2232 } elsif ($cur =~ /^(:)/o) {
2233 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
2235 substr($var, length($res), 1, $av_pend_colon);
2236 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
2241 $av_pend_colon = 'O';
2243 } elsif ($cur =~ /^(\[)/o) {
2244 print "CLOSE($1)\n" if ($dbg_values > 1);
2247 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
2250 print "OPV($1)\n" if ($dbg_values > 1);
2257 substr($var, length($res), 1, $variant);
2260 } elsif ($cur =~ /^($Operators)/o) {
2261 print "OP($1)\n" if ($dbg_values > 1);
2262 if ($1 ne '++' && $1 ne '--') {
2266 } elsif ($cur =~ /(^.)/o) {
2267 print "C($1)\n" if ($dbg_values > 1);
2270 $cur = substr($cur, length($1));
2271 $res .= $type x
length($1);
2275 return ($res, $var);
2279 my ($possible, $line) = @_;
2280 my $notPermitted = qr{(?
:
2297 ^(?
:typedef
|struct
|enum
)\b
2299 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2300 if ($possible !~ $notPermitted) {
2301 # Check for modifiers.
2302 $possible =~ s/\s*$Storage\s*//g;
2303 $possible =~ s/\s*$Sparse\s*//g;
2304 if ($possible =~ /^\s*$/) {
2306 } elsif ($possible =~ /\s/) {
2307 $possible =~ s/\s*$Type\s*//g;
2308 for my $modifier (split(' ', $possible)) {
2309 if ($modifier !~ $notPermitted) {
2310 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
2311 push(@modifierListFile, $modifier);
2316 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
2317 push(@typeListFile, $possible);
2321 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
2330 $type =~ tr/[a-z]/[A-Z]/;
2332 return defined $use_type{$type} if (scalar keys %use_type > 0);
2334 return !defined $ignore_type{$type};
2338 my ($level, $type, $msg) = @_;
2340 if (!show_type
($type) ||
2341 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2346 if ($level eq 'ERROR') {
2348 } elsif ($level eq 'WARNING') {
2354 $output .= $prefix . $level . ':';
2356 $output .= BLUE
if ($color);
2357 $output .= "$type:";
2359 $output .= RESET
if ($color);
2360 $output .= ' ' . $msg . "\n";
2363 my @lines = split("\n", $output, -1);
2364 splice(@lines, 1, 1);
2365 $output = join("\n", @lines);
2369 $output = (split('\n', $output))[0] . "\n";
2372 if ($verbose && exists($verbose_messages{$type}) &&
2373 !exists($verbose_emitted{$type})) {
2374 $output .= $verbose_messages{$type} . "\n\n";
2375 $verbose_emitted{$type} = 1;
2378 push(our @report, $output);
2387 sub fixup_current_range
{
2388 my ($lineRef, $offset, $length) = @_;
2390 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2393 my $no = $o + $offset;
2394 my $nl = $l + $length;
2395 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2399 sub fix_inserted_deleted_lines
{
2400 my ($linesRef, $insertedRef, $deletedRef) = @_;
2402 my $range_last_linenr = 0;
2403 my $delta_offset = 0;
2408 my $next_insert = 0;
2409 my $next_delete = 0;
2413 my $inserted = @
{$insertedRef}[$next_insert++];
2414 my $deleted = @
{$deletedRef}[$next_delete++];
2416 foreach my $old_line (@
{$linesRef}) {
2418 my $line = $old_line; #don't modify the array
2419 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
2421 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2422 $range_last_linenr = $new_linenr;
2423 fixup_current_range
(\
$line, $delta_offset, 0);
2426 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2427 $deleted = @
{$deletedRef}[$next_delete++];
2429 fixup_current_range
(\
$lines[$range_last_linenr], $delta_offset--, -1);
2432 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2433 push(@lines, ${$inserted}{'LINE'});
2434 $inserted = @
{$insertedRef}[$next_insert++];
2436 fixup_current_range
(\
$lines[$range_last_linenr], $delta_offset++, 1);
2440 push(@lines, $line);
2450 sub fix_insert_line
{
2451 my ($linenr, $line) = @_;
2457 push(@fixed_inserted, $inserted);
2460 sub fix_delete_line
{
2461 my ($linenr, $line) = @_;
2468 push(@fixed_deleted, $deleted);
2472 my ($type, $msg) = @_;
2474 if (report
("ERROR", $type, $msg)) {
2482 my ($type, $msg) = @_;
2484 if (report
("WARNING", $type, $msg)) {
2492 my ($type, $msg) = @_;
2494 if ($check && report
("CHECK", $type, $msg)) {
2502 sub check_absolute_file
{
2503 my ($absolute, $herecurr) = @_;
2504 my $file = $absolute;
2506 ##print "absolute<$absolute>\n";
2508 # See if any suffix of this path is a path within the tree.
2509 while ($file =~ s@
^[^/]*/@@
) {
2510 if (-f
"$root/$file") {
2511 ##print "file<$file>\n";
2519 # It is, so see if the prefix is acceptable.
2520 my $prefix = $absolute;
2521 substr($prefix, -length($file)) = '';
2523 ##print "prefix<$prefix>\n";
2524 if ($prefix ne ".../") {
2525 WARN
("USE_RELATIVE_PATH",
2526 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
2533 $string =~ s/^\s+|\s+$//g;
2541 $string =~ s/^\s+//;
2549 $string =~ s/\s+$//;
2554 sub string_find_replace
{
2555 my ($string, $find, $replace) = @_;
2557 $string =~ s/$find/$replace/g;
2565 my $source_indent = $tabsize;
2566 my $max_spaces_before_tab = $source_indent - 1;
2567 my $spaces_to_tab = " " x
$source_indent;
2569 #convert leading spaces to tabs
2570 1 while $leading =~ s@
^([\t]*)$spaces_to_tab@
$1\t@g;
2571 #Remove spaces before a tab
2572 1 while $leading =~ s@
^([\t]*)( {1,$max_spaces_before_tab})\t@
$1\t@g;
2577 sub pos_last_openparen
{
2582 my $opens = $line =~ tr/\(/\(/;
2583 my $closes = $line =~ tr/\)/\)/;
2585 my $last_openparen = 0;
2587 if (($opens == 0) || ($closes >= $opens)) {
2591 my $len = length($line);
2593 for ($pos = 0; $pos < $len; $pos++) {
2594 my $string = substr($line, $pos);
2595 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2596 $pos += length($1) - 1;
2597 } elsif (substr($line, $pos, 1) eq '(') {
2598 $last_openparen = $pos;
2599 } elsif (index($string, '(') == -1) {
2604 return length(expand_tabs
(substr($line, 0, $last_openparen))) + 1;
2607 sub get_raw_comment
{
2608 my ($line, $rawline) = @_;
2611 for my $i (0 .. (length($line) - 1)) {
2612 if (substr($line, $i, 1) eq "$;") {
2613 $comment .= substr($rawline, $i, 1);
2620 sub exclude_global_initialisers
{
2621 my ($realfile) = @_;
2623 # Do not check for BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c).
2624 return $realfile =~ m@
^tools
/testing
/selftests
/bpf
/progs/.*\
.c
$@
||
2625 $realfile =~ m@
^samples
/bpf/.*_kern\
.c
$@
||
2626 $realfile =~ m@
/bpf/.*\
.bpf\
.c
$@
;
2630 my $filename = shift;
2636 my $stashrawline="";
2646 my $authorsignoff = 0;
2647 my $author_sob = '';
2649 my $is_binding_patch = -1;
2650 my $in_header_lines = $file ?
0 : 1;
2651 my $in_commit_log = 0; #Scanning lines before patch
2652 my $has_patch_separator = 0; #Found a --- line
2653 my $has_commit_log = 0; #Encountered lines before patch
2654 my $commit_log_lines = 0; #Number of commit log lines
2655 my $commit_log_possible_stack_dump = 0;
2656 my $commit_log_long_line = 0;
2657 my $commit_log_has_diff = 0;
2658 my $reported_maintainer_file = 0;
2659 my $non_utf8_charset = 0;
2661 my $last_git_commit_id_linenr = -1;
2663 my $last_blank_line = 0;
2664 my $last_coalesced_string_linenr = -1;
2672 # Trace the real file/line as we go.
2677 my $context_function; #undef'd unless there's a known function
2679 my $comment_edge = 0;
2683 my $prev_values = 'E';
2686 my %suppress_ifbraces;
2687 my %suppress_whiletrailers;
2688 my %suppress_export;
2689 my $suppress_statement = 0;
2691 my %signatures = ();
2693 # Pre-scan the patch sanitizing the lines.
2694 # Pre-scan the patch looking for any __setup documentation.
2696 my @setup_docs = ();
2699 my $camelcase_file_seeded = 0;
2701 my $checklicenseline = 1;
2703 sanitise_line_reset
();
2705 foreach my $rawline (@rawlines) {
2709 push(@fixed, $rawline) if ($fix);
2711 if ($rawline=~/^\+\+\+\s+(\S+)/) {
2713 if ($1 =~ m
@Documentation/admin-guide/kernel
-parameters
.txt
$@
) {
2718 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2727 # Guestimate if this is a continuing comment. Run
2728 # the context looking for a comment "edge". If this
2729 # edge is a close comment then we must be in a comment
2733 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2734 next if (defined $rawlines[$ln - 1] &&
2735 $rawlines[$ln - 1] =~ /^-/);
2737 #print "RAW<$rawlines[$ln - 1]>\n";
2738 last if (!defined $rawlines[$ln - 1]);
2739 if ($rawlines[$ln - 1] =~ m@
(/\*|\*/)@
&&
2740 $rawlines[$ln - 1] !~ m@
"[^"]*(?
:/\*|\*/)[^"]*"@
) {
2745 if (defined $edge && $edge eq '*/') {
2749 # Guestimate if this is a continuing comment. If this
2750 # is the start of a diff block and this line starts
2751 # ' *' then it is very likely a comment.
2752 if (!defined $edge &&
2753 $rawlines[$linenr] =~ m@
^.\s
*(?
:\
*\
*+| \
*)(?
:\s
|$)@
)
2758 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2759 sanitise_line_reset
($in_comment);
2761 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2762 # Standardise the strings and chars within the input to
2763 # simplify matching -- only bother with positive lines.
2764 $line = sanitise_line
($rawline);
2766 push(@lines, $line);
2769 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2774 #print "==>$rawline\n";
2775 #print "-->$line\n";
2777 if ($setup_docs && $line =~ /^\+/) {
2778 push(@setup_docs, $line);
2787 foreach my $line (@lines) {
2790 my $sline = $line; #copy of $line
2791 $sline =~ s/$;/ /g; #with comments as spaces
2793 my $rawline = $rawlines[$linenr - 1];
2794 my $raw_comment = get_raw_comment
($line, $rawline);
2796 # check if it's a mode change, rename or start of a patch
2797 if (!$in_commit_log &&
2798 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2799 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2800 $line =~ /^diff --git a\/[\w\
/\.\_\-]+ b\/\S
+\s
*$/))) {
2804 #extract the line range in the file after the patch is applied
2805 if (!$in_commit_log &&
2806 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2809 $first_line = $linenr + 1;
2819 %suppress_ifbraces = ();
2820 %suppress_whiletrailers = ();
2821 %suppress_export = ();
2822 $suppress_statement = 0;
2823 if ($context =~ /\b(\w+)\s*\(/) {
2824 $context_function = $1;
2826 undef $context_function;
2830 # track the line number as we move through the hunk, note that
2831 # new versions of GNU diff omit the leading space on completely
2832 # blank context lines so we need to count that too.
2833 } elsif ($line =~ /^( |\+|$)/) {
2835 $realcnt-- if ($realcnt != 0);
2837 # Measure the line length and indent.
2838 ($length, $indent) = line_stats
($rawline);
2840 # Track the previous line.
2841 ($prevline, $stashline) = ($stashline, $line);
2842 ($previndent, $stashindent) = ($stashindent, $indent);
2843 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2845 #warn "line<$line>\n";
2847 } elsif ($realcnt == 1) {
2851 my $hunk_line = ($realcnt != 0);
2853 $here = "#$linenr: " if (!$file);
2854 $here = "#$realline: " if ($file);
2857 # extract the filename as it passes
2858 if ($line =~ /^diff --git.*?(\S+)$/) {
2860 $realfile =~ s@
^([^/]*)/@@
if (!$file);
2863 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2865 $realfile =~ s@
^([^/]*)/@@
if (!$file);
2869 if (!$file && $tree && $p1_prefix ne '' &&
2870 -e
"$root/$p1_prefix") {
2871 WARN
("PATCH_PREFIX",
2872 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2875 if ($realfile =~ m@
^include
/asm/@
) {
2876 ERROR
("MODIFIED_INCLUDE_ASM",
2877 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2882 #make up the handle for any error we report on this line
2884 $prefix = "$realfile:$realline: "
2887 $prefix = "$filename:$realline: ";
2889 $prefix = "$filename:$linenr: ";
2894 if (is_maintained_obsolete
($realfile)) {
2896 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2898 if ($realfile =~ m@
^(?
:drivers
/net
/|net
/|drivers
/staging/)@
) {
2901 $check = $check_orig;
2903 $checklicenseline = 1;
2905 if ($realfile !~ /^MAINTAINERS/) {
2906 my $last_binding_patch = $is_binding_patch;
2908 $is_binding_patch = () = $realfile =~ m@
^(?
:Documentation
/devicetree/|include
/dt-bindings/)@
;
2910 if (($last_binding_patch != -1) &&
2911 ($last_binding_patch ^ $is_binding_patch)) {
2912 WARN
("DT_SPLIT_BINDING_PATCH",
2913 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n");
2920 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2922 my $hereline = "$here\n$rawline\n";
2923 my $herecurr = "$here\n$rawline\n";
2924 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2926 $cnt_lines++ if ($realcnt != 0);
2928 # Verify the existence of a commit log if appropriate
2929 # 2 is used because a $signature is counted in $commit_log_lines
2930 if ($in_commit_log) {
2931 if ($line !~ /^\s*$/) {
2932 $commit_log_lines++; #could be a $signature
2934 } elsif ($has_commit_log && $commit_log_lines < 2) {
2935 WARN
("COMMIT_MESSAGE",
2936 "Missing commit description - Add an appropriate one\n");
2937 $commit_log_lines = 2; #warn only once
2940 # Check if the commit log has what seems like a diff which can confuse patch
2941 if ($in_commit_log && !$commit_log_has_diff &&
2942 (($line =~ m@
^\s
+diff
\b.*a
/([\w/]+)@
&&
2943 $line =~ m@
^\s
+diff
\b.*a
/[\w/]+\s
+b
/$1\b@
) ||
2944 $line =~ m@
^\s
*(?
:\
-\
-\
-\s
+a
/|\+\+\+\s+b/)@
||
2945 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2946 ERROR
("DIFF_IN_COMMIT_MSG",
2947 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2948 $commit_log_has_diff = 1;
2951 # Check for incorrect file permissions
2952 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2953 my $permhere = $here . "FILE: $realfile\n";
2954 if ($realfile !~ m
@scripts/@
&&
2955 $realfile !~ /\.(py|pl|awk|sh)$/) {
2956 ERROR
("EXECUTE_PERMISSIONS",
2957 "do not set execute permissions for source files\n" . $permhere);
2961 # Check the patch for a From:
2962 if (decode
("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2964 my $curline = $linenr;
2965 while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2968 $author = encode
("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2970 $author = reformat_email
($author);
2973 # Check the patch for a signoff:
2974 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
2977 if ($author ne '' && $authorsignoff != 1) {
2978 if (same_email_addresses
($1, $author)) {
2982 my ($email_name, $email_comment, $email_address, $comment1) = parse_email
($ctx);
2983 my ($author_name, $author_comment, $author_address, $comment2) = parse_email
($author);
2985 if (lc $email_address eq lc $author_address && $email_name eq $author_name) {
2988 } elsif (lc $email_address eq lc $author_address) {
2991 } elsif ($email_name eq $author_name) {
2995 my $address1 = $email_address;
2996 my $address2 = $author_address;
2998 if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
3001 if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
3004 if ($address1 eq $address2) {
3012 # Check for patch separator
3013 if ($line =~ /^---$/) {
3014 $has_patch_separator = 1;
3018 # Check if MAINTAINERS is being updated. If so, there's probably no need to
3019 # emit the "does MAINTAINERS need updating?" message on file add/move/delete
3020 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
3021 $reported_maintainer_file = 1;
3024 # Check signature styles
3025 if (!$in_header_lines &&
3026 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
3027 my $space_before = $1;
3029 my $space_after = $3;
3031 my $ucfirst_sign_off = ucfirst(lc($sign_off));
3033 if ($sign_off !~ /$signature_tags/) {
3034 my $suggested_signature = find_standard_signature
($sign_off);
3035 if ($suggested_signature eq "") {
3036 WARN
("BAD_SIGN_OFF",
3037 "Non-standard signature: $sign_off\n" . $herecurr);
3039 if (WARN
("BAD_SIGN_OFF",
3040 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
3042 $fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
3046 if (defined $space_before && $space_before ne "") {
3047 if (WARN
("BAD_SIGN_OFF",
3048 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
3050 $fixed[$fixlinenr] =
3051 "$ucfirst_sign_off $email";
3054 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
3055 if (WARN
("BAD_SIGN_OFF",
3056 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
3058 $fixed[$fixlinenr] =
3059 "$ucfirst_sign_off $email";
3063 if (!defined $space_after || $space_after ne " ") {
3064 if (WARN
("BAD_SIGN_OFF",
3065 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
3067 $fixed[$fixlinenr] =
3068 "$ucfirst_sign_off $email";
3072 my ($email_name, $name_comment, $email_address, $comment) = parse_email
($email);
3073 my $suggested_email = format_email
(($email_name, $name_comment, $email_address, $comment));
3074 if ($suggested_email eq "") {
3075 ERROR
("BAD_SIGN_OFF",
3076 "Unrecognized email address: '$email'\n" . $herecurr);
3078 my $dequoted = $suggested_email;
3079 $dequoted =~ s/^"//;
3080 $dequoted =~ s/" </ </;
3081 # Don't force email to have quotes
3082 # Allow just an angle bracketed address
3083 if (!same_email_addresses
($email, $suggested_email)) {
3084 if (WARN
("BAD_SIGN_OFF",
3085 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
3087 $fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
3091 # Address part shouldn't have comments
3092 my $stripped_address = $email_address;
3093 $stripped_address =~ s/\([^\(\)]*\)//g;
3094 if ($email_address ne $stripped_address) {
3095 if (WARN
("BAD_SIGN_OFF",
3096 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
3098 $fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
3102 # Only one name comment should be allowed
3103 my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
3104 if ($comment_count > 1) {
3105 WARN
("BAD_SIGN_OFF",
3106 "Use a single name comment in email: '$email'\n" . $herecurr);
3110 # stable@vger.kernel.org or stable@kernel.org shouldn't
3111 # have an email name. In addition comments should strictly
3113 if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
3114 if (($comment ne "" && $comment !~ /^#.+/) ||
3115 ($email_name ne "")) {
3116 my $cur_name = $email_name;
3117 my $new_comment = $comment;
3118 $cur_name =~ s/[a-zA-Z\s\-\"]+//g;
3120 # Remove brackets enclosing comment text
3121 # and # from start of comments to get comment text
3122 $new_comment =~ s/^\((.*)\)$/$1/;
3123 $new_comment =~ s/^\[(.*)\]$/$1/;
3124 $new_comment =~ s/^[\s\#]+|\s+$//g;
3126 $new_comment = trim
("$new_comment $cur_name") if ($cur_name ne $new_comment);
3127 $new_comment = " # $new_comment" if ($new_comment ne "");
3128 my $new_email = "$email_address$new_comment";
3130 if (WARN
("BAD_STABLE_ADDRESS_STYLE",
3131 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
3133 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3136 } elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
3137 my $new_comment = $comment;
3139 # Extract comment text from within brackets or
3140 # c89 style /*...*/ comments
3141 $new_comment =~ s/^\[(.*)\]$/$1/;
3142 $new_comment =~ s/^\/\*(.*)\*\/$/$1/;
3144 $new_comment = trim
($new_comment);
3145 $new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
3146 $new_comment = "($new_comment)" if ($new_comment ne "");
3147 my $new_email = format_email
($email_name, $name_comment, $email_address, $new_comment);
3149 if (WARN
("BAD_SIGN_OFF",
3150 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
3152 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3157 # Check for duplicate signatures
3158 my $sig_nospace = $line;
3159 $sig_nospace =~ s/\s//g;
3160 $sig_nospace = lc($sig_nospace);
3161 if (defined $signatures{$sig_nospace}) {
3162 WARN
("BAD_SIGN_OFF",
3163 "Duplicate signature\n" . $herecurr);
3165 $signatures{$sig_nospace} = 1;
3168 # Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
3169 if ($sign_off =~ /^co-developed-by:$/i) {
3170 if ($email eq $author) {
3171 WARN
("BAD_SIGN_OFF",
3172 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
3174 if (!defined $lines[$linenr]) {
3175 WARN
("BAD_SIGN_OFF",
3176 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
3177 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
3178 WARN
("BAD_SIGN_OFF",
3179 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
3180 } elsif ($1 ne $email) {
3181 WARN
("BAD_SIGN_OFF",
3182 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
3187 # Check email subject for common tools that don't need to be mentioned
3188 if ($in_header_lines &&
3189 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
3190 WARN
("EMAIL_SUBJECT",
3191 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
3194 # Check for Gerrit Change-Ids not in any patch context
3195 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
3196 if (ERROR
("GERRIT_CHANGE_ID",
3197 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
3199 fix_delete_line
($fixlinenr, $rawline);
3203 # Check if the commit log is in a possible stack dump
3204 if ($in_commit_log && !$commit_log_possible_stack_dump &&
3205 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
3206 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
3208 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
3209 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
3210 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
3211 # stack dump address styles
3212 $commit_log_possible_stack_dump = 1;
3215 # Check for line lengths > 75 in commit log, warn once
3216 if ($in_commit_log && !$commit_log_long_line &&
3217 length($line) > 75 &&
3218 !($line =~ /^\s*[a-zA-Z0-9_\/\
.]+\s
+\
|\s
+\d
+/ ||
3219 # file delta changes
3220 $line =~ /^\s*(?:[\w\.\-\+]*\/)++[\w\
.\
-\
+]+:/ ||
3222 $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i ||
3223 # A Fixes: or Link: line or signature tag line
3224 $commit_log_possible_stack_dump)) {
3225 WARN
("COMMIT_LOG_LONG_LINE",
3226 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
3227 $commit_log_long_line = 1;
3230 # Reset possible stack dump if a blank line is found
3231 if ($in_commit_log && $commit_log_possible_stack_dump &&
3233 $commit_log_possible_stack_dump = 0;
3236 # Check for lines starting with a #
3237 if ($in_commit_log && $line =~ /^#/) {
3238 if (WARN
("COMMIT_COMMENT_SYMBOL",
3239 "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
3241 $fixed[$fixlinenr] =~ s/^/ /;
3245 # Check for git id commit length and improperly formed commit descriptions
3246 # A correctly formed commit description is:
3247 # commit <SHA-1 hash length 12+ chars> ("Complete commit subject")
3248 # with the commit subject '("' prefix and '")' suffix
3249 # This is a fairly compilicated block as it tests for what appears to be
3250 # bare SHA-1 hash with minimum length of 5. It also avoids several types of
3251 # possible SHA-1 matches.
3252 # A commit match can span multiple lines so this block attempts to find a
3253 # complete typical commit on a maximum of 3 lines
3254 if ($perl_version_ok &&
3255 $in_commit_log && !$commit_log_possible_stack_dump &&
3256 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
3257 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
3258 (($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
3259 ($line =~ /\bcommit\s*$/i && defined($rawlines[$linenr]) && $rawlines[$linenr] =~ /^\s*[0-9a-f]{5,}\b/i)) ||
3260 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
3261 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
3262 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
3263 my $init_char = "c";
3264 my $orig_commit = "";
3269 my $id = '0123456789ab';
3270 my $orig_desc = "commit description";
3271 my $description = "";
3272 my $herectx = $herecurr;
3277 if ($line =~ /(?:\bcommit\s+[0-9a-f]{5,}|\bcommit\s*$)/i) {
3278 for (my $n = 0; $n < 2; $n++) {
3279 if ($input =~ /\bcommit\s+[0-9a-f]{5,}\s*($balanced_parens)/i) {
3282 # Always strip leading/trailing parens then double quotes if existing
3283 $orig_desc = substr($orig_desc, 1, -1);
3284 if ($orig_desc =~ /^".*"$/) {
3285 $orig_desc = substr($orig_desc, 1, -1);
3290 last if ($#lines < $linenr + $n);
3291 $input .= " " . trim
($rawlines[$linenr + $n]);
3292 $herectx .= "$rawlines[$linenr + $n]\n";
3294 $herectx = $herecurr if (!$has_parens);
3297 if ($input =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
3299 $orig_commit = lc($2);
3300 $short = 0 if ($input =~ /\bcommit\s+[0-9a-f]{12,40}/i);
3301 $long = 1 if ($input =~ /\bcommit\s+[0-9a-f]{41,}/i);
3302 $space = 0 if ($input =~ /\bcommit [0-9a-f]/i);
3303 $case = 0 if ($input =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
3304 } elsif ($input =~ /\b([0-9a-f]{12,40})\b/i) {
3305 $orig_commit = lc($1);
3308 ($id, $description) = git_commit_info
($orig_commit,
3312 ($short || $long || $space || $case || ($orig_desc ne $description) || !$has_quotes) &&
3313 $last_git_commit_id_linenr != $linenr - 1) {
3314 ERROR
("GIT_COMMIT_ID",
3315 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herectx);
3317 #don't report the next line if this line ends in commit and the sha1 hash is the next line
3318 $last_git_commit_id_linenr = $linenr if ($line =~ /\bcommit\s*$/i);
3321 # Check for added, moved or deleted files
3322 if (!$reported_maintainer_file && !$in_commit_log &&
3323 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
3324 $line =~ /^rename (?:from|to) [\w\/\
.\
-]+\s
*$/ ||
3325 ($line =~ /\{\s*([\w\/\
.\
-]*)\s
*\
=\
>\s
*([\w\
/\.\-]*)\s*\}/ &&
3326 (defined($1) || defined($2))))) {
3328 $reported_maintainer_file = 1;
3329 WARN
("FILE_PATH_CHANGES",
3330 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
3333 # Check for adding new DT bindings not in schema format
3334 if (!$in_commit_log &&
3335 ($line =~ /^new file mode\s*\d+\s*$/) &&
3336 ($realfile =~ m@
^Documentation
/devicetree/bindings
/.*\
.txt
$@
)) {
3337 WARN
("DT_SCHEMA_BINDING_PATCH",
3338 "DT bindings should be in DT schema format. See: Documentation/devicetree/bindings/writing-schema.rst\n");
3341 # Check for wrappage within a valid hunk of the file
3342 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
3343 ERROR
("CORRUPTED_PATCH",
3344 "patch seems to be corrupt (line wrapped?)\n" .
3345 $herecurr) if (!$emitted_corrupt++);
3348 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3349 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
3350 $rawline !~ m/^$UTF8*$/) {
3351 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3353 my $blank = copy_spacing
($rawline);
3354 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3355 my $hereptr = "$hereline$ptr\n";
3358 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
3361 # Check if it's the start of a commit log
3362 # (not a header line and we haven't seen the patch filename)
3363 if ($in_header_lines && $realfile =~ /^$/ &&
3364 !($rawline =~ /^\s+(?:\S|$)/ ||
3365 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
3366 $in_header_lines = 0;
3368 $has_commit_log = 1;
3371 # Check if there is UTF-8 in a commit log when a mail header has explicitly
3372 # declined it, i.e defined some charset where it is missing.
3373 if ($in_header_lines &&
3374 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3376 $non_utf8_charset = 1;
3379 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
3380 $rawline =~ /$NON_ASCII_UTF8/) {
3381 WARN
("UTF8_BEFORE_PATCH",
3382 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3385 # Check for absolute kernel paths in commit message
3386 if ($tree && $in_commit_log) {
3387 while ($line =~ m{(?:^|\s)(/\S*)}g) {
3390 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3391 check_absolute_file
($1, $herecurr)) {
3394 check_absolute_file
($file, $herecurr);
3399 # Check for various typo / spelling mistakes
3400 if (defined($misspellings) &&
3401 # OpenOCD specific: Begin: don't check spelling on spelling_file
3402 index($spelling_file, $realfile) + length($realfile) != length($spelling_file) &&
3403 # OpenOCD specific: End
3404 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
3405 while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
3407 my $blank = copy_spacing
($rawline);
3408 my $ptr = substr($blank, 0, $-[1]) . "^" x
length($typo);
3409 my $hereptr = "$hereline$ptr\n";
3410 my $typo_fix = $spelling_fix{lc($typo)};
3411 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3412 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
3413 my $msg_level = \
&WARN
;
3414 $msg_level = \
&CHK
if ($file);
3415 if (&{$msg_level}("TYPO_SPELLING",
3416 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
3418 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3423 # check for invalid commit id
3424 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3427 ($id, $description) = git_commit_info
($2, undef, undef);
3428 if (!defined($id)) {
3429 WARN
("UNKNOWN_COMMIT_ID",
3430 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3434 # check for repeated words separated by a single space
3435 # avoid false positive from list command eg, '-rw-r--r-- 1 root root'
3436 if (($rawline =~ /^\+/ || $in_commit_log) &&
3437 $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
3438 pos($rawline) = 1 if (!$in_commit_log);
3439 while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3443 my $start_pos = $-[1];
3444 my $end_pos = $+[2];
3445 if ($first =~ /(?:struct|union|enum)/) {
3446 pos($rawline) += length($first) + length($second) + 1;
3450 next if (lc($first) ne lc($second));
3451 next if ($first eq 'long');
3453 # check for character before and after the word matches
3454 my $start_char = '';
3456 $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ?
0 : 1));
3457 $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
3459 next if ($start_char =~ /^\S$/);
3460 next if (index(" \t.,;?!", $end_char) == -1);
3462 # avoid repeating hex occurrences like 'ff ff fe 09 ...'
3463 if ($first =~ /\b[0-9a-f]{2,}\b/i) {
3464 next if (!exists($allow_repeated_words{lc($first)}));
3467 if (WARN
("REPEATED_WORD",
3468 "Possible repeated word: '$first'\n" . $herecurr) &&
3470 $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3474 # if it's a repeated word on consecutive lines in a comment block
3475 if ($prevline =~ /$;+\s*$/ &&
3476 $prevrawline =~ /($word_pattern)\s*$/) {
3478 if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3479 if (WARN
("REPEATED_WORD",
3480 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3482 $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3488 # ignore non-hunk lines and lines being removed
3489 next if (!$hunk_line || $line =~ /^-/);
3491 #trailing whitespace
3492 if ($line =~ /^\+.*\015/) {
3493 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
3494 if (ERROR
("DOS_LINE_ENDINGS",
3495 "DOS line endings\n" . $herevet) &&
3497 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
3499 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3500 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
3501 if (ERROR
("TRAILING_WHITESPACE",
3502 "trailing whitespace\n" . $herevet) &&
3504 $fixed[$fixlinenr] =~ s/\s+$//;
3510 # Check for FSF mailing addresses.
3511 if ($rawline =~ /\bwrite to the Free/i ||
3512 $rawline =~ /\b675\s+Mass\s+Ave/i ||
3513 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3514 $rawline =~ /\b51\s+Franklin\s+St/i) {
3515 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
3516 my $msg_level = \
&ERROR
;
3517 $msg_level = \
&CHK
if ($file);
3518 &{$msg_level}("FSF_MAILING_ADDRESS",
3519 "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)
3522 # check for Kconfig help text having a real description
3523 # Only applies when adding the entry originally, after that we do not have
3524 # sufficient context to determine whether it is indeed long enough.
3525 if ($realfile =~ /Kconfig/ &&
3526 # 'choice' is usually the last thing on the line (though
3527 # Kconfig supports named choices), so use a word boundary
3528 # (\b) rather than a whitespace character (\s)
3529 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
3533 my $help_length = 0;
3534 while (defined $lines[$ln]) {
3535 my $f = $lines[$ln++];
3537 next if ($f =~ /^-/);
3538 last if ($f !~ /^[\+ ]/); # !patch context
3540 if ($f =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
3544 if ($f =~ /^\+\s*help\s*$/) {
3549 $f =~ s/^.//; # strip patch context [+ ]
3550 $f =~ s/#.*//; # strip # directives
3551 $f =~ s/^\s+//; # strip leading blanks
3552 next if ($f =~ /^$/); # skip blank lines
3554 # At the end of this Kconfig block:
3555 # This only checks context lines in the patch
3556 # and so hopefully shouldn't trigger false
3557 # positives, even though some of these are
3558 # common words in help texts
3559 if ($f =~ /^(?
:config
|menuconfig
|choice
|endchoice
|
3560 if|endif
|menu
|endmenu
|source
)\b/x
) {
3563 $help_length++ if ($has_help);
3566 $help_length < $min_conf_desc_length) {
3567 my $stat_real = get_stat_real
($linenr, $ln - 1);
3568 WARN
("CONFIG_DESCRIPTION",
3569 "please write a help paragraph that fully describes the config symbol\n" . "$here\n$stat_real\n");
3573 # check MAINTAINERS entries
3574 if ($realfile =~ /^MAINTAINERS$/) {
3575 # check MAINTAINERS entries for the right form
3576 if ($rawline =~ /^\+[A-Z]:/ &&
3577 $rawline !~ /^\+[A-Z]:\t\S/) {
3578 if (WARN
("MAINTAINERS_STYLE",
3579 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3581 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3584 # check MAINTAINERS entries for the right ordering too
3585 my $preferred_order = 'MRLSWQBCPTFXNK';
3586 if ($rawline =~ /^\+[A-Z]:/ &&
3587 $prevrawline =~ /^[\+ ][A-Z]:/) {
3588 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3591 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3594 my $curindex = index($preferred_order, $cur);
3595 my $previndex = index($preferred_order, $prev);
3596 if ($curindex < 0) {
3597 WARN
("MAINTAINERS_STYLE",
3598 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3600 if ($previndex >= 0 && $curindex < $previndex) {
3601 WARN
("MAINTAINERS_STYLE",
3602 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3603 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3604 ($prev eq 'X' && $cur eq 'X')) &&
3605 ($prevval cmp $curval) > 0) {
3606 WARN
("MAINTAINERS_STYLE",
3607 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3613 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3614 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3617 'EXTRA_AFLAGS' => 'asflags-y',
3618 'EXTRA_CFLAGS' => 'ccflags-y',
3619 'EXTRA_CPPFLAGS' => 'cppflags-y',
3620 'EXTRA_LDFLAGS' => 'ldflags-y',
3623 WARN
("DEPRECATED_VARIABLE",
3624 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3627 # check for DT compatible documentation
3628 if (defined $root &&
3629 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3630 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3632 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3634 my $dt_path = $root . "/Documentation/devicetree/bindings/";
3635 my $vp_file = $dt_path . "vendor-prefixes.yaml";
3637 foreach my $compat (@compats) {
3638 my $compat2 = $compat;
3639 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3640 my $compat3 = $compat;
3641 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3642 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3644 WARN
("UNDOCUMENTED_DT_STRING",
3645 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3648 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3650 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
3652 WARN
("UNDOCUMENTED_DT_STRING",
3653 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3658 # check for using SPDX license tag at beginning of files
3659 if ($realline == $checklicenseline) {
3660 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3661 $checklicenseline = 2;
3662 } elsif ($rawline =~ /^\+/) {
3664 if ($realfile =~ /\.(h|s|S)$/) {
3666 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3668 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
3670 } elsif ($realfile =~ /\.rst$/) {
3674 # check SPDX comment style for .[chsS] files
3675 if ($realfile =~ /\.[chsS]$/ &&
3676 $rawline =~ /SPDX-License-Identifier:/ &&
3677 $rawline !~ m@
^\
+\s
*\Q
$comment\E\s
*@
) {
3678 WARN
("SPDX_LICENSE_TAG",
3679 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3682 if ($comment !~ /^$/ &&
3683 $rawline !~ m@
^\
+\Q
$comment\E SPDX
-License
-Identifier
: @
) {
3684 WARN
("SPDX_LICENSE_TAG",
3685 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
3686 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
3687 my $spdx_license = $1;
3688 if (!is_SPDX_License_valid
($spdx_license)) {
3689 WARN
("SPDX_LICENSE_TAG",
3690 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3692 if ($realfile =~ m@
^Documentation
/devicetree/bindings
/@
&&
3693 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3694 my $msg_level = \
&WARN
;
3695 $msg_level = \
&CHK
if ($file);
3696 if (&{$msg_level}("SPDX_LICENSE_TAG",
3698 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3700 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3707 # check for embedded filenames
3708 if ($rawline =~ /^\+.*\Q$realfile\E/) {
3709 WARN
("EMBEDDED_FILENAME",
3710 "It's generally not useful to have the filename in the file\n" . $herecurr);
3713 # check we are in a valid source file if not then ignore this hunk
3714 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
3716 # check for using SPDX-License-Identifier on the wrong line number
3717 if ($realline != $checklicenseline &&
3718 $rawline =~ /\bSPDX-License-Identifier:/ &&
3719 substr($line, @
-, @
+ - @
-) eq "$;" x
(@
+ - @
-)) {
3720 WARN
("SPDX_LICENSE_TAG",
3721 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3724 # line length limit (with some exclusions)
3726 # There are a few types of lines that may extend beyond $max_line_length:
3727 # logging functions like pr_info that end in a string
3728 # lines with a single string
3729 # #defines that are a single string
3730 # lines with an RFC3986 like URL
3732 # There are 3 different line length message types:
3733 # LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
3734 # LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3735 # LONG_LINE all other lines longer than $max_line_length
3737 # if LONG_LINE is ignored, the other 2 types are also ignored
3740 if ($line =~ /^\+/ && $length > $max_line_length) {
3741 my $msg_type = "LONG_LINE";
3743 # Check the allowed long line types first
3745 # logging functions that end in a string that starts
3746 # before $max_line_length
3747 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3748 length(expand_tabs
(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3751 # lines with only strings (w/ possible termination)
3752 # #defines with only strings
3753 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3754 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3757 # More special cases
3758 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3759 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3762 # URL ($rawline is used in case the URL is in a comment)
3763 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\
/\S+/i) {
3766 # Otherwise set the alternate message types
3768 # a comment starts before $max_line_length
3769 } elsif ($line =~ /($;[\s$;]*)$/ &&
3770 length(expand_tabs
(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3771 $msg_type = "LONG_LINE_COMMENT"
3773 # a quoted string starts before $max_line_length
3774 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3775 length(expand_tabs
(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3776 $msg_type = "LONG_LINE_STRING"
3779 if ($msg_type ne "" &&
3780 (show_type
("LONG_LINE") || show_type
($msg_type))) {
3781 my $msg_level = \
&WARN
;
3782 $msg_level = \
&CHK
if ($file);
3783 &{$msg_level}($msg_type,
3784 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
3788 # check for adding lines without a newline.
3789 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3790 if (WARN
("MISSING_EOF_NEWLINE",
3791 "adding a line without newline at end of file\n" . $herecurr) &&
3793 fix_delete_line
($fixlinenr+1, "No newline at end of file");
3797 # check for .L prefix local symbols in .S files
3798 if ($realfile =~ /\.S$/ &&
3799 $line =~ /^\+\s*(?:[A-Z]+_)?SYM_[A-Z]+_(?:START|END)(?:_[A-Z_]+)?\s*\(\s*\.L/) {
3800 WARN
("AVOID_L_PREFIX",
3801 "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);
3804 # check we are in a valid source file C or perl if not then ignore this hunk
3805 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
3807 # at the beginning of a line any tabs must come first and anything
3808 # more than $tabsize must use tabs.
3809 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3810 $rawline =~ /^\+\s* \s*/) {
3811 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
3813 if (ERROR
("CODE_INDENT",
3814 "code indent should use tabs where possible\n" . $herevet) &&
3816 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
3820 # check for space before tabs.
3821 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3822 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
3823 if (WARN
("SPACE_BEFORE_TAB",
3824 "please, no space before tabs\n" . $herevet) &&
3826 while ($fixed[$fixlinenr] =~
3827 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
3828 while ($fixed[$fixlinenr] =~
3829 s/(^\+.*) +\t/$1\t/) {}
3833 # check for assignments on the start of a line
3834 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3836 if (CHK
("ASSIGNMENT_CONTINUATIONS",
3837 "Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3838 $fix && $prevrawline =~ /^\+/) {
3839 # add assignment operator to the previous line, remove from current line
3840 $fixed[$fixlinenr - 1] .= " $operator";
3841 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3845 # check for && or || at the start of a line
3846 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3848 if (CHK
("LOGICAL_CONTINUATIONS",
3849 "Logical continuations should be on the previous line\n" . $hereprev) &&
3850 $fix && $prevrawline =~ /^\+/) {
3851 # insert logical operator at last non-comment, non-whitepsace char on previous line
3852 $prevline =~ /[\s$;]*$/;
3853 my $line_end = substr($prevrawline, $-[0]);
3854 $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
3855 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3859 # check indentation starts on a tab stop
3860 if ($perl_version_ok &&
3861 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3862 my $indent = length($1);
3863 if ($indent % $tabsize) {
3865 "Statements should start on a tabstop\n" . $herecurr) &&
3867 $fixed[$fixlinenr] =~ s@
(^\
+\t+) +@
$1 . "\t" x
($indent/$tabsize)@e;
3872 # check multi-line statement indentation matches previous line
3873 if ($perl_version_ok &&
3874 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3875 $prevline =~ /^\+(\t*)(.*)$/;
3879 my $pos = pos_last_openparen
($rest);
3881 $line =~ /^(\+| )([ \t]*)/;
3884 my $goodtabindent = $oldindent .
3885 "\t" x
($pos / $tabsize) .
3886 " " x
($pos % $tabsize);
3887 my $goodspaceindent = $oldindent . " " x
$pos;
3889 if ($newindent ne $goodtabindent &&
3890 $newindent ne $goodspaceindent) {
3892 if (CHK
("PARENTHESIS_ALIGNMENT",
3893 "Alignment should match open parenthesis\n" . $hereprev) &&
3894 $fix && $line =~ /^\+/) {
3895 $fixed[$fixlinenr] =~
3896 s/^\+[ \t]*/\+$goodtabindent/;
3902 # check for space after cast like "(int) foo" or "(struct foo) bar"
3903 # avoid checking a few false positives:
3904 # "sizeof(<type>)" or "__alignof__(<type>)"
3905 # function pointer declarations like "(*foo)(int) = bar;"
3906 # structure definitions like "(struct foo) { 0 };"
3907 # multiline macros that define functions
3908 # known attributes or the __attribute__ keyword
3909 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3910 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
3912 "No space is necessary after a cast\n" . $herecurr) &&
3914 $fixed[$fixlinenr] =~
3915 s/(\(\s*$Type\s*\))[ \t]+/$1/;
3919 # Block comment styles
3920 # Networking with an initial /*
3921 if ($realfile =~ m@
^(drivers
/net
/|net
/)@
&&
3922 $prevrawline =~ /^\+[ \t]*\/\
*[ \t]*$/ &&
3923 $rawline =~ /^\+[ \t]*\*/ &&
3924 $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
3925 WARN
("NETWORKING_BLOCK_COMMENT_STYLE",
3926 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3929 # Block comments use * on subsequent lines
3930 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3931 $prevrawline =~ /^\+.*?\/\
*/ && #starting /*
3932 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
3933 $rawline =~ /^\+/ && #line is new
3934 $rawline !~ /^\+[ \t]*\*/) { #no leading *
3935 WARN
("BLOCK_COMMENT_STYLE",
3936 "Block comments use * on subsequent lines\n" . $hereprev);
3939 # Block comments use */ on trailing lines
3940 if ($rawline !~ m@
^\
+[ \t]*\
*/[ \t]*$@ && #trailing */
3941 $rawline !~ m@
^\
+.*/\*.*\*/[ \t]*$@
&& #inline /*...*/
3942 $rawline !~ m@
^\
+.*\
*{2,}/[ \t]*$@ && #trailing **/
3943 $rawline =~ m@
^\
+[ \t]*.+\
*\
/[ \t]*$@) { #non blank */
3944 WARN
("BLOCK_COMMENT_STYLE",
3945 "Block comments use a trailing */ on a separate line\n" . $herecurr);
3948 # Block comment * alignment
3949 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3950 $line =~ /^\+[ \t]*$;/ && #leading comment
3951 $rawline =~ /^\+[ \t]*\*/ && #leading *
3952 (($prevrawline =~ /^\+.*?\/\
*/ && #leading /*
3953 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
3954 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3956 $prevrawline =~ m@
^\
+([ \t]*/?
)\
*@
;
3958 $oldindent = expand_tabs
($1);
3960 $prevrawline =~ m@
^\
+(.*/?
)\
*@
;
3961 $oldindent = expand_tabs
($1);
3963 $rawline =~ m@
^\
+([ \t]*)\
*@
;
3965 $newindent = expand_tabs
($newindent);
3966 if (length($oldindent) ne length($newindent)) {
3967 WARN
("BLOCK_COMMENT_STYLE",
3968 "Block comments should align the * on each line\n" . $hereprev);
3972 # check for missing blank lines after struct/union declarations
3973 # with exceptions for various attributes and macros
3974 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3976 !($line =~ /^\+\s*$/ ||
3977 $line =~ /^\+\s*(?:EXPORT_SYMBOL|early_param)/ ||
3978 $line =~ /^\+\s*MODULE_/i ||
3979 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3980 $line =~ /^\+[a-z_]*init/ ||
3981 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3982 $line =~ /^\+\s*DECLARE/ ||
3983 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
3984 $line =~ /^\+\s*__setup/)) {
3985 if (CHK
("LINE_SPACING",
3986 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3988 fix_insert_line
($fixlinenr, "\+");
3992 # check for multiple consecutive blank lines
3993 if ($prevline =~ /^[\+ ]\s*$/ &&
3994 $line =~ /^\+\s*$/ &&
3995 $last_blank_line != ($linenr - 1)) {
3996 if (CHK
("LINE_SPACING",
3997 "Please don't use multiple blank lines\n" . $hereprev) &&
3999 fix_delete_line
($fixlinenr, $rawline);
4002 $last_blank_line = $linenr;
4005 # check for missing blank lines after declarations
4006 # (declarations must have the same indentation and not be at the start of line)
4007 if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) {
4011 # remove $Attribute/$Sparse uses to simplify comparisons
4012 $sl =~ s/\b(?:$Attribute|$Sparse)\b//g;
4013 $pl =~ s/\b(?:$Attribute|$Sparse)\b//g;
4014 if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
4015 # function pointer declarations
4016 $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
4017 # foo bar; where foo is some local typedef or #define
4018 $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
4019 # known declaration macros
4020 $pl =~ /^\+\s+$declaration_macros/) &&
4021 # for "else if" which can look like "$Ident $Ident"
4022 !($pl =~ /^\+\s+$c90_Keywords\b/ ||
4023 # other possible extensions of declaration lines
4024 $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
4025 # not starting a section or a macro "\" extended line
4026 $pl =~ /(?:\{\s*|\\)$/) &&
4027 # looks like a declaration
4028 !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
4029 # function pointer declarations
4030 $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
4031 # foo bar; where foo is some local typedef or #define
4032 $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
4033 # known declaration macros
4034 $sl =~ /^\+\s+$declaration_macros/ ||
4035 # start of struct or union or enum
4036 $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
4037 # start or end of block or continuation of declaration
4038 $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
4039 # bitfield continuation
4040 $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
4041 # other possible extensions of declaration lines
4042 $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) {
4043 if (WARN
("LINE_SPACING",
4044 "Missing a blank line after declarations\n" . $hereprev) &&
4046 fix_insert_line
($fixlinenr, "\+");
4051 # check for spaces at the beginning of a line.
4053 # 1) within comments
4054 # 2) indented preprocessor commands
4056 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
4057 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
4058 if (WARN
("LEADING_SPACE",
4059 "please, no spaces at the start of a line\n" . $herevet) &&
4061 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
4065 # check we are in a valid C source file if not then ignore this hunk
4066 next if ($realfile !~ /\.(h|c)$/);
4068 # check for unusual line ending [ or (
4069 if ($line =~ /^\+.*([\[\(])\s*$/) {
4070 CHK
("OPEN_ENDED_LINE",
4071 "Lines should not end with a '$1'\n" . $herecurr);
4074 # check if this appears to be the start function declaration, save the name
4075 if ($sline =~ /^\+\{\s*$/ &&
4076 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
4077 $context_function = $1;
4080 # check if this appears to be the end of function declaration
4081 if ($sline =~ /^\+\}\s*$/) {
4082 undef $context_function;
4085 # check indentation of any line with a bare else
4086 # (but not if it is a multiple line "if (foo) return bar; else return baz;")
4087 # if the previous line is a break or return and is indented 1 tab more...
4088 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
4089 my $tabs = length($1) + 1;
4090 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
4091 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
4092 defined $lines[$linenr] &&
4093 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
4094 WARN
("UNNECESSARY_ELSE",
4095 "else is not generally useful after a break or return\n" . $hereprev);
4099 # check indentation of a line with a break;
4100 # if the previous line is a goto, return or break
4101 # and is indented the same # of tabs
4102 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
4104 if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
4105 if (WARN
("UNNECESSARY_BREAK",
4106 "break is not useful after a $1\n" . $hereprev) &&
4108 fix_delete_line
($fixlinenr, $rawline);
4113 # check for RCS/CVS revision markers
4114 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
4116 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
4119 # check for old HOTPLUG __dev<foo> section markings
4120 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
4121 WARN
("HOTPLUG_SECTION",
4122 "Using $1 is unnecessary\n" . $herecurr);
4125 # Check for potential 'bare' types
4126 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
4128 #print "LINE<$line>\n";
4129 if ($linenr > $suppress_statement &&
4130 $realcnt && $sline =~ /.\s*\S/) {
4131 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4132 ctx_statement_block
($linenr, $realcnt, 0);
4133 $stat =~ s/\n./\n /g;
4134 $cond =~ s/\n./\n /g;
4136 #print "linenr<$linenr> <$stat>\n";
4137 # If this statement has no statement boundaries within
4138 # it there is no point in retrying a statement scan
4139 # until we hit end of it.
4140 my $frag = $stat; $frag =~ s/;+\s*$//;
4141 if ($frag !~ /(?:{|;)/) {
4142 #print "skip<$line_nr_next>\n";
4143 $suppress_statement = $line_nr_next;
4146 # Find the real next line.
4147 $realline_next = $line_nr_next;
4148 if (defined $realline_next &&
4149 (!defined $lines[$realline_next - 1] ||
4150 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
4157 # Ignore goto labels.
4158 if ($s =~ /$Ident:\*$/s) {
4160 # Ignore functions being called
4161 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
4163 } elsif ($s =~ /^.\s*else\b/s) {
4165 # declarations always start with types
4166 } 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) {
4169 possible
($type, "A:" . $s);
4171 # definitions in global scope can only start with types
4172 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
4173 possible
($1, "B:" . $s);
4176 # any (foo ... *) is a pointer cast, and foo is a type
4177 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
4178 possible
($1, "C:" . $s);
4181 # Check for any sort of function declaration.
4182 # int foo(something bar, other baz);
4183 # void (*store_gdt)(x86_descr_ptr *);
4184 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
4185 my ($name_len) = length($1);
4188 substr($ctx, 0, $name_len + 1, '');
4189 $ctx =~ s/\)[^\)]*$//;
4191 for my $arg (split(/\s*,\s*/, $ctx)) {
4192 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
4194 possible
($1, "D:" . $s);
4202 # Checks which may be anchored in the context.
4205 # Check for switch () and associated case and default
4206 # statements should be at the same indent.
4207 if ($line=~/\bswitch\s*\(.*\)/) {
4210 my @ctx = ctx_block_outer
($linenr, $realcnt);
4212 for my $ctx (@ctx) {
4213 my ($clen, $cindent) = line_stats
($ctx);
4214 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
4215 $indent != $cindent) {
4216 $err .= "$sep$ctx\n";
4223 ERROR
("SWITCH_CASE_INDENT_LEVEL",
4224 "switch and case should be at the same indent\n$hereline$err");
4228 # if/while/etc brace do not go on next line, unless defining a do while loop,
4229 # or if that brace on the next line is for something else
4230 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
4231 my $pre_ctx = "$1$2";
4233 my ($level, @ctx) = ctx_statement_level
($linenr, $realcnt, 0);
4235 if ($line =~ /^\+\t{6,}/) {
4236 WARN
("DEEP_INDENTATION",
4237 "Too many leading tabs - consider code refactoring\n" . $herecurr);
4240 my $ctx_cnt = $realcnt - $#ctx - 1;
4241 my $ctx = join("\n", @ctx);
4243 my $ctx_ln = $linenr;
4244 my $ctx_skip = $realcnt;
4246 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
4247 defined $lines[$ctx_ln - 1] &&
4248 $lines[$ctx_ln - 1] =~ /^-/)) {
4249 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
4250 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
4254 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
4255 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
4257 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
4259 "that open brace { should be on the previous line\n" .
4260 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
4262 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
4263 $ctx =~ /\)\s*\;\s*$/ &&
4264 defined $lines[$ctx_ln - 1])
4266 my ($nlength, $nindent) = line_stats
($lines[$ctx_ln - 1]);
4267 if ($nindent > $indent) {
4268 WARN
("TRAILING_SEMICOLON",
4269 "trailing semicolon indicates no statements, indent implies otherwise\n" .
4270 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
4275 # Check relative indent for conditionals and blocks.
4276 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
4277 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4278 ctx_statement_block
($linenr, $realcnt, 0)
4279 if (!defined $stat);
4280 my ($s, $c) = ($stat, $cond);
4282 substr($s, 0, length($c), '');
4284 # remove inline comments
4288 # Find out how long the conditional actually is.
4289 my @newlines = ($c =~ /\n/gs);
4290 my $cond_lines = 1 + $#newlines;
4292 # Make sure we remove the line prefixes as we have
4293 # none on the first line, and are going to readd them
4296 while ($s =~ /\n\s+\\\n/) {
4297 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
4300 # We want to check the first line inside the block
4301 # starting at the end of the conditional, so remove:
4302 # 1) any blank line termination
4303 # 2) any opening brace { on end of the line
4305 my $continuation = 0;
4307 $s =~ s/^.*\bdo\b//;
4309 if ($s =~ s/^\s*\\//) {
4312 if ($s =~ s/^\s*?\n//) {
4317 # Also ignore a loop construct at the end of a
4318 # preprocessor statement.
4319 if (($prevline =~ /^.\s*#\s*define\s/ ||
4320 $prevline =~ /\\\s*$/) && $continuation == 0) {
4326 while ($cond_ptr != $cond_lines) {
4327 $cond_ptr = $cond_lines;
4329 # If we see an #else/#elif then the code
4331 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4336 # 1) blank lines, they should be at 0,
4337 # 2) preprocessor lines, and
4339 if ($continuation ||
4341 $s =~ /^\s*#\s*?/ ||
4342 $s =~ /^\s*$Ident\s*:/) {
4343 $continuation = ($s =~ /^.*?\\\n/) ?
1 : 0;
4344 if ($s =~ s/^.*?\n//) {
4350 my (undef, $sindent) = line_stats
("+" . $s);
4351 my $stat_real = raw_line
($linenr, $cond_lines);
4353 # Check if either of these lines are modified, else
4354 # this is not this patch's fault.
4355 if (!defined($stat_real) ||
4356 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
4359 if (defined($stat_real) && $cond_lines > 1) {
4360 $stat_real = "[...]\n$stat_real";
4363 #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";
4365 if ($check && $s ne '' &&
4366 (($sindent % $tabsize) != 0 ||
4367 ($sindent < $indent) ||
4368 ($sindent == $indent &&
4369 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
4370 ($sindent > $indent + $tabsize))) {
4371 WARN
("SUSPECT_CODE_INDENT",
4372 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
4376 # Track the 'values' across context and added lines.
4377 my $opline = $line; $opline =~ s/^./ /;
4378 my ($curr_values, $curr_vars) =
4379 annotate_values
($opline . "\n", $prev_values);
4380 $curr_values = $prev_values . $curr_values;
4382 my $outline = $opline; $outline =~ s/\t/ /g;
4383 print "$linenr > .$outline\n";
4384 print "$linenr > $curr_values\n";
4385 print "$linenr > $curr_vars\n";
4387 $prev_values = substr($curr_values, -1);
4389 #ignore lines not being added
4390 next if ($line =~ /^[^\+]/);
4392 # check for self assignments used to avoid compiler warnings
4393 # e.g.: int foo = foo, *bar = NULL;
4394 # struct foo bar = *(&(bar));
4395 if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
4397 if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
4398 WARN
("SELF_ASSIGNMENT",
4399 "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
4403 # check for dereferences that span multiple lines
4404 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
4405 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
4406 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
4408 $line =~ /^.\s*($Lval)/;
4411 WARN
("MULTILINE_DEREFERENCE",
4412 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
4415 # check for declarations of signed or unsigned without int
4416 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
4419 $var = "" if (!defined $var);
4420 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
4424 $pointer = "" if (!defined $pointer);
4426 if (WARN
("UNSPECIFIED_INT",
4427 "Prefer '" . trim
($sign) . " int" . rtrim
($pointer) . "' to bare use of '$sign" . rtrim
($pointer) . "'\n" . $herecurr) &&
4429 my $decl = trim
($sign) . " int ";
4430 my $comp_pointer = $pointer;
4431 $comp_pointer =~ s/\s//g;
4432 $decl .= $comp_pointer;
4433 $decl = rtrim
($decl) if ($var eq "");
4434 $fixed[$fixlinenr] =~ s@
\b$sign\s
*\Q
$pointer\E\s
*$var\b@
$decl$var@
;
4439 # TEST: allow direct testing of the type matcher.
4441 if ($line =~ /^.\s*$Declare\s*$/) {
4443 "TEST: is type\n" . $herecurr);
4444 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
4445 ERROR
("TEST_NOT_TYPE",
4446 "TEST: is not type ($1 is)\n". $herecurr);
4450 # TEST: allow direct testing of the attribute matcher.
4452 if ($line =~ /^.\s*$Modifier\s*$/) {
4454 "TEST: is attr\n" . $herecurr);
4455 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
4456 ERROR
("TEST_NOT_ATTR",
4457 "TEST: is not attr ($1 is)\n". $herecurr);
4462 # check for initialisation to aggregates open brace on the next line
4463 if ($line =~ /^.\s*{/ &&
4464 $prevline =~ /(?:^|[^=])=\s*$/) {
4465 if (ERROR
("OPEN_BRACE",
4466 "that open brace { should be on the previous line\n" . $hereprev) &&
4467 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4468 fix_delete_line
($fixlinenr - 1, $prevrawline);
4469 fix_delete_line
($fixlinenr, $rawline);
4470 my $fixedline = $prevrawline;
4471 $fixedline =~ s/\s*=\s*$/ = {/;
4472 fix_insert_line
($fixlinenr, $fixedline);
4474 $fixedline =~ s/^(.\s*)\{\s*/$1/;
4475 fix_insert_line
($fixlinenr, $fixedline);
4480 # Checks which are anchored on the added line.
4483 # check for malformed paths in #include statements (uses RAW line)
4484 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
4486 if ($path =~ m{//}) {
4487 ERROR
("MALFORMED_INCLUDE",
4488 "malformed #include filename\n" . $herecurr);
4490 if ($path =~ "^uapi/" && $realfile =~ m@
\binclude
/uapi
/@
) {
4491 ERROR
("UAPI_INCLUDE",
4492 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
4496 # no C99 // comments
4497 if ($line =~ m{//}) {
4498 if (ERROR
("C99_COMMENTS",
4499 "do not use C99 // comments\n" . $herecurr) &&
4501 my $line = $fixed[$fixlinenr];
4502 if ($line =~ /\/\
/(.*)$/) {
4503 my $comment = trim
($1);
4504 $fixed[$fixlinenr] =~ s@\
/\/(.*)$@
/\* $comment \*/@
;
4508 # Remove C99 comments.
4510 $opline =~ s@
//.*@@
;
4512 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4513 # the whole statement.
4514 #print "APW <$lines[$realline_next - 1]>\n";
4515 if (defined $realline_next &&
4516 exists $lines[$realline_next - 1] &&
4517 !defined $suppress_export{$realline_next} &&
4518 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
4519 # Handle definitions which produce identifiers with
4522 # EXPORT_SYMBOL(something_foo);
4524 $name =~ s/^\s*($Ident).*/$1/;
4525 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
4526 $name =~ /^${Ident}_$2/) {
4527 #print "FOO C name<$name>\n";
4528 $suppress_export{$realline_next} = 1;
4530 } elsif ($stat !~ /(?
:
4532 ^.DEFINE_
$Ident\
(\Q
$name\E\
)|
4533 ^.DECLARE_
$Ident\
(\Q
$name\E\
)|
4534 ^.LIST_HEAD\
(\Q
$name\E\
)|
4535 ^.(?
:$Storage\s
+)?
$Type\s
*\
(\s
*\
*\s
*\Q
$name\E\s
*\
)\s
*\
(|
4536 \b\Q
$name\E
(?
:\s
+$Attribute)*\s
*(?
:;|=|\
[|\
()
4538 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4539 $suppress_export{$realline_next} = 2;
4541 $suppress_export{$realline_next} = 1;
4544 if (!defined $suppress_export{$linenr} &&
4545 $prevline =~ /^.\s*$/ &&
4546 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
4547 #print "FOO B <$lines[$linenr - 1]>\n";
4548 $suppress_export{$linenr} = 2;
4550 if (defined $suppress_export{$linenr} &&
4551 $suppress_export{$linenr} == 2) {
4552 WARN
("EXPORT_SYMBOL",
4553 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
4556 # check for global initialisers.
4557 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ &&
4558 !exclude_global_initialisers
($realfile)) {
4559 if (ERROR
("GLOBAL_INITIALISERS",
4560 "do not initialise globals to $1\n" . $herecurr) &&
4562 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
4565 # check for static initialisers.
4566 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
4567 if (ERROR
("INITIALISED_STATIC",
4568 "do not initialise statics to $1\n" .
4571 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
4575 # check for misordered declarations of char/short/int/long with signed/unsigned
4576 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4578 WARN
("MISORDERED_TYPE",
4579 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4582 # check for unnecessary <signed> int declarations of short/long/long long
4583 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4584 my $type = trim
($1);
4585 next if ($type !~ /\bint\b/);
4586 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4587 my $new_type = $type;
4588 $new_type =~ s/\b\s*int\s*\b/ /;
4589 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4590 $new_type =~ s/^const\s+//;
4591 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4592 $new_type = "const $new_type" if ($type =~ /^const\b/);
4593 $new_type =~ s/\s+/ /g;
4594 $new_type = trim
($new_type);
4595 if (WARN
("UNNECESSARY_INT",
4596 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4598 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4602 # check for static const char * arrays.
4603 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4604 WARN
("STATIC_CONST_CHAR_ARRAY",
4605 "static const char * array should probably be static const char * const\n" .
4609 # check for initialized const char arrays that should be static const
4610 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4611 if (WARN
("STATIC_CONST_CHAR_ARRAY",
4612 "const array should probably be static const\n" . $herecurr) &&
4614 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4618 # check for static char foo[] = "bar" declarations.
4619 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4620 WARN
("STATIC_CONST_CHAR_ARRAY",
4621 "static char array declaration should probably be static const char\n" .
4625 # check for const <foo> const where <foo> is not a pointer or array type
4626 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4628 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4630 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4631 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4633 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4637 # check for const static or static <non ptr type> const declarations
4638 # prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
4639 if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
4640 $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
4641 if (WARN
("STATIC_CONST",
4642 "Move const after static - use 'static const $1'\n" . $herecurr) &&
4644 $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
4645 $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
4649 # check for non-global char *foo[] = {"bar", ...} declarations.
4650 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4651 WARN
("STATIC_CONST_CHAR_ARRAY",
4652 "char * array declaration might be better as static const\n" .
4656 # check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4657 if ($line =~ m@
\bsizeof\s
*\
(\s
*($Lval)\s
*\
)@
) {
4659 if ($line =~ m@
\b(sizeof\s
*\
(\s
*\Q
$array\E\s
*\
)\s
*/\s
*sizeof\s
*\
(\s
*\Q
$array\E\s
*\
[\s
*0\s
*\
]\s
*\
))@
) {
4661 if (WARN
("ARRAY_SIZE",
4662 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4664 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4669 # check for function declarations without arguments like "int foo()"
4670 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
4671 if (ERROR
("FUNCTION_WITHOUT_ARGS",
4672 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4674 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4678 # check for new typedefs, only function parameters and sparse annotations
4680 if ($line =~ /\btypedef\s/ &&
4681 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4682 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
4683 $line !~ /\b$typeTypedefs\b/ &&
4684 $line !~ /\b__bitwise\b/) {
4685 WARN
("NEW_TYPEDEFS",
4686 "do not add new typedefs\n" . $herecurr);
4689 # * goes on variable not on type
4691 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4693 my ($ident, $from, $to) = ($1, $2, $2);
4695 # Should start with a space.
4696 $to =~ s/^(\S)/ $1/;
4697 # Should not end with a space.
4699 # '*'s should not have spaces between.
4700 while ($to =~ s/\*\s+\*/\*\*/) {
4703 ## print "1: from<$from> to<$to> ident<$ident>\n";
4705 if (ERROR
("POINTER_LOCATION",
4706 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4708 my $sub_from = $ident;
4709 my $sub_to = $ident;
4710 $sub_to =~ s/\Q$from\E/$to/;
4711 $fixed[$fixlinenr] =~
4712 s@\Q
$sub_from\E@
$sub_to@
;
4716 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4718 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4720 # Should start with a space.
4721 $to =~ s/^(\S)/ $1/;
4722 # Should not end with a space.
4724 # '*'s should not have spaces between.
4725 while ($to =~ s/\*\s+\*/\*\*/) {
4727 # Modifiers should have spaces.
4728 $to =~ s/(\b$Modifier$)/$1 /;
4730 ## print "2: from<$from> to<$to> ident<$ident>\n";
4731 if ($from ne $to && $ident !~ /^$Modifier$/) {
4732 if (ERROR
("POINTER_LOCATION",
4733 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4736 my $sub_from = $match;
4737 my $sub_to = $match;
4738 $sub_to =~ s/\Q$from\E/$to/;
4739 $fixed[$fixlinenr] =~
4740 s@\Q
$sub_from\E@
$sub_to@
;
4745 # avoid BUG() or BUG_ON()
4746 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
4747 my $msg_level = \
&WARN
;
4748 $msg_level = \
&CHK
if ($file);
4749 &{$msg_level}("AVOID_BUG",
4750 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
4753 # avoid LINUX_VERSION_CODE
4754 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4755 WARN
("LINUX_VERSION_CODE",
4756 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
4759 # check for uses of printk_ratelimit
4760 if ($line =~ /\bprintk_ratelimit\s*\(/) {
4761 WARN
("PRINTK_RATELIMITED",
4762 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
4765 # printk should use KERN_* levels
4766 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4767 WARN
("PRINTK_WITHOUT_KERN_LEVEL",
4768 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
4771 # prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL>
4772 if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) {
4776 $modifier = "" if (!defined($modifier));
4777 my $level = lc($orig);
4778 $level = "warn" if ($level eq "warning");
4779 my $level2 = $level;
4780 $level2 = "dbg" if ($level eq "debug");
4781 $level .= $modifier;
4782 $level2 .= $modifier;
4783 WARN
("PREFER_PR_LEVEL",
4784 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to $printk(KERN_$orig ...\n" . $herecurr);
4787 # prefer dev_<level> to dev_printk(KERN_<LEVEL>
4788 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4790 my $level = lc($orig);
4791 $level = "warn" if ($level eq "warning");
4792 $level = "dbg" if ($level eq "debug");
4793 WARN
("PREFER_DEV_LEVEL",
4794 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4797 # trace_printk should not be used in production code.
4798 if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
4799 WARN
("TRACE_PRINTK",
4800 "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
4803 # ENOSYS means "bad syscall nr" and nothing else. This will have a small
4804 # number of false positives, but assembly files are not checked, so at
4805 # least the arch entry code will not trigger this warning.
4806 if ($line =~ /\bENOSYS\b/) {
4808 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4811 # ENOTSUPP is not a standard error code and should be avoided in new patches.
4812 # Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4813 # Similarly to ENOSYS warning a small number of false positives is expected.
4814 if (!$file && $line =~ /\bENOTSUPP\b/) {
4815 if (WARN
("ENOTSUPP",
4816 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4818 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4822 # function brace can't be on same line, except for #defines of do while,
4823 # or if closed on same line
4824 if ($perl_version_ok &&
4825 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4826 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4828 if (ERROR
("OPEN_BRACE",
4829 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
4831 fix_delete_line
($fixlinenr, $rawline);
4832 my $fixed_line = $rawline;
4833 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
4836 fix_insert_line
($fixlinenr, ltrim
($line1));
4837 fix_insert_line
($fixlinenr, "\+{");
4838 if ($line2 !~ /^\s*$/) {
4839 fix_insert_line
($fixlinenr, "\+\t" . trim
($line2));
4844 # open braces for enum, union and struct go on the same line.
4845 if ($line =~ /^.\s*{/ &&
4846 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
4847 if (ERROR
("OPEN_BRACE",
4848 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4849 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4850 fix_delete_line
($fixlinenr - 1, $prevrawline);
4851 fix_delete_line
($fixlinenr, $rawline);
4852 my $fixedline = rtrim
($prevrawline) . " {";
4853 fix_insert_line
($fixlinenr, $fixedline);
4854 $fixedline = $rawline;
4855 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
4856 if ($fixedline !~ /^\+\s*$/) {
4857 fix_insert_line
($fixlinenr, $fixedline);
4862 # missing space after union, struct or enum definition
4863 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4865 "missing space after $1 definition\n" . $herecurr) &&
4867 $fixed[$fixlinenr] =~
4868 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4872 # Function pointer declarations
4873 # check spacing between type, funcptr, and args
4874 # canonical declaration is "type (*funcptr)(args...)"
4875 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
4877 my $pre_pointer_space = $2;
4878 my $post_pointer_space = $3;
4880 my $post_funcname_space = $5;
4881 my $pre_args_space = $6;
4883 # the $Declare variable will capture all spaces after the type
4884 # so check it for a missing trailing missing space but pointer return types
4885 # don't need a space so don't warn for those.
4886 my $post_declare_space = "";
4887 if ($declare =~ /(\s+)$/) {
4888 $post_declare_space = $1;
4889 $declare = rtrim
($declare);
4891 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
4893 "missing space after return type\n" . $herecurr);
4894 $post_declare_space = " ";
4897 # unnecessary space "type (*funcptr)(args...)"
4898 # This test is not currently implemented because these declarations are
4900 # int foo(int bar, ...)
4901 # and this is form shouldn't/doesn't generate a checkpatch warning.
4903 # elsif ($declare =~ /\s{2,}$/) {
4905 # "Multiple spaces after return type\n" . $herecurr);
4908 # unnecessary space "type ( *funcptr)(args...)"
4909 if (defined $pre_pointer_space &&
4910 $pre_pointer_space =~ /^\s/) {
4912 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4915 # unnecessary space "type (* funcptr)(args...)"
4916 if (defined $post_pointer_space &&
4917 $post_pointer_space =~ /^\s/) {
4919 "Unnecessary space before function pointer name\n" . $herecurr);
4922 # unnecessary space "type (*funcptr )(args...)"
4923 if (defined $post_funcname_space &&
4924 $post_funcname_space =~ /^\s/) {
4926 "Unnecessary space after function pointer name\n" . $herecurr);
4929 # unnecessary space "type (*funcptr) (args...)"
4930 if (defined $pre_args_space &&
4931 $pre_args_space =~ /^\s/) {
4933 "Unnecessary space before function pointer arguments\n" . $herecurr);
4936 if (show_type
("SPACING") && $fix) {
4937 $fixed[$fixlinenr] =~
4938 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
4942 # check for spacing round square brackets; allowed:
4943 # 1. with a type on the left -- int [] a;
4944 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4945 # 3. inside a curly brace -- = { [0...10] = 5 }
4946 while ($line =~ /(.*?\s)\[/g) {
4947 my ($where, $prefix) = ($-[1], $1);
4948 if ($prefix !~ /$Type\s+$/ &&
4949 ($where != 0 || $prefix !~ /^.\s+$/) &&
4950 $prefix !~ /[{,:]\s+$/) {
4951 if (ERROR
("BRACKET_SPACE",
4952 "space prohibited before open square bracket '['\n" . $herecurr) &&
4954 $fixed[$fixlinenr] =~
4955 s/^(\+.*?)\s+\[/$1\[/;
4960 # check for spaces between functions and their parentheses.
4961 while ($line =~ /($Ident)\s+\(/g) {
4963 my $ctx_before = substr($line, 0, $-[1]);
4964 my $ctx = "$ctx_before$name";
4966 # Ignore those directives where spaces _are_ permitted.
4968 if|for|while|switch
|return|case
|
4969 volatile
|__volatile__
|
4970 __attribute__
|format
|__extension__
|
4973 # cpp #define statements have non-optional spaces, ie
4974 # if there is a space between the name and the open
4975 # parenthesis it is simply not a parameter group.
4976 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4978 # cpp #elif statement condition may start with a (
4979 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4981 # If this whole things ends with a type its most
4982 # likely a typedef for a function.
4983 } elsif ($ctx =~ /$Type$/) {
4987 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4989 $fixed[$fixlinenr] =~
4990 s/\b$name\s+\(/$name\(/;
4995 # Check operator spacing.
4996 if (!($line=~/\#\s*include/)) {
4997 my $fixed_line = "";
5001 <<=|>>=|<=|>=|==|!=|
5002 \
+=|-=|\
*=|\
/=|%=|\
^=|\
|=|&=|
5003 =>|->|<<|>>|<|>|=|!|~|
5004 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%|
5007 my @elements = split(/($ops|;)/, $opline);
5009 ## print("element count: <" . $#elements . ">\n");
5010 ## foreach my $el (@elements) {
5011 ## print("el: <$el>\n");
5014 my @fix_elements = ();
5017 foreach my $el (@elements) {
5018 push(@fix_elements, substr($rawline, $off, length($el)));
5019 $off += length($el);
5024 my $blank = copy_spacing
($opline);
5025 my $last_after = -1;
5027 for (my $n = 0; $n < $#elements; $n += 2) {
5029 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
5031 ## print("n: <$n> good: <$good>\n");
5033 $off += length($elements[$n]);
5035 # Pick up the preceding and succeeding characters.
5036 my $ca = substr($opline, 0, $off);
5038 if (length($opline) >= ($off + length($elements[$n + 1]))) {
5039 $cc = substr($opline, $off + length($elements[$n + 1]));
5041 my $cb = "$ca$;$cc";
5044 $a = 'V' if ($elements[$n] ne '');
5045 $a = 'W' if ($elements[$n] =~ /\s$/);
5046 $a = 'C' if ($elements[$n] =~ /$;$/);
5047 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
5048 $a = 'O' if ($elements[$n] eq '');
5049 $a = 'E' if ($ca =~ /^\s*$/);
5051 my $op = $elements[$n + 1];
5054 if (defined $elements[$n + 2]) {
5055 $c = 'V' if ($elements[$n + 2] ne '');
5056 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
5057 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
5058 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
5059 $c = 'O' if ($elements[$n + 2] eq '');
5060 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
5065 my $ctx = "${a}x${c}";
5067 my $at = "(ctx:$ctx)";
5069 my $ptr = substr($blank, 0, $off) . "^";
5070 my $hereptr = "$hereline$ptr\n";
5072 # Pull out the value of this operator.
5073 my $op_type = substr($curr_values, $off + 1, 1);
5075 # Get the full operator variant.
5076 my $opv = $op . substr($curr_vars, $off, 1);
5078 # Ignore operators passed as parameters.
5079 if ($op_type ne 'V' &&
5080 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
5083 # } elsif ($op =~ /^$;+$/) {
5085 # ; should have either the end of line or a space or \ after it
5086 } elsif ($op eq ';') {
5087 if ($ctx !~ /.x[WEBC]/ &&
5088 $cc !~ /^\\/ && $cc !~ /^;/) {
5089 if (ERROR
("SPACING",
5090 "space required after that '$op' $at\n" . $hereptr)) {
5091 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]) . " ";
5097 } elsif ($op eq '//') {
5099 # : when part of a bitfield
5100 } elsif ($opv eq ':B') {
5101 # skip the bitfield test for now
5105 } elsif ($op eq '->') {
5106 if ($ctx =~ /Wx.|.xW/) {
5107 if (ERROR
("SPACING",
5108 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
5109 $good = rtrim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
5110 if (defined $fix_elements[$n + 2]) {
5111 $fix_elements[$n + 2] =~ s/^\s+//;
5117 # , must not have a space before and must have a space on the right.
5118 } elsif ($op eq ',') {
5119 my $rtrim_before = 0;
5120 my $space_after = 0;
5121 if ($ctx =~ /Wx./) {
5122 if (ERROR
("SPACING",
5123 "space prohibited before that '$op' $at\n" . $hereptr)) {
5128 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
5129 if (ERROR
("SPACING",
5130 "space required after that '$op' $at\n" . $hereptr)) {
5136 if ($rtrim_before || $space_after) {
5137 if ($rtrim_before) {
5138 $good = rtrim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
5140 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]);
5147 # '*' as part of a type definition -- reported already.
5148 } elsif ($opv eq '*_') {
5149 #warn "'*' is part of type\n";
5151 # unary operators should have a space before and
5152 # none after. May be left adjacent to another
5153 # unary operator, or a cast
5154 } elsif ($op eq '!' || $op eq '~' ||
5155 $opv eq '*U' || $opv eq '-U' ||
5156 $opv eq '&U' || $opv eq '&&U') {
5157 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
5158 if (ERROR
("SPACING",
5159 "space required before that '$op' $at\n" . $hereptr)) {
5160 if ($n != $last_after + 2) {
5161 $good = $fix_elements[$n] . " " . ltrim
($fix_elements[$n + 1]);
5166 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
5167 # A unary '*' may be const
5169 } elsif ($ctx =~ /.xW/) {
5170 if (ERROR
("SPACING",
5171 "space prohibited after that '$op' $at\n" . $hereptr)) {
5172 $good = $fix_elements[$n] . rtrim
($fix_elements[$n + 1]);
5173 if (defined $fix_elements[$n + 2]) {
5174 $fix_elements[$n + 2] =~ s/^\s+//;
5180 # unary ++ and unary -- are allowed no space on one side.
5181 } elsif ($op eq '++' or $op eq '--') {
5182 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
5183 if (ERROR
("SPACING",
5184 "space required one side of that '$op' $at\n" . $hereptr)) {
5185 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]) . " ";
5189 if ($ctx =~ /Wx[BE]/ ||
5190 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
5191 if (ERROR
("SPACING",
5192 "space prohibited before that '$op' $at\n" . $hereptr)) {
5193 $good = rtrim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
5197 if ($ctx =~ /ExW/) {
5198 if (ERROR
("SPACING",
5199 "space prohibited after that '$op' $at\n" . $hereptr)) {
5200 $good = $fix_elements[$n] . trim
($fix_elements[$n + 1]);
5201 if (defined $fix_elements[$n + 2]) {
5202 $fix_elements[$n + 2] =~ s/^\s+//;
5208 # << and >> may either have or not have spaces both sides
5209 } elsif ($op eq '<<' or $op eq '>>' or
5210 $op eq '&' or $op eq '^' or $op eq '|' or
5211 $op eq '+' or $op eq '-' or
5212 $op eq '*' or $op eq '/' or
5216 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
5218 "spaces preferred around that '$op' $at\n" . $hereptr)) {
5219 $good = rtrim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]) . " ";
5220 $fix_elements[$n + 2] =~ s/^\s+//;
5223 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
5225 "space preferred before that '$op' $at\n" . $hereptr)) {
5226 $good = rtrim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]);
5230 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
5231 if (ERROR
("SPACING",
5232 "need consistent spacing around '$op' $at\n" . $hereptr)) {
5233 $good = rtrim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]) . " ";
5234 if (defined $fix_elements[$n + 2]) {
5235 $fix_elements[$n + 2] =~ s/^\s+//;
5241 # A colon needs no spaces before when it is
5242 # terminating a case value or a label.
5243 } elsif ($opv eq ':C' || $opv eq ':L') {
5244 if ($ctx =~ /Wx./ and $realfile !~ m@
.*\
.lds\
.h
$@
) {
5245 if (ERROR
("SPACING",
5246 "space prohibited before that '$op' $at\n" . $hereptr)) {
5247 $good = rtrim
($fix_elements[$n]) . trim
($fix_elements[$n + 1]);
5252 # All the others need spaces both sides.
5253 } elsif ($ctx !~ /[EWC]x[CWE]/) {
5256 # Ignore email addresses <foo@bar>
5258 $cc =~ /^\S+\@\S+>/) ||
5260 $ca =~ /<\S+\@\S+$/))
5265 # for asm volatile statements
5266 # ignore a colon with another
5267 # colon immediately before or after
5269 ($ca =~ /:$/ || $cc =~ /^:/)) {
5273 # messages are ERROR, but ?: are CHK
5275 my $msg_level = \
&ERROR
;
5276 $msg_level = \
&CHK
if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
5278 if (&{$msg_level}("SPACING",
5279 "spaces required around that '$op' $at\n" . $hereptr)) {
5280 $good = rtrim
($fix_elements[$n]) . " " . trim
($fix_elements[$n + 1]) . " ";
5281 if (defined $fix_elements[$n + 2]) {
5282 $fix_elements[$n + 2] =~ s/^\s+//;
5288 $off += length($elements[$n + 1]);
5290 ## print("n: <$n> GOOD: <$good>\n");
5292 $fixed_line = $fixed_line . $good;
5295 if (($#elements % 2) == 0) {
5296 $fixed_line = $fixed_line . $fix_elements[$#elements];
5299 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
5300 $fixed[$fixlinenr] = $fixed_line;
5306 # check for whitespace before a non-naked semicolon
5307 if ($line =~ /^\+.*\S\s+;\s*$/) {
5309 "space prohibited before semicolon\n" . $herecurr) &&
5311 1 while $fixed[$fixlinenr] =~
5312 s/^(\+.*\S)\s+;/$1;/;
5316 # check for multiple assignments
5317 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
5318 CHK
("MULTIPLE_ASSIGNMENTS",
5319 "multiple assignments should be avoided\n" . $herecurr);
5322 ## # check for multiple declarations, allowing for a function declaration
5324 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
5325 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
5327 ## # Remove any bracketed sections to ensure we do not
5328 ## # falsely report the parameters of functions.
5330 ## while ($ln =~ s/\([^\(\)]*\)//g) {
5332 ## if ($ln =~ /,/) {
5333 ## WARN("MULTIPLE_DECLARATION",
5334 ## "declaring multiple variables together should be avoided\n" . $herecurr);
5338 #need space before brace following if, while, etc
5339 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
5340 $line =~ /\b(?:else|do)\{/) {
5341 if (ERROR
("SPACING",
5342 "space required before the open brace '{'\n" . $herecurr) &&
5344 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
5348 ## # check for blank lines before declarations
5349 ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5350 ## $prevrawline =~ /^.\s*$/) {
5352 ## "No blank lines before declarations\n" . $hereprev);
5356 # closing brace should have a space following it when it has anything
5358 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
5359 if (ERROR
("SPACING",
5360 "space required after that close brace '}'\n" . $herecurr) &&
5362 $fixed[$fixlinenr] =~
5363 s/}((?!(?:,|;|\)))\S)/} $1/;
5367 # check spacing on square brackets
5368 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
5369 if (ERROR
("SPACING",
5370 "space prohibited after that open square bracket '['\n" . $herecurr) &&
5372 $fixed[$fixlinenr] =~
5376 if ($line =~ /\s\]/) {
5377 if (ERROR
("SPACING",
5378 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
5380 $fixed[$fixlinenr] =~
5385 # check spacing on parentheses
5386 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
5387 $line !~ /for\s*\(\s+;/) {
5388 if (ERROR
("SPACING",
5389 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
5391 $fixed[$fixlinenr] =~
5395 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
5396 $line !~ /for\s*\(.*;\s+\)/ &&
5397 $line !~ /:\s+\)/) {
5398 if (ERROR
("SPACING",
5399 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
5401 $fixed[$fixlinenr] =~
5406 # check unnecessary parentheses around addressof/dereference single $Lvals
5407 # ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5409 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
5411 if (CHK
("UNNECESSARY_PARENTHESES",
5412 "Unnecessary parentheses around $var\n" . $herecurr) &&
5414 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5418 # check for unnecessary parentheses around function pointer uses
5419 # ie: (foo->bar)(); should be foo->bar();
5420 # but not "if (foo->bar) (" to avoid some false positives
5421 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5423 if (CHK
("UNNECESSARY_PARENTHESES",
5424 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5426 my $var2 = deparenthesize
($var);
5428 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5432 # check for unnecessary parentheses around comparisons in if uses
5433 # when !drivers/staging or command-line uses --strict
5434 if (($realfile !~ m@
^(?
:drivers
/staging/)@
|| $check_orig) &&
5435 $perl_version_ok && defined($stat) &&
5436 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
5438 my $test = substr($2, 1, -1);
5440 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
5442 # avoid parentheses around potential macro args
5443 next if ($match =~ /^\s*\w+\s*$/);
5444 if (!defined($herectx)) {
5445 $herectx = $here . "\n";
5446 my $cnt = statement_rawlines
($if_stat);
5447 for (my $n = 0; $n < $cnt; $n++) {
5448 my $rl = raw_line
($linenr, $n);
5449 $herectx .= $rl . "\n";
5450 last if $rl =~ /^[ \+].*\{/;
5453 CHK
("UNNECESSARY_PARENTHESES",
5454 "Unnecessary parentheses around '$match'\n" . $herectx);
5458 # check that goto labels aren't indented (allow a single space indentation)
5459 # and ignore bitfield definitions like foo:1
5460 # Strictly, labels can have whitespace after the identifier and before the :
5461 # but this is not allowed here as many ?: uses would appear to be labels
5462 if ($sline =~ /^.\s+[A-Za-z_][A-Za-z\d_]*:(?!\s*\d+)/ &&
5463 $sline !~ /^. [A-Za-z\d_][A-Za-z\d_]*:/ &&
5464 $sline !~ /^.\s+default:/) {
5465 if (WARN
("INDENTED_LABEL",
5466 "labels should not be indented\n" . $herecurr) &&
5468 $fixed[$fixlinenr] =~
5473 # check if a statement with a comma should be two statements like:
5474 # foo = bar(), /* comma should be semicolon */
5476 if (defined($stat) &&
5477 $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
5478 my $cnt = statement_rawlines
($stat);
5479 my $herectx = get_stat_here
($linenr, $cnt, $here);
5480 WARN
("SUSPECT_COMMA_SEMICOLON",
5481 "Possible comma where semicolon could be used\n" . $herectx);
5484 # return is not a function
5485 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
5487 if ($perl_version_ok &&
5488 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
5490 $value = deparenthesize
($value);
5491 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5492 ERROR
("RETURN_PARENTHESES",
5493 "return is not a function, parentheses are not required\n" . $herecurr);
5495 } elsif ($spacing !~ /\s+/) {
5497 "space required before the open parenthesis '('\n" . $herecurr);
5501 # unnecessary return in a void function
5502 # at end-of-function, with the previous line a single leading tab, then return;
5503 # and the line before that not a goto label target like "out:"
5504 if ($sline =~ /^[ \+]}\s*$/ &&
5505 $prevline =~ /^\+\treturn\s*;\s*$/ &&
5507 $lines[$linenr - 3] =~ /^[ +]/ &&
5508 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
5510 "void function return statements are not generally useful\n" . $hereprev);
5513 # if statements using unnecessary parentheses - ie: if ((foo == bar))
5514 if ($perl_version_ok &&
5515 $line =~ /\bif\s*((?:\(\s*){2,})/) {
5516 my $openparens = $1;
5517 my $count = $openparens =~ tr@\
(@\
(@
;
5519 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5520 my $comp = $4; #Not $1 because of $LvalOrFunc
5521 $msg = " - maybe == should be = ?" if ($comp eq "==");
5522 WARN
("UNNECESSARY_PARENTHESES",
5523 "Unnecessary parentheses$msg\n" . $herecurr);
5527 # comparisons with a constant or upper case identifier on the left
5528 # avoid cases like "foo + BAR < baz"
5529 # only fix matches surrounded by parentheses to avoid incorrect
5530 # conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
5531 if ($perl_version_ok &&
5532 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5537 my $newcomp = $comp;
5538 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
5539 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5540 WARN
("CONSTANT_COMPARISON",
5541 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5545 } elsif ($comp eq "<=") {
5547 } elsif ($comp eq ">") {
5549 } elsif ($comp eq ">=") {
5552 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5556 # Return of what appears to be an errno should normally be negative
5557 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
5559 if ($name ne 'EOF' && $name ne 'ERROR' && $name !~ /^EPOLL/) {
5560 WARN
("USE_NEGATIVE_ERRNO",
5561 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
5565 # Need a space before open parenthesis after if, while etc
5566 if ($line =~ /\b(if|while|for|switch)\(/) {
5567 if (ERROR
("SPACING",
5568 "space required before the open parenthesis '('\n" . $herecurr) &&
5570 $fixed[$fixlinenr] =~
5571 s/\b(if|while|for|switch)\(/$1 \(/;
5575 # Check for illegal assignment in if conditional -- and check for trailing
5576 # statements after the conditional.
5577 if ($line =~ /do\s*(?!{)/) {
5578 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5579 ctx_statement_block
($linenr, $realcnt, 0)
5580 if (!defined $stat);
5581 my ($stat_next) = ctx_statement_block
($line_nr_next,
5582 $remain_next, $off_next);
5583 $stat_next =~ s/\n./\n /g;
5584 ##print "stat<$stat> stat_next<$stat_next>\n";
5586 if ($stat_next =~ /^\s*while\b/) {
5587 # If the statement carries leading newlines,
5588 # then count those as offsets.
5590 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5592 statement_rawlines
($whitespace) - 1;
5594 $suppress_whiletrailers{$line_nr_next +
5598 if (!defined $suppress_whiletrailers{$linenr} &&
5599 defined($stat) && defined($cond) &&
5600 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
5601 my ($s, $c) = ($stat, $cond);
5602 my $fixed_assign_in_if = 0;
5604 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
5605 if (ERROR
("ASSIGN_IN_IF",
5606 "do not use assignment in if condition\n" . $herecurr) &&
5607 $fix && $perl_version_ok) {
5608 if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
5616 fix_delete_line
($fixlinenr, $rawline);
5617 fix_insert_line
($fixlinenr, "$space$statement;");
5618 my $newline = "${space}if (";
5619 $newline .= '!' if defined($not);
5620 $newline .= '(' if (defined $not && defined($test) && defined($against));
5621 $newline .= "$assigned";
5622 $newline .= " $test $against" if (defined($test) && defined($against));
5623 $newline .= ')' if (defined $not && defined($test) && defined($against));
5625 $newline .= " {" if (defined($brace));
5626 fix_insert_line
($fixlinenr + 1, $newline);
5627 $fixed_assign_in_if = 1;
5632 # Find out what is on the end of the line after the
5634 substr($s, 0, length($c), '');
5636 $s =~ s/$;//g; # Remove any comments
5637 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5638 $c !~ /}\s*while\s*/)
5640 # Find out how long the conditional actually is.
5641 my @newlines = ($c =~ /\n/gs);
5642 my $cond_lines = 1 + $#newlines;
5645 $stat_real = raw_line
($linenr, $cond_lines)
5646 . "\n" if ($cond_lines);
5647 if (defined($stat_real) && $cond_lines > 1) {
5648 $stat_real = "[...]\n$stat_real";
5651 if (ERROR
("TRAILING_STATEMENTS",
5652 "trailing statements should be on next line\n" . $herecurr . $stat_real) &&
5653 !$fixed_assign_in_if &&
5655 $fix && $perl_version_ok &&
5656 $fixed[$fixlinenr] =~ /^\+(\s*)((?:if|while|for)\s*$balanced_parens)\s*(.*)$/) {
5659 my $rest = rtrim
($4);
5660 if ($rest =~ /;$/) {
5661 $fixed[$fixlinenr] = "\+$indent$test";
5662 fix_insert_line
($fixlinenr + 1, "$indent\t$rest");
5668 # Check for bitwise tests written as boolean
5680 WARN
("HEXADECIMAL_BOOLEAN_TEST",
5681 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
5684 # if and else should not have general statements after it
5685 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5687 $s =~ s/$;//g; # Remove any comments
5688 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5689 ERROR
("TRAILING_STATEMENTS",
5690 "trailing statements should be on next line\n" . $herecurr);
5693 # if should not continue a brace
5694 if ($line =~ /}\s*if\b/) {
5695 ERROR
("TRAILING_STATEMENTS",
5696 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
5699 # case and default should not have general statements after them
5700 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5702 (?
:\s
*$;*)(?
:\s
*{)?
(?
:\s
*$;*)(?
:\s
*\\)?\s
*$|
5706 ERROR
("TRAILING_STATEMENTS",
5707 "trailing statements should be on next line\n" . $herecurr);
5710 # Check for }<nl>else {, these must be at the same
5711 # indent level to be relevant to each other.
5712 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5713 $previndent == $indent) {
5714 if (ERROR
("ELSE_AFTER_BRACE",
5715 "else should follow close brace '}'\n" . $hereprev) &&
5716 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5717 fix_delete_line
($fixlinenr - 1, $prevrawline);
5718 fix_delete_line
($fixlinenr, $rawline);
5719 my $fixedline = $prevrawline;
5720 $fixedline =~ s/}\s*$//;
5721 if ($fixedline !~ /^\+\s*$/) {
5722 fix_insert_line
($fixlinenr, $fixedline);
5724 $fixedline = $rawline;
5725 $fixedline =~ s/^(.\s*)else/$1} else/;
5726 fix_insert_line
($fixlinenr, $fixedline);
5730 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5731 $previndent == $indent) {
5732 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
5734 # Find out what is on the end of the line after the
5736 substr($s, 0, length($c), '');
5739 if ($s =~ /^\s*;/) {
5740 if (ERROR
("WHILE_AFTER_BRACE",
5741 "while should follow close brace '}'\n" . $hereprev) &&
5742 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5743 fix_delete_line
($fixlinenr - 1, $prevrawline);
5744 fix_delete_line
($fixlinenr, $rawline);
5745 my $fixedline = $prevrawline;
5746 my $trailing = $rawline;
5747 $trailing =~ s/^\+//;
5748 $trailing = trim
($trailing);
5749 $fixedline =~ s/}\s*$/} $trailing/;
5750 fix_insert_line
($fixlinenr, $fixedline);
5755 #Specific variable tests
5756 while ($line =~ m{($Constant|$Lval)}g) {
5760 if ($var !~ /^$Constant$/ &&
5761 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
5762 #Ignore some autogenerated defines and enum values
5763 $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
5764 #Ignore Page<foo> variants
5765 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5766 #Ignore SI style variants like nS, mV and dB
5767 #(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5768 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
5769 #Ignore some three character SI units explicitly, like MiB and KHz
5770 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
5771 while ($var =~ m{\b($Ident)}g) {
5773 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5775 seed_camelcase_includes
();
5776 if (!$file && !$camelcase_file_seeded) {
5777 seed_camelcase_file
($realfile);
5778 $camelcase_file_seeded = 1;
5781 if (!defined $camelcase{$word}) {
5782 $camelcase{$word} = 1;
5784 "Avoid CamelCase: <$word>\n" . $herecurr);
5790 #no spaces allowed after \ in define
5791 if ($line =~ /\#\s*define.*\\\s+$/) {
5792 if (WARN
("WHITESPACE_AFTER_LINE_CONTINUATION",
5793 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5795 $fixed[$fixlinenr] =~ s/\s+$//;
5799 # warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5800 # itself <asm/foo.h> (uses RAW line)
5801 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5803 my $checkfile = "include/linux/$file";
5804 if (-f
"$root/$checkfile" &&
5805 $realfile ne $checkfile &&
5806 $1 !~ /$allowed_asm_includes/)
5808 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5809 if ($asminclude > 0) {
5810 if ($realfile =~ m{^arch/}) {
5811 CHK
("ARCH_INCLUDE_LINUX",
5812 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5814 WARN
("INCLUDE_LINUX",
5815 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5821 # multi-statement macros should be enclosed in a do while loop, grab the
5822 # first statement and ensure its the whole macro if its not enclosed
5823 # in a known good container
5824 if ($realfile !~ m@
/vmlinux
.lds
.h
$@
&&
5825 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5828 my ($off, $dstat, $dcond, $rest);
5830 my $has_flow_statement = 0;
5831 my $has_arg_concat = 0;
5832 ($dstat, $dcond, $ln, $cnt, $off) =
5833 ctx_statement_block
($linenr, $realcnt, 0);
5835 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5836 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5838 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
5839 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
5841 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5842 my $define_args = $1;
5843 my $define_stmt = $dstat;
5846 if (defined $define_args && $define_args ne "") {
5847 $define_args = substr($define_args, 1, length($define_args) - 2);
5848 $define_args =~ s/\s*//g;
5849 $define_args =~ s/\\\+?//g;
5850 @def_args = split(",", $define_args);
5854 $dstat =~ s/\\\n.//g;
5855 $dstat =~ s/^\s*//s;
5856 $dstat =~ s/\s*$//s;
5858 # Flatten any parentheses and braces
5859 while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
5860 $dstat =~ s/\{[^\{\}]*\}/1u/ ||
5861 $dstat =~ s/.\[[^\[\]]*\]/1u/)
5865 # Flatten any obvious string concatenation.
5866 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5867 $dstat =~ s/$Ident\s*($String)/$1/)
5871 # Make asm volatile uses seem like a generic function
5872 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5874 my $exceptions = qr{
5887 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5890 my $stmt_cnt = statement_rawlines
($ctx);
5891 my $herectx = get_stat_here
($linenr, $stmt_cnt, $here);
5894 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5895 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
5896 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5897 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
5898 $dstat !~ /$exceptions/ &&
5899 $dstat !~ /^\.$Ident\s*=/ && # .foo =
5900 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
5901 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
5902 $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...}
5903 $dstat !~ /^for\s*$Constant$/ && # for (...)
5904 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5905 $dstat !~ /^do\s*{/ && # do {...
5906 $dstat !~ /^\(\{/ && # ({...
5907 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5909 if ($dstat =~ /^\s*if\b/) {
5910 ERROR
("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5911 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5912 } elsif ($dstat =~ /;/) {
5913 ERROR
("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5914 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5916 ERROR
("COMPLEX_MACRO",
5917 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5922 # Make $define_stmt single line, comment-free, etc
5923 my @stmt_array = split('\n', $define_stmt);
5926 foreach my $l (@stmt_array) {
5931 } elsif ($l =~ /^[\+ ]/) {
5932 $define_stmt .= substr($l, 1);
5935 $define_stmt =~ s/$;//g;
5936 $define_stmt =~ s/\s+/ /g;
5937 $define_stmt = trim
($define_stmt);
5939 # check if any macro arguments are reused (ignore '...' and 'type')
5940 foreach my $arg (@def_args) {
5941 next if ($arg =~ /\.\.\./);
5942 next if ($arg =~ /^type$/i);
5943 my $tmp_stmt = $define_stmt;
5944 $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;
5945 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5946 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
5947 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5949 CHK
("MACRO_ARG_REUSE",
5950 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5952 # check if any macro arguments may have other precedence issues
5953 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
5954 ((defined($1) && $1 ne ',') ||
5955 (defined($2) && $2 ne ','))) {
5956 CHK
("MACRO_ARG_PRECEDENCE",
5957 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
5961 # check for macros with flow control, but without ## concatenation
5962 # ## concatenation is commonly a macro that defines a function so ignore those
5963 if ($has_flow_statement && !$has_arg_concat) {
5964 my $cnt = statement_rawlines
($ctx);
5965 my $herectx = get_stat_here
($linenr, $cnt, $here);
5967 WARN
("MACRO_WITH_FLOW_CONTROL",
5968 "Macros with flow control statements should be avoided\n" . "$herectx");
5971 # check for line continuations outside of #defines, preprocessor #, and asm
5974 if ($prevline !~ /^..*\\$/ &&
5975 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5976 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
5977 $line =~ /^\+.*\\$/) {
5978 WARN
("LINE_CONTINUATIONS",
5979 "Avoid unnecessary line continuations\n" . $herecurr);
5983 # do {} while (0) macro tests:
5984 # single-statement macros do not need to be enclosed in do while (0) loop,
5985 # macro should not end with a semicolon
5986 if ($perl_version_ok &&
5987 $realfile !~ m@
/vmlinux
.lds
.h
$@
&&
5988 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5991 my ($off, $dstat, $dcond, $rest);
5993 ($dstat, $dcond, $ln, $cnt, $off) =
5994 ctx_statement_block
($linenr, $realcnt, 0);
5997 $dstat =~ s/\\\n.//g;
6000 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
6005 my $cnt = statement_rawlines
($ctx);
6006 my $herectx = get_stat_here
($linenr, $cnt, $here);
6008 if (($stmts =~ tr/;/;/) == 1 &&
6009 $stmts !~ /^\s*(if|while|for|switch)\b/) {
6010 WARN
("SINGLE_STATEMENT_DO_WHILE_MACRO",
6011 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
6013 if (defined $semis && $semis ne "") {
6014 WARN
("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
6015 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
6017 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
6019 my $cnt = statement_rawlines
($ctx);
6020 my $herectx = get_stat_here
($linenr, $cnt, $here);
6022 WARN
("TRAILING_SEMICOLON",
6023 "macros should not use a trailing semicolon\n" . "$herectx");
6027 # check for redundant bracing round if etc
6028 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
6029 my ($level, $endln, @chunks) =
6030 ctx_statement_full
($linenr, $realcnt, 1);
6031 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
6032 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
6033 if ($#chunks > 0 && $level == 0) {
6037 my $herectx = $here . "\n";
6038 my $ln = $linenr - 1;
6039 for my $chunk (@chunks) {
6040 my ($cond, $block) = @
{$chunk};
6042 # If the condition carries leading newlines, then count those as offsets.
6043 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
6044 my $offset = statement_rawlines
($whitespace) - 1;
6046 $allowed[$allow] = 0;
6047 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
6049 # We have looked at and allowed this specific line.
6050 $suppress_ifbraces{$ln + $offset} = 1;
6052 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
6053 $ln += statement_rawlines
($block) - 1;
6055 substr($block, 0, length($cond), '');
6057 $seen++ if ($block =~ /^\s*{/);
6059 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
6060 if (statement_lines
($cond) > 1) {
6061 #print "APW: ALLOWED: cond<$cond>\n";
6062 $allowed[$allow] = 1;
6064 if ($block =~/\b(?:if|for|while)\b/) {
6065 #print "APW: ALLOWED: block<$block>\n";
6066 $allowed[$allow] = 1;
6068 if (statement_block_size
($block) > 1) {
6069 #print "APW: ALLOWED: lines block<$block>\n";
6070 $allowed[$allow] = 1;
6075 my $sum_allowed = 0;
6076 foreach (@allowed) {
6079 if ($sum_allowed == 0) {
6081 "braces {} are not necessary for any arm of this statement\n" . $herectx);
6082 } elsif ($sum_allowed != $allow &&
6085 "braces {} should be used on all arms of this statement\n" . $herectx);
6090 if (!defined $suppress_ifbraces{$linenr - 1} &&
6091 $line =~ /\b(if|while|for|else)\b/) {
6094 # Check the pre-context.
6095 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
6096 #print "APW: ALLOWED: pre<$1>\n";
6100 my ($level, $endln, @chunks) =
6101 ctx_statement_full
($linenr, $realcnt, $-[0]);
6103 # Check the condition.
6104 my ($cond, $block) = @
{$chunks[0]};
6105 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
6106 if (defined $cond) {
6107 substr($block, 0, length($cond), '');
6109 if (statement_lines
($cond) > 1) {
6110 #print "APW: ALLOWED: cond<$cond>\n";
6113 if ($block =~/\b(?:if|for|while)\b/) {
6114 #print "APW: ALLOWED: block<$block>\n";
6117 if (statement_block_size
($block) > 1) {
6118 #print "APW: ALLOWED: lines block<$block>\n";
6121 # Check the post-context.
6122 if (defined $chunks[1]) {
6123 my ($cond, $block) = @
{$chunks[1]};
6124 if (defined $cond) {
6125 substr($block, 0, length($cond), '');
6127 if ($block =~ /^\s*\{/) {
6128 #print "APW: ALLOWED: chunk-1 block<$block>\n";
6132 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
6133 my $cnt = statement_rawlines
($block);
6134 my $herectx = get_stat_here
($linenr, $cnt, $here);
6137 "braces {} are not necessary for single statement blocks\n" . $herectx);
6141 # check for single line unbalanced braces
6142 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
6143 $sline =~ /^.\s*else\s*\{\s*$/) {
6144 CHK
("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
6147 # check for unnecessary blank lines around braces
6148 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
6150 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
6151 $fix && $prevrawline =~ /^\+/) {
6152 fix_delete_line
($fixlinenr - 1, $prevrawline);
6155 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
6157 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
6159 fix_delete_line
($fixlinenr, $rawline);
6163 # no volatiles please
6164 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
6165 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
6167 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
6170 # Check for user-visible strings broken across lines, which breaks the ability
6171 # to grep for the string. Make exceptions when the previous string ends in a
6172 # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
6173 # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
6174 if ($line =~ /^\+\s*$String/ &&
6175 $prevline =~ /"\s*$/ &&
6176 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
6177 if (WARN
("SPLIT_STRING",
6178 "quoted string split across lines\n" . $hereprev) &&
6180 $prevrawline =~ /^\+.*"\s*$/ &&
6181 $last_coalesced_string_linenr != $linenr - 1) {
6182 my $extracted_string = get_quoted_string
($line, $rawline);
6183 my $comma_close = "";
6184 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
6188 fix_delete_line
($fixlinenr - 1, $prevrawline);
6189 fix_delete_line
($fixlinenr, $rawline);
6190 my $fixedline = $prevrawline;
6191 $fixedline =~ s/"\s*$//;
6192 $fixedline .= substr($extracted_string, 1) . trim
($comma_close);
6193 fix_insert_line
($fixlinenr - 1, $fixedline);
6194 $fixedline = $rawline;
6195 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
6196 if ($fixedline !~ /\+\s*$/) {
6197 fix_insert_line
($fixlinenr, $fixedline);
6199 $last_coalesced_string_linenr = $linenr;
6203 # check for missing a space in a string concatenation
6204 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
6205 WARN
('MISSING_SPACE',
6206 "break quoted strings at a space character\n" . $hereprev);
6209 # check for an embedded function name in a string when the function is known
6210 # This does not work very well for -f --file checking as it depends on patch
6211 # context providing the function name or a single line form for in-file
6212 # function declarations
6213 if ($line =~ /^\+.*$String/ &&
6214 defined($context_function) &&
6215 get_quoted_string
($line, $rawline) =~ /\b$context_function\b/ &&
6216 length(get_quoted_string
($line, $rawline)) != (length($context_function) + 2)) {
6217 WARN
("EMBEDDED_FUNCTION_NAME",
6218 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
6221 # check for unnecessary function tracing like uses
6222 # This does not use $logFunctions because there are many instances like
6223 # 'dprintk(FOO, "%s()\n", __func__);' which do not match $logFunctions
6224 if ($rawline =~ /^\+.*\([^"]*"$tracing_logging_tags{0,3}%s(?:\s*\(\s*\)\s*)?$tracing_logging_tags{0,3}(?:\\n)?"\s*,\s*__func__\s*\)\s*;/) {
6225 if (WARN
("TRACING_LOGGING",
6226 "Unnecessary ftrace-like logging - prefer using ftrace\n" . $herecurr) &&
6228 fix_delete_line
($fixlinenr, $rawline);
6232 # check for spaces before a quoted newline
6233 if ($rawline =~ /^.*\".*\s\\n/) {
6234 if (WARN
("QUOTED_WHITESPACE_BEFORE_NEWLINE",
6235 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
6237 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
6242 # concatenated string without spaces between elements
6243 if ($line =~ /$String[A-Z_]/ ||
6244 ($line =~ /([A-Za-z0-9_]+)$String/ && $1 !~ /^[Lu]$/)) {
6245 if (CHK
("CONCATENATED_STRING",
6246 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
6248 while ($line =~ /($String)/g) {
6249 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6250 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
6251 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
6256 # uncoalesced string fragments
6257 if ($line =~ /$String\s*[Lu]?"/) {
6258 if (WARN
("STRING_FRAGMENTS",
6259 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
6261 while ($line =~ /($String)(?=\s*")/g) {
6262 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6263 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
6268 # check for non-standard and hex prefixed decimal printf formats
6269 my $show_L = 1; #don't show the same defect twice
6271 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
6272 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
6273 $string =~ s/%%/__/g;
6275 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
6277 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
6281 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
6283 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
6286 # check for 0x<decimal>
6287 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
6288 ERROR
("PRINTF_0XDECIMAL",
6289 "Prefixing 0x with decimal output is defective\n" . $herecurr);
6293 # check for line continuations in quoted strings with odd counts of "
6294 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
6295 WARN
("LINE_CONTINUATIONS",
6296 "Avoid line continuations in quoted strings\n" . $herecurr);
6300 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
6302 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
6306 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
6308 "Consider removing the #if 1 and its #endif\n" . $herecurr);
6311 # check for needless "if (<foo>) fn(<foo>)" uses
6312 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
6313 my $tested = quotemeta($1);
6314 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
6315 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
6317 if (WARN
('NEEDLESS_IF',
6318 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
6321 my $leading_tabs = "";
6322 my $new_leading_tabs = "";
6323 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
6328 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
6329 $new_leading_tabs = $1;
6330 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
6337 fix_delete_line
($fixlinenr - 1, $prevrawline);
6338 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
6344 # check for unnecessary "Out of Memory" messages
6345 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
6346 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
6347 (defined $1 || defined $3) &&
6350 my $testline = $lines[$linenr - 3];
6352 my ($s, $c) = ctx_statement_block
($linenr - 3, $realcnt, 0);
6353 # print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
6355 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6356 $s !~ /\b__GFP_NOWARN\b/ ) {
6358 "Possible unnecessary 'out of memory' message\n" . $hereprev);
6362 # check for logging functions with KERN_<LEVEL>
6363 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
6364 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6366 if (WARN
("UNNECESSARY_KERN_LEVEL",
6367 "Possible unnecessary $level\n" . $herecurr) &&
6369 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
6373 # check for logging continuations
6374 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
6375 WARN
("LOGGING_CONTINUATION",
6376 "Avoid logging continuation uses where feasible\n" . $herecurr);
6379 # check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
6380 if (defined $stat &&
6381 $line =~ /\b$logFunctions\s*\(/ &&
6382 index($stat, '"') >= 0) {
6383 my $lc = $stat =~ tr@
\n@@
;
6384 $lc = $lc + $linenr;
6385 my $stat_real = get_stat_real
($linenr, $lc);
6386 pos($stat_real) = index($stat_real, '"');
6387 while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
6390 my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@
\n@@
;
6391 if (WARN
("UNNECESSARY_MODIFIER",
6392 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
6393 $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
6396 $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
6401 # check for mask then right shift without a parentheses
6402 if ($perl_version_ok &&
6403 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6404 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6405 WARN
("MASK_THEN_SHIFT",
6406 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6409 # check for pointer comparisons to NULL
6410 if ($perl_version_ok) {
6411 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6414 $equal = "" if ($4 eq "!=");
6415 if (CHK
("COMPARISON_TO_NULL",
6416 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6418 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6423 # check for bad placement of section $InitAttribute (e.g.: __initdata)
6424 if ($line =~ /(\b$InitAttribute\b)/) {
6426 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
6429 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
6430 ERROR
("MISPLACED_INIT",
6431 "$attr should be placed after $var\n" . $herecurr)) ||
6432 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
6433 WARN
("MISPLACED_INIT",
6434 "$attr should be placed after $var\n" . $herecurr))) &&
6436 $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;
6441 # check for $InitAttributeData (ie: __initdata) with const
6442 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6444 $attr =~ /($InitAttributePrefix)(.*)/;
6445 my $attr_prefix = $1;
6447 if (ERROR
("INIT_ATTRIBUTE",
6448 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6450 $fixed[$fixlinenr] =~
6451 s/$InitAttributeData/${attr_prefix}initconst/;
6455 # check for $InitAttributeConst (ie: __initconst) without const
6456 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6458 if (ERROR
("INIT_ATTRIBUTE",
6459 "Use of $attr requires a separate use of const\n" . $herecurr) &&
6461 my $lead = $fixed[$fixlinenr] =~
6462 /(^\+\s*(?:static\s+))/;
6464 $lead = "$lead " if ($lead !~ /^\+$/);
6465 $lead = "${lead}const ";
6466 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
6470 # check for __read_mostly with const non-pointer (should just be const)
6471 if ($line =~ /\b__read_mostly\b/ &&
6472 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6473 if (ERROR
("CONST_READ_MOSTLY",
6474 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6476 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6480 # don't use __constant_<foo> functions outside of include/uapi/
6481 if ($realfile !~ m@
^include
/uapi/@
&&
6482 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6483 my $constant_func = $1;
6484 my $func = $constant_func;
6485 $func =~ s/^__constant_//;
6486 if (WARN
("CONSTANT_CONVERSION",
6487 "$constant_func should be $func\n" . $herecurr) &&
6489 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
6493 # prefer usleep_range over udelay
6494 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
6496 # ignore udelay's < 10, however
6497 if (! ($delay < 10) ) {
6499 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
6501 if ($delay > 2000) {
6503 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
6507 # warn about unexpectedly long msleep's
6508 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
6511 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
6515 # check for comparisons of jiffies
6516 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
6517 WARN
("JIFFIES_COMPARISON",
6518 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
6521 # check for comparisons of get_jiffies_64()
6522 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
6523 WARN
("JIFFIES_COMPARISON",
6524 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
6527 # warn about #ifdefs in C files
6528 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
6529 # print "#ifdef in C files should be avoided\n";
6530 # print "$herecurr";
6534 # warn about spacing in #ifdefs
6535 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
6536 if (ERROR
("SPACING",
6537 "exactly one space required after that #$1\n" . $herecurr) &&
6539 $fixed[$fixlinenr] =~
6540 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
6545 # check for spinlock_t definitions without a comment.
6546 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6547 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
6549 if (!ctx_has_comment
($first_line, $linenr)) {
6550 CHK
("UNCOMMENTED_DEFINITION",
6551 "$1 definition without comment\n" . $herecurr);
6554 # check for memory barriers without a comment.
6561 my $barrier_stems = qr{
6569 my $all_barriers = qr{
6571 smp_
(?
:$barrier_stems)|
6572 virt_
(?
:$barrier_stems)
6575 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
6576 if (!ctx_has_comment
($first_line, $linenr)) {
6577 WARN
("MEMORY_BARRIER",
6578 "memory barrier without comment\n" . $herecurr);
6582 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6584 if ($realfile !~ m@
^include
/asm-generic/@
&&
6585 $realfile !~ m@
/barrier\
.h
$@
&&
6586 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6587 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6588 WARN
("MEMORY_BARRIER",
6589 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6592 # check for waitqueue_active without a comment.
6593 if ($line =~ /\bwaitqueue_active\s*\(/) {
6594 if (!ctx_has_comment
($first_line, $linenr)) {
6595 WARN
("WAITQUEUE_ACTIVE",
6596 "waitqueue_active without comment\n" . $herecurr);
6600 # check for data_race without a comment.
6601 if ($line =~ /\bdata_race\s*\(/) {
6602 if (!ctx_has_comment
($first_line, $linenr)) {
6604 "data_race without comment\n" . $herecurr);
6608 # check of hardware specific defines
6609 if ($line =~ m@
^.\s
*\#\s
*if.*\b(__i386__
|__powerpc64__
|__sun__
|__s390x__
)\b@
&& $realfile !~ m
@include/asm
-@
) {
6611 "architecture specific defines should be avoided\n" . $herecurr);
6614 # check that the storage class is not after a type
6615 if ($line =~ /\b($Type)\s+($Storage)\b/) {
6616 WARN
("STORAGE_CLASS",
6617 "storage class '$2' should be located before type '$1'\n" . $herecurr);
6619 # Check that the storage class is at the beginning of a declaration
6620 if ($line =~ /\b$Storage\b/ &&
6621 $line !~ /^.\s*$Storage/ &&
6622 $line =~ /^.\s*(.+?)\$Storage\s/ &&
6623 $1 !~ /[\,\)]\s*$/) {
6624 WARN
("STORAGE_CLASS",
6625 "storage class should be at the beginning of the declaration\n" . $herecurr);
6628 # check the location of the inline attribute, that it is between
6629 # storage class and type.
6630 if ($line =~ /\b$Type\s+$Inline\b/ ||
6631 $line =~ /\b$Inline\s+$Storage\b/) {
6632 ERROR
("INLINE_LOCATION",
6633 "inline keyword should sit between storage class and type\n" . $herecurr);
6636 # Check for __inline__ and __inline, prefer inline
6637 if ($realfile !~ m@
\binclude
/uapi/@
&&
6638 $line =~ /\b(__inline__|__inline)\b/) {
6640 "plain inline is preferred over $1\n" . $herecurr) &&
6642 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
6647 # Check for compiler attributes
6648 if ($realfile !~ m@
\binclude
/uapi/@
&&
6649 $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
6651 $attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
6654 "alias" => "__alias",
6655 "aligned" => "__aligned",
6656 "always_inline" => "__always_inline",
6657 "assume_aligned" => "__assume_aligned",
6659 "const" => "__attribute_const__",
6661 "designated_init" => "__designated_init",
6662 "externally_visible" => "__visible",
6663 "format" => "printf|scanf",
6664 "gnu_inline" => "__gnu_inline",
6665 "malloc" => "__malloc",
6667 "no_caller_saved_registers" => "__no_caller_saved_registers",
6668 "noclone" => "__noclone",
6669 "noinline" => "noinline",
6670 "nonstring" => "__nonstring",
6671 "noreturn" => "__noreturn",
6672 "packed" => "__packed",
6674 "section" => "__section",
6679 while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
6682 $params = $2 if defined($2);
6683 my $curr_attr = $orig_attr;
6684 $curr_attr =~ s/^[\s_]+|[\s_]+$//g;
6685 if (exists($attr_list{$curr_attr})) {
6686 my $new = $attr_list{$curr_attr};
6687 if ($curr_attr eq "format" && $params) {
6688 $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
6691 $new = "$new$params";
6693 if (WARN
("PREFER_DEFINED_ATTRIBUTE_MACRO",
6694 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
6696 my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6697 $fixed[$fixlinenr] =~ s/$remove//;
6698 $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6699 $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6700 $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
6705 # Check for __attribute__ unused, prefer __always_unused or __maybe_unused
6706 if ($attr =~ /^_*unused/) {
6707 WARN
("PREFER_DEFINED_ATTRIBUTE_MACRO",
6708 "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
6712 # Check for __attribute__ weak, or __weak declarations (may have link issues)
6713 if ($perl_version_ok &&
6714 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6715 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6716 $line =~ /\b__weak\b/)) {
6717 ERROR
("WEAK_DECLARATION",
6718 "Using weak declarations can have unintended link defects\n" . $herecurr);
6721 # check for c99 types like uint8_t used outside of uapi/ and tools/
6722 if ($realfile !~ m@
\binclude
/uapi/@
&&
6723 $realfile !~ m@
\btools
/@
&&
6724 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6726 if ($type =~ /\b($typeC99Typedefs)\b/) {
6728 my $kernel_type = 'u';
6729 $kernel_type = 's' if ($type =~ /^_*[si]/);
6732 if (CHK
("PREFER_KERNEL_TYPES",
6733 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6735 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6740 # check for cast of C90 native int or longer types constants
6741 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6745 my $newconst = $const;
6746 $newconst =~ s/${Int_type}$//;
6747 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6748 if ($cast =~ /\blong\s+long\b/) {
6750 } elsif ($cast =~ /\blong\b/) {
6753 if (WARN
("TYPECAST_INT_CONSTANT",
6754 "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) &&
6756 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6760 # check for sizeof(&)
6761 if ($line =~ /\bsizeof\s*\(\s*\&/) {
6762 WARN
("SIZEOF_ADDRESS",
6763 "sizeof(& should be avoided\n" . $herecurr);
6766 # check for sizeof without parenthesis
6767 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
6768 if (WARN
("SIZEOF_PARENTHESIS",
6769 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6771 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
6775 # check for struct spinlock declarations
6776 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6777 WARN
("USE_SPINLOCK_T",
6778 "struct spinlock should be spinlock_t\n" . $herecurr);
6781 # check for seq_printf uses that could be seq_puts
6782 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6783 my $fmt = get_quoted_string
($line, $rawline);
6786 if (WARN
("PREFER_SEQ_PUTS",
6787 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6789 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6794 # check for vsprintf extension %p<foo> misuses
6795 if ($perl_version_ok &&
6797 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6798 $1 !~ /^_*volatile_*$/) {
6801 my $lc = $stat =~ tr@
\n@@
;
6802 $lc = $lc + $linenr;
6803 for (my $count = $linenr; $count <= $lc; $count++) {
6807 my $bad_specifier = "";
6808 my $fmt = get_quoted_string
($lines[$count - 1], raw_line
($count, 0));
6811 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
6815 if ($extension !~ /[4SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
6816 ($extension eq "f" &&
6817 defined $qualifier && $qualifier !~ /^w/) ||
6818 ($extension eq "4" &&
6819 defined $qualifier && $qualifier !~ /^cc/)) {
6820 $bad_specifier = $specifier;
6823 if ($extension eq "x" && !defined($stat_real)) {
6824 if (!defined($stat_real)) {
6825 $stat_real = get_stat_real
($linenr, $lc);
6827 WARN
("VSPRINTF_SPECIFIER_PX",
6828 "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");
6831 if ($bad_specifier ne "") {
6832 my $stat_real = get_stat_real
($linenr, $lc);
6833 my $ext_type = "Invalid";
6835 if ($bad_specifier =~ /p[Ff]/) {
6836 $use = " - use %pS instead";
6837 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6840 WARN
("VSPRINTF_POINTER_EXTENSION",
6841 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6846 # Check for misused memsets
6847 if ($perl_version_ok &&
6849 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6855 if ($ms_size =~ /^(0x|)0$/i) {
6857 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6858 } elsif ($ms_size =~ /^(0x|)1$/i) {
6860 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6864 # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
6865 # if ($perl_version_ok &&
6867 # $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6868 # if (WARN("PREFER_ETHER_ADDR_COPY",
6869 # "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6871 # $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6875 # Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
6876 # if ($perl_version_ok &&
6878 # $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6879 # WARN("PREFER_ETHER_ADDR_EQUAL",
6880 # "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6883 # check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6884 # check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
6885 # if ($perl_version_ok &&
6887 # $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6891 # if ($ms_val =~ /^(?:0x|)0+$/i) {
6892 # if (WARN("PREFER_ETH_ZERO_ADDR",
6893 # "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6895 # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6897 # } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6898 # if (WARN("PREFER_ETH_BROADCAST_ADDR",
6899 # "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6901 # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6906 # strlcpy uses that should likely be strscpy
6907 if ($line =~ /\bstrlcpy\s*\(/) {
6909 "Prefer strscpy over strlcpy - see: https://lore.kernel.org/r/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw\@mail.gmail.com/\n" . $herecurr);
6912 # typecasts on min/max could be min_t/max_t
6913 if ($perl_version_ok &&
6915 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6916 if (defined $2 || defined $7) {
6918 my $cast1 = deparenthesize
($2);
6920 my $cast2 = deparenthesize
($7);
6924 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6925 $cast = "$cast1 or $cast2";
6926 } elsif ($cast1 ne "") {
6932 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6936 # check usleep_range arguments
6937 if ($perl_version_ok &&
6939 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6943 WARN
("USLEEP_RANGE",
6944 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
6945 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6947 WARN
("USLEEP_RANGE",
6948 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
6952 # check for naked sscanf
6953 if ($perl_version_ok &&
6955 $line =~ /\bsscanf\b/ &&
6956 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6957 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6958 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6959 my $lc = $stat =~ tr@
\n@@
;
6960 $lc = $lc + $linenr;
6961 my $stat_real = get_stat_real
($linenr, $lc);
6962 WARN
("NAKED_SSCANF",
6963 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6966 # check for simple sscanf that should be kstrto<foo>
6967 if ($perl_version_ok &&
6969 $line =~ /\bsscanf\b/) {
6970 my $lc = $stat =~ tr@
\n@@
;
6971 $lc = $lc + $linenr;
6972 my $stat_real = get_stat_real
($linenr, $lc);
6973 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6975 my $count = $format =~ tr@
%@
%@
;
6977 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6978 WARN
("SSCANF_TO_KSTRTO",
6979 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6984 # check for new externs in .h files.
6985 if ($realfile =~ /\.h$/ &&
6986 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6987 if (CHK
("AVOID_EXTERNS",
6988 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
6990 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
6994 # check for new externs in .c files.
6995 if ($realfile =~ /\.c$/ && defined $stat &&
6996 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6998 my $function_name = $1;
6999 my $paren_space = $2;
7002 if (defined $cond) {
7003 substr($s, 0, length($cond), '');
7007 WARN
("AVOID_EXTERNS",
7008 "externs should be avoided in .c files\n" . $herecurr);
7011 if ($paren_space =~ /\n/) {
7012 WARN
("FUNCTION_ARGUMENTS",
7013 "arguments for function declarations should follow identifier\n" . $herecurr);
7016 } elsif ($realfile =~ /\.c$/ && defined $stat &&
7017 $stat =~ /^.\s*extern\s+/)
7019 WARN
("AVOID_EXTERNS",
7020 "externs should be avoided in .c files\n" . $herecurr);
7023 # check for function declarations that have arguments without identifier names
7024 if (defined $stat &&
7025 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
7027 my $args = trim
($1);
7028 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
7030 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
7031 WARN
("FUNCTION_ARGUMENTS",
7032 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
7037 # check for function definitions
7038 if ($perl_version_ok &&
7040 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
7041 $context_function = $1;
7043 # check for multiline function definition with misplaced open brace
7045 my $cnt = statement_rawlines
($stat);
7046 my $herectx = $here . "\n";
7047 for (my $n = 0; $n < $cnt; $n++) {
7048 my $rl = raw_line
($linenr, $n);
7049 $herectx .= $rl . "\n";
7050 $ok = 1 if ($rl =~ /^[ \+]\{/);
7051 $ok = 1 if ($rl =~ /\{/ && $n == 0);
7052 last if $rl =~ /^[ \+].*\{/;
7056 "open brace '{' following function definitions go on the next line\n" . $herectx);
7060 # checks for new __setup's
7061 if ($rawline =~ /\b__setup\("([^"]*)"/) {
7064 if (!grep(/$name/, @setup_docs)) {
7065 CHK
("UNDOCUMENTED_SETUP",
7066 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
7070 # check for pointless casting of alloc functions
7071 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
7072 WARN
("UNNECESSARY_CASTS",
7073 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
7077 # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
7078 if ($perl_version_ok &&
7079 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
7080 CHK
("ALLOC_SIZEOF_STRUCT",
7081 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
7084 # check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kvmalloc_array/kvcalloc/kcalloc
7085 if ($perl_version_ok &&
7087 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
7091 my $newfunc = "kmalloc_array";
7092 $newfunc = "kvmalloc_array" if ($oldfunc eq "kvmalloc");
7093 $newfunc = "kvcalloc" if ($oldfunc eq "kvzalloc");
7094 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
7097 if ($a1 =~ /^sizeof\s*\S/) {
7101 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
7102 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
7103 my $cnt = statement_rawlines
($stat);
7104 my $herectx = get_stat_here
($linenr, $cnt, $here);
7106 if (WARN
("ALLOC_WITH_MULTIPLY",
7107 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
7110 $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;
7115 # check for krealloc arg reuse
7116 if ($perl_version_ok &&
7117 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
7119 WARN
("KREALLOC_ARG_REUSE",
7120 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
7123 # check for alloc argument mismatch
7124 if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
7125 WARN
("ALLOC_ARRAY_ARGS",
7126 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
7129 # check for multiple semicolons
7130 if ($line =~ /;\s*;\s*$/) {
7131 if (WARN
("ONE_SEMICOLON",
7132 "Statements terminations use 1 semicolon\n" . $herecurr) &&
7134 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
7138 # check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
7139 if ($realfile !~ m@
^include
/uapi/@
&&
7140 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
7142 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
7143 if (CHK
("BIT_MACRO",
7144 "Prefer using the BIT$ull macro\n" . $herecurr) &&
7146 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
7150 # check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
7151 if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
7152 WARN
("IS_ENABLED_CONFIG",
7153 "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
7156 # check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
7157 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(${CONFIG_}[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
7159 if (WARN
("PREFER_IS_ENABLED",
7160 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
7162 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
7166 # check for /* fallthrough */ like comment, prefer fallthrough;
7167 my @fallthroughs = (
7170 'lint -fallthrough[ \t]*',
7171 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
7172 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
7173 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7174 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7176 if ($raw_comment ne '') {
7177 foreach my $ft (@fallthroughs) {
7178 if ($raw_comment =~ /$ft/) {
7179 my $msg_level = \
&WARN
;
7180 $msg_level = \
&CHK
if ($file);
7181 &{$msg_level}("PREFER_FALLTHROUGH",
7182 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
7188 # check for switch/default statements without a break;
7189 if ($perl_version_ok &&
7191 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
7192 my $cnt = statement_rawlines
($stat);
7193 my $herectx = get_stat_here
($linenr, $cnt, $here);
7195 WARN
("DEFAULT_NO_BREAK",
7196 "switch default: should use break\n" . $herectx);
7199 # check for gcc specific __FUNCTION__
7200 if ($line =~ /\b__FUNCTION__\b/) {
7201 if (WARN
("USE_FUNC",
7202 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
7204 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
7208 # check for uses of __DATE__, __TIME__, __TIMESTAMP__
7209 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
7211 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
7214 # check for use of yield()
7215 if ($line =~ /\byield\s*\(\s*\)/) {
7217 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
7220 # check for comparisons against true and false
7221 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
7229 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
7231 my $type = lc($otype);
7232 if ($type =~ /^(?:true|false)$/) {
7233 if (("$test" eq "==" && "$type" eq "true") ||
7234 ("$test" eq "!=" && "$type" eq "false")) {
7238 CHK
("BOOL_COMPARISON",
7239 "Using comparison to $otype is error prone\n" . $herecurr);
7241 ## maybe suggesting a correct construct would better
7242 ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
7247 # check for semaphores initialized locked
7248 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
7249 WARN
("CONSIDER_COMPLETION",
7250 "consider using a completion\n" . $herecurr);
7253 # recommend kstrto* over simple_strto* and strict_strto*
7254 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
7255 WARN
("CONSIDER_KSTRTO",
7256 "$1 is obsolete, use k$3 instead\n" . $herecurr);
7259 # check for __initcall(), use device_initcall() explicitly or more appropriate function please
7260 if ($line =~ /^.\s*__initcall\s*\(/) {
7261 WARN
("USE_DEVICE_INITCALL",
7262 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
7265 # check for spin_is_locked(), suggest lockdep instead
7266 if ($line =~ /\bspin_is_locked\(/) {
7268 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
7271 # check for deprecated apis
7272 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
7273 my $deprecated_api = $1;
7274 my $new_api = $deprecated_apis{$deprecated_api};
7275 WARN
("DEPRECATED_API",
7276 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
7279 # check for various structs that are normally const (ops, kgdb, device_tree)
7280 # and avoid what seem like struct definitions 'struct foo {'
7281 if (defined($const_structs) &&
7282 $line !~ /\bconst\b/ &&
7283 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
7284 WARN
("CONST_STRUCT",
7285 "struct $1 should normally be const\n" . $herecurr);
7288 # use of NR_CPUS is usually wrong
7289 # ignore definitions of NR_CPUS and usage to define arrays as likely right
7290 # ignore designated initializers using NR_CPUS
7291 if ($line =~ /\bNR_CPUS\b/ &&
7292 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
7293 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
7294 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
7295 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
7296 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
7297 $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/)
7300 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
7303 # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
7304 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
7305 ERROR
("DEFINE_ARCH_HAS",
7306 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
7309 # likely/unlikely comparisons similar to "(likely(foo) > 0)"
7310 if ($perl_version_ok &&
7311 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
7312 WARN
("LIKELY_MISUSE",
7313 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
7316 # return sysfs_emit(foo, fmt, ...) fmt without newline
7317 if ($line =~ /\breturn\s+sysfs_emit\s*\(\s*$FuncArg\s*,\s*($String)/ &&
7318 substr($rawline, $-[6], $+[6] - $-[6]) !~ /\\n"$/) {
7319 my $offset = $+[6] - 1;
7320 if (WARN
("SYSFS_EMIT",
7321 "return sysfs_emit(...) formats should include a terminating newline\n" . $herecurr) &&
7323 substr($fixed[$fixlinenr], $offset, 0) = '\\n';
7327 # nested likely/unlikely calls
7328 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
7329 WARN
("LIKELY_MISUSE",
7330 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
7333 # whine mightly about in_atomic
7334 if ($line =~ /\bin_atomic\s*\(/) {
7335 if ($realfile =~ m@
^drivers
/@
) {
7337 "do not use in_atomic in drivers\n" . $herecurr);
7338 } elsif ($realfile !~ m@
^kernel
/@
) {
7340 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
7344 # check for lockdep_set_novalidate_class
7345 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
7346 $line =~ /__lockdep_no_validate__\s*\)/ ) {
7347 if ($realfile !~ m@
^kernel
/lockdep@
&&
7348 $realfile !~ m@
^include
/linux/lockdep@
&&
7349 $realfile !~ m@
^drivers
/base/core@
) {
7351 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
7355 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
7356 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
7357 WARN
("EXPORTED_WORLD_WRITABLE",
7358 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
7361 # check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
7362 # and whether or not function naming is typical and if
7363 # DEVICE_ATTR permissions uses are unusual too
7364 if ($perl_version_ok &&
7366 $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*\)/) {
7371 my $octal_perms = perms_to_octal
($perms);
7372 if ($show =~ /^${var}_show$/ &&
7373 $store =~ /^${var}_store$/ &&
7374 $octal_perms eq "0644") {
7375 if (WARN
("DEVICE_ATTR_RW",
7376 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
7378 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
7380 } elsif ($show =~ /^${var}_show$/ &&
7381 $store =~ /^NULL$/ &&
7382 $octal_perms eq "0444") {
7383 if (WARN
("DEVICE_ATTR_RO",
7384 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
7386 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
7388 } elsif ($show =~ /^NULL$/ &&
7389 $store =~ /^${var}_store$/ &&
7390 $octal_perms eq "0200") {
7391 if (WARN
("DEVICE_ATTR_WO",
7392 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
7394 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
7396 } elsif ($octal_perms eq "0644" ||
7397 $octal_perms eq "0444" ||
7398 $octal_perms eq "0200") {
7399 my $newshow = "$show";
7400 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
7401 my $newstore = $store;
7402 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
7404 if ($show ne $newshow) {
7405 $rename .= " '$show' to '$newshow'";
7407 if ($store ne $newstore) {
7408 $rename .= " '$store' to '$newstore'";
7410 WARN
("DEVICE_ATTR_FUNCTIONS",
7411 "Consider renaming function(s)$rename\n" . $herecurr);
7413 WARN
("DEVICE_ATTR_PERMS",
7414 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
7418 # Mode permission misuses where it seems decimal should be octal
7419 # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
7420 # o Ignore module_param*(...) uses with a decimal 0 permission as that has a
7421 # specific definition of not visible in sysfs.
7422 # o Ignore proc_create*(...) uses with a decimal 0 permission as that means
7423 # use the default permissions
7424 if ($perl_version_ok &&
7426 $line =~ /$mode_perms_search/) {
7427 foreach my $entry (@mode_permission_funcs) {
7428 my $func = $entry->[0];
7429 my $arg_pos = $entry->[1];
7431 my $lc = $stat =~ tr@
\n@@
;
7432 $lc = $lc + $linenr;
7433 my $stat_real = get_stat_real
($linenr, $lc);
7438 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
7440 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
7441 if ($stat =~ /$test/) {
7443 $val = $6 if ($skip_args ne "");
7444 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
7445 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
7446 ($val =~ /^$Octal$/ && length($val) ne 4))) {
7447 ERROR
("NON_OCTAL_PERMISSIONS",
7448 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
7450 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
7451 ERROR
("EXPORTED_WORLD_WRITABLE",
7452 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
7458 # check for uses of S_<PERMS> that could be octal for readability
7459 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
7461 my $octal = perms_to_octal
($oval);
7462 if (WARN
("SYMBOLIC_PERMS",
7463 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7465 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
7469 # validate content of MODULE_LICENSE against list from include/linux/module.h
7470 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
7471 my $extracted_string = get_quoted_string
($line, $rawline);
7472 my $valid_licenses = qr{
7475 GPL\
and\ additional\ rights
|
7481 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
7482 WARN
("MODULE_LICENSE",
7483 "unknown module license " . $extracted_string . "\n" . $herecurr);
7485 if (!$file && $extracted_string eq '"GPL v2"') {
7486 if (WARN
("MODULE_LICENSE",
7487 "Prefer \"GPL\" over \"GPL v2\" - see commit bf7fbeeae6db (\"module: Cure the MODULE_LICENSE \"GPL\" vs. \"GPL v2\" bogosity\")\n" . $herecurr) &&
7489 $fixed[$fixlinenr] =~ s/\bMODULE_LICENSE\s*\(\s*"GPL v2"\s*\)/MODULE_LICENSE("GPL")/;
7494 # check for sysctl duplicate constants
7495 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
7496 WARN
("DUPLICATED_SYSCTL_CONST",
7497 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
7501 # If we have no input at all, then there is nothing to report on
7502 # so just keep quiet.
7503 if ($#rawlines == -1) {
7507 # In mailback mode only produce a report in the negative, for
7508 # things that appear to be patches.
7509 if ($mailback && ($clean == 1 || !$is_patch)) {
7513 # This is not a patch, and we are in 'no-patch' mode so
7515 if (!$chk_patch && !$is_patch) {
7519 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
7520 ERROR
("NOT_UNIFIED_DIFF",
7521 "Does not appear to be a unified-diff format patch\n");
7523 if ($is_patch && $has_commit_log && $chk_signoff) {
7524 if ($signoff == 0) {
7525 ERROR
("MISSING_SIGN_OFF",
7526 "Missing Signed-off-by: line(s)\n");
7527 } elsif ($authorsignoff != 1) {
7528 # authorsignoff values:
7529 # 0 -> missing sign off
7530 # 1 -> sign off identical
7531 # 2 -> names and addresses match, comments mismatch
7532 # 3 -> addresses match, names different
7533 # 4 -> names match, addresses different
7534 # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
7536 my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
7538 if ($authorsignoff == 0) {
7539 ERROR
("NO_AUTHOR_SIGN_OFF",
7540 "Missing Signed-off-by: line by nominal patch author '$author'\n");
7541 } elsif ($authorsignoff == 2) {
7542 CHK
("FROM_SIGN_OFF_MISMATCH",
7543 "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
7544 } elsif ($authorsignoff == 3) {
7545 WARN
("FROM_SIGN_OFF_MISMATCH",
7546 "From:/Signed-off-by: email name mismatch: $sob_msg\n");
7547 } elsif ($authorsignoff == 4) {
7548 WARN
("FROM_SIGN_OFF_MISMATCH",
7549 "From:/Signed-off-by: email address mismatch: $sob_msg\n");
7550 } elsif ($authorsignoff == 5) {
7551 WARN
("FROM_SIGN_OFF_MISMATCH",
7552 "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
7557 print report_dump
();
7558 if ($summary && !($clean == 1 && $quiet == 1)) {
7559 print "$filename " if ($summary_file);
7560 print "total: $cnt_error errors, $cnt_warn warnings, " .
7561 (($check)?
"$cnt_chk checks, " : "") .
7562 "$cnt_lines lines checked\n";
7566 # If there were any defects found and not already fixing them
7567 if (!$clean and !$fix) {
7570 NOTE
: For some of the reported defects
, checkpatch may be able to
7571 mechanically convert to the typical style using
--fix
or --fix
-inplace
.
7574 # If there were whitespace errors which cleanpatch can fix
7575 # then suggest that.
7576 if ($rpt_cleaners) {
7580 NOTE
: Whitespace errors detected
.
7581 You may wish to
use scripts
/cleanpatch or scripts/cleanfile
7586 if ($clean == 0 && $fix &&
7587 ("@rawlines" ne "@fixed" ||
7588 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
7589 my $newfile = $filename;
7590 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
7594 @fixed = fix_inserted_deleted_lines
(\
@fixed, \
@fixed_inserted, \
@fixed_deleted);
7596 open($f, '>', $newfile)
7597 or die "$P: Can't open $newfile for write\n";
7598 foreach my $fixed_line (@fixed) {
7601 if ($linecount > 3) {
7602 $fixed_line =~ s/^\+//;
7603 print $f $fixed_line . "\n";
7606 print $f $fixed_line . "\n";
7614 Wrote EXPERIMENTAL
--fix correction
(s
) to
'$newfile'
7616 Do _NOT_ trust the results written to this file
.
7617 Do _NOT_ submit these changes without inspecting them
for correctness
.
7619 This EXPERIMENTAL file is simply a convenience to help rewrite patches
.
7620 No warranties
, expressed
or implied
...
7628 print "$vname has no obvious style problems and is ready for submission.\n";
7630 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)